From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id C81446EC40; Mon, 16 Aug 2021 10:53:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C81446EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629100410; bh=8SLh3t02fvlBqDFuv2YZBy4gD1mJqYLddlTPjoybnRI=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=eaXpCKm4pGAwE4c3FD9jIUX7PEHNoIeOgEQtcpwOcT3PexbktvcjK2z+h4yONow6o ocqjEH0XWYaCP0feohwYVZRVpkM3teBfl/w+XY7eEmGDUx84J/3/gx5a87/3z6ZDLb TSe6wHBgXLWu6Gp9RDogXgDPqIzmBIcn6qXXwAZ0= Received: from smtp52.i.mail.ru (smtp52.i.mail.ru [94.100.177.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 47BA46EC40 for ; Mon, 16 Aug 2021 10:53:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 47BA46EC40 Received: by smtp52.i.mail.ru with esmtpa (envelope-from ) id 1mFXR1-0005eP-Ps; Mon, 16 Aug 2021 10:53:28 +0300 To: Oleg Babin , vdavydov@tarantool.org, sergos@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: <3a954f7793e6356cefb8cd0151a6434382ec876c.1628810253.git.sergepetrenko@tarantool.org> <9b40f22d-49ad-23fb-35f8-3187612d9086@tarantool.org> Message-ID: Date: Mon, 16 Aug 2021 10:53:27 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9736CF3E71F18CE0C3E1D5927724F4AAA182A05F538085040A5514CB3A4E0E462B2590EB9A58BC5289FA52C3D7CB924874B9B8FEA1F7CC4A9 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7EBC3E7439A970C64EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379448E89E2A57838D8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8176019E238BBDB4322E6C80AC0F2BF43117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B55B19328CBC4F849A471835C12D1D977C4224003CC836476EB9C4185024447017B076A6E789B0E975F5C1EE8F4F765FC94C40504FA40089E3AA81AA40904B5D9CF19DD082D7633A078D18283394535A93AA81AA40904B5D98AA50765F7900637091F58CA40FFB641D81D268191BDAD3D698AB9A7B718F8C4D1B931868CE1C5781A620F70A64A45A98AA50765F79006372E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F616AD31D0D18CD5C6D8C47C27EEC5E9FB5C8C57E37DE458BEDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5057D0F94C05354BA96682808B54696C1D2 X-C1DE0DAB: 0D63561A33F958A5C4F64C29ED0A5BBE1BBCC430491EB899B1609103A5339E7FD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752DA3D96DA0CEF5C48E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D344A2E3AE0A8792E69A5B9299F7B8B2A4B62EE09A514FA2409317F8E4CA6E44EF7B47B0D14638287301D7E09C32AA3244C6F160B0F0DDB49F672A83BD860C31832B018FE5BB746DCD1927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojIrFL/N5KnVHlJah6q1hCdg== X-Mailru-Sender: 583F1D7ACE8F49BD31DE23046B3A8460F461619D1E70B43147C8585074263476CDE10F0E9DBD3FFB6BB2E709EA627F343C7DDD459B58856F0E45BC603594F5A135B915D4279FF0579437F6177E88F7363CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v2 1/2] lua: introduce table.equals method X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 13.08.2021 23:13, Oleg Babin пишет: > Thanks for your fixes. Seems I found one more issue. > > ``` > > mt = {__eq = function(a, b) return a.a == b.a end} > > t1 = setmetatable({a = 3}, mt) > > t2 = setmetatable({a = 3}, mt) > > tarantool> t1 == t2 -- metamethod is triggered > --- > - true > ... > > tarantool> t1 == {a = 3} -- tables have different metatables > --- > - false > ... > > tarantool> {a = 3} == t1 > --- > - false > ... > > tarantool> table.equals(t1, t2) -- OK > --- > - true > ... > > tarantool> table.equals(t1, {a = 3}) -- Seems also ok > --- > - false > ... > > tarantool> table.equals({a = 3}, t1) -- It's strange > --- > - true > ... > > ``` > > > Seems you can use two tables if they have the same metatable. > > Probably here it's described better > https://stackoverflow.com/questions/32637684/equality-operator-on-mixed-types-in-lua > > Thanks for a thorough review! I've force pushed the following fixes: ============================================= diff --git a/src/lua/table.lua b/src/lua/table.lua index 3d5e69e97..edd60d1be 100644 --- a/src/lua/table.lua +++ b/src/lua/table.lua @@ -65,8 +65,11 @@ local function table_equals(a, b)      if type(a) ~= 'table' or type(b) ~= 'table' then          return type(a) == type(b) and a == b      end -    local mt = getmetatable(a) -    if mt and mt.__eq then +    local mta = getmetatable(a) +    local mtb = getmetatable(b) +    -- Let Lua decide what should happen when at least one of the tables has a +    -- metatable. +    if mta and mta.__eq or mtb and mtb.__eq then          return a == b      end      for k, v in pairs(a) do diff --git a/test/app-tap/table.test.lua b/test/app-tap/table.test.lua index ec81593f3..029b923fb 100755 --- a/test/app-tap/table.test.lua +++ b/test/app-tap/table.test.lua @@ -8,7 +8,7 @@ yaml.cfg{      encode_invalid_as_nil  = true,  }  local test = require('tap').test('table') -test:plan(40) +test:plan(43)  do -- check basic table.copy (deepcopy)      local example_table = { @@ -254,6 +254,14 @@ do -- check table.equals      test:ok(table.equals(tbl_a, tbl_c),              "table.equals for shallow copied tables after modification")      test:is(table.equals(tbl_a, tbl_b), false, "table.equals does a deep check") +    local mt = {__eq = function(a, b) return true end} +    local tbl_d = setmetatable({a=15}, mt) +    local tbl_e = setmetatable({b=2, c=3}, mt) +    test:ok(table.equals(tbl_d, tbl_e), "table.equals respects __eq") +    test:is(table.equals(tbl_d, {a=15}), false, +            "table.equals when metatables don't match") +    test:is(table.equals({a=15}, tbl_d), false, +            "table.equals when metatables don't match")  end  os.exit(test:check() == true and 0 or 1) ============================================= > > On 13.08.2021 13:22, Serge Petrenko wrote: >> >> >> 13.08.2021 08:30, Oleg Babin пишет: >>> Thanks for your patch. >>> >>> >>> It seems it works in quite strange way: >>> >>> ``` >>> >>> tarantool> table.equals({a = box.NULL}, {}) >>> --- >>> - true >>> ... >>> >>> tarantool> table.equals({}, {a = box.NULL}) >>> --- >>> - false >>> ... >>> >>> ``` >>> >>> >>> I can change arguments order to get different result. Expected false >>> in both cases. >>> >>> For tap it was considered as buggy behaviour >>> https://github.com/tarantool/tarantool/issues/4125 >>> >>> >> >> ==================================== >> >> Good catch! Thanks! >> >> Check out the diff: >> >> diff --git a/src/lua/table.lua b/src/lua/table.lua >> index 5f35a30f6..3d5e69e97 100644 >> --- a/src/lua/table.lua >> +++ b/src/lua/table.lua >> @@ -63,7 +63,7 @@ end >>  -- @return true when the two tables are equal (false otherwise). >>  local function table_equals(a, b) >>      if type(a) ~= 'table' or type(b) ~= 'table' then >> -        return a == b >> +        return type(a) == type(b) and a == b >>      end >>      local mt = getmetatable(a) >>      if mt and mt.__eq then >> diff --git a/test/app-tap/table.test.lua b/test/app-tap/table.test.lua >> index a3c9aa123..ec81593f3 100755 >> --- a/test/app-tap/table.test.lua >> +++ b/test/app-tap/table.test.lua >> @@ -8,7 +8,7 @@ yaml.cfg{ >>      encode_invalid_as_nil  = true, >>  } >>  local test = require('tap').test('table') >> -test:plan(38) >> +test:plan(40) >> >>  do -- check basic table.copy (deepcopy) >>      local example_table = { >> @@ -227,6 +227,10 @@ do -- check table.equals >>      test:ok(table.equals({}, {}), "table.equals for empty tables") >>      test:is(table.equals({}, {1}), false, "table.equals with one >> empty table") >>      test:is(table.equals({1}, {}), false, "table.equals with one >> empty table") >> +    test:is(table.equals({key = box.NULL}, {key = nil}), false, >> +            "table.equals for box.NULL and nil") >> +    test:is(table.equals({key = nil}, {key = box.NULL}), false, >> +            "table.equals for box.NULL and nil") >>      local tbl_a = { >>          first = { >>              1, >> >> ==================================== >>> On 13.08.2021 02:30, Serge Petrenko via Tarantool-patches wrote: >>>> Introduce table.equals for comparing tables. >>>> The method respects __eq metamethod, if provided. >>>> >>>> Needed-for #5894 >>>> --- >>>> >> -- Serge Petrenko