4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 1.89 1998/01/11 03:41:38 momjian Exp $
16 * AUTHOR DATE MAJOR EVENT
17 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
18 * Andrew Yu Oct, 1994 lispy code conversion
21 * CAPITALS are used to represent terminal symbols.
22 * non-capitals are used to represent non-terminals.
23 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
24 * to help isolate the non-extensible portions of the parser.
26 * if you use list, make sure the datum is a node so that the printing
30 * sometimes we assign constants to makeStrings. Make sure we don't free
33 *-------------------------------------------------------------------------
39 #include "nodes/parsenodes.h"
40 #include "nodes/print.h"
41 #include "parser/gramparse.h"
42 #include "parser/parse_type.h"
43 #include "utils/acl.h"
44 #include "utils/palloc.h"
45 #include "catalog/catname.h"
46 #include "utils/elog.h"
47 #include "access/xact.h"
49 static char saved_relname[NAMEDATALEN]; /* need this for complex attributes */
50 static bool QueryIsRule = FALSE;
51 static Node *saved_In_Expr;
52 static Oid *param_type_info;
53 static int pfunc_num_args;
54 extern List *parsetree;
58 * If you need access to certain yacc-generated variables and find that
59 * they're static by default, uncomment the next line. (this is not a
62 /*#define __YYSCLASS*/
64 static char *xlateSqlType(char *);
65 static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
66 static Node *makeRowExpr(char *opr, List *largs, List *rargs);
67 void mapTargetColumns(List *source, List *target);
68 static List *makeConstantList( A_Const *node);
69 static char *FlattenStringList(List *list);
70 static char *fmtId(char *rawid);
71 static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr);
72 static void param_type_init(Oid *typev, int nargs);
74 Oid param_type(int t); /* used in parse_expr.c */
76 /* old versions of flex define this as a macro */
90 bool* pboolean; /* for pg_user privileges */
100 SortGroupBy *sortgroupby;
115 AddAttrStmt, ClosePortalStmt,
116 CopyStmt, CreateStmt, CreateAsStmt, CreateSeqStmt, DefineStmt, DestroyStmt,
117 ExtendStmt, FetchStmt, GrantStmt, CreateTrigStmt, DropTrigStmt,
118 CreatePLangStmt, DropPLangStmt,
119 IndexStmt, ListenStmt, OptimizableStmt,
120 ProcedureStmt, RecipeStmt, RemoveAggrStmt, RemoveOperStmt,
121 RemoveFuncStmt, RemoveStmt,
122 RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
123 CreatedbStmt, DestroydbStmt, VacuumStmt, CursorStmt, SubSelect,
124 UpdateStmt, InsertStmt, SelectStmt, NotifyStmt, DeleteStmt, ClusterStmt,
125 ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
126 CreateUserStmt, AlterUserStmt, DropUserStmt
130 %type <str> opt_database, location
132 %type <pboolean> user_createdb_clause, user_createuser_clause
133 %type <str> user_passwd_clause
134 %type <str> user_valid_clause
135 %type <list> user_group_list, user_group_clause
137 %type <str> join_expr, join_outer, join_spec
138 %type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
140 %type <str> TriggerEvents, TriggerFuncArg
142 %type <str> relation_name, copy_file_name, copy_delimiter, def_name,
143 database_name, access_method_clause, access_method, attr_name,
144 class, index_name, name, file_name, recipe_name, aggr_argtype
146 %type <str> opt_id, opt_portal_name,
147 all_Op, MathOp, opt_name, opt_unique,
148 result, OptUseOp, opt_class, SpecialRuleRelation
150 %type <str> privileges, operation_commalist, grantee
151 %type <chr> operation, TriggerOneEvent
153 %type <list> stmtblock, stmtmulti,
154 relation_name_list, OptTableElementList,
155 OptInherit, definition,
156 opt_with, def_args, def_name_list, func_argtypes,
157 oper_argtypes, OptStmtList, OptStmtBlock, OptStmtMulti,
158 opt_column_list, columnList, opt_va_list, va_list,
159 sort_clause, sortby_list, index_params, index_list, name_list,
160 from_clause, from_list, opt_array_bounds, nest_array_bounds,
161 expr_list, attrs, res_target_list, res_target_list2,
162 def_list, opt_indirection, group_clause, groupby_list, TriggerFuncArgs
164 %type <list> union_clause, select_list
165 %type <list> join_list
168 %type <boolean> opt_union
170 %type <node> position_expr
171 %type <list> extract_list, position_list
172 %type <list> substr_list, substr_from, substr_for, trim_list
173 %type <list> opt_interval
175 %type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
176 index_opt_unique, opt_verbose, opt_analyze
178 %type <ival> copy_dirn, def_type, opt_direction, remove_type,
181 %type <ival> fetch_how_many
183 %type <list> OptSeqList
184 %type <defelt> OptSeqElem
186 %type <dstmt> def_rest
187 %type <astmt> insert_rest
189 %type <node> OptTableElement, ConstraintElem
190 %type <node> columnDef, alter_clause
191 %type <defelt> def_elem
192 %type <node> def_arg, columnElem, where_clause,
193 a_expr, a_expr_or_null, AexprConst,
194 in_expr, in_expr_nodes, not_in_expr, not_in_expr_nodes,
196 %type <list> row_descriptor, row_list
197 %type <node> row_expr
198 %type <list> OptCreateAs, CreateAsList
199 %type <node> CreateAsElement
200 %type <value> NumConst
201 %type <attr> event_object, attr
202 %type <sortgroupby> groupby
203 %type <sortgroupby> sortby
204 %type <ielem> index_elem, func_index
205 %type <range> from_val
206 %type <relexp> relation_expr
207 %type <target> res_target_el, res_target_el2
208 %type <paramno> ParamNo
210 %type <typnam> Typename, opt_type, Array, Generic, Character, Datetime, Numeric
211 %type <str> generic, character, datetime
212 %type <str> opt_charset, opt_collate
213 %type <str> opt_float, opt_numeric, opt_decimal
214 %type <boolean> opt_varying, opt_timezone
218 %type <str> Id, var_value, zone_value
219 %type <str> ColId, ColLabel
221 %type <node> TableConstraint
222 %type <list> constraint_list, constraint_expr
223 %type <list> default_list, default_expr
224 %type <list> ColQualList, ColQualifier
225 %type <node> ColConstraint, ColConstraintElem
226 %type <list> key_actions, key_action
227 %type <str> key_match, key_reference
230 * If you make any token changes, remember to:
231 * - use "yacc -d" and update parse.h
232 * - update the keyword table in parser/keywords.c
235 /* Reserved word tokens
236 * SQL92 syntax has many type-specific constructs.
237 * So, go ahead and make these types reserved words,
238 * and call-out the syntax explicitly.
239 * This gets annoying when trying to also retain Postgres' nice
240 * type-extensible features, but we don't really have a choice.
241 * - thomas 1997-10-11
244 /* Keywords (in SQL92 reserved words) */
245 %token ACTION, ADD, ALL, ALTER, AND, AS, ASC,
246 BEGIN_TRANS, BETWEEN, BOTH, BY,
247 CASCADE, CAST, CHAR, CHARACTER, CHECK, CLOSE, COLLATE, COLUMN, COMMIT,
248 CONSTRAINT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME,
249 CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
250 DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
251 END_TRANS, EXECUTE, EXISTS, EXTRACT,
252 FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
253 GRANT, GROUP, HAVING, HOUR_P,
254 IN, INNER_P, INSERT, INTERVAL, INTO, IS,
255 JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
256 MATCH, MINUTE_P, MONTH_P,
257 NATIONAL, NATURAL, NCHAR, NO, NOT, NOTIFY, NULL_P, NUMERIC,
258 ON, OPTION, OR, ORDER, OUTER_P,
259 PARTIAL, POSITION, PRECISION, PRIMARY, PRIVILEGES, PROCEDURE, PUBLIC,
260 REFERENCES, REVOKE, RIGHT, ROLLBACK,
261 SECOND_P, SELECT, SET, SUBSTRING,
262 TABLE, TIME, TIMESTAMP, TO, TRAILING, TRANSACTION, TRIM,
263 UNION, UNIQUE, UPDATE, USING,
264 VALUES, VARCHAR, VARYING, VERBOSE, VERSION, VIEW,
265 WHERE, WITH, WORK, YEAR_P, ZONE
267 /* Keywords (in SQL3 reserved words) */
268 %token FALSE_P, TRIGGER, TRUE_P
270 /* Keywords (in SQL92 non-reserved words) */
273 /* Keywords for Postgres support (not in SQL92 reserved words) */
274 %token ABORT_TRANS, ACL, AFTER, AGGREGATE, ANALYZE,
275 APPEND, BACKWARD, BEFORE, BINARY, CHANGE, CLUSTER, COPY,
276 DATABASE, DELIMITERS, DO, EXPLAIN, EXTEND,
277 FORWARD, FUNCTION, HANDLER,
278 INDEX, INHERITS, INSTEAD, ISNULL,
279 LANCOMPILER, LISTEN, LOAD, LOCATION, MERGE, MOVE,
280 NEW, NONE, NOTHING, NOTNULL, OIDS, OPERATOR, PROCEDURAL,
281 RECIPE, RENAME, REPLACE, RESET, RETRIEVE, RETURNS, RULE,
282 SEQUENCE, SETOF, SHOW, STDIN, STDOUT, TRUSTED,
283 VACUUM, VERBOSE, VERSION
285 /* Keywords (obsolete; retain temporarily for parser - thomas 1997-12-04) */
289 * Tokens for pg_passwd support. The CREATEDB and CREATEUSER tokens should go away
290 * when some sort of pg_privileges relation is introduced.
294 %token USER, PASSWORD, CREATEDB, NOCREATEDB, CREATEUSER, NOCREATEUSER, VALID, UNTIL
296 /* Special keywords, not in the query language - see the "lex" file */
297 %token <str> IDENT, SCONST, Op
298 %token <ival> ICONST, PARAM
301 /* these are not real. they are here so that they get generated as #define's*/
313 %nonassoc Op /* multi-character ops and user-defined operators */
319 %left '|' /* this is the relation union op, not logical or */
320 /* Unary Operators */
322 %left ';' /* end of statement or natural log */
334 { parsetree = lcons($1,NIL); }
337 stmtmulti: stmtmulti stmt ';'
338 { $$ = lappend($1, $2); }
340 { $$ = lappend($1, $2); }
342 { $$ = lcons($1,NIL); }
388 /*****************************************************************************
390 * Create a new Postgres DBMS user
393 *****************************************************************************/
395 CreateUserStmt: CREATE USER Id user_passwd_clause user_createdb_clause
396 user_createuser_clause user_group_clause user_valid_clause
398 CreateUserStmt *n = makeNode(CreateUserStmt);
409 /*****************************************************************************
411 * Alter a postresql DBMS user
414 *****************************************************************************/
416 AlterUserStmt: ALTER USER Id user_passwd_clause user_createdb_clause
417 user_createuser_clause user_group_clause user_valid_clause
419 AlterUserStmt *n = makeNode(AlterUserStmt);
430 /*****************************************************************************
432 * Drop a postresql DBMS user
435 *****************************************************************************/
437 DropUserStmt: DROP USER Id
439 DropUserStmt *n = makeNode(DropUserStmt);
445 user_passwd_clause: WITH PASSWORD Id { $$ = $3; }
446 | /*EMPTY*/ { $$ = NULL; }
449 user_createdb_clause: CREATEDB
452 $$ = (b = (bool*)palloc(sizeof(bool)));
458 $$ = (b = (bool*)palloc(sizeof(bool)));
461 | /*EMPTY*/ { $$ = NULL; }
464 user_createuser_clause: CREATEUSER
467 $$ = (b = (bool*)palloc(sizeof(bool)));
473 $$ = (b = (bool*)palloc(sizeof(bool)));
476 | /*EMPTY*/ { $$ = NULL; }
479 user_group_list: user_group_list ',' Id
481 $$ = lcons((void*)makeString($3), $1);
485 $$ = lcons((void*)makeString($1), NIL);
489 user_group_clause: IN GROUP user_group_list { $$ = $3; }
490 | /*EMPTY*/ { $$ = NULL; }
493 user_valid_clause: VALID UNTIL SCONST { $$ = $3; }
494 | /*EMPTY*/ { $$ = NULL; }
497 /*****************************************************************************
499 * Set PG internal variable
500 * SET name TO 'var_value'
501 * Include SQL92 syntax (thomas 1997-10-22):
502 * SET TIME ZONE 'var_value'
504 *****************************************************************************/
506 VariableSetStmt: SET ColId TO var_value
508 VariableSetStmt *n = makeNode(VariableSetStmt);
513 | SET ColId '=' var_value
515 VariableSetStmt *n = makeNode(VariableSetStmt);
520 | SET TIME ZONE zone_value
522 VariableSetStmt *n = makeNode(VariableSetStmt);
523 n->name = "timezone";
529 var_value: Sconst { $$ = $1; }
530 | DEFAULT { $$ = NULL; }
533 zone_value: Sconst { $$ = $1; }
534 | DEFAULT { $$ = NULL; }
535 | LOCAL { $$ = "default"; }
538 VariableShowStmt: SHOW ColId
540 VariableShowStmt *n = makeNode(VariableShowStmt);
546 VariableShowStmt *n = makeNode(VariableShowStmt);
547 n->name = "timezone";
552 VariableResetStmt: RESET ColId
554 VariableResetStmt *n = makeNode(VariableResetStmt);
560 VariableResetStmt *n = makeNode(VariableResetStmt);
561 n->name = "timezone";
567 /*****************************************************************************
570 * addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
572 *****************************************************************************/
574 AddAttrStmt: ALTER TABLE relation_name opt_inh_star alter_clause
576 AddAttrStmt *n = makeNode(AddAttrStmt);
584 alter_clause: ADD opt_column columnDef
588 | ADD '(' OptTableElementList ')'
590 Node *lp = lfirst($3);
593 elog(ERROR,"ALTER TABLE/ADD() allows one column only");
596 | DROP opt_column ColId
597 { elog(ERROR,"ALTER TABLE/DROP COLUMN not yet implemented"); }
598 | ALTER opt_column ColId SET DEFAULT default_expr
599 { elog(ERROR,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented"); }
600 | ALTER opt_column ColId DROP DEFAULT
601 { elog(ERROR,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented"); }
603 { elog(ERROR,"ALTER TABLE/ADD CONSTRAINT not yet implemented"); }
607 /*****************************************************************************
612 *****************************************************************************/
614 ClosePortalStmt: CLOSE opt_id
616 ClosePortalStmt *n = makeNode(ClosePortalStmt);
623 /*****************************************************************************
626 * COPY [BINARY] <relname> FROM/TO
627 * [USING DELIMITERS <delimiter>]
629 *****************************************************************************/
631 CopyStmt: COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
633 CopyStmt *n = makeNode(CopyStmt);
651 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
652 * used depends on the direction. (It really doesn't make sense to copy from
653 * stdout. We silently correct the "typo". - AY 9/94
655 copy_file_name: Sconst { $$ = $1; }
656 | STDIN { $$ = NULL; }
657 | STDOUT { $$ = NULL; }
660 opt_binary: BINARY { $$ = TRUE; }
661 | /*EMPTY*/ { $$ = FALSE; }
664 opt_with_copy: WITH OIDS { $$ = TRUE; }
665 | /*EMPTY*/ { $$ = FALSE; }
669 * the default copy delimiter is tab but the user can configure it
671 copy_delimiter: USING DELIMITERS Sconst { $$ = $3; }
672 | /*EMPTY*/ { $$ = "\t"; }
676 /*****************************************************************************
681 *****************************************************************************/
683 CreateStmt: CREATE TABLE relation_name '(' OptTableElementList ')'
684 OptInherit OptArchiveType
686 CreateStmt *n = makeNode(CreateStmt);
690 n->constraints = NIL;
695 OptTableElementList: OptTableElementList ',' OptTableElement
696 { $$ = lappend($1, $3); }
697 | OptTableElement { $$ = lcons($1, NIL); }
698 | /*EMPTY*/ { $$ = NULL; }
701 OptTableElement: columnDef { $$ = $1; }
702 | TableConstraint { $$ = $1; }
705 columnDef: ColId Typename ColQualifier
707 ColumnDef *n = makeNode(ColumnDef);
711 n->is_not_null = FALSE;
717 ColQualifier: ColQualList { $$ = $1; }
718 | /*EMPTY*/ { $$ = NULL; }
721 ColQualList: ColQualList ColConstraint { $$ = lappend($1,$2); }
722 | ColConstraint { $$ = lcons($1, NIL); }
726 CONSTRAINT name ColConstraintElem
728 Constraint *n = (Constraint *)$3;
736 ColConstraintElem: CHECK '(' constraint_expr ')'
738 Constraint *n = makeNode(Constraint);
739 n->contype = CONSTR_CHECK;
741 n->def = FlattenStringList($3);
745 | DEFAULT default_expr
747 Constraint *n = makeNode(Constraint);
748 n->contype = CONSTR_DEFAULT;
750 n->def = FlattenStringList($2);
756 Constraint *n = makeNode(Constraint);
757 n->contype = CONSTR_NOTNULL;
765 Constraint *n = makeNode(Constraint);
766 n->contype = CONSTR_UNIQUE;
774 Constraint *n = makeNode(Constraint);
775 n->contype = CONSTR_PRIMARY;
781 | REFERENCES ColId opt_column_list key_match key_actions
783 elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
788 default_list: default_list ',' default_expr
790 $$ = lappend($1,makeString(","));
799 default_expr: AexprConst
800 { $$ = makeConstantList((A_Const *) $1); }
802 { $$ = lcons( makeString("NULL"), NIL); }
803 | '-' default_expr %prec UMINUS
804 { $$ = lcons( makeString( "-"), $2); }
805 | default_expr '+' default_expr
806 { $$ = nconc( $1, lcons( makeString( "+"), $3)); }
807 | default_expr '-' default_expr
808 { $$ = nconc( $1, lcons( makeString( "-"), $3)); }
809 | default_expr '/' default_expr
810 { $$ = nconc( $1, lcons( makeString( "/"), $3)); }
811 | default_expr '*' default_expr
812 { $$ = nconc( $1, lcons( makeString( "*"), $3)); }
813 | default_expr '=' default_expr
814 { elog(ERROR,"boolean expressions not supported in DEFAULT"); }
815 | default_expr '<' default_expr
816 { elog(ERROR,"boolean expressions not supported in DEFAULT"); }
817 | default_expr '>' default_expr
818 { elog(ERROR,"boolean expressions not supported in DEFAULT"); }
820 { $$ = lcons( makeString( ":"), $2); }
822 { $$ = lcons( makeString( ";"), $2); }
824 { $$ = lcons( makeString( "|"), $2); }
825 | default_expr TYPECAST Typename
827 $3->name = fmtId($3->name);
828 $$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
830 | CAST default_expr AS Typename
832 $4->name = fmtId($4->name);
833 $$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
835 | '(' default_expr ')'
836 { $$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
839 $$ = makeList( makeString($1), makeString("("), -1);
840 $$ = lappend( $$, makeString(")"));
842 | name '(' default_list ')'
844 $$ = makeList( makeString($1), makeString("("), -1);
846 $$ = lappend( $$, makeString(")"));
848 | default_expr Op default_expr
850 if (!strcmp("<=", $2) || !strcmp(">=", $2))
851 elog(ERROR,"boolean expressions not supported in DEFAULT");
852 $$ = nconc( $1, lcons( makeString( $2), $3));
855 { $$ = lcons( makeString( $1), $2); }
857 { $$ = lappend( $1, makeString( $2)); }
858 /* XXX - thomas 1997-10-07 v6.2 function-specific code to be changed */
860 { $$ = lcons( makeString( "date( 'current'::datetime + '0 sec')"), NIL); }
862 { $$ = lcons( makeString( "'now'::time"), NIL); }
863 | CURRENT_TIME '(' Iconst ')'
866 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
867 $$ = lcons( makeString( "'now'::time"), NIL);
870 { $$ = lcons( makeString( "now()"), NIL); }
871 | CURRENT_TIMESTAMP '(' Iconst ')'
874 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
875 $$ = lcons( makeString( "now()"), NIL);
878 { $$ = lcons( makeString( "CURRENT_USER"), NIL); }
881 /* ConstraintElem specifies constraint syntax which is not embedded into
882 * a column definition. ColConstraintElem specifies the embedded form.
883 * - thomas 1997-12-03
885 TableConstraint: CONSTRAINT name ConstraintElem
887 Constraint *n = (Constraint *)$3;
895 ConstraintElem: CHECK '(' constraint_expr ')'
897 Constraint *n = makeNode(Constraint);
898 n->contype = CONSTR_CHECK;
900 n->def = FlattenStringList($3);
903 | UNIQUE '(' columnList ')'
905 Constraint *n = makeNode(Constraint);
906 n->contype = CONSTR_UNIQUE;
912 | PRIMARY KEY '(' columnList ')'
914 Constraint *n = makeNode(Constraint);
915 n->contype = CONSTR_PRIMARY;
921 | FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
922 { elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented"); }
925 constraint_list: constraint_list ',' constraint_expr
927 $$ = lappend($1,makeString(","));
936 constraint_expr: AexprConst
937 { $$ = makeConstantList((A_Const *) $1); }
939 { $$ = lcons( makeString("NULL"), NIL); }
942 $$ = lcons( makeString(fmtId($1)), NIL);
944 | '-' constraint_expr %prec UMINUS
945 { $$ = lcons( makeString( "-"), $2); }
946 | constraint_expr '+' constraint_expr
947 { $$ = nconc( $1, lcons( makeString( "+"), $3)); }
948 | constraint_expr '-' constraint_expr
949 { $$ = nconc( $1, lcons( makeString( "-"), $3)); }
950 | constraint_expr '/' constraint_expr
951 { $$ = nconc( $1, lcons( makeString( "/"), $3)); }
952 | constraint_expr '*' constraint_expr
953 { $$ = nconc( $1, lcons( makeString( "*"), $3)); }
954 | constraint_expr '=' constraint_expr
955 { $$ = nconc( $1, lcons( makeString( "="), $3)); }
956 | constraint_expr '<' constraint_expr
957 { $$ = nconc( $1, lcons( makeString( "<"), $3)); }
958 | constraint_expr '>' constraint_expr
959 { $$ = nconc( $1, lcons( makeString( ">"), $3)); }
960 | ':' constraint_expr
961 { $$ = lcons( makeString( ":"), $2); }
962 | ';' constraint_expr
963 { $$ = lcons( makeString( ";"), $2); }
964 | '|' constraint_expr
965 { $$ = lcons( makeString( "|"), $2); }
966 | constraint_expr TYPECAST Typename
968 $3->name = fmtId($3->name);
969 $$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
971 | CAST constraint_expr AS Typename
973 $4->name = fmtId($4->name);
974 $$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
976 | '(' constraint_expr ')'
977 { $$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
980 $$ = makeList( makeString($1), makeString("("), -1);
981 $$ = lappend( $$, makeString(")"));
983 | name '(' constraint_list ')'
985 $$ = makeList( makeString($1), makeString("("), -1);
987 $$ = lappend( $$, makeString(")"));
989 | constraint_expr Op constraint_expr
990 { $$ = nconc( $1, lcons( makeString( $2), $3)); }
991 | constraint_expr AND constraint_expr
992 { $$ = nconc( $1, lcons( makeString( "AND"), $3)); }
993 | constraint_expr OR constraint_expr
994 { $$ = nconc( $1, lcons( makeString( "OR"), $3)); }
995 | NOT constraint_expr
996 { $$ = lcons( makeString( "NOT"), $2); }
998 { $$ = lcons( makeString( $1), $2); }
1000 { $$ = lappend( $1, makeString( $2)); }
1001 | constraint_expr ISNULL
1002 { $$ = lappend( $1, makeString( "IS NULL")); }
1003 | constraint_expr IS NULL_P
1004 { $$ = lappend( $1, makeString( "IS NULL")); }
1005 | constraint_expr NOTNULL
1006 { $$ = lappend( $1, makeString( "IS NOT NULL")); }
1007 | constraint_expr IS NOT NULL_P
1008 { $$ = lappend( $1, makeString( "IS NOT NULL")); }
1009 | constraint_expr IS TRUE_P
1010 { $$ = lappend( $1, makeString( "IS TRUE")); }
1011 | constraint_expr IS FALSE_P
1012 { $$ = lappend( $1, makeString( "IS FALSE")); }
1013 | constraint_expr IS NOT TRUE_P
1014 { $$ = lappend( $1, makeString( "IS NOT TRUE")); }
1015 | constraint_expr IS NOT FALSE_P
1016 { $$ = lappend( $1, makeString( "IS NOT FALSE")); }
1019 key_match: MATCH FULL { $$ = NULL; }
1020 | MATCH PARTIAL { $$ = NULL; }
1021 | /*EMPTY*/ { $$ = NULL; }
1024 key_actions: key_action key_action { $$ = NIL; }
1025 | key_action { $$ = NIL; }
1026 | /*EMPTY*/ { $$ = NIL; }
1029 key_action: ON DELETE key_reference { $$ = NIL; }
1030 | ON UPDATE key_reference { $$ = NIL; }
1033 key_reference: NO ACTION { $$ = NULL; }
1034 | CASCADE { $$ = NULL; }
1035 | SET DEFAULT { $$ = NULL; }
1036 | SET NULL_P { $$ = NULL; }
1039 OptInherit: INHERITS '(' relation_name_list ')' { $$ = $3; }
1040 | /*EMPTY*/ { $$ = NIL; }
1044 * "ARCHIVE" keyword was removed in 6.3, but we keep it for now
1045 * so people can upgrade with old pg_dump scripts. - momjian 1997-11-20(?)
1047 OptArchiveType: ARCHIVE '=' NONE { }
1051 CreateAsStmt: CREATE TABLE relation_name OptCreateAs AS SubSelect
1053 SelectStmt *n = (SelectStmt *)$6;
1055 mapTargetColumns($4, n->targetList);
1061 OptCreateAs: '(' CreateAsList ')' { $$ = $2; }
1062 | /*EMPTY*/ { $$ = NULL; }
1065 CreateAsList: CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1066 | CreateAsElement { $$ = lcons($1, NIL); }
1069 CreateAsElement: ColId
1071 ColumnDef *n = makeNode(ColumnDef);
1075 n->is_not_null = FALSE;
1076 n->constraints = NULL;
1082 /*****************************************************************************
1085 * CREATE SEQUENCE seqname
1087 *****************************************************************************/
1089 CreateSeqStmt: CREATE SEQUENCE relation_name OptSeqList
1091 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1099 OptSeqList OptSeqElem
1100 { $$ = lappend($1, $2); }
1104 OptSeqElem: IDENT NumConst
1106 $$ = makeNode(DefElem);
1108 $$->arg = (Node *)$2;
1112 $$ = makeNode(DefElem);
1114 $$->arg = (Node *)NULL;
1118 /*****************************************************************************
1121 * CREATE PROCEDURAL LANGUAGE ...
1122 * DROP PROCEDURAL LANGUAGE ...
1124 *****************************************************************************/
1126 CreatePLangStmt: CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst
1127 HANDLER def_name LANCOMPILER Sconst
1129 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1138 PLangTrusted: TRUSTED { $$ = TRUE; }
1141 DropPLangStmt: DROP PROCEDURAL LANGUAGE Sconst
1143 DropPLangStmt *n = makeNode(DropPLangStmt);
1149 /*****************************************************************************
1152 * CREATE TRIGGER ...
1155 *****************************************************************************/
1157 CreateTrigStmt: CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1158 relation_name TriggerForSpec EXECUTE PROCEDURE
1159 name '(' TriggerFuncArgs ')'
1161 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1168 memcpy (n->actions, $5, 4);
1173 TriggerActionTime: BEFORE { $$ = TRUE; }
1174 | AFTER { $$ = FALSE; }
1177 TriggerEvents: TriggerOneEvent
1179 char *e = palloc (4);
1180 e[0] = $1; e[1] = 0; $$ = e;
1182 | TriggerOneEvent OR TriggerOneEvent
1184 char *e = palloc (4);
1185 e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
1187 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
1189 char *e = palloc (4);
1190 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
1195 TriggerOneEvent: INSERT { $$ = 'i'; }
1196 | DELETE { $$ = 'd'; }
1197 | UPDATE { $$ = 'u'; }
1200 TriggerForSpec: FOR name name
1202 if ( strcmp ($2, "each") != 0 )
1203 elog(ERROR,"parser: syntax error near %s",$2);
1204 if ( strcmp ($3, "row") == 0 )
1206 else if ( strcmp ($3, "statement") == 0 )
1209 elog(ERROR,"parser: syntax error near %s",$3);
1213 TriggerFuncArgs: TriggerFuncArg
1214 { $$ = lcons($1, NIL); }
1215 | TriggerFuncArgs ',' TriggerFuncArg
1216 { $$ = lappend($1, $3); }
1221 TriggerFuncArg: ICONST
1223 char *s = (char *) palloc (256);
1224 sprintf (s, "%d", $1);
1229 char *s = (char *) palloc (256);
1230 sprintf (s, "%g", $1);
1233 | Sconst { $$ = $1; }
1234 | IDENT { $$ = $1; }
1237 DropTrigStmt: DROP TRIGGER name ON relation_name
1239 DropTrigStmt *n = makeNode(DropTrigStmt);
1247 /*****************************************************************************
1250 * define (type,operator,aggregate)
1252 *****************************************************************************/
1254 DefineStmt: CREATE def_type def_rest
1261 def_rest: def_name definition
1263 $$ = makeNode(DefineStmt);
1265 $$->definition = $2;
1269 def_type: OPERATOR { $$ = OPERATOR; }
1270 | TYPE_P { $$ = TYPE_P; }
1271 | AGGREGATE { $$ = AGGREGATE; }
1274 def_name: PROCEDURE { $$ = "procedure"; }
1275 | JOIN { $$ = "join"; }
1276 | ColId { $$ = $1; }
1277 | MathOp { $$ = $1; }
1281 definition: '(' def_list ')' { $$ = $2; }
1284 def_list: def_elem { $$ = lcons($1, NIL); }
1285 | def_list ',' def_elem { $$ = lappend($1, $3); }
1288 def_elem: def_name '=' def_arg
1290 $$ = makeNode(DefElem);
1292 $$->arg = (Node *)$3;
1296 $$ = makeNode(DefElem);
1298 $$->arg = (Node *)NULL;
1300 | DEFAULT '=' def_arg
1302 $$ = makeNode(DefElem);
1303 $$->defname = "default";
1304 $$->arg = (Node *)$3;
1308 def_arg: ColId { $$ = (Node *)makeString($1); }
1309 | all_Op { $$ = (Node *)makeString($1); }
1310 | NumConst { $$ = (Node *)$1; /* already a Value */ }
1311 | Sconst { $$ = (Node *)makeString($1); }
1314 TypeName *n = makeNode(TypeName);
1317 n->arrayBounds = NULL;
1320 | DOUBLE { $$ = (Node *)makeString("double"); }
1324 /*****************************************************************************
1327 * destroy <relname1> [, <relname2> .. <relnameN> ]
1329 *****************************************************************************/
1331 DestroyStmt: DROP TABLE relation_name_list
1333 DestroyStmt *n = makeNode(DestroyStmt);
1335 n->sequence = FALSE;
1338 | DROP SEQUENCE relation_name_list
1340 DestroyStmt *n = makeNode(DestroyStmt);
1348 /*****************************************************************************
1351 * fetch/move [forward | backward] [number | all ] [ in <portalname> ]
1353 *****************************************************************************/
1355 FetchStmt: FETCH opt_direction fetch_how_many opt_portal_name
1357 FetchStmt *n = makeNode(FetchStmt);
1364 | MOVE opt_direction fetch_how_many opt_portal_name
1366 FetchStmt *n = makeNode(FetchStmt);
1375 opt_direction: FORWARD { $$ = FORWARD; }
1376 | BACKWARD { $$ = BACKWARD; }
1377 | /*EMPTY*/ { $$ = FORWARD; /* default */ }
1380 fetch_how_many: Iconst
1382 if ($1 <= 0) elog(ERROR,"Please specify nonnegative count for fetch"); }
1383 | ALL { $$ = 0; /* 0 means fetch all tuples*/ }
1384 | /*EMPTY*/ { $$ = 1; /*default*/ }
1387 opt_portal_name: IN name { $$ = $2; }
1388 | /*EMPTY*/ { $$ = NULL; }
1392 /*****************************************************************************
1395 * GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1397 *****************************************************************************/
1399 GrantStmt: GRANT privileges ON relation_name_list TO grantee opt_with_grant
1401 $$ = (Node*)makeAclStmt($2,$4,$6,'+');
1407 privileges: ALL PRIVILEGES
1409 $$ = aclmakepriv("rwaR",0);
1413 $$ = aclmakepriv("rwaR",0);
1415 | operation_commalist
1421 operation_commalist: operation
1423 $$ = aclmakepriv("",$1);
1425 | operation_commalist ',' operation
1427 $$ = aclmakepriv($1,$3);
1434 $$ = ACL_MODE_RD_CHR;
1438 $$ = ACL_MODE_AP_CHR;
1442 $$ = ACL_MODE_WR_CHR;
1446 $$ = ACL_MODE_WR_CHR;
1450 $$ = ACL_MODE_RU_CHR;
1456 $$ = aclmakeuser("A","");
1460 $$ = aclmakeuser("G",$2);
1464 $$ = aclmakeuser("U",$1);
1468 opt_with_grant: WITH GRANT OPTION
1470 yyerror("WITH GRANT OPTION is not supported. Only relation owners can set privileges");
1476 /*****************************************************************************
1479 * REVOKE [privileges] ON [relation_name] FROM [user]
1481 *****************************************************************************/
1483 RevokeStmt: REVOKE privileges ON relation_name_list FROM grantee
1485 $$ = (Node*)makeAclStmt($2,$4,$6,'-');
1492 /*****************************************************************************
1495 * create index <indexname> on <relname>
1496 * using <access> "(" (<col> with <op>)+ ")" [with
1499 * [where <qual>] is not supported anymore
1500 *****************************************************************************/
1502 IndexStmt: CREATE index_opt_unique INDEX index_name ON relation_name
1503 access_method_clause '(' index_params ')' opt_with
1505 /* should check that access_method is valid,
1506 etc ... but doesn't */
1507 IndexStmt *n = makeNode(IndexStmt);
1511 n->accessMethod = $7;
1512 n->indexParams = $9;
1513 n->withClause = $11;
1514 n->whereClause = NULL;
1519 index_opt_unique: UNIQUE { $$ = TRUE; }
1520 | /*EMPTY*/ { $$ = FALSE; }
1523 access_method_clause: USING access_method { $$ = $2; }
1524 | /*EMPTY*/ { $$ = "btree"; }
1527 index_params: index_list { $$ = $1; }
1528 | func_index { $$ = lcons($1,NIL); }
1531 index_list: index_list ',' index_elem { $$ = lappend($1, $3); }
1532 | index_elem { $$ = lcons($1, NIL); }
1535 func_index: name '(' name_list ')' opt_type opt_class
1537 $$ = makeNode(IndexElem);
1545 index_elem: attr_name opt_type opt_class
1547 $$ = makeNode(IndexElem);
1555 opt_type: ':' Typename { $$ = $2; }
1556 | FOR Typename { $$ = $2; }
1557 | /*EMPTY*/ { $$ = NULL; }
1560 /* opt_class "WITH class" conflicts with preceeding opt_type
1561 * for Typename of "TIMESTAMP WITH TIME ZONE"
1562 * So, remove "WITH class" from the syntax. OK??
1563 * - thomas 1997-10-12
1564 * | WITH class { $$ = $2; }
1566 opt_class: class { $$ = $1; }
1567 | USING class { $$ = $2; }
1568 | /*EMPTY*/ { $$ = NULL; }
1572 /*****************************************************************************
1575 * extend index <indexname> [where <qual>]
1577 *****************************************************************************/
1579 ExtendStmt: EXTEND INDEX index_name where_clause
1581 ExtendStmt *n = makeNode(ExtendStmt);
1583 n->whereClause = $4;
1589 /*****************************************************************************
1592 * execute recipe <recipeName>
1594 *****************************************************************************/
1596 RecipeStmt: EXECUTE RECIPE recipe_name
1599 if (!IsTransactionBlock())
1600 elog(ERROR,"EXECUTE RECIPE may only be used in begin/end transaction blocks");
1602 n = makeNode(RecipeStmt);
1609 /*****************************************************************************
1612 * define function <fname>
1613 * (language = <lang>, returntype = <typename>
1614 * [, arch_pct = <percentage | pre-defined>]
1615 * [, disk_pct = <percentage | pre-defined>]
1616 * [, byte_pct = <percentage | pre-defined>]
1617 * [, perbyte_cpu = <int | pre-defined>]
1618 * [, percall_cpu = <int | pre-defined>]
1620 * [arg is (<type-1> { , <type-n>})]
1621 * as <filename or code in language as appropriate>
1623 *****************************************************************************/
1625 ProcedureStmt: CREATE FUNCTION def_name def_args
1626 RETURNS def_arg opt_with AS Sconst LANGUAGE Sconst
1628 ProcedureStmt *n = makeNode(ProcedureStmt);
1631 n->returnType = (Node *)$6;
1638 opt_with: WITH definition { $$ = $2; }
1639 | /*EMPTY*/ { $$ = NIL; }
1642 def_args: '(' def_name_list ')' { $$ = $2; }
1643 | '(' ')' { $$ = NIL; }
1646 def_name_list: name_list;
1648 /*****************************************************************************
1652 * remove function <funcname>
1653 * (REMOVE FUNCTION "funcname" (arg1, arg2, ...))
1654 * remove aggregate <aggname>
1655 * (REMOVE AGGREGATE "aggname" "aggtype")
1656 * remove operator <opname>
1657 * (REMOVE OPERATOR "opname" (leftoperand_typ rightoperand_typ))
1658 * remove type <typename>
1659 * (REMOVE TYPE "typename")
1660 * remove rule <rulename>
1661 * (REMOVE RULE "rulename")
1663 *****************************************************************************/
1665 RemoveStmt: DROP remove_type name
1667 RemoveStmt *n = makeNode(RemoveStmt);
1674 remove_type: TYPE_P { $$ = TYPE_P; }
1675 | INDEX { $$ = INDEX; }
1676 | RULE { $$ = RULE; }
1677 | VIEW { $$ = VIEW; }
1680 RemoveAggrStmt: DROP AGGREGATE name aggr_argtype
1682 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
1689 aggr_argtype: name { $$ = $1; }
1690 | '*' { $$ = NULL; }
1693 RemoveFuncStmt: DROP FUNCTION name '(' func_argtypes ')'
1695 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
1702 func_argtypes: name_list { $$ = $1; }
1703 | /*EMPTY*/ { $$ = NIL; }
1706 RemoveOperStmt: DROP OPERATOR all_Op '(' oper_argtypes ')'
1708 RemoveOperStmt *n = makeNode(RemoveOperStmt);
1715 all_Op: Op | MathOp;
1717 MathOp: '+' { $$ = "+"; }
1728 elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
1731 { $$ = makeList(makeString($1), makeString($3), -1); }
1732 | NONE ',' name /* left unary */
1733 { $$ = makeList(NULL, makeString($3), -1); }
1734 | name ',' NONE /* right unary */
1735 { $$ = makeList(makeString($1), NULL, -1); }
1739 /*****************************************************************************
1742 * rename <attrname1> in <relname> [*] to <attrname2>
1743 * rename <relname1> to <relname2>
1745 *****************************************************************************/
1747 RenameStmt: ALTER TABLE relation_name opt_inh_star
1748 RENAME opt_column opt_name TO name
1750 RenameStmt *n = makeNode(RenameStmt);
1759 opt_name: name { $$ = $1; }
1760 | /*EMPTY*/ { $$ = NULL; }
1763 opt_column: COLUMN { $$ = COLUMN; }
1764 | /*EMPTY*/ { $$ = 0; }
1768 /*****************************************************************************
1770 * QUERY: Define Rewrite Rule , Define Tuple Rule
1771 * Define Rule <old rules >
1773 * only rewrite rule is supported -- ay 9/94
1775 *****************************************************************************/
1777 RuleStmt: CREATE RULE name AS
1778 { QueryIsRule=TRUE; }
1779 ON event TO event_object where_clause
1780 DO opt_instead OptStmtList
1782 RuleStmt *n = makeNode(RuleStmt);
1786 n->whereClause = $10;
1793 OptStmtList: NOTHING { $$ = NIL; }
1794 | OptimizableStmt { $$ = lcons($1, NIL); }
1795 | '[' OptStmtBlock ']' { $$ = $2; }
1798 OptStmtBlock: OptStmtMulti
1801 { $$ = lcons($1, NIL); }
1804 OptStmtMulti: OptStmtMulti OptimizableStmt ';'
1805 { $$ = lappend($1, $2); }
1806 | OptStmtMulti OptimizableStmt
1807 { $$ = lappend($1, $2); }
1808 | OptimizableStmt ';'
1809 { $$ = lcons($1, NIL); }
1812 event_object: relation_name '.' attr_name
1814 $$ = makeNode(Attr);
1817 $$->attrs = lcons(makeString($3), NIL);
1818 $$->indirection = NIL;
1822 $$ = makeNode(Attr);
1826 $$->indirection = NIL;
1830 /* change me to select, update, etc. some day */
1831 event: SELECT { $$ = CMD_SELECT; }
1832 | UPDATE { $$ = CMD_UPDATE; }
1833 | DELETE { $$ = CMD_DELETE; }
1834 | INSERT { $$ = CMD_INSERT; }
1837 opt_instead: INSTEAD { $$ = TRUE; }
1838 | /*EMPTY*/ { $$ = FALSE; }
1842 /*****************************************************************************
1845 * NOTIFY <relation_name> can appear both in rule bodies and
1846 * as a query-level command
1848 *****************************************************************************/
1850 NotifyStmt: NOTIFY relation_name
1852 NotifyStmt *n = makeNode(NotifyStmt);
1858 ListenStmt: LISTEN relation_name
1860 ListenStmt *n = makeNode(ListenStmt);
1867 /*****************************************************************************
1878 *****************************************************************************/
1880 TransactionStmt: ABORT_TRANS TRANSACTION
1882 TransactionStmt *n = makeNode(TransactionStmt);
1883 n->command = ABORT_TRANS;
1886 | BEGIN_TRANS TRANSACTION
1888 TransactionStmt *n = makeNode(TransactionStmt);
1889 n->command = BEGIN_TRANS;
1894 TransactionStmt *n = makeNode(TransactionStmt);
1895 n->command = BEGIN_TRANS;
1900 TransactionStmt *n = makeNode(TransactionStmt);
1901 n->command = END_TRANS;
1904 | END_TRANS TRANSACTION
1906 TransactionStmt *n = makeNode(TransactionStmt);
1907 n->command = END_TRANS;
1912 TransactionStmt *n = makeNode(TransactionStmt);
1913 n->command = ABORT_TRANS;
1919 TransactionStmt *n = makeNode(TransactionStmt);
1920 n->command = ABORT_TRANS;
1925 TransactionStmt *n = makeNode(TransactionStmt);
1926 n->command = BEGIN_TRANS;
1931 TransactionStmt *n = makeNode(TransactionStmt);
1932 n->command = END_TRANS;
1938 TransactionStmt *n = makeNode(TransactionStmt);
1939 n->command = END_TRANS;
1944 TransactionStmt *n = makeNode(TransactionStmt);
1945 n->command = ABORT_TRANS;
1951 /*****************************************************************************
1954 * define view <viewname> '('target-list ')' [where <quals> ]
1956 *****************************************************************************/
1958 ViewStmt: CREATE VIEW name AS SelectStmt
1960 ViewStmt *n = makeNode(ViewStmt);
1962 n->query = (Query *)$5;
1963 if (((SelectStmt *)n->query)->unionClause != NULL)
1964 elog(ERROR,"Views on unions not implemented.");
1970 /*****************************************************************************
1975 *****************************************************************************/
1977 LoadStmt: LOAD file_name
1979 LoadStmt *n = makeNode(LoadStmt);
1986 /*****************************************************************************
1991 *****************************************************************************/
1993 CreatedbStmt: CREATE DATABASE database_name opt_database
1995 CreatedbStmt *n = makeNode(CreatedbStmt);
2002 opt_database: WITH LOCATION '=' location { $$ = $4; }
2003 | /*EMPTY*/ { $$ = NULL; }
2006 location: Sconst { $$ = $1; }
2007 | DEFAULT { $$ = NULL; }
2008 | /*EMPTY*/ { $$ = NULL; }
2011 /*****************************************************************************
2016 *****************************************************************************/
2018 DestroydbStmt: DROP DATABASE database_name
2020 DestroydbStmt *n = makeNode(DestroydbStmt);
2027 /*****************************************************************************
2030 * cluster <index_name> on <relation_name>
2032 *****************************************************************************/
2034 ClusterStmt: CLUSTER index_name ON relation_name
2036 ClusterStmt *n = makeNode(ClusterStmt);
2044 /*****************************************************************************
2049 *****************************************************************************/
2051 VacuumStmt: VACUUM opt_verbose opt_analyze
2053 VacuumStmt *n = makeNode(VacuumStmt);
2060 | VACUUM opt_verbose opt_analyze relation_name opt_va_list
2062 VacuumStmt *n = makeNode(VacuumStmt);
2067 if ( $5 != NIL && !$4 )
2068 elog(ERROR,"parser: syntax error at or near \"(\"");
2073 opt_verbose: VERBOSE { $$ = TRUE; }
2074 | /*EMPTY*/ { $$ = FALSE; }
2077 opt_analyze: ANALYZE { $$ = TRUE; }
2078 | /*EMPTY*/ { $$ = FALSE; }
2081 opt_va_list: '(' va_list ')'
2088 { $$=lcons($1,NIL); }
2090 { $$=lappend($1,$3); }
2094 /*****************************************************************************
2099 *****************************************************************************/
2101 ExplainStmt: EXPLAIN opt_verbose OptimizableStmt
2103 ExplainStmt *n = makeNode(ExplainStmt);
2105 n->query = (Query*)$3;
2111 /*****************************************************************************
2113 * Optimizable Stmts: *
2115 * one of the five queries processed by the planner *
2117 * [ultimately] produces query-trees as specified *
2118 * in the query-spec document in ~postgres/ref *
2120 *****************************************************************************/
2122 OptimizableStmt: SelectStmt
2127 | DeleteStmt /* by default all are $$=$1 */
2131 /*****************************************************************************
2136 *****************************************************************************/
2138 InsertStmt: INSERT INTO relation_name opt_column_list insert_rest
2146 insert_rest: VALUES '(' res_target_list2 ')'
2148 $$ = makeNode(InsertStmt);
2150 $$->targetList = $3;
2151 $$->fromClause = NIL;
2152 $$->whereClause = NIL;
2153 $$->groupClause = NIL;
2154 $$->havingClause = NIL;
2155 $$->unionClause = NIL;
2157 | SELECT opt_unique res_target_list2
2158 from_clause where_clause
2159 group_clause having_clause
2162 $$ = makeNode(InsertStmt);
2164 $$->targetList = $3;
2165 $$->fromClause = $4;
2166 $$->whereClause = $5;
2167 $$->groupClause = $6;
2168 $$->havingClause = $7;
2169 $$->unionClause = $8;
2173 opt_column_list: '(' columnList ')' { $$ = $2; }
2174 | /*EMPTY*/ { $$ = NIL; }
2178 columnList ',' columnElem
2179 { $$ = lappend($1, $3); }
2181 { $$ = lcons($1, NIL); }
2184 columnElem: ColId opt_indirection
2186 Ident *id = makeNode(Ident);
2188 id->indirection = $2;
2194 /*****************************************************************************
2199 *****************************************************************************/
2201 DeleteStmt: DELETE FROM relation_name
2204 DeleteStmt *n = makeNode(DeleteStmt);
2206 n->whereClause = $4;
2212 /*****************************************************************************
2215 * UpdateStmt (UPDATE)
2217 *****************************************************************************/
2219 UpdateStmt: UPDATE relation_name
2224 UpdateStmt *n = makeNode(UpdateStmt);
2228 n->whereClause = $6;
2234 /*****************************************************************************
2239 *****************************************************************************/
2240 CursorStmt: DECLARE name opt_binary CURSOR FOR
2241 SELECT opt_unique res_target_list2
2242 from_clause where_clause
2243 group_clause having_clause
2244 union_clause sort_clause
2246 SelectStmt *n = makeNode(SelectStmt);
2248 /* from PORTAL name */
2250 * 15 august 1991 -- since 3.0 postgres does locking
2251 * right, we discovered that portals were violating
2252 * locking protocol. portal locks cannot span xacts.
2253 * as a short-term fix, we installed the check here.
2256 if (!IsTransactionBlock())
2257 elog(ERROR,"Named portals may only be used in begin/end transaction blocks");
2264 n->whereClause = $10;
2265 n->groupClause = $11;
2266 n->havingClause = $12;
2267 n->unionClause = $13;
2268 n->sortClause = $14;
2274 /*****************************************************************************
2279 *****************************************************************************/
2281 SelectStmt: SELECT opt_unique res_target_list2
2282 result from_clause where_clause
2283 group_clause having_clause
2284 union_clause sort_clause
2286 SelectStmt *n = makeNode(SelectStmt);
2291 n->whereClause = $6;
2292 n->groupClause = $7;
2293 n->havingClause = $8;
2294 n->unionClause = $9;
2295 n->sortClause = $10;
2300 union_clause: UNION opt_union select_list
2302 SelectStmt *n = (SelectStmt *)lfirst($3);
2310 select_list: select_list UNION opt_union SubSelect
2312 SelectStmt *n = (SelectStmt *)$4;
2314 $$ = lappend($1, $4);
2317 { $$ = lcons($1, NIL); }
2320 SubSelect: SELECT opt_unique res_target_list2
2321 from_clause where_clause
2322 group_clause having_clause
2324 SelectStmt *n = makeNode(SelectStmt);
2326 n->unionall = FALSE;
2329 n->whereClause = $5;
2330 n->groupClause = $6;
2331 n->havingClause = $7;
2336 result: INTO TABLE relation_name
2342 opt_union: ALL { $$ = TRUE; }
2343 | /*EMPTY*/ { $$ = FALSE; }
2346 opt_unique: DISTINCT { $$ = "*"; }
2347 | DISTINCT ON ColId { $$ = $3; }
2348 | ALL { $$ = NULL; }
2349 | /*EMPTY*/ { $$ = NULL; }
2352 sort_clause: ORDER BY sortby_list { $$ = $3; }
2353 | /*EMPTY*/ { $$ = NIL; }
2356 sortby_list: sortby { $$ = lcons($1, NIL); }
2357 | sortby_list ',' sortby { $$ = lappend($1, $3); }
2360 sortby: ColId OptUseOp
2362 $$ = makeNode(SortGroupBy);
2368 | ColId '.' ColId OptUseOp
2370 $$ = makeNode(SortGroupBy);
2378 $$ = makeNode(SortGroupBy);
2386 OptUseOp: USING Op { $$ = $2; }
2387 | USING '<' { $$ = "<"; }
2388 | USING '>' { $$ = ">"; }
2390 | DESC { $$ = ">"; }
2391 | /*EMPTY*/ { $$ = "<"; /*default*/ }
2395 * jimmy bell-style recursive queries aren't supported in the
2398 * ...however, recursive addattr and rename supported. make special
2401 opt_inh_star: '*' { $$ = TRUE; }
2402 | /*EMPTY*/ { $$ = FALSE; }
2405 relation_name_list: name_list;
2408 { $$ = lcons(makeString($1),NIL); }
2409 | name_list ',' name
2410 { $$ = lappend($1,makeString($3)); }
2413 group_clause: GROUP BY groupby_list { $$ = $3; }
2414 | /*EMPTY*/ { $$ = NIL; }
2417 groupby_list: groupby { $$ = lcons($1, NIL); }
2418 | groupby_list ',' groupby { $$ = lappend($1, $3); }
2423 $$ = makeNode(SortGroupBy);
2431 $$ = makeNode(SortGroupBy);
2439 $$ = makeNode(SortGroupBy);
2447 having_clause: HAVING a_expr { $$ = $2; }
2448 | /*EMPTY*/ { $$ = NULL; }
2452 /*****************************************************************************
2454 * clauses common to all Optimizable Stmts:
2458 *****************************************************************************/
2460 from_clause: FROM '(' relation_expr join_expr JOIN relation_expr join_spec ')'
2463 elog(ERROR,"JOIN not yet implemented");
2465 | FROM from_list { $$ = $2; }
2466 | /*EMPTY*/ { $$ = NIL; }
2469 from_list: from_list ',' from_val
2470 { $$ = lappend($1, $3); }
2471 | from_val CROSS JOIN from_val
2472 { elog(ERROR,"CROSS JOIN not yet implemented"); }
2474 { $$ = lcons($1, NIL); }
2477 from_val: relation_expr AS ColLabel
2479 $$ = makeNode(RangeVar);
2483 | relation_expr ColId
2485 $$ = makeNode(RangeVar);
2491 $$ = makeNode(RangeVar);
2497 join_expr: NATURAL join_expr { $$ = NULL; }
2499 { elog(ERROR,"FULL OUTER JOIN not yet implemented"); }
2501 { elog(ERROR,"LEFT OUTER JOIN not yet implemented"); }
2503 { elog(ERROR,"RIGHT OUTER JOIN not yet implemented"); }
2505 { elog(ERROR,"OUTER JOIN not yet implemented"); }
2507 { elog(ERROR,"INNER JOIN not yet implemented"); }
2509 { elog(ERROR,"UNION JOIN not yet implemented"); }
2511 { elog(ERROR,"INNER JOIN not yet implemented"); }
2514 join_outer: OUTER_P { $$ = NULL; }
2515 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2518 join_spec: ON '(' a_expr ')' { $$ = NULL; }
2519 | USING '(' join_list ')' { $$ = NULL; }
2520 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2523 join_list: join_using { $$ = lcons($1, NIL); }
2524 | join_list ',' join_using { $$ = lappend($1, $3); }
2529 $$ = makeNode(SortGroupBy);
2537 $$ = makeNode(SortGroupBy);
2545 $$ = makeNode(SortGroupBy);
2553 where_clause: WHERE a_expr { $$ = $2; }
2554 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2557 relation_expr: relation_name
2559 /* normal relations */
2560 $$ = makeNode(RelExpr);
2564 | relation_name '*' %prec '='
2566 /* inheritance query */
2567 $$ = makeNode(RelExpr);
2572 opt_array_bounds: '[' ']' nest_array_bounds
2573 { $$ = lcons(makeInteger(-1), $3); }
2574 | '[' Iconst ']' nest_array_bounds
2575 { $$ = lcons(makeInteger($2), $4); }
2580 nest_array_bounds: '[' ']' nest_array_bounds
2581 { $$ = lcons(makeInteger(-1), $3); }
2582 | '[' Iconst ']' nest_array_bounds
2583 { $$ = lcons(makeInteger($2), $4); }
2589 /*****************************************************************************
2592 * SQL92 introduces a large amount of type-specific syntax.
2593 * Define individual clauses to handle these cases, and use
2594 * the generic case to handle regular type-extensible Postgres syntax.
2595 * - thomas 1997-10-10
2597 *****************************************************************************/
2599 Typename: Array opt_array_bounds
2602 $$->arrayBounds = $2;
2604 /* Is this the name of a complex type? If so, implement
2607 if (!strcmp(saved_relname, $$->name))
2608 /* This attr is the same type as the relation
2609 * being defined. The classic example: create
2610 * emp(name=text,mgr=emp)
2613 else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
2614 /* (Eventually add in here that the set can only
2615 * contain one element.)
2636 $$ = makeNode(TypeName);
2637 $$->name = xlateSqlType($1);
2641 generic: Id { $$ = $1; }
2642 | TYPE_P { $$ = xlateSqlType("type"); }
2643 | DOUBLE PRECISION { $$ = xlateSqlType("float8"); }
2646 /* SQL92 numeric data types
2647 * Check FLOAT() precision limits assuming IEEE floating types.
2648 * Provide rudimentary DECIMAL() and NUMERIC() implementations
2649 * by checking parameters and making sure they match what is possible with INTEGER.
2650 * - thomas 1997-09-18
2652 Numeric: FLOAT opt_float
2654 $$ = makeNode(TypeName);
2655 $$->name = xlateSqlType($2);
2657 | DECIMAL opt_decimal
2659 $$ = makeNode(TypeName);
2660 $$->name = xlateSqlType("integer");
2662 | NUMERIC opt_numeric
2664 $$ = makeNode(TypeName);
2665 $$->name = xlateSqlType("integer");
2669 opt_float: '(' Iconst ')'
2672 elog(ERROR,"precision for FLOAT must be at least 1");
2674 $$ = xlateSqlType("float4");
2676 $$ = xlateSqlType("float8");
2678 elog(ERROR,"precision for FLOAT must be less than 16");
2682 $$ = xlateSqlType("float8");
2686 opt_numeric: '(' Iconst ',' Iconst ')'
2689 elog(ERROR,"NUMERIC precision %d must be 9",$2);
2691 elog(ERROR,"NUMERIC scale %d must be zero",$4);
2696 elog(ERROR,"NUMERIC precision %d must be 9",$2);
2704 opt_decimal: '(' Iconst ',' Iconst ')'
2707 elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2709 elog(ERROR,"DECIMAL scale %d must be zero",$4);
2715 elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2724 /* SQL92 character data types
2725 * The following implements CHAR() and VARCHAR().
2726 * We do it here instead of the 'Generic' production
2727 * because we don't want to allow arrays of VARCHAR().
2728 * I haven't thought about whether that will work or not.
2731 Character: character '(' Iconst ')'
2733 $$ = makeNode(TypeName);
2734 if (!strcasecmp($1, "char"))
2735 $$->name = xlateSqlType("bpchar");
2736 else if (!strcasecmp($1, "varchar"))
2737 $$->name = xlateSqlType("varchar");
2739 yyerror("parse error");
2741 elog(ERROR,"length for '%s' type must be at least 1",$1);
2743 /* we can store a char() of length up to the size
2744 * of a page (8KB) - page headers and friends but
2745 * just to be safe here... - ay 6/95
2746 * XXX note this hardcoded limit - thomas 1997-07-13
2748 elog(ERROR,"length for type '%s' cannot exceed 4096",$1);
2750 /* we actually implement this sort of like a varlen, so
2751 * the first 4 bytes is the length. (the difference
2752 * between this and "text" is that we blank-pad and
2753 * truncate where necessary
2755 $$->typlen = VARHDRSZ + $3;
2759 $$ = makeNode(TypeName);
2760 $$->name = xlateSqlType($1);
2764 character: CHARACTER opt_varying opt_charset opt_collate
2767 if (($3 == NULL) || (strcasecmp($3, "sql_text") == 0)) {
2768 if ($2) type = xlateSqlType("varchar");
2769 else type = xlateSqlType("char");
2772 c = palloc(strlen("var") + strlen($3) + 1);
2775 type = xlateSqlType(c);
2777 type = xlateSqlType($3);
2781 elog(ERROR,"COLLATE %s not yet implemented",$4);
2784 | CHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
2785 | VARCHAR { $$ = xlateSqlType("varchar"); }
2786 | NATIONAL CHARACTER opt_varying { $$ = xlateSqlType($3? "varchar": "char"); }
2787 | NCHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
2790 opt_varying: VARYING { $$ = TRUE; }
2791 | /*EMPTY*/ { $$ = FALSE; }
2794 opt_charset: CHARACTER SET ColId { $$ = $3; }
2795 | /*EMPTY*/ { $$ = NULL; }
2798 opt_collate: COLLATE ColId { $$ = $2; }
2799 | /*EMPTY*/ { $$ = NULL; }
2804 $$ = makeNode(TypeName);
2805 $$->name = xlateSqlType($1);
2807 | TIMESTAMP opt_timezone
2809 $$ = makeNode(TypeName);
2810 $$->name = xlateSqlType("timestamp");
2815 $$ = makeNode(TypeName);
2816 $$->name = xlateSqlType("time");
2818 | INTERVAL opt_interval
2820 $$ = makeNode(TypeName);
2821 $$->name = xlateSqlType("interval");
2825 datetime: YEAR_P { $$ = "year"; }
2826 | MONTH_P { $$ = "month"; }
2827 | DAY_P { $$ = "day"; }
2828 | HOUR_P { $$ = "hour"; }
2829 | MINUTE_P { $$ = "minute"; }
2830 | SECOND_P { $$ = "second"; }
2833 opt_timezone: WITH TIME ZONE { $$ = TRUE; }
2834 | /*EMPTY*/ { $$ = FALSE; }
2837 opt_interval: datetime { $$ = lcons($1, NIL); }
2838 | YEAR_P TO MONTH_P { $$ = NIL; }
2839 | DAY_P TO HOUR_P { $$ = NIL; }
2840 | DAY_P TO MINUTE_P { $$ = NIL; }
2841 | DAY_P TO SECOND_P { $$ = NIL; }
2842 | HOUR_P TO MINUTE_P { $$ = NIL; }
2843 | HOUR_P TO SECOND_P { $$ = NIL; }
2844 | /*EMPTY*/ { $$ = NIL; }
2848 /*****************************************************************************
2850 * expression grammar, still needs some cleanup
2852 *****************************************************************************/
2854 a_expr_or_null: a_expr
2858 A_Const *n = makeNode(A_Const);
2859 n->val.type = T_Null;
2864 /* Expressions using row descriptors
2865 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
2866 * with singleton expressions.
2868 row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
2872 | '(' row_descriptor ')' NOT IN '(' SubSelect ')'
2876 | '(' row_descriptor ')' '=' '(' row_descriptor ')'
2878 $$ = makeRowExpr("=", $2, $6);
2880 | '(' row_descriptor ')' '<' '(' row_descriptor ')'
2882 $$ = makeRowExpr("<", $2, $6);
2884 | '(' row_descriptor ')' '>' '(' row_descriptor ')'
2886 $$ = makeRowExpr("<", $2, $6);
2888 | '(' row_descriptor ')' Op '(' row_descriptor ')'
2890 $$ = makeRowExpr($4, $2, $6);
2894 row_descriptor: row_list ',' a_expr
2896 $$ = lappend($1, $3);
2900 row_list: row_list ',' a_expr
2902 $$ = lappend($1, $3);
2906 $$ = lcons($1, NIL);
2910 a_expr: attr opt_indirection
2912 $1->indirection = $2;
2919 | '-' a_expr %prec UMINUS
2920 { $$ = makeA_Expr(OP, "-", NULL, $2); }
2922 { $$ = makeA_Expr(OP, "+", $1, $3); }
2924 { $$ = makeA_Expr(OP, "-", $1, $3); }
2926 { $$ = makeA_Expr(OP, "/", $1, $3); }
2928 { $$ = makeA_Expr(OP, "*", $1, $3); }
2930 { $$ = makeA_Expr(OP, "<", $1, $3); }
2932 { $$ = makeA_Expr(OP, ">", $1, $3); }
2934 { $$ = makeA_Expr(OP, "=", $1, $3); }
2936 { $$ = makeA_Expr(OP, ":", NULL, $2); }
2938 { $$ = makeA_Expr(OP, ";", NULL, $2); }
2940 { $$ = makeA_Expr(OP, "|", NULL, $2); }
2941 | a_expr TYPECAST Typename
2944 /* AexprConst can be either A_Const or ParamNo */
2945 if (nodeTag($1) == T_A_Const) {
2946 ((A_Const *)$1)->typename = $3;
2947 } else if (nodeTag($1) == T_Param) {
2948 ((ParamNo *)$1)->typename = $3;
2949 /* otherwise, try to transform to a function call */
2951 FuncCall *n = makeNode(FuncCall);
2952 n->funcname = $3->name;
2953 n->args = lcons($1,NIL);
2957 | CAST a_expr AS Typename
2960 /* AexprConst can be either A_Const or ParamNo */
2961 if (nodeTag($2) == T_A_Const) {
2962 ((A_Const *)$2)->typename = $4;
2963 } else if (nodeTag($2) == T_Param) {
2964 ((ParamNo *)$2)->typename = $4;
2965 /* otherwise, try to transform to a function call */
2967 FuncCall *n = makeNode(FuncCall);
2968 n->funcname = $4->name;
2969 n->args = lcons($2,NIL);
2973 | '(' a_expr_or_null ')'
2976 { $$ = makeIndexable($2,$1,$3); }
2977 | a_expr LIKE a_expr
2978 { $$ = makeIndexable("~~", $1, $3); }
2979 | a_expr NOT LIKE a_expr
2980 { $$ = makeA_Expr(OP, "!~~", $1, $4); }
2982 { $$ = makeA_Expr(OP, $1, NULL, $2); }
2984 { $$ = makeA_Expr(OP, $2, $1, NULL); }
2987 /* could be a column name or a relation_name */
2988 Ident *n = makeNode(Ident);
2990 n->indirection = NULL;
2995 /* cheap hack for aggregate (eg. count) */
2996 FuncCall *n = makeNode(FuncCall);
2997 A_Const *star = makeNode(A_Const);
2999 star->val.type = T_String;
3000 star->val.val.str = "";
3002 n->args = lcons(star, NIL);
3007 FuncCall *n = makeNode(FuncCall);
3012 | name '(' expr_list ')'
3014 FuncCall *n = makeNode(FuncCall);
3021 A_Const *n = makeNode(A_Const);
3022 TypeName *t = makeNode(TypeName);
3024 n->val.type = T_String;
3025 n->val.val.str = "now";
3028 t->name = xlateSqlType("date");
3035 A_Const *n = makeNode(A_Const);
3036 TypeName *t = makeNode(TypeName);
3038 n->val.type = T_String;
3039 n->val.val.str = "now";
3042 t->name = xlateSqlType("time");
3047 | CURRENT_TIME '(' Iconst ')'
3049 FuncCall *n = makeNode(FuncCall);
3050 A_Const *s = makeNode(A_Const);
3051 TypeName *t = makeNode(TypeName);
3053 n->funcname = xlateSqlType("time");
3054 n->args = lcons(s, NIL);
3056 s->val.type = T_String;
3057 s->val.val.str = "now";
3060 t->name = xlateSqlType("time");
3064 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3070 A_Const *n = makeNode(A_Const);
3071 TypeName *t = makeNode(TypeName);
3073 n->val.type = T_String;
3074 n->val.val.str = "now";
3077 t->name = xlateSqlType("timestamp");
3082 | CURRENT_TIMESTAMP '(' Iconst ')'
3084 FuncCall *n = makeNode(FuncCall);
3085 A_Const *s = makeNode(A_Const);
3086 TypeName *t = makeNode(TypeName);
3088 n->funcname = xlateSqlType("timestamp");
3089 n->args = lcons(s, NIL);
3091 s->val.type = T_String;
3092 s->val.val.str = "now";
3095 t->name = xlateSqlType("timestamp");
3099 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
3105 FuncCall *n = makeNode(FuncCall);
3106 n->funcname = "getpgusername";
3110 /* We probably need to define an "exists" node,
3111 * since the optimizer could choose to find only one match.
3112 * Perhaps the first implementation could just check for
3113 * count(*) > 0? - thomas 1997-07-19
3115 | EXISTS '(' SubSelect ')'
3117 elog(ERROR,"EXISTS not yet implemented");
3120 | EXTRACT '(' extract_list ')'
3122 FuncCall *n = makeNode(FuncCall);
3123 n->funcname = "date_part";
3127 | POSITION '(' position_list ')'
3129 FuncCall *n = makeNode(FuncCall);
3130 n->funcname = "strpos";
3134 | SUBSTRING '(' substr_list ')'
3136 FuncCall *n = makeNode(FuncCall);
3137 n->funcname = "substr";
3141 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
3142 | TRIM '(' BOTH trim_list ')'
3144 FuncCall *n = makeNode(FuncCall);
3145 n->funcname = "btrim";
3149 | TRIM '(' LEADING trim_list ')'
3151 FuncCall *n = makeNode(FuncCall);
3152 n->funcname = "ltrim";
3156 | TRIM '(' TRAILING trim_list ')'
3158 FuncCall *n = makeNode(FuncCall);
3159 n->funcname = "rtrim";
3163 | TRIM '(' trim_list ')'
3165 FuncCall *n = makeNode(FuncCall);
3166 n->funcname = "btrim";
3171 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3173 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3175 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3176 | a_expr IS NOT NULL_P
3177 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3178 /* IS TRUE, IS FALSE, etc used to be function calls
3179 * but let's make them expressions to allow the optimizer
3180 * a chance to eliminate them if a_expr is a constant string.
3181 * - thomas 1997-12-22
3185 A_Const *n = makeNode(A_Const);
3186 n->val.type = T_String;
3187 n->val.val.str = "t";
3188 n->typename = makeNode(TypeName);
3189 n->typename->name = xlateSqlType("bool");
3190 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3192 | a_expr IS NOT FALSE_P
3194 A_Const *n = makeNode(A_Const);
3195 n->val.type = T_String;
3196 n->val.val.str = "t";
3197 n->typename = makeNode(TypeName);
3198 n->typename->name = xlateSqlType("bool");
3199 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3203 A_Const *n = makeNode(A_Const);
3204 n->val.type = T_String;
3205 n->val.val.str = "f";
3206 n->typename = makeNode(TypeName);
3207 n->typename->name = xlateSqlType("bool");
3208 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3210 | a_expr IS NOT TRUE_P
3212 A_Const *n = makeNode(A_Const);
3213 n->val.type = T_String;
3214 n->val.val.str = "f";
3215 n->typename = makeNode(TypeName);
3216 n->typename->name = xlateSqlType("bool");
3217 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3219 | a_expr BETWEEN AexprConst AND AexprConst
3221 $$ = makeA_Expr(AND, NULL,
3222 makeA_Expr(OP, ">=", $1, $3),
3223 makeA_Expr(OP, "<=", $1, $5));
3225 | a_expr NOT BETWEEN AexprConst AND AexprConst
3227 $$ = makeA_Expr(OR, NULL,
3228 makeA_Expr(OP, "<", $1, $4),
3229 makeA_Expr(OP, ">", $1, $6));
3231 | a_expr IN { saved_In_Expr = $1; } '(' in_expr ')'
3233 | a_expr NOT IN { saved_In_Expr = $1; } '(' not_in_expr ')'
3236 { $$ = makeA_Expr(AND, NULL, $1, $3); }
3238 { $$ = makeA_Expr(OR, NULL, $1, $3); }
3240 { $$ = makeA_Expr(NOT, NULL, NULL, $2); }
3243 opt_indirection: '[' a_expr ']' opt_indirection
3245 A_Indices *ai = makeNode(A_Indices);
3250 | '[' a_expr ':' a_expr ']' opt_indirection
3252 A_Indices *ai = makeNode(A_Indices);
3261 expr_list: a_expr_or_null
3262 { $$ = lcons($1, NIL); }
3263 | expr_list ',' a_expr_or_null
3264 { $$ = lappend($1, $3); }
3265 | expr_list USING a_expr
3266 { $$ = lappend($1, $3); }
3269 extract_list: datetime FROM a_expr
3271 A_Const *n = makeNode(A_Const);
3272 n->val.type = T_String;
3273 n->val.val.str = $1;
3274 $$ = lappend(lcons((Node *)n,NIL), $3);
3280 position_list: position_expr IN position_expr
3281 { $$ = makeList($3, $1, -1); }
3286 position_expr: attr opt_indirection
3288 $1->indirection = $2;
3293 | '-' position_expr %prec UMINUS
3294 { $$ = makeA_Expr(OP, "-", NULL, $2); }
3295 | position_expr '+' position_expr
3296 { $$ = makeA_Expr(OP, "+", $1, $3); }
3297 | position_expr '-' position_expr
3298 { $$ = makeA_Expr(OP, "-", $1, $3); }
3299 | position_expr '/' position_expr
3300 { $$ = makeA_Expr(OP, "/", $1, $3); }
3301 | position_expr '*' position_expr
3302 { $$ = makeA_Expr(OP, "*", $1, $3); }
3304 { $$ = makeA_Expr(OP, "|", NULL, $2); }
3305 | position_expr TYPECAST Typename
3308 /* AexprConst can be either A_Const or ParamNo */
3309 if (nodeTag($1) == T_A_Const) {
3310 ((A_Const *)$1)->typename = $3;
3311 } else if (nodeTag($1) == T_Param) {
3312 ((ParamNo *)$1)->typename = $3;
3313 /* otherwise, try to transform to a function call */
3315 FuncCall *n = makeNode(FuncCall);
3316 n->funcname = $3->name;
3317 n->args = lcons($1,NIL);
3321 | CAST position_expr AS Typename
3324 /* AexprConst can be either A_Const or ParamNo */
3325 if (nodeTag($2) == T_A_Const) {
3326 ((A_Const *)$2)->typename = $4;
3327 } else if (nodeTag($2) == T_Param) {
3328 ((ParamNo *)$2)->typename = $4;
3329 /* otherwise, try to transform to a function call */
3331 FuncCall *n = makeNode(FuncCall);
3332 n->funcname = $4->name;
3333 n->args = lcons($2,NIL);
3337 | '(' position_expr ')'
3339 | position_expr Op position_expr
3340 { $$ = makeA_Expr(OP, $2, $1, $3); }
3342 { $$ = makeA_Expr(OP, $1, NULL, $2); }
3344 { $$ = makeA_Expr(OP, $2, $1, NULL); }
3347 /* could be a column name or a relation_name */
3348 Ident *n = makeNode(Ident);
3350 n->indirection = NULL;
3355 FuncCall *n = makeNode(FuncCall);
3360 | name '(' expr_list ')'
3362 FuncCall *n = makeNode(FuncCall);
3367 | POSITION '(' position_list ')'
3369 FuncCall *n = makeNode(FuncCall);
3370 n->funcname = "strpos";
3374 | SUBSTRING '(' substr_list ')'
3376 FuncCall *n = makeNode(FuncCall);
3377 n->funcname = "substr";
3381 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
3382 | TRIM '(' BOTH trim_list ')'
3384 FuncCall *n = makeNode(FuncCall);
3385 n->funcname = "btrim";
3389 | TRIM '(' LEADING trim_list ')'
3391 FuncCall *n = makeNode(FuncCall);
3392 n->funcname = "ltrim";
3396 | TRIM '(' TRAILING trim_list ')'
3398 FuncCall *n = makeNode(FuncCall);
3399 n->funcname = "rtrim";
3403 | TRIM '(' trim_list ')'
3405 FuncCall *n = makeNode(FuncCall);
3406 n->funcname = "btrim";
3412 substr_list: expr_list substr_from substr_for
3414 $$ = nconc(nconc($1,$2),$3);
3420 substr_from: FROM expr_list
3424 A_Const *n = makeNode(A_Const);
3425 n->val.type = T_Integer;
3426 n->val.val.ival = 1;
3427 $$ = lcons((Node *)n,NIL);
3431 substr_for: FOR expr_list
3437 trim_list: a_expr FROM expr_list
3438 { $$ = lappend($3, $1); }
3447 elog(ERROR,"IN (SUBSELECT) not yet implemented");
3454 in_expr_nodes: AexprConst
3455 { $$ = makeA_Expr(OP, "=", saved_In_Expr, $1); }
3456 | in_expr_nodes ',' AexprConst
3457 { $$ = makeA_Expr(OR, NULL, $1,
3458 makeA_Expr(OP, "=", saved_In_Expr, $3));
3462 not_in_expr: SubSelect
3464 elog(ERROR,"NOT IN (SUBSELECT) not yet implemented");
3471 not_in_expr_nodes: AexprConst
3472 { $$ = makeA_Expr(OP, "<>", saved_In_Expr, $1); }
3473 | not_in_expr_nodes ',' AexprConst
3474 { $$ = makeA_Expr(AND, NULL, $1,
3475 makeA_Expr(OP, "<>", saved_In_Expr, $3));
3479 attr: relation_name '.' attrs
3481 $$ = makeNode(Attr);
3485 $$->indirection = NULL;
3489 $$ = makeNode(Attr);
3493 $$->indirection = NULL;
3498 { $$ = lcons(makeString($1), NIL); }
3499 | attrs '.' attr_name
3500 { $$ = lappend($1, makeString($3)); }
3502 { $$ = lappend($1, makeString("*")); }
3506 /*****************************************************************************
3510 *****************************************************************************/
3512 res_target_list: res_target_list ',' res_target_el
3513 { $$ = lappend($1,$3); }
3515 { $$ = lcons($1, NIL); }
3518 ResTarget *rt = makeNode(ResTarget);
3519 Attr *att = makeNode(Attr);
3521 att->paramNo = NULL;
3523 att->indirection = NIL;
3525 rt->indirection = NULL;
3526 rt->val = (Node *)att;
3527 $$ = lcons(rt, NIL);
3531 res_target_el: ColId opt_indirection '=' a_expr_or_null
3533 $$ = makeNode(ResTarget);
3535 $$->indirection = $2;
3536 $$->val = (Node *)$4;
3538 | attr opt_indirection
3540 $$ = makeNode(ResTarget);
3542 $$->indirection = $2;
3543 $$->val = (Node *)$1;
3545 | relation_name '.' '*'
3547 Attr *att = makeNode(Attr);
3549 att->paramNo = NULL;
3550 att->attrs = lcons(makeString("*"), NIL);
3551 att->indirection = NIL;
3552 $$ = makeNode(ResTarget);
3554 $$->indirection = NULL;
3555 $$->val = (Node *)att;
3560 ** target list for select.
3561 ** should get rid of the other but is still needed by the defunct retrieve into
3562 ** and update (uses a subset)
3564 res_target_list2: res_target_list2 ',' res_target_el2
3565 { $$ = lappend($1, $3); }
3567 { $$ = lcons($1, NIL); }
3570 /* AS is not optional because shift/red conflict with unary ops */
3571 res_target_el2: a_expr_or_null AS ColLabel
3573 $$ = makeNode(ResTarget);
3575 $$->indirection = NULL;
3576 $$->val = (Node *)$1;
3580 $$ = makeNode(ResTarget);
3582 $$->indirection = NULL;
3583 $$->val = (Node *)$1;
3585 | relation_name '.' '*'
3587 Attr *att = makeNode(Attr);
3589 att->paramNo = NULL;
3590 att->attrs = lcons(makeString("*"), NIL);
3591 att->indirection = NIL;
3592 $$ = makeNode(ResTarget);
3594 $$->indirection = NULL;
3595 $$->val = (Node *)att;
3599 Attr *att = makeNode(Attr);
3601 att->paramNo = NULL;
3603 att->indirection = NIL;
3604 $$ = makeNode(ResTarget);
3606 $$->indirection = NULL;
3607 $$->val = (Node *)att;
3611 opt_id: ColId { $$ = $1; }
3612 | /* EMPTY */ { $$ = NULL; }
3615 relation_name: SpecialRuleRelation
3618 StrNCpy(saved_relname, $1, NAMEDATALEN);
3622 /* disallow refs to variable system tables */
3623 if (strcmp(LogRelationName, $1) == 0
3624 || strcmp(VariableRelationName, $1) == 0)
3625 elog(ERROR,"%s cannot be accessed by users",$1);
3628 StrNCpy(saved_relname, $1, NAMEDATALEN);
3632 database_name: ColId { $$ = $1; };
3633 access_method: Id { $$ = $1; };
3634 attr_name: ColId { $$ = $1; };
3635 class: Id { $$ = $1; };
3636 index_name: ColId { $$ = $1; };
3639 * Include date/time keywords as SQL92 extension.
3640 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
3642 name: ColId { $$ = $1; };
3644 file_name: Sconst { $$ = $1; };
3645 recipe_name: Id { $$ = $1; };
3648 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
3652 A_Const *n = makeNode(A_Const);
3653 n->val.type = T_Integer;
3654 n->val.val.ival = $1;
3659 A_Const *n = makeNode(A_Const);
3660 n->val.type = T_Float;
3661 n->val.val.dval = $1;
3666 A_Const *n = makeNode(A_Const);
3667 n->val.type = T_String;
3668 n->val.val.str = $1;
3673 A_Const *n = makeNode(A_Const);
3675 n->val.type = T_String;
3676 n->val.val.str = $2;
3680 { $$ = (Node *)$1; }
3683 A_Const *n = makeNode(A_Const);
3684 n->val.type = T_String;
3685 n->val.val.str = "t";
3686 n->typename = makeNode(TypeName);
3687 n->typename->name = xlateSqlType("bool");
3692 A_Const *n = makeNode(A_Const);
3693 n->val.type = T_String;
3694 n->val.val.str = "f";
3695 n->typename = makeNode(TypeName);
3696 n->typename->name = xlateSqlType("bool");
3703 $$ = makeNode(ParamNo);
3708 NumConst: Iconst { $$ = makeInteger($1); }
3709 | FCONST { $$ = makeFloat($1); }
3712 Iconst: ICONST { $$ = $1; };
3713 Sconst: SCONST { $$ = $1; };
3715 /* Column and type identifier
3716 * Does not include explicit datetime types
3717 * since these must be decoupled in Typename syntax.
3718 * Use ColId for most identifiers. - thomas 1997-10-21
3720 Id: IDENT { $$ = $1; };
3722 /* Column identifier
3723 * Include date/time keywords as SQL92 extension.
3724 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
3725 * Add other keywords. Note that as the syntax expands,
3726 * some of these keywords will have to be removed from this
3727 * list due to shift/reduce conflicts in yacc. If so, move
3728 * down to the ColLabel entity. - thomas 1997-11-06
3730 ColId: Id { $$ = $1; }
3731 | datetime { $$ = $1; }
3732 | ACTION { $$ = "action"; }
3733 | DATABASE { $$ = "database"; }
3734 | DELIMITERS { $$ = "delimiters"; }
3735 | FUNCTION { $$ = "function"; }
3736 | INDEX { $$ = "index"; }
3737 | KEY { $$ = "key"; }
3738 | LANGUAGE { $$ = "language"; }
3739 | LOCATION { $$ = "location"; }
3740 | MATCH { $$ = "match"; }
3741 | OPERATOR { $$ = "operator"; }
3742 | OPTION { $$ = "option"; }
3743 | PRIVILEGES { $$ = "privileges"; }
3744 | RECIPE { $$ = "recipe"; }
3745 | TIME { $$ = "time"; }
3746 | TRIGGER { $$ = "trigger"; }
3747 | TYPE_P { $$ = "type"; }
3748 | VERSION { $$ = "version"; }
3749 | ZONE { $$ = "zone"; }
3753 * Allowed labels in "AS" clauses.
3754 * Include TRUE/FALSE SQL3 reserved words for Postgres backward
3755 * compatibility. Cannot allow this for column names since the
3756 * syntax would not distinguish between the constant value and
3757 * a column name. - thomas 1997-10-24
3758 * Add other keywords to this list. Note that they appear here
3759 * rather than in ColId if there was a shift/reduce conflict
3760 * when used as a full identifier. - thomas 1997-11-06
3762 ColLabel: ColId { $$ = $1; }
3763 | ARCHIVE { $$ = "archive"; }
3764 | CLUSTER { $$ = "cluster"; }
3765 | CONSTRAINT { $$ = "constraint"; }
3766 | CROSS { $$ = "cross"; }
3767 | FOREIGN { $$ = "foreign"; }
3768 | GROUP { $$ = "group"; }
3769 | LOAD { $$ = "load"; }
3770 | ORDER { $$ = "order"; }
3771 | POSITION { $$ = "position"; }
3772 | PRECISION { $$ = "precision"; }
3773 | TABLE { $$ = "table"; }
3774 | TRANSACTION { $$ = "transaction"; }
3775 | TRUE_P { $$ = "true"; }
3776 | FALSE_P { $$ = "false"; }
3779 SpecialRuleRelation: CURRENT
3784 elog(ERROR,"CURRENT used in non-rule query");
3791 elog(ERROR,"NEW used in non-rule query");
3798 makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
3800 A_Expr *a = makeNode(A_Expr);
3809 * Generate separate operator nodes for a single row descriptor expression.
3810 * Perhaps this should go deeper in the parser someday... - thomas 1997-12-22
3813 makeRowExpr(char *opr, List *largs, List *rargs)
3818 if (length(largs) != length(rargs))
3819 elog(ERROR,"Unequal number of entries in row expression");
3821 if (lnext(largs) != NIL)
3822 expr = makeRowExpr(opr,lnext(largs),lnext(rargs));
3824 larg = lfirst(largs);
3825 rarg = lfirst(rargs);
3827 if ((strcmp(opr, "=") == 0)
3828 || (strcmp(opr, "<") == 0)
3829 || (strcmp(opr, "<=") == 0)
3830 || (strcmp(opr, ">") == 0)
3831 || (strcmp(opr, ">=") == 0))
3834 expr = makeA_Expr(OP, opr, larg, rarg);
3836 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3838 else if (strcmp(opr, "<>") == 0)
3841 expr = makeA_Expr(OP, opr, larg, rarg);
3843 expr = makeA_Expr(OR, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3847 elog(ERROR,"Operator '%s' not implemented for row expressions",opr);
3851 while ((largs != NIL) && (rargs != NIL))
3853 larg = lfirst(largs);
3854 rarg = lfirst(rargs);
3857 expr = makeA_Expr(OP, opr, larg, rarg);
3859 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3861 largs = lnext(largs);
3862 rargs = lnext(rargs);
3868 } /* makeRowExpr() */
3871 mapTargetColumns(List *src, List *dst)
3876 if (length(src) != length(dst))
3877 elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
3879 while ((src != NIL) && (dst != NIL))
3881 s = (ColumnDef *)lfirst(src);
3882 d = (ResTarget *)lfirst(dst);
3884 d->name = s->colname;
3891 } /* mapTargetColumns() */
3893 static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr)
3895 Node *result = NULL;
3897 /* we do this so indexes can be used */
3898 if (strcmp(opname,"~") == 0 ||
3899 strcmp(opname,"~*") == 0)
3901 if (nodeTag(rexpr) == T_A_Const &&
3902 ((A_Const *)rexpr)->val.type == T_String &&
3903 ((A_Const *)rexpr)->val.val.str[0] == '^')
3905 A_Const *n = (A_Const *)rexpr;
3906 char *match_least = palloc(strlen(n->val.val.str)+2);
3907 char *match_most = palloc(strlen(n->val.val.str)+2);
3908 int pos, match_pos=0;
3910 /* skip leading ^ */
3911 for (pos = 1; n->val.val.str[pos]; pos++)
3913 if (n->val.val.str[pos] == '.' ||
3914 n->val.val.str[pos] == '?' ||
3915 n->val.val.str[pos] == '*' ||
3916 n->val.val.str[pos] == '[' ||
3917 n->val.val.str[pos] == '$' ||
3918 (strcmp(opname,"~*") == 0 && isalpha(n->val.val.str[pos])))
3920 if (n->val.val.str[pos] == '\\')
3922 match_least[match_pos] = n->val.val.str[pos];
3923 match_most[match_pos++] = n->val.val.str[pos];
3928 A_Const *least = makeNode(A_Const);
3929 A_Const *most = makeNode(A_Const);
3931 /* make strings to be used in index use */
3932 match_least[match_pos] = '\0';
3933 match_most[match_pos] = '\377';
3934 match_most[match_pos+1] = '\0';
3935 least->val.type = T_String;
3936 least->val.val.str = match_least;
3937 most->val.type = T_String;
3938 most->val.val.str = match_most;
3939 result = makeA_Expr(AND, NULL,
3940 makeA_Expr(OP, "~", lexpr, rexpr),
3941 makeA_Expr(AND, NULL,
3942 makeA_Expr(OP, ">=", lexpr, (Node *)least),
3943 makeA_Expr(OP, "<=", lexpr, (Node *)most)));
3947 else if (strcmp(opname,"~~") == 0)
3949 if (nodeTag(rexpr) == T_A_Const &&
3950 ((A_Const *)rexpr)->val.type == T_String)
3952 A_Const *n = (A_Const *)rexpr;
3953 char *match_least = palloc(strlen(n->val.val.str)+2);
3954 char *match_most = palloc(strlen(n->val.val.str)+2);
3955 int pos, match_pos=0;
3957 for (pos = 0; n->val.val.str[pos]; pos++)
3959 if ((n->val.val.str[pos] == '%' &&
3960 n->val.val.str[pos+1] != '%') ||
3961 (n->val.val.str[pos] == '_' &&
3962 n->val.val.str[pos+1] != '_'))
3964 if (n->val.val.str[pos] == '%' ||
3965 n->val.val.str[pos] == '_' ||
3966 n->val.val.str[pos] == '\\')
3968 match_least[match_pos] = n->val.val.str[pos];
3969 match_most[match_pos++] = n->val.val.str[pos];
3974 A_Const *least = makeNode(A_Const);
3975 A_Const *most = makeNode(A_Const);
3977 /* make strings to be used in index use */
3978 match_least[match_pos] = '\0';
3979 match_most[match_pos] = '\377';
3980 match_most[match_pos+1] = '\0';
3981 least->val.type = T_String;
3982 least->val.val.str = match_least;
3983 most->val.type = T_String;
3984 most->val.val.str = match_most;
3985 result = makeA_Expr(AND, NULL,
3986 makeA_Expr(OP, "~~", lexpr, rexpr),
3987 makeA_Expr(AND, NULL,
3988 makeA_Expr(OP, ">=", lexpr, (Node *)least),
3989 makeA_Expr(OP, "<=", lexpr, (Node *)most)));
3995 result = makeA_Expr(OP, opname, lexpr, rexpr);
3997 } /* makeIndexable() */
4001 * Convert alternate type names to internal Postgres types.
4002 * Do not convert "float", since that is handled elsewhere
4003 * for FLOAT(p) syntax.
4006 xlateSqlType(char *name)
4008 if (!strcasecmp(name,"int")
4009 || !strcasecmp(name,"integer"))
4011 else if (!strcasecmp(name, "smallint"))
4013 else if (!strcasecmp(name, "real"))
4015 else if (!strcasecmp(name, "interval"))
4017 else if (!strcasecmp(name, "boolean"))
4021 } /* xlateSqlName() */
4024 void parser_init(Oid *typev, int nargs)
4026 QueryIsRule = FALSE;
4027 saved_relname[0]= '\0';
4028 saved_In_Expr = NULL;
4030 param_type_init(typev, nargs);
4034 /* FlattenStringList()
4035 * Traverse list of string nodes and convert to a single string.
4036 * Used for reconstructing string form of complex expressions.
4038 * Allocate at least one byte for terminator.
4041 FlattenStringList(List *list)
4049 nlist = length(list);
4052 v = (Value *)lfirst(l);
4059 s = (char*) palloc(len+1);
4064 v = (Value *)lfirst(l);
4068 if (l != NIL) strcat(s," ");
4073 printf( "flattened string is \"%s\"\n", s);
4077 } /* FlattenStringList() */
4080 /* makeConstantList()
4081 * Convert constant value node into string node.
4084 makeConstantList( A_Const *n)
4086 char *defval = NULL;
4087 if (nodeTag(n) != T_A_Const) {
4088 elog(ERROR,"Cannot handle non-constant parameter");
4090 } else if (n->val.type == T_Float) {
4091 defval = (char*) palloc(20+1);
4092 sprintf( defval, "%g", n->val.val.dval);
4094 } else if (n->val.type == T_Integer) {
4095 defval = (char*) palloc(20+1);
4096 sprintf( defval, "%ld", n->val.val.ival);
4098 } else if (n->val.type == T_String) {
4099 defval = (char*) palloc(strlen( ((A_Const *) n)->val.val.str) + 3);
4100 strcpy( defval, "'");
4101 strcat( defval, ((A_Const *) n)->val.val.str);
4102 strcat( defval, "'");
4105 elog(ERROR,"Internal error in makeConstantList(): cannot encode node");
4109 printf( "AexprConst argument is \"%s\"\n", defval);
4112 return( lcons( makeString(defval), NIL));
4113 } /* makeConstantList() */
4117 * Check input string for non-lowercase/non-numeric characters.
4118 * Returns either input string or input surrounded by double quotes.
4125 for (cp = rawid; *cp != '\0'; cp++)
4126 if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break;
4129 cp = palloc(strlen(rawid)+1);
4138 printf("fmtId- %sconvert %s to %s\n", ((cp == rawid)? "do not ": ""), rawid, cp);
4147 * keep enough information around fill out the type of param nodes
4148 * used in postquel functions
4151 param_type_init(Oid *typev, int nargs)
4153 pfunc_num_args = nargs;
4154 param_type_info = typev;
4157 Oid param_type(int t)
4159 if ((t > pfunc_num_args) || (t == 0))
4161 return param_type_info[t - 1];