4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.492 2005/06/08 21:15:28 tgl Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled inside parse_analyze() so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "nodes/makefuncs.h"
57 #include "parser/gramparse.h"
58 #include "storage/lmgr.h"
59 #include "utils/date.h"
60 #include "utils/datetime.h"
61 #include "utils/numeric.h"
64 extern List *parsetree; /* final parse result is delivered here */
66 static bool QueryIsRule = FALSE;
69 * If you need access to certain yacc-generated variables and find that
70 * they're static by default, uncomment the next line. (this is not a
73 /*#define __YYSCLASS*/
75 static Node *makeColumnRef(char *relname, List *indirection);
76 static Node *makeTypeCast(Node *arg, TypeName *typename);
77 static Node *makeStringConst(char *str, TypeName *typename);
78 static Node *makeIntConst(int val);
79 static Node *makeFloatConst(char *str);
80 static Node *makeAConst(Value *v);
81 static Node *makeRowNullTest(NullTestType test, RowExpr *row);
82 static DefElem *makeDefElem(char *name, Node *arg);
83 static A_Const *makeBoolAConst(bool state);
84 static FuncCall *makeOverlaps(List *largs, List *rargs);
85 static void check_qualified_name(List *names);
86 static List *check_func_name(List *names);
87 static List *extractArgTypes(List *parameters);
88 static SelectStmt *findLeftmostSelect(SelectStmt *node);
89 static void insertSelectOptions(SelectStmt *stmt,
90 List *sortClause, List *lockingClause,
91 Node *limitOffset, Node *limitCount);
92 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
93 static Node *doNegate(Node *n);
94 static void doNegateFloat(Value *v);
107 DropBehavior dbehavior;
108 OnCommitAction oncommit;
109 ContainsOids withoids;
116 FunctionParameter *fun_param;
117 FunctionParameterMode fun_param_mode;
118 FuncWithArgs *funwithargs;
127 PrivTarget *privtarget;
130 VariableSetStmt *vsetstmt;
133 %type <node> stmt schema_stmt
134 AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt AlterOwnerStmt
135 AlterSeqStmt AlterTableStmt AlterUserStmt AlterUserSetStmt
136 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
137 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
138 CreateDomainStmt CreateGroupStmt CreateOpClassStmt CreatePLangStmt
139 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
140 CreateAssertStmt CreateTrigStmt CreateUserStmt
141 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt
142 DropGroupStmt DropOpClassStmt DropPLangStmt DropStmt
143 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt
144 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
145 GrantStmt IndexStmt InsertStmt ListenStmt LoadStmt
146 LockStmt NotifyStmt ExplainableStmt PreparableStmt
147 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
148 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt
149 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
150 SelectStmt TransactionStmt TruncateStmt
151 UnlistenStmt UpdateStmt VacuumStmt
152 VariableResetStmt VariableSetStmt VariableShowStmt
153 ViewStmt CheckPointStmt CreateConversionStmt
154 DeallocateStmt PrepareStmt ExecuteStmt
156 %type <node> select_no_parens select_with_parens select_clause
159 %type <node> alter_column_default opclass_item alter_using
160 %type <ival> add_drop
162 %type <node> alter_table_cmd alter_rel_cmd
163 %type <list> alter_table_cmds alter_rel_cmds
165 %type <dbehavior> opt_drop_behavior
167 %type <list> createdb_opt_list copy_opt_list transaction_mode_list
168 %type <defelt> createdb_opt_item copy_opt_item transaction_mode_item
170 %type <ival> opt_lock lock_type cast_context
171 %type <boolean> opt_force opt_or_replace
172 opt_grant_grant_option opt_revoke_grant_option
175 %type <boolean> like_including_defaults
177 %type <list> user_list
179 %type <list> OptGroupList
180 %type <defelt> OptGroupElem
182 %type <list> OptUserList
183 %type <defelt> OptUserElem
185 %type <str> OptSchemaName
186 %type <list> OptSchemaEltList
188 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
189 %type <str> opt_lancompiler
191 %type <str> TriggerEvents
192 %type <value> TriggerFuncArg
194 %type <str> relation_name copy_file_name
195 database_name access_method_clause access_method attr_name
196 index_name name function_name file_name
198 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
199 opt_class opt_validator
201 %type <range> qualified_name OptConstrFromTable
203 %type <str> all_Op MathOp SpecialRuleRelation
205 %type <str> iso_level opt_encoding
207 %type <list> grantee_list
208 %type <ival> privilege
209 %type <list> privileges privilege_list
210 %type <privtarget> privilege_target
211 %type <funwithargs> function_with_argtypes
212 %type <list> function_with_argtypes_list
213 %type <chr> TriggerOneEvent
215 %type <list> stmtblock stmtmulti
216 OptTableElementList TableElementList OptInherit definition
217 opt_distinct opt_definition func_args
218 func_args_list func_as createfunc_opt_list alterfunc_opt_list
219 oper_argtypes RuleActionList RuleActionMulti
220 opt_column_list columnList opt_name_list
221 sort_clause opt_sort_clause sortby_list index_params
222 name_list from_clause from_list opt_array_bounds
223 qualified_name_list any_name any_name_list
224 any_operator expr_list attrs
225 target_list update_target_list insert_column_list
226 insert_target_list def_list indirection opt_indirection
227 group_clause TriggerFuncArgs select_limit
228 opt_select_limit opclass_item_list
229 transaction_mode_list_or_empty
231 prep_type_clause prep_type_list
232 execute_param_clause using_clause
234 %type <range> into_clause OptTempTableName
236 %type <defelt> createfunc_opt_item common_func_opt_item
237 %type <fun_param> func_arg
238 %type <fun_param_mode> arg_class
239 %type <typnam> func_return func_type aggr_argtype
241 %type <boolean> TriggerForType OptTemp
242 %type <oncommit> OnCommitOption
243 %type <withoids> OptWithOids WithOidsAs
245 %type <list> for_locking_clause opt_for_locking_clause
247 %type <boolean> opt_all
249 %type <node> join_outer join_qual
250 %type <jtype> join_type
252 %type <list> extract_list overlay_list position_list
253 %type <list> substr_list trim_list
254 %type <ival> opt_interval
255 %type <node> overlay_placing substr_from substr_for
257 %type <boolean> opt_instead opt_analyze
258 %type <boolean> index_opt_unique opt_verbose opt_full
259 %type <boolean> opt_freeze opt_default opt_recheck
260 %type <defelt> opt_binary opt_oids copy_delimiter
262 %type <boolean> copy_from opt_hold
264 %type <ival> fetch_count opt_column event cursor_options
265 %type <objtype> reindex_type drop_type comment_type
267 %type <node> fetch_direction select_limit_value select_offset_value
269 %type <list> OptSeqList
270 %type <defelt> OptSeqElem
272 %type <istmt> insert_rest
274 %type <vsetstmt> set_rest
276 %type <node> TableElement ConstraintElem TableFuncElement
277 %type <node> columnDef
278 %type <defelt> def_elem
279 %type <node> def_arg columnElem where_clause
280 a_expr b_expr c_expr func_expr AexprConst indirection_el
281 columnref in_expr having_clause func_table array_expr
282 %type <list> row type_list array_expr_list
283 %type <node> case_expr case_arg when_clause case_default
284 %type <list> when_clause_list
285 %type <ival> sub_type
286 %type <list> OptCreateAs CreateAsList
287 %type <node> CreateAsElement
288 %type <value> NumericOnly FloatOnly IntegerOnly
289 %type <alias> alias_clause
290 %type <sortby> sortby
291 %type <ielem> index_elem
292 %type <node> table_ref
293 %type <jexpr> joined_table
294 %type <range> relation_expr
295 %type <target> target_el insert_target_el update_target_el insert_column_item
297 %type <typnam> Typename SimpleTypename ConstTypename
298 GenericType Numeric opt_float
299 Character ConstCharacter
300 CharacterWithLength CharacterWithoutLength
301 ConstDatetime ConstInterval
302 Bit ConstBit BitWithLength BitWithoutLength
303 %type <str> character
304 %type <str> extract_arg
305 %type <str> opt_charset
306 %type <ival> opt_numeric opt_decimal
307 %type <boolean> opt_varying opt_timezone
310 %type <str> Sconst comment_text
311 %type <str> UserId opt_boolean ColId_or_Sconst
312 %type <list> var_list var_list_or_default
313 %type <str> ColId ColLabel var_name type_name param_name
314 %type <node> var_value zone_value
316 %type <keyword> unreserved_keyword func_name_keyword
317 %type <keyword> col_name_keyword reserved_keyword
319 %type <node> TableConstraint TableLikeClause
320 %type <list> ColQualList
321 %type <node> ColConstraint ColConstraintElem ConstraintAttr
322 %type <ival> key_actions key_delete key_match key_update key_action
323 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
326 %type <list> constraints_set_list
327 %type <boolean> constraints_set_mode
328 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
332 * If you make any token changes, update the keyword table in
333 * parser/keywords.c and add new keywords to the appropriate one of
334 * the reserved-or-not-so-reserved keyword lists, below; search
335 * this file for "Name classification hierarchy".
338 /* ordinary key words in alphabetical order */
339 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD AFTER
340 AGGREGATE ALL ALSO ALTER ANALYSE ANALYZE AND ANY ARRAY AS ASC
341 ASSERTION ASSIGNMENT AT AUTHORIZATION
343 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
346 CACHE CALLED CASCADE CASE CAST CHAIN CHAR_P
347 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
348 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
349 COMMITTED CONSTRAINT CONSTRAINTS CONVERSION_P CONVERT COPY CREATE CREATEDB
350 CREATEUSER CROSS CSV CURRENT_DATE CURRENT_TIME
351 CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
353 DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
354 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS
355 DESC DISTINCT DO DOMAIN_P DOUBLE_P DROP
357 EACH ELSE ENCODING ENCRYPTED END_P ESCAPE EXCEPT EXCLUDING
358 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
360 FALSE_P FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
361 FREEZE FROM FULL FUNCTION
365 HANDLER HAVING HEADER HOLD HOUR_P
367 ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCLUDING INCREMENT
368 INDEX INHERITS INITIALLY INNER_P INOUT INPUT_P
369 INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT
370 INTERVAL INTO INVOKER IS ISNULL ISOLATION
376 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEFT LEVEL LIKE LIMIT
377 LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
380 MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
382 NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
383 NOCREATEUSER NONE NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P
386 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
387 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNER
389 PARTIAL PASSWORD PLACING POSITION
390 PRECISION PRESERVE PREPARE PRIMARY
391 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
395 READ REAL RECHECK REFERENCES REINDEX RELATIVE_P RELEASE RENAME
396 REPEATABLE REPLACE RESET RESTART RESTRICT RETURNS REVOKE RIGHT
397 ROLLBACK ROW ROWS RULE
399 SAVEPOINT SCHEMA SCROLL SECOND_P SECURITY SELECT SEQUENCE
400 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
401 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE START STATEMENT
402 STATISTICS STDIN STDOUT STORAGE STRICT_P SUBSTRING SYSID
404 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY THEN TIME TIMESTAMP
405 TO TOAST TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
406 TRUNCATE TRUSTED TYPE_P
408 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
409 UPDATE USAGE USER USING
411 VACUUM VALID VALIDATOR VALUES VARCHAR VARYING
412 VERBOSE VIEW VOLATILE
414 WHEN WHERE WITH WITHOUT WORK WRITE
420 /* The grammar thinks these are keywords, but they are not in the keywords.c
421 * list and so can never be entered directly. The filter in parser.c
422 * creates these tokens when required.
426 /* Special token types, not actually keywords - see the "lex" file */
427 %token <str> IDENT FCONST SCONST BCONST XCONST Op
428 %token <ival> ICONST PARAM
430 /* precedence: lowest to highest */
438 %nonassoc LIKE ILIKE SIMILAR
443 %left POSTFIXOP /* dummy for postfix Op rules */
444 %left Op OPERATOR /* multi-character ops and user-defined operators */
447 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
451 /* Unary Operators */
452 %left AT ZONE /* sets precedence for AT TIME ZONE */
459 * These might seem to be low-precedence, but actually they are not part
460 * of the arithmetic hierarchy at all in their use as JOIN operators.
461 * We make them high-precedence to support their use as function names.
462 * They wouldn't be given a precedence at all, were it not that we need
463 * left-associativity among the JOIN rules themselves.
465 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
469 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
470 * psql already handles such cases, but other interfaces don't.
473 stmtblock: stmtmulti { parsetree = $1; }
476 /* the thrashing around here is to discard "empty" statements... */
477 stmtmulti: stmtmulti ';' stmt
479 $$ = lappend($1, $3);
511 | CreateConversionStmt
520 | CreateTableSpaceStmt
571 /*****************************************************************************
573 * Create a new Postgres DBMS user
576 *****************************************************************************/
579 CREATE USER UserId opt_with OptUserList
581 CreateUserStmt *n = makeNode(CreateUserStmt);
593 /*****************************************************************************
595 * Alter a postgresql DBMS user
598 *****************************************************************************/
601 ALTER USER UserId opt_with OptUserList
603 AlterUserStmt *n = makeNode(AlterUserStmt);
612 ALTER USER UserId SET set_rest
614 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
616 n->variable = $5->name;
620 | ALTER USER UserId VariableResetStmt
622 AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
624 n->variable = ((VariableResetStmt *)$4)->name;
631 /*****************************************************************************
633 * Drop a postgresql DBMS user
635 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
636 * might own objects in multiple databases, there is presently no way to
637 * implement either cascading or restricting. Caveat DBA.
638 *****************************************************************************/
643 DropUserStmt *n = makeNode(DropUserStmt);
650 * Options for CREATE USER and ALTER USER
653 OptUserList OptUserElem { $$ = lappend($1, $2); }
654 | /* EMPTY */ { $$ = NIL; }
660 $$ = makeDefElem("password", (Node *)makeString($2));
662 | ENCRYPTED PASSWORD Sconst
664 $$ = makeDefElem("encryptedPassword", (Node *)makeString($3));
666 | UNENCRYPTED PASSWORD Sconst
668 $$ = makeDefElem("unencryptedPassword", (Node *)makeString($3));
672 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
676 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
680 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
684 $$ = makeDefElem("createuser", (Node *)makeInteger(TRUE));
688 $$ = makeDefElem("createuser", (Node *)makeInteger(FALSE));
690 | IN_P GROUP_P user_list
692 $$ = makeDefElem("groupElts", (Node *)$3);
696 $$ = makeDefElem("validUntil", (Node *)makeString($3));
700 user_list: user_list ',' UserId { $$ = lappend($1, makeString($3)); }
701 | UserId { $$ = list_make1(makeString($1)); }
706 /*****************************************************************************
708 * Create a postgresql group
711 *****************************************************************************/
714 CREATE GROUP_P UserId opt_with OptGroupList
716 CreateGroupStmt *n = makeNode(CreateGroupStmt);
724 * Options for CREATE GROUP
727 OptGroupList OptGroupElem { $$ = lappend($1, $2); }
728 | /* EMPTY */ { $$ = NIL; }
734 $$ = makeDefElem("userElts", (Node *)$2);
738 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
743 /*****************************************************************************
745 * Alter a postgresql group
748 *****************************************************************************/
751 ALTER GROUP_P UserId add_drop USER user_list
753 AlterGroupStmt *n = makeNode(AlterGroupStmt);
761 add_drop: ADD { $$ = +1; }
766 /*****************************************************************************
768 * Drop a postgresql group
770 * XXX see above notes about cascading DROP USER; groups have same problem.
771 *****************************************************************************/
776 DropGroupStmt *n = makeNode(DropGroupStmt);
783 /*****************************************************************************
785 * Manipulate a schema
787 *****************************************************************************/
790 CREATE SCHEMA OptSchemaName AUTHORIZATION UserId OptSchemaEltList
792 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
793 /* One can omit the schema name or the authorization id. */
802 | CREATE SCHEMA ColId OptSchemaEltList
804 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
805 /* ...but not both */
815 | /* EMPTY */ { $$ = NULL; }
819 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
820 | /* EMPTY */ { $$ = NIL; }
824 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
825 * statement (in addition to by themselves).
837 /*****************************************************************************
839 * Set PG internal variable
840 * SET name TO 'var_value'
841 * Include SQL92 syntax (thomas 1997-10-22):
842 * SET TIME ZONE 'var_value'
844 *****************************************************************************/
849 VariableSetStmt *n = $2;
855 VariableSetStmt *n = $3;
859 | SET SESSION set_rest
861 VariableSetStmt *n = $3;
867 set_rest: var_name TO var_list_or_default
869 VariableSetStmt *n = makeNode(VariableSetStmt);
874 | var_name '=' var_list_or_default
876 VariableSetStmt *n = makeNode(VariableSetStmt);
881 | TIME ZONE zone_value
883 VariableSetStmt *n = makeNode(VariableSetStmt);
884 n->name = "timezone";
886 n->args = list_make1($3);
889 | TRANSACTION transaction_mode_list
891 VariableSetStmt *n = makeNode(VariableSetStmt);
892 n->name = "TRANSACTION";
896 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
898 VariableSetStmt *n = makeNode(VariableSetStmt);
899 n->name = "SESSION CHARACTERISTICS";
905 VariableSetStmt *n = makeNode(VariableSetStmt);
906 n->name = "client_encoding";
908 n->args = list_make1(makeStringConst($2, NULL));
911 | SESSION AUTHORIZATION ColId_or_Sconst
913 VariableSetStmt *n = makeNode(VariableSetStmt);
914 n->name = "session_authorization";
915 n->args = list_make1(makeStringConst($3, NULL));
918 | SESSION AUTHORIZATION DEFAULT
920 VariableSetStmt *n = makeNode(VariableSetStmt);
921 n->name = "session_authorization";
931 int qLen = strlen($1);
932 char* qualName = palloc(qLen + strlen($3) + 2);
933 strcpy(qualName, $1);
934 qualName[qLen] = '.';
935 strcpy(qualName + qLen + 1, $3);
941 var_list { $$ = $1; }
942 | DEFAULT { $$ = NIL; }
945 var_list: var_value { $$ = list_make1($1); }
946 | var_list ',' var_value { $$ = lappend($1, $3); }
949 var_value: opt_boolean
950 { $$ = makeStringConst($1, NULL); }
952 { $$ = makeStringConst($1, NULL); }
954 { $$ = makeAConst($1); }
957 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
958 | READ COMMITTED { $$ = "read committed"; }
959 | REPEATABLE READ { $$ = "repeatable read"; }
960 | SERIALIZABLE { $$ = "serializable"; }
964 TRUE_P { $$ = "true"; }
965 | FALSE_P { $$ = "false"; }
967 | OFF { $$ = "off"; }
970 /* Timezone values can be:
971 * - a string such as 'pst8pdt'
972 * - an identifier such as "pst8pdt"
973 * - an integer or floating point number
974 * - a time interval per SQL99
975 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
976 * so use IDENT and reject anything which is a reserved word.
981 $$ = makeStringConst($1, NULL);
985 $$ = makeStringConst($1, NULL);
987 | ConstInterval Sconst opt_interval
989 A_Const *n = (A_Const *) makeStringConst($2, $1);
990 if ($3 != INTERVAL_FULL_RANGE)
992 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
994 (errcode(ERRCODE_SYNTAX_ERROR),
995 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
996 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
1000 | ConstInterval '(' Iconst ')' Sconst opt_interval
1002 A_Const *n = (A_Const *) makeStringConst($5, $1);
1005 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1006 errmsg("INTERVAL(%d) precision must not be negative",
1008 if ($3 > MAX_INTERVAL_PRECISION)
1011 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1012 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1013 $3, MAX_INTERVAL_PRECISION)));
1014 $3 = MAX_INTERVAL_PRECISION;
1017 if (($6 != INTERVAL_FULL_RANGE)
1018 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
1020 (errcode(ERRCODE_SYNTAX_ERROR),
1021 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1023 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
1027 | NumericOnly { $$ = makeAConst($1); }
1028 | DEFAULT { $$ = NULL; }
1029 | LOCAL { $$ = NULL; }
1034 | DEFAULT { $$ = NULL; }
1035 | /*EMPTY*/ { $$ = NULL; }
1040 | SCONST { $$ = $1; }
1047 VariableShowStmt *n = makeNode(VariableShowStmt);
1053 VariableShowStmt *n = makeNode(VariableShowStmt);
1054 n->name = "timezone";
1057 | SHOW TRANSACTION ISOLATION LEVEL
1059 VariableShowStmt *n = makeNode(VariableShowStmt);
1060 n->name = "transaction_isolation";
1063 | SHOW SESSION AUTHORIZATION
1065 VariableShowStmt *n = makeNode(VariableShowStmt);
1066 n->name = "session_authorization";
1071 VariableShowStmt *n = makeNode(VariableShowStmt);
1080 VariableResetStmt *n = makeNode(VariableResetStmt);
1086 VariableResetStmt *n = makeNode(VariableResetStmt);
1087 n->name = "timezone";
1090 | RESET TRANSACTION ISOLATION LEVEL
1092 VariableResetStmt *n = makeNode(VariableResetStmt);
1093 n->name = "transaction_isolation";
1096 | RESET SESSION AUTHORIZATION
1098 VariableResetStmt *n = makeNode(VariableResetStmt);
1099 n->name = "session_authorization";
1104 VariableResetStmt *n = makeNode(VariableResetStmt);
1112 SET CONSTRAINTS constraints_set_list constraints_set_mode
1114 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1115 n->constraints = $3;
1121 constraints_set_list:
1123 | name_list { $$ = $1; }
1126 constraints_set_mode:
1127 DEFERRED { $$ = TRUE; }
1128 | IMMEDIATE { $$ = FALSE; }
1133 * Checkpoint statement
1138 CheckPointStmt *n = makeNode(CheckPointStmt);
1144 /*****************************************************************************
1146 * ALTER [ TABLE | INDEX ] variations
1148 *****************************************************************************/
1151 ALTER TABLE relation_expr alter_table_cmds
1153 AlterTableStmt *n = makeNode(AlterTableStmt);
1156 n->relkind = OBJECT_TABLE;
1159 | ALTER INDEX relation_expr alter_rel_cmds
1161 AlterTableStmt *n = makeNode(AlterTableStmt);
1164 n->relkind = OBJECT_INDEX;
1170 alter_table_cmd { $$ = list_make1($1); }
1171 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1174 /* Subcommands that are for ALTER TABLE only */
1176 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1177 ADD opt_column columnDef
1179 AlterTableCmd *n = makeNode(AlterTableCmd);
1180 n->subtype = AT_AddColumn;
1184 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1185 | ALTER opt_column ColId alter_column_default
1187 AlterTableCmd *n = makeNode(AlterTableCmd);
1188 n->subtype = AT_ColumnDefault;
1193 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1194 | ALTER opt_column ColId DROP NOT NULL_P
1196 AlterTableCmd *n = makeNode(AlterTableCmd);
1197 n->subtype = AT_DropNotNull;
1201 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1202 | ALTER opt_column ColId SET NOT NULL_P
1204 AlterTableCmd *n = makeNode(AlterTableCmd);
1205 n->subtype = AT_SetNotNull;
1209 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1210 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1212 AlterTableCmd *n = makeNode(AlterTableCmd);
1213 n->subtype = AT_SetStatistics;
1215 n->def = (Node *) $6;
1218 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1219 | ALTER opt_column ColId SET STORAGE ColId
1221 AlterTableCmd *n = makeNode(AlterTableCmd);
1222 n->subtype = AT_SetStorage;
1224 n->def = (Node *) makeString($6);
1227 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1228 | DROP opt_column ColId opt_drop_behavior
1230 AlterTableCmd *n = makeNode(AlterTableCmd);
1231 n->subtype = AT_DropColumn;
1237 * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
1238 * [ USING <expression> ]
1240 | ALTER opt_column ColId TYPE_P Typename alter_using
1242 AlterTableCmd *n = makeNode(AlterTableCmd);
1243 n->subtype = AT_AlterColumnType;
1245 n->def = (Node *) $5;
1249 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1250 | ADD TableConstraint
1252 AlterTableCmd *n = makeNode(AlterTableCmd);
1253 n->subtype = AT_AddConstraint;
1257 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1258 | DROP CONSTRAINT name opt_drop_behavior
1260 AlterTableCmd *n = makeNode(AlterTableCmd);
1261 n->subtype = AT_DropConstraint;
1266 /* ALTER TABLE <relation> SET WITHOUT OIDS */
1269 AlterTableCmd *n = makeNode(AlterTableCmd);
1270 n->subtype = AT_DropOids;
1273 /* ALTER TABLE <name> CREATE TOAST TABLE -- ONLY */
1274 | CREATE TOAST TABLE
1276 AlterTableCmd *n = makeNode(AlterTableCmd);
1277 n->subtype = AT_ToastTable;
1280 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1283 AlterTableCmd *n = makeNode(AlterTableCmd);
1284 n->subtype = AT_ClusterOn;
1288 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1289 | SET WITHOUT CLUSTER
1291 AlterTableCmd *n = makeNode(AlterTableCmd);
1292 n->subtype = AT_DropCluster;
1303 alter_rel_cmd { $$ = list_make1($1); }
1304 | alter_rel_cmds ',' alter_rel_cmd { $$ = lappend($1, $3); }
1307 /* Subcommands that are for ALTER TABLE or ALTER INDEX */
1309 /* ALTER [TABLE|INDEX] <name> OWNER TO UserId */
1312 AlterTableCmd *n = makeNode(AlterTableCmd);
1313 n->subtype = AT_ChangeOwner;
1317 /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
1318 | SET TABLESPACE name
1320 AlterTableCmd *n = makeNode(AlterTableCmd);
1321 n->subtype = AT_SetTableSpace;
1327 alter_column_default:
1330 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1331 if (exprIsNullConstant($3))
1336 | DROP DEFAULT { $$ = NULL; }
1340 CASCADE { $$ = DROP_CASCADE; }
1341 | RESTRICT { $$ = DROP_RESTRICT; }
1342 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1346 USING a_expr { $$ = $2; }
1347 | /* EMPTY */ { $$ = NULL; }
1352 /*****************************************************************************
1355 * close <portalname>
1357 *****************************************************************************/
1362 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1369 /*****************************************************************************
1372 * COPY <relname> ['(' columnList ')'] FROM/TO [WITH options]
1374 * BINARY, OIDS, and DELIMITERS kept in old locations
1375 * for backward compatibility. 2002-06-18
1377 *****************************************************************************/
1379 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1380 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1382 CopyStmt *n = makeNode(CopyStmt);
1389 /* Concatenate user-supplied flags */
1391 n->options = lappend(n->options, $2);
1393 n->options = lappend(n->options, $5);
1395 n->options = lappend(n->options, $8);
1397 n->options = list_concat(n->options, $10);
1404 | TO { $$ = FALSE; }
1408 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1409 * used depends on the direction. (It really doesn't make sense to copy from
1410 * stdout. We silently correct the "typo". - AY 9/94
1414 | STDIN { $$ = NULL; }
1415 | STDOUT { $$ = NULL; }
1421 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1422 | /* EMPTY */ { $$ = NIL; }
1429 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1433 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1435 | DELIMITER opt_as Sconst
1437 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1439 | NULL_P opt_as Sconst
1441 $$ = makeDefElem("null", (Node *)makeString($3));
1445 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1449 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1451 | QUOTE opt_as Sconst
1453 $$ = makeDefElem("quote", (Node *)makeString($3));
1455 | ESCAPE opt_as Sconst
1457 $$ = makeDefElem("escape", (Node *)makeString($3));
1459 | FORCE QUOTE columnList
1461 $$ = makeDefElem("force_quote", (Node *)$3);
1463 | FORCE NOT NULL_P columnList
1465 $$ = makeDefElem("force_notnull", (Node *)$4);
1469 /* The following exist for backward compatibility */
1474 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1476 | /*EMPTY*/ { $$ = NULL; }
1482 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1484 | /*EMPTY*/ { $$ = NULL; }
1488 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1489 opt_using DELIMITERS Sconst
1491 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1493 | /*EMPTY*/ { $$ = NULL; }
1502 /*****************************************************************************
1505 * CREATE TABLE relname
1507 *****************************************************************************/
1509 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1510 OptInherit OptWithOids OnCommitOption OptTableSpace
1512 CreateStmt *n = makeNode(CreateStmt);
1516 n->inhRelations = $8;
1517 n->constraints = NIL;
1520 n->tablespacename = $11;
1523 | CREATE OptTemp TABLE qualified_name OF qualified_name
1524 '(' OptTableElementList ')' OptWithOids OnCommitOption OptTableSpace
1526 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1527 * by our inheritance capabilities. Let's try it...
1529 CreateStmt *n = makeNode(CreateStmt);
1533 n->inhRelations = list_make1($6);
1534 n->constraints = NIL;
1537 n->tablespacename = $12;
1543 * Redundancy here is needed to avoid shift/reduce conflicts,
1544 * since TEMP is not a reserved word. See also OptTempTableName.
1546 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1547 * the LOCAL keyword is really meaningless.
1549 OptTemp: TEMPORARY { $$ = TRUE; }
1550 | TEMP { $$ = TRUE; }
1551 | LOCAL TEMPORARY { $$ = TRUE; }
1552 | LOCAL TEMP { $$ = TRUE; }
1553 | GLOBAL TEMPORARY { $$ = TRUE; }
1554 | GLOBAL TEMP { $$ = TRUE; }
1555 | /*EMPTY*/ { $$ = FALSE; }
1558 OptTableElementList:
1559 TableElementList { $$ = $1; }
1560 | /*EMPTY*/ { $$ = NIL; }
1566 $$ = list_make1($1);
1568 | TableElementList ',' TableElement
1570 $$ = lappend($1, $3);
1575 columnDef { $$ = $1; }
1576 | TableLikeClause { $$ = $1; }
1577 | TableConstraint { $$ = $1; }
1580 columnDef: ColId Typename ColQualList
1582 ColumnDef *n = makeNode(ColumnDef);
1585 n->constraints = $3;
1592 ColQualList ColConstraint { $$ = lappend($1, $2); }
1593 | /*EMPTY*/ { $$ = NIL; }
1597 CONSTRAINT name ColConstraintElem
1599 switch (nodeTag($3))
1603 Constraint *n = (Constraint *)$3;
1607 case T_FkConstraint:
1609 FkConstraint *n = (FkConstraint *)$3;
1610 n->constr_name = $2;
1618 | ColConstraintElem { $$ = $1; }
1619 | ConstraintAttr { $$ = $1; }
1622 /* DEFAULT NULL is already the default for Postgres.
1623 * But define it here and carry it forward into the system
1624 * to make it explicit.
1625 * - thomas 1998-09-13
1627 * WITH NULL and NULL are not SQL92-standard syntax elements,
1628 * so leave them out. Use DEFAULT NULL to explicitly indicate
1629 * that a column may have that value. WITH NULL leads to
1630 * shift/reduce conflicts with WITH TIME ZONE anyway.
1631 * - thomas 1999-01-08
1633 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1634 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1635 * or be part of a_expr NOT LIKE or similar constructs).
1640 Constraint *n = makeNode(Constraint);
1641 n->contype = CONSTR_NOTNULL;
1644 n->cooked_expr = NULL;
1646 n->indexspace = NULL;
1651 Constraint *n = makeNode(Constraint);
1652 n->contype = CONSTR_NULL;
1655 n->cooked_expr = NULL;
1657 n->indexspace = NULL;
1660 | UNIQUE OptConsTableSpace
1662 Constraint *n = makeNode(Constraint);
1663 n->contype = CONSTR_UNIQUE;
1666 n->cooked_expr = NULL;
1671 | PRIMARY KEY OptConsTableSpace
1673 Constraint *n = makeNode(Constraint);
1674 n->contype = CONSTR_PRIMARY;
1677 n->cooked_expr = NULL;
1682 | CHECK '(' a_expr ')'
1684 Constraint *n = makeNode(Constraint);
1685 n->contype = CONSTR_CHECK;
1688 n->cooked_expr = NULL;
1690 n->indexspace = NULL;
1695 Constraint *n = makeNode(Constraint);
1696 n->contype = CONSTR_DEFAULT;
1698 if (exprIsNullConstant($2))
1700 /* DEFAULT NULL should be reported as empty expr */
1707 n->cooked_expr = NULL;
1709 n->indexspace = NULL;
1712 | REFERENCES qualified_name opt_column_list key_match key_actions
1714 FkConstraint *n = makeNode(FkConstraint);
1715 n->constr_name = NULL;
1719 n->fk_matchtype = $4;
1720 n->fk_upd_action = (char) ($5 >> 8);
1721 n->fk_del_action = (char) ($5 & 0xFF);
1722 n->deferrable = FALSE;
1723 n->initdeferred = FALSE;
1729 * ConstraintAttr represents constraint attributes, which we parse as if
1730 * they were independent constraint clauses, in order to avoid shift/reduce
1731 * conflicts (since NOT might start either an independent NOT NULL clause
1732 * or an attribute). analyze.c is responsible for attaching the attribute
1733 * information to the preceding "real" constraint node, and for complaining
1734 * if attribute clauses appear in the wrong place or wrong combinations.
1736 * See also ConstraintAttributeSpec, which can be used in places where
1737 * there is no parsing conflict.
1742 Constraint *n = makeNode(Constraint);
1743 n->contype = CONSTR_ATTR_DEFERRABLE;
1748 Constraint *n = makeNode(Constraint);
1749 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1752 | INITIALLY DEFERRED
1754 Constraint *n = makeNode(Constraint);
1755 n->contype = CONSTR_ATTR_DEFERRED;
1758 | INITIALLY IMMEDIATE
1760 Constraint *n = makeNode(Constraint);
1761 n->contype = CONSTR_ATTR_IMMEDIATE;
1768 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
1769 * This seems to be a poor man's inheritance capability, with the resulting
1770 * tables completely decoupled except for the original commonality in definitions.
1772 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
1773 * which is a part of SQL 200N
1776 LIKE qualified_name like_including_defaults
1778 InhRelation *n = makeNode(InhRelation);
1780 n->including_defaults = $3;
1786 like_including_defaults:
1787 INCLUDING DEFAULTS { $$ = true; }
1788 | EXCLUDING DEFAULTS { $$ = false; }
1789 | /* EMPTY */ { $$ = false; }
1793 /* ConstraintElem specifies constraint syntax which is not embedded into
1794 * a column definition. ColConstraintElem specifies the embedded form.
1795 * - thomas 1997-12-03
1798 CONSTRAINT name ConstraintElem
1800 switch (nodeTag($3))
1804 Constraint *n = (Constraint *)$3;
1808 case T_FkConstraint:
1810 FkConstraint *n = (FkConstraint *)$3;
1811 n->constr_name = $2;
1819 | ConstraintElem { $$ = $1; }
1823 CHECK '(' a_expr ')'
1825 Constraint *n = makeNode(Constraint);
1826 n->contype = CONSTR_CHECK;
1829 n->cooked_expr = NULL;
1830 n->indexspace = NULL;
1833 | UNIQUE '(' columnList ')' OptConsTableSpace
1835 Constraint *n = makeNode(Constraint);
1836 n->contype = CONSTR_UNIQUE;
1839 n->cooked_expr = NULL;
1844 | PRIMARY KEY '(' columnList ')' OptConsTableSpace
1846 Constraint *n = makeNode(Constraint);
1847 n->contype = CONSTR_PRIMARY;
1850 n->cooked_expr = NULL;
1855 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
1856 opt_column_list key_match key_actions ConstraintAttributeSpec
1858 FkConstraint *n = makeNode(FkConstraint);
1859 n->constr_name = NULL;
1863 n->fk_matchtype = $9;
1864 n->fk_upd_action = (char) ($10 >> 8);
1865 n->fk_del_action = (char) ($10 & 0xFF);
1866 n->deferrable = ($11 & 1) != 0;
1867 n->initdeferred = ($11 & 2) != 0;
1873 '(' columnList ')' { $$ = $2; }
1874 | /*EMPTY*/ { $$ = NIL; }
1878 columnElem { $$ = list_make1($1); }
1879 | columnList ',' columnElem { $$ = lappend($1, $3); }
1884 $$ = (Node *) makeString($1);
1888 key_match: MATCH FULL
1890 $$ = FKCONSTR_MATCH_FULL;
1895 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1896 errmsg("MATCH PARTIAL not yet implemented")));
1897 $$ = FKCONSTR_MATCH_PARTIAL;
1901 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1905 $$ = FKCONSTR_MATCH_UNSPECIFIED;
1910 * We combine the update and delete actions into one value temporarily
1911 * for simplicity of parsing, and then break them down again in the
1912 * calling production. update is in the left 8 bits, delete in the right.
1913 * Note that NOACTION is the default.
1917 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1919 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
1920 | key_update key_delete
1921 { $$ = ($1 << 8) | ($2 & 0xFF); }
1922 | key_delete key_update
1923 { $$ = ($2 << 8) | ($1 & 0xFF); }
1925 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
1928 key_update: ON UPDATE key_action { $$ = $3; }
1931 key_delete: ON DELETE_P key_action { $$ = $3; }
1935 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
1936 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
1937 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
1938 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
1939 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
1942 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
1943 | /*EMPTY*/ { $$ = NIL; }
1947 WITH OIDS { $$ = MUST_HAVE_OIDS; }
1948 | WITHOUT OIDS { $$ = MUST_NOT_HAVE_OIDS; }
1949 | /*EMPTY*/ { $$ = DEFAULT_OIDS; }
1952 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
1953 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
1954 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
1955 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
1958 OptTableSpace: TABLESPACE name { $$ = $2; }
1959 | /*EMPTY*/ { $$ = NULL; }
1962 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
1963 | /*EMPTY*/ { $$ = NULL; }
1968 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
1973 CREATE OptTemp TABLE qualified_name OptCreateAs WithOidsAs SelectStmt
1976 * When the SelectStmt is a set-operation tree, we must
1977 * stuff the INTO information into the leftmost component
1978 * Select, because that's where analyze.c will expect
1979 * to find it. Similarly, the output column names must
1980 * be attached to that Select's target list.
1982 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
1983 if (n->into != NULL)
1985 (errcode(ERRCODE_SYNTAX_ERROR),
1986 errmsg("CREATE TABLE AS may not specify INTO")));
1989 n->intoColNames = $5;
1990 n->intoHasOids = $6;
1996 * To avoid a shift/reduce conflict in CreateAsStmt, we need to
1997 * include the 'AS' terminal in the parsing of WITH/WITHOUT
1998 * OIDS. Unfortunately that means this production is effectively a
1999 * duplicate of OptWithOids.
2002 WITH OIDS AS { $$ = MUST_HAVE_OIDS; }
2003 | WITHOUT OIDS AS { $$ = MUST_NOT_HAVE_OIDS; }
2004 | AS { $$ = DEFAULT_OIDS; }
2008 '(' CreateAsList ')' { $$ = $2; }
2009 | /*EMPTY*/ { $$ = NIL; }
2013 CreateAsElement { $$ = list_make1($1); }
2014 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2020 ColumnDef *n = makeNode(ColumnDef);
2025 n->is_not_null = false;
2026 n->raw_default = NULL;
2027 n->cooked_default = NULL;
2028 n->constraints = NIL;
2035 /*****************************************************************************
2038 * CREATE SEQUENCE seqname
2039 * ALTER SEQUENCE seqname
2041 *****************************************************************************/
2044 CREATE OptTemp SEQUENCE qualified_name OptSeqList
2046 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2055 ALTER SEQUENCE qualified_name OptSeqList
2057 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2064 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
2065 | /*EMPTY*/ { $$ = NIL; }
2068 OptSeqElem: CACHE NumericOnly
2070 $$ = makeDefElem("cache", (Node *)$2);
2074 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2078 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2080 | INCREMENT opt_by NumericOnly
2082 $$ = makeDefElem("increment", (Node *)$3);
2084 | MAXVALUE NumericOnly
2086 $$ = makeDefElem("maxvalue", (Node *)$2);
2088 | MINVALUE NumericOnly
2090 $$ = makeDefElem("minvalue", (Node *)$2);
2094 $$ = makeDefElem("maxvalue", NULL);
2098 $$ = makeDefElem("minvalue", NULL);
2100 | START opt_with NumericOnly
2102 $$ = makeDefElem("start", (Node *)$3);
2104 | RESTART opt_with NumericOnly
2106 $$ = makeDefElem("restart", (Node *)$3);
2115 FloatOnly { $$ = $1; }
2116 | IntegerOnly { $$ = $1; }
2119 FloatOnly: FCONST { $$ = makeFloat($1); }
2130 $$ = makeInteger($1);
2134 $$ = makeInteger($2);
2135 $$->val.ival = - $$->val.ival;
2139 /*****************************************************************************
2142 * CREATE PROCEDURAL LANGUAGE ...
2143 * DROP PROCEDURAL LANGUAGE ...
2145 *****************************************************************************/
2148 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2149 HANDLER handler_name opt_validator opt_lancompiler
2151 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2154 n->plvalidator = $8;
2161 TRUSTED { $$ = TRUE; }
2162 | /*EMPTY*/ { $$ = FALSE; }
2165 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2166 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2167 * Work around by using simple names, instead.
2170 name { $$ = list_make1(makeString($1)); }
2171 | name attrs { $$ = lcons(makeString($1), $2); }
2175 LANCOMPILER Sconst { $$ = $2; }
2176 | /*EMPTY*/ { $$ = ""; }
2180 VALIDATOR handler_name { $$ = $2; }
2181 | /*EMPTY*/ { $$ = NULL; }
2185 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2187 DropPLangStmt *n = makeNode(DropPLangStmt);
2199 /*****************************************************************************
2202 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2204 *****************************************************************************/
2206 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2208 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2209 n->tablespacename = $3;
2216 OptTableSpaceOwner: OWNER name { $$ = $2; }
2217 | /*EMPTY */ { $$ = NULL; }
2220 /*****************************************************************************
2223 * DROP TABLESPACE <tablespace>
2225 * No need for drop behaviour as we cannot implement dependencies for
2226 * objects in other databases; we can only support RESTRICT.
2228 ****************************************************************************/
2230 DropTableSpaceStmt: DROP TABLESPACE name
2232 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2233 n->tablespacename = $3;
2238 /*****************************************************************************
2241 * CREATE TRIGGER ...
2244 *****************************************************************************/
2247 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2248 qualified_name TriggerForSpec EXECUTE PROCEDURE
2249 func_name '(' TriggerFuncArgs ')'
2251 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2258 memcpy(n->actions, $5, 4);
2259 n->isconstraint = FALSE;
2260 n->deferrable = FALSE;
2261 n->initdeferred = FALSE;
2262 n->constrrel = NULL;
2265 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2266 qualified_name OptConstrFromTable
2267 ConstraintAttributeSpec
2268 FOR EACH ROW EXECUTE PROCEDURE
2269 func_name '(' TriggerFuncArgs ')'
2271 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2278 memcpy(n->actions, $6, 4);
2279 n->isconstraint = TRUE;
2280 n->deferrable = ($10 & 1) != 0;
2281 n->initdeferred = ($10 & 2) != 0;
2289 BEFORE { $$ = TRUE; }
2290 | AFTER { $$ = FALSE; }
2296 char *e = palloc(4);
2297 e[0] = $1; e[1] = '\0';
2300 | TriggerOneEvent OR TriggerOneEvent
2302 char *e = palloc(4);
2303 e[0] = $1; e[1] = $3; e[2] = '\0';
2306 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2308 char *e = palloc(4);
2309 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2315 INSERT { $$ = 'i'; }
2316 | DELETE_P { $$ = 'd'; }
2317 | UPDATE { $$ = 'u'; }
2321 FOR TriggerForOpt TriggerForType
2328 * If ROW/STATEMENT not specified, default to
2329 * STATEMENT, per SQL
2342 | STATEMENT { $$ = FALSE; }
2346 TriggerFuncArg { $$ = list_make1($1); }
2347 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2348 | /*EMPTY*/ { $$ = NIL; }
2355 snprintf(buf, sizeof(buf), "%d", $1);
2356 $$ = makeString(pstrdup(buf));
2358 | FCONST { $$ = makeString($1); }
2359 | Sconst { $$ = makeString($1); }
2360 | BCONST { $$ = makeString($1); }
2361 | XCONST { $$ = makeString($1); }
2362 | ColId { $$ = makeString($1); }
2366 FROM qualified_name { $$ = $2; }
2367 | /*EMPTY*/ { $$ = NULL; }
2370 ConstraintAttributeSpec:
2371 ConstraintDeferrabilitySpec
2373 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2375 if ($1 == 0 && $2 != 0)
2377 (errcode(ERRCODE_SYNTAX_ERROR),
2378 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2381 | ConstraintTimeSpec
2388 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2390 if ($2 == 0 && $1 != 0)
2392 (errcode(ERRCODE_SYNTAX_ERROR),
2393 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2400 ConstraintDeferrabilitySpec:
2401 NOT DEFERRABLE { $$ = 0; }
2402 | DEFERRABLE { $$ = 1; }
2406 INITIALLY IMMEDIATE { $$ = 0; }
2407 | INITIALLY DEFERRED { $$ = 2; }
2412 DROP TRIGGER name ON qualified_name opt_drop_behavior
2414 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2418 n->removeType = OBJECT_TRIGGER;
2424 /*****************************************************************************
2427 * CREATE ASSERTION ...
2428 * DROP ASSERTION ...
2430 *****************************************************************************/
2433 CREATE ASSERTION name CHECK '(' a_expr ')'
2434 ConstraintAttributeSpec
2436 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2438 n->args = list_make1($6);
2439 n->isconstraint = TRUE;
2440 n->deferrable = ($8 & 1) != 0;
2441 n->initdeferred = ($8 & 2) != 0;
2444 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2445 errmsg("CREATE ASSERTION is not yet implemented")));
2452 DROP ASSERTION name opt_drop_behavior
2454 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2458 n->removeType = OBJECT_TRIGGER; /* XXX */
2460 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2461 errmsg("DROP ASSERTION is not yet implemented")));
2467 /*****************************************************************************
2470 * define (aggregate,operator,type)
2472 *****************************************************************************/
2475 CREATE AGGREGATE func_name definition
2477 DefineStmt *n = makeNode(DefineStmt);
2478 n->kind = OBJECT_AGGREGATE;
2483 | CREATE OPERATOR any_operator definition
2485 DefineStmt *n = makeNode(DefineStmt);
2486 n->kind = OBJECT_OPERATOR;
2491 | CREATE TYPE_P any_name definition
2493 DefineStmt *n = makeNode(DefineStmt);
2494 n->kind = OBJECT_TYPE;
2499 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2501 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2502 RangeVar *r = makeNode(RangeVar);
2504 /* can't use qualified_name, sigh */
2505 switch (list_length($3))
2508 r->catalogname = NULL;
2509 r->schemaname = NULL;
2510 r->relname = strVal(linitial($3));
2513 r->catalogname = NULL;
2514 r->schemaname = strVal(linitial($3));
2515 r->relname = strVal(lsecond($3));
2518 r->catalogname = strVal(linitial($3));
2519 r->schemaname = strVal(lsecond($3));
2520 r->relname = strVal(lthird($3));
2524 (errcode(ERRCODE_SYNTAX_ERROR),
2525 errmsg("improper qualified name (too many dotted names): %s",
2526 NameListToString($3))));
2535 definition: '(' def_list ')' { $$ = $2; }
2538 def_list: def_elem { $$ = list_make1($1); }
2539 | def_list ',' def_elem { $$ = lappend($1, $3); }
2542 def_elem: ColLabel '=' def_arg
2544 $$ = makeDefElem($1, (Node *)$3);
2548 $$ = makeDefElem($1, NULL);
2552 /* Note: any simple identifier will be returned as a type name! */
2553 def_arg: func_type { $$ = (Node *)$1; }
2554 | qual_all_Op { $$ = (Node *)$1; }
2555 | NumericOnly { $$ = (Node *)$1; }
2556 | Sconst { $$ = (Node *)makeString($1); }
2560 /*****************************************************************************
2563 * CREATE OPERATOR CLASS ...
2564 * DROP OPERATOR CLASS ...
2566 *****************************************************************************/
2569 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
2570 USING access_method AS opclass_item_list
2572 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
2573 n->opclassname = $4;
2583 opclass_item { $$ = list_make1($1); }
2584 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
2588 OPERATOR Iconst any_operator opt_recheck
2590 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2591 n->itemtype = OPCLASS_ITEM_OPERATOR;
2598 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
2600 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2601 n->itemtype = OPCLASS_ITEM_OPERATOR;
2608 | FUNCTION Iconst func_name func_args
2610 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2611 n->itemtype = OPCLASS_ITEM_FUNCTION;
2613 n->args = extractArgTypes($4);
2619 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2620 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
2626 opt_default: DEFAULT { $$ = TRUE; }
2627 | /*EMPTY*/ { $$ = FALSE; }
2630 opt_recheck: RECHECK { $$ = TRUE; }
2631 | /*EMPTY*/ { $$ = FALSE; }
2636 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
2638 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
2639 n->opclassname = $4;
2647 /*****************************************************************************
2651 * DROP itemtype itemname [, itemname ...] [ RESTRICT | CASCADE ]
2653 *****************************************************************************/
2655 DropStmt: DROP drop_type any_name_list opt_drop_behavior
2657 DropStmt *n = makeNode(DropStmt);
2665 drop_type: TABLE { $$ = OBJECT_TABLE; }
2666 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
2667 | VIEW { $$ = OBJECT_VIEW; }
2668 | INDEX { $$ = OBJECT_INDEX; }
2669 | TYPE_P { $$ = OBJECT_TYPE; }
2670 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
2671 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
2672 | SCHEMA { $$ = OBJECT_SCHEMA; }
2676 any_name { $$ = list_make1($1); }
2677 | any_name_list ',' any_name { $$ = lappend($1, $3); }
2680 any_name: ColId { $$ = list_make1(makeString($1)); }
2681 | ColId attrs { $$ = lcons(makeString($1), $2); }
2684 attrs: '.' attr_name
2685 { $$ = list_make1(makeString($2)); }
2686 | attrs '.' attr_name
2687 { $$ = lappend($1, makeString($3)); }
2691 /*****************************************************************************
2694 * truncate table relname1, relname2, ...
2696 *****************************************************************************/
2699 TRUNCATE opt_table qualified_name_list
2701 TruncateStmt *n = makeNode(TruncateStmt);
2707 /*****************************************************************************
2709 * The COMMENT ON statement can take different forms based upon the type of
2710 * the object associated with the comment. The form of the statement is:
2712 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
2713 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
2714 * CAST ] <objname> |
2715 * AGGREGATE <aggname> (<aggtype>) |
2716 * FUNCTION <funcname> (arg1, arg2, ...) |
2717 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
2718 * TRIGGER <triggername> ON <relname> |
2719 * RULE <rulename> ON <relname> ]
2722 *****************************************************************************/
2725 COMMENT ON comment_type any_name IS comment_text
2727 CommentStmt *n = makeNode(CommentStmt);
2734 | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')'
2737 CommentStmt *n = makeNode(CommentStmt);
2738 n->objtype = OBJECT_AGGREGATE;
2740 n->objargs = list_make1($6);
2744 | COMMENT ON FUNCTION func_name func_args IS comment_text
2746 CommentStmt *n = makeNode(CommentStmt);
2747 n->objtype = OBJECT_FUNCTION;
2749 n->objargs = extractArgTypes($5);
2753 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
2756 CommentStmt *n = makeNode(CommentStmt);
2757 n->objtype = OBJECT_OPERATOR;
2763 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
2765 CommentStmt *n = makeNode(CommentStmt);
2766 n->objtype = OBJECT_CONSTRAINT;
2767 n->objname = lappend($6, makeString($4));
2772 | COMMENT ON RULE name ON any_name IS comment_text
2774 CommentStmt *n = makeNode(CommentStmt);
2775 n->objtype = OBJECT_RULE;
2776 n->objname = lappend($6, makeString($4));
2781 | COMMENT ON RULE name IS comment_text
2783 /* Obsolete syntax supported for awhile for compatibility */
2784 CommentStmt *n = makeNode(CommentStmt);
2785 n->objtype = OBJECT_RULE;
2786 n->objname = list_make1(makeString($4));
2791 | COMMENT ON TRIGGER name ON any_name IS comment_text
2793 CommentStmt *n = makeNode(CommentStmt);
2794 n->objtype = OBJECT_TRIGGER;
2795 n->objname = lappend($6, makeString($4));
2800 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
2802 CommentStmt *n = makeNode(CommentStmt);
2803 n->objtype = OBJECT_OPCLASS;
2805 n->objargs = list_make1(makeString($7));
2809 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
2811 CommentStmt *n = makeNode(CommentStmt);
2812 n->objtype = OBJECT_LARGEOBJECT;
2813 n->objname = list_make1($5);
2818 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
2820 CommentStmt *n = makeNode(CommentStmt);
2821 n->objtype = OBJECT_CAST;
2822 n->objname = list_make1($5);
2823 n->objargs = list_make1($7);
2827 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
2829 CommentStmt *n = makeNode(CommentStmt);
2830 n->objtype = OBJECT_LANGUAGE;
2839 COLUMN { $$ = OBJECT_COLUMN; }
2840 | DATABASE { $$ = OBJECT_DATABASE; }
2841 | SCHEMA { $$ = OBJECT_SCHEMA; }
2842 | INDEX { $$ = OBJECT_INDEX; }
2843 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
2844 | TABLE { $$ = OBJECT_TABLE; }
2845 | DOMAIN_P { $$ = OBJECT_TYPE; }
2846 | TYPE_P { $$ = OBJECT_TYPE; }
2847 | VIEW { $$ = OBJECT_VIEW; }
2848 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
2853 | NULL_P { $$ = NULL; }
2856 /*****************************************************************************
2861 *****************************************************************************/
2863 FetchStmt: FETCH fetch_direction from_in name
2865 FetchStmt *n = (FetchStmt *) $2;
2872 FetchStmt *n = makeNode(FetchStmt);
2873 n->direction = FETCH_FORWARD;
2879 | MOVE fetch_direction from_in name
2881 FetchStmt *n = (FetchStmt *) $2;
2888 FetchStmt *n = makeNode(FetchStmt);
2889 n->direction = FETCH_FORWARD;
2900 FetchStmt *n = makeNode(FetchStmt);
2901 n->direction = FETCH_FORWARD;
2907 FetchStmt *n = makeNode(FetchStmt);
2908 n->direction = FETCH_FORWARD;
2914 FetchStmt *n = makeNode(FetchStmt);
2915 n->direction = FETCH_BACKWARD;
2921 FetchStmt *n = makeNode(FetchStmt);
2922 n->direction = FETCH_ABSOLUTE;
2928 FetchStmt *n = makeNode(FetchStmt);
2929 n->direction = FETCH_ABSOLUTE;
2933 | ABSOLUTE_P fetch_count
2935 FetchStmt *n = makeNode(FetchStmt);
2936 n->direction = FETCH_ABSOLUTE;
2940 | RELATIVE_P fetch_count
2942 FetchStmt *n = makeNode(FetchStmt);
2943 n->direction = FETCH_RELATIVE;
2949 FetchStmt *n = makeNode(FetchStmt);
2950 n->direction = FETCH_FORWARD;
2956 FetchStmt *n = makeNode(FetchStmt);
2957 n->direction = FETCH_FORWARD;
2958 n->howMany = FETCH_ALL;
2963 FetchStmt *n = makeNode(FetchStmt);
2964 n->direction = FETCH_FORWARD;
2968 | FORWARD fetch_count
2970 FetchStmt *n = makeNode(FetchStmt);
2971 n->direction = FETCH_FORWARD;
2977 FetchStmt *n = makeNode(FetchStmt);
2978 n->direction = FETCH_FORWARD;
2979 n->howMany = FETCH_ALL;
2984 FetchStmt *n = makeNode(FetchStmt);
2985 n->direction = FETCH_BACKWARD;
2989 | BACKWARD fetch_count
2991 FetchStmt *n = makeNode(FetchStmt);
2992 n->direction = FETCH_BACKWARD;
2998 FetchStmt *n = makeNode(FetchStmt);
2999 n->direction = FETCH_BACKWARD;
3000 n->howMany = FETCH_ALL;
3007 | '-' Iconst { $$ = - $2; }
3015 /*****************************************************************************
3017 * GRANT and REVOKE statements
3019 *****************************************************************************/
3021 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3022 opt_grant_grant_option
3024 GrantStmt *n = makeNode(GrantStmt);
3027 n->objtype = ($4)->objtype;
3028 n->objects = ($4)->objs;
3030 n->grant_option = $7;
3035 RevokeStmt: REVOKE opt_revoke_grant_option privileges ON privilege_target
3036 FROM grantee_list opt_drop_behavior
3038 GrantStmt *n = makeNode(GrantStmt);
3039 n->is_grant = false;
3041 n->objtype = ($5)->objtype;
3042 n->objects = ($5)->objs;
3044 n->grant_option = $2;
3052 /* either ALL [PRIVILEGES] or a list of individual privileges */
3053 privileges: privilege_list { $$ = $1; }
3054 | ALL { $$ = list_make1_int(ACL_ALL_RIGHTS); }
3055 | ALL PRIVILEGES { $$ = list_make1_int(ACL_ALL_RIGHTS); }
3059 privilege { $$ = list_make1_int($1); }
3060 | privilege_list ',' privilege { $$ = lappend_int($1, $3); }
3063 /* Not all of these privilege types apply to all objects, but that
3064 * gets sorted out later.
3066 privilege: SELECT { $$ = ACL_SELECT; }
3067 | INSERT { $$ = ACL_INSERT; }
3068 | UPDATE { $$ = ACL_UPDATE; }
3069 | DELETE_P { $$ = ACL_DELETE; }
3070 | RULE { $$ = ACL_RULE; }
3071 | REFERENCES { $$ = ACL_REFERENCES; }
3072 | TRIGGER { $$ = ACL_TRIGGER; }
3073 | EXECUTE { $$ = ACL_EXECUTE; }
3074 | USAGE { $$ = ACL_USAGE; }
3075 | CREATE { $$ = ACL_CREATE; }
3076 | TEMPORARY { $$ = ACL_CREATE_TEMP; }
3077 | TEMP { $$ = ACL_CREATE_TEMP; }
3081 /* Don't bother trying to fold the first two rules into one using
3082 opt_table. You're going to get conflicts. */
3086 PrivTarget *n = makeNode(PrivTarget);
3087 n->objtype = ACL_OBJECT_RELATION;
3091 | TABLE qualified_name_list
3093 PrivTarget *n = makeNode(PrivTarget);
3094 n->objtype = ACL_OBJECT_RELATION;
3098 | FUNCTION function_with_argtypes_list
3100 PrivTarget *n = makeNode(PrivTarget);
3101 n->objtype = ACL_OBJECT_FUNCTION;
3105 | DATABASE name_list
3107 PrivTarget *n = makeNode(PrivTarget);
3108 n->objtype = ACL_OBJECT_DATABASE;
3112 | LANGUAGE name_list
3114 PrivTarget *n = makeNode(PrivTarget);
3115 n->objtype = ACL_OBJECT_LANGUAGE;
3121 PrivTarget *n = makeNode(PrivTarget);
3122 n->objtype = ACL_OBJECT_NAMESPACE;
3126 | TABLESPACE name_list
3128 PrivTarget *n = makeNode(PrivTarget);
3129 n->objtype = ACL_OBJECT_TABLESPACE;
3137 grantee { $$ = list_make1($1); }
3138 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3143 PrivGrantee *n = makeNode(PrivGrantee);
3144 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3145 if (strcmp($1, "public") == 0)
3149 n->groupname = NULL;
3154 PrivGrantee *n = makeNode(PrivGrantee);
3155 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3156 if (strcmp($2, "public") == 0)
3157 n->groupname = NULL;
3166 opt_grant_grant_option:
3167 WITH GRANT OPTION { $$ = TRUE; }
3168 | /*EMPTY*/ { $$ = FALSE; }
3171 opt_revoke_grant_option:
3172 GRANT OPTION FOR { $$ = TRUE; }
3173 | /*EMPTY*/ { $$ = FALSE; }
3177 function_with_argtypes_list:
3178 function_with_argtypes { $$ = list_make1($1); }
3179 | function_with_argtypes_list ',' function_with_argtypes
3180 { $$ = lappend($1, $3); }
3183 function_with_argtypes:
3186 FuncWithArgs *n = makeNode(FuncWithArgs);
3188 n->funcargs = extractArgTypes($2);
3194 /*****************************************************************************
3197 * create index <indexname> on <relname>
3198 * [ using <access> ] "(" ( <col> [ using <opclass> ] )+ ")"
3199 * [ tablespace <tablespacename> ] [ where <predicate> ]
3201 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
3202 * willing to make TABLESPACE a fully reserved word.
3203 *****************************************************************************/
3205 IndexStmt: CREATE index_opt_unique INDEX index_name ON qualified_name
3206 access_method_clause '(' index_params ')' OptTableSpace where_clause
3208 IndexStmt *n = makeNode(IndexStmt);
3212 n->accessMethod = $7;
3213 n->indexParams = $9;
3214 n->tableSpace = $11;
3215 n->whereClause = $12;
3221 UNIQUE { $$ = TRUE; }
3222 | /*EMPTY*/ { $$ = FALSE; }
3225 access_method_clause:
3226 USING access_method { $$ = $2; }
3227 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
3230 index_params: index_elem { $$ = list_make1($1); }
3231 | index_params ',' index_elem { $$ = lappend($1, $3); }
3235 * Index attributes can be either simple column references, or arbitrary
3236 * expressions in parens. For backwards-compatibility reasons, we allow
3237 * an expression that's just a function call to be written without parens.
3239 index_elem: ColId opt_class
3241 $$ = makeNode(IndexElem);
3246 | func_expr opt_class
3248 $$ = makeNode(IndexElem);
3253 | '(' a_expr ')' opt_class
3255 $$ = makeNode(IndexElem);
3262 opt_class: any_name { $$ = $1; }
3263 | USING any_name { $$ = $2; }
3264 | /*EMPTY*/ { $$ = NIL; }
3267 /*****************************************************************************
3270 * create [or replace] function <fname>
3271 * [(<type-1> { , <type-n>})]
3273 * as <filename or code in language as appropriate>
3274 * language <lang> [with parameters]
3276 *****************************************************************************/
3279 CREATE opt_or_replace FUNCTION func_name func_args
3280 RETURNS func_return createfunc_opt_list opt_definition
3282 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3291 | CREATE opt_or_replace FUNCTION func_name func_args
3292 createfunc_opt_list opt_definition
3294 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3298 n->returnType = NULL;
3306 OR REPLACE { $$ = TRUE; }
3307 | /*EMPTY*/ { $$ = FALSE; }
3310 func_args: '(' func_args_list ')' { $$ = $2; }
3311 | '(' ')' { $$ = NIL; }
3315 func_arg { $$ = list_make1($1); }
3316 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
3320 * The style with arg_class first is SQL99 standard, but Oracle puts
3321 * param_name first; accept both since it's likely people will try both
3322 * anyway. Don't bother trying to save productions by letting arg_class
3323 * have an empty alternative ... you'll get shift/reduce conflicts.
3325 * We can catch over-specified arguments here if we want to,
3326 * but for now better to silently swallow typmod, etc.
3327 * - thomas 2000-03-22
3330 arg_class param_name func_type
3332 FunctionParameter *n = makeNode(FunctionParameter);
3338 | param_name arg_class func_type
3340 FunctionParameter *n = makeNode(FunctionParameter);
3346 | param_name func_type
3348 FunctionParameter *n = makeNode(FunctionParameter);
3351 n->mode = FUNC_PARAM_IN;
3354 | arg_class func_type
3356 FunctionParameter *n = makeNode(FunctionParameter);
3364 FunctionParameter *n = makeNode(FunctionParameter);
3367 n->mode = FUNC_PARAM_IN;
3372 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
3373 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
3374 | OUT_P { $$ = FUNC_PARAM_OUT; }
3375 | INOUT { $$ = FUNC_PARAM_INOUT; }
3376 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
3380 * Ideally param_name should be ColId, but that causes too many conflicts.
3382 param_name: function_name
3388 /* We can catch over-specified results here if we want to,
3389 * but for now better to silently swallow typmod, etc.
3390 * - thomas 2000-03-22
3397 * We would like to make the second production here be ColId attrs etc,
3398 * but that causes reduce/reduce conflicts. type_name is next best choice.
3400 func_type: Typename { $$ = $1; }
3401 | type_name attrs '%' TYPE_P
3403 $$ = makeNode(TypeName);
3404 $$->names = lcons(makeString($1), $2);
3405 $$->pct_type = true;
3411 createfunc_opt_list:
3412 /* Must be at least one to prevent conflict */
3413 createfunc_opt_item { $$ = list_make1($1); }
3414 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
3418 * Options common to both CREATE FUNCTION and ALTER FUNCTION
3420 common_func_opt_item:
3421 CALLED ON NULL_P INPUT_P
3423 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
3425 | RETURNS NULL_P ON NULL_P INPUT_P
3427 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3431 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3435 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
3439 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
3443 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
3445 | EXTERNAL SECURITY DEFINER
3447 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3449 | EXTERNAL SECURITY INVOKER
3451 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3455 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3459 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3463 createfunc_opt_item:
3466 $$ = makeDefElem("as", (Node *)$2);
3468 | LANGUAGE ColId_or_Sconst
3470 $$ = makeDefElem("language", (Node *)makeString($2));
3472 | common_func_opt_item
3478 func_as: Sconst { $$ = list_make1(makeString($1)); }
3481 $$ = list_make2(makeString($1), makeString($3));
3486 WITH definition { $$ = $2; }
3487 | /*EMPTY*/ { $$ = NIL; }
3490 /*****************************************************************************
3493 * RENAME and OWNER subcommands are already provided by the generic
3494 * ALTER infrastructure, here we just specify alterations that can
3495 * only be applied to functions.
3497 *****************************************************************************/
3499 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
3501 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
3509 /* At least one option must be specified */
3510 common_func_opt_item { $$ = list_make1($1); }
3511 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
3514 /* Ignored, merely for SQL compliance */
3521 /*****************************************************************************
3525 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
3526 * DROP AGGREGATE aggname (aggtype) [ RESTRICT | CASCADE ]
3527 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
3529 *****************************************************************************/
3532 DROP FUNCTION func_name func_args opt_drop_behavior
3534 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
3536 n->args = extractArgTypes($4);
3543 DROP AGGREGATE func_name '(' aggr_argtype ')' opt_drop_behavior
3545 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
3554 Typename { $$ = $1; }
3555 | '*' { $$ = NULL; }
3559 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
3561 RemoveOperStmt *n = makeNode(RemoveOperStmt);
3573 (errcode(ERRCODE_SYNTAX_ERROR),
3574 errmsg("missing argument"),
3575 errhint("Use NONE to denote the missing argument of a unary operator.")));
3577 | Typename ',' Typename
3578 { $$ = list_make2($1, $3); }
3579 | NONE ',' Typename /* left unary */
3580 { $$ = list_make2(NULL, $3); }
3581 | Typename ',' NONE /* right unary */
3582 { $$ = list_make2($1, NULL); }
3587 { $$ = list_make1(makeString($1)); }
3588 | ColId '.' any_operator
3589 { $$ = lcons(makeString($1), $3); }
3593 /*****************************************************************************
3595 * CREATE CAST / DROP CAST
3597 *****************************************************************************/
3599 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
3600 WITH FUNCTION function_with_argtypes cast_context
3602 CreateCastStmt *n = makeNode(CreateCastStmt);
3606 n->context = (CoercionContext) $11;
3609 | CREATE CAST '(' Typename AS Typename ')'
3610 WITHOUT FUNCTION cast_context
3612 CreateCastStmt *n = makeNode(CreateCastStmt);
3616 n->context = (CoercionContext) $10;
3621 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
3622 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
3623 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
3627 DropCastStmt: DROP CAST '(' Typename AS Typename ')' opt_drop_behavior
3629 DropCastStmt *n = makeNode(DropCastStmt);
3639 /*****************************************************************************
3643 * REINDEX type <typename> [FORCE] [ALL]
3645 *****************************************************************************/
3648 REINDEX reindex_type qualified_name opt_force
3650 ReindexStmt *n = makeNode(ReindexStmt);
3657 | REINDEX DATABASE name opt_force
3659 ReindexStmt *n = makeNode(ReindexStmt);
3660 n->kind = OBJECT_DATABASE;
3669 INDEX { $$ = OBJECT_INDEX; }
3670 | TABLE { $$ = OBJECT_TABLE; }
3673 opt_force: FORCE { $$ = TRUE; }
3674 | /* EMPTY */ { $$ = FALSE; }
3678 /*****************************************************************************
3680 * ALTER THING name RENAME TO newname
3682 *****************************************************************************/
3684 RenameStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' RENAME TO name
3686 RenameStmt *n = makeNode(RenameStmt);
3687 n->renameType = OBJECT_AGGREGATE;
3689 n->objarg = list_make1($5);
3693 | ALTER CONVERSION_P any_name RENAME TO name
3695 RenameStmt *n = makeNode(RenameStmt);
3696 n->renameType = OBJECT_CONVERSION;
3701 | ALTER DATABASE database_name RENAME TO database_name
3703 RenameStmt *n = makeNode(RenameStmt);
3704 n->renameType = OBJECT_DATABASE;
3709 | ALTER FUNCTION func_name func_args RENAME TO name
3711 RenameStmt *n = makeNode(RenameStmt);
3712 n->renameType = OBJECT_FUNCTION;
3714 n->objarg = extractArgTypes($4);
3718 | ALTER GROUP_P UserId RENAME TO UserId
3720 RenameStmt *n = makeNode(RenameStmt);
3721 n->renameType = OBJECT_GROUP;
3726 | ALTER LANGUAGE name RENAME TO name
3728 RenameStmt *n = makeNode(RenameStmt);
3729 n->renameType = OBJECT_LANGUAGE;
3734 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
3736 RenameStmt *n = makeNode(RenameStmt);
3737 n->renameType = OBJECT_OPCLASS;
3743 | ALTER SCHEMA name RENAME TO name
3745 RenameStmt *n = makeNode(RenameStmt);
3746 n->renameType = OBJECT_SCHEMA;
3751 | ALTER TABLE relation_expr RENAME TO name
3753 RenameStmt *n = makeNode(RenameStmt);
3754 n->renameType = OBJECT_TABLE;
3760 | ALTER INDEX relation_expr RENAME TO name
3762 RenameStmt *n = makeNode(RenameStmt);
3763 n->renameType = OBJECT_INDEX;
3769 | ALTER TABLE relation_expr RENAME opt_column name TO name
3771 RenameStmt *n = makeNode(RenameStmt);
3772 n->renameType = OBJECT_COLUMN;
3778 | ALTER TRIGGER name ON relation_expr RENAME TO name
3780 RenameStmt *n = makeNode(RenameStmt);
3784 n->renameType = OBJECT_TRIGGER;
3787 | ALTER USER UserId RENAME TO UserId
3789 RenameStmt *n = makeNode(RenameStmt);
3790 n->renameType = OBJECT_USER;
3795 | ALTER TABLESPACE name RENAME TO name
3797 RenameStmt *n = makeNode(RenameStmt);
3798 n->renameType = OBJECT_TABLESPACE;
3805 opt_column: COLUMN { $$ = COLUMN; }
3806 | /*EMPTY*/ { $$ = 0; }
3810 /*****************************************************************************
3812 * ALTER THING name OWNER TO newname.
3814 *****************************************************************************/
3816 AlterOwnerStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' OWNER TO UserId
3818 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3819 n->objectType = OBJECT_AGGREGATE;
3821 n->objarg = list_make1($5);
3825 | ALTER CONVERSION_P any_name OWNER TO UserId
3827 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3828 n->objectType = OBJECT_CONVERSION;
3833 | ALTER DATABASE database_name OWNER TO UserId
3835 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3836 n->objectType = OBJECT_DATABASE;
3837 n->object = list_make1($3);
3841 | ALTER DOMAIN_P any_name OWNER TO UserId
3843 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3844 n->objectType = OBJECT_DOMAIN;
3849 | ALTER FUNCTION func_name func_args OWNER TO UserId
3851 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3852 n->objectType = OBJECT_FUNCTION;
3854 n->objarg = extractArgTypes($4);
3858 | ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO UserId
3860 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3861 n->objectType = OBJECT_OPERATOR;
3867 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO UserId
3869 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3870 n->objectType = OBJECT_OPCLASS;
3876 | ALTER SCHEMA name OWNER TO UserId
3878 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3879 n->objectType = OBJECT_SCHEMA;
3880 n->object = list_make1($3);
3884 | ALTER TYPE_P any_name OWNER TO UserId
3886 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3887 n->objectType = OBJECT_TYPE;
3892 | ALTER TABLESPACE name OWNER TO UserId
3894 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
3895 n->objectType = OBJECT_TABLESPACE;
3896 n->object = list_make1($3);
3903 /*****************************************************************************
3905 * QUERY: Define Rewrite Rule
3907 *****************************************************************************/
3909 RuleStmt: CREATE opt_or_replace RULE name AS
3910 { QueryIsRule=TRUE; }
3911 ON event TO qualified_name where_clause
3912 DO opt_instead RuleActionList
3914 RuleStmt *n = makeNode(RuleStmt);
3918 n->whereClause = $11;
3928 NOTHING { $$ = NIL; }
3929 | RuleActionStmt { $$ = list_make1($1); }
3930 | '(' RuleActionMulti ')' { $$ = $2; }
3933 /* the thrashing around here is to discard "empty" statements... */
3935 RuleActionMulti ';' RuleActionStmtOrEmpty
3937 $$ = lappend($1, $3);
3941 | RuleActionStmtOrEmpty
3943 $$ = list_make1($1);
3957 RuleActionStmtOrEmpty:
3958 RuleActionStmt { $$ = $1; }
3959 | /*EMPTY*/ { $$ = NULL; }
3962 /* change me to select, update, etc. some day */
3963 event: SELECT { $$ = CMD_SELECT; }
3964 | UPDATE { $$ = CMD_UPDATE; }
3965 | DELETE_P { $$ = CMD_DELETE; }
3966 | INSERT { $$ = CMD_INSERT; }
3970 INSTEAD { $$ = TRUE; }
3971 | ALSO { $$ = FALSE; }
3972 | /*EMPTY*/ { $$ = FALSE; }
3977 DROP RULE name ON qualified_name opt_drop_behavior
3979 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3983 n->removeType = OBJECT_RULE;
3989 /*****************************************************************************
3992 * NOTIFY <qualified_name> can appear both in rule bodies and
3993 * as a query-level command
3995 *****************************************************************************/
3997 NotifyStmt: NOTIFY qualified_name
3999 NotifyStmt *n = makeNode(NotifyStmt);
4005 ListenStmt: LISTEN qualified_name
4007 ListenStmt *n = makeNode(ListenStmt);
4014 UNLISTEN qualified_name
4016 UnlistenStmt *n = makeNode(UnlistenStmt);
4022 UnlistenStmt *n = makeNode(UnlistenStmt);
4023 n->relation = makeNode(RangeVar);
4024 n->relation->relname = "*";
4025 n->relation->schemaname = NULL;
4031 /*****************************************************************************
4035 * BEGIN / COMMIT / ROLLBACK
4036 * (also older versions END / ABORT)
4038 *****************************************************************************/
4041 ABORT_P opt_transaction
4043 TransactionStmt *n = makeNode(TransactionStmt);
4044 n->kind = TRANS_STMT_ROLLBACK;
4048 | BEGIN_P opt_transaction transaction_mode_list_or_empty
4050 TransactionStmt *n = makeNode(TransactionStmt);
4051 n->kind = TRANS_STMT_BEGIN;
4055 | START TRANSACTION transaction_mode_list_or_empty
4057 TransactionStmt *n = makeNode(TransactionStmt);
4058 n->kind = TRANS_STMT_START;
4062 | COMMIT opt_transaction
4064 TransactionStmt *n = makeNode(TransactionStmt);
4065 n->kind = TRANS_STMT_COMMIT;
4069 | END_P opt_transaction
4071 TransactionStmt *n = makeNode(TransactionStmt);
4072 n->kind = TRANS_STMT_COMMIT;
4076 | ROLLBACK opt_transaction
4078 TransactionStmt *n = makeNode(TransactionStmt);
4079 n->kind = TRANS_STMT_ROLLBACK;
4085 TransactionStmt *n = makeNode(TransactionStmt);
4086 n->kind = TRANS_STMT_SAVEPOINT;
4087 n->options = list_make1(makeDefElem("savepoint_name",
4088 (Node *)makeString($2)));
4091 | RELEASE SAVEPOINT ColId
4093 TransactionStmt *n = makeNode(TransactionStmt);
4094 n->kind = TRANS_STMT_RELEASE;
4095 n->options = list_make1(makeDefElem("savepoint_name",
4096 (Node *)makeString($3)));
4101 TransactionStmt *n = makeNode(TransactionStmt);
4102 n->kind = TRANS_STMT_RELEASE;
4103 n->options = list_make1(makeDefElem("savepoint_name",
4104 (Node *)makeString($2)));
4107 | ROLLBACK opt_transaction TO SAVEPOINT ColId
4109 TransactionStmt *n = makeNode(TransactionStmt);
4110 n->kind = TRANS_STMT_ROLLBACK_TO;
4111 n->options = list_make1(makeDefElem("savepoint_name",
4112 (Node *)makeString($5)));
4115 | ROLLBACK opt_transaction TO ColId
4117 TransactionStmt *n = makeNode(TransactionStmt);
4118 n->kind = TRANS_STMT_ROLLBACK_TO;
4119 n->options = list_make1(makeDefElem("savepoint_name",
4120 (Node *)makeString($4)));
4125 opt_transaction: WORK {}
4130 transaction_mode_item:
4131 ISOLATION LEVEL iso_level
4132 { $$ = makeDefElem("transaction_isolation",
4133 makeStringConst($3, NULL)); }
4135 { $$ = makeDefElem("transaction_read_only",
4136 makeIntConst(TRUE)); }
4138 { $$ = makeDefElem("transaction_read_only",
4139 makeIntConst(FALSE)); }
4142 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
4143 transaction_mode_list:
4144 transaction_mode_item
4145 { $$ = list_make1($1); }
4146 | transaction_mode_list ',' transaction_mode_item
4147 { $$ = lappend($1, $3); }
4148 | transaction_mode_list transaction_mode_item
4149 { $$ = lappend($1, $2); }
4152 transaction_mode_list_or_empty:
4153 transaction_mode_list
4159 /*****************************************************************************
4162 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')' AS <query>
4164 *****************************************************************************/
4166 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
4169 ViewStmt *n = makeNode(ViewStmt);
4172 n->view->istemp = $2;
4174 n->query = (Query *) $7;
4177 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
4180 ViewStmt *n = makeNode(ViewStmt);
4183 n->view->istemp = $4;
4185 n->query = (Query *) $9;
4190 /*****************************************************************************
4195 *****************************************************************************/
4197 LoadStmt: LOAD file_name
4199 LoadStmt *n = makeNode(LoadStmt);
4206 /*****************************************************************************
4210 *****************************************************************************/
4213 CREATE DATABASE database_name opt_with createdb_opt_list
4215 CreatedbStmt *n = makeNode(CreatedbStmt);
4223 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
4224 | /* EMPTY */ { $$ = NIL; }
4228 TABLESPACE opt_equal name
4230 $$ = makeDefElem("tablespace", (Node *)makeString($3));
4232 | TABLESPACE opt_equal DEFAULT
4234 $$ = makeDefElem("tablespace", NULL);
4236 | LOCATION opt_equal Sconst
4238 $$ = makeDefElem("location", (Node *)makeString($3));
4240 | LOCATION opt_equal DEFAULT
4242 $$ = makeDefElem("location", NULL);
4244 | TEMPLATE opt_equal name
4246 $$ = makeDefElem("template", (Node *)makeString($3));
4248 | TEMPLATE opt_equal DEFAULT
4250 $$ = makeDefElem("template", NULL);
4252 | ENCODING opt_equal Sconst
4254 $$ = makeDefElem("encoding", (Node *)makeString($3));
4256 | ENCODING opt_equal Iconst
4258 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
4260 | ENCODING opt_equal DEFAULT
4262 $$ = makeDefElem("encoding", NULL);
4264 | OWNER opt_equal name
4266 $$ = makeDefElem("owner", (Node *)makeString($3));
4268 | OWNER opt_equal DEFAULT
4270 $$ = makeDefElem("owner", NULL);
4275 * Though the equals sign doesn't match other WITH options, pg_dump uses
4276 * equals for backward compability, and it doesn't seem worth removing it.
4284 /*****************************************************************************
4288 *****************************************************************************/
4290 AlterDatabaseSetStmt:
4291 ALTER DATABASE database_name SET set_rest
4293 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
4295 n->variable = $5->name;
4296 n->value = $5->args;
4299 | ALTER DATABASE database_name VariableResetStmt
4301 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
4303 n->variable = ((VariableResetStmt *)$4)->name;
4310 /*****************************************************************************
4314 * This is implicitly CASCADE, no need for drop behavior
4315 *****************************************************************************/
4317 DropdbStmt: DROP DATABASE database_name
4319 DropdbStmt *n = makeNode(DropdbStmt);
4326 /*****************************************************************************
4328 * Manipulate a domain
4330 *****************************************************************************/
4333 CREATE DOMAIN_P any_name opt_as Typename ColQualList
4335 CreateDomainStmt *n = makeNode(CreateDomainStmt);
4338 n->constraints = $6;
4344 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
4345 ALTER DOMAIN_P any_name alter_column_default
4347 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4353 /* ALTER DOMAIN <domain> DROP NOT NULL */
4354 | ALTER DOMAIN_P any_name DROP NOT NULL_P
4356 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4361 /* ALTER DOMAIN <domain> SET NOT NULL */
4362 | ALTER DOMAIN_P any_name SET NOT NULL_P
4364 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4369 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
4370 | ALTER DOMAIN_P any_name ADD TableConstraint
4372 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4378 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
4379 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
4381 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4395 /*****************************************************************************
4397 * Manipulate a conversion
4399 * CREATE [DEFAULT] CONVERSION <conversion_name>
4400 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
4402 *****************************************************************************/
4404 CreateConversionStmt:
4405 CREATE opt_default CONVERSION_P any_name FOR Sconst
4406 TO Sconst FROM any_name
4408 CreateConversionStmt *n = makeNode(CreateConversionStmt);
4409 n->conversion_name = $4;
4410 n->for_encoding_name = $6;
4411 n->to_encoding_name = $8;
4418 /*****************************************************************************
4421 * cluster <index_name> on <qualified_name>
4422 * cluster <qualified_name>
4425 *****************************************************************************/
4428 CLUSTER index_name ON qualified_name
4430 ClusterStmt *n = makeNode(ClusterStmt);
4435 | CLUSTER qualified_name
4437 ClusterStmt *n = makeNode(ClusterStmt);
4439 n->indexname = NULL;
4444 ClusterStmt *n = makeNode(ClusterStmt);
4446 n->indexname = NULL;
4451 /*****************************************************************************
4457 *****************************************************************************/
4459 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
4461 VacuumStmt *n = makeNode(VacuumStmt);
4471 | VACUUM opt_full opt_freeze opt_verbose qualified_name
4473 VacuumStmt *n = makeNode(VacuumStmt);
4483 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
4485 VacuumStmt *n = (VacuumStmt *) $5;
4495 analyze_keyword opt_verbose
4497 VacuumStmt *n = makeNode(VacuumStmt);
4507 | analyze_keyword opt_verbose qualified_name opt_name_list
4509 VacuumStmt *n = makeNode(VacuumStmt);
4523 | ANALYSE /* British */ {}
4527 VERBOSE { $$ = TRUE; }
4528 | /*EMPTY*/ { $$ = FALSE; }
4531 opt_full: FULL { $$ = TRUE; }
4532 | /*EMPTY*/ { $$ = FALSE; }
4535 opt_freeze: FREEZE { $$ = TRUE; }
4536 | /*EMPTY*/ { $$ = FALSE; }
4540 '(' name_list ')' { $$ = $2; }
4541 | /*EMPTY*/ { $$ = NIL; }
4545 /*****************************************************************************
4548 * EXPLAIN [ANALYZE] [VERBOSE] query
4550 *****************************************************************************/
4552 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
4554 ExplainStmt *n = makeNode(ExplainStmt);
4557 n->query = (Query*)$4;
4568 | ExecuteStmt /* by default all are $$=$1 */
4572 analyze_keyword { $$ = TRUE; }
4573 | /* EMPTY */ { $$ = FALSE; }
4576 /*****************************************************************************
4579 * PREPARE <plan_name> [(args, ...)] AS <query>
4581 *****************************************************************************/
4583 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
4585 PrepareStmt *n = makeNode(PrepareStmt);
4588 n->query = (Query *) $5;
4593 prep_type_clause: '(' prep_type_list ')' { $$ = $2; }
4594 | /* EMPTY */ { $$ = NIL; }
4597 prep_type_list: Typename { $$ = list_make1($1); }
4598 | prep_type_list ',' Typename
4599 { $$ = lappend($1, $3); }
4606 | DeleteStmt /* by default all are $$=$1 */
4609 /*****************************************************************************
4611 * EXECUTE <plan_name> [(params, ...)]
4612 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
4614 *****************************************************************************/
4616 ExecuteStmt: EXECUTE name execute_param_clause
4618 ExecuteStmt *n = makeNode(ExecuteStmt);
4624 | CREATE OptTemp TABLE qualified_name OptCreateAs AS EXECUTE name execute_param_clause
4626 ExecuteStmt *n = makeNode(ExecuteStmt);
4633 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4634 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
4635 /* ... because it's not implemented, but it could be */
4640 execute_param_clause: '(' expr_list ')' { $$ = $2; }
4641 | /* EMPTY */ { $$ = NIL; }
4644 /*****************************************************************************
4647 * DEALLOCATE [PREPARE] <plan_name>
4649 *****************************************************************************/
4651 DeallocateStmt: DEALLOCATE name
4653 DeallocateStmt *n = makeNode(DeallocateStmt);
4657 | DEALLOCATE PREPARE name
4659 DeallocateStmt *n = makeNode(DeallocateStmt);
4665 /*****************************************************************************
4670 *****************************************************************************/
4673 INSERT INTO qualified_name insert_rest
4681 VALUES '(' insert_target_list ')'
4683 $$ = makeNode(InsertStmt);
4685 $$->targetList = $3;
4686 $$->selectStmt = NULL;
4690 $$ = makeNode(InsertStmt);
4692 $$->targetList = NIL;
4693 $$->selectStmt = NULL;
4697 $$ = makeNode(InsertStmt);
4699 $$->targetList = NIL;
4700 $$->selectStmt = $1;
4702 | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
4704 $$ = makeNode(InsertStmt);
4706 $$->targetList = $6;
4707 $$->selectStmt = NULL;
4709 | '(' insert_column_list ')' SelectStmt
4711 $$ = makeNode(InsertStmt);
4713 $$->targetList = NIL;
4714 $$->selectStmt = $4;
4720 { $$ = list_make1($1); }
4721 | insert_column_list ',' insert_column_item
4722 { $$ = lappend($1, $3); }
4726 ColId opt_indirection
4728 $$ = makeNode(ResTarget);
4730 $$->indirection = $2;
4736 /*****************************************************************************
4741 *****************************************************************************/
4743 DeleteStmt: DELETE_P FROM relation_expr using_clause where_clause
4745 DeleteStmt *n = makeNode(DeleteStmt);
4747 n->usingClause = $4;
4748 n->whereClause = $5;
4754 USING from_list { $$ = $2; }
4755 | /*EMPTY*/ { $$ = NIL; }
4758 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
4760 LockStmt *n = makeNode(LockStmt);
4769 opt_lock: IN_P lock_type MODE { $$ = $2; }
4770 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
4773 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
4774 | ROW SHARE { $$ = RowShareLock; }
4775 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
4776 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
4777 | SHARE { $$ = ShareLock; }
4778 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
4779 | EXCLUSIVE { $$ = ExclusiveLock; }
4780 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
4783 opt_nowait: NOWAIT { $$ = TRUE; }
4784 | /*EMPTY*/ { $$ = FALSE; }
4788 /*****************************************************************************
4791 * UpdateStmt (UPDATE)
4793 *****************************************************************************/
4795 UpdateStmt: UPDATE relation_expr
4796 SET update_target_list
4800 UpdateStmt *n = makeNode(UpdateStmt);
4804 n->whereClause = $6;
4810 /*****************************************************************************
4815 *****************************************************************************/
4816 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
4818 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
4823 n->options |= CURSOR_OPT_HOLD;
4828 cursor_options: /*EMPTY*/ { $$ = 0; }
4829 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
4830 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
4831 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
4832 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
4835 opt_hold: /* EMPTY */ { $$ = FALSE; }
4836 | WITH HOLD { $$ = TRUE; }
4837 | WITHOUT HOLD { $$ = FALSE; }
4840 /*****************************************************************************
4845 *****************************************************************************/
4847 /* A complete SELECT statement looks like this.
4849 * The rule returns either a single SelectStmt node or a tree of them,
4850 * representing a set-operation tree.
4852 * There is an ambiguity when a sub-SELECT is within an a_expr and there
4853 * are excess parentheses: do the parentheses belong to the sub-SELECT or
4854 * to the surrounding a_expr? We don't really care, but yacc wants to know.
4855 * To resolve the ambiguity, we are careful to define the grammar so that
4856 * the decision is staved off as long as possible: as long as we can keep
4857 * absorbing parentheses into the sub-SELECT, we will do so, and only when
4858 * it's no longer possible to do that will we decide that parens belong to
4859 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
4860 * parentheses are treated as part of the sub-select. The necessity of doing
4861 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
4862 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
4863 * SELECT viewpoint when we see the UNION.
4865 * This approach is implemented by defining a nonterminal select_with_parens,
4866 * which represents a SELECT with at least one outer layer of parentheses,
4867 * and being careful to use select_with_parens, never '(' SelectStmt ')',
4868 * in the expression grammar. We will then have shift-reduce conflicts
4869 * which we can resolve in favor of always treating '(' <select> ')' as
4870 * a select_with_parens. To resolve the conflicts, the productions that
4871 * conflict with the select_with_parens productions are manually given
4872 * precedences lower than the precedence of ')', thereby ensuring that we
4873 * shift ')' (and then reduce to select_with_parens) rather than trying to
4874 * reduce the inner <select> nonterminal to something else. We use UMINUS
4875 * precedence for this, which is a fairly arbitrary choice.
4877 * To be able to define select_with_parens itself without ambiguity, we need
4878 * a nonterminal select_no_parens that represents a SELECT structure with no
4879 * outermost parentheses. This is a little bit tedious, but it works.
4881 * In non-expression contexts, we use SelectStmt which can represent a SELECT
4882 * with or without outer parentheses.
4885 SelectStmt: select_no_parens %prec UMINUS
4886 | select_with_parens %prec UMINUS
4890 '(' select_no_parens ')' { $$ = $2; }
4891 | '(' select_with_parens ')' { $$ = $2; }
4895 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
4896 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
4897 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
4901 simple_select { $$ = $1; }
4902 | select_clause sort_clause
4904 insertSelectOptions((SelectStmt *) $1, $2, NIL,
4908 | select_clause opt_sort_clause for_locking_clause opt_select_limit
4910 insertSelectOptions((SelectStmt *) $1, $2, $3,
4911 list_nth($4, 0), list_nth($4, 1));
4914 | select_clause opt_sort_clause select_limit opt_for_locking_clause
4916 insertSelectOptions((SelectStmt *) $1, $2, $4,
4917 list_nth($3, 0), list_nth($3, 1));
4923 simple_select { $$ = $1; }
4924 | select_with_parens { $$ = $1; }
4928 * This rule parses SELECT statements that can appear within set operations,
4929 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
4930 * the ordering of the set operations. Without '(' and ')' we want the
4931 * operations to be ordered per the precedence specs at the head of this file.
4933 * As with select_no_parens, simple_select cannot have outer parentheses,
4934 * but can have parenthesized subclauses.
4936 * Note that sort clauses cannot be included at this level --- SQL92 requires
4937 * SELECT foo UNION SELECT bar ORDER BY baz
4939 * (SELECT foo UNION SELECT bar) ORDER BY baz
4941 * SELECT foo UNION (SELECT bar ORDER BY baz)
4942 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
4943 * as part of the select_no_parens production, not simple_select.
4944 * This does not limit functionality, because you can reintroduce sort and
4945 * limit clauses inside parentheses.
4947 * NOTE: only the leftmost component SelectStmt should have INTO.
4948 * However, this is not checked by the grammar; parse analysis must check it.
4951 SELECT opt_distinct target_list
4952 into_clause from_clause where_clause
4953 group_clause having_clause
4955 SelectStmt *n = makeNode(SelectStmt);
4956 n->distinctClause = $2;
4959 n->intoColNames = NIL;
4960 n->intoHasOids = DEFAULT_OIDS;
4962 n->whereClause = $6;
4963 n->groupClause = $7;
4964 n->havingClause = $8;
4967 | select_clause UNION opt_all select_clause
4969 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
4971 | select_clause INTERSECT opt_all select_clause
4973 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
4975 | select_clause EXCEPT opt_all select_clause
4977 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
4982 INTO OptTempTableName { $$ = $2; }
4983 | /*EMPTY*/ { $$ = NULL; }
4987 * Redundancy here is needed to avoid shift/reduce conflicts,
4988 * since TEMP is not a reserved word. See also OptTemp.
4991 TEMPORARY opt_table qualified_name
4996 | TEMP opt_table qualified_name
5001 | LOCAL TEMPORARY opt_table qualified_name
5006 | LOCAL TEMP opt_table qualified_name
5011 | GLOBAL TEMPORARY opt_table qualified_name
5016 | GLOBAL TEMP opt_table qualified_name
5021 | TABLE qualified_name
5037 opt_all: ALL { $$ = TRUE; }
5038 | DISTINCT { $$ = FALSE; }
5039 | /*EMPTY*/ { $$ = FALSE; }
5042 /* We use (NIL) as a placeholder to indicate that all target expressions
5043 * should be placed in the DISTINCT list during parsetree analysis.
5046 DISTINCT { $$ = list_make1(NIL); }
5047 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
5049 | /*EMPTY*/ { $$ = NIL; }
5053 sort_clause { $$ = $1;}
5054 | /*EMPTY*/ { $$ = NIL; }
5058 ORDER BY sortby_list { $$ = $3; }
5062 sortby { $$ = list_make1($1); }
5063 | sortby_list ',' sortby { $$ = lappend($1, $3); }
5066 sortby: a_expr USING qual_all_Op
5068 $$ = makeNode(SortBy);
5070 $$->sortby_kind = SORTBY_USING;
5075 $$ = makeNode(SortBy);
5077 $$->sortby_kind = SORTBY_ASC;
5082 $$ = makeNode(SortBy);
5084 $$->sortby_kind = SORTBY_DESC;
5089 $$ = makeNode(SortBy);
5091 $$->sortby_kind = SORTBY_ASC; /* default */
5098 LIMIT select_limit_value OFFSET select_offset_value
5099 { $$ = list_make2($4, $2); }
5100 | OFFSET select_offset_value LIMIT select_limit_value
5101 { $$ = list_make2($2, $4); }
5102 | LIMIT select_limit_value
5103 { $$ = list_make2(NULL, $2); }
5104 | OFFSET select_offset_value
5105 { $$ = list_make2($2, NULL); }
5106 | LIMIT select_limit_value ',' select_offset_value
5108 /* Disabled because it was too confusing, bjm 2002-02-18 */
5110 (errcode(ERRCODE_SYNTAX_ERROR),
5111 errmsg("LIMIT #,# syntax is not supported"),
5112 errhint("Use separate LIMIT and OFFSET clauses.")));
5117 select_limit { $$ = $1; }
5119 { $$ = list_make2(NULL,NULL); }
5126 /* LIMIT ALL is represented as a NULL constant */
5127 A_Const *n = makeNode(A_Const);
5128 n->val.type = T_Null;
5133 select_offset_value:
5138 * jimmy bell-style recursive queries aren't supported in the
5141 * ...however, recursive addattr and rename supported. make special
5146 GROUP_P BY expr_list { $$ = $3; }
5147 | /*EMPTY*/ { $$ = NIL; }
5151 HAVING a_expr { $$ = $2; }
5152 | /*EMPTY*/ { $$ = NULL; }
5156 FOR UPDATE update_list { $$ = lcons(makeString("for_update"), $3); }
5157 | FOR SHARE update_list { $$ = lcons(makeString("for_share"), $3); }
5158 | FOR READ ONLY { $$ = NULL; }
5161 opt_for_locking_clause:
5162 for_locking_clause { $$ = $1; }
5163 | /* EMPTY */ { $$ = NULL; }
5167 OF name_list { $$ = $2; }
5168 | /* EMPTY */ { $$ = list_make1(NULL); }
5171 /*****************************************************************************
5173 * clauses common to all Optimizable Stmts:
5174 * from_clause - allow list of both JOIN expressions and table names
5175 * where_clause - qualifications for joins or restrictions
5177 *****************************************************************************/
5180 FROM from_list { $$ = $2; }
5181 | /*EMPTY*/ { $$ = NIL; }
5185 table_ref { $$ = list_make1($1); }
5186 | from_list ',' table_ref { $$ = lappend($1, $3); }
5190 * table_ref is where an alias clause can be attached. Note we cannot make
5191 * alias_clause have an empty production because that causes parse conflicts
5192 * between table_ref := '(' joined_table ')' alias_clause
5193 * and joined_table := '(' joined_table ')'. So, we must have the
5194 * redundant-looking productions here instead.
5196 table_ref: relation_expr
5200 | relation_expr alias_clause
5207 RangeFunction *n = makeNode(RangeFunction);
5208 n->funccallnode = $1;
5209 n->coldeflist = NIL;
5212 | func_table alias_clause
5214 RangeFunction *n = makeNode(RangeFunction);
5215 n->funccallnode = $1;
5217 n->coldeflist = NIL;
5220 | func_table AS '(' TableFuncElementList ')'
5222 RangeFunction *n = makeNode(RangeFunction);
5223 n->funccallnode = $1;
5227 | func_table AS ColId '(' TableFuncElementList ')'
5229 RangeFunction *n = makeNode(RangeFunction);
5230 Alias *a = makeNode(Alias);
5231 n->funccallnode = $1;
5237 | func_table ColId '(' TableFuncElementList ')'
5239 RangeFunction *n = makeNode(RangeFunction);
5240 Alias *a = makeNode(Alias);
5241 n->funccallnode = $1;
5247 | select_with_parens
5250 * The SQL spec does not permit a subselect
5251 * (<derived_table>) without an alias clause,
5252 * so we don't either. This avoids the problem
5253 * of needing to invent a unique refname for it.
5254 * That could be surmounted if there's sufficient
5255 * popular demand, but for now let's just implement
5256 * the spec and see if anyone complains.
5257 * However, it does seem like a good idea to emit
5258 * an error message that's better than "syntax error".
5261 (errcode(ERRCODE_SYNTAX_ERROR),
5262 errmsg("subquery in FROM must have an alias"),
5263 errhint("For example, FROM (SELECT ...) [AS] foo.")));
5266 | select_with_parens alias_clause
5268 RangeSubselect *n = makeNode(RangeSubselect);
5277 | '(' joined_table ')' alias_clause
5286 * It may seem silly to separate joined_table from table_ref, but there is
5287 * method in SQL92's madness: if you don't do it this way you get reduce-
5288 * reduce conflicts, because it's not clear to the parser generator whether
5289 * to expect alias_clause after ')' or not. For the same reason we must
5290 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
5291 * join_type to expand to empty; if we try it, the parser generator can't
5292 * figure out when to reduce an empty join_type right after table_ref.
5294 * Note that a CROSS JOIN is the same as an unqualified
5295 * INNER JOIN, and an INNER JOIN/ON has the same shape
5296 * but a qualification expression to limit membership.
5297 * A NATURAL JOIN implicitly matches column names between
5298 * tables and the shape is determined by which columns are
5299 * in common. We'll collect columns during the later transformations.
5303 '(' joined_table ')'
5307 | table_ref CROSS JOIN table_ref
5309 /* CROSS JOIN is same as unqualified inner join */
5310 JoinExpr *n = makeNode(JoinExpr);
5311 n->jointype = JOIN_INNER;
5312 n->isNatural = FALSE;
5319 | table_ref UNIONJOIN table_ref
5321 /* UNION JOIN is made into 1 token to avoid shift/reduce
5322 * conflict against regular UNION keyword.
5324 JoinExpr *n = makeNode(JoinExpr);
5325 n->jointype = JOIN_UNION;
5326 n->isNatural = FALSE;
5333 | table_ref join_type JOIN table_ref join_qual
5335 JoinExpr *n = makeNode(JoinExpr);
5337 n->isNatural = FALSE;
5340 if ($5 != NULL && IsA($5, List))
5341 n->using = (List *) $5; /* USING clause */
5343 n->quals = $5; /* ON clause */
5346 | table_ref JOIN table_ref join_qual
5348 /* letting join_type reduce to empty doesn't work */
5349 JoinExpr *n = makeNode(JoinExpr);
5350 n->jointype = JOIN_INNER;
5351 n->isNatural = FALSE;
5354 if ($4 != NULL && IsA($4, List))
5355 n->using = (List *) $4; /* USING clause */
5357 n->quals = $4; /* ON clause */
5360 | table_ref NATURAL join_type JOIN table_ref
5362 JoinExpr *n = makeNode(JoinExpr);
5364 n->isNatural = TRUE;
5367 n->using = NIL; /* figure out which columns later... */
5368 n->quals = NULL; /* fill later */
5371 | table_ref NATURAL JOIN table_ref
5373 /* letting join_type reduce to empty doesn't work */
5374 JoinExpr *n = makeNode(JoinExpr);
5375 n->jointype = JOIN_INNER;
5376 n->isNatural = TRUE;
5379 n->using = NIL; /* figure out which columns later... */
5380 n->quals = NULL; /* fill later */
5386 AS ColId '(' name_list ')'
5388 $$ = makeNode(Alias);
5394 $$ = makeNode(Alias);
5397 | ColId '(' name_list ')'
5399 $$ = makeNode(Alias);
5405 $$ = makeNode(Alias);
5410 join_type: FULL join_outer { $$ = JOIN_FULL; }
5411 | LEFT join_outer { $$ = JOIN_LEFT; }
5412 | RIGHT join_outer { $$ = JOIN_RIGHT; }
5413 | INNER_P { $$ = JOIN_INNER; }
5416 /* OUTER is just noise... */
5417 join_outer: OUTER_P { $$ = NULL; }
5418 | /*EMPTY*/ { $$ = NULL; }
5421 /* JOIN qualification clauses
5422 * Possibilities are:
5423 * USING ( column list ) allows only unqualified column names,
5424 * which must match between tables.
5425 * ON expr allows more general qualifications.
5427 * We return USING as a List node, while an ON-expr will not be a List.
5430 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
5431 | ON a_expr { $$ = $2; }
5438 /* default inheritance */
5440 $$->inhOpt = INH_DEFAULT;
5443 | qualified_name '*'
5445 /* inheritance query */
5447 $$->inhOpt = INH_YES;
5450 | ONLY qualified_name
5452 /* no inheritance */
5454 $$->inhOpt = INH_NO;
5457 | ONLY '(' qualified_name ')'
5459 /* no inheritance, SQL99-style syntax */
5461 $$->inhOpt = INH_NO;
5467 func_table: func_expr { $$ = $1; }
5472 WHERE a_expr { $$ = $2; }
5473 | /*EMPTY*/ { $$ = NULL; }
5477 TableFuncElementList:
5480 $$ = list_make1($1);
5482 | TableFuncElementList ',' TableFuncElement
5484 $$ = lappend($1, $3);
5488 TableFuncElement: ColId Typename
5490 ColumnDef *n = makeNode(ColumnDef);
5493 n->constraints = NIL;
5499 /*****************************************************************************
5502 * SQL92 introduces a large amount of type-specific syntax.
5503 * Define individual clauses to handle these cases, and use
5504 * the generic case to handle regular type-extensible Postgres syntax.
5505 * - thomas 1997-10-10
5507 *****************************************************************************/
5509 Typename: SimpleTypename opt_array_bounds
5512 $$->arrayBounds = $2;
5514 | SETOF SimpleTypename opt_array_bounds
5517 $$->arrayBounds = $3;
5520 | SimpleTypename ARRAY '[' Iconst ']'
5522 /* SQL99's redundant syntax */
5524 $$->arrayBounds = list_make1(makeInteger($4));
5526 | SETOF SimpleTypename ARRAY '[' Iconst ']'
5528 /* SQL99's redundant syntax */
5530 $$->arrayBounds = list_make1(makeInteger($5));
5536 opt_array_bounds '[' ']'
5537 { $$ = lappend($1, makeInteger(-1)); }
5538 | opt_array_bounds '[' Iconst ']'
5539 { $$ = lappend($1, makeInteger($3)); }
5545 * XXX ideally, the production for a qualified typename should be ColId attrs
5546 * (there's no obvious reason why the first name should need to be restricted)
5547 * and should be an alternative of GenericType (so that it can be used to
5548 * specify a type for a literal in AExprConst). However doing either causes
5549 * reduce/reduce conflicts that I haven't been able to find a workaround
5553 GenericType { $$ = $1; }
5554 | Numeric { $$ = $1; }
5556 | Character { $$ = $1; }
5557 | ConstDatetime { $$ = $1; }
5558 | ConstInterval opt_interval
5561 if ($2 != INTERVAL_FULL_RANGE)
5562 $$->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $2);
5564 | ConstInterval '(' Iconst ')' opt_interval
5569 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5570 errmsg("INTERVAL(%d) precision must not be negative",
5572 if ($3 > MAX_INTERVAL_PRECISION)
5575 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5576 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
5577 $3, MAX_INTERVAL_PRECISION)));
5578 $3 = MAX_INTERVAL_PRECISION;
5580 $$->typmod = INTERVAL_TYPMOD($3, $5);
5584 $$ = makeNode(TypeName);
5585 $$->names = lcons(makeString($1), $2);
5590 /* We have a separate ConstTypename to allow defaulting fixed-length
5591 * types such as CHAR() and BIT() to an unspecified length.
5592 * SQL9x requires that these default to a length of one, but this
5593 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
5594 * where there is an obvious better choice to make.
5595 * Note that ConstInterval is not included here since it must
5596 * be pushed up higher in the rules to accomodate the postfix
5597 * options (e.g. INTERVAL '1' YEAR).
5600 GenericType { $$ = $1; }
5601 | Numeric { $$ = $1; }
5602 | ConstBit { $$ = $1; }
5603 | ConstCharacter { $$ = $1; }
5604 | ConstDatetime { $$ = $1; }
5610 $$ = makeTypeName($1);
5614 /* SQL92 numeric data types
5615 * Check FLOAT() precision limits assuming IEEE floating types.
5616 * - thomas 1997-09-18
5617 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
5621 $$ = SystemTypeName("int4");
5625 $$ = SystemTypeName("int4");
5629 $$ = SystemTypeName("int2");
5633 $$ = SystemTypeName("int8");
5637 $$ = SystemTypeName("float4");
5643 | DOUBLE_P PRECISION
5645 $$ = SystemTypeName("float8");
5647 | DECIMAL_P opt_decimal
5649 $$ = SystemTypeName("numeric");
5654 $$ = SystemTypeName("numeric");
5657 | NUMERIC opt_numeric
5659 $$ = SystemTypeName("numeric");
5664 $$ = SystemTypeName("bool");
5668 opt_float: '(' Iconst ')'
5672 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5673 errmsg("precision for type float must be at least 1 bit")));
5675 $$ = SystemTypeName("float4");
5677 $$ = SystemTypeName("float8");
5680 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5681 errmsg("precision for type float must be less than 54 bits")));
5685 $$ = SystemTypeName("float8");
5690 '(' Iconst ',' Iconst ')'
5692 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5694 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5695 errmsg("NUMERIC precision %d must be between 1 and %d",
5696 $2, NUMERIC_MAX_PRECISION)));
5697 if ($4 < 0 || $4 > $2)
5699 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5700 errmsg("NUMERIC scale %d must be between 0 and precision %d",
5703 $$ = (($2 << 16) | $4) + VARHDRSZ;
5707 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5709 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5710 errmsg("NUMERIC precision %d must be between 1 and %d",
5711 $2, NUMERIC_MAX_PRECISION)));
5713 $$ = ($2 << 16) + VARHDRSZ;
5717 /* Insert "-1" meaning "no limit" */
5723 '(' Iconst ',' Iconst ')'
5725 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5727 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5728 errmsg("DECIMAL precision %d must be between 1 and %d",
5729 $2, NUMERIC_MAX_PRECISION)));
5730 if ($4 < 0 || $4 > $2)
5732 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5733 errmsg("DECIMAL scale %d must be between 0 and precision %d",
5736 $$ = (($2 << 16) | $4) + VARHDRSZ;
5740 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5742 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5743 errmsg("DECIMAL precision %d must be between 1 and %d",
5744 $2, NUMERIC_MAX_PRECISION)));
5746 $$ = ($2 << 16) + VARHDRSZ;
5750 /* Insert "-1" meaning "no limit" */
5757 * SQL92 bit-field data types
5758 * The following implements BIT() and BIT VARYING().
5770 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
5771 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
5772 ConstBit: BitWithLength
5784 BIT opt_varying '(' Iconst ')'
5788 typname = $2 ? "varbit" : "bit";
5789 $$ = SystemTypeName(typname);
5792 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5793 errmsg("length for type %s must be at least 1",
5795 else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
5797 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5798 errmsg("length for type %s cannot exceed %d",
5799 typname, MaxAttrSize * BITS_PER_BYTE)));
5807 /* bit defaults to bit(1), varbit to no limit */
5810 $$ = SystemTypeName("varbit");
5815 $$ = SystemTypeName("bit");
5823 * SQL92 character data types
5824 * The following implements CHAR() and VARCHAR().
5826 Character: CharacterWithLength
5830 | CharacterWithoutLength
5836 ConstCharacter: CharacterWithLength
5840 | CharacterWithoutLength
5842 /* Length was not specified so allow to be unrestricted.
5843 * This handles problems with fixed-length (bpchar) strings
5844 * which in column definitions must default to a length
5845 * of one, but should not be constrained if the length
5846 * was not specified.
5853 CharacterWithLength: character '(' Iconst ')' opt_charset
5855 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
5859 type = palloc(strlen($1) + 1 + strlen($5) + 1);
5866 $$ = SystemTypeName($1);
5870 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5871 errmsg("length for type %s must be at least 1",
5873 else if ($3 > MaxAttrSize)
5875 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5876 errmsg("length for type %s cannot exceed %d",
5879 /* we actually implement these like a varlen, so
5880 * the first 4 bytes is the length. (the difference
5881 * between these and "text" is that we blank-pad and
5882 * truncate where necessary)
5884 $$->typmod = VARHDRSZ + $3;
5888 CharacterWithoutLength: character opt_charset
5890 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
5894 type = palloc(strlen($1) + 1 + strlen($2) + 1);
5901 $$ = SystemTypeName($1);
5903 /* char defaults to char(1), varchar to no limit */
5904 if (strcmp($1, "bpchar") == 0)
5905 $$->typmod = VARHDRSZ + 1;
5911 character: CHARACTER opt_varying
5912 { $$ = $2 ? "varchar": "bpchar"; }
5913 | CHAR_P opt_varying
5914 { $$ = $2 ? "varchar": "bpchar"; }
5917 | NATIONAL CHARACTER opt_varying
5918 { $$ = $3 ? "varchar": "bpchar"; }
5919 | NATIONAL CHAR_P opt_varying
5920 { $$ = $3 ? "varchar": "bpchar"; }
5922 { $$ = $2 ? "varchar": "bpchar"; }
5926 VARYING { $$ = TRUE; }
5927 | /*EMPTY*/ { $$ = FALSE; }
5931 CHARACTER SET ColId { $$ = $3; }
5932 | /*EMPTY*/ { $$ = NULL; }
5936 TIMESTAMP '(' Iconst ')' opt_timezone
5939 $$ = SystemTypeName("timestamptz");
5941 $$ = SystemTypeName("timestamp");
5942 /* XXX the timezone field seems to be unused
5943 * - thomas 2001-09-06
5948 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5949 errmsg("TIMESTAMP(%d)%s precision must not be negative",
5950 $3, ($5 ? " WITH TIME ZONE": ""))));
5951 if ($3 > MAX_TIMESTAMP_PRECISION)
5954 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5955 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
5956 $3, ($5 ? " WITH TIME ZONE": ""),
5957 MAX_TIMESTAMP_PRECISION)));
5958 $3 = MAX_TIMESTAMP_PRECISION;
5962 | TIMESTAMP opt_timezone
5965 $$ = SystemTypeName("timestamptz");
5967 $$ = SystemTypeName("timestamp");
5968 /* XXX the timezone field seems to be unused
5969 * - thomas 2001-09-06
5972 /* SQL99 specified a default precision of six
5973 * for schema definitions. But for timestamp
5974 * literals we don't want to throw away precision
5975 * so leave this as unspecified for now.
5976 * Later, we may want a different production
5977 * for schemas. - thomas 2001-12-07
5981 | TIME '(' Iconst ')' opt_timezone
5984 $$ = SystemTypeName("timetz");
5986 $$ = SystemTypeName("time");
5989 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5990 errmsg("TIME(%d)%s precision must not be negative",
5991 $3, ($5 ? " WITH TIME ZONE": ""))));
5992 if ($3 > MAX_TIME_PRECISION)
5995 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5996 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
5997 $3, ($5 ? " WITH TIME ZONE": ""),
5998 MAX_TIME_PRECISION)));
5999 $3 = MAX_TIME_PRECISION;
6006 $$ = SystemTypeName("timetz");
6008 $$ = SystemTypeName("time");
6009 /* SQL99 specified a default precision of zero.
6010 * See comments for timestamp above on why we will
6011 * leave this unspecified for now. - thomas 2001-12-07
6018 INTERVAL { $$ = SystemTypeName("interval"); }
6022 WITH TIME ZONE { $$ = TRUE; }
6023 | WITHOUT TIME ZONE { $$ = FALSE; }
6024 | /*EMPTY*/ { $$ = FALSE; }
6028 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
6029 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
6030 | DAY_P { $$ = INTERVAL_MASK(DAY); }
6031 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
6032 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
6033 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
6035 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
6037 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
6039 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
6040 | INTERVAL_MASK(MINUTE); }
6042 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
6043 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
6044 | HOUR_P TO MINUTE_P
6045 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
6046 | HOUR_P TO SECOND_P
6047 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
6048 | INTERVAL_MASK(SECOND); }
6049 | MINUTE_P TO SECOND_P
6050 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
6051 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
6055 /*****************************************************************************
6057 * expression grammar
6059 *****************************************************************************/
6062 * General expressions
6063 * This is the heart of the expression syntax.
6065 * We have two expression types: a_expr is the unrestricted kind, and
6066 * b_expr is a subset that must be used in some places to avoid shift/reduce
6067 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
6068 * because that use of AND conflicts with AND as a boolean operator. So,
6069 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
6071 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
6072 * always be used by surrounding it with parens.
6074 * c_expr is all the productions that are common to a_expr and b_expr;
6075 * it's factored out just to eliminate redundant coding.
6077 a_expr: c_expr { $$ = $1; }
6078 | a_expr TYPECAST Typename
6079 { $$ = makeTypeCast($1, $3); }
6080 | a_expr AT TIME ZONE a_expr
6082 FuncCall *n = makeNode(FuncCall);
6083 n->funcname = SystemFuncName("timezone");
6084 n->args = list_make2($5, $1);
6085 n->agg_star = FALSE;
6086 n->agg_distinct = FALSE;
6090 * These operators must be called out explicitly in order to make use
6091 * of yacc/bison's automatic operator-precedence handling. All other
6092 * operator names are handled by the generic productions using "Op",
6093 * below; and all those operators will have the same precedence.
6095 * If you add more explicitly-known operators, be sure to add them
6096 * also to b_expr and to the MathOp list above.
6098 | '+' a_expr %prec UMINUS
6099 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
6100 | '-' a_expr %prec UMINUS
6101 { $$ = doNegate($2); }
6103 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
6105 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
6107 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
6109 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
6111 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
6113 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
6115 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
6117 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
6119 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
6121 | a_expr qual_Op a_expr %prec Op
6122 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
6123 | qual_Op a_expr %prec Op
6124 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
6125 | a_expr qual_Op %prec POSTFIXOP
6126 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
6129 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3); }
6131 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3); }
6133 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2); }
6135 | a_expr LIKE a_expr
6136 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3); }
6137 | a_expr LIKE a_expr ESCAPE a_expr
6139 FuncCall *n = makeNode(FuncCall);
6140 n->funcname = SystemFuncName("like_escape");
6141 n->args = list_make2($3, $5);
6142 n->agg_star = FALSE;
6143 n->agg_distinct = FALSE;
6144 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n);
6146 | a_expr NOT LIKE a_expr
6147 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4); }
6148 | a_expr NOT LIKE a_expr ESCAPE a_expr
6150 FuncCall *n = makeNode(FuncCall);
6151 n->funcname = SystemFuncName("like_escape");
6152 n->args = list_make2($4, $6);
6153 n->agg_star = FALSE;
6154 n->agg_distinct = FALSE;
6155 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n);
6157 | a_expr ILIKE a_expr
6158 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3); }
6159 | a_expr ILIKE a_expr ESCAPE a_expr
6161 FuncCall *n = makeNode(FuncCall);
6162 n->funcname = SystemFuncName("like_escape");
6163 n->args = list_make2($3, $5);
6164 n->agg_star = FALSE;
6165 n->agg_distinct = FALSE;
6166 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n);
6168 | a_expr NOT ILIKE a_expr
6169 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4); }
6170 | a_expr NOT ILIKE a_expr ESCAPE a_expr
6172 FuncCall *n = makeNode(FuncCall);
6173 n->funcname = SystemFuncName("like_escape");
6174 n->args = list_make2($4, $6);
6175 n->agg_star = FALSE;
6176 n->agg_distinct = FALSE;
6177 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n);
6180 | a_expr SIMILAR TO a_expr %prec SIMILAR
6182 A_Const *c = makeNode(A_Const);
6183 FuncCall *n = makeNode(FuncCall);
6184 c->val.type = T_Null;
6185 n->funcname = SystemFuncName("similar_escape");
6186 n->args = list_make2($4, (Node *) c);
6187 n->agg_star = FALSE;
6188 n->agg_distinct = FALSE;
6189 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
6191 | a_expr SIMILAR TO a_expr ESCAPE a_expr
6193 FuncCall *n = makeNode(FuncCall);
6194 n->funcname = SystemFuncName("similar_escape");
6195 n->args = list_make2($4, $6);
6196 n->agg_star = FALSE;
6197 n->agg_distinct = FALSE;
6198 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
6200 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
6202 A_Const *c = makeNode(A_Const);
6203 FuncCall *n = makeNode(FuncCall);
6204 c->val.type = T_Null;
6205 n->funcname = SystemFuncName("similar_escape");
6206 n->args = list_make2($5, (Node *) c);
6207 n->agg_star = FALSE;
6208 n->agg_distinct = FALSE;
6209 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
6211 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
6213 FuncCall *n = makeNode(FuncCall);
6214 n->funcname = SystemFuncName("similar_escape");
6215 n->args = list_make2($5, $7);
6216 n->agg_star = FALSE;
6217 n->agg_distinct = FALSE;
6218 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
6222 * Define SQL92-style Null test clause.
6223 * Allow two forms described in the standard:
6226 * Allow two SQL extensions
6232 if (IsA($1, RowExpr))
6233 $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
6236 NullTest *n = makeNode(NullTest);
6237 n->arg = (Expr *) $1;
6238 n->nulltesttype = IS_NULL;
6244 if (IsA($1, RowExpr))
6245 $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
6248 NullTest *n = makeNode(NullTest);
6249 n->arg = (Expr *) $1;
6250 n->nulltesttype = IS_NULL;
6256 if (IsA($1, RowExpr))
6257 $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
6260 NullTest *n = makeNode(NullTest);
6261 n->arg = (Expr *) $1;
6262 n->nulltesttype = IS_NOT_NULL;
6266 | a_expr IS NOT NULL_P
6268 if (IsA($1, RowExpr))
6269 $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
6272 NullTest *n = makeNode(NullTest);
6273 n->arg = (Expr *) $1;
6274 n->nulltesttype = IS_NOT_NULL;
6280 $$ = (Node *)makeOverlaps($1, $3);
6284 BooleanTest *b = makeNode(BooleanTest);
6285 b->arg = (Expr *) $1;
6286 b->booltesttype = IS_TRUE;
6289 | a_expr IS NOT TRUE_P
6291 BooleanTest *b = makeNode(BooleanTest);
6292 b->arg = (Expr *) $1;
6293 b->booltesttype = IS_NOT_TRUE;
6298 BooleanTest *b = makeNode(BooleanTest);
6299 b->arg = (Expr *) $1;
6300 b->booltesttype = IS_FALSE;
6303 | a_expr IS NOT FALSE_P
6305 BooleanTest *b = makeNode(BooleanTest);
6306 b->arg = (Expr *) $1;
6307 b->booltesttype = IS_NOT_FALSE;
6312 BooleanTest *b = makeNode(BooleanTest);
6313 b->arg = (Expr *) $1;
6314 b->booltesttype = IS_UNKNOWN;
6317 | a_expr IS NOT UNKNOWN
6319 BooleanTest *b = makeNode(BooleanTest);
6320 b->arg = (Expr *) $1;
6321 b->booltesttype = IS_NOT_UNKNOWN;
6324 | a_expr IS DISTINCT FROM a_expr %prec IS
6326 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
6328 | a_expr IS OF '(' type_list ')' %prec IS
6330 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
6332 | a_expr IS NOT OF '(' type_list ')' %prec IS
6334 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
6336 | a_expr BETWEEN b_expr AND b_expr %prec BETWEEN
6338 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
6339 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3),
6340 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $5));
6342 | a_expr NOT BETWEEN b_expr AND b_expr %prec BETWEEN
6344 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
6345 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $4),
6346 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $6));
6348 | a_expr IN_P in_expr
6350 /* in_expr returns a SubLink or a list of a_exprs */
6351 if (IsA($3, SubLink))
6353 SubLink *n = (SubLink *)$3;
6354 n->subLinkType = ANY_SUBLINK;
6355 if (IsA($1, RowExpr))
6356 n->lefthand = ((RowExpr *) $1)->args;
6358 n->lefthand = list_make1($1);
6359 n->operName = list_make1(makeString("="));
6366 foreach(l, (List *) $3)
6369 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, lfirst(l));
6373 n = (Node *) makeA_Expr(AEXPR_OR, NIL, n, cmp);
6378 | a_expr NOT IN_P in_expr
6380 /* in_expr returns a SubLink or a list of a_exprs */
6381 if (IsA($4, SubLink))
6383 /* Make an IN node */
6384 SubLink *n = (SubLink *)$4;
6385 n->subLinkType = ANY_SUBLINK;
6386 if (IsA($1, RowExpr))
6387 n->lefthand = ((RowExpr *) $1)->args;
6389 n->lefthand = list_make1($1);
6390 n->operName = list_make1(makeString("="));
6391 /* Stick a NOT on top */
6392 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n);
6398 foreach(l, (List *) $4)
6401 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, lfirst(l));
6405 n = (Node *) makeA_Expr(AEXPR_AND, NIL, n, cmp);
6410 | a_expr subquery_Op sub_type select_with_parens %prec Op
6412 SubLink *n = makeNode(SubLink);
6413 n->subLinkType = $3;
6414 if (IsA($1, RowExpr))
6415 n->lefthand = ((RowExpr *) $1)->args;
6417 n->lefthand = list_make1($1);
6422 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
6424 if ($3 == ANY_SUBLINK)
6425 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5);
6427 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5);
6429 | UNIQUE select_with_parens %prec Op
6431 /* Not sure how to get rid of the parentheses
6432 * but there are lots of shift/reduce errors without them.
6434 * Should be able to implement this by plopping the entire
6435 * select into a node, then transforming the target expressions
6436 * from whatever they are into count(*), and testing the
6437 * entire result equal to one.
6438 * But, will probably implement a separate node in the executor.
6441 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6442 errmsg("UNIQUE predicate is not yet implemented")));
6447 * Restricted expressions
6449 * b_expr is a subset of the complete expression syntax defined by a_expr.
6451 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
6452 * cause trouble in the places where b_expr is used. For simplicity, we
6453 * just eliminate all the boolean-keyword-operator productions from b_expr.
6457 | b_expr TYPECAST Typename
6458 { $$ = makeTypeCast($1, $3); }
6459 | '+' b_expr %prec UMINUS
6460 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
6461 | '-' b_expr %prec UMINUS
6462 { $$ = doNegate($2); }
6464 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
6466 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
6468 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
6470 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
6472 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
6474 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
6476 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
6478 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
6480 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
6481 | b_expr qual_Op b_expr %prec Op
6482 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
6483 | qual_Op b_expr %prec Op
6484 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
6485 | b_expr qual_Op %prec POSTFIXOP
6486 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
6487 | b_expr IS DISTINCT FROM b_expr %prec IS
6489 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
6491 | b_expr IS OF '(' type_list ')' %prec IS
6493 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
6495 | b_expr IS NOT OF '(' type_list ')' %prec IS
6497 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
6502 * Productions that can be used in both a_expr and b_expr.
6504 * Note: productions that refer recursively to a_expr or b_expr mostly
6505 * cannot appear here. However, it's OK to refer to a_exprs that occur
6506 * inside parentheses, such as function arguments; that cannot introduce
6507 * ambiguity to the b_expr syntax.
6509 c_expr: columnref { $$ = $1; }
6510 | AexprConst { $$ = $1; }
6511 | PARAM opt_indirection
6513 ParamRef *p = makeNode(ParamRef);
6517 A_Indirection *n = makeNode(A_Indirection);
6518 n->arg = (Node *) p;
6519 n->indirection = $2;
6525 | '(' a_expr ')' opt_indirection
6529 A_Indirection *n = makeNode(A_Indirection);
6531 n->indirection = $4;
6541 | select_with_parens %prec UMINUS
6543 SubLink *n = makeNode(SubLink);
6544 n->subLinkType = EXPR_SUBLINK;
6550 | EXISTS select_with_parens
6552 SubLink *n = makeNode(SubLink);
6553 n->subLinkType = EXISTS_SUBLINK;
6559 | ARRAY select_with_parens
6561 SubLink *n = makeNode(SubLink);
6562 n->subLinkType = ARRAY_SUBLINK;
6572 RowExpr *r = makeNode(RowExpr);
6574 r->row_typeid = InvalidOid; /* not analyzed yet */
6580 * func_expr is split out from c_expr just so that we have a classification
6581 * for "everything that is a function call or looks like one". This isn't
6582 * very important, but it saves us having to document which variants are
6583 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
6584 * (Note that many of the special SQL functions wouldn't actually make any
6585 * sense as functional index entries, but we ignore that consideration here.)
6587 func_expr: func_name '(' ')'
6589 FuncCall *n = makeNode(FuncCall);
6592 n->agg_star = FALSE;
6593 n->agg_distinct = FALSE;
6596 | func_name '(' expr_list ')'
6598 FuncCall *n = makeNode(FuncCall);
6601 n->agg_star = FALSE;
6602 n->agg_distinct = FALSE;
6605 | func_name '(' ALL expr_list ')'
6607 FuncCall *n = makeNode(FuncCall);
6610 n->agg_star = FALSE;
6611 n->agg_distinct = FALSE;
6612 /* Ideally we'd mark the FuncCall node to indicate
6613 * "must be an aggregate", but there's no provision
6614 * for that in FuncCall at the moment.
6618 | func_name '(' DISTINCT expr_list ')'
6620 FuncCall *n = makeNode(FuncCall);
6623 n->agg_star = FALSE;
6624 n->agg_distinct = TRUE;
6627 | func_name '(' '*' ')'
6630 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
6632 * This does the right thing for COUNT(*) (in fact,
6633 * any certainly-non-null expression would do for COUNT),
6634 * and there are no other aggregates in SQL92 that accept
6637 * The FuncCall node is also marked agg_star = true,
6638 * so that later processing can detect what the argument
6641 FuncCall *n = makeNode(FuncCall);
6642 A_Const *star = makeNode(A_Const);
6644 star->val.type = T_Integer;
6645 star->val.val.ival = 1;
6647 n->args = list_make1(star);
6649 n->agg_distinct = FALSE;
6655 * Translate as "'now'::text::date".
6657 * We cannot use "'now'::date" because coerce_type() will
6658 * immediately reduce that to a constant representing
6659 * today's date. We need to delay the conversion until
6660 * runtime, else the wrong things will happen when
6661 * CURRENT_DATE is used in a column default value or rule.
6663 * This could be simplified if we had a way to generate
6664 * an expression tree representing runtime application
6665 * of type-input conversion functions...
6667 A_Const *s = makeNode(A_Const);
6670 s->val.type = T_String;
6671 s->val.val.str = "now";
6672 s->typename = SystemTypeName("text");
6674 d = SystemTypeName("date");
6676 $$ = (Node *)makeTypeCast((Node *)s, d);
6681 * Translate as "'now'::text::timetz".
6682 * See comments for CURRENT_DATE.
6684 A_Const *s = makeNode(A_Const);
6687 s->val.type = T_String;
6688 s->val.val.str = "now";
6689 s->typename = SystemTypeName("text");
6691 d = SystemTypeName("timetz");
6692 /* SQL99 mandates a default precision of zero for TIME
6693 * fields in schemas. However, for CURRENT_TIME
6694 * let's preserve the microsecond precision we
6695 * might see from the system clock. - thomas 2001-12-07
6699 $$ = (Node *)makeTypeCast((Node *)s, d);
6701 | CURRENT_TIME '(' Iconst ')'
6704 * Translate as "'now'::text::timetz(n)".
6705 * See comments for CURRENT_DATE.
6707 A_Const *s = makeNode(A_Const);
6710 s->val.type = T_String;
6711 s->val.val.str = "now";
6712 s->typename = SystemTypeName("text");
6713 d = SystemTypeName("timetz");
6716 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6717 errmsg("CURRENT_TIME(%d) precision must not be negative",
6719 if ($3 > MAX_TIME_PRECISION)
6722 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6723 errmsg("CURRENT_TIME(%d) precision reduced to maximum allowed, %d",
6724 $3, MAX_TIME_PRECISION)));
6725 $3 = MAX_TIME_PRECISION;
6729 $$ = (Node *)makeTypeCast((Node *)s, d);
6734 * Translate as "'now'::text::timestamptz".
6735 * See comments for CURRENT_DATE.
6737 A_Const *s = makeNode(A_Const);
6740 s->val.type = T_String;
6741 s->val.val.str = "now";
6742 s->typename = SystemTypeName("text");
6744 d = SystemTypeName("timestamptz");
6745 /* SQL99 mandates a default precision of 6 for timestamp.
6746 * Also, that is about as precise as we will get since
6747 * we are using a microsecond time interface.
6748 * - thomas 2001-12-07
6752 $$ = (Node *)makeTypeCast((Node *)s, d);
6754 | CURRENT_TIMESTAMP '(' Iconst ')'
6757 * Translate as "'now'::text::timestamptz(n)".
6758 * See comments for CURRENT_DATE.
6760 A_Const *s = makeNode(A_Const);
6763 s->val.type = T_String;
6764 s->val.val.str = "now";
6765 s->typename = SystemTypeName("text");
6767 d = SystemTypeName("timestamptz");
6770 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6771 errmsg("CURRENT_TIMESTAMP(%d) precision must not be negative",
6773 if ($3 > MAX_TIMESTAMP_PRECISION)
6776 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6777 errmsg("CURRENT_TIMESTAMP(%d) precision reduced to maximum allowed, %d",
6778 $3, MAX_TIMESTAMP_PRECISION)));
6779 $3 = MAX_TIMESTAMP_PRECISION;
6783 $$ = (Node *)makeTypeCast((Node *)s, d);
6788 * Translate as "'now'::text::time".
6789 * See comments for CURRENT_DATE.
6791 A_Const *s = makeNode(A_Const);
6794 s->val.type = T_String;
6795 s->val.val.str = "now";
6796 s->typename = SystemTypeName("text");
6798 d = SystemTypeName("time");
6799 /* SQL99 mandates a default precision of zero for TIME
6800 * fields in schemas. However, for LOCALTIME
6801 * let's preserve the microsecond precision we
6802 * might see from the system clock. - thomas 2001-12-07
6806 $$ = (Node *)makeTypeCast((Node *)s, d);
6808 | LOCALTIME '(' Iconst ')'
6811 * Translate as "'now'::text::time(n)".
6812 * See comments for CURRENT_DATE.
6814 A_Const *s = makeNode(A_Const);
6817 s->val.type = T_String;
6818 s->val.val.str = "now";
6819 s->typename = SystemTypeName("text");
6820 d = SystemTypeName("time");
6823 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6824 errmsg("LOCALTIME(%d) precision must not be negative",
6826 if ($3 > MAX_TIME_PRECISION)
6829 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6830 errmsg("LOCALTIME(%d) precision reduced to maximum allowed, %d",
6831 $3, MAX_TIME_PRECISION)));
6832 $3 = MAX_TIME_PRECISION;
6836 $$ = (Node *)makeTypeCast((Node *)s, d);
6841 * Translate as "'now'::text::timestamp".
6842 * See comments for CURRENT_DATE.
6844 A_Const *s = makeNode(A_Const);
6847 s->val.type = T_String;
6848 s->val.val.str = "now";
6849 s->typename = SystemTypeName("text");
6851 d = SystemTypeName("timestamp");
6852 /* SQL99 mandates a default precision of 6 for timestamp.
6853 * Also, that is about as precise as we will get since
6854 * we are using a microsecond time interface.
6855 * - thomas 2001-12-07
6859 $$ = (Node *)makeTypeCast((Node *)s, d);
6861 | LOCALTIMESTAMP '(' Iconst ')'
6864 * Translate as "'now'::text::timestamp(n)".
6865 * See comments for CURRENT_DATE.
6867 A_Const *s = makeNode(A_Const);
6870 s->val.type = T_String;
6871 s->val.val.str = "now";
6872 s->typename = SystemTypeName("text");
6874 d = SystemTypeName("timestamp");
6877 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6878 errmsg("LOCALTIMESTAMP(%d) precision must not be negative",
6880 if ($3 > MAX_TIMESTAMP_PRECISION)
6883 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6884 errmsg("LOCALTIMESTAMP(%d) precision reduced to maximum allowed, %d",
6885 $3, MAX_TIMESTAMP_PRECISION)));
6886 $3 = MAX_TIMESTAMP_PRECISION;
6890 $$ = (Node *)makeTypeCast((Node *)s, d);
6894 FuncCall *n = makeNode(FuncCall);
6895 n->funcname = SystemFuncName("current_user");
6897 n->agg_star = FALSE;
6898 n->agg_distinct = FALSE;
6903 FuncCall *n = makeNode(FuncCall);
6904 n->funcname = SystemFuncName("session_user");
6906 n->agg_star = FALSE;
6907 n->agg_distinct = FALSE;
6912 FuncCall *n = makeNode(FuncCall);
6913 n->funcname = SystemFuncName("current_user");
6915 n->agg_star = FALSE;
6916 n->agg_distinct = FALSE;
6919 | CAST '(' a_expr AS Typename ')'
6920 { $$ = makeTypeCast($3, $5); }
6921 | EXTRACT '(' extract_list ')'
6923 FuncCall *n = makeNode(FuncCall);
6924 n->funcname = SystemFuncName("date_part");
6926 n->agg_star = FALSE;
6927 n->agg_distinct = FALSE;
6930 | OVERLAY '(' overlay_list ')'
6932 /* overlay(A PLACING B FROM C FOR D) is converted to
6933 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
6934 * overlay(A PLACING B FROM C) is converted to
6935 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
6937 FuncCall *n = makeNode(FuncCall);
6938 n->funcname = SystemFuncName("overlay");
6940 n->agg_star = FALSE;
6941 n->agg_distinct = FALSE;
6944 | POSITION '(' position_list ')'
6946 /* position(A in B) is converted to position(B, A) */
6947 FuncCall *n = makeNode(FuncCall);
6948 n->funcname = SystemFuncName("position");
6950 n->agg_star = FALSE;
6951 n->agg_distinct = FALSE;
6954 | SUBSTRING '(' substr_list ')'
6956 /* substring(A from B for C) is converted to
6957 * substring(A, B, C) - thomas 2000-11-28
6959 FuncCall *n = makeNode(FuncCall);
6960 n->funcname = SystemFuncName("substring");
6962 n->agg_star = FALSE;
6963 n->agg_distinct = FALSE;
6966 | TREAT '(' a_expr AS Typename ')'
6968 /* TREAT(expr AS target) converts expr of a particular type to target,
6969 * which is defined to be a subtype of the original expression.
6970 * In SQL99, this is intended for use with structured UDTs,
6971 * but let's make this a generally useful form allowing stronger
6972 * coersions than are handled by implicit casting.
6974 FuncCall *n = makeNode(FuncCall);
6975 /* Convert SystemTypeName() to SystemFuncName() even though
6976 * at the moment they result in the same thing.
6978 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
6979 n->args = list_make1($3);
6982 | TRIM '(' BOTH trim_list ')'
6984 /* various trim expressions are defined in SQL92
6985 * - thomas 1997-07-19
6987 FuncCall *n = makeNode(FuncCall);
6988 n->funcname = SystemFuncName("btrim");
6990 n->agg_star = FALSE;
6991 n->agg_distinct = FALSE;
6994 | TRIM '(' LEADING trim_list ')'
6996 FuncCall *n = makeNode(FuncCall);
6997 n->funcname = SystemFuncName("ltrim");
6999 n->agg_star = FALSE;
7000 n->agg_distinct = FALSE;
7003 | TRIM '(' TRAILING trim_list ')'
7005 FuncCall *n = makeNode(FuncCall);
7006 n->funcname = SystemFuncName("rtrim");
7008 n->agg_star = FALSE;
7009 n->agg_distinct = FALSE;
7012 | TRIM '(' trim_list ')'
7014 FuncCall *n = makeNode(FuncCall);
7015 n->funcname = SystemFuncName("btrim");
7017 n->agg_star = FALSE;
7018 n->agg_distinct = FALSE;
7021 | CONVERT '(' a_expr USING any_name ')'
7023 FuncCall *n = makeNode(FuncCall);
7024 A_Const *c = makeNode(A_Const);
7026 c->val.type = T_String;
7027 c->val.val.str = NameListToQuotedString($5);
7029 n->funcname = SystemFuncName("convert_using");
7030 n->args = list_make2($3, c);
7031 n->agg_star = FALSE;
7032 n->agg_distinct = FALSE;
7035 | CONVERT '(' expr_list ')'
7037 FuncCall *n = makeNode(FuncCall);
7038 n->funcname = SystemFuncName("convert");
7040 n->agg_star = FALSE;
7041 n->agg_distinct = FALSE;
7044 | NULLIF '(' a_expr ',' a_expr ')'
7046 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5);
7048 | COALESCE '(' expr_list ')'
7050 CoalesceExpr *c = makeNode(CoalesceExpr);
7057 * Supporting nonterminals for expressions.
7060 /* Explicit row production.
7062 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
7063 * without conflicting with the parenthesized a_expr production. Without the
7064 * ROW keyword, there must be more than one a_expr inside the parens.
7066 row: ROW '(' expr_list ')' { $$ = $3; }
7067 | ROW '(' ')' { $$ = NIL; }
7068 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
7071 sub_type: ANY { $$ = ANY_SUBLINK; }
7072 | SOME { $$ = ANY_SUBLINK; }
7073 | ALL { $$ = ALL_SUBLINK; }
7076 all_Op: Op { $$ = $1; }
7077 | MathOp { $$ = $1; }
7080 MathOp: '+' { $$ = "+"; }
7092 { $$ = list_make1(makeString($1)); }
7093 | OPERATOR '(' any_operator ')'
7099 { $$ = list_make1(makeString($1)); }
7100 | OPERATOR '(' any_operator ')'
7106 { $$ = list_make1(makeString($1)); }
7107 | OPERATOR '(' any_operator ')'
7110 { $$ = list_make1(makeString("~~")); }
7112 { $$ = list_make1(makeString("!~~")); }
7114 { $$ = list_make1(makeString("~~*")); }
7116 { $$ = list_make1(makeString("!~~*")); }
7117 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
7118 * the regular expression is preprocessed by a function (similar_escape),
7119 * and the ~ operator for posix regular expressions is used.
7120 * x SIMILAR TO y -> x ~ similar_escape(y)
7121 * this transformation is made on the fly by the parser upwards.
7122 * however the SubLink structure which handles any/some/all stuff
7123 * is not ready for such a thing.
7129 $$ = list_make1($1);
7131 | expr_list ',' a_expr
7133 $$ = lappend($1, $3);
7138 extract_arg FROM a_expr
7140 A_Const *n = makeNode(A_Const);
7141 n->val.type = T_String;
7142 n->val.val.str = $1;
7143 $$ = list_make2((Node *) n, $3);
7145 | /*EMPTY*/ { $$ = NIL; }
7148 type_list: type_list ',' Typename
7150 $$ = lappend($1, $3);
7154 $$ = list_make1($1);
7158 array_expr_list: array_expr
7159 { $$ = list_make1($1); }
7160 | array_expr_list ',' array_expr
7161 { $$ = lappend($1, $3); }
7164 array_expr: '[' expr_list ']'
7166 ArrayExpr *n = makeNode(ArrayExpr);
7170 | '[' array_expr_list ']'
7172 ArrayExpr *n = makeNode(ArrayExpr);
7178 /* Allow delimited string SCONST in extract_arg as an SQL extension.
7179 * - thomas 2001-04-12
7184 | YEAR_P { $$ = "year"; }
7185 | MONTH_P { $$ = "month"; }
7186 | DAY_P { $$ = "day"; }
7187 | HOUR_P { $$ = "hour"; }
7188 | MINUTE_P { $$ = "minute"; }
7189 | SECOND_P { $$ = "second"; }
7190 | SCONST { $$ = $1; }
7193 /* OVERLAY() arguments
7194 * SQL99 defines the OVERLAY() function:
7195 * o overlay(text placing text from int for int)
7196 * o overlay(text placing text from int)
7199 a_expr overlay_placing substr_from substr_for
7201 $$ = list_make4($1, $2, $3, $4);
7203 | a_expr overlay_placing substr_from
7205 $$ = list_make3($1, $2, $3);
7214 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
7217 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
7218 | /*EMPTY*/ { $$ = NIL; }
7221 /* SUBSTRING() arguments
7222 * SQL9x defines a specific syntax for arguments to SUBSTRING():
7223 * o substring(text from int for int)
7224 * o substring(text from int) get entire string from starting point "int"
7225 * o substring(text from pattern) get entire string matching pattern
7226 * o substring(text for int) get first "int" characters of string
7227 * We also want to implement generic substring functions which accept
7228 * the usual generic list of arguments. So we will accept both styles
7229 * here, and convert the SQL9x style to the generic list for further
7230 * processing. - thomas 2000-11-28
7233 a_expr substr_from substr_for
7235 $$ = list_make3($1, $2, $3);
7237 | a_expr substr_for substr_from
7239 $$ = list_make3($1, $3, $2);
7241 | a_expr substr_from
7243 $$ = list_make2($1, $2);
7247 A_Const *n = makeNode(A_Const);
7248 n->val.type = T_Integer;
7249 n->val.val.ival = 1;
7250 $$ = list_make3($1, (Node *)n, $2);
7261 FROM a_expr { $$ = $2; }
7264 substr_for: FOR a_expr { $$ = $2; }
7267 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
7268 | FROM expr_list { $$ = $2; }
7269 | expr_list { $$ = $1; }
7272 in_expr: select_with_parens
7274 SubLink *n = makeNode(SubLink);
7276 /* other fields will be filled later */
7279 | '(' expr_list ')' { $$ = (Node *)$2; }
7283 * Define SQL92-style case clause.
7284 * - Full specification
7285 * CASE WHEN a = b THEN c ... ELSE d END
7286 * - Implicit argument
7287 * CASE a WHEN b THEN c ... ELSE d END
7289 case_expr: CASE case_arg when_clause_list case_default END_P
7291 CaseExpr *c = makeNode(CaseExpr);
7292 c->casetype = InvalidOid; /* not analyzed yet */
7293 c->arg = (Expr *) $2;
7295 c->defresult = (Expr *) $4;
7301 /* There must be at least one */
7302 when_clause { $$ = list_make1($1); }
7303 | when_clause_list when_clause { $$ = lappend($1, $2); }
7307 WHEN a_expr THEN a_expr
7309 CaseWhen *w = makeNode(CaseWhen);
7310 w->expr = (Expr *) $2;
7311 w->result = (Expr *) $4;
7317 ELSE a_expr { $$ = $2; }
7318 | /*EMPTY*/ { $$ = NULL; }
7321 case_arg: a_expr { $$ = $1; }
7322 | /*EMPTY*/ { $$ = NULL; }
7326 * columnref starts with relation_name not ColId, so that OLD and NEW
7327 * references can be accepted. Note that when there are more than two
7328 * dotted names, the first name is not actually a relation name...
7330 columnref: relation_name
7332 $$ = makeColumnRef($1, NIL);
7334 | relation_name indirection
7336 $$ = makeColumnRef($1, $2);
7343 $$ = (Node *) makeString($2);
7347 $$ = (Node *) makeString("*");
7351 A_Indices *ai = makeNode(A_Indices);
7356 | '[' a_expr ':' a_expr ']'
7358 A_Indices *ai = makeNode(A_Indices);
7366 indirection_el { $$ = list_make1($1); }
7367 | indirection indirection_el { $$ = lappend($1, $2); }
7371 /*EMPTY*/ { $$ = NIL; }
7372 | opt_indirection indirection_el { $$ = lappend($1, $2); }
7376 /*****************************************************************************
7378 * target lists for SELECT, UPDATE, INSERT
7380 *****************************************************************************/
7383 target_el { $$ = list_make1($1); }
7384 | target_list ',' target_el { $$ = lappend($1, $3); }
7387 /* AS is not optional because shift/red conflict with unary ops */
7388 target_el: a_expr AS ColLabel
7390 $$ = makeNode(ResTarget);
7392 $$->indirection = NIL;
7393 $$->val = (Node *)$1;
7397 $$ = makeNode(ResTarget);
7399 $$->indirection = NIL;
7400 $$->val = (Node *)$1;
7404 ColumnRef *n = makeNode(ColumnRef);
7405 n->fields = list_make1(makeString("*"));
7407 $$ = makeNode(ResTarget);
7409 $$->indirection = NIL;
7410 $$->val = (Node *)n;
7415 update_target_el { $$ = list_make1($1); }
7416 | update_target_list ',' update_target_el { $$ = lappend($1,$3); }
7420 ColId opt_indirection '=' a_expr
7422 $$ = makeNode(ResTarget);
7424 $$->indirection = $2;
7425 $$->val = (Node *) $4;
7427 | ColId opt_indirection '=' DEFAULT
7429 $$ = makeNode(ResTarget);
7431 $$->indirection = $2;
7432 $$->val = (Node *) makeNode(SetToDefault);
7438 insert_target_el { $$ = list_make1($1); }
7439 | insert_target_list ',' insert_target_el { $$ = lappend($1, $3); }
7445 $$ = makeNode(ResTarget);
7447 $$->indirection = NIL;
7448 $$->val = (Node *)$1;
7452 $$ = makeNode(ResTarget);
7454 $$->indirection = NIL;
7455 $$->val = (Node *) makeNode(SetToDefault);
7460 /*****************************************************************************
7462 * Names and constants
7464 *****************************************************************************/
7467 SpecialRuleRelation { $$ = $1; }
7468 | ColId { $$ = $1; }
7471 qualified_name_list:
7472 qualified_name { $$ = list_make1($1); }
7473 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
7477 * The production for a qualified relation name has to exactly match the
7478 * production for a qualified func_name, because in a FROM clause we cannot
7479 * tell which we are parsing until we see what comes after it ('(' for a
7480 * func_name, something else for a relation). Therefore we allow 'indirection'
7481 * which may contain subscripts, and reject that case in the C code.
7486 $$ = makeNode(RangeVar);
7487 $$->catalogname = NULL;
7488 $$->schemaname = NULL;
7491 | relation_name indirection
7493 check_qualified_name($2);
7494 $$ = makeNode(RangeVar);
7495 switch (list_length($2))
7498 $$->catalogname = NULL;
7499 $$->schemaname = $1;
7500 $$->relname = strVal(linitial($2));
7503 $$->catalogname = $1;
7504 $$->schemaname = strVal(linitial($2));
7505 $$->relname = strVal(lsecond($2));
7509 (errcode(ERRCODE_SYNTAX_ERROR),
7510 errmsg("improper qualified name (too many dotted names): %s",
7511 NameListToString(lcons(makeString($1), $2)))));
7518 { $$ = list_make1(makeString($1)); }
7519 | name_list ',' name
7520 { $$ = lappend($1, makeString($3)); }
7524 name: ColId { $$ = $1; };
7532 attr_name: ColLabel { $$ = $1; };
7534 index_name: ColId { $$ = $1; };
7536 file_name: Sconst { $$ = $1; };
7539 * The production for a qualified func_name has to exactly match the
7540 * production for a qualified columnref, because we cannot tell which we
7541 * are parsing until we see what comes after it ('(' for a func_name,
7542 * anything else for a columnref). Therefore we allow 'indirection' which
7543 * may contain subscripts, and reject that case in the C code. (If we
7544 * ever implement SQL99-like methods, such syntax may actually become legal!)
7546 func_name: function_name
7547 { $$ = list_make1(makeString($1)); }
7548 | relation_name indirection
7549 { $$ = check_func_name(lcons(makeString($1), $2)); }
7558 A_Const *n = makeNode(A_Const);
7559 n->val.type = T_Integer;
7560 n->val.val.ival = $1;
7565 A_Const *n = makeNode(A_Const);
7566 n->val.type = T_Float;
7567 n->val.val.str = $1;
7572 A_Const *n = makeNode(A_Const);
7573 n->val.type = T_String;
7574 n->val.val.str = $1;
7579 A_Const *n = makeNode(A_Const);
7580 n->val.type = T_BitString;
7581 n->val.val.str = $1;
7586 /* This is a bit constant per SQL99:
7587 * Without Feature F511, "BIT data type",
7588 * a <general literal> shall not be a
7589 * <bit string literal> or a <hex string literal>.
7591 A_Const *n = makeNode(A_Const);
7592 n->val.type = T_BitString;
7593 n->val.val.str = $1;
7596 | ConstTypename Sconst
7598 A_Const *n = makeNode(A_Const);
7600 n->val.type = T_String;
7601 n->val.val.str = $2;
7604 | ConstInterval Sconst opt_interval
7606 A_Const *n = makeNode(A_Const);
7608 n->val.type = T_String;
7609 n->val.val.str = $2;
7610 /* precision is not specified, but fields may be... */
7611 if ($3 != INTERVAL_FULL_RANGE)
7612 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
7615 | ConstInterval '(' Iconst ')' Sconst opt_interval
7617 A_Const *n = makeNode(A_Const);
7619 n->val.type = T_String;
7620 n->val.val.str = $5;
7621 /* precision specified, and fields may be... */
7624 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7625 errmsg("INTERVAL(%d) precision must not be negative",
7627 if ($3 > MAX_INTERVAL_PRECISION)
7630 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7631 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
7632 $3, MAX_INTERVAL_PRECISION)));
7633 $3 = MAX_INTERVAL_PRECISION;
7635 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
7640 $$ = (Node *)makeBoolAConst(TRUE);
7644 $$ = (Node *)makeBoolAConst(FALSE);
7648 A_Const *n = makeNode(A_Const);
7649 n->val.type = T_Null;
7654 Iconst: ICONST { $$ = $1; };
7655 Sconst: SCONST { $$ = $1; };
7656 UserId: ColId { $$ = $1; };
7659 * Name classification hierarchy.
7661 * IDENT is the lexeme returned by the lexer for identifiers that match
7662 * no known keyword. In most cases, we can accept certain keywords as
7663 * names, not only IDENTs. We prefer to accept as many such keywords
7664 * as possible to minimize the impact of "reserved words" on programmers.
7665 * So, we divide names into several possible classes. The classification
7666 * is chosen in part to make keywords acceptable as names wherever possible.
7669 /* Column identifier --- names that can be column, table, etc names.
7671 ColId: IDENT { $$ = $1; }
7672 | unreserved_keyword { $$ = pstrdup($1); }
7673 | col_name_keyword { $$ = pstrdup($1); }
7676 /* Type identifier --- names that can be type names.
7678 type_name: IDENT { $$ = $1; }
7679 | unreserved_keyword { $$ = pstrdup($1); }
7682 /* Function identifier --- names that can be function names.
7686 | unreserved_keyword { $$ = pstrdup($1); }
7687 | func_name_keyword { $$ = pstrdup($1); }
7690 /* Column label --- allowed labels in "AS" clauses.
7691 * This presently includes *all* Postgres keywords.
7693 ColLabel: IDENT { $$ = $1; }
7694 | unreserved_keyword { $$ = pstrdup($1); }
7695 | col_name_keyword { $$ = pstrdup($1); }
7696 | func_name_keyword { $$ = pstrdup($1); }
7697 | reserved_keyword { $$ = pstrdup($1); }
7702 * Keyword classification lists. Generally, every keyword present in
7703 * the Postgres grammar should appear in exactly one of these lists.
7705 * Put a new keyword into the first list that it can go into without causing
7706 * shift or reduce conflicts. The earlier lists define "less reserved"
7707 * categories of keywords.
7710 /* "Unreserved" keywords --- available for use as any kind of name.
7904 /* Column identifier --- keywords that can be column, table, etc names.
7906 * Many of these keywords will in fact be recognized as type or function
7907 * names too; but they have special productions for the purpose, and so
7908 * can't be treated as "generic" type or function names.
7910 * The type names appearing here are not usable as function names
7911 * because they can be followed by '(' in typename productions, which
7912 * looks too much like a function call for an LR(1) parser.
7952 /* Function identifier --- keywords that can be function names.
7954 * Most of these are keywords that are used as operators in expressions;
7955 * in general such keywords can't be column names because they would be
7956 * ambiguous with variables, but they are unambiguous as function identifiers.
7958 * Do not include POSITION, SUBSTRING, etc here since they have explicit
7959 * productions in a_expr to support the goofy SQL9x argument syntax.
7960 * - thomas 2000-11-28
7985 /* Reserved keyword --- these keywords are usable only as a ColLabel.
7987 * Keywords appear here if they could not be distinguished from variable,
7988 * type, or function names in some contexts. Don't put things here unless
8065 SpecialRuleRelation:
8072 (errcode(ERRCODE_SYNTAX_ERROR),
8073 errmsg("OLD used in query that is not in a rule")));
8081 (errcode(ERRCODE_SYNTAX_ERROR),
8082 errmsg("NEW used in query that is not in a rule")));
8089 makeColumnRef(char *relname, List *indirection)
8092 * Generate a ColumnRef node, with an A_Indirection node added if there
8093 * is any subscripting in the specified indirection list. However,
8094 * any field selection at the start of the indirection list must be
8095 * transposed into the "fields" part of the ColumnRef node.
8097 ColumnRef *c = makeNode(ColumnRef);
8101 foreach(l, indirection)
8103 if (IsA(lfirst(l), A_Indices))
8105 A_Indirection *i = makeNode(A_Indirection);
8109 /* easy case - all indirection goes to A_Indirection */
8110 c->fields = list_make1(makeString(relname));
8111 i->indirection = indirection;
8115 /* got to split the list in two */
8116 i->indirection = list_copy_tail(indirection, nfields);
8117 indirection = list_truncate(indirection, nfields);
8118 c->fields = lcons(makeString(relname), indirection);
8120 i->arg = (Node *) c;
8125 /* No subscripting, so all indirection gets added to field list */
8126 c->fields = lcons(makeString(relname), indirection);
8131 makeTypeCast(Node *arg, TypeName *typename)
8134 * Simply generate a TypeCast node.
8136 * Earlier we would determine whether an A_Const would
8137 * be acceptable, however Domains require coerce_type()
8138 * to process them -- applying constraints as required.
8140 TypeCast *n = makeNode(TypeCast);
8142 n->typename = typename;
8147 makeStringConst(char *str, TypeName *typename)
8149 A_Const *n = makeNode(A_Const);
8151 n->val.type = T_String;
8152 n->val.val.str = str;
8153 n->typename = typename;
8159 makeIntConst(int val)
8161 A_Const *n = makeNode(A_Const);
8162 n->val.type = T_Integer;
8163 n->val.val.ival = val;
8164 n->typename = SystemTypeName("int4");
8170 makeFloatConst(char *str)
8172 A_Const *n = makeNode(A_Const);
8174 n->val.type = T_Float;
8175 n->val.val.str = str;
8176 n->typename = SystemTypeName("float8");
8182 makeAConst(Value *v)
8189 n = makeFloatConst(v->val.str);
8193 n = makeIntConst(v->val.ival);
8198 n = makeStringConst(v->val.str, NULL);
8206 * Create a DefElem node and set contents.
8207 * Could be moved to nodes/makefuncs.c if this is useful elsewhere.
8210 makeDefElem(char *name, Node *arg)
8212 DefElem *f = makeNode(DefElem);
8219 * Create an A_Const node and initialize to a boolean constant.
8222 makeBoolAConst(bool state)
8224 A_Const *n = makeNode(A_Const);
8225 n->val.type = T_String;
8226 n->val.val.str = (state? "t": "f");
8227 n->typename = SystemTypeName("bool");
8231 /* makeRowNullTest()
8232 * Generate separate operator nodes for a single row descriptor test.
8234 * Eventually this should be eliminated in favor of making the NullTest
8235 * node type capable of handling it directly.
8238 makeRowNullTest(NullTestType test, RowExpr *row)
8240 Node *result = NULL;
8243 foreach(arg, row->args)
8247 n = makeNode(NullTest);
8248 n->arg = (Expr *) lfirst(arg);
8249 n->nulltesttype = test;
8252 result = (Node *) n;
8253 else if (test == IS_NOT_NULL)
8254 result = (Node *) makeA_Expr(AEXPR_OR, NIL, result, (Node *)n);
8256 result = (Node *) makeA_Expr(AEXPR_AND, NIL, result, (Node *)n);
8261 /* zero-length rows? Generate constant TRUE or FALSE */
8262 result = (Node *) makeBoolAConst(test == IS_NULL);
8269 * Create and populate a FuncCall node to support the OVERLAPS operator.
8272 makeOverlaps(List *largs, List *rargs)
8274 FuncCall *n = makeNode(FuncCall);
8275 n->funcname = SystemFuncName("overlaps");
8276 if (list_length(largs) == 1)
8277 largs = lappend(largs, largs);
8278 else if (list_length(largs) != 2)
8280 (errcode(ERRCODE_SYNTAX_ERROR),
8281 errmsg("wrong number of parameters on left side of OVERLAPS expression")));
8282 if (list_length(rargs) == 1)
8283 rargs = lappend(rargs, rargs);
8284 else if (list_length(rargs) != 2)
8286 (errcode(ERRCODE_SYNTAX_ERROR),
8287 errmsg("wrong number of parameters on right side of OVERLAPS expression")));
8288 n->args = list_concat(largs, rargs);
8289 n->agg_star = FALSE;
8290 n->agg_distinct = FALSE;
8294 /* check_qualified_name --- check the result of qualified_name production
8296 * It's easiest to let the grammar production for qualified_name allow
8297 * subscripts and '*', which we then must reject here.
8300 check_qualified_name(List *names)
8306 if (!IsA(lfirst(i), String))
8307 yyerror("syntax error");
8308 else if (strcmp(strVal(lfirst(i)), "*") == 0)
8309 yyerror("syntax error");
8313 /* check_func_name --- check the result of func_name production
8315 * It's easiest to let the grammar production for func_name allow subscripts
8316 * and '*', which we then must reject here.
8319 check_func_name(List *names)
8325 if (!IsA(lfirst(i), String))
8326 yyerror("syntax error");
8327 else if (strcmp(strVal(lfirst(i)), "*") == 0)
8328 yyerror("syntax error");
8333 /* extractArgTypes()
8334 * Given a list of FunctionParameter nodes, extract a list of just the
8335 * argument types (TypeNames) for input parameters only. This is what
8336 * is needed to look up an existing function, which is what is wanted by
8337 * the productions that use this call.
8340 extractArgTypes(List *parameters)
8345 foreach(i, parameters)
8347 FunctionParameter *p = (FunctionParameter *) lfirst(i);
8349 if (p->mode != FUNC_PARAM_OUT) /* keep if IN or INOUT */
8350 result = lappend(result, p->argType);
8355 /* findLeftmostSelect()
8356 * Find the leftmost component SelectStmt in a set-operation parsetree.
8359 findLeftmostSelect(SelectStmt *node)
8361 while (node && node->op != SETOP_NONE)
8363 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
8367 /* insertSelectOptions()
8368 * Insert ORDER BY, etc into an already-constructed SelectStmt.
8370 * This routine is just to avoid duplicating code in SelectStmt productions.
8373 insertSelectOptions(SelectStmt *stmt,
8374 List *sortClause, List *lockingClause,
8375 Node *limitOffset, Node *limitCount)
8378 * Tests here are to reject constructs like
8379 * (SELECT foo ORDER BY bar) ORDER BY baz
8383 if (stmt->sortClause)
8385 (errcode(ERRCODE_SYNTAX_ERROR),
8386 errmsg("multiple ORDER BY clauses not allowed")));
8387 stmt->sortClause = sortClause;
8393 if (stmt->lockedRels)
8395 (errcode(ERRCODE_SYNTAX_ERROR),
8396 errmsg("multiple FOR UPDATE/FOR SHARE clauses not allowed")));
8398 Assert(list_length(lockingClause) > 1);
8399 /* 1st is Value node containing "for_update" or "for_share" */
8400 type = (Value *) linitial(lockingClause);
8401 Assert(IsA(type, String));
8402 if (strcmp(strVal(type), "for_update") == 0)
8403 stmt->forUpdate = true;
8404 else if (strcmp(strVal(type), "for_share") == 0)
8405 stmt->forUpdate = false;
8407 elog(ERROR, "invalid first node in locking clause");
8409 stmt->lockedRels = list_delete_first(lockingClause);
8413 if (stmt->limitOffset)
8415 (errcode(ERRCODE_SYNTAX_ERROR),
8416 errmsg("multiple OFFSET clauses not allowed")));
8417 stmt->limitOffset = limitOffset;
8421 if (stmt->limitCount)
8423 (errcode(ERRCODE_SYNTAX_ERROR),
8424 errmsg("multiple LIMIT clauses not allowed")));
8425 stmt->limitCount = limitCount;
8430 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
8432 SelectStmt *n = makeNode(SelectStmt);
8436 n->larg = (SelectStmt *) larg;
8437 n->rarg = (SelectStmt *) rarg;
8442 * Build a properly-qualified reference to a built-in function.
8445 SystemFuncName(char *name)
8447 return list_make2(makeString("pg_catalog"), makeString(name));
8451 * Build a properly-qualified reference to a built-in type.
8453 * typmod is defaulted, but may be changed afterwards by caller.
8456 SystemTypeName(char *name)
8458 TypeName *n = makeNode(TypeName);
8460 n->names = list_make2(makeString("pg_catalog"), makeString(name));
8466 * Initialize to parse one query string
8471 QueryIsRule = FALSE;
8474 /* exprIsNullConstant()
8475 * Test whether an a_expr is a plain NULL constant or not.
8478 exprIsNullConstant(Node *arg)
8480 if (arg && IsA(arg, A_Const))
8482 A_Const *con = (A_Const *) arg;
8484 if (con->val.type == T_Null &&
8485 con->typename == NULL)
8492 * Handle negation of a numeric constant.
8494 * Formerly, we did this here because the optimizer couldn't cope with
8495 * indexquals that looked like "var = -4" --- it wants "var = const"
8496 * and a unary minus operator applied to a constant didn't qualify.
8497 * As of Postgres 7.0, that problem doesn't exist anymore because there
8498 * is a constant-subexpression simplifier in the optimizer. However,
8499 * there's still a good reason for doing this here, which is that we can
8500 * postpone committing to a particular internal representation for simple
8501 * negative constants. It's better to leave "-123.456" in string form
8502 * until we know what the desired type is.
8507 if (IsA(n, A_Const))
8509 A_Const *con = (A_Const *)n;
8511 if (con->val.type == T_Integer)
8513 con->val.val.ival = -con->val.val.ival;
8516 if (con->val.type == T_Float)
8518 doNegateFloat(&con->val);
8523 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n);
8527 doNegateFloat(Value *v)
8529 char *oldval = v->val.str;
8531 Assert(IsA(v, Float));
8535 v->val.str = oldval+1; /* just strip the '-' */
8538 char *newval = (char *) palloc(strlen(oldval) + 2);
8541 strcpy(newval+1, oldval);
8542 v->val.str = newval;