pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
sqrt_bigint_v2
2+ messages / 1 participants
[nested] [flat]

* sqrt_bigint_v2
@ 2021-11-12 06:36  孤傲小二~阿沐 <2903807914@qq.com>
  0 siblings, 0 replies; 2+ messages in thread

From: 孤傲小二~阿沐 @ 2021-11-12 06:36 UTC (permalink / raw)
  To: David&nbsp;G.&nbsp;Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>; tgl <tgl@sss.pgh.pa.us>

This function is already OK, I think tom is correct. After I modified the relevant files, the problem was solved.


Now my newly added functions and the regression test of the database itself are all OK


Thank you both for your continuous support and blessings from China!

Attachments:

sqrt_bigint_v2.patch (application/octet-stream, 9.0K) download diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index eb24195..4cf000c 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -478,7 +478,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type <node> fetch_args select_limit_value offset_clause select_offset_value - select_fetch_first_value I_or_F_const + select_fetch_first_value I_or_F_const I_or_BIGI_const %type <ival> row_or_rows first_or_next %type <list> OptSeqOptList SeqOptList OptParenthesizedSeqOptList @@ -623,9 +623,9 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); * DOT_DOT is unused in the core SQL grammar, and so will always provoke * parse errors. It is needed by PL/pgSQL. */ -%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op +%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op BIGICONST %token <ival> ICONST PARAM -%token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER +%token TYPECAST DOT_DOT COLON_EQUALS SQRT EQUALS_GREATER %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS /* @@ -804,6 +804,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %left '[' ']' %left '(' ')' %left TYPECAST +%left SQRT %left '.' /* * These might seem to be low-precedence, but actually they are not part @@ -4452,12 +4453,19 @@ opt_by: BY NumericOnly: FCONST { $$ = makeFloat($1); } + | BIGICONST { $$ = makeFloat($1); } | '+' FCONST { $$ = makeFloat($2); } + | '+' BIGICONST { $$ = makeFloat($2); } | '-' FCONST { $$ = makeFloat($2); doNegateFloat($$); } + | '-' BIGICONST + { + $$ = makeFloat($2); + doNegateFloat($$); + } | SignedIconst { $$ = makeInteger($1); } ; @@ -5530,6 +5538,7 @@ TriggerFuncArg: $$ = makeString(psprintf("%d", $1)); } | FCONST { $$ = makeString($1); } + | BIGICONST { $$ = makeString($1); } | Sconst { $$ = makeString($1); } | ColLabel { $$ = makeString($1); } ; @@ -11894,6 +11903,11 @@ I_or_F_const: | FCONST { $$ = makeFloatConst($1,@1); } ; +I_or_BIGI_const: + Iconst { $$ = makeIntConst($1,@1); } + | BIGICONST { $$ = makeFloatConst($1,@1); } + ; + /* noise words */ row_or_rows: ROW { $$ = 0; } | ROWS { $$ = 0; } @@ -13274,6 +13288,13 @@ a_expr: c_expr { $$ = $1; } { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); } | '-' a_expr %prec UMINUS { $$ = doNegate($2, @1); } + | SQRT I_or_BIGI_const %prec SQRT + { + $$ = (Node *) makeFuncCall(SystemFuncName("sqrt"), + list_make1($2), + COERCE_SQL_SYNTAX, + @1); + } | a_expr '+' a_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); } | a_expr '-' a_expr @@ -13703,6 +13724,13 @@ b_expr: c_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); } | '-' b_expr %prec UMINUS { $$ = doNegate($2, @1); } + | SQRT I_or_BIGI_const %prec SQRT + { + $$ = (Node *) makeFuncCall(SystemFuncName("sqrt"), + list_make1($2), + COERCE_SQL_SYNTAX, + @2); + } | b_expr '+' b_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); } | b_expr '-' b_expr @@ -15149,6 +15177,10 @@ AexprConst: Iconst { $$ = makeFloatConst($1, @1); } + | BIGICONST + { + $$ = makeFloatConst($1, @1); + } | Sconst { $$ = makeStringConst($1, @1); diff --git a/src/backend/parser/parse_type.c b/src/backend/parser/parse_type.c index abe131e..0baec9d 100644 --- a/src/backend/parser/parse_type.c +++ b/src/backend/parser/parse_type.c @@ -718,7 +718,7 @@ pts_error_callback(void *arg) { const char *str = (const char *) arg; - errcontext("invalid type name \"%s\"", str); + errcontext("invalid type name HERE2 \"%s\"", str); } /* @@ -763,7 +763,7 @@ typeStringToTypeName(const char *str) fail: ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("invalid type name \"%s\"", str))); + errmsg("invalid type name HERE1 \"%s\"", str))); return NULL; /* keep compiler quiet */ } diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index 9f9d8a1..70f1d9f 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -124,6 +124,7 @@ static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner); static char *litbufdup(core_yyscan_t yyscanner); static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner); static int process_integer_literal(const char *token, YYSTYPE *lval); +static int process_integer_bigint_literal(const char *token, YYSTYPE *lval); static void addunicode(pg_wchar c, yyscan_t yyscanner); #define yyerror(msg) scanner_yyerror(msg, yyscanner) @@ -350,6 +351,7 @@ identifier {ident_start}{ident_cont}* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -826,6 +828,11 @@ other . return COLON_EQUALS; } +{sqrt} { + SET_YYLLOC(); + return SQRT; + } + {equals_greater} { SET_YYLLOC(); return EQUALS_GREATER; @@ -974,8 +981,9 @@ other . {integer} { SET_YYLLOC(); - return process_integer_literal(yytext, yylval); + return process_integer_bigint_literal(yytext, yylval); } + {decimal} { SET_YYLLOC(); yylval->str = pstrdup(yytext); @@ -1319,6 +1327,24 @@ process_integer_literal(const char *token, YYSTYPE *lval) return ICONST; } +static int +process_integer_bigint_literal(const char *token, YYSTYPE *lval) +{ + int val; + char *endptr; + + errno = 0; + val = strtoint(token, &endptr, 10); + if (*endptr != '\0' || errno == ERANGE) + { + /* integer too large (or contains decimal pt), treat it as a float */ + lval->str = pstrdup(token); + return BIGICONST; + } + lval->ival = val; + return ICONST; +} + static void addunicode(pg_wchar c, core_yyscan_t yyscanner) { diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index 0fab48a..6c1ab33 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -289,6 +289,7 @@ identifier {ident_start}{ident_cont}* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -606,6 +607,10 @@ other . ECHO; } +{sqrt} { + ECHO; + } + {equals_greater} { ECHO; } diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index 7a03566..a8e2c94 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -58,6 +58,7 @@ static bool include_next; static void addlit(char *ytext, int yleng); static void addlitchar(unsigned char); static int process_integer_literal(const char *token, YYSTYPE *lval); +static int process_integer_bigint_literal(const char *token, YYSTYPE *lval); static void parse_include(void); static bool ecpg_isspace(char ch); static bool isdefine(void); @@ -317,6 +318,7 @@ array ({ident_cont}|{whitespace}|[\[\]\+\-\*\%\/\(\)\>\.])* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -771,6 +773,10 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ return COLON_EQUALS; } +{sqrt} { + return SQRT; + } + {equals_greater} { return EQUALS_GREATER; } @@ -924,7 +930,7 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ <C,SQL>{ {integer} { - return process_integer_literal(yytext, &base_yylval); + return process_integer_bigint_literal(yytext, &base_yylval); } {decimal} { base_yylval.str = mm_strdup(yytext); @@ -1567,6 +1573,24 @@ process_integer_literal(const char *token, YYSTYPE *lval) return ICONST; } +static int +process_integer_bigint_literal(const char *token, YYSTYPE *lval) +{ + int val; + char *endptr; + + errno = 0; + val = strtoint(token, &endptr, 10); + if (*endptr != '\0' || errno == ERANGE) + { + /* integer too large (or contains decimal pt), treat it as a float */ + lval->str = pstrdup(token); + return BIGICONST; + } + lval->ival = val; + return ICONST; +} + static void parse_include(void) { diff --git a/src/pl/plpgsql/src/pl_gram.y b/src/pl/plpgsql/src/pl_gram.y index 0f6a5b3..3229cd2 100644 --- a/src/pl/plpgsql/src/pl_gram.y +++ b/src/pl/plpgsql/src/pl_gram.y @@ -230,9 +230,9 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt); * Some of these are not directly referenced in this file, but they must be * here anyway. */ -%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op +%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op BIGICONST %token <ival> ICONST PARAM -%token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER +%token TYPECAST DOT_DOT COLON_EQUALS SQRT EQUALS_GREATER %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS /*
^ permalink raw reply [nested|flat] 2+ messages in thread

* sqrt_bigint_v2
@ 2021-11-12 06:37  孤傲小二~阿沐 <2903807914@qq.com>
  0 siblings, 0 replies; 2+ messages in thread

From: 孤傲小二~阿沐 @ 2021-11-12 06:37 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>; tgl <tgl@sss.pgh.pa.us>

This function is already OK, I think tom is correct. After I modified the relevant files, the problem was solved.


Now my newly added functions and the regression test of the database itself are all OK


Thank you both for your continuous support and blessings from China!

Attachments:

580D2B64@557CCA3C.9E0B8E61.png.jpg (image/jpeg, 30.0K) download view image
sqrt_bigint_v2.patch (application/octet-stream, 9.0K) download diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index eb24195..4cf000c 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -478,7 +478,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type <node> fetch_args select_limit_value offset_clause select_offset_value - select_fetch_first_value I_or_F_const + select_fetch_first_value I_or_F_const I_or_BIGI_const %type <ival> row_or_rows first_or_next %type <list> OptSeqOptList SeqOptList OptParenthesizedSeqOptList @@ -623,9 +623,9 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); * DOT_DOT is unused in the core SQL grammar, and so will always provoke * parse errors. It is needed by PL/pgSQL. */ -%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op +%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op BIGICONST %token <ival> ICONST PARAM -%token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER +%token TYPECAST DOT_DOT COLON_EQUALS SQRT EQUALS_GREATER %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS /* @@ -804,6 +804,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %left '[' ']' %left '(' ')' %left TYPECAST +%left SQRT %left '.' /* * These might seem to be low-precedence, but actually they are not part @@ -4452,12 +4453,19 @@ opt_by: BY NumericOnly: FCONST { $$ = makeFloat($1); } + | BIGICONST { $$ = makeFloat($1); } | '+' FCONST { $$ = makeFloat($2); } + | '+' BIGICONST { $$ = makeFloat($2); } | '-' FCONST { $$ = makeFloat($2); doNegateFloat($$); } + | '-' BIGICONST + { + $$ = makeFloat($2); + doNegateFloat($$); + } | SignedIconst { $$ = makeInteger($1); } ; @@ -5530,6 +5538,7 @@ TriggerFuncArg: $$ = makeString(psprintf("%d", $1)); } | FCONST { $$ = makeString($1); } + | BIGICONST { $$ = makeString($1); } | Sconst { $$ = makeString($1); } | ColLabel { $$ = makeString($1); } ; @@ -11894,6 +11903,11 @@ I_or_F_const: | FCONST { $$ = makeFloatConst($1,@1); } ; +I_or_BIGI_const: + Iconst { $$ = makeIntConst($1,@1); } + | BIGICONST { $$ = makeFloatConst($1,@1); } + ; + /* noise words */ row_or_rows: ROW { $$ = 0; } | ROWS { $$ = 0; } @@ -13274,6 +13288,13 @@ a_expr: c_expr { $$ = $1; } { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); } | '-' a_expr %prec UMINUS { $$ = doNegate($2, @1); } + | SQRT I_or_BIGI_const %prec SQRT + { + $$ = (Node *) makeFuncCall(SystemFuncName("sqrt"), + list_make1($2), + COERCE_SQL_SYNTAX, + @1); + } | a_expr '+' a_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); } | a_expr '-' a_expr @@ -13703,6 +13724,13 @@ b_expr: c_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); } | '-' b_expr %prec UMINUS { $$ = doNegate($2, @1); } + | SQRT I_or_BIGI_const %prec SQRT + { + $$ = (Node *) makeFuncCall(SystemFuncName("sqrt"), + list_make1($2), + COERCE_SQL_SYNTAX, + @2); + } | b_expr '+' b_expr { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); } | b_expr '-' b_expr @@ -15149,6 +15177,10 @@ AexprConst: Iconst { $$ = makeFloatConst($1, @1); } + | BIGICONST + { + $$ = makeFloatConst($1, @1); + } | Sconst { $$ = makeStringConst($1, @1); diff --git a/src/backend/parser/parse_type.c b/src/backend/parser/parse_type.c index abe131e..0baec9d 100644 --- a/src/backend/parser/parse_type.c +++ b/src/backend/parser/parse_type.c @@ -718,7 +718,7 @@ pts_error_callback(void *arg) { const char *str = (const char *) arg; - errcontext("invalid type name \"%s\"", str); + errcontext("invalid type name HERE2 \"%s\"", str); } /* @@ -763,7 +763,7 @@ typeStringToTypeName(const char *str) fail: ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), - errmsg("invalid type name \"%s\"", str))); + errmsg("invalid type name HERE1 \"%s\"", str))); return NULL; /* keep compiler quiet */ } diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index 9f9d8a1..70f1d9f 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -124,6 +124,7 @@ static void addlitchar(unsigned char ychar, core_yyscan_t yyscanner); static char *litbufdup(core_yyscan_t yyscanner); static unsigned char unescape_single_char(unsigned char c, core_yyscan_t yyscanner); static int process_integer_literal(const char *token, YYSTYPE *lval); +static int process_integer_bigint_literal(const char *token, YYSTYPE *lval); static void addunicode(pg_wchar c, yyscan_t yyscanner); #define yyerror(msg) scanner_yyerror(msg, yyscanner) @@ -350,6 +351,7 @@ identifier {ident_start}{ident_cont}* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -826,6 +828,11 @@ other . return COLON_EQUALS; } +{sqrt} { + SET_YYLLOC(); + return SQRT; + } + {equals_greater} { SET_YYLLOC(); return EQUALS_GREATER; @@ -974,8 +981,9 @@ other . {integer} { SET_YYLLOC(); - return process_integer_literal(yytext, yylval); + return process_integer_bigint_literal(yytext, yylval); } + {decimal} { SET_YYLLOC(); yylval->str = pstrdup(yytext); @@ -1319,6 +1327,24 @@ process_integer_literal(const char *token, YYSTYPE *lval) return ICONST; } +static int +process_integer_bigint_literal(const char *token, YYSTYPE *lval) +{ + int val; + char *endptr; + + errno = 0; + val = strtoint(token, &endptr, 10); + if (*endptr != '\0' || errno == ERANGE) + { + /* integer too large (or contains decimal pt), treat it as a float */ + lval->str = pstrdup(token); + return BIGICONST; + } + lval->ival = val; + return ICONST; +} + static void addunicode(pg_wchar c, core_yyscan_t yyscanner) { diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index 0fab48a..6c1ab33 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -289,6 +289,7 @@ identifier {ident_start}{ident_cont}* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -606,6 +607,10 @@ other . ECHO; } +{sqrt} { + ECHO; + } + {equals_greater} { ECHO; } diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index 7a03566..a8e2c94 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -58,6 +58,7 @@ static bool include_next; static void addlit(char *ytext, int yleng); static void addlitchar(unsigned char); static int process_integer_literal(const char *token, YYSTYPE *lval); +static int process_integer_bigint_literal(const char *token, YYSTYPE *lval); static void parse_include(void); static bool ecpg_isspace(char ch); static bool isdefine(void); @@ -317,6 +318,7 @@ array ({ident_cont}|{whitespace}|[\[\]\+\-\*\%\/\(\)\>\.])* typecast "::" dot_dot \.\. colon_equals ":=" +sqrt "���" /* * These operator-like tokens (unlike the above ones) also match the {operator} @@ -771,6 +773,10 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ return COLON_EQUALS; } +{sqrt} { + return SQRT; + } + {equals_greater} { return EQUALS_GREATER; } @@ -924,7 +930,7 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ <C,SQL>{ {integer} { - return process_integer_literal(yytext, &base_yylval); + return process_integer_bigint_literal(yytext, &base_yylval); } {decimal} { base_yylval.str = mm_strdup(yytext); @@ -1567,6 +1573,24 @@ process_integer_literal(const char *token, YYSTYPE *lval) return ICONST; } +static int +process_integer_bigint_literal(const char *token, YYSTYPE *lval) +{ + int val; + char *endptr; + + errno = 0; + val = strtoint(token, &endptr, 10); + if (*endptr != '\0' || errno == ERANGE) + { + /* integer too large (or contains decimal pt), treat it as a float */ + lval->str = pstrdup(token); + return BIGICONST; + } + lval->ival = val; + return ICONST; +} + static void parse_include(void) { diff --git a/src/pl/plpgsql/src/pl_gram.y b/src/pl/plpgsql/src/pl_gram.y index 0f6a5b3..3229cd2 100644 --- a/src/pl/plpgsql/src/pl_gram.y +++ b/src/pl/plpgsql/src/pl_gram.y @@ -230,9 +230,9 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt); * Some of these are not directly referenced in this file, but they must be * here anyway. */ -%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op +%token <str> IDENT UIDENT FCONST SCONST USCONST BCONST XCONST Op BIGICONST %token <ival> ICONST PARAM -%token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER +%token TYPECAST DOT_DOT COLON_EQUALS SQRT EQUALS_GREATER %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS /*
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2021-11-12 06:37 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-11-12 06:36 sqrt_bigint_v2 孤傲小二~阿沐 <2903807914@qq.com>
2021-11-12 06:37 sqrt_bigint_v2 孤傲小二~阿沐 <2903807914@qq.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox