Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pGhIl-0007qO-8D for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Jan 2023 14:14:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pGhIi-0003Dv-Qd for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Jan 2023 14:14:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pGhIi-0003BH-32 for pgsql-hackers@lists.postgresql.org; Sat, 14 Jan 2023 14:14:28 +0000 Received: from mail-oa1-x30.google.com ([2001:4860:4864:20::30]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pGhIa-0000nY-98 for pgsql-hackers@postgresql.org; Sat, 14 Jan 2023 14:14:26 +0000 Received: by mail-oa1-x30.google.com with SMTP id 586e51a60fabf-15b9c93848dso16861570fac.1 for ; Sat, 14 Jan 2023 06:14:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=singh-im.20210112.gappssmtp.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=xXuNqD7m5r4i2fntkx3ggGPvMXHpWvr8YQywhF4EnjU=; b=nJwwQ7lwJzs/dt7Rj5LnjEhyg8xbZzxcgvkfsJFp2aeL7824k5ypppK9TkaONgAEZY ye6qlVUJh5LKn12Z+A3sC0Q8IIrhF+rklfBMwqlwNWdZfLsMNr+hl5BUcBT1gUIi4J32 3teiAANIe3gxYeSrIAcxhHogkfXIwDPINX9w4LNtp2HGBTxlGGH6TKH1p3knYFmbuKA0 6Q1mbblidQwzsG326X9PMB4dUNC+0uJAM2OY+18rDIIj8gWCL4xUvp7HYoIcEXuo1Pg/ ijnSOlFTNUfS4K/NDcT0gvdT7LeQeP9Lr+KL2QEcJw1FZZSv5o/iLqbiY3qcb5/TBwFo +1mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xXuNqD7m5r4i2fntkx3ggGPvMXHpWvr8YQywhF4EnjU=; b=5iTfv4ZI5PYSnZjeGzpPw3cOSlnczMRz/6bMcTTEMiUJuvCn9ev+RzB5A863BV+gRg +0l8f39CxwIy0189MJCp2f/V7Ia/ohYmUUwwhHN2WzRcDfF1aHws5s7eCrVoadJVIAe2 Fpahze/ssEgKfcEAXWuoKVz73QnT2LK+XP9enHdVw/KHFQodFl7G3ynM7fLzGTi0Ufyk en/SpHqI+6s+I8TI9jBT+XMDMwmGc7ldEQEvCMGf/3CXPMZcm8cdA3WfdyWcQq2pfcoJ 5KU4Sq4AH4ivQ37p7+rAkjmZqVrYo3t4cvHxw1IrEiZGWSIczc05+UvhuVBL79aqGkES bJ2w== X-Gm-Message-State: AFqh2kq/22laYKl9L08yO9tVD/l9y2v/YmdXUV027MMVw87rxeq2zk92 fX/g5vQBFnuwYpNlt3NnnIJapbLCBqcpQxWJEgU= X-Google-Smtp-Source: AMrXdXuaib8Mll9zd0P++fuHZwd8cbWsL4iIZ4SXHYbNProaESbOo1unjdgnJ1vf0A57Q5f+23lC3rG0TiZ5FixLqxc= X-Received: by 2002:a05:6870:b919:b0:15f:1dc:605b with SMTP id gx25-20020a056870b91900b0015f01dc605bmr304710oab.126.1673705659119; Sat, 14 Jan 2023 06:14:19 -0800 (PST) MIME-Version: 1.0 References: <1097504.1673543640@sss.pgh.pa.us> <847c18d4-4c76-1cec-3177-53e62104afd4@postgresfriends.org> In-Reply-To: <847c18d4-4c76-1cec-3177-53e62104afd4@postgresfriends.org> From: Gurjeet Singh Date: Sat, 14 Jan 2023 06:14:02 -0800 Message-ID: Subject: Re: Named Operators To: Vik Fearing Cc: Tom Lane , Isaac Morland , Matthias van de Meent , Postgres Hackers Content-Type: multipart/mixed; boundary="000000000000454f5a05f239f795" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000454f5a05f239f795 Content-Type: text/plain; charset="UTF-8" On Thu, Jan 12, 2023 at 5:55 AM Matthias van de Meent wrote: > On Thu, 12 Jan 2023 at 11:59, Gurjeet Singh wrote: > > ... defining an operator > > gives you many additional features that the planner can use to > > optimize your query differently, which it can't do with functions. See > > the COMMUTATOR, HASHES, etc. clause in the CREATE OPERATOR command. > > I see. Wouldn't it be better then to instead make it possible for the > planner to detect the use of the functions used in operators and treat > them as aliases of the operator? Or am I missing something w.r.t. > differences between operator and function invocation? > > E.g. indexes on `int8pl(my_bigint, 1)` does not match queries for > `my_bigint + 1` (and vice versa), while they should be able to support > that, as OPERATOR(pg_catalog.+(int8, int8)) 's function is int8pl. Such a feature would be immensely useful in its own right. But it's also going to be at least 2 orders of magnitude (or more) effort to implement, and to get accepted in the community. I'm thinking of changes in planner, catalogs, etc. On Thu, Jan 12, 2023 at 7:21 AM Tom Lane wrote: > Matthias van de Meent writes: > > I'm -1 on the chosen syntax; :name: shadows common variable > > substitution patterns including those of psql. > > Yeah, this syntax is DOA because of that. I think almost > anything you might invent is going to have conflict risks. I remember discussing this in a meeting with Joe Conway a few weeks ago, when this was just a proposal in my head and I was just bouncing it off him. And I remember pointing out that colons would be a bad choice because of their use in psql; but for life of me I can't think of a reason (except temporary memory loss) why I failed to consider the psql conflict when implementing the feature. If only some test in `make check` would have pointed out the mistake, I wouldn't have made this obvious mistake. > We could probably make it work by allowing the existing OPERATOR > syntax to take things that look like names as well as operators, > like > > expr3 OPERATOR(contains_all) expr4 > > But that's bulky enough that nobody will care to use it. +1. Although that'd be better for readers than the all-special-char names, this format is bulky enough that you won't be able to convince the query writers to bother using it. But if all other efforts fail, I'll take this format over the cryptic ones any day. > On the whole I don't see this proposal going anywhere. > There's too much investment in the existing operator names, > and too much risk of conflicts if you try to shorten the > syntax. I wouldn't give up on the idea, yet :-) See new proposal below. On Thu, Jan 12, 2023 at 9:14 AM Tom Lane wrote: > Isaac Morland writes: > > What about backticks (`)? > > Since they're already allowed as operator characters, you can't > use them for this purpose without breaking existing use-cases. > > Even if they were completely unused, I'd be pretty hesitant to > adopt them for this purpose because of the potential confusion > for users coming from mysql. Since when have we started caring for the convenience of users of other databases?!! /s > Pretty much the only available syntax space is curly braces, > and I don't really want to give those up for this either. > (One has to assume that the SQL committee has their eyes > on those too.) On Thu, Jan 12, 2023 at 9:45 AM Vik Fearing wrote: > They are used in row pattern recognition. I was very hopeful of using { }, and hoping that we'd beat the SQL committee to it, so that they have to choose something else, if we release this into the wild before them. But it seems that they beat us to it long ago. (tangent: Reading some blog posts, I have to say I loved the Row Pattern Recognition feature!) Considering that there are almost no printable characters left in 1-255 ASCII range for us to choose from, I had to get creative; and I believe I have found a way to make it work. Unless the SQL committee has their eyes on a freestanding backslash \ character for something, I believe we can use it as a prefix for Named Operators. Since the most common use of backslash is for escaping characters, I believe it would feel natural for the users to use it as described below. New scheme for the named operators: \#foo That is, an identifier prefixed with \# would serve as an operator name. psql considers \ to be the start of its commands, but it wasn't hard to convince psql to ignore \# and let it pass through to server. I agree that an identifier _surrounded_ by the same token (e.g. #foo#) or the pairing token (e.g. {foo}) looks better aesthetically, so I am okay with any of the following variations of the scheme, as well: \#foo\# (tested; works) \#foo# (not tested; reduces ident length by 1) We can choose a different character, instead of #. Perhaps \{foo} ! Attached is the v2 patch that supports \#foo style Named Operators. Following is the SQL snippet to see what the usage looks like. create operator \#add_point (function = box_add, leftarg = box, rightarg = point); create table test(a box); insert into test values('((0,0),(1,1))'), ('((0,0),(2,1))'); select a as original, a \#add_point '(1,1)' as modified from test; drop operator \#add_point(box, point); Although we have never done it before, but by using backslash we might be able to define new custom token types as well, if needed. For those interested, I have couple of different branches with named_operators* prefix in my Git fork [1] where I'm trying different combinations. [1]: https://github.com/gurjeet/postgres/branches Best regards, Gurjeet http://Gurje.et --000000000000454f5a05f239f795 Content-Type: application/octet-stream; name="named_operators_v2.patch" Content-Disposition: attachment; filename="named_operators_v2.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lcw0zcen0 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2NhdGFsb2cvcGdfb3BlcmF0b3IuYyBiL3NyYy9iYWNr ZW5kL2NhdGFsb2cvcGdfb3BlcmF0b3IuYwppbmRleCAxMDE3ZjJlZWQxLi5jNWI4NTYyY2I1IDEw MDY0NAotLS0gYS9zcmMvYmFja2VuZC9jYXRhbG9nL3BnX29wZXJhdG9yLmMKKysrIGIvc3JjL2Jh Y2tlbmQvY2F0YWxvZy9wZ19vcGVyYXRvci5jCkBAIC0zMSw2ICszMSw3IEBACiAjaW5jbHVkZSAi Y2F0YWxvZy9wZ190eXBlLmgiCiAjaW5jbHVkZSAibWlzY2FkbWluLmgiCiAjaW5jbHVkZSAicGFy c2VyL3BhcnNlX29wZXIuaCIKKyNpbmNsdWRlICJwYXJzZXIvc2NhbnN1cC5oIgogI2luY2x1ZGUg InV0aWxzL2FjbC5oIgogI2luY2x1ZGUgInV0aWxzL2J1aWx0aW5zLmgiCiAjaW5jbHVkZSAidXRp bHMvbHN5c2NhY2hlLmgiCkBAIC03OSw2ICs4MCwxMCBAQCB2YWxpZE9wZXJhdG9yTmFtZShjb25z dCBjaGFyICpuYW1lKQogCWlmIChsZW4gPT0gMCB8fCBsZW4gPj0gTkFNRURBVEFMRU4pCiAJCXJl dHVybiBmYWxzZTsKIAorCS8qIElzIHRoaXMgYSBOYW1lZCBPcGVyYXRvcj8gKi8KKwlpZiAodmFs aWROYW1lZE9wZXJhdG9yKG5hbWUpKQorCQlyZXR1cm4gdHJ1ZTsKKw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