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.389 2002/12/30 15:31:47 momjian Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled inside parse_analyze() so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
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 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 CursorStmt 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 OptimizableStmt
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
167 %type <list> user_list
169 %type <list> OptGroupList
170 %type <defelt> OptGroupElem
172 %type <list> OptUserList
173 %type <defelt> OptUserElem
175 %type <str> OptSchemaName
176 %type <list> OptSchemaEltList
178 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
179 %type <str> opt_lancompiler
181 %type <str> TriggerEvents
182 %type <value> TriggerFuncArg
184 %type <str> relation_name copy_file_name
185 database_name access_method_clause access_method attr_name
186 index_name name function_name file_name
188 %type <list> func_name handler_name qual_Op qual_all_Op OptUseOp
189 opt_class opt_validator
191 %type <range> qualified_name OptConstrFromTable
193 %type <str> opt_id all_Op MathOp opt_name SpecialRuleRelation
195 %type <str> iso_level opt_encoding
197 %type <list> grantee_list
198 %type <ival> privilege
199 %type <list> privileges privilege_list
200 %type <privtarget> privilege_target
201 %type <node> function_with_argtypes
202 %type <list> function_with_argtypes_list
203 %type <chr> TriggerOneEvent
205 %type <list> stmtblock stmtmulti
206 OptTableElementList TableElementList OptInherit definition
207 opt_distinct opt_definition func_args
208 func_args_list func_as createfunc_opt_list
209 oper_argtypes RuleActionList RuleActionMulti
210 opt_column_list columnList opt_name_list
211 sort_clause opt_sort_clause sortby_list index_params
212 index_list name_list from_clause from_list opt_array_bounds
213 qualified_name_list any_name any_name_list
214 any_operator expr_list dotted_name attrs
215 target_list update_target_list insert_column_list
216 insert_target_list def_list opt_indirection
217 group_clause TriggerFuncArgs select_limit
218 opt_select_limit opclass_item_list trans_options
220 prep_type_clause prep_type_list
223 %type <range> into_clause OptTempTableName
225 %type <defelt> createfunc_opt_item
226 %type <typnam> func_arg func_return func_type aggr_argtype
228 %type <boolean> opt_arg TriggerForType OptTemp OptWithOids
229 %type <oncommit> OnCommitOption
231 %type <list> for_update_clause opt_for_update_clause update_list
232 %type <boolean> opt_all
234 %type <node> join_outer join_qual
235 %type <jtype> join_type
237 %type <list> extract_list overlay_list position_list
238 %type <list> substr_list trim_list
239 %type <ival> opt_interval
240 %type <node> overlay_placing substr_from substr_for
242 %type <boolean> opt_instead opt_cursor
243 %type <boolean> index_opt_unique opt_verbose opt_full
244 %type <boolean> opt_freeze opt_default opt_recheck
245 %type <defelt> opt_binary opt_oids copy_delimiter
247 %type <boolean> copy_from
249 %type <ival> direction reindex_type drop_type
250 opt_column event comment_type
252 %type <ival> fetch_how_many
254 %type <node> 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
269 %type <list> row row_descriptor type_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_TRANS ABSOLUTE ACCESS ACTION ADD AFTER
326 AGGREGATE ALL ALTER ANALYSE ANALYZE AND ANY AS ASC
327 ASSERTION ASSIGNMENT AT AUTHORIZATION
329 BACKWARD BEFORE BEGIN_TRANS 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_TRANS ESCAPE EXCEPT
344 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
346 FALSE_P FETCH FLOAT_P FOR FORCE FOREIGN FORWARD
347 FREEZE FROM FULL FUNCTION
349 GET GLOBAL GRANT GROUP_P
351 HANDLER HAVING 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 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 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 /* these are not real. they are here so that they get generated as #define's*/
418 /* precedence: lowest to highest */
426 %nonassoc LIKE ILIKE SIMILAR
431 %left POSTFIXOP /* dummy for postfix Op rules */
432 %left Op OPERATOR /* multi-character ops and user-defined operators */
435 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
439 /* Unary Operators */
440 %left AT ZONE /* sets precedence for AT TIME ZONE */
448 * These might seem to be low-precedence, but actually they are not part
449 * of the arithmetic hierarchy at all in their use as JOIN operators.
450 * We make them high-precedence to support their use as function names.
451 * They wouldn't be given a precedence at all, were it not that we need
452 * left-associativity among the JOIN rules themselves.
454 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
458 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
459 * psql already handles such cases, but other interfaces don't.
462 stmtblock: stmtmulti { parsetree = $1; }
465 /* the thrashing around here is to discard "empty" statements... */
466 stmtmulti: stmtmulti ';' stmt
467 { if ($3 != (Node *)NULL)
468 $$ = lappend($1, $3);
473 { if ($1 != (Node *)NULL)
546 | CreateConversionStmt
548 { $$ = (Node *)NULL; }
551 /*****************************************************************************
553 * Create a new Postgres DBMS user
556 *****************************************************************************/
559 CREATE USER UserId opt_with OptUserList
561 CreateUserStmt *n = makeNode(CreateUserStmt);
573 /*****************************************************************************
575 * Alter a postgresql DBMS user
578 *****************************************************************************/
581 ALTER USER UserId opt_with OptUserList
583 AlterUserStmt *n = makeNode(AlterUserStmt);
592 ALTER USER UserId SET set_rest
594 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
596 n->variable = $5->name;
600 | ALTER USER UserId VariableResetStmt
602 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
604 n->variable = ((VariableResetStmt *)$4)->name;
611 /*****************************************************************************
613 * Drop a postgresql DBMS user
615 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
616 * might own objects in multiple databases, there is presently no way to
617 * implement either cascading or restricting. Caveat DBA.
618 *****************************************************************************/
623 DropUserStmt *n = makeNode(DropUserStmt);
630 * Options for CREATE USER and ALTER USER
633 OptUserList OptUserElem { $$ = lappend($1, $2); }
634 | /* EMPTY */ { $$ = NIL; }
640 $$ = makeDefElem("password", (Node *)makeString($2));
642 | ENCRYPTED PASSWORD Sconst
644 $$ = makeDefElem("encryptedPassword", (Node *)makeString($3));
646 | UNENCRYPTED PASSWORD Sconst
648 $$ = makeDefElem("unencryptedPassword", (Node *)makeString($3));
652 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
656 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
660 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
664 $$ = makeDefElem("createuser", (Node *)makeInteger(TRUE));
668 $$ = makeDefElem("createuser", (Node *)makeInteger(FALSE));
670 | IN_P GROUP_P user_list
672 $$ = makeDefElem("groupElts", (Node *)$3);
676 $$ = makeDefElem("validUntil", (Node *)makeString($3));
680 user_list: user_list ',' UserId { $$ = lappend($1, makeString($3)); }
681 | UserId { $$ = makeList1(makeString($1)); }
686 /*****************************************************************************
688 * Create a postgresql group
691 *****************************************************************************/
694 CREATE GROUP_P UserId opt_with OptGroupList
696 CreateGroupStmt *n = makeNode(CreateGroupStmt);
704 * Options for CREATE GROUP
707 OptGroupList OptGroupElem { $$ = lappend($1, $2); }
708 | /* EMPTY */ { $$ = NIL; }
714 $$ = makeDefElem("userElts", (Node *)$2);
718 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
723 /*****************************************************************************
725 * Alter a postgresql group
728 *****************************************************************************/
731 ALTER GROUP_P UserId add_drop USER user_list
733 AlterGroupStmt *n = makeNode(AlterGroupStmt);
741 add_drop: ADD { $$ = +1; }
746 /*****************************************************************************
748 * Drop a postgresql group
750 * XXX see above notes about cascading DROP USER; groups have same problem.
751 *****************************************************************************/
756 DropGroupStmt *n = makeNode(DropGroupStmt);
763 /*****************************************************************************
765 * Manipulate a schema
767 *****************************************************************************/
770 CREATE SCHEMA OptSchemaName AUTHORIZATION UserId OptSchemaEltList
772 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
773 /* One can omit the schema name or the authorization id. */
782 | CREATE SCHEMA ColId OptSchemaEltList
784 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
785 /* ...but not both */
795 | /* EMPTY */ { $$ = NULL; }
799 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
800 | /* EMPTY */ { $$ = NIL; }
804 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
805 * statement (in addition to by themselves).
814 /*****************************************************************************
816 * Set PG internal variable
817 * SET name TO 'var_value'
818 * Include SQL92 syntax (thomas 1997-10-22):
819 * SET TIME ZONE 'var_value'
821 *****************************************************************************/
826 VariableSetStmt *n = $2;
832 VariableSetStmt *n = $3;
836 | SET SESSION set_rest
838 VariableSetStmt *n = $3;
844 set_rest: ColId TO var_list_or_default
846 VariableSetStmt *n = makeNode(VariableSetStmt);
851 | ColId '=' var_list_or_default
853 VariableSetStmt *n = makeNode(VariableSetStmt);
858 | TIME ZONE zone_value
860 VariableSetStmt *n = makeNode(VariableSetStmt);
861 n->name = "timezone";
863 n->args = makeList1($3);
866 | TRANSACTION ISOLATION LEVEL iso_level opt_mode
868 VariableSetStmt *n = makeNode(VariableSetStmt);
869 n->name = "TRANSACTION ISOLATION LEVEL";
870 n->args = makeList1(makeStringConst($4, NULL));
873 | SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL iso_level
875 VariableSetStmt *n = makeNode(VariableSetStmt);
876 n->name = "default_transaction_isolation";
877 n->args = makeList1(makeStringConst($7, NULL));
882 VariableSetStmt *n = makeNode(VariableSetStmt);
883 n->name = "client_encoding";
885 n->args = makeList1(makeStringConst($2, NULL));
888 | SESSION AUTHORIZATION ColId_or_Sconst
890 VariableSetStmt *n = makeNode(VariableSetStmt);
891 n->name = "session_authorization";
892 n->args = makeList1(makeStringConst($3, NULL));
895 | SESSION AUTHORIZATION DEFAULT
897 VariableSetStmt *n = makeNode(VariableSetStmt);
898 n->name = "session_authorization";
905 var_list { $$ = $1; }
906 | DEFAULT { $$ = NIL; }
909 var_list: var_value { $$ = makeList1($1); }
910 | var_list ',' var_value { $$ = lappend($1, $3); }
913 var_value: opt_boolean
914 { $$ = makeStringConst($1, NULL); }
916 { $$ = makeStringConst($1, NULL); }
918 { $$ = makeAConst($1); }
921 iso_level: READ COMMITTED { $$ = "read committed"; }
922 | SERIALIZABLE { $$ = "serializable"; }
929 elog(ERROR, "SET TRANSACTION/READ ONLY not yet supported");
936 TRUE_P { $$ = "true"; }
937 | FALSE_P { $$ = "false"; }
939 | OFF { $$ = "off"; }
942 /* Timezone values can be:
943 * - a string such as 'pst8pdt'
944 * - an identifier such as "pst8pdt"
945 * - an integer or floating point number
946 * - a time interval per SQL99
947 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
948 * so use IDENT and reject anything which is a reserved word.
953 $$ = makeStringConst($1, NULL);
957 $$ = makeStringConst($1, NULL);
959 | ConstInterval Sconst opt_interval
961 A_Const *n = (A_Const *) makeStringConst($2, $1);
962 if ($3 != INTERVAL_FULL_RANGE)
964 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
967 " must be HOUR or HOUR TO MINUTE");
968 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
972 | ConstInterval '(' Iconst ')' Sconst opt_interval
974 A_Const *n = (A_Const *) makeStringConst($5, $1);
975 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
977 "INTERVAL(%d) precision must be between %d and %d",
978 $3, 0, MAX_INTERVAL_PRECISION);
980 if (($6 != INTERVAL_FULL_RANGE)
981 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
984 " must be HOUR or HOUR TO MINUTE");
986 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
990 | NumericOnly { $$ = makeAConst($1); }
991 | DEFAULT { $$ = NULL; }
992 | LOCAL { $$ = NULL; }
997 | DEFAULT { $$ = NULL; }
998 | /*EMPTY*/ { $$ = NULL; }
1003 | SCONST { $$ = $1; }
1010 VariableShowStmt *n = makeNode(VariableShowStmt);
1016 VariableShowStmt *n = makeNode(VariableShowStmt);
1017 n->name = "timezone";
1020 | SHOW TRANSACTION ISOLATION LEVEL
1022 VariableShowStmt *n = makeNode(VariableShowStmt);
1023 n->name = "TRANSACTION ISOLATION LEVEL";
1026 | SHOW SESSION AUTHORIZATION
1028 VariableShowStmt *n = makeNode(VariableShowStmt);
1029 n->name = "session_authorization";
1034 VariableShowStmt *n = makeNode(VariableShowStmt);
1043 VariableResetStmt *n = makeNode(VariableResetStmt);
1049 VariableResetStmt *n = makeNode(VariableResetStmt);
1050 n->name = "timezone";
1053 | RESET TRANSACTION ISOLATION LEVEL
1055 VariableResetStmt *n = makeNode(VariableResetStmt);
1056 n->name = "TRANSACTION ISOLATION LEVEL";
1059 | RESET SESSION AUTHORIZATION
1061 VariableResetStmt *n = makeNode(VariableResetStmt);
1062 n->name = "session_authorization";
1067 VariableResetStmt *n = makeNode(VariableResetStmt);
1075 SET CONSTRAINTS constraints_set_list constraints_set_mode
1077 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1078 n->constraints = $3;
1084 constraints_set_list:
1086 | name_list { $$ = $1; }
1089 constraints_set_mode:
1090 DEFERRED { $$ = TRUE; }
1091 | IMMEDIATE { $$ = FALSE; }
1096 * Checkpoint statement
1101 CheckPointStmt *n = makeNode(CheckPointStmt);
1107 /*****************************************************************************
1109 * ALTER TABLE variations
1111 *****************************************************************************/
1114 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1115 ALTER TABLE relation_expr ADD opt_column columnDef
1117 AlterTableStmt *n = makeNode(AlterTableStmt);
1123 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1124 | ALTER TABLE relation_expr ALTER opt_column ColId alter_column_default
1126 AlterTableStmt *n = makeNode(AlterTableStmt);
1133 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1134 | ALTER TABLE relation_expr ALTER opt_column ColId DROP NOT NULL_P
1136 AlterTableStmt *n = makeNode(AlterTableStmt);
1142 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1143 | ALTER TABLE relation_expr ALTER opt_column ColId SET NOT NULL_P
1145 AlterTableStmt *n = makeNode(AlterTableStmt);
1151 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1152 | ALTER TABLE relation_expr ALTER opt_column ColId SET STATISTICS IntegerOnly
1154 AlterTableStmt *n = makeNode(AlterTableStmt);
1158 n->def = (Node *) $9;
1161 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1162 | ALTER TABLE relation_expr ALTER opt_column ColId
1165 AlterTableStmt *n = makeNode(AlterTableStmt);
1169 n->def = (Node *) makeString($9);
1172 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1173 | ALTER TABLE relation_expr DROP opt_column ColId opt_drop_behavior
1175 AlterTableStmt *n = makeNode(AlterTableStmt);
1182 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1183 | ALTER TABLE relation_expr ADD TableConstraint
1185 AlterTableStmt *n = makeNode(AlterTableStmt);
1191 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1192 | ALTER TABLE relation_expr DROP CONSTRAINT name opt_drop_behavior
1194 AlterTableStmt *n = makeNode(AlterTableStmt);
1201 /* ALTER TABLE <name> CREATE TOAST TABLE */
1202 | ALTER TABLE qualified_name CREATE TOAST TABLE
1204 AlterTableStmt *n = makeNode(AlterTableStmt);
1206 $3->inhOpt = INH_NO;
1210 /* ALTER TABLE <name> OWNER TO UserId */
1211 | ALTER TABLE qualified_name OWNER TO UserId
1213 AlterTableStmt *n = makeNode(AlterTableStmt);
1215 $3->inhOpt = INH_NO;
1222 alter_column_default:
1225 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1226 if (exprIsNullConstant($3))
1231 | DROP DEFAULT { $$ = NULL; }
1235 CASCADE { $$ = DROP_CASCADE; }
1236 | RESTRICT { $$ = DROP_RESTRICT; }
1237 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1242 /*****************************************************************************
1247 *****************************************************************************/
1252 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1258 opt_id: ColId { $$ = $1; }
1259 | /*EMPTY*/ { $$ = NULL; }
1263 /*****************************************************************************
1266 * COPY <relname> ['(' columnList ')'] FROM/TO [WITH options]
1268 * BINARY, OIDS, and DELIMITERS kept in old locations
1269 * for backward compatibility. 2002-06-18
1271 *****************************************************************************/
1273 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1274 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1276 CopyStmt *n = makeNode(CopyStmt);
1283 /* Concatenate user-supplied flags */
1285 n->options = lappend(n->options, $2);
1287 n->options = lappend(n->options, $5);
1289 n->options = lappend(n->options, $8);
1291 n->options = nconc(n->options, $10);
1298 | TO { $$ = FALSE; }
1302 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1303 * used depends on the direction. (It really doesn't make sense to copy from
1304 * stdout. We silently correct the "typo". - AY 9/94
1308 | STDIN { $$ = NULL; }
1309 | STDOUT { $$ = NULL; }
1315 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1316 | /* EMPTY */ { $$ = NIL; }
1323 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1327 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1329 | DELIMITER opt_as Sconst
1331 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1333 | NULL_P opt_as Sconst
1335 $$ = makeDefElem("null", (Node *)makeString($3));
1339 /* The following exist for backward compatibility */
1344 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1346 | /*EMPTY*/ { $$ = NULL; }
1352 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1354 | /*EMPTY*/ { $$ = NULL; }
1358 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1359 opt_using DELIMITERS Sconst
1361 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1363 | /*EMPTY*/ { $$ = NULL; }
1372 /*****************************************************************************
1375 * CREATE TABLE relname
1377 *****************************************************************************/
1379 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1380 OptInherit OptWithOids OnCommitOption
1382 CreateStmt *n = makeNode(CreateStmt);
1386 n->inhRelations = $8;
1387 n->constraints = NIL;
1392 | CREATE OptTemp TABLE qualified_name OF qualified_name
1393 '(' OptTableElementList ')' OptWithOids OnCommitOption
1395 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1396 * by our inheritance capabilities. Let's try it...
1398 CreateStmt *n = makeNode(CreateStmt);
1402 n->inhRelations = makeList1($6);
1403 n->constraints = NIL;
1411 * Redundancy here is needed to avoid shift/reduce conflicts,
1412 * since TEMP is not a reserved word. See also OptTempTableName.
1414 OptTemp: TEMPORARY { $$ = TRUE; }
1415 | TEMP { $$ = TRUE; }
1416 | LOCAL TEMPORARY { $$ = TRUE; }
1417 | LOCAL TEMP { $$ = TRUE; }
1421 "GLOBAL TEMPORARY TABLE is not currently supported");
1427 "GLOBAL TEMPORARY TABLE is not currently supported");
1430 | /*EMPTY*/ { $$ = FALSE; }
1433 OptTableElementList:
1434 TableElementList { $$ = $1; }
1435 | /*EMPTY*/ { $$ = NIL; }
1443 | TableElementList ',' TableElement
1445 $$ = lappend($1, $3);
1450 columnDef { $$ = $1; }
1451 | TableLikeClause { $$ = $1; }
1452 | TableConstraint { $$ = $1; }
1455 columnDef: ColId Typename ColQualList opt_collate
1457 ColumnDef *n = makeNode(ColumnDef);
1460 n->constraints = $3;
1465 "CREATE TABLE / COLLATE %s not yet implemented; "
1466 "clause ignored", $4);
1473 ColQualList ColConstraint { $$ = lappend($1, $2); }
1474 | /*EMPTY*/ { $$ = NIL; }
1478 CONSTRAINT name ColConstraintElem
1480 switch (nodeTag($3))
1484 Constraint *n = (Constraint *)$3;
1488 case T_FkConstraint:
1490 FkConstraint *n = (FkConstraint *)$3;
1491 n->constr_name = $2;
1499 | ColConstraintElem { $$ = $1; }
1500 | ConstraintAttr { $$ = $1; }
1503 /* DEFAULT NULL is already the default for Postgres.
1504 * But define it here and carry it forward into the system
1505 * to make it explicit.
1506 * - thomas 1998-09-13
1508 * WITH NULL and NULL are not SQL92-standard syntax elements,
1509 * so leave them out. Use DEFAULT NULL to explicitly indicate
1510 * that a column may have that value. WITH NULL leads to
1511 * shift/reduce conflicts with WITH TIME ZONE anyway.
1512 * - thomas 1999-01-08
1514 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1515 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1516 * or be part of a_expr NOT LIKE or similar constructs).
1521 Constraint *n = makeNode(Constraint);
1522 n->contype = CONSTR_NOTNULL;
1525 n->cooked_expr = NULL;
1531 Constraint *n = makeNode(Constraint);
1532 n->contype = CONSTR_NULL;
1535 n->cooked_expr = NULL;
1541 Constraint *n = makeNode(Constraint);
1542 n->contype = CONSTR_UNIQUE;
1545 n->cooked_expr = NULL;
1551 Constraint *n = makeNode(Constraint);
1552 n->contype = CONSTR_PRIMARY;
1555 n->cooked_expr = NULL;
1559 | CHECK '(' a_expr ')'
1561 Constraint *n = makeNode(Constraint);
1562 n->contype = CONSTR_CHECK;
1565 n->cooked_expr = NULL;
1571 Constraint *n = makeNode(Constraint);
1572 n->contype = CONSTR_DEFAULT;
1574 if (exprIsNullConstant($2))
1576 /* DEFAULT NULL should be reported as empty expr */
1583 n->cooked_expr = NULL;
1587 | REFERENCES qualified_name opt_column_list key_match key_actions
1589 FkConstraint *n = makeNode(FkConstraint);
1590 n->constr_name = NULL;
1594 n->fk_matchtype = $4;
1595 n->fk_upd_action = (char) ($5 >> 8);
1596 n->fk_del_action = (char) ($5 & 0xFF);
1597 n->deferrable = FALSE;
1598 n->initdeferred = FALSE;
1604 * ConstraintAttr represents constraint attributes, which we parse as if
1605 * they were independent constraint clauses, in order to avoid shift/reduce
1606 * conflicts (since NOT might start either an independent NOT NULL clause
1607 * or an attribute). analyze.c is responsible for attaching the attribute
1608 * information to the preceding "real" constraint node, and for complaining
1609 * if attribute clauses appear in the wrong place or wrong combinations.
1611 * See also ConstraintAttributeSpec, which can be used in places where
1612 * there is no parsing conflict.
1617 Constraint *n = makeNode(Constraint);
1618 n->contype = CONSTR_ATTR_DEFERRABLE;
1623 Constraint *n = makeNode(Constraint);
1624 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1627 | INITIALLY DEFERRED
1629 Constraint *n = makeNode(Constraint);
1630 n->contype = CONSTR_ATTR_DEFERRED;
1633 | INITIALLY IMMEDIATE
1635 Constraint *n = makeNode(Constraint);
1636 n->contype = CONSTR_ATTR_IMMEDIATE;
1642 /* SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
1643 * This seems to be a poor man's inheritance capability, with the resulting
1644 * tables completely decoupled except for the original commonality in definitions.
1645 * Seems to have much in common with CREATE TABLE AS. - thomas 2002-06-19
1647 TableLikeClause: LIKE any_name
1649 elog(ERROR, "LIKE in table definitions not yet supported");
1655 /* ConstraintElem specifies constraint syntax which is not embedded into
1656 * a column definition. ColConstraintElem specifies the embedded form.
1657 * - thomas 1997-12-03
1660 CONSTRAINT name ConstraintElem
1662 switch (nodeTag($3))
1666 Constraint *n = (Constraint *)$3;
1670 case T_FkConstraint:
1672 FkConstraint *n = (FkConstraint *)$3;
1673 n->constr_name = $2;
1681 | ConstraintElem { $$ = $1; }
1685 CHECK '(' a_expr ')'
1687 Constraint *n = makeNode(Constraint);
1688 n->contype = CONSTR_CHECK;
1691 n->cooked_expr = NULL;
1694 | UNIQUE '(' columnList ')'
1696 Constraint *n = makeNode(Constraint);
1697 n->contype = CONSTR_UNIQUE;
1700 n->cooked_expr = NULL;
1704 | PRIMARY KEY '(' columnList ')'
1706 Constraint *n = makeNode(Constraint);
1707 n->contype = CONSTR_PRIMARY;
1710 n->cooked_expr = NULL;
1714 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
1715 opt_column_list key_match key_actions ConstraintAttributeSpec
1717 FkConstraint *n = makeNode(FkConstraint);
1718 n->constr_name = NULL;
1722 n->fk_matchtype = $9;
1723 n->fk_upd_action = (char) ($10 >> 8);
1724 n->fk_del_action = (char) ($10 & 0xFF);
1725 n->deferrable = ($11 & 1) != 0;
1726 n->initdeferred = ($11 & 2) != 0;
1732 '(' columnList ')' { $$ = $2; }
1733 | /*EMPTY*/ { $$ = NIL; }
1737 columnElem { $$ = makeList1($1); }
1738 | columnList ',' columnElem { $$ = lappend($1, $3); }
1743 $$ = (Node *) makeString($1);
1747 key_match: MATCH FULL
1749 $$ = FKCONSTR_MATCH_FULL;
1753 elog(ERROR, "FOREIGN KEY/MATCH PARTIAL not yet implemented");
1754 $$ = FKCONSTR_MATCH_PARTIAL;
1758 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1762 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1767 * We combine the update and delete actions into one value temporarily
1768 * for simplicity of parsing, and then break them down again in the
1769 * calling production. update is in the left 8 bits, delete in the right.
1770 * Note that NOACTION is the default.
1774 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1776 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
1777 | key_update key_delete
1778 { $$ = ($1 << 8) | ($2 & 0xFF); }
1779 | key_delete key_update
1780 { $$ = ($2 << 8) | ($1 & 0xFF); }
1782 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1785 key_update: ON UPDATE key_action { $$ = $3; }
1788 key_delete: ON DELETE_P key_action { $$ = $3; }
1792 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
1793 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
1794 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
1795 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
1796 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
1799 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
1800 | /*EMPTY*/ { $$ = NIL; }
1804 WITH OIDS { $$ = TRUE; }
1805 | WITHOUT OIDS { $$ = FALSE; }
1806 | /*EMPTY*/ { $$ = TRUE; }
1809 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
1810 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
1811 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
1812 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
1817 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
1822 CREATE OptTemp TABLE qualified_name OptCreateAs AS SelectStmt
1825 * When the SelectStmt is a set-operation tree, we must
1826 * stuff the INTO information into the leftmost component
1827 * Select, because that's where analyze.c will expect
1828 * to find it. Similarly, the output column names must
1829 * be attached to that Select's target list.
1831 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
1832 if (n->into != NULL)
1833 elog(ERROR, "CREATE TABLE AS may not specify INTO");
1836 n->intoColNames = $5;
1842 '(' CreateAsList ')' { $$ = $2; }
1843 | /*EMPTY*/ { $$ = NIL; }
1847 CreateAsElement { $$ = makeList1($1); }
1848 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1854 ColumnDef *n = makeNode(ColumnDef);
1859 n->is_not_null = false;
1860 n->raw_default = NULL;
1861 n->cooked_default = NULL;
1862 n->constraints = NIL;
1869 /*****************************************************************************
1872 * CREATE SEQUENCE seqname
1874 *****************************************************************************/
1877 CREATE OptTemp SEQUENCE qualified_name OptSeqList
1879 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1887 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
1888 | /*EMPTY*/ { $$ = NIL; }
1891 OptSeqElem: CACHE NumericOnly
1893 $$ = makeDefElem("cache", (Node *)$2);
1897 $$ = makeDefElem("cycle", (Node *)true);
1901 $$ = makeDefElem("cycle", (Node *)false);
1903 | INCREMENT opt_by NumericOnly
1905 $$ = makeDefElem("increment", (Node *)$3);
1907 | MAXVALUE NumericOnly
1909 $$ = makeDefElem("maxvalue", (Node *)$2);
1911 | MINVALUE NumericOnly
1913 $$ = makeDefElem("minvalue", (Node *)$2);
1915 | START opt_with NumericOnly
1917 $$ = makeDefElem("start", (Node *)$3);
1926 FloatOnly { $$ = $1; }
1927 | IntegerOnly { $$ = $1; }
1930 FloatOnly: FCONST { $$ = makeFloat($1); }
1941 $$ = makeInteger($1);
1945 $$ = makeInteger($2);
1946 $$->val.ival = - $$->val.ival;
1950 /*****************************************************************************
1953 * CREATE PROCEDURAL LANGUAGE ...
1954 * DROP PROCEDURAL LANGUAGE ...
1956 *****************************************************************************/
1959 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
1960 HANDLER handler_name opt_validator opt_lancompiler
1962 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1965 n->plvalidator = $8;
1972 TRUSTED { $$ = TRUE; }
1973 | /*EMPTY*/ { $$ = FALSE; }
1976 /* This ought to be just func_name, but that causes reduce/reduce conflicts
1977 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
1978 * Work around by using name and dotted_name separately.
1982 { $$ = makeList1(makeString($1)); }
1983 | dotted_name { $$ = $1; }
1987 LANCOMPILER Sconst { $$ = $2; }
1988 | /*EMPTY*/ { $$ = ""; }
1992 VALIDATOR handler_name { $$ = $2; }
1993 | /*EMPTY*/ { $$ = NULL; }
1997 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
1999 DropPLangStmt *n = makeNode(DropPLangStmt);
2011 /*****************************************************************************
2014 * CREATE TRIGGER ...
2017 *****************************************************************************/
2020 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2021 qualified_name TriggerForSpec EXECUTE PROCEDURE
2022 func_name '(' TriggerFuncArgs ')'
2024 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2031 memcpy(n->actions, $5, 4);
2032 n->isconstraint = FALSE;
2033 n->deferrable = FALSE;
2034 n->initdeferred = FALSE;
2035 n->constrrel = NULL;
2038 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2039 qualified_name OptConstrFromTable
2040 ConstraintAttributeSpec
2041 FOR EACH ROW EXECUTE PROCEDURE
2042 func_name '(' TriggerFuncArgs ')'
2044 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2051 memcpy(n->actions, $6, 4);
2052 n->isconstraint = TRUE;
2053 n->deferrable = ($10 & 1) != 0;
2054 n->initdeferred = ($10 & 2) != 0;
2062 BEFORE { $$ = TRUE; }
2063 | AFTER { $$ = FALSE; }
2069 char *e = palloc(4);
2070 e[0] = $1; e[1] = '\0';
2073 | TriggerOneEvent OR TriggerOneEvent
2075 char *e = palloc(4);
2076 e[0] = $1; e[1] = $3; e[2] = '\0';
2079 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2081 char *e = palloc(4);
2082 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2088 INSERT { $$ = 'i'; }
2089 | DELETE_P { $$ = 'd'; }
2090 | UPDATE { $$ = 'u'; }
2094 FOR TriggerForOpt TriggerForType
2101 * If ROW/STATEMENT not specified, default to
2102 * STATEMENT, per SQL
2115 | STATEMENT { $$ = FALSE; }
2119 TriggerFuncArg { $$ = makeList1($1); }
2120 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2121 | /*EMPTY*/ { $$ = NIL; }
2128 snprintf(buf, sizeof(buf), "%d", $1);
2129 $$ = makeString(pstrdup(buf));
2131 | FCONST { $$ = makeString($1); }
2132 | Sconst { $$ = makeString($1); }
2133 | BCONST { $$ = makeString($1); }
2134 | XCONST { $$ = makeString($1); }
2135 | ColId { $$ = makeString($1); }
2139 FROM qualified_name { $$ = $2; }
2140 | /*EMPTY*/ { $$ = NULL; }
2143 ConstraintAttributeSpec:
2144 ConstraintDeferrabilitySpec
2146 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2148 if ($1 == 0 && $2 != 0)
2150 "INITIALLY DEFERRED constraint must be DEFERRABLE");
2153 | ConstraintTimeSpec
2160 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2162 if ($2 == 0 && $1 != 0)
2164 "INITIALLY DEFERRED constraint must be DEFERRABLE");
2171 ConstraintDeferrabilitySpec:
2172 NOT DEFERRABLE { $$ = 0; }
2173 | DEFERRABLE { $$ = 1; }
2177 INITIALLY IMMEDIATE { $$ = 0; }
2178 | INITIALLY DEFERRED { $$ = 2; }
2183 DROP TRIGGER name ON qualified_name opt_drop_behavior
2185 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2189 n->removeType = DROP_TRIGGER;
2195 /*****************************************************************************
2198 * CREATE ASSERTION ...
2199 * DROP ASSERTION ...
2201 *****************************************************************************/
2204 CREATE ASSERTION name CHECK '(' a_expr ')'
2205 ConstraintAttributeSpec
2207 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2209 n->args = makeList1($6);
2210 n->isconstraint = TRUE;
2211 n->deferrable = ($8 & 1) != 0;
2212 n->initdeferred = ($8 & 2) != 0;
2214 elog(ERROR, "CREATE ASSERTION is not yet supported");
2221 DROP ASSERTION name opt_drop_behavior
2223 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2227 n->removeType = DROP_TRIGGER; /* XXX */
2228 elog(ERROR, "DROP ASSERTION is not yet supported");
2234 /*****************************************************************************
2237 * define (aggregate,operator,type)
2239 *****************************************************************************/
2242 CREATE AGGREGATE func_name definition
2244 DefineStmt *n = makeNode(DefineStmt);
2245 n->defType = AGGREGATE;
2250 | CREATE OPERATOR any_operator definition
2252 DefineStmt *n = makeNode(DefineStmt);
2253 n->defType = OPERATOR;
2258 | CREATE TYPE_P any_name definition
2260 DefineStmt *n = makeNode(DefineStmt);
2261 n->defType = TYPE_P;
2266 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2268 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2269 RangeVar *r = makeNode(RangeVar);
2271 /* can't use qualified_name, sigh */
2275 r->catalogname = NULL;
2276 r->schemaname = NULL;
2277 r->relname = strVal(lfirst($3));
2280 r->catalogname = NULL;
2281 r->schemaname = strVal(lfirst($3));
2282 r->relname = strVal(lsecond($3));
2285 r->catalogname = strVal(lfirst($3));
2286 r->schemaname = strVal(lsecond($3));
2287 r->relname = strVal(lfirst(lnext(lnext($3))));
2291 "Improper qualified name (too many dotted names): %s",
2292 NameListToString($3));
2299 | CREATE CHARACTER SET opt_as any_name GET definition opt_collate
2301 DefineStmt *n = makeNode(DefineStmt);
2302 n->defType = CHARACTER;
2309 definition: '(' def_list ')' { $$ = $2; }
2312 def_list: def_elem { $$ = makeList1($1); }
2313 | def_list ',' def_elem { $$ = lappend($1, $3); }
2316 def_elem: ColLabel '=' def_arg
2318 $$ = makeDefElem($1, (Node *)$3);
2322 $$ = makeDefElem($1, (Node *)NULL);
2326 /* Note: any simple identifier will be returned as a type name! */
2327 def_arg: func_return { $$ = (Node *)$1; }
2328 | qual_all_Op { $$ = (Node *)$1; }
2329 | NumericOnly { $$ = (Node *)$1; }
2330 | Sconst { $$ = (Node *)makeString($1); }
2334 /*****************************************************************************
2337 * CREATE OPERATOR CLASS ...
2338 * DROP OPERATOR CLASS ...
2340 *****************************************************************************/
2343 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
2344 USING access_method AS opclass_item_list
2346 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
2347 n->opclassname = $4;
2357 opclass_item { $$ = makeList1($1); }
2358 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
2362 OPERATOR Iconst any_operator opt_recheck
2364 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2365 n->itemtype = OPCLASS_ITEM_OPERATOR;
2372 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
2374 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2375 n->itemtype = OPCLASS_ITEM_OPERATOR;
2382 | FUNCTION Iconst func_name func_args
2384 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2385 n->itemtype = OPCLASS_ITEM_FUNCTION;
2393 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2394 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
2400 opt_default: DEFAULT { $$ = TRUE; }
2401 | /*EMPTY*/ { $$ = FALSE; }
2404 opt_recheck: RECHECK { $$ = TRUE; }
2405 | /*EMPTY*/ { $$ = FALSE; }
2410 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
2412 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
2413 n->opclassname = $4;
2421 /*****************************************************************************
2425 * DROP itemtype itemname [, itemname ...] [ RESTRICT | CASCADE ]
2427 *****************************************************************************/
2429 DropStmt: DROP drop_type any_name_list opt_drop_behavior
2431 DropStmt *n = makeNode(DropStmt);
2439 drop_type: TABLE { $$ = DROP_TABLE; }
2440 | SEQUENCE { $$ = DROP_SEQUENCE; }
2441 | VIEW { $$ = DROP_VIEW; }
2442 | INDEX { $$ = DROP_INDEX; }
2443 | TYPE_P { $$ = DROP_TYPE; }
2444 | DOMAIN_P { $$ = DROP_DOMAIN; }
2445 | CONVERSION_P { $$ = DROP_CONVERSION; }
2446 | SCHEMA { $$ = DROP_SCHEMA; }
2450 any_name { $$ = makeList1($1); }
2451 | any_name_list ',' any_name { $$ = lappend($1, $3); }
2454 any_name: ColId { $$ = makeList1(makeString($1)); }
2455 | dotted_name { $$ = $1; }
2458 /*****************************************************************************
2461 * truncate table relname
2463 *****************************************************************************/
2466 TRUNCATE opt_table qualified_name
2468 TruncateStmt *n = makeNode(TruncateStmt);
2474 /*****************************************************************************
2476 * The COMMENT ON statement can take different forms based upon the type of
2477 * the object associated with the comment. The form of the statement is:
2479 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW ]
2480 * <objname> | AGGREGATE <aggname> (<aggtype>) | FUNCTION
2481 * <funcname> (arg1, arg2, ...) | OPERATOR <op>
2482 * (leftoperand_typ rightoperand_typ) | TRIGGER <triggername> ON
2483 * <relname> | RULE <rulename> ON <relname> ] IS 'text'
2485 *****************************************************************************/
2488 COMMENT ON comment_type any_name IS comment_text
2490 CommentStmt *n = makeNode(CommentStmt);
2497 | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')'
2500 CommentStmt *n = makeNode(CommentStmt);
2501 n->objtype = COMMENT_ON_AGGREGATE;
2503 n->objargs = makeList1($6);
2507 | COMMENT ON FUNCTION func_name func_args IS comment_text
2509 CommentStmt *n = makeNode(CommentStmt);
2510 n->objtype = COMMENT_ON_FUNCTION;
2516 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
2519 CommentStmt *n = makeNode(CommentStmt);
2520 n->objtype = COMMENT_ON_OPERATOR;
2526 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
2528 CommentStmt *n = makeNode(CommentStmt);
2529 n->objtype = COMMENT_ON_CONSTRAINT;
2530 n->objname = lappend($6, makeString($4));
2535 | COMMENT ON RULE name ON any_name IS comment_text
2537 CommentStmt *n = makeNode(CommentStmt);
2538 n->objtype = COMMENT_ON_RULE;
2539 n->objname = lappend($6, makeString($4));
2544 | COMMENT ON RULE name IS comment_text
2546 /* Obsolete syntax supported for awhile for compatibility */
2547 CommentStmt *n = makeNode(CommentStmt);
2548 n->objtype = COMMENT_ON_RULE;
2549 n->objname = makeList1(makeString($4));
2554 | COMMENT ON TRIGGER name ON any_name IS comment_text
2556 CommentStmt *n = makeNode(CommentStmt);
2557 n->objtype = COMMENT_ON_TRIGGER;
2558 n->objname = lappend($6, makeString($4));
2566 COLUMN { $$ = COMMENT_ON_COLUMN; }
2567 | DATABASE { $$ = COMMENT_ON_DATABASE; }
2568 | SCHEMA { $$ = COMMENT_ON_SCHEMA; }
2569 | INDEX { $$ = COMMENT_ON_INDEX; }
2570 | SEQUENCE { $$ = COMMENT_ON_SEQUENCE; }
2571 | TABLE { $$ = COMMENT_ON_TABLE; }
2572 | DOMAIN_P { $$ = COMMENT_ON_TYPE; }
2573 | TYPE_P { $$ = COMMENT_ON_TYPE; }
2574 | VIEW { $$ = COMMENT_ON_VIEW; }
2579 | NULL_P { $$ = NULL; }
2582 /*****************************************************************************
2585 * fetch/move [forward | backward] [ # | all ] [ in <portalname> ]
2586 * fetch [ forward | backward | absolute | relative ]
2587 * [ # | all | next | prior ] [ [ in | from ] <portalname> ]
2589 *****************************************************************************/
2591 FetchStmt: FETCH direction fetch_how_many from_in name
2593 FetchStmt *n = makeNode(FetchStmt);
2597 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2605 | FETCH fetch_how_many from_in name
2607 FetchStmt *n = makeNode(FetchStmt);
2611 n->direction = BACKWARD;
2615 n->direction = FORWARD;
2622 | FETCH direction from_in name
2624 FetchStmt *n = makeNode(FetchStmt);
2631 | FETCH from_in name
2633 FetchStmt *n = makeNode(FetchStmt);
2634 n->direction = FORWARD;
2642 FetchStmt *n = makeNode(FetchStmt);
2643 n->direction = FORWARD;
2649 | MOVE direction fetch_how_many from_in name
2651 FetchStmt *n = makeNode(FetchStmt);
2655 $2 = (($2 == FORWARD) ? BACKWARD: FORWARD);
2663 | MOVE fetch_how_many from_in name
2665 FetchStmt *n = makeNode(FetchStmt);
2669 n->direction = BACKWARD;
2673 n->direction = FORWARD;
2680 | MOVE direction from_in name
2682 FetchStmt *n = makeNode(FetchStmt);
2691 FetchStmt *n = makeNode(FetchStmt);
2692 n->direction = FORWARD;
2700 FetchStmt *n = makeNode(FetchStmt);
2701 n->direction = FORWARD;
2709 direction: FORWARD { $$ = FORWARD; }
2710 | BACKWARD { $$ = BACKWARD; }
2711 | RELATIVE { $$ = FORWARD; }
2715 "FETCH / ABSOLUTE not supported, using RELATIVE");
2722 | '-' Iconst { $$ = - $2; }
2723 | ALL { $$ = LONG_MAX; }
2724 | LAST { $$ = LONG_MAX; }
2726 | PRIOR { $$ = -1; }
2734 /*****************************************************************************
2736 * GRANT and REVOKE statements
2738 *****************************************************************************/
2740 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
2741 opt_grant_grant_option
2743 GrantStmt *n = makeNode(GrantStmt);
2746 n->objtype = ($4)->objtype;
2747 n->objects = ($4)->objs;
2753 RevokeStmt: REVOKE opt_revoke_grant_option privileges ON privilege_target
2756 GrantStmt *n = makeNode(GrantStmt);
2757 n->is_grant = false;
2759 n->objtype = ($5)->objtype;
2760 n->objects = ($5)->objs;
2767 /* either ALL [PRIVILEGES] or a list of individual privileges */
2768 privileges: privilege_list { $$ = $1; }
2769 | ALL { $$ = makeListi1(ACL_ALL_RIGHTS); }
2770 | ALL PRIVILEGES { $$ = makeListi1(ACL_ALL_RIGHTS); }
2774 privilege { $$ = makeListi1($1); }
2775 | privilege_list ',' privilege { $$ = lappendi($1, $3); }
2778 /* Not all of these privilege types apply to all objects, but that
2779 * gets sorted out later.
2781 privilege: SELECT { $$ = ACL_SELECT; }
2782 | INSERT { $$ = ACL_INSERT; }
2783 | UPDATE { $$ = ACL_UPDATE; }
2784 | DELETE_P { $$ = ACL_DELETE; }
2785 | RULE { $$ = ACL_RULE; }
2786 | REFERENCES { $$ = ACL_REFERENCES; }
2787 | TRIGGER { $$ = ACL_TRIGGER; }
2788 | EXECUTE { $$ = ACL_EXECUTE; }
2789 | USAGE { $$ = ACL_USAGE; }
2790 | CREATE { $$ = ACL_CREATE; }
2791 | TEMPORARY { $$ = ACL_CREATE_TEMP; }
2792 | TEMP { $$ = ACL_CREATE_TEMP; }
2796 /* Don't bother trying to fold the first two rules into one using
2797 opt_table. You're going to get conflicts. */
2801 PrivTarget *n = makeNode(PrivTarget);
2802 n->objtype = ACL_OBJECT_RELATION;
2806 | TABLE qualified_name_list
2808 PrivTarget *n = makeNode(PrivTarget);
2809 n->objtype = ACL_OBJECT_RELATION;
2813 | FUNCTION function_with_argtypes_list
2815 PrivTarget *n = makeNode(PrivTarget);
2816 n->objtype = ACL_OBJECT_FUNCTION;
2820 | DATABASE name_list
2822 PrivTarget *n = makeNode(PrivTarget);
2823 n->objtype = ACL_OBJECT_DATABASE;
2827 | LANGUAGE name_list
2829 PrivTarget *n = makeNode(PrivTarget);
2830 n->objtype = ACL_OBJECT_LANGUAGE;
2836 PrivTarget *n = makeNode(PrivTarget);
2837 n->objtype = ACL_OBJECT_NAMESPACE;
2845 grantee { $$ = makeList1($1); }
2846 | grantee_list ',' grantee { $$ = lappend($1, $3); }
2851 PrivGrantee *n = makeNode(PrivGrantee);
2852 /* This hack lets us avoid reserving PUBLIC as a keyword*/
2853 if (strcmp($1, "public") == 0)
2857 n->groupname = NULL;
2862 PrivGrantee *n = makeNode(PrivGrantee);
2863 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
2864 if (strcmp($2, "public") == 0)
2865 n->groupname = NULL;
2874 opt_grant_grant_option:
2877 elog(ERROR, "grant options are not implemented");
2882 opt_revoke_grant_option:
2885 elog(ERROR, "grant options are not implemented");
2891 function_with_argtypes_list:
2892 function_with_argtypes { $$ = makeList1($1); }
2893 | function_with_argtypes_list ',' function_with_argtypes
2894 { $$ = lappend($1, $3); }
2897 function_with_argtypes:
2900 FuncWithArgs *n = makeNode(FuncWithArgs);
2908 /*****************************************************************************
2911 * create index <indexname> on <relname>
2912 * [ using <access> ] "(" (<col> with <op>)+ ")"
2913 * [ where <predicate> ]
2915 *****************************************************************************/
2917 IndexStmt: CREATE index_opt_unique INDEX index_name ON qualified_name
2918 access_method_clause '(' index_params ')' where_clause
2920 IndexStmt *n = makeNode(IndexStmt);
2924 n->accessMethod = $7;
2925 n->indexParams = $9;
2926 n->whereClause = $11;
2932 UNIQUE { $$ = TRUE; }
2933 | /*EMPTY*/ { $$ = FALSE; }
2936 access_method_clause:
2937 USING access_method { $$ = $2; }
2938 /* If btree changes as our default, update pg_get_indexdef() */
2939 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
2943 index_list { $$ = $1; }
2944 | func_index { $$ = makeList1($1); }
2947 index_list: index_elem { $$ = makeList1($1); }
2948 | index_list ',' index_elem { $$ = lappend($1, $3); }
2951 func_index: func_name '(' name_list ')' opt_class
2953 $$ = makeNode(IndexElem);
2961 index_elem: attr_name opt_class
2963 $$ = makeNode(IndexElem);
2974 * Release 7.0 removed network_ops, timespan_ops, and
2975 * datetime_ops, so we suppress it from being passed to
2976 * the parser so the default *_ops is used. This can be
2977 * removed in some later release. bjm 2000/02/07
2979 * Release 7.1 removes lztext_ops, so suppress that too
2980 * for a while. tgl 2000/07/30
2982 * Release 7.2 renames timestamp_ops to timestamptz_ops,
2983 * so suppress that too for awhile. I'm starting to
2984 * think we need a better approach. tgl 2000/10/01
2986 if (length($1) == 1)
2988 char *claname = strVal(lfirst($1));
2990 if (strcmp(claname, "network_ops") != 0 &&
2991 strcmp(claname, "timespan_ops") != 0 &&
2992 strcmp(claname, "datetime_ops") != 0 &&
2993 strcmp(claname, "lztext_ops") != 0 &&
2994 strcmp(claname, "timestamp_ops") != 0)
3002 | USING any_name { $$ = $2; }
3003 | /*EMPTY*/ { $$ = NIL; }
3006 /*****************************************************************************
3009 * create [or replace] function <fname>
3010 * [(<type-1> { , <type-n>})]
3012 * as <filename or code in language as appropriate>
3013 * language <lang> [with parameters]
3015 *****************************************************************************/
3018 CREATE opt_or_replace FUNCTION func_name func_args
3019 RETURNS func_return createfunc_opt_list opt_definition
3021 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3033 OR REPLACE { $$ = TRUE; }
3034 | /*EMPTY*/ { $$ = FALSE; }
3037 func_args: '(' func_args_list ')' { $$ = $2; }
3038 | '(' ')' { $$ = NIL; }
3042 func_arg { $$ = makeList1($1); }
3043 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
3046 func_arg: opt_arg func_type
3048 /* We can catch over-specified arguments here if we want to,
3049 * but for now better to silently swallow typmod, etc.
3050 * - thomas 2000-03-22
3054 | func_type { $$ = $1; }
3057 opt_arg: IN_P { $$ = FALSE; }
3061 "CREATE FUNCTION / OUT parameters are not supported");
3067 "CREATE FUNCTION / INOUT parameters are not supported");
3075 /* We can catch over-specified arguments here if we want to,
3076 * but for now better to silently swallow typmod, etc.
3077 * - thomas 2000-03-22
3084 * We would like to make the second production here be ColId attrs etc,
3085 * but that causes reduce/reduce conflicts. type_name is next best choice.
3087 func_type: Typename { $$ = $1; }
3088 | type_name attrs '%' TYPE_P
3090 $$ = makeNode(TypeName);
3091 $$->names = lcons(makeString($1), $2);
3092 $$->pct_type = true;
3098 createfunc_opt_list:
3099 /* Must be at least one to prevent conflict */
3100 createfunc_opt_item { $$ = makeList1($1); }
3101 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
3104 createfunc_opt_item:
3107 $$ = makeDefElem("as", (Node *)$2);
3109 | LANGUAGE ColId_or_Sconst
3111 $$ = makeDefElem("language", (Node *)makeString($2));
3115 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
3119 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
3123 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
3125 | CALLED ON NULL_P INPUT
3127 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
3129 | RETURNS NULL_P ON NULL_P INPUT
3131 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3135 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3137 | EXTERNAL SECURITY DEFINER
3139 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3141 | EXTERNAL SECURITY INVOKER
3143 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3147 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3151 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3155 func_as: Sconst { $$ = makeList1(makeString($1)); }
3158 $$ = makeList2(makeString($1), makeString($3));
3163 WITH definition { $$ = $2; }
3164 | /*EMPTY*/ { $$ = NIL; }
3168 /*****************************************************************************
3172 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
3173 * DROP AGGREGATE aggname (aggtype) [ RESTRICT | CASCADE ]
3174 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
3176 *****************************************************************************/
3179 DROP FUNCTION func_name func_args opt_drop_behavior
3181 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
3190 DROP AGGREGATE func_name '(' aggr_argtype ')' opt_drop_behavior
3192 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
3201 Typename { $$ = $1; }
3202 | '*' { $$ = NULL; }
3206 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
3208 RemoveOperStmt *n = makeNode(RemoveOperStmt);
3219 elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
3221 | Typename ',' Typename
3222 { $$ = makeList2($1, $3); }
3223 | NONE ',' Typename /* left unary */
3224 { $$ = makeList2(NULL, $3); }
3225 | Typename ',' NONE /* right unary */
3226 { $$ = makeList2($1, NULL); }
3231 { $$ = makeList1(makeString($1)); }
3232 | ColId '.' any_operator
3233 { $$ = lcons(makeString($1), $3); }
3237 /*****************************************************************************
3239 * CREATE CAST / DROP CAST
3241 *****************************************************************************/
3243 CreateCastStmt: CREATE CAST '(' ConstTypename AS ConstTypename ')'
3244 WITH FUNCTION function_with_argtypes cast_context
3246 CreateCastStmt *n = makeNode(CreateCastStmt);
3249 n->func = (FuncWithArgs *) $10;
3250 n->context = (CoercionContext) $11;
3253 | CREATE CAST '(' ConstTypename AS ConstTypename ')'
3254 WITHOUT FUNCTION cast_context
3256 CreateCastStmt *n = makeNode(CreateCastStmt);
3260 n->context = (CoercionContext) $10;
3265 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
3266 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
3267 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
3271 DropCastStmt: DROP CAST '(' ConstTypename AS ConstTypename ')' opt_drop_behavior
3273 DropCastStmt *n = makeNode(DropCastStmt);
3283 /*****************************************************************************
3287 * REINDEX type <typename> [FORCE] [ALL]
3289 *****************************************************************************/
3292 REINDEX reindex_type qualified_name opt_force
3294 ReindexStmt *n = makeNode(ReindexStmt);
3295 n->reindexType = $2;
3301 | REINDEX DATABASE name opt_force
3303 ReindexStmt *n = makeNode(ReindexStmt);
3304 n->reindexType = DATABASE;
3313 INDEX { $$ = INDEX; }
3314 | TABLE { $$ = TABLE; }
3317 opt_force: FORCE { $$ = TRUE; }
3318 | /* EMPTY */ { $$ = FALSE; }
3322 /*****************************************************************************
3325 * rename <attrname1> in <relname> [*] to <attrname2>
3326 * rename <relname1> to <relname2>
3328 *****************************************************************************/
3330 RenameStmt: ALTER TABLE relation_expr RENAME opt_column opt_name TO name
3332 RenameStmt *n = makeNode(RenameStmt);
3337 n->renameType = RENAME_TABLE;
3339 n->renameType = RENAME_COLUMN;
3342 | ALTER TRIGGER name ON relation_expr RENAME TO name
3344 RenameStmt *n = makeNode(RenameStmt);
3348 n->renameType = RENAME_TRIGGER;
3353 opt_name: name { $$ = $1; }
3354 | /*EMPTY*/ { $$ = NULL; }
3357 opt_column: COLUMN { $$ = COLUMN; }
3358 | /*EMPTY*/ { $$ = 0; }
3362 /*****************************************************************************
3364 * QUERY: Define Rewrite Rule
3366 *****************************************************************************/
3368 RuleStmt: CREATE opt_or_replace RULE name AS
3369 { QueryIsRule=TRUE; }
3370 ON event TO qualified_name where_clause
3371 DO opt_instead RuleActionList
3373 RuleStmt *n = makeNode(RuleStmt);
3377 n->whereClause = $11;
3387 NOTHING { $$ = NIL; }
3388 | RuleActionStmt { $$ = makeList1($1); }
3389 | '(' RuleActionMulti ')' { $$ = $2; }
3392 /* the thrashing around here is to discard "empty" statements... */
3394 RuleActionMulti ';' RuleActionStmtOrEmpty
3395 { if ($3 != (Node *) NULL)
3396 $$ = lappend($1, $3);
3400 | RuleActionStmtOrEmpty
3401 { if ($1 != (Node *) NULL)
3416 RuleActionStmtOrEmpty:
3417 RuleActionStmt { $$ = $1; }
3418 | /*EMPTY*/ { $$ = (Node *)NULL; }
3421 /* change me to select, update, etc. some day */
3422 event: SELECT { $$ = CMD_SELECT; }
3423 | UPDATE { $$ = CMD_UPDATE; }
3424 | DELETE_P { $$ = CMD_DELETE; }
3425 | INSERT { $$ = CMD_INSERT; }
3429 INSTEAD { $$ = TRUE; }
3430 | /*EMPTY*/ { $$ = FALSE; }
3435 DROP RULE name ON qualified_name opt_drop_behavior
3437 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3441 n->removeType = DROP_RULE;
3447 /*****************************************************************************
3450 * NOTIFY <qualified_name> can appear both in rule bodies and
3451 * as a query-level command
3453 *****************************************************************************/
3455 NotifyStmt: NOTIFY qualified_name
3457 NotifyStmt *n = makeNode(NotifyStmt);
3463 ListenStmt: LISTEN qualified_name
3465 ListenStmt *n = makeNode(ListenStmt);
3472 UNLISTEN qualified_name
3474 UnlistenStmt *n = makeNode(UnlistenStmt);
3480 UnlistenStmt *n = makeNode(UnlistenStmt);
3481 n->relation = makeNode(RangeVar);
3482 n->relation->relname = "*";
3483 n->relation->schemaname = NULL;
3489 /*****************************************************************************
3493 * BEGIN / COMMIT / ROLLBACK
3494 * (also older versions END / ABORT)
3496 *****************************************************************************/
3499 ABORT_TRANS opt_trans
3501 TransactionStmt *n = makeNode(TransactionStmt);
3502 n->command = ROLLBACK;
3506 | BEGIN_TRANS opt_trans
3508 TransactionStmt *n = makeNode(TransactionStmt);
3509 n->command = BEGIN_TRANS;
3513 | START TRANSACTION trans_options
3515 TransactionStmt *n = makeNode(TransactionStmt);
3522 TransactionStmt *n = makeNode(TransactionStmt);
3523 n->command = COMMIT;
3527 | END_TRANS opt_trans
3529 TransactionStmt *n = makeNode(TransactionStmt);
3530 n->command = COMMIT;
3534 | ROLLBACK opt_trans
3536 TransactionStmt *n = makeNode(TransactionStmt);
3537 n->command = ROLLBACK;
3543 trans_options: ISOLATION LEVEL iso_level
3544 { $$ = makeList1(makeStringConst($3, NULL)); }
3545 | /* EMPTY */ { $$ = NIL; }
3553 /*****************************************************************************
3556 * create view <viewname> '('target-list ')' AS <query>
3558 *****************************************************************************/
3560 ViewStmt: CREATE opt_or_replace VIEW qualified_name opt_column_list
3563 ViewStmt *n = makeNode(ViewStmt);
3567 n->query = (Query *) $7;
3573 /*****************************************************************************
3578 *****************************************************************************/
3580 LoadStmt: LOAD file_name
3582 LoadStmt *n = makeNode(LoadStmt);
3589 /*****************************************************************************
3593 *****************************************************************************/
3596 CREATE DATABASE database_name opt_with createdb_opt_list
3598 CreatedbStmt *n = makeNode(CreatedbStmt);
3606 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
3607 | /* EMPTY */ { $$ = NIL; }
3611 LOCATION opt_equal Sconst
3613 $$ = makeDefElem("location", (Node *)makeString($3));
3615 | LOCATION opt_equal DEFAULT
3617 $$ = makeDefElem("location", NULL);
3619 | TEMPLATE opt_equal name
3621 $$ = makeDefElem("template", (Node *)makeString($3));
3623 | TEMPLATE opt_equal DEFAULT
3625 $$ = makeDefElem("template", NULL);
3627 | ENCODING opt_equal Sconst
3629 $$ = makeDefElem("encoding", (Node *)makeString($3));
3631 | ENCODING opt_equal Iconst
3633 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
3635 | ENCODING opt_equal DEFAULT
3637 $$ = makeDefElem("encoding", NULL);
3639 | OWNER opt_equal name
3641 $$ = makeDefElem("owner", (Node *)makeString($3));
3643 | OWNER opt_equal DEFAULT
3645 $$ = makeDefElem("owner", NULL);
3650 * Though the equals sign doesn't match other WITH options, pg_dump uses
3651 * equals for backward compability, and it doesn't seem worth removing it.
3659 /*****************************************************************************
3663 *****************************************************************************/
3665 AlterDatabaseSetStmt:
3666 ALTER DATABASE database_name SET set_rest
3668 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3670 n->variable = $5->name;
3671 n->value = $5->args;
3674 | ALTER DATABASE database_name VariableResetStmt
3676 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
3678 n->variable = ((VariableResetStmt *)$4)->name;
3685 /*****************************************************************************
3689 * This is implicitly CASCADE, no need for drop behavior
3690 *****************************************************************************/
3692 DropdbStmt: DROP DATABASE database_name
3694 DropdbStmt *n = makeNode(DropdbStmt);
3701 /*****************************************************************************
3703 * Manipulate a domain
3705 *****************************************************************************/
3708 CREATE DOMAIN_P any_name opt_as Typename ColQualList opt_collate
3710 CreateDomainStmt *n = makeNode(CreateDomainStmt);
3713 n->constraints = $6;
3716 elog(NOTICE,"CREATE DOMAIN / COLLATE %s not yet "
3717 "implemented; clause ignored", $7);
3723 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
3724 ALTER DOMAIN_P any_name alter_column_default
3726 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3732 /* ALTER DOMAIN <domain> DROP NOT NULL */
3733 | ALTER DOMAIN_P any_name DROP NOT NULL_P
3735 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3740 /* ALTER DOMAIN <domain> SET NOT NULL */
3741 | ALTER DOMAIN_P any_name SET NOT NULL_P
3743 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3748 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
3749 | ALTER DOMAIN_P any_name ADD TableConstraint
3751 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3757 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
3758 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
3760 AlterDomainStmt *n = makeNode(AlterDomainStmt);
3774 /*****************************************************************************
3776 * Manipulate a conversion
3778 * CREATE [DEFAULT] CONVERSION <conversion_name>
3779 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
3781 *****************************************************************************/
3783 CreateConversionStmt:
3784 CREATE opt_default CONVERSION_P any_name FOR Sconst
3785 TO Sconst FROM any_name
3787 CreateConversionStmt *n = makeNode(CreateConversionStmt);
3788 n->conversion_name = $4;
3789 n->for_encoding_name = $6;
3790 n->to_encoding_name = $8;
3797 /*****************************************************************************
3800 * cluster <index_name> on <qualified_name>
3801 * cluster <qualified_name>
3804 *****************************************************************************/
3807 CLUSTER index_name ON qualified_name
3809 ClusterStmt *n = makeNode(ClusterStmt);
3814 | CLUSTER qualified_name
3816 ClusterStmt *n = makeNode(ClusterStmt);
3818 n->indexname = NULL;
3823 ClusterStmt *n = makeNode(ClusterStmt);
3825 n->indexname = NULL;
3830 /*****************************************************************************
3836 *****************************************************************************/
3838 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
3840 VacuumStmt *n = makeNode(VacuumStmt);
3850 | VACUUM opt_full opt_freeze opt_verbose qualified_name
3852 VacuumStmt *n = makeNode(VacuumStmt);
3862 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
3864 VacuumStmt *n = (VacuumStmt *) $5;
3874 analyze_keyword opt_verbose
3876 VacuumStmt *n = makeNode(VacuumStmt);
3886 | analyze_keyword opt_verbose qualified_name opt_name_list
3888 VacuumStmt *n = makeNode(VacuumStmt);
3902 | ANALYSE /* British */ {}
3906 VERBOSE { $$ = TRUE; }
3907 | /*EMPTY*/ { $$ = FALSE; }
3910 opt_full: FULL { $$ = TRUE; }
3911 | /*EMPTY*/ { $$ = FALSE; }
3914 opt_freeze: FREEZE { $$ = TRUE; }
3915 | /*EMPTY*/ { $$ = FALSE; }
3919 '(' name_list ')' { $$ = $2; }
3920 | /*EMPTY*/ { $$ = NIL; }
3924 /*****************************************************************************
3928 * EXPLAIN ANALYZE query
3930 *****************************************************************************/
3933 EXPLAIN opt_verbose OptimizableStmt
3935 ExplainStmt *n = makeNode(ExplainStmt);
3938 n->query = (Query*)$3;
3941 | EXPLAIN analyze_keyword opt_verbose OptimizableStmt
3943 ExplainStmt *n = makeNode(ExplainStmt);
3946 n->query = (Query*)$4;
3951 /*****************************************************************************
3954 * PREPARE <plan_name> [(args, ...)] AS <query>
3956 *****************************************************************************/
3958 PrepareStmt: PREPARE name prep_type_clause AS OptimizableStmt
3960 PrepareStmt *n = makeNode(PrepareStmt);
3963 n->query = (Query *) $5;
3968 prep_type_clause: '(' prep_type_list ')' { $$ = $2; }
3969 | /* EMPTY */ { $$ = NIL; }
3972 prep_type_list: Typename { $$ = makeList1($1); }
3973 | prep_type_list ',' Typename
3974 { $$ = lappend($1, $3); }
3977 /*****************************************************************************
3980 * EXECUTE <plan_name> [(params, ...)] [INTO ...]
3982 *****************************************************************************/
3984 ExecuteStmt: EXECUTE name execute_param_clause into_clause
3986 ExecuteStmt *n = makeNode(ExecuteStmt);
3994 execute_param_clause: '(' expr_list ')' { $$ = $2; }
3995 | /* EMPTY */ { $$ = NIL; }
3998 /*****************************************************************************
4001 * DEALLOCATE [PREPARE] <plan_name>
4003 *****************************************************************************/
4005 DeallocateStmt: DEALLOCATE name
4007 DeallocateStmt *n = makeNode(DeallocateStmt);
4011 | DEALLOCATE PREPARE name
4013 DeallocateStmt *n = makeNode(DeallocateStmt);
4019 /*****************************************************************************
4021 * Optimizable Stmts: *
4023 * one of the five queries processed by the planner *
4025 * [ultimately] produces query-trees as specified *
4026 * in the query-spec document in ~postgres/ref *
4028 *****************************************************************************/
4035 | DeleteStmt /* by default all are $$=$1 */
4039 /*****************************************************************************
4044 *****************************************************************************/
4047 INSERT INTO qualified_name insert_rest
4055 VALUES '(' insert_target_list ')'
4057 $$ = makeNode(InsertStmt);
4059 $$->targetList = $3;
4060 $$->selectStmt = NULL;
4064 $$ = makeNode(InsertStmt);
4066 $$->targetList = NIL;
4067 $$->selectStmt = NULL;
4071 $$ = makeNode(InsertStmt);
4073 $$->targetList = NIL;
4074 $$->selectStmt = $1;
4076 | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
4078 $$ = makeNode(InsertStmt);
4080 $$->targetList = $6;
4081 $$->selectStmt = NULL;
4083 | '(' insert_column_list ')' SelectStmt
4085 $$ = makeNode(InsertStmt);
4087 $$->targetList = NIL;
4088 $$->selectStmt = $4;
4093 insert_column_item { $$ = makeList1($1); }
4094 | insert_column_list ',' insert_column_item
4095 { $$ = lappend($1, $3); }
4099 ColId opt_indirection
4101 ResTarget *n = makeNode(ResTarget);
4103 n->indirection = $2;
4110 /*****************************************************************************
4115 *****************************************************************************/
4117 DeleteStmt: DELETE_P FROM relation_expr where_clause
4119 DeleteStmt *n = makeNode(DeleteStmt);
4121 n->whereClause = $4;
4126 LockStmt: LOCK_P opt_table qualified_name_list opt_lock
4128 LockStmt *n = makeNode(LockStmt);
4136 opt_lock: IN_P lock_type MODE { $$ = $2; }
4137 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
4140 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
4141 | ROW SHARE { $$ = RowShareLock; }
4142 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
4143 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
4144 | SHARE { $$ = ShareLock; }
4145 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
4146 | EXCLUSIVE { $$ = ExclusiveLock; }
4147 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
4151 /*****************************************************************************
4154 * UpdateStmt (UPDATE)
4156 *****************************************************************************/
4158 UpdateStmt: UPDATE relation_expr
4159 SET update_target_list
4163 UpdateStmt *n = makeNode(UpdateStmt);
4167 n->whereClause = $6;
4173 /*****************************************************************************
4178 *****************************************************************************/
4179 CursorStmt: DECLARE name opt_cursor CURSOR FOR SelectStmt
4181 SelectStmt *n = (SelectStmt *)$6;
4188 opt_cursor: BINARY { $$ = TRUE; }
4189 | INSENSITIVE { $$ = FALSE; }
4190 | SCROLL { $$ = FALSE; }
4191 | INSENSITIVE SCROLL { $$ = FALSE; }
4192 | /*EMPTY*/ { $$ = FALSE; }
4195 /*****************************************************************************
4200 *****************************************************************************/
4202 /* A complete SELECT statement looks like this.
4204 * The rule returns either a single SelectStmt node or a tree of them,
4205 * representing a set-operation tree.
4207 * There is an ambiguity when a sub-SELECT is within an a_expr and there
4208 * are excess parentheses: do the parentheses belong to the sub-SELECT or
4209 * to the surrounding a_expr? We don't really care, but yacc wants to know.
4210 * To resolve the ambiguity, we are careful to define the grammar so that
4211 * the decision is staved off as long as possible: as long as we can keep
4212 * absorbing parentheses into the sub-SELECT, we will do so, and only when
4213 * it's no longer possible to do that will we decide that parens belong to
4214 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
4215 * parentheses are treated as part of the sub-select. The necessity of doing
4216 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
4217 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
4218 * SELECT viewpoint when we see the UNION.
4220 * This approach is implemented by defining a nonterminal select_with_parens,
4221 * which represents a SELECT with at least one outer layer of parentheses,
4222 * and being careful to use select_with_parens, never '(' SelectStmt ')',
4223 * in the expression grammar. We will then have shift-reduce conflicts
4224 * which we can resolve in favor of always treating '(' <select> ')' as
4225 * a select_with_parens. To resolve the conflicts, the productions that
4226 * conflict with the select_with_parens productions are manually given
4227 * precedences lower than the precedence of ')', thereby ensuring that we
4228 * shift ')' (and then reduce to select_with_parens) rather than trying to
4229 * reduce the inner <select> nonterminal to something else. We use UMINUS
4230 * precedence for this, which is a fairly arbitrary choice.
4232 * To be able to define select_with_parens itself without ambiguity, we need
4233 * a nonterminal select_no_parens that represents a SELECT structure with no
4234 * outermost parentheses. This is a little bit tedious, but it works.
4236 * In non-expression contexts, we use SelectStmt which can represent a SELECT
4237 * with or without outer parentheses.
4240 SelectStmt: select_no_parens %prec UMINUS
4241 | select_with_parens %prec UMINUS
4245 '(' select_no_parens ')' { $$ = $2; }
4246 | '(' select_with_parens ')' { $$ = $2; }
4250 * FOR UPDATE may be before or after LIMIT/OFFSET.
4251 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
4252 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE
4256 simple_select { $$ = $1; }
4257 | select_clause sort_clause
4259 insertSelectOptions((SelectStmt *) $1, $2, NIL,
4263 | select_clause opt_sort_clause for_update_clause opt_select_limit
4265 insertSelectOptions((SelectStmt *) $1, $2, $3,
4266 nth(0, $4), nth(1, $4));
4269 | select_clause opt_sort_clause select_limit opt_for_update_clause
4271 insertSelectOptions((SelectStmt *) $1, $2, $4,
4272 nth(0, $3), nth(1, $3));
4278 simple_select { $$ = $1; }
4279 | select_with_parens { $$ = $1; }
4283 * This rule parses SELECT statements that can appear within set operations,
4284 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
4285 * the ordering of the set operations. Without '(' and ')' we want the
4286 * operations to be ordered per the precedence specs at the head of this file.
4288 * As with select_no_parens, simple_select cannot have outer parentheses,
4289 * but can have parenthesized subclauses.
4291 * Note that sort clauses cannot be included at this level --- SQL92 requires
4292 * SELECT foo UNION SELECT bar ORDER BY baz
4294 * (SELECT foo UNION SELECT bar) ORDER BY baz
4296 * SELECT foo UNION (SELECT bar ORDER BY baz)
4297 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
4298 * as part of the select_no_parens production, not simple_select.
4299 * This does not limit functionality, because you can reintroduce sort and
4300 * limit clauses inside parentheses.
4302 * NOTE: only the leftmost component SelectStmt should have INTO.
4303 * However, this is not checked by the grammar; parse analysis must check it.
4306 SELECT opt_distinct target_list
4307 into_clause from_clause where_clause
4308 group_clause having_clause
4310 SelectStmt *n = makeNode(SelectStmt);
4311 n->distinctClause = $2;
4314 n->intoColNames = NIL;
4316 n->whereClause = $6;
4317 n->groupClause = $7;
4318 n->havingClause = $8;
4321 | select_clause UNION opt_all select_clause
4323 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
4325 | select_clause INTERSECT opt_all select_clause
4327 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
4329 | select_clause EXCEPT opt_all select_clause
4331 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
4336 INTO OptTempTableName { $$ = $2; }
4337 | /*EMPTY*/ { $$ = NULL; }
4341 * Redundancy here is needed to avoid shift/reduce conflicts,
4342 * since TEMP is not a reserved word. See also OptTemp.
4345 TEMPORARY opt_table qualified_name
4350 | TEMP opt_table qualified_name
4355 | LOCAL TEMPORARY opt_table qualified_name
4360 | LOCAL TEMP opt_table qualified_name
4365 | GLOBAL TEMPORARY opt_table qualified_name
4368 "GLOBAL TEMPORARY TABLE is not currently supported");
4372 | GLOBAL TEMP opt_table qualified_name
4375 "GLOBAL TEMPORARY TABLE is not currently supported");
4379 | TABLE qualified_name
4395 opt_all: ALL { $$ = TRUE; }
4396 | DISTINCT { $$ = FALSE; }
4397 | /*EMPTY*/ { $$ = FALSE; }
4400 /* We use (NIL) as a placeholder to indicate that all target expressions
4401 * should be placed in the DISTINCT list during parsetree analysis.
4404 DISTINCT { $$ = makeList1(NIL); }
4405 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
4407 | /*EMPTY*/ { $$ = NIL; }
4411 sort_clause { $$ = $1;}
4412 | /*EMPTY*/ { $$ = NIL; }
4416 ORDER BY sortby_list { $$ = $3; }
4420 sortby { $$ = makeList1($1); }
4421 | sortby_list ',' sortby { $$ = lappend($1, $3); }
4424 sortby: a_expr OptUseOp
4426 $$ = makeNode(SortGroupBy);
4432 OptUseOp: USING qual_all_Op { $$ = $2; }
4434 { $$ = makeList1(makeString("<")); }
4436 { $$ = makeList1(makeString(">")); }
4438 { $$ = makeList1(makeString("<")); /*default*/ }
4443 LIMIT select_limit_value OFFSET select_offset_value
4444 { $$ = makeList2($4, $2); }
4445 | OFFSET select_offset_value LIMIT select_limit_value
4446 { $$ = makeList2($2, $4); }
4447 | LIMIT select_limit_value
4448 { $$ = makeList2(NULL, $2); }
4449 | OFFSET select_offset_value
4450 { $$ = makeList2($2, NULL); }
4451 | LIMIT select_limit_value ',' select_offset_value
4452 /* Disabled because it was too confusing, bjm 2002-02-18 */
4454 "LIMIT #,# syntax not supported.\n\tUse separate LIMIT and OFFSET clauses."); }
4459 select_limit { $$ = $1; }
4461 { $$ = makeList2(NULL,NULL); }
4467 Const *n = makeNode(Const);
4470 elog(ERROR, "LIMIT must not be negative");
4472 n->consttype = INT4OID;
4473 n->constlen = sizeof(int4);
4474 n->constvalue = Int32GetDatum($1);
4475 n->constisnull = FALSE;
4476 n->constbyval = TRUE;
4481 /* LIMIT ALL is represented as a NULL constant */
4482 Const *n = makeNode(Const);
4484 n->consttype = INT4OID;
4485 n->constlen = sizeof(int4);
4486 n->constvalue = (Datum) 0;
4487 n->constisnull = TRUE;
4488 n->constbyval = TRUE;
4493 Param *n = makeNode(Param);
4495 n->paramkind = PARAM_NUM;
4497 n->paramtype = INT4OID;
4502 select_offset_value:
4505 Const *n = makeNode(Const);
4508 elog(ERROR, "OFFSET must not be negative");
4510 n->consttype = INT4OID;
4511 n->constlen = sizeof(int4);
4512 n->constvalue = Int32GetDatum($1);
4513 n->constisnull = FALSE;
4514 n->constbyval = TRUE;
4519 Param *n = makeNode(Param);
4521 n->paramkind = PARAM_NUM;
4523 n->paramtype = INT4OID;
4529 * jimmy bell-style recursive queries aren't supported in the
4532 * ...however, recursive addattr and rename supported. make special
4537 GROUP_P BY expr_list { $$ = $3; }
4538 | /*EMPTY*/ { $$ = NIL; }
4542 HAVING a_expr { $$ = $2; }
4543 | /*EMPTY*/ { $$ = NULL; }
4547 FOR UPDATE update_list { $$ = $3; }
4548 | FOR READ ONLY { $$ = NULL; }
4551 opt_for_update_clause:
4552 for_update_clause { $$ = $1; }
4553 | /* EMPTY */ { $$ = NULL; }
4557 OF name_list { $$ = $2; }
4558 | /* EMPTY */ { $$ = makeList1(NULL); }
4561 /*****************************************************************************
4563 * clauses common to all Optimizable Stmts:
4564 * from_clause - allow list of both JOIN expressions and table names
4565 * where_clause - qualifications for joins or restrictions
4567 *****************************************************************************/
4570 FROM from_list { $$ = $2; }
4571 | /*EMPTY*/ { $$ = NIL; }
4575 table_ref { $$ = makeList1($1); }
4576 | from_list ',' table_ref { $$ = lappend($1, $3); }
4580 * table_ref is where an alias clause can be attached. Note we cannot make
4581 * alias_clause have an empty production because that causes parse conflicts
4582 * between table_ref := '(' joined_table ')' alias_clause
4583 * and joined_table := '(' joined_table ')'. So, we must have the
4584 * redundant-looking productions here instead.
4586 table_ref: relation_expr
4590 | relation_expr alias_clause
4597 RangeFunction *n = makeNode(RangeFunction);
4598 n->funccallnode = $1;
4599 n->coldeflist = NIL;
4602 | func_table alias_clause
4604 RangeFunction *n = makeNode(RangeFunction);
4605 n->funccallnode = $1;
4607 n->coldeflist = NIL;
4610 | func_table AS '(' TableFuncElementList ')'
4612 RangeFunction *n = makeNode(RangeFunction);
4613 n->funccallnode = $1;
4617 | func_table AS ColId '(' TableFuncElementList ')'
4619 RangeFunction *n = makeNode(RangeFunction);
4620 Alias *a = makeNode(Alias);
4621 n->funccallnode = $1;
4627 | func_table ColId '(' TableFuncElementList ')'
4629 RangeFunction *n = makeNode(RangeFunction);
4630 Alias *a = makeNode(Alias);
4631 n->funccallnode = $1;
4637 | select_with_parens
4640 * The SQL spec does not permit a subselect
4641 * (<derived_table>) without an alias clause,
4642 * so we don't either. This avoids the problem
4643 * of needing to invent a unique refname for it.
4644 * That could be surmounted if there's sufficient
4645 * popular demand, but for now let's just implement
4646 * the spec and see if anyone complains.
4647 * However, it does seem like a good idea to emit
4648 * an error message that's better than "parse error".
4650 elog(ERROR, "sub-SELECT in FROM must have an alias"
4651 "\n\tFor example, FROM (SELECT ...) [AS] foo");
4654 | select_with_parens alias_clause
4656 RangeSubselect *n = makeNode(RangeSubselect);
4665 | '(' joined_table ')' alias_clause
4674 * It may seem silly to separate joined_table from table_ref, but there is
4675 * method in SQL92's madness: if you don't do it this way you get reduce-
4676 * reduce conflicts, because it's not clear to the parser generator whether
4677 * to expect alias_clause after ')' or not. For the same reason we must
4678 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
4679 * join_type to expand to empty; if we try it, the parser generator can't
4680 * figure out when to reduce an empty join_type right after table_ref.
4682 * Note that a CROSS JOIN is the same as an unqualified
4683 * INNER JOIN, and an INNER JOIN/ON has the same shape
4684 * but a qualification expression to limit membership.
4685 * A NATURAL JOIN implicitly matches column names between
4686 * tables and the shape is determined by which columns are
4687 * in common. We'll collect columns during the later transformations.
4691 '(' joined_table ')'
4695 | table_ref CROSS JOIN table_ref
4697 /* CROSS JOIN is same as unqualified inner join */
4698 JoinExpr *n = makeNode(JoinExpr);
4699 n->jointype = JOIN_INNER;
4700 n->isNatural = FALSE;
4707 | table_ref UNIONJOIN table_ref
4709 /* UNION JOIN is made into 1 token to avoid shift/reduce
4710 * conflict against regular UNION keyword.
4712 JoinExpr *n = makeNode(JoinExpr);
4713 n->jointype = JOIN_UNION;
4714 n->isNatural = FALSE;
4721 | table_ref join_type JOIN table_ref join_qual
4723 JoinExpr *n = makeNode(JoinExpr);
4725 n->isNatural = FALSE;
4728 if ($5 != NULL && IsA($5, List))
4729 n->using = (List *) $5; /* USING clause */
4731 n->quals = $5; /* ON clause */
4734 | table_ref JOIN table_ref join_qual
4736 /* letting join_type reduce to empty doesn't work */
4737 JoinExpr *n = makeNode(JoinExpr);
4738 n->jointype = JOIN_INNER;
4739 n->isNatural = FALSE;
4742 if ($4 != NULL && IsA($4, List))
4743 n->using = (List *) $4; /* USING clause */
4745 n->quals = $4; /* ON clause */
4748 | table_ref NATURAL join_type JOIN table_ref
4750 JoinExpr *n = makeNode(JoinExpr);
4752 n->isNatural = TRUE;
4755 n->using = NIL; /* figure out which columns later... */
4756 n->quals = NULL; /* fill later */
4759 | table_ref NATURAL JOIN table_ref
4761 /* letting join_type reduce to empty doesn't work */
4762 JoinExpr *n = makeNode(JoinExpr);
4763 n->jointype = JOIN_INNER;
4764 n->isNatural = TRUE;
4767 n->using = NIL; /* figure out which columns later... */
4768 n->quals = NULL; /* fill later */
4774 AS ColId '(' name_list ')'
4776 $$ = makeNode(Alias);
4782 $$ = makeNode(Alias);
4785 | ColId '(' name_list ')'
4787 $$ = makeNode(Alias);
4793 $$ = makeNode(Alias);
4798 join_type: FULL join_outer { $$ = JOIN_FULL; }
4799 | LEFT join_outer { $$ = JOIN_LEFT; }
4800 | RIGHT join_outer { $$ = JOIN_RIGHT; }
4801 | INNER_P { $$ = JOIN_INNER; }
4804 /* OUTER is just noise... */
4805 join_outer: OUTER_P { $$ = NULL; }
4806 | /*EMPTY*/ { $$ = NULL; }
4809 /* JOIN qualification clauses
4810 * Possibilities are:
4811 * USING ( column list ) allows only unqualified column names,
4812 * which must match between tables.
4813 * ON expr allows more general qualifications.
4815 * We return USING as a List node, while an ON-expr will not be a List.
4818 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
4819 | ON a_expr { $$ = $2; }
4826 /* default inheritance */
4828 $$->inhOpt = INH_DEFAULT;
4831 | qualified_name '*'
4833 /* inheritance query */
4835 $$->inhOpt = INH_YES;
4838 | ONLY qualified_name
4840 /* no inheritance */
4842 $$->inhOpt = INH_NO;
4845 | ONLY '(' qualified_name ')'
4847 /* no inheritance, SQL99-style syntax */
4849 $$->inhOpt = INH_NO;
4855 func_table: func_name '(' ')'
4857 FuncCall *n = makeNode(FuncCall);
4860 n->agg_star = FALSE;
4861 n->agg_distinct = FALSE;
4864 | func_name '(' expr_list ')'
4866 FuncCall *n = makeNode(FuncCall);
4869 n->agg_star = FALSE;
4870 n->agg_distinct = FALSE;
4877 WHERE a_expr { $$ = $2; }
4878 | /*EMPTY*/ { $$ = NULL; }
4882 TableFuncElementList:
4887 | TableFuncElementList ',' TableFuncElement
4889 $$ = lappend($1, $3);
4893 TableFuncElement: ColId Typename
4895 ColumnDef *n = makeNode(ColumnDef);
4898 n->constraints = NIL;
4904 /*****************************************************************************
4907 * SQL92 introduces a large amount of type-specific syntax.
4908 * Define individual clauses to handle these cases, and use
4909 * the generic case to handle regular type-extensible Postgres syntax.
4910 * - thomas 1997-10-10
4912 *****************************************************************************/
4914 Typename: SimpleTypename opt_array_bounds
4917 $$->arrayBounds = $2;
4919 | SETOF SimpleTypename
4927 opt_array_bounds '[' ']'
4928 { $$ = lappend($1, makeInteger(-1)); }
4929 | opt_array_bounds '[' Iconst ']'
4930 { $$ = lappend($1, makeInteger($3)); }
4936 * XXX ideally, the production for a qualified typename should be ColId attrs
4937 * (there's no obvious reason why the first name should need to be restricted)
4938 * and should be an alternative of GenericType (so that it can be used to
4939 * specify a type for a literal in AExprConst). However doing either causes
4940 * reduce/reduce conflicts that I haven't been able to find a workaround
4944 GenericType { $$ = $1; }
4945 | Numeric { $$ = $1; }
4947 | Character { $$ = $1; }
4948 | ConstDatetime { $$ = $1; }
4949 | ConstInterval opt_interval
4952 if ($2 != INTERVAL_FULL_RANGE)
4953 $$->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $2);
4955 | ConstInterval '(' Iconst ')' opt_interval
4958 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
4960 "INTERVAL(%d) precision must be between %d and %d",
4961 $3, 0, MAX_INTERVAL_PRECISION);
4962 $$->typmod = INTERVAL_TYPMOD($3, $5);
4966 $$ = makeNode(TypeName);
4967 $$->names = lcons(makeString($1), $2);
4972 /* We have a separate ConstTypename to allow defaulting fixed-length
4973 * types such as CHAR() and BIT() to an unspecified length.
4974 * SQL9x requires that these default to a length of one, but this
4975 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
4976 * where there is an obvious better choice to make.
4977 * Note that ConstInterval is not included here since it must
4978 * be pushed up higher in the rules to accomodate the postfix
4979 * options (e.g. INTERVAL '1' YEAR).
4982 GenericType { $$ = $1; }
4983 | Numeric { $$ = $1; }
4984 | ConstBit { $$ = $1; }
4985 | ConstCharacter { $$ = $1; }
4986 | ConstDatetime { $$ = $1; }
4992 $$ = makeTypeName($1);
4996 /* SQL92 numeric data types
4997 * Check FLOAT() precision limits assuming IEEE floating types.
4998 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
4999 * - thomas 1997-09-18
5003 $$ = SystemTypeName("int4");
5007 $$ = SystemTypeName("int4");
5011 $$ = SystemTypeName("int2");
5015 $$ = SystemTypeName("int8");
5019 $$ = SystemTypeName("float4");
5027 $$ = SystemTypeName("float8");
5029 | DECIMAL opt_decimal
5031 $$ = SystemTypeName("numeric");
5036 $$ = SystemTypeName("numeric");
5039 | NUMERIC opt_numeric
5041 $$ = SystemTypeName("numeric");
5046 $$ = SystemTypeName("bool");
5050 opt_float: '(' Iconst ')'
5054 "precision for FLOAT must be at least 1");
5056 $$ = SystemTypeName("float4");
5058 $$ = SystemTypeName("float8");
5061 "precision for FLOAT must be less than 16");
5065 $$ = SystemTypeName("float8");
5070 '(' Iconst ',' Iconst ')'
5072 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5074 "NUMERIC precision %d must be between 1 and %d",
5075 $2, NUMERIC_MAX_PRECISION);
5076 if ($4 < 0 || $4 > $2)
5078 "NUMERIC scale %d must be between 0 and precision %d",
5081 $$ = (($2 << 16) | $4) + VARHDRSZ;
5085 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5087 "NUMERIC precision %d must be between 1 and %d",
5088 $2, NUMERIC_MAX_PRECISION);
5090 $$ = ($2 << 16) + VARHDRSZ;
5094 /* Insert "-1" meaning "no limit" */
5100 '(' Iconst ',' Iconst ')'
5102 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5104 "DECIMAL precision %d must be between 1 and %d",
5105 $2, NUMERIC_MAX_PRECISION);
5106 if ($4 < 0 || $4 > $2)
5108 "DECIMAL scale %d must be between 0 and precision %d",
5111 $$ = (($2 << 16) | $4) + VARHDRSZ;
5115 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5117 "DECIMAL precision %d must be between 1 and %d",
5118 $2, NUMERIC_MAX_PRECISION);
5120 $$ = ($2 << 16) + VARHDRSZ;
5124 /* Insert "-1" meaning "no limit" */
5131 * SQL92 bit-field data types
5132 * The following implements BIT() and BIT VARYING().
5144 ConstBit: BitWithLength
5156 BIT opt_varying '(' Iconst ')'
5160 typname = $2 ? "varbit" : "bit";
5161 $$ = SystemTypeName(typname);
5163 elog(ERROR, "length for type '%s' must be at least 1",
5165 else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
5166 elog(ERROR, "length for type '%s' cannot exceed %d",
5167 typname, (MaxAttrSize * BITS_PER_BYTE));
5175 /* bit defaults to bit(1), varbit to no limit */
5178 $$ = SystemTypeName("varbit");
5183 $$ = SystemTypeName("bit");
5191 * SQL92 character data types
5192 * The following implements CHAR() and VARCHAR().
5194 Character: CharacterWithLength
5198 | CharacterWithoutLength
5204 ConstCharacter: CharacterWithLength
5208 | CharacterWithoutLength
5210 /* Length was not specified so allow to be unrestricted.
5211 * This handles problems with fixed-length (bpchar) strings
5212 * which in column definitions must default to a length
5213 * of one, but should not be constrained if the length
5214 * was not specified.
5221 CharacterWithLength: character '(' Iconst ')' opt_charset
5223 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
5227 type = palloc(strlen($1) + 1 + strlen($5) + 1);
5234 $$ = SystemTypeName($1);
5237 elog(ERROR, "length for type '%s' must be at least 1",
5239 else if ($3 > MaxAttrSize)
5240 elog(ERROR, "length for type '%s' cannot exceed %d",
5243 /* we actually implement these like a varlen, so
5244 * the first 4 bytes is the length. (the difference
5245 * between these and "text" is that we blank-pad and
5246 * truncate where necessary)
5248 $$->typmod = VARHDRSZ + $3;
5252 CharacterWithoutLength: character opt_charset
5254 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
5258 type = palloc(strlen($1) + 1 + strlen($2) + 1);
5265 $$ = SystemTypeName($1);
5267 /* char defaults to char(1), varchar to no limit */
5268 if (strcmp($1, "bpchar") == 0)
5269 $$->typmod = VARHDRSZ + 1;
5275 character: CHARACTER opt_varying
5276 { $$ = $2 ? "varchar": "bpchar"; }
5277 | CHAR_P opt_varying
5278 { $$ = $2 ? "varchar": "bpchar"; }
5281 | NATIONAL CHARACTER opt_varying
5282 { $$ = $3 ? "varchar": "bpchar"; }
5283 | NATIONAL CHAR_P opt_varying
5284 { $$ = $3 ? "varchar": "bpchar"; }
5286 { $$ = $2 ? "varchar": "bpchar"; }
5290 VARYING { $$ = TRUE; }
5291 | /*EMPTY*/ { $$ = FALSE; }
5295 CHARACTER SET ColId { $$ = $3; }
5296 | /*EMPTY*/ { $$ = NULL; }
5300 COLLATE ColId { $$ = $2; }
5301 | /*EMPTY*/ { $$ = NULL; }
5305 TIMESTAMP '(' Iconst ')' opt_timezone
5308 $$ = SystemTypeName("timestamptz");
5310 $$ = SystemTypeName("timestamp");
5311 /* XXX the timezone field seems to be unused
5312 * - thomas 2001-09-06
5315 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
5317 "TIMESTAMP(%d)%s precision must be between %d and %d",
5318 $3, ($5 ? " WITH TIME ZONE": ""), 0,
5319 MAX_TIMESTAMP_PRECISION);
5322 | TIMESTAMP opt_timezone
5325 $$ = SystemTypeName("timestamptz");
5327 $$ = SystemTypeName("timestamp");
5328 /* XXX the timezone field seems to be unused
5329 * - thomas 2001-09-06
5332 /* SQL99 specified a default precision of six
5333 * for schema definitions. But for timestamp
5334 * literals we don't want to throw away precision
5335 * so leave this as unspecified for now.
5336 * Later, we may want a different production
5337 * for schemas. - thomas 2001-12-07
5341 | TIME '(' Iconst ')' opt_timezone
5344 $$ = SystemTypeName("timetz");
5346 $$ = SystemTypeName("time");
5347 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
5349 "TIME(%d)%s precision must be between %d and %d",
5350 $3, ($5 ? " WITH TIME ZONE": ""), 0,
5351 MAX_TIME_PRECISION);
5357 $$ = SystemTypeName("timetz");
5359 $$ = SystemTypeName("time");
5360 /* SQL99 specified a default precision of zero.
5361 * See comments for timestamp above on why we will
5362 * leave this unspecified for now. - thomas 2001-12-07
5369 INTERVAL { $$ = SystemTypeName("interval"); }
5373 WITH TIME ZONE { $$ = TRUE; }
5374 | WITHOUT TIME ZONE { $$ = FALSE; }
5375 | /*EMPTY*/ { $$ = FALSE; }
5379 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
5380 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
5381 | DAY_P { $$ = INTERVAL_MASK(DAY); }
5382 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
5383 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
5384 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
5386 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
5388 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
5390 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
5391 | INTERVAL_MASK(MINUTE); }
5393 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
5394 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
5395 | HOUR_P TO MINUTE_P
5396 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
5397 | HOUR_P TO SECOND_P
5398 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
5399 | INTERVAL_MASK(SECOND); }
5400 | MINUTE_P TO SECOND_P
5401 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
5402 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
5406 /*****************************************************************************
5408 * expression grammar
5410 *****************************************************************************/
5412 /* Expressions using row descriptors
5413 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
5414 * with singleton expressions. Use SQL99's ROW keyword to allow rows of
5417 r_expr: row IN_P select_with_parens
5419 SubLink *n = makeNode(SubLink);
5421 n->oper = (List *) makeSimpleA_Expr(OP, "=", NULL, NULL);
5423 n->subLinkType = ANY_SUBLINK;
5427 | row NOT IN_P select_with_parens
5429 SubLink *n = makeNode(SubLink);
5431 n->oper = (List *) makeSimpleA_Expr(OP, "<>", NULL, NULL);
5433 n->subLinkType = ALL_SUBLINK;
5437 | row qual_all_Op sub_type select_with_parens
5440 SubLink *n = makeNode(SubLink);
5442 n->oper = (List *) makeA_Expr(OP, $2, NULL, NULL);
5443 if (strcmp(strVal(llast($2)), "<>") == 0)
5447 n->subLinkType = $3;
5451 | row qual_all_Op select_with_parens
5454 SubLink *n = makeNode(SubLink);
5456 n->oper = (List *) makeA_Expr(OP, $2, NULL, NULL);
5457 if (strcmp(strVal(llast($2)), "<>") == 0)
5461 n->subLinkType = MULTIEXPR_SUBLINK;
5465 | row qual_all_Op row
5468 $$ = makeRowExpr($2, $1, $3);
5472 $$ = makeRowNullTest(IS_NULL, $1);
5476 $$ = makeRowNullTest(IS_NOT_NULL, $1);
5480 $$ = (Node *)makeOverlaps($1, $3);
5482 | row IS DISTINCT FROM row
5485 /* IS DISTINCT FROM has the following rules for non-array types:
5486 * a) the row lengths must be equal
5487 * b) if both rows are zero-length, then they are not distinct
5488 * c) if any element is distinct, the rows are distinct
5489 * The rules for an element being distinct:
5490 * a) if the elements are both NULL, then they are not distinct
5491 * b) if the elements compare to be equal, then they are not distinct
5492 * c) otherwise, they are distinct
5496 /* lengths don't match? then complain */
5497 if (length(largs) != length(rargs))
5499 elog(ERROR, "Unequal number of entries in row expression");
5501 /* both are zero-length rows? then they are not distinct */
5502 else if (length(largs) <= 0)
5504 $$ = (Node *)makeBoolConst(FALSE);
5506 /* otherwise, we need to compare each element */
5509 $$ = (Node *)makeDistinctExpr(largs, rargs);
5514 /* Explicit row production.
5515 * SQL99 allows an optional ROW keyword, so we can now do single-element productions
5516 * without conflicting with the parenthesized a_expr production.
5518 row: ROW '(' row_descriptor ')' { $$ = $3; }
5519 | ROW '(' a_expr ')' { $$ = makeList1($3); }
5520 | ROW '(' ')' { $$ = NULL; }
5521 | '(' row_descriptor ')' { $$ = $2; }
5524 row_descriptor: expr_list ',' a_expr { $$ = lappend($1, $3); }
5527 sub_type: ANY { $$ = ANY_SUBLINK; }
5528 | SOME { $$ = ANY_SUBLINK; }
5529 | ALL { $$ = ALL_SUBLINK; }
5532 all_Op: Op { $$ = $1; }
5533 | MathOp { $$ = $1; }
5536 MathOp: '+' { $$ = "+"; }
5548 { $$ = makeList1(makeString($1)); }
5549 | OPERATOR '(' any_operator ')' { $$ = $3; }
5554 { $$ = makeList1(makeString($1)); }
5555 | OPERATOR '(' any_operator ')' { $$ = $3; }
5559 * General expressions
5560 * This is the heart of the expression syntax.
5562 * We have two expression types: a_expr is the unrestricted kind, and
5563 * b_expr is a subset that must be used in some places to avoid shift/reduce
5564 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
5565 * because that use of AND conflicts with AND as a boolean operator. So,
5566 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
5568 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
5569 * always be used by surrounding it with parens.
5571 * c_expr is all the productions that are common to a_expr and b_expr;
5572 * it's factored out just to eliminate redundant coding.
5574 a_expr: c_expr { $$ = $1; }
5575 | a_expr TYPECAST Typename
5576 { $$ = makeTypeCast($1, $3); }
5577 | a_expr COLLATE ColId
5579 FuncCall *n = makeNode(FuncCall);
5580 n->funcname = SystemFuncName($3);
5581 n->args = makeList1($1);
5582 n->agg_star = FALSE;
5583 n->agg_distinct = FALSE;
5586 | a_expr AT TIME ZONE c_expr
5588 FuncCall *n = makeNode(FuncCall);
5589 n->funcname = SystemFuncName("timezone");
5590 n->args = makeList2($5, $1);
5591 n->agg_star = FALSE;
5592 n->agg_distinct = FALSE;
5596 * These operators must be called out explicitly in order to make use
5597 * of yacc/bison's automatic operator-precedence handling. All other
5598 * operator names are handled by the generic productions using "Op",
5599 * below; and all those operators will have the same precedence.
5601 * If you add more explicitly-known operators, be sure to add them
5602 * also to b_expr and to the MathOp list above.
5604 | '+' a_expr %prec UMINUS
5605 { $$ = (Node *) makeSimpleA_Expr(OP, "+", NULL, $2); }
5606 | '-' a_expr %prec UMINUS
5607 { $$ = doNegate($2); }
5609 { $$ = (Node *) makeSimpleA_Expr(OP, "%", NULL, $2); }
5611 { $$ = (Node *) makeSimpleA_Expr(OP, "^", NULL, $2); }
5613 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, NULL); }
5615 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, NULL); }
5617 { $$ = (Node *) makeSimpleA_Expr(OP, "+", $1, $3); }
5619 { $$ = (Node *) makeSimpleA_Expr(OP, "-", $1, $3); }
5621 { $$ = (Node *) makeSimpleA_Expr(OP, "*", $1, $3); }
5623 { $$ = (Node *) makeSimpleA_Expr(OP, "/", $1, $3); }
5625 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, $3); }
5627 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, $3); }
5629 { $$ = (Node *) makeSimpleA_Expr(OP, "<", $1, $3); }
5631 { $$ = (Node *) makeSimpleA_Expr(OP, ">", $1, $3); }
5633 { $$ = (Node *) makeSimpleA_Expr(OP, "=", $1, $3); }
5635 | a_expr qual_Op a_expr %prec Op
5636 { $$ = (Node *) makeA_Expr(OP, $2, $1, $3); }
5637 | qual_Op a_expr %prec Op
5638 { $$ = (Node *) makeA_Expr(OP, $1, NULL, $2); }
5639 | a_expr qual_Op %prec POSTFIXOP
5640 { $$ = (Node *) makeA_Expr(OP, $2, $1, NULL); }
5643 { $$ = (Node *) makeA_Expr(AND, NIL, $1, $3); }
5645 { $$ = (Node *) makeA_Expr(OR, NIL, $1, $3); }
5647 { $$ = (Node *) makeA_Expr(NOT, NIL, NULL, $2); }
5649 | a_expr LIKE a_expr
5650 { $$ = (Node *) makeSimpleA_Expr(OP, "~~", $1, $3); }
5651 | a_expr LIKE a_expr ESCAPE a_expr
5653 FuncCall *n = makeNode(FuncCall);
5654 n->funcname = SystemFuncName("like_escape");
5655 n->args = makeList2($3, $5);
5656 n->agg_star = FALSE;
5657 n->agg_distinct = FALSE;
5658 $$ = (Node *) makeSimpleA_Expr(OP, "~~", $1, (Node *) n);
5660 | a_expr NOT LIKE a_expr
5661 { $$ = (Node *) makeSimpleA_Expr(OP, "!~~", $1, $4); }
5662 | a_expr NOT LIKE a_expr ESCAPE a_expr
5664 FuncCall *n = makeNode(FuncCall);
5665 n->funcname = SystemFuncName("like_escape");
5666 n->args = makeList2($4, $6);
5667 n->agg_star = FALSE;
5668 n->agg_distinct = FALSE;
5669 $$ = (Node *) makeSimpleA_Expr(OP, "!~~", $1, (Node *) n);
5671 | a_expr ILIKE a_expr
5672 { $$ = (Node *) makeSimpleA_Expr(OP, "~~*", $1, $3); }
5673 | a_expr ILIKE a_expr ESCAPE a_expr
5675 FuncCall *n = makeNode(FuncCall);
5676 n->funcname = SystemFuncName("like_escape");
5677 n->args = makeList2($3, $5);
5678 n->agg_star = FALSE;
5679 n->agg_distinct = FALSE;
5680 $$ = (Node *) makeSimpleA_Expr(OP, "~~*", $1, (Node *) n);
5682 | a_expr NOT ILIKE a_expr
5683 { $$ = (Node *) makeSimpleA_Expr(OP, "!~~*", $1, $4); }
5684 | a_expr NOT ILIKE a_expr ESCAPE a_expr
5686 FuncCall *n = makeNode(FuncCall);
5687 n->funcname = SystemFuncName("like_escape");
5688 n->args = makeList2($4, $6);
5689 n->agg_star = FALSE;
5690 n->agg_distinct = FALSE;
5691 $$ = (Node *) makeSimpleA_Expr(OP, "!~~*", $1, (Node *) n);
5694 | a_expr SIMILAR TO a_expr %prec SIMILAR
5696 A_Const *c = makeNode(A_Const);
5697 FuncCall *n = makeNode(FuncCall);
5698 c->val.type = T_Null;
5699 n->funcname = SystemFuncName("similar_escape");
5700 n->args = makeList2($4, (Node *) c);
5701 n->agg_star = FALSE;
5702 n->agg_distinct = FALSE;
5703 $$ = (Node *) makeSimpleA_Expr(OP, "~", $1, (Node *) n);
5705 | a_expr SIMILAR TO a_expr ESCAPE a_expr
5707 FuncCall *n = makeNode(FuncCall);
5708 n->funcname = SystemFuncName("similar_escape");
5709 n->args = makeList2($4, $6);
5710 n->agg_star = FALSE;
5711 n->agg_distinct = FALSE;
5712 $$ = (Node *) makeSimpleA_Expr(OP, "~", $1, (Node *) n);
5714 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
5716 A_Const *c = makeNode(A_Const);
5717 FuncCall *n = makeNode(FuncCall);
5718 c->val.type = T_Null;
5719 n->funcname = SystemFuncName("similar_escape");
5720 n->args = makeList2($5, (Node *) c);
5721 n->agg_star = FALSE;
5722 n->agg_distinct = FALSE;
5723 $$ = (Node *) makeSimpleA_Expr(OP, "!~", $1, (Node *) n);
5725 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
5727 FuncCall *n = makeNode(FuncCall);
5728 n->funcname = SystemFuncName("similar_escape");
5729 n->args = makeList2($5, $7);
5730 n->agg_star = FALSE;
5731 n->agg_distinct = FALSE;
5732 $$ = (Node *) makeSimpleA_Expr(OP, "!~", $1, (Node *) n);
5736 * Define SQL92-style Null test clause.
5737 * Allow two forms described in the standard:
5740 * Allow two SQL extensions
5746 NullTest *n = makeNode(NullTest);
5747 n->arg = (Expr *) $1;
5748 n->nulltesttype = IS_NULL;
5753 NullTest *n = makeNode(NullTest);
5754 n->arg = (Expr *) $1;
5755 n->nulltesttype = IS_NULL;
5760 NullTest *n = makeNode(NullTest);
5761 n->arg = (Expr *) $1;
5762 n->nulltesttype = IS_NOT_NULL;
5765 | a_expr IS NOT NULL_P
5767 NullTest *n = makeNode(NullTest);
5768 n->arg = (Expr *) $1;
5769 n->nulltesttype = IS_NOT_NULL;
5774 BooleanTest *b = makeNode(BooleanTest);
5775 b->arg = (Expr *) $1;
5776 b->booltesttype = IS_TRUE;
5779 | a_expr IS NOT TRUE_P
5781 BooleanTest *b = makeNode(BooleanTest);
5782 b->arg = (Expr *) $1;
5783 b->booltesttype = IS_NOT_TRUE;
5788 BooleanTest *b = makeNode(BooleanTest);
5789 b->arg = (Expr *) $1;
5790 b->booltesttype = IS_FALSE;
5793 | a_expr IS NOT FALSE_P
5795 BooleanTest *b = makeNode(BooleanTest);
5796 b->arg = (Expr *) $1;
5797 b->booltesttype = IS_NOT_FALSE;
5802 BooleanTest *b = makeNode(BooleanTest);
5803 b->arg = (Expr *) $1;
5804 b->booltesttype = IS_UNKNOWN;
5807 | a_expr IS NOT UNKNOWN
5809 BooleanTest *b = makeNode(BooleanTest);
5810 b->arg = (Expr *) $1;
5811 b->booltesttype = IS_NOT_UNKNOWN;
5814 | a_expr IS DISTINCT FROM a_expr %prec IS
5815 { $$ = (Node *) makeSimpleA_Expr(DISTINCT, "=", $1, $5); }
5816 | a_expr IS OF '(' type_list ')' %prec IS
5818 $$ = (Node *) makeSimpleA_Expr(OF, "=", $1, (Node *) $5);
5820 | a_expr IS NOT OF '(' type_list ')' %prec IS
5822 $$ = (Node *) makeSimpleA_Expr(OF, "!=", $1, (Node *) $6);
5824 | a_expr BETWEEN b_expr AND b_expr %prec BETWEEN
5826 $$ = (Node *) makeA_Expr(AND, NIL,
5827 (Node *) makeSimpleA_Expr(OP, ">=", $1, $3),
5828 (Node *) makeSimpleA_Expr(OP, "<=", $1, $5));
5830 | a_expr NOT BETWEEN b_expr AND b_expr %prec BETWEEN
5832 $$ = (Node *) makeA_Expr(OR, NIL,
5833 (Node *) makeSimpleA_Expr(OP, "<", $1, $4),
5834 (Node *) makeSimpleA_Expr(OP, ">", $1, $6));
5836 | a_expr IN_P in_expr
5838 /* in_expr returns a SubLink or a list of a_exprs */
5839 if (IsA($3, SubLink))
5841 SubLink *n = (SubLink *)$3;
5842 n->lefthand = makeList1($1);
5843 n->oper = (List *) makeSimpleA_Expr(OP, "=", NULL, NULL);
5845 n->subLinkType = ANY_SUBLINK;
5852 foreach(l, (List *) $3)
5855 cmp = (Node *) makeSimpleA_Expr(OP, "=", $1, lfirst(l));
5859 n = (Node *) makeA_Expr(OR, NIL, n, cmp);
5864 | a_expr NOT IN_P in_expr
5866 /* in_expr returns a SubLink or a list of a_exprs */
5867 if (IsA($4, SubLink))
5869 SubLink *n = (SubLink *)$4;
5870 n->lefthand = makeList1($1);
5871 n->oper = (List *) makeSimpleA_Expr(OP, "<>", NULL, NULL);
5873 n->subLinkType = ALL_SUBLINK;
5880 foreach(l, (List *) $4)
5883 cmp = (Node *) makeSimpleA_Expr(OP, "<>", $1, lfirst(l));
5887 n = (Node *) makeA_Expr(AND, NIL, n, cmp);
5892 | a_expr qual_all_Op sub_type select_with_parens %prec Op
5894 SubLink *n = makeNode(SubLink);
5895 n->lefthand = makeList1($1);
5896 n->oper = (List *) makeA_Expr(OP, $2, NULL, NULL);
5897 n->useor = FALSE; /* doesn't matter since only one col */
5898 n->subLinkType = $3;
5902 | UNIQUE select_with_parens %prec Op
5904 /* Not sure how to get rid of the parentheses
5905 * but there are lots of shift/reduce errors without them.
5907 * Should be able to implement this by plopping the entire
5908 * select into a node, then transforming the target expressions
5909 * from whatever they are into count(*), and testing the
5910 * entire result equal to one.
5911 * But, will probably implement a separate node in the executor.
5913 elog(ERROR, "UNIQUE predicate is not yet implemented");
5920 * Restricted expressions
5922 * b_expr is a subset of the complete expression syntax defined by a_expr.
5924 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
5925 * cause trouble in the places where b_expr is used. For simplicity, we
5926 * just eliminate all the boolean-keyword-operator productions from b_expr.
5930 | b_expr TYPECAST Typename
5931 { $$ = makeTypeCast($1, $3); }
5932 | '+' b_expr %prec UMINUS
5933 { $$ = (Node *) makeSimpleA_Expr(OP, "+", NULL, $2); }
5934 | '-' b_expr %prec UMINUS
5935 { $$ = doNegate($2); }
5937 { $$ = (Node *) makeSimpleA_Expr(OP, "%", NULL, $2); }
5939 { $$ = (Node *) makeSimpleA_Expr(OP, "^", NULL, $2); }
5941 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, NULL); }
5943 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, NULL); }
5945 { $$ = (Node *) makeSimpleA_Expr(OP, "+", $1, $3); }
5947 { $$ = (Node *) makeSimpleA_Expr(OP, "-", $1, $3); }
5949 { $$ = (Node *) makeSimpleA_Expr(OP, "*", $1, $3); }
5951 { $$ = (Node *) makeSimpleA_Expr(OP, "/", $1, $3); }
5953 { $$ = (Node *) makeSimpleA_Expr(OP, "%", $1, $3); }
5955 { $$ = (Node *) makeSimpleA_Expr(OP, "^", $1, $3); }
5957 { $$ = (Node *) makeSimpleA_Expr(OP, "<", $1, $3); }
5959 { $$ = (Node *) makeSimpleA_Expr(OP, ">", $1, $3); }
5961 { $$ = (Node *) makeSimpleA_Expr(OP, "=", $1, $3); }
5962 | b_expr qual_Op b_expr %prec Op
5963 { $$ = (Node *) makeA_Expr(OP, $2, $1, $3); }
5964 | qual_Op b_expr %prec Op
5965 { $$ = (Node *) makeA_Expr(OP, $1, NULL, $2); }
5966 | b_expr qual_Op %prec POSTFIXOP
5967 { $$ = (Node *) makeA_Expr(OP, $2, $1, NULL); }
5968 | b_expr IS DISTINCT FROM b_expr %prec IS
5969 { $$ = (Node *) makeSimpleA_Expr(DISTINCT, "=", $1, $5); }
5970 | b_expr IS OF '(' type_list ')' %prec IS
5972 $$ = (Node *) makeSimpleA_Expr(OF, "=", $1, (Node *) $5);
5974 | b_expr IS NOT OF '(' type_list ')' %prec IS
5976 $$ = (Node *) makeSimpleA_Expr(OF, "!=", $1, (Node *) $6);
5981 * Productions that can be used in both a_expr and b_expr.
5983 * Note: productions that refer recursively to a_expr or b_expr mostly
5984 * cannot appear here. However, it's OK to refer to a_exprs that occur
5985 * inside parentheses, such as function arguments; that cannot introduce
5986 * ambiguity to the b_expr syntax.
5988 c_expr: columnref { $$ = (Node *) $1; }
5989 | AexprConst { $$ = $1; }
5990 | PARAM attrs opt_indirection
5993 * PARAM without field names is considered a constant,
5994 * but with 'em, it is not. Not very consistent ...
5996 ParamRef *n = makeNode(ParamRef);
5999 n->indirection = $3;
6002 | '(' a_expr ')' { $$ = $2; }
6003 | '(' a_expr ')' attrs opt_indirection
6005 ExprFieldSelect *n = makeNode(ExprFieldSelect);
6008 n->indirection = $5;
6015 FuncCall *n = makeNode(FuncCall);
6018 n->agg_star = FALSE;
6019 n->agg_distinct = FALSE;
6022 | func_name '(' expr_list ')'
6024 FuncCall *n = makeNode(FuncCall);
6027 n->agg_star = FALSE;
6028 n->agg_distinct = FALSE;
6031 | func_name '(' ALL expr_list ')'
6033 FuncCall *n = makeNode(FuncCall);
6036 n->agg_star = FALSE;
6037 n->agg_distinct = FALSE;
6038 /* Ideally we'd mark the FuncCall node to indicate
6039 * "must be an aggregate", but there's no provision
6040 * for that in FuncCall at the moment.
6044 | func_name '(' DISTINCT expr_list ')'
6046 FuncCall *n = makeNode(FuncCall);
6049 n->agg_star = FALSE;
6050 n->agg_distinct = TRUE;
6053 | func_name '(' '*' ')'
6056 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
6058 * This does the right thing for COUNT(*) (in fact,
6059 * any certainly-non-null expression would do for COUNT),
6060 * and there are no other aggregates in SQL92 that accept
6063 * The FuncCall node is also marked agg_star = true,
6064 * so that later processing can detect what the argument
6067 FuncCall *n = makeNode(FuncCall);
6068 A_Const *star = makeNode(A_Const);
6070 star->val.type = T_Integer;
6071 star->val.val.ival = 1;
6073 n->args = makeList1(star);
6075 n->agg_distinct = FALSE;
6081 * Translate as "'now'::text::date".
6083 * We cannot use "'now'::date" because coerce_type() will
6084 * immediately reduce that to a constant representing
6085 * today's date. We need to delay the conversion until
6086 * runtime, else the wrong things will happen when
6087 * CURRENT_DATE is used in a column default value or rule.
6089 * This could be simplified if we had a way to generate
6090 * an expression tree representing runtime application
6091 * of type-input conversion functions...
6093 A_Const *s = makeNode(A_Const);
6096 s->val.type = T_String;
6097 s->val.val.str = "now";
6098 s->typename = SystemTypeName("text");
6100 d = SystemTypeName("date");
6102 $$ = (Node *)makeTypeCast((Node *)s, d);
6107 * Translate as "'now'::text::timetz".
6108 * See comments for CURRENT_DATE.
6110 A_Const *s = makeNode(A_Const);
6113 s->val.type = T_String;
6114 s->val.val.str = "now";
6115 s->typename = SystemTypeName("text");
6117 d = SystemTypeName("timetz");
6118 /* SQL99 mandates a default precision of zero for TIME
6119 * fields in schemas. However, for CURRENT_TIME
6120 * let's preserve the microsecond precision we
6121 * might see from the system clock. - thomas 2001-12-07
6125 $$ = (Node *)makeTypeCast((Node *)s, d);
6127 | CURRENT_TIME '(' Iconst ')'
6130 * Translate as "'now'::text::timetz(n)".
6131 * See comments for CURRENT_DATE.
6133 A_Const *s = makeNode(A_Const);
6136 s->val.type = T_String;
6137 s->val.val.str = "now";
6138 s->typename = SystemTypeName("text");
6139 d = SystemTypeName("timetz");
6140 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
6142 "CURRENT_TIME(%d) precision must be between %d and %d",
6143 $3, 0, MAX_TIME_PRECISION);
6146 $$ = (Node *)makeTypeCast((Node *)s, d);
6151 * Translate as "'now'::text::timestamptz".
6152 * See comments for CURRENT_DATE.
6154 A_Const *s = makeNode(A_Const);
6157 s->val.type = T_String;
6158 s->val.val.str = "now";
6159 s->typename = SystemTypeName("text");
6161 d = SystemTypeName("timestamptz");
6162 /* SQL99 mandates a default precision of 6 for timestamp.
6163 * Also, that is about as precise as we will get since
6164 * we are using a microsecond time interface.
6165 * - thomas 2001-12-07
6169 $$ = (Node *)makeTypeCast((Node *)s, d);
6171 | CURRENT_TIMESTAMP '(' Iconst ')'
6174 * Translate as "'now'::text::timestamptz(n)".
6175 * See comments for CURRENT_DATE.
6177 A_Const *s = makeNode(A_Const);
6180 s->val.type = T_String;
6181 s->val.val.str = "now";
6182 s->typename = SystemTypeName("text");
6184 d = SystemTypeName("timestamptz");
6185 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
6187 "CURRENT_TIMESTAMP(%d) precision "
6188 "must be between %d and %d",
6189 $3, 0, MAX_TIMESTAMP_PRECISION);
6192 $$ = (Node *)makeTypeCast((Node *)s, d);
6197 * Translate as "'now'::text::time".
6198 * See comments for CURRENT_DATE.
6200 A_Const *s = makeNode(A_Const);
6203 s->val.type = T_String;
6204 s->val.val.str = "now";
6205 s->typename = SystemTypeName("text");
6207 d = SystemTypeName("time");
6208 /* SQL99 mandates a default precision of zero for TIME
6209 * fields in schemas. However, for LOCALTIME
6210 * let's preserve the microsecond precision we
6211 * might see from the system clock. - thomas 2001-12-07
6215 $$ = (Node *)makeTypeCast((Node *)s, d);
6217 | LOCALTIME '(' Iconst ')'
6220 * Translate as "'now'::text::time(n)".
6221 * See comments for CURRENT_DATE.
6223 A_Const *s = makeNode(A_Const);
6226 s->val.type = T_String;
6227 s->val.val.str = "now";
6228 s->typename = SystemTypeName("text");
6229 d = SystemTypeName("time");
6230 if (($3 < 0) || ($3 > MAX_TIME_PRECISION))
6232 "LOCALTIME(%d) precision must be between %d and %d",
6233 $3, 0, MAX_TIME_PRECISION);
6236 $$ = (Node *)makeTypeCast((Node *)s, d);
6241 * Translate as "'now'::text::timestamp".
6242 * See comments for CURRENT_DATE.
6244 A_Const *s = makeNode(A_Const);
6247 s->val.type = T_String;
6248 s->val.val.str = "now";
6249 s->typename = SystemTypeName("text");
6251 d = SystemTypeName("timestamp");
6252 /* SQL99 mandates a default precision of 6 for timestamp.
6253 * Also, that is about as precise as we will get since
6254 * we are using a microsecond time interface.
6255 * - thomas 2001-12-07
6259 $$ = (Node *)makeTypeCast((Node *)s, d);
6261 | LOCALTIMESTAMP '(' Iconst ')'
6264 * Translate as "'now'::text::timestamp(n)".
6265 * See comments for CURRENT_DATE.
6267 A_Const *s = makeNode(A_Const);
6270 s->val.type = T_String;
6271 s->val.val.str = "now";
6272 s->typename = SystemTypeName("text");
6274 d = SystemTypeName("timestamp");
6275 if (($3 < 0) || ($3 > MAX_TIMESTAMP_PRECISION))
6277 "LOCALTIMESTAMP(%d) precision must be "
6278 "between %d and %d",
6279 $3, 0, MAX_TIMESTAMP_PRECISION);
6282 $$ = (Node *)makeTypeCast((Node *)s, d);
6286 FuncCall *n = makeNode(FuncCall);
6287 n->funcname = SystemFuncName("current_user");
6289 n->agg_star = FALSE;
6290 n->agg_distinct = FALSE;
6295 FuncCall *n = makeNode(FuncCall);
6296 n->funcname = SystemFuncName("session_user");
6298 n->agg_star = FALSE;
6299 n->agg_distinct = FALSE;
6304 FuncCall *n = makeNode(FuncCall);
6305 n->funcname = SystemFuncName("current_user");
6307 n->agg_star = FALSE;
6308 n->agg_distinct = FALSE;
6311 | CAST '(' a_expr AS Typename ')'
6312 { $$ = makeTypeCast($3, $5); }
6313 | EXTRACT '(' extract_list ')'
6315 FuncCall *n = makeNode(FuncCall);
6316 n->funcname = SystemFuncName("date_part");
6318 n->agg_star = FALSE;
6319 n->agg_distinct = FALSE;
6322 | OVERLAY '(' overlay_list ')'
6324 /* overlay(A PLACING B FROM C FOR D) is converted to
6325 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
6326 * overlay(A PLACING B FROM C) is converted to
6327 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
6329 FuncCall *n = makeNode(FuncCall);
6330 n->funcname = SystemFuncName("overlay");
6332 n->agg_star = FALSE;
6333 n->agg_distinct = FALSE;
6336 | POSITION '(' position_list ')'
6338 /* position(A in B) is converted to position(B, A) */
6339 FuncCall *n = makeNode(FuncCall);
6340 n->funcname = SystemFuncName("position");
6342 n->agg_star = FALSE;
6343 n->agg_distinct = FALSE;
6346 | SUBSTRING '(' substr_list ')'
6348 /* substring(A from B for C) is converted to
6349 * substring(A, B, C) - thomas 2000-11-28
6351 FuncCall *n = makeNode(FuncCall);
6352 n->funcname = SystemFuncName("substring");
6354 n->agg_star = FALSE;
6355 n->agg_distinct = FALSE;
6358 | TREAT '(' a_expr AS Typename ')'
6360 /* TREAT(expr AS target) converts expr of a particular type to target,
6361 * which is defined to be a subtype of the original expression.
6362 * In SQL99, this is intended for use with structured UDTs,
6363 * but let's make this a generally useful form allowing stronger
6364 * coersions than are handled by implicit casting.
6366 FuncCall *n = makeNode(FuncCall);
6367 /* Convert SystemTypeName() to SystemFuncName() even though
6368 * at the moment they result in the same thing.
6370 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
6371 n->args = makeList1($3);
6374 | TRIM '(' BOTH trim_list ')'
6376 /* various trim expressions are defined in SQL92
6377 * - thomas 1997-07-19
6379 FuncCall *n = makeNode(FuncCall);
6380 n->funcname = SystemFuncName("btrim");
6382 n->agg_star = FALSE;
6383 n->agg_distinct = FALSE;
6386 | TRIM '(' LEADING trim_list ')'
6388 FuncCall *n = makeNode(FuncCall);
6389 n->funcname = SystemFuncName("ltrim");
6391 n->agg_star = FALSE;
6392 n->agg_distinct = FALSE;
6395 | TRIM '(' TRAILING trim_list ')'
6397 FuncCall *n = makeNode(FuncCall);
6398 n->funcname = SystemFuncName("rtrim");
6400 n->agg_star = FALSE;
6401 n->agg_distinct = FALSE;
6404 | TRIM '(' trim_list ')'
6406 FuncCall *n = makeNode(FuncCall);
6407 n->funcname = SystemFuncName("btrim");
6409 n->agg_star = FALSE;
6410 n->agg_distinct = FALSE;
6413 | CONVERT '(' a_expr USING any_name ')'
6415 FuncCall *n = makeNode(FuncCall);
6416 A_Const *c = makeNode(A_Const);
6418 c->val.type = T_String;
6419 c->val.val.str = NameListToQuotedString($5);
6421 n->funcname = SystemFuncName("convert_using");
6422 n->args = makeList2($3, c);
6423 n->agg_star = FALSE;
6424 n->agg_distinct = FALSE;
6427 | CONVERT '(' expr_list ')'
6429 FuncCall *n = makeNode(FuncCall);
6430 n->funcname = SystemFuncName("convert");
6432 n->agg_star = FALSE;
6433 n->agg_distinct = FALSE;
6436 | select_with_parens %prec UMINUS
6438 SubLink *n = makeNode(SubLink);
6442 n->subLinkType = EXPR_SUBLINK;
6446 | EXISTS select_with_parens
6448 SubLink *n = makeNode(SubLink);
6452 n->subLinkType = EXISTS_SUBLINK;
6459 * Supporting nonterminals for expressions.
6463 opt_indirection '[' a_expr ']'
6465 A_Indices *ai = makeNode(A_Indices);
6468 $$ = lappend($1, ai);
6470 | opt_indirection '[' a_expr ':' a_expr ']'
6472 A_Indices *ai = makeNode(A_Indices);
6475 $$ = lappend($1, ai);
6481 expr_list: a_expr { $$ = makeList1($1); }
6482 | expr_list ',' a_expr { $$ = lappend($1, $3); }
6486 extract_arg FROM a_expr
6488 A_Const *n = makeNode(A_Const);
6489 n->val.type = T_String;
6490 n->val.val.str = $1;
6491 $$ = makeList2((Node *) n, $3);
6493 | /*EMPTY*/ { $$ = NIL; }
6496 type_list: type_list ',' Typename
6498 $$ = lappend($1, $3);
6506 /* Allow delimited string SCONST in extract_arg as an SQL extension.
6507 * - thomas 2001-04-12
6512 | YEAR_P { $$ = "year"; }
6513 | MONTH_P { $$ = "month"; }
6514 | DAY_P { $$ = "day"; }
6515 | HOUR_P { $$ = "hour"; }
6516 | MINUTE_P { $$ = "minute"; }
6517 | SECOND_P { $$ = "second"; }
6518 | SCONST { $$ = $1; }
6521 /* OVERLAY() arguments
6522 * SQL99 defines the OVERLAY() function:
6523 * o overlay(text placing text from int for int)
6524 * o overlay(text placing text from int)
6527 a_expr overlay_placing substr_from substr_for
6529 $$ = makeList4($1, $2, $3, $4);
6531 | a_expr overlay_placing substr_from
6533 $$ = makeList3($1, $2, $3);
6542 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
6545 b_expr IN_P b_expr { $$ = makeList2($3, $1); }
6546 | /*EMPTY*/ { $$ = NIL; }
6549 /* SUBSTRING() arguments
6550 * SQL9x defines a specific syntax for arguments to SUBSTRING():
6551 * o substring(text from int for int)
6552 * o substring(text from int) get entire string from starting point "int"
6553 * o substring(text from pattern) get entire string matching pattern
6554 * o substring(text for int) get first "int" characters of string
6555 * We also want to implement generic substring functions which accept
6556 * the usual generic list of arguments. So we will accept both styles
6557 * here, and convert the SQL9x style to the generic list for further
6558 * processing. - thomas 2000-11-28
6561 a_expr substr_from substr_for
6563 $$ = makeList3($1, $2, $3);
6565 | a_expr substr_for substr_from
6567 $$ = makeList3($1, $3, $2);
6569 | a_expr substr_from
6571 $$ = makeList2($1, $2);
6575 A_Const *n = makeNode(A_Const);
6576 n->val.type = T_Integer;
6577 n->val.val.ival = 1;
6578 $$ = makeList3($1, (Node *)n, $2);
6589 FROM a_expr { $$ = $2; }
6592 substr_for: FOR a_expr { $$ = $2; }
6595 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
6596 | FROM expr_list { $$ = $2; }
6597 | expr_list { $$ = $1; }
6600 in_expr: select_with_parens
6602 SubLink *n = makeNode(SubLink);
6606 | '(' expr_list ')' { $$ = (Node *)$2; }
6610 * Define SQL92-style case clause.
6611 * Allow all four forms described in the standard:
6612 * - Full specification
6613 * CASE WHEN a = b THEN c ... ELSE d END
6614 * - Implicit argument
6615 * CASE a WHEN b THEN c ... ELSE d END
6616 * - Conditional NULL
6618 * same as CASE WHEN x = y THEN NULL ELSE x END
6619 * - Conditional substitution from list, use first non-null argument
6621 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
6622 * - thomas 1998-11-09
6624 case_expr: CASE case_arg when_clause_list case_default END_TRANS
6626 CaseExpr *c = makeNode(CaseExpr);
6627 c->arg = (Expr *) $2;
6629 c->defresult = (Expr *) $4;
6632 | NULLIF '(' a_expr ',' a_expr ')'
6634 CaseExpr *c = makeNode(CaseExpr);
6635 CaseWhen *w = makeNode(CaseWhen);
6637 w->expr = (Expr *) makeSimpleA_Expr(OP, "=", $3, $5);
6638 /* w->result is left NULL */
6639 c->args = makeList1(w);
6640 c->defresult = (Expr *) $3;
6643 | COALESCE '(' expr_list ')'
6645 CaseExpr *c = makeNode(CaseExpr);
6649 CaseWhen *w = makeNode(CaseWhen);
6650 NullTest *n = makeNode(NullTest);
6652 n->nulltesttype = IS_NOT_NULL;
6653 w->expr = (Expr *) n;
6654 w->result = lfirst(l);
6655 c->args = lappend(c->args, w);
6662 /* There must be at least one */
6663 when_clause { $$ = makeList1($1); }
6664 | when_clause_list when_clause { $$ = lappend($1, $2); }
6668 WHEN a_expr THEN a_expr
6670 CaseWhen *w = makeNode(CaseWhen);
6671 w->expr = (Expr *) $2;
6672 w->result = (Expr *) $4;
6678 ELSE a_expr { $$ = $2; }
6679 | /*EMPTY*/ { $$ = NULL; }
6682 case_arg: a_expr { $$ = $1; }
6683 | /*EMPTY*/ { $$ = NULL; }
6687 * columnref starts with relation_name not ColId, so that OLD and NEW
6688 * references can be accepted. Note that when there are more than two
6689 * dotted names, the first name is not actually a relation name...
6691 columnref: relation_name opt_indirection
6693 $$ = makeNode(ColumnRef);
6694 $$->fields = makeList1(makeString($1));
6695 $$->indirection = $2;
6697 | dotted_name opt_indirection
6699 $$ = makeNode(ColumnRef);
6701 $$->indirection = $2;
6707 { $$ = lcons(makeString($1), $2); }
6710 attrs: '.' attr_name
6711 { $$ = makeList1(makeString($2)); }
6713 { $$ = makeList1(makeString("*")); }
6714 | '.' attr_name attrs
6715 { $$ = lcons(makeString($2), $3); }
6719 /*****************************************************************************
6723 *****************************************************************************/
6725 /* Target lists as found in SELECT ... and INSERT VALUES ( ... ) */
6728 target_el { $$ = makeList1($1); }
6729 | target_list ',' target_el { $$ = lappend($1, $3); }
6732 /* AS is not optional because shift/red conflict with unary ops */
6733 target_el: a_expr AS ColLabel
6735 $$ = makeNode(ResTarget);
6737 $$->indirection = NIL;
6738 $$->val = (Node *)$1;
6742 $$ = makeNode(ResTarget);
6744 $$->indirection = NIL;
6745 $$->val = (Node *)$1;
6749 ColumnRef *n = makeNode(ColumnRef);
6750 n->fields = makeList1(makeString("*"));
6751 n->indirection = NIL;
6752 $$ = makeNode(ResTarget);
6754 $$->indirection = NIL;
6755 $$->val = (Node *)n;
6759 /* Target list as found in UPDATE table SET ...
6760 | '(' row_ ')' = '(' row_ ')'
6766 update_target_el { $$ = makeList1($1); }
6767 | update_target_list ',' update_target_el { $$ = lappend($1,$3); }
6771 ColId opt_indirection '=' a_expr
6773 $$ = makeNode(ResTarget);
6775 $$->indirection = $2;
6776 $$->val = (Node *)$4;
6781 insert_target_el { $$ = makeList1($1); }
6782 | insert_target_list ',' insert_target_el { $$ = lappend($1, $3); }
6786 target_el { $$ = $1; }
6789 InsertDefault *def = makeNode(InsertDefault);
6790 $$ = makeNode(ResTarget);
6792 $$->indirection = NULL;
6793 $$->val = (Node *)def;
6798 /*****************************************************************************
6800 * Names and constants
6802 *****************************************************************************/
6805 SpecialRuleRelation { $$ = $1; }
6806 | ColId { $$ = $1; }
6809 qualified_name_list:
6810 qualified_name { $$ = makeList1($1); }
6811 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
6817 $$ = makeNode(RangeVar);
6818 $$->catalogname = NULL;
6819 $$->schemaname = NULL;
6824 $$ = makeNode(RangeVar);
6828 $$->catalogname = NULL;
6829 $$->schemaname = strVal(lfirst($1));
6830 $$->relname = strVal(lsecond($1));
6833 $$->catalogname = strVal(lfirst($1));
6834 $$->schemaname = strVal(lsecond($1));
6835 $$->relname = strVal(lfirst(lnext(lnext($1))));
6839 "Improper qualified name "
6840 "(too many dotted names): %s",
6841 NameListToString($1));
6848 { $$ = makeList1(makeString($1)); }
6849 | name_list ',' name
6850 { $$ = lappend($1, makeString($3)); }
6854 name: ColId { $$ = $1; };
6862 attr_name: ColId { $$ = $1; };
6864 index_name: ColId { $$ = $1; };
6866 file_name: Sconst { $$ = $1; };
6868 func_name: function_name
6869 { $$ = makeList1(makeString($1)); }
6870 | dotted_name { $$ = $1; }
6879 A_Const *n = makeNode(A_Const);
6880 n->val.type = T_Integer;
6881 n->val.val.ival = $1;
6886 A_Const *n = makeNode(A_Const);
6887 n->val.type = T_Float;
6888 n->val.val.str = $1;
6893 A_Const *n = makeNode(A_Const);
6894 n->val.type = T_String;
6895 n->val.val.str = $1;
6900 A_Const *n = makeNode(A_Const);
6901 n->val.type = T_BitString;
6902 n->val.val.str = $1;
6907 /* This is a bit constant per SQL99:
6908 * Without Feature F511, "BIT data type",
6909 * a <general literal> shall not be a
6910 * <bit string literal> or a <hex string literal>.
6912 A_Const *n = makeNode(A_Const);
6913 n->val.type = T_BitString;
6914 n->val.val.str = $1;
6917 | ConstTypename Sconst
6919 A_Const *n = makeNode(A_Const);
6921 n->val.type = T_String;
6922 n->val.val.str = $2;
6925 | ConstInterval Sconst opt_interval
6927 A_Const *n = makeNode(A_Const);
6929 n->val.type = T_String;
6930 n->val.val.str = $2;
6931 /* precision is not specified, but fields may be... */
6932 if ($3 != INTERVAL_FULL_RANGE)
6933 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
6936 | ConstInterval '(' Iconst ')' Sconst opt_interval
6938 A_Const *n = makeNode(A_Const);
6940 n->val.type = T_String;
6941 n->val.val.str = $5;
6942 /* precision specified, and fields may be... */
6943 if (($3 < 0) || ($3 > MAX_INTERVAL_PRECISION))
6945 "INTERVAL(%d) precision must be between %d and %d",
6946 $3, 0, MAX_INTERVAL_PRECISION);
6947 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
6950 | PARAM opt_indirection
6952 ParamRef *n = makeNode(ParamRef);
6955 n->indirection = $2;
6960 $$ = (Node *)makeBoolConst(TRUE);
6964 $$ = (Node *)makeBoolConst(FALSE);
6968 A_Const *n = makeNode(A_Const);
6969 n->val.type = T_Null;
6974 Iconst: ICONST { $$ = $1; };
6975 Sconst: SCONST { $$ = $1; };
6976 UserId: ColId { $$ = $1; };
6979 * Name classification hierarchy.
6981 * IDENT is the lexeme returned by the lexer for identifiers that match
6982 * no known keyword. In most cases, we can accept certain keywords as
6983 * names, not only IDENTs. We prefer to accept as many such keywords
6984 * as possible to minimize the impact of "reserved words" on programmers.
6985 * So, we divide names into several possible classes. The classification
6986 * is chosen in part to make keywords acceptable as names wherever possible.
6989 /* Column identifier --- names that can be column, table, etc names.
6991 ColId: IDENT { $$ = $1; }
6992 | unreserved_keyword { $$ = pstrdup($1); }
6993 | col_name_keyword { $$ = pstrdup($1); }
6996 /* Type identifier --- names that can be type names.
6998 type_name: IDENT { $$ = $1; }
6999 | unreserved_keyword { $$ = pstrdup($1); }
7002 /* Function identifier --- names that can be function names.
7006 | unreserved_keyword { $$ = pstrdup($1); }
7007 | func_name_keyword { $$ = pstrdup($1); }
7010 /* Column label --- allowed labels in "AS" clauses.
7011 * This presently includes *all* Postgres keywords.
7013 ColLabel: IDENT { $$ = $1; }
7014 | unreserved_keyword { $$ = pstrdup($1); }
7015 | col_name_keyword { $$ = pstrdup($1); }
7016 | func_name_keyword { $$ = pstrdup($1); }
7017 | reserved_keyword { $$ = pstrdup($1); }
7022 * Keyword classification lists. Generally, every keyword present in
7023 * the Postgres grammar should appear in exactly one of these lists.
7025 * Put a new keyword into the first list that it can go into without causing
7026 * shift or reduce conflicts. The earlier lists define "less reserved"
7027 * categories of keywords.
7030 /* "Unreserved" keywords --- available for use as any kind of name.
7214 /* Column identifier --- keywords that can be column, table, etc names.
7216 * Many of these keywords will in fact be recognized as type or function
7217 * names too; but they have special productions for the purpose, and so
7218 * can't be treated as "generic" type or function names.
7220 * The type names appearing here are not usable as function names
7221 * because they can be followed by '(' in typename productions, which
7222 * looks too much like a function call for an LR(1) parser.
7258 /* Function identifier --- keywords that can be function names.
7260 * Most of these are keywords that are used as operators in expressions;
7261 * in general such keywords can't be column names because they would be
7262 * ambiguous with variables, but they are unambiguous as function identifiers.
7264 * Do not include POSITION, SUBSTRING, etc here since they have explicit
7265 * productions in a_expr to support the goofy SQL9x argument syntax.
7266 * - thomas 2000-11-28
7292 /* Reserved keyword --- these keywords are usable only as a ColLabel.
7294 * Keywords appear here if they could not be distinguished from variable,
7295 * type, or function names in some contexts. Don't put things here unless
7370 SpecialRuleRelation:
7376 elog(ERROR, "OLD used in non-rule query");
7383 elog(ERROR, "NEW used in non-rule query");
7390 makeTypeCast(Node *arg, TypeName *typename)
7393 * Simply generate a TypeCast node.
7395 * Earlier we would determine whether an A_Const would
7396 * be acceptable, however Domains require coerce_type()
7397 * to process them -- applying constraints as required.
7399 TypeCast *n = makeNode(TypeCast);
7401 n->typename = typename;
7406 makeStringConst(char *str, TypeName *typename)
7408 A_Const *n = makeNode(A_Const);
7410 n->val.type = T_String;
7411 n->val.val.str = str;
7412 n->typename = typename;
7418 makeIntConst(int val)
7420 A_Const *n = makeNode(A_Const);
7421 n->val.type = T_Integer;
7422 n->val.val.ival = val;
7423 n->typename = SystemTypeName("int4");
7429 makeFloatConst(char *str)
7431 A_Const *n = makeNode(A_Const);
7433 n->val.type = T_Float;
7434 n->val.val.str = str;
7435 n->typename = SystemTypeName("float8");
7441 makeAConst(Value *v)
7448 n = makeFloatConst(v->val.str);
7452 n = makeIntConst(v->val.ival);
7457 n = makeStringConst(v->val.str, NULL);
7465 * Create a DefElem node and set contents.
7466 * Could be moved to nodes/makefuncs.c if this is useful elsewhere.
7469 makeDefElem(char *name, Node *arg)
7471 DefElem *f = makeNode(DefElem);
7478 * Create an A_Const node and initialize to a boolean constant.
7481 makeBoolConst(bool state)
7483 A_Const *n = makeNode(A_Const);
7484 n->val.type = T_String;
7485 n->val.val.str = (state? "t": "f");
7486 n->typename = SystemTypeName("bool");
7491 * Generate separate operator nodes for a single row descriptor expression.
7492 * Perhaps this should go deeper in the parser someday...
7493 * - thomas 1997-12-22
7496 makeRowExpr(List *opr, List *largs, List *rargs)
7502 if (length(largs) != length(rargs))
7503 elog(ERROR, "Unequal number of entries in row expression");
7505 if (lnext(largs) != NIL)
7506 expr = makeRowExpr(opr, lnext(largs), lnext(rargs));
7508 larg = lfirst(largs);
7509 rarg = lfirst(rargs);
7511 oprname = strVal(llast(opr));
7513 if ((strcmp(oprname, "=") == 0) ||
7514 (strcmp(oprname, "<") == 0) ||
7515 (strcmp(oprname, "<=") == 0) ||
7516 (strcmp(oprname, ">") == 0) ||
7517 (strcmp(oprname, ">=") == 0))
7520 expr = (Node *) makeA_Expr(OP, opr, larg, rarg);
7522 expr = (Node *) makeA_Expr(AND, NIL, expr,
7523 (Node *) makeA_Expr(OP, opr,
7526 else if (strcmp(oprname, "<>") == 0)
7529 expr = (Node *) makeA_Expr(OP, opr, larg, rarg);
7531 expr = (Node *) makeA_Expr(OR, NIL, expr,
7532 (Node *) makeA_Expr(OP, opr,
7537 elog(ERROR, "Operator '%s' not implemented for row expressions",
7544 /* makeDistinctExpr()
7545 * Generate separate operator nodes for a single row descriptor expression.
7546 * Same comments as for makeRowExpr().
7549 makeDistinctExpr(List *largs, List *rargs)
7554 if (length(largs) != length(rargs))
7555 elog(ERROR, "Unequal number of entries in row expression");
7557 if (lnext(largs) != NIL)
7558 expr = makeDistinctExpr(lnext(largs), lnext(rargs));
7560 larg = lfirst(largs);
7561 rarg = lfirst(rargs);
7564 expr = (Node *) makeSimpleA_Expr(DISTINCT, "=", larg, rarg);
7566 expr = (Node *) makeA_Expr(OR, NIL, expr,
7567 (Node *) makeSimpleA_Expr(DISTINCT, "=",
7573 /* makeRowNullTest()
7574 * Generate separate operator nodes for a single row descriptor test.
7577 makeRowNullTest(NullTestType test, List *args)
7582 if (lnext(args) != NIL)
7583 expr = makeRowNullTest(test, lnext(args));
7585 n = makeNode(NullTest);
7586 n->arg = (Expr *) lfirst(args);
7587 n->nulltesttype = test;
7591 else if (test == IS_NOT_NULL)
7592 expr = (Node *) makeA_Expr(OR, NIL, expr, (Node *)n);
7594 expr = (Node *) makeA_Expr(AND, NIL, expr, (Node *)n);
7600 * Create and populate a FuncCall node to support the OVERLAPS operator.
7603 makeOverlaps(List *largs, List *rargs)
7605 FuncCall *n = makeNode(FuncCall);
7606 n->funcname = SystemFuncName("overlaps");
7607 if (length(largs) == 1)
7608 largs = lappend(largs, largs);
7609 else if (length(largs) != 2)
7610 elog(ERROR, "Wrong number of parameters"
7611 " on left side of OVERLAPS expression");
7612 if (length(rargs) == 1)
7613 rargs = lappend(rargs, rargs);
7614 else if (length(rargs) != 2)
7615 elog(ERROR, "Wrong number of parameters"
7616 " on right side of OVERLAPS expression");
7617 n->args = nconc(largs, rargs);
7618 n->agg_star = FALSE;
7619 n->agg_distinct = FALSE;
7623 /* findLeftmostSelect()
7624 * Find the leftmost component SelectStmt in a set-operation parsetree.
7627 findLeftmostSelect(SelectStmt *node)
7629 while (node && node->op != SETOP_NONE)
7631 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
7635 /* insertSelectOptions()
7636 * Insert ORDER BY, etc into an already-constructed SelectStmt.
7638 * This routine is just to avoid duplicating code in SelectStmt productions.
7641 insertSelectOptions(SelectStmt *stmt,
7642 List *sortClause, List *forUpdate,
7643 Node *limitOffset, Node *limitCount)
7646 * Tests here are to reject constructs like
7647 * (SELECT foo ORDER BY bar) ORDER BY baz
7651 if (stmt->sortClause)
7652 elog(ERROR, "Multiple ORDER BY clauses not allowed");
7653 stmt->sortClause = sortClause;
7657 if (stmt->forUpdate)
7658 elog(ERROR, "Multiple FOR UPDATE clauses not allowed");
7659 stmt->forUpdate = forUpdate;
7663 if (stmt->limitOffset)
7664 elog(ERROR, "Multiple OFFSET clauses not allowed");
7665 stmt->limitOffset = limitOffset;
7669 if (stmt->limitCount)
7670 elog(ERROR, "Multiple LIMIT clauses not allowed");
7671 stmt->limitCount = limitCount;
7676 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
7678 SelectStmt *n = makeNode(SelectStmt);
7682 n->larg = (SelectStmt *) larg;
7683 n->rarg = (SelectStmt *) rarg;
7688 * Build a properly-qualified reference to a built-in function.
7691 SystemFuncName(char *name)
7693 return makeList2(makeString("pg_catalog"), makeString(name));
7697 * Build a properly-qualified reference to a built-in type.
7699 * typmod is defaulted, but may be changed afterwards by caller.
7702 SystemTypeName(char *name)
7704 TypeName *n = makeNode(TypeName);
7706 n->names = makeList2(makeString("pg_catalog"), makeString(name));
7712 * Initialize to parse one query string
7717 QueryIsRule = FALSE;
7720 /* exprIsNullConstant()
7721 * Test whether an a_expr is a plain NULL constant or not.
7724 exprIsNullConstant(Node *arg)
7726 if (arg && IsA(arg, A_Const))
7728 A_Const *con = (A_Const *) arg;
7730 if (con->val.type == T_Null &&
7731 con->typename == NULL)
7738 * Handle negation of a numeric constant.
7740 * Formerly, we did this here because the optimizer couldn't cope with
7741 * indexquals that looked like "var = -4" --- it wants "var = const"
7742 * and a unary minus operator applied to a constant didn't qualify.
7743 * As of Postgres 7.0, that problem doesn't exist anymore because there
7744 * is a constant-subexpression simplifier in the optimizer. However,
7745 * there's still a good reason for doing this here, which is that we can
7746 * postpone committing to a particular internal representation for simple
7747 * negative constants. It's better to leave "-123.456" in string form
7748 * until we know what the desired type is.
7753 if (IsA(n, A_Const))
7755 A_Const *con = (A_Const *)n;
7757 if (con->val.type == T_Integer)
7759 con->val.val.ival = -con->val.val.ival;
7762 if (con->val.type == T_Float)
7764 doNegateFloat(&con->val);
7769 return (Node *) makeSimpleA_Expr(OP, "-", NULL, n);
7773 doNegateFloat(Value *v)
7775 char *oldval = v->val.str;
7777 Assert(IsA(v, Float));
7781 v->val.str = oldval+1; /* just strip the '-' */
7784 char *newval = (char *) palloc(strlen(oldval) + 2);
7787 strcpy(newval+1, oldval);
7788 v->val.str = newval;