4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.324 2002/06/17 05:40:32 momjian Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled inside parse_analyze() so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
53 #include "access/htup.h"
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "catalog/pg_type.h"
57 #include "nodes/makefuncs.h"
58 #include "nodes/params.h"
59 #include "nodes/parsenodes.h"
60 #include "parser/gramparse.h"
61 #include "storage/lmgr.h"
62 #include "utils/numeric.h"
63 #include "utils/datetime.h"
64 #include "utils/date.h"
67 #include "mb/pg_wchar.h"
69 #define GetStandardEncoding() 0 /* PG_SQL_ASCII */
70 #define GetStandardEncodingName() "SQL_ASCII"
73 extern List *parsetree; /* final parse result is delivered here */
75 static bool QueryIsRule = FALSE;
76 static Oid *param_type_info;
77 static int pfunc_num_args;
81 * If you need access to certain yacc-generated variables and find that
82 * they're static by default, uncomment the next line. (this is not a
85 /*#define __YYSCLASS*/
87 static Node *makeTypeCast(Node *arg, TypeName *typename);
88 static Node *makeStringConst(char *str, TypeName *typename);
89 static Node *makeIntConst(int val);
90 static Node *makeFloatConst(char *str);
91 static Node *makeAConst(Value *v);
92 static Node *makeRowExpr(List *opr, List *largs, List *rargs);
93 static SelectStmt *findLeftmostSelect(SelectStmt *node);
94 static void insertSelectOptions(SelectStmt *stmt,
95 List *sortClause, List *forUpdate,
96 Node *limitOffset, Node *limitCount);
97 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
98 static Node *doNegate(Node *n);
99 static void doNegateFloat(Value *v);
101 #define MASK(b) (1 << (b))
117 ColumnRef *columnref;
121 SortGroupBy *sortgroupby;
128 PrivTarget *privtarget;
131 VariableSetStmt *vsetstmt;
134 %type <node> stmt, schema_stmt,
135 AlterDatabaseSetStmt, AlterGroupStmt, AlterSchemaStmt, AlterTableStmt,
136 AlterUserStmt, AlterUserSetStmt, AnalyzeStmt,
137 ClosePortalStmt, ClusterStmt, CommentStmt, ConstraintsSetStmt,
138 CopyStmt, CreateAsStmt, CreateDomainStmt, CreateGroupStmt, CreatePLangStmt,
139 CreateSchemaStmt, CreateSeqStmt, CreateStmt, CreateAssertStmt, CreateTrigStmt,
140 CreateUserStmt, CreatedbStmt, CursorStmt, DefineStmt, DeleteStmt,
141 DropGroupStmt, DropPLangStmt, DropSchemaStmt, DropStmt, DropAssertStmt, DropTrigStmt,
142 DropRuleStmt, DropUserStmt, DropdbStmt, ExplainStmt, FetchStmt,
143 GrantStmt, IndexStmt, InsertStmt, ListenStmt, LoadStmt, LockStmt,
144 NotifyStmt, OptimizableStmt, CreateFunctionStmt, ReindexStmt,
145 RemoveAggrStmt, RemoveFuncStmt, RemoveOperStmt,
146 RenameStmt, RevokeStmt, RuleActionStmt, RuleActionStmtOrEmpty,
147 RuleStmt, SelectStmt, TransactionStmt, TruncateStmt,
148 UnlistenStmt, UpdateStmt, VacuumStmt, VariableResetStmt,
149 VariableSetStmt, VariableShowStmt, ViewStmt, CheckPointStmt
151 %type <node> select_no_parens, select_with_parens, select_clause,
154 %type <node> alter_column_default
155 %type <ival> drop_behavior, opt_drop_behavior
157 %type <list> createdb_opt_list, createdb_opt_item
158 %type <boolean> opt_equal
160 %type <ival> opt_lock, lock_type
161 %type <boolean> opt_force, opt_or_replace
163 %type <list> user_list
165 %type <list> OptGroupList
166 %type <defelt> OptGroupElem
168 %type <list> OptUserList
169 %type <defelt> OptUserElem
171 %type <str> OptSchemaName
172 %type <list> OptSchemaEltList
174 %type <boolean> TriggerActionTime, TriggerForSpec, opt_trusted, opt_procedural
175 %type <str> opt_lancompiler
177 %type <str> TriggerEvents
178 %type <value> TriggerFuncArg
180 %type <str> relation_name, copy_file_name, copy_delimiter, copy_null,
181 database_name, access_method_clause, access_method, attr_name,
182 index_name, name, function_name, file_name
184 %type <list> func_name, handler_name, qual_Op, qual_all_Op, OptUseOp,
185 opt_class, opt_validator
187 %type <range> qualified_name, OptConstrFromTable
190 all_Op, MathOp, opt_name, SpecialRuleRelation
192 %type <str> opt_level, opt_encoding
194 %type <list> grantee_list
195 %type <ival> privilege
196 %type <list> privileges, privilege_list
197 %type <privtarget> privilege_target
198 %type <node> function_with_argtypes
199 %type <list> function_with_argtypes_list
200 %type <chr> TriggerOneEvent
202 %type <list> stmtblock, stmtmulti,
203 OptTableElementList, OptInherit, definition, opt_distinct,
204 opt_definition, func_args, func_args_list, func_as, createfunc_opt_list
205 oper_argtypes, RuleActionList, RuleActionMulti,
206 opt_column_list, columnList, opt_name_list,
207 sort_clause, sortby_list, index_params, index_list, name_list,
208 from_clause, from_list, opt_array_bounds, qualified_name_list,
209 any_name, any_name_list, any_operator, expr_list, dotted_name, attrs,
210 target_list, update_target_list, insert_column_list,
212 def_list, opt_indirection, group_clause, TriggerFuncArgs,
213 select_limit, opt_select_limit
215 %type <range> into_clause, OptTempTableName
217 %type <defelt> createfunc_opt_item
218 %type <typnam> func_arg, func_return, func_type, aggr_argtype
220 %type <boolean> opt_arg, TriggerForOpt, TriggerForType, OptTemp, OptWithOids
222 %type <list> for_update_clause, opt_for_update_clause, update_list
223 %type <boolean> opt_all
224 %type <boolean> opt_table
225 %type <boolean> opt_chain, opt_trans
227 %type <node> join_outer, join_qual
228 %type <jtype> join_type
230 %type <list> extract_list, overlay_list, position_list
231 %type <list> substr_list, trim_list
232 %type <ival> opt_interval
233 %type <node> overlay_placing, substr_from, substr_for
235 %type <boolean> opt_binary, opt_using, opt_instead, opt_cursor, opt_with
236 %type <boolean> opt_with_copy, index_opt_unique, opt_verbose, opt_full
237 %type <boolean> opt_freeze, analyze_keyword
239 %type <ival> copy_dirn, direction, reindex_type, drop_type,
240 opt_column, event, comment_type
242 %type <ival> fetch_how_many
244 %type <node> select_limit_value, select_offset_value
246 %type <list> OptSeqList
247 %type <defelt> OptSeqElem
249 %type <istmt> insert_rest
251 %type <vsetstmt> set_rest
253 %type <node> OptTableElement, ConstraintElem
254 %type <node> columnDef
255 %type <defelt> def_elem
256 %type <node> def_arg, columnElem, where_clause, insert_column_item,
257 a_expr, b_expr, c_expr, AexprConst,
258 in_expr, having_clause, func_table
259 %type <list> row_descriptor, row_list, in_expr_nodes
260 %type <node> row_expr
261 %type <node> case_expr, case_arg, when_clause, case_default
262 %type <list> when_clause_list
263 %type <ival> sub_type
264 %type <list> OptCreateAs, CreateAsList
265 %type <node> CreateAsElement
266 %type <value> NumericOnly, FloatOnly, IntegerOnly
267 %type <columnref> columnref
268 %type <alias> alias_clause
269 %type <sortgroupby> sortby
270 %type <ielem> index_elem, func_index
271 %type <node> table_ref
272 %type <jexpr> joined_table
273 %type <range> relation_expr
274 %type <target> target_el, insert_target_el, update_target_el
276 %type <typnam> Typename, SimpleTypename, ConstTypename,
277 GenericType, Numeric, opt_float, Character,
278 ConstDatetime, ConstInterval, Bit
279 %type <str> character
280 %type <str> extract_arg
281 %type <str> opt_charset, opt_collate
282 %type <ival> opt_numeric, opt_decimal
283 %type <boolean> opt_varying, opt_timezone
286 %type <str> Sconst, comment_text
287 %type <str> UserId, opt_boolean, ColId_or_Sconst
288 %type <list> var_list, var_list_or_default
289 %type <str> ColId, ColLabel, type_name
290 %type <node> var_value, zone_value
292 %type <keyword> unreserved_keyword, func_name_keyword
293 %type <keyword> col_name_keyword, reserved_keyword
295 %type <node> TableConstraint
296 %type <list> ColQualList
297 %type <node> ColConstraint, ColConstraintElem, ConstraintAttr
298 %type <ival> key_actions, key_delete, key_update, key_reference
299 %type <str> key_match
300 %type <ival> ConstraintAttributeSpec, ConstraintDeferrabilitySpec,
303 %type <list> constraints_set_list
304 %type <boolean> constraints_set_mode
306 %type <boolean> opt_as
310 * If you make any token changes, update the keyword table in
311 * parser/keywords.c and add new keywords to the appropriate one of
312 * the reserved-or-not-so-reserved keyword lists, below.
315 /* ordinary key words in alphabetical order */
316 %token <keyword> ABORT_TRANS, ABSOLUTE, ACCESS, ACTION, ADD, AFTER,
317 AGGREGATE, ALL, ALTER, ANALYSE, ANALYZE, AND, ANY, AS, ASC, ASSERTION,
320 BACKWARD, BEFORE, BEGIN_TRANS, BETWEEN, BIGINT, BINARY, BIT, BOTH,
323 CACHE, CALLED, CASCADE, CASE, CAST, CHAIN, CHAR_P, CHARACTER,
324 CHARACTERISTICS, CHECK, CHECKPOINT, CLOSE, CLUSTER, COALESCE, COLLATE,
325 COLUMN, COMMENT, COMMIT, COMMITTED, CONSTRAINT, CONSTRAINTS, COPY,
326 CREATE, CREATEDB, CREATEUSER, CROSS, CURRENT_DATE, CURRENT_TIME,
327 CURRENT_TIMESTAMP, CURRENT_USER, CURSOR, CYCLE,
329 DATABASE, DAY_P, DEC, DECIMAL, DECLARE, DEFAULT, DEFERRABLE, DEFERRED,
330 DEFINER, DELETE_P, DELIMITERS, DESC, DISTINCT, DO, DOMAIN_P, DOUBLE, DROP,
332 EACH, ELSE, ENCODING, ENCRYPTED, END_TRANS, ESCAPE, EXCEPT, EXCLUSIVE,
333 EXECUTE, EXISTS, EXPLAIN, EXTERNAL, EXTRACT,
335 FALSE_P, FETCH, FLOAT_P, FOR, FORCE, FOREIGN, FORWARD, FREEZE, FROM,
338 GET, GLOBAL, GRANT, GROUP_P,
339 HANDLER, HAVING, HOUR_P,
341 ILIKE, IMMEDIATE, IMMUTABLE, IMPLICIT, IN_P, INCREMENT, INDEX, INHERITS,
342 INITIALLY, INNER_P, INOUT, INPUT, INSENSITIVE, INSERT, INSTEAD, INT,
343 INTEGER, INTERSECT, INTERVAL, INTO, INVOKER, IS, ISNULL, ISOLATION,
348 LANCOMPILER, LANGUAGE, LEADING, LEFT, LEVEL, LIKE, LIMIT, LISTEN,
349 LOAD, LOCAL, LOCALTIME, LOCALTIMESTAMP, LOCATION, LOCK_P,
351 MATCH, MAXVALUE, MINUTE_P, MINVALUE, MODE, MONTH_P, MOVE,
353 NAMES, NATIONAL, NATURAL, NCHAR, NEW, NEXT, NO, NOCREATEDB,
354 NOCREATEUSER, NONE, NOT, NOTHING, NOTIFY, NOTNULL, NULL_P, NULLIF,
357 OF, OFF, OFFSET, OIDS, OLD, ON, ONLY, OPERATOR, OPTION, OR, ORDER,
358 OUT_P, OUTER_P, OVERLAPS, OVERLAY, OWNER,
360 PARTIAL, PASSWORD, PATH_P, PENDANT, PLACING, POSITION, PRECISION, PRIMARY,
361 PRIOR, PRIVILEGES, PROCEDURE, PROCEDURAL,
363 READ, REAL, REFERENCES, REINDEX, RELATIVE, RENAME, REPLACE, RESET,
364 RESTRICT, RETURNS, REVOKE, RIGHT, ROLLBACK, ROW, RULE,
366 SCHEMA, SCROLL, SECOND_P, SECURITY, SELECT, SEQUENCE, SERIALIZABLE,
367 SESSION, SESSION_USER, SET, SETOF, SHARE, SHOW, SIMILAR, SMALLINT, SOME,
368 STABLE, START, STATEMENT, STATISTICS, STDIN, STDOUT, STORAGE, STRICT,
371 TABLE, TEMP, TEMPLATE, TEMPORARY, THEN, TIME, TIMESTAMP, TO, TOAST,
372 TRAILING, TRANSACTION, TRIGGER, TRIM, TRUE_P, TRUNCATE, TRUSTED, TYPE_P,
374 UNENCRYPTED, UNION, UNIQUE, UNKNOWN, UNLISTEN, UNTIL, UPDATE, USAGE,
377 VACUUM, VALID, VALIDATOR, VALUES, VARCHAR, VARYING, VERBOSE, VERSION, VIEW, VOLATILE,
378 WHEN, WHERE, WITH, WITHOUT, WORK,
382 /* The grammar thinks these are keywords, but they are not in the keywords.c
383 * list and so can never be entered directly. The filter in parser.c
384 * creates these tokens when required.
388 /* Special keywords, not in the query language - see the "lex" file */
389 %token <str> IDENT, FCONST, SCONST, BITCONST, Op
390 %token <ival> ICONST, PARAM
392 /* these are not real. they are here so that they get generated as #define's*/
395 /* precedence: lowest to highest */
398 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
404 %nonassoc LIKE ILIKE SIMILAR
409 %left POSTFIXOP /* dummy for postfix Op rules */
410 %left Op OPERATOR /* multi-character ops and user-defined operators */
413 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
417 /* Unary Operators */
418 %left AT ZONE /* sets precedence for AT TIME ZONE */
428 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
429 * psql already handles such cases, but other interfaces don't.
436 /* the thrashing around here is to discard "empty" statements... */
437 stmtmulti: stmtmulti ';' stmt
438 { if ($3 != (Node *)NULL)
439 $$ = lappend($1, $3);
444 { if ($1 != (Node *)NULL)
451 stmt : AlterDatabaseSetStmt
511 { $$ = (Node *)NULL; }
514 /*****************************************************************************
516 * Create a new Postgres DBMS user
519 *****************************************************************************/
521 CreateUserStmt: CREATE USER UserId opt_with OptUserList
523 CreateUserStmt *n = makeNode(CreateUserStmt);
531 opt_with: WITH { $$ = TRUE; }
532 | /*EMPTY*/ { $$ = TRUE; }
535 /*****************************************************************************
537 * Alter a postgresql DBMS user
540 *****************************************************************************/
542 AlterUserStmt: ALTER USER UserId opt_with OptUserList
544 AlterUserStmt *n = makeNode(AlterUserStmt);
552 AlterUserSetStmt: ALTER USER UserId SET set_rest
554 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
556 n->variable = $5->name;
560 | ALTER USER UserId VariableResetStmt
562 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
564 n->variable = ((VariableResetStmt *)$4)->name;
571 /*****************************************************************************
573 * Drop a postgresql DBMS user
576 *****************************************************************************/
578 DropUserStmt: DROP USER user_list
580 DropUserStmt *n = makeNode(DropUserStmt);
587 * Options for CREATE USER and ALTER USER
589 OptUserList: OptUserList OptUserElem { $$ = lappend($1, $2); }
590 | /* EMPTY */ { $$ = NIL; }
593 OptUserElem: PASSWORD Sconst
595 $$ = makeNode(DefElem);
596 $$->defname = "password";
597 $$->arg = (Node *)makeString($2);
599 | ENCRYPTED PASSWORD Sconst
601 $$ = makeNode(DefElem);
602 $$->defname = "encryptedPassword";
603 $$->arg = (Node *)makeString($3);
605 | UNENCRYPTED PASSWORD Sconst
607 $$ = makeNode(DefElem);
608 $$->defname = "unencryptedPassword";
609 $$->arg = (Node *)makeString($3);
613 $$ = makeNode(DefElem);
614 $$->defname = "sysid";
615 $$->arg = (Node *)makeInteger($2);
619 $$ = makeNode(DefElem);
620 $$->defname = "createdb";
621 $$->arg = (Node *)makeInteger(TRUE);
625 $$ = makeNode(DefElem);
626 $$->defname = "createdb";
627 $$->arg = (Node *)makeInteger(FALSE);
631 $$ = makeNode(DefElem);
632 $$->defname = "createuser";
633 $$->arg = (Node *)makeInteger(TRUE);
637 $$ = makeNode(DefElem);
638 $$->defname = "createuser";
639 $$->arg = (Node *)makeInteger(FALSE);
641 | IN_P GROUP_P user_list
643 $$ = makeNode(DefElem);
644 $$->defname = "groupElts";
645 $$->arg = (Node *)$3;
649 $$ = makeNode(DefElem);
650 $$->defname = "validUntil";
651 $$->arg = (Node *)makeString($3);
655 user_list: user_list ',' UserId
657 $$ = lappend($1, makeString($3));
661 $$ = makeList1(makeString($1));
667 /*****************************************************************************
669 * Create a postgresql group
672 *****************************************************************************/
674 CreateGroupStmt: CREATE GROUP_P UserId opt_with OptGroupList
676 CreateGroupStmt *n = makeNode(CreateGroupStmt);
684 * Options for CREATE GROUP
686 OptGroupList: OptGroupList OptGroupElem { $$ = lappend($1, $2); }
687 | /* EMPTY */ { $$ = NIL; }
690 OptGroupElem: USER user_list
692 $$ = makeNode(DefElem);
693 $$->defname = "userElts";
694 $$->arg = (Node *)$2;
698 $$ = makeNode(DefElem);
699 $$->defname = "sysid";
700 $$->arg = (Node *)makeInteger($2);
705 /*****************************************************************************
707 * Alter a postgresql group
710 *****************************************************************************/
712 AlterGroupStmt: ALTER GROUP_P UserId ADD USER user_list
714 AlterGroupStmt *n = makeNode(AlterGroupStmt);
720 | ALTER GROUP_P UserId DROP USER user_list
722 AlterGroupStmt *n = makeNode(AlterGroupStmt);
731 /*****************************************************************************
733 * Drop a postgresql group
736 *****************************************************************************/
738 DropGroupStmt: DROP GROUP_P UserId
740 DropGroupStmt *n = makeNode(DropGroupStmt);
747 /*****************************************************************************
749 * Manipulate a schema
752 *****************************************************************************/
754 CreateSchemaStmt: CREATE SCHEMA OptSchemaName AUTHORIZATION UserId OptSchemaEltList
756 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
757 /* One can omit the schema name or the authorization id... */
766 | CREATE SCHEMA ColId OptSchemaEltList
768 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
769 /* ...but not both */
777 AlterSchemaStmt: ALTER SCHEMA ColId
779 elog(ERROR, "ALTER SCHEMA not yet supported");
783 DropSchemaStmt: DROP SCHEMA ColId
785 elog(ERROR, "DROP SCHEMA not yet supported");
789 OptSchemaName: ColId { $$ = $1; }
790 | /* EMPTY */ { $$ = NULL; }
793 OptSchemaEltList: OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
794 | /* EMPTY */ { $$ = NIL; }
798 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
799 * statement (in addition to by themselves).
801 schema_stmt: CreateStmt
807 /*****************************************************************************
809 * Set PG internal variable
810 * SET name TO 'var_value'
811 * Include SQL92 syntax (thomas 1997-10-22):
812 * SET TIME ZONE 'var_value'
814 *****************************************************************************/
816 VariableSetStmt: SET set_rest
818 VariableSetStmt *n = $2;
824 VariableSetStmt *n = $3;
828 | SET SESSION set_rest
830 VariableSetStmt *n = $3;
836 set_rest: ColId TO var_list_or_default
838 VariableSetStmt *n = makeNode(VariableSetStmt);
843 | ColId '=' var_list_or_default
845 VariableSetStmt *n = makeNode(VariableSetStmt);
850 | TIME ZONE zone_value
852 VariableSetStmt *n = makeNode(VariableSetStmt);
853 n->name = "timezone";
855 n->args = makeList1($3);
858 | TRANSACTION ISOLATION LEVEL opt_level
860 VariableSetStmt *n = makeNode(VariableSetStmt);
861 n->name = "TRANSACTION ISOLATION LEVEL";
862 n->args = makeList1(makeStringConst($4, NULL));
865 | SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL opt_level
867 VariableSetStmt *n = makeNode(VariableSetStmt);
868 n->name = "default_transaction_isolation";
869 n->args = makeList1(makeStringConst($7, NULL));
874 VariableSetStmt *n = makeNode(VariableSetStmt);
875 n->name = "client_encoding";
877 n->args = makeList1(makeStringConst($2, NULL));
880 | SESSION AUTHORIZATION ColId_or_Sconst
882 VariableSetStmt *n = makeNode(VariableSetStmt);
883 n->name = "session_authorization";
884 n->args = makeList1(makeStringConst($3, NULL));
887 | SESSION AUTHORIZATION DEFAULT
889 VariableSetStmt *n = makeNode(VariableSetStmt);
890 n->name = "session_authorization";
896 var_list_or_default: var_list
903 { $$ = makeList1($1); }
904 | var_list ',' var_value
905 { $$ = lappend($1, $3); }
908 var_value: opt_boolean
909 { $$ = makeStringConst($1, NULL); }
911 { $$ = makeStringConst($1, NULL); }
913 { $$ = makeAConst($1); }
916 opt_level: READ COMMITTED { $$ = "read committed"; }
917 | SERIALIZABLE { $$ = "serializable"; }
920 opt_boolean: TRUE_P { $$ = "true"; }
921 | FALSE_P { $$ = "false"; }
923 | OFF { $$ = "off"; }
926 /* Timezone values can be:
927 * - a string such as 'pst8pdt'
928 * - an identifier such as "pst8pdt"
929 * - an integer or floating point number
930 * - a time interval per SQL99
931 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
932 * so use IDENT and reject anything which is a reserved word.
936 $$ = makeStringConst($1, NULL);
940 $$ = makeStringConst($1, NULL);
942 | ConstInterval Sconst opt_interval
944 A_Const *n = (A_Const *) makeStringConst($2, $1);
947 if (($3 & ~(MASK(HOUR) | MASK(MINUTE))) != 0)
948 elog(ERROR, "Time zone interval must be HOUR or HOUR TO MINUTE");
949 n->typename->typmod = ((($3 & 0x7FFF) << 16) | 0xFFFF);
953 | ConstInterval '(' Iconst ')' Sconst opt_interval
955 A_Const *n = (A_Const *) makeStringConst($5, $1);
956 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
957 elog(ERROR, "INTERVAL(%d) precision must be between %d and %d",
958 $3, 0, MAX_INTERVAL_PRECISION);
961 if (($6 & ~(MASK(HOUR) | MASK(MINUTE))) != 0)
962 elog(ERROR, "Time zone interval must be HOUR or HOUR TO MINUTE");
963 n->typename->typmod = ((($6 & 0x7FFF) << 16) | $3);
967 n->typename->typmod = ((0x7FFF << 16) | $3);
972 | NumericOnly { $$ = makeAConst($1); }
973 | DEFAULT { $$ = NULL; }
974 | LOCAL { $$ = NULL; }
977 opt_encoding: Sconst { $$ = $1; }
978 | DEFAULT { $$ = NULL; }
979 | /*EMPTY*/ { $$ = NULL; }
982 ColId_or_Sconst: ColId { $$ = $1; }
983 | SCONST { $$ = $1; }
987 VariableShowStmt: SHOW ColId
989 VariableShowStmt *n = makeNode(VariableShowStmt);
995 VariableShowStmt *n = makeNode(VariableShowStmt);
996 n->name = "timezone";
999 | SHOW TRANSACTION ISOLATION LEVEL
1001 VariableShowStmt *n = makeNode(VariableShowStmt);
1002 n->name = "TRANSACTION ISOLATION LEVEL";
1005 | SHOW SESSION AUTHORIZATION
1007 VariableShowStmt *n = makeNode(VariableShowStmt);
1008 n->name = "session_authorization";
1013 VariableShowStmt *n = makeNode(VariableShowStmt);
1019 VariableResetStmt: RESET ColId
1021 VariableResetStmt *n = makeNode(VariableResetStmt);
1027 VariableResetStmt *n = makeNode(VariableResetStmt);
1028 n->name = "timezone";
1031 | RESET TRANSACTION ISOLATION LEVEL
1033 VariableResetStmt *n = makeNode(VariableResetStmt);
1034 n->name = "TRANSACTION ISOLATION LEVEL";
1037 | RESET SESSION AUTHORIZATION
1039 VariableResetStmt *n = makeNode(VariableResetStmt);
1040 n->name = "session_authorization";
1045 VariableResetStmt *n = makeNode(VariableResetStmt);
1052 ConstraintsSetStmt: SET CONSTRAINTS constraints_set_list constraints_set_mode
1054 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1055 n->constraints = $3;
1061 constraints_set_list: ALL { $$ = NIL; }
1062 | name_list { $$ = $1; }
1065 constraints_set_mode: DEFERRED { $$ = TRUE; }
1066 | IMMEDIATE { $$ = FALSE; }
1071 * Checkpoint statement
1073 CheckPointStmt: CHECKPOINT
1075 CheckPointStmt *n = makeNode(CheckPointStmt);
1080 /*****************************************************************************
1082 * ALTER TABLE variations
1084 *****************************************************************************/
1087 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1088 ALTER TABLE relation_expr ADD opt_column columnDef
1090 AlterTableStmt *n = makeNode(AlterTableStmt);
1096 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1097 | ALTER TABLE relation_expr ALTER opt_column ColId alter_column_default
1099 AlterTableStmt *n = makeNode(AlterTableStmt);
1106 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1107 | ALTER TABLE relation_expr ALTER opt_column ColId DROP NOT NULL_P
1109 AlterTableStmt *n = makeNode(AlterTableStmt);
1115 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1116 | ALTER TABLE relation_expr ALTER opt_column ColId SET NOT NULL_P
1118 AlterTableStmt *n = makeNode(AlterTableStmt);
1124 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <Iconst> */
1125 | ALTER TABLE relation_expr ALTER opt_column ColId SET STATISTICS Iconst
1127 AlterTableStmt *n = makeNode(AlterTableStmt);
1131 n->def = (Node *) makeInteger($9);
1134 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1135 | ALTER TABLE relation_expr ALTER opt_column ColId SET STORAGE ColId
1137 AlterTableStmt *n = makeNode(AlterTableStmt);
1141 n->def = (Node *) makeString($9);
1144 /* ALTER TABLE <relation> DROP [COLUMN] <colname> {RESTRICT|CASCADE} */
1145 | ALTER TABLE relation_expr DROP opt_column ColId drop_behavior
1147 AlterTableStmt *n = makeNode(AlterTableStmt);
1154 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1155 | ALTER TABLE relation_expr ADD TableConstraint
1157 AlterTableStmt *n = makeNode(AlterTableStmt);
1163 /* ALTER TABLE <relation> DROP CONSTRAINT <name> {RESTRICT|CASCADE} */
1164 | ALTER TABLE relation_expr DROP CONSTRAINT name drop_behavior
1166 AlterTableStmt *n = makeNode(AlterTableStmt);
1173 /* ALTER TABLE <name> CREATE TOAST TABLE */
1174 | ALTER TABLE qualified_name CREATE TOAST TABLE
1176 AlterTableStmt *n = makeNode(AlterTableStmt);
1178 $3->inhOpt = INH_NO;
1182 /* ALTER TABLE <name> OWNER TO UserId */
1183 | ALTER TABLE qualified_name OWNER TO UserId
1185 AlterTableStmt *n = makeNode(AlterTableStmt);
1187 $3->inhOpt = INH_NO;
1194 alter_column_default:
1197 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1198 if (exprIsNullConstant($3))
1203 | DROP DEFAULT { $$ = NULL; }
1206 drop_behavior: CASCADE { $$ = CASCADE; }
1207 | RESTRICT { $$ = RESTRICT; }
1210 opt_drop_behavior: CASCADE { $$ = CASCADE; }
1211 | RESTRICT { $$ = RESTRICT; }
1212 | /* EMPTY */ { $$ = RESTRICT; /* default */ }
1217 /*****************************************************************************
1222 *****************************************************************************/
1224 ClosePortalStmt: CLOSE opt_id
1226 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1232 opt_id: ColId { $$ = $1; }
1233 | /*EMPTY*/ { $$ = NULL; }
1237 /*****************************************************************************
1240 * COPY [BINARY] <relname> FROM/TO
1241 * [USING DELIMITERS <delimiter>]
1243 *****************************************************************************/
1245 CopyStmt: COPY opt_binary qualified_name opt_with_copy copy_dirn copy_file_name copy_delimiter copy_null
1247 CopyStmt *n = makeNode(CopyStmt);
1266 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1267 * used depends on the direction. (It really doesn't make sense to copy from
1268 * stdout. We silently correct the "typo". - AY 9/94
1270 copy_file_name: Sconst { $$ = $1; }
1271 | STDIN { $$ = NULL; }
1272 | STDOUT { $$ = NULL; }
1275 opt_binary: BINARY { $$ = TRUE; }
1276 | /*EMPTY*/ { $$ = FALSE; }
1279 opt_with_copy: WITH OIDS { $$ = TRUE; }
1280 | /*EMPTY*/ { $$ = FALSE; }
1284 * the default copy delimiter is tab but the user can configure it
1286 copy_delimiter: opt_using DELIMITERS Sconst { $$ = $3; }
1287 | /*EMPTY*/ { $$ = "\t"; }
1290 opt_using: USING { $$ = TRUE; }
1291 | /*EMPTY*/ { $$ = TRUE; }
1294 copy_null: WITH NULL_P AS Sconst { $$ = $4; }
1295 | /*EMPTY*/ { $$ = "\\N"; }
1298 /*****************************************************************************
1303 *****************************************************************************/
1305 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')' OptInherit OptWithOids
1307 CreateStmt *n = makeNode(CreateStmt);
1311 n->inhRelations = $8;
1312 n->constraints = NIL;
1319 * Redundancy here is needed to avoid shift/reduce conflicts,
1320 * since TEMP is not a reserved word. See also OptTempTableName.
1322 OptTemp: TEMPORARY { $$ = TRUE; }
1323 | TEMP { $$ = TRUE; }
1324 | LOCAL TEMPORARY { $$ = TRUE; }
1325 | LOCAL TEMP { $$ = TRUE; }
1328 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
1333 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
1336 | /*EMPTY*/ { $$ = FALSE; }
1339 OptTableElementList: OptTableElementList ',' OptTableElement
1342 $$ = lappend($1, $3);
1353 | /*EMPTY*/ { $$ = NIL; }
1356 OptTableElement: columnDef { $$ = $1; }
1357 | TableConstraint { $$ = $1; }
1360 columnDef: ColId Typename ColQualList opt_collate
1362 ColumnDef *n = makeNode(ColumnDef);
1365 n->constraints = $3;
1368 elog(NOTICE, "CREATE TABLE / COLLATE %s not yet implemented"
1369 "; clause ignored", $4);
1375 ColQualList: ColQualList ColConstraint { $$ = lappend($1, $2); }
1376 | /*EMPTY*/ { $$ = NIL; }
1380 CONSTRAINT name ColConstraintElem
1382 switch (nodeTag($3))
1386 Constraint *n = (Constraint *)$3;
1390 case T_FkConstraint:
1392 FkConstraint *n = (FkConstraint *)$3;
1393 n->constr_name = $2;
1407 /* DEFAULT NULL is already the default for Postgres.
1408 * But define it here and carry it forward into the system
1409 * to make it explicit.
1410 * - thomas 1998-09-13
1412 * WITH NULL and NULL are not SQL92-standard syntax elements,
1413 * so leave them out. Use DEFAULT NULL to explicitly indicate
1414 * that a column may have that value. WITH NULL leads to
1415 * shift/reduce conflicts with WITH TIME ZONE anyway.
1416 * - thomas 1999-01-08
1418 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1419 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1420 * or be part of a_expr NOT LIKE or similar constructs).
1425 Constraint *n = makeNode(Constraint);
1426 n->contype = CONSTR_NOTNULL;
1429 n->cooked_expr = NULL;
1435 Constraint *n = makeNode(Constraint);
1436 n->contype = CONSTR_NULL;
1439 n->cooked_expr = NULL;
1445 Constraint *n = makeNode(Constraint);
1446 n->contype = CONSTR_UNIQUE;
1449 n->cooked_expr = NULL;
1455 Constraint *n = makeNode(Constraint);
1456 n->contype = CONSTR_PRIMARY;
1459 n->cooked_expr = NULL;
1463 | CHECK '(' a_expr ')'
1465 Constraint *n = makeNode(Constraint);
1466 n->contype = CONSTR_CHECK;
1469 n->cooked_expr = NULL;
1475 Constraint *n = makeNode(Constraint);
1476 n->contype = CONSTR_DEFAULT;
1478 if (exprIsNullConstant($2))
1480 /* DEFAULT NULL should be reported as empty expr */
1487 n->cooked_expr = NULL;
1491 | REFERENCES qualified_name opt_column_list key_match key_actions
1493 FkConstraint *n = makeNode(FkConstraint);
1494 n->constr_name = NULL;
1500 n->deferrable = FALSE;
1501 n->initdeferred = FALSE;
1507 * ConstraintAttr represents constraint attributes, which we parse as if
1508 * they were independent constraint clauses, in order to avoid shift/reduce
1509 * conflicts (since NOT might start either an independent NOT NULL clause
1510 * or an attribute). analyze.c is responsible for attaching the attribute
1511 * information to the preceding "real" constraint node, and for complaining
1512 * if attribute clauses appear in the wrong place or wrong combinations.
1514 * See also ConstraintAttributeSpec, which can be used in places where
1515 * there is no parsing conflict.
1517 ConstraintAttr: DEFERRABLE
1519 Constraint *n = makeNode(Constraint);
1520 n->contype = CONSTR_ATTR_DEFERRABLE;
1525 Constraint *n = makeNode(Constraint);
1526 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1529 | INITIALLY DEFERRED
1531 Constraint *n = makeNode(Constraint);
1532 n->contype = CONSTR_ATTR_DEFERRED;
1535 | INITIALLY IMMEDIATE
1537 Constraint *n = makeNode(Constraint);
1538 n->contype = CONSTR_ATTR_IMMEDIATE;
1544 /* ConstraintElem specifies constraint syntax which is not embedded into
1545 * a column definition. ColConstraintElem specifies the embedded form.
1546 * - thomas 1997-12-03
1548 TableConstraint: CONSTRAINT name ConstraintElem
1550 switch (nodeTag($3))
1554 Constraint *n = (Constraint *)$3;
1558 case T_FkConstraint:
1560 FkConstraint *n = (FkConstraint *)$3;
1561 n->constr_name = $2;
1573 ConstraintElem: CHECK '(' a_expr ')'
1575 Constraint *n = makeNode(Constraint);
1576 n->contype = CONSTR_CHECK;
1579 n->cooked_expr = NULL;
1582 | UNIQUE '(' columnList ')'
1584 Constraint *n = makeNode(Constraint);
1585 n->contype = CONSTR_UNIQUE;
1588 n->cooked_expr = NULL;
1592 | PRIMARY KEY '(' columnList ')'
1594 Constraint *n = makeNode(Constraint);
1595 n->contype = CONSTR_PRIMARY;
1598 n->cooked_expr = NULL;
1602 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name opt_column_list
1603 key_match key_actions ConstraintAttributeSpec
1605 FkConstraint *n = makeNode(FkConstraint);
1606 n->constr_name = NULL;
1612 n->deferrable = ($11 & 1) != 0;
1613 n->initdeferred = ($11 & 2) != 0;
1618 opt_column_list: '(' columnList ')' { $$ = $2; }
1619 | /*EMPTY*/ { $$ = NIL; }
1622 columnList: columnList ',' columnElem
1623 { $$ = lappend($1, $3); }
1625 { $$ = makeList1($1); }
1630 Ident *id = makeNode(Ident);
1636 key_match: MATCH FULL
1642 elog(ERROR, "FOREIGN KEY/MATCH PARTIAL not yet implemented");
1651 key_actions: key_delete { $$ = $1; }
1652 | key_update { $$ = $1; }
1653 | key_delete key_update { $$ = $1 | $2; }
1654 | key_update key_delete { $$ = $1 | $2; }
1655 | /*EMPTY*/ { $$ = 0; }
1658 key_delete: ON DELETE_P key_reference { $$ = $3 << FKCONSTR_ON_DELETE_SHIFT; }
1661 key_update: ON UPDATE key_reference { $$ = $3 << FKCONSTR_ON_UPDATE_SHIFT; }
1664 key_reference: NO ACTION { $$ = FKCONSTR_ON_KEY_NOACTION; }
1665 | RESTRICT { $$ = FKCONSTR_ON_KEY_RESTRICT; }
1666 | CASCADE { $$ = FKCONSTR_ON_KEY_CASCADE; }
1667 | SET NULL_P { $$ = FKCONSTR_ON_KEY_SETNULL; }
1668 | SET DEFAULT { $$ = FKCONSTR_ON_KEY_SETDEFAULT; }
1671 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
1672 | /*EMPTY*/ { $$ = NIL; }
1675 OptWithOids: WITH OIDS { $$ = TRUE; }
1676 | WITHOUT OIDS { $$ = FALSE; }
1677 | /*EMPTY*/ { $$ = TRUE; }
1682 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
1686 CreateAsStmt: CREATE OptTemp TABLE qualified_name OptCreateAs AS SelectStmt
1689 * When the SelectStmt is a set-operation tree, we must
1690 * stuff the INTO information into the leftmost component
1691 * Select, because that's where analyze.c will expect
1692 * to find it. Similarly, the output column names must
1693 * be attached to that Select's target list.
1695 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
1696 if (n->into != NULL)
1697 elog(ERROR, "CREATE TABLE AS may not specify INTO");
1700 n->intoColNames = $5;
1705 OptCreateAs: '(' CreateAsList ')' { $$ = $2; }
1706 | /*EMPTY*/ { $$ = NIL; }
1709 CreateAsList: CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1710 | CreateAsElement { $$ = makeList1($1); }
1713 CreateAsElement: ColId
1715 ColumnDef *n = makeNode(ColumnDef);
1718 n->raw_default = NULL;
1719 n->cooked_default = NULL;
1720 n->is_not_null = FALSE;
1721 n->constraints = NULL;
1727 /*****************************************************************************
1730 * CREATE SEQUENCE seqname
1732 *****************************************************************************/
1734 CreateSeqStmt: CREATE OptTemp SEQUENCE qualified_name OptSeqList
1736 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1744 OptSeqList: OptSeqList OptSeqElem
1745 { $$ = lappend($1, $2); }
1749 OptSeqElem: CACHE NumericOnly
1751 $$ = makeNode(DefElem);
1752 $$->defname = "cache";
1753 $$->arg = (Node *)$2;
1757 $$ = makeNode(DefElem);
1758 $$->defname = "cycle";
1759 $$->arg = (Node *)NULL;
1761 | INCREMENT NumericOnly
1763 $$ = makeNode(DefElem);
1764 $$->defname = "increment";
1765 $$->arg = (Node *)$2;
1767 | MAXVALUE NumericOnly
1769 $$ = makeNode(DefElem);
1770 $$->defname = "maxvalue";
1771 $$->arg = (Node *)$2;
1773 | MINVALUE NumericOnly
1775 $$ = makeNode(DefElem);
1776 $$->defname = "minvalue";
1777 $$->arg = (Node *)$2;
1781 $$ = makeNode(DefElem);
1782 $$->defname = "start";
1783 $$->arg = (Node *)$2;
1787 NumericOnly: FloatOnly { $$ = $1; }
1788 | IntegerOnly { $$ = $1; }
1804 $$ = makeInteger($1);
1808 $$ = makeInteger($2);
1809 $$->val.ival = - $$->val.ival;
1813 /*****************************************************************************
1816 * CREATE PROCEDURAL LANGUAGE ...
1817 * DROP PROCEDURAL LANGUAGE ...
1819 *****************************************************************************/
1821 CreatePLangStmt: CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
1822 HANDLER handler_name opt_validator opt_lancompiler
1824 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1827 n->plvalidator = $8;
1834 opt_trusted: TRUSTED { $$ = TRUE; }
1835 | /*EMPTY*/ { $$ = FALSE; }
1838 /* This ought to be just func_name, but that causes reduce/reduce conflicts
1839 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
1840 * Work around by using name and dotted_name separately.
1843 { $$ = makeList1(makeString($1)); }
1848 opt_lancompiler: LANCOMPILER Sconst { $$ = $2; }
1849 | /*EMPTY*/ { $$ = ""; }
1852 opt_validator: VALIDATOR handler_name { $$ = $2; }
1853 | /*EMPTY*/ { $$ = NULL; }
1856 DropPLangStmt: DROP opt_procedural LANGUAGE ColId_or_Sconst
1858 DropPLangStmt *n = makeNode(DropPLangStmt);
1864 opt_procedural: PROCEDURAL { $$ = TRUE; }
1865 | /*EMPTY*/ { $$ = TRUE; }
1868 /*****************************************************************************
1871 * CREATE TRIGGER ...
1874 *****************************************************************************/
1876 CreateTrigStmt: CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1877 qualified_name TriggerForSpec EXECUTE PROCEDURE
1878 func_name '(' TriggerFuncArgs ')'
1880 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1887 memcpy (n->actions, $5, 4);
1888 n->lang = NULL; /* unused */
1889 n->text = NULL; /* unused */
1890 n->attr = NULL; /* unused */
1891 n->when = NULL; /* unused */
1893 n->isconstraint = FALSE;
1894 n->deferrable = FALSE;
1895 n->initdeferred = FALSE;
1896 n->constrrel = NULL;
1899 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
1900 qualified_name OptConstrFromTable
1901 ConstraintAttributeSpec
1902 FOR EACH ROW EXECUTE PROCEDURE
1903 func_name '(' TriggerFuncArgs ')'
1905 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1912 memcpy (n->actions, $6, 4);
1913 n->lang = NULL; /* unused */
1914 n->text = NULL; /* unused */
1915 n->attr = NULL; /* unused */
1916 n->when = NULL; /* unused */
1918 n->isconstraint = TRUE;
1919 n->deferrable = ($10 & 1) != 0;
1920 n->initdeferred = ($10 & 2) != 0;
1927 TriggerActionTime: BEFORE { $$ = TRUE; }
1928 | AFTER { $$ = FALSE; }
1931 TriggerEvents: TriggerOneEvent
1933 char *e = palloc (4);
1934 e[0] = $1; e[1] = 0; $$ = e;
1936 | TriggerOneEvent OR TriggerOneEvent
1938 char *e = palloc (4);
1939 e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
1941 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
1943 char *e = palloc (4);
1944 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
1949 TriggerOneEvent: INSERT { $$ = 'i'; }
1950 | DELETE_P { $$ = 'd'; }
1951 | UPDATE { $$ = 'u'; }
1954 TriggerForSpec: FOR TriggerForOpt TriggerForType
1960 TriggerForOpt: EACH { $$ = TRUE; }
1961 | /*EMPTY*/ { $$ = FALSE; }
1964 TriggerForType: ROW { $$ = TRUE; }
1965 | STATEMENT { $$ = FALSE; }
1968 TriggerFuncArgs: TriggerFuncArg
1969 { $$ = makeList1($1); }
1970 | TriggerFuncArgs ',' TriggerFuncArg
1971 { $$ = lappend($1, $3); }
1976 TriggerFuncArg: ICONST
1979 sprintf (buf, "%d", $1);
1980 $$ = makeString(pstrdup(buf));
1984 $$ = makeString($1);
1988 $$ = makeString($1);
1992 $$ = makeString($1);
1996 $$ = makeString($1);
2000 OptConstrFromTable: /* Empty */
2004 | FROM qualified_name
2010 ConstraintAttributeSpec: ConstraintDeferrabilitySpec
2012 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2014 if ($1 == 0 && $2 != 0)
2015 elog(ERROR, "INITIALLY DEFERRED constraint must be DEFERRABLE");
2018 | ConstraintTimeSpec
2025 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2027 if ($2 == 0 && $1 != 0)
2028 elog(ERROR, "INITIALLY DEFERRED constraint must be DEFERRABLE");
2035 ConstraintDeferrabilitySpec: NOT DEFERRABLE
2041 ConstraintTimeSpec: INITIALLY IMMEDIATE
2043 | INITIALLY DEFERRED
2048 DropTrigStmt: DROP TRIGGER name ON qualified_name
2050 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2053 n->removeType = DROP_TRIGGER;
2059 /*****************************************************************************
2062 * CREATE ASSERTION ...
2063 * DROP ASSERTION ...
2065 *****************************************************************************/
2067 CreateAssertStmt: CREATE ASSERTION name
2068 CHECK '(' a_expr ')' ConstraintAttributeSpec
2070 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2072 n->args = makeList1($6);
2073 n->isconstraint = TRUE;
2074 n->deferrable = ($8 & 1) != 0;
2075 n->initdeferred = ($8 & 2) != 0;
2077 elog(ERROR, "CREATE ASSERTION is not yet supported");
2083 DropAssertStmt: DROP ASSERTION name
2085 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2088 n->removeType = DROP_TRIGGER;
2089 elog(ERROR, "DROP ASSERTION is not yet supported");
2095 /*****************************************************************************
2098 * define (aggregate,operator,type)
2100 *****************************************************************************/
2102 DefineStmt: CREATE AGGREGATE func_name definition
2104 DefineStmt *n = makeNode(DefineStmt);
2105 n->defType = AGGREGATE;
2110 | CREATE OPERATOR any_operator definition
2112 DefineStmt *n = makeNode(DefineStmt);
2113 n->defType = OPERATOR;
2118 | CREATE TYPE_P any_name definition
2120 DefineStmt *n = makeNode(DefineStmt);
2121 n->defType = TYPE_P;
2126 | CREATE CHARACTER SET opt_as any_name GET definition opt_collate
2128 DefineStmt *n = makeNode(DefineStmt);
2129 n->defType = CHARACTER;
2136 definition: '(' def_list ')' { $$ = $2; }
2139 def_list: def_elem { $$ = makeList1($1); }
2140 | def_list ',' def_elem { $$ = lappend($1, $3); }
2143 def_elem: ColLabel '=' def_arg
2145 $$ = makeNode(DefElem);
2147 $$->arg = (Node *)$3;
2151 $$ = makeNode(DefElem);
2153 $$->arg = (Node *)NULL;
2157 /* Note: any simple identifier will be returned as a type name! */
2158 def_arg: func_return { $$ = (Node *)$1; }
2159 | all_Op { $$ = (Node *)makeString($1); }
2160 | NumericOnly { $$ = (Node *)$1; }
2161 | Sconst { $$ = (Node *)makeString($1); }
2165 /*****************************************************************************
2169 * DROP itemtype itemname [, itemname ...]
2171 *****************************************************************************/
2173 DropStmt: DROP drop_type any_name_list opt_drop_behavior
2175 DropStmt *n = makeNode(DropStmt);
2183 drop_type: TABLE { $$ = DROP_TABLE; }
2184 | SEQUENCE { $$ = DROP_SEQUENCE; }
2185 | VIEW { $$ = DROP_VIEW; }
2186 | INDEX { $$ = DROP_INDEX; }
2187 | TYPE_P { $$ = DROP_TYPE; }
2188 | DOMAIN_P { $$ = DROP_DOMAIN; }
2191 any_name_list: any_name
2192 { $$ = makeList1($1); }
2193 | any_name_list ',' any_name
2194 { $$ = lappend($1, $3); }
2198 { $$ = makeList1(makeString($1)); }
2203 /*****************************************************************************
2206 * truncate table relname
2208 *****************************************************************************/
2210 TruncateStmt: TRUNCATE opt_table qualified_name
2212 TruncateStmt *n = makeNode(TruncateStmt);
2218 /*****************************************************************************
2220 * The COMMENT ON statement can take different forms based upon the type of
2221 * the object associated with the comment. The form of the statement is:
2223 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW ]
2224 * <objname> | AGGREGATE <aggname> (<aggtype>) | FUNCTION
2225 * <funcname> (arg1, arg2, ...) | OPERATOR <op>
2226 * (leftoperand_typ rightoperand_typ) | TRIGGER <triggername> ON
2227 * <relname> | RULE <rulename> ON <relname> ] IS 'text'
2229 *****************************************************************************/
2231 CommentStmt: COMMENT ON comment_type any_name IS comment_text
2233 CommentStmt *n = makeNode(CommentStmt);
2240 | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')' IS comment_text
2242 CommentStmt *n = makeNode(CommentStmt);
2243 n->objtype = AGGREGATE;
2245 n->objargs = makeList1($6);
2249 | COMMENT ON FUNCTION func_name func_args IS comment_text
2251 CommentStmt *n = makeNode(CommentStmt);
2252 n->objtype = FUNCTION;
2258 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')' IS comment_text
2260 CommentStmt *n = makeNode(CommentStmt);
2261 n->objtype = OPERATOR;
2267 | COMMENT ON TRIGGER name ON any_name IS comment_text
2269 CommentStmt *n = makeNode(CommentStmt);
2270 n->objtype = TRIGGER;
2271 n->objname = lappend($6, makeString($4));
2276 | COMMENT ON RULE name ON any_name IS comment_text
2278 CommentStmt *n = makeNode(CommentStmt);
2280 n->objname = lappend($6, makeString($4));
2285 | COMMENT ON RULE name IS comment_text
2287 /* Obsolete syntax supported for awhile for compatibility */
2288 CommentStmt *n = makeNode(CommentStmt);
2290 n->objname = makeList1(makeString($4));
2297 comment_type: COLUMN { $$ = COLUMN; }
2298 | DATABASE { $$ = DATABASE; }
2299 | SCHEMA { $$ = SCHEMA; }
2300 | INDEX { $$ = INDEX; }
2301 | SEQUENCE { $$ = SEQUENCE; }
2302 | TABLE { $$ = TABLE; }
2303 | DOMAIN_P { $$ = TYPE_P; }
2304 | TYPE_P { $$ = TYPE_P; }
2305 | VIEW { $$ = VIEW; }
2308 comment_text: Sconst { $$ = $1; }
2309 | NULL_P { $$ = NULL; }
2312 /*****************************************************************************
2315 * fetch/move [forward | backward] [ # | all ] [ in <portalname> ]
2316 * fetch [ forward | backward | absolute | relative ]
2317 * [ # | all | next | prior ] [ [ in | from ] <portalname> ]
2319 *****************************************************************************/
2321 FetchStmt: FETCH direction fetch_how_many from_in name
2323 FetchStmt *n = makeNode(FetchStmt);
2327 elog(ERROR,"FETCH / RELATIVE at current position is not supported");
2333 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2341 | FETCH fetch_how_many from_in name
2343 FetchStmt *n = makeNode(FetchStmt);
2347 n->direction = BACKWARD;
2351 n->direction = FORWARD;
2358 | FETCH direction from_in name
2360 FetchStmt *n = makeNode(FetchStmt);
2371 | FETCH from_in name
2373 FetchStmt *n = makeNode(FetchStmt);
2374 n->direction = FORWARD;
2382 FetchStmt *n = makeNode(FetchStmt);
2383 n->direction = FORWARD;
2390 | MOVE direction fetch_how_many from_in name
2392 FetchStmt *n = makeNode(FetchStmt);
2396 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2404 | MOVE fetch_how_many from_in name
2406 FetchStmt *n = makeNode(FetchStmt);
2410 n->direction = BACKWARD;
2414 n->direction = FORWARD;
2421 | MOVE direction from_in name
2423 FetchStmt *n = makeNode(FetchStmt);
2432 FetchStmt *n = makeNode(FetchStmt);
2433 n->direction = FORWARD;
2441 FetchStmt *n = makeNode(FetchStmt);
2442 n->direction = FORWARD;
2450 direction: FORWARD { $$ = FORWARD; }
2451 | BACKWARD { $$ = BACKWARD; }
2452 | RELATIVE { $$ = RELATIVE; }
2455 elog(NOTICE,"FETCH / ABSOLUTE not supported, using RELATIVE");
2460 fetch_how_many: Iconst { $$ = $1; }
2461 | '-' Iconst { $$ = - $2; }
2462 | ALL { $$ = 0; /* 0 means fetch all tuples*/ }
2464 | PRIOR { $$ = -1; }
2472 /*****************************************************************************
2474 * GRANT and REVOKE statements
2476 *****************************************************************************/
2478 GrantStmt: GRANT privileges ON privilege_target TO grantee_list opt_grant_grant_option
2480 GrantStmt *n = makeNode(GrantStmt);
2483 n->objtype = ($4)->objtype;
2484 n->objects = ($4)->objs;
2490 RevokeStmt: REVOKE opt_revoke_grant_option privileges ON privilege_target FROM grantee_list
2492 GrantStmt *n = makeNode(GrantStmt);
2493 n->is_grant = false;
2495 n->objtype = ($5)->objtype;
2496 n->objects = ($5)->objs;
2503 /* either ALL [PRIVILEGES] or a list of individual privileges */
2504 privileges: privilege_list { $$ = $1; }
2505 | ALL { $$ = makeListi1(ACL_ALL_RIGHTS); }
2506 | ALL PRIVILEGES { $$ = makeListi1(ACL_ALL_RIGHTS); }
2509 privilege_list: privilege { $$ = makeListi1($1); }
2510 | privilege_list ',' privilege { $$ = lappendi($1, $3); }
2513 /* Not all of these privilege types apply to all objects, but that
2514 * gets sorted out later.
2516 privilege: SELECT { $$ = ACL_SELECT; }
2517 | INSERT { $$ = ACL_INSERT; }
2518 | UPDATE { $$ = ACL_UPDATE; }
2519 | DELETE_P { $$ = ACL_DELETE; }
2520 | RULE { $$ = ACL_RULE; }
2521 | REFERENCES { $$ = ACL_REFERENCES; }
2522 | TRIGGER { $$ = ACL_TRIGGER; }
2523 | EXECUTE { $$ = ACL_EXECUTE; }
2524 | USAGE { $$ = ACL_USAGE; }
2525 | CREATE { $$ = ACL_CREATE; }
2526 | TEMPORARY { $$ = ACL_CREATE_TEMP; }
2527 | TEMP { $$ = ACL_CREATE_TEMP; }
2531 /* Don't bother trying to fold the first two rules into one using
2532 opt_table. You're going to get conflicts. */
2533 privilege_target: qualified_name_list
2535 PrivTarget *n = makeNode(PrivTarget);
2536 n->objtype = ACL_OBJECT_RELATION;
2540 | TABLE qualified_name_list
2542 PrivTarget *n = makeNode(PrivTarget);
2543 n->objtype = ACL_OBJECT_RELATION;
2547 | FUNCTION function_with_argtypes_list
2549 PrivTarget *n = makeNode(PrivTarget);
2550 n->objtype = ACL_OBJECT_FUNCTION;
2554 | DATABASE name_list
2556 PrivTarget *n = makeNode(PrivTarget);
2557 n->objtype = ACL_OBJECT_DATABASE;
2561 | LANGUAGE name_list
2563 PrivTarget *n = makeNode(PrivTarget);
2564 n->objtype = ACL_OBJECT_LANGUAGE;
2570 PrivTarget *n = makeNode(PrivTarget);
2571 n->objtype = ACL_OBJECT_NAMESPACE;
2578 grantee_list: grantee { $$ = makeList1($1); }
2579 | grantee_list ',' grantee { $$ = lappend($1, $3); }
2584 PrivGrantee *n = makeNode(PrivGrantee);
2585 /* This hack lets us avoid reserving PUBLIC as a keyword */
2586 if (strcmp($1, "public") == 0)
2590 n->groupname = NULL;
2595 PrivGrantee *n = makeNode(PrivGrantee);
2596 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
2597 if (strcmp($2, "public") == 0)
2598 n->groupname = NULL;
2607 opt_grant_grant_option: WITH GRANT OPTION
2609 elog(ERROR, "grant options are not implemented");
2614 opt_revoke_grant_option: GRANT OPTION FOR
2616 elog(ERROR, "grant options are not implemented");
2622 function_with_argtypes_list: function_with_argtypes
2623 { $$ = makeList1($1); }
2624 | function_with_argtypes_list ',' function_with_argtypes
2625 { $$ = lappend($1, $3); }
2628 function_with_argtypes: func_name func_args
2630 FuncWithArgs *n = makeNode(FuncWithArgs);
2638 /*****************************************************************************
2641 * create index <indexname> on <relname>
2642 * [ using <access> ] "(" (<col> with <op>)+ ")"
2643 * [ where <predicate> ]
2645 *****************************************************************************/
2647 IndexStmt: CREATE index_opt_unique INDEX index_name ON qualified_name
2648 access_method_clause '(' index_params ')' where_clause
2650 IndexStmt *n = makeNode(IndexStmt);
2654 n->accessMethod = $7;
2655 n->indexParams = $9;
2656 n->whereClause = $11;
2661 index_opt_unique: UNIQUE { $$ = TRUE; }
2662 | /*EMPTY*/ { $$ = FALSE; }
2665 access_method_clause: USING access_method { $$ = $2; }
2666 /* If btree changes as our default, update pg_get_indexdef() */
2667 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
2670 index_params: index_list { $$ = $1; }
2671 | func_index { $$ = makeList1($1); }
2674 index_list: index_list ',' index_elem { $$ = lappend($1, $3); }
2675 | index_elem { $$ = makeList1($1); }
2678 func_index: func_name '(' name_list ')' opt_class
2680 $$ = makeNode(IndexElem);
2688 index_elem: attr_name opt_class
2690 $$ = makeNode(IndexElem);
2701 * Release 7.0 removed network_ops, timespan_ops, and
2702 * datetime_ops, so we suppress it from being passed to
2703 * the parser so the default *_ops is used. This can be
2704 * removed in some later release. bjm 2000/02/07
2706 * Release 7.1 removes lztext_ops, so suppress that too
2707 * for a while. tgl 2000/07/30
2709 * Release 7.2 renames timestamp_ops to timestamptz_ops,
2710 * so suppress that too for awhile. I'm starting to
2711 * think we need a better approach. tgl 2000/10/01
2713 if (length($1) == 1)
2715 char *claname = strVal(lfirst($1));
2717 if (strcmp(claname, "network_ops") != 0 &&
2718 strcmp(claname, "timespan_ops") != 0 &&
2719 strcmp(claname, "datetime_ops") != 0 &&
2720 strcmp(claname, "lztext_ops") != 0 &&
2721 strcmp(claname, "timestamp_ops") != 0)
2729 | USING any_name { $$ = $2; }
2730 | /*EMPTY*/ { $$ = NIL; }
2733 /*****************************************************************************
2736 * execute recipe <recipeName>
2738 *****************************************************************************/
2741 RecipeStmt: EXECUTE RECIPE recipe_name
2743 RecipeStmt *n = makeNode(RecipeStmt);
2750 /*****************************************************************************
2753 * create [or replace] function <fname>
2754 * [(<type-1> { , <type-n>})]
2756 * as <filename or code in language as appropriate>
2757 * language <lang> [with parameters]
2759 *****************************************************************************/
2761 CreateFunctionStmt: CREATE opt_or_replace FUNCTION func_name func_args
2762 RETURNS func_return createfunc_opt_list opt_definition
2764 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
2774 opt_or_replace: OR REPLACE { $$ = TRUE; }
2775 | /*EMPTY*/ { $$ = FALSE; }
2778 func_args: '(' func_args_list ')' { $$ = $2; }
2779 | '(' ')' { $$ = NIL; }
2782 func_args_list: func_arg
2783 { $$ = makeList1($1); }
2784 | func_args_list ',' func_arg
2785 { $$ = lappend($1, $3); }
2788 func_arg: opt_arg func_type
2790 /* We can catch over-specified arguments here if we want to,
2791 * but for now better to silently swallow typmod, etc.
2792 * - thomas 2000-03-22
2808 elog(ERROR, "CREATE FUNCTION / OUT parameters are not supported");
2813 elog(ERROR, "CREATE FUNCTION / INOUT parameters are not supported");
2818 func_return: func_type
2820 /* We can catch over-specified arguments here if we want to,
2821 * but for now better to silently swallow typmod, etc.
2822 * - thomas 2000-03-22
2829 * We would like to make the second production here be ColId attrs etc,
2830 * but that causes reduce/reduce conflicts. type_name is next best choice.
2836 | type_name attrs '%' TYPE_P
2838 $$ = makeNode(TypeName);
2839 $$->names = lcons(makeString($1), $2);
2840 $$->pct_type = true;
2846 createfunc_opt_list: createfunc_opt_item
2847 { $$ = makeList1($1); }
2848 | createfunc_opt_list createfunc_opt_item
2849 { $$ = lappend($1, $2); }
2852 createfunc_opt_item: AS func_as
2854 $$ = makeNode(DefElem);
2856 $$->arg = (Node *)$2;
2858 | LANGUAGE ColId_or_Sconst
2860 $$ = makeNode(DefElem);
2861 $$->defname = "language";
2862 $$->arg = (Node *)makeString($2);
2866 $$ = makeNode(DefElem);
2867 $$->defname = "volatility";
2868 $$->arg = (Node *)makeString("immutable");
2872 $$ = makeNode(DefElem);
2873 $$->defname = "volatility";
2874 $$->arg = (Node *)makeString("stable");
2878 $$ = makeNode(DefElem);
2879 $$->defname = "volatility";
2880 $$->arg = (Node *)makeString("volatile");
2882 | CALLED ON NULL_P INPUT
2884 $$ = makeNode(DefElem);
2885 $$->defname = "strict";
2886 $$->arg = (Node *)makeInteger(FALSE);
2888 | RETURNS NULL_P ON NULL_P INPUT
2890 $$ = makeNode(DefElem);
2891 $$->defname = "strict";
2892 $$->arg = (Node *)makeInteger(TRUE);
2896 $$ = makeNode(DefElem);
2897 $$->defname = "strict";
2898 $$->arg = (Node *)makeInteger(TRUE);
2900 | EXTERNAL SECURITY DEFINER
2902 $$ = makeNode(DefElem);
2903 $$->defname = "security";
2904 $$->arg = (Node *)makeInteger(TRUE);
2906 | EXTERNAL SECURITY INVOKER
2908 $$ = makeNode(DefElem);
2909 $$->defname = "security";
2910 $$->arg = (Node *)makeInteger(FALSE);
2914 $$ = makeNode(DefElem);
2915 $$->defname = "security";
2916 $$->arg = (Node *)makeInteger(TRUE);
2920 $$ = makeNode(DefElem);
2921 $$->defname = "security";
2922 $$->arg = (Node *)makeInteger(FALSE);
2926 $$ = makeNode(DefElem);
2927 $$->defname = "implicit";
2928 $$->arg = (Node *)makeInteger(TRUE);
2933 { $$ = makeList1(makeString($1)); }
2935 { $$ = makeList2(makeString($1), makeString($3)); }
2938 opt_definition: WITH definition { $$ = $2; }
2939 | /*EMPTY*/ { $$ = NIL; }
2943 /*****************************************************************************
2947 * DROP FUNCTION funcname (arg1, arg2, ...)
2948 * DROP AGGREGATE aggname (aggtype)
2949 * DROP OPERATOR opname (leftoperand_typ rightoperand_typ)
2951 *****************************************************************************/
2953 RemoveFuncStmt: DROP FUNCTION func_name func_args
2955 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
2962 RemoveAggrStmt: DROP AGGREGATE func_name '(' aggr_argtype ')'
2964 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
2971 aggr_argtype: Typename { $$ = $1; }
2972 | '*' { $$ = NULL; }
2975 RemoveOperStmt: DROP OPERATOR any_operator '(' oper_argtypes ')'
2977 RemoveOperStmt *n = makeNode(RemoveOperStmt);
2984 oper_argtypes: Typename
2986 elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
2988 | Typename ',' Typename
2989 { $$ = makeList2($1, $3); }
2990 | NONE ',' Typename /* left unary */
2991 { $$ = makeList2(NULL, $3); }
2992 | Typename ',' NONE /* right unary */
2993 { $$ = makeList2($1, NULL); }
2996 any_operator: all_Op
2997 { $$ = makeList1(makeString($1)); }
2998 | ColId '.' any_operator
2999 { $$ = lcons(makeString($1), $3); }
3003 /*****************************************************************************
3007 * REINDEX type <typename> [FORCE] [ALL]
3009 *****************************************************************************/
3011 ReindexStmt: REINDEX reindex_type qualified_name opt_force
3013 ReindexStmt *n = makeNode(ReindexStmt);
3014 n->reindexType = $2;
3020 | REINDEX DATABASE name opt_force
3022 ReindexStmt *n = makeNode(ReindexStmt);
3023 n->reindexType = DATABASE;
3031 reindex_type: INDEX { $$ = INDEX; }
3032 | TABLE { $$ = TABLE; }
3035 opt_force: FORCE { $$ = TRUE; }
3036 | /* EMPTY */ { $$ = FALSE; }
3040 /*****************************************************************************
3043 * rename <attrname1> in <relname> [*] to <attrname2>
3044 * rename <relname1> to <relname2>
3046 *****************************************************************************/
3048 RenameStmt: ALTER TABLE relation_expr RENAME opt_column opt_name TO name
3050 RenameStmt *n = makeNode(RenameStmt);
3055 n->renameType = RENAME_TABLE;
3057 n->renameType = RENAME_COLUMN;
3060 | ALTER TRIGGER name ON relation_expr RENAME TO name
3062 RenameStmt *n = makeNode(RenameStmt);
3066 n->renameType = RENAME_TRIGGER;
3071 opt_name: name { $$ = $1; }
3072 | /*EMPTY*/ { $$ = NULL; }
3075 opt_column: COLUMN { $$ = COLUMN; }
3076 | /*EMPTY*/ { $$ = 0; }
3080 /*****************************************************************************
3082 * QUERY: Define Rewrite Rule
3084 *****************************************************************************/
3086 RuleStmt: CREATE RULE name AS
3087 { QueryIsRule=TRUE; }
3088 ON event TO qualified_name where_clause
3089 DO opt_instead RuleActionList
3091 RuleStmt *n = makeNode(RuleStmt);
3094 n->whereClause = $10;
3103 RuleActionList: NOTHING { $$ = NIL; }
3104 | RuleActionStmt { $$ = makeList1($1); }
3105 | '(' RuleActionMulti ')' { $$ = $2; }
3108 /* the thrashing around here is to discard "empty" statements... */
3109 RuleActionMulti: RuleActionMulti ';' RuleActionStmtOrEmpty
3110 { if ($3 != (Node *) NULL)
3111 $$ = lappend($1, $3);
3115 | RuleActionStmtOrEmpty
3116 { if ($1 != (Node *) NULL)
3123 RuleActionStmt: SelectStmt
3130 RuleActionStmtOrEmpty: RuleActionStmt
3132 { $$ = (Node *)NULL; }
3135 /* change me to select, update, etc. some day */
3136 event: SELECT { $$ = CMD_SELECT; }
3137 | UPDATE { $$ = CMD_UPDATE; }
3138 | DELETE_P { $$ = CMD_DELETE; }
3139 | INSERT { $$ = CMD_INSERT; }
3142 opt_instead: INSTEAD { $$ = TRUE; }
3143 | /*EMPTY*/ { $$ = FALSE; }
3147 DropRuleStmt: DROP RULE name ON qualified_name
3149 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3152 n->removeType = DROP_RULE;
3158 /*****************************************************************************
3161 * NOTIFY <qualified_name> can appear both in rule bodies and
3162 * as a query-level command
3164 *****************************************************************************/
3166 NotifyStmt: NOTIFY qualified_name
3168 NotifyStmt *n = makeNode(NotifyStmt);
3174 ListenStmt: LISTEN qualified_name
3176 ListenStmt *n = makeNode(ListenStmt);
3182 UnlistenStmt: UNLISTEN qualified_name
3184 UnlistenStmt *n = makeNode(UnlistenStmt);
3190 UnlistenStmt *n = makeNode(UnlistenStmt);
3191 n->relation = makeNode(RangeVar);
3192 n->relation->relname = "*";
3193 n->relation->schemaname = NULL;
3199 /*****************************************************************************
3203 * BEGIN / COMMIT / ROLLBACK
3204 * (also older versions END / ABORT)
3206 *****************************************************************************/
3208 TransactionStmt: ABORT_TRANS opt_trans
3210 TransactionStmt *n = makeNode(TransactionStmt);
3211 n->command = ROLLBACK;
3214 | BEGIN_TRANS opt_trans
3216 TransactionStmt *n = makeNode(TransactionStmt);
3217 n->command = BEGIN_TRANS;
3222 TransactionStmt *n = makeNode(TransactionStmt);
3223 n->command = COMMIT;
3226 | COMMIT opt_trans opt_chain
3228 TransactionStmt *n = makeNode(TransactionStmt);
3229 n->command = COMMIT;
3232 | END_TRANS opt_trans
3234 TransactionStmt *n = makeNode(TransactionStmt);
3235 n->command = COMMIT;
3238 | ROLLBACK opt_trans
3240 TransactionStmt *n = makeNode(TransactionStmt);
3241 n->command = ROLLBACK;
3244 | ROLLBACK opt_trans opt_chain
3246 TransactionStmt *n = makeNode(TransactionStmt);
3247 n->command = ROLLBACK;
3252 opt_trans: WORK { $$ = TRUE; }
3253 | TRANSACTION { $$ = TRUE; }
3254 | /*EMPTY*/ { $$ = TRUE; }
3257 opt_chain: AND NO CHAIN
3261 /* SQL99 asks that conforming dbs reject AND CHAIN
3262 * if they don't support it. So we can't just ignore it.
3263 * - thomas 2000-08-06
3265 elog(ERROR, "COMMIT / CHAIN not yet supported");
3271 /*****************************************************************************
3274 * define view <viewname> '('target-list ')' [where <quals> ]
3276 *****************************************************************************/
3278 ViewStmt: CREATE VIEW qualified_name opt_column_list AS SelectStmt
3280 ViewStmt *n = makeNode(ViewStmt);
3283 n->query = (Query *) $6;
3289 /*****************************************************************************
3294 *****************************************************************************/
3296 LoadStmt: LOAD file_name
3298 LoadStmt *n = makeNode(LoadStmt);
3305 /*****************************************************************************
3309 *****************************************************************************/
3311 CreatedbStmt: CREATE DATABASE database_name opt_with createdb_opt_list
3313 CreatedbStmt *n = makeNode(CreatedbStmt);
3317 /* set default options */
3320 n->dbtemplate = NULL;
3322 /* process additional options */
3325 List *optitem = (List *) lfirst(l);
3327 switch (lfirsti(optitem))
3330 n->dbpath = (char *) lsecond(optitem);
3333 n->dbtemplate = (char *) lsecond(optitem);
3336 n->encoding = lfirsti(lnext(optitem));
3339 n->dbowner = (char *) lsecond(optitem);
3345 | CREATE DATABASE database_name
3347 CreatedbStmt *n = makeNode(CreatedbStmt);
3351 n->dbtemplate = NULL;
3357 createdb_opt_list: createdb_opt_item
3358 { $$ = makeList1($1); }
3359 | createdb_opt_list createdb_opt_item
3360 { $$ = lappend($1, $2); }
3364 * createdb_opt_item returns 2-element lists, with the first element
3365 * being an integer code to indicate which item was specified.
3367 createdb_opt_item: LOCATION opt_equal Sconst
3369 $$ = lconsi(1, makeList1($3));
3371 | LOCATION opt_equal DEFAULT
3373 $$ = lconsi(1, makeList1(NULL));
3375 | TEMPLATE opt_equal name
3377 $$ = lconsi(2, makeList1($3));
3379 | TEMPLATE opt_equal DEFAULT
3381 $$ = lconsi(2, makeList1(NULL));
3383 | ENCODING opt_equal Sconst
3387 encoding = pg_char_to_encoding($3);
3389 elog(ERROR, "%s is not a valid encoding name", $3);
3391 if (strcasecmp($3, GetStandardEncodingName()) != 0)
3392 elog(ERROR, "Multi-byte support is not enabled");
3393 encoding = GetStandardEncoding();
3395 $$ = lconsi(3, makeListi1(encoding));
3397 | ENCODING opt_equal Iconst
3400 if (!pg_get_enconv_by_encoding($3))
3401 elog(ERROR, "%d is not a valid encoding code", $3);
3403 if ($3 != GetStandardEncoding())
3404 elog(ERROR, "Multi-byte support is not enabled");
3406 $$ = lconsi(3, makeListi1($3));
3408 | ENCODING opt_equal DEFAULT
3410 $$ = lconsi(3, makeListi1(-1));
3412 | OWNER opt_equal name
3414 $$ = lconsi(4, makeList1($3));
3416 | OWNER opt_equal DEFAULT
3418 $$ = lconsi(4, makeList1(NULL));
3423 * Though the equals sign doesn't match other WITH options, pg_dump uses
3424 * equals for backward compability, and it doesn't seem worth removing it.
3427 opt_equal: '=' { $$ = TRUE; }
3428 | /*EMPTY*/ { $$ = FALSE; }
3432 /*****************************************************************************
3436 *****************************************************************************/
3438 AlterDatabaseSetStmt: ALTER DATABASE database_name SET set_rest
3440 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3442 n->variable = $5->name;
3443 n->value = $5->args;
3446 | ALTER DATABASE database_name VariableResetStmt
3448 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3450 n->variable = ((VariableResetStmt *)$4)->name;
3457 /*****************************************************************************
3461 *****************************************************************************/
3463 DropdbStmt: DROP DATABASE database_name
3465 DropdbStmt *n = makeNode(DropdbStmt);
3472 /*****************************************************************************
3474 * Manipulate a domain
3476 *****************************************************************************/
3478 CreateDomainStmt: CREATE DOMAIN_P any_name opt_as Typename ColQualList opt_collate
3480 CreateDomainStmt *n = makeNode(CreateDomainStmt);
3483 n->constraints = $6;
3486 elog(NOTICE,"CREATE DOMAIN / COLLATE %s not yet "
3487 "implemented; clause ignored", $7);
3492 opt_as: AS {$$ = TRUE; }
3493 | /* EMPTY */ {$$ = FALSE; }
3497 /*****************************************************************************
3500 * cluster <index_name> on <qualified_name>
3502 *****************************************************************************/
3504 ClusterStmt: CLUSTER index_name ON qualified_name
3506 ClusterStmt *n = makeNode(ClusterStmt);
3513 /*****************************************************************************
3519 *****************************************************************************/
3521 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
3523 VacuumStmt *n = makeNode(VacuumStmt);
3533 | VACUUM opt_full opt_freeze opt_verbose qualified_name
3535 VacuumStmt *n = makeNode(VacuumStmt);
3545 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
3547 VacuumStmt *n = (VacuumStmt *) $5;
3556 AnalyzeStmt: analyze_keyword opt_verbose
3558 VacuumStmt *n = makeNode(VacuumStmt);
3568 | analyze_keyword opt_verbose qualified_name opt_name_list
3570 VacuumStmt *n = makeNode(VacuumStmt);
3582 analyze_keyword: ANALYZE { $$ = TRUE; }
3583 | ANALYSE /* British */ { $$ = TRUE; }
3586 opt_verbose: VERBOSE { $$ = TRUE; }
3587 | /*EMPTY*/ { $$ = FALSE; }
3590 opt_full: FULL { $$ = TRUE; }
3591 | /*EMPTY*/ { $$ = FALSE; }
3594 opt_freeze: FREEZE { $$ = TRUE; }
3595 | /*EMPTY*/ { $$ = FALSE; }
3598 opt_name_list: '(' name_list ')' { $$ = $2; }
3599 | /*EMPTY*/ { $$ = NIL; }
3603 /*****************************************************************************
3607 * EXPLAIN ANALYZE query
3609 *****************************************************************************/
3611 ExplainStmt: EXPLAIN opt_verbose OptimizableStmt
3613 ExplainStmt *n = makeNode(ExplainStmt);
3616 n->query = (Query*)$3;
3619 | EXPLAIN analyze_keyword opt_verbose OptimizableStmt
3621 ExplainStmt *n = makeNode(ExplainStmt);
3624 n->query = (Query*)$4;
3630 /*****************************************************************************
3632 * Optimizable Stmts: *
3634 * one of the five queries processed by the planner *
3636 * [ultimately] produces query-trees as specified *
3637 * in the query-spec document in ~postgres/ref *
3639 *****************************************************************************/
3641 OptimizableStmt: SelectStmt
3645 | DeleteStmt /* by default all are $$=$1 */
3649 /*****************************************************************************
3654 *****************************************************************************/
3656 InsertStmt: INSERT INTO qualified_name insert_rest
3663 insert_rest: VALUES '(' insert_target_list ')'
3665 $$ = makeNode(InsertStmt);
3667 $$->targetList = $3;
3668 $$->selectStmt = NULL;
3672 $$ = makeNode(InsertStmt);
3674 $$->targetList = NIL;
3675 $$->selectStmt = NULL;
3679 $$ = makeNode(InsertStmt);
3681 $$->targetList = NIL;
3682 $$->selectStmt = $1;
3684 | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
3686 $$ = makeNode(InsertStmt);
3688 $$->targetList = $6;
3689 $$->selectStmt = NULL;
3691 | '(' insert_column_list ')' SelectStmt
3693 $$ = makeNode(InsertStmt);
3695 $$->targetList = NIL;
3696 $$->selectStmt = $4;
3700 insert_column_list: insert_column_list ',' insert_column_item
3701 { $$ = lappend($1, $3); }
3702 | insert_column_item
3703 { $$ = makeList1($1); }
3706 insert_column_item: ColId opt_indirection
3708 ResTarget *n = makeNode(ResTarget);
3710 n->indirection = $2;
3717 /*****************************************************************************
3722 *****************************************************************************/
3724 DeleteStmt: DELETE_P FROM relation_expr where_clause
3726 DeleteStmt *n = makeNode(DeleteStmt);
3728 n->whereClause = $4;
3733 LockStmt: LOCK_P opt_table qualified_name_list opt_lock
3735 LockStmt *n = makeNode(LockStmt);
3743 opt_lock: IN_P lock_type MODE { $$ = $2; }
3744 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
3747 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
3748 | ROW SHARE { $$ = RowShareLock; }
3749 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
3750 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
3751 | SHARE { $$ = ShareLock; }
3752 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
3753 | EXCLUSIVE { $$ = ExclusiveLock; }
3754 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
3758 /*****************************************************************************
3761 * UpdateStmt (UPDATE)
3763 *****************************************************************************/
3765 UpdateStmt: UPDATE relation_expr
3766 SET update_target_list
3770 UpdateStmt *n = makeNode(UpdateStmt);
3774 n->whereClause = $6;
3780 /*****************************************************************************
3785 *****************************************************************************/
3786 CursorStmt: DECLARE name opt_cursor CURSOR FOR SelectStmt
3788 SelectStmt *n = (SelectStmt *)$6;
3795 opt_cursor: BINARY { $$ = TRUE; }
3796 | INSENSITIVE { $$ = FALSE; }
3797 | SCROLL { $$ = FALSE; }
3798 | INSENSITIVE SCROLL { $$ = FALSE; }
3799 | /*EMPTY*/ { $$ = FALSE; }
3802 /*****************************************************************************
3807 *****************************************************************************/
3809 /* A complete SELECT statement looks like this.
3811 * The rule returns either a single SelectStmt node or a tree of them,
3812 * representing a set-operation tree.
3814 * There is an ambiguity when a sub-SELECT is within an a_expr and there
3815 * are excess parentheses: do the parentheses belong to the sub-SELECT or
3816 * to the surrounding a_expr? We don't really care, but yacc wants to know.
3817 * To resolve the ambiguity, we are careful to define the grammar so that
3818 * the decision is staved off as long as possible: as long as we can keep
3819 * absorbing parentheses into the sub-SELECT, we will do so, and only when
3820 * it's no longer possible to do that will we decide that parens belong to
3821 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
3822 * parentheses are treated as part of the sub-select. The necessity of doing
3823 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
3824 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
3825 * SELECT viewpoint when we see the UNION.
3827 * This approach is implemented by defining a nonterminal select_with_parens,
3828 * which represents a SELECT with at least one outer layer of parentheses,
3829 * and being careful to use select_with_parens, never '(' SelectStmt ')',
3830 * in the expression grammar. We will then have shift-reduce conflicts
3831 * which we can resolve in favor of always treating '(' <select> ')' as
3832 * a select_with_parens. To resolve the conflicts, the productions that
3833 * conflict with the select_with_parens productions are manually given
3834 * precedences lower than the precedence of ')', thereby ensuring that we
3835 * shift ')' (and then reduce to select_with_parens) rather than trying to
3836 * reduce the inner <select> nonterminal to something else. We use UMINUS
3837 * precedence for this, which is a fairly arbitrary choice.
3839 * To be able to define select_with_parens itself without ambiguity, we need
3840 * a nonterminal select_no_parens that represents a SELECT structure with no
3841 * outermost parentheses. This is a little bit tedious, but it works.
3843 * In non-expression contexts, we use SelectStmt which can represent a SELECT
3844 * with or without outer parentheses.
3847 SelectStmt: select_no_parens %prec UMINUS
3848 | select_with_parens %prec UMINUS
3851 select_with_parens: '(' select_no_parens ')'
3855 | '(' select_with_parens ')'
3861 select_no_parens: simple_select
3865 | select_clause sort_clause opt_for_update_clause opt_select_limit
3867 insertSelectOptions((SelectStmt *) $1, $2, $3,
3868 nth(0, $4), nth(1, $4));
3871 | select_clause for_update_clause opt_select_limit
3873 insertSelectOptions((SelectStmt *) $1, NIL, $2,
3874 nth(0, $3), nth(1, $3));
3877 | select_clause select_limit
3879 insertSelectOptions((SelectStmt *) $1, NIL, NIL,
3880 nth(0, $2), nth(1, $2));
3885 select_clause: simple_select
3886 | select_with_parens
3890 * This rule parses SELECT statements that can appear within set operations,
3891 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
3892 * the ordering of the set operations. Without '(' and ')' we want the
3893 * operations to be ordered per the precedence specs at the head of this file.
3895 * As with select_no_parens, simple_select cannot have outer parentheses,
3896 * but can have parenthesized subclauses.
3898 * Note that sort clauses cannot be included at this level --- SQL92 requires
3899 * SELECT foo UNION SELECT bar ORDER BY baz
3901 * (SELECT foo UNION SELECT bar) ORDER BY baz
3903 * SELECT foo UNION (SELECT bar ORDER BY baz)
3904 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
3905 * as part of the select_no_parens production, not simple_select.
3906 * This does not limit functionality, because you can reintroduce sort and
3907 * limit clauses inside parentheses.
3909 * NOTE: only the leftmost component SelectStmt should have INTO.
3910 * However, this is not checked by the grammar; parse analysis must check it.
3912 simple_select: SELECT opt_distinct target_list
3913 into_clause from_clause where_clause
3914 group_clause having_clause
3916 SelectStmt *n = makeNode(SelectStmt);
3917 n->distinctClause = $2;
3920 n->intoColNames = NIL;
3922 n->whereClause = $6;
3923 n->groupClause = $7;
3924 n->havingClause = $8;
3927 | select_clause UNION opt_all select_clause
3929 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
3931 | select_clause INTERSECT opt_all select_clause
3933 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
3935 | select_clause EXCEPT opt_all select_clause
3937 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
3941 into_clause: INTO OptTempTableName { $$ = $2; }
3942 | /*EMPTY*/ { $$ = NULL; }
3946 * Redundancy here is needed to avoid shift/reduce conflicts,
3947 * since TEMP is not a reserved word. See also OptTemp.
3949 OptTempTableName: TEMPORARY opt_table qualified_name
3954 | TEMP opt_table qualified_name
3959 | LOCAL TEMPORARY opt_table qualified_name
3964 | LOCAL TEMP opt_table qualified_name
3969 | GLOBAL TEMPORARY opt_table qualified_name
3971 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
3975 | GLOBAL TEMP opt_table qualified_name
3977 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
3981 | TABLE qualified_name
3993 opt_table: TABLE { $$ = TRUE; }
3994 | /*EMPTY*/ { $$ = FALSE; }
3997 opt_all: ALL { $$ = TRUE; }
3998 | /*EMPTY*/ { $$ = FALSE; }
4001 /* We use (NIL) as a placeholder to indicate that all target expressions
4002 * should be placed in the DISTINCT list during parsetree analysis.
4004 opt_distinct: DISTINCT { $$ = makeList1(NIL); }
4005 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
4007 | /*EMPTY*/ { $$ = NIL; }
4010 sort_clause: ORDER BY sortby_list { $$ = $3; }
4013 sortby_list: sortby { $$ = makeList1($1); }
4014 | sortby_list ',' sortby { $$ = lappend($1, $3); }
4017 sortby: a_expr OptUseOp
4019 $$ = makeNode(SortGroupBy);
4025 OptUseOp: USING qual_all_Op
4028 { $$ = makeList1(makeString("<")); }
4030 { $$ = makeList1(makeString(">")); }
4032 { $$ = makeList1(makeString("<")); /*default*/ }
4036 select_limit: LIMIT select_limit_value OFFSET select_offset_value
4037 { $$ = makeList2($4, $2); }
4038 | OFFSET select_offset_value LIMIT select_limit_value
4039 { $$ = makeList2($2, $4); }
4040 | LIMIT select_limit_value
4041 { $$ = makeList2(NULL, $2); }
4042 | OFFSET select_offset_value
4043 { $$ = makeList2($2, NULL); }
4044 | LIMIT select_limit_value ',' select_offset_value
4045 /* Disabled because it was too confusing, bjm 2002-02-18 */
4046 { elog(ERROR, "LIMIT #,# syntax not supported.\n\tUse separate LIMIT and OFFSET clauses."); }
4050 opt_select_limit: select_limit { $$ = $1; }
4051 | /* EMPTY */ { $$ = makeList2(NULL,NULL); }
4054 select_limit_value: Iconst
4056 Const *n = makeNode(Const);
4059 elog(ERROR, "LIMIT must not be negative");
4061 n->consttype = INT4OID;
4062 n->constlen = sizeof(int4);
4063 n->constvalue = Int32GetDatum($1);
4064 n->constisnull = FALSE;
4065 n->constbyval = TRUE;
4066 n->constisset = FALSE;
4067 n->constiscast = FALSE;
4072 /* LIMIT ALL is represented as a NULL constant */
4073 Const *n = makeNode(Const);
4075 n->consttype = INT4OID;
4076 n->constlen = sizeof(int4);
4077 n->constvalue = (Datum) 0;
4078 n->constisnull = TRUE;
4079 n->constbyval = TRUE;
4080 n->constisset = FALSE;
4081 n->constiscast = FALSE;
4086 Param *n = makeNode(Param);
4088 n->paramkind = PARAM_NUM;
4090 n->paramtype = INT4OID;
4095 select_offset_value: Iconst
4097 Const *n = makeNode(Const);
4100 elog(ERROR, "OFFSET must not be negative");
4102 n->consttype = INT4OID;
4103 n->constlen = sizeof(int4);
4104 n->constvalue = Int32GetDatum($1);
4105 n->constisnull = FALSE;
4106 n->constbyval = TRUE;
4107 n->constisset = FALSE;
4108 n->constiscast = FALSE;
4113 Param *n = makeNode(Param);
4115 n->paramkind = PARAM_NUM;
4117 n->paramtype = INT4OID;
4123 * jimmy bell-style recursive queries aren't supported in the
4126 * ...however, recursive addattr and rename supported. make special
4130 group_clause: GROUP_P BY expr_list { $$ = $3; }
4131 | /*EMPTY*/ { $$ = NIL; }
4134 having_clause: HAVING a_expr
4138 | /*EMPTY*/ { $$ = NULL; }
4141 for_update_clause: FOR UPDATE update_list { $$ = $3; }
4142 | FOR READ ONLY { $$ = NULL; }
4145 opt_for_update_clause: for_update_clause { $$ = $1; }
4146 | /* EMPTY */ { $$ = NULL; }
4149 update_list: OF name_list { $$ = $2; }
4150 | /* EMPTY */ { $$ = makeList1(NULL); }
4153 /*****************************************************************************
4155 * clauses common to all Optimizable Stmts:
4156 * from_clause - allow list of both JOIN expressions and table names
4157 * where_clause - qualifications for joins or restrictions
4159 *****************************************************************************/
4161 from_clause: FROM from_list { $$ = $2; }
4162 | /*EMPTY*/ { $$ = NIL; }
4165 from_list: from_list ',' table_ref { $$ = lappend($1, $3); }
4166 | table_ref { $$ = makeList1($1); }
4170 * table_ref is where an alias clause can be attached. Note we cannot make
4171 * alias_clause have an empty production because that causes parse conflicts
4172 * between table_ref := '(' joined_table ')' alias_clause
4173 * and joined_table := '(' joined_table ')'. So, we must have the
4174 * redundant-looking productions here instead.
4176 table_ref: relation_expr
4180 | relation_expr alias_clause
4187 RangeFunction *n = makeNode(RangeFunction);
4188 n->funccallnode = $1;
4191 | func_table alias_clause
4193 RangeFunction *n = makeNode(RangeFunction);
4194 n->funccallnode = $1;
4198 | select_with_parens
4201 * The SQL spec does not permit a subselect
4202 * (<derived_table>) without an alias clause,
4203 * so we don't either. This avoids the problem
4204 * of needing to invent a unique refname for it.
4205 * That could be surmounted if there's sufficient
4206 * popular demand, but for now let's just implement
4207 * the spec and see if anyone complains.
4208 * However, it does seem like a good idea to emit
4209 * an error message that's better than "parse error".
4211 elog(ERROR, "sub-SELECT in FROM must have an alias"
4212 "\n\tFor example, FROM (SELECT ...) [AS] foo");
4215 | select_with_parens alias_clause
4217 RangeSubselect *n = makeNode(RangeSubselect);
4226 | '(' joined_table ')' alias_clause
4235 * It may seem silly to separate joined_table from table_ref, but there is
4236 * method in SQL92's madness: if you don't do it this way you get reduce-
4237 * reduce conflicts, because it's not clear to the parser generator whether
4238 * to expect alias_clause after ')' or not. For the same reason we must
4239 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
4240 * join_type to expand to empty; if we try it, the parser generator can't
4241 * figure out when to reduce an empty join_type right after table_ref.
4243 * Note that a CROSS JOIN is the same as an unqualified
4244 * INNER JOIN, and an INNER JOIN/ON has the same shape
4245 * but a qualification expression to limit membership.
4246 * A NATURAL JOIN implicitly matches column names between
4247 * tables and the shape is determined by which columns are
4248 * in common. We'll collect columns during the later transformations.
4251 joined_table: '(' joined_table ')'
4255 | table_ref CROSS JOIN table_ref
4257 /* CROSS JOIN is same as unqualified inner join */
4258 JoinExpr *n = makeNode(JoinExpr);
4259 n->jointype = JOIN_INNER;
4260 n->isNatural = FALSE;
4267 | table_ref UNIONJOIN table_ref
4269 /* UNION JOIN is made into 1 token to avoid shift/reduce
4270 * conflict against regular UNION keyword.
4272 JoinExpr *n = makeNode(JoinExpr);
4273 n->jointype = JOIN_UNION;
4274 n->isNatural = FALSE;
4281 | table_ref join_type JOIN table_ref join_qual
4283 JoinExpr *n = makeNode(JoinExpr);
4285 n->isNatural = FALSE;
4288 if ($5 != NULL && IsA($5, List))
4289 n->using = (List *) $5; /* USING clause */
4291 n->quals = $5; /* ON clause */
4294 | table_ref JOIN table_ref join_qual
4296 /* letting join_type reduce to empty doesn't work */
4297 JoinExpr *n = makeNode(JoinExpr);
4298 n->jointype = JOIN_INNER;
4299 n->isNatural = FALSE;
4302 if ($4 != NULL && IsA($4, List))
4303 n->using = (List *) $4; /* USING clause */
4305 n->quals = $4; /* ON clause */
4308 | table_ref NATURAL join_type JOIN table_ref
4310 JoinExpr *n = makeNode(JoinExpr);
4312 n->isNatural = TRUE;
4315 n->using = NIL; /* figure out which columns later... */
4316 n->quals = NULL; /* fill later */
4319 | table_ref NATURAL JOIN table_ref
4321 /* letting join_type reduce to empty doesn't work */
4322 JoinExpr *n = makeNode(JoinExpr);
4323 n->jointype = JOIN_INNER;
4324 n->isNatural = TRUE;
4327 n->using = NIL; /* figure out which columns later... */
4328 n->quals = NULL; /* fill later */
4333 alias_clause: AS ColId '(' name_list ')'
4335 $$ = makeNode(Alias);
4341 $$ = makeNode(Alias);
4344 | ColId '(' name_list ')'
4346 $$ = makeNode(Alias);
4352 $$ = makeNode(Alias);
4357 join_type: FULL join_outer { $$ = JOIN_FULL; }
4358 | LEFT join_outer { $$ = JOIN_LEFT; }
4359 | RIGHT join_outer { $$ = JOIN_RIGHT; }
4360 | INNER_P { $$ = JOIN_INNER; }
4363 /* OUTER is just noise... */
4364 join_outer: OUTER_P { $$ = NULL; }
4365 | /*EMPTY*/ { $$ = NULL; }
4368 /* JOIN qualification clauses
4369 * Possibilities are:
4370 * USING ( column list ) allows only unqualified column names,
4371 * which must match between tables.
4372 * ON expr allows more general qualifications.
4374 * We return USING as a List node, while an ON-expr will not be a List.
4377 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
4378 | ON a_expr { $$ = $2; }
4382 relation_expr: qualified_name
4384 /* default inheritance */
4386 $$->inhOpt = INH_DEFAULT;
4389 | qualified_name '*'
4391 /* inheritance query */
4393 $$->inhOpt = INH_YES;
4396 | ONLY qualified_name
4398 /* no inheritance */
4400 $$->inhOpt = INH_NO;
4406 func_table: func_name '(' ')'
4408 FuncCall *n = makeNode(FuncCall);
4411 n->agg_star = FALSE;
4412 n->agg_distinct = FALSE;
4415 | func_name '(' expr_list ')'
4417 FuncCall *n = makeNode(FuncCall);
4420 n->agg_star = FALSE;
4421 n->agg_distinct = FALSE;
4427 where_clause: WHERE a_expr { $$ = $2; }
4428 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
4432 /*****************************************************************************
4435 * SQL92 introduces a large amount of type-specific syntax.
4436 * Define individual clauses to handle these cases, and use
4437 * the generic case to handle regular type-extensible Postgres syntax.
4438 * - thomas 1997-10-10
4440 *****************************************************************************/
4442 Typename: SimpleTypename opt_array_bounds
4445 $$->arrayBounds = $2;
4447 | SETOF SimpleTypename
4454 opt_array_bounds: opt_array_bounds '[' ']'
4455 { $$ = lappend($1, makeInteger(-1)); }
4456 | opt_array_bounds '[' Iconst ']'
4457 { $$ = lappend($1, makeInteger($3)); }
4463 * XXX ideally, the production for a qualified typename should be ColId attrs
4464 * (there's no obvious reason why the first name should need to be restricted)
4465 * and should be an alternative of GenericType (so that it can be used to
4466 * specify a type for a literal in AExprConst). However doing either causes
4467 * reduce/reduce conflicts that I haven't been able to find a workaround
4470 SimpleTypename: ConstTypename
4471 | ConstInterval opt_interval
4475 $$->typmod = ((($2 & 0x7FFF) << 16) | 0xFFFF);
4477 | ConstInterval '(' Iconst ')' opt_interval
4480 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
4481 elog(ERROR, "INTERVAL(%d) precision must be between %d and %d",
4482 $3, 0, MAX_INTERVAL_PRECISION);
4483 $$->typmod = ((($5 & 0x7FFF) << 16) | $3);
4487 $$ = makeNode(TypeName);
4488 $$->names = lcons(makeString($1), $2);
4493 ConstTypename: GenericType
4500 GenericType: type_name
4502 $$ = makeTypeName($1);
4506 /* SQL92 numeric data types
4507 * Check FLOAT() precision limits assuming IEEE floating types.
4508 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
4509 * - thomas 1997-09-18
4513 $$ = SystemTypeName("int4");
4517 $$ = SystemTypeName("int4");
4521 $$ = SystemTypeName("int2");
4525 $$ = SystemTypeName("int8");
4529 $$ = SystemTypeName("float4");
4537 $$ = SystemTypeName("float8");
4539 | DECIMAL opt_decimal
4541 $$ = SystemTypeName("numeric");
4546 $$ = SystemTypeName("numeric");
4549 | NUMERIC opt_numeric
4551 $$ = SystemTypeName("numeric");
4556 $$ = SystemTypeName("bool");
4560 opt_float: '(' Iconst ')'
4563 elog(ERROR, "precision for FLOAT must be at least 1");
4565 $$ = SystemTypeName("float4");
4567 $$ = SystemTypeName("float8");
4569 elog(ERROR, "precision for FLOAT must be less than 16");
4573 $$ = SystemTypeName("float8");
4577 opt_numeric: '(' Iconst ',' Iconst ')'
4579 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4580 elog(ERROR, "NUMERIC precision %d must be between 1 and %d",
4581 $2, NUMERIC_MAX_PRECISION);
4582 if ($4 < 0 || $4 > $2)
4583 elog(ERROR, "NUMERIC scale %d must be between 0 and precision %d",
4586 $$ = (($2 << 16) | $4) + VARHDRSZ;
4590 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4591 elog(ERROR, "NUMERIC precision %d must be between 1 and %d",
4592 $2, NUMERIC_MAX_PRECISION);
4594 $$ = ($2 << 16) + VARHDRSZ;
4598 /* Insert "-1" meaning "no limit" */
4603 opt_decimal: '(' Iconst ',' Iconst ')'
4605 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4606 elog(ERROR, "DECIMAL precision %d must be between 1 and %d",
4607 $2, NUMERIC_MAX_PRECISION);
4608 if ($4 < 0 || $4 > $2)
4609 elog(ERROR, "DECIMAL scale %d must be between 0 and precision %d",
4612 $$ = (($2 << 16) | $4) + VARHDRSZ;
4616 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4617 elog(ERROR, "DECIMAL precision %d must be between 1 and %d",
4618 $2, NUMERIC_MAX_PRECISION);
4620 $$ = ($2 << 16) + VARHDRSZ;
4624 /* Insert "-1" meaning "no limit" */
4631 * SQL92 bit-field data types
4632 * The following implements BIT() and BIT VARYING().
4634 Bit: BIT opt_varying '(' Iconst ')'
4638 typname = $2 ? "varbit" : "bit";
4639 $$ = SystemTypeName(typname);
4641 elog(ERROR, "length for type '%s' must be at least 1",
4643 else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
4644 elog(ERROR, "length for type '%s' cannot exceed %d",
4645 typname, (MaxAttrSize * BITS_PER_BYTE));
4650 /* bit defaults to bit(1), varbit to no limit */
4653 $$ = SystemTypeName("varbit");
4658 $$ = SystemTypeName("bit");
4666 * SQL92 character data types
4667 * The following implements CHAR() and VARCHAR().
4669 Character: character '(' Iconst ')' opt_charset
4671 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
4675 type = palloc(strlen($1) + 1 + strlen($5) + 1);
4682 $$ = SystemTypeName($1);
4685 elog(ERROR, "length for type '%s' must be at least 1",
4687 else if ($3 > MaxAttrSize)
4688 elog(ERROR, "length for type '%s' cannot exceed %d",
4691 /* we actually implement these like a varlen, so
4692 * the first 4 bytes is the length. (the difference
4693 * between these and "text" is that we blank-pad and
4694 * truncate where necessary)
4696 $$->typmod = VARHDRSZ + $3;
4698 | character opt_charset
4700 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
4704 type = palloc(strlen($1) + 1 + strlen($2) + 1);
4711 $$ = SystemTypeName($1);
4713 /* char defaults to char(1), varchar to no limit */
4714 if (strcmp($1, "bpchar") == 0)
4715 $$->typmod = VARHDRSZ + 1;
4721 character: CHARACTER opt_varying { $$ = $2 ? "varchar": "bpchar"; }
4722 | CHAR_P opt_varying { $$ = $2 ? "varchar": "bpchar"; }
4723 | VARCHAR { $$ = "varchar"; }
4724 | NATIONAL CHARACTER opt_varying { $$ = $3 ? "varchar": "bpchar"; }
4725 | NATIONAL CHAR_P opt_varying { $$ = $3 ? "varchar": "bpchar"; }
4726 | NCHAR opt_varying { $$ = $2 ? "varchar": "bpchar"; }
4729 opt_varying: VARYING { $$ = TRUE; }
4730 | /*EMPTY*/ { $$ = FALSE; }
4733 opt_charset: CHARACTER SET ColId { $$ = $3; }
4734 | /*EMPTY*/ { $$ = NULL; }
4737 opt_collate: COLLATE ColId { $$ = $2; }
4738 | /*EMPTY*/ { $$ = NULL; }
4741 ConstDatetime: TIMESTAMP '(' Iconst ')' opt_timezone
4744 $$ = SystemTypeName("timestamptz");
4746 $$ = SystemTypeName("timestamp");
4747 /* XXX the timezone field seems to be unused
4748 * - thomas 2001-09-06
4751 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
4752 elog(ERROR, "TIMESTAMP(%d)%s precision must be between %d and %d",
4753 $3, ($5 ? " WITH TIME ZONE": ""), 0, MAX_TIMESTAMP_PRECISION);
4756 | TIMESTAMP opt_timezone
4759 $$ = SystemTypeName("timestamptz");
4761 $$ = SystemTypeName("timestamp");
4762 /* XXX the timezone field seems to be unused
4763 * - thomas 2001-09-06
4766 /* SQL99 specified a default precision of six
4767 * for schema definitions. But for timestamp
4768 * literals we don't want to throw away precision
4769 * so leave this as unspecified for now.
4770 * Later, we may want a different production
4771 * for schemas. - thomas 2001-12-07
4775 | TIME '(' Iconst ')' opt_timezone
4778 $$ = SystemTypeName("timetz");
4780 $$ = SystemTypeName("time");
4781 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
4782 elog(ERROR, "TIME(%d)%s precision must be between %d and %d",
4783 $3, ($5 ? " WITH TIME ZONE": ""), 0, MAX_TIME_PRECISION);
4789 $$ = SystemTypeName("timetz");
4791 $$ = SystemTypeName("time");
4792 /* SQL99 specified a default precision of zero.
4793 * See comments for timestamp above on why we will
4794 * leave this unspecified for now. - thomas 2001-12-07
4800 ConstInterval: INTERVAL
4802 $$ = SystemTypeName("interval");
4806 opt_timezone: WITH TIME ZONE { $$ = TRUE; }
4807 | WITHOUT TIME ZONE { $$ = FALSE; }
4808 | /*EMPTY*/ { $$ = FALSE; }
4811 opt_interval: YEAR_P { $$ = MASK(YEAR); }
4812 | MONTH_P { $$ = MASK(MONTH); }
4813 | DAY_P { $$ = MASK(DAY); }
4814 | HOUR_P { $$ = MASK(HOUR); }
4815 | MINUTE_P { $$ = MASK(MINUTE); }
4816 | SECOND_P { $$ = MASK(SECOND); }
4817 | YEAR_P TO MONTH_P { $$ = MASK(YEAR) | MASK(MONTH); }
4818 | DAY_P TO HOUR_P { $$ = MASK(DAY) | MASK(HOUR); }
4819 | DAY_P TO MINUTE_P { $$ = MASK(DAY) | MASK(HOUR) | MASK(MINUTE); }
4820 | DAY_P TO SECOND_P { $$ = MASK(DAY) | MASK(HOUR) | MASK(MINUTE) | MASK(SECOND); }
4821 | HOUR_P TO MINUTE_P { $$ = MASK(HOUR) | MASK(MINUTE); }
4822 | HOUR_P TO SECOND_P { $$ = MASK(HOUR) | MASK(MINUTE) | MASK(SECOND); }
4823 | MINUTE_P TO SECOND_P { $$ = MASK(MINUTE) | MASK(SECOND); }
4824 | /*EMPTY*/ { $$ = -1; }
4828 /*****************************************************************************
4830 * expression grammar
4832 *****************************************************************************/
4834 /* Expressions using row descriptors
4835 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
4836 * with singleton expressions.
4838 row_expr: '(' row_descriptor ')' IN_P select_with_parens
4840 SubLink *n = makeNode(SubLink);
4842 n->oper = (List *) makeSimpleA_Expr(OP, "=", NULL, NULL);
4844 n->subLinkType = ANY_SUBLINK;
4848 | '(' row_descriptor ')' NOT IN_P select_with_parens
4850 SubLink *n = makeNode(SubLink);
4852 n->oper = (List *) makeSimpleA_Expr(OP, "<>", NULL, NULL);
4854 n->subLinkType = ALL_SUBLINK;
4858 | '(' row_descriptor ')' qual_all_Op sub_type select_with_parens %prec Op
4860 SubLink *n = makeNode(SubLink);
4862 n->oper = (List *) makeA_Expr(OP, $4, NULL, NULL);
4863 if (strcmp(strVal(llast($4)), "<>") == 0)
4867 n->subLinkType = $5;
4871 | '(' row_descriptor ')' qual_all_Op select_with_parens %prec Op
4873 SubLink *n = makeNode(SubLink);
4875 n->oper = (List *) makeA_Expr(OP, $4, NULL, NULL);
4876 if (strcmp(strVal(llast($4)), "<>") == 0)
4880 n->subLinkType = MULTIEXPR_SUBLINK;
4884 | '(' row_descriptor ')' qual_all_Op '(' row_descriptor ')' %prec Op
4886 $$ = makeRowExpr($4, $2, $6);
4888 | '(' row_descriptor ')' OVERLAPS '(' row_descriptor ')'
4890 FuncCall *n = makeNode(FuncCall);
4893 n->funcname = SystemFuncName("overlaps");
4894 if (length(largs) == 1)
4895 largs = lappend(largs, $2);
4896 else if (length(largs) != 2)
4897 elog(ERROR, "Wrong number of parameters"
4898 " on left side of OVERLAPS expression");
4899 if (length(rargs) == 1)
4900 rargs = lappend(rargs, $6);
4901 else if (length(rargs) != 2)
4902 elog(ERROR, "Wrong number of parameters"
4903 " on right side of OVERLAPS expression");
4904 n->args = nconc(largs, rargs);
4905 n->agg_star = FALSE;
4906 n->agg_distinct = FALSE;
4911 row_descriptor: row_list ',' a_expr
4913 $$ = lappend($1, $3);
4917 row_list: row_list ',' a_expr
4919 $$ = lappend($1, $3);
4927 sub_type: ANY { $$ = ANY_SUBLINK; }
4928 | SOME { $$ = ANY_SUBLINK; }
4929 | ALL { $$ = ALL_SUBLINK; }
4932 all_Op: Op | MathOp;
4934 MathOp: '+' { $$ = "+"; }
4946 { $$ = makeList1(makeString($1)); }
4947 | OPERATOR '(' any_operator ')'
4952 { $$ = makeList1(makeString($1)); }
4953 | OPERATOR '(' any_operator ')'
4958 * General expressions
4959 * This is the heart of the expression syntax.
4961 * We have two expression types: a_expr is the unrestricted kind, and
4962 * b_expr is a subset that must be used in some places to avoid shift/reduce
4963 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
4964 * because that use of AND conflicts with AND as a boolean operator. So,
4965 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
4967 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
4968 * always be used by surrounding it with parens.
4970 * c_expr is all the productions that are common to a_expr and b_expr;
4971 * it's factored out just to eliminate redundant coding.
4975 | a_expr TYPECAST Typename
4976 { $$ = makeTypeCast($1, $3); }
4977 | a_expr COLLATE ColId
4979 FuncCall *n = makeNode(FuncCall);
4980 n->funcname = SystemFuncName($3);
4981 n->args = makeList1($1);
4982 n->agg_star = FALSE;
4983 n->agg_distinct = FALSE;
4986 | a_expr AT TIME ZONE c_expr
4988 FuncCall *n = makeNode(FuncCall);
4989 n->funcname = SystemFuncName("timezone");
4990 n->args = makeList2($5, $1);
4991 n->agg_star = FALSE;
4992 n->agg_distinct = FALSE;
4996 * These operators must be called out explicitly in order to make use
4997 * of yacc/bison's automatic operator-precedence handling. All other
4998 * operator names are handled by the generic productions using "Op",
4999 * below; and all those operators will have the same precedence.
5001 * If you add more explicitly-known operators, be sure to add them
5002 * also to b_expr and to the MathOp list above.
5004 | '+' a_expr %prec UMINUS
5005 { $$ = (Node *) makeSimpleA_Expr(OP, "+", NULL, $2); }
5006 | '-' a_expr %prec UMINUS
5007 { $$ = doNegate($2); }
5009 { $$ = (Node *) makeSimpleA_Expr(OP, "%", NULL, $2); }
5011 { $$ = (Node *) makeSimpleA_Expr(OP, "^", NULL, $2); }
5013 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, NULL); }
5015 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, NULL); }
5017 { $$ = (Node *) makeSimpleA_Expr(OP, "+", $1, $3); }
5019 { $$ = (Node *) makeSimpleA_Expr(OP, "-", $1, $3); }
5021 { $$ = (Node *) makeSimpleA_Expr(OP, "*", $1, $3); }
5023 { $$ = (Node *) makeSimpleA_Expr(OP, "/", $1, $3); }
5025 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, $3); }
5027 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, $3); }
5029 { $$ = (Node *) makeSimpleA_Expr(OP, "<", $1, $3); }
5031 { $$ = (Node *) makeSimpleA_Expr(OP, ">", $1, $3); }
5033 { $$ = (Node *) makeSimpleA_Expr(OP, "=", $1, $3); }
5035 | a_expr qual_Op a_expr %prec Op
5036 { $$ = (Node *) makeA_Expr(OP, $2, $1, $3); }
5037 | qual_Op a_expr %prec Op
5038 { $$ = (Node *) makeA_Expr(OP, $1, NULL, $2); }
5039 | a_expr qual_Op %prec POSTFIXOP
5040 { $$ = (Node *) makeA_Expr(OP, $2, $1, NULL); }
5043 { $$ = (Node *) makeA_Expr(AND, NIL, $1, $3); }
5045 { $$ = (Node *) makeA_Expr(OR, NIL, $1, $3); }
5047 { $$ = (Node *) makeA_Expr(NOT, NIL, NULL, $2); }
5049 | a_expr LIKE a_expr
5050 { $$ = (Node *) makeSimpleA_Expr(OP, "~~", $1, $3); }
5051 | a_expr LIKE a_expr ESCAPE a_expr
5053 FuncCall *n = makeNode(FuncCall);
5054 n->funcname = SystemFuncName("like_escape");
5055 n->args = makeList2($3, $5);
5056 n->agg_star = FALSE;
5057 n->agg_distinct = FALSE;
5058 $$ = (Node *) makeSimpleA_Expr(OP, "~~", $1, (Node *) n);
5060 | a_expr NOT LIKE a_expr
5061 { $$ = (Node *) makeSimpleA_Expr(OP, "!~~", $1, $4); }
5062 | a_expr NOT LIKE a_expr ESCAPE a_expr
5064 FuncCall *n = makeNode(FuncCall);
5065 n->funcname = SystemFuncName("like_escape");
5066 n->args = makeList2($4, $6);
5067 n->agg_star = FALSE;
5068 n->agg_distinct = FALSE;
5069 $$ = (Node *) makeSimpleA_Expr(OP, "!~~", $1, (Node *) n);
5071 | a_expr ILIKE a_expr
5072 { $$ = (Node *) makeSimpleA_Expr(OP, "~~*", $1, $3); }
5073 | a_expr ILIKE a_expr ESCAPE a_expr
5075 FuncCall *n = makeNode(FuncCall);
5076 n->funcname = SystemFuncName("like_escape");
5077 n->args = makeList2($3, $5);
5078 n->agg_star = FALSE;
5079 n->agg_distinct = FALSE;
5080 $$ = (Node *) makeSimpleA_Expr(OP, "~~*", $1, (Node *) n);
5082 | a_expr NOT ILIKE a_expr
5083 { $$ = (Node *) makeSimpleA_Expr(OP, "!~~*", $1, $4); }
5084 | a_expr NOT ILIKE a_expr ESCAPE a_expr
5086 FuncCall *n = makeNode(FuncCall);
5087 n->funcname = SystemFuncName("like_escape");
5088 n->args = makeList2($4, $6);
5089 n->agg_star = FALSE;
5090 n->agg_distinct = FALSE;
5091 $$ = (Node *) makeSimpleA_Expr(OP, "!~~*", $1, (Node *) n);
5094 | a_expr SIMILAR TO a_expr %prec SIMILAR
5095 { $$ = (Node *) makeSimpleA_Expr(OP, "~", $1, $4); }
5096 | a_expr SIMILAR TO a_expr ESCAPE a_expr
5098 FuncCall *n = makeNode(FuncCall);
5099 n->funcname = SystemFuncName("like_escape");
5100 n->args = makeList2($4, $6);
5101 n->agg_star = FALSE;
5102 n->agg_distinct = FALSE;
5103 $$ = (Node *) makeSimpleA_Expr(OP, "~", $1, (Node *) n);
5105 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
5106 { $$ = (Node *) makeSimpleA_Expr(OP, "!~", $1, $5); }
5107 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
5109 FuncCall *n = makeNode(FuncCall);
5110 n->funcname = SystemFuncName("like_escape");
5111 n->args = makeList2($5, $7);
5112 n->agg_star = FALSE;
5113 n->agg_distinct = FALSE;
5114 $$ = (Node *) makeSimpleA_Expr(OP, "!~", $1, (Node *) n);
5118 * Define SQL92-style Null test clause.
5119 * Allow two forms described in the standard:
5122 * Allow two SQL extensions
5125 * NOTE: this is not yet fully SQL-compatible, since SQL92
5126 * allows a row constructor as argument, not just a scalar.
5130 NullTest *n = makeNode(NullTest);
5132 n->nulltesttype = IS_NULL;
5137 NullTest *n = makeNode(NullTest);
5139 n->nulltesttype = IS_NULL;
5144 NullTest *n = makeNode(NullTest);
5146 n->nulltesttype = IS_NOT_NULL;
5149 | a_expr IS NOT NULL_P
5151 NullTest *n = makeNode(NullTest);
5153 n->nulltesttype = IS_NOT_NULL;
5156 /* IS TRUE, IS FALSE, etc used to be function calls
5157 * but let's make them expressions to allow the optimizer
5158 * a chance to eliminate them if a_expr is a constant string.
5159 * - thomas 1997-12-22
5161 * Created BooleanTest Node type, and changed handling
5167 BooleanTest *b = makeNode(BooleanTest);
5169 b->booltesttype = IS_TRUE;
5172 | a_expr IS NOT TRUE_P
5174 BooleanTest *b = makeNode(BooleanTest);
5176 b->booltesttype = IS_NOT_TRUE;
5181 BooleanTest *b = makeNode(BooleanTest);
5183 b->booltesttype = IS_FALSE;
5186 | a_expr IS NOT FALSE_P
5188 BooleanTest *b = makeNode(BooleanTest);
5190 b->booltesttype = IS_NOT_FALSE;
5195 BooleanTest *b = makeNode(BooleanTest);
5197 b->booltesttype = IS_UNKNOWN;
5200 | a_expr IS NOT UNKNOWN
5202 BooleanTest *b = makeNode(BooleanTest);
5204 b->booltesttype = IS_NOT_UNKNOWN;
5207 | a_expr BETWEEN b_expr AND b_expr %prec BETWEEN
5209 $$ = (Node *) makeA_Expr(AND, NIL,
5210 (Node *) makeSimpleA_Expr(OP, ">=", $1, $3),
5211 (Node *) makeSimpleA_Expr(OP, "<=", $1, $5));
5213 | a_expr NOT BETWEEN b_expr AND b_expr %prec BETWEEN
5215 $$ = (Node *) makeA_Expr(OR, NIL,
5216 (Node *) makeSimpleA_Expr(OP, "<", $1, $4),
5217 (Node *) makeSimpleA_Expr(OP, ">", $1, $6));
5219 | a_expr IN_P in_expr
5221 /* in_expr returns a SubLink or a list of a_exprs */
5222 if (IsA($3, SubLink))
5224 SubLink *n = (SubLink *)$3;
5225 n->lefthand = makeList1($1);
5226 n->oper = (List *) makeSimpleA_Expr(OP, "=",
5229 n->subLinkType = ANY_SUBLINK;
5236 foreach(l, (List *) $3)
5239 cmp = (Node *) makeSimpleA_Expr(OP, "=",
5244 n = (Node *) makeA_Expr(OR, NIL, n, cmp);
5249 | a_expr NOT IN_P in_expr
5251 /* in_expr returns a SubLink or a list of a_exprs */
5252 if (IsA($4, SubLink))
5254 SubLink *n = (SubLink *)$4;
5255 n->lefthand = makeList1($1);
5256 n->oper = (List *) makeSimpleA_Expr(OP, "<>",
5259 n->subLinkType = ALL_SUBLINK;
5266 foreach(l, (List *) $4)
5269 cmp = (Node *) makeSimpleA_Expr(OP, "<>",
5274 n = (Node *) makeA_Expr(AND, NIL, n, cmp);
5279 | a_expr qual_all_Op sub_type select_with_parens %prec Op
5281 SubLink *n = makeNode(SubLink);
5282 n->lefthand = makeList1($1);
5283 n->oper = (List *) makeA_Expr(OP, $2, NULL, NULL);
5284 n->useor = FALSE; /* doesn't matter since only one col */
5285 n->subLinkType = $3;
5294 * Restricted expressions
5296 * b_expr is a subset of the complete expression syntax defined by a_expr.
5298 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
5299 * cause trouble in the places where b_expr is used. For simplicity, we
5300 * just eliminate all the boolean-keyword-operator productions from b_expr.
5304 | b_expr TYPECAST Typename
5305 { $$ = makeTypeCast($1, $3); }
5306 | '+' b_expr %prec UMINUS
5307 { $$ = (Node *) makeSimpleA_Expr(OP, "+", NULL, $2); }
5308 | '-' b_expr %prec UMINUS
5309 { $$ = doNegate($2); }
5311 { $$ = (Node *) makeSimpleA_Expr(OP, "%", NULL, $2); }
5313 { $$ = (Node *) makeSimpleA_Expr(OP, "^", NULL, $2); }
5315 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, NULL); }
5317 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, NULL); }
5319 { $$ = (Node *) makeSimpleA_Expr(OP, "+", $1, $3); }
5321 { $$ = (Node *) makeSimpleA_Expr(OP, "-", $1, $3); }
5323 { $$ = (Node *) makeSimpleA_Expr(OP, "*", $1, $3); }
5325 { $$ = (Node *) makeSimpleA_Expr(OP, "/", $1, $3); }
5327 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, $3); }
5329 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, $3); }
5331 { $$ = (Node *) makeSimpleA_Expr(OP, "<", $1, $3); }
5333 { $$ = (Node *) makeSimpleA_Expr(OP, ">", $1, $3); }
5335 { $$ = (Node *) makeSimpleA_Expr(OP, "=", $1, $3); }
5337 | b_expr qual_Op b_expr %prec Op
5338 { $$ = (Node *) makeA_Expr(OP, $2, $1, $3); }
5339 | qual_Op b_expr %prec Op
5340 { $$ = (Node *) makeA_Expr(OP, $1, NULL, $2); }
5341 | b_expr qual_Op %prec POSTFIXOP
5342 { $$ = (Node *) makeA_Expr(OP, $2, $1, NULL); }
5346 * Productions that can be used in both a_expr and b_expr.
5348 * Note: productions that refer recursively to a_expr or b_expr mostly
5349 * cannot appear here. However, it's OK to refer to a_exprs that occur
5350 * inside parentheses, such as function arguments; that cannot introduce
5351 * ambiguity to the b_expr syntax.
5354 { $$ = (Node *) $1; }
5357 | PARAM attrs opt_indirection
5360 * PARAM without field names is considered a constant,
5361 * but with 'em, it is not. Not very consistent ...
5363 ParamRef *n = makeNode(ParamRef);
5366 n->indirection = $3;
5371 | '(' a_expr ')' attrs opt_indirection
5373 ExprFieldSelect *n = makeNode(ExprFieldSelect);
5376 n->indirection = $5;
5379 | CAST '(' a_expr AS Typename ')'
5380 { $$ = makeTypeCast($3, $5); }
5385 FuncCall *n = makeNode(FuncCall);
5388 n->agg_star = FALSE;
5389 n->agg_distinct = FALSE;
5392 | func_name '(' expr_list ')'
5394 FuncCall *n = makeNode(FuncCall);
5397 n->agg_star = FALSE;
5398 n->agg_distinct = FALSE;
5401 | func_name '(' ALL expr_list ')'
5403 FuncCall *n = makeNode(FuncCall);
5406 n->agg_star = FALSE;
5407 n->agg_distinct = FALSE;
5408 /* Ideally we'd mark the FuncCall node to indicate
5409 * "must be an aggregate", but there's no provision
5410 * for that in FuncCall at the moment.
5414 | func_name '(' DISTINCT expr_list ')'
5416 FuncCall *n = makeNode(FuncCall);
5419 n->agg_star = FALSE;
5420 n->agg_distinct = TRUE;
5423 | func_name '(' '*' ')'
5426 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
5428 * This does the right thing for COUNT(*) (in fact,
5429 * any certainly-non-null expression would do for COUNT),
5430 * and there are no other aggregates in SQL92 that accept
5433 * The FuncCall node is also marked agg_star = true,
5434 * so that later processing can detect what the argument
5437 FuncCall *n = makeNode(FuncCall);
5438 A_Const *star = makeNode(A_Const);
5440 star->val.type = T_Integer;
5441 star->val.val.ival = 1;
5443 n->args = makeList1(star);
5445 n->agg_distinct = FALSE;
5451 * Translate as "'now'::text::date".
5453 * We cannot use "'now'::date" because coerce_type() will
5454 * immediately reduce that to a constant representing
5455 * today's date. We need to delay the conversion until
5456 * runtime, else the wrong things will happen when
5457 * CURRENT_DATE is used in a column default value or rule.
5459 * This could be simplified if we had a way to generate
5460 * an expression tree representing runtime application
5461 * of type-input conversion functions...
5463 A_Const *s = makeNode(A_Const);
5466 s->val.type = T_String;
5467 s->val.val.str = "now";
5468 s->typename = SystemTypeName("text");
5470 d = SystemTypeName("date");
5472 $$ = (Node *)makeTypeCast((Node *)s, d);
5477 * Translate as "'now'::text::timetz".
5478 * See comments for CURRENT_DATE.
5480 A_Const *s = makeNode(A_Const);
5483 s->val.type = T_String;
5484 s->val.val.str = "now";
5485 s->typename = SystemTypeName("text");
5487 d = SystemTypeName("timetz");
5488 /* SQL99 mandates a default precision of zero for TIME
5489 * fields in schemas. However, for CURRENT_TIME
5490 * let's preserve the microsecond precision we
5491 * might see from the system clock. - thomas 2001-12-07
5495 $$ = (Node *)makeTypeCast((Node *)s, d);
5497 | CURRENT_TIME '(' Iconst ')'
5500 * Translate as "'now'::text::timetz(n)".
5501 * See comments for CURRENT_DATE.
5503 A_Const *s = makeNode(A_Const);
5506 s->val.type = T_String;
5507 s->val.val.str = "now";
5508 s->typename = SystemTypeName("text");
5509 d = SystemTypeName("timetz");
5510 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
5511 elog(ERROR, "CURRENT_TIME(%d) precision must be between %d and %d",
5512 $3, 0, MAX_TIME_PRECISION);
5515 $$ = (Node *)makeTypeCast((Node *)s, d);
5520 * Translate as "'now'::text::timestamptz".
5521 * See comments for CURRENT_DATE.
5523 A_Const *s = makeNode(A_Const);
5526 s->val.type = T_String;
5527 s->val.val.str = "now";
5528 s->typename = SystemTypeName("text");
5530 d = SystemTypeName("timestamptz");
5531 /* SQL99 mandates a default precision of 6 for timestamp.
5532 * Also, that is about as precise as we will get since
5533 * we are using a microsecond time interface.
5534 * - thomas 2001-12-07
5538 $$ = (Node *)makeTypeCast((Node *)s, d);
5540 | CURRENT_TIMESTAMP '(' Iconst ')'
5543 * Translate as "'now'::text::timestamptz(n)".
5544 * See comments for CURRENT_DATE.
5546 A_Const *s = makeNode(A_Const);
5549 s->val.type = T_String;
5550 s->val.val.str = "now";
5551 s->typename = SystemTypeName("text");
5553 d = SystemTypeName("timestamptz");
5554 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
5555 elog(ERROR, "CURRENT_TIMESTAMP(%d) precision must be between %d and %d",
5556 $3, 0, MAX_TIMESTAMP_PRECISION);
5559 $$ = (Node *)makeTypeCast((Node *)s, d);
5564 * Translate as "'now'::text::time".
5565 * See comments for CURRENT_DATE.
5567 A_Const *s = makeNode(A_Const);
5570 s->val.type = T_String;
5571 s->val.val.str = "now";
5572 s->typename = SystemTypeName("text");
5574 d = SystemTypeName("time");
5575 /* SQL99 mandates a default precision of zero for TIME
5576 * fields in schemas. However, for LOCALTIME
5577 * let's preserve the microsecond precision we
5578 * might see from the system clock. - thomas 2001-12-07
5582 $$ = (Node *)makeTypeCast((Node *)s, d);
5584 | LOCALTIME '(' Iconst ')'
5587 * Translate as "'now'::text::time(n)".
5588 * See comments for CURRENT_DATE.
5590 A_Const *s = makeNode(A_Const);
5593 s->val.type = T_String;
5594 s->val.val.str = "now";
5595 s->typename = SystemTypeName("text");
5596 d = SystemTypeName("time");
5597 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
5598 elog(ERROR, "LOCALTIME(%d) precision must be between %d and %d",
5599 $3, 0, MAX_TIME_PRECISION);
5602 $$ = (Node *)makeTypeCast((Node *)s, d);
5607 * Translate as "'now'::text::timestamp".
5608 * See comments for CURRENT_DATE.
5610 A_Const *s = makeNode(A_Const);
5613 s->val.type = T_String;
5614 s->val.val.str = "now";
5615 s->typename = SystemTypeName("text");
5617 d = SystemTypeName("timestamp");
5618 /* SQL99 mandates a default precision of 6 for timestamp.
5619 * Also, that is about as precise as we will get since
5620 * we are using a microsecond time interface.
5621 * - thomas 2001-12-07
5625 $$ = (Node *)makeTypeCast((Node *)s, d);
5627 | LOCALTIMESTAMP '(' Iconst ')'
5630 * Translate as "'now'::text::timestamp(n)".
5631 * See comments for CURRENT_DATE.
5633 A_Const *s = makeNode(A_Const);
5636 s->val.type = T_String;
5637 s->val.val.str = "now";
5638 s->typename = SystemTypeName("text");
5640 d = SystemTypeName("timestamp");
5641 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
5642 elog(ERROR, "LOCALTIMESTAMP(%d) precision must be between %d and %d",
5643 $3, 0, MAX_TIMESTAMP_PRECISION);
5646 $$ = (Node *)makeTypeCast((Node *)s, d);
5650 FuncCall *n = makeNode(FuncCall);
5651 n->funcname = SystemFuncName("current_user");
5653 n->agg_star = FALSE;
5654 n->agg_distinct = FALSE;
5659 FuncCall *n = makeNode(FuncCall);
5660 n->funcname = SystemFuncName("session_user");
5662 n->agg_star = FALSE;
5663 n->agg_distinct = FALSE;
5668 FuncCall *n = makeNode(FuncCall);
5669 n->funcname = SystemFuncName("current_user");
5671 n->agg_star = FALSE;
5672 n->agg_distinct = FALSE;
5675 | EXTRACT '(' extract_list ')'
5677 FuncCall *n = makeNode(FuncCall);
5678 n->funcname = SystemFuncName("date_part");
5680 n->agg_star = FALSE;
5681 n->agg_distinct = FALSE;
5684 | OVERLAY '(' overlay_list ')'
5686 /* overlay(A PLACING B FROM C FOR D) is converted to
5687 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
5688 * overlay(A PLACING B FROM C) is converted to
5689 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
5691 FuncCall *n = makeNode(FuncCall);
5692 n->funcname = SystemFuncName("overlay");
5694 n->agg_star = FALSE;
5695 n->agg_distinct = FALSE;
5698 | POSITION '(' position_list ')'
5700 /* position(A in B) is converted to position(B, A) */
5701 FuncCall *n = makeNode(FuncCall);
5702 n->funcname = SystemFuncName("position");
5704 n->agg_star = FALSE;
5705 n->agg_distinct = FALSE;
5708 | SUBSTRING '(' substr_list ')'
5710 /* substring(A from B for C) is converted to
5711 * substring(A, B, C) - thomas 2000-11-28
5713 FuncCall *n = makeNode(FuncCall);
5714 n->funcname = SystemFuncName("substring");
5716 n->agg_star = FALSE;
5717 n->agg_distinct = FALSE;
5720 | TRIM '(' BOTH trim_list ')'
5722 /* various trim expressions are defined in SQL92
5723 * - thomas 1997-07-19
5725 FuncCall *n = makeNode(FuncCall);
5726 n->funcname = SystemFuncName("btrim");
5728 n->agg_star = FALSE;
5729 n->agg_distinct = FALSE;
5732 | TRIM '(' LEADING trim_list ')'
5734 FuncCall *n = makeNode(FuncCall);
5735 n->funcname = SystemFuncName("ltrim");
5737 n->agg_star = FALSE;
5738 n->agg_distinct = FALSE;
5741 | TRIM '(' TRAILING trim_list ')'
5743 FuncCall *n = makeNode(FuncCall);
5744 n->funcname = SystemFuncName("rtrim");
5746 n->agg_star = FALSE;
5747 n->agg_distinct = FALSE;
5750 | TRIM '(' trim_list ')'
5752 FuncCall *n = makeNode(FuncCall);
5753 n->funcname = SystemFuncName("btrim");
5755 n->agg_star = FALSE;
5756 n->agg_distinct = FALSE;
5759 | select_with_parens %prec UMINUS
5761 SubLink *n = makeNode(SubLink);
5765 n->subLinkType = EXPR_SUBLINK;
5769 | EXISTS select_with_parens
5771 SubLink *n = makeNode(SubLink);
5775 n->subLinkType = EXISTS_SUBLINK;
5782 * Supporting nonterminals for expressions.
5785 opt_indirection: opt_indirection '[' a_expr ']'
5787 A_Indices *ai = makeNode(A_Indices);
5790 $$ = lappend($1, ai);
5792 | opt_indirection '[' a_expr ':' a_expr ']'
5794 A_Indices *ai = makeNode(A_Indices);
5797 $$ = lappend($1, ai);
5804 { $$ = makeList1($1); }
5805 | expr_list ',' a_expr
5806 { $$ = lappend($1, $3); }
5807 | expr_list USING a_expr
5808 { $$ = lappend($1, $3); }
5811 extract_list: extract_arg FROM a_expr
5813 A_Const *n = makeNode(A_Const);
5814 n->val.type = T_String;
5815 n->val.val.str = $1;
5816 $$ = makeList2((Node *) n, $3);
5822 /* Allow delimited string SCONST in extract_arg as an SQL extension.
5823 * - thomas 2001-04-12
5826 extract_arg: IDENT { $$ = $1; }
5827 | YEAR_P { $$ = "year"; }
5828 | MONTH_P { $$ = "month"; }
5829 | DAY_P { $$ = "day"; }
5830 | HOUR_P { $$ = "hour"; }
5831 | MINUTE_P { $$ = "minute"; }
5832 | SECOND_P { $$ = "second"; }
5833 | SCONST { $$ = $1; }
5836 /* OVERLAY() arguments
5837 * SQL99 defines the OVERLAY() function:
5838 * o overlay(text placing text from int for int)
5839 * o overlay(text placing text from int)
5841 overlay_list: a_expr overlay_placing substr_from substr_for
5843 $$ = makeList4($1, $2, $3, $4);
5845 | a_expr overlay_placing substr_from
5847 $$ = makeList3($1, $2, $3);
5851 overlay_placing: PLACING a_expr
5855 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
5857 position_list: b_expr IN_P b_expr
5858 { $$ = makeList2($3, $1); }
5863 /* SUBSTRING() arguments
5864 * SQL9x defines a specific syntax for arguments to SUBSTRING():
5865 * o substring(text from int for int)
5866 * o substring(text from int) get entire string from starting point "int"
5867 * o substring(text for int) get first "int" characters of string
5868 * We also want to implement generic substring functions which accept
5869 * the usual generic list of arguments. So we will accept both styles
5870 * here, and convert the SQL9x style to the generic list for further
5871 * processing. - thomas 2000-11-28
5873 substr_list: a_expr substr_from substr_for
5875 $$ = makeList3($1, $2, $3);
5877 | a_expr substr_for substr_from
5879 $$ = makeList3($1, $3, $2);
5881 | a_expr substr_from
5883 $$ = makeList2($1, $2);
5887 A_Const *n = makeNode(A_Const);
5888 n->val.type = T_Integer;
5889 n->val.val.ival = 1;
5890 $$ = makeList3($1, (Node *)n, $2);
5900 substr_from: FROM a_expr
5904 substr_for: FOR a_expr
5908 trim_list: a_expr FROM expr_list
5909 { $$ = lappend($3, $1); }
5916 in_expr: select_with_parens
5918 SubLink *n = makeNode(SubLink);
5922 | '(' in_expr_nodes ')'
5923 { $$ = (Node *)$2; }
5926 in_expr_nodes: a_expr
5927 { $$ = makeList1($1); }
5928 | in_expr_nodes ',' a_expr
5929 { $$ = lappend($1, $3); }
5933 * Define SQL92-style case clause.
5934 * Allow all four forms described in the standard:
5935 * - Full specification
5936 * CASE WHEN a = b THEN c ... ELSE d END
5937 * - Implicit argument
5938 * CASE a WHEN b THEN c ... ELSE d END
5939 * - Conditional NULL
5941 * same as CASE WHEN x = y THEN NULL ELSE x END
5942 * - Conditional substitution from list, use first non-null argument
5944 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
5945 * - thomas 1998-11-09
5947 case_expr: CASE case_arg when_clause_list case_default END_TRANS
5949 CaseExpr *c = makeNode(CaseExpr);
5955 | NULLIF '(' a_expr ',' a_expr ')'
5957 CaseExpr *c = makeNode(CaseExpr);
5958 CaseWhen *w = makeNode(CaseWhen);
5960 w->expr = (Node *) makeSimpleA_Expr(OP, "=", $3, $5);
5961 /* w->result is left NULL */
5962 c->args = makeList1(w);
5966 | COALESCE '(' expr_list ')'
5968 CaseExpr *c = makeNode(CaseExpr);
5972 CaseWhen *w = makeNode(CaseWhen);
5973 NullTest *n = makeNode(NullTest);
5975 n->nulltesttype = IS_NOT_NULL;
5976 w->expr = (Node *) n;
5977 w->result = lfirst(l);
5978 c->args = lappend(c->args, w);
5984 when_clause_list: when_clause_list when_clause
5985 { $$ = lappend($1, $2); }
5987 { $$ = makeList1($1); }
5990 when_clause: WHEN a_expr THEN a_expr
5992 CaseWhen *w = makeNode(CaseWhen);
5999 case_default: ELSE a_expr { $$ = $2; }
6000 | /*EMPTY*/ { $$ = NULL; }
6010 * columnref starts with relation_name not ColId, so that OLD and NEW
6011 * references can be accepted. Note that when there are more than two
6012 * dotted names, the first name is not actually a relation name...
6014 columnref: relation_name opt_indirection
6016 $$ = makeNode(ColumnRef);
6017 $$->fields = makeList1(makeString($1));
6018 $$->indirection = $2;
6020 | dotted_name opt_indirection
6022 $$ = makeNode(ColumnRef);
6024 $$->indirection = $2;
6028 dotted_name: relation_name attrs
6029 { $$ = lcons(makeString($1), $2); }
6032 attrs: '.' attr_name
6033 { $$ = makeList1(makeString($2)); }
6035 { $$ = makeList1(makeString("*")); }
6036 | '.' attr_name attrs
6037 { $$ = lcons(makeString($2), $3); }
6041 /*****************************************************************************
6045 *****************************************************************************/
6047 /* Target lists as found in SELECT ... and INSERT VALUES ( ... ) */
6049 target_list: target_list ',' target_el
6050 { $$ = lappend($1, $3); }
6052 { $$ = makeList1($1); }
6055 /* AS is not optional because shift/red conflict with unary ops */
6056 target_el: a_expr AS ColLabel
6058 $$ = makeNode(ResTarget);
6060 $$->indirection = NIL;
6061 $$->val = (Node *)$1;
6065 $$ = makeNode(ResTarget);
6067 $$->indirection = NIL;
6068 $$->val = (Node *)$1;
6072 ColumnRef *n = makeNode(ColumnRef);
6073 n->fields = makeList1(makeString("*"));
6074 n->indirection = NIL;
6075 $$ = makeNode(ResTarget);
6077 $$->indirection = NIL;
6078 $$->val = (Node *)n;
6082 /* Target list as found in UPDATE table SET ...
6083 | '(' row_ ')' = '(' row_ ')'
6088 update_target_list: update_target_list ',' update_target_el
6089 { $$ = lappend($1,$3); }
6091 { $$ = makeList1($1); }
6094 update_target_el: ColId opt_indirection '=' a_expr
6096 $$ = makeNode(ResTarget);
6098 $$->indirection = $2;
6099 $$->val = (Node *)$4;
6103 insert_target_list: insert_target_list ',' insert_target_el
6104 { $$ = lappend($1, $3); }
6106 { $$ = makeList1($1); }
6109 insert_target_el: target_el { $$ = $1; }
6111 InsertDefault *def = makeNode(InsertDefault);
6112 $$ = makeNode(ResTarget);
6114 $$->indirection = NULL;
6115 $$->val = (Node *)def;
6120 /*****************************************************************************
6122 * Names and constants
6124 *****************************************************************************/
6126 relation_name: SpecialRuleRelation
6136 qualified_name_list: qualified_name
6137 { $$ = makeList1($1); }
6138 | qualified_name_list ',' qualified_name
6139 { $$ = lappend($1, $3); }
6142 qualified_name: relation_name
6144 $$ = makeNode(RangeVar);
6145 $$->catalogname = NULL;
6146 $$->schemaname = NULL;
6151 $$ = makeNode(RangeVar);
6155 $$->catalogname = NULL;
6156 $$->schemaname = strVal(lfirst($1));
6157 $$->relname = strVal(lsecond($1));
6160 $$->catalogname = strVal(lfirst($1));
6161 $$->schemaname = strVal(lsecond($1));
6162 $$->relname = strVal(lfirst(lnext(lnext($1))));
6165 elog(ERROR, "Improper qualified name (too many dotted names): %s",
6166 NameListToString($1));
6173 { $$ = makeList1(makeString($1)); }
6174 | name_list ',' name
6175 { $$ = lappend($1, makeString($3)); }
6179 name: ColId { $$ = $1; };
6180 database_name: ColId { $$ = $1; };
6181 access_method: ColId { $$ = $1; };
6182 attr_name: ColId { $$ = $1; };
6183 index_name: ColId { $$ = $1; };
6184 file_name: Sconst { $$ = $1; };
6186 func_name: function_name
6187 { $$ = makeList1(makeString($1)); }
6194 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
6198 A_Const *n = makeNode(A_Const);
6199 n->val.type = T_Integer;
6200 n->val.val.ival = $1;
6205 A_Const *n = makeNode(A_Const);
6206 n->val.type = T_Float;
6207 n->val.val.str = $1;
6212 A_Const *n = makeNode(A_Const);
6213 n->val.type = T_String;
6214 n->val.val.str = $1;
6219 A_Const *n = makeNode(A_Const);
6220 n->val.type = T_BitString;
6221 n->val.val.str = $1;
6224 /* This rule formerly used Typename,
6225 * but that causes reduce conflicts with subscripted column names.
6226 * Now, separate into ConstTypename and ConstInterval,
6227 * to allow implementing the SQL92 syntax for INTERVAL literals.
6228 * - thomas 2000-06-24
6230 | ConstTypename Sconst
6232 A_Const *n = makeNode(A_Const);
6234 n->val.type = T_String;
6235 n->val.val.str = $2;
6238 | ConstInterval Sconst opt_interval
6240 A_Const *n = makeNode(A_Const);
6242 n->val.type = T_String;
6243 n->val.val.str = $2;
6244 /* precision is not specified, but fields may be... */
6246 n->typename->typmod = ((($3 & 0x7FFF) << 16) | 0xFFFF);
6249 | ConstInterval '(' Iconst ')' Sconst opt_interval
6251 A_Const *n = makeNode(A_Const);
6253 n->val.type = T_String;
6254 n->val.val.str = $5;
6255 /* precision specified, and fields may be... */
6256 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
6257 elog(ERROR, "INTERVAL(%d) precision must be between %d and %d",
6258 $3, 0, MAX_INTERVAL_PRECISION);
6259 n->typename->typmod = ((($6 & 0x7FFF) << 16) | $3);
6262 | PARAM opt_indirection
6264 ParamRef *n = makeNode(ParamRef);
6267 n->indirection = $2;
6272 A_Const *n = makeNode(A_Const);
6273 n->val.type = T_String;
6274 n->val.val.str = "t";
6275 n->typename = SystemTypeName("bool");
6280 A_Const *n = makeNode(A_Const);
6281 n->val.type = T_String;
6282 n->val.val.str = "f";
6283 n->typename = SystemTypeName("bool");
6288 A_Const *n = makeNode(A_Const);
6289 n->val.type = T_Null;
6294 Iconst: ICONST { $$ = $1; };
6295 Sconst: SCONST { $$ = $1; };
6296 UserId: ColId { $$ = $1; };
6299 * Name classification hierarchy.
6301 * IDENT is the lexeme returned by the lexer for identifiers that match
6302 * no known keyword. In most cases, we can accept certain keywords as
6303 * names, not only IDENTs. We prefer to accept as many such keywords
6304 * as possible to minimize the impact of "reserved words" on programmers.
6305 * So, we divide names into several possible classes. The classification
6306 * is chosen in part to make keywords acceptable as names wherever possible.
6309 /* Column identifier --- names that can be column, table, etc names.
6311 ColId: IDENT { $$ = $1; }
6312 | unreserved_keyword { $$ = pstrdup($1); }
6313 | col_name_keyword { $$ = pstrdup($1); }
6316 /* Type identifier --- names that can be type names.
6318 type_name: IDENT { $$ = $1; }
6319 | unreserved_keyword { $$ = pstrdup($1); }
6322 /* Function identifier --- names that can be function names.
6324 function_name: IDENT { $$ = $1; }
6325 | unreserved_keyword { $$ = pstrdup($1); }
6326 | func_name_keyword { $$ = pstrdup($1); }
6329 /* Column label --- allowed labels in "AS" clauses.
6330 * This presently includes *all* Postgres keywords.
6332 ColLabel: IDENT { $$ = $1; }
6333 | unreserved_keyword { $$ = pstrdup($1); }
6334 | col_name_keyword { $$ = pstrdup($1); }
6335 | func_name_keyword { $$ = pstrdup($1); }
6336 | reserved_keyword { $$ = pstrdup($1); }
6341 * Keyword classification lists. Generally, every keyword present in
6342 * the Postgres grammar should appear in exactly one of these lists.
6344 * Put a new keyword into the first list that it can go into without causing
6345 * shift or reduce conflicts. The earlier lists define "less reserved"
6346 * categories of keywords.
6349 /* "Unreserved" keywords --- available for use as any kind of name.
6522 /* Column identifier --- keywords that can be column, table, etc names.
6524 * Many of these keywords will in fact be recognized as type or function
6525 * names too; but they have special productions for the purpose, and so
6526 * can't be treated as "generic" type or function names.
6528 * The type names appearing here are not usable as function names
6529 * because they can be followed by '(' in typename productions, which
6530 * looks too much like a function call for an LR(1) parser.
6563 /* Function identifier --- keywords that can be function names.
6565 * Most of these are keywords that are used as operators in expressions;
6566 * in general such keywords can't be column names because they would be
6567 * ambiguous with variables, but they are unambiguous as function identifiers.
6569 * Do not include POSITION, SUBSTRING, etc here since they have explicit
6570 * productions in a_expr to support the goofy SQL9x argument syntax.
6571 * - thomas 2000-11-28
6597 /* Reserved keyword --- these keywords are usable only as a ColLabel.
6599 * Keywords appear here if they could not be distinguished from variable,
6600 * type, or function names in some contexts. Don't put things here unless
6675 SpecialRuleRelation: OLD
6680 elog(ERROR, "OLD used in non-rule query");
6687 elog(ERROR, "NEW used in non-rule query");
6694 makeTypeCast(Node *arg, TypeName *typename)
6697 * If arg is an A_Const, just stick the typename into the
6698 * field reserved for it --- unless there's something there already!
6699 * (We don't want to collapse x::type1::type2 into just x::type2.)
6700 * Otherwise, generate a TypeCast node.
6702 if (IsA(arg, A_Const) &&
6703 ((A_Const *) arg)->typename == NULL)
6705 ((A_Const *) arg)->typename = typename;
6710 TypeCast *n = makeNode(TypeCast);
6712 n->typename = typename;
6718 makeStringConst(char *str, TypeName *typename)
6720 A_Const *n = makeNode(A_Const);
6722 n->val.type = T_String;
6723 n->val.val.str = str;
6724 n->typename = typename;
6730 makeIntConst(int val)
6732 A_Const *n = makeNode(A_Const);
6733 n->val.type = T_Integer;
6734 n->val.val.ival = val;
6735 n->typename = SystemTypeName("int4");
6741 makeFloatConst(char *str)
6743 A_Const *n = makeNode(A_Const);
6745 n->val.type = T_Float;
6746 n->val.val.str = str;
6747 n->typename = SystemTypeName("float8");
6753 makeAConst(Value *v)
6760 n = makeFloatConst(v->val.str);
6764 n = makeIntConst(v->val.ival);
6769 n = makeStringConst(v->val.str, NULL);
6777 * Generate separate operator nodes for a single row descriptor expression.
6778 * Perhaps this should go deeper in the parser someday...
6779 * - thomas 1997-12-22
6782 makeRowExpr(List *opr, List *largs, List *rargs)
6788 if (length(largs) != length(rargs))
6789 elog(ERROR, "Unequal number of entries in row expression");
6791 if (lnext(largs) != NIL)
6792 expr = makeRowExpr(opr, lnext(largs), lnext(rargs));
6794 larg = lfirst(largs);
6795 rarg = lfirst(rargs);
6797 oprname = strVal(llast(opr));
6799 if ((strcmp(oprname, "=") == 0) ||
6800 (strcmp(oprname, "<") == 0) ||
6801 (strcmp(oprname, "<=") == 0) ||
6802 (strcmp(oprname, ">") == 0) ||
6803 (strcmp(oprname, ">=") == 0))
6806 expr = (Node *) makeA_Expr(OP, opr, larg, rarg);
6808 expr = (Node *) makeA_Expr(AND, NIL, expr,
6809 (Node *) makeA_Expr(OP, opr,
6812 else if (strcmp(oprname, "<>") == 0)
6815 expr = (Node *) makeA_Expr(OP, opr, larg, rarg);
6817 expr = (Node *) makeA_Expr(OR, NIL, expr,
6818 (Node *) makeA_Expr(OP, opr,
6823 elog(ERROR, "Operator '%s' not implemented for row expressions",
6830 /* findLeftmostSelect()
6831 * Find the leftmost component SelectStmt in a set-operation parsetree.
6834 findLeftmostSelect(SelectStmt *node)
6836 while (node && node->op != SETOP_NONE)
6838 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
6842 /* insertSelectOptions()
6843 * Insert ORDER BY, etc into an already-constructed SelectStmt.
6845 * This routine is just to avoid duplicating code in SelectStmt productions.
6848 insertSelectOptions(SelectStmt *stmt,
6849 List *sortClause, List *forUpdate,
6850 Node *limitOffset, Node *limitCount)
6853 * Tests here are to reject constructs like
6854 * (SELECT foo ORDER BY bar) ORDER BY baz
6858 if (stmt->sortClause)
6859 elog(ERROR, "Multiple ORDER BY clauses not allowed");
6860 stmt->sortClause = sortClause;
6864 if (stmt->forUpdate)
6865 elog(ERROR, "Multiple FOR UPDATE clauses not allowed");
6866 stmt->forUpdate = forUpdate;
6870 if (stmt->limitOffset)
6871 elog(ERROR, "Multiple OFFSET clauses not allowed");
6872 stmt->limitOffset = limitOffset;
6876 if (stmt->limitCount)
6877 elog(ERROR, "Multiple LIMIT clauses not allowed");
6878 stmt->limitCount = limitCount;
6883 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
6885 SelectStmt *n = makeNode(SelectStmt);
6889 n->larg = (SelectStmt *) larg;
6890 n->rarg = (SelectStmt *) rarg;
6895 * Build a properly-qualified reference to a built-in function.
6898 SystemFuncName(char *name)
6900 return makeList2(makeString("pg_catalog"), makeString(name));
6904 * Build a properly-qualified reference to a built-in type.
6906 * typmod is defaulted, but may be changed afterwards by caller.
6909 SystemTypeName(char *name)
6911 TypeName *n = makeNode(TypeName);
6913 n->names = makeList2(makeString("pg_catalog"), makeString(name));
6919 * Initialize to parse one query string
6922 parser_init(Oid *typev, int nargs)
6924 QueryIsRule = FALSE;
6926 * Keep enough information around to fill out the type of param nodes
6927 * used in postquel functions
6929 param_type_info = typev;
6930 pfunc_num_args = nargs;
6934 * Fetch a parameter type previously passed to parser_init
6939 if ((t > pfunc_num_args) || (t <= 0))
6941 return param_type_info[t - 1];
6945 * Test whether an a_expr is a plain NULL constant or not.
6948 exprIsNullConstant(Node *arg)
6950 if (arg && IsA(arg, A_Const))
6952 A_Const *con = (A_Const *) arg;
6954 if (con->val.type == T_Null &&
6955 con->typename == NULL)
6962 * doNegate --- handle negation of a numeric constant.
6964 * Formerly, we did this here because the optimizer couldn't cope with
6965 * indexquals that looked like "var = -4" --- it wants "var = const"
6966 * and a unary minus operator applied to a constant didn't qualify.
6967 * As of Postgres 7.0, that problem doesn't exist anymore because there
6968 * is a constant-subexpression simplifier in the optimizer. However,
6969 * there's still a good reason for doing this here, which is that we can
6970 * postpone committing to a particular internal representation for simple
6971 * negative constants. It's better to leave "-123.456" in string form
6972 * until we know what the desired type is.
6977 if (IsA(n, A_Const))
6979 A_Const *con = (A_Const *)n;
6981 if (con->val.type == T_Integer)
6983 con->val.val.ival = -con->val.val.ival;
6986 if (con->val.type == T_Float)
6988 doNegateFloat(&con->val);
6993 return (Node *) makeSimpleA_Expr(OP, "-", NULL, n);
6997 doNegateFloat(Value *v)
6999 char *oldval = v->val.str;
7001 Assert(IsA(v, Float));
7005 v->val.str = oldval+1; /* just strip the '-' */
7008 char *newval = (char *) palloc(strlen(oldval) + 2);
7011 strcpy(newval+1, oldval);
7012 v->val.str = newval;