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.503 2005/07/26 16:38:27 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 AlterRoleStmt AlterRoleSetStmt
137 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
138 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
139 CreateDomainStmt CreateGroupStmt CreateOpClassStmt CreatePLangStmt
140 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
141 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
142 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt
143 DropGroupStmt DropOpClassStmt DropPLangStmt DropStmt
144 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
145 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
146 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
147 LockStmt NotifyStmt ExplainableStmt PreparableStmt
148 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
149 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
150 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
151 SelectStmt TransactionStmt TruncateStmt
152 UnlistenStmt UpdateStmt VacuumStmt
153 VariableResetStmt VariableSetStmt VariableShowStmt
154 ViewStmt CheckPointStmt CreateConversionStmt
155 DeallocateStmt PrepareStmt ExecuteStmt
157 %type <node> select_no_parens select_with_parens select_clause
160 %type <node> alter_column_default opclass_item alter_using
161 %type <ival> add_drop
163 %type <node> alter_table_cmd alter_rel_cmd
164 %type <list> alter_table_cmds alter_rel_cmds
166 %type <dbehavior> opt_drop_behavior
168 %type <list> createdb_opt_list copy_opt_list transaction_mode_list
169 %type <defelt> createdb_opt_item copy_opt_item transaction_mode_item
171 %type <ival> opt_lock lock_type cast_context
172 %type <boolean> opt_force opt_or_replace
173 opt_grant_grant_option opt_grant_admin_option
176 %type <boolean> like_including_defaults
178 %type <list> OptRoleList
179 %type <defelt> OptRoleElem
181 %type <str> OptSchemaName
182 %type <list> OptSchemaEltList
184 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
185 %type <str> opt_lancompiler
187 %type <str> TriggerEvents
188 %type <value> TriggerFuncArg
190 %type <str> relation_name copy_file_name
191 database_name access_method_clause access_method attr_name
192 index_name name function_name file_name
194 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
195 opt_class opt_validator
197 %type <range> qualified_name OptConstrFromTable
199 %type <str> all_Op MathOp SpecialRuleRelation
201 %type <str> iso_level opt_encoding
203 %type <list> grantee_list
204 %type <str> privilege
205 %type <list> privileges privilege_list
206 %type <privtarget> privilege_target
207 %type <funwithargs> function_with_argtypes
208 %type <list> function_with_argtypes_list
209 %type <chr> TriggerOneEvent
211 %type <list> stmtblock stmtmulti
212 OptTableElementList TableElementList OptInherit definition
213 opt_distinct opt_definition func_args
214 func_args_list func_as createfunc_opt_list alterfunc_opt_list
215 oper_argtypes RuleActionList RuleActionMulti
216 opt_column_list columnList opt_name_list
217 sort_clause opt_sort_clause sortby_list index_params
218 name_list from_clause from_list opt_array_bounds
219 qualified_name_list any_name any_name_list
220 any_operator expr_list attrs
221 target_list update_target_list insert_column_list
222 insert_target_list def_list indirection opt_indirection
223 group_clause TriggerFuncArgs select_limit
224 opt_select_limit opclass_item_list
225 transaction_mode_list_or_empty
227 prep_type_clause prep_type_list
228 execute_param_clause using_clause
230 %type <range> into_clause OptTempTableName
232 %type <defelt> createfunc_opt_item common_func_opt_item
233 %type <fun_param> func_arg
234 %type <fun_param_mode> arg_class
235 %type <typnam> func_return func_type aggr_argtype
237 %type <boolean> TriggerForType OptTemp
238 %type <oncommit> OnCommitOption
239 %type <withoids> OptWithOids WithOidsAs
241 %type <list> for_locking_clause opt_for_locking_clause
243 %type <boolean> opt_all
245 %type <node> join_outer join_qual
246 %type <jtype> join_type
248 %type <list> extract_list overlay_list position_list
249 %type <list> substr_list trim_list
250 %type <ival> opt_interval
251 %type <node> overlay_placing substr_from substr_for
253 %type <boolean> opt_instead opt_analyze
254 %type <boolean> index_opt_unique opt_verbose opt_full
255 %type <boolean> opt_freeze opt_default opt_recheck
256 %type <defelt> opt_binary opt_oids copy_delimiter
258 %type <boolean> copy_from opt_hold
260 %type <ival> fetch_count opt_column event cursor_options
261 %type <objtype> reindex_type drop_type comment_type
263 %type <node> fetch_direction select_limit_value select_offset_value
265 %type <list> OptSeqList
266 %type <defelt> OptSeqElem
268 %type <istmt> insert_rest
270 %type <vsetstmt> set_rest
272 %type <node> TableElement ConstraintElem TableFuncElement
273 %type <node> columnDef
274 %type <defelt> def_elem
275 %type <node> def_arg columnElem where_clause
276 a_expr b_expr c_expr func_expr AexprConst indirection_el
277 columnref in_expr having_clause func_table array_expr
278 %type <list> row type_list array_expr_list
279 %type <node> case_expr case_arg when_clause case_default
280 %type <list> when_clause_list
281 %type <ival> sub_type
282 %type <list> OptCreateAs CreateAsList
283 %type <node> CreateAsElement
284 %type <value> NumericOnly FloatOnly IntegerOnly
285 %type <alias> alias_clause
286 %type <sortby> sortby
287 %type <ielem> index_elem
288 %type <node> table_ref
289 %type <jexpr> joined_table
290 %type <range> relation_expr
291 %type <target> target_el insert_target_el update_target_el insert_column_item
293 %type <typnam> Typename SimpleTypename ConstTypename
294 GenericType Numeric opt_float
295 Character ConstCharacter
296 CharacterWithLength CharacterWithoutLength
297 ConstDatetime ConstInterval
298 Bit ConstBit BitWithLength BitWithoutLength
299 %type <str> character
300 %type <str> extract_arg
301 %type <str> opt_charset
302 %type <ival> opt_numeric opt_decimal
303 %type <boolean> opt_varying opt_timezone
306 %type <str> Sconst comment_text
307 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
308 %type <list> var_list var_list_or_default
309 %type <str> ColId ColLabel var_name type_name param_name
310 %type <node> var_value zone_value
312 %type <keyword> unreserved_keyword func_name_keyword
313 %type <keyword> col_name_keyword reserved_keyword
315 %type <node> TableConstraint TableLikeClause
316 %type <list> ColQualList
317 %type <node> ColConstraint ColConstraintElem ConstraintAttr
318 %type <ival> key_actions key_delete key_match key_update key_action
319 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
322 %type <list> constraints_set_list
323 %type <boolean> constraints_set_mode
324 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
328 * If you make any token changes, update the keyword table in
329 * parser/keywords.c and add new keywords to the appropriate one of
330 * the reserved-or-not-so-reserved keyword lists, below; search
331 * this file for "Name classification hierarchy".
334 /* ordinary key words in alphabetical order */
335 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD ADMIN AFTER
336 AGGREGATE ALL ALSO ALTER ANALYSE ANALYZE AND ANY ARRAY AS ASC
337 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
339 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
342 CACHE CALLED CASCADE CASE CAST CHAIN CHAR_P
343 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
344 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
345 COMMITTED CONSTRAINT CONSTRAINTS CONVERSION_P CONVERT COPY CREATE CREATEDB
346 CREATEROLE CREATEUSER CROSS CSV CURRENT_DATE CURRENT_ROLE CURRENT_TIME
347 CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
349 DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
350 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS
351 DESC DISTINCT DO DOMAIN_P DOUBLE_P DROP
353 EACH ELSE ENCODING ENCRYPTED END_P ESCAPE EXCEPT EXCLUDING
354 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
356 FALSE_P FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
357 FREEZE FROM FULL FUNCTION
359 GLOBAL GRANT GRANTED GREATEST GROUP_P
361 HANDLER HAVING HEADER HOLD HOUR_P
363 ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCLUDING INCREMENT
364 INDEX INHERIT INHERITS INITIALLY INNER_P INOUT INPUT_P
365 INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT
366 INTERVAL INTO INVOKER IS ISNULL ISOLATION
372 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
373 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
376 MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
378 NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
379 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
380 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NUMERIC
382 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
383 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNER
385 PARTIAL PASSWORD PLACING POSITION
386 PRECISION PRESERVE PREPARE PREPARED PRIMARY
387 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
391 READ REAL RECHECK REFERENCES REINDEX RELATIVE_P RELEASE RENAME
392 REPEATABLE REPLACE RESET RESTART RESTRICT RETURNS REVOKE RIGHT
393 ROLE ROLLBACK ROW ROWS RULE
395 SAVEPOINT SCHEMA SCROLL SECOND_P SECURITY SELECT SEQUENCE
396 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
397 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE START STATEMENT
398 STATISTICS STDIN STDOUT STORAGE STRICT_P SUBSTRING SUPERUSER_P SYMMETRIC
401 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY THEN TIME TIMESTAMP
402 TO TOAST TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
403 TRUNCATE TRUSTED TYPE_P
405 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
408 VACUUM VALID VALIDATOR VALUES VARCHAR VARYING
409 VERBOSE VIEW VOLATILE
411 WHEN WHERE WITH WITHOUT WORK WRITE
417 /* The grammar thinks these are keywords, but they are not in the keywords.c
418 * list and so can never be entered directly. The filter in parser.c
419 * creates these tokens when required.
423 /* Special token types, not actually keywords - see the "lex" file */
424 %token <str> IDENT FCONST SCONST BCONST XCONST Op
425 %token <ival> ICONST PARAM
427 /* precedence: lowest to highest */
435 %nonassoc LIKE ILIKE SIMILAR
440 %left POSTFIXOP /* dummy for postfix Op rules */
441 %left Op OPERATOR /* multi-character ops and user-defined operators */
444 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
448 /* Unary Operators */
449 %left AT ZONE /* sets precedence for AT TIME ZONE */
456 * These might seem to be low-precedence, but actually they are not part
457 * of the arithmetic hierarchy at all in their use as JOIN operators.
458 * We make them high-precedence to support their use as function names.
459 * They wouldn't be given a precedence at all, were it not that we need
460 * left-associativity among the JOIN rules themselves.
462 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
466 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
467 * psql already handles such cases, but other interfaces don't.
470 stmtblock: stmtmulti { parsetree = $1; }
473 /* the thrashing around here is to discard "empty" statements... */
474 stmtmulti: stmtmulti ';' stmt
476 $$ = lappend($1, $3);
510 | CreateConversionStmt
519 | CreateTableSpaceStmt
574 /*****************************************************************************
576 * Create a new Postgres DBMS role
578 *****************************************************************************/
581 CREATE ROLE RoleId opt_with OptRoleList
583 CreateRoleStmt *n = makeNode(CreateRoleStmt);
584 n->stmt_type = ROLESTMT_ROLE;
597 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
598 * for backwards compatibility). Note: the only option required by SQL99
599 * is "WITH ADMIN name".
602 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
603 | /* EMPTY */ { $$ = NIL; }
609 $$ = makeDefElem("password",
610 (Node *)makeString($2));
612 | ENCRYPTED PASSWORD Sconst
614 $$ = makeDefElem("encryptedPassword",
615 (Node *)makeString($3));
617 | UNENCRYPTED PASSWORD Sconst
619 $$ = makeDefElem("unencryptedPassword",
620 (Node *)makeString($3));
624 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
628 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
632 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
636 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
640 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
644 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
648 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
652 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
656 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
660 /* For backwards compatibility, synonym for SUPERUSER */
661 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
665 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
669 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
673 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
675 | IN_P ROLE name_list
677 $$ = makeDefElem("addroleto", (Node *)$3);
679 | IN_P GROUP_P name_list
681 $$ = makeDefElem("addroleto", (Node *)$3);
685 $$ = makeDefElem("validUntil", (Node *)makeString($3));
689 $$ = makeDefElem("adminmembers", (Node *)$2);
693 $$ = makeDefElem("rolemembers", (Node *)$2);
697 $$ = makeDefElem("rolemembers", (Node *)$2);
702 /*****************************************************************************
704 * Create a new Postgres DBMS user (role with implied login ability)
706 *****************************************************************************/
709 CREATE USER RoleId opt_with OptRoleList
711 CreateRoleStmt *n = makeNode(CreateRoleStmt);
712 n->stmt_type = ROLESTMT_USER;
720 /*****************************************************************************
722 * Alter a postgresql DBMS role
724 *****************************************************************************/
727 ALTER ROLE RoleId opt_with OptRoleList
729 AlterRoleStmt *n = makeNode(AlterRoleStmt);
737 ALTER ROLE RoleId SET set_rest
739 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
741 n->variable = $5->name;
745 | ALTER ROLE RoleId VariableResetStmt
747 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
749 n->variable = ((VariableResetStmt *)$4)->name;
756 /*****************************************************************************
758 * Alter a postgresql DBMS user
760 *****************************************************************************/
763 ALTER USER RoleId opt_with OptRoleList
765 AlterRoleStmt *n = makeNode(AlterRoleStmt);
774 ALTER USER RoleId SET set_rest
776 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
778 n->variable = $5->name;
782 | ALTER USER RoleId VariableResetStmt
784 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
786 n->variable = ((VariableResetStmt *)$4)->name;
793 /*****************************************************************************
795 * Drop a postgresql DBMS role
797 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
798 * might own objects in multiple databases, there is presently no way to
799 * implement either cascading or restricting. Caveat DBA.
800 *****************************************************************************/
805 DropRoleStmt *n = makeNode(DropRoleStmt);
811 /*****************************************************************************
813 * Drop a postgresql DBMS user
815 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
816 * might own objects in multiple databases, there is presently no way to
817 * implement either cascading or restricting. Caveat DBA.
818 *****************************************************************************/
823 DropRoleStmt *n = makeNode(DropRoleStmt);
830 /*****************************************************************************
832 * Create a postgresql group (role without login ability)
834 *****************************************************************************/
837 CREATE GROUP_P RoleId opt_with OptRoleList
839 CreateRoleStmt *n = makeNode(CreateRoleStmt);
840 n->stmt_type = ROLESTMT_GROUP;
848 /*****************************************************************************
850 * Alter a postgresql group
852 *****************************************************************************/
855 ALTER GROUP_P RoleId add_drop USER name_list
857 AlterRoleStmt *n = makeNode(AlterRoleStmt);
860 n->options = list_make1(makeDefElem("rolemembers",
866 add_drop: ADD { $$ = +1; }
871 /*****************************************************************************
873 * Drop a postgresql group
875 * XXX see above notes about cascading DROP USER; groups have same problem.
876 *****************************************************************************/
879 DROP GROUP_P name_list
881 DropRoleStmt *n = makeNode(DropRoleStmt);
888 /*****************************************************************************
890 * Manipulate a schema
892 *****************************************************************************/
895 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
897 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
898 /* One can omit the schema name or the authorization id. */
907 | CREATE SCHEMA ColId OptSchemaEltList
909 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
910 /* ...but not both */
920 | /* EMPTY */ { $$ = NULL; }
924 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
925 | /* EMPTY */ { $$ = NIL; }
929 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
930 * statement (in addition to by themselves).
942 /*****************************************************************************
944 * Set PG internal variable
945 * SET name TO 'var_value'
946 * Include SQL92 syntax (thomas 1997-10-22):
947 * SET TIME ZONE 'var_value'
949 *****************************************************************************/
954 VariableSetStmt *n = $2;
960 VariableSetStmt *n = $3;
964 | SET SESSION set_rest
966 VariableSetStmt *n = $3;
972 set_rest: var_name TO var_list_or_default
974 VariableSetStmt *n = makeNode(VariableSetStmt);
979 | var_name '=' var_list_or_default
981 VariableSetStmt *n = makeNode(VariableSetStmt);
986 | TIME ZONE zone_value
988 VariableSetStmt *n = makeNode(VariableSetStmt);
989 n->name = "timezone";
991 n->args = list_make1($3);
994 | TRANSACTION transaction_mode_list
996 VariableSetStmt *n = makeNode(VariableSetStmt);
997 n->name = "TRANSACTION";
1001 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1003 VariableSetStmt *n = makeNode(VariableSetStmt);
1004 n->name = "SESSION CHARACTERISTICS";
1008 | NAMES opt_encoding
1010 VariableSetStmt *n = makeNode(VariableSetStmt);
1011 n->name = "client_encoding";
1013 n->args = list_make1(makeStringConst($2, NULL));
1016 | ROLE ColId_or_Sconst
1018 VariableSetStmt *n = makeNode(VariableSetStmt);
1020 n->args = list_make1(makeStringConst($2, NULL));
1023 | SESSION AUTHORIZATION ColId_or_Sconst
1025 VariableSetStmt *n = makeNode(VariableSetStmt);
1026 n->name = "session_authorization";
1027 n->args = list_make1(makeStringConst($3, NULL));
1030 | SESSION AUTHORIZATION DEFAULT
1032 VariableSetStmt *n = makeNode(VariableSetStmt);
1033 n->name = "session_authorization";
1041 | var_name '.' ColId
1043 int qLen = strlen($1);
1044 char* qualName = palloc(qLen + strlen($3) + 2);
1045 strcpy(qualName, $1);
1046 qualName[qLen] = '.';
1047 strcpy(qualName + qLen + 1, $3);
1052 var_list_or_default:
1053 var_list { $$ = $1; }
1054 | DEFAULT { $$ = NIL; }
1057 var_list: var_value { $$ = list_make1($1); }
1058 | var_list ',' var_value { $$ = lappend($1, $3); }
1061 var_value: opt_boolean
1062 { $$ = makeStringConst($1, NULL); }
1064 { $$ = makeStringConst($1, NULL); }
1066 { $$ = makeAConst($1); }
1069 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1070 | READ COMMITTED { $$ = "read committed"; }
1071 | REPEATABLE READ { $$ = "repeatable read"; }
1072 | SERIALIZABLE { $$ = "serializable"; }
1076 TRUE_P { $$ = "true"; }
1077 | FALSE_P { $$ = "false"; }
1079 | OFF { $$ = "off"; }
1082 /* Timezone values can be:
1083 * - a string such as 'pst8pdt'
1084 * - an identifier such as "pst8pdt"
1085 * - an integer or floating point number
1086 * - a time interval per SQL99
1087 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1088 * so use IDENT and reject anything which is a reserved word.
1093 $$ = makeStringConst($1, NULL);
1097 $$ = makeStringConst($1, NULL);
1099 | ConstInterval Sconst opt_interval
1101 A_Const *n = (A_Const *) makeStringConst($2, $1);
1102 if ($3 != INTERVAL_FULL_RANGE)
1104 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1106 (errcode(ERRCODE_SYNTAX_ERROR),
1107 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1108 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
1112 | ConstInterval '(' Iconst ')' Sconst opt_interval
1114 A_Const *n = (A_Const *) makeStringConst($5, $1);
1117 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1118 errmsg("INTERVAL(%d) precision must not be negative",
1120 if ($3 > MAX_INTERVAL_PRECISION)
1123 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1124 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
1125 $3, MAX_INTERVAL_PRECISION)));
1126 $3 = MAX_INTERVAL_PRECISION;
1129 if (($6 != INTERVAL_FULL_RANGE)
1130 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
1132 (errcode(ERRCODE_SYNTAX_ERROR),
1133 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1135 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
1139 | NumericOnly { $$ = makeAConst($1); }
1140 | DEFAULT { $$ = NULL; }
1141 | LOCAL { $$ = NULL; }
1146 | DEFAULT { $$ = NULL; }
1147 | /*EMPTY*/ { $$ = NULL; }
1152 | SCONST { $$ = $1; }
1159 VariableShowStmt *n = makeNode(VariableShowStmt);
1165 VariableShowStmt *n = makeNode(VariableShowStmt);
1166 n->name = "timezone";
1169 | SHOW TRANSACTION ISOLATION LEVEL
1171 VariableShowStmt *n = makeNode(VariableShowStmt);
1172 n->name = "transaction_isolation";
1175 | SHOW SESSION AUTHORIZATION
1177 VariableShowStmt *n = makeNode(VariableShowStmt);
1178 n->name = "session_authorization";
1183 VariableShowStmt *n = makeNode(VariableShowStmt);
1192 VariableResetStmt *n = makeNode(VariableResetStmt);
1198 VariableResetStmt *n = makeNode(VariableResetStmt);
1199 n->name = "timezone";
1202 | RESET TRANSACTION ISOLATION LEVEL
1204 VariableResetStmt *n = makeNode(VariableResetStmt);
1205 n->name = "transaction_isolation";
1208 | RESET SESSION AUTHORIZATION
1210 VariableResetStmt *n = makeNode(VariableResetStmt);
1211 n->name = "session_authorization";
1216 VariableResetStmt *n = makeNode(VariableResetStmt);
1224 SET CONSTRAINTS constraints_set_list constraints_set_mode
1226 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1227 n->constraints = $3;
1233 constraints_set_list:
1235 | name_list { $$ = $1; }
1238 constraints_set_mode:
1239 DEFERRED { $$ = TRUE; }
1240 | IMMEDIATE { $$ = FALSE; }
1245 * Checkpoint statement
1250 CheckPointStmt *n = makeNode(CheckPointStmt);
1256 /*****************************************************************************
1258 * ALTER [ TABLE | INDEX ] variations
1260 *****************************************************************************/
1263 ALTER TABLE relation_expr alter_table_cmds
1265 AlterTableStmt *n = makeNode(AlterTableStmt);
1268 n->relkind = OBJECT_TABLE;
1271 | ALTER INDEX relation_expr alter_rel_cmds
1273 AlterTableStmt *n = makeNode(AlterTableStmt);
1276 n->relkind = OBJECT_INDEX;
1282 alter_table_cmd { $$ = list_make1($1); }
1283 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1286 /* Subcommands that are for ALTER TABLE only */
1288 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1289 ADD opt_column columnDef
1291 AlterTableCmd *n = makeNode(AlterTableCmd);
1292 n->subtype = AT_AddColumn;
1296 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1297 | ALTER opt_column ColId alter_column_default
1299 AlterTableCmd *n = makeNode(AlterTableCmd);
1300 n->subtype = AT_ColumnDefault;
1305 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1306 | ALTER opt_column ColId DROP NOT NULL_P
1308 AlterTableCmd *n = makeNode(AlterTableCmd);
1309 n->subtype = AT_DropNotNull;
1313 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1314 | ALTER opt_column ColId SET NOT NULL_P
1316 AlterTableCmd *n = makeNode(AlterTableCmd);
1317 n->subtype = AT_SetNotNull;
1321 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1322 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1324 AlterTableCmd *n = makeNode(AlterTableCmd);
1325 n->subtype = AT_SetStatistics;
1327 n->def = (Node *) $6;
1330 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1331 | ALTER opt_column ColId SET STORAGE ColId
1333 AlterTableCmd *n = makeNode(AlterTableCmd);
1334 n->subtype = AT_SetStorage;
1336 n->def = (Node *) makeString($6);
1339 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1340 | DROP opt_column ColId opt_drop_behavior
1342 AlterTableCmd *n = makeNode(AlterTableCmd);
1343 n->subtype = AT_DropColumn;
1349 * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
1350 * [ USING <expression> ]
1352 | ALTER opt_column ColId TYPE_P Typename alter_using
1354 AlterTableCmd *n = makeNode(AlterTableCmd);
1355 n->subtype = AT_AlterColumnType;
1357 n->def = (Node *) $5;
1361 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1362 | ADD TableConstraint
1364 AlterTableCmd *n = makeNode(AlterTableCmd);
1365 n->subtype = AT_AddConstraint;
1369 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1370 | DROP CONSTRAINT name opt_drop_behavior
1372 AlterTableCmd *n = makeNode(AlterTableCmd);
1373 n->subtype = AT_DropConstraint;
1378 /* ALTER TABLE <relation> SET WITHOUT OIDS */
1381 AlterTableCmd *n = makeNode(AlterTableCmd);
1382 n->subtype = AT_DropOids;
1385 /* ALTER TABLE <name> CREATE TOAST TABLE -- ONLY */
1386 | CREATE TOAST TABLE
1388 AlterTableCmd *n = makeNode(AlterTableCmd);
1389 n->subtype = AT_ToastTable;
1392 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1395 AlterTableCmd *n = makeNode(AlterTableCmd);
1396 n->subtype = AT_ClusterOn;
1400 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1401 | SET WITHOUT CLUSTER
1403 AlterTableCmd *n = makeNode(AlterTableCmd);
1404 n->subtype = AT_DropCluster;
1415 alter_rel_cmd { $$ = list_make1($1); }
1416 | alter_rel_cmds ',' alter_rel_cmd { $$ = lappend($1, $3); }
1419 /* Subcommands that are for ALTER TABLE or ALTER INDEX */
1421 /* ALTER [TABLE|INDEX] <name> OWNER TO RoleId */
1424 AlterTableCmd *n = makeNode(AlterTableCmd);
1425 n->subtype = AT_ChangeOwner;
1429 /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
1430 | SET TABLESPACE name
1432 AlterTableCmd *n = makeNode(AlterTableCmd);
1433 n->subtype = AT_SetTableSpace;
1439 alter_column_default:
1442 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1443 if (exprIsNullConstant($3))
1448 | DROP DEFAULT { $$ = NULL; }
1452 CASCADE { $$ = DROP_CASCADE; }
1453 | RESTRICT { $$ = DROP_RESTRICT; }
1454 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1458 USING a_expr { $$ = $2; }
1459 | /* EMPTY */ { $$ = NULL; }
1464 /*****************************************************************************
1467 * close <portalname>
1469 *****************************************************************************/
1474 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1481 /*****************************************************************************
1484 * COPY <relname> ['(' columnList ')'] FROM/TO [WITH options]
1486 * BINARY, OIDS, and DELIMITERS kept in old locations
1487 * for backward compatibility. 2002-06-18
1489 *****************************************************************************/
1491 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1492 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1494 CopyStmt *n = makeNode(CopyStmt);
1501 /* Concatenate user-supplied flags */
1503 n->options = lappend(n->options, $2);
1505 n->options = lappend(n->options, $5);
1507 n->options = lappend(n->options, $8);
1509 n->options = list_concat(n->options, $10);
1516 | TO { $$ = FALSE; }
1520 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1521 * used depends on the direction. (It really doesn't make sense to copy from
1522 * stdout. We silently correct the "typo". - AY 9/94
1526 | STDIN { $$ = NULL; }
1527 | STDOUT { $$ = NULL; }
1533 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1534 | /* EMPTY */ { $$ = NIL; }
1541 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1545 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1547 | DELIMITER opt_as Sconst
1549 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1551 | NULL_P opt_as Sconst
1553 $$ = makeDefElem("null", (Node *)makeString($3));
1557 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1561 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1563 | QUOTE opt_as Sconst
1565 $$ = makeDefElem("quote", (Node *)makeString($3));
1567 | ESCAPE opt_as Sconst
1569 $$ = makeDefElem("escape", (Node *)makeString($3));
1571 | FORCE QUOTE columnList
1573 $$ = makeDefElem("force_quote", (Node *)$3);
1575 | FORCE NOT NULL_P columnList
1577 $$ = makeDefElem("force_notnull", (Node *)$4);
1581 /* The following exist for backward compatibility */
1586 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1588 | /*EMPTY*/ { $$ = NULL; }
1594 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1596 | /*EMPTY*/ { $$ = NULL; }
1600 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1601 opt_using DELIMITERS Sconst
1603 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1605 | /*EMPTY*/ { $$ = NULL; }
1614 /*****************************************************************************
1617 * CREATE TABLE relname
1619 *****************************************************************************/
1621 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1622 OptInherit OptWithOids OnCommitOption OptTableSpace
1624 CreateStmt *n = makeNode(CreateStmt);
1628 n->inhRelations = $8;
1629 n->constraints = NIL;
1632 n->tablespacename = $11;
1635 | CREATE OptTemp TABLE qualified_name OF qualified_name
1636 '(' OptTableElementList ')' OptWithOids OnCommitOption OptTableSpace
1638 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1639 * by our inheritance capabilities. Let's try it...
1641 CreateStmt *n = makeNode(CreateStmt);
1645 n->inhRelations = list_make1($6);
1646 n->constraints = NIL;
1649 n->tablespacename = $12;
1655 * Redundancy here is needed to avoid shift/reduce conflicts,
1656 * since TEMP is not a reserved word. See also OptTempTableName.
1658 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1659 * the LOCAL keyword is really meaningless.
1661 OptTemp: TEMPORARY { $$ = TRUE; }
1662 | TEMP { $$ = TRUE; }
1663 | LOCAL TEMPORARY { $$ = TRUE; }
1664 | LOCAL TEMP { $$ = TRUE; }
1665 | GLOBAL TEMPORARY { $$ = TRUE; }
1666 | GLOBAL TEMP { $$ = TRUE; }
1667 | /*EMPTY*/ { $$ = FALSE; }
1670 OptTableElementList:
1671 TableElementList { $$ = $1; }
1672 | /*EMPTY*/ { $$ = NIL; }
1678 $$ = list_make1($1);
1680 | TableElementList ',' TableElement
1682 $$ = lappend($1, $3);
1687 columnDef { $$ = $1; }
1688 | TableLikeClause { $$ = $1; }
1689 | TableConstraint { $$ = $1; }
1692 columnDef: ColId Typename ColQualList
1694 ColumnDef *n = makeNode(ColumnDef);
1697 n->constraints = $3;
1704 ColQualList ColConstraint { $$ = lappend($1, $2); }
1705 | /*EMPTY*/ { $$ = NIL; }
1709 CONSTRAINT name ColConstraintElem
1711 switch (nodeTag($3))
1715 Constraint *n = (Constraint *)$3;
1719 case T_FkConstraint:
1721 FkConstraint *n = (FkConstraint *)$3;
1722 n->constr_name = $2;
1730 | ColConstraintElem { $$ = $1; }
1731 | ConstraintAttr { $$ = $1; }
1734 /* DEFAULT NULL is already the default for Postgres.
1735 * But define it here and carry it forward into the system
1736 * to make it explicit.
1737 * - thomas 1998-09-13
1739 * WITH NULL and NULL are not SQL92-standard syntax elements,
1740 * so leave them out. Use DEFAULT NULL to explicitly indicate
1741 * that a column may have that value. WITH NULL leads to
1742 * shift/reduce conflicts with WITH TIME ZONE anyway.
1743 * - thomas 1999-01-08
1745 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1746 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1747 * or be part of a_expr NOT LIKE or similar constructs).
1752 Constraint *n = makeNode(Constraint);
1753 n->contype = CONSTR_NOTNULL;
1756 n->cooked_expr = NULL;
1758 n->indexspace = NULL;
1763 Constraint *n = makeNode(Constraint);
1764 n->contype = CONSTR_NULL;
1767 n->cooked_expr = NULL;
1769 n->indexspace = NULL;
1772 | UNIQUE OptConsTableSpace
1774 Constraint *n = makeNode(Constraint);
1775 n->contype = CONSTR_UNIQUE;
1778 n->cooked_expr = NULL;
1783 | PRIMARY KEY OptConsTableSpace
1785 Constraint *n = makeNode(Constraint);
1786 n->contype = CONSTR_PRIMARY;
1789 n->cooked_expr = NULL;
1794 | CHECK '(' a_expr ')'
1796 Constraint *n = makeNode(Constraint);
1797 n->contype = CONSTR_CHECK;
1800 n->cooked_expr = NULL;
1802 n->indexspace = NULL;
1807 Constraint *n = makeNode(Constraint);
1808 n->contype = CONSTR_DEFAULT;
1810 if (exprIsNullConstant($2))
1812 /* DEFAULT NULL should be reported as empty expr */
1819 n->cooked_expr = NULL;
1821 n->indexspace = NULL;
1824 | REFERENCES qualified_name opt_column_list key_match key_actions
1826 FkConstraint *n = makeNode(FkConstraint);
1827 n->constr_name = NULL;
1831 n->fk_matchtype = $4;
1832 n->fk_upd_action = (char) ($5 >> 8);
1833 n->fk_del_action = (char) ($5 & 0xFF);
1834 n->deferrable = FALSE;
1835 n->initdeferred = FALSE;
1841 * ConstraintAttr represents constraint attributes, which we parse as if
1842 * they were independent constraint clauses, in order to avoid shift/reduce
1843 * conflicts (since NOT might start either an independent NOT NULL clause
1844 * or an attribute). analyze.c is responsible for attaching the attribute
1845 * information to the preceding "real" constraint node, and for complaining
1846 * if attribute clauses appear in the wrong place or wrong combinations.
1848 * See also ConstraintAttributeSpec, which can be used in places where
1849 * there is no parsing conflict.
1854 Constraint *n = makeNode(Constraint);
1855 n->contype = CONSTR_ATTR_DEFERRABLE;
1860 Constraint *n = makeNode(Constraint);
1861 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1864 | INITIALLY DEFERRED
1866 Constraint *n = makeNode(Constraint);
1867 n->contype = CONSTR_ATTR_DEFERRED;
1870 | INITIALLY IMMEDIATE
1872 Constraint *n = makeNode(Constraint);
1873 n->contype = CONSTR_ATTR_IMMEDIATE;
1880 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
1881 * This seems to be a poor man's inheritance capability, with the resulting
1882 * tables completely decoupled except for the original commonality in definitions.
1884 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
1885 * which is a part of SQL 200N
1888 LIKE qualified_name like_including_defaults
1890 InhRelation *n = makeNode(InhRelation);
1892 n->including_defaults = $3;
1898 like_including_defaults:
1899 INCLUDING DEFAULTS { $$ = true; }
1900 | EXCLUDING DEFAULTS { $$ = false; }
1901 | /* EMPTY */ { $$ = false; }
1905 /* ConstraintElem specifies constraint syntax which is not embedded into
1906 * a column definition. ColConstraintElem specifies the embedded form.
1907 * - thomas 1997-12-03
1910 CONSTRAINT name ConstraintElem
1912 switch (nodeTag($3))
1916 Constraint *n = (Constraint *)$3;
1920 case T_FkConstraint:
1922 FkConstraint *n = (FkConstraint *)$3;
1923 n->constr_name = $2;
1931 | ConstraintElem { $$ = $1; }
1935 CHECK '(' a_expr ')'
1937 Constraint *n = makeNode(Constraint);
1938 n->contype = CONSTR_CHECK;
1941 n->cooked_expr = NULL;
1942 n->indexspace = NULL;
1945 | UNIQUE '(' columnList ')' OptConsTableSpace
1947 Constraint *n = makeNode(Constraint);
1948 n->contype = CONSTR_UNIQUE;
1951 n->cooked_expr = NULL;
1956 | PRIMARY KEY '(' columnList ')' OptConsTableSpace
1958 Constraint *n = makeNode(Constraint);
1959 n->contype = CONSTR_PRIMARY;
1962 n->cooked_expr = NULL;
1967 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
1968 opt_column_list key_match key_actions ConstraintAttributeSpec
1970 FkConstraint *n = makeNode(FkConstraint);
1971 n->constr_name = NULL;
1975 n->fk_matchtype = $9;
1976 n->fk_upd_action = (char) ($10 >> 8);
1977 n->fk_del_action = (char) ($10 & 0xFF);
1978 n->deferrable = ($11 & 1) != 0;
1979 n->initdeferred = ($11 & 2) != 0;
1985 '(' columnList ')' { $$ = $2; }
1986 | /*EMPTY*/ { $$ = NIL; }
1990 columnElem { $$ = list_make1($1); }
1991 | columnList ',' columnElem { $$ = lappend($1, $3); }
1996 $$ = (Node *) makeString($1);
2000 key_match: MATCH FULL
2002 $$ = FKCONSTR_MATCH_FULL;
2007 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2008 errmsg("MATCH PARTIAL not yet implemented")));
2009 $$ = FKCONSTR_MATCH_PARTIAL;
2013 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2017 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2022 * We combine the update and delete actions into one value temporarily
2023 * for simplicity of parsing, and then break them down again in the
2024 * calling production. update is in the left 8 bits, delete in the right.
2025 * Note that NOACTION is the default.
2029 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2031 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2032 | key_update key_delete
2033 { $$ = ($1 << 8) | ($2 & 0xFF); }
2034 | key_delete key_update
2035 { $$ = ($2 << 8) | ($1 & 0xFF); }
2037 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2040 key_update: ON UPDATE key_action { $$ = $3; }
2043 key_delete: ON DELETE_P key_action { $$ = $3; }
2047 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2048 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2049 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2050 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2051 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2054 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2055 | /*EMPTY*/ { $$ = NIL; }
2059 WITH OIDS { $$ = MUST_HAVE_OIDS; }
2060 | WITHOUT OIDS { $$ = MUST_NOT_HAVE_OIDS; }
2061 | /*EMPTY*/ { $$ = DEFAULT_OIDS; }
2064 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2065 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2066 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2067 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2070 OptTableSpace: TABLESPACE name { $$ = $2; }
2071 | /*EMPTY*/ { $$ = NULL; }
2074 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2075 | /*EMPTY*/ { $$ = NULL; }
2080 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2085 CREATE OptTemp TABLE qualified_name OptCreateAs WithOidsAs SelectStmt
2088 * When the SelectStmt is a set-operation tree, we must
2089 * stuff the INTO information into the leftmost component
2090 * Select, because that's where analyze.c will expect
2091 * to find it. Similarly, the output column names must
2092 * be attached to that Select's target list.
2094 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
2095 if (n->into != NULL)
2097 (errcode(ERRCODE_SYNTAX_ERROR),
2098 errmsg("CREATE TABLE AS may not specify INTO")));
2101 n->intoColNames = $5;
2102 n->intoHasOids = $6;
2108 * To avoid a shift/reduce conflict in CreateAsStmt, we need to
2109 * include the 'AS' terminal in the parsing of WITH/WITHOUT
2110 * OIDS. Unfortunately that means this production is effectively a
2111 * duplicate of OptWithOids.
2114 WITH OIDS AS { $$ = MUST_HAVE_OIDS; }
2115 | WITHOUT OIDS AS { $$ = MUST_NOT_HAVE_OIDS; }
2116 | AS { $$ = DEFAULT_OIDS; }
2120 '(' CreateAsList ')' { $$ = $2; }
2121 | /*EMPTY*/ { $$ = NIL; }
2125 CreateAsElement { $$ = list_make1($1); }
2126 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2132 ColumnDef *n = makeNode(ColumnDef);
2137 n->is_not_null = false;
2138 n->raw_default = NULL;
2139 n->cooked_default = NULL;
2140 n->constraints = NIL;
2147 /*****************************************************************************
2150 * CREATE SEQUENCE seqname
2151 * ALTER SEQUENCE seqname
2153 *****************************************************************************/
2156 CREATE OptTemp SEQUENCE qualified_name OptSeqList
2158 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2167 ALTER SEQUENCE qualified_name OptSeqList
2169 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2176 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
2177 | /*EMPTY*/ { $$ = NIL; }
2180 OptSeqElem: CACHE NumericOnly
2182 $$ = makeDefElem("cache", (Node *)$2);
2186 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2190 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2192 | INCREMENT opt_by NumericOnly
2194 $$ = makeDefElem("increment", (Node *)$3);
2196 | MAXVALUE NumericOnly
2198 $$ = makeDefElem("maxvalue", (Node *)$2);
2200 | MINVALUE NumericOnly
2202 $$ = makeDefElem("minvalue", (Node *)$2);
2206 $$ = makeDefElem("maxvalue", NULL);
2210 $$ = makeDefElem("minvalue", NULL);
2212 | START opt_with NumericOnly
2214 $$ = makeDefElem("start", (Node *)$3);
2216 | RESTART opt_with NumericOnly
2218 $$ = makeDefElem("restart", (Node *)$3);
2227 FloatOnly { $$ = $1; }
2228 | IntegerOnly { $$ = $1; }
2231 FloatOnly: FCONST { $$ = makeFloat($1); }
2242 $$ = makeInteger($1);
2246 $$ = makeInteger($2);
2247 $$->val.ival = - $$->val.ival;
2251 /*****************************************************************************
2254 * CREATE PROCEDURAL LANGUAGE ...
2255 * DROP PROCEDURAL LANGUAGE ...
2257 *****************************************************************************/
2260 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2261 HANDLER handler_name opt_validator opt_lancompiler
2263 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2266 n->plvalidator = $8;
2273 TRUSTED { $$ = TRUE; }
2274 | /*EMPTY*/ { $$ = FALSE; }
2277 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2278 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2279 * Work around by using simple names, instead.
2282 name { $$ = list_make1(makeString($1)); }
2283 | name attrs { $$ = lcons(makeString($1), $2); }
2287 LANCOMPILER Sconst { $$ = $2; }
2288 | /*EMPTY*/ { $$ = ""; }
2292 VALIDATOR handler_name { $$ = $2; }
2293 | /*EMPTY*/ { $$ = NULL; }
2297 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2299 DropPLangStmt *n = makeNode(DropPLangStmt);
2311 /*****************************************************************************
2314 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2316 *****************************************************************************/
2318 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2320 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2321 n->tablespacename = $3;
2328 OptTableSpaceOwner: OWNER name { $$ = $2; }
2329 | /*EMPTY */ { $$ = NULL; }
2332 /*****************************************************************************
2335 * DROP TABLESPACE <tablespace>
2337 * No need for drop behaviour as we cannot implement dependencies for
2338 * objects in other databases; we can only support RESTRICT.
2340 ****************************************************************************/
2342 DropTableSpaceStmt: DROP TABLESPACE name
2344 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2345 n->tablespacename = $3;
2350 /*****************************************************************************
2353 * CREATE TRIGGER ...
2356 *****************************************************************************/
2359 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2360 qualified_name TriggerForSpec EXECUTE PROCEDURE
2361 func_name '(' TriggerFuncArgs ')'
2363 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2370 memcpy(n->actions, $5, 4);
2371 n->isconstraint = FALSE;
2372 n->deferrable = FALSE;
2373 n->initdeferred = FALSE;
2374 n->constrrel = NULL;
2377 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2378 qualified_name OptConstrFromTable
2379 ConstraintAttributeSpec
2380 FOR EACH ROW EXECUTE PROCEDURE
2381 func_name '(' TriggerFuncArgs ')'
2383 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2390 memcpy(n->actions, $6, 4);
2391 n->isconstraint = TRUE;
2392 n->deferrable = ($10 & 1) != 0;
2393 n->initdeferred = ($10 & 2) != 0;
2401 BEFORE { $$ = TRUE; }
2402 | AFTER { $$ = FALSE; }
2408 char *e = palloc(4);
2409 e[0] = $1; e[1] = '\0';
2412 | TriggerOneEvent OR TriggerOneEvent
2414 char *e = palloc(4);
2415 e[0] = $1; e[1] = $3; e[2] = '\0';
2418 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2420 char *e = palloc(4);
2421 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2427 INSERT { $$ = 'i'; }
2428 | DELETE_P { $$ = 'd'; }
2429 | UPDATE { $$ = 'u'; }
2433 FOR TriggerForOpt TriggerForType
2440 * If ROW/STATEMENT not specified, default to
2441 * STATEMENT, per SQL
2454 | STATEMENT { $$ = FALSE; }
2458 TriggerFuncArg { $$ = list_make1($1); }
2459 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2460 | /*EMPTY*/ { $$ = NIL; }
2467 snprintf(buf, sizeof(buf), "%d", $1);
2468 $$ = makeString(pstrdup(buf));
2470 | FCONST { $$ = makeString($1); }
2471 | Sconst { $$ = makeString($1); }
2472 | BCONST { $$ = makeString($1); }
2473 | XCONST { $$ = makeString($1); }
2474 | ColId { $$ = makeString($1); }
2478 FROM qualified_name { $$ = $2; }
2479 | /*EMPTY*/ { $$ = NULL; }
2482 ConstraintAttributeSpec:
2483 ConstraintDeferrabilitySpec
2485 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2487 if ($1 == 0 && $2 != 0)
2489 (errcode(ERRCODE_SYNTAX_ERROR),
2490 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2493 | ConstraintTimeSpec
2500 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2502 if ($2 == 0 && $1 != 0)
2504 (errcode(ERRCODE_SYNTAX_ERROR),
2505 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2512 ConstraintDeferrabilitySpec:
2513 NOT DEFERRABLE { $$ = 0; }
2514 | DEFERRABLE { $$ = 1; }
2518 INITIALLY IMMEDIATE { $$ = 0; }
2519 | INITIALLY DEFERRED { $$ = 2; }
2524 DROP TRIGGER name ON qualified_name opt_drop_behavior
2526 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2530 n->removeType = OBJECT_TRIGGER;
2536 /*****************************************************************************
2539 * CREATE ASSERTION ...
2540 * DROP ASSERTION ...
2542 *****************************************************************************/
2545 CREATE ASSERTION name CHECK '(' a_expr ')'
2546 ConstraintAttributeSpec
2548 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2550 n->args = list_make1($6);
2551 n->isconstraint = TRUE;
2552 n->deferrable = ($8 & 1) != 0;
2553 n->initdeferred = ($8 & 2) != 0;
2556 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2557 errmsg("CREATE ASSERTION is not yet implemented")));
2564 DROP ASSERTION name opt_drop_behavior
2566 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2570 n->removeType = OBJECT_TRIGGER; /* XXX */
2572 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2573 errmsg("DROP ASSERTION is not yet implemented")));
2579 /*****************************************************************************
2582 * define (aggregate,operator,type)
2584 *****************************************************************************/
2587 CREATE AGGREGATE func_name definition
2589 DefineStmt *n = makeNode(DefineStmt);
2590 n->kind = OBJECT_AGGREGATE;
2595 | CREATE OPERATOR any_operator definition
2597 DefineStmt *n = makeNode(DefineStmt);
2598 n->kind = OBJECT_OPERATOR;
2603 | CREATE TYPE_P any_name definition
2605 DefineStmt *n = makeNode(DefineStmt);
2606 n->kind = OBJECT_TYPE;
2611 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2613 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2614 RangeVar *r = makeNode(RangeVar);
2616 /* can't use qualified_name, sigh */
2617 switch (list_length($3))
2620 r->catalogname = NULL;
2621 r->schemaname = NULL;
2622 r->relname = strVal(linitial($3));
2625 r->catalogname = NULL;
2626 r->schemaname = strVal(linitial($3));
2627 r->relname = strVal(lsecond($3));
2630 r->catalogname = strVal(linitial($3));
2631 r->schemaname = strVal(lsecond($3));
2632 r->relname = strVal(lthird($3));
2636 (errcode(ERRCODE_SYNTAX_ERROR),
2637 errmsg("improper qualified name (too many dotted names): %s",
2638 NameListToString($3))));
2647 definition: '(' def_list ')' { $$ = $2; }
2650 def_list: def_elem { $$ = list_make1($1); }
2651 | def_list ',' def_elem { $$ = lappend($1, $3); }
2654 def_elem: ColLabel '=' def_arg
2656 $$ = makeDefElem($1, (Node *)$3);
2660 $$ = makeDefElem($1, NULL);
2664 /* Note: any simple identifier will be returned as a type name! */
2665 def_arg: func_type { $$ = (Node *)$1; }
2666 | qual_all_Op { $$ = (Node *)$1; }
2667 | NumericOnly { $$ = (Node *)$1; }
2668 | Sconst { $$ = (Node *)makeString($1); }
2672 /*****************************************************************************
2675 * CREATE OPERATOR CLASS ...
2676 * DROP OPERATOR CLASS ...
2678 *****************************************************************************/
2681 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
2682 USING access_method AS opclass_item_list
2684 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
2685 n->opclassname = $4;
2695 opclass_item { $$ = list_make1($1); }
2696 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
2700 OPERATOR Iconst any_operator opt_recheck
2702 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2703 n->itemtype = OPCLASS_ITEM_OPERATOR;
2710 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
2712 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2713 n->itemtype = OPCLASS_ITEM_OPERATOR;
2720 | FUNCTION Iconst func_name func_args
2722 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2723 n->itemtype = OPCLASS_ITEM_FUNCTION;
2725 n->args = extractArgTypes($4);
2731 CreateOpClassItem *n = makeNode(CreateOpClassItem);
2732 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
2738 opt_default: DEFAULT { $$ = TRUE; }
2739 | /*EMPTY*/ { $$ = FALSE; }
2742 opt_recheck: RECHECK { $$ = TRUE; }
2743 | /*EMPTY*/ { $$ = FALSE; }
2748 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
2750 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
2751 n->opclassname = $4;
2759 /*****************************************************************************
2763 * DROP itemtype itemname [, itemname ...] [ RESTRICT | CASCADE ]
2765 *****************************************************************************/
2767 DropStmt: DROP drop_type any_name_list opt_drop_behavior
2769 DropStmt *n = makeNode(DropStmt);
2777 drop_type: TABLE { $$ = OBJECT_TABLE; }
2778 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
2779 | VIEW { $$ = OBJECT_VIEW; }
2780 | INDEX { $$ = OBJECT_INDEX; }
2781 | TYPE_P { $$ = OBJECT_TYPE; }
2782 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
2783 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
2784 | SCHEMA { $$ = OBJECT_SCHEMA; }
2788 any_name { $$ = list_make1($1); }
2789 | any_name_list ',' any_name { $$ = lappend($1, $3); }
2792 any_name: ColId { $$ = list_make1(makeString($1)); }
2793 | ColId attrs { $$ = lcons(makeString($1), $2); }
2796 attrs: '.' attr_name
2797 { $$ = list_make1(makeString($2)); }
2798 | attrs '.' attr_name
2799 { $$ = lappend($1, makeString($3)); }
2803 /*****************************************************************************
2806 * truncate table relname1, relname2, ...
2808 *****************************************************************************/
2811 TRUNCATE opt_table qualified_name_list
2813 TruncateStmt *n = makeNode(TruncateStmt);
2819 /*****************************************************************************
2821 * The COMMENT ON statement can take different forms based upon the type of
2822 * the object associated with the comment. The form of the statement is:
2824 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
2825 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
2826 * CAST ] <objname> |
2827 * AGGREGATE <aggname> (<aggtype>) |
2828 * FUNCTION <funcname> (arg1, arg2, ...) |
2829 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
2830 * TRIGGER <triggername> ON <relname> |
2831 * RULE <rulename> ON <relname> ]
2834 *****************************************************************************/
2837 COMMENT ON comment_type any_name IS comment_text
2839 CommentStmt *n = makeNode(CommentStmt);
2846 | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')'
2849 CommentStmt *n = makeNode(CommentStmt);
2850 n->objtype = OBJECT_AGGREGATE;
2852 n->objargs = list_make1($6);
2856 | COMMENT ON FUNCTION func_name func_args IS comment_text
2858 CommentStmt *n = makeNode(CommentStmt);
2859 n->objtype = OBJECT_FUNCTION;
2861 n->objargs = extractArgTypes($5);
2865 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
2868 CommentStmt *n = makeNode(CommentStmt);
2869 n->objtype = OBJECT_OPERATOR;
2875 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
2877 CommentStmt *n = makeNode(CommentStmt);
2878 n->objtype = OBJECT_CONSTRAINT;
2879 n->objname = lappend($6, makeString($4));
2884 | COMMENT ON RULE name ON any_name IS comment_text
2886 CommentStmt *n = makeNode(CommentStmt);
2887 n->objtype = OBJECT_RULE;
2888 n->objname = lappend($6, makeString($4));
2893 | COMMENT ON RULE name IS comment_text
2895 /* Obsolete syntax supported for awhile for compatibility */
2896 CommentStmt *n = makeNode(CommentStmt);
2897 n->objtype = OBJECT_RULE;
2898 n->objname = list_make1(makeString($4));
2903 | COMMENT ON TRIGGER name ON any_name IS comment_text
2905 CommentStmt *n = makeNode(CommentStmt);
2906 n->objtype = OBJECT_TRIGGER;
2907 n->objname = lappend($6, makeString($4));
2912 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
2914 CommentStmt *n = makeNode(CommentStmt);
2915 n->objtype = OBJECT_OPCLASS;
2917 n->objargs = list_make1(makeString($7));
2921 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
2923 CommentStmt *n = makeNode(CommentStmt);
2924 n->objtype = OBJECT_LARGEOBJECT;
2925 n->objname = list_make1($5);
2930 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
2932 CommentStmt *n = makeNode(CommentStmt);
2933 n->objtype = OBJECT_CAST;
2934 n->objname = list_make1($5);
2935 n->objargs = list_make1($7);
2939 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
2941 CommentStmt *n = makeNode(CommentStmt);
2942 n->objtype = OBJECT_LANGUAGE;
2951 COLUMN { $$ = OBJECT_COLUMN; }
2952 | DATABASE { $$ = OBJECT_DATABASE; }
2953 | SCHEMA { $$ = OBJECT_SCHEMA; }
2954 | INDEX { $$ = OBJECT_INDEX; }
2955 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
2956 | TABLE { $$ = OBJECT_TABLE; }
2957 | DOMAIN_P { $$ = OBJECT_TYPE; }
2958 | TYPE_P { $$ = OBJECT_TYPE; }
2959 | VIEW { $$ = OBJECT_VIEW; }
2960 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
2965 | NULL_P { $$ = NULL; }
2968 /*****************************************************************************
2973 *****************************************************************************/
2975 FetchStmt: FETCH fetch_direction from_in name
2977 FetchStmt *n = (FetchStmt *) $2;
2984 FetchStmt *n = makeNode(FetchStmt);
2985 n->direction = FETCH_FORWARD;
2991 | MOVE fetch_direction from_in name
2993 FetchStmt *n = (FetchStmt *) $2;
3000 FetchStmt *n = makeNode(FetchStmt);
3001 n->direction = FETCH_FORWARD;
3012 FetchStmt *n = makeNode(FetchStmt);
3013 n->direction = FETCH_FORWARD;
3019 FetchStmt *n = makeNode(FetchStmt);
3020 n->direction = FETCH_FORWARD;
3026 FetchStmt *n = makeNode(FetchStmt);
3027 n->direction = FETCH_BACKWARD;
3033 FetchStmt *n = makeNode(FetchStmt);
3034 n->direction = FETCH_ABSOLUTE;
3040 FetchStmt *n = makeNode(FetchStmt);
3041 n->direction = FETCH_ABSOLUTE;
3045 | ABSOLUTE_P fetch_count
3047 FetchStmt *n = makeNode(FetchStmt);
3048 n->direction = FETCH_ABSOLUTE;
3052 | RELATIVE_P fetch_count
3054 FetchStmt *n = makeNode(FetchStmt);
3055 n->direction = FETCH_RELATIVE;
3061 FetchStmt *n = makeNode(FetchStmt);
3062 n->direction = FETCH_FORWARD;
3068 FetchStmt *n = makeNode(FetchStmt);
3069 n->direction = FETCH_FORWARD;
3070 n->howMany = FETCH_ALL;
3075 FetchStmt *n = makeNode(FetchStmt);
3076 n->direction = FETCH_FORWARD;
3080 | FORWARD fetch_count
3082 FetchStmt *n = makeNode(FetchStmt);
3083 n->direction = FETCH_FORWARD;
3089 FetchStmt *n = makeNode(FetchStmt);
3090 n->direction = FETCH_FORWARD;
3091 n->howMany = FETCH_ALL;
3096 FetchStmt *n = makeNode(FetchStmt);
3097 n->direction = FETCH_BACKWARD;
3101 | BACKWARD fetch_count
3103 FetchStmt *n = makeNode(FetchStmt);
3104 n->direction = FETCH_BACKWARD;
3110 FetchStmt *n = makeNode(FetchStmt);
3111 n->direction = FETCH_BACKWARD;
3112 n->howMany = FETCH_ALL;
3119 | '-' Iconst { $$ = - $2; }
3127 /*****************************************************************************
3129 * GRANT and REVOKE statements
3131 *****************************************************************************/
3133 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3134 opt_grant_grant_option
3136 GrantStmt *n = makeNode(GrantStmt);
3139 n->objtype = ($4)->objtype;
3140 n->objects = ($4)->objs;
3142 n->grant_option = $7;
3148 REVOKE privileges ON privilege_target
3149 FROM grantee_list opt_drop_behavior
3151 GrantStmt *n = makeNode(GrantStmt);
3152 n->is_grant = false;
3153 n->grant_option = false;
3155 n->objtype = ($4)->objtype;
3156 n->objects = ($4)->objs;
3161 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3162 FROM grantee_list opt_drop_behavior
3164 GrantStmt *n = makeNode(GrantStmt);
3165 n->is_grant = false;
3166 n->grant_option = true;
3168 n->objtype = ($7)->objtype;
3169 n->objects = ($7)->objs;
3178 * A privilege list is represented as a list of strings; the validity of
3179 * the privilege names gets checked at execution. This is a bit annoying
3180 * but we have little choice because of the syntactic conflict with lists
3181 * of role names in GRANT/REVOKE. What's more, we have to call out in
3182 * the "privilege" production any reserved keywords that need to be usable
3183 * as privilege names.
3186 /* either ALL [PRIVILEGES] or a list of individual privileges */
3187 privileges: privilege_list
3195 privilege_list: privilege
3196 { $$ = list_make1(makeString($1)); }
3197 | privilege_list ',' privilege
3198 { $$ = lappend($1, makeString($3)); }
3201 privilege: SELECT { $$ = pstrdup($1); }
3202 | REFERENCES { $$ = pstrdup($1); }
3203 | CREATE { $$ = pstrdup($1); }
3204 | ColId { $$ = $1; }
3208 /* Don't bother trying to fold the first two rules into one using
3209 * opt_table. You're going to get conflicts.
3214 PrivTarget *n = makeNode(PrivTarget);
3215 n->objtype = ACL_OBJECT_RELATION;
3219 | TABLE qualified_name_list
3221 PrivTarget *n = makeNode(PrivTarget);
3222 n->objtype = ACL_OBJECT_RELATION;
3226 | FUNCTION function_with_argtypes_list
3228 PrivTarget *n = makeNode(PrivTarget);
3229 n->objtype = ACL_OBJECT_FUNCTION;
3233 | DATABASE name_list
3235 PrivTarget *n = makeNode(PrivTarget);
3236 n->objtype = ACL_OBJECT_DATABASE;
3240 | LANGUAGE name_list
3242 PrivTarget *n = makeNode(PrivTarget);
3243 n->objtype = ACL_OBJECT_LANGUAGE;
3249 PrivTarget *n = makeNode(PrivTarget);
3250 n->objtype = ACL_OBJECT_NAMESPACE;
3254 | TABLESPACE name_list
3256 PrivTarget *n = makeNode(PrivTarget);
3257 n->objtype = ACL_OBJECT_TABLESPACE;
3265 grantee { $$ = list_make1($1); }
3266 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3271 PrivGrantee *n = makeNode(PrivGrantee);
3272 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3273 if (strcmp($1, "public") == 0)
3281 PrivGrantee *n = makeNode(PrivGrantee);
3282 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3283 if (strcmp($2, "public") == 0)
3292 opt_grant_grant_option:
3293 WITH GRANT OPTION { $$ = TRUE; }
3294 | /*EMPTY*/ { $$ = FALSE; }
3297 function_with_argtypes_list:
3298 function_with_argtypes { $$ = list_make1($1); }
3299 | function_with_argtypes_list ',' function_with_argtypes
3300 { $$ = lappend($1, $3); }
3303 function_with_argtypes:
3306 FuncWithArgs *n = makeNode(FuncWithArgs);
3308 n->funcargs = extractArgTypes($2);
3313 /*****************************************************************************
3315 * GRANT and REVOKE ROLE statements
3317 *****************************************************************************/
3320 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
3322 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3324 n->granted_roles = $2;
3325 n->grantee_roles = $4;
3333 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
3335 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3336 n->is_grant = false;
3337 n->admin_opt = false;
3338 n->granted_roles = $2;
3339 n->grantee_roles = $4;
3343 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
3345 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3346 n->is_grant = false;
3347 n->admin_opt = true;
3348 n->granted_roles = $5;
3349 n->grantee_roles = $7;
3355 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
3356 | /*EMPTY*/ { $$ = FALSE; }
3359 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
3360 | /*EMPTY*/ { $$ = NULL; }
3364 /*****************************************************************************
3367 * create index <indexname> on <relname>
3368 * [ using <access> ] "(" ( <col> [ using <opclass> ] )+ ")"
3369 * [ tablespace <tablespacename> ] [ where <predicate> ]
3371 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
3372 * willing to make TABLESPACE a fully reserved word.
3373 *****************************************************************************/
3375 IndexStmt: CREATE index_opt_unique INDEX index_name ON qualified_name
3376 access_method_clause '(' index_params ')' OptTableSpace where_clause
3378 IndexStmt *n = makeNode(IndexStmt);
3382 n->accessMethod = $7;
3383 n->indexParams = $9;
3384 n->tableSpace = $11;
3385 n->whereClause = $12;
3391 UNIQUE { $$ = TRUE; }
3392 | /*EMPTY*/ { $$ = FALSE; }
3395 access_method_clause:
3396 USING access_method { $$ = $2; }
3397 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
3400 index_params: index_elem { $$ = list_make1($1); }
3401 | index_params ',' index_elem { $$ = lappend($1, $3); }
3405 * Index attributes can be either simple column references, or arbitrary
3406 * expressions in parens. For backwards-compatibility reasons, we allow
3407 * an expression that's just a function call to be written without parens.
3409 index_elem: ColId opt_class
3411 $$ = makeNode(IndexElem);
3416 | func_expr opt_class
3418 $$ = makeNode(IndexElem);
3423 | '(' a_expr ')' opt_class
3425 $$ = makeNode(IndexElem);
3432 opt_class: any_name { $$ = $1; }
3433 | USING any_name { $$ = $2; }
3434 | /*EMPTY*/ { $$ = NIL; }
3437 /*****************************************************************************
3440 * create [or replace] function <fname>
3441 * [(<type-1> { , <type-n>})]
3443 * as <filename or code in language as appropriate>
3444 * language <lang> [with parameters]
3446 *****************************************************************************/
3449 CREATE opt_or_replace FUNCTION func_name func_args
3450 RETURNS func_return createfunc_opt_list opt_definition
3452 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3461 | CREATE opt_or_replace FUNCTION func_name func_args
3462 createfunc_opt_list opt_definition
3464 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
3468 n->returnType = NULL;
3476 OR REPLACE { $$ = TRUE; }
3477 | /*EMPTY*/ { $$ = FALSE; }
3480 func_args: '(' func_args_list ')' { $$ = $2; }
3481 | '(' ')' { $$ = NIL; }
3485 func_arg { $$ = list_make1($1); }
3486 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
3490 * The style with arg_class first is SQL99 standard, but Oracle puts
3491 * param_name first; accept both since it's likely people will try both
3492 * anyway. Don't bother trying to save productions by letting arg_class
3493 * have an empty alternative ... you'll get shift/reduce conflicts.
3495 * We can catch over-specified arguments here if we want to,
3496 * but for now better to silently swallow typmod, etc.
3497 * - thomas 2000-03-22
3500 arg_class param_name func_type
3502 FunctionParameter *n = makeNode(FunctionParameter);
3508 | param_name arg_class func_type
3510 FunctionParameter *n = makeNode(FunctionParameter);
3516 | param_name func_type
3518 FunctionParameter *n = makeNode(FunctionParameter);
3521 n->mode = FUNC_PARAM_IN;
3524 | arg_class func_type
3526 FunctionParameter *n = makeNode(FunctionParameter);
3534 FunctionParameter *n = makeNode(FunctionParameter);
3537 n->mode = FUNC_PARAM_IN;
3542 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
3543 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
3544 | OUT_P { $$ = FUNC_PARAM_OUT; }
3545 | INOUT { $$ = FUNC_PARAM_INOUT; }
3546 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
3550 * Ideally param_name should be ColId, but that causes too many conflicts.
3552 param_name: function_name
3558 /* We can catch over-specified results here if we want to,
3559 * but for now better to silently swallow typmod, etc.
3560 * - thomas 2000-03-22
3567 * We would like to make the second production here be ColId attrs etc,
3568 * but that causes reduce/reduce conflicts. type_name is next best choice.
3570 func_type: Typename { $$ = $1; }
3571 | type_name attrs '%' TYPE_P
3573 $$ = makeNode(TypeName);
3574 $$->names = lcons(makeString($1), $2);
3575 $$->pct_type = true;
3581 createfunc_opt_list:
3582 /* Must be at least one to prevent conflict */
3583 createfunc_opt_item { $$ = list_make1($1); }
3584 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
3588 * Options common to both CREATE FUNCTION and ALTER FUNCTION
3590 common_func_opt_item:
3591 CALLED ON NULL_P INPUT_P
3593 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
3595 | RETURNS NULL_P ON NULL_P INPUT_P
3597 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3601 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
3605 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
3609 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
3613 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
3615 | EXTERNAL SECURITY DEFINER
3617 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3619 | EXTERNAL SECURITY INVOKER
3621 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3625 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
3629 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
3633 createfunc_opt_item:
3636 $$ = makeDefElem("as", (Node *)$2);
3638 | LANGUAGE ColId_or_Sconst
3640 $$ = makeDefElem("language", (Node *)makeString($2));
3642 | common_func_opt_item
3648 func_as: Sconst { $$ = list_make1(makeString($1)); }
3651 $$ = list_make2(makeString($1), makeString($3));
3656 WITH definition { $$ = $2; }
3657 | /*EMPTY*/ { $$ = NIL; }
3660 /*****************************************************************************
3663 * RENAME and OWNER subcommands are already provided by the generic
3664 * ALTER infrastructure, here we just specify alterations that can
3665 * only be applied to functions.
3667 *****************************************************************************/
3669 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
3671 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
3679 /* At least one option must be specified */
3680 common_func_opt_item { $$ = list_make1($1); }
3681 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
3684 /* Ignored, merely for SQL compliance */
3691 /*****************************************************************************
3695 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
3696 * DROP AGGREGATE aggname (aggtype) [ RESTRICT | CASCADE ]
3697 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
3699 *****************************************************************************/
3702 DROP FUNCTION func_name func_args opt_drop_behavior
3704 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
3706 n->args = extractArgTypes($4);
3713 DROP AGGREGATE func_name '(' aggr_argtype ')' opt_drop_behavior
3715 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
3724 Typename { $$ = $1; }
3725 | '*' { $$ = NULL; }
3729 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
3731 RemoveOperStmt *n = makeNode(RemoveOperStmt);
3743 (errcode(ERRCODE_SYNTAX_ERROR),
3744 errmsg("missing argument"),
3745 errhint("Use NONE to denote the missing argument of a unary operator.")));
3747 | Typename ',' Typename
3748 { $$ = list_make2($1, $3); }
3749 | NONE ',' Typename /* left unary */
3750 { $$ = list_make2(NULL, $3); }
3751 | Typename ',' NONE /* right unary */
3752 { $$ = list_make2($1, NULL); }
3757 { $$ = list_make1(makeString($1)); }
3758 | ColId '.' any_operator
3759 { $$ = lcons(makeString($1), $3); }
3763 /*****************************************************************************
3765 * CREATE CAST / DROP CAST
3767 *****************************************************************************/
3769 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
3770 WITH FUNCTION function_with_argtypes cast_context
3772 CreateCastStmt *n = makeNode(CreateCastStmt);
3776 n->context = (CoercionContext) $11;
3779 | CREATE CAST '(' Typename AS Typename ')'
3780 WITHOUT FUNCTION cast_context
3782 CreateCastStmt *n = makeNode(CreateCastStmt);
3786 n->context = (CoercionContext) $10;
3791 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
3792 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
3793 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
3797 DropCastStmt: DROP CAST '(' Typename AS Typename ')' opt_drop_behavior
3799 DropCastStmt *n = makeNode(DropCastStmt);
3809 /*****************************************************************************
3813 * REINDEX type <name> [FORCE]
3815 * FORCE no longer does anything, but we accept it for backwards compatibility
3816 *****************************************************************************/
3819 REINDEX reindex_type qualified_name opt_force
3821 ReindexStmt *n = makeNode(ReindexStmt);
3827 | REINDEX SYSTEM_P name opt_force
3829 ReindexStmt *n = makeNode(ReindexStmt);
3830 n->kind = OBJECT_DATABASE;
3833 n->do_system = true;
3837 | REINDEX DATABASE name opt_force
3839 ReindexStmt *n = makeNode(ReindexStmt);
3840 n->kind = OBJECT_DATABASE;
3843 n->do_system = true;
3850 INDEX { $$ = OBJECT_INDEX; }
3851 | TABLE { $$ = OBJECT_TABLE; }
3854 opt_force: FORCE { $$ = TRUE; }
3855 | /* EMPTY */ { $$ = FALSE; }
3859 /*****************************************************************************
3861 * ALTER THING name RENAME TO newname
3863 *****************************************************************************/
3865 RenameStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' RENAME TO name
3867 RenameStmt *n = makeNode(RenameStmt);
3868 n->renameType = OBJECT_AGGREGATE;
3870 n->objarg = list_make1($5);
3874 | ALTER CONVERSION_P any_name RENAME TO name
3876 RenameStmt *n = makeNode(RenameStmt);
3877 n->renameType = OBJECT_CONVERSION;
3882 | ALTER DATABASE database_name RENAME TO database_name
3884 RenameStmt *n = makeNode(RenameStmt);
3885 n->renameType = OBJECT_DATABASE;
3890 | ALTER FUNCTION func_name func_args RENAME TO name
3892 RenameStmt *n = makeNode(RenameStmt);
3893 n->renameType = OBJECT_FUNCTION;
3895 n->objarg = extractArgTypes($4);
3899 | ALTER GROUP_P RoleId RENAME TO RoleId
3901 RenameStmt *n = makeNode(RenameStmt);
3902 n->renameType = OBJECT_ROLE;
3907 | ALTER LANGUAGE name RENAME TO name
3909 RenameStmt *n = makeNode(RenameStmt);
3910 n->renameType = OBJECT_LANGUAGE;
3915 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
3917 RenameStmt *n = makeNode(RenameStmt);
3918 n->renameType = OBJECT_OPCLASS;
3924 | ALTER SCHEMA name RENAME TO name
3926 RenameStmt *n = makeNode(RenameStmt);
3927 n->renameType = OBJECT_SCHEMA;
3932 | ALTER TABLE relation_expr RENAME TO name
3934 RenameStmt *n = makeNode(RenameStmt);
3935 n->renameType = OBJECT_TABLE;
3941 | ALTER INDEX relation_expr RENAME TO name
3943 RenameStmt *n = makeNode(RenameStmt);
3944 n->renameType = OBJECT_INDEX;
3950 | ALTER TABLE relation_expr RENAME opt_column name TO name
3952 RenameStmt *n = makeNode(RenameStmt);
3953 n->renameType = OBJECT_COLUMN;
3959 | ALTER TRIGGER name ON relation_expr RENAME TO name
3961 RenameStmt *n = makeNode(RenameStmt);
3965 n->renameType = OBJECT_TRIGGER;
3968 | ALTER ROLE RoleId RENAME TO RoleId
3970 RenameStmt *n = makeNode(RenameStmt);
3971 n->renameType = OBJECT_ROLE;
3976 | ALTER USER RoleId RENAME TO RoleId
3978 RenameStmt *n = makeNode(RenameStmt);
3979 n->renameType = OBJECT_ROLE;
3984 | ALTER TABLESPACE name RENAME TO name
3986 RenameStmt *n = makeNode(RenameStmt);
3987 n->renameType = OBJECT_TABLESPACE;
3994 opt_column: COLUMN { $$ = COLUMN; }
3995 | /*EMPTY*/ { $$ = 0; }
3999 /*****************************************************************************
4001 * ALTER THING name OWNER TO newname.
4003 *****************************************************************************/
4005 AlterOwnerStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' OWNER TO RoleId
4007 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4008 n->objectType = OBJECT_AGGREGATE;
4010 n->objarg = list_make1($5);
4014 | ALTER CONVERSION_P any_name OWNER TO RoleId
4016 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4017 n->objectType = OBJECT_CONVERSION;
4022 | ALTER DATABASE database_name OWNER TO RoleId
4024 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4025 n->objectType = OBJECT_DATABASE;
4026 n->object = list_make1($3);
4030 | ALTER DOMAIN_P any_name OWNER TO RoleId
4032 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4033 n->objectType = OBJECT_DOMAIN;
4038 | ALTER FUNCTION func_name func_args OWNER TO RoleId
4040 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4041 n->objectType = OBJECT_FUNCTION;
4043 n->objarg = extractArgTypes($4);
4047 | ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO RoleId
4049 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4050 n->objectType = OBJECT_OPERATOR;
4056 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
4058 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4059 n->objectType = OBJECT_OPCLASS;
4065 | ALTER SCHEMA name OWNER TO RoleId
4067 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4068 n->objectType = OBJECT_SCHEMA;
4069 n->object = list_make1($3);
4073 | ALTER TYPE_P any_name OWNER TO RoleId
4075 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4076 n->objectType = OBJECT_TYPE;
4081 | ALTER TABLESPACE name OWNER TO RoleId
4083 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4084 n->objectType = OBJECT_TABLESPACE;
4085 n->object = list_make1($3);
4092 /*****************************************************************************
4094 * QUERY: Define Rewrite Rule
4096 *****************************************************************************/
4098 RuleStmt: CREATE opt_or_replace RULE name AS
4099 { QueryIsRule=TRUE; }
4100 ON event TO qualified_name where_clause
4101 DO opt_instead RuleActionList
4103 RuleStmt *n = makeNode(RuleStmt);
4107 n->whereClause = $11;
4117 NOTHING { $$ = NIL; }
4118 | RuleActionStmt { $$ = list_make1($1); }
4119 | '(' RuleActionMulti ')' { $$ = $2; }
4122 /* the thrashing around here is to discard "empty" statements... */
4124 RuleActionMulti ';' RuleActionStmtOrEmpty
4126 $$ = lappend($1, $3);
4130 | RuleActionStmtOrEmpty
4132 $$ = list_make1($1);
4146 RuleActionStmtOrEmpty:
4147 RuleActionStmt { $$ = $1; }
4148 | /*EMPTY*/ { $$ = NULL; }
4151 /* change me to select, update, etc. some day */
4152 event: SELECT { $$ = CMD_SELECT; }
4153 | UPDATE { $$ = CMD_UPDATE; }
4154 | DELETE_P { $$ = CMD_DELETE; }
4155 | INSERT { $$ = CMD_INSERT; }
4159 INSTEAD { $$ = TRUE; }
4160 | ALSO { $$ = FALSE; }
4161 | /*EMPTY*/ { $$ = FALSE; }
4166 DROP RULE name ON qualified_name opt_drop_behavior
4168 DropPropertyStmt *n = makeNode(DropPropertyStmt);
4172 n->removeType = OBJECT_RULE;
4178 /*****************************************************************************
4181 * NOTIFY <qualified_name> can appear both in rule bodies and
4182 * as a query-level command
4184 *****************************************************************************/
4186 NotifyStmt: NOTIFY qualified_name
4188 NotifyStmt *n = makeNode(NotifyStmt);
4194 ListenStmt: LISTEN qualified_name
4196 ListenStmt *n = makeNode(ListenStmt);
4203 UNLISTEN qualified_name
4205 UnlistenStmt *n = makeNode(UnlistenStmt);
4211 UnlistenStmt *n = makeNode(UnlistenStmt);
4212 n->relation = makeNode(RangeVar);
4213 n->relation->relname = "*";
4214 n->relation->schemaname = NULL;
4220 /*****************************************************************************
4224 * BEGIN / COMMIT / ROLLBACK
4225 * (also older versions END / ABORT)
4227 *****************************************************************************/
4230 ABORT_P opt_transaction
4232 TransactionStmt *n = makeNode(TransactionStmt);
4233 n->kind = TRANS_STMT_ROLLBACK;
4237 | BEGIN_P opt_transaction transaction_mode_list_or_empty
4239 TransactionStmt *n = makeNode(TransactionStmt);
4240 n->kind = TRANS_STMT_BEGIN;
4244 | START TRANSACTION transaction_mode_list_or_empty
4246 TransactionStmt *n = makeNode(TransactionStmt);
4247 n->kind = TRANS_STMT_START;
4251 | COMMIT opt_transaction
4253 TransactionStmt *n = makeNode(TransactionStmt);
4254 n->kind = TRANS_STMT_COMMIT;
4258 | END_P opt_transaction
4260 TransactionStmt *n = makeNode(TransactionStmt);
4261 n->kind = TRANS_STMT_COMMIT;
4265 | ROLLBACK opt_transaction
4267 TransactionStmt *n = makeNode(TransactionStmt);
4268 n->kind = TRANS_STMT_ROLLBACK;
4274 TransactionStmt *n = makeNode(TransactionStmt);
4275 n->kind = TRANS_STMT_SAVEPOINT;
4276 n->options = list_make1(makeDefElem("savepoint_name",
4277 (Node *)makeString($2)));
4280 | RELEASE SAVEPOINT ColId
4282 TransactionStmt *n = makeNode(TransactionStmt);
4283 n->kind = TRANS_STMT_RELEASE;
4284 n->options = list_make1(makeDefElem("savepoint_name",
4285 (Node *)makeString($3)));
4290 TransactionStmt *n = makeNode(TransactionStmt);
4291 n->kind = TRANS_STMT_RELEASE;
4292 n->options = list_make1(makeDefElem("savepoint_name",
4293 (Node *)makeString($2)));
4296 | ROLLBACK opt_transaction TO SAVEPOINT ColId
4298 TransactionStmt *n = makeNode(TransactionStmt);
4299 n->kind = TRANS_STMT_ROLLBACK_TO;
4300 n->options = list_make1(makeDefElem("savepoint_name",
4301 (Node *)makeString($5)));
4304 | ROLLBACK opt_transaction TO ColId
4306 TransactionStmt *n = makeNode(TransactionStmt);
4307 n->kind = TRANS_STMT_ROLLBACK_TO;
4308 n->options = list_make1(makeDefElem("savepoint_name",
4309 (Node *)makeString($4)));
4312 | PREPARE TRANSACTION Sconst
4314 TransactionStmt *n = makeNode(TransactionStmt);
4315 n->kind = TRANS_STMT_PREPARE;
4319 | COMMIT PREPARED Sconst
4321 TransactionStmt *n = makeNode(TransactionStmt);
4322 n->kind = TRANS_STMT_COMMIT_PREPARED;
4326 | ROLLBACK PREPARED Sconst
4328 TransactionStmt *n = makeNode(TransactionStmt);
4329 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
4335 opt_transaction: WORK {}
4340 transaction_mode_item:
4341 ISOLATION LEVEL iso_level
4342 { $$ = makeDefElem("transaction_isolation",
4343 makeStringConst($3, NULL)); }
4345 { $$ = makeDefElem("transaction_read_only",
4346 makeIntConst(TRUE)); }
4348 { $$ = makeDefElem("transaction_read_only",
4349 makeIntConst(FALSE)); }
4352 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
4353 transaction_mode_list:
4354 transaction_mode_item
4355 { $$ = list_make1($1); }
4356 | transaction_mode_list ',' transaction_mode_item
4357 { $$ = lappend($1, $3); }
4358 | transaction_mode_list transaction_mode_item
4359 { $$ = lappend($1, $2); }
4362 transaction_mode_list_or_empty:
4363 transaction_mode_list
4369 /*****************************************************************************
4372 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')' AS <query>
4374 *****************************************************************************/
4376 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
4379 ViewStmt *n = makeNode(ViewStmt);
4382 n->view->istemp = $2;
4384 n->query = (Query *) $7;
4387 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
4390 ViewStmt *n = makeNode(ViewStmt);
4393 n->view->istemp = $4;
4395 n->query = (Query *) $9;
4400 /*****************************************************************************
4405 *****************************************************************************/
4407 LoadStmt: LOAD file_name
4409 LoadStmt *n = makeNode(LoadStmt);
4416 /*****************************************************************************
4420 *****************************************************************************/
4423 CREATE DATABASE database_name opt_with createdb_opt_list
4425 CreatedbStmt *n = makeNode(CreatedbStmt);
4433 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
4434 | /* EMPTY */ { $$ = NIL; }
4438 TABLESPACE opt_equal name
4440 $$ = makeDefElem("tablespace", (Node *)makeString($3));
4442 | TABLESPACE opt_equal DEFAULT
4444 $$ = makeDefElem("tablespace", NULL);
4446 | LOCATION opt_equal Sconst
4448 $$ = makeDefElem("location", (Node *)makeString($3));
4450 | LOCATION opt_equal DEFAULT
4452 $$ = makeDefElem("location", NULL);
4454 | TEMPLATE opt_equal name
4456 $$ = makeDefElem("template", (Node *)makeString($3));
4458 | TEMPLATE opt_equal DEFAULT
4460 $$ = makeDefElem("template", NULL);
4462 | ENCODING opt_equal Sconst
4464 $$ = makeDefElem("encoding", (Node *)makeString($3));
4466 | ENCODING opt_equal Iconst
4468 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
4470 | ENCODING opt_equal DEFAULT
4472 $$ = makeDefElem("encoding", NULL);
4474 | OWNER opt_equal name
4476 $$ = makeDefElem("owner", (Node *)makeString($3));
4478 | OWNER opt_equal DEFAULT
4480 $$ = makeDefElem("owner", NULL);
4485 * Though the equals sign doesn't match other WITH options, pg_dump uses
4486 * equals for backward compability, and it doesn't seem worth removing it.
4494 /*****************************************************************************
4498 *****************************************************************************/
4500 AlterDatabaseSetStmt:
4501 ALTER DATABASE database_name SET set_rest
4503 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
4505 n->variable = $5->name;
4506 n->value = $5->args;
4509 | ALTER DATABASE database_name VariableResetStmt
4511 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
4513 n->variable = ((VariableResetStmt *)$4)->name;
4520 /*****************************************************************************
4524 * This is implicitly CASCADE, no need for drop behavior
4525 *****************************************************************************/
4527 DropdbStmt: DROP DATABASE database_name
4529 DropdbStmt *n = makeNode(DropdbStmt);
4536 /*****************************************************************************
4538 * Manipulate a domain
4540 *****************************************************************************/
4543 CREATE DOMAIN_P any_name opt_as Typename ColQualList
4545 CreateDomainStmt *n = makeNode(CreateDomainStmt);
4548 n->constraints = $6;
4554 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
4555 ALTER DOMAIN_P any_name alter_column_default
4557 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4563 /* ALTER DOMAIN <domain> DROP NOT NULL */
4564 | ALTER DOMAIN_P any_name DROP NOT NULL_P
4566 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4571 /* ALTER DOMAIN <domain> SET NOT NULL */
4572 | ALTER DOMAIN_P any_name SET NOT NULL_P
4574 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4579 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
4580 | ALTER DOMAIN_P any_name ADD TableConstraint
4582 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4588 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
4589 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
4591 AlterDomainStmt *n = makeNode(AlterDomainStmt);
4605 /*****************************************************************************
4607 * Manipulate a conversion
4609 * CREATE [DEFAULT] CONVERSION <conversion_name>
4610 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
4612 *****************************************************************************/
4614 CreateConversionStmt:
4615 CREATE opt_default CONVERSION_P any_name FOR Sconst
4616 TO Sconst FROM any_name
4618 CreateConversionStmt *n = makeNode(CreateConversionStmt);
4619 n->conversion_name = $4;
4620 n->for_encoding_name = $6;
4621 n->to_encoding_name = $8;
4628 /*****************************************************************************
4631 * cluster <index_name> on <qualified_name>
4632 * cluster <qualified_name>
4635 *****************************************************************************/
4638 CLUSTER index_name ON qualified_name
4640 ClusterStmt *n = makeNode(ClusterStmt);
4645 | CLUSTER qualified_name
4647 ClusterStmt *n = makeNode(ClusterStmt);
4649 n->indexname = NULL;
4654 ClusterStmt *n = makeNode(ClusterStmt);
4656 n->indexname = NULL;
4661 /*****************************************************************************
4667 *****************************************************************************/
4669 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
4671 VacuumStmt *n = makeNode(VacuumStmt);
4681 | VACUUM opt_full opt_freeze opt_verbose qualified_name
4683 VacuumStmt *n = makeNode(VacuumStmt);
4693 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
4695 VacuumStmt *n = (VacuumStmt *) $5;
4705 analyze_keyword opt_verbose
4707 VacuumStmt *n = makeNode(VacuumStmt);
4717 | analyze_keyword opt_verbose qualified_name opt_name_list
4719 VacuumStmt *n = makeNode(VacuumStmt);
4733 | ANALYSE /* British */ {}
4737 VERBOSE { $$ = TRUE; }
4738 | /*EMPTY*/ { $$ = FALSE; }
4741 opt_full: FULL { $$ = TRUE; }
4742 | /*EMPTY*/ { $$ = FALSE; }
4745 opt_freeze: FREEZE { $$ = TRUE; }
4746 | /*EMPTY*/ { $$ = FALSE; }
4750 '(' name_list ')' { $$ = $2; }
4751 | /*EMPTY*/ { $$ = NIL; }
4755 /*****************************************************************************
4758 * EXPLAIN [ANALYZE] [VERBOSE] query
4760 *****************************************************************************/
4762 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
4764 ExplainStmt *n = makeNode(ExplainStmt);
4767 n->query = (Query*)$4;
4778 | ExecuteStmt /* by default all are $$=$1 */
4782 analyze_keyword { $$ = TRUE; }
4783 | /* EMPTY */ { $$ = FALSE; }
4786 /*****************************************************************************
4789 * PREPARE <plan_name> [(args, ...)] AS <query>
4791 *****************************************************************************/
4793 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
4795 PrepareStmt *n = makeNode(PrepareStmt);
4798 n->query = (Query *) $5;
4803 prep_type_clause: '(' prep_type_list ')' { $$ = $2; }
4804 | /* EMPTY */ { $$ = NIL; }
4807 prep_type_list: Typename { $$ = list_make1($1); }
4808 | prep_type_list ',' Typename
4809 { $$ = lappend($1, $3); }
4816 | DeleteStmt /* by default all are $$=$1 */
4819 /*****************************************************************************
4821 * EXECUTE <plan_name> [(params, ...)]
4822 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
4824 *****************************************************************************/
4826 ExecuteStmt: EXECUTE name execute_param_clause
4828 ExecuteStmt *n = makeNode(ExecuteStmt);
4834 | CREATE OptTemp TABLE qualified_name OptCreateAs AS EXECUTE name execute_param_clause
4836 ExecuteStmt *n = makeNode(ExecuteStmt);
4843 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4844 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
4845 /* ... because it's not implemented, but it could be */
4850 execute_param_clause: '(' expr_list ')' { $$ = $2; }
4851 | /* EMPTY */ { $$ = NIL; }
4854 /*****************************************************************************
4857 * DEALLOCATE [PREPARE] <plan_name>
4859 *****************************************************************************/
4861 DeallocateStmt: DEALLOCATE name
4863 DeallocateStmt *n = makeNode(DeallocateStmt);
4867 | DEALLOCATE PREPARE name
4869 DeallocateStmt *n = makeNode(DeallocateStmt);
4875 /*****************************************************************************
4880 *****************************************************************************/
4883 INSERT INTO qualified_name insert_rest
4891 VALUES '(' insert_target_list ')'
4893 $$ = makeNode(InsertStmt);
4895 $$->targetList = $3;
4896 $$->selectStmt = NULL;
4900 $$ = makeNode(InsertStmt);
4902 $$->targetList = NIL;
4903 $$->selectStmt = NULL;
4907 $$ = makeNode(InsertStmt);
4909 $$->targetList = NIL;
4910 $$->selectStmt = $1;
4912 | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
4914 $$ = makeNode(InsertStmt);
4916 $$->targetList = $6;
4917 $$->selectStmt = NULL;
4919 | '(' insert_column_list ')' SelectStmt
4921 $$ = makeNode(InsertStmt);
4923 $$->targetList = NIL;
4924 $$->selectStmt = $4;
4930 { $$ = list_make1($1); }
4931 | insert_column_list ',' insert_column_item
4932 { $$ = lappend($1, $3); }
4936 ColId opt_indirection
4938 $$ = makeNode(ResTarget);
4940 $$->indirection = $2;
4946 /*****************************************************************************
4951 *****************************************************************************/
4953 DeleteStmt: DELETE_P FROM relation_expr using_clause where_clause
4955 DeleteStmt *n = makeNode(DeleteStmt);
4957 n->usingClause = $4;
4958 n->whereClause = $5;
4964 USING from_list { $$ = $2; }
4965 | /*EMPTY*/ { $$ = NIL; }
4968 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
4970 LockStmt *n = makeNode(LockStmt);
4979 opt_lock: IN_P lock_type MODE { $$ = $2; }
4980 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
4983 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
4984 | ROW SHARE { $$ = RowShareLock; }
4985 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
4986 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
4987 | SHARE { $$ = ShareLock; }
4988 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
4989 | EXCLUSIVE { $$ = ExclusiveLock; }
4990 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
4993 opt_nowait: NOWAIT { $$ = TRUE; }
4994 | /*EMPTY*/ { $$ = FALSE; }
4998 /*****************************************************************************
5001 * UpdateStmt (UPDATE)
5003 *****************************************************************************/
5005 UpdateStmt: UPDATE relation_expr
5006 SET update_target_list
5010 UpdateStmt *n = makeNode(UpdateStmt);
5014 n->whereClause = $6;
5020 /*****************************************************************************
5025 *****************************************************************************/
5026 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
5028 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
5033 n->options |= CURSOR_OPT_HOLD;
5038 cursor_options: /*EMPTY*/ { $$ = 0; }
5039 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
5040 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
5041 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
5042 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
5045 opt_hold: /* EMPTY */ { $$ = FALSE; }
5046 | WITH HOLD { $$ = TRUE; }
5047 | WITHOUT HOLD { $$ = FALSE; }
5050 /*****************************************************************************
5055 *****************************************************************************/
5057 /* A complete SELECT statement looks like this.
5059 * The rule returns either a single SelectStmt node or a tree of them,
5060 * representing a set-operation tree.
5062 * There is an ambiguity when a sub-SELECT is within an a_expr and there
5063 * are excess parentheses: do the parentheses belong to the sub-SELECT or
5064 * to the surrounding a_expr? We don't really care, but yacc wants to know.
5065 * To resolve the ambiguity, we are careful to define the grammar so that
5066 * the decision is staved off as long as possible: as long as we can keep
5067 * absorbing parentheses into the sub-SELECT, we will do so, and only when
5068 * it's no longer possible to do that will we decide that parens belong to
5069 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
5070 * parentheses are treated as part of the sub-select. The necessity of doing
5071 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
5072 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
5073 * SELECT viewpoint when we see the UNION.
5075 * This approach is implemented by defining a nonterminal select_with_parens,
5076 * which represents a SELECT with at least one outer layer of parentheses,
5077 * and being careful to use select_with_parens, never '(' SelectStmt ')',
5078 * in the expression grammar. We will then have shift-reduce conflicts
5079 * which we can resolve in favor of always treating '(' <select> ')' as
5080 * a select_with_parens. To resolve the conflicts, the productions that
5081 * conflict with the select_with_parens productions are manually given
5082 * precedences lower than the precedence of ')', thereby ensuring that we
5083 * shift ')' (and then reduce to select_with_parens) rather than trying to
5084 * reduce the inner <select> nonterminal to something else. We use UMINUS
5085 * precedence for this, which is a fairly arbitrary choice.
5087 * To be able to define select_with_parens itself without ambiguity, we need
5088 * a nonterminal select_no_parens that represents a SELECT structure with no
5089 * outermost parentheses. This is a little bit tedious, but it works.
5091 * In non-expression contexts, we use SelectStmt which can represent a SELECT
5092 * with or without outer parentheses.
5095 SelectStmt: select_no_parens %prec UMINUS
5096 | select_with_parens %prec UMINUS
5100 '(' select_no_parens ')' { $$ = $2; }
5101 | '(' select_with_parens ')' { $$ = $2; }
5105 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
5106 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
5107 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
5111 simple_select { $$ = $1; }
5112 | select_clause sort_clause
5114 insertSelectOptions((SelectStmt *) $1, $2, NIL,
5118 | select_clause opt_sort_clause for_locking_clause opt_select_limit
5120 insertSelectOptions((SelectStmt *) $1, $2, $3,
5121 list_nth($4, 0), list_nth($4, 1));
5124 | select_clause opt_sort_clause select_limit opt_for_locking_clause
5126 insertSelectOptions((SelectStmt *) $1, $2, $4,
5127 list_nth($3, 0), list_nth($3, 1));
5133 simple_select { $$ = $1; }
5134 | select_with_parens { $$ = $1; }
5138 * This rule parses SELECT statements that can appear within set operations,
5139 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
5140 * the ordering of the set operations. Without '(' and ')' we want the
5141 * operations to be ordered per the precedence specs at the head of this file.
5143 * As with select_no_parens, simple_select cannot have outer parentheses,
5144 * but can have parenthesized subclauses.
5146 * Note that sort clauses cannot be included at this level --- SQL92 requires
5147 * SELECT foo UNION SELECT bar ORDER BY baz
5149 * (SELECT foo UNION SELECT bar) ORDER BY baz
5151 * SELECT foo UNION (SELECT bar ORDER BY baz)
5152 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
5153 * as part of the select_no_parens production, not simple_select.
5154 * This does not limit functionality, because you can reintroduce sort and
5155 * limit clauses inside parentheses.
5157 * NOTE: only the leftmost component SelectStmt should have INTO.
5158 * However, this is not checked by the grammar; parse analysis must check it.
5161 SELECT opt_distinct target_list
5162 into_clause from_clause where_clause
5163 group_clause having_clause
5165 SelectStmt *n = makeNode(SelectStmt);
5166 n->distinctClause = $2;
5169 n->intoColNames = NIL;
5170 n->intoHasOids = DEFAULT_OIDS;
5172 n->whereClause = $6;
5173 n->groupClause = $7;
5174 n->havingClause = $8;
5177 | select_clause UNION opt_all select_clause
5179 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
5181 | select_clause INTERSECT opt_all select_clause
5183 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
5185 | select_clause EXCEPT opt_all select_clause
5187 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
5192 INTO OptTempTableName { $$ = $2; }
5193 | /*EMPTY*/ { $$ = NULL; }
5197 * Redundancy here is needed to avoid shift/reduce conflicts,
5198 * since TEMP is not a reserved word. See also OptTemp.
5201 TEMPORARY opt_table qualified_name
5206 | TEMP opt_table qualified_name
5211 | LOCAL TEMPORARY opt_table qualified_name
5216 | LOCAL TEMP opt_table qualified_name
5221 | GLOBAL TEMPORARY opt_table qualified_name
5226 | GLOBAL TEMP opt_table qualified_name
5231 | TABLE qualified_name
5247 opt_all: ALL { $$ = TRUE; }
5248 | DISTINCT { $$ = FALSE; }
5249 | /*EMPTY*/ { $$ = FALSE; }
5252 /* We use (NIL) as a placeholder to indicate that all target expressions
5253 * should be placed in the DISTINCT list during parsetree analysis.
5256 DISTINCT { $$ = list_make1(NIL); }
5257 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
5259 | /*EMPTY*/ { $$ = NIL; }
5263 sort_clause { $$ = $1;}
5264 | /*EMPTY*/ { $$ = NIL; }
5268 ORDER BY sortby_list { $$ = $3; }
5272 sortby { $$ = list_make1($1); }
5273 | sortby_list ',' sortby { $$ = lappend($1, $3); }
5276 sortby: a_expr USING qual_all_Op
5278 $$ = makeNode(SortBy);
5280 $$->sortby_kind = SORTBY_USING;
5285 $$ = makeNode(SortBy);
5287 $$->sortby_kind = SORTBY_ASC;
5292 $$ = makeNode(SortBy);
5294 $$->sortby_kind = SORTBY_DESC;
5299 $$ = makeNode(SortBy);
5301 $$->sortby_kind = SORTBY_ASC; /* default */
5308 LIMIT select_limit_value OFFSET select_offset_value
5309 { $$ = list_make2($4, $2); }
5310 | OFFSET select_offset_value LIMIT select_limit_value
5311 { $$ = list_make2($2, $4); }
5312 | LIMIT select_limit_value
5313 { $$ = list_make2(NULL, $2); }
5314 | OFFSET select_offset_value
5315 { $$ = list_make2($2, NULL); }
5316 | LIMIT select_limit_value ',' select_offset_value
5318 /* Disabled because it was too confusing, bjm 2002-02-18 */
5320 (errcode(ERRCODE_SYNTAX_ERROR),
5321 errmsg("LIMIT #,# syntax is not supported"),
5322 errhint("Use separate LIMIT and OFFSET clauses.")));
5327 select_limit { $$ = $1; }
5329 { $$ = list_make2(NULL,NULL); }
5336 /* LIMIT ALL is represented as a NULL constant */
5337 A_Const *n = makeNode(A_Const);
5338 n->val.type = T_Null;
5343 select_offset_value:
5348 * jimmy bell-style recursive queries aren't supported in the
5351 * ...however, recursive addattr and rename supported. make special
5356 GROUP_P BY expr_list { $$ = $3; }
5357 | /*EMPTY*/ { $$ = NIL; }
5361 HAVING a_expr { $$ = $2; }
5362 | /*EMPTY*/ { $$ = NULL; }
5366 FOR UPDATE update_list { $$ = lcons(makeString("for_update"), $3); }
5367 | FOR SHARE update_list { $$ = lcons(makeString("for_share"), $3); }
5368 | FOR READ ONLY { $$ = NULL; }
5371 opt_for_locking_clause:
5372 for_locking_clause { $$ = $1; }
5373 | /* EMPTY */ { $$ = NULL; }
5377 OF name_list { $$ = $2; }
5378 | /* EMPTY */ { $$ = list_make1(NULL); }
5381 /*****************************************************************************
5383 * clauses common to all Optimizable Stmts:
5384 * from_clause - allow list of both JOIN expressions and table names
5385 * where_clause - qualifications for joins or restrictions
5387 *****************************************************************************/
5390 FROM from_list { $$ = $2; }
5391 | /*EMPTY*/ { $$ = NIL; }
5395 table_ref { $$ = list_make1($1); }
5396 | from_list ',' table_ref { $$ = lappend($1, $3); }
5400 * table_ref is where an alias clause can be attached. Note we cannot make
5401 * alias_clause have an empty production because that causes parse conflicts
5402 * between table_ref := '(' joined_table ')' alias_clause
5403 * and joined_table := '(' joined_table ')'. So, we must have the
5404 * redundant-looking productions here instead.
5406 table_ref: relation_expr
5410 | relation_expr alias_clause
5417 RangeFunction *n = makeNode(RangeFunction);
5418 n->funccallnode = $1;
5419 n->coldeflist = NIL;
5422 | func_table alias_clause
5424 RangeFunction *n = makeNode(RangeFunction);
5425 n->funccallnode = $1;
5427 n->coldeflist = NIL;
5430 | func_table AS '(' TableFuncElementList ')'
5432 RangeFunction *n = makeNode(RangeFunction);
5433 n->funccallnode = $1;
5437 | func_table AS ColId '(' TableFuncElementList ')'
5439 RangeFunction *n = makeNode(RangeFunction);
5440 Alias *a = makeNode(Alias);
5441 n->funccallnode = $1;
5447 | func_table ColId '(' TableFuncElementList ')'
5449 RangeFunction *n = makeNode(RangeFunction);
5450 Alias *a = makeNode(Alias);
5451 n->funccallnode = $1;
5457 | select_with_parens
5460 * The SQL spec does not permit a subselect
5461 * (<derived_table>) without an alias clause,
5462 * so we don't either. This avoids the problem
5463 * of needing to invent a unique refname for it.
5464 * That could be surmounted if there's sufficient
5465 * popular demand, but for now let's just implement
5466 * the spec and see if anyone complains.
5467 * However, it does seem like a good idea to emit
5468 * an error message that's better than "syntax error".
5471 (errcode(ERRCODE_SYNTAX_ERROR),
5472 errmsg("subquery in FROM must have an alias"),
5473 errhint("For example, FROM (SELECT ...) [AS] foo.")));
5476 | select_with_parens alias_clause
5478 RangeSubselect *n = makeNode(RangeSubselect);
5487 | '(' joined_table ')' alias_clause
5496 * It may seem silly to separate joined_table from table_ref, but there is
5497 * method in SQL92's madness: if you don't do it this way you get reduce-
5498 * reduce conflicts, because it's not clear to the parser generator whether
5499 * to expect alias_clause after ')' or not. For the same reason we must
5500 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
5501 * join_type to expand to empty; if we try it, the parser generator can't
5502 * figure out when to reduce an empty join_type right after table_ref.
5504 * Note that a CROSS JOIN is the same as an unqualified
5505 * INNER JOIN, and an INNER JOIN/ON has the same shape
5506 * but a qualification expression to limit membership.
5507 * A NATURAL JOIN implicitly matches column names between
5508 * tables and the shape is determined by which columns are
5509 * in common. We'll collect columns during the later transformations.
5513 '(' joined_table ')'
5517 | table_ref CROSS JOIN table_ref
5519 /* CROSS JOIN is same as unqualified inner join */
5520 JoinExpr *n = makeNode(JoinExpr);
5521 n->jointype = JOIN_INNER;
5522 n->isNatural = FALSE;
5529 | table_ref UNIONJOIN table_ref
5531 /* UNION JOIN is made into 1 token to avoid shift/reduce
5532 * conflict against regular UNION keyword.
5534 JoinExpr *n = makeNode(JoinExpr);
5535 n->jointype = JOIN_UNION;
5536 n->isNatural = FALSE;
5543 | table_ref join_type JOIN table_ref join_qual
5545 JoinExpr *n = makeNode(JoinExpr);
5547 n->isNatural = FALSE;
5550 if ($5 != NULL && IsA($5, List))
5551 n->using = (List *) $5; /* USING clause */
5553 n->quals = $5; /* ON clause */
5556 | table_ref JOIN table_ref join_qual
5558 /* letting join_type reduce to empty doesn't work */
5559 JoinExpr *n = makeNode(JoinExpr);
5560 n->jointype = JOIN_INNER;
5561 n->isNatural = FALSE;
5564 if ($4 != NULL && IsA($4, List))
5565 n->using = (List *) $4; /* USING clause */
5567 n->quals = $4; /* ON clause */
5570 | table_ref NATURAL join_type JOIN table_ref
5572 JoinExpr *n = makeNode(JoinExpr);
5574 n->isNatural = TRUE;
5577 n->using = NIL; /* figure out which columns later... */
5578 n->quals = NULL; /* fill later */
5581 | table_ref NATURAL JOIN table_ref
5583 /* letting join_type reduce to empty doesn't work */
5584 JoinExpr *n = makeNode(JoinExpr);
5585 n->jointype = JOIN_INNER;
5586 n->isNatural = TRUE;
5589 n->using = NIL; /* figure out which columns later... */
5590 n->quals = NULL; /* fill later */
5596 AS ColId '(' name_list ')'
5598 $$ = makeNode(Alias);
5604 $$ = makeNode(Alias);
5607 | ColId '(' name_list ')'
5609 $$ = makeNode(Alias);
5615 $$ = makeNode(Alias);
5620 join_type: FULL join_outer { $$ = JOIN_FULL; }
5621 | LEFT join_outer { $$ = JOIN_LEFT; }
5622 | RIGHT join_outer { $$ = JOIN_RIGHT; }
5623 | INNER_P { $$ = JOIN_INNER; }
5626 /* OUTER is just noise... */
5627 join_outer: OUTER_P { $$ = NULL; }
5628 | /*EMPTY*/ { $$ = NULL; }
5631 /* JOIN qualification clauses
5632 * Possibilities are:
5633 * USING ( column list ) allows only unqualified column names,
5634 * which must match between tables.
5635 * ON expr allows more general qualifications.
5637 * We return USING as a List node, while an ON-expr will not be a List.
5640 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
5641 | ON a_expr { $$ = $2; }
5648 /* default inheritance */
5650 $$->inhOpt = INH_DEFAULT;
5653 | qualified_name '*'
5655 /* inheritance query */
5657 $$->inhOpt = INH_YES;
5660 | ONLY qualified_name
5662 /* no inheritance */
5664 $$->inhOpt = INH_NO;
5667 | ONLY '(' qualified_name ')'
5669 /* no inheritance, SQL99-style syntax */
5671 $$->inhOpt = INH_NO;
5677 func_table: func_expr { $$ = $1; }
5682 WHERE a_expr { $$ = $2; }
5683 | /*EMPTY*/ { $$ = NULL; }
5687 TableFuncElementList:
5690 $$ = list_make1($1);
5692 | TableFuncElementList ',' TableFuncElement
5694 $$ = lappend($1, $3);
5698 TableFuncElement: ColId Typename
5700 ColumnDef *n = makeNode(ColumnDef);
5703 n->constraints = NIL;
5709 /*****************************************************************************
5712 * SQL92 introduces a large amount of type-specific syntax.
5713 * Define individual clauses to handle these cases, and use
5714 * the generic case to handle regular type-extensible Postgres syntax.
5715 * - thomas 1997-10-10
5717 *****************************************************************************/
5719 Typename: SimpleTypename opt_array_bounds
5722 $$->arrayBounds = $2;
5724 | SETOF SimpleTypename opt_array_bounds
5727 $$->arrayBounds = $3;
5730 | SimpleTypename ARRAY '[' Iconst ']'
5732 /* SQL99's redundant syntax */
5734 $$->arrayBounds = list_make1(makeInteger($4));
5736 | SETOF SimpleTypename ARRAY '[' Iconst ']'
5738 /* SQL99's redundant syntax */
5740 $$->arrayBounds = list_make1(makeInteger($5));
5746 opt_array_bounds '[' ']'
5747 { $$ = lappend($1, makeInteger(-1)); }
5748 | opt_array_bounds '[' Iconst ']'
5749 { $$ = lappend($1, makeInteger($3)); }
5755 * XXX ideally, the production for a qualified typename should be ColId attrs
5756 * (there's no obvious reason why the first name should need to be restricted)
5757 * and should be an alternative of GenericType (so that it can be used to
5758 * specify a type for a literal in AExprConst). However doing either causes
5759 * reduce/reduce conflicts that I haven't been able to find a workaround
5763 GenericType { $$ = $1; }
5764 | Numeric { $$ = $1; }
5766 | Character { $$ = $1; }
5767 | ConstDatetime { $$ = $1; }
5768 | ConstInterval opt_interval
5771 if ($2 != INTERVAL_FULL_RANGE)
5772 $$->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $2);
5774 | ConstInterval '(' Iconst ')' opt_interval
5779 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5780 errmsg("INTERVAL(%d) precision must not be negative",
5782 if ($3 > MAX_INTERVAL_PRECISION)
5785 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5786 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
5787 $3, MAX_INTERVAL_PRECISION)));
5788 $3 = MAX_INTERVAL_PRECISION;
5790 $$->typmod = INTERVAL_TYPMOD($3, $5);
5794 $$ = makeNode(TypeName);
5795 $$->names = lcons(makeString($1), $2);
5800 /* We have a separate ConstTypename to allow defaulting fixed-length
5801 * types such as CHAR() and BIT() to an unspecified length.
5802 * SQL9x requires that these default to a length of one, but this
5803 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
5804 * where there is an obvious better choice to make.
5805 * Note that ConstInterval is not included here since it must
5806 * be pushed up higher in the rules to accomodate the postfix
5807 * options (e.g. INTERVAL '1' YEAR).
5810 GenericType { $$ = $1; }
5811 | Numeric { $$ = $1; }
5812 | ConstBit { $$ = $1; }
5813 | ConstCharacter { $$ = $1; }
5814 | ConstDatetime { $$ = $1; }
5820 $$ = makeTypeName($1);
5824 /* SQL92 numeric data types
5825 * Check FLOAT() precision limits assuming IEEE floating types.
5826 * - thomas 1997-09-18
5827 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
5831 $$ = SystemTypeName("int4");
5835 $$ = SystemTypeName("int4");
5839 $$ = SystemTypeName("int2");
5843 $$ = SystemTypeName("int8");
5847 $$ = SystemTypeName("float4");
5853 | DOUBLE_P PRECISION
5855 $$ = SystemTypeName("float8");
5857 | DECIMAL_P opt_decimal
5859 $$ = SystemTypeName("numeric");
5864 $$ = SystemTypeName("numeric");
5867 | NUMERIC opt_numeric
5869 $$ = SystemTypeName("numeric");
5874 $$ = SystemTypeName("bool");
5878 opt_float: '(' Iconst ')'
5882 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5883 errmsg("precision for type float must be at least 1 bit")));
5885 $$ = SystemTypeName("float4");
5887 $$ = SystemTypeName("float8");
5890 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5891 errmsg("precision for type float must be less than 54 bits")));
5895 $$ = SystemTypeName("float8");
5900 '(' Iconst ',' Iconst ')'
5902 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5904 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5905 errmsg("NUMERIC precision %d must be between 1 and %d",
5906 $2, NUMERIC_MAX_PRECISION)));
5907 if ($4 < 0 || $4 > $2)
5909 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5910 errmsg("NUMERIC scale %d must be between 0 and precision %d",
5913 $$ = (($2 << 16) | $4) + VARHDRSZ;
5917 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5919 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5920 errmsg("NUMERIC precision %d must be between 1 and %d",
5921 $2, NUMERIC_MAX_PRECISION)));
5923 $$ = ($2 << 16) + VARHDRSZ;
5927 /* Insert "-1" meaning "no limit" */
5933 '(' Iconst ',' Iconst ')'
5935 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5937 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5938 errmsg("DECIMAL precision %d must be between 1 and %d",
5939 $2, NUMERIC_MAX_PRECISION)));
5940 if ($4 < 0 || $4 > $2)
5942 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5943 errmsg("DECIMAL scale %d must be between 0 and precision %d",
5946 $$ = (($2 << 16) | $4) + VARHDRSZ;
5950 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
5952 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5953 errmsg("DECIMAL precision %d must be between 1 and %d",
5954 $2, NUMERIC_MAX_PRECISION)));
5956 $$ = ($2 << 16) + VARHDRSZ;
5960 /* Insert "-1" meaning "no limit" */
5967 * SQL92 bit-field data types
5968 * The following implements BIT() and BIT VARYING().
5980 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
5981 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
5982 ConstBit: BitWithLength
5994 BIT opt_varying '(' Iconst ')'
5998 typname = $2 ? "varbit" : "bit";
5999 $$ = SystemTypeName(typname);
6002 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6003 errmsg("length for type %s must be at least 1",
6005 else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
6007 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6008 errmsg("length for type %s cannot exceed %d",
6009 typname, MaxAttrSize * BITS_PER_BYTE)));
6017 /* bit defaults to bit(1), varbit to no limit */
6020 $$ = SystemTypeName("varbit");
6025 $$ = SystemTypeName("bit");
6033 * SQL92 character data types
6034 * The following implements CHAR() and VARCHAR().
6036 Character: CharacterWithLength
6040 | CharacterWithoutLength
6046 ConstCharacter: CharacterWithLength
6050 | CharacterWithoutLength
6052 /* Length was not specified so allow to be unrestricted.
6053 * This handles problems with fixed-length (bpchar) strings
6054 * which in column definitions must default to a length
6055 * of one, but should not be constrained if the length
6056 * was not specified.
6063 CharacterWithLength: character '(' Iconst ')' opt_charset
6065 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
6069 type = palloc(strlen($1) + 1 + strlen($5) + 1);
6076 $$ = SystemTypeName($1);
6080 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6081 errmsg("length for type %s must be at least 1",
6083 else if ($3 > MaxAttrSize)
6085 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6086 errmsg("length for type %s cannot exceed %d",
6089 /* we actually implement these like a varlen, so
6090 * the first 4 bytes is the length. (the difference
6091 * between these and "text" is that we blank-pad and
6092 * truncate where necessary)
6094 $$->typmod = VARHDRSZ + $3;
6098 CharacterWithoutLength: character opt_charset
6100 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
6104 type = palloc(strlen($1) + 1 + strlen($2) + 1);
6111 $$ = SystemTypeName($1);
6113 /* char defaults to char(1), varchar to no limit */
6114 if (strcmp($1, "bpchar") == 0)
6115 $$->typmod = VARHDRSZ + 1;
6121 character: CHARACTER opt_varying
6122 { $$ = $2 ? "varchar": "bpchar"; }
6123 | CHAR_P opt_varying
6124 { $$ = $2 ? "varchar": "bpchar"; }
6127 | NATIONAL CHARACTER opt_varying
6128 { $$ = $3 ? "varchar": "bpchar"; }
6129 | NATIONAL CHAR_P opt_varying
6130 { $$ = $3 ? "varchar": "bpchar"; }
6132 { $$ = $2 ? "varchar": "bpchar"; }
6136 VARYING { $$ = TRUE; }
6137 | /*EMPTY*/ { $$ = FALSE; }
6141 CHARACTER SET ColId { $$ = $3; }
6142 | /*EMPTY*/ { $$ = NULL; }
6146 TIMESTAMP '(' Iconst ')' opt_timezone
6149 $$ = SystemTypeName("timestamptz");
6151 $$ = SystemTypeName("timestamp");
6152 /* XXX the timezone field seems to be unused
6153 * - thomas 2001-09-06
6158 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6159 errmsg("TIMESTAMP(%d)%s precision must not be negative",
6160 $3, ($5 ? " WITH TIME ZONE": ""))));
6161 if ($3 > MAX_TIMESTAMP_PRECISION)
6164 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6165 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
6166 $3, ($5 ? " WITH TIME ZONE": ""),
6167 MAX_TIMESTAMP_PRECISION)));
6168 $3 = MAX_TIMESTAMP_PRECISION;
6172 | TIMESTAMP opt_timezone
6175 $$ = SystemTypeName("timestamptz");
6177 $$ = SystemTypeName("timestamp");
6178 /* XXX the timezone field seems to be unused
6179 * - thomas 2001-09-06
6183 | TIME '(' Iconst ')' opt_timezone
6186 $$ = SystemTypeName("timetz");
6188 $$ = SystemTypeName("time");
6191 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6192 errmsg("TIME(%d)%s precision must not be negative",
6193 $3, ($5 ? " WITH TIME ZONE": ""))));
6194 if ($3 > MAX_TIME_PRECISION)
6197 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6198 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
6199 $3, ($5 ? " WITH TIME ZONE": ""),
6200 MAX_TIME_PRECISION)));
6201 $3 = MAX_TIME_PRECISION;
6208 $$ = SystemTypeName("timetz");
6210 $$ = SystemTypeName("time");
6215 INTERVAL { $$ = SystemTypeName("interval"); }
6219 WITH TIME ZONE { $$ = TRUE; }
6220 | WITHOUT TIME ZONE { $$ = FALSE; }
6221 | /*EMPTY*/ { $$ = FALSE; }
6225 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
6226 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
6227 | DAY_P { $$ = INTERVAL_MASK(DAY); }
6228 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
6229 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
6230 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
6232 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
6234 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
6236 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
6237 | INTERVAL_MASK(MINUTE); }
6239 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
6240 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
6241 | HOUR_P TO MINUTE_P
6242 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
6243 | HOUR_P TO SECOND_P
6244 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
6245 | INTERVAL_MASK(SECOND); }
6246 | MINUTE_P TO SECOND_P
6247 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
6248 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
6252 /*****************************************************************************
6254 * expression grammar
6256 *****************************************************************************/
6259 * General expressions
6260 * This is the heart of the expression syntax.
6262 * We have two expression types: a_expr is the unrestricted kind, and
6263 * b_expr is a subset that must be used in some places to avoid shift/reduce
6264 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
6265 * because that use of AND conflicts with AND as a boolean operator. So,
6266 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
6268 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
6269 * always be used by surrounding it with parens.
6271 * c_expr is all the productions that are common to a_expr and b_expr;
6272 * it's factored out just to eliminate redundant coding.
6274 a_expr: c_expr { $$ = $1; }
6275 | a_expr TYPECAST Typename
6276 { $$ = makeTypeCast($1, $3); }
6277 | a_expr AT TIME ZONE a_expr
6279 FuncCall *n = makeNode(FuncCall);
6280 n->funcname = SystemFuncName("timezone");
6281 n->args = list_make2($5, $1);
6282 n->agg_star = FALSE;
6283 n->agg_distinct = FALSE;
6287 * These operators must be called out explicitly in order to make use
6288 * of yacc/bison's automatic operator-precedence handling. All other
6289 * operator names are handled by the generic productions using "Op",
6290 * below; and all those operators will have the same precedence.
6292 * If you add more explicitly-known operators, be sure to add them
6293 * also to b_expr and to the MathOp list above.
6295 | '+' a_expr %prec UMINUS
6296 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
6297 | '-' a_expr %prec UMINUS
6298 { $$ = doNegate($2); }
6300 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
6302 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
6304 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
6306 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
6308 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
6310 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
6312 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
6314 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
6316 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
6318 | a_expr qual_Op a_expr %prec Op
6319 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
6320 | qual_Op a_expr %prec Op
6321 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
6322 | a_expr qual_Op %prec POSTFIXOP
6323 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
6326 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3); }
6328 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3); }
6330 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2); }
6332 | a_expr LIKE a_expr
6333 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3); }
6334 | a_expr LIKE a_expr ESCAPE a_expr
6336 FuncCall *n = makeNode(FuncCall);
6337 n->funcname = SystemFuncName("like_escape");
6338 n->args = list_make2($3, $5);
6339 n->agg_star = FALSE;
6340 n->agg_distinct = FALSE;
6341 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n);
6343 | a_expr NOT LIKE a_expr
6344 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4); }
6345 | a_expr NOT LIKE a_expr ESCAPE a_expr
6347 FuncCall *n = makeNode(FuncCall);
6348 n->funcname = SystemFuncName("like_escape");
6349 n->args = list_make2($4, $6);
6350 n->agg_star = FALSE;
6351 n->agg_distinct = FALSE;
6352 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n);
6354 | a_expr ILIKE a_expr
6355 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3); }
6356 | a_expr ILIKE a_expr ESCAPE a_expr
6358 FuncCall *n = makeNode(FuncCall);
6359 n->funcname = SystemFuncName("like_escape");
6360 n->args = list_make2($3, $5);
6361 n->agg_star = FALSE;
6362 n->agg_distinct = FALSE;
6363 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n);
6365 | a_expr NOT ILIKE a_expr
6366 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4); }
6367 | a_expr NOT ILIKE a_expr ESCAPE a_expr
6369 FuncCall *n = makeNode(FuncCall);
6370 n->funcname = SystemFuncName("like_escape");
6371 n->args = list_make2($4, $6);
6372 n->agg_star = FALSE;
6373 n->agg_distinct = FALSE;
6374 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n);
6377 | a_expr SIMILAR TO a_expr %prec SIMILAR
6379 A_Const *c = makeNode(A_Const);
6380 FuncCall *n = makeNode(FuncCall);
6381 c->val.type = T_Null;
6382 n->funcname = SystemFuncName("similar_escape");
6383 n->args = list_make2($4, (Node *) c);
6384 n->agg_star = FALSE;
6385 n->agg_distinct = FALSE;
6386 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
6388 | a_expr SIMILAR TO a_expr ESCAPE a_expr
6390 FuncCall *n = makeNode(FuncCall);
6391 n->funcname = SystemFuncName("similar_escape");
6392 n->args = list_make2($4, $6);
6393 n->agg_star = FALSE;
6394 n->agg_distinct = FALSE;
6395 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
6397 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
6399 A_Const *c = makeNode(A_Const);
6400 FuncCall *n = makeNode(FuncCall);
6401 c->val.type = T_Null;
6402 n->funcname = SystemFuncName("similar_escape");
6403 n->args = list_make2($5, (Node *) c);
6404 n->agg_star = FALSE;
6405 n->agg_distinct = FALSE;
6406 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
6408 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
6410 FuncCall *n = makeNode(FuncCall);
6411 n->funcname = SystemFuncName("similar_escape");
6412 n->args = list_make2($5, $7);
6413 n->agg_star = FALSE;
6414 n->agg_distinct = FALSE;
6415 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
6419 * Define SQL92-style Null test clause.
6420 * Allow two forms described in the standard:
6423 * Allow two SQL extensions
6429 if (IsA($1, RowExpr))
6430 $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
6433 NullTest *n = makeNode(NullTest);
6434 n->arg = (Expr *) $1;
6435 n->nulltesttype = IS_NULL;
6441 if (IsA($1, RowExpr))
6442 $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
6445 NullTest *n = makeNode(NullTest);
6446 n->arg = (Expr *) $1;
6447 n->nulltesttype = IS_NULL;
6453 if (IsA($1, RowExpr))
6454 $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
6457 NullTest *n = makeNode(NullTest);
6458 n->arg = (Expr *) $1;
6459 n->nulltesttype = IS_NOT_NULL;
6463 | a_expr IS NOT NULL_P
6465 if (IsA($1, RowExpr))
6466 $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
6469 NullTest *n = makeNode(NullTest);
6470 n->arg = (Expr *) $1;
6471 n->nulltesttype = IS_NOT_NULL;
6477 $$ = (Node *)makeOverlaps($1, $3);
6481 BooleanTest *b = makeNode(BooleanTest);
6482 b->arg = (Expr *) $1;
6483 b->booltesttype = IS_TRUE;
6486 | a_expr IS NOT TRUE_P
6488 BooleanTest *b = makeNode(BooleanTest);
6489 b->arg = (Expr *) $1;
6490 b->booltesttype = IS_NOT_TRUE;
6495 BooleanTest *b = makeNode(BooleanTest);
6496 b->arg = (Expr *) $1;
6497 b->booltesttype = IS_FALSE;
6500 | a_expr IS NOT FALSE_P
6502 BooleanTest *b = makeNode(BooleanTest);
6503 b->arg = (Expr *) $1;
6504 b->booltesttype = IS_NOT_FALSE;
6509 BooleanTest *b = makeNode(BooleanTest);
6510 b->arg = (Expr *) $1;
6511 b->booltesttype = IS_UNKNOWN;
6514 | a_expr IS NOT UNKNOWN
6516 BooleanTest *b = makeNode(BooleanTest);
6517 b->arg = (Expr *) $1;
6518 b->booltesttype = IS_NOT_UNKNOWN;
6521 | a_expr IS DISTINCT FROM a_expr %prec IS
6523 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
6525 | a_expr IS OF '(' type_list ')' %prec IS
6527 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
6529 | a_expr IS NOT OF '(' type_list ')' %prec IS
6531 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
6533 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
6535 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
6536 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4),
6537 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6));
6539 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
6541 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
6542 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5),
6543 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7));
6545 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
6547 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
6548 (Node *) makeA_Expr(AEXPR_AND, NIL,
6549 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4),
6550 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6)),
6551 (Node *) makeA_Expr(AEXPR_AND, NIL,
6552 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6),
6553 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4)));
6555 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
6557 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
6558 (Node *) makeA_Expr(AEXPR_OR, NIL,
6559 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5),
6560 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7)),
6561 (Node *) makeA_Expr(AEXPR_OR, NIL,
6562 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7),
6563 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5)));
6565 | a_expr IN_P in_expr
6567 /* in_expr returns a SubLink or a list of a_exprs */
6568 if (IsA($3, SubLink))
6570 SubLink *n = (SubLink *)$3;
6571 n->subLinkType = ANY_SUBLINK;
6572 if (IsA($1, RowExpr))
6573 n->lefthand = ((RowExpr *) $1)->args;
6575 n->lefthand = list_make1($1);
6576 n->operName = list_make1(makeString("="));
6583 foreach(l, (List *) $3)
6586 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, lfirst(l));
6590 n = (Node *) makeA_Expr(AEXPR_OR, NIL, n, cmp);
6595 | a_expr NOT IN_P in_expr
6597 /* in_expr returns a SubLink or a list of a_exprs */
6598 if (IsA($4, SubLink))
6600 /* Make an IN node */
6601 SubLink *n = (SubLink *)$4;
6602 n->subLinkType = ANY_SUBLINK;
6603 if (IsA($1, RowExpr))
6604 n->lefthand = ((RowExpr *) $1)->args;
6606 n->lefthand = list_make1($1);
6607 n->operName = list_make1(makeString("="));
6608 /* Stick a NOT on top */
6609 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n);
6615 foreach(l, (List *) $4)
6618 cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, lfirst(l));
6622 n = (Node *) makeA_Expr(AEXPR_AND, NIL, n, cmp);
6627 | a_expr subquery_Op sub_type select_with_parens %prec Op
6629 SubLink *n = makeNode(SubLink);
6630 n->subLinkType = $3;
6631 if (IsA($1, RowExpr))
6632 n->lefthand = ((RowExpr *) $1)->args;
6634 n->lefthand = list_make1($1);
6639 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
6641 if ($3 == ANY_SUBLINK)
6642 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5);
6644 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5);
6646 | UNIQUE select_with_parens %prec Op
6648 /* Not sure how to get rid of the parentheses
6649 * but there are lots of shift/reduce errors without them.
6651 * Should be able to implement this by plopping the entire
6652 * select into a node, then transforming the target expressions
6653 * from whatever they are into count(*), and testing the
6654 * entire result equal to one.
6655 * But, will probably implement a separate node in the executor.
6658 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6659 errmsg("UNIQUE predicate is not yet implemented")));
6664 * Restricted expressions
6666 * b_expr is a subset of the complete expression syntax defined by a_expr.
6668 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
6669 * cause trouble in the places where b_expr is used. For simplicity, we
6670 * just eliminate all the boolean-keyword-operator productions from b_expr.
6674 | b_expr TYPECAST Typename
6675 { $$ = makeTypeCast($1, $3); }
6676 | '+' b_expr %prec UMINUS
6677 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
6678 | '-' b_expr %prec UMINUS
6679 { $$ = doNegate($2); }
6681 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
6683 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
6685 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
6687 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
6689 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
6691 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
6693 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
6695 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
6697 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
6698 | b_expr qual_Op b_expr %prec Op
6699 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
6700 | qual_Op b_expr %prec Op
6701 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
6702 | b_expr qual_Op %prec POSTFIXOP
6703 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
6704 | b_expr IS DISTINCT FROM b_expr %prec IS
6706 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
6708 | b_expr IS OF '(' type_list ')' %prec IS
6710 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
6712 | b_expr IS NOT OF '(' type_list ')' %prec IS
6714 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
6719 * Productions that can be used in both a_expr and b_expr.
6721 * Note: productions that refer recursively to a_expr or b_expr mostly
6722 * cannot appear here. However, it's OK to refer to a_exprs that occur
6723 * inside parentheses, such as function arguments; that cannot introduce
6724 * ambiguity to the b_expr syntax.
6726 c_expr: columnref { $$ = $1; }
6727 | AexprConst { $$ = $1; }
6728 | PARAM opt_indirection
6730 ParamRef *p = makeNode(ParamRef);
6734 A_Indirection *n = makeNode(A_Indirection);
6735 n->arg = (Node *) p;
6736 n->indirection = $2;
6742 | '(' a_expr ')' opt_indirection
6746 A_Indirection *n = makeNode(A_Indirection);
6748 n->indirection = $4;
6758 | select_with_parens %prec UMINUS
6760 SubLink *n = makeNode(SubLink);
6761 n->subLinkType = EXPR_SUBLINK;
6767 | EXISTS select_with_parens
6769 SubLink *n = makeNode(SubLink);
6770 n->subLinkType = EXISTS_SUBLINK;
6776 | ARRAY select_with_parens
6778 SubLink *n = makeNode(SubLink);
6779 n->subLinkType = ARRAY_SUBLINK;
6789 RowExpr *r = makeNode(RowExpr);
6791 r->row_typeid = InvalidOid; /* not analyzed yet */
6797 * func_expr is split out from c_expr just so that we have a classification
6798 * for "everything that is a function call or looks like one". This isn't
6799 * very important, but it saves us having to document which variants are
6800 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
6801 * (Note that many of the special SQL functions wouldn't actually make any
6802 * sense as functional index entries, but we ignore that consideration here.)
6804 func_expr: func_name '(' ')'
6806 FuncCall *n = makeNode(FuncCall);
6809 n->agg_star = FALSE;
6810 n->agg_distinct = FALSE;
6813 | func_name '(' expr_list ')'
6815 FuncCall *n = makeNode(FuncCall);
6818 n->agg_star = FALSE;
6819 n->agg_distinct = FALSE;
6822 | func_name '(' ALL expr_list ')'
6824 FuncCall *n = makeNode(FuncCall);
6827 n->agg_star = FALSE;
6828 n->agg_distinct = FALSE;
6829 /* Ideally we'd mark the FuncCall node to indicate
6830 * "must be an aggregate", but there's no provision
6831 * for that in FuncCall at the moment.
6835 | func_name '(' DISTINCT expr_list ')'
6837 FuncCall *n = makeNode(FuncCall);
6840 n->agg_star = FALSE;
6841 n->agg_distinct = TRUE;
6844 | func_name '(' '*' ')'
6847 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
6849 * This does the right thing for COUNT(*) (in fact,
6850 * any certainly-non-null expression would do for COUNT),
6851 * and there are no other aggregates in SQL92 that accept
6854 * The FuncCall node is also marked agg_star = true,
6855 * so that later processing can detect what the argument
6858 FuncCall *n = makeNode(FuncCall);
6859 A_Const *star = makeNode(A_Const);
6861 star->val.type = T_Integer;
6862 star->val.val.ival = 1;
6864 n->args = list_make1(star);
6866 n->agg_distinct = FALSE;
6872 * Translate as "'now'::text::date".
6874 * We cannot use "'now'::date" because coerce_type() will
6875 * immediately reduce that to a constant representing
6876 * today's date. We need to delay the conversion until
6877 * runtime, else the wrong things will happen when
6878 * CURRENT_DATE is used in a column default value or rule.
6880 * This could be simplified if we had a way to generate
6881 * an expression tree representing runtime application
6882 * of type-input conversion functions. (As of PG 7.3
6883 * that is actually possible, but not clear that we want
6886 A_Const *s = makeNode(A_Const);
6889 s->val.type = T_String;
6890 s->val.val.str = "now";
6891 s->typename = SystemTypeName("text");
6893 d = SystemTypeName("date");
6895 $$ = (Node *)makeTypeCast((Node *)s, d);
6900 * Translate as "'now'::text::timetz".
6901 * See comments for CURRENT_DATE.
6903 A_Const *s = makeNode(A_Const);
6906 s->val.type = T_String;
6907 s->val.val.str = "now";
6908 s->typename = SystemTypeName("text");
6910 d = SystemTypeName("timetz");
6912 $$ = (Node *)makeTypeCast((Node *)s, d);
6914 | CURRENT_TIME '(' Iconst ')'
6917 * Translate as "'now'::text::timetz(n)".
6918 * See comments for CURRENT_DATE.
6920 A_Const *s = makeNode(A_Const);
6923 s->val.type = T_String;
6924 s->val.val.str = "now";
6925 s->typename = SystemTypeName("text");
6926 d = SystemTypeName("timetz");
6929 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6930 errmsg("CURRENT_TIME(%d) precision must not be negative",
6932 if ($3 > MAX_TIME_PRECISION)
6935 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6936 errmsg("CURRENT_TIME(%d) precision reduced to maximum allowed, %d",
6937 $3, MAX_TIME_PRECISION)));
6938 $3 = MAX_TIME_PRECISION;
6942 $$ = (Node *)makeTypeCast((Node *)s, d);
6947 * Translate as "now()", since we have a function that
6948 * does exactly what is needed.
6950 FuncCall *n = makeNode(FuncCall);
6951 n->funcname = SystemFuncName("now");
6953 n->agg_star = FALSE;
6954 n->agg_distinct = FALSE;
6957 | CURRENT_TIMESTAMP '(' Iconst ')'
6960 * Translate as "'now'::text::timestamptz(n)".
6961 * See comments for CURRENT_DATE.
6963 A_Const *s = makeNode(A_Const);
6966 s->val.type = T_String;
6967 s->val.val.str = "now";
6968 s->typename = SystemTypeName("text");
6970 d = SystemTypeName("timestamptz");
6973 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6974 errmsg("CURRENT_TIMESTAMP(%d) precision must not be negative",
6976 if ($3 > MAX_TIMESTAMP_PRECISION)
6979 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
6980 errmsg("CURRENT_TIMESTAMP(%d) precision reduced to maximum allowed, %d",
6981 $3, MAX_TIMESTAMP_PRECISION)));
6982 $3 = MAX_TIMESTAMP_PRECISION;
6986 $$ = (Node *)makeTypeCast((Node *)s, d);
6991 * Translate as "'now'::text::time".
6992 * See comments for CURRENT_DATE.
6994 A_Const *s = makeNode(A_Const);
6997 s->val.type = T_String;
6998 s->val.val.str = "now";
6999 s->typename = SystemTypeName("text");
7001 d = SystemTypeName("time");
7003 $$ = (Node *)makeTypeCast((Node *)s, d);
7005 | LOCALTIME '(' Iconst ')'
7008 * Translate as "'now'::text::time(n)".
7009 * See comments for CURRENT_DATE.
7011 A_Const *s = makeNode(A_Const);
7014 s->val.type = T_String;
7015 s->val.val.str = "now";
7016 s->typename = SystemTypeName("text");
7017 d = SystemTypeName("time");
7020 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7021 errmsg("LOCALTIME(%d) precision must not be negative",
7023 if ($3 > MAX_TIME_PRECISION)
7026 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7027 errmsg("LOCALTIME(%d) precision reduced to maximum allowed, %d",
7028 $3, MAX_TIME_PRECISION)));
7029 $3 = MAX_TIME_PRECISION;
7033 $$ = (Node *)makeTypeCast((Node *)s, d);
7038 * Translate as "'now'::text::timestamp".
7039 * See comments for CURRENT_DATE.
7041 A_Const *s = makeNode(A_Const);
7044 s->val.type = T_String;
7045 s->val.val.str = "now";
7046 s->typename = SystemTypeName("text");
7048 d = SystemTypeName("timestamp");
7050 $$ = (Node *)makeTypeCast((Node *)s, d);
7052 | LOCALTIMESTAMP '(' Iconst ')'
7055 * Translate as "'now'::text::timestamp(n)".
7056 * See comments for CURRENT_DATE.
7058 A_Const *s = makeNode(A_Const);
7061 s->val.type = T_String;
7062 s->val.val.str = "now";
7063 s->typename = SystemTypeName("text");
7065 d = SystemTypeName("timestamp");
7068 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7069 errmsg("LOCALTIMESTAMP(%d) precision must not be negative",
7071 if ($3 > MAX_TIMESTAMP_PRECISION)
7074 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7075 errmsg("LOCALTIMESTAMP(%d) precision reduced to maximum allowed, %d",
7076 $3, MAX_TIMESTAMP_PRECISION)));
7077 $3 = MAX_TIMESTAMP_PRECISION;
7081 $$ = (Node *)makeTypeCast((Node *)s, d);
7085 FuncCall *n = makeNode(FuncCall);
7086 n->funcname = SystemFuncName("current_user");
7088 n->agg_star = FALSE;
7089 n->agg_distinct = FALSE;
7094 FuncCall *n = makeNode(FuncCall);
7095 n->funcname = SystemFuncName("current_user");
7097 n->agg_star = FALSE;
7098 n->agg_distinct = FALSE;
7103 FuncCall *n = makeNode(FuncCall);
7104 n->funcname = SystemFuncName("session_user");
7106 n->agg_star = FALSE;
7107 n->agg_distinct = FALSE;
7112 FuncCall *n = makeNode(FuncCall);
7113 n->funcname = SystemFuncName("current_user");
7115 n->agg_star = FALSE;
7116 n->agg_distinct = FALSE;
7119 | CAST '(' a_expr AS Typename ')'
7120 { $$ = makeTypeCast($3, $5); }
7121 | EXTRACT '(' extract_list ')'
7123 FuncCall *n = makeNode(FuncCall);
7124 n->funcname = SystemFuncName("date_part");
7126 n->agg_star = FALSE;
7127 n->agg_distinct = FALSE;
7130 | OVERLAY '(' overlay_list ')'
7132 /* overlay(A PLACING B FROM C FOR D) is converted to
7133 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
7134 * overlay(A PLACING B FROM C) is converted to
7135 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
7137 FuncCall *n = makeNode(FuncCall);
7138 n->funcname = SystemFuncName("overlay");
7140 n->agg_star = FALSE;
7141 n->agg_distinct = FALSE;
7144 | POSITION '(' position_list ')'
7146 /* position(A in B) is converted to position(B, A) */
7147 FuncCall *n = makeNode(FuncCall);
7148 n->funcname = SystemFuncName("position");
7150 n->agg_star = FALSE;
7151 n->agg_distinct = FALSE;
7154 | SUBSTRING '(' substr_list ')'
7156 /* substring(A from B for C) is converted to
7157 * substring(A, B, C) - thomas 2000-11-28
7159 FuncCall *n = makeNode(FuncCall);
7160 n->funcname = SystemFuncName("substring");
7162 n->agg_star = FALSE;
7163 n->agg_distinct = FALSE;
7166 | TREAT '(' a_expr AS Typename ')'
7168 /* TREAT(expr AS target) converts expr of a particular type to target,
7169 * which is defined to be a subtype of the original expression.
7170 * In SQL99, this is intended for use with structured UDTs,
7171 * but let's make this a generally useful form allowing stronger
7172 * coersions than are handled by implicit casting.
7174 FuncCall *n = makeNode(FuncCall);
7175 /* Convert SystemTypeName() to SystemFuncName() even though
7176 * at the moment they result in the same thing.
7178 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
7179 n->args = list_make1($3);
7182 | TRIM '(' BOTH trim_list ')'
7184 /* various trim expressions are defined in SQL92
7185 * - thomas 1997-07-19
7187 FuncCall *n = makeNode(FuncCall);
7188 n->funcname = SystemFuncName("btrim");
7190 n->agg_star = FALSE;
7191 n->agg_distinct = FALSE;
7194 | TRIM '(' LEADING trim_list ')'
7196 FuncCall *n = makeNode(FuncCall);
7197 n->funcname = SystemFuncName("ltrim");
7199 n->agg_star = FALSE;
7200 n->agg_distinct = FALSE;
7203 | TRIM '(' TRAILING trim_list ')'
7205 FuncCall *n = makeNode(FuncCall);
7206 n->funcname = SystemFuncName("rtrim");
7208 n->agg_star = FALSE;
7209 n->agg_distinct = FALSE;
7212 | TRIM '(' trim_list ')'
7214 FuncCall *n = makeNode(FuncCall);
7215 n->funcname = SystemFuncName("btrim");
7217 n->agg_star = FALSE;
7218 n->agg_distinct = FALSE;
7221 | CONVERT '(' a_expr USING any_name ')'
7223 FuncCall *n = makeNode(FuncCall);
7224 A_Const *c = makeNode(A_Const);
7226 c->val.type = T_String;
7227 c->val.val.str = NameListToQuotedString($5);
7229 n->funcname = SystemFuncName("convert_using");
7230 n->args = list_make2($3, c);
7231 n->agg_star = FALSE;
7232 n->agg_distinct = FALSE;
7235 | CONVERT '(' expr_list ')'
7237 FuncCall *n = makeNode(FuncCall);
7238 n->funcname = SystemFuncName("convert");
7240 n->agg_star = FALSE;
7241 n->agg_distinct = FALSE;
7244 | NULLIF '(' a_expr ',' a_expr ')'
7246 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5);
7248 | COALESCE '(' expr_list ')'
7250 CoalesceExpr *c = makeNode(CoalesceExpr);
7254 | GREATEST '(' expr_list ')'
7256 MinMaxExpr *v = makeNode(MinMaxExpr);
7258 v->op = IS_GREATEST;
7261 | LEAST '(' expr_list ')'
7263 MinMaxExpr *v = makeNode(MinMaxExpr);
7271 * Supporting nonterminals for expressions.
7274 /* Explicit row production.
7276 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
7277 * without conflicting with the parenthesized a_expr production. Without the
7278 * ROW keyword, there must be more than one a_expr inside the parens.
7280 row: ROW '(' expr_list ')' { $$ = $3; }
7281 | ROW '(' ')' { $$ = NIL; }
7282 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
7285 sub_type: ANY { $$ = ANY_SUBLINK; }
7286 | SOME { $$ = ANY_SUBLINK; }
7287 | ALL { $$ = ALL_SUBLINK; }
7290 all_Op: Op { $$ = $1; }
7291 | MathOp { $$ = $1; }
7294 MathOp: '+' { $$ = "+"; }
7306 { $$ = list_make1(makeString($1)); }
7307 | OPERATOR '(' any_operator ')'
7313 { $$ = list_make1(makeString($1)); }
7314 | OPERATOR '(' any_operator ')'
7320 { $$ = list_make1(makeString($1)); }
7321 | OPERATOR '(' any_operator ')'
7324 { $$ = list_make1(makeString("~~")); }
7326 { $$ = list_make1(makeString("!~~")); }
7328 { $$ = list_make1(makeString("~~*")); }
7330 { $$ = list_make1(makeString("!~~*")); }
7331 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
7332 * the regular expression is preprocessed by a function (similar_escape),
7333 * and the ~ operator for posix regular expressions is used.
7334 * x SIMILAR TO y -> x ~ similar_escape(y)
7335 * this transformation is made on the fly by the parser upwards.
7336 * however the SubLink structure which handles any/some/all stuff
7337 * is not ready for such a thing.
7343 $$ = list_make1($1);
7345 | expr_list ',' a_expr
7347 $$ = lappend($1, $3);
7352 extract_arg FROM a_expr
7354 A_Const *n = makeNode(A_Const);
7355 n->val.type = T_String;
7356 n->val.val.str = $1;
7357 $$ = list_make2((Node *) n, $3);
7359 | /*EMPTY*/ { $$ = NIL; }
7362 type_list: type_list ',' Typename
7364 $$ = lappend($1, $3);
7368 $$ = list_make1($1);
7372 array_expr_list: array_expr
7373 { $$ = list_make1($1); }
7374 | array_expr_list ',' array_expr
7375 { $$ = lappend($1, $3); }
7378 array_expr: '[' expr_list ']'
7380 ArrayExpr *n = makeNode(ArrayExpr);
7384 | '[' array_expr_list ']'
7386 ArrayExpr *n = makeNode(ArrayExpr);
7392 /* Allow delimited string SCONST in extract_arg as an SQL extension.
7393 * - thomas 2001-04-12
7398 | YEAR_P { $$ = "year"; }
7399 | MONTH_P { $$ = "month"; }
7400 | DAY_P { $$ = "day"; }
7401 | HOUR_P { $$ = "hour"; }
7402 | MINUTE_P { $$ = "minute"; }
7403 | SECOND_P { $$ = "second"; }
7404 | SCONST { $$ = $1; }
7407 /* OVERLAY() arguments
7408 * SQL99 defines the OVERLAY() function:
7409 * o overlay(text placing text from int for int)
7410 * o overlay(text placing text from int)
7413 a_expr overlay_placing substr_from substr_for
7415 $$ = list_make4($1, $2, $3, $4);
7417 | a_expr overlay_placing substr_from
7419 $$ = list_make3($1, $2, $3);
7428 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
7431 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
7432 | /*EMPTY*/ { $$ = NIL; }
7435 /* SUBSTRING() arguments
7436 * SQL9x defines a specific syntax for arguments to SUBSTRING():
7437 * o substring(text from int for int)
7438 * o substring(text from int) get entire string from starting point "int"
7439 * o substring(text from pattern) get entire string matching pattern
7440 * o substring(text for int) get first "int" characters of string
7441 * We also want to implement generic substring functions which accept
7442 * the usual generic list of arguments. So we will accept both styles
7443 * here, and convert the SQL9x style to the generic list for further
7444 * processing. - thomas 2000-11-28
7447 a_expr substr_from substr_for
7449 $$ = list_make3($1, $2, $3);
7451 | a_expr substr_for substr_from
7453 $$ = list_make3($1, $3, $2);
7455 | a_expr substr_from
7457 $$ = list_make2($1, $2);
7461 A_Const *n = makeNode(A_Const);
7462 n->val.type = T_Integer;
7463 n->val.val.ival = 1;
7464 $$ = list_make3($1, (Node *)n, $2);
7475 FROM a_expr { $$ = $2; }
7478 substr_for: FOR a_expr { $$ = $2; }
7481 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
7482 | FROM expr_list { $$ = $2; }
7483 | expr_list { $$ = $1; }
7486 in_expr: select_with_parens
7488 SubLink *n = makeNode(SubLink);
7490 /* other fields will be filled later */
7493 | '(' expr_list ')' { $$ = (Node *)$2; }
7497 * Define SQL92-style case clause.
7498 * - Full specification
7499 * CASE WHEN a = b THEN c ... ELSE d END
7500 * - Implicit argument
7501 * CASE a WHEN b THEN c ... ELSE d END
7503 case_expr: CASE case_arg when_clause_list case_default END_P
7505 CaseExpr *c = makeNode(CaseExpr);
7506 c->casetype = InvalidOid; /* not analyzed yet */
7507 c->arg = (Expr *) $2;
7509 c->defresult = (Expr *) $4;
7515 /* There must be at least one */
7516 when_clause { $$ = list_make1($1); }
7517 | when_clause_list when_clause { $$ = lappend($1, $2); }
7521 WHEN a_expr THEN a_expr
7523 CaseWhen *w = makeNode(CaseWhen);
7524 w->expr = (Expr *) $2;
7525 w->result = (Expr *) $4;
7531 ELSE a_expr { $$ = $2; }
7532 | /*EMPTY*/ { $$ = NULL; }
7535 case_arg: a_expr { $$ = $1; }
7536 | /*EMPTY*/ { $$ = NULL; }
7540 * columnref starts with relation_name not ColId, so that OLD and NEW
7541 * references can be accepted. Note that when there are more than two
7542 * dotted names, the first name is not actually a relation name...
7544 columnref: relation_name
7546 $$ = makeColumnRef($1, NIL);
7548 | relation_name indirection
7550 $$ = makeColumnRef($1, $2);
7557 $$ = (Node *) makeString($2);
7561 $$ = (Node *) makeString("*");
7565 A_Indices *ai = makeNode(A_Indices);
7570 | '[' a_expr ':' a_expr ']'
7572 A_Indices *ai = makeNode(A_Indices);
7580 indirection_el { $$ = list_make1($1); }
7581 | indirection indirection_el { $$ = lappend($1, $2); }
7585 /*EMPTY*/ { $$ = NIL; }
7586 | opt_indirection indirection_el { $$ = lappend($1, $2); }
7589 opt_asymmetric: ASYMMETRIC
7594 /*****************************************************************************
7596 * target lists for SELECT, UPDATE, INSERT
7598 *****************************************************************************/
7601 target_el { $$ = list_make1($1); }
7602 | target_list ',' target_el { $$ = lappend($1, $3); }
7605 /* AS is not optional because shift/red conflict with unary ops */
7606 target_el: a_expr AS ColLabel
7608 $$ = makeNode(ResTarget);
7610 $$->indirection = NIL;
7611 $$->val = (Node *)$1;
7615 $$ = makeNode(ResTarget);
7617 $$->indirection = NIL;
7618 $$->val = (Node *)$1;
7622 ColumnRef *n = makeNode(ColumnRef);
7623 n->fields = list_make1(makeString("*"));
7625 $$ = makeNode(ResTarget);
7627 $$->indirection = NIL;
7628 $$->val = (Node *)n;
7633 update_target_el { $$ = list_make1($1); }
7634 | update_target_list ',' update_target_el { $$ = lappend($1,$3); }
7638 ColId opt_indirection '=' a_expr
7640 $$ = makeNode(ResTarget);
7642 $$->indirection = $2;
7643 $$->val = (Node *) $4;
7645 | ColId opt_indirection '=' DEFAULT
7647 $$ = makeNode(ResTarget);
7649 $$->indirection = $2;
7650 $$->val = (Node *) makeNode(SetToDefault);
7656 insert_target_el { $$ = list_make1($1); }
7657 | insert_target_list ',' insert_target_el { $$ = lappend($1, $3); }
7663 $$ = makeNode(ResTarget);
7665 $$->indirection = NIL;
7666 $$->val = (Node *)$1;
7670 $$ = makeNode(ResTarget);
7672 $$->indirection = NIL;
7673 $$->val = (Node *) makeNode(SetToDefault);
7678 /*****************************************************************************
7680 * Names and constants
7682 *****************************************************************************/
7685 SpecialRuleRelation { $$ = $1; }
7686 | ColId { $$ = $1; }
7689 qualified_name_list:
7690 qualified_name { $$ = list_make1($1); }
7691 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
7695 * The production for a qualified relation name has to exactly match the
7696 * production for a qualified func_name, because in a FROM clause we cannot
7697 * tell which we are parsing until we see what comes after it ('(' for a
7698 * func_name, something else for a relation). Therefore we allow 'indirection'
7699 * which may contain subscripts, and reject that case in the C code.
7704 $$ = makeNode(RangeVar);
7705 $$->catalogname = NULL;
7706 $$->schemaname = NULL;
7709 | relation_name indirection
7711 check_qualified_name($2);
7712 $$ = makeNode(RangeVar);
7713 switch (list_length($2))
7716 $$->catalogname = NULL;
7717 $$->schemaname = $1;
7718 $$->relname = strVal(linitial($2));
7721 $$->catalogname = $1;
7722 $$->schemaname = strVal(linitial($2));
7723 $$->relname = strVal(lsecond($2));
7727 (errcode(ERRCODE_SYNTAX_ERROR),
7728 errmsg("improper qualified name (too many dotted names): %s",
7729 NameListToString(lcons(makeString($1), $2)))));
7736 { $$ = list_make1(makeString($1)); }
7737 | name_list ',' name
7738 { $$ = lappend($1, makeString($3)); }
7742 name: ColId { $$ = $1; };
7750 attr_name: ColLabel { $$ = $1; };
7752 index_name: ColId { $$ = $1; };
7754 file_name: Sconst { $$ = $1; };
7757 * The production for a qualified func_name has to exactly match the
7758 * production for a qualified columnref, because we cannot tell which we
7759 * are parsing until we see what comes after it ('(' for a func_name,
7760 * anything else for a columnref). Therefore we allow 'indirection' which
7761 * may contain subscripts, and reject that case in the C code. (If we
7762 * ever implement SQL99-like methods, such syntax may actually become legal!)
7764 func_name: function_name
7765 { $$ = list_make1(makeString($1)); }
7766 | relation_name indirection
7767 { $$ = check_func_name(lcons(makeString($1), $2)); }
7776 A_Const *n = makeNode(A_Const);
7777 n->val.type = T_Integer;
7778 n->val.val.ival = $1;
7783 A_Const *n = makeNode(A_Const);
7784 n->val.type = T_Float;
7785 n->val.val.str = $1;
7790 A_Const *n = makeNode(A_Const);
7791 n->val.type = T_String;
7792 n->val.val.str = $1;
7797 A_Const *n = makeNode(A_Const);
7798 n->val.type = T_BitString;
7799 n->val.val.str = $1;
7804 /* This is a bit constant per SQL99:
7805 * Without Feature F511, "BIT data type",
7806 * a <general literal> shall not be a
7807 * <bit string literal> or a <hex string literal>.
7809 A_Const *n = makeNode(A_Const);
7810 n->val.type = T_BitString;
7811 n->val.val.str = $1;
7814 | ConstTypename Sconst
7816 A_Const *n = makeNode(A_Const);
7818 n->val.type = T_String;
7819 n->val.val.str = $2;
7822 | ConstInterval Sconst opt_interval
7824 A_Const *n = makeNode(A_Const);
7826 n->val.type = T_String;
7827 n->val.val.str = $2;
7828 /* precision is not specified, but fields may be... */
7829 if ($3 != INTERVAL_FULL_RANGE)
7830 n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
7833 | ConstInterval '(' Iconst ')' Sconst opt_interval
7835 A_Const *n = makeNode(A_Const);
7837 n->val.type = T_String;
7838 n->val.val.str = $5;
7839 /* precision specified, and fields may be... */
7842 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7843 errmsg("INTERVAL(%d) precision must not be negative",
7845 if ($3 > MAX_INTERVAL_PRECISION)
7848 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7849 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
7850 $3, MAX_INTERVAL_PRECISION)));
7851 $3 = MAX_INTERVAL_PRECISION;
7853 n->typename->typmod = INTERVAL_TYPMOD($3, $6);
7858 $$ = (Node *)makeBoolAConst(TRUE);
7862 $$ = (Node *)makeBoolAConst(FALSE);
7866 A_Const *n = makeNode(A_Const);
7867 n->val.type = T_Null;
7872 Iconst: ICONST { $$ = $1; };
7873 Sconst: SCONST { $$ = $1; };
7874 RoleId: ColId { $$ = $1; };
7877 * Name classification hierarchy.
7879 * IDENT is the lexeme returned by the lexer for identifiers that match
7880 * no known keyword. In most cases, we can accept certain keywords as
7881 * names, not only IDENTs. We prefer to accept as many such keywords
7882 * as possible to minimize the impact of "reserved words" on programmers.
7883 * So, we divide names into several possible classes. The classification
7884 * is chosen in part to make keywords acceptable as names wherever possible.
7887 /* Column identifier --- names that can be column, table, etc names.
7889 ColId: IDENT { $$ = $1; }
7890 | unreserved_keyword { $$ = pstrdup($1); }
7891 | col_name_keyword { $$ = pstrdup($1); }
7894 /* Type identifier --- names that can be type names.
7896 type_name: IDENT { $$ = $1; }
7897 | unreserved_keyword { $$ = pstrdup($1); }
7900 /* Function identifier --- names that can be function names.
7904 | unreserved_keyword { $$ = pstrdup($1); }
7905 | func_name_keyword { $$ = pstrdup($1); }
7908 /* Column label --- allowed labels in "AS" clauses.
7909 * This presently includes *all* Postgres keywords.
7911 ColLabel: IDENT { $$ = $1; }
7912 | unreserved_keyword { $$ = pstrdup($1); }
7913 | col_name_keyword { $$ = pstrdup($1); }
7914 | func_name_keyword { $$ = pstrdup($1); }
7915 | reserved_keyword { $$ = pstrdup($1); }
7920 * Keyword classification lists. Generally, every keyword present in
7921 * the Postgres grammar should appear in exactly one of these lists.
7923 * Put a new keyword into the first list that it can go into without causing
7924 * shift or reduce conflicts. The earlier lists define "less reserved"
7925 * categories of keywords.
7928 /* "Unreserved" keywords --- available for use as any kind of name.
8134 /* Column identifier --- keywords that can be column, table, etc names.
8136 * Many of these keywords will in fact be recognized as type or function
8137 * names too; but they have special productions for the purpose, and so
8138 * can't be treated as "generic" type or function names.
8140 * The type names appearing here are not usable as function names
8141 * because they can be followed by '(' in typename productions, which
8142 * looks too much like a function call for an LR(1) parser.
8184 /* Function identifier --- keywords that can be function names.
8186 * Most of these are keywords that are used as operators in expressions;
8187 * in general such keywords can't be column names because they would be
8188 * ambiguous with variables, but they are unambiguous as function identifiers.
8190 * Do not include POSITION, SUBSTRING, etc here since they have explicit
8191 * productions in a_expr to support the goofy SQL9x argument syntax.
8192 * - thomas 2000-11-28
8217 /* Reserved keyword --- these keywords are usable only as a ColLabel.
8219 * Keywords appear here if they could not be distinguished from variable,
8220 * type, or function names in some contexts. Don't put things here unless
8300 SpecialRuleRelation:
8307 (errcode(ERRCODE_SYNTAX_ERROR),
8308 errmsg("OLD used in query that is not in a rule")));
8316 (errcode(ERRCODE_SYNTAX_ERROR),
8317 errmsg("NEW used in query that is not in a rule")));
8324 makeColumnRef(char *relname, List *indirection)
8327 * Generate a ColumnRef node, with an A_Indirection node added if there
8328 * is any subscripting in the specified indirection list. However,
8329 * any field selection at the start of the indirection list must be
8330 * transposed into the "fields" part of the ColumnRef node.
8332 ColumnRef *c = makeNode(ColumnRef);
8336 foreach(l, indirection)
8338 if (IsA(lfirst(l), A_Indices))
8340 A_Indirection *i = makeNode(A_Indirection);
8344 /* easy case - all indirection goes to A_Indirection */
8345 c->fields = list_make1(makeString(relname));
8346 i->indirection = indirection;
8350 /* got to split the list in two */
8351 i->indirection = list_copy_tail(indirection, nfields);
8352 indirection = list_truncate(indirection, nfields);
8353 c->fields = lcons(makeString(relname), indirection);
8355 i->arg = (Node *) c;
8360 /* No subscripting, so all indirection gets added to field list */
8361 c->fields = lcons(makeString(relname), indirection);
8366 makeTypeCast(Node *arg, TypeName *typename)
8369 * Simply generate a TypeCast node.
8371 * Earlier we would determine whether an A_Const would
8372 * be acceptable, however Domains require coerce_type()
8373 * to process them -- applying constraints as required.
8375 TypeCast *n = makeNode(TypeCast);
8377 n->typename = typename;
8382 makeStringConst(char *str, TypeName *typename)
8384 A_Const *n = makeNode(A_Const);
8386 n->val.type = T_String;
8387 n->val.val.str = str;
8388 n->typename = typename;
8394 makeIntConst(int val)
8396 A_Const *n = makeNode(A_Const);
8397 n->val.type = T_Integer;
8398 n->val.val.ival = val;
8399 n->typename = SystemTypeName("int4");
8405 makeFloatConst(char *str)
8407 A_Const *n = makeNode(A_Const);
8409 n->val.type = T_Float;
8410 n->val.val.str = str;
8411 n->typename = SystemTypeName("float8");
8417 makeAConst(Value *v)
8424 n = makeFloatConst(v->val.str);
8428 n = makeIntConst(v->val.ival);
8433 n = makeStringConst(v->val.str, NULL);
8441 * Create a DefElem node and set contents.
8442 * Could be moved to nodes/makefuncs.c if this is useful elsewhere.
8445 makeDefElem(char *name, Node *arg)
8447 DefElem *f = makeNode(DefElem);
8454 * Create an A_Const node and initialize to a boolean constant.
8457 makeBoolAConst(bool state)
8459 A_Const *n = makeNode(A_Const);
8460 n->val.type = T_String;
8461 n->val.val.str = (state? "t": "f");
8462 n->typename = SystemTypeName("bool");
8466 /* makeRowNullTest()
8467 * Generate separate operator nodes for a single row descriptor test.
8469 * Eventually this should be eliminated in favor of making the NullTest
8470 * node type capable of handling it directly.
8473 makeRowNullTest(NullTestType test, RowExpr *row)
8475 Node *result = NULL;
8478 foreach(arg, row->args)
8482 n = makeNode(NullTest);
8483 n->arg = (Expr *) lfirst(arg);
8484 n->nulltesttype = test;
8487 result = (Node *) n;
8488 else if (test == IS_NOT_NULL)
8489 result = (Node *) makeA_Expr(AEXPR_OR, NIL, result, (Node *)n);
8491 result = (Node *) makeA_Expr(AEXPR_AND, NIL, result, (Node *)n);
8496 /* zero-length rows? Generate constant TRUE or FALSE */
8497 result = (Node *) makeBoolAConst(test == IS_NULL);
8504 * Create and populate a FuncCall node to support the OVERLAPS operator.
8507 makeOverlaps(List *largs, List *rargs)
8509 FuncCall *n = makeNode(FuncCall);
8510 n->funcname = SystemFuncName("overlaps");
8511 if (list_length(largs) == 1)
8512 largs = lappend(largs, largs);
8513 else if (list_length(largs) != 2)
8515 (errcode(ERRCODE_SYNTAX_ERROR),
8516 errmsg("wrong number of parameters on left side of OVERLAPS expression")));
8517 if (list_length(rargs) == 1)
8518 rargs = lappend(rargs, rargs);
8519 else if (list_length(rargs) != 2)
8521 (errcode(ERRCODE_SYNTAX_ERROR),
8522 errmsg("wrong number of parameters on right side of OVERLAPS expression")));
8523 n->args = list_concat(largs, rargs);
8524 n->agg_star = FALSE;
8525 n->agg_distinct = FALSE;
8529 /* check_qualified_name --- check the result of qualified_name production
8531 * It's easiest to let the grammar production for qualified_name allow
8532 * subscripts and '*', which we then must reject here.
8535 check_qualified_name(List *names)
8541 if (!IsA(lfirst(i), String))
8542 yyerror("syntax error");
8543 else if (strcmp(strVal(lfirst(i)), "*") == 0)
8544 yyerror("syntax error");
8548 /* check_func_name --- check the result of func_name production
8550 * It's easiest to let the grammar production for func_name allow subscripts
8551 * and '*', which we then must reject here.
8554 check_func_name(List *names)
8560 if (!IsA(lfirst(i), String))
8561 yyerror("syntax error");
8562 else if (strcmp(strVal(lfirst(i)), "*") == 0)
8563 yyerror("syntax error");
8568 /* extractArgTypes()
8569 * Given a list of FunctionParameter nodes, extract a list of just the
8570 * argument types (TypeNames) for input parameters only. This is what
8571 * is needed to look up an existing function, which is what is wanted by
8572 * the productions that use this call.
8575 extractArgTypes(List *parameters)
8580 foreach(i, parameters)
8582 FunctionParameter *p = (FunctionParameter *) lfirst(i);
8584 if (p->mode != FUNC_PARAM_OUT) /* keep if IN or INOUT */
8585 result = lappend(result, p->argType);
8590 /* findLeftmostSelect()
8591 * Find the leftmost component SelectStmt in a set-operation parsetree.
8594 findLeftmostSelect(SelectStmt *node)
8596 while (node && node->op != SETOP_NONE)
8598 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
8602 /* insertSelectOptions()
8603 * Insert ORDER BY, etc into an already-constructed SelectStmt.
8605 * This routine is just to avoid duplicating code in SelectStmt productions.
8608 insertSelectOptions(SelectStmt *stmt,
8609 List *sortClause, List *lockingClause,
8610 Node *limitOffset, Node *limitCount)
8613 * Tests here are to reject constructs like
8614 * (SELECT foo ORDER BY bar) ORDER BY baz
8618 if (stmt->sortClause)
8620 (errcode(ERRCODE_SYNTAX_ERROR),
8621 errmsg("multiple ORDER BY clauses not allowed")));
8622 stmt->sortClause = sortClause;
8628 if (stmt->lockedRels)
8630 (errcode(ERRCODE_SYNTAX_ERROR),
8631 errmsg("multiple FOR UPDATE/FOR SHARE clauses not allowed")));
8633 Assert(list_length(lockingClause) > 1);
8634 /* 1st is Value node containing "for_update" or "for_share" */
8635 type = (Value *) linitial(lockingClause);
8636 Assert(IsA(type, String));
8637 if (strcmp(strVal(type), "for_update") == 0)
8638 stmt->forUpdate = true;
8639 else if (strcmp(strVal(type), "for_share") == 0)
8640 stmt->forUpdate = false;
8642 elog(ERROR, "invalid first node in locking clause");
8644 stmt->lockedRels = list_delete_first(lockingClause);
8648 if (stmt->limitOffset)
8650 (errcode(ERRCODE_SYNTAX_ERROR),
8651 errmsg("multiple OFFSET clauses not allowed")));
8652 stmt->limitOffset = limitOffset;
8656 if (stmt->limitCount)
8658 (errcode(ERRCODE_SYNTAX_ERROR),
8659 errmsg("multiple LIMIT clauses not allowed")));
8660 stmt->limitCount = limitCount;
8665 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
8667 SelectStmt *n = makeNode(SelectStmt);
8671 n->larg = (SelectStmt *) larg;
8672 n->rarg = (SelectStmt *) rarg;
8677 * Build a properly-qualified reference to a built-in function.
8680 SystemFuncName(char *name)
8682 return list_make2(makeString("pg_catalog"), makeString(name));
8686 * Build a properly-qualified reference to a built-in type.
8688 * typmod is defaulted, but may be changed afterwards by caller.
8691 SystemTypeName(char *name)
8693 TypeName *n = makeNode(TypeName);
8695 n->names = list_make2(makeString("pg_catalog"), makeString(name));
8701 * Initialize to parse one query string
8706 QueryIsRule = FALSE;
8709 /* exprIsNullConstant()
8710 * Test whether an a_expr is a plain NULL constant or not.
8713 exprIsNullConstant(Node *arg)
8715 if (arg && IsA(arg, A_Const))
8717 A_Const *con = (A_Const *) arg;
8719 if (con->val.type == T_Null &&
8720 con->typename == NULL)
8727 * Handle negation of a numeric constant.
8729 * Formerly, we did this here because the optimizer couldn't cope with
8730 * indexquals that looked like "var = -4" --- it wants "var = const"
8731 * and a unary minus operator applied to a constant didn't qualify.
8732 * As of Postgres 7.0, that problem doesn't exist anymore because there
8733 * is a constant-subexpression simplifier in the optimizer. However,
8734 * there's still a good reason for doing this here, which is that we can
8735 * postpone committing to a particular internal representation for simple
8736 * negative constants. It's better to leave "-123.456" in string form
8737 * until we know what the desired type is.
8742 if (IsA(n, A_Const))
8744 A_Const *con = (A_Const *)n;
8746 if (con->val.type == T_Integer)
8748 con->val.val.ival = -con->val.val.ival;
8751 if (con->val.type == T_Float)
8753 doNegateFloat(&con->val);
8758 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n);
8762 doNegateFloat(Value *v)
8764 char *oldval = v->val.str;
8766 Assert(IsA(v, Float));
8770 v->val.str = oldval+1; /* just strip the '-' */
8773 char *newval = (char *) palloc(strlen(oldval) + 2);
8776 strcpy(newval+1, oldval);
8777 v->val.str = newval;