grammars-v4 icon indicating copy to clipboard operation
grammars-v4 copied to clipboard

PostgreSql: table check constraint is parsed as a column.

Open victor-wiki opened this issue 2 years ago • 1 comments

CREATE TABLE test(
NAME character varying NOT NULL check (length(name)>0),
constraint ck_name check (length(name)>0)
)

the constraint ck_name check (length(name)>0) is parsed as ColumnDefContext, actually it's TableconstraintContext.

victor-wiki avatar Dec 08 '22 00:12 victor-wiki

I just checked, and this problem is still there. This is due to an ambiguity in the grammar. The Trash Toolkit shows the ambiguity in the parse.

$ dotnet trparse --ambig x.sql | dotnet trtree -a
CSharp 0 x.sql success 0.5698721
(root (stmtblock (stmtmulti (stmt (createstmt (CREATE "CREATE") (TABLE "TABLE") (qualified_name (colid (identifier (Identifier "test")))) (OPEN_PAREN "(") (opttableelementlist (tableelementlist (tableelement (columnDef (colid (unreserved_keyword (NAME_P "NAME"))) (typename (simpletypename (character (character_c (CHARACTER "character") (varying_ (VARYING "varying"))))) (opt_array_bounds)) (colquallist (colconstraint (colconstraintelem (NOT "NOT") (NULL_P "NULL"))) (colconstraint (colconstraintelem (CHECK "check") (OPEN_PAREN "(") (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (func_expr (func_application (func_name (builtin_function_name (LENGTH "length"))) (OPEN_PAREN "(") (func_arg_list (func_arg_expr (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (columnref (colid (unreserved_keyword (NAME_P "name")))))))))))))))))))))))))))) (CLOSE_PAREN ")")))))))))))))) (GT ">") (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (aexprconst (iconst (Integral "0"))))))))))))))))))))))))) (CLOSE_PAREN ")")))))) (COMMA ",") (tableelement (tableconstraint (CONSTRAINT "constraint") (name (colid (identifier (Identifier "ck_name")))) (constraintelem (CHECK "check") (OPEN_PAREN "(") (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (func_expr (func_application (func_name (builtin_function_name (LENGTH "length"))) (OPEN_PAREN "(") (func_arg_list (func_arg_expr (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (columnref (colid (unreserved_keyword (NAME_P "name")))))))))))))))))))))))))))) (CLOSE_PAREN ")")))))))))))))) (GT ">") (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (aexprconst (iconst (Integral "0"))))))))))))))))))))))))) (CLOSE_PAREN ")") (constraintattributespec)))))) (CLOSE_PAREN ")"))) (SEMI ";"))) (EOF ""))
(root (stmtblock (stmtmulti (stmt (createstmt (CREATE "CREATE") (TABLE "TABLE") (qualified_name (colid (identifier (Identifier "test")))) (OPEN_PAREN "(") (opttableelementlist (tableelementlist (tableelement (columnDef (colid (unreserved_keyword (NAME_P "NAME"))) (typename (simpletypename (character (character_c (CHARACTER "character") (varying_ (VARYING "varying"))))) (opt_array_bounds)) (colquallist (colconstraint (colconstraintelem (NOT "NOT") (NULL_P "NULL"))) (colconstraint (colconstraintelem (CHECK "check") (OPEN_PAREN "(") (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (func_expr (func_application (func_name (builtin_function_name (LENGTH "length"))) (OPEN_PAREN "(") (func_arg_list (func_arg_expr (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (columnref (colid (unreserved_keyword (NAME_P "name")))))))))))))))))))))))))))) (CLOSE_PAREN ")")))))))))))))) (GT ">") (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (aexprconst (iconst (Integral "0"))))))))))))))))))))))))) (CLOSE_PAREN ")")))))) (COMMA ",") (tableelement (columnDef (colid (identifier (plsql_unreserved_keyword (CONSTRAINT "constraint")))) (typename (simpletypename (generictype (type_function_name (identifier (Identifier "ck_name"))))) (opt_array_bounds)) (colquallist (colconstraint (colconstraintelem (CHECK "check") (OPEN_PAREN "(") (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (func_expr (func_application (func_name (builtin_function_name (LENGTH "length"))) (OPEN_PAREN "(") (func_arg_list (func_arg_expr (a_expr (a_expr_qual (a_expr_lessless (a_expr_or (a_expr_and (a_expr_between (a_expr_in (a_expr_unary_not (a_expr_isnull (a_expr_is_not (a_expr_compare (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (columnref (colid (unreserved_keyword (NAME_P "name")))))))))))))))))))))))))))) (CLOSE_PAREN ")")))))))))))))) (GT ">") (a_expr_like (a_expr_qual_op (a_expr_unary_qualop (a_expr_add (a_expr_mul (a_expr_caret (a_expr_unary_sign (a_expr_at_time_zone (a_expr_collate (a_expr_typecast (c_expr (aexprconst (iconst (Integral "0"))))))))))))))))))))))))) (CLOSE_PAREN ")")))))))) (CLOSE_PAREN ")"))) (SEMI ";"))) (EOF ""))

kaby76 avatar Nov 04 '24 14:11 kaby76