4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2002, 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.413 2003/05/04 00:03:55 tgl 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 *-------------------------------------------------------------------------
54 #include "access/htup.h"
55 #include "catalog/index.h"
56 #include "catalog/namespace.h"
57 #include "catalog/pg_type.h"
58 #include "nodes/makefuncs.h"
59 #include "nodes/params.h"
60 #include "nodes/parsenodes.h"
61 #include "parser/gramparse.h"
62 #include "storage/lmgr.h"
63 #include "utils/numeric.h"
64 #include "utils/datetime.h"
65 #include "utils/date.h"
67 extern List *parsetree; /* final parse result is delivered here */
69 static bool QueryIsRule = FALSE;
72 * If you need access to certain yacc-generated variables and find that
73 * they're static by default, uncomment the next line. (this is not a
76 /*#define __YYSCLASS*/
78 static Node *makeTypeCast(Node *arg, TypeName *typename);
79 static Node *makeStringConst(char *str, TypeName *typename);
80 static Node *makeIntConst(int val);
81 static Node *makeFloatConst(char *str);
82 static Node *makeAConst(Value *v);
83 static Node *makeRowExpr(List *opr, List *largs, List *rargs);
84 static Node *makeDistinctExpr(List *largs, List *rargs);
85 static Node *makeRowNullTest(NullTestType test, List *args);
86 static DefElem *makeDefElem(char *name, Node *arg);
87 static A_Const *makeBoolConst(bool state);
88 static FuncCall *makeOverlaps(List *largs, List *rargs);
89 static SelectStmt *findLeftmostSelect(SelectStmt *node);
90 static void insertSelectOptions(SelectStmt *stmt,
91 List *sortClause, List *forUpdate,
92 Node *limitOffset, Node *limitCount);
93 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
94 static Node *doNegate(Node *n);
95 static void doNegateFloat(Value *v);
108 DropBehavior dbehavior;
109 OnCommitAction oncommit;
113 ColumnRef *columnref;
117 SortGroupBy *sortgroupby;
124 PrivTarget *privtarget;
127 VariableSetStmt *vsetstmt;
130 %type <node> stmt schema_stmt
131 AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
132 AlterSeqStmt AlterTableStmt AlterUserStmt AlterUserSetStmt
133 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
134 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
135 CreateDomainStmt CreateGroupStmt CreateOpClassStmt CreatePLangStmt
136 CreateSchemaStmt CreateSeqStmt CreateStmt
137 CreateAssertStmt CreateTrigStmt CreateUserStmt
138 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt
139 DropGroupStmt DropOpClassStmt DropPLangStmt DropStmt
140 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt
141 DropUserStmt DropdbStmt ExplainStmt FetchStmt
142 GrantStmt IndexStmt InsertStmt ListenStmt LoadStmt
143 LockStmt NotifyStmt ExplainableStmt PreparableStmt
144 CreateFunctionStmt ReindexStmt RemoveAggrStmt
145 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt
146 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
147 SelectStmt TransactionStmt TruncateStmt
148 UnlistenStmt UpdateStmt VacuumStmt
149 VariableResetStmt VariableSetStmt VariableShowStmt
150 ViewStmt CheckPointStmt CreateConversionStmt
151 DeallocateStmt PrepareStmt ExecuteStmt
153 %type <node> select_no_parens select_with_parens select_clause
156 %type <node> alter_column_default opclass_item
157 %type <ival> add_drop
159 %type <dbehavior> opt_drop_behavior
161 %type <list> createdb_opt_list copy_opt_list
162 %type <defelt> createdb_opt_item copy_opt_item
164 %type <ival> opt_lock lock_type cast_context
165 %type <boolean> opt_force opt_or_replace transaction_access_mode
166 opt_grant_grant_option opt_revoke_grant_option
168 %type <list> user_list
170 %type <list> OptGroupList
171 %type <defelt> OptGroupElem
173 %type <list> OptUserList
174 %type <defelt> OptUserElem
176 %type <str> OptSchemaName
177 %type <list> OptSchemaEltList
179 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
180 %type <str> opt_lancompiler
182 %type <str> TriggerEvents
183 %type <value> TriggerFuncArg
185 %type <str> relation_name copy_file_name
186 database_name access_method_clause access_method attr_name
187 index_name name function_name file_name
189 %type <list> func_name handler_name qual_Op qual_all_Op OptUseOp
190 opt_class opt_validator
192 %type <range> qualified_name OptConstrFromTable
194 %type <str> all_Op MathOp opt_name SpecialRuleRelation
196 %type <str> iso_level opt_encoding
198 %type <list> grantee_list
199 %type <ival> privilege
200 %type <list> privileges privilege_list
201 %type <privtarget> privilege_target
202 %type <node> function_with_argtypes
203 %type <list> function_with_argtypes_list
204 %type <chr> TriggerOneEvent
206 %type <list> stmtblock stmtmulti
207 OptTableElementList TableElementList OptInherit definition
208 opt_distinct opt_definition func_args
209 func_args_list func_as createfunc_opt_list
210 oper_argtypes RuleActionList RuleActionMulti
211 opt_column_list columnList opt_name_list
212 sort_clause opt_sort_clause sortby_list index_params
213 index_list name_list from_clause from_list opt_array_bounds
214 qualified_name_list any_name any_name_list
215 any_operator expr_list dotted_name attrs
216 target_list update_target_list insert_column_list
217 insert_target_list def_list opt_indirection
218 group_clause TriggerFuncArgs select_limit
219 opt_select_limit opclass_item_list transaction_mode_list
220 transaction_mode_list_or_empty
222 prep_type_clause prep_type_list
225 %type <range> into_clause OptTempTableName
227 %type <defelt> createfunc_opt_item
228 %type <typnam> func_arg func_return func_type aggr_argtype
230 %type <boolean> opt_arg TriggerForType OptTemp OptWithOids
231 %type <oncommit> OnCommitOption
233 %type <list> for_update_clause opt_for_update_clause update_list
234 %type <boolean> opt_all
236 %type <node> join_outer join_qual
237 %type <jtype> join_type
239 %type <list> extract_list overlay_list position_list
240 %type <list> substr_list trim_list
241 %type <ival> opt_interval
242 %type <node> overlay_placing substr_from substr_for
244 %type <boolean> opt_instead opt_analyze
245 %type <boolean> index_opt_unique opt_verbose opt_full
246 %type <boolean> opt_freeze opt_default opt_recheck
247 %type <defelt> opt_binary opt_oids copy_delimiter
249 %type <boolean> copy_from opt_hold
251 %type <ival> reindex_type drop_type fetch_count
252 opt_column event comment_type cursor_options
254 %type <node> fetch_direction select_limit_value select_offset_value
256 %type <list> OptSeqList
257 %type <defelt> OptSeqElem
259 %type <istmt> insert_rest
261 %type <vsetstmt> set_rest
263 %type <node> TableElement ConstraintElem TableFuncElement
264 %type <node> columnDef
265 %type <defelt> def_elem
266 %type <node> def_arg columnElem where_clause insert_column_item
267 a_expr b_expr c_expr r_expr AexprConst
268 in_expr having_clause func_table array_expr
269 %type <list> row row_descriptor type_list array_expr_list
270 %type <node> case_expr case_arg when_clause case_default
271 %type <list> when_clause_list
272 %type <ival> sub_type
273 %type <list> OptCreateAs CreateAsList
274 %type <node> CreateAsElement
275 %type <value> NumericOnly FloatOnly IntegerOnly
276 %type <columnref> columnref
277 %type <alias> alias_clause
278 %type <sortgroupby> sortby
279 %type <ielem> index_elem func_index
280 %type <node> table_ref
281 %type <jexpr> joined_table
282 %type <range> relation_expr
283 %type <target> target_el insert_target_el update_target_el
285 %type <typnam> Typename SimpleTypename ConstTypename
286 GenericType Numeric opt_float
287 Character ConstCharacter
288 CharacterWithLength CharacterWithoutLength
289 ConstDatetime ConstInterval
290 Bit ConstBit BitWithLength BitWithoutLength
291 %type <str> character
292 %type <str> extract_arg
293 %type <str> opt_charset opt_collate
294 %type <ival> opt_numeric opt_decimal
295 %type <boolean> opt_varying opt_timezone
298 %type <str> Sconst comment_text
299 %type <str> UserId opt_boolean ColId_or_Sconst
300 %type <list> var_list var_list_or_default
301 %type <str> ColId ColLabel type_name
302 %type <node> var_value zone_value
304 %type <keyword> unreserved_keyword func_name_keyword
305 %type <keyword> col_name_keyword reserved_keyword
307 %type <node> TableConstraint TableLikeClause
308 %type <list> ColQualList
309 %type <node> ColConstraint ColConstraintElem ConstraintAttr
310 %type <ival> key_actions key_delete key_match key_update key_action
311 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
314 %type <list> constraints_set_list
315 %type <boolean> constraints_set_mode
319 * If you make any token changes, update the keyword table in
320 * parser/keywords.c and add new keywords to the appropriate one of
321 * the reserved-or-not-so-reserved keyword lists, below.
324 /* ordinary key words in alphabetical order */
325 %token <keyword> ABORT_P ABSOLUTE ACCESS ACTION ADD AFTER
326 AGGREGATE ALL ALTER ANALYSE ANALYZE AND ANY ARRAY AS ASC
327 ASSERTION ASSIGNMENT AT AUTHORIZATION
329 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
332 CACHE CALLED CASCADE CASE CAST CHAIN CHAR_P
333 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
334 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
335 COMMITTED CONSTRAINT CONSTRAINTS CONVERSION_P CONVERT COPY CREATE CREATEDB
336 CREATEUSER CROSS CURRENT_DATE CURRENT_TIME
337 CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
339 DATABASE DAY_P DEALLOCATE DEC DECIMAL DECLARE DEFAULT
340 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS
341 DESC DISTINCT DO DOMAIN_P DOUBLE DROP
343 EACH ELSE ENCODING ENCRYPTED END_P ESCAPE EXCEPT
344 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
346 FALSE_P FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
347 FREEZE FROM FULL FUNCTION
351 HANDLER HAVING HOLD HOUR_P
353 ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCREMENT
354 INDEX INHERITS INITIALLY INNER_P INOUT INPUT
355 INSENSITIVE INSERT INSTEAD INT INTEGER INTERSECT
356 INTERVAL INTO INVOKER IS ISNULL ISOLATION
362 LANCOMPILER LANGUAGE LAST_P LEADING LEFT LEVEL LIKE LIMIT
363 LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
366 MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
368 NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
369 NOCREATEUSER NONE NOT NOTHING NOTIFY NOTNULL NULL_P
372 OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
373 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNER
375 PARTIAL PASSWORD PATH_P PENDANT PLACING POSITION
376 PRECISION PRESERVE PREPARE PRIMARY PRIOR PRIVILEGES PROCEDURAL
379 READ REAL RECHECK REFERENCES REINDEX RELATIVE RENAME REPLACE
380 RESET RESTART RESTRICT RETURNS REVOKE RIGHT ROLLBACK ROW ROWS
383 SCHEMA SCROLL SECOND_P SECURITY SELECT SEQUENCE
384 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
385 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE START STATEMENT
386 STATISTICS STDIN STDOUT STORAGE STRICT SUBSTRING
389 TABLE TEMP TEMPLATE TEMPORARY THEN TIME TIMESTAMP
390 TO TOAST TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
391 TRUNCATE TRUSTED TYPE_P
393 UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
394 UPDATE USAGE USER USING
396 VACUUM VALID VALIDATOR VALUES VARCHAR VARYING
397 VERBOSE VERSION VIEW VOLATILE
399 WHEN WHERE WITH WITHOUT WORK WRITE
405 /* The grammar thinks these are keywords, but they are not in the keywords.c
406 * list and so can never be entered directly. The filter in parser.c
407 * creates these tokens when required.
411 /* Special keywords, not in the query language - see the "lex" file */
412 %token <str> IDENT FCONST SCONST NCONST BCONST XCONST Op
413 %token <ival> ICONST PARAM
415 /* precedence: lowest to highest */
423 %nonassoc LIKE ILIKE SIMILAR
428 %left POSTFIXOP /* dummy for postfix Op rules */
429 %left Op OPERATOR /* multi-character ops and user-defined operators */
432 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
436 /* Unary Operators */
437 %left AT ZONE /* sets precedence for AT TIME ZONE */
445 * These might seem to be low-precedence, but actually they are not part
446 * of the arithmetic hierarchy at all in their use as JOIN operators.
447 * We make them high-precedence to support their use as function names.
448 * They wouldn't be given a precedence at all, were it not that we need
449 * left-associativity among the JOIN rules themselves.
451 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
455 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
456 * psql already handles such cases, but other interfaces don't.
459 stmtblock: stmtmulti { parsetree = $1; }
462 /* the thrashing around here is to discard "empty" statements... */
463 stmtmulti: stmtmulti ';' stmt
464 { if ($3 != (Node *)NULL)
465 $$ = lappend($1, $3);
470 { if ($1 != (Node *)NULL)
495 | CreateConversionStmt
550 { $$ = (Node *)NULL; }
553 /*****************************************************************************
555 * Create a new Postgres DBMS user
558 *****************************************************************************/
561 CREATE USER UserId opt_with OptUserList
563 CreateUserStmt *n = makeNode(CreateUserStmt);
575 /*****************************************************************************
577 * Alter a postgresql DBMS user
580 *****************************************************************************/
583 ALTER USER UserId opt_with OptUserList
585 AlterUserStmt *n = makeNode(AlterUserStmt);
594 ALTER USER UserId SET set_rest
596 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
598 n->variable = $5->name;
602 | ALTER USER UserId VariableResetStmt
604 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
606 n->variable = ((VariableResetStmt *)$4)->name;
613 /*****************************************************************************
615 * Drop a postgresql DBMS user
617 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
618 * might own objects in multiple databases, there is presently no way to
619 * implement either cascading or restricting. Caveat DBA.
620 *****************************************************************************/
625 DropUserStmt *n = makeNode(DropUserStmt);
632 * Options for CREATE USER and ALTER USER
635 OptUserList OptUserElem { $$ = lappend($1, $2); }
636 | /* EMPTY */ { $$ = NIL; }
642 $$ = makeDefElem("password", (Node *)makeString($2));
644 | ENCRYPTED PASSWORD Sconst
646 $$ = makeDefElem("encryptedPassword", (Node *)makeString($3));
648 | UNENCRYPTED PASSWORD Sconst
650 $$ = makeDefElem("unencryptedPassword", (Node *)makeString($3));
654 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
658 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
662 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
666 $$ = makeDefElem("createuser", (Node *)makeInteger(TRUE));
670 $$ = makeDefElem("createuser", (Node *)makeInteger(FALSE));
672 | IN_P GROUP_P user_list
674 $$ = makeDefElem("groupElts", (Node *)$3);
678 $$ = makeDefElem("validUntil", (Node *)makeString($3));
682 user_list: user_list ',' UserId { $$ = lappend($1, makeString($3)); }
683 | UserId { $$ = makeList1(makeString($1)); }
688 /*****************************************************************************
690 * Create a postgresql group
693 *****************************************************************************/
696 CREATE GROUP_P UserId opt_with OptGroupList
698 CreateGroupStmt *n = makeNode(CreateGroupStmt);
706 * Options for CREATE GROUP
709 OptGroupList OptGroupElem { $$ = lappend($1, $2); }
710 | /* EMPTY */ { $$ = NIL; }
716 $$ = makeDefElem("userElts", (Node *)$2);
720 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
725 /*****************************************************************************
727 * Alter a postgresql group
730 *****************************************************************************/
733 ALTER GROUP_P UserId add_drop USER user_list
735 AlterGroupStmt *n = makeNode(AlterGroupStmt);
743 add_drop: ADD { $$ = +1; }
748 /*****************************************************************************
750 * Drop a postgresql group
752 * XXX see above notes about cascading DROP USER; groups have same problem.
753 *****************************************************************************/
758 DropGroupStmt *n = makeNode(DropGroupStmt);
765 /*****************************************************************************
767 * Manipulate a schema
769 *****************************************************************************/
772 CREATE SCHEMA OptSchemaName AUTHORIZATION UserId OptSchemaEltList
774 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
775 /* One can omit the schema name or the authorization id. */
784 | CREATE SCHEMA ColId OptSchemaEltList
786 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
787 /* ...but not both */
797 | /* EMPTY */ { $$ = NULL; }
801 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
802 | /* EMPTY */ { $$ = NIL; }
806 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
807 * statement (in addition to by themselves).
816 /*****************************************************************************
818 * Set PG internal variable
819 * SET name TO 'var_value'
820 * Include SQL92 syntax (thomas 1997-10-22):
821 * SET TIME ZONE 'var_value'
823 *****************************************************************************/
828 VariableSetStmt *n = $2;
834 VariableSetStmt *n = $3;
838 | SET SESSION set_rest
840 VariableSetStmt *n = $3;
846 set_rest: ColId TO var_list_or_default
848 VariableSetStmt *n = makeNode(VariableSetStmt);
853 | ColId '=' var_list_or_default
855 VariableSetStmt *n = makeNode(VariableSetStmt);
860 | TIME ZONE zone_value
862 VariableSetStmt *n = makeNode(VariableSetStmt);
863 n->name = "timezone";
865 n->args = makeList1($3);
868 | TRANSACTION transaction_mode_list
870 VariableSetStmt *n = makeNode(VariableSetStmt);
871 n->name = "TRANSACTION";
875 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
877 VariableSetStmt *n = makeNode(VariableSetStmt);
878 n->name = "SESSION CHARACTERISTICS";
884 VariableSetStmt *n = makeNode(VariableSetStmt);
885 n->name = "client_encoding";
887 n->args = makeList1(makeStringConst($2, NULL));
890 | SESSION AUTHORIZATION ColId_or_Sconst
892 VariableSetStmt *n = makeNode(VariableSetStmt);
893 n->name = "session_authorization";
894 n->args = makeList1(makeStringConst($3, NULL));
897 | SESSION AUTHORIZATION DEFAULT
899 VariableSetStmt *n = makeNode(VariableSetStmt);
900 n->name = "session_authorization";
907 var_list { $$ = $1; }
908 | DEFAULT { $$ = NIL; }
911 var_list: var_value { $$ = makeList1($1); }
912 | var_list ',' var_value { $$ = lappend($1, $3); }
915 var_value: opt_boolean
916 { $$ = makeStringConst($1, NULL); }
918 { $$ = makeStringConst($1, NULL); }
920 { $$ = makeAConst($1); }
923 iso_level: READ COMMITTED { $$ = "read committed"; }
924 | SERIALIZABLE { $$ = "serializable"; }
928 TRUE_P { $$ = "true"; }
929 | FALSE_P { $$ = "false"; }
931 | OFF { $$ = "off"; }
934 /* Timezone values can be:
935 * - a string such as 'pst8pdt'
936 * - an identifier such as "pst8pdt"
937 * - an integer or floating point number
938 * - a time interval per SQL99
939 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
940 * so use IDENT and reject anything which is a reserved word.
945 $$ = makeStringConst($1, NULL);
949 $$ = makeStringConst($1, NULL);
951 | ConstInterval Sconst opt_interval
953 A_Const *n = (A_Const *) makeStringConst($2, $1);
954 if ($3 != INTERVAL_FULL_RANGE)
956 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
959 " must be HOUR or HOUR TO MINUTE");
960 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
964 | ConstInterval '(' Iconst ')' Sconst opt_interval
966 A_Const *n = (A_Const *) makeStringConst($5, $1);
969 "INTERVAL(%d) precision must not be negative",
971 if ($3 > MAX_INTERVAL_PRECISION)
974 "INTERVAL(%d) precision reduced to maximum allowed, %d",
975 $3, MAX_INTERVAL_PRECISION);
976 $3 = MAX_INTERVAL_PRECISION;
979 if (($6 != INTERVAL_FULL_RANGE)
980 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
983 " must be HOUR or HOUR TO MINUTE");
985 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
989 | NumericOnly { $$ = makeAConst($1); }
990 | DEFAULT { $$ = NULL; }
991 | LOCAL { $$ = NULL; }
996 | DEFAULT { $$ = NULL; }
997 | /*EMPTY*/ { $$ = NULL; }
1002 | SCONST { $$ = $1; }
1009 VariableShowStmt *n = makeNode(VariableShowStmt);
1015 VariableShowStmt *n = makeNode(VariableShowStmt);
1016 n->name = "timezone";
1019 | SHOW TRANSACTION ISOLATION LEVEL
1021 VariableShowStmt *n = makeNode(VariableShowStmt);
1022 n->name = "transaction_isolation";
1025 | SHOW SESSION AUTHORIZATION
1027 VariableShowStmt *n = makeNode(VariableShowStmt);
1028 n->name = "session_authorization";
1033 VariableShowStmt *n = makeNode(VariableShowStmt);
1042 VariableResetStmt *n = makeNode(VariableResetStmt);
1048 VariableResetStmt *n = makeNode(VariableResetStmt);
1049 n->name = "timezone";
1052 | RESET TRANSACTION ISOLATION LEVEL
1054 VariableResetStmt *n = makeNode(VariableResetStmt);
1055 n->name = "transaction_isolation";
1058 | RESET SESSION AUTHORIZATION
1060 VariableResetStmt *n = makeNode(VariableResetStmt);
1061 n->name = "session_authorization";
1066 VariableResetStmt *n = makeNode(VariableResetStmt);
1074 SET CONSTRAINTS constraints_set_list constraints_set_mode
1076 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1077 n->constraints = $3;
1083 constraints_set_list:
1085 | name_list { $$ = $1; }
1088 constraints_set_mode:
1089 DEFERRED { $$ = TRUE; }
1090 | IMMEDIATE { $$ = FALSE; }
1095 * Checkpoint statement
1100 CheckPointStmt *n = makeNode(CheckPointStmt);
1106 /*****************************************************************************
1108 * ALTER TABLE variations
1110 *****************************************************************************/
1113 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1114 ALTER TABLE relation_expr ADD opt_column columnDef
1116 AlterTableStmt *n = makeNode(AlterTableStmt);
1122 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1123 | ALTER TABLE relation_expr ALTER opt_column ColId alter_column_default
1125 AlterTableStmt *n = makeNode(AlterTableStmt);
1132 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1133 | ALTER TABLE relation_expr ALTER opt_column ColId DROP NOT NULL_P
1135 AlterTableStmt *n = makeNode(AlterTableStmt);
1141 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1142 | ALTER TABLE relation_expr ALTER opt_column ColId SET NOT NULL_P
1144 AlterTableStmt *n = makeNode(AlterTableStmt);
1150 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1151 | ALTER TABLE relation_expr ALTER opt_column ColId SET STATISTICS IntegerOnly
1153 AlterTableStmt *n = makeNode(AlterTableStmt);
1157 n->def = (Node *) $9;
1160 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1161 | ALTER TABLE relation_expr ALTER opt_column ColId
1164 AlterTableStmt *n = makeNode(AlterTableStmt);
1168 n->def = (Node *) makeString($9);
1171 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1172 | ALTER TABLE relation_expr DROP opt_column ColId opt_drop_behavior
1174 AlterTableStmt *n = makeNode(AlterTableStmt);
1181 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1182 | ALTER TABLE relation_expr ADD TableConstraint
1184 AlterTableStmt *n = makeNode(AlterTableStmt);
1190 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1191 | ALTER TABLE relation_expr DROP CONSTRAINT name opt_drop_behavior
1193 AlterTableStmt *n = makeNode(AlterTableStmt);
1200 /* ALTER TABLE <relation> SET WITHOUT OIDS */
1201 | ALTER TABLE relation_expr SET WITHOUT OIDS
1203 AlterTableStmt *n = makeNode(AlterTableStmt);
1208 /* ALTER TABLE <name> CREATE TOAST TABLE */
1209 | ALTER TABLE qualified_name CREATE TOAST TABLE
1211 AlterTableStmt *n = makeNode(AlterTableStmt);
1213 $3->inhOpt = INH_NO;
1217 /* ALTER TABLE <name> OWNER TO UserId */
1218 | ALTER TABLE qualified_name OWNER TO UserId
1220 AlterTableStmt *n = makeNode(AlterTableStmt);
1222 $3->inhOpt = INH_NO;
1227 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1228 | ALTER TABLE qualified_name CLUSTER ON name
1230 AlterTableStmt *n = makeNode(AlterTableStmt);
1238 alter_column_default:
1241 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1242 if (exprIsNullConstant($3))
1247 | DROP DEFAULT { $$ = NULL; }
1251 CASCADE { $$ = DROP_CASCADE; }
1252 | RESTRICT { $$ = DROP_RESTRICT; }
1253 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1257 /*****************************************************************************
1260 * close <portalname>
1262 *****************************************************************************/
1267 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1274 /*****************************************************************************
1277 * COPY <relname> ['(' columnList ')'] FROM/TO [WITH options]
1279 * BINARY, OIDS, and DELIMITERS kept in old locations
1280 * for backward compatibility. 2002-06-18
1282 *****************************************************************************/
1284 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1285 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1287 CopyStmt *n = makeNode(CopyStmt);
1294 /* Concatenate user-supplied flags */
1296 n->options = lappend(n->options, $2);
1298 n->options = lappend(n->options, $5);
1300 n->options = lappend(n->options, $8);
1302 n->options = nconc(n->options, $10);
1309 | TO { $$ = FALSE; }
1313 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1314 * used depends on the direction. (It really doesn't make sense to copy from
1315 * stdout. We silently correct the "typo". - AY 9/94
1319 | STDIN { $$ = NULL; }
1320 | STDOUT { $$ = NULL; }
1326 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1327 | /* EMPTY */ { $$ = NIL; }
1334 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1338 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1340 | DELIMITER opt_as Sconst
1342 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1344 | NULL_P opt_as Sconst
1346 $$ = makeDefElem("null", (Node *)makeString($3));
1350 /* The following exist for backward compatibility */
1355 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1357 | /*EMPTY*/ { $$ = NULL; }
1363 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1365 | /*EMPTY*/ { $$ = NULL; }
1369 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1370 opt_using DELIMITERS Sconst
1372 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1374 | /*EMPTY*/ { $$ = NULL; }
1383 /*****************************************************************************
1386 * CREATE TABLE relname
1388 *****************************************************************************/
1390 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1391 OptInherit OptWithOids OnCommitOption
1393 CreateStmt *n = makeNode(CreateStmt);
1397 n->inhRelations = $8;
1398 n->constraints = NIL;
1403 | CREATE OptTemp TABLE qualified_name OF qualified_name
1404 '(' OptTableElementList ')' OptWithOids OnCommitOption
1406 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1407 * by our inheritance capabilities. Let's try it...
1409 CreateStmt *n = makeNode(CreateStmt);
1413 n->inhRelations = makeList1($6);
1414 n->constraints = NIL;
1422 * Redundancy here is needed to avoid shift/reduce conflicts,
1423 * since TEMP is not a reserved word. See also OptTempTableName.
1425 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1426 * the LOCAL keyword is really meaningless.
1428 OptTemp: TEMPORARY { $$ = TRUE; }
1429 | TEMP { $$ = TRUE; }
1430 | LOCAL TEMPORARY { $$ = TRUE; }
1431 | LOCAL TEMP { $$ = TRUE; }
1432 | GLOBAL TEMPORARY { $$ = TRUE; }
1433 | GLOBAL TEMP { $$ = TRUE; }
1434 | /*EMPTY*/ { $$ = FALSE; }
1437 OptTableElementList:
1438 TableElementList { $$ = $1; }
1439 | /*EMPTY*/ { $$ = NIL; }
1447 | TableElementList ',' TableElement
1449 $$ = lappend($1, $3);
1454 columnDef { $$ = $1; }
1455 | TableLikeClause { $$ = $1; }
1456 | TableConstraint { $$ = $1; }
1459 columnDef: ColId Typename ColQualList opt_collate
1461 ColumnDef *n = makeNode(ColumnDef);
1464 n->constraints = $3;
1469 "CREATE TABLE / COLLATE %s not yet implemented; "
1470 "clause ignored", $4);
1477 ColQualList ColConstraint { $$ = lappend($1, $2); }
1478 | /*EMPTY*/ { $$ = NIL; }
1482 CONSTRAINT name ColConstraintElem
1484 switch (nodeTag($3))
1488 Constraint *n = (Constraint *)$3;
1492 case T_FkConstraint:
1494 FkConstraint *n = (FkConstraint *)$3;
1495 n->constr_name = $2;
1503 | ColConstraintElem { $$ = $1; }
1504 | ConstraintAttr { $$ = $1; }
1507 /* DEFAULT NULL is already the default for Postgres.
1508 * But define it here and carry it forward into the system
1509 * to make it explicit.
1510 * - thomas 1998-09-13
1512 * WITH NULL and NULL are not SQL92-standard syntax elements,
1513 * so leave them out. Use DEFAULT NULL to explicitly indicate
1514 * that a column may have that value. WITH NULL leads to
1515 * shift/reduce conflicts with WITH TIME ZONE anyway.
1516 * - thomas 1999-01-08
1518 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1519 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1520 * or be part of a_expr NOT LIKE or similar constructs).
1525 Constraint *n = makeNode(Constraint);
1526 n->contype = CONSTR_NOTNULL;
1529 n->cooked_expr = NULL;
1535 Constraint *n = makeNode(Constraint);
1536 n->contype = CONSTR_NULL;
1539 n->cooked_expr = NULL;
1545 Constraint *n = makeNode(Constraint);
1546 n->contype = CONSTR_UNIQUE;
1549 n->cooked_expr = NULL;
1555 Constraint *n = makeNode(Constraint);
1556 n->contype = CONSTR_PRIMARY;
1559 n->cooked_expr = NULL;
1563 | CHECK '(' a_expr ')'
1565 Constraint *n = makeNode(Constraint);
1566 n->contype = CONSTR_CHECK;
1569 n->cooked_expr = NULL;
1575 Constraint *n = makeNode(Constraint);
1576 n->contype = CONSTR_DEFAULT;
1578 if (exprIsNullConstant($2))
1580 /* DEFAULT NULL should be reported as empty expr */
1587 n->cooked_expr = NULL;
1591 | REFERENCES qualified_name opt_column_list key_match key_actions
1593 FkConstraint *n = makeNode(FkConstraint);
1594 n->constr_name = NULL;
1598 n->fk_matchtype = $4;
1599 n->fk_upd_action = (char) ($5 >> 8);
1600 n->fk_del_action = (char) ($5 & 0xFF);
1601 n->deferrable = FALSE;
1602 n->initdeferred = FALSE;
1608 * ConstraintAttr represents constraint attributes, which we parse as if
1609 * they were independent constraint clauses, in order to avoid shift/reduce
1610 * conflicts (since NOT might start either an independent NOT NULL clause
1611 * or an attribute). analyze.c is responsible for attaching the attribute
1612 * information to the preceding "real" constraint node, and for complaining
1613 * if attribute clauses appear in the wrong place or wrong combinations.
1615 * See also ConstraintAttributeSpec, which can be used in places where
1616 * there is no parsing conflict.
1621 Constraint *n = makeNode(Constraint);
1622 n->contype = CONSTR_ATTR_DEFERRABLE;
1627 Constraint *n = makeNode(Constraint);
1628 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1631 | INITIALLY DEFERRED
1633 Constraint *n = makeNode(Constraint);
1634 n->contype = CONSTR_ATTR_DEFERRED;
1637 | INITIALLY IMMEDIATE
1639 Constraint *n = makeNode(Constraint);
1640 n->contype = CONSTR_ATTR_IMMEDIATE;
1646 /* SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
1647 * This seems to be a poor man's inheritance capability, with the resulting
1648 * tables completely decoupled except for the original commonality in definitions.
1649 * Seems to have much in common with CREATE TABLE AS. - thomas 2002-06-19
1651 TableLikeClause: LIKE any_name
1653 elog(ERROR, "LIKE in table definitions not yet supported");
1659 /* ConstraintElem specifies constraint syntax which is not embedded into
1660 * a column definition. ColConstraintElem specifies the embedded form.
1661 * - thomas 1997-12-03
1664 CONSTRAINT name ConstraintElem
1666 switch (nodeTag($3))
1670 Constraint *n = (Constraint *)$3;
1674 case T_FkConstraint:
1676 FkConstraint *n = (FkConstraint *)$3;
1677 n->constr_name = $2;
1685 | ConstraintElem { $$ = $1; }
1689 CHECK '(' a_expr ')'
1691 Constraint *n = makeNode(Constraint);
1692 n->contype = CONSTR_CHECK;
1695 n->cooked_expr = NULL;
1698 | UNIQUE '(' columnList ')'
1700 Constraint *n = makeNode(Constraint);
1701 n->contype = CONSTR_UNIQUE;
1704 n->cooked_expr = NULL;
1708 | PRIMARY KEY '(' columnList ')'
1710 Constraint *n = makeNode(Constraint);
1711 n->contype = CONSTR_PRIMARY;
1714 n->cooked_expr = NULL;
1718 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
1719 opt_column_list key_match key_actions ConstraintAttributeSpec
1721 FkConstraint *n = makeNode(FkConstraint);
1722 n->constr_name = NULL;
1726 n->fk_matchtype = $9;
1727 n->fk_upd_action = (char) ($10 >> 8);
1728 n->fk_del_action = (char) ($10 & 0xFF);
1729 n->deferrable = ($11 & 1) != 0;
1730 n->initdeferred = ($11 & 2) != 0;
1736 '(' columnList ')' { $$ = $2; }
1737 | /*EMPTY*/ { $$ = NIL; }
1741 columnElem { $$ = makeList1($1); }
1742 | columnList ',' columnElem { $$ = lappend($1, $3); }
1747 $$ = (Node *) makeString($1);
1751 key_match: MATCH FULL
1753 $$ = FKCONSTR_MATCH_FULL;
1757 elog(ERROR, "FOREIGN KEY/MATCH PARTIAL not yet implemented");
1758 $$ = FKCONSTR_MATCH_PARTIAL;
1762 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1766 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1771 * We combine the update and delete actions into one value temporarily
1772 * for simplicity of parsing, and then break them down again in the
1773 * calling production. update is in the left 8 bits, delete in the right.
1774 * Note that NOACTION is the default.
1778 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1780 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
1781 | key_update key_delete
1782 { $$ = ($1 << 8) | ($2 & 0xFF); }
1783 | key_delete key_update
1784 { $$ = ($2 << 8) | ($1 & 0xFF); }
1786 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1789 key_update: ON UPDATE key_action { $$ = $3; }
1792 key_delete: ON DELETE_P key_action { $$ = $3; }
1796 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
1797 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
1798 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
1799 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
1800 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
1803 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
1804 | /*EMPTY*/ { $$ = NIL; }
1808 WITH OIDS { $$ = TRUE; }
1809 | WITHOUT OIDS { $$ = FALSE; }
1810 | /*EMPTY*/ { $$ = TRUE; }
1813 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
1814 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
1815 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
1816 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
1821 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
1826 CREATE OptTemp TABLE qualified_name OptCreateAs AS SelectStmt
1829 * When the SelectStmt is a set-operation tree, we must
1830 * stuff the INTO information into the leftmost component
1831 * Select, because that's where analyze.c will expect
1832 * to find it. Similarly, the output column names must
1833 * be attached to that Select's target list.
1835 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
1836 if (n->into != NULL)
1837 elog(ERROR, "CREATE TABLE AS may not specify INTO");
1840 n->intoColNames = $5;
1846 '(' CreateAsList ')' { $$ = $2; }
1847 | /*EMPTY*/ { $$ = NIL; }
1851 CreateAsElement { $$ = makeList1($1); }
1852 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1858 ColumnDef *n = makeNode(ColumnDef);
1863 n->is_not_null = false;
1864 n->raw_default = NULL;
1865 n->cooked_default = NULL;
1866 n->constraints = NIL;
1873 /*****************************************************************************
1876 * CREATE SEQUENCE seqname
1877 * ALTER SEQUENCE seqname
1879 *****************************************************************************/
1882 CREATE OptTemp SEQUENCE qualified_name OptSeqList
1884 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1893 ALTER SEQUENCE qualified_name OptSeqList
1895 AlterSeqStmt *n = makeNode(AlterSeqStmt);
1902 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
1903 | /*EMPTY*/ { $$ = NIL; }
1906 OptSeqElem: CACHE NumericOnly
1908 $$ = makeDefElem("cache", (Node *)$2);
1912 $$ = makeDefElem("cycle", (Node *)true);
1916 $$ = makeDefElem("cycle", (Node *)false);
1918 | INCREMENT opt_by NumericOnly
1920 $$ = makeDefElem("increment", (Node *)$3);
1922 | MAXVALUE NumericOnly
1924 $$ = makeDefElem("maxvalue", (Node *)$2);
1926 | MINVALUE NumericOnly
1928 $$ = makeDefElem("minvalue", (Node *)$2);
1932 $$ = makeDefElem("maxvalue", (Node *)NULL);
1936 $$ = makeDefElem("minvalue", (Node *)NULL);
1938 | START opt_with NumericOnly
1940 $$ = makeDefElem("start", (Node *)$3);
1942 | RESTART opt_with NumericOnly
1944 $$ = makeDefElem("restart", (Node *)$3);
1953 FloatOnly { $$ = $1; }
1954 | IntegerOnly { $$ = $1; }
1957 FloatOnly: FCONST { $$ = makeFloat($1); }
1968 $$ = makeInteger($1);
1972 $$ = makeInteger($2);
1973 $$->val.ival = - $$->val.ival;
1977 /*****************************************************************************
1980 * CREATE PROCEDURAL LANGUAGE ...
1981 * DROP PROCEDURAL LANGUAGE ...
1983 *****************************************************************************/
1986 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
1987 HANDLER handler_name opt_validator opt_lancompiler
1989 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1992 n->plvalidator = $8;
1999 TRUSTED { $$ = TRUE; }
2000 | /*EMPTY*/ { $$ = FALSE; }
2003 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2004 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2005 * Work around by using name and dotted_name separately.
2009 { $$ = makeList1(makeString($1)); }
2010 | dotted_name { $$ = $1; }
2014 LANCOMPILER Sconst { $$ = $2; }
2015 | /*EMPTY*/ { $$ = ""; }
2019 VALIDATOR handler_name { $$ = $2; }
2020 | /*EMPTY*/ { $$ = NULL; }
2024 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2026 DropPLangStmt *n = makeNode(DropPLangStmt);
2038 /*****************************************************************************
2041 * CREATE TRIGGER ...
2044 *****************************************************************************/
2047 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2048 qualified_name TriggerForSpec EXECUTE PROCEDURE
2049 func_name '(' TriggerFuncArgs ')'
2051 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2058 memcpy(n->actions, $5, 4);
2059 n->isconstraint = FALSE;
2060 n->deferrable = FALSE;
2061 n->initdeferred = FALSE;
2062 n->constrrel = NULL;
2065 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2066 qualified_name OptConstrFromTable
2067 ConstraintAttributeSpec
2068 FOR EACH ROW EXECUTE PROCEDURE
2069 func_name '(' TriggerFuncArgs ')'
2071 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2078 memcpy(n->actions, $6, 4);
2079 n->isconstraint = TRUE;
2080 n->deferrable = ($10 & 1) != 0;
2081 n->initdeferred = ($10 & 2) != 0;
2089 BEFORE { $$ = TRUE; }
2090 | AFTER { $$ = FALSE; }
2096 char *e = palloc(4);
2097 e[0] = $1; e[1] = '\0';
2100 | TriggerOneEvent OR TriggerOneEvent
2102 char *e = palloc(4);
2103 e[0] = $1; e[1] = $3; e[2] = '\0';
2106 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2108 char *e = palloc(4);
2109 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2115 INSERT { $$ = 'i'; }
2116 | DELETE_P { $$ = 'd'; }
2117 | UPDATE { $$ = 'u'; }
2121 FOR TriggerForOpt TriggerForType
2128 * If ROW/STATEMENT not specified, default to
2129 * STATEMENT, per SQL
2142 | STATEMENT { $$ = FALSE; }
2146 TriggerFuncArg { $$ = makeList1($1); }
2147 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2148 | /*EMPTY*/ { $$ = NIL; }
2155 snprintf(buf, sizeof(buf), "%d", $1);
2156 $$ = makeString(pstrdup(buf));
2158 | FCONST { $$ = makeString($1); }
2159 | Sconst { $$ = makeString($1); }
2160 | BCONST { $$ = makeString($1); }
2161 | XCONST { $$ = makeString($1); }
2162 | ColId { $$ = makeString($1); }
2166 FROM qualified_name { $$ = $2; }
2167 | /*EMPTY*/ { $$ = NULL; }
2170 ConstraintAttributeSpec:
2171 ConstraintDeferrabilitySpec
2173 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2175 if ($1 == 0 && $2 != 0)
2177 "INITIALLY DEFERRED constraint must be DEFERRABLE");
2180 | ConstraintTimeSpec
2187 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2189 if ($2 == 0 && $1 != 0)
2191 "INITIALLY DEFERRED constraint must be DEFERRABLE");
2198 ConstraintDeferrabilitySpec:
2199 NOT DEFERRABLE { $$ = 0; }
2200 | DEFERRABLE { $$ = 1; }
2204 INITIALLY IMMEDIATE { $$ = 0; }
2205 | INITIALLY DEFERRED { $$ = 2; }
2210 DROP TRIGGER name ON qualified_name opt_drop_behavior
2212 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2216 n->removeType = DROP_TRIGGER;
2222 /*****************************************************************************
2225 * CREATE ASSERTION ...
2226 * DROP ASSERTION ...
2228 *****************************************************************************/
2231 CREATE ASSERTION name CHECK '(' a_expr ')'
2232 ConstraintAttributeSpec
2234 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2236 n->args = makeList1($6);
2237 n->isconstraint = TRUE;
2238 n->deferrable = ($8 & 1) != 0;
2239 n->initdeferred = ($8 & 2) != 0;
2241 elog(ERROR, "CREATE ASSERTION is not yet supported");
2248 DROP ASSERTION name opt_drop_behavior
2250 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2254 n->removeType = DROP_TRIGGER; /* XXX */
2255 elog(ERROR, "DROP ASSERTION is not yet supported");
2261 /*****************************************************************************
2264 * define (aggregate,operator,type)
2266 *****************************************************************************/
2269 CREATE AGGREGATE func_name definition
2271 DefineStmt *n = makeNode(DefineStmt);
2272 n->kind = DEFINE_STMT_AGGREGATE;
2277 | CREATE OPERATOR any_operator definition
2279 DefineStmt *n = makeNode(DefineStmt);
2280 n->kind = DEFINE_STMT_OPERATOR;
2285 | CREATE TYPE_P any_name definition
2287 DefineStmt *n = makeNode(DefineStmt);
2288 n->kind = DEFINE_STMT_TYPE;
2293 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2295 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2296 RangeVar *r = makeNode(RangeVar);
2298 /* can't use qualified_name, sigh */
2302 r->catalogname = NULL;
2303 r->schemaname = NULL;
2304 r->relname = strVal(lfirst($3));
2307 r->catalogname = NULL;
2308 r->schemaname = strVal(lfirst($3));
2309 r->relname = strVal(lsecond($3));
2312 r->catalogname = strVal(lfirst($3));
2313 r->schemaname = strVal(lsecond($3));
2314 r->relname = strVal(lthird($3));
2318 "Improper qualified name (too many dotted names): %s",
2319 NameListToString($3));
2328 definition: '(' def_list ')' { $$ = $2; }
2331 def_list: def_elem { $$ = makeList1($1); }
2332 | def_list ',' def_elem { $$ = lappend($1, $3); }
2335 def_elem: ColLabel '=' def_arg
2337 $$ = makeDefElem($1, (Node *)$3);
2341 $$ = makeDefElem($1, (Node *)NULL);
2345 /* Note: any simple identifier will be returned as a type name! */
2346 def_arg: func_return { $$ = (Node *)$1; }
2347 | qual_all_Op { $$ = (Node *)$1; }
2348 | NumericOnly { $$ = (Node *)$1; }
2349 | Sconst { $$ = (Node *)makeString($1); }
2353 /*****************************************************************************
2356 * CREATE OPERATOR CLASS ...
2357 * DROP OPERATOR CLASS ...
2359 *****************************************************************************/
2362 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
2363 USING access_method AS opclass_item_list
2365 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
2366 n->opclassname = $4;
2376 opclass_item { $$ = makeList1($1); }
2377 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
2381 OPERATOR Iconst any_operator opt_recheck
2383 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2384 n->itemtype = OPCLASS_ITEM_OPERATOR;
2391 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
2393 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2394 n->itemtype = OPCLASS_ITEM_OPERATOR;
2401 | FUNCTION Iconst func_name func_args
2403 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2404 n->itemtype = OPCLASS_ITEM_FUNCTION;
2412 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2413 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
2419 opt_default: DEFAULT { $$ = TRUE; }
2420 | /*EMPTY*/ { $$ = FALSE; }
2423 opt_recheck: RECHECK { $$ = TRUE; }
2424 | /*EMPTY*/ { $$ = FALSE; }
2429 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
2431 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
2432 n->opclassname = $4;
2440 /*****************************************************************************
2444 * DROP itemtype itemname [, itemname ...] [ RESTRICT | CASCADE ]
2446 *****************************************************************************/
2448 DropStmt: DROP drop_type any_name_list opt_drop_behavior
2450 DropStmt *n = makeNode(DropStmt);
2458 drop_type: TABLE { $$ = DROP_TABLE; }
2459 | SEQUENCE { $$ = DROP_SEQUENCE; }
2460 | VIEW { $$ = DROP_VIEW; }
2461 | INDEX { $$ = DROP_INDEX; }
2462 | TYPE_P { $$ = DROP_TYPE; }
2463 | DOMAIN_P { $$ = DROP_DOMAIN; }
2464 | CONVERSION_P { $$ = DROP_CONVERSION; }
2465 | SCHEMA { $$ = DROP_SCHEMA; }
2469 any_name { $$ = makeList1($1); }
2470 | any_name_list ',' any_name { $$ = lappend($1, $3); }
2473 any_name: ColId { $$ = makeList1(makeString($1)); }
2474 | dotted_name { $$ = $1; }
2477 /*****************************************************************************
2480 * truncate table relname
2482 *****************************************************************************/
2485 TRUNCATE opt_table qualified_name
2487 TruncateStmt *n = makeNode(TruncateStmt);
2493 /*****************************************************************************
2495 * The COMMENT ON statement can take different forms based upon the type of
2496 * the object associated with the comment. The form of the statement is:
2498 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW ]
2499 * <objname> | AGGREGATE <aggname> (<aggtype>) | FUNCTION
2500 * <funcname> (arg1, arg2, ...) | OPERATOR <op>
2501 * (leftoperand_typ rightoperand_typ) | TRIGGER <triggername> ON
2502 * <relname> | RULE <rulename> ON <relname> ] IS 'text'
2504 *****************************************************************************/
2507 COMMENT ON comment_type any_name IS comment_text
2509 CommentStmt *n = makeNode(CommentStmt);
2516 | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')'
2519 CommentStmt *n = makeNode(CommentStmt);
2520 n->objtype = COMMENT_ON_AGGREGATE;
2522 n->objargs = makeList1($6);
2526 | COMMENT ON FUNCTION func_name func_args IS comment_text
2528 CommentStmt *n = makeNode(CommentStmt);
2529 n->objtype = COMMENT_ON_FUNCTION;
2535 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
2538 CommentStmt *n = makeNode(CommentStmt);
2539 n->objtype = COMMENT_ON_OPERATOR;
2545 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
2547 CommentStmt *n = makeNode(CommentStmt);
2548 n->objtype = COMMENT_ON_CONSTRAINT;
2549 n->objname = lappend($6, makeString($4));
2554 | COMMENT ON RULE name ON any_name IS comment_text
2556 CommentStmt *n = makeNode(CommentStmt);
2557 n->objtype = COMMENT_ON_RULE;
2558 n->objname = lappend($6, makeString($4));
2563 | COMMENT ON RULE name IS comment_text
2565 /* Obsolete syntax supported for awhile for compatibility */
2566 CommentStmt *n = makeNode(CommentStmt);
2567 n->objtype = COMMENT_ON_RULE;
2568 n->objname = makeList1(makeString($4));
2573 | COMMENT ON TRIGGER name ON any_name IS comment_text
2575 CommentStmt *n = makeNode(CommentStmt);
2576 n->objtype = COMMENT_ON_TRIGGER;
2577 n->objname = lappend($6, makeString($4));
2585 COLUMN { $$ = COMMENT_ON_COLUMN; }
2586 | DATABASE { $$ = COMMENT_ON_DATABASE; }
2587 | SCHEMA { $$ = COMMENT_ON_SCHEMA; }
2588 | INDEX { $$ = COMMENT_ON_INDEX; }
2589 | SEQUENCE { $$ = COMMENT_ON_SEQUENCE; }
2590 | TABLE { $$ = COMMENT_ON_TABLE; }
2591 | DOMAIN_P { $$ = COMMENT_ON_TYPE; }
2592 | TYPE_P { $$ = COMMENT_ON_TYPE; }
2593 | VIEW { $$ = COMMENT_ON_VIEW; }
2598 | NULL_P { $$ = NULL; }
2601 /*****************************************************************************
2606 *****************************************************************************/
2608 FetchStmt: FETCH fetch_direction from_in name
2610 FetchStmt *n = (FetchStmt *) $2;
2617 FetchStmt *n = makeNode(FetchStmt);
2618 n->direction = FETCH_FORWARD;
2624 | MOVE fetch_direction from_in name
2626 FetchStmt *n = (FetchStmt *) $2;
2633 FetchStmt *n = makeNode(FetchStmt);
2634 n->direction = FETCH_FORWARD;
2645 FetchStmt *n = makeNode(FetchStmt);
2646 n->direction = FETCH_FORWARD;
2652 FetchStmt *n = makeNode(FetchStmt);
2653 n->direction = FETCH_FORWARD;
2659 FetchStmt *n = makeNode(FetchStmt);
2660 n->direction = FETCH_BACKWARD;
2666 FetchStmt *n = makeNode(FetchStmt);
2667 n->direction = FETCH_ABSOLUTE;
2673 FetchStmt *n = makeNode(FetchStmt);
2674 n->direction = FETCH_ABSOLUTE;
2678 | ABSOLUTE fetch_count
2680 FetchStmt *n = makeNode(FetchStmt);
2681 n->direction = FETCH_ABSOLUTE;
2685 | RELATIVE fetch_count
2687 FetchStmt *n = makeNode(FetchStmt);
2688 n->direction = FETCH_RELATIVE;
2694 FetchStmt *n = makeNode(FetchStmt);
2695 n->direction = FETCH_FORWARD;
2701 FetchStmt *n = makeNode(FetchStmt);
2702 n->direction = FETCH_FORWARD;
2703 n->howMany = FETCH_ALL;
2708 FetchStmt *n = makeNode(FetchStmt);
2709 n->direction = FETCH_FORWARD;
2713 | FORWARD fetch_count
2715 FetchStmt *n = makeNode(FetchStmt);
2716 n->direction = FETCH_FORWARD;
2722 FetchStmt *n = makeNode(FetchStmt);
2723 n->direction = FETCH_FORWARD;
2724 n->howMany = FETCH_ALL;
2729 FetchStmt *n = makeNode(FetchStmt);
2730 n->direction = FETCH_BACKWARD;
2734 | BACKWARD fetch_count
2736 FetchStmt *n = makeNode(FetchStmt);
2737 n->direction = FETCH_BACKWARD;
2743 FetchStmt *n = makeNode(FetchStmt);
2744 n->direction = FETCH_BACKWARD;
2745 n->howMany = FETCH_ALL;
2752 | '-' Iconst { $$ = - $2; }
2760 /*****************************************************************************
2762 * GRANT and REVOKE statements
2764 *****************************************************************************/
2766 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
2767 opt_grant_grant_option
2769 GrantStmt *n = makeNode(GrantStmt);
2772 n->objtype = ($4)->objtype;
2773 n->objects = ($4)->objs;
2775 n->grant_option = $7;
2780 RevokeStmt: REVOKE opt_revoke_grant_option privileges ON privilege_target
2781 FROM grantee_list opt_drop_behavior
2783 GrantStmt *n = makeNode(GrantStmt);
2784 n->is_grant = false;
2786 n->objtype = ($5)->objtype;
2787 n->objects = ($5)->objs;
2789 n->grant_option = $2;
2797 /* either ALL [PRIVILEGES] or a list of individual privileges */
2798 privileges: privilege_list { $$ = $1; }
2799 | ALL { $$ = makeListi1(ACL_ALL_RIGHTS); }
2800 | ALL PRIVILEGES { $$ = makeListi1(ACL_ALL_RIGHTS); }
2804 privilege { $$ = makeListi1($1); }
2805 | privilege_list ',' privilege { $$ = lappendi($1, $3); }
2808 /* Not all of these privilege types apply to all objects, but that
2809 * gets sorted out later.
2811 privilege: SELECT { $$ = ACL_SELECT; }
2812 | INSERT { $$ = ACL_INSERT; }
2813 | UPDATE { $$ = ACL_UPDATE; }
2814 | DELETE_P { $$ = ACL_DELETE; }
2815 | RULE { $$ = ACL_RULE; }
2816 | REFERENCES { $$ = ACL_REFERENCES; }
2817 | TRIGGER { $$ = ACL_TRIGGER; }
2818 | EXECUTE { $$ = ACL_EXECUTE; }
2819 | USAGE { $$ = ACL_USAGE; }
2820 | CREATE { $$ = ACL_CREATE; }
2821 | TEMPORARY { $$ = ACL_CREATE_TEMP; }
2822 | TEMP { $$ = ACL_CREATE_TEMP; }
2826 /* Don't bother trying to fold the first two rules into one using
2827 opt_table. You're going to get conflicts. */
2831 PrivTarget *n = makeNode(PrivTarget);
2832 n->objtype = ACL_OBJECT_RELATION;
2836 | TABLE qualified_name_list
2838 PrivTarget *n = makeNode(PrivTarget);
2839 n->objtype = ACL_OBJECT_RELATION;
2843 | FUNCTION function_with_argtypes_list
2845 PrivTarget *n = makeNode(PrivTarget);
2846 n->objtype = ACL_OBJECT_FUNCTION;
2850 | DATABASE name_list
2852 PrivTarget *n = makeNode(PrivTarget);
2853 n->objtype = ACL_OBJECT_DATABASE;
2857 | LANGUAGE name_list
2859 PrivTarget *n = makeNode(PrivTarget);
2860 n->objtype = ACL_OBJECT_LANGUAGE;
2866 PrivTarget *n = makeNode(PrivTarget);
2867 n->objtype = ACL_OBJECT_NAMESPACE;
2875 grantee { $$ = makeList1($1); }
2876 | grantee_list ',' grantee { $$ = lappend($1, $3); }
2881 PrivGrantee *n = makeNode(PrivGrantee);
2882 /* This hack lets us avoid reserving PUBLIC as a keyword*/
2883 if (strcmp($1, "public") == 0)
2887 n->groupname = NULL;
2892 PrivGrantee *n = makeNode(PrivGrantee);
2893 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
2894 if (strcmp($2, "public") == 0)
2895 n->groupname = NULL;
2904 opt_grant_grant_option:
2905 WITH GRANT OPTION { $$ = TRUE; }
2906 | /*EMPTY*/ { $$ = FALSE; }
2909 opt_revoke_grant_option:
2910 GRANT OPTION FOR { $$ = TRUE; }
2911 | /*EMPTY*/ { $$ = FALSE; }
2915 function_with_argtypes_list:
2916 function_with_argtypes { $$ = makeList1($1); }
2917 | function_with_argtypes_list ',' function_with_argtypes
2918 { $$ = lappend($1, $3); }
2921 function_with_argtypes:
2924 FuncWithArgs *n = makeNode(FuncWithArgs);
2932 /*****************************************************************************
2935 * create index <indexname> on <relname>
2936 * [ using <access> ] "(" (<col> with <op>)+ ")"
2937 * [ where <predicate> ]
2939 *****************************************************************************/
2941 IndexStmt: CREATE index_opt_unique INDEX index_name ON qualified_name
2942 access_method_clause '(' index_params ')' where_clause
2944 IndexStmt *n = makeNode(IndexStmt);
2948 n->accessMethod = $7;
2949 n->indexParams = $9;
2950 n->whereClause = $11;
2956 UNIQUE { $$ = TRUE; }
2957 | /*EMPTY*/ { $$ = FALSE; }
2960 access_method_clause:
2961 USING access_method { $$ = $2; }
2962 /* If btree changes as our default, update pg_get_indexdef() */
2963 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
2967 index_list { $$ = $1; }
2968 | func_index { $$ = makeList1($1); }
2971 index_list: index_elem { $$ = makeList1($1); }
2972 | index_list ',' index_elem { $$ = lappend($1, $3); }
2975 func_index: func_name '(' name_list ')' opt_class
2977 $$ = makeNode(IndexElem);
2985 index_elem: attr_name opt_class
2987 $$ = makeNode(IndexElem);
2998 * Release 7.0 removed network_ops, timespan_ops, and
2999 * datetime_ops, so we suppress it from being passed to
3000 * the parser so the default *_ops is used. This can be
3001 * removed in some later release. bjm 2000/02/07
3003 * Release 7.1 removes lztext_ops, so suppress that too
3004 * for a while. tgl 2000/07/30
3006 * Release 7.2 renames timestamp_ops to timestamptz_ops,
3007 * so suppress that too for awhile. I'm starting to
3008 * think we need a better approach. tgl 2000/10/01
3010 if (length($1) == 1)
3012 char *claname = strVal(lfirst($1));
3014 if (strcmp(claname, "network_ops") != 0 &&
3015 strcmp(claname, "timespan_ops") != 0 &&
3016 strcmp(claname, "datetime_ops") != 0 &&
3017 strcmp(claname, "lztext_ops") != 0 &&
3018 strcmp(claname, "timestamp_ops") != 0)
3026 | USING any_name { $$ = $2; }
3027 | /*EMPTY*/ { $$ = NIL; }
3030 /*****************************************************************************
3033 * create [or replace] function <fname>
3034 * [(<type-1> { , <type-n>})]
3036 * as <filename or code in language as appropriate>
3037 * language <lang> [with parameters]
3039 *****************************************************************************/
3042 CREATE opt_or_replace FUNCTION func_name func_args
3043 RETURNS func_return createfunc_opt_list opt_definition
3045 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3057 OR REPLACE { $$ = TRUE; }
3058 | /*EMPTY*/ { $$ = FALSE; }
3061 func_args: '(' func_args_list ')' { $$ = $2; }
3062 | '(' ')' { $$ = NIL; }
3066 func_arg { $$ = makeList1($1); }
3067 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
3070 func_arg: opt_arg func_type
3072 /* We can catch over-specified arguments here if we want to,
3073 * but for now better to silently swallow typmod, etc.
3074 * - thomas 2000-03-22
3078 | func_type { $$ = $1; }
3081 opt_arg: IN_P { $$ = FALSE; }
3085 "CREATE FUNCTION / OUT parameters are not supported");
3091 "CREATE FUNCTION / INOUT parameters are not supported");
3099 /* We can catch over-specified arguments here if we want to,
3100 * but for now better to silently swallow typmod, etc.
3101 * - thomas 2000-03-22
3108 * We would like to make the second production here be ColId attrs etc,
3109 * but that causes reduce/reduce conflicts. type_name is next best choice.
3111 func_type: Typename { $$ = $1; }
3112 | type_name attrs '%' TYPE_P
3114 $$ = makeNode(TypeName);
3115 $$->names = lcons(makeString($1), $2);
3116 $$->pct_type = true;
3122 createfunc_opt_list:
3123 /* Must be at least one to prevent conflict */
3124 createfunc_opt_item { $$ = makeList1($1); }
3125 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
3128 createfunc_opt_item:
3131 $$ = makeDefElem("as", (Node *)$2);
3133 | LANGUAGE ColId_or_Sconst
3135 $$ = makeDefElem("language", (Node *)makeString($2));
3139 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
3143 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
3147 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
3149 | CALLED ON NULL_P INPUT
3151 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
3153 | RETURNS NULL_P ON NULL_P INPUT
3155 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3159 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3161 | EXTERNAL SECURITY DEFINER
3163 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3165 | EXTERNAL SECURITY INVOKER
3167 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3171 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3175 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3179 func_as: Sconst { $$ = makeList1(makeString($1)); }
3182 $$ = makeList2(makeString($1), makeString($3));
3187 WITH definition { $$ = $2; }
3188 | /*EMPTY*/ { $$ = NIL; }
3192 /*****************************************************************************
3196 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
3197 * DROP AGGREGATE aggname (aggtype) [ RESTRICT | CASCADE ]
3198 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
3200 *****************************************************************************/
3203 DROP FUNCTION func_name func_args opt_drop_behavior
3205 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
3214 DROP AGGREGATE func_name '(' aggr_argtype ')' opt_drop_behavior
3216 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
3225 Typename { $$ = $1; }
3226 | '*' { $$ = NULL; }
3230 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
3232 RemoveOperStmt *n = makeNode(RemoveOperStmt);
3243 elog(ERROR, "parser: argument type missing (use NONE for unary operators)");
3245 | Typename ',' Typename
3246 { $$ = makeList2($1, $3); }
3247 | NONE ',' Typename /* left unary */
3248 { $$ = makeList2(NULL, $3); }
3249 | Typename ',' NONE /* right unary */
3250 { $$ = makeList2($1, NULL); }
3255 { $$ = makeList1(makeString($1)); }
3256 | ColId '.' any_operator
3257 { $$ = lcons(makeString($1), $3); }
3261 /*****************************************************************************
3263 * CREATE CAST / DROP CAST
3265 *****************************************************************************/
3267 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
3268 WITH FUNCTION function_with_argtypes cast_context
3270 CreateCastStmt *n = makeNode(CreateCastStmt);
3273 n->func = (FuncWithArgs *) $10;
3274 n->context = (CoercionContext) $11;
3277 | CREATE CAST '(' Typename AS Typename ')'
3278 WITHOUT FUNCTION cast_context
3280 CreateCastStmt *n = makeNode(CreateCastStmt);
3284 n->context = (CoercionContext) $10;
3289 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
3290 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
3291 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
3295 DropCastStmt: DROP CAST '(' Typename AS Typename ')' opt_drop_behavior
3297 DropCastStmt *n = makeNode(DropCastStmt);
3307 /*****************************************************************************
3311 * REINDEX type <typename> [FORCE] [ALL]
3313 *****************************************************************************/
3316 REINDEX reindex_type qualified_name opt_force
3318 ReindexStmt *n = makeNode(ReindexStmt);
3325 | REINDEX DATABASE name opt_force
3327 ReindexStmt *n = makeNode(ReindexStmt);
3328 n->kind = REINDEX_DATABASE;
3337 INDEX { $$ = REINDEX_INDEX; }
3338 | TABLE { $$ = REINDEX_TABLE; }
3341 opt_force: FORCE { $$ = TRUE; }
3342 | /* EMPTY */ { $$ = FALSE; }
3346 /*****************************************************************************
3349 * rename <attrname1> in <relname> [*] to <attrname2>
3350 * rename <relname1> to <relname2>
3352 *****************************************************************************/
3354 RenameStmt: ALTER TABLE relation_expr RENAME opt_column opt_name TO name
3356 RenameStmt *n = makeNode(RenameStmt);
3361 n->renameType = RENAME_TABLE;
3363 n->renameType = RENAME_COLUMN;
3366 | ALTER TRIGGER name ON relation_expr RENAME TO name
3368 RenameStmt *n = makeNode(RenameStmt);
3372 n->renameType = RENAME_TRIGGER;
3377 opt_name: name { $$ = $1; }
3378 | /*EMPTY*/ { $$ = NULL; }
3381 opt_column: COLUMN { $$ = COLUMN; }
3382 | /*EMPTY*/ { $$ = 0; }
3386 /*****************************************************************************
3388 * QUERY: Define Rewrite Rule
3390 *****************************************************************************/
3392 RuleStmt: CREATE opt_or_replace RULE name AS
3393 { QueryIsRule=TRUE; }
3394 ON event TO qualified_name where_clause
3395 DO opt_instead RuleActionList
3397 RuleStmt *n = makeNode(RuleStmt);
3401 n->whereClause = $11;
3411 NOTHING { $$ = NIL; }
3412 | RuleActionStmt { $$ = makeList1($1); }
3413 | '(' RuleActionMulti ')' { $$ = $2; }
3416 /* the thrashing around here is to discard "empty" statements... */
3418 RuleActionMulti ';' RuleActionStmtOrEmpty
3419 { if ($3 != (Node *) NULL)
3420 $$ = lappend($1, $3);
3424 | RuleActionStmtOrEmpty
3425 { if ($1 != (Node *) NULL)
3440 RuleActionStmtOrEmpty:
3441 RuleActionStmt { $$ = $1; }
3442 | /*EMPTY*/ { $$ = (Node *)NULL; }
3445 /* change me to select, update, etc. some day */
3446 event: SELECT { $$ = CMD_SELECT; }
3447 | UPDATE { $$ = CMD_UPDATE; }
3448 | DELETE_P { $$ = CMD_DELETE; }
3449 | INSERT { $$ = CMD_INSERT; }
3453 INSTEAD { $$ = TRUE; }
3454 | /*EMPTY*/ { $$ = FALSE; }
3459 DROP RULE name ON qualified_name opt_drop_behavior
3461 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3465 n->removeType = DROP_RULE;
3471 /*****************************************************************************
3474 * NOTIFY <qualified_name> can appear both in rule bodies and
3475 * as a query-level command
3477 *****************************************************************************/
3479 NotifyStmt: NOTIFY qualified_name
3481 NotifyStmt *n = makeNode(NotifyStmt);
3487 ListenStmt: LISTEN qualified_name
3489 ListenStmt *n = makeNode(ListenStmt);
3496 UNLISTEN qualified_name
3498 UnlistenStmt *n = makeNode(UnlistenStmt);
3504 UnlistenStmt *n = makeNode(UnlistenStmt);
3505 n->relation = makeNode(RangeVar);
3506 n->relation->relname = "*";
3507 n->relation->schemaname = NULL;
3513 /*****************************************************************************
3517 * BEGIN / COMMIT / ROLLBACK
3518 * (also older versions END / ABORT)
3520 *****************************************************************************/
3523 ABORT_P opt_transaction
3525 TransactionStmt *n = makeNode(TransactionStmt);
3526 n->kind = TRANS_STMT_ROLLBACK;
3530 | BEGIN_P opt_transaction
3532 TransactionStmt *n = makeNode(TransactionStmt);
3533 n->kind = TRANS_STMT_BEGIN;
3537 | START TRANSACTION transaction_mode_list_or_empty
3539 TransactionStmt *n = makeNode(TransactionStmt);
3540 n->kind = TRANS_STMT_START;
3544 | COMMIT opt_transaction
3546 TransactionStmt *n = makeNode(TransactionStmt);
3547 n->kind = TRANS_STMT_COMMIT;
3551 | END_P opt_transaction
3553 TransactionStmt *n = makeNode(TransactionStmt);
3554 n->kind = TRANS_STMT_COMMIT;
3558 | ROLLBACK opt_transaction
3560 TransactionStmt *n = makeNode(TransactionStmt);
3561 n->kind = TRANS_STMT_ROLLBACK;
3567 opt_transaction: WORK {}
3572 transaction_mode_list:
3573 ISOLATION LEVEL iso_level
3574 { $$ = makeList1(makeDefElem("transaction_isolation",
3575 makeStringConst($3, NULL))); }
3576 | transaction_access_mode
3577 { $$ = makeList1(makeDefElem("transaction_read_only",
3578 makeIntConst($1))); }
3579 | ISOLATION LEVEL iso_level transaction_access_mode
3581 $$ = makeList2(makeDefElem("transaction_isolation",
3582 makeStringConst($3, NULL)),
3583 makeDefElem("transaction_read_only",
3586 | transaction_access_mode ISOLATION LEVEL iso_level
3588 $$ = makeList2(makeDefElem("transaction_read_only",
3590 makeDefElem("transaction_isolation",
3591 makeStringConst($4, NULL)));
3595 transaction_mode_list_or_empty:
3596 transaction_mode_list
3601 transaction_access_mode:
3602 READ ONLY { $$ = TRUE; }
3603 | READ WRITE { $$ = FALSE; }
3607 /*****************************************************************************
3610 * create view <viewname> '('target-list ')' AS <query>
3612 *****************************************************************************/
3614 ViewStmt: CREATE opt_or_replace VIEW qualified_name opt_column_list
3617 ViewStmt *n = makeNode(ViewStmt);
3621 n->query = (Query *) $7;
3627 /*****************************************************************************
3632 *****************************************************************************/
3634 LoadStmt: LOAD file_name
3636 LoadStmt *n = makeNode(LoadStmt);
3643 /*****************************************************************************
3647 *****************************************************************************/
3650 CREATE DATABASE database_name opt_with createdb_opt_list
3652 CreatedbStmt *n = makeNode(CreatedbStmt);
3660 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
3661 | /* EMPTY */ { $$ = NIL; }
3665 LOCATION opt_equal Sconst
3667 $$ = makeDefElem("location", (Node *)makeString($3));
3669 | LOCATION opt_equal DEFAULT
3671 $$ = makeDefElem("location", NULL);
3673 | TEMPLATE opt_equal name
3675 $$ = makeDefElem("template", (Node *)makeString($3));
3677 | TEMPLATE opt_equal DEFAULT
3679 $$ = makeDefElem("template", NULL);
3681 | ENCODING opt_equal Sconst
3683 $$ = makeDefElem("encoding", (Node *)makeString($3));
3685 | ENCODING opt_equal Iconst
3687 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
3689 | ENCODING opt_equal DEFAULT
3691 $$ = makeDefElem("encoding", NULL);
3693 | OWNER opt_equal name
3695 $$ = makeDefElem("owner", (Node *)makeString($3));
3697 | OWNER opt_equal DEFAULT
3699 $$ = makeDefElem("owner", NULL);
3704 * Though the equals sign doesn't match other WITH options, pg_dump uses
3705 * equals for backward compability, and it doesn't seem worth removing it.
3713 /*****************************************************************************
3717 *****************************************************************************/
3719 AlterDatabaseSetStmt:
3720 ALTER DATABASE database_name SET set_rest
3722 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3724 n->variable = $5->name;
3725 n->value = $5->args;
3728 | ALTER DATABASE database_name VariableResetStmt
3730 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3732 n->variable = ((VariableResetStmt *)$4)->name;
3739 /*****************************************************************************
3743 * This is implicitly CASCADE, no need for drop behavior
3744 *****************************************************************************/
3746 DropdbStmt: DROP DATABASE database_name
3748 DropdbStmt *n = makeNode(DropdbStmt);
3755 /*****************************************************************************
3757 * Manipulate a domain
3759 *****************************************************************************/
3762 CREATE DOMAIN_P any_name opt_as Typename ColQualList opt_collate
3764 CreateDomainStmt *n = makeNode(CreateDomainStmt);
3767 n->constraints = $6;
3770 elog(NOTICE,"CREATE DOMAIN / COLLATE %s not yet "
3771 "implemented; clause ignored", $7);
3777 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
3778 ALTER DOMAIN_P any_name alter_column_default
3780 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3786 /* ALTER DOMAIN <domain> DROP NOT NULL */
3787 | ALTER DOMAIN_P any_name DROP NOT NULL_P
3789 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3794 /* ALTER DOMAIN <domain> SET NOT NULL */
3795 | ALTER DOMAIN_P any_name SET NOT NULL_P
3797 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3802 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
3803 | ALTER DOMAIN_P any_name ADD TableConstraint
3805 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3811 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
3812 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
3814 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3821 /* ALTER DOMAIN <domain> OWNER TO UserId */
3822 | ALTER DOMAIN_P any_name OWNER TO UserId
3824 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3837 /*****************************************************************************
3839 * Manipulate a conversion
3841 * CREATE [DEFAULT] CONVERSION <conversion_name>
3842 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
3844 *****************************************************************************/
3846 CreateConversionStmt:
3847 CREATE opt_default CONVERSION_P any_name FOR Sconst
3848 TO Sconst FROM any_name
3850 CreateConversionStmt *n = makeNode(CreateConversionStmt);
3851 n->conversion_name = $4;
3852 n->for_encoding_name = $6;
3853 n->to_encoding_name = $8;
3860 /*****************************************************************************
3863 * cluster <index_name> on <qualified_name>
3864 * cluster <qualified_name>
3867 *****************************************************************************/
3870 CLUSTER index_name ON qualified_name
3872 ClusterStmt *n = makeNode(ClusterStmt);
3877 | CLUSTER qualified_name
3879 ClusterStmt *n = makeNode(ClusterStmt);
3881 n->indexname = NULL;
3886 ClusterStmt *n = makeNode(ClusterStmt);
3888 n->indexname = NULL;
3893 /*****************************************************************************
3899 *****************************************************************************/
3901 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
3903 VacuumStmt *n = makeNode(VacuumStmt);
3913 | VACUUM opt_full opt_freeze opt_verbose qualified_name
3915 VacuumStmt *n = makeNode(VacuumStmt);
3925 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
3927 VacuumStmt *n = (VacuumStmt *) $5;
3937 analyze_keyword opt_verbose
3939 VacuumStmt *n = makeNode(VacuumStmt);
3949 | analyze_keyword opt_verbose qualified_name opt_name_list
3951 VacuumStmt *n = makeNode(VacuumStmt);
3965 | ANALYSE /* British */ {}
3969 VERBOSE { $$ = TRUE; }
3970 | /*EMPTY*/ { $$ = FALSE; }
3973 opt_full: FULL { $$ = TRUE; }
3974 | /*EMPTY*/ { $$ = FALSE; }
3977 opt_freeze: FREEZE { $$ = TRUE; }
3978 | /*EMPTY*/ { $$ = FALSE; }
3982 '(' name_list ')' { $$ = $2; }
3983 | /*EMPTY*/ { $$ = NIL; }
3987 /*****************************************************************************
3990 * EXPLAIN [ANALYZE] [VERBOSE] query
3992 *****************************************************************************/
3994 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
3996 ExplainStmt *n = makeNode(ExplainStmt);
3999 n->query = (Query*)$4;
4010 | ExecuteStmt /* by default all are $$=$1 */
4014 analyze_keyword { $$ = TRUE; }
4015 | /* EMPTY */ { $$ = FALSE; }
4018 /*****************************************************************************
4021 * PREPARE <plan_name> [(args, ...)] AS <query>
4023 *****************************************************************************/
4025 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
4027 PrepareStmt *n = makeNode(PrepareStmt);
4030 n->query = (Query *) $5;
4035 prep_type_clause: '(' prep_type_list ')' { $$ = $2; }
4036 | /* EMPTY */ { $$ = NIL; }
4039 prep_type_list: Typename { $$ = makeList1($1); }
4040 | prep_type_list ',' Typename
4041 { $$ = lappend($1, $3); }
4048 | DeleteStmt /* by default all are $$=$1 */
4051 /*****************************************************************************
4054 * EXECUTE <plan_name> [(params, ...)] [INTO ...]
4056 *****************************************************************************/
4058 ExecuteStmt: EXECUTE name execute_param_clause into_clause
4060 ExecuteStmt *n = makeNode(ExecuteStmt);
4068 execute_param_clause: '(' expr_list ')' { $$ = $2; }
4069 | /* EMPTY */ { $$ = NIL; }
4072 /*****************************************************************************
4075 * DEALLOCATE [PREPARE] <plan_name>
4077 *****************************************************************************/
4079 DeallocateStmt: DEALLOCATE name
4081 DeallocateStmt *n = makeNode(DeallocateStmt);
4085 | DEALLOCATE PREPARE name
4087 DeallocateStmt *n = makeNode(DeallocateStmt);
4093 /*****************************************************************************
4098 *****************************************************************************/
4101 INSERT INTO qualified_name insert_rest
4109 VALUES '(' insert_target_list ')'
4111 $$ = makeNode(InsertStmt);
4113 $$->targetList = $3;
4114 $$->selectStmt = NULL;
4118 $$ = makeNode(InsertStmt);
4120 $$->targetList = NIL;
4121 $$->selectStmt = NULL;
4125 $$ = makeNode(InsertStmt);
4127 $$->targetList = NIL;
4128 $$->selectStmt = $1;
4130 | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
4132 $$ = makeNode(InsertStmt);
4134 $$->targetList = $6;
4135 $$->selectStmt = NULL;
4137 | '(' insert_column_list ')' SelectStmt
4139 $$ = makeNode(InsertStmt);
4141 $$->targetList = NIL;
4142 $$->selectStmt = $4;
4147 insert_column_item { $$ = makeList1($1); }
4148 | insert_column_list ',' insert_column_item
4149 { $$ = lappend($1, $3); }
4153 ColId opt_indirection
4155 ResTarget *n = makeNode(ResTarget);
4157 n->indirection = $2;
4164 /*****************************************************************************
4169 *****************************************************************************/
4171 DeleteStmt: DELETE_P FROM relation_expr where_clause
4173 DeleteStmt *n = makeNode(DeleteStmt);
4175 n->whereClause = $4;
4180 LockStmt: LOCK_P opt_table qualified_name_list opt_lock
4182 LockStmt *n = makeNode(LockStmt);
4190 opt_lock: IN_P lock_type MODE { $$ = $2; }
4191 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
4194 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
4195 | ROW SHARE { $$ = RowShareLock; }
4196 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
4197 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
4198 | SHARE { $$ = ShareLock; }
4199 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
4200 | EXCLUSIVE { $$ = ExclusiveLock; }
4201 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
4205 /*****************************************************************************
4208 * UpdateStmt (UPDATE)
4210 *****************************************************************************/
4212 UpdateStmt: UPDATE relation_expr
4213 SET update_target_list
4217 UpdateStmt *n = makeNode(UpdateStmt);
4221 n->whereClause = $6;
4227 /*****************************************************************************
4232 *****************************************************************************/
4233 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
4235 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
4240 n->options |= CURSOR_OPT_HOLD;
4245 cursor_options: /*EMPTY*/ { $$ = 0; }
4246 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
4247 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
4248 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
4249 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
4252 opt_hold: /* EMPTY */ { $$ = FALSE; }
4253 | WITH HOLD { $$ = TRUE; }
4254 | WITHOUT HOLD { $$ = FALSE; }
4257 /*****************************************************************************
4262 *****************************************************************************/
4264 /* A complete SELECT statement looks like this.
4266 * The rule returns either a single SelectStmt node or a tree of them,
4267 * representing a set-operation tree.
4269 * There is an ambiguity when a sub-SELECT is within an a_expr and there
4270 * are excess parentheses: do the parentheses belong to the sub-SELECT or
4271 * to the surrounding a_expr? We don't really care, but yacc wants to know.
4272 * To resolve the ambiguity, we are careful to define the grammar so that
4273 * the decision is staved off as long as possible: as long as we can keep
4274 * absorbing parentheses into the sub-SELECT, we will do so, and only when
4275 * it's no longer possible to do that will we decide that parens belong to
4276 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
4277 * parentheses are treated as part of the sub-select. The necessity of doing
4278 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
4279 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
4280 * SELECT viewpoint when we see the UNION.
4282 * This approach is implemented by defining a nonterminal select_with_parens,
4283 * which represents a SELECT with at least one outer layer of parentheses,
4284 * and being careful to use select_with_parens, never '(' SelectStmt ')',
4285 * in the expression grammar. We will then have shift-reduce conflicts
4286 * which we can resolve in favor of always treating '(' <select> ')' as
4287 * a select_with_parens. To resolve the conflicts, the productions that
4288 * conflict with the select_with_parens productions are manually given
4289 * precedences lower than the precedence of ')', thereby ensuring that we
4290 * shift ')' (and then reduce to select_with_parens) rather than trying to
4291 * reduce the inner <select> nonterminal to something else. We use UMINUS
4292 * precedence for this, which is a fairly arbitrary choice.
4294 * To be able to define select_with_parens itself without ambiguity, we need
4295 * a nonterminal select_no_parens that represents a SELECT structure with no
4296 * outermost parentheses. This is a little bit tedious, but it works.
4298 * In non-expression contexts, we use SelectStmt which can represent a SELECT
4299 * with or without outer parentheses.
4302 SelectStmt: select_no_parens %prec UMINUS
4303 | select_with_parens %prec UMINUS
4307 '(' select_no_parens ')' { $$ = $2; }
4308 | '(' select_with_parens ')' { $$ = $2; }
4312 * FOR UPDATE may be before or after LIMIT/OFFSET.
4313 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
4314 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE
4318 simple_select { $$ = $1; }
4319 | select_clause sort_clause
4321 insertSelectOptions((SelectStmt *) $1, $2, NIL,
4325 | select_clause opt_sort_clause for_update_clause opt_select_limit
4327 insertSelectOptions((SelectStmt *) $1, $2, $3,
4328 nth(0, $4), nth(1, $4));
4331 | select_clause opt_sort_clause select_limit opt_for_update_clause
4333 insertSelectOptions((SelectStmt *) $1, $2, $4,
4334 nth(0, $3), nth(1, $3));
4340 simple_select { $$ = $1; }
4341 | select_with_parens { $$ = $1; }
4345 * This rule parses SELECT statements that can appear within set operations,
4346 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
4347 * the ordering of the set operations. Without '(' and ')' we want the
4348 * operations to be ordered per the precedence specs at the head of this file.
4350 * As with select_no_parens, simple_select cannot have outer parentheses,
4351 * but can have parenthesized subclauses.
4353 * Note that sort clauses cannot be included at this level --- SQL92 requires
4354 * SELECT foo UNION SELECT bar ORDER BY baz
4356 * (SELECT foo UNION SELECT bar) ORDER BY baz
4358 * SELECT foo UNION (SELECT bar ORDER BY baz)
4359 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
4360 * as part of the select_no_parens production, not simple_select.
4361 * This does not limit functionality, because you can reintroduce sort and
4362 * limit clauses inside parentheses.
4364 * NOTE: only the leftmost component SelectStmt should have INTO.
4365 * However, this is not checked by the grammar; parse analysis must check it.
4368 SELECT opt_distinct target_list
4369 into_clause from_clause where_clause
4370 group_clause having_clause
4372 SelectStmt *n = makeNode(SelectStmt);
4373 n->distinctClause = $2;
4376 n->intoColNames = NIL;
4378 n->whereClause = $6;
4379 n->groupClause = $7;
4380 n->havingClause = $8;
4383 | select_clause UNION opt_all select_clause
4385 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
4387 | select_clause INTERSECT opt_all select_clause
4389 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
4391 | select_clause EXCEPT opt_all select_clause
4393 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
4398 INTO OptTempTableName { $$ = $2; }
4399 | /*EMPTY*/ { $$ = NULL; }
4403 * Redundancy here is needed to avoid shift/reduce conflicts,
4404 * since TEMP is not a reserved word. See also OptTemp.
4407 TEMPORARY opt_table qualified_name
4412 | TEMP opt_table qualified_name
4417 | LOCAL TEMPORARY opt_table qualified_name
4422 | LOCAL TEMP opt_table qualified_name
4427 | GLOBAL TEMPORARY opt_table qualified_name
4432 | GLOBAL TEMP opt_table qualified_name
4437 | TABLE qualified_name
4453 opt_all: ALL { $$ = TRUE; }
4454 | DISTINCT { $$ = FALSE; }
4455 | /*EMPTY*/ { $$ = FALSE; }
4458 /* We use (NIL) as a placeholder to indicate that all target expressions
4459 * should be placed in the DISTINCT list during parsetree analysis.
4462 DISTINCT { $$ = makeList1(NIL); }
4463 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
4465 | /*EMPTY*/ { $$ = NIL; }
4469 sort_clause { $$ = $1;}
4470 | /*EMPTY*/ { $$ = NIL; }
4474 ORDER BY sortby_list { $$ = $3; }
4478 sortby { $$ = makeList1($1); }
4479 | sortby_list ',' sortby { $$ = lappend($1, $3); }
4482 sortby: a_expr OptUseOp
4484 $$ = makeNode(SortGroupBy);
4490 OptUseOp: USING qual_all_Op { $$ = $2; }
4492 { $$ = makeList1(makeString("<")); }
4494 { $$ = makeList1(makeString(">")); }
4496 { $$ = makeList1(makeString("<")); /*default*/ }
4501 LIMIT select_limit_value OFFSET select_offset_value
4502 { $$ = makeList2($4, $2); }
4503 | OFFSET select_offset_value LIMIT select_limit_value
4504 { $$ = makeList2($2, $4); }
4505 | LIMIT select_limit_value
4506 { $$ = makeList2(NULL, $2); }
4507 | OFFSET select_offset_value
4508 { $$ = makeList2($2, NULL); }
4509 | LIMIT select_limit_value ',' select_offset_value
4510 /* Disabled because it was too confusing, bjm 2002-02-18 */
4512 "LIMIT #,# syntax not supported.\n\tUse separate LIMIT and OFFSET clauses."); }
4517 select_limit { $$ = $1; }
4519 { $$ = makeList2(NULL,NULL); }
4525 Const *n = makeNode(Const);
4528 elog(ERROR, "LIMIT must not be negative");
4530 n->consttype = INT4OID;
4531 n->constlen = sizeof(int4);
4532 n->constvalue = Int32GetDatum($1);
4533 n->constisnull = FALSE;
4534 n->constbyval = TRUE;
4539 /* LIMIT ALL is represented as a NULL constant */
4540 Const *n = makeNode(Const);
4542 n->consttype = INT4OID;
4543 n->constlen = sizeof(int4);
4544 n->constvalue = (Datum) 0;
4545 n->constisnull = TRUE;
4546 n->constbyval = TRUE;
4551 Param *n = makeNode(Param);
4553 n->paramkind = PARAM_NUM;
4555 n->paramtype = INT4OID;
4560 select_offset_value:
4563 Const *n = makeNode(Const);
4566 elog(ERROR, "OFFSET must not be negative");
4568 n->consttype = INT4OID;
4569 n->constlen = sizeof(int4);
4570 n->constvalue = Int32GetDatum($1);
4571 n->constisnull = FALSE;
4572 n->constbyval = TRUE;
4577 Param *n = makeNode(Param);
4579 n->paramkind = PARAM_NUM;
4581 n->paramtype = INT4OID;
4587 * jimmy bell-style recursive queries aren't supported in the
4590 * ...however, recursive addattr and rename supported. make special
4595 GROUP_P BY expr_list { $$ = $3; }
4596 | /*EMPTY*/ { $$ = NIL; }
4600 HAVING a_expr { $$ = $2; }
4601 | /*EMPTY*/ { $$ = NULL; }
4605 FOR UPDATE update_list { $$ = $3; }
4606 | FOR READ ONLY { $$ = NULL; }
4609 opt_for_update_clause:
4610 for_update_clause { $$ = $1; }
4611 | /* EMPTY */ { $$ = NULL; }
4615 OF name_list { $$ = $2; }
4616 | /* EMPTY */ { $$ = makeList1(NULL); }
4619 /*****************************************************************************
4621 * clauses common to all Optimizable Stmts:
4622 * from_clause - allow list of both JOIN expressions and table names
4623 * where_clause - qualifications for joins or restrictions
4625 *****************************************************************************/
4628 FROM from_list { $$ = $2; }
4629 | /*EMPTY*/ { $$ = NIL; }
4633 table_ref { $$ = makeList1($1); }
4634 | from_list ',' table_ref { $$ = lappend($1, $3); }
4638 * table_ref is where an alias clause can be attached. Note we cannot make
4639 * alias_clause have an empty production because that causes parse conflicts
4640 * between table_ref := '(' joined_table ')' alias_clause
4641 * and joined_table := '(' joined_table ')'. So, we must have the
4642 * redundant-looking productions here instead.
4644 table_ref: relation_expr
4648 | relation_expr alias_clause
4655 RangeFunction *n = makeNode(RangeFunction);
4656 n->funccallnode = $1;
4657 n->coldeflist = NIL;
4660 | func_table alias_clause
4662 RangeFunction *n = makeNode(RangeFunction);
4663 n->funccallnode = $1;
4665 n->coldeflist = NIL;
4668 | func_table AS '(' TableFuncElementList ')'
4670 RangeFunction *n = makeNode(RangeFunction);
4671 n->funccallnode = $1;
4675 | func_table AS ColId '(' TableFuncElementList ')'
4677 RangeFunction *n = makeNode(RangeFunction);
4678 Alias *a = makeNode(Alias);
4679 n->funccallnode = $1;
4685 | func_table ColId '(' TableFuncElementList ')'
4687 RangeFunction *n = makeNode(RangeFunction);
4688 Alias *a = makeNode(Alias);
4689 n->funccallnode = $1;
4695 | select_with_parens
4698 * The SQL spec does not permit a subselect
4699 * (<derived_table>) without an alias clause,
4700 * so we don't either. This avoids the problem
4701 * of needing to invent a unique refname for it.
4702 * That could be surmounted if there's sufficient
4703 * popular demand, but for now let's just implement
4704 * the spec and see if anyone complains.
4705 * However, it does seem like a good idea to emit
4706 * an error message that's better than "parse error".
4708 elog(ERROR, "sub-SELECT in FROM must have an alias"
4709 "\n\tFor example, FROM (SELECT ...) [AS] foo");
4712 | select_with_parens alias_clause
4714 RangeSubselect *n = makeNode(RangeSubselect);
4723 | '(' joined_table ')' alias_clause
4732 * It may seem silly to separate joined_table from table_ref, but there is
4733 * method in SQL92's madness: if you don't do it this way you get reduce-
4734 * reduce conflicts, because it's not clear to the parser generator whether
4735 * to expect alias_clause after ')' or not. For the same reason we must
4736 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
4737 * join_type to expand to empty; if we try it, the parser generator can't
4738 * figure out when to reduce an empty join_type right after table_ref.
4740 * Note that a CROSS JOIN is the same as an unqualified
4741 * INNER JOIN, and an INNER JOIN/ON has the same shape
4742 * but a qualification expression to limit membership.
4743 * A NATURAL JOIN implicitly matches column names between
4744 * tables and the shape is determined by which columns are
4745 * in common. We'll collect columns during the later transformations.
4749 '(' joined_table ')'
4753 | table_ref CROSS JOIN table_ref
4755 /* CROSS JOIN is same as unqualified inner join */
4756 JoinExpr *n = makeNode(JoinExpr);
4757 n->jointype = JOIN_INNER;
4758 n->isNatural = FALSE;
4765 | table_ref UNIONJOIN table_ref
4767 /* UNION JOIN is made into 1 token to avoid shift/reduce
4768 * conflict against regular UNION keyword.
4770 JoinExpr *n = makeNode(JoinExpr);
4771 n->jointype = JOIN_UNION;
4772 n->isNatural = FALSE;
4779 | table_ref join_type JOIN table_ref join_qual
4781 JoinExpr *n = makeNode(JoinExpr);
4783 n->isNatural = FALSE;
4786 if ($5 != NULL && IsA($5, List))
4787 n->using = (List *) $5; /* USING clause */
4789 n->quals = $5; /* ON clause */
4792 | table_ref JOIN table_ref join_qual
4794 /* letting join_type reduce to empty doesn't work */
4795 JoinExpr *n = makeNode(JoinExpr);
4796 n->jointype = JOIN_INNER;
4797 n->isNatural = FALSE;
4800 if ($4 != NULL && IsA($4, List))
4801 n->using = (List *) $4; /* USING clause */
4803 n->quals = $4; /* ON clause */
4806 | table_ref NATURAL join_type JOIN table_ref
4808 JoinExpr *n = makeNode(JoinExpr);
4810 n->isNatural = TRUE;
4813 n->using = NIL; /* figure out which columns later... */
4814 n->quals = NULL; /* fill later */
4817 | table_ref NATURAL JOIN table_ref
4819 /* letting join_type reduce to empty doesn't work */
4820 JoinExpr *n = makeNode(JoinExpr);
4821 n->jointype = JOIN_INNER;
4822 n->isNatural = TRUE;
4825 n->using = NIL; /* figure out which columns later... */
4826 n->quals = NULL; /* fill later */
4832 AS ColId '(' name_list ')'
4834 $$ = makeNode(Alias);
4840 $$ = makeNode(Alias);
4843 | ColId '(' name_list ')'
4845 $$ = makeNode(Alias);
4851 $$ = makeNode(Alias);
4856 join_type: FULL join_outer { $$ = JOIN_FULL; }
4857 | LEFT join_outer { $$ = JOIN_LEFT; }
4858 | RIGHT join_outer { $$ = JOIN_RIGHT; }
4859 | INNER_P { $$ = JOIN_INNER; }
4862 /* OUTER is just noise... */
4863 join_outer: OUTER_P { $$ = NULL; }
4864 | /*EMPTY*/ { $$ = NULL; }
4867 /* JOIN qualification clauses
4868 * Possibilities are:
4869 * USING ( column list ) allows only unqualified column names,
4870 * which must match between tables.
4871 * ON expr allows more general qualifications.
4873 * We return USING as a List node, while an ON-expr will not be a List.
4876 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
4877 | ON a_expr { $$ = $2; }
4884 /* default inheritance */
4886 $$->inhOpt = INH_DEFAULT;
4889 | qualified_name '*'
4891 /* inheritance query */
4893 $$->inhOpt = INH_YES;
4896 | ONLY qualified_name
4898 /* no inheritance */
4900 $$->inhOpt = INH_NO;
4903 | ONLY '(' qualified_name ')'
4905 /* no inheritance, SQL99-style syntax */
4907 $$->inhOpt = INH_NO;
4913 func_table: func_name '(' ')'
4915 FuncCall *n = makeNode(FuncCall);
4918 n->agg_star = FALSE;
4919 n->agg_distinct = FALSE;
4922 | func_name '(' expr_list ')'
4924 FuncCall *n = makeNode(FuncCall);
4927 n->agg_star = FALSE;
4928 n->agg_distinct = FALSE;
4935 WHERE a_expr { $$ = $2; }
4936 | /*EMPTY*/ { $$ = NULL; }
4940 TableFuncElementList:
4945 | TableFuncElementList ',' TableFuncElement
4947 $$ = lappend($1, $3);
4951 TableFuncElement: ColId Typename
4953 ColumnDef *n = makeNode(ColumnDef);
4956 n->constraints = NIL;
4962 /*****************************************************************************
4965 * SQL92 introduces a large amount of type-specific syntax.
4966 * Define individual clauses to handle these cases, and use
4967 * the generic case to handle regular type-extensible Postgres syntax.
4968 * - thomas 1997-10-10
4970 *****************************************************************************/
4972 Typename: SimpleTypename opt_array_bounds
4975 $$->arrayBounds = $2;
4977 | SETOF SimpleTypename opt_array_bounds
4980 $$->arrayBounds = $3;
4983 | SimpleTypename ARRAY '[' Iconst ']'
4985 /* SQL99's redundant syntax */
4987 $$->arrayBounds = makeList1(makeInteger($4));
4989 | SETOF SimpleTypename ARRAY '[' Iconst ']'
4991 /* SQL99's redundant syntax */
4993 $$->arrayBounds = makeList1(makeInteger($5));
4999 opt_array_bounds '[' ']'
5000 { $$ = lappend($1, makeInteger(-1)); }
5001 | opt_array_bounds '[' Iconst ']'
5002 { $$ = lappend($1, makeInteger($3)); }
5008 * XXX ideally, the production for a qualified typename should be ColId attrs
5009 * (there's no obvious reason why the first name should need to be restricted)
5010 * and should be an alternative of GenericType (so that it can be used to
5011 * specify a type for a literal in AExprConst). However doing either causes
5012 * reduce/reduce conflicts that I haven't been able to find a workaround
5016 GenericType { $$ = $1; }
5017 | Numeric { $$ = $1; }
5019 | Character { $$ = $1; }
5020 | ConstDatetime { $$ = $1; }
5021 | ConstInterval opt_interval
5024 if ($2 != INTERVAL_FULL_RANGE)
5025 $$->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $2);
5027 | ConstInterval '(' Iconst ')' opt_interval
5032 "INTERVAL(%d) precision must not be negative",
5034 if ($3 > MAX_INTERVAL_PRECISION)
5037 "INTERVAL(%d) precision reduced to maximum allowed, %d",
5038 $3, MAX_INTERVAL_PRECISION);
5039 $3 = MAX_INTERVAL_PRECISION;
5041 $$->typmod = INTERVAL_TYPMOD($3, $5);
5045 $$ = makeNode(TypeName);
5046 $$->names = lcons(makeString($1), $2);
5051 /* We have a separate ConstTypename to allow defaulting fixed-length
5052 * types such as CHAR() and BIT() to an unspecified length.
5053 * SQL9x requires that these default to a length of one, but this
5054 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
5055 * where there is an obvious better choice to make.
5056 * Note that ConstInterval is not included here since it must
5057 * be pushed up higher in the rules to accomodate the postfix
5058 * options (e.g. INTERVAL '1' YEAR).
5061 GenericType { $$ = $1; }
5062 | Numeric { $$ = $1; }
5063 | ConstBit { $$ = $1; }
5064 | ConstCharacter { $$ = $1; }
5065 | ConstDatetime { $$ = $1; }
5071 $$ = makeTypeName($1);
5075 /* SQL92 numeric data types
5076 * Check FLOAT() precision limits assuming IEEE floating types.
5077 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
5078 * - thomas 1997-09-18
5082 $$ = SystemTypeName("int4");
5086 $$ = SystemTypeName("int4");
5090 $$ = SystemTypeName("int2");
5094 $$ = SystemTypeName("int8");
5098 $$ = SystemTypeName("float4");
5106 $$ = SystemTypeName("float8");
5108 | DECIMAL opt_decimal
5110 $$ = SystemTypeName("numeric");
5115 $$ = SystemTypeName("numeric");
5118 | NUMERIC opt_numeric
5120 $$ = SystemTypeName("numeric");
5125 $$ = SystemTypeName("bool");
5129 opt_float: '(' Iconst ')'
5133 "precision for FLOAT must be at least 1");
5135 $$ = SystemTypeName("float4");
5137 $$ = SystemTypeName("float8");
5140 "precision for FLOAT must be less than 16");
5144 $$ = SystemTypeName("float8");
5149 '(' Iconst ',' Iconst ')'
5151 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5153 "NUMERIC precision %d must be between 1 and %d",
5154 $2, NUMERIC_MAX_PRECISION);
5155 if ($4 < 0 || $4 > $2)
5157 "NUMERIC scale %d must be between 0 and precision %d",
5160 $$ = (($2 << 16) | $4) + VARHDRSZ;
5164 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5166 "NUMERIC precision %d must be between 1 and %d",
5167 $2, NUMERIC_MAX_PRECISION);
5169 $$ = ($2 << 16) + VARHDRSZ;
5173 /* Insert "-1" meaning "no limit" */
5179 '(' Iconst ',' Iconst ')'
5181 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5183 "DECIMAL precision %d must be between 1 and %d",
5184 $2, NUMERIC_MAX_PRECISION);
5185 if ($4 < 0 || $4 > $2)
5187 "DECIMAL scale %d must be between 0 and precision %d",
5190 $$ = (($2 << 16) | $4) + VARHDRSZ;
5194 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5196 "DECIMAL precision %d must be between 1 and %d",
5197 $2, NUMERIC_MAX_PRECISION);
5199 $$ = ($2 << 16) + VARHDRSZ;
5203 /* Insert "-1" meaning "no limit" */
5210 * SQL92 bit-field data types
5211 * The following implements BIT() and BIT VARYING().
5223 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
5224 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
5225 ConstBit: BitWithLength
5237 BIT opt_varying '(' Iconst ')'
5241 typname = $2 ? "varbit" : "bit";
5242 $$ = SystemTypeName(typname);
5244 elog(ERROR, "length for type '%s' must be at least 1",
5246 else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
5247 elog(ERROR, "length for type '%s' cannot exceed %d",
5248 typname, (MaxAttrSize * BITS_PER_BYTE));
5256 /* bit defaults to bit(1), varbit to no limit */
5259 $$ = SystemTypeName("varbit");
5264 $$ = SystemTypeName("bit");
5272 * SQL92 character data types
5273 * The following implements CHAR() and VARCHAR().
5275 Character: CharacterWithLength
5279 | CharacterWithoutLength
5285 ConstCharacter: CharacterWithLength
5289 | CharacterWithoutLength
5291 /* Length was not specified so allow to be unrestricted.
5292 * This handles problems with fixed-length (bpchar) strings
5293 * which in column definitions must default to a length
5294 * of one, but should not be constrained if the length
5295 * was not specified.
5302 CharacterWithLength: character '(' Iconst ')' opt_charset
5304 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
5308 type = palloc(strlen($1) + 1 + strlen($5) + 1);
5315 $$ = SystemTypeName($1);
5318 elog(ERROR, "length for type '%s' must be at least 1",
5320 else if ($3 > MaxAttrSize)
5321 elog(ERROR, "length for type '%s' cannot exceed %d",
5324 /* we actually implement these like a varlen, so
5325 * the first 4 bytes is the length. (the difference
5326 * between these and "text" is that we blank-pad and
5327 * truncate where necessary)
5329 $$->typmod = VARHDRSZ + $3;
5333 CharacterWithoutLength: character opt_charset
5335 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
5339 type = palloc(strlen($1) + 1 + strlen($2) + 1);
5346 $$ = SystemTypeName($1);
5348 /* char defaults to char(1), varchar to no limit */
5349 if (strcmp($1, "bpchar") == 0)
5350 $$->typmod = VARHDRSZ + 1;
5356 character: CHARACTER opt_varying
5357 { $$ = $2 ? "varchar": "bpchar"; }
5358 | CHAR_P opt_varying
5359 { $$ = $2 ? "varchar": "bpchar"; }
5362 | NATIONAL CHARACTER opt_varying
5363 { $$ = $3 ? "varchar": "bpchar"; }
5364 | NATIONAL CHAR_P opt_varying
5365 { $$ = $3 ? "varchar": "bpchar"; }
5367 { $$ = $2 ? "varchar": "bpchar"; }
5371 VARYING { $$ = TRUE; }
5372 | /*EMPTY*/ { $$ = FALSE; }
5376 CHARACTER SET ColId { $$ = $3; }
5377 | /*EMPTY*/ { $$ = NULL; }
5381 COLLATE ColId { $$ = $2; }
5382 | /*EMPTY*/ { $$ = NULL; }
5386 TIMESTAMP '(' Iconst ')' opt_timezone
5389 $$ = SystemTypeName("timestamptz");
5391 $$ = SystemTypeName("timestamp");
5392 /* XXX the timezone field seems to be unused
5393 * - thomas 2001-09-06
5398 "TIMESTAMP(%d)%s precision must not be negative",
5399 $3, ($5 ? " WITH TIME ZONE": ""));
5400 if ($3 > MAX_TIMESTAMP_PRECISION)
5403 "TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
5404 $3, ($5 ? " WITH TIME ZONE": ""),
5405 MAX_TIMESTAMP_PRECISION);
5406 $3 = MAX_TIMESTAMP_PRECISION;
5410 | TIMESTAMP opt_timezone
5413 $$ = SystemTypeName("timestamptz");
5415 $$ = SystemTypeName("timestamp");
5416 /* XXX the timezone field seems to be unused
5417 * - thomas 2001-09-06
5420 /* SQL99 specified a default precision of six
5421 * for schema definitions. But for timestamp
5422 * literals we don't want to throw away precision
5423 * so leave this as unspecified for now.
5424 * Later, we may want a different production
5425 * for schemas. - thomas 2001-12-07
5429 | TIME '(' Iconst ')' opt_timezone
5432 $$ = SystemTypeName("timetz");
5434 $$ = SystemTypeName("time");
5437 "TIME(%d)%s precision must not be negative",
5438 $3, ($5 ? " WITH TIME ZONE": ""));
5439 if ($3 > MAX_TIME_PRECISION)
5442 "TIME(%d)%s precision reduced to maximum allowed, %d",
5443 $3, ($5 ? " WITH TIME ZONE": ""),
5444 MAX_TIME_PRECISION);
5445 $3 = MAX_TIME_PRECISION;
5452 $$ = SystemTypeName("timetz");
5454 $$ = SystemTypeName("time");
5455 /* SQL99 specified a default precision of zero.
5456 * See comments for timestamp above on why we will
5457 * leave this unspecified for now. - thomas 2001-12-07
5464 INTERVAL { $$ = SystemTypeName("interval"); }
5468 WITH TIME ZONE { $$ = TRUE; }
5469 | WITHOUT TIME ZONE { $$ = FALSE; }
5470 | /*EMPTY*/ { $$ = FALSE; }
5474 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
5475 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
5476 | DAY_P { $$ = INTERVAL_MASK(DAY); }
5477 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
5478 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
5479 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
5481 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
5483 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
5485 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
5486 | INTERVAL_MASK(MINUTE); }
5488 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
5489 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
5490 | HOUR_P TO MINUTE_P
5491 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
5492 | HOUR_P TO SECOND_P
5493 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
5494 | INTERVAL_MASK(SECOND); }
5495 | MINUTE_P TO SECOND_P
5496 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
5497 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
5501 /*****************************************************************************
5503 * expression grammar
5505 *****************************************************************************/
5507 /* Expressions using row descriptors
5508 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
5509 * with singleton expressions. Use SQL99's ROW keyword to allow rows of
5512 r_expr: row IN_P select_with_parens
5514 SubLink *n = makeNode(SubLink);
5515 n->subLinkType = ANY_SUBLINK;
5517 n->operName = makeList1(makeString("="));
5521 | row NOT IN_P select_with_parens
5523 /* Make an IN node */
5524 SubLink *n = makeNode(SubLink);
5525 n->subLinkType = ANY_SUBLINK;
5527 n->operName = makeList1(makeString("="));
5529 /* Stick a NOT on top */
5530 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n);
5532 | row qual_all_Op sub_type select_with_parens
5535 SubLink *n = makeNode(SubLink);
5536 n->subLinkType = $3;
5542 | row qual_all_Op select_with_parens
5545 SubLink *n = makeNode(SubLink);
5546 n->subLinkType = MULTIEXPR_SUBLINK;
5552 | row qual_all_Op row
5555 $$ = makeRowExpr($2, $1, $3);
5559 $$ = makeRowNullTest(IS_NULL, $1);
5563 $$ = makeRowNullTest(IS_NOT_NULL, $1);
5567 $$ = (Node *)makeOverlaps($1, $3);
5569 | row IS DISTINCT FROM row
5572 /* IS DISTINCT FROM has the following rules for non-array types:
5573 * a) the row lengths must be equal
5574 * b) if both rows are zero-length, then they are not distinct
5575 * c) if any element is distinct, the rows are distinct
5576 * The rules for an element being distinct:
5577 * a) if the elements are both NULL, then they are not distinct
5578 * b) if the elements compare to be equal, then they are not distinct
5579 * c) otherwise, they are distinct
5583 /* lengths don't match? then complain */
5584 if (length(largs) != length(rargs))
5586 elog(ERROR, "Unequal number of entries in row expression");
5588 /* both are zero-length rows? then they are not distinct */
5589 else if (length(largs) <= 0)
5591 $$ = (Node *)makeBoolConst(FALSE);
5593 /* otherwise, we need to compare each element */
5596 $$ = (Node *)makeDistinctExpr(largs, rargs);
5601 /* Explicit row production.
5602 * SQL99 allows an optional ROW keyword, so we can now do single-element productions
5603 * without conflicting with the parenthesized a_expr production.
5605 row: ROW '(' row_descriptor ')' { $$ = $3; }
5606 | ROW '(' a_expr ')' { $$ = makeList1($3); }
5607 | ROW '(' ')' { $$ = NULL; }
5608 | '(' row_descriptor ')' { $$ = $2; }
5611 row_descriptor: expr_list ',' a_expr { $$ = lappend($1, $3); }
5614 sub_type: ANY { $$ = ANY_SUBLINK; }
5615 | SOME { $$ = ANY_SUBLINK; }
5616 | ALL { $$ = ALL_SUBLINK; }
5619 all_Op: Op { $$ = $1; }
5620 | MathOp { $$ = $1; }
5623 MathOp: '+' { $$ = "+"; }
5635 { $$ = makeList1(makeString($1)); }
5636 | OPERATOR '(' any_operator ')' { $$ = $3; }
5641 { $$ = makeList1(makeString($1)); }
5642 | OPERATOR '(' any_operator ')' { $$ = $3; }
5646 * General expressions
5647 * This is the heart of the expression syntax.
5649 * We have two expression types: a_expr is the unrestricted kind, and
5650 * b_expr is a subset that must be used in some places to avoid shift/reduce
5651 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
5652 * because that use of AND conflicts with AND as a boolean operator. So,
5653 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
5655 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
5656 * always be used by surrounding it with parens.
5658 * c_expr is all the productions that are common to a_expr and b_expr;
5659 * it's factored out just to eliminate redundant coding.
5661 a_expr: c_expr { $$ = $1; }
5662 | a_expr TYPECAST Typename
5663 { $$ = makeTypeCast($1, $3); }
5664 | a_expr COLLATE ColId
5666 FuncCall *n = makeNode(FuncCall);
5667 n->funcname = SystemFuncName($3);
5668 n->args = makeList1($1);
5669 n->agg_star = FALSE;
5670 n->agg_distinct = FALSE;
5673 | a_expr AT TIME ZONE c_expr
5675 FuncCall *n = makeNode(FuncCall);
5676 n->funcname = SystemFuncName("timezone");
5677 n->args = makeList2($5, $1);
5678 n->agg_star = FALSE;
5679 n->agg_distinct = FALSE;
5683 * These operators must be called out explicitly in order to make use
5684 * of yacc/bison's automatic operator-precedence handling. All other
5685 * operator names are handled by the generic productions using "Op",
5686 * below; and all those operators will have the same precedence.
5688 * If you add more explicitly-known operators, be sure to add them
5689 * also to b_expr and to the MathOp list above.
5691 | '+' a_expr %prec UMINUS
5692 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
5693 | '-' a_expr %prec UMINUS
5694 { $$ = doNegate($2); }
5696 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", NULL, $2); }
5698 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", NULL, $2); }
5700 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, NULL); }
5702 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, NULL); }
5704 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
5706 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
5708 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
5710 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
5712 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
5714 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
5716 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
5718 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
5720 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
5722 | a_expr qual_Op a_expr %prec Op
5723 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
5724 | qual_Op a_expr %prec Op
5725 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
5726 | a_expr qual_Op %prec POSTFIXOP
5727 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
5730 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3); }
5732 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3); }
5734 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2); }
5736 | a_expr LIKE a_expr
5737 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3); }
5738 | a_expr LIKE a_expr ESCAPE a_expr
5740 FuncCall *n = makeNode(FuncCall);
5741 n->funcname = SystemFuncName("like_escape");
5742 n->args = makeList2($3, $5);
5743 n->agg_star = FALSE;
5744 n->agg_distinct = FALSE;
5745 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n);
5747 | a_expr NOT LIKE a_expr
5748 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4); }
5749 | a_expr NOT LIKE a_expr ESCAPE a_expr
5751 FuncCall *n = makeNode(FuncCall);
5752 n->funcname = SystemFuncName("like_escape");
5753 n->args = makeList2($4, $6);
5754 n->agg_star = FALSE;
5755 n->agg_distinct = FALSE;
5756 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n);
5758 | a_expr ILIKE a_expr
5759 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3); }
5760 | a_expr ILIKE a_expr ESCAPE a_expr
5762 FuncCall *n = makeNode(FuncCall);
5763 n->funcname = SystemFuncName("like_escape");
5764 n->args = makeList2($3, $5);
5765 n->agg_star = FALSE;
5766 n->agg_distinct = FALSE;
5767 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n);
5769 | a_expr NOT ILIKE a_expr
5770 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4); }
5771 | a_expr NOT ILIKE a_expr ESCAPE a_expr
5773 FuncCall *n = makeNode(FuncCall);
5774 n->funcname = SystemFuncName("like_escape");
5775 n->args = makeList2($4, $6);
5776 n->agg_star = FALSE;
5777 n->agg_distinct = FALSE;
5778 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n);
5781 | a_expr SIMILAR TO a_expr %prec SIMILAR
5783 A_Const *c = makeNode(A_Const);
5784 FuncCall *n = makeNode(FuncCall);
5785 c->val.type = T_Null;
5786 n->funcname = SystemFuncName("similar_escape");
5787 n->args = makeList2($4, (Node *) c);
5788 n->agg_star = FALSE;
5789 n->agg_distinct = FALSE;
5790 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
5792 | a_expr SIMILAR TO a_expr ESCAPE a_expr
5794 FuncCall *n = makeNode(FuncCall);
5795 n->funcname = SystemFuncName("similar_escape");
5796 n->args = makeList2($4, $6);
5797 n->agg_star = FALSE;
5798 n->agg_distinct = FALSE;
5799 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
5801 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
5803 A_Const *c = makeNode(A_Const);
5804 FuncCall *n = makeNode(FuncCall);
5805 c->val.type = T_Null;
5806 n->funcname = SystemFuncName("similar_escape");
5807 n->args = makeList2($5, (Node *) c);
5808 n->agg_star = FALSE;
5809 n->agg_distinct = FALSE;
5810 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
5812 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
5814 FuncCall *n = makeNode(FuncCall);
5815 n->funcname = SystemFuncName("similar_escape");
5816 n->args = makeList2($5, $7);
5817 n->agg_star = FALSE;
5818 n->agg_distinct = FALSE;
5819 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
5823 * Define SQL92-style Null test clause.
5824 * Allow two forms described in the standard:
5827 * Allow two SQL extensions
5833 NullTest *n = makeNode(NullTest);
5834 n->arg = (Expr *) $1;
5835 n->nulltesttype = IS_NULL;
5840 NullTest *n = makeNode(NullTest);
5841 n->arg = (Expr *) $1;
5842 n->nulltesttype = IS_NULL;
5847 NullTest *n = makeNode(NullTest);
5848 n->arg = (Expr *) $1;
5849 n->nulltesttype = IS_NOT_NULL;
5852 | a_expr IS NOT NULL_P
5854 NullTest *n = makeNode(NullTest);
5855 n->arg = (Expr *) $1;
5856 n->nulltesttype = IS_NOT_NULL;
5861 BooleanTest *b = makeNode(BooleanTest);
5862 b->arg = (Expr *) $1;
5863 b->booltesttype = IS_TRUE;
5866 | a_expr IS NOT TRUE_P
5868 BooleanTest *b = makeNode(BooleanTest);
5869 b->arg = (Expr *) $1;
5870 b->booltesttype = IS_NOT_TRUE;
5875 BooleanTest *b = makeNode(BooleanTest);
5876 b->arg = (Expr *) $1;
5877 b->booltesttype = IS_FALSE;
5880 | a_expr IS NOT FALSE_P
5882 BooleanTest *b = makeNode(BooleanTest);
5883 b->arg = (Expr *) $1;
5884 b->booltesttype = IS_NOT_FALSE;
5889 BooleanTest *b = makeNode(BooleanTest);
5890 b->arg = (Expr *) $1;
5891 b->booltesttype = IS_UNKNOWN;
5894 | a_expr IS NOT UNKNOWN
5896 BooleanTest *b = makeNode(BooleanTest);
5897 b->arg = (Expr *) $1;
5898 b->booltesttype = IS_NOT_UNKNOWN;
5901 | a_expr IS DISTINCT FROM a_expr %prec IS
5902 { $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5); }
5903 | a_expr IS OF '(' type_list ')' %prec IS
5905 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
5907 | a_expr IS NOT OF '(' type_list ')' %prec IS
5909 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
5911 | a_expr BETWEEN b_expr AND b_expr %prec BETWEEN
5913 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
5914 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3),
5915 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $5));
5917 | a_expr NOT BETWEEN b_expr AND b_expr %prec BETWEEN
5919 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
5920 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $4),
5921 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $6));
5923 | a_expr IN_P in_expr
5925 /* in_expr returns a SubLink or a list of a_exprs */
5926 if (IsA($3, SubLink))
5928 SubLink *n = (SubLink *)$3;
5929 n->subLinkType = ANY_SUBLINK;
5930 n->lefthand = makeList1($1);
5931 n->operName = makeList1(makeString("="));
5938 foreach(l, (List *) $3)
5941 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, lfirst(l));
5945 n = (Node *) makeA_Expr(AEXPR_OR, NIL, n, cmp);
5950 | a_expr NOT IN_P in_expr
5952 /* in_expr returns a SubLink or a list of a_exprs */
5953 if (IsA($4, SubLink))
5955 /* Make an IN node */
5956 SubLink *n = (SubLink *)$4;
5957 n->subLinkType = ANY_SUBLINK;
5958 n->lefthand = makeList1($1);
5959 n->operName = makeList1(makeString("="));
5960 /* Stick a NOT on top */
5961 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n);
5967 foreach(l, (List *) $4)
5970 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, lfirst(l));
5974 n = (Node *) makeA_Expr(AEXPR_AND, NIL, n, cmp);
5979 | a_expr qual_all_Op sub_type select_with_parens %prec Op
5981 SubLink *n = makeNode(SubLink);
5982 n->subLinkType = $3;
5983 n->lefthand = makeList1($1);
5988 | UNIQUE select_with_parens %prec Op
5990 /* Not sure how to get rid of the parentheses
5991 * but there are lots of shift/reduce errors without them.
5993 * Should be able to implement this by plopping the entire
5994 * select into a node, then transforming the target expressions
5995 * from whatever they are into count(*), and testing the
5996 * entire result equal to one.
5997 * But, will probably implement a separate node in the executor.
5999 elog(ERROR, "UNIQUE predicate is not yet implemented");
6006 * Restricted expressions
6008 * b_expr is a subset of the complete expression syntax defined by a_expr.
6010 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
6011 * cause trouble in the places where b_expr is used. For simplicity, we
6012 * just eliminate all the boolean-keyword-operator productions from b_expr.
6016 | b_expr TYPECAST Typename
6017 { $$ = makeTypeCast($1, $3); }
6018 | '+' b_expr %prec UMINUS
6019 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
6020 | '-' b_expr %prec UMINUS
6021 { $$ = doNegate($2); }
6023 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", NULL, $2); }
6025 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", NULL, $2); }
6027 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, NULL); }
6029 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, NULL); }
6031 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
6033 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
6035 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
6037 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
6039 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
6041 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
6043 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
6045 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
6047 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
6048 | b_expr qual_Op b_expr %prec Op
6049 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
6050 | qual_Op b_expr %prec Op
6051 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
6052 | b_expr qual_Op %prec POSTFIXOP
6053 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
6054 | b_expr IS DISTINCT FROM b_expr %prec IS
6055 { $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5); }
6056 | b_expr IS OF '(' type_list ')' %prec IS
6058 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
6060 | b_expr IS NOT OF '(' type_list ')' %prec IS
6062 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
6067 * Productions that can be used in both a_expr and b_expr.
6069 * Note: productions that refer recursively to a_expr or b_expr mostly
6070 * cannot appear here. However, it's OK to refer to a_exprs that occur
6071 * inside parentheses, such as function arguments; that cannot introduce
6072 * ambiguity to the b_expr syntax.
6074 c_expr: columnref { $$ = (Node *) $1; }
6075 | AexprConst { $$ = $1; }
6076 | PARAM attrs opt_indirection
6079 * PARAM without field names is considered a constant,
6080 * but with 'em, it is not. Not very consistent ...
6082 ParamRef *n = makeNode(ParamRef);
6085 n->indirection = $3;
6088 | '(' a_expr ')' attrs opt_indirection
6090 ExprFieldSelect *n = makeNode(ExprFieldSelect);
6093 n->indirection = $5;
6096 | '(' a_expr ')' opt_indirection
6100 ExprFieldSelect *n = makeNode(ExprFieldSelect);
6103 n->indirection = $4;
6113 FuncCall *n = makeNode(FuncCall);
6116 n->agg_star = FALSE;
6117 n->agg_distinct = FALSE;
6120 | func_name '(' expr_list ')'
6122 FuncCall *n = makeNode(FuncCall);
6125 n->agg_star = FALSE;
6126 n->agg_distinct = FALSE;
6129 | func_name '(' ALL expr_list ')'
6131 FuncCall *n = makeNode(FuncCall);
6134 n->agg_star = FALSE;
6135 n->agg_distinct = FALSE;
6136 /* Ideally we'd mark the FuncCall node to indicate
6137 * "must be an aggregate", but there's no provision
6138 * for that in FuncCall at the moment.
6142 | func_name '(' DISTINCT expr_list ')'
6144 FuncCall *n = makeNode(FuncCall);
6147 n->agg_star = FALSE;
6148 n->agg_distinct = TRUE;
6151 | func_name '(' '*' ')'
6154 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
6156 * This does the right thing for COUNT(*) (in fact,
6157 * any certainly-non-null expression would do for COUNT),
6158 * and there are no other aggregates in SQL92 that accept
6161 * The FuncCall node is also marked agg_star = true,
6162 * so that later processing can detect what the argument
6165 FuncCall *n = makeNode(FuncCall);
6166 A_Const *star = makeNode(A_Const);
6168 star->val.type = T_Integer;
6169 star->val.val.ival = 1;
6171 n->args = makeList1(star);
6173 n->agg_distinct = FALSE;
6179 * Translate as "'now'::text::date".
6181 * We cannot use "'now'::date" because coerce_type() will
6182 * immediately reduce that to a constant representing
6183 * today's date. We need to delay the conversion until
6184 * runtime, else the wrong things will happen when
6185 * CURRENT_DATE is used in a column default value or rule.
6187 * This could be simplified if we had a way to generate
6188 * an expression tree representing runtime application
6189 * of type-input conversion functions...
6191 A_Const *s = makeNode(A_Const);
6194 s->val.type = T_String;
6195 s->val.val.str = "now";
6196 s->typename = SystemTypeName("text");
6198 d = SystemTypeName("date");
6200 $$ = (Node *)makeTypeCast((Node *)s, d);
6205 * Translate as "'now'::text::timetz".
6206 * See comments for CURRENT_DATE.
6208 A_Const *s = makeNode(A_Const);
6211 s->val.type = T_String;
6212 s->val.val.str = "now";
6213 s->typename = SystemTypeName("text");
6215 d = SystemTypeName("timetz");
6216 /* SQL99 mandates a default precision of zero for TIME
6217 * fields in schemas. However, for CURRENT_TIME
6218 * let's preserve the microsecond precision we
6219 * might see from the system clock. - thomas 2001-12-07
6223 $$ = (Node *)makeTypeCast((Node *)s, d);
6225 | CURRENT_TIME '(' Iconst ')'
6228 * Translate as "'now'::text::timetz(n)".
6229 * See comments for CURRENT_DATE.
6231 A_Const *s = makeNode(A_Const);
6234 s->val.type = T_String;
6235 s->val.val.str = "now";
6236 s->typename = SystemTypeName("text");
6237 d = SystemTypeName("timetz");
6240 "CURRENT_TIME(%d) precision must not be negative",
6242 if ($3 > MAX_TIME_PRECISION)
6245 "CURRENT_TIME(%d) precision reduced to maximum allowed, %d",
6246 $3, MAX_TIME_PRECISION);
6247 $3 = MAX_TIME_PRECISION;
6251 $$ = (Node *)makeTypeCast((Node *)s, d);
6256 * Translate as "'now'::text::timestamptz".
6257 * See comments for CURRENT_DATE.
6259 A_Const *s = makeNode(A_Const);
6262 s->val.type = T_String;
6263 s->val.val.str = "now";
6264 s->typename = SystemTypeName("text");
6266 d = SystemTypeName("timestamptz");
6267 /* SQL99 mandates a default precision of 6 for timestamp.
6268 * Also, that is about as precise as we will get since
6269 * we are using a microsecond time interface.
6270 * - thomas 2001-12-07
6274 $$ = (Node *)makeTypeCast((Node *)s, d);
6276 | CURRENT_TIMESTAMP '(' Iconst ')'
6279 * Translate as "'now'::text::timestamptz(n)".
6280 * See comments for CURRENT_DATE.
6282 A_Const *s = makeNode(A_Const);
6285 s->val.type = T_String;
6286 s->val.val.str = "now";
6287 s->typename = SystemTypeName("text");
6289 d = SystemTypeName("timestamptz");
6292 "CURRENT_TIMESTAMP(%d) precision must not be negative",
6294 if ($3 > MAX_TIMESTAMP_PRECISION)
6297 "CURRENT_TIMESTAMP(%d) precision reduced to maximum allowed, %d",
6298 $3, MAX_TIMESTAMP_PRECISION);
6299 $3 = MAX_TIMESTAMP_PRECISION;
6303 $$ = (Node *)makeTypeCast((Node *)s, d);
6308 * Translate as "'now'::text::time".
6309 * See comments for CURRENT_DATE.
6311 A_Const *s = makeNode(A_Const);
6314 s->val.type = T_String;
6315 s->val.val.str = "now";
6316 s->typename = SystemTypeName("text");
6318 d = SystemTypeName("time");
6319 /* SQL99 mandates a default precision of zero for TIME
6320 * fields in schemas. However, for LOCALTIME
6321 * let's preserve the microsecond precision we
6322 * might see from the system clock. - thomas 2001-12-07
6326 $$ = (Node *)makeTypeCast((Node *)s, d);
6328 | LOCALTIME '(' Iconst ')'
6331 * Translate as "'now'::text::time(n)".
6332 * See comments for CURRENT_DATE.
6334 A_Const *s = makeNode(A_Const);
6337 s->val.type = T_String;
6338 s->val.val.str = "now";
6339 s->typename = SystemTypeName("text");
6340 d = SystemTypeName("time");
6343 "LOCALTIME(%d) precision must not be negative",
6345 if ($3 > MAX_TIME_PRECISION)
6348 "LOCALTIME(%d) precision reduced to maximum allowed, %d",
6349 $3, MAX_TIME_PRECISION);
6350 $3 = MAX_TIME_PRECISION;
6354 $$ = (Node *)makeTypeCast((Node *)s, d);
6359 * Translate as "'now'::text::timestamp".
6360 * See comments for CURRENT_DATE.
6362 A_Const *s = makeNode(A_Const);
6365 s->val.type = T_String;
6366 s->val.val.str = "now";
6367 s->typename = SystemTypeName("text");
6369 d = SystemTypeName("timestamp");
6370 /* SQL99 mandates a default precision of 6 for timestamp.
6371 * Also, that is about as precise as we will get since
6372 * we are using a microsecond time interface.
6373 * - thomas 2001-12-07
6377 $$ = (Node *)makeTypeCast((Node *)s, d);
6379 | LOCALTIMESTAMP '(' Iconst ')'
6382 * Translate as "'now'::text::timestamp(n)".
6383 * See comments for CURRENT_DATE.
6385 A_Const *s = makeNode(A_Const);
6388 s->val.type = T_String;
6389 s->val.val.str = "now";
6390 s->typename = SystemTypeName("text");
6392 d = SystemTypeName("timestamp");
6395 "LOCALTIMESTAMP(%d) precision must not be negative",
6397 if ($3 > MAX_TIMESTAMP_PRECISION)
6400 "LOCALTIMESTAMP(%d) precision reduced to maximum allowed, %d",
6401 $3, MAX_TIMESTAMP_PRECISION);
6402 $3 = MAX_TIMESTAMP_PRECISION;
6406 $$ = (Node *)makeTypeCast((Node *)s, d);
6410 FuncCall *n = makeNode(FuncCall);
6411 n->funcname = SystemFuncName("current_user");
6413 n->agg_star = FALSE;
6414 n->agg_distinct = FALSE;
6419 FuncCall *n = makeNode(FuncCall);
6420 n->funcname = SystemFuncName("session_user");
6422 n->agg_star = FALSE;
6423 n->agg_distinct = FALSE;
6428 FuncCall *n = makeNode(FuncCall);
6429 n->funcname = SystemFuncName("current_user");
6431 n->agg_star = FALSE;
6432 n->agg_distinct = FALSE;
6435 | CAST '(' a_expr AS Typename ')'
6436 { $$ = makeTypeCast($3, $5); }
6437 | EXTRACT '(' extract_list ')'
6439 FuncCall *n = makeNode(FuncCall);
6440 n->funcname = SystemFuncName("date_part");
6442 n->agg_star = FALSE;
6443 n->agg_distinct = FALSE;
6446 | OVERLAY '(' overlay_list ')'
6448 /* overlay(A PLACING B FROM C FOR D) is converted to
6449 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
6450 * overlay(A PLACING B FROM C) is converted to
6451 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
6453 FuncCall *n = makeNode(FuncCall);
6454 n->funcname = SystemFuncName("overlay");
6456 n->agg_star = FALSE;
6457 n->agg_distinct = FALSE;
6460 | POSITION '(' position_list ')'
6462 /* position(A in B) is converted to position(B, A) */
6463 FuncCall *n = makeNode(FuncCall);
6464 n->funcname = SystemFuncName("position");
6466 n->agg_star = FALSE;
6467 n->agg_distinct = FALSE;
6470 | SUBSTRING '(' substr_list ')'
6472 /* substring(A from B for C) is converted to
6473 * substring(A, B, C) - thomas 2000-11-28
6475 FuncCall *n = makeNode(FuncCall);
6476 n->funcname = SystemFuncName("substring");
6478 n->agg_star = FALSE;
6479 n->agg_distinct = FALSE;
6482 | TREAT '(' a_expr AS Typename ')'
6484 /* TREAT(expr AS target) converts expr of a particular type to target,
6485 * which is defined to be a subtype of the original expression.
6486 * In SQL99, this is intended for use with structured UDTs,
6487 * but let's make this a generally useful form allowing stronger
6488 * coersions than are handled by implicit casting.
6490 FuncCall *n = makeNode(FuncCall);
6491 /* Convert SystemTypeName() to SystemFuncName() even though
6492 * at the moment they result in the same thing.
6494 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
6495 n->args = makeList1($3);
6498 | TRIM '(' BOTH trim_list ')'
6500 /* various trim expressions are defined in SQL92
6501 * - thomas 1997-07-19
6503 FuncCall *n = makeNode(FuncCall);
6504 n->funcname = SystemFuncName("btrim");
6506 n->agg_star = FALSE;
6507 n->agg_distinct = FALSE;
6510 | TRIM '(' LEADING trim_list ')'
6512 FuncCall *n = makeNode(FuncCall);
6513 n->funcname = SystemFuncName("ltrim");
6515 n->agg_star = FALSE;
6516 n->agg_distinct = FALSE;
6519 | TRIM '(' TRAILING trim_list ')'
6521 FuncCall *n = makeNode(FuncCall);
6522 n->funcname = SystemFuncName("rtrim");
6524 n->agg_star = FALSE;
6525 n->agg_distinct = FALSE;
6528 | TRIM '(' trim_list ')'
6530 FuncCall *n = makeNode(FuncCall);
6531 n->funcname = SystemFuncName("btrim");
6533 n->agg_star = FALSE;
6534 n->agg_distinct = FALSE;
6537 | CONVERT '(' a_expr USING any_name ')'
6539 FuncCall *n = makeNode(FuncCall);
6540 A_Const *c = makeNode(A_Const);
6542 c->val.type = T_String;
6543 c->val.val.str = NameListToQuotedString($5);
6545 n->funcname = SystemFuncName("convert_using");
6546 n->args = makeList2($3, c);
6547 n->agg_star = FALSE;
6548 n->agg_distinct = FALSE;
6551 | CONVERT '(' expr_list ')'
6553 FuncCall *n = makeNode(FuncCall);
6554 n->funcname = SystemFuncName("convert");
6556 n->agg_star = FALSE;
6557 n->agg_distinct = FALSE;
6560 | select_with_parens %prec UMINUS
6562 SubLink *n = makeNode(SubLink);
6563 n->subLinkType = EXPR_SUBLINK;
6569 | EXISTS select_with_parens
6571 SubLink *n = makeNode(SubLink);
6572 n->subLinkType = EXISTS_SUBLINK;
6578 | ARRAY select_with_parens
6580 SubLink *n = makeNode(SubLink);
6581 n->subLinkType = ARRAY_SUBLINK;
6592 * Supporting nonterminals for expressions.
6596 opt_indirection '[' a_expr ']'
6598 A_Indices *ai = makeNode(A_Indices);
6601 $$ = lappend($1, ai);
6603 | opt_indirection '[' a_expr ':' a_expr ']'
6605 A_Indices *ai = makeNode(A_Indices);
6608 $$ = lappend($1, ai);
6614 expr_list: a_expr { $$ = makeList1($1); }
6615 | expr_list ',' a_expr { $$ = lappend($1, $3); }
6619 extract_arg FROM a_expr
6621 A_Const *n = makeNode(A_Const);
6622 n->val.type = T_String;
6623 n->val.val.str = $1;
6624 $$ = makeList2((Node *) n, $3);
6626 | /*EMPTY*/ { $$ = NIL; }
6629 type_list: type_list ',' Typename
6631 $$ = lappend($1, $3);
6639 array_expr_list: array_expr
6640 { $$ = makeList1($1); }
6641 | array_expr_list ',' array_expr
6642 { $$ = lappend($1, $3); }
6645 array_expr: '[' expr_list ']'
6647 ArrayExpr *n = makeNode(ArrayExpr);
6651 | '[' array_expr_list ']'
6653 ArrayExpr *n = makeNode(ArrayExpr);
6659 /* Allow delimited string SCONST in extract_arg as an SQL extension.
6660 * - thomas 2001-04-12
6665 | YEAR_P { $$ = "year"; }
6666 | MONTH_P { $$ = "month"; }
6667 | DAY_P { $$ = "day"; }
6668 | HOUR_P { $$ = "hour"; }
6669 | MINUTE_P { $$ = "minute"; }
6670 | SECOND_P { $$ = "second"; }
6671 | SCONST { $$ = $1; }
6674 /* OVERLAY() arguments
6675 * SQL99 defines the OVERLAY() function:
6676 * o overlay(text placing text from int for int)
6677 * o overlay(text placing text from int)
6680 a_expr overlay_placing substr_from substr_for
6682 $$ = makeList4($1, $2, $3, $4);
6684 | a_expr overlay_placing substr_from
6686 $$ = makeList3($1, $2, $3);
6695 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
6698 b_expr IN_P b_expr { $$ = makeList2($3, $1); }
6699 | /*EMPTY*/ { $$ = NIL; }
6702 /* SUBSTRING() arguments
6703 * SQL9x defines a specific syntax for arguments to SUBSTRING():
6704 * o substring(text from int for int)
6705 * o substring(text from int) get entire string from starting point "int"
6706 * o substring(text from pattern) get entire string matching pattern
6707 * o substring(text for int) get first "int" characters of string
6708 * We also want to implement generic substring functions which accept
6709 * the usual generic list of arguments. So we will accept both styles
6710 * here, and convert the SQL9x style to the generic list for further
6711 * processing. - thomas 2000-11-28
6714 a_expr substr_from substr_for
6716 $$ = makeList3($1, $2, $3);
6718 | a_expr substr_for substr_from
6720 $$ = makeList3($1, $3, $2);
6722 | a_expr substr_from
6724 $$ = makeList2($1, $2);
6728 A_Const *n = makeNode(A_Const);
6729 n->val.type = T_Integer;
6730 n->val.val.ival = 1;
6731 $$ = makeList3($1, (Node *)n, $2);
6742 FROM a_expr { $$ = $2; }
6745 substr_for: FOR a_expr { $$ = $2; }
6748 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
6749 | FROM expr_list { $$ = $2; }
6750 | expr_list { $$ = $1; }
6753 in_expr: select_with_parens
6755 SubLink *n = makeNode(SubLink);
6757 /* other fields will be filled later */
6760 | '(' expr_list ')' { $$ = (Node *)$2; }
6764 * Define SQL92-style case clause.
6765 * Allow all four forms described in the standard:
6766 * - Full specification
6767 * CASE WHEN a = b THEN c ... ELSE d END
6768 * - Implicit argument
6769 * CASE a WHEN b THEN c ... ELSE d END
6770 * - Conditional NULL
6772 * same as CASE WHEN x = y THEN NULL ELSE x END
6773 * - Conditional substitution from list, use first non-null argument
6775 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
6776 * - thomas 1998-11-09
6778 * NULLIF and COALESCE have become first class nodes to
6779 * prevent double evaluation of arguments.
6780 * - Kris Jurka 2003-02-11
6782 case_expr: CASE case_arg when_clause_list case_default END_P
6784 CaseExpr *c = makeNode(CaseExpr);
6785 c->arg = (Expr *) $2;
6787 c->defresult = (Expr *) $4;
6790 | NULLIF '(' a_expr ',' a_expr ')'
6792 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5);
6794 | COALESCE '(' expr_list ')'
6796 CoalesceExpr *c = makeNode(CoalesceExpr);
6803 /* There must be at least one */
6804 when_clause { $$ = makeList1($1); }
6805 | when_clause_list when_clause { $$ = lappend($1, $2); }
6809 WHEN a_expr THEN a_expr
6811 CaseWhen *w = makeNode(CaseWhen);
6812 w->expr = (Expr *) $2;
6813 w->result = (Expr *) $4;
6819 ELSE a_expr { $$ = $2; }
6820 | /*EMPTY*/ { $$ = NULL; }
6823 case_arg: a_expr { $$ = $1; }
6824 | /*EMPTY*/ { $$ = NULL; }
6828 * columnref starts with relation_name not ColId, so that OLD and NEW
6829 * references can be accepted. Note that when there are more than two
6830 * dotted names, the first name is not actually a relation name...
6832 columnref: relation_name opt_indirection
6834 $$ = makeNode(ColumnRef);
6835 $$->fields = makeList1(makeString($1));
6836 $$->indirection = $2;
6838 | dotted_name opt_indirection
6840 $$ = makeNode(ColumnRef);
6842 $$->indirection = $2;
6848 { $$ = lcons(makeString($1), $2); }
6851 attrs: '.' attr_name
6852 { $$ = makeList1(makeString($2)); }
6854 { $$ = makeList1(makeString("*")); }
6855 | '.' attr_name attrs
6856 { $$ = lcons(makeString($2), $3); }
6860 /*****************************************************************************
6864 *****************************************************************************/
6866 /* Target lists as found in SELECT ... and INSERT VALUES ( ... ) */
6869 target_el { $$ = makeList1($1); }
6870 | target_list ',' target_el { $$ = lappend($1, $3); }
6873 /* AS is not optional because shift/red conflict with unary ops */
6874 target_el: a_expr AS ColLabel
6876 $$ = makeNode(ResTarget);
6878 $$->indirection = NIL;
6879 $$->val = (Node *)$1;
6883 $$ = makeNode(ResTarget);
6885 $$->indirection = NIL;
6886 $$->val = (Node *)$1;
6890 ColumnRef *n = makeNode(ColumnRef);
6891 n->fields = makeList1(makeString("*"));
6892 n->indirection = NIL;
6893 $$ = makeNode(ResTarget);
6895 $$->indirection = NIL;
6896 $$->val = (Node *)n;
6900 /* Target list as found in UPDATE table SET ...
6901 | '(' row_ ')' = '(' row_ ')'
6907 update_target_el { $$ = makeList1($1); }
6908 | update_target_list ',' update_target_el { $$ = lappend($1,$3); }
6912 ColId opt_indirection '=' a_expr
6914 $$ = makeNode(ResTarget);
6916 $$->indirection = $2;
6917 $$->val = (Node *)$4;
6922 insert_target_el { $$ = makeList1($1); }
6923 | insert_target_list ',' insert_target_el { $$ = lappend($1, $3); }
6927 target_el { $$ = $1; }
6930 InsertDefault *def = makeNode(InsertDefault);
6931 $$ = makeNode(ResTarget);
6933 $$->indirection = NULL;
6934 $$->val = (Node *)def;
6939 /*****************************************************************************
6941 * Names and constants
6943 *****************************************************************************/
6946 SpecialRuleRelation { $$ = $1; }
6947 | ColId { $$ = $1; }
6950 qualified_name_list:
6951 qualified_name { $$ = makeList1($1); }
6952 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
6958 $$ = makeNode(RangeVar);
6959 $$->catalogname = NULL;
6960 $$->schemaname = NULL;
6965 $$ = makeNode(RangeVar);
6969 $$->catalogname = NULL;
6970 $$->schemaname = strVal(lfirst($1));
6971 $$->relname = strVal(lsecond($1));
6974 $$->catalogname = strVal(lfirst($1));
6975 $$->schemaname = strVal(lsecond($1));
6976 $$->relname = strVal(lthird($1));
6980 "Improper qualified name "
6981 "(too many dotted names): %s",
6982 NameListToString($1));
6989 { $$ = makeList1(makeString($1)); }
6990 | name_list ',' name
6991 { $$ = lappend($1, makeString($3)); }
6995 name: ColId { $$ = $1; };
7003 attr_name: ColId { $$ = $1; };
7005 index_name: ColId { $$ = $1; };
7007 file_name: Sconst { $$ = $1; };
7009 func_name: function_name
7010 { $$ = makeList1(makeString($1)); }
7011 | dotted_name { $$ = $1; }
7020 A_Const *n = makeNode(A_Const);
7021 n->val.type = T_Integer;
7022 n->val.val.ival = $1;
7027 A_Const *n = makeNode(A_Const);
7028 n->val.type = T_Float;
7029 n->val.val.str = $1;
7034 A_Const *n = makeNode(A_Const);
7035 n->val.type = T_String;
7036 n->val.val.str = $1;
7041 A_Const *n = makeNode(A_Const);
7042 n->val.type = T_BitString;
7043 n->val.val.str = $1;
7048 /* This is a bit constant per SQL99:
7049 * Without Feature F511, "BIT data type",
7050 * a <general literal> shall not be a
7051 * <bit string literal> or a <hex string literal>.
7053 A_Const *n = makeNode(A_Const);
7054 n->val.type = T_BitString;
7055 n->val.val.str = $1;
7058 | ConstTypename Sconst
7060 A_Const *n = makeNode(A_Const);
7062 n->val.type = T_String;
7063 n->val.val.str = $2;
7066 | ConstInterval Sconst opt_interval
7068 A_Const *n = makeNode(A_Const);
7070 n->val.type = T_String;
7071 n->val.val.str = $2;
7072 /* precision is not specified, but fields may be... */
7073 if ($3 != INTERVAL_FULL_RANGE)
7074 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
7077 | ConstInterval '(' Iconst ')' Sconst opt_interval
7079 A_Const *n = makeNode(A_Const);
7081 n->val.type = T_String;
7082 n->val.val.str = $5;
7083 /* precision specified, and fields may be... */
7086 "INTERVAL(%d) precision must not be negative",
7088 if ($3 > MAX_INTERVAL_PRECISION)
7091 "INTERVAL(%d) precision reduced to maximum allowed, %d",
7092 $3, MAX_INTERVAL_PRECISION);
7093 $3 = MAX_INTERVAL_PRECISION;
7095 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
7098 | PARAM opt_indirection
7100 ParamRef *n = makeNode(ParamRef);
7103 n->indirection = $2;
7108 $$ = (Node *)makeBoolConst(TRUE);
7112 $$ = (Node *)makeBoolConst(FALSE);
7116 A_Const *n = makeNode(A_Const);
7117 n->val.type = T_Null;
7122 Iconst: ICONST { $$ = $1; };
7123 Sconst: SCONST { $$ = $1; };
7124 UserId: ColId { $$ = $1; };
7127 * Name classification hierarchy.
7129 * IDENT is the lexeme returned by the lexer for identifiers that match
7130 * no known keyword. In most cases, we can accept certain keywords as
7131 * names, not only IDENTs. We prefer to accept as many such keywords
7132 * as possible to minimize the impact of "reserved words" on programmers.
7133 * So, we divide names into several possible classes. The classification
7134 * is chosen in part to make keywords acceptable as names wherever possible.
7137 /* Column identifier --- names that can be column, table, etc names.
7139 ColId: IDENT { $$ = $1; }
7140 | unreserved_keyword { $$ = pstrdup($1); }
7141 | col_name_keyword { $$ = pstrdup($1); }
7144 /* Type identifier --- names that can be type names.
7146 type_name: IDENT { $$ = $1; }
7147 | unreserved_keyword { $$ = pstrdup($1); }
7150 /* Function identifier --- names that can be function names.
7154 | unreserved_keyword { $$ = pstrdup($1); }
7155 | func_name_keyword { $$ = pstrdup($1); }
7158 /* Column label --- allowed labels in "AS" clauses.
7159 * This presently includes *all* Postgres keywords.
7161 ColLabel: IDENT { $$ = $1; }
7162 | unreserved_keyword { $$ = pstrdup($1); }
7163 | col_name_keyword { $$ = pstrdup($1); }
7164 | func_name_keyword { $$ = pstrdup($1); }
7165 | reserved_keyword { $$ = pstrdup($1); }
7170 * Keyword classification lists. Generally, every keyword present in
7171 * the Postgres grammar should appear in exactly one of these lists.
7173 * Put a new keyword into the first list that it can go into without causing
7174 * shift or reduce conflicts. The earlier lists define "less reserved"
7175 * categories of keywords.
7178 /* "Unreserved" keywords --- available for use as any kind of name.
7364 /* Column identifier --- keywords that can be column, table, etc names.
7366 * Many of these keywords will in fact be recognized as type or function
7367 * names too; but they have special productions for the purpose, and so
7368 * can't be treated as "generic" type or function names.
7370 * The type names appearing here are not usable as function names
7371 * because they can be followed by '(' in typename productions, which
7372 * looks too much like a function call for an LR(1) parser.
7408 /* Function identifier --- keywords that can be function names.
7410 * Most of these are keywords that are used as operators in expressions;
7411 * in general such keywords can't be column names because they would be
7412 * ambiguous with variables, but they are unambiguous as function identifiers.
7414 * Do not include POSITION, SUBSTRING, etc here since they have explicit
7415 * productions in a_expr to support the goofy SQL9x argument syntax.
7416 * - thomas 2000-11-28
7442 /* Reserved keyword --- these keywords are usable only as a ColLabel.
7444 * Keywords appear here if they could not be distinguished from variable,
7445 * type, or function names in some contexts. Don't put things here unless
7521 SpecialRuleRelation:
7527 elog(ERROR, "OLD used in non-rule query");
7534 elog(ERROR, "NEW used in non-rule query");
7541 makeTypeCast(Node *arg, TypeName *typename)
7544 * Simply generate a TypeCast node.
7546 * Earlier we would determine whether an A_Const would
7547 * be acceptable, however Domains require coerce_type()
7548 * to process them -- applying constraints as required.
7550 TypeCast *n = makeNode(TypeCast);
7552 n->typename = typename;
7557 makeStringConst(char *str, TypeName *typename)
7559 A_Const *n = makeNode(A_Const);
7561 n->val.type = T_String;
7562 n->val.val.str = str;
7563 n->typename = typename;
7569 makeIntConst(int val)
7571 A_Const *n = makeNode(A_Const);
7572 n->val.type = T_Integer;
7573 n->val.val.ival = val;
7574 n->typename = SystemTypeName("int4");
7580 makeFloatConst(char *str)
7582 A_Const *n = makeNode(A_Const);
7584 n->val.type = T_Float;
7585 n->val.val.str = str;
7586 n->typename = SystemTypeName("float8");
7592 makeAConst(Value *v)
7599 n = makeFloatConst(v->val.str);
7603 n = makeIntConst(v->val.ival);
7608 n = makeStringConst(v->val.str, NULL);
7616 * Create a DefElem node and set contents.
7617 * Could be moved to nodes/makefuncs.c if this is useful elsewhere.
7620 makeDefElem(char *name, Node *arg)
7622 DefElem *f = makeNode(DefElem);
7629 * Create an A_Const node and initialize to a boolean constant.
7632 makeBoolConst(bool state)
7634 A_Const *n = makeNode(A_Const);
7635 n->val.type = T_String;
7636 n->val.val.str = (state? "t": "f");
7637 n->typename = SystemTypeName("bool");
7642 * Generate separate operator nodes for a single row descriptor expression.
7643 * Perhaps this should go deeper in the parser someday...
7644 * - thomas 1997-12-22
7647 makeRowExpr(List *opr, List *largs, List *rargs)
7653 if (length(largs) != length(rargs))
7654 elog(ERROR, "Unequal number of entries in row expression");
7656 if (lnext(largs) != NIL)
7657 expr = makeRowExpr(opr, lnext(largs), lnext(rargs));
7659 larg = lfirst(largs);
7660 rarg = lfirst(rargs);
7662 oprname = strVal(llast(opr));
7664 if ((strcmp(oprname, "=") == 0) ||
7665 (strcmp(oprname, "<") == 0) ||
7666 (strcmp(oprname, "<=") == 0) ||
7667 (strcmp(oprname, ">") == 0) ||
7668 (strcmp(oprname, ">=") == 0))
7671 expr = (Node *) makeA_Expr(AEXPR_OP, opr, larg, rarg);
7673 expr = (Node *) makeA_Expr(AEXPR_AND, NIL, expr,
7674 (Node *) makeA_Expr(AEXPR_OP, opr,
7677 else if (strcmp(oprname, "<>") == 0)
7680 expr = (Node *) makeA_Expr(AEXPR_OP, opr, larg, rarg);
7682 expr = (Node *) makeA_Expr(AEXPR_OR, NIL, expr,
7683 (Node *) makeA_Expr(AEXPR_OP, opr,
7688 elog(ERROR, "Operator '%s' not implemented for row expressions",
7695 /* makeDistinctExpr()
7696 * Generate separate operator nodes for a single row descriptor expression.
7697 * Same comments as for makeRowExpr().
7700 makeDistinctExpr(List *largs, List *rargs)
7705 if (length(largs) != length(rargs))
7706 elog(ERROR, "Unequal number of entries in row expression");
7708 if (lnext(largs) != NIL)
7709 expr = makeDistinctExpr(lnext(largs), lnext(rargs));
7711 larg = lfirst(largs);
7712 rarg = lfirst(rargs);
7715 expr = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", larg, rarg);
7717 expr = (Node *) makeA_Expr(AEXPR_OR, NIL, expr,
7718 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=",
7724 /* makeRowNullTest()
7725 * Generate separate operator nodes for a single row descriptor test.
7728 makeRowNullTest(NullTestType test, List *args)
7733 if (lnext(args) != NIL)
7734 expr = makeRowNullTest(test, lnext(args));
7736 n = makeNode(NullTest);
7737 n->arg = (Expr *) lfirst(args);
7738 n->nulltesttype = test;
7742 else if (test == IS_NOT_NULL)
7743 expr = (Node *) makeA_Expr(AEXPR_OR, NIL, expr, (Node *)n);
7745 expr = (Node *) makeA_Expr(AEXPR_AND, NIL, expr, (Node *)n);
7751 * Create and populate a FuncCall node to support the OVERLAPS operator.
7754 makeOverlaps(List *largs, List *rargs)
7756 FuncCall *n = makeNode(FuncCall);
7757 n->funcname = SystemFuncName("overlaps");
7758 if (length(largs) == 1)
7759 largs = lappend(largs, largs);
7760 else if (length(largs) != 2)
7761 elog(ERROR, "Wrong number of parameters"
7762 " on left side of OVERLAPS expression");
7763 if (length(rargs) == 1)
7764 rargs = lappend(rargs, rargs);
7765 else if (length(rargs) != 2)
7766 elog(ERROR, "Wrong number of parameters"
7767 " on right side of OVERLAPS expression");
7768 n->args = nconc(largs, rargs);
7769 n->agg_star = FALSE;
7770 n->agg_distinct = FALSE;
7774 /* findLeftmostSelect()
7775 * Find the leftmost component SelectStmt in a set-operation parsetree.
7778 findLeftmostSelect(SelectStmt *node)
7780 while (node && node->op != SETOP_NONE)
7782 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
7786 /* insertSelectOptions()
7787 * Insert ORDER BY, etc into an already-constructed SelectStmt.
7789 * This routine is just to avoid duplicating code in SelectStmt productions.
7792 insertSelectOptions(SelectStmt *stmt,
7793 List *sortClause, List *forUpdate,
7794 Node *limitOffset, Node *limitCount)
7797 * Tests here are to reject constructs like
7798 * (SELECT foo ORDER BY bar) ORDER BY baz
7802 if (stmt->sortClause)
7803 elog(ERROR, "Multiple ORDER BY clauses not allowed");
7804 stmt->sortClause = sortClause;
7808 if (stmt->forUpdate)
7809 elog(ERROR, "Multiple FOR UPDATE clauses not allowed");
7810 stmt->forUpdate = forUpdate;
7814 if (stmt->limitOffset)
7815 elog(ERROR, "Multiple OFFSET clauses not allowed");
7816 stmt->limitOffset = limitOffset;
7820 if (stmt->limitCount)
7821 elog(ERROR, "Multiple LIMIT clauses not allowed");
7822 stmt->limitCount = limitCount;
7827 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
7829 SelectStmt *n = makeNode(SelectStmt);
7833 n->larg = (SelectStmt *) larg;
7834 n->rarg = (SelectStmt *) rarg;
7839 * Build a properly-qualified reference to a built-in function.
7842 SystemFuncName(char *name)
7844 return makeList2(makeString("pg_catalog"), makeString(name));
7848 * Build a properly-qualified reference to a built-in type.
7850 * typmod is defaulted, but may be changed afterwards by caller.
7853 SystemTypeName(char *name)
7855 TypeName *n = makeNode(TypeName);
7857 n->names = makeList2(makeString("pg_catalog"), makeString(name));
7863 * Initialize to parse one query string
7868 QueryIsRule = FALSE;
7871 /* exprIsNullConstant()
7872 * Test whether an a_expr is a plain NULL constant or not.
7875 exprIsNullConstant(Node *arg)
7877 if (arg && IsA(arg, A_Const))
7879 A_Const *con = (A_Const *) arg;
7881 if (con->val.type == T_Null &&
7882 con->typename == NULL)
7889 * Handle negation of a numeric constant.
7891 * Formerly, we did this here because the optimizer couldn't cope with
7892 * indexquals that looked like "var = -4" --- it wants "var = const"
7893 * and a unary minus operator applied to a constant didn't qualify.
7894 * As of Postgres 7.0, that problem doesn't exist anymore because there
7895 * is a constant-subexpression simplifier in the optimizer. However,
7896 * there's still a good reason for doing this here, which is that we can
7897 * postpone committing to a particular internal representation for simple
7898 * negative constants. It's better to leave "-123.456" in string form
7899 * until we know what the desired type is.
7904 if (IsA(n, A_Const))
7906 A_Const *con = (A_Const *)n;
7908 if (con->val.type == T_Integer)
7910 con->val.val.ival = -con->val.val.ival;
7913 if (con->val.type == T_Float)
7915 doNegateFloat(&con->val);
7920 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n);
7924 doNegateFloat(Value *v)
7926 char *oldval = v->val.str;
7928 Assert(IsA(v, Float));
7932 v->val.str = oldval+1; /* just strip the '-' */
7935 char *newval = (char *) palloc(strlen(oldval) + 2);
7938 strcpy(newval+1, oldval);
7939 v->val.str = newval;