v16-0001-misc-fix-for-v16.no-cfbot

application/octet-stream

Filename: v16-0001-misc-fix-for-v16.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Extended Statistics set/restore/clear functions.
From 81638f93039f9cb173b769173d9447ec03ab0042 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 21 Nov 2025 15:06:00 +0800
Subject: [PATCH v16 1/1] misc fix for v16

mainly regress tests for coverage improvements

discussion: https://postgr.es/m/CADkLM=eRop0t==hg0TU3KuDH6DgnUxQ6vZxV9th=6vW1JG36MQ@mail.gmail.com
---
 src/backend/utils/adt/pg_dependencies.c       | 25 ++++--
 src/test/regress/expected/pg_dependencies.out | 79 ++++++++++++++++++-
 src/test/regress/sql/pg_dependencies.sql      | 17 ++++
 3 files changed, 112 insertions(+), 9 deletions(-)

diff --git a/src/backend/utils/adt/pg_dependencies.c b/src/backend/utils/adt/pg_dependencies.c
index bc8795448b2..1bcc5c7af4d 100644
--- a/src/backend/utils/adt/pg_dependencies.c
+++ b/src/backend/utils/adt/pg_dependencies.c
@@ -193,7 +193,7 @@ dependencies_object_end(void *state)
 		errsave(parse->escontext,
 				errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 				errmsg("malformed pg_dependencies: \"%s\"", parse->str),
-				errdetail("The \"%s\" key must contain an array of at least %d "
+				errdetail("The \"%s\" key must contain an array of at least %d"
 						  " and no than %d elements.",
 						  PG_DEPENDENCIES_KEY_ATTRIBUTES, 1, STATS_MAX_DIMENSIONS - 1));
 		return JSON_SEM_ACTION_FAILED;
@@ -338,7 +338,7 @@ dependencies_object_field_start(void *state, char *fname, bool isnull)
 					errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 					errmsg("malformed pg_dependencies: \"%s\"", parse->str),
 					errdetail("Multiple \"%s\" keys are not allowed.",
-							PG_DEPENDENCIES_KEY_ATTRIBUTES));
+							  PG_DEPENDENCIES_KEY_ATTRIBUTES));
 			return JSON_SEM_ACTION_FAILED;
 		}
 
@@ -355,7 +355,7 @@ dependencies_object_field_start(void *state, char *fname, bool isnull)
 					errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 					errmsg("malformed pg_dependencies: \"%s\"", parse->str),
 					errdetail("Multiple \"%s\" keys are not allowed.",
-							PG_DEPENDENCIES_KEY_DEPENDENCY));
+							  PG_DEPENDENCIES_KEY_DEPENDENCY));
 			return JSON_SEM_ACTION_FAILED;
 		}
 
@@ -372,7 +372,7 @@ dependencies_object_field_start(void *state, char *fname, bool isnull)
 					errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 					errmsg("malformed pg_dependencies: \"%s\"", parse->str),
 					errdetail("Multiple \"%s\" keys are not allowed.",
-							PG_DEPENDENCIES_KEY_DEGREE));
+							  PG_DEPENDENCIES_KEY_DEGREE));
 			return JSON_SEM_ACTION_FAILED;
 		}
 
@@ -510,7 +510,7 @@ dependencies_scalar(void *state, char *token, JsonTokenType tokentype)
 							errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
 							errmsg("malformed pg_dependencies: \"%s\"", parse->str),
 							errdetail("Invalid \"%s\" element: %d cannot follow %d.",
-									PG_DEPENDENCIES_KEY_ATTRIBUTES, attnum, prev));
+									  PG_DEPENDENCIES_KEY_ATTRIBUTES, attnum, prev));
 					return JSON_SEM_ACTION_FAILED;
 				}
 			}
@@ -532,6 +532,21 @@ dependencies_scalar(void *state, char *token, JsonTokenType tokentype)
 				return JSON_SEM_ACTION_FAILED;
 			}
 
+			/*
+			 * The attnum cannot be zero a negative number beyond the number of the
+			 * possible expressions.
+			 */
+			if (parse->dependency == 0 || parse->dependency < (0-STATS_MAX_DIMENSIONS))
+			{
+				errsave(parse->escontext,
+						errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+						errmsg("malformed pg_dependencies: \"%s\"", parse->str),
+						errdetail("Invalid \"%s\" element: %d.",
+								  PG_DEPENDENCIES_KEY_DEPENDENCY, parse->dependency));
+
+				return JSON_SEM_ACTION_FAILED;
+			}
+
 			parse->state = DEPS_EXPECT_KEY;
 			return JSON_SUCCESS;
 			break;
diff --git a/src/test/regress/expected/pg_dependencies.out b/src/test/regress/expected/pg_dependencies.out
index c263c133f08..b9f5ea45085 100644
--- a/src/test/regress/expected/pg_dependencies.out
+++ b/src/test/regress/expected/pg_dependencies.out
@@ -117,11 +117,11 @@ SELECT '[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4, "degree": 1.000}]'
 ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4, "degree": 1.000}]"
 LINE 1: SELECT '[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4...
                ^
-DETAIL:  The "attributes" key must contain an array of at least 1  and no than 7 elements.
+DETAIL:  The "attributes" key must contain an array of at least 1 and no than 7 elements.
 SELECT * FROM pg_input_error_info('[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4, "degree": 1.000}]', 'pg_dependencies');
-                                               message                                                |                                      detail                                       | hint | sql_error_code 
-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------+------+----------------
- malformed pg_dependencies: "[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4, "degree": 1.000}]" | The "attributes" key must contain an array of at least 1  and no than 7 elements. |      | 22P02
+                                               message                                                |                                      detail                                      | hint | sql_error_code 
+------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------+------+----------------
+ malformed pg_dependencies: "[{"attributes" : [1,2,3,4,5,6,7,8], "dependency" : 4, "degree": 1.000}]" | The "attributes" key must contain an array of at least 1 and no than 7 elements. |      | 22P02
 (1 row)
 
 -- Valid keys, invalid values
@@ -246,6 +246,77 @@ SELECT * FROM pg_input_error_info('[{"attributes" : [2,3], "dependency" : 4, "de
  malformed pg_dependencies: "[{"attributes" : [2,3], "dependency" : 4, "degree": NaN}]" | Must be valid JSON. |      | 22P02
 (1 row)
 
+SELECT '[{"attributes": [], "dependency": 2, "degree": 1}]' ::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes": [], "dependency": 2, "degree": 1}]"
+LINE 1: SELECT '[{"attributes": [], "dependency": 2, "degree": 1}]' ...
+               ^
+DETAIL:  The "attributes" key must be an non-empty array.
+SELECT '[{"attributes" : {"a": 1}, "dependency" : 4, "degree": "1.2"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : {"a": 1}, "dependency" : 4, "degree": "1.2"}]"
+LINE 1: SELECT '[{"attributes" : {"a": 1}, "dependency" : 4, "degree...
+               ^
+DETAIL:  Value of "attributes" must be an array of attribute numbers.
+SELECT '[{"dependency" : 4, "degree": "1.2"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"dependency" : 4, "degree": "1.2"}]"
+LINE 1: SELECT '[{"dependency" : 4, "degree": "1.2"}]'::pg_dependenc...
+               ^
+DETAIL:  Item must contain "attributes" key
+SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : 0, "degree": "1.2"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2,3,4,5,6,7], "dependency" : 0, "degree": "1.2"}]"
+LINE 1: SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : 0, ...
+               ^
+DETAIL:  Invalid "dependency" element: 0.
+SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : -9, "degree": "1.2"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2,3,4,5,6,7], "dependency" : -9, "degree": "1.2"}]"
+LINE 1: SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : -9,...
+               ^
+DETAIL:  Invalid "dependency" element: -9.
+SELECT '[{"attributes": [1,2], "dependency": 2, "degree": 1}]' ::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes": [1,2], "dependency": 2, "degree": 1}]"
+LINE 1: SELECT '[{"attributes": [1,2], "dependency": 2, "degree": 1}...
+               ^
+DETAIL:  Item "dependency" value 2 found in the "attributes" list.
+SELECT '[{"attributes" : [1, {}], "dependency" : 1, "degree": "1.2"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1, {}], "dependency" : 1, "degree": "1.2"}]"
+LINE 1: SELECT '[{"attributes" : [1, {}], "dependency" : 1, "degree"...
+               ^
+DETAIL:  Attribute lists can only contain attribute numbers.
+SELECT '[{"attributes" : [1,2], "dependency" : {}, "degree": 1.0}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2], "dependency" : {}, "degree": 1.0}]"
+LINE 1: SELECT '[{"attributes" : [1,2], "dependency" : {}, "degree":...
+               ^
+DETAIL:  Value of "dependency" must be an integer.
+SELECT '[{"attributes" : [1,2], "dependency" : 3, "degree": {}}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2], "dependency" : 3, "degree": {}}]"
+LINE 1: SELECT '[{"attributes" : [1,2], "dependency" : 3, "degree": ...
+               ^
+DETAIL:  Value of "degree" must be an integer.
+SELECT '[{"attributes" : [1,2], "dependency" : 1, "degree": "a"}]'::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes" : [1,2], "dependency" : 1, "degree": "a"}]"
+LINE 1: SELECT '[{"attributes" : [1,2], "dependency" : 1, "degree": ...
+               ^
+DETAIL:  Invalid "degree" value.
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "NaN"}]'::pg_dependencies;
+                    pg_dependencies                    
+-------------------------------------------------------
+ [{"attributes": [2], "dependency": 4, "degree": NaN}]
+(1 row)
+
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "-inf"}]'::pg_dependencies;
+                       pg_dependencies                       
+-------------------------------------------------------------
+ [{"attributes": [2], "dependency": 4, "degree": -Infinity}]
+(1 row)
+
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "inf"}]'::pg_dependencies;
+                      pg_dependencies                       
+------------------------------------------------------------
+ [{"attributes": [2], "dependency": 4, "degree": Infinity}]
+(1 row)
+
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "-inf"}]'::pg_dependencies::text::pg_dependencies;
+ERROR:  malformed pg_dependencies: "[{"attributes": [2], "dependency": 4, "degree": -Infinity}]"
+DETAIL:  Must be valid JSON.
 -- Duplicated keys
 SELECT '[{"attributes" : [2,3], "attributes": [1,2], "dependency" : 4, "degree": 1.000}]'::pg_dependencies;
 ERROR:  malformed pg_dependencies: "[{"attributes" : [2,3], "attributes": [1,2], "dependency" : 4, "degree": 1.000}]"
diff --git a/src/test/regress/sql/pg_dependencies.sql b/src/test/regress/sql/pg_dependencies.sql
index 0dda9f76b1c..ad91df99110 100644
--- a/src/test/regress/sql/pg_dependencies.sql
+++ b/src/test/regress/sql/pg_dependencies.sql
@@ -54,6 +54,23 @@ SELECT * FROM pg_input_error_info('[{"attributes" : 1, "dependency" : 4, "degree
 SELECT * FROM pg_input_error_info('[{"attributes" : "a", "dependency" : 4, "degree": 1.000}]', 'pg_dependencies');
 SELECT * FROM pg_input_error_info('[{"attributes" : [2,3], "dependency" : 4, "degree": NaN}]', 'pg_dependencies');
 
+SELECT '[{"attributes": [], "dependency": 2, "degree": 1}]' ::pg_dependencies;
+SELECT '[{"attributes" : {"a": 1}, "dependency" : 4, "degree": "1.2"}]'::pg_dependencies;
+
+SELECT '[{"dependency" : 4, "degree": "1.2"}]'::pg_dependencies;
+SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : 0, "degree": "1.2"}]'::pg_dependencies;
+SELECT '[{"attributes" : [1,2,3,4,5,6,7], "dependency" : -9, "degree": "1.2"}]'::pg_dependencies;
+SELECT '[{"attributes": [1,2], "dependency": 2, "degree": 1}]' ::pg_dependencies;
+SELECT '[{"attributes" : [1, {}], "dependency" : 1, "degree": "1.2"}]'::pg_dependencies;
+SELECT '[{"attributes" : [1,2], "dependency" : {}, "degree": 1.0}]'::pg_dependencies;
+SELECT '[{"attributes" : [1,2], "dependency" : 3, "degree": {}}]'::pg_dependencies;
+SELECT '[{"attributes" : [1,2], "dependency" : 1, "degree": "a"}]'::pg_dependencies;
+
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "NaN"}]'::pg_dependencies;
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "-inf"}]'::pg_dependencies;
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "inf"}]'::pg_dependencies;
+SELECT '[{"attributes" : [2], "dependency" : 4, "degree": "-inf"}]'::pg_dependencies::text::pg_dependencies;
+
 -- Duplicated keys
 SELECT '[{"attributes" : [2,3], "attributes": [1,2], "dependency" : 4, "degree": 1.000}]'::pg_dependencies;
 SELECT '[{"attributes" : [2,3], "dependency" : 4, "dependency": 4, "degree": 1.000}]'::pg_dependencies;
-- 
2.34.1