Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBJKA-00AM9d-PE for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 18:42:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rBJK9-001iIA-EM for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Dec 2023 18:42:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBJK7-001iHw-HR for pgsql-hackers@lists.postgresql.org; Thu, 07 Dec 2023 18:42:13 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rBJJz-00AbWE-IJ for pgsql-hackers@postgresql.org; Thu, 07 Dec 2023 18:42:10 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id 34E473200B30; Thu, 7 Dec 2023 13:41:59 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Thu, 07 Dec 2023 13:41:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701974518; x= 1702060918; bh=TEkk9VsbYIdSLRQgdJXV7BvrY5UMS8Yt7b3ZIOpgvGw=; b=A QqJfU0QBLrBQ+QPSE4WH0/8gwqInOUm07JypHwlL+ShG9zVCH/rBJh0y869zq+x8 ZozQUm0oTuF4txEsIZyj509N2zhEuYCfcxuhWVWKkX6qyilHJdLOvkPKKauaTtfr utTe6kTcgmBdOkNGf1KqJbbgkdPRJeMlTVDezPZdN5MR8fj3S1BpYBv27Jq1KrrR bBxdhxirx8+TdqKto5brfgq7Bdd5gtXWNg7CeD3NWWf0GBg6j1YwxLt+hmX4egS3 DVFZXOAARYGx2IhI8jjS/Uukik+bgdzX5OqXD0cNWQFqvTBUmJHTJU5ZcOL0okSQ Qmcr0XEoIAcE8jwXhE4Yg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudekfedgheehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesmhekreertddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepudelkeekgfdttdettdekgfduvdeiffelteegjeeihfet heekveeftedtuddvhfeunecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 7 Dec 2023 13:41:57 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1701974515; bh=LyY3LTSt/LT5rJE+eIwU/oVZ0cU3Pk80FgtO4O9Lm80=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=LVcLy647zcccEUu4QvLVb7r8dlBODtOIxmKBGheEh5b9SvcR5S+xb+etIdJL35YCG 6oomHld9HoIY5mdaHdrKh4W6Bxsc5X3Z1CpFTF63w13At93hsu8Czb/ogj4YFSKFqc oQsYSCYzYUdVEw15O2sgpcLbzOfxwpK709Qp2A/eY/P+Ur98ejdOvcScoL7Y/teuVJ zumi+2L+hqOjyf0Zt/I8JxRi2cmlxHDd4z5JUaDmgYRBUWhOyJQZyU6tPuNrvFL4N9 Ktm4b2nqZkZNqS/yvos+6Ciyv9g2e01pIfMIj9pxpIvHqlen6FDKb6g02ay7vatQzN HRzeI/a8vPQJA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 54947C5; Thu, 7 Dec 2023 19:41:55 +0100 (CET) Date: Thu, 7 Dec 2023 19:41:55 +0100 From: Alvaro Herrera To: Amit Langote Cc: Erik Rijkers , jian he , Andres Freund , Andrew Dunstan , PostgreSQL-development Subject: Re: remaining sql/json patches Message-ID: <202312071841.u2gueb5dsrbk@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="f7aagnfbdwrvaw4y" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --f7aagnfbdwrvaw4y Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit I noticed that JSON_TABLE uses an explicit FORMAT JSON in one of the rules, instead of using json_format_clause_opt like everywhere else. I wondered why, and noticed that it's because it wants to set coltype JTC_FORMATTED when the clause is present but JTC_REGULAR otherwise. This seemed a little odd, but I thought to split json_format_clause_opt in two productions, one without the empty rule (json_format_clause) and another with it. This is not a groundbreaking improvement, but it seems more natural, and it helps contain the FORMAT stuff a little better. I also noticed while at it that we can do away not only with the json_encoding_clause_opt clause, but also with makeJsonEncoding(). The attach patch does it. This is not derived from the patches you're currently working on; it's more of a revise of the previous SQL/JSON code I committed in 7081ac46ace8. It goes before your 0003 and has a couple of easily resolved conflicts with both 0003 and 0004; then in 0004 you have to edit the JSON_TABLE rule that has FORMAT_LA and replace that with json_format_clause. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "El que vive para el futuro es un iluso, y el que vive para el pasado, un imbécil" (Luis Adler, "Los tripulantes de la noche") --f7aagnfbdwrvaw4y Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-simplify-json_format_clause-by-removing-the-opt-part.patch" From 06e0814e06d04e5ec268e08323eba1b62a656926 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 7 Dec 2023 15:56:15 +0100 Subject: [PATCH] simplify json_format_clause by removing the 'opt' part --- src/backend/nodes/makefuncs.c | 21 --------------- src/backend/parser/gram.y | 51 +++++++++++++++++++++++------------ src/include/nodes/makefuncs.h | 1 - 3 files changed, 34 insertions(+), 39 deletions(-) diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c index c6fb571982..89e77adbc7 100644 --- a/src/backend/nodes/makefuncs.c +++ b/src/backend/nodes/makefuncs.c @@ -857,27 +857,6 @@ makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, return jve; } -/* - * makeJsonEncoding - - * converts JSON encoding name to enum JsonEncoding - */ -JsonEncoding -makeJsonEncoding(char *name) -{ - if (!pg_strcasecmp(name, "utf8")) - return JS_ENC_UTF8; - if (!pg_strcasecmp(name, "utf16")) - return JS_ENC_UTF16; - if (!pg_strcasecmp(name, "utf32")) - return JS_ENC_UTF32; - - ereport(ERROR, - errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("unrecognized JSON encoding: %s", name)); - - return JS_ENC_DEFAULT; -} - /* * makeJsonKeyValue - * creates a JsonKeyValue node diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index d631ac89a9..f16bbd3cdd 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -645,7 +645,8 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type hash_partbound %type hash_partbound_elem -%type json_format_clause_opt +%type json_format_clause + json_format_clause_opt json_value_expr json_returning_clause_opt json_name_and_value @@ -653,8 +654,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type json_name_and_value_list json_value_expr_list json_array_aggregate_order_by_clause_opt -%type json_encoding_clause_opt - json_predicate_type_constraint +%type json_predicate_type_constraint %type json_key_uniqueness_constraint_opt json_object_constructor_null_clause_opt json_array_constructor_null_clause_opt @@ -14962,12 +14962,11 @@ a_expr: c_expr { $$ = $1; } /* * Required by SQL/JSON, but there are conflicts | a_expr - FORMAT_LA JSON json_encoding_clause_opt + json_format_clause IS json_predicate_type_constraint json_key_uniqueness_constraint_opt %prec IS { - $3.location = @2; - $$ = makeJsonIsPredicate($1, $3, $5, $6, @1); + $$ = makeJsonIsPredicate($1, $2, $4, $5, @1); } */ | a_expr IS NOT @@ -14981,13 +14980,12 @@ a_expr: c_expr { $$ = $1; } /* * Required by SQL/JSON, but there are conflicts | a_expr - FORMAT_LA JSON json_encoding_clause_opt + json_format_clause IS NOT json_predicate_type_constraint json_key_uniqueness_constraint_opt %prec IS { - $3.location = @2; - $$ = makeNotExpr(makeJsonIsPredicate($1, $3, $6, $7, @1), @1); + $$ = makeNotExpr(makeJsonIsPredicate($1, $2, $5, $6, @1), @1); } */ | DEFAULT @@ -16503,10 +16501,34 @@ json_value_expr: } ; -json_format_clause_opt: - FORMAT_LA JSON json_encoding_clause_opt +json_format_clause: + FORMAT_LA JSON ENCODING name { - $$ = (Node *) makeJsonFormat(JS_FORMAT_JSON, $3, @1); + int encoding; + + if (!pg_strcasecmp($4, "utf8")) + encoding = JS_ENC_UTF8; + else if (!pg_strcasecmp($4, "utf16")) + encoding = JS_ENC_UTF16; + else if (!pg_strcasecmp($4, "utf32")) + encoding = JS_ENC_UTF32; + else + ereport(ERROR, + errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("unrecognized JSON encoding: %s", $4)); + + $$ = (Node *) makeJsonFormat(JS_FORMAT_JSON, encoding, @1); + } + | FORMAT_LA JSON + { + $$ = (Node *) makeJsonFormat(JS_FORMAT_JSON, JS_ENC_DEFAULT, @1); + } + ; + +json_format_clause_opt: + json_format_clause + { + $$ = $1; } | /* EMPTY */ { @@ -16514,11 +16536,6 @@ json_format_clause_opt: } ; -json_encoding_clause_opt: - ENCODING name { $$ = makeJsonEncoding($2); } - | /* EMPTY */ { $$ = JS_ENC_DEFAULT; } - ; - json_returning_clause_opt: RETURNING Typename json_format_clause_opt { diff --git a/src/include/nodes/makefuncs.h b/src/include/nodes/makefuncs.h index 3180703005..aca0ee54df 100644 --- a/src/include/nodes/makefuncs.h +++ b/src/include/nodes/makefuncs.h @@ -116,6 +116,5 @@ extern Node *makeJsonKeyValue(Node *key, Node *value); extern Node *makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, int location); -extern JsonEncoding makeJsonEncoding(char *name); #endif /* MAKEFUNC_H */ -- 2.39.2 --f7aagnfbdwrvaw4y--