From 260b7124b5aa7b547db25734542bf20ddcbadf9a Mon Sep 17 00:00:00 2001 From: suncan <1006949218@qq.com> Date: Sat, 19 Nov 2022 20:11:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dset=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E8=AE=BE=E7=BD=AE=E4=B8=8D=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=8F=98=E9=87=8F=E8=BD=AC=E6=8D=A2=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84core=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/backend/parser/parse_coerce.cpp | 34 +++++++++++++------ .../input/set_system_variables_test.source | 12 +++++++ .../output/set_system_variables_test.source | 22 ++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/common/backend/parser/parse_coerce.cpp b/src/common/backend/parser/parse_coerce.cpp index 4b07f6e86..c5bf52937 100644 --- a/src/common/backend/parser/parse_coerce.cpp +++ b/src/common/backend/parser/parse_coerce.cpp @@ -2807,18 +2807,32 @@ Node *transferConstToAconst(Node *node) const char *constr = value ? "t" : "f"; val = makeString((char*)constr); } break; - case UNKNOWNOID: - { - Const* con = (Const *)node; - Node* expr = coerce_type(NULL,(Node*)con , con->consttype, TEXTOID, -1, COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1); - char* constr = TextDatumGetCString(((Const*)expr)->constvalue); - val = makeString(constr); - } break; + /* + * set configuration only support types above. + * if assigned value are not types above, will attempt to convert the type to text. + * if the type connot be converted to text also, an error is reported. + */ default: { - char* constr = TextDatumGetCString(constval); - val = makeString(constr); - } break; + PG_TRY(); + { + Const* con = (Const *)node; + Node* expr = coerce_type(NULL,(Node*)con , con->consttype, TEXTOID, -1, COERCION_IMPLICIT, COERCE_IMPLICIT_CAST, -1); + if (IsA(expr, Const) && (((Const*)expr)->consttype = TEXTOID)) { + char* constr = TextDatumGetCString(((Const*)expr)->constvalue); + val = makeString(constr); + } else { + ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), + errmsg("set value cannot be assigned to the %s type", format_type_be(consttype)))); + } + } + PG_CATCH(); + { + ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), + errmsg("set value cannot be assigned to the %s type", format_type_be(consttype)))); + } + PG_END_TRY(); + } break; } result = makeAConst(val, -1); diff --git a/src/test/regress/input/set_system_variables_test.source b/src/test/regress/input/set_system_variables_test.source index 410da46fb..f9ed41f55 100644 --- a/src/test/regress/input/set_system_variables_test.source +++ b/src/test/regress/input/set_system_variables_test.source @@ -19,6 +19,12 @@ set session codegen_cost_threshold = 10000 * 1; set codegen_cost_threshold = 10000 * 1; set codegen_cost_threshold = (select 10000); +-- test type not support +set codegen_cost_threshold = '1'::money; +set @@session.codegen_cost_threshold = '1'::money; +set application_name = '00:00:01'::time; +set @@session.application_name = '00:00:01'::time; + create database test_set dbcompatibility 'b'; \c test_set -- dbcompatibility is B Format, enable_set_variable_b_format=off @@ -243,6 +249,12 @@ set global log_destination = stderr,syslog , @v1:= 1, @@codegen_cost_threshold = set global log_destination = stderr,syslog , @v1:= 1, @@codegen_cost_threshold = aa; set global log_destination = 'stderr', @v1:= 1, @@codegen_cost_threshold = 'aa'; +-- test type not support +set codegen_cost_threshold = '1'::money; +set @@session.codegen_cost_threshold = '1'::money; +set application_name = '00:00:01'::time; +set @@session.application_name = '00:00:01'::time; + -- test procedure create table test_pro(a int); diff --git a/src/test/regress/output/set_system_variables_test.source b/src/test/regress/output/set_system_variables_test.source index 550a47e74..96b7845fd 100644 --- a/src/test/regress/output/set_system_variables_test.source +++ b/src/test/regress/output/set_system_variables_test.source @@ -43,6 +43,19 @@ set codegen_cost_threshold = 10000 * 1; ERROR: SET supported expr value only in B_FORMAT and enable_set_variable_b_format is on. set codegen_cost_threshold = (select 10000); ERROR: SET supported expr value only in B_FORMAT and enable_set_variable_b_format is on. +-- test type not support +set codegen_cost_threshold = '1'::money; +ERROR: SET supported expr value only in B_FORMAT and enable_set_variable_b_format is on. +set @@session.codegen_cost_threshold = '1'::money; +ERROR: syntax error at or near "@@" +LINE 1: set @@session.codegen_cost_threshold = '1'::money; + ^ +set application_name = '00:00:01'::time; +ERROR: SET supported expr value only in B_FORMAT and enable_set_variable_b_format is on. +set @@session.application_name = '00:00:01'::time; +ERROR: syntax error at or near "@@" +LINE 1: set @@session.application_name = '00:00:01'::time; + ^ create database test_set dbcompatibility 'b'; \c test_set -- dbcompatibility is B Format, enable_set_variable_b_format=off @@ -522,6 +535,15 @@ LINE 1: set global log_destination = stderr,syslog , @v1:= 1, @@code... set global log_destination = 'stderr', @v1:= 1, @@codegen_cost_threshold = 'aa'; NOTICE: global parameter log_destination has been set ERROR: invalid value for parameter "codegen_cost_threshold": "aa" +-- test type not support +set codegen_cost_threshold = '1'::money; +ERROR: set value cannot be assigned to the money type +set @@session.codegen_cost_threshold = '1'::money; +ERROR: set value cannot be assigned to the money type +set application_name = '00:00:01'::time; +ERROR: set value cannot be assigned to the time without time zone type +set @@session.application_name = '00:00:01'::time; +ERROR: set value cannot be assigned to the time without time zone type -- test procedure create table test_pro(a int); create or replace procedure pro1()