4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2008, 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.609 2008/03/20 21:42:48 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 during parse analysis 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 "commands/defrem.h"
57 #include "nodes/makefuncs.h"
58 #include "parser/gramparse.h"
59 #include "storage/lmgr.h"
60 #include "utils/date.h"
61 #include "utils/datetime.h"
62 #include "utils/numeric.h"
63 #include "utils/xml.h"
66 /* Location tracking support --- simpler than bison's default */
67 #define YYLLOC_DEFAULT(Current, Rhs, N) \
70 (Current) = (Rhs)[1]; \
72 (Current) = (Rhs)[0]; \
76 * The %name-prefix option below will make bison call base_yylex, but we
77 * really want it to call filtered_base_yylex (see parser.c).
79 #define base_yylex filtered_base_yylex
81 extern List *parsetree; /* final parse result is delivered here */
83 static bool QueryIsRule = FALSE;
86 * If you need access to certain yacc-generated variables and find that
87 * they're static by default, uncomment the next line. (this is not a
90 /*#define __YYSCLASS*/
92 static Node *makeColumnRef(char *relname, List *indirection, int location);
93 static Node *makeTypeCast(Node *arg, TypeName *typename);
94 static Node *makeStringConst(char *str, TypeName *typename);
95 static Node *makeIntConst(int val);
96 static Node *makeFloatConst(char *str);
97 static Node *makeAConst(Value *v);
98 static A_Const *makeBoolAConst(bool state);
99 static FuncCall *makeOverlaps(List *largs, List *rargs, int location);
100 static void check_qualified_name(List *names);
101 static List *check_func_name(List *names);
102 static List *extractArgTypes(List *parameters);
103 static SelectStmt *findLeftmostSelect(SelectStmt *node);
104 static void insertSelectOptions(SelectStmt *stmt,
105 List *sortClause, List *lockingClause,
106 Node *limitOffset, Node *limitCount);
107 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
108 static Node *doNegate(Node *n, int location);
109 static void doNegateFloat(Value *v);
110 static Node *makeAArrayExpr(List *elements);
111 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args);
115 %name-prefix="base_yy"
126 DropBehavior dbehavior;
127 OnCommitAction oncommit;
134 FunctionParameter *fun_param;
135 FunctionParameterMode fun_param_mode;
136 FuncWithArgs *funwithargs;
146 PrivTarget *privtarget;
149 VariableSetStmt *vsetstmt;
152 %type <node> stmt schema_stmt
153 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
154 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
155 AlterUserStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
156 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
157 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
158 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
159 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
160 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
161 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
162 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
163 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
164 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
165 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
166 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
167 LockStmt NotifyStmt ExplainableStmt PreparableStmt
168 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
169 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
170 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
171 SelectStmt TransactionStmt TruncateStmt
172 UnlistenStmt UpdateStmt VacuumStmt
173 VariableResetStmt VariableSetStmt VariableShowStmt
174 ViewStmt CheckPointStmt CreateConversionStmt
175 DeallocateStmt PrepareStmt ExecuteStmt
176 DropOwnedStmt ReassignOwnedStmt
177 AlterTSConfigurationStmt AlterTSDictionaryStmt
179 %type <node> select_no_parens select_with_parens select_clause
180 simple_select values_clause
182 %type <node> alter_column_default opclass_item opclass_drop alter_using
183 %type <ival> add_drop opt_asc_desc opt_nulls_order
185 %type <node> alter_table_cmd alter_rel_cmd
186 %type <list> alter_table_cmds alter_rel_cmds
188 %type <dbehavior> opt_drop_behavior
190 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
191 transaction_mode_list
192 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
193 transaction_mode_item
195 %type <ival> opt_lock lock_type cast_context
196 %type <boolean> opt_force opt_or_replace
197 opt_grant_grant_option opt_grant_admin_option
198 opt_nowait opt_if_exists
200 %type <list> OptRoleList
201 %type <defelt> OptRoleElem
203 %type <str> OptSchemaName
204 %type <list> OptSchemaEltList
206 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
207 %type <str> opt_lancompiler
209 %type <str> TriggerEvents
210 %type <value> TriggerFuncArg
212 %type <str> relation_name copy_file_name
213 database_name access_method_clause access_method attr_name
214 index_name name file_name cluster_index_specification
216 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
217 opt_class opt_validator
219 %type <range> qualified_name OptConstrFromTable
221 %type <str> all_Op MathOp SpecialRuleRelation
223 %type <str> iso_level opt_encoding
225 %type <list> grantee_list
226 %type <str> privilege
227 %type <list> privileges privilege_list
228 %type <privtarget> privilege_target
229 %type <funwithargs> function_with_argtypes
230 %type <list> function_with_argtypes_list
231 %type <chr> TriggerOneEvent
233 %type <list> stmtblock stmtmulti
234 OptTableElementList TableElementList OptInherit definition
235 OptWith opt_distinct opt_definition func_args func_args_list
236 func_as createfunc_opt_list alterfunc_opt_list
237 aggr_args old_aggr_definition old_aggr_list
238 oper_argtypes RuleActionList RuleActionMulti
239 opt_column_list columnList opt_name_list
240 sort_clause opt_sort_clause sortby_list index_params
241 name_list from_clause from_list opt_array_bounds
242 qualified_name_list any_name any_name_list
243 any_operator expr_list attrs
244 target_list insert_column_list set_target_list
245 set_clause_list set_clause multiple_set_clause
246 ctext_expr_list ctext_row def_list indirection opt_indirection
247 group_clause TriggerFuncArgs select_limit
248 opt_select_limit opclass_item_list opclass_drop_list
249 opt_opfamily transaction_mode_list_or_empty
250 TableFuncElementList opt_type_modifiers
252 execute_param_clause using_clause returning_clause
255 %type <range> OptTempTableName
256 %type <into> into_clause create_as_target
258 %type <defelt> createfunc_opt_item common_func_opt_item
259 %type <fun_param> func_arg
260 %type <fun_param_mode> arg_class
261 %type <typnam> func_return func_type
263 %type <boolean> TriggerForType OptTemp
264 %type <oncommit> OnCommitOption
266 %type <node> for_locking_item
267 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
268 %type <list> locked_rels_list
269 %type <boolean> opt_all
271 %type <node> join_outer join_qual
272 %type <jtype> join_type
274 %type <list> extract_list overlay_list position_list
275 %type <list> substr_list trim_list
276 %type <ival> opt_interval
277 %type <node> overlay_placing substr_from substr_for
279 %type <boolean> opt_instead opt_analyze
280 %type <boolean> index_opt_unique opt_verbose opt_full
281 %type <boolean> opt_freeze opt_default opt_recheck
282 %type <defelt> opt_binary opt_oids copy_delimiter
284 %type <boolean> copy_from
286 %type <ival> opt_column event cursor_options opt_hold
287 %type <objtype> reindex_type drop_type comment_type
289 %type <node> fetch_direction select_limit_value select_offset_value
291 %type <list> OptSeqList
292 %type <defelt> OptSeqElem
294 %type <istmt> insert_rest
296 %type <vsetstmt> set_rest SetResetClause
298 %type <node> TableElement ConstraintElem TableFuncElement
299 %type <node> columnDef
300 %type <defelt> def_elem old_aggr_elem
301 %type <node> def_arg columnElem where_clause where_or_current_clause
302 a_expr b_expr c_expr func_expr AexprConst indirection_el
303 columnref in_expr having_clause func_table array_expr
304 %type <list> row type_list array_expr_list
305 %type <node> case_expr case_arg when_clause case_default
306 %type <list> when_clause_list
307 %type <ival> sub_type
308 %type <list> OptCreateAs CreateAsList
309 %type <node> CreateAsElement ctext_expr
310 %type <value> NumericOnly FloatOnly IntegerOnly
311 %type <alias> alias_clause
312 %type <sortby> sortby
313 %type <ielem> index_elem
314 %type <node> table_ref
315 %type <jexpr> joined_table
316 %type <range> relation_expr
317 %type <range> relation_expr_opt_alias
318 %type <target> target_el single_set_clause set_target insert_column_item
320 %type <typnam> Typename SimpleTypename ConstTypename
321 GenericType Numeric opt_float
322 Character ConstCharacter
323 CharacterWithLength CharacterWithoutLength
324 ConstDatetime ConstInterval
325 Bit ConstBit BitWithLength BitWithoutLength
326 %type <str> character
327 %type <str> extract_arg
328 %type <str> opt_charset
329 %type <boolean> opt_varying opt_timezone
331 %type <ival> Iconst SignedIconst
332 %type <str> Sconst comment_text
333 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
334 %type <list> var_list
335 %type <str> ColId ColLabel var_name type_function_name param_name
336 %type <node> var_value zone_value
338 %type <keyword> unreserved_keyword type_func_name_keyword
339 %type <keyword> col_name_keyword reserved_keyword
341 %type <node> TableConstraint TableLikeClause
342 %type <list> TableLikeOptionList
343 %type <ival> TableLikeOption
344 %type <list> ColQualList
345 %type <node> ColConstraint ColConstraintElem ConstraintAttr
346 %type <ival> key_actions key_delete key_match key_update key_action
347 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
350 %type <list> constraints_set_list
351 %type <boolean> constraints_set_mode
352 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
353 %type <list> opt_check_option
355 %type <target> xml_attribute_el
356 %type <list> xml_attribute_list xml_attributes
357 %type <node> xml_root_version opt_xml_root_standalone
358 %type <ival> document_or_content
359 %type <boolean> xml_whitespace_option
363 * If you make any token changes, update the keyword table in
364 * parser/keywords.c and add new keywords to the appropriate one of
365 * the reserved-or-not-so-reserved keyword lists, below; search
366 * this file for "Name classification hierarchy".
369 /* ordinary key words in alphabetical order */
370 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
371 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
372 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
374 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
377 CACHE CALLED CASCADE CASCADED CASE CAST CHAIN CHAR_P
378 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
379 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
380 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
381 CONTENT_P CONVERSION_P COPY COST CREATE CREATEDB
382 CREATEROLE CREATEUSER CROSS CSV CURRENT_P CURRENT_DATE CURRENT_ROLE
383 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
385 DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
386 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
387 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
389 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
390 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
392 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
393 FREEZE FROM FULL FUNCTION
395 GLOBAL GRANT GRANTED GREATEST GROUP_P
397 HANDLER HAVING HEADER_P HOLD HOUR_P
399 IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCLUDING INCREMENT
400 INDEX INDEXES INHERIT INHERITS INITIALLY INNER_P INOUT INPUT_P
401 INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT
402 INTERVAL INTO INVOKER IS ISNULL ISOLATION
408 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
409 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
412 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
414 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
415 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
416 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
418 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
419 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
421 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
422 PRECISION PRESERVE PREPARE PREPARED PRIMARY
423 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
427 READ REAL REASSIGN RECHECK REFERENCES REINDEX RELATIVE_P RELEASE RENAME
428 REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS REVOKE
429 RIGHT ROLE ROLLBACK ROW ROWS RULE
431 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
432 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
433 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
434 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
435 SYMMETRIC SYSID SYSTEM_P
437 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
438 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
439 TRUNCATE TRUSTED TYPE_P
441 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
444 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARYING
445 VERBOSE VERSION_P VIEW VOLATILE
447 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRITE
449 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
450 XMLPI XMLROOT XMLSERIALIZE
456 /* The grammar thinks these are keywords, but they are not in the keywords.c
457 * list and so can never be entered directly. The filter in parser.c
458 * creates these tokens when required.
460 %token NULLS_FIRST NULLS_LAST WITH_CASCADED WITH_LOCAL WITH_CHECK
462 /* Special token types, not actually keywords - see the "lex" file */
463 %token <str> IDENT FCONST SCONST BCONST XCONST Op
464 %token <ival> ICONST PARAM
466 /* precedence: lowest to highest */
467 %nonassoc SET /* see relation_expr_opt_alias */
475 %nonassoc LIKE ILIKE SIMILAR
480 %left POSTFIXOP /* dummy for postfix Op rules */
481 %nonassoc IDENT /* to support target_el without AS */
482 %left Op OPERATOR /* multi-character ops and user-defined operators */
485 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
489 /* Unary Operators */
490 %left AT ZONE /* sets precedence for AT TIME ZONE */
497 * These might seem to be low-precedence, but actually they are not part
498 * of the arithmetic hierarchy at all in their use as JOIN operators.
499 * We make them high-precedence to support their use as function names.
500 * They wouldn't be given a precedence at all, were it not that we need
501 * left-associativity among the JOIN rules themselves.
503 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
504 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
505 %right PRESERVE STRIP_P
509 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
510 * psql already handles such cases, but other interfaces don't.
513 stmtblock: stmtmulti { parsetree = $1; }
516 /* the thrashing around here is to discard "empty" statements... */
517 stmtmulti: stmtmulti ';' stmt
519 $$ = lappend($1, $3);
533 | AlterDatabaseSetStmt
537 | AlterObjectSchemaStmt
543 | AlterTSConfigurationStmt
544 | AlterTSDictionaryStmt
557 | CreateConversionStmt
568 | CreateTableSpaceStmt
627 /*****************************************************************************
629 * Create a new Postgres DBMS role
631 *****************************************************************************/
634 CREATE ROLE RoleId opt_with OptRoleList
636 CreateRoleStmt *n = makeNode(CreateRoleStmt);
637 n->stmt_type = ROLESTMT_ROLE;
650 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
651 * for backwards compatibility). Note: the only option required by SQL99
652 * is "WITH ADMIN name".
655 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
656 | /* EMPTY */ { $$ = NIL; }
662 $$ = makeDefElem("password",
663 (Node *)makeString($2));
667 $$ = makeDefElem("password", NULL);
669 | ENCRYPTED PASSWORD Sconst
671 $$ = makeDefElem("encryptedPassword",
672 (Node *)makeString($3));
674 | UNENCRYPTED PASSWORD Sconst
676 $$ = makeDefElem("unencryptedPassword",
677 (Node *)makeString($3));
681 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
685 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
689 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
693 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
697 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
701 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
705 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
709 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
713 /* For backwards compatibility, synonym for SUPERUSER */
714 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
718 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
722 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
726 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
728 | CONNECTION LIMIT SignedIconst
730 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
734 $$ = makeDefElem("validUntil", (Node *)makeString($3));
736 /* Supported but not documented for roles, for use by ALTER GROUP. */
739 $$ = makeDefElem("rolemembers", (Node *)$2);
741 /* The following are not supported by ALTER ROLE/USER/GROUP */
744 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
748 $$ = makeDefElem("adminmembers", (Node *)$2);
752 $$ = makeDefElem("rolemembers", (Node *)$2);
754 | IN_P ROLE name_list
756 $$ = makeDefElem("addroleto", (Node *)$3);
758 | IN_P GROUP_P name_list
760 $$ = makeDefElem("addroleto", (Node *)$3);
765 /*****************************************************************************
767 * Create a new Postgres DBMS user (role with implied login ability)
769 *****************************************************************************/
772 CREATE USER RoleId opt_with OptRoleList
774 CreateRoleStmt *n = makeNode(CreateRoleStmt);
775 n->stmt_type = ROLESTMT_USER;
783 /*****************************************************************************
785 * Alter a postgresql DBMS role
787 *****************************************************************************/
790 ALTER ROLE RoleId opt_with OptRoleList
792 AlterRoleStmt *n = makeNode(AlterRoleStmt);
794 n->action = +1; /* add, if there are members */
801 ALTER ROLE RoleId SetResetClause
803 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
811 /*****************************************************************************
813 * Alter a postgresql DBMS user
815 *****************************************************************************/
818 ALTER USER RoleId opt_with OptRoleList
820 AlterRoleStmt *n = makeNode(AlterRoleStmt);
822 n->action = +1; /* add, if there are members */
830 ALTER USER RoleId SetResetClause
832 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
840 /*****************************************************************************
842 * Drop a postgresql DBMS role
844 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
845 * might own objects in multiple databases, there is presently no way to
846 * implement either cascading or restricting. Caveat DBA.
847 *****************************************************************************/
852 DropRoleStmt *n = makeNode(DropRoleStmt);
853 n->missing_ok = FALSE;
857 | DROP ROLE IF_P EXISTS name_list
859 DropRoleStmt *n = makeNode(DropRoleStmt);
860 n->missing_ok = TRUE;
866 /*****************************************************************************
868 * Drop a postgresql DBMS user
870 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
871 * might own objects in multiple databases, there is presently no way to
872 * implement either cascading or restricting. Caveat DBA.
873 *****************************************************************************/
878 DropRoleStmt *n = makeNode(DropRoleStmt);
879 n->missing_ok = FALSE;
883 | DROP USER IF_P EXISTS name_list
885 DropRoleStmt *n = makeNode(DropRoleStmt);
887 n->missing_ok = TRUE;
893 /*****************************************************************************
895 * Create a postgresql group (role without login ability)
897 *****************************************************************************/
900 CREATE GROUP_P RoleId opt_with OptRoleList
902 CreateRoleStmt *n = makeNode(CreateRoleStmt);
903 n->stmt_type = ROLESTMT_GROUP;
911 /*****************************************************************************
913 * Alter a postgresql group
915 *****************************************************************************/
918 ALTER GROUP_P RoleId add_drop USER name_list
920 AlterRoleStmt *n = makeNode(AlterRoleStmt);
923 n->options = list_make1(makeDefElem("rolemembers",
929 add_drop: ADD_P { $$ = +1; }
934 /*****************************************************************************
936 * Drop a postgresql group
938 * XXX see above notes about cascading DROP USER; groups have same problem.
939 *****************************************************************************/
942 DROP GROUP_P name_list
944 DropRoleStmt *n = makeNode(DropRoleStmt);
945 n->missing_ok = FALSE;
949 | DROP GROUP_P IF_P EXISTS name_list
951 DropRoleStmt *n = makeNode(DropRoleStmt);
952 n->missing_ok = TRUE;
959 /*****************************************************************************
961 * Manipulate a schema
963 *****************************************************************************/
966 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
968 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
969 /* One can omit the schema name or the authorization id. */
978 | CREATE SCHEMA ColId OptSchemaEltList
980 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
981 /* ...but not both */
991 | /* EMPTY */ { $$ = NULL; }
995 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
996 | /* EMPTY */ { $$ = NIL; }
1000 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1001 * statement (in addition to by themselves).
1013 /*****************************************************************************
1015 * Set PG internal variable
1016 * SET name TO 'var_value'
1017 * Include SQL92 syntax (thomas 1997-10-22):
1018 * SET TIME ZONE 'var_value'
1020 *****************************************************************************/
1025 VariableSetStmt *n = $2;
1026 n->is_local = false;
1029 | SET LOCAL set_rest
1031 VariableSetStmt *n = $3;
1035 | SET SESSION set_rest
1037 VariableSetStmt *n = $3;
1038 n->is_local = false;
1043 set_rest: /* Generic SET syntaxes: */
1044 var_name TO var_list
1046 VariableSetStmt *n = makeNode(VariableSetStmt);
1047 n->kind = VAR_SET_VALUE;
1052 | var_name '=' var_list
1054 VariableSetStmt *n = makeNode(VariableSetStmt);
1055 n->kind = VAR_SET_VALUE;
1060 | var_name TO DEFAULT
1062 VariableSetStmt *n = makeNode(VariableSetStmt);
1063 n->kind = VAR_SET_DEFAULT;
1067 | var_name '=' DEFAULT
1069 VariableSetStmt *n = makeNode(VariableSetStmt);
1070 n->kind = VAR_SET_DEFAULT;
1074 | var_name FROM CURRENT_P
1076 VariableSetStmt *n = makeNode(VariableSetStmt);
1077 n->kind = VAR_SET_CURRENT;
1081 /* Special syntaxes mandated by SQL standard: */
1082 | TIME ZONE zone_value
1084 VariableSetStmt *n = makeNode(VariableSetStmt);
1085 n->kind = VAR_SET_VALUE;
1086 n->name = "timezone";
1088 n->args = list_make1($3);
1090 n->kind = VAR_SET_DEFAULT;
1093 | TRANSACTION transaction_mode_list
1095 VariableSetStmt *n = makeNode(VariableSetStmt);
1096 n->kind = VAR_SET_MULTI;
1097 n->name = "TRANSACTION";
1101 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1103 VariableSetStmt *n = makeNode(VariableSetStmt);
1104 n->kind = VAR_SET_MULTI;
1105 n->name = "SESSION CHARACTERISTICS";
1109 | NAMES opt_encoding
1111 VariableSetStmt *n = makeNode(VariableSetStmt);
1112 n->kind = VAR_SET_VALUE;
1113 n->name = "client_encoding";
1115 n->args = list_make1(makeStringConst($2, NULL));
1117 n->kind = VAR_SET_DEFAULT;
1120 | ROLE ColId_or_Sconst
1122 VariableSetStmt *n = makeNode(VariableSetStmt);
1123 n->kind = VAR_SET_VALUE;
1125 n->args = list_make1(makeStringConst($2, NULL));
1128 | SESSION AUTHORIZATION ColId_or_Sconst
1130 VariableSetStmt *n = makeNode(VariableSetStmt);
1131 n->kind = VAR_SET_VALUE;
1132 n->name = "session_authorization";
1133 n->args = list_make1(makeStringConst($3, NULL));
1136 | SESSION AUTHORIZATION DEFAULT
1138 VariableSetStmt *n = makeNode(VariableSetStmt);
1139 n->kind = VAR_SET_DEFAULT;
1140 n->name = "session_authorization";
1143 | XML_P OPTION document_or_content
1145 VariableSetStmt *n = makeNode(VariableSetStmt);
1146 n->kind = VAR_SET_VALUE;
1147 n->name = "xmloption";
1148 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", NULL));
1153 var_name: ColId { $$ = $1; }
1154 | var_name '.' ColId
1156 $$ = palloc(strlen($1) + strlen($3) + 2);
1157 sprintf($$, "%s.%s", $1, $3);
1161 var_list: var_value { $$ = list_make1($1); }
1162 | var_list ',' var_value { $$ = lappend($1, $3); }
1165 var_value: opt_boolean
1166 { $$ = makeStringConst($1, NULL); }
1168 { $$ = makeStringConst($1, NULL); }
1170 { $$ = makeAConst($1); }
1173 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1174 | READ COMMITTED { $$ = "read committed"; }
1175 | REPEATABLE READ { $$ = "repeatable read"; }
1176 | SERIALIZABLE { $$ = "serializable"; }
1180 TRUE_P { $$ = "true"; }
1181 | FALSE_P { $$ = "false"; }
1183 | OFF { $$ = "off"; }
1186 /* Timezone values can be:
1187 * - a string such as 'pst8pdt'
1188 * - an identifier such as "pst8pdt"
1189 * - an integer or floating point number
1190 * - a time interval per SQL99
1191 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1192 * so use IDENT and reject anything which is a reserved word.
1197 $$ = makeStringConst($1, NULL);
1201 $$ = makeStringConst($1, NULL);
1203 | ConstInterval Sconst opt_interval
1205 A_Const *n = (A_Const *) makeStringConst($2, $1);
1206 if ($3 != INTERVAL_FULL_RANGE)
1208 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1210 (errcode(ERRCODE_SYNTAX_ERROR),
1211 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1212 n->typename->typmods = list_make1(makeIntConst($3));
1216 | ConstInterval '(' Iconst ')' Sconst opt_interval
1218 A_Const *n = (A_Const *) makeStringConst($5, $1);
1219 if (($6 != INTERVAL_FULL_RANGE)
1220 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
1222 (errcode(ERRCODE_SYNTAX_ERROR),
1223 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1224 n->typename->typmods = list_make2(makeIntConst($6),
1228 | NumericOnly { $$ = makeAConst($1); }
1229 | DEFAULT { $$ = NULL; }
1230 | LOCAL { $$ = NULL; }
1235 | DEFAULT { $$ = NULL; }
1236 | /*EMPTY*/ { $$ = NULL; }
1241 | SCONST { $$ = $1; }
1247 VariableSetStmt *n = makeNode(VariableSetStmt);
1248 n->kind = VAR_RESET;
1254 VariableSetStmt *n = makeNode(VariableSetStmt);
1255 n->kind = VAR_RESET;
1256 n->name = "timezone";
1259 | RESET TRANSACTION ISOLATION LEVEL
1261 VariableSetStmt *n = makeNode(VariableSetStmt);
1262 n->kind = VAR_RESET;
1263 n->name = "transaction_isolation";
1266 | RESET SESSION AUTHORIZATION
1268 VariableSetStmt *n = makeNode(VariableSetStmt);
1269 n->kind = VAR_RESET;
1270 n->name = "session_authorization";
1275 VariableSetStmt *n = makeNode(VariableSetStmt);
1276 n->kind = VAR_RESET_ALL;
1281 /* SetResetClause allows SET or RESET without LOCAL */
1283 SET set_rest { $$ = $2; }
1284 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1291 VariableShowStmt *n = makeNode(VariableShowStmt);
1297 VariableShowStmt *n = makeNode(VariableShowStmt);
1298 n->name = "timezone";
1301 | SHOW TRANSACTION ISOLATION LEVEL
1303 VariableShowStmt *n = makeNode(VariableShowStmt);
1304 n->name = "transaction_isolation";
1307 | SHOW SESSION AUTHORIZATION
1309 VariableShowStmt *n = makeNode(VariableShowStmt);
1310 n->name = "session_authorization";
1315 VariableShowStmt *n = makeNode(VariableShowStmt);
1323 SET CONSTRAINTS constraints_set_list constraints_set_mode
1325 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1326 n->constraints = $3;
1332 constraints_set_list:
1334 | qualified_name_list { $$ = $1; }
1337 constraints_set_mode:
1338 DEFERRED { $$ = TRUE; }
1339 | IMMEDIATE { $$ = FALSE; }
1344 * Checkpoint statement
1349 CheckPointStmt *n = makeNode(CheckPointStmt);
1355 /*****************************************************************************
1357 * DISCARD { ALL | TEMP | PLANS }
1359 *****************************************************************************/
1364 DiscardStmt *n = makeNode(DiscardStmt);
1365 n->target = DISCARD_ALL;
1370 DiscardStmt *n = makeNode(DiscardStmt);
1371 n->target = DISCARD_TEMP;
1376 DiscardStmt *n = makeNode(DiscardStmt);
1377 n->target = DISCARD_TEMP;
1382 DiscardStmt *n = makeNode(DiscardStmt);
1383 n->target = DISCARD_PLANS;
1389 /*****************************************************************************
1391 * ALTER [ TABLE | INDEX ] variations
1393 *****************************************************************************/
1396 ALTER TABLE relation_expr alter_table_cmds
1398 AlterTableStmt *n = makeNode(AlterTableStmt);
1401 n->relkind = OBJECT_TABLE;
1404 | ALTER INDEX relation_expr alter_rel_cmds
1406 AlterTableStmt *n = makeNode(AlterTableStmt);
1409 n->relkind = OBJECT_INDEX;
1415 alter_table_cmd { $$ = list_make1($1); }
1416 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1419 /* Subcommands that are for ALTER TABLE only */
1421 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1422 ADD_P opt_column columnDef
1424 AlterTableCmd *n = makeNode(AlterTableCmd);
1425 n->subtype = AT_AddColumn;
1429 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1430 | ALTER opt_column ColId alter_column_default
1432 AlterTableCmd *n = makeNode(AlterTableCmd);
1433 n->subtype = AT_ColumnDefault;
1438 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1439 | ALTER opt_column ColId DROP NOT NULL_P
1441 AlterTableCmd *n = makeNode(AlterTableCmd);
1442 n->subtype = AT_DropNotNull;
1446 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1447 | ALTER opt_column ColId SET NOT NULL_P
1449 AlterTableCmd *n = makeNode(AlterTableCmd);
1450 n->subtype = AT_SetNotNull;
1454 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1455 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1457 AlterTableCmd *n = makeNode(AlterTableCmd);
1458 n->subtype = AT_SetStatistics;
1460 n->def = (Node *) $6;
1463 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1464 | ALTER opt_column ColId SET STORAGE ColId
1466 AlterTableCmd *n = makeNode(AlterTableCmd);
1467 n->subtype = AT_SetStorage;
1469 n->def = (Node *) makeString($6);
1472 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1473 | DROP opt_column ColId opt_drop_behavior
1475 AlterTableCmd *n = makeNode(AlterTableCmd);
1476 n->subtype = AT_DropColumn;
1482 * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
1483 * [ USING <expression> ]
1485 | ALTER opt_column ColId TYPE_P Typename alter_using
1487 AlterTableCmd *n = makeNode(AlterTableCmd);
1488 n->subtype = AT_AlterColumnType;
1490 n->def = (Node *) $5;
1494 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1495 | ADD_P TableConstraint
1497 AlterTableCmd *n = makeNode(AlterTableCmd);
1498 n->subtype = AT_AddConstraint;
1502 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1503 | DROP CONSTRAINT name opt_drop_behavior
1505 AlterTableCmd *n = makeNode(AlterTableCmd);
1506 n->subtype = AT_DropConstraint;
1511 /* ALTER TABLE <relation> SET WITHOUT OIDS */
1514 AlterTableCmd *n = makeNode(AlterTableCmd);
1515 n->subtype = AT_DropOids;
1518 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1521 AlterTableCmd *n = makeNode(AlterTableCmd);
1522 n->subtype = AT_ClusterOn;
1526 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1527 | SET WITHOUT CLUSTER
1529 AlterTableCmd *n = makeNode(AlterTableCmd);
1530 n->subtype = AT_DropCluster;
1534 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1535 | ENABLE_P TRIGGER name
1537 AlterTableCmd *n = makeNode(AlterTableCmd);
1538 n->subtype = AT_EnableTrig;
1542 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1543 | ENABLE_P ALWAYS TRIGGER name
1545 AlterTableCmd *n = makeNode(AlterTableCmd);
1546 n->subtype = AT_EnableAlwaysTrig;
1550 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1551 | ENABLE_P REPLICA TRIGGER name
1553 AlterTableCmd *n = makeNode(AlterTableCmd);
1554 n->subtype = AT_EnableReplicaTrig;
1558 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1559 | ENABLE_P TRIGGER ALL
1561 AlterTableCmd *n = makeNode(AlterTableCmd);
1562 n->subtype = AT_EnableTrigAll;
1565 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1566 | ENABLE_P TRIGGER USER
1568 AlterTableCmd *n = makeNode(AlterTableCmd);
1569 n->subtype = AT_EnableTrigUser;
1572 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1573 | DISABLE_P TRIGGER name
1575 AlterTableCmd *n = makeNode(AlterTableCmd);
1576 n->subtype = AT_DisableTrig;
1580 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1581 | DISABLE_P TRIGGER ALL
1583 AlterTableCmd *n = makeNode(AlterTableCmd);
1584 n->subtype = AT_DisableTrigAll;
1587 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1588 | DISABLE_P TRIGGER USER
1590 AlterTableCmd *n = makeNode(AlterTableCmd);
1591 n->subtype = AT_DisableTrigUser;
1594 /* ALTER TABLE <name> ENABLE RULE <rule> */
1595 | ENABLE_P RULE name
1597 AlterTableCmd *n = makeNode(AlterTableCmd);
1598 n->subtype = AT_EnableRule;
1602 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1603 | ENABLE_P ALWAYS RULE name
1605 AlterTableCmd *n = makeNode(AlterTableCmd);
1606 n->subtype = AT_EnableAlwaysRule;
1610 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1611 | ENABLE_P REPLICA RULE name
1613 AlterTableCmd *n = makeNode(AlterTableCmd);
1614 n->subtype = AT_EnableReplicaRule;
1618 /* ALTER TABLE <name> DISABLE RULE <rule> */
1619 | DISABLE_P RULE name
1621 AlterTableCmd *n = makeNode(AlterTableCmd);
1622 n->subtype = AT_DisableRule;
1626 /* ALTER TABLE <name> INHERIT <parent> */
1627 | INHERIT qualified_name
1629 AlterTableCmd *n = makeNode(AlterTableCmd);
1630 n->subtype = AT_AddInherit;
1631 n->def = (Node *) $2;
1634 /* ALTER TABLE <name> NO INHERIT <parent> */
1635 | NO INHERIT qualified_name
1637 AlterTableCmd *n = makeNode(AlterTableCmd);
1638 n->subtype = AT_DropInherit;
1639 n->def = (Node *) $3;
1649 alter_rel_cmd { $$ = list_make1($1); }
1650 | alter_rel_cmds ',' alter_rel_cmd { $$ = lappend($1, $3); }
1653 /* Subcommands that are for ALTER TABLE or ALTER INDEX */
1655 /* ALTER [TABLE|INDEX] <name> OWNER TO RoleId */
1658 AlterTableCmd *n = makeNode(AlterTableCmd);
1659 n->subtype = AT_ChangeOwner;
1663 /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
1664 | SET TABLESPACE name
1666 AlterTableCmd *n = makeNode(AlterTableCmd);
1667 n->subtype = AT_SetTableSpace;
1671 /* ALTER [TABLE|INDEX] <name> SET (...) */
1674 AlterTableCmd *n = makeNode(AlterTableCmd);
1675 n->subtype = AT_SetRelOptions;
1676 n->def = (Node *)$2;
1679 /* ALTER [TABLE|INDEX] <name> RESET (...) */
1682 AlterTableCmd *n = makeNode(AlterTableCmd);
1683 n->subtype = AT_ResetRelOptions;
1684 n->def = (Node *)$2;
1689 alter_column_default:
1690 SET DEFAULT a_expr { $$ = $3; }
1691 | DROP DEFAULT { $$ = NULL; }
1695 CASCADE { $$ = DROP_CASCADE; }
1696 | RESTRICT { $$ = DROP_RESTRICT; }
1697 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1701 USING a_expr { $$ = $2; }
1702 | /* EMPTY */ { $$ = NULL; }
1707 /*****************************************************************************
1710 * close <portalname>
1712 *****************************************************************************/
1717 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1723 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1724 n->portalname = NULL;
1730 /*****************************************************************************
1733 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1735 * BINARY, OIDS, and DELIMITERS kept in old locations
1736 * for backward compatibility. 2002-06-18
1738 * COPY ( SELECT ... ) TO file [WITH options]
1739 * This form doesn't have the backwards-compatible option
1742 *****************************************************************************/
1744 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1745 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1747 CopyStmt *n = makeNode(CopyStmt);
1755 /* Concatenate user-supplied flags */
1757 n->options = lappend(n->options, $2);
1759 n->options = lappend(n->options, $5);
1761 n->options = lappend(n->options, $8);
1763 n->options = list_concat(n->options, $10);
1766 | COPY select_with_parens TO copy_file_name opt_with
1769 CopyStmt *n = makeNode(CopyStmt);
1782 | TO { $$ = FALSE; }
1786 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1787 * used depends on the direction. (It really doesn't make sense to copy from
1788 * stdout. We silently correct the "typo".) - AY 9/94
1792 | STDIN { $$ = NULL; }
1793 | STDOUT { $$ = NULL; }
1799 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1800 | /* EMPTY */ { $$ = NIL; }
1807 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1811 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1813 | DELIMITER opt_as Sconst
1815 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1817 | NULL_P opt_as Sconst
1819 $$ = makeDefElem("null", (Node *)makeString($3));
1823 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1827 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1829 | QUOTE opt_as Sconst
1831 $$ = makeDefElem("quote", (Node *)makeString($3));
1833 | ESCAPE opt_as Sconst
1835 $$ = makeDefElem("escape", (Node *)makeString($3));
1837 | FORCE QUOTE columnList
1839 $$ = makeDefElem("force_quote", (Node *)$3);
1841 | FORCE NOT NULL_P columnList
1843 $$ = makeDefElem("force_notnull", (Node *)$4);
1847 /* The following exist for backward compatibility */
1852 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1854 | /*EMPTY*/ { $$ = NULL; }
1860 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1862 | /*EMPTY*/ { $$ = NULL; }
1866 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1867 opt_using DELIMITERS Sconst
1869 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1871 | /*EMPTY*/ { $$ = NULL; }
1880 /*****************************************************************************
1883 * CREATE TABLE relname
1885 *****************************************************************************/
1887 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1888 OptInherit OptWith OnCommitOption OptTableSpace
1890 CreateStmt *n = makeNode(CreateStmt);
1894 n->inhRelations = $8;
1895 n->constraints = NIL;
1898 n->tablespacename = $11;
1901 | CREATE OptTemp TABLE qualified_name OF qualified_name
1902 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1904 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1905 * by our inheritance capabilities. Let's try it...
1907 CreateStmt *n = makeNode(CreateStmt);
1911 n->inhRelations = list_make1($6);
1912 n->constraints = NIL;
1915 n->tablespacename = $12;
1921 * Redundancy here is needed to avoid shift/reduce conflicts,
1922 * since TEMP is not a reserved word. See also OptTempTableName.
1924 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1925 * the LOCAL keyword is really meaningless.
1927 OptTemp: TEMPORARY { $$ = TRUE; }
1928 | TEMP { $$ = TRUE; }
1929 | LOCAL TEMPORARY { $$ = TRUE; }
1930 | LOCAL TEMP { $$ = TRUE; }
1931 | GLOBAL TEMPORARY { $$ = TRUE; }
1932 | GLOBAL TEMP { $$ = TRUE; }
1933 | /*EMPTY*/ { $$ = FALSE; }
1936 OptTableElementList:
1937 TableElementList { $$ = $1; }
1938 | /*EMPTY*/ { $$ = NIL; }
1944 $$ = list_make1($1);
1946 | TableElementList ',' TableElement
1948 $$ = lappend($1, $3);
1953 columnDef { $$ = $1; }
1954 | TableLikeClause { $$ = $1; }
1955 | TableConstraint { $$ = $1; }
1958 columnDef: ColId Typename ColQualList
1960 ColumnDef *n = makeNode(ColumnDef);
1963 n->constraints = $3;
1970 ColQualList ColConstraint { $$ = lappend($1, $2); }
1971 | /*EMPTY*/ { $$ = NIL; }
1975 CONSTRAINT name ColConstraintElem
1977 switch (nodeTag($3))
1981 Constraint *n = (Constraint *)$3;
1985 case T_FkConstraint:
1987 FkConstraint *n = (FkConstraint *)$3;
1988 n->constr_name = $2;
1996 | ColConstraintElem { $$ = $1; }
1997 | ConstraintAttr { $$ = $1; }
2000 /* DEFAULT NULL is already the default for Postgres.
2001 * But define it here and carry it forward into the system
2002 * to make it explicit.
2003 * - thomas 1998-09-13
2005 * WITH NULL and NULL are not SQL92-standard syntax elements,
2006 * so leave them out. Use DEFAULT NULL to explicitly indicate
2007 * that a column may have that value. WITH NULL leads to
2008 * shift/reduce conflicts with WITH TIME ZONE anyway.
2009 * - thomas 1999-01-08
2011 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2012 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2013 * or be part of a_expr NOT LIKE or similar constructs).
2018 Constraint *n = makeNode(Constraint);
2019 n->contype = CONSTR_NOTNULL;
2022 n->cooked_expr = NULL;
2024 n->indexspace = NULL;
2029 Constraint *n = makeNode(Constraint);
2030 n->contype = CONSTR_NULL;
2033 n->cooked_expr = NULL;
2035 n->indexspace = NULL;
2038 | UNIQUE opt_definition OptConsTableSpace
2040 Constraint *n = makeNode(Constraint);
2041 n->contype = CONSTR_UNIQUE;
2044 n->cooked_expr = NULL;
2050 | PRIMARY KEY opt_definition OptConsTableSpace
2052 Constraint *n = makeNode(Constraint);
2053 n->contype = CONSTR_PRIMARY;
2056 n->cooked_expr = NULL;
2062 | CHECK '(' a_expr ')'
2064 Constraint *n = makeNode(Constraint);
2065 n->contype = CONSTR_CHECK;
2068 n->cooked_expr = NULL;
2070 n->indexspace = NULL;
2075 Constraint *n = makeNode(Constraint);
2076 n->contype = CONSTR_DEFAULT;
2079 n->cooked_expr = NULL;
2081 n->indexspace = NULL;
2084 | REFERENCES qualified_name opt_column_list key_match key_actions
2086 FkConstraint *n = makeNode(FkConstraint);
2087 n->constr_name = NULL;
2091 n->fk_matchtype = $4;
2092 n->fk_upd_action = (char) ($5 >> 8);
2093 n->fk_del_action = (char) ($5 & 0xFF);
2094 n->deferrable = FALSE;
2095 n->initdeferred = FALSE;
2101 * ConstraintAttr represents constraint attributes, which we parse as if
2102 * they were independent constraint clauses, in order to avoid shift/reduce
2103 * conflicts (since NOT might start either an independent NOT NULL clause
2104 * or an attribute). parse_utilcmd.c is responsible for attaching the
2105 * attribute information to the preceding "real" constraint node, and for
2106 * complaining if attribute clauses appear in the wrong place or wrong
2109 * See also ConstraintAttributeSpec, which can be used in places where
2110 * there is no parsing conflict.
2115 Constraint *n = makeNode(Constraint);
2116 n->contype = CONSTR_ATTR_DEFERRABLE;
2121 Constraint *n = makeNode(Constraint);
2122 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2125 | INITIALLY DEFERRED
2127 Constraint *n = makeNode(Constraint);
2128 n->contype = CONSTR_ATTR_DEFERRED;
2131 | INITIALLY IMMEDIATE
2133 Constraint *n = makeNode(Constraint);
2134 n->contype = CONSTR_ATTR_IMMEDIATE;
2141 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2142 * This seems to be a poor man's inheritance capability, with the resulting
2143 * tables completely decoupled except for the original commonality in definitions.
2145 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2146 * which is a part of SQL 200N
2149 LIKE qualified_name TableLikeOptionList
2151 InhRelation *n = makeNode(InhRelation);
2158 TableLikeOptionList:
2159 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2160 | /* EMPTY */ { $$ = NIL; }
2164 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2165 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2166 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2167 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2168 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2169 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2173 /* ConstraintElem specifies constraint syntax which is not embedded into
2174 * a column definition. ColConstraintElem specifies the embedded form.
2175 * - thomas 1997-12-03
2178 CONSTRAINT name ConstraintElem
2180 switch (nodeTag($3))
2184 Constraint *n = (Constraint *)$3;
2188 case T_FkConstraint:
2190 FkConstraint *n = (FkConstraint *)$3;
2191 n->constr_name = $2;
2199 | ConstraintElem { $$ = $1; }
2203 CHECK '(' a_expr ')'
2205 Constraint *n = makeNode(Constraint);
2206 n->contype = CONSTR_CHECK;
2209 n->cooked_expr = NULL;
2210 n->indexspace = NULL;
2213 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2215 Constraint *n = makeNode(Constraint);
2216 n->contype = CONSTR_UNIQUE;
2219 n->cooked_expr = NULL;
2225 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2227 Constraint *n = makeNode(Constraint);
2228 n->contype = CONSTR_PRIMARY;
2231 n->cooked_expr = NULL;
2237 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2238 opt_column_list key_match key_actions ConstraintAttributeSpec
2240 FkConstraint *n = makeNode(FkConstraint);
2241 n->constr_name = NULL;
2245 n->fk_matchtype = $9;
2246 n->fk_upd_action = (char) ($10 >> 8);
2247 n->fk_del_action = (char) ($10 & 0xFF);
2248 n->deferrable = ($11 & 1) != 0;
2249 n->initdeferred = ($11 & 2) != 0;
2255 '(' columnList ')' { $$ = $2; }
2256 | /*EMPTY*/ { $$ = NIL; }
2260 columnElem { $$ = list_make1($1); }
2261 | columnList ',' columnElem { $$ = lappend($1, $3); }
2266 $$ = (Node *) makeString($1);
2270 key_match: MATCH FULL
2272 $$ = FKCONSTR_MATCH_FULL;
2277 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2278 errmsg("MATCH PARTIAL not yet implemented")));
2279 $$ = FKCONSTR_MATCH_PARTIAL;
2283 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2287 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2292 * We combine the update and delete actions into one value temporarily
2293 * for simplicity of parsing, and then break them down again in the
2294 * calling production. update is in the left 8 bits, delete in the right.
2295 * Note that NOACTION is the default.
2299 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2301 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2302 | key_update key_delete
2303 { $$ = ($1 << 8) | ($2 & 0xFF); }
2304 | key_delete key_update
2305 { $$ = ($2 << 8) | ($1 & 0xFF); }
2307 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2310 key_update: ON UPDATE key_action { $$ = $3; }
2313 key_delete: ON DELETE_P key_action { $$ = $3; }
2317 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2318 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2319 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2320 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2321 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2324 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2325 | /*EMPTY*/ { $$ = NIL; }
2328 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2330 WITH definition { $$ = $2; }
2331 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2332 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2333 | /*EMPTY*/ { $$ = NIL; }
2336 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2337 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2338 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2339 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2342 OptTableSpace: TABLESPACE name { $$ = $2; }
2343 | /*EMPTY*/ { $$ = NULL; }
2346 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2347 | /*EMPTY*/ { $$ = NULL; }
2352 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2357 CREATE OptTemp TABLE create_as_target AS SelectStmt
2360 * When the SelectStmt is a set-operation tree, we must
2361 * stuff the INTO information into the leftmost component
2362 * Select, because that's where analyze.c will expect
2363 * to find it. Similarly, the output column names must
2364 * be attached to that Select's target list.
2366 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2367 if (n->intoClause != NULL)
2369 (errcode(ERRCODE_SYNTAX_ERROR),
2370 errmsg("CREATE TABLE AS cannot specify INTO")));
2371 $4->rel->istemp = $2;
2378 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2380 $$ = makeNode(IntoClause);
2385 $$->tableSpaceName = $5;
2390 '(' CreateAsList ')' { $$ = $2; }
2391 | /*EMPTY*/ { $$ = NIL; }
2395 CreateAsElement { $$ = list_make1($1); }
2396 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2402 ColumnDef *n = makeNode(ColumnDef);
2407 n->is_not_null = false;
2408 n->raw_default = NULL;
2409 n->cooked_default = NULL;
2410 n->constraints = NIL;
2416 /*****************************************************************************
2419 * CREATE SEQUENCE seqname
2420 * ALTER SEQUENCE seqname
2422 *****************************************************************************/
2425 CREATE OptTemp SEQUENCE qualified_name OptSeqList
2427 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2436 ALTER SEQUENCE qualified_name OptSeqList
2438 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2445 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
2446 | /*EMPTY*/ { $$ = NIL; }
2449 OptSeqElem: CACHE NumericOnly
2451 $$ = makeDefElem("cache", (Node *)$2);
2455 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2459 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2461 | INCREMENT opt_by NumericOnly
2463 $$ = makeDefElem("increment", (Node *)$3);
2465 | MAXVALUE NumericOnly
2467 $$ = makeDefElem("maxvalue", (Node *)$2);
2469 | MINVALUE NumericOnly
2471 $$ = makeDefElem("minvalue", (Node *)$2);
2475 $$ = makeDefElem("maxvalue", NULL);
2479 $$ = makeDefElem("minvalue", NULL);
2483 $$ = makeDefElem("owned_by", (Node *)$3);
2485 | START opt_with NumericOnly
2487 $$ = makeDefElem("start", (Node *)$3);
2489 | RESTART opt_with NumericOnly
2491 $$ = makeDefElem("restart", (Node *)$3);
2500 FloatOnly { $$ = $1; }
2501 | IntegerOnly { $$ = $1; }
2504 FloatOnly: FCONST { $$ = makeFloat($1); }
2512 IntegerOnly: SignedIconst { $$ = makeInteger($1); };
2515 /*****************************************************************************
2518 * CREATE PROCEDURAL LANGUAGE ...
2519 * DROP PROCEDURAL LANGUAGE ...
2521 *****************************************************************************/
2524 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2526 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2528 /* parameters are all to be supplied by system */
2530 n->plvalidator = NIL;
2531 n->pltrusted = false;
2534 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2535 HANDLER handler_name opt_validator opt_lancompiler
2537 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2540 n->plvalidator = $8;
2542 /* LANCOMPILER is now ignored entirely */
2548 TRUSTED { $$ = TRUE; }
2549 | /*EMPTY*/ { $$ = FALSE; }
2552 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2553 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2554 * Work around by using simple names, instead.
2557 name { $$ = list_make1(makeString($1)); }
2558 | name attrs { $$ = lcons(makeString($1), $2); }
2562 VALIDATOR handler_name { $$ = $2; }
2563 | /*EMPTY*/ { $$ = NIL; }
2567 LANCOMPILER Sconst { $$ = $2; }
2568 | /*EMPTY*/ { $$ = NULL; }
2572 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2574 DropPLangStmt *n = makeNode(DropPLangStmt);
2577 n->missing_ok = false;
2580 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2582 DropPLangStmt *n = makeNode(DropPLangStmt);
2585 n->missing_ok = true;
2595 /*****************************************************************************
2598 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2600 *****************************************************************************/
2602 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2604 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2605 n->tablespacename = $3;
2612 OptTableSpaceOwner: OWNER name { $$ = $2; }
2613 | /*EMPTY */ { $$ = NULL; }
2616 /*****************************************************************************
2619 * DROP TABLESPACE <tablespace>
2621 * No need for drop behaviour as we cannot implement dependencies for
2622 * objects in other databases; we can only support RESTRICT.
2624 ****************************************************************************/
2626 DropTableSpaceStmt: DROP TABLESPACE name
2628 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2629 n->tablespacename = $3;
2630 n->missing_ok = false;
2633 | DROP TABLESPACE IF_P EXISTS name
2635 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2636 n->tablespacename = $5;
2637 n->missing_ok = true;
2642 /*****************************************************************************
2645 * CREATE TRIGGER ...
2648 *****************************************************************************/
2651 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2652 qualified_name TriggerForSpec EXECUTE PROCEDURE
2653 func_name '(' TriggerFuncArgs ')'
2655 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2662 memcpy(n->actions, $5, 4);
2663 n->isconstraint = FALSE;
2664 n->deferrable = FALSE;
2665 n->initdeferred = FALSE;
2666 n->constrrel = NULL;
2669 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2670 qualified_name OptConstrFromTable
2671 ConstraintAttributeSpec
2672 FOR EACH ROW EXECUTE PROCEDURE
2673 func_name '(' TriggerFuncArgs ')'
2675 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2682 memcpy(n->actions, $6, 4);
2683 n->isconstraint = TRUE;
2684 n->deferrable = ($10 & 1) != 0;
2685 n->initdeferred = ($10 & 2) != 0;
2693 BEFORE { $$ = TRUE; }
2694 | AFTER { $$ = FALSE; }
2700 char *e = palloc(4);
2701 e[0] = $1; e[1] = '\0';
2704 | TriggerOneEvent OR TriggerOneEvent
2706 char *e = palloc(4);
2707 e[0] = $1; e[1] = $3; e[2] = '\0';
2710 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2712 char *e = palloc(4);
2713 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2719 INSERT { $$ = 'i'; }
2720 | DELETE_P { $$ = 'd'; }
2721 | UPDATE { $$ = 'u'; }
2725 FOR TriggerForOpt TriggerForType
2732 * If ROW/STATEMENT not specified, default to
2733 * STATEMENT, per SQL
2746 | STATEMENT { $$ = FALSE; }
2750 TriggerFuncArg { $$ = list_make1($1); }
2751 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2752 | /*EMPTY*/ { $$ = NIL; }
2759 snprintf(buf, sizeof(buf), "%d", $1);
2760 $$ = makeString(pstrdup(buf));
2762 | FCONST { $$ = makeString($1); }
2763 | Sconst { $$ = makeString($1); }
2764 | BCONST { $$ = makeString($1); }
2765 | XCONST { $$ = makeString($1); }
2766 | ColId { $$ = makeString($1); }
2770 FROM qualified_name { $$ = $2; }
2771 | /*EMPTY*/ { $$ = NULL; }
2774 ConstraintAttributeSpec:
2775 ConstraintDeferrabilitySpec
2777 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2779 if ($1 == 0 && $2 != 0)
2781 (errcode(ERRCODE_SYNTAX_ERROR),
2782 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2785 | ConstraintTimeSpec
2792 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2794 if ($2 == 0 && $1 != 0)
2796 (errcode(ERRCODE_SYNTAX_ERROR),
2797 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2804 ConstraintDeferrabilitySpec:
2805 NOT DEFERRABLE { $$ = 0; }
2806 | DEFERRABLE { $$ = 1; }
2810 INITIALLY IMMEDIATE { $$ = 0; }
2811 | INITIALLY DEFERRED { $$ = 2; }
2816 DROP TRIGGER name ON qualified_name opt_drop_behavior
2818 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2822 n->removeType = OBJECT_TRIGGER;
2823 n->missing_ok = false;
2826 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
2828 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2832 n->removeType = OBJECT_TRIGGER;
2833 n->missing_ok = true;
2839 /*****************************************************************************
2842 * CREATE ASSERTION ...
2843 * DROP ASSERTION ...
2845 *****************************************************************************/
2848 CREATE ASSERTION name CHECK '(' a_expr ')'
2849 ConstraintAttributeSpec
2851 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2853 n->args = list_make1($6);
2854 n->isconstraint = TRUE;
2855 n->deferrable = ($8 & 1) != 0;
2856 n->initdeferred = ($8 & 2) != 0;
2859 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2860 errmsg("CREATE ASSERTION is not yet implemented")));
2867 DROP ASSERTION name opt_drop_behavior
2869 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2873 n->removeType = OBJECT_TRIGGER; /* XXX */
2875 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2876 errmsg("DROP ASSERTION is not yet implemented")));
2882 /*****************************************************************************
2885 * define (aggregate,operator,type)
2887 *****************************************************************************/
2890 CREATE AGGREGATE func_name aggr_args definition
2892 DefineStmt *n = makeNode(DefineStmt);
2893 n->kind = OBJECT_AGGREGATE;
2894 n->oldstyle = false;
2900 | CREATE AGGREGATE func_name old_aggr_definition
2902 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
2903 DefineStmt *n = makeNode(DefineStmt);
2904 n->kind = OBJECT_AGGREGATE;
2911 | CREATE OPERATOR any_operator definition
2913 DefineStmt *n = makeNode(DefineStmt);
2914 n->kind = OBJECT_OPERATOR;
2915 n->oldstyle = false;
2921 | CREATE TYPE_P any_name definition
2923 DefineStmt *n = makeNode(DefineStmt);
2924 n->kind = OBJECT_TYPE;
2925 n->oldstyle = false;
2931 | CREATE TYPE_P any_name
2933 /* Shell type (identified by lack of definition) */
2934 DefineStmt *n = makeNode(DefineStmt);
2935 n->kind = OBJECT_TYPE;
2936 n->oldstyle = false;
2939 n->definition = NIL;
2942 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2944 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2945 RangeVar *r = makeNode(RangeVar);
2947 /* can't use qualified_name, sigh */
2948 switch (list_length($3))
2951 r->catalogname = NULL;
2952 r->schemaname = NULL;
2953 r->relname = strVal(linitial($3));
2956 r->catalogname = NULL;
2957 r->schemaname = strVal(linitial($3));
2958 r->relname = strVal(lsecond($3));
2961 r->catalogname = strVal(linitial($3));
2962 r->schemaname = strVal(lsecond($3));
2963 r->relname = strVal(lthird($3));
2967 (errcode(ERRCODE_SYNTAX_ERROR),
2968 errmsg("improper qualified name (too many dotted names): %s",
2969 NameListToString($3))));
2976 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
2978 CreateEnumStmt *n = makeNode(CreateEnumStmt);
2983 | CREATE TEXT_P SEARCH PARSER any_name definition
2985 DefineStmt *n = makeNode(DefineStmt);
2986 n->kind = OBJECT_TSPARSER;
2992 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
2994 DefineStmt *n = makeNode(DefineStmt);
2995 n->kind = OBJECT_TSDICTIONARY;
3001 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3003 DefineStmt *n = makeNode(DefineStmt);
3004 n->kind = OBJECT_TSTEMPLATE;
3010 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3012 DefineStmt *n = makeNode(DefineStmt);
3013 n->kind = OBJECT_TSCONFIGURATION;
3021 definition: '(' def_list ')' { $$ = $2; }
3024 def_list: def_elem { $$ = list_make1($1); }
3025 | def_list ',' def_elem { $$ = lappend($1, $3); }
3028 def_elem: ColLabel '=' def_arg
3030 $$ = makeDefElem($1, (Node *)$3);
3034 $$ = makeDefElem($1, NULL);
3038 /* Note: any simple identifier will be returned as a type name! */
3039 def_arg: func_type { $$ = (Node *)$1; }
3040 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3041 | qual_all_Op { $$ = (Node *)$1; }
3042 | NumericOnly { $$ = (Node *)$1; }
3043 | Sconst { $$ = (Node *)makeString($1); }
3046 aggr_args: '(' type_list ')' { $$ = $2; }
3047 | '(' '*' ')' { $$ = NIL; }
3050 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3053 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3054 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3057 old_aggr_elem: IDENT '=' def_arg
3059 $$ = makeDefElem($1, (Node *)$3);
3063 enum_val_list: Sconst
3064 { $$ = list_make1(makeString($1)); }
3065 | enum_val_list ',' Sconst
3066 { $$ = lappend($1, makeString($3)); }
3070 /*****************************************************************************
3073 * CREATE OPERATOR CLASS ...
3074 * CREATE OPERATOR FAMILY ...
3075 * ALTER OPERATOR FAMILY ...
3076 * DROP OPERATOR CLASS ...
3077 * DROP OPERATOR FAMILY ...
3079 *****************************************************************************/
3082 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3083 USING access_method opt_opfamily AS opclass_item_list
3085 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3086 n->opclassname = $4;
3090 n->opfamilyname = $11;
3097 opclass_item { $$ = list_make1($1); }
3098 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3102 OPERATOR Iconst any_operator opt_recheck
3104 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3105 n->itemtype = OPCLASS_ITEM_OPERATOR;
3112 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
3114 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3115 n->itemtype = OPCLASS_ITEM_OPERATOR;
3122 | FUNCTION Iconst func_name func_args
3124 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3125 n->itemtype = OPCLASS_ITEM_FUNCTION;
3127 n->args = extractArgTypes($4);
3131 | FUNCTION Iconst '(' type_list ')' func_name func_args
3133 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3134 n->itemtype = OPCLASS_ITEM_FUNCTION;
3136 n->args = extractArgTypes($7);
3143 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3144 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3150 opt_default: DEFAULT { $$ = TRUE; }
3151 | /*EMPTY*/ { $$ = FALSE; }
3154 opt_opfamily: FAMILY any_name { $$ = $2; }
3155 | /*EMPTY*/ { $$ = NIL; }
3158 opt_recheck: RECHECK { $$ = TRUE; }
3159 | /*EMPTY*/ { $$ = FALSE; }
3164 CREATE OPERATOR FAMILY any_name USING access_method
3166 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3167 n->opfamilyname = $4;
3174 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3176 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3177 n->opfamilyname = $4;
3183 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3185 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3186 n->opfamilyname = $4;
3195 opclass_drop { $$ = list_make1($1); }
3196 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3200 OPERATOR Iconst '(' type_list ')'
3202 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3203 n->itemtype = OPCLASS_ITEM_OPERATOR;
3208 | FUNCTION Iconst '(' type_list ')'
3210 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3211 n->itemtype = OPCLASS_ITEM_FUNCTION;
3220 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3222 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3223 n->opclassname = $4;
3226 n->missing_ok = false;
3229 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3231 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3232 n->opclassname = $6;
3235 n->missing_ok = true;
3241 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3243 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3244 n->opfamilyname = $4;
3247 n->missing_ok = false;
3250 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3252 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3253 n->opfamilyname = $6;
3256 n->missing_ok = true;
3262 /*****************************************************************************
3266 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3267 * REASSIGN OWNED BY username [, username ...] TO username
3269 *****************************************************************************/
3271 DROP OWNED BY name_list opt_drop_behavior
3273 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3281 REASSIGN OWNED BY name_list TO name
3283 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3290 /*****************************************************************************
3294 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3295 * [ RESTRICT | CASCADE ]
3297 *****************************************************************************/
3299 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3301 DropStmt *n = makeNode(DropStmt);
3303 n->missing_ok = TRUE;
3308 | DROP drop_type any_name_list opt_drop_behavior
3310 DropStmt *n = makeNode(DropStmt);
3312 n->missing_ok = FALSE;
3320 drop_type: TABLE { $$ = OBJECT_TABLE; }
3321 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3322 | VIEW { $$ = OBJECT_VIEW; }
3323 | INDEX { $$ = OBJECT_INDEX; }
3324 | TYPE_P { $$ = OBJECT_TYPE; }
3325 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3326 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3327 | SCHEMA { $$ = OBJECT_SCHEMA; }
3328 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3329 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3330 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3331 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3335 any_name { $$ = list_make1($1); }
3336 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3339 any_name: ColId { $$ = list_make1(makeString($1)); }
3340 | ColId attrs { $$ = lcons(makeString($1), $2); }
3343 attrs: '.' attr_name
3344 { $$ = list_make1(makeString($2)); }
3345 | attrs '.' attr_name
3346 { $$ = lappend($1, makeString($3)); }
3350 /*****************************************************************************
3353 * truncate table relname1, relname2, ...
3355 *****************************************************************************/
3358 TRUNCATE opt_table qualified_name_list opt_drop_behavior
3360 TruncateStmt *n = makeNode(TruncateStmt);
3367 /*****************************************************************************
3369 * The COMMENT ON statement can take different forms based upon the type of
3370 * the object associated with the comment. The form of the statement is:
3372 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3373 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3374 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3375 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3376 * TEXT SEARCH TEMPLATE |
3377 * TEXT SEARCH CONFIGURATION ] <objname> |
3378 * AGGREGATE <aggname> (arg1, ...) |
3379 * FUNCTION <funcname> (arg1, arg2, ...) |
3380 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3381 * TRIGGER <triggername> ON <relname> |
3382 * CONSTRAINT <constraintname> ON <relname> |
3383 * RULE <rulename> ON <relname> ]
3386 *****************************************************************************/
3389 COMMENT ON comment_type any_name IS comment_text
3391 CommentStmt *n = makeNode(CommentStmt);
3398 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3400 CommentStmt *n = makeNode(CommentStmt);
3401 n->objtype = OBJECT_AGGREGATE;
3407 | COMMENT ON FUNCTION func_name func_args IS comment_text
3409 CommentStmt *n = makeNode(CommentStmt);
3410 n->objtype = OBJECT_FUNCTION;
3412 n->objargs = extractArgTypes($5);
3416 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
3419 CommentStmt *n = makeNode(CommentStmt);
3420 n->objtype = OBJECT_OPERATOR;
3426 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3428 CommentStmt *n = makeNode(CommentStmt);
3429 n->objtype = OBJECT_CONSTRAINT;
3430 n->objname = lappend($6, makeString($4));
3435 | COMMENT ON RULE name ON any_name IS comment_text
3437 CommentStmt *n = makeNode(CommentStmt);
3438 n->objtype = OBJECT_RULE;
3439 n->objname = lappend($6, makeString($4));
3444 | COMMENT ON RULE name IS comment_text
3446 /* Obsolete syntax supported for awhile for compatibility */
3447 CommentStmt *n = makeNode(CommentStmt);
3448 n->objtype = OBJECT_RULE;
3449 n->objname = list_make1(makeString($4));
3454 | COMMENT ON TRIGGER name ON any_name IS comment_text
3456 CommentStmt *n = makeNode(CommentStmt);
3457 n->objtype = OBJECT_TRIGGER;
3458 n->objname = lappend($6, makeString($4));
3463 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3465 CommentStmt *n = makeNode(CommentStmt);
3466 n->objtype = OBJECT_OPCLASS;
3468 n->objargs = list_make1(makeString($7));
3472 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3474 CommentStmt *n = makeNode(CommentStmt);
3475 n->objtype = OBJECT_OPFAMILY;
3477 n->objargs = list_make1(makeString($7));
3481 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3483 CommentStmt *n = makeNode(CommentStmt);
3484 n->objtype = OBJECT_LARGEOBJECT;
3485 n->objname = list_make1($5);
3490 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3492 CommentStmt *n = makeNode(CommentStmt);
3493 n->objtype = OBJECT_CAST;
3494 n->objname = list_make1($5);
3495 n->objargs = list_make1($7);
3499 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3501 CommentStmt *n = makeNode(CommentStmt);
3502 n->objtype = OBJECT_LANGUAGE;
3508 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3510 CommentStmt *n = makeNode(CommentStmt);
3511 n->objtype = OBJECT_TSPARSER;
3516 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3518 CommentStmt *n = makeNode(CommentStmt);
3519 n->objtype = OBJECT_TSDICTIONARY;
3524 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3526 CommentStmt *n = makeNode(CommentStmt);
3527 n->objtype = OBJECT_TSTEMPLATE;
3532 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3534 CommentStmt *n = makeNode(CommentStmt);
3535 n->objtype = OBJECT_TSCONFIGURATION;
3543 COLUMN { $$ = OBJECT_COLUMN; }
3544 | DATABASE { $$ = OBJECT_DATABASE; }
3545 | SCHEMA { $$ = OBJECT_SCHEMA; }
3546 | INDEX { $$ = OBJECT_INDEX; }
3547 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3548 | TABLE { $$ = OBJECT_TABLE; }
3549 | DOMAIN_P { $$ = OBJECT_TYPE; }
3550 | TYPE_P { $$ = OBJECT_TYPE; }
3551 | VIEW { $$ = OBJECT_VIEW; }
3552 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3553 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3554 | ROLE { $$ = OBJECT_ROLE; }
3559 | NULL_P { $$ = NULL; }
3562 /*****************************************************************************
3567 *****************************************************************************/
3569 FetchStmt: FETCH fetch_direction from_in name
3571 FetchStmt *n = (FetchStmt *) $2;
3578 FetchStmt *n = makeNode(FetchStmt);
3579 n->direction = FETCH_FORWARD;
3585 | MOVE fetch_direction from_in name
3587 FetchStmt *n = (FetchStmt *) $2;
3594 FetchStmt *n = makeNode(FetchStmt);
3595 n->direction = FETCH_FORWARD;
3606 FetchStmt *n = makeNode(FetchStmt);
3607 n->direction = FETCH_FORWARD;
3613 FetchStmt *n = makeNode(FetchStmt);
3614 n->direction = FETCH_FORWARD;
3620 FetchStmt *n = makeNode(FetchStmt);
3621 n->direction = FETCH_BACKWARD;
3627 FetchStmt *n = makeNode(FetchStmt);
3628 n->direction = FETCH_ABSOLUTE;
3634 FetchStmt *n = makeNode(FetchStmt);
3635 n->direction = FETCH_ABSOLUTE;
3639 | ABSOLUTE_P SignedIconst
3641 FetchStmt *n = makeNode(FetchStmt);
3642 n->direction = FETCH_ABSOLUTE;
3646 | RELATIVE_P SignedIconst
3648 FetchStmt *n = makeNode(FetchStmt);
3649 n->direction = FETCH_RELATIVE;
3655 FetchStmt *n = makeNode(FetchStmt);
3656 n->direction = FETCH_FORWARD;
3662 FetchStmt *n = makeNode(FetchStmt);
3663 n->direction = FETCH_FORWARD;
3664 n->howMany = FETCH_ALL;
3669 FetchStmt *n = makeNode(FetchStmt);
3670 n->direction = FETCH_FORWARD;
3674 | FORWARD SignedIconst
3676 FetchStmt *n = makeNode(FetchStmt);
3677 n->direction = FETCH_FORWARD;
3683 FetchStmt *n = makeNode(FetchStmt);
3684 n->direction = FETCH_FORWARD;
3685 n->howMany = FETCH_ALL;
3690 FetchStmt *n = makeNode(FetchStmt);
3691 n->direction = FETCH_BACKWARD;
3695 | BACKWARD SignedIconst
3697 FetchStmt *n = makeNode(FetchStmt);
3698 n->direction = FETCH_BACKWARD;
3704 FetchStmt *n = makeNode(FetchStmt);
3705 n->direction = FETCH_BACKWARD;
3706 n->howMany = FETCH_ALL;
3716 /*****************************************************************************
3718 * GRANT and REVOKE statements
3720 *****************************************************************************/
3722 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3723 opt_grant_grant_option
3725 GrantStmt *n = makeNode(GrantStmt);
3728 n->objtype = ($4)->objtype;
3729 n->objects = ($4)->objs;
3731 n->grant_option = $7;
3737 REVOKE privileges ON privilege_target
3738 FROM grantee_list opt_drop_behavior
3740 GrantStmt *n = makeNode(GrantStmt);
3741 n->is_grant = false;
3742 n->grant_option = false;
3744 n->objtype = ($4)->objtype;
3745 n->objects = ($4)->objs;
3750 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3751 FROM grantee_list opt_drop_behavior
3753 GrantStmt *n = makeNode(GrantStmt);
3754 n->is_grant = false;
3755 n->grant_option = true;
3757 n->objtype = ($7)->objtype;
3758 n->objects = ($7)->objs;
3767 * A privilege list is represented as a list of strings; the validity of
3768 * the privilege names gets checked at execution. This is a bit annoying
3769 * but we have little choice because of the syntactic conflict with lists
3770 * of role names in GRANT/REVOKE. What's more, we have to call out in
3771 * the "privilege" production any reserved keywords that need to be usable
3772 * as privilege names.
3775 /* either ALL [PRIVILEGES] or a list of individual privileges */
3776 privileges: privilege_list
3784 privilege_list: privilege
3785 { $$ = list_make1(makeString($1)); }
3786 | privilege_list ',' privilege
3787 { $$ = lappend($1, makeString($3)); }
3790 privilege: SELECT { $$ = pstrdup($1); }
3791 | REFERENCES { $$ = pstrdup($1); }
3792 | CREATE { $$ = pstrdup($1); }
3793 | ColId { $$ = $1; }
3797 /* Don't bother trying to fold the first two rules into one using
3798 * opt_table. You're going to get conflicts.
3803 PrivTarget *n = makeNode(PrivTarget);
3804 n->objtype = ACL_OBJECT_RELATION;
3808 | TABLE qualified_name_list
3810 PrivTarget *n = makeNode(PrivTarget);
3811 n->objtype = ACL_OBJECT_RELATION;
3815 | SEQUENCE qualified_name_list
3817 PrivTarget *n = makeNode(PrivTarget);
3818 n->objtype = ACL_OBJECT_SEQUENCE;
3822 | FUNCTION function_with_argtypes_list
3824 PrivTarget *n = makeNode(PrivTarget);
3825 n->objtype = ACL_OBJECT_FUNCTION;
3829 | DATABASE name_list
3831 PrivTarget *n = makeNode(PrivTarget);
3832 n->objtype = ACL_OBJECT_DATABASE;
3836 | LANGUAGE name_list
3838 PrivTarget *n = makeNode(PrivTarget);
3839 n->objtype = ACL_OBJECT_LANGUAGE;
3845 PrivTarget *n = makeNode(PrivTarget);
3846 n->objtype = ACL_OBJECT_NAMESPACE;
3850 | TABLESPACE name_list
3852 PrivTarget *n = makeNode(PrivTarget);
3853 n->objtype = ACL_OBJECT_TABLESPACE;
3861 grantee { $$ = list_make1($1); }
3862 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3867 PrivGrantee *n = makeNode(PrivGrantee);
3868 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3869 if (strcmp($1, "public") == 0)
3877 PrivGrantee *n = makeNode(PrivGrantee);
3878 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3879 if (strcmp($2, "public") == 0)
3888 opt_grant_grant_option:
3889 WITH GRANT OPTION { $$ = TRUE; }
3890 | /*EMPTY*/ { $$ = FALSE; }
3893 function_with_argtypes_list:
3894 function_with_argtypes { $$ = list_make1($1); }
3895 | function_with_argtypes_list ',' function_with_argtypes
3896 { $$ = lappend($1, $3); }
3899 function_with_argtypes:
3902 FuncWithArgs *n = makeNode(FuncWithArgs);
3904 n->funcargs = extractArgTypes($2);
3909 /*****************************************************************************
3911 * GRANT and REVOKE ROLE statements
3913 *****************************************************************************/
3916 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
3918 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3920 n->granted_roles = $2;
3921 n->grantee_roles = $4;
3929 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
3931 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3932 n->is_grant = false;
3933 n->admin_opt = false;
3934 n->granted_roles = $2;
3935 n->grantee_roles = $4;
3939 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
3941 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3942 n->is_grant = false;
3943 n->admin_opt = true;
3944 n->granted_roles = $5;
3945 n->grantee_roles = $7;
3951 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
3952 | /*EMPTY*/ { $$ = FALSE; }
3955 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
3956 | /*EMPTY*/ { $$ = NULL; }
3960 /*****************************************************************************
3962 * QUERY: CREATE INDEX
3964 * Note: we can't factor CONCURRENTLY into a separate production without
3965 * making it a reserved word.
3967 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
3968 * willing to make TABLESPACE a fully reserved word.
3969 *****************************************************************************/
3971 IndexStmt: CREATE index_opt_unique INDEX index_name
3972 ON qualified_name access_method_clause '(' index_params ')'
3973 opt_definition OptTableSpace where_clause
3975 IndexStmt *n = makeNode(IndexStmt);
3977 n->concurrent = false;
3980 n->accessMethod = $7;
3981 n->indexParams = $9;
3983 n->tableSpace = $12;
3984 n->whereClause = $13;
3987 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
3988 ON qualified_name access_method_clause '(' index_params ')'
3989 opt_definition OptTableSpace where_clause
3991 IndexStmt *n = makeNode(IndexStmt);
3993 n->concurrent = true;
3996 n->accessMethod = $8;
3997 n->indexParams = $10;
3999 n->tableSpace = $13;
4000 n->whereClause = $14;
4006 UNIQUE { $$ = TRUE; }
4007 | /*EMPTY*/ { $$ = FALSE; }
4010 access_method_clause:
4011 USING access_method { $$ = $2; }
4012 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4015 index_params: index_elem { $$ = list_make1($1); }
4016 | index_params ',' index_elem { $$ = lappend($1, $3); }
4020 * Index attributes can be either simple column references, or arbitrary
4021 * expressions in parens. For backwards-compatibility reasons, we allow
4022 * an expression that's just a function call to be written without parens.
4024 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4026 $$ = makeNode(IndexElem);
4031 $$->nulls_ordering = $4;
4033 | func_expr opt_class opt_asc_desc opt_nulls_order
4035 $$ = makeNode(IndexElem);
4040 $$->nulls_ordering = $4;
4042 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4044 $$ = makeNode(IndexElem);
4049 $$->nulls_ordering = $6;
4053 opt_class: any_name { $$ = $1; }
4054 | USING any_name { $$ = $2; }
4055 | /*EMPTY*/ { $$ = NIL; }
4058 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4059 | DESC { $$ = SORTBY_DESC; }
4060 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4063 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4064 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4065 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4069 /*****************************************************************************
4072 * create [or replace] function <fname>
4073 * [(<type-1> { , <type-n>})]
4075 * as <filename or code in language as appropriate>
4076 * language <lang> [with parameters]
4078 *****************************************************************************/
4081 CREATE opt_or_replace FUNCTION func_name func_args
4082 RETURNS func_return createfunc_opt_list opt_definition
4084 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4093 | CREATE opt_or_replace FUNCTION func_name func_args
4094 createfunc_opt_list opt_definition
4096 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4100 n->returnType = NULL;
4108 OR REPLACE { $$ = TRUE; }
4109 | /*EMPTY*/ { $$ = FALSE; }
4112 func_args: '(' func_args_list ')' { $$ = $2; }
4113 | '(' ')' { $$ = NIL; }
4117 func_arg { $$ = list_make1($1); }
4118 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4122 * The style with arg_class first is SQL99 standard, but Oracle puts
4123 * param_name first; accept both since it's likely people will try both
4124 * anyway. Don't bother trying to save productions by letting arg_class
4125 * have an empty alternative ... you'll get shift/reduce conflicts.
4127 * We can catch over-specified arguments here if we want to,
4128 * but for now better to silently swallow typmod, etc.
4129 * - thomas 2000-03-22
4132 arg_class param_name func_type
4134 FunctionParameter *n = makeNode(FunctionParameter);
4140 | param_name arg_class func_type
4142 FunctionParameter *n = makeNode(FunctionParameter);
4148 | param_name func_type
4150 FunctionParameter *n = makeNode(FunctionParameter);
4153 n->mode = FUNC_PARAM_IN;
4156 | arg_class func_type
4158 FunctionParameter *n = makeNode(FunctionParameter);
4166 FunctionParameter *n = makeNode(FunctionParameter);
4169 n->mode = FUNC_PARAM_IN;
4174 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4175 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4176 | OUT_P { $$ = FUNC_PARAM_OUT; }
4177 | INOUT { $$ = FUNC_PARAM_INOUT; }
4178 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4182 * Ideally param_name should be ColId, but that causes too many conflicts.
4184 param_name: type_function_name
4190 /* We can catch over-specified results here if we want to,
4191 * but for now better to silently swallow typmod, etc.
4192 * - thomas 2000-03-22
4199 * We would like to make the %TYPE productions here be ColId attrs etc,
4200 * but that causes reduce/reduce conflicts. type_function_name
4201 * is next best choice.
4203 func_type: Typename { $$ = $1; }
4204 | type_function_name attrs '%' TYPE_P
4206 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4207 $$->pct_type = true;
4210 | SETOF type_function_name attrs '%' TYPE_P
4212 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4213 $$->pct_type = true;
4220 createfunc_opt_list:
4221 /* Must be at least one to prevent conflict */
4222 createfunc_opt_item { $$ = list_make1($1); }
4223 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4227 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4229 common_func_opt_item:
4230 CALLED ON NULL_P INPUT_P
4232 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4234 | RETURNS NULL_P ON NULL_P INPUT_P
4236 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4240 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4244 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4248 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4252 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4254 | EXTERNAL SECURITY DEFINER
4256 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4258 | EXTERNAL SECURITY INVOKER
4260 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4264 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4268 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4272 $$ = makeDefElem("cost", (Node *)$2);
4276 $$ = makeDefElem("rows", (Node *)$2);
4280 /* we abuse the normal content of a DefElem here */
4281 $$ = makeDefElem("set", (Node *)$1);
4285 createfunc_opt_item:
4288 $$ = makeDefElem("as", (Node *)$2);
4290 | LANGUAGE ColId_or_Sconst
4292 $$ = makeDefElem("language", (Node *)makeString($2));
4294 | common_func_opt_item
4300 func_as: Sconst { $$ = list_make1(makeString($1)); }
4303 $$ = list_make2(makeString($1), makeString($3));
4308 WITH definition { $$ = $2; }
4309 | /*EMPTY*/ { $$ = NIL; }
4312 /*****************************************************************************
4315 * RENAME and OWNER subcommands are already provided by the generic
4316 * ALTER infrastructure, here we just specify alterations that can
4317 * only be applied to functions.
4319 *****************************************************************************/
4321 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4323 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4331 /* At least one option must be specified */
4332 common_func_opt_item { $$ = list_make1($1); }
4333 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4336 /* Ignored, merely for SQL compliance */
4343 /*****************************************************************************
4347 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4348 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4349 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4351 *****************************************************************************/
4354 DROP FUNCTION func_name func_args opt_drop_behavior
4356 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4357 n->kind = OBJECT_FUNCTION;
4359 n->args = extractArgTypes($4);
4361 n->missing_ok = false;
4364 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4366 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4367 n->kind = OBJECT_FUNCTION;
4369 n->args = extractArgTypes($6);
4371 n->missing_ok = true;
4377 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4379 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4380 n->kind = OBJECT_AGGREGATE;
4384 n->missing_ok = false;
4387 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4389 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4390 n->kind = OBJECT_AGGREGATE;
4394 n->missing_ok = true;
4400 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
4402 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4403 n->kind = OBJECT_OPERATOR;
4407 n->missing_ok = false;
4410 | DROP OPERATOR IF_P EXISTS any_operator '(' oper_argtypes ')' opt_drop_behavior
4412 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4413 n->kind = OBJECT_OPERATOR;
4417 n->missing_ok = true;
4426 (errcode(ERRCODE_SYNTAX_ERROR),
4427 errmsg("missing argument"),
4428 errhint("Use NONE to denote the missing argument of a unary operator.")));
4430 | Typename ',' Typename
4431 { $$ = list_make2($1, $3); }
4432 | NONE ',' Typename /* left unary */
4433 { $$ = list_make2(NULL, $3); }
4434 | Typename ',' NONE /* right unary */
4435 { $$ = list_make2($1, NULL); }
4440 { $$ = list_make1(makeString($1)); }
4441 | ColId '.' any_operator
4442 { $$ = lcons(makeString($1), $3); }
4446 /*****************************************************************************
4448 * CREATE CAST / DROP CAST
4450 *****************************************************************************/
4452 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4453 WITH FUNCTION function_with_argtypes cast_context
4455 CreateCastStmt *n = makeNode(CreateCastStmt);
4459 n->context = (CoercionContext) $11;
4462 | CREATE CAST '(' Typename AS Typename ')'
4463 WITHOUT FUNCTION cast_context
4465 CreateCastStmt *n = makeNode(CreateCastStmt);
4469 n->context = (CoercionContext) $10;
4474 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
4475 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
4476 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
4480 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
4482 DropCastStmt *n = makeNode(DropCastStmt);
4491 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
4492 | /*EMPTY*/ { $$ = FALSE; }
4496 /*****************************************************************************
4500 * REINDEX type <name> [FORCE]
4502 * FORCE no longer does anything, but we accept it for backwards compatibility
4503 *****************************************************************************/
4506 REINDEX reindex_type qualified_name opt_force
4508 ReindexStmt *n = makeNode(ReindexStmt);
4514 | REINDEX SYSTEM_P name opt_force
4516 ReindexStmt *n = makeNode(ReindexStmt);
4517 n->kind = OBJECT_DATABASE;
4520 n->do_system = true;
4524 | REINDEX DATABASE name opt_force
4526 ReindexStmt *n = makeNode(ReindexStmt);
4527 n->kind = OBJECT_DATABASE;
4530 n->do_system = true;
4537 INDEX { $$ = OBJECT_INDEX; }
4538 | TABLE { $$ = OBJECT_TABLE; }
4541 opt_force: FORCE { $$ = TRUE; }
4542 | /* EMPTY */ { $$ = FALSE; }
4546 /*****************************************************************************
4548 * ALTER THING name RENAME TO newname
4550 *****************************************************************************/
4552 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
4554 RenameStmt *n = makeNode(RenameStmt);
4555 n->renameType = OBJECT_AGGREGATE;
4561 | ALTER CONVERSION_P any_name RENAME TO name
4563 RenameStmt *n = makeNode(RenameStmt);
4564 n->renameType = OBJECT_CONVERSION;
4569 | ALTER DATABASE database_name RENAME TO database_name
4571 RenameStmt *n = makeNode(RenameStmt);
4572 n->renameType = OBJECT_DATABASE;
4577 | ALTER FUNCTION function_with_argtypes RENAME TO name
4579 RenameStmt *n = makeNode(RenameStmt);
4580 n->renameType = OBJECT_FUNCTION;
4581 n->object = $3->funcname;
4582 n->objarg = $3->funcargs;
4586 | ALTER GROUP_P RoleId RENAME TO RoleId
4588 RenameStmt *n = makeNode(RenameStmt);
4589 n->renameType = OBJECT_ROLE;
4594 | ALTER opt_procedural LANGUAGE name RENAME TO name
4596 RenameStmt *n = makeNode(RenameStmt);
4597 n->renameType = OBJECT_LANGUAGE;
4602 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
4604 RenameStmt *n = makeNode(RenameStmt);
4605 n->renameType = OBJECT_OPCLASS;
4611 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
4613 RenameStmt *n = makeNode(RenameStmt);
4614 n->renameType = OBJECT_OPFAMILY;
4620 | ALTER SCHEMA name RENAME TO name
4622 RenameStmt *n = makeNode(RenameStmt);
4623 n->renameType = OBJECT_SCHEMA;
4628 | ALTER TABLE relation_expr RENAME TO name
4630 RenameStmt *n = makeNode(RenameStmt);
4631 n->renameType = OBJECT_TABLE;
4637 | ALTER SEQUENCE relation_expr RENAME TO name
4639 RenameStmt *n = makeNode(RenameStmt);
4640 n->renameType = OBJECT_SEQUENCE;
4646 | ALTER VIEW relation_expr RENAME TO name
4648 RenameStmt *n = makeNode(RenameStmt);
4649 n->renameType = OBJECT_VIEW;
4655 | ALTER INDEX relation_expr RENAME TO name
4657 RenameStmt *n = makeNode(RenameStmt);
4658 n->renameType = OBJECT_INDEX;
4664 | ALTER TABLE relation_expr RENAME opt_column name TO name
4666 RenameStmt *n = makeNode(RenameStmt);
4667 n->renameType = OBJECT_COLUMN;
4673 | ALTER TRIGGER name ON relation_expr RENAME TO name
4675 RenameStmt *n = makeNode(RenameStmt);
4676 n->renameType = OBJECT_TRIGGER;
4682 | ALTER ROLE RoleId RENAME TO RoleId
4684 RenameStmt *n = makeNode(RenameStmt);
4685 n->renameType = OBJECT_ROLE;
4690 | ALTER USER RoleId RENAME TO RoleId
4692 RenameStmt *n = makeNode(RenameStmt);
4693 n->renameType = OBJECT_ROLE;
4698 | ALTER TABLESPACE name RENAME TO name
4700 RenameStmt *n = makeNode(RenameStmt);
4701 n->renameType = OBJECT_TABLESPACE;
4706 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
4708 RenameStmt *n = makeNode(RenameStmt);
4709 n->renameType = OBJECT_TSPARSER;
4714 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
4716 RenameStmt *n = makeNode(RenameStmt);
4717 n->renameType = OBJECT_TSDICTIONARY;
4722 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
4724 RenameStmt *n = makeNode(RenameStmt);
4725 n->renameType = OBJECT_TSTEMPLATE;
4730 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
4732 RenameStmt *n = makeNode(RenameStmt);
4733 n->renameType = OBJECT_TSCONFIGURATION;
4738 | ALTER TYPE_P any_name RENAME TO name
4740 RenameStmt *n = makeNode(RenameStmt);
4741 n->renameType = OBJECT_TYPE;
4748 opt_column: COLUMN { $$ = COLUMN; }
4749 | /*EMPTY*/ { $$ = 0; }
4752 /*****************************************************************************
4754 * ALTER THING name SET SCHEMA name
4756 *****************************************************************************/
4758 AlterObjectSchemaStmt:
4759 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
4761 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4762 n->objectType = OBJECT_AGGREGATE;
4768 | ALTER DOMAIN_P any_name SET SCHEMA name
4770 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4771 n->objectType = OBJECT_DOMAIN;
4776 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
4778 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4779 n->objectType = OBJECT_FUNCTION;
4780 n->object = $3->funcname;
4781 n->objarg = $3->funcargs;
4785 | ALTER SEQUENCE relation_expr SET SCHEMA name
4787 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4788 n->objectType = OBJECT_SEQUENCE;
4793 | ALTER TABLE relation_expr SET SCHEMA name
4795 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4796 n->objectType = OBJECT_TABLE;
4801 | ALTER TYPE_P any_name SET SCHEMA name
4803 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4804 n->objectType = OBJECT_TYPE;
4811 /*****************************************************************************
4813 * ALTER THING name OWNER TO newname
4815 *****************************************************************************/
4817 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
4819 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4820 n->objectType = OBJECT_AGGREGATE;
4826 | ALTER CONVERSION_P any_name OWNER TO RoleId
4828 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4829 n->objectType = OBJECT_CONVERSION;
4834 | ALTER DATABASE database_name OWNER TO RoleId
4836 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4837 n->objectType = OBJECT_DATABASE;
4838 n->object = list_make1(makeString($3));
4842 | ALTER DOMAIN_P any_name OWNER TO RoleId
4844 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4845 n->objectType = OBJECT_DOMAIN;
4850 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
4852 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4853 n->objectType = OBJECT_FUNCTION;
4854 n->object = $3->funcname;
4855 n->objarg = $3->funcargs;
4859 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
4861 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4862 n->objectType = OBJECT_LANGUAGE;
4863 n->object = list_make1(makeString($4));
4867 | ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO RoleId
4869 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4870 n->objectType = OBJECT_OPERATOR;
4876 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
4878 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4879 n->objectType = OBJECT_OPCLASS;
4885 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
4887 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4888 n->objectType = OBJECT_OPFAMILY;
4894 | ALTER SCHEMA name OWNER TO RoleId
4896 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4897 n->objectType = OBJECT_SCHEMA;
4898 n->object = list_make1(makeString($3));
4902 | ALTER TYPE_P any_name OWNER TO RoleId
4904 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4905 n->objectType = OBJECT_TYPE;
4910 | ALTER TABLESPACE name OWNER TO RoleId
4912 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4913 n->objectType = OBJECT_TABLESPACE;
4914 n->object = list_make1(makeString($3));
4918 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
4920 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4921 n->objectType = OBJECT_TSDICTIONARY;
4926 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
4928 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4929 n->objectType = OBJECT_TSCONFIGURATION;
4937 /*****************************************************************************
4939 * QUERY: Define Rewrite Rule
4941 *****************************************************************************/
4943 RuleStmt: CREATE opt_or_replace RULE name AS
4944 { QueryIsRule=TRUE; }
4945 ON event TO qualified_name where_clause
4946 DO opt_instead RuleActionList
4948 RuleStmt *n = makeNode(RuleStmt);
4952 n->whereClause = $11;
4962 NOTHING { $$ = NIL; }
4963 | RuleActionStmt { $$ = list_make1($1); }
4964 | '(' RuleActionMulti ')' { $$ = $2; }
4967 /* the thrashing around here is to discard "empty" statements... */
4969 RuleActionMulti ';' RuleActionStmtOrEmpty
4971 $$ = lappend($1, $3);
4975 | RuleActionStmtOrEmpty
4977 $$ = list_make1($1);
4991 RuleActionStmtOrEmpty:
4992 RuleActionStmt { $$ = $1; }
4993 | /*EMPTY*/ { $$ = NULL; }
4996 event: SELECT { $$ = CMD_SELECT; }
4997 | UPDATE { $$ = CMD_UPDATE; }
4998 | DELETE_P { $$ = CMD_DELETE; }
4999 | INSERT { $$ = CMD_INSERT; }
5003 INSTEAD { $$ = TRUE; }
5004 | ALSO { $$ = FALSE; }
5005 | /*EMPTY*/ { $$ = FALSE; }
5010 DROP RULE name ON qualified_name opt_drop_behavior
5012 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5016 n->removeType = OBJECT_RULE;
5017 n->missing_ok = false;
5020 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5022 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5026 n->removeType = OBJECT_RULE;
5027 n->missing_ok = true;
5033 /*****************************************************************************
5036 * NOTIFY <qualified_name> can appear both in rule bodies and
5037 * as a query-level command
5039 *****************************************************************************/
5041 NotifyStmt: NOTIFY ColId
5043 NotifyStmt *n = makeNode(NotifyStmt);
5044 n->relation = makeNode(RangeVar);
5045 n->relation->relname = $2;
5046 n->relation->schemaname = NULL;
5051 ListenStmt: LISTEN ColId
5053 ListenStmt *n = makeNode(ListenStmt);
5054 n->relation = makeNode(RangeVar);
5055 n->relation->relname = $2;
5056 n->relation->schemaname = NULL;
5064 UnlistenStmt *n = makeNode(UnlistenStmt);
5065 n->relation = makeNode(RangeVar);
5066 n->relation->relname = $2;
5067 n->relation->schemaname = NULL;
5072 UnlistenStmt *n = makeNode(UnlistenStmt);
5073 n->relation = makeNode(RangeVar);
5074 n->relation->relname = "*";
5075 n->relation->schemaname = NULL;
5081 /*****************************************************************************
5085 * BEGIN / COMMIT / ROLLBACK
5086 * (also older versions END / ABORT)
5088 *****************************************************************************/
5091 ABORT_P opt_transaction
5093 TransactionStmt *n = makeNode(TransactionStmt);
5094 n->kind = TRANS_STMT_ROLLBACK;
5098 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5100 TransactionStmt *n = makeNode(TransactionStmt);
5101 n->kind = TRANS_STMT_BEGIN;
5105 | START TRANSACTION transaction_mode_list_or_empty
5107 TransactionStmt *n = makeNode(TransactionStmt);
5108 n->kind = TRANS_STMT_START;
5112 | COMMIT opt_transaction
5114 TransactionStmt *n = makeNode(TransactionStmt);
5115 n->kind = TRANS_STMT_COMMIT;
5119 | END_P opt_transaction
5121 TransactionStmt *n = makeNode(TransactionStmt);
5122 n->kind = TRANS_STMT_COMMIT;
5126 | ROLLBACK opt_transaction
5128 TransactionStmt *n = makeNode(TransactionStmt);
5129 n->kind = TRANS_STMT_ROLLBACK;
5135 TransactionStmt *n = makeNode(TransactionStmt);
5136 n->kind = TRANS_STMT_SAVEPOINT;
5137 n->options = list_make1(makeDefElem("savepoint_name",
5138 (Node *)makeString($2)));
5141 | RELEASE SAVEPOINT ColId
5143 TransactionStmt *n = makeNode(TransactionStmt);
5144 n->kind = TRANS_STMT_RELEASE;
5145 n->options = list_make1(makeDefElem("savepoint_name",
5146 (Node *)makeString($3)));
5151 TransactionStmt *n = makeNode(TransactionStmt);
5152 n->kind = TRANS_STMT_RELEASE;
5153 n->options = list_make1(makeDefElem("savepoint_name",
5154 (Node *)makeString($2)));
5157 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5159 TransactionStmt *n = makeNode(TransactionStmt);
5160 n->kind = TRANS_STMT_ROLLBACK_TO;
5161 n->options = list_make1(makeDefElem("savepoint_name",
5162 (Node *)makeString($5)));
5165 | ROLLBACK opt_transaction TO ColId
5167 TransactionStmt *n = makeNode(TransactionStmt);
5168 n->kind = TRANS_STMT_ROLLBACK_TO;
5169 n->options = list_make1(makeDefElem("savepoint_name",
5170 (Node *)makeString($4)));
5173 | PREPARE TRANSACTION Sconst
5175 TransactionStmt *n = makeNode(TransactionStmt);
5176 n->kind = TRANS_STMT_PREPARE;
5180 | COMMIT PREPARED Sconst
5182 TransactionStmt *n = makeNode(TransactionStmt);
5183 n->kind = TRANS_STMT_COMMIT_PREPARED;
5187 | ROLLBACK PREPARED Sconst
5189 TransactionStmt *n = makeNode(TransactionStmt);
5190 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5196 opt_transaction: WORK {}
5201 transaction_mode_item:
5202 ISOLATION LEVEL iso_level
5203 { $$ = makeDefElem("transaction_isolation",
5204 makeStringConst($3, NULL)); }
5206 { $$ = makeDefElem("transaction_read_only",
5207 makeIntConst(TRUE)); }
5209 { $$ = makeDefElem("transaction_read_only",
5210 makeIntConst(FALSE)); }
5213 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5214 transaction_mode_list:
5215 transaction_mode_item
5216 { $$ = list_make1($1); }
5217 | transaction_mode_list ',' transaction_mode_item
5218 { $$ = lappend($1, $3); }
5219 | transaction_mode_list transaction_mode_item
5220 { $$ = lappend($1, $2); }
5223 transaction_mode_list_or_empty:
5224 transaction_mode_list
5230 /*****************************************************************************
5233 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5234 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5236 *****************************************************************************/
5238 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5239 AS SelectStmt opt_check_option
5241 ViewStmt *n = makeNode(ViewStmt);
5243 n->view->istemp = $2;
5249 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5250 AS SelectStmt opt_check_option
5252 ViewStmt *n = makeNode(ViewStmt);
5254 n->view->istemp = $4;
5263 * We use merged tokens here to avoid creating shift/reduce conflicts against
5264 * a whole lot of other uses of WITH.
5270 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5271 errmsg("WITH CHECK OPTION is not implemented")));
5273 | WITH_CASCADED CHECK OPTION
5276 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5277 errmsg("WITH CHECK OPTION is not implemented")));
5279 | WITH_LOCAL CHECK OPTION
5282 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5283 errmsg("WITH CHECK OPTION is not implemented")));
5285 | /* EMPTY */ { $$ = NIL; }
5288 /*****************************************************************************
5293 *****************************************************************************/
5295 LoadStmt: LOAD file_name
5297 LoadStmt *n = makeNode(LoadStmt);
5304 /*****************************************************************************
5308 *****************************************************************************/
5311 CREATE DATABASE database_name opt_with createdb_opt_list
5313 CreatedbStmt *n = makeNode(CreatedbStmt);
5321 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5322 | /* EMPTY */ { $$ = NIL; }
5326 TABLESPACE opt_equal name
5328 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5330 | TABLESPACE opt_equal DEFAULT
5332 $$ = makeDefElem("tablespace", NULL);
5334 | LOCATION opt_equal Sconst
5336 $$ = makeDefElem("location", (Node *)makeString($3));
5338 | LOCATION opt_equal DEFAULT
5340 $$ = makeDefElem("location", NULL);
5342 | TEMPLATE opt_equal name
5344 $$ = makeDefElem("template", (Node *)makeString($3));
5346 | TEMPLATE opt_equal DEFAULT
5348 $$ = makeDefElem("template", NULL);
5350 | ENCODING opt_equal Sconst
5352 $$ = makeDefElem("encoding", (Node *)makeString($3));
5354 | ENCODING opt_equal Iconst
5356 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5358 | ENCODING opt_equal DEFAULT
5360 $$ = makeDefElem("encoding", NULL);
5362 | CONNECTION LIMIT opt_equal SignedIconst
5364 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5366 | OWNER opt_equal name
5368 $$ = makeDefElem("owner", (Node *)makeString($3));
5370 | OWNER opt_equal DEFAULT
5372 $$ = makeDefElem("owner", NULL);
5377 * Though the equals sign doesn't match other WITH options, pg_dump uses
5378 * equals for backward compatibility, and it doesn't seem worth removing it.
5385 /*****************************************************************************
5389 *****************************************************************************/
5392 ALTER DATABASE database_name opt_with alterdb_opt_list
5394 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5401 AlterDatabaseSetStmt:
5402 ALTER DATABASE database_name SetResetClause
5404 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5413 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5414 | /* EMPTY */ { $$ = NIL; }
5418 CONNECTION LIMIT opt_equal SignedIconst
5420 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5425 /*****************************************************************************
5427 * DROP DATABASE [ IF EXISTS ]
5429 * This is implicitly CASCADE, no need for drop behavior
5430 *****************************************************************************/
5432 DropdbStmt: DROP DATABASE database_name
5434 DropdbStmt *n = makeNode(DropdbStmt);
5436 n->missing_ok = FALSE;
5439 | DROP DATABASE IF_P EXISTS database_name
5441 DropdbStmt *n = makeNode(DropdbStmt);
5443 n->missing_ok = TRUE;
5449 /*****************************************************************************
5451 * Manipulate a domain
5453 *****************************************************************************/
5456 CREATE DOMAIN_P any_name opt_as Typename ColQualList
5458 CreateDomainStmt *n = makeNode(CreateDomainStmt);
5461 n->constraints = $6;
5467 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
5468 ALTER DOMAIN_P any_name alter_column_default
5470 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5476 /* ALTER DOMAIN <domain> DROP NOT NULL */
5477 | ALTER DOMAIN_P any_name DROP NOT NULL_P
5479 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5484 /* ALTER DOMAIN <domain> SET NOT NULL */
5485 | ALTER DOMAIN_P any_name SET NOT NULL_P
5487 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5492 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
5493 | ALTER DOMAIN_P any_name ADD_P TableConstraint
5495 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5501 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
5502 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
5504 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5518 /*****************************************************************************
5520 * Manipulate a text search dictionary or configuration
5522 *****************************************************************************/
5524 AlterTSDictionaryStmt:
5525 ALTER TEXT_P SEARCH DICTIONARY any_name definition
5527 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
5534 AlterTSConfigurationStmt:
5535 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
5537 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5541 n->override = false;
5545 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
5547 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5555 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
5557 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5560 n->dicts = list_make2($9,$11);
5561 n->override = false;
5565 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
5567 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5570 n->dicts = list_make2($11,$13);
5571 n->override = false;
5575 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
5577 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5580 n->missing_ok = false;
5583 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
5585 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5588 n->missing_ok = true;
5594 /*****************************************************************************
5596 * Manipulate a conversion
5598 * CREATE [DEFAULT] CONVERSION <conversion_name>
5599 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
5601 *****************************************************************************/
5603 CreateConversionStmt:
5604 CREATE opt_default CONVERSION_P any_name FOR Sconst
5605 TO Sconst FROM any_name
5607 CreateConversionStmt *n = makeNode(CreateConversionStmt);
5608 n->conversion_name = $4;
5609 n->for_encoding_name = $6;
5610 n->to_encoding_name = $8;
5617 /*****************************************************************************
5620 * CLUSTER <qualified_name> [ USING <index_name> ]
5622 * CLUSTER <index_name> ON <qualified_name> (for pre-8.3)
5624 *****************************************************************************/
5627 CLUSTER qualified_name cluster_index_specification
5629 ClusterStmt *n = makeNode(ClusterStmt);
5636 ClusterStmt *n = makeNode(ClusterStmt);
5638 n->indexname = NULL;
5641 /* kept for pre-8.3 compatibility */
5642 | CLUSTER index_name ON qualified_name
5644 ClusterStmt *n = makeNode(ClusterStmt);
5651 cluster_index_specification:
5652 USING index_name { $$ = $2; }
5653 | /*EMPTY*/ { $$ = NULL; }
5657 /*****************************************************************************
5663 *****************************************************************************/
5665 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
5667 VacuumStmt *n = makeNode(VacuumStmt);
5671 n->freeze_min_age = $3 ? 0 : -1;
5677 | VACUUM opt_full opt_freeze opt_verbose qualified_name
5679 VacuumStmt *n = makeNode(VacuumStmt);
5683 n->freeze_min_age = $3 ? 0 : -1;
5689 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
5691 VacuumStmt *n = (VacuumStmt *) $5;
5694 n->freeze_min_age = $3 ? 0 : -1;
5701 analyze_keyword opt_verbose
5703 VacuumStmt *n = makeNode(VacuumStmt);
5707 n->freeze_min_age = -1;
5713 | analyze_keyword opt_verbose qualified_name opt_name_list
5715 VacuumStmt *n = makeNode(VacuumStmt);
5719 n->freeze_min_age = -1;
5729 | ANALYSE /* British */ {}
5733 VERBOSE { $$ = TRUE; }
5734 | /*EMPTY*/ { $$ = FALSE; }
5737 opt_full: FULL { $$ = TRUE; }
5738 | /*EMPTY*/ { $$ = FALSE; }
5741 opt_freeze: FREEZE { $$ = TRUE; }
5742 | /*EMPTY*/ { $$ = FALSE; }
5746 '(' name_list ')' { $$ = $2; }
5747 | /*EMPTY*/ { $$ = NIL; }
5751 /*****************************************************************************
5754 * EXPLAIN [ANALYZE] [VERBOSE] query
5756 *****************************************************************************/
5758 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
5760 ExplainStmt *n = makeNode(ExplainStmt);
5774 | ExecuteStmt /* by default all are $$=$1 */
5778 analyze_keyword { $$ = TRUE; }
5779 | /* EMPTY */ { $$ = FALSE; }
5782 /*****************************************************************************
5785 * PREPARE <plan_name> [(args, ...)] AS <query>
5787 *****************************************************************************/
5789 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
5791 PrepareStmt *n = makeNode(PrepareStmt);
5799 prep_type_clause: '(' type_list ')' { $$ = $2; }
5800 | /* EMPTY */ { $$ = NIL; }
5807 | DeleteStmt /* by default all are $$=$1 */
5810 /*****************************************************************************
5812 * EXECUTE <plan_name> [(params, ...)]
5813 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
5815 *****************************************************************************/
5817 ExecuteStmt: EXECUTE name execute_param_clause
5819 ExecuteStmt *n = makeNode(ExecuteStmt);
5825 | CREATE OptTemp TABLE create_as_target AS
5826 EXECUTE name execute_param_clause
5828 ExecuteStmt *n = makeNode(ExecuteStmt);
5831 $4->rel->istemp = $2;
5835 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5836 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
5837 /* ... because it's not implemented, but it could be */
5842 execute_param_clause: '(' expr_list ')' { $$ = $2; }
5843 | /* EMPTY */ { $$ = NIL; }
5846 /*****************************************************************************
5849 * DEALLOCATE [PREPARE] <plan_name>
5851 *****************************************************************************/
5853 DeallocateStmt: DEALLOCATE name
5855 DeallocateStmt *n = makeNode(DeallocateStmt);
5859 | DEALLOCATE PREPARE name
5861 DeallocateStmt *n = makeNode(DeallocateStmt);
5867 DeallocateStmt *n = makeNode(DeallocateStmt);
5871 | DEALLOCATE PREPARE ALL
5873 DeallocateStmt *n = makeNode(DeallocateStmt);
5879 /*****************************************************************************
5884 *****************************************************************************/
5887 INSERT INTO qualified_name insert_rest returning_clause
5890 $4->returningList = $5;
5898 $$ = makeNode(InsertStmt);
5900 $$->selectStmt = $1;
5902 | '(' insert_column_list ')' SelectStmt
5904 $$ = makeNode(InsertStmt);
5906 $$->selectStmt = $4;
5910 $$ = makeNode(InsertStmt);
5912 $$->selectStmt = NULL;
5918 { $$ = list_make1($1); }
5919 | insert_column_list ',' insert_column_item
5920 { $$ = lappend($1, $3); }
5924 ColId opt_indirection
5926 $$ = makeNode(ResTarget);
5928 $$->indirection = $2;
5935 RETURNING target_list { $$ = $2; }
5936 | /* EMPTY */ { $$ = NIL; }
5940 /*****************************************************************************
5945 *****************************************************************************/
5947 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
5948 using_clause where_or_current_clause returning_clause
5950 DeleteStmt *n = makeNode(DeleteStmt);
5952 n->usingClause = $4;
5953 n->whereClause = $5;
5954 n->returningList = $6;
5960 USING from_list { $$ = $2; }
5961 | /*EMPTY*/ { $$ = NIL; }
5964 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
5966 LockStmt *n = makeNode(LockStmt);
5975 opt_lock: IN_P lock_type MODE { $$ = $2; }
5976 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
5979 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
5980 | ROW SHARE { $$ = RowShareLock; }
5981 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
5982 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
5983 | SHARE { $$ = ShareLock; }
5984 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
5985 | EXCLUSIVE { $$ = ExclusiveLock; }
5986 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
5989 opt_nowait: NOWAIT { $$ = TRUE; }
5990 | /*EMPTY*/ { $$ = FALSE; }
5994 /*****************************************************************************
5997 * UpdateStmt (UPDATE)
5999 *****************************************************************************/
6001 UpdateStmt: UPDATE relation_expr_opt_alias
6004 where_or_current_clause
6007 UpdateStmt *n = makeNode(UpdateStmt);
6011 n->whereClause = $6;
6012 n->returningList = $7;
6018 set_clause { $$ = $1; }
6019 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6023 single_set_clause { $$ = list_make1($1); }
6024 | multiple_set_clause { $$ = $1; }
6028 set_target '=' ctext_expr
6031 $$->val = (Node *) $3;
6035 multiple_set_clause:
6036 '(' set_target_list ')' '=' ctext_row
6042 * Break the ctext_row apart, merge individual expressions
6043 * into the destination ResTargets. XXX this approach
6044 * cannot work for general row expressions as sources.
6046 if (list_length($2) != list_length($5))
6048 (errcode(ERRCODE_SYNTAX_ERROR),
6049 errmsg("number of columns does not match number of values")));
6050 forboth(col_cell, $2, val_cell, $5)
6052 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6053 Node *res_val = (Node *) lfirst(val_cell);
6055 res_col->val = res_val;
6063 ColId opt_indirection
6065 $$ = makeNode(ResTarget);
6067 $$->indirection = $2;
6068 $$->val = NULL; /* upper production sets this */
6074 set_target { $$ = list_make1($1); }
6075 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6079 /*****************************************************************************
6084 *****************************************************************************/
6085 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6087 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6089 /* currently we always set FAST_PLAN option */
6090 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6096 cursor_options: /*EMPTY*/ { $$ = 0; }
6097 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6098 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6099 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6100 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6103 opt_hold: /* EMPTY */ { $$ = 0; }
6104 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6105 | WITHOUT HOLD { $$ = 0; }
6108 /*****************************************************************************
6113 *****************************************************************************/
6115 /* A complete SELECT statement looks like this.
6117 * The rule returns either a single SelectStmt node or a tree of them,
6118 * representing a set-operation tree.
6120 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6121 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6122 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6123 * To resolve the ambiguity, we are careful to define the grammar so that
6124 * the decision is staved off as long as possible: as long as we can keep
6125 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6126 * it's no longer possible to do that will we decide that parens belong to
6127 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6128 * parentheses are treated as part of the sub-select. The necessity of doing
6129 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6130 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6131 * SELECT viewpoint when we see the UNION.
6133 * This approach is implemented by defining a nonterminal select_with_parens,
6134 * which represents a SELECT with at least one outer layer of parentheses,
6135 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6136 * in the expression grammar. We will then have shift-reduce conflicts
6137 * which we can resolve in favor of always treating '(' <select> ')' as
6138 * a select_with_parens. To resolve the conflicts, the productions that
6139 * conflict with the select_with_parens productions are manually given
6140 * precedences lower than the precedence of ')', thereby ensuring that we
6141 * shift ')' (and then reduce to select_with_parens) rather than trying to
6142 * reduce the inner <select> nonterminal to something else. We use UMINUS
6143 * precedence for this, which is a fairly arbitrary choice.
6145 * To be able to define select_with_parens itself without ambiguity, we need
6146 * a nonterminal select_no_parens that represents a SELECT structure with no
6147 * outermost parentheses. This is a little bit tedious, but it works.
6149 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6150 * with or without outer parentheses.
6153 SelectStmt: select_no_parens %prec UMINUS
6154 | select_with_parens %prec UMINUS
6158 '(' select_no_parens ')' { $$ = $2; }
6159 | '(' select_with_parens ')' { $$ = $2; }
6163 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6164 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6165 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6169 simple_select { $$ = $1; }
6170 | select_clause sort_clause
6172 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6176 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6178 insertSelectOptions((SelectStmt *) $1, $2, $3,
6179 list_nth($4, 0), list_nth($4, 1));
6182 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6184 insertSelectOptions((SelectStmt *) $1, $2, $4,
6185 list_nth($3, 0), list_nth($3, 1));
6191 simple_select { $$ = $1; }
6192 | select_with_parens { $$ = $1; }
6196 * This rule parses SELECT statements that can appear within set operations,
6197 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6198 * the ordering of the set operations. Without '(' and ')' we want the
6199 * operations to be ordered per the precedence specs at the head of this file.
6201 * As with select_no_parens, simple_select cannot have outer parentheses,
6202 * but can have parenthesized subclauses.
6204 * Note that sort clauses cannot be included at this level --- SQL92 requires
6205 * SELECT foo UNION SELECT bar ORDER BY baz
6207 * (SELECT foo UNION SELECT bar) ORDER BY baz
6209 * SELECT foo UNION (SELECT bar ORDER BY baz)
6210 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
6211 * as part of the select_no_parens production, not simple_select.
6212 * This does not limit functionality, because you can reintroduce sort and
6213 * limit clauses inside parentheses.
6215 * NOTE: only the leftmost component SelectStmt should have INTO.
6216 * However, this is not checked by the grammar; parse analysis must check it.
6219 SELECT opt_distinct target_list
6220 into_clause from_clause where_clause
6221 group_clause having_clause
6223 SelectStmt *n = makeNode(SelectStmt);
6224 n->distinctClause = $2;
6228 n->whereClause = $6;
6229 n->groupClause = $7;
6230 n->havingClause = $8;
6233 | values_clause { $$ = $1; }
6234 | select_clause UNION opt_all select_clause
6236 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6238 | select_clause INTERSECT opt_all select_clause
6240 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6242 | select_clause EXCEPT opt_all select_clause
6244 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6249 INTO OptTempTableName
6251 $$ = makeNode(IntoClause);
6255 $$->onCommit = ONCOMMIT_NOOP;
6256 $$->tableSpaceName = NULL;
6263 * Redundancy here is needed to avoid shift/reduce conflicts,
6264 * since TEMP is not a reserved word. See also OptTemp.
6267 TEMPORARY opt_table qualified_name
6272 | TEMP opt_table qualified_name
6277 | LOCAL TEMPORARY opt_table qualified_name
6282 | LOCAL TEMP opt_table qualified_name
6287 | GLOBAL TEMPORARY opt_table qualified_name
6292 | GLOBAL TEMP opt_table qualified_name
6297 | TABLE qualified_name
6313 opt_all: ALL { $$ = TRUE; }
6314 | DISTINCT { $$ = FALSE; }
6315 | /*EMPTY*/ { $$ = FALSE; }
6318 /* We use (NIL) as a placeholder to indicate that all target expressions
6319 * should be placed in the DISTINCT list during parsetree analysis.
6322 DISTINCT { $$ = list_make1(NIL); }
6323 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6325 | /*EMPTY*/ { $$ = NIL; }
6329 sort_clause { $$ = $1;}
6330 | /*EMPTY*/ { $$ = NIL; }
6334 ORDER BY sortby_list { $$ = $3; }
6338 sortby { $$ = list_make1($1); }
6339 | sortby_list ',' sortby { $$ = lappend($1, $3); }
6342 sortby: a_expr USING qual_all_Op opt_nulls_order
6344 $$ = makeNode(SortBy);
6346 $$->sortby_dir = SORTBY_USING;
6347 $$->sortby_nulls = $4;
6350 | a_expr opt_asc_desc opt_nulls_order
6352 $$ = makeNode(SortBy);
6354 $$->sortby_dir = $2;
6355 $$->sortby_nulls = $3;
6362 LIMIT select_limit_value OFFSET select_offset_value
6363 { $$ = list_make2($4, $2); }
6364 | OFFSET select_offset_value LIMIT select_limit_value
6365 { $$ = list_make2($2, $4); }
6366 | LIMIT select_limit_value
6367 { $$ = list_make2(NULL, $2); }
6368 | OFFSET select_offset_value
6369 { $$ = list_make2($2, NULL); }
6370 | LIMIT select_limit_value ',' select_offset_value
6372 /* Disabled because it was too confusing, bjm 2002-02-18 */
6374 (errcode(ERRCODE_SYNTAX_ERROR),
6375 errmsg("LIMIT #,# syntax is not supported"),
6376 errhint("Use separate LIMIT and OFFSET clauses.")));
6381 select_limit { $$ = $1; }
6383 { $$ = list_make2(NULL,NULL); }
6390 /* LIMIT ALL is represented as a NULL constant */
6391 A_Const *n = makeNode(A_Const);
6392 n->val.type = T_Null;
6397 select_offset_value:
6402 GROUP_P BY expr_list { $$ = $3; }
6403 | /*EMPTY*/ { $$ = NIL; }
6407 HAVING a_expr { $$ = $2; }
6408 | /*EMPTY*/ { $$ = NULL; }
6412 for_locking_items { $$ = $1; }
6413 | FOR READ ONLY { $$ = NIL; }
6416 opt_for_locking_clause:
6417 for_locking_clause { $$ = $1; }
6418 | /* EMPTY */ { $$ = NIL; }
6422 for_locking_item { $$ = list_make1($1); }
6423 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
6427 FOR UPDATE locked_rels_list opt_nowait
6429 LockingClause *n = makeNode(LockingClause);
6431 n->forUpdate = TRUE;
6435 | FOR SHARE locked_rels_list opt_nowait
6437 LockingClause *n = makeNode(LockingClause);
6439 n->forUpdate = FALSE;
6446 OF name_list { $$ = $2; }
6447 | /* EMPTY */ { $$ = NIL; }
6454 SelectStmt *n = makeNode(SelectStmt);
6455 n->valuesLists = list_make1($2);
6458 | values_clause ',' ctext_row
6460 SelectStmt *n = (SelectStmt *) $1;
6461 n->valuesLists = lappend(n->valuesLists, $3);
6467 /*****************************************************************************
6469 * clauses common to all Optimizable Stmts:
6470 * from_clause - allow list of both JOIN expressions and table names
6471 * where_clause - qualifications for joins or restrictions
6473 *****************************************************************************/
6476 FROM from_list { $$ = $2; }
6477 | /*EMPTY*/ { $$ = NIL; }
6481 table_ref { $$ = list_make1($1); }
6482 | from_list ',' table_ref { $$ = lappend($1, $3); }
6486 * table_ref is where an alias clause can be attached. Note we cannot make
6487 * alias_clause have an empty production because that causes parse conflicts
6488 * between table_ref := '(' joined_table ')' alias_clause
6489 * and joined_table := '(' joined_table ')'. So, we must have the
6490 * redundant-looking productions here instead.
6492 table_ref: relation_expr
6496 | relation_expr alias_clause
6503 RangeFunction *n = makeNode(RangeFunction);
6504 n->funccallnode = $1;
6505 n->coldeflist = NIL;
6508 | func_table alias_clause
6510 RangeFunction *n = makeNode(RangeFunction);
6511 n->funccallnode = $1;
6513 n->coldeflist = NIL;
6516 | func_table AS '(' TableFuncElementList ')'
6518 RangeFunction *n = makeNode(RangeFunction);
6519 n->funccallnode = $1;
6523 | func_table AS ColId '(' TableFuncElementList ')'
6525 RangeFunction *n = makeNode(RangeFunction);
6526 Alias *a = makeNode(Alias);
6527 n->funccallnode = $1;
6533 | func_table ColId '(' TableFuncElementList ')'
6535 RangeFunction *n = makeNode(RangeFunction);
6536 Alias *a = makeNode(Alias);
6537 n->funccallnode = $1;
6543 | select_with_parens
6546 * The SQL spec does not permit a subselect
6547 * (<derived_table>) without an alias clause,
6548 * so we don't either. This avoids the problem
6549 * of needing to invent a unique refname for it.
6550 * That could be surmounted if there's sufficient
6551 * popular demand, but for now let's just implement
6552 * the spec and see if anyone complains.
6553 * However, it does seem like a good idea to emit
6554 * an error message that's better than "syntax error".
6556 if (IsA($1, SelectStmt) &&
6557 ((SelectStmt *) $1)->valuesLists)
6559 (errcode(ERRCODE_SYNTAX_ERROR),
6560 errmsg("VALUES in FROM must have an alias"),
6561 errhint("For example, FROM (VALUES ...) [AS] foo.")));
6564 (errcode(ERRCODE_SYNTAX_ERROR),
6565 errmsg("subquery in FROM must have an alias"),
6566 errhint("For example, FROM (SELECT ...) [AS] foo.")));
6569 | select_with_parens alias_clause
6571 RangeSubselect *n = makeNode(RangeSubselect);
6580 | '(' joined_table ')' alias_clause
6589 * It may seem silly to separate joined_table from table_ref, but there is
6590 * method in SQL92's madness: if you don't do it this way you get reduce-
6591 * reduce conflicts, because it's not clear to the parser generator whether
6592 * to expect alias_clause after ')' or not. For the same reason we must
6593 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
6594 * join_type to expand to empty; if we try it, the parser generator can't
6595 * figure out when to reduce an empty join_type right after table_ref.
6597 * Note that a CROSS JOIN is the same as an unqualified
6598 * INNER JOIN, and an INNER JOIN/ON has the same shape
6599 * but a qualification expression to limit membership.
6600 * A NATURAL JOIN implicitly matches column names between
6601 * tables and the shape is determined by which columns are
6602 * in common. We'll collect columns during the later transformations.
6606 '(' joined_table ')'
6610 | table_ref CROSS JOIN table_ref
6612 /* CROSS JOIN is same as unqualified inner join */
6613 JoinExpr *n = makeNode(JoinExpr);
6614 n->jointype = JOIN_INNER;
6615 n->isNatural = FALSE;
6622 | table_ref join_type JOIN table_ref join_qual
6624 JoinExpr *n = makeNode(JoinExpr);
6626 n->isNatural = FALSE;
6629 if ($5 != NULL && IsA($5, List))
6630 n->using = (List *) $5; /* USING clause */
6632 n->quals = $5; /* ON clause */
6635 | table_ref JOIN table_ref join_qual
6637 /* letting join_type reduce to empty doesn't work */
6638 JoinExpr *n = makeNode(JoinExpr);
6639 n->jointype = JOIN_INNER;
6640 n->isNatural = FALSE;
6643 if ($4 != NULL && IsA($4, List))
6644 n->using = (List *) $4; /* USING clause */
6646 n->quals = $4; /* ON clause */
6649 | table_ref NATURAL join_type JOIN table_ref
6651 JoinExpr *n = makeNode(JoinExpr);
6653 n->isNatural = TRUE;
6656 n->using = NIL; /* figure out which columns later... */
6657 n->quals = NULL; /* fill later */
6660 | table_ref NATURAL JOIN table_ref
6662 /* letting join_type reduce to empty doesn't work */
6663 JoinExpr *n = makeNode(JoinExpr);
6664 n->jointype = JOIN_INNER;
6665 n->isNatural = TRUE;
6668 n->using = NIL; /* figure out which columns later... */
6669 n->quals = NULL; /* fill later */
6675 AS ColId '(' name_list ')'
6677 $$ = makeNode(Alias);
6683 $$ = makeNode(Alias);
6686 | ColId '(' name_list ')'
6688 $$ = makeNode(Alias);
6694 $$ = makeNode(Alias);
6699 join_type: FULL join_outer { $$ = JOIN_FULL; }
6700 | LEFT join_outer { $$ = JOIN_LEFT; }
6701 | RIGHT join_outer { $$ = JOIN_RIGHT; }
6702 | INNER_P { $$ = JOIN_INNER; }
6705 /* OUTER is just noise... */
6706 join_outer: OUTER_P { $$ = NULL; }
6707 | /*EMPTY*/ { $$ = NULL; }
6710 /* JOIN qualification clauses
6711 * Possibilities are:
6712 * USING ( column list ) allows only unqualified column names,
6713 * which must match between tables.
6714 * ON expr allows more general qualifications.
6716 * We return USING as a List node, while an ON-expr will not be a List.
6719 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
6720 | ON a_expr { $$ = $2; }
6727 /* default inheritance */
6729 $$->inhOpt = INH_DEFAULT;
6732 | qualified_name '*'
6734 /* inheritance query */
6736 $$->inhOpt = INH_YES;
6739 | ONLY qualified_name
6741 /* no inheritance */
6743 $$->inhOpt = INH_NO;
6746 | ONLY '(' qualified_name ')'
6748 /* no inheritance, SQL99-style syntax */
6750 $$->inhOpt = INH_NO;
6757 * Given "UPDATE foo set set ...", we have to decide without looking any
6758 * further ahead whether the first "set" is an alias or the UPDATE's SET
6759 * keyword. Since "set" is allowed as a column name both interpretations
6760 * are feasible. We resolve the shift/reduce conflict by giving the first
6761 * relation_expr_opt_alias production a higher precedence than the SET token
6762 * has, causing the parser to prefer to reduce, in effect assuming that the
6763 * SET is not an alias.
6765 relation_expr_opt_alias: relation_expr %prec UMINUS
6769 | relation_expr ColId
6771 Alias *alias = makeNode(Alias);
6772 alias->aliasname = $2;
6776 | relation_expr AS ColId
6778 Alias *alias = makeNode(Alias);
6779 alias->aliasname = $3;
6786 func_table: func_expr { $$ = $1; }
6791 WHERE a_expr { $$ = $2; }
6792 | /*EMPTY*/ { $$ = NULL; }
6795 /* variant for UPDATE and DELETE */
6796 where_or_current_clause:
6797 WHERE a_expr { $$ = $2; }
6798 | WHERE CURRENT_P OF name
6800 CurrentOfExpr *n = makeNode(CurrentOfExpr);
6801 /* cvarno is filled in by parse analysis */
6802 n->cursor_name = $4;
6803 n->cursor_param = 0;
6806 | WHERE CURRENT_P OF PARAM
6808 CurrentOfExpr *n = makeNode(CurrentOfExpr);
6809 /* cvarno is filled in by parse analysis */
6810 n->cursor_name = NULL;
6811 n->cursor_param = $4;
6814 | /*EMPTY*/ { $$ = NULL; }
6818 TableFuncElementList:
6821 $$ = list_make1($1);
6823 | TableFuncElementList ',' TableFuncElement
6825 $$ = lappend($1, $3);
6829 TableFuncElement: ColId Typename
6831 ColumnDef *n = makeNode(ColumnDef);
6834 n->constraints = NIL;
6840 /*****************************************************************************
6843 * SQL92 introduces a large amount of type-specific syntax.
6844 * Define individual clauses to handle these cases, and use
6845 * the generic case to handle regular type-extensible Postgres syntax.
6846 * - thomas 1997-10-10
6848 *****************************************************************************/
6850 Typename: SimpleTypename opt_array_bounds
6853 $$->arrayBounds = $2;
6855 | SETOF SimpleTypename opt_array_bounds
6858 $$->arrayBounds = $3;
6861 | SimpleTypename ARRAY '[' Iconst ']'
6863 /* SQL99's redundant syntax */
6865 $$->arrayBounds = list_make1(makeInteger($4));
6867 | SETOF SimpleTypename ARRAY '[' Iconst ']'
6869 /* SQL99's redundant syntax */
6871 $$->arrayBounds = list_make1(makeInteger($5));
6877 opt_array_bounds '[' ']'
6878 { $$ = lappend($1, makeInteger(-1)); }
6879 | opt_array_bounds '[' Iconst ']'
6880 { $$ = lappend($1, makeInteger($3)); }
6886 GenericType { $$ = $1; }
6887 | Numeric { $$ = $1; }
6889 | Character { $$ = $1; }
6890 | ConstDatetime { $$ = $1; }
6891 | ConstInterval opt_interval
6894 if ($2 != INTERVAL_FULL_RANGE)
6895 $$->typmods = list_make1(makeIntConst($2));
6897 | ConstInterval '(' Iconst ')' opt_interval
6900 $$->typmods = list_make2(makeIntConst($5),
6905 /* We have a separate ConstTypename to allow defaulting fixed-length
6906 * types such as CHAR() and BIT() to an unspecified length.
6907 * SQL9x requires that these default to a length of one, but this
6908 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
6909 * where there is an obvious better choice to make.
6910 * Note that ConstInterval is not included here since it must
6911 * be pushed up higher in the rules to accomodate the postfix
6912 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
6913 * the generic-type-name case in AExprConst to avoid premature
6914 * reduce/reduce conflicts against function names.
6917 Numeric { $$ = $1; }
6918 | ConstBit { $$ = $1; }
6919 | ConstCharacter { $$ = $1; }
6920 | ConstDatetime { $$ = $1; }
6924 * GenericType covers all type names that don't have special syntax mandated
6925 * by the standard, including qualified names. We also allow type modifiers.
6926 * To avoid parsing conflicts against function invocations, the modifiers
6927 * have to be shown as expr_list here, but parse analysis will only accept
6928 * constants for them.
6931 type_function_name opt_type_modifiers
6933 $$ = makeTypeName($1);
6937 | type_function_name attrs opt_type_modifiers
6939 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
6945 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
6946 | /* EMPTY */ { $$ = NIL; }
6950 * SQL92 numeric data types
6954 $$ = SystemTypeName("int4");
6959 $$ = SystemTypeName("int4");
6964 $$ = SystemTypeName("int2");
6969 $$ = SystemTypeName("int8");
6974 $$ = SystemTypeName("float4");
6982 | DOUBLE_P PRECISION
6984 $$ = SystemTypeName("float8");
6987 | DECIMAL_P opt_type_modifiers
6989 $$ = SystemTypeName("numeric");
6993 | DEC opt_type_modifiers
6995 $$ = SystemTypeName("numeric");
6999 | NUMERIC opt_type_modifiers
7001 $$ = SystemTypeName("numeric");
7007 $$ = SystemTypeName("bool");
7012 opt_float: '(' Iconst ')'
7015 * Check FLOAT() precision limits assuming IEEE floating
7016 * types - thomas 1997-09-18
7020 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7021 errmsg("precision for type float must be at least 1 bit")));
7023 $$ = SystemTypeName("float4");
7025 $$ = SystemTypeName("float8");
7028 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7029 errmsg("precision for type float must be less than 54 bits")));
7033 $$ = SystemTypeName("float8");
7038 * SQL92 bit-field data types
7039 * The following implements BIT() and BIT VARYING().
7051 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7052 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7053 ConstBit: BitWithLength
7065 BIT opt_varying '(' expr_list ')'
7069 typname = $2 ? "varbit" : "bit";
7070 $$ = SystemTypeName(typname);
7079 /* bit defaults to bit(1), varbit to no limit */
7082 $$ = SystemTypeName("varbit");
7086 $$ = SystemTypeName("bit");
7087 $$->typmods = list_make1(makeIntConst(1));
7095 * SQL92 character data types
7096 * The following implements CHAR() and VARCHAR().
7098 Character: CharacterWithLength
7102 | CharacterWithoutLength
7108 ConstCharacter: CharacterWithLength
7112 | CharacterWithoutLength
7114 /* Length was not specified so allow to be unrestricted.
7115 * This handles problems with fixed-length (bpchar) strings
7116 * which in column definitions must default to a length
7117 * of one, but should not be constrained if the length
7118 * was not specified.
7125 CharacterWithLength: character '(' Iconst ')' opt_charset
7127 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7131 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7138 $$ = SystemTypeName($1);
7139 $$->typmods = list_make1(makeIntConst($3));
7144 CharacterWithoutLength: character opt_charset
7146 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7150 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7157 $$ = SystemTypeName($1);
7159 /* char defaults to char(1), varchar to no limit */
7160 if (strcmp($1, "bpchar") == 0)
7161 $$->typmods = list_make1(makeIntConst(1));
7167 character: CHARACTER opt_varying
7168 { $$ = $2 ? "varchar": "bpchar"; }
7169 | CHAR_P opt_varying
7170 { $$ = $2 ? "varchar": "bpchar"; }
7173 | NATIONAL CHARACTER opt_varying
7174 { $$ = $3 ? "varchar": "bpchar"; }
7175 | NATIONAL CHAR_P opt_varying
7176 { $$ = $3 ? "varchar": "bpchar"; }
7178 { $$ = $2 ? "varchar": "bpchar"; }
7182 VARYING { $$ = TRUE; }
7183 | /*EMPTY*/ { $$ = FALSE; }
7187 CHARACTER SET ColId { $$ = $3; }
7188 | /*EMPTY*/ { $$ = NULL; }
7192 * SQL92 date/time types
7195 TIMESTAMP '(' Iconst ')' opt_timezone
7198 $$ = SystemTypeName("timestamptz");
7200 $$ = SystemTypeName("timestamp");
7201 /* XXX the timezone field seems to be unused
7202 * - thomas 2001-09-06
7205 $$->typmods = list_make1(makeIntConst($3));
7208 | TIMESTAMP opt_timezone
7211 $$ = SystemTypeName("timestamptz");
7213 $$ = SystemTypeName("timestamp");
7214 /* XXX the timezone field seems to be unused
7215 * - thomas 2001-09-06
7220 | TIME '(' Iconst ')' opt_timezone
7223 $$ = SystemTypeName("timetz");
7225 $$ = SystemTypeName("time");
7226 $$->typmods = list_make1(makeIntConst($3));
7232 $$ = SystemTypeName("timetz");
7234 $$ = SystemTypeName("time");
7242 $$ = SystemTypeName("interval");
7248 WITH TIME ZONE { $$ = TRUE; }
7249 | WITHOUT TIME ZONE { $$ = FALSE; }
7250 | /*EMPTY*/ { $$ = FALSE; }
7254 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
7255 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
7256 | DAY_P { $$ = INTERVAL_MASK(DAY); }
7257 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
7258 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
7259 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
7261 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
7263 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
7265 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
7266 | INTERVAL_MASK(MINUTE); }
7268 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
7269 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
7270 | HOUR_P TO MINUTE_P
7271 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
7272 | HOUR_P TO SECOND_P
7273 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
7274 | INTERVAL_MASK(SECOND); }
7275 | MINUTE_P TO SECOND_P
7276 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
7277 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
7281 /*****************************************************************************
7283 * expression grammar
7285 *****************************************************************************/
7288 * General expressions
7289 * This is the heart of the expression syntax.
7291 * We have two expression types: a_expr is the unrestricted kind, and
7292 * b_expr is a subset that must be used in some places to avoid shift/reduce
7293 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
7294 * because that use of AND conflicts with AND as a boolean operator. So,
7295 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
7297 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
7298 * always be used by surrounding it with parens.
7300 * c_expr is all the productions that are common to a_expr and b_expr;
7301 * it's factored out just to eliminate redundant coding.
7303 a_expr: c_expr { $$ = $1; }
7304 | a_expr TYPECAST Typename
7305 { $$ = makeTypeCast($1, $3); }
7306 | a_expr AT TIME ZONE a_expr
7308 FuncCall *n = makeNode(FuncCall);
7309 n->funcname = SystemFuncName("timezone");
7310 n->args = list_make2($5, $1);
7311 n->agg_star = FALSE;
7312 n->agg_distinct = FALSE;
7317 * These operators must be called out explicitly in order to make use
7318 * of yacc/bison's automatic operator-precedence handling. All other
7319 * operator names are handled by the generic productions using "Op",
7320 * below; and all those operators will have the same precedence.
7322 * If you add more explicitly-known operators, be sure to add them
7323 * also to b_expr and to the MathOp list above.
7325 | '+' a_expr %prec UMINUS
7326 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7327 | '-' a_expr %prec UMINUS
7328 { $$ = doNegate($2, @1); }
7330 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7332 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7334 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7336 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7338 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7340 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7342 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7344 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7346 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7348 | a_expr qual_Op a_expr %prec Op
7349 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7350 | qual_Op a_expr %prec Op
7351 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7352 | a_expr qual_Op %prec POSTFIXOP
7353 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7356 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
7358 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
7360 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
7362 | a_expr LIKE a_expr
7363 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
7364 | a_expr LIKE a_expr ESCAPE a_expr
7366 FuncCall *n = makeNode(FuncCall);
7367 n->funcname = SystemFuncName("like_escape");
7368 n->args = list_make2($3, $5);
7369 n->agg_star = FALSE;
7370 n->agg_distinct = FALSE;
7372 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
7374 | a_expr NOT LIKE a_expr
7375 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
7376 | a_expr NOT LIKE a_expr ESCAPE a_expr
7378 FuncCall *n = makeNode(FuncCall);
7379 n->funcname = SystemFuncName("like_escape");
7380 n->args = list_make2($4, $6);
7381 n->agg_star = FALSE;
7382 n->agg_distinct = FALSE;
7384 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
7386 | a_expr ILIKE a_expr
7387 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
7388 | a_expr ILIKE a_expr ESCAPE a_expr
7390 FuncCall *n = makeNode(FuncCall);
7391 n->funcname = SystemFuncName("like_escape");
7392 n->args = list_make2($3, $5);
7393 n->agg_star = FALSE;
7394 n->agg_distinct = FALSE;
7396 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
7398 | a_expr NOT ILIKE a_expr
7399 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
7400 | a_expr NOT ILIKE a_expr ESCAPE a_expr
7402 FuncCall *n = makeNode(FuncCall);
7403 n->funcname = SystemFuncName("like_escape");
7404 n->args = list_make2($4, $6);
7405 n->agg_star = FALSE;
7406 n->agg_distinct = FALSE;
7408 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
7411 | a_expr SIMILAR TO a_expr %prec SIMILAR
7413 A_Const *c = makeNode(A_Const);
7414 FuncCall *n = makeNode(FuncCall);
7415 c->val.type = T_Null;
7416 n->funcname = SystemFuncName("similar_escape");
7417 n->args = list_make2($4, (Node *) c);
7418 n->agg_star = FALSE;
7419 n->agg_distinct = FALSE;
7421 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7423 | a_expr SIMILAR TO a_expr ESCAPE a_expr
7425 FuncCall *n = makeNode(FuncCall);
7426 n->funcname = SystemFuncName("similar_escape");
7427 n->args = list_make2($4, $6);
7428 n->agg_star = FALSE;
7429 n->agg_distinct = FALSE;
7431 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7433 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
7435 A_Const *c = makeNode(A_Const);
7436 FuncCall *n = makeNode(FuncCall);
7437 c->val.type = T_Null;
7438 n->funcname = SystemFuncName("similar_escape");
7439 n->args = list_make2($5, (Node *) c);
7440 n->agg_star = FALSE;
7441 n->agg_distinct = FALSE;
7443 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7445 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
7447 FuncCall *n = makeNode(FuncCall);
7448 n->funcname = SystemFuncName("similar_escape");
7449 n->args = list_make2($5, $7);
7450 n->agg_star = FALSE;
7451 n->agg_distinct = FALSE;
7453 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7457 * Define SQL92-style Null test clause.
7458 * Allow two forms described in the standard:
7461 * Allow two SQL extensions
7467 NullTest *n = makeNode(NullTest);
7468 n->arg = (Expr *) $1;
7469 n->nulltesttype = IS_NULL;
7474 NullTest *n = makeNode(NullTest);
7475 n->arg = (Expr *) $1;
7476 n->nulltesttype = IS_NULL;
7479 | a_expr IS NOT NULL_P
7481 NullTest *n = makeNode(NullTest);
7482 n->arg = (Expr *) $1;
7483 n->nulltesttype = IS_NOT_NULL;
7488 NullTest *n = makeNode(NullTest);
7489 n->arg = (Expr *) $1;
7490 n->nulltesttype = IS_NOT_NULL;
7495 $$ = (Node *)makeOverlaps($1, $3, @2);
7499 BooleanTest *b = makeNode(BooleanTest);
7500 b->arg = (Expr *) $1;
7501 b->booltesttype = IS_TRUE;
7504 | a_expr IS NOT TRUE_P
7506 BooleanTest *b = makeNode(BooleanTest);
7507 b->arg = (Expr *) $1;
7508 b->booltesttype = IS_NOT_TRUE;
7513 BooleanTest *b = makeNode(BooleanTest);
7514 b->arg = (Expr *) $1;
7515 b->booltesttype = IS_FALSE;
7518 | a_expr IS NOT FALSE_P
7520 BooleanTest *b = makeNode(BooleanTest);
7521 b->arg = (Expr *) $1;
7522 b->booltesttype = IS_NOT_FALSE;
7527 BooleanTest *b = makeNode(BooleanTest);
7528 b->arg = (Expr *) $1;
7529 b->booltesttype = IS_UNKNOWN;
7532 | a_expr IS NOT UNKNOWN
7534 BooleanTest *b = makeNode(BooleanTest);
7535 b->arg = (Expr *) $1;
7536 b->booltesttype = IS_NOT_UNKNOWN;
7539 | a_expr IS DISTINCT FROM a_expr %prec IS
7541 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7543 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
7545 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7546 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
7551 | a_expr IS OF '(' type_list ')' %prec IS
7553 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7555 | a_expr IS NOT OF '(' type_list ')' %prec IS
7557 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7559 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7561 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7562 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7563 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7566 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7568 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7569 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7570 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7573 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7575 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7576 (Node *) makeA_Expr(AEXPR_AND, NIL,
7577 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7578 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7580 (Node *) makeA_Expr(AEXPR_AND, NIL,
7581 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
7582 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
7586 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7588 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7589 (Node *) makeA_Expr(AEXPR_OR, NIL,
7590 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7591 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7593 (Node *) makeA_Expr(AEXPR_OR, NIL,
7594 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
7595 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
7599 | a_expr IN_P in_expr
7601 /* in_expr returns a SubLink or a list of a_exprs */
7602 if (IsA($3, SubLink))
7604 /* generate foo = ANY (subquery) */
7605 SubLink *n = (SubLink *) $3;
7606 n->subLinkType = ANY_SUBLINK;
7608 n->operName = list_make1(makeString("="));
7613 /* generate scalar IN expression */
7614 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
7617 | a_expr NOT IN_P in_expr
7619 /* in_expr returns a SubLink or a list of a_exprs */
7620 if (IsA($4, SubLink))
7622 /* generate NOT (foo = ANY (subquery)) */
7623 /* Make an = ANY node */
7624 SubLink *n = (SubLink *) $4;
7625 n->subLinkType = ANY_SUBLINK;
7627 n->operName = list_make1(makeString("="));
7628 /* Stick a NOT on top */
7629 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
7633 /* generate scalar NOT IN expression */
7634 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
7637 | a_expr subquery_Op sub_type select_with_parens %prec Op
7639 SubLink *n = makeNode(SubLink);
7640 n->subLinkType = $3;
7646 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
7648 if ($3 == ANY_SUBLINK)
7649 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
7651 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
7653 | UNIQUE select_with_parens
7655 /* Not sure how to get rid of the parentheses
7656 * but there are lots of shift/reduce errors without them.
7658 * Should be able to implement this by plopping the entire
7659 * select into a node, then transforming the target expressions
7660 * from whatever they are into count(*), and testing the
7661 * entire result equal to one.
7662 * But, will probably implement a separate node in the executor.
7665 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7666 errmsg("UNIQUE predicate is not yet implemented")));
7668 | a_expr IS DOCUMENT_P %prec IS
7670 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1));
7672 | a_expr IS NOT DOCUMENT_P %prec IS
7674 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7675 makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1)),
7681 * Restricted expressions
7683 * b_expr is a subset of the complete expression syntax defined by a_expr.
7685 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
7686 * cause trouble in the places where b_expr is used. For simplicity, we
7687 * just eliminate all the boolean-keyword-operator productions from b_expr.
7691 | b_expr TYPECAST Typename
7692 { $$ = makeTypeCast($1, $3); }
7693 | '+' b_expr %prec UMINUS
7694 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7695 | '-' b_expr %prec UMINUS
7696 { $$ = doNegate($2, @1); }
7698 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7700 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7702 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7704 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7706 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7708 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7710 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7712 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7714 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7715 | b_expr qual_Op b_expr %prec Op
7716 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7717 | qual_Op b_expr %prec Op
7718 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7719 | b_expr qual_Op %prec POSTFIXOP
7720 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7721 | b_expr IS DISTINCT FROM b_expr %prec IS
7723 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7725 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
7727 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
7728 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
7730 | b_expr IS OF '(' type_list ')' %prec IS
7732 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7734 | b_expr IS NOT OF '(' type_list ')' %prec IS
7736 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7738 | b_expr IS DOCUMENT_P %prec IS
7740 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1));
7742 | b_expr IS NOT DOCUMENT_P %prec IS
7744 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7745 makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1)),
7751 * Productions that can be used in both a_expr and b_expr.
7753 * Note: productions that refer recursively to a_expr or b_expr mostly
7754 * cannot appear here. However, it's OK to refer to a_exprs that occur
7755 * inside parentheses, such as function arguments; that cannot introduce
7756 * ambiguity to the b_expr syntax.
7758 c_expr: columnref { $$ = $1; }
7759 | AexprConst { $$ = $1; }
7760 | PARAM opt_indirection
7762 ParamRef *p = makeNode(ParamRef);
7766 A_Indirection *n = makeNode(A_Indirection);
7767 n->arg = (Node *) p;
7768 n->indirection = $2;
7774 | '(' a_expr ')' opt_indirection
7778 A_Indirection *n = makeNode(A_Indirection);
7780 n->indirection = $4;
7790 | select_with_parens %prec UMINUS
7792 SubLink *n = makeNode(SubLink);
7793 n->subLinkType = EXPR_SUBLINK;
7799 | EXISTS select_with_parens
7801 SubLink *n = makeNode(SubLink);
7802 n->subLinkType = EXISTS_SUBLINK;
7808 | ARRAY select_with_parens
7810 SubLink *n = makeNode(SubLink);
7811 n->subLinkType = ARRAY_SUBLINK;
7821 RowExpr *r = makeNode(RowExpr);
7823 r->row_typeid = InvalidOid; /* not analyzed yet */
7829 * func_expr is split out from c_expr just so that we have a classification
7830 * for "everything that is a function call or looks like one". This isn't
7831 * very important, but it saves us having to document which variants are
7832 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
7833 * (Note that many of the special SQL functions wouldn't actually make any
7834 * sense as functional index entries, but we ignore that consideration here.)
7836 func_expr: func_name '(' ')'
7838 FuncCall *n = makeNode(FuncCall);
7841 n->agg_star = FALSE;
7842 n->agg_distinct = FALSE;
7846 | func_name '(' expr_list ')'
7848 FuncCall *n = makeNode(FuncCall);
7851 n->agg_star = FALSE;
7852 n->agg_distinct = FALSE;
7856 | func_name '(' ALL expr_list ')'
7858 FuncCall *n = makeNode(FuncCall);
7861 n->agg_star = FALSE;
7862 n->agg_distinct = FALSE;
7863 /* Ideally we'd mark the FuncCall node to indicate
7864 * "must be an aggregate", but there's no provision
7865 * for that in FuncCall at the moment.
7870 | func_name '(' DISTINCT expr_list ')'
7872 FuncCall *n = makeNode(FuncCall);
7875 n->agg_star = FALSE;
7876 n->agg_distinct = TRUE;
7880 | func_name '(' '*' ')'
7883 * We consider AGGREGATE(*) to invoke a parameterless
7884 * aggregate. This does the right thing for COUNT(*),
7885 * and there are no other aggregates in SQL92 that accept
7888 * The FuncCall node is also marked agg_star = true,
7889 * so that later processing can detect what the argument
7892 FuncCall *n = makeNode(FuncCall);
7896 n->agg_distinct = FALSE;
7903 * Translate as "'now'::text::date".
7905 * We cannot use "'now'::date" because coerce_type() will
7906 * immediately reduce that to a constant representing
7907 * today's date. We need to delay the conversion until
7908 * runtime, else the wrong things will happen when
7909 * CURRENT_DATE is used in a column default value or rule.
7911 * This could be simplified if we had a way to generate
7912 * an expression tree representing runtime application
7913 * of type-input conversion functions. (As of PG 7.3
7914 * that is actually possible, but not clear that we want
7917 A_Const *s = makeNode(A_Const);
7920 s->val.type = T_String;
7921 s->val.val.str = "now";
7922 s->typename = SystemTypeName("text");
7924 d = SystemTypeName("date");
7926 $$ = (Node *)makeTypeCast((Node *)s, d);
7931 * Translate as "'now'::text::timetz".
7932 * See comments for CURRENT_DATE.
7934 A_Const *s = makeNode(A_Const);
7937 s->val.type = T_String;
7938 s->val.val.str = "now";
7939 s->typename = SystemTypeName("text");
7941 d = SystemTypeName("timetz");
7943 $$ = (Node *)makeTypeCast((Node *)s, d);
7945 | CURRENT_TIME '(' Iconst ')'
7948 * Translate as "'now'::text::timetz(n)".
7949 * See comments for CURRENT_DATE.
7951 A_Const *s = makeNode(A_Const);
7954 s->val.type = T_String;
7955 s->val.val.str = "now";
7956 s->typename = SystemTypeName("text");
7957 d = SystemTypeName("timetz");
7958 d->typmods = list_make1(makeIntConst($3));
7960 $$ = (Node *)makeTypeCast((Node *)s, d);
7965 * Translate as "now()", since we have a function that
7966 * does exactly what is needed.
7968 FuncCall *n = makeNode(FuncCall);
7969 n->funcname = SystemFuncName("now");
7971 n->agg_star = FALSE;
7972 n->agg_distinct = FALSE;
7976 | CURRENT_TIMESTAMP '(' Iconst ')'
7979 * Translate as "'now'::text::timestamptz(n)".
7980 * See comments for CURRENT_DATE.
7982 A_Const *s = makeNode(A_Const);
7985 s->val.type = T_String;
7986 s->val.val.str = "now";
7987 s->typename = SystemTypeName("text");
7989 d = SystemTypeName("timestamptz");
7990 d->typmods = list_make1(makeIntConst($3));
7992 $$ = (Node *)makeTypeCast((Node *)s, d);
7997 * Translate as "'now'::text::time".
7998 * See comments for CURRENT_DATE.
8000 A_Const *s = makeNode(A_Const);
8003 s->val.type = T_String;
8004 s->val.val.str = "now";
8005 s->typename = SystemTypeName("text");
8007 d = SystemTypeName("time");
8009 $$ = (Node *)makeTypeCast((Node *)s, d);
8011 | LOCALTIME '(' Iconst ')'
8014 * Translate as "'now'::text::time(n)".
8015 * See comments for CURRENT_DATE.
8017 A_Const *s = makeNode(A_Const);
8020 s->val.type = T_String;
8021 s->val.val.str = "now";
8022 s->typename = SystemTypeName("text");
8023 d = SystemTypeName("time");
8024 d->typmods = list_make1(makeIntConst($3));
8026 $$ = (Node *)makeTypeCast((Node *)s, d);
8031 * Translate as "'now'::text::timestamp".
8032 * See comments for CURRENT_DATE.
8034 A_Const *s = makeNode(A_Const);
8037 s->val.type = T_String;
8038 s->val.val.str = "now";
8039 s->typename = SystemTypeName("text");
8041 d = SystemTypeName("timestamp");
8043 $$ = (Node *)makeTypeCast((Node *)s, d);
8045 | LOCALTIMESTAMP '(' Iconst ')'
8048 * Translate as "'now'::text::timestamp(n)".
8049 * See comments for CURRENT_DATE.
8051 A_Const *s = makeNode(A_Const);
8054 s->val.type = T_String;
8055 s->val.val.str = "now";
8056 s->typename = SystemTypeName("text");
8058 d = SystemTypeName("timestamp");
8059 d->typmods = list_make1(makeIntConst($3));
8061 $$ = (Node *)makeTypeCast((Node *)s, d);
8065 FuncCall *n = makeNode(FuncCall);
8066 n->funcname = SystemFuncName("current_user");
8068 n->agg_star = FALSE;
8069 n->agg_distinct = FALSE;
8075 FuncCall *n = makeNode(FuncCall);
8076 n->funcname = SystemFuncName("current_user");
8078 n->agg_star = FALSE;
8079 n->agg_distinct = FALSE;
8085 FuncCall *n = makeNode(FuncCall);
8086 n->funcname = SystemFuncName("session_user");
8088 n->agg_star = FALSE;
8089 n->agg_distinct = FALSE;
8095 FuncCall *n = makeNode(FuncCall);
8096 n->funcname = SystemFuncName("current_user");
8098 n->agg_star = FALSE;
8099 n->agg_distinct = FALSE;
8103 | CAST '(' a_expr AS Typename ')'
8104 { $$ = makeTypeCast($3, $5); }
8105 | EXTRACT '(' extract_list ')'
8107 FuncCall *n = makeNode(FuncCall);
8108 n->funcname = SystemFuncName("date_part");
8110 n->agg_star = FALSE;
8111 n->agg_distinct = FALSE;
8115 | OVERLAY '(' overlay_list ')'
8117 /* overlay(A PLACING B FROM C FOR D) is converted to
8118 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8119 * overlay(A PLACING B FROM C) is converted to
8120 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8122 FuncCall *n = makeNode(FuncCall);
8123 n->funcname = SystemFuncName("overlay");
8125 n->agg_star = FALSE;
8126 n->agg_distinct = FALSE;
8130 | POSITION '(' position_list ')'
8132 /* position(A in B) is converted to position(B, A) */
8133 FuncCall *n = makeNode(FuncCall);
8134 n->funcname = SystemFuncName("position");
8136 n->agg_star = FALSE;
8137 n->agg_distinct = FALSE;
8141 | SUBSTRING '(' substr_list ')'
8143 /* substring(A from B for C) is converted to
8144 * substring(A, B, C) - thomas 2000-11-28
8146 FuncCall *n = makeNode(FuncCall);
8147 n->funcname = SystemFuncName("substring");
8149 n->agg_star = FALSE;
8150 n->agg_distinct = FALSE;
8154 | TREAT '(' a_expr AS Typename ')'
8156 /* TREAT(expr AS target) converts expr of a particular type to target,
8157 * which is defined to be a subtype of the original expression.
8158 * In SQL99, this is intended for use with structured UDTs,
8159 * but let's make this a generally useful form allowing stronger
8160 * coercions than are handled by implicit casting.
8162 FuncCall *n = makeNode(FuncCall);
8163 /* Convert SystemTypeName() to SystemFuncName() even though
8164 * at the moment they result in the same thing.
8166 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8167 n->args = list_make1($3);
8168 n->agg_star = FALSE;
8169 n->agg_distinct = FALSE;
8173 | TRIM '(' BOTH trim_list ')'
8175 /* various trim expressions are defined in SQL92
8176 * - thomas 1997-07-19
8178 FuncCall *n = makeNode(FuncCall);
8179 n->funcname = SystemFuncName("btrim");
8181 n->agg_star = FALSE;
8182 n->agg_distinct = FALSE;
8186 | TRIM '(' LEADING trim_list ')'
8188 FuncCall *n = makeNode(FuncCall);
8189 n->funcname = SystemFuncName("ltrim");
8191 n->agg_star = FALSE;
8192 n->agg_distinct = FALSE;
8196 | TRIM '(' TRAILING trim_list ')'
8198 FuncCall *n = makeNode(FuncCall);
8199 n->funcname = SystemFuncName("rtrim");
8201 n->agg_star = FALSE;
8202 n->agg_distinct = FALSE;
8206 | TRIM '(' trim_list ')'
8208 FuncCall *n = makeNode(FuncCall);
8209 n->funcname = SystemFuncName("btrim");
8211 n->agg_star = FALSE;
8212 n->agg_distinct = FALSE;
8216 | NULLIF '(' a_expr ',' a_expr ')'
8218 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
8220 | COALESCE '(' expr_list ')'
8222 CoalesceExpr *c = makeNode(CoalesceExpr);
8226 | GREATEST '(' expr_list ')'
8228 MinMaxExpr *v = makeNode(MinMaxExpr);
8230 v->op = IS_GREATEST;
8233 | LEAST '(' expr_list ')'
8235 MinMaxExpr *v = makeNode(MinMaxExpr);
8240 | XMLCONCAT '(' expr_list ')'
8242 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3);
8244 | XMLELEMENT '(' NAME_P ColLabel ')'
8246 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL);
8248 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
8250 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL);
8252 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
8254 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6);
8256 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
8258 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8);
8260 | XMLFOREST '(' xml_attribute_list ')'
8262 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL);
8264 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
8266 XmlExpr *x = (XmlExpr *) makeXmlExpr(IS_XMLPARSE, NULL, NIL,
8268 makeBoolAConst($5)));
8272 | XMLPI '(' NAME_P ColLabel ')'
8274 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL);
8276 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
8278 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6));
8280 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
8282 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
8283 list_make3($3, $5, $6));
8285 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
8287 XmlSerialize *n = makeNode(XmlSerialize);
8298 xml_root_version: VERSION_P a_expr
8300 | VERSION_P NO VALUE_P
8302 A_Const *val = makeNode(A_Const);
8303 val->val.type = T_Null;
8308 opt_xml_root_standalone: ',' STANDALONE_P YES_P
8309 { $$ = (Node *) makeIntConst(XML_STANDALONE_YES); }
8310 | ',' STANDALONE_P NO
8311 { $$ = (Node *) makeIntConst(XML_STANDALONE_NO); }
8312 | ',' STANDALONE_P NO VALUE_P
8313 { $$ = (Node *) makeIntConst(XML_STANDALONE_NO_VALUE); }
8315 { $$ = (Node *) makeIntConst(XML_STANDALONE_OMITTED); }
8318 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
8321 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
8322 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
8325 xml_attribute_el: a_expr AS ColLabel
8327 $$ = makeNode(ResTarget);
8329 $$->indirection = NULL;
8330 $$->val = (Node *) $1;
8335 $$ = makeNode(ResTarget);
8337 $$->indirection = NULL;
8338 $$->val = (Node *) $1;
8343 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
8344 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
8347 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
8348 | STRIP_P WHITESPACE_P { $$ = FALSE; }
8349 | /*EMPTY*/ { $$ = FALSE; }
8353 * Supporting nonterminals for expressions.
8356 /* Explicit row production.
8358 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
8359 * without conflicting with the parenthesized a_expr production. Without the
8360 * ROW keyword, there must be more than one a_expr inside the parens.
8362 row: ROW '(' expr_list ')' { $$ = $3; }
8363 | ROW '(' ')' { $$ = NIL; }
8364 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
8367 sub_type: ANY { $$ = ANY_SUBLINK; }
8368 | SOME { $$ = ANY_SUBLINK; }
8369 | ALL { $$ = ALL_SUBLINK; }
8372 all_Op: Op { $$ = $1; }
8373 | MathOp { $$ = $1; }
8376 MathOp: '+' { $$ = "+"; }
8388 { $$ = list_make1(makeString($1)); }
8389 | OPERATOR '(' any_operator ')'
8395 { $$ = list_make1(makeString($1)); }
8396 | OPERATOR '(' any_operator ')'
8402 { $$ = list_make1(makeString($1)); }
8403 | OPERATOR '(' any_operator ')'
8406 { $$ = list_make1(makeString("~~")); }
8408 { $$ = list_make1(makeString("!~~")); }
8410 { $$ = list_make1(makeString("~~*")); }
8412 { $$ = list_make1(makeString("!~~*")); }
8413 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
8414 * the regular expression is preprocessed by a function (similar_escape),
8415 * and the ~ operator for posix regular expressions is used.
8416 * x SIMILAR TO y -> x ~ similar_escape(y)
8417 * this transformation is made on the fly by the parser upwards.
8418 * however the SubLink structure which handles any/some/all stuff
8419 * is not ready for such a thing.
8425 $$ = list_make1($1);
8427 | expr_list ',' a_expr
8429 $$ = lappend($1, $3);
8433 type_list: Typename { $$ = list_make1($1); }
8434 | type_list ',' Typename { $$ = lappend($1, $3); }
8437 array_expr: '[' expr_list ']'
8439 $$ = makeAArrayExpr($2);
8441 | '[' array_expr_list ']'
8443 $$ = makeAArrayExpr($2);
8447 $$ = makeAArrayExpr(NIL);
8451 array_expr_list: array_expr { $$ = list_make1($1); }
8452 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
8457 extract_arg FROM a_expr
8459 A_Const *n = makeNode(A_Const);
8460 n->val.type = T_String;
8461 n->val.val.str = $1;
8462 $$ = list_make2((Node *) n, $3);
8464 | /*EMPTY*/ { $$ = NIL; }
8467 /* Allow delimited string SCONST in extract_arg as an SQL extension.
8468 * - thomas 2001-04-12
8472 | YEAR_P { $$ = "year"; }
8473 | MONTH_P { $$ = "month"; }
8474 | DAY_P { $$ = "day"; }
8475 | HOUR_P { $$ = "hour"; }
8476 | MINUTE_P { $$ = "minute"; }
8477 | SECOND_P { $$ = "second"; }
8478 | SCONST { $$ = $1; }
8481 /* OVERLAY() arguments
8482 * SQL99 defines the OVERLAY() function:
8483 * o overlay(text placing text from int for int)
8484 * o overlay(text placing text from int)
8487 a_expr overlay_placing substr_from substr_for
8489 $$ = list_make4($1, $2, $3, $4);
8491 | a_expr overlay_placing substr_from
8493 $$ = list_make3($1, $2, $3);
8502 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
8505 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
8506 | /*EMPTY*/ { $$ = NIL; }
8509 /* SUBSTRING() arguments
8510 * SQL9x defines a specific syntax for arguments to SUBSTRING():
8511 * o substring(text from int for int)
8512 * o substring(text from int) get entire string from starting point "int"
8513 * o substring(text for int) get first "int" characters of string
8514 * o substring(text from pattern) get entire string matching pattern
8515 * o substring(text from pattern for escape) same with specified escape char
8516 * We also want to support generic substring functions which accept
8517 * the usual generic list of arguments. So we will accept both styles
8518 * here, and convert the SQL9x style to the generic list for further
8519 * processing. - thomas 2000-11-28
8522 a_expr substr_from substr_for
8524 $$ = list_make3($1, $2, $3);
8526 | a_expr substr_for substr_from
8528 /* not legal per SQL99, but might as well allow it */
8529 $$ = list_make3($1, $3, $2);
8531 | a_expr substr_from
8533 $$ = list_make2($1, $2);
8538 * Since there are no cases where this syntax allows
8539 * a textual FOR value, we forcibly cast the argument
8540 * to int4. The possible matches in pg_proc are
8541 * substring(text,int4) and substring(text,text),
8542 * and we don't want the parser to choose the latter,
8543 * which it is likely to do if the second argument
8544 * is unknown or doesn't have an implicit cast to int4.
8546 A_Const *n = makeNode(A_Const);
8547 n->val.type = T_Integer;
8548 n->val.val.ival = 1;
8549 $$ = list_make3($1, (Node *) n,
8550 makeTypeCast($2, SystemTypeName("int4")));
8561 FROM a_expr { $$ = $2; }
8564 substr_for: FOR a_expr { $$ = $2; }
8567 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
8568 | FROM expr_list { $$ = $2; }
8569 | expr_list { $$ = $1; }
8572 in_expr: select_with_parens
8574 SubLink *n = makeNode(SubLink);
8576 /* other fields will be filled later */
8579 | '(' expr_list ')' { $$ = (Node *)$2; }
8583 * Define SQL92-style case clause.
8584 * - Full specification
8585 * CASE WHEN a = b THEN c ... ELSE d END
8586 * - Implicit argument
8587 * CASE a WHEN b THEN c ... ELSE d END
8589 case_expr: CASE case_arg when_clause_list case_default END_P
8591 CaseExpr *c = makeNode(CaseExpr);
8592 c->casetype = InvalidOid; /* not analyzed yet */
8593 c->arg = (Expr *) $2;
8595 c->defresult = (Expr *) $4;
8601 /* There must be at least one */
8602 when_clause { $$ = list_make1($1); }
8603 | when_clause_list when_clause { $$ = lappend($1, $2); }
8607 WHEN a_expr THEN a_expr
8609 CaseWhen *w = makeNode(CaseWhen);
8610 w->expr = (Expr *) $2;
8611 w->result = (Expr *) $4;
8617 ELSE a_expr { $$ = $2; }
8618 | /*EMPTY*/ { $$ = NULL; }
8621 case_arg: a_expr { $$ = $1; }
8622 | /*EMPTY*/ { $$ = NULL; }
8626 * columnref starts with relation_name not ColId, so that OLD and NEW
8627 * references can be accepted. Note that when there are more than two
8628 * dotted names, the first name is not actually a relation name...
8630 columnref: relation_name
8632 $$ = makeColumnRef($1, NIL, @1);
8634 | relation_name indirection
8636 $$ = makeColumnRef($1, $2, @1);
8643 $$ = (Node *) makeString($2);
8647 $$ = (Node *) makeString("*");
8651 A_Indices *ai = makeNode(A_Indices);
8656 | '[' a_expr ':' a_expr ']'
8658 A_Indices *ai = makeNode(A_Indices);
8666 indirection_el { $$ = list_make1($1); }
8667 | indirection indirection_el { $$ = lappend($1, $2); }
8671 /*EMPTY*/ { $$ = NIL; }
8672 | opt_indirection indirection_el { $$ = lappend($1, $2); }
8675 opt_asymmetric: ASYMMETRIC
8680 * The SQL spec defines "contextually typed value expressions" and
8681 * "contextually typed row value constructors", which for our purposes
8682 * are the same as "a_expr" and "row" except that DEFAULT can appear at
8687 a_expr { $$ = (Node *) $1; }
8688 | DEFAULT { $$ = (Node *) makeNode(SetToDefault); }
8692 ctext_expr { $$ = list_make1($1); }
8693 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
8697 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
8698 * making VALUES a fully reserved word, which will probably break more apps
8699 * than allowing the noise-word is worth.
8701 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
8705 /*****************************************************************************
8707 * target list for SELECT
8709 *****************************************************************************/
8712 target_el { $$ = list_make1($1); }
8713 | target_list ',' target_el { $$ = lappend($1, $3); }
8716 target_el: a_expr AS ColLabel
8718 $$ = makeNode(ResTarget);
8720 $$->indirection = NIL;
8721 $$->val = (Node *)$1;
8725 * We support omitting AS only for column labels that aren't
8726 * any known keyword. There is an ambiguity against postfix
8727 * operators: is "a ! b" an infix expression, or a postfix
8728 * expression and a column label? We prefer to resolve this
8729 * as an infix expression, which we accomplish by assigning
8730 * IDENT a precedence higher than POSTFIXOP.
8734 $$ = makeNode(ResTarget);
8736 $$->indirection = NIL;
8737 $$->val = (Node *)$1;
8742 $$ = makeNode(ResTarget);
8744 $$->indirection = NIL;
8745 $$->val = (Node *)$1;
8750 ColumnRef *n = makeNode(ColumnRef);
8751 n->fields = list_make1(makeString("*"));
8754 $$ = makeNode(ResTarget);
8756 $$->indirection = NIL;
8757 $$->val = (Node *)n;
8763 /*****************************************************************************
8765 * Names and constants
8767 *****************************************************************************/
8770 SpecialRuleRelation { $$ = $1; }
8771 | ColId { $$ = $1; }
8774 qualified_name_list:
8775 qualified_name { $$ = list_make1($1); }
8776 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
8780 * The production for a qualified relation name has to exactly match the
8781 * production for a qualified func_name, because in a FROM clause we cannot
8782 * tell which we are parsing until we see what comes after it ('(' for a
8783 * func_name, something else for a relation). Therefore we allow 'indirection'
8784 * which may contain subscripts, and reject that case in the C code.
8789 $$ = makeNode(RangeVar);
8790 $$->catalogname = NULL;
8791 $$->schemaname = NULL;
8794 | relation_name indirection
8796 check_qualified_name($2);
8797 $$ = makeNode(RangeVar);
8798 switch (list_length($2))
8801 $$->catalogname = NULL;
8802 $$->schemaname = $1;
8803 $$->relname = strVal(linitial($2));
8806 $$->catalogname = $1;
8807 $$->schemaname = strVal(linitial($2));
8808 $$->relname = strVal(lsecond($2));
8812 (errcode(ERRCODE_SYNTAX_ERROR),
8813 errmsg("improper qualified name (too many dotted names): %s",
8814 NameListToString(lcons(makeString($1), $2)))));
8821 { $$ = list_make1(makeString($1)); }
8822 | name_list ',' name
8823 { $$ = lappend($1, makeString($3)); }
8827 name: ColId { $$ = $1; };
8835 attr_name: ColLabel { $$ = $1; };
8837 index_name: ColId { $$ = $1; };
8839 file_name: Sconst { $$ = $1; };
8842 * The production for a qualified func_name has to exactly match the
8843 * production for a qualified columnref, because we cannot tell which we
8844 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
8845 * anything else for a columnref). Therefore we allow 'indirection' which
8846 * may contain subscripts, and reject that case in the C code. (If we
8847 * ever implement SQL99-like methods, such syntax may actually become legal!)
8849 func_name: type_function_name
8850 { $$ = list_make1(makeString($1)); }
8851 | relation_name indirection
8852 { $$ = check_func_name(lcons(makeString($1), $2)); }
8861 A_Const *n = makeNode(A_Const);
8862 n->val.type = T_Integer;
8863 n->val.val.ival = $1;
8868 A_Const *n = makeNode(A_Const);
8869 n->val.type = T_Float;
8870 n->val.val.str = $1;
8875 A_Const *n = makeNode(A_Const);
8876 n->val.type = T_String;
8877 n->val.val.str = $1;
8882 A_Const *n = makeNode(A_Const);
8883 n->val.type = T_BitString;
8884 n->val.val.str = $1;
8889 /* This is a bit constant per SQL99:
8890 * Without Feature F511, "BIT data type",
8891 * a <general literal> shall not be a
8892 * <bit string literal> or a <hex string literal>.
8894 A_Const *n = makeNode(A_Const);
8895 n->val.type = T_BitString;
8896 n->val.val.str = $1;
8901 /* generic type 'literal' syntax */
8902 A_Const *n = makeNode(A_Const);
8903 n->typename = makeTypeNameFromNameList($1);
8904 n->typename->location = @1;
8905 n->val.type = T_String;
8906 n->val.val.str = $2;
8909 | func_name '(' expr_list ')' Sconst
8911 /* generic syntax with a type modifier */
8912 A_Const *n = makeNode(A_Const);
8913 n->typename = makeTypeNameFromNameList($1);
8914 n->typename->typmods = $3;
8915 n->typename->location = @1;
8916 n->val.type = T_String;
8917 n->val.val.str = $5;
8920 | ConstTypename Sconst
8922 A_Const *n = makeNode(A_Const);
8924 n->val.type = T_String;
8925 n->val.val.str = $2;
8928 | ConstInterval Sconst opt_interval
8930 A_Const *n = makeNode(A_Const);
8932 n->val.type = T_String;
8933 n->val.val.str = $2;
8934 /* precision is not specified, but fields may be... */
8935 if ($3 != INTERVAL_FULL_RANGE)
8936 n->typename->typmods = list_make1(makeIntConst($3));
8939 | ConstInterval '(' Iconst ')' Sconst opt_interval
8941 A_Const *n = makeNode(A_Const);
8943 n->val.type = T_String;
8944 n->val.val.str = $5;
8945 n->typename->typmods = list_make2(makeIntConst($6),
8951 $$ = (Node *)makeBoolAConst(TRUE);
8955 $$ = (Node *)makeBoolAConst(FALSE);
8959 A_Const *n = makeNode(A_Const);
8960 n->val.type = T_Null;
8965 Iconst: ICONST { $$ = $1; };
8966 Sconst: SCONST { $$ = $1; };
8967 RoleId: ColId { $$ = $1; };
8969 SignedIconst: ICONST { $$ = $1; }
8970 | '-' ICONST { $$ = - $2; }
8974 * Name classification hierarchy.
8976 * IDENT is the lexeme returned by the lexer for identifiers that match
8977 * no known keyword. In most cases, we can accept certain keywords as
8978 * names, not only IDENTs. We prefer to accept as many such keywords
8979 * as possible to minimize the impact of "reserved words" on programmers.
8980 * So, we divide names into several possible classes. The classification
8981 * is chosen in part to make keywords acceptable as names wherever possible.
8984 /* Column identifier --- names that can be column, table, etc names.
8986 ColId: IDENT { $$ = $1; }
8987 | unreserved_keyword { $$ = pstrdup($1); }
8988 | col_name_keyword { $$ = pstrdup($1); }
8991 /* Type/function identifier --- names that can be type or function names.
8993 type_function_name: IDENT { $$ = $1; }
8994 | unreserved_keyword { $$ = pstrdup($1); }
8995 | type_func_name_keyword { $$ = pstrdup($1); }
8998 /* Column label --- allowed labels in "AS" clauses.
8999 * This presently includes *all* Postgres keywords.
9001 ColLabel: IDENT { $$ = $1; }
9002 | unreserved_keyword { $$ = pstrdup($1); }
9003 | col_name_keyword { $$ = pstrdup($1); }
9004 | type_func_name_keyword { $$ = pstrdup($1); }
9005 | reserved_keyword { $$ = pstrdup($1); }
9010 * Keyword category lists. Generally, every keyword present in
9011 * the Postgres grammar should appear in exactly one of these lists.
9013 * Put a new keyword into the first list that it can go into without causing
9014 * shift or reduce conflicts. The earlier lists define "less reserved"
9015 * categories of keywords.
9017 * Make sure that each keyword's category in keywords.c matches where
9018 * it is listed here. (Someday we may be able to generate these lists and
9019 * keywords.c's table from a common master list.)
9022 /* "Unreserved" keywords --- available for use as any kind of name.
9260 /* Column identifier --- keywords that can be column, table, etc names.
9262 * Many of these keywords will in fact be recognized as type or function
9263 * names too; but they have special productions for the purpose, and so
9264 * can't be treated as "generic" type or function names.
9266 * The type names appearing here are not usable as function names
9267 * because they can be followed by '(' in typename productions, which
9268 * looks too much like a function call for an LR(1) parser.
9318 /* Type/function identifier --- keywords that can be type or function names.
9320 * Most of these are keywords that are used as operators in expressions;
9321 * in general such keywords can't be column names because they would be
9322 * ambiguous with variables, but they are unambiguous as function identifiers.
9324 * Do not include POSITION, SUBSTRING, etc here since they have explicit
9325 * productions in a_expr to support the goofy SQL9x argument syntax.
9326 * - thomas 2000-11-28
9328 type_func_name_keyword:
9351 /* Reserved keyword --- these keywords are usable only as a ColLabel.
9353 * Keywords appear here if they could not be distinguished from variable,
9354 * type, or function names in some contexts. Don't put things here unless
9435 SpecialRuleRelation:
9442 (errcode(ERRCODE_SYNTAX_ERROR),
9443 errmsg("OLD used in query that is not in a rule")));
9451 (errcode(ERRCODE_SYNTAX_ERROR),
9452 errmsg("NEW used in query that is not in a rule")));
9459 makeColumnRef(char *relname, List *indirection, int location)
9462 * Generate a ColumnRef node, with an A_Indirection node added if there
9463 * is any subscripting in the specified indirection list. However,
9464 * any field selection at the start of the indirection list must be
9465 * transposed into the "fields" part of the ColumnRef node.
9467 ColumnRef *c = makeNode(ColumnRef);
9471 c->location = location;
9472 foreach(l, indirection)
9474 if (IsA(lfirst(l), A_Indices))
9476 A_Indirection *i = makeNode(A_Indirection);
9480 /* easy case - all indirection goes to A_Indirection */
9481 c->fields = list_make1(makeString(relname));
9482 i->indirection = indirection;
9486 /* got to split the list in two */
9487 i->indirection = list_copy_tail(indirection, nfields);
9488 indirection = list_truncate(indirection, nfields);
9489 c->fields = lcons(makeString(relname), indirection);
9491 i->arg = (Node *) c;
9496 /* No subscripting, so all indirection gets added to field list */
9497 c->fields = lcons(makeString(relname), indirection);
9502 makeTypeCast(Node *arg, TypeName *typename)
9504 TypeCast *n = makeNode(TypeCast);
9506 n->typename = typename;
9511 makeStringConst(char *str, TypeName *typename)
9513 A_Const *n = makeNode(A_Const);
9515 n->val.type = T_String;
9516 n->val.val.str = str;
9517 n->typename = typename;
9523 makeIntConst(int val)
9525 A_Const *n = makeNode(A_Const);
9526 n->val.type = T_Integer;
9527 n->val.val.ival = val;
9528 n->typename = SystemTypeName("int4");
9534 makeFloatConst(char *str)
9536 A_Const *n = makeNode(A_Const);
9538 n->val.type = T_Float;
9539 n->val.val.str = str;
9540 n->typename = SystemTypeName("float8");
9546 makeAConst(Value *v)
9553 n = makeFloatConst(v->val.str);
9557 n = makeIntConst(v->val.ival);
9562 n = makeStringConst(v->val.str, NULL);
9570 * Create an A_Const node and initialize to a boolean constant.
9573 makeBoolAConst(bool state)
9575 A_Const *n = makeNode(A_Const);
9576 n->val.type = T_String;
9577 n->val.val.str = (state ? "t" : "f");
9578 n->typename = SystemTypeName("bool");
9583 * Create and populate a FuncCall node to support the OVERLAPS operator.
9586 makeOverlaps(List *largs, List *rargs, int location)
9588 FuncCall *n = makeNode(FuncCall);
9590 n->funcname = SystemFuncName("overlaps");
9591 if (list_length(largs) == 1)
9592 largs = lappend(largs, largs);
9593 else if (list_length(largs) != 2)
9595 (errcode(ERRCODE_SYNTAX_ERROR),
9596 errmsg("wrong number of parameters on left side of OVERLAPS expression")));
9597 if (list_length(rargs) == 1)
9598 rargs = lappend(rargs, rargs);
9599 else if (list_length(rargs) != 2)
9601 (errcode(ERRCODE_SYNTAX_ERROR),
9602 errmsg("wrong number of parameters on right side of OVERLAPS expression")));
9603 n->args = list_concat(largs, rargs);
9604 n->agg_star = FALSE;
9605 n->agg_distinct = FALSE;
9606 n->location = location;
9610 /* check_qualified_name --- check the result of qualified_name production
9612 * It's easiest to let the grammar production for qualified_name allow
9613 * subscripts and '*', which we then must reject here.
9616 check_qualified_name(List *names)
9622 if (!IsA(lfirst(i), String))
9623 yyerror("syntax error");
9624 else if (strcmp(strVal(lfirst(i)), "*") == 0)
9625 yyerror("syntax error");
9629 /* check_func_name --- check the result of func_name production
9631 * It's easiest to let the grammar production for func_name allow subscripts
9632 * and '*', which we then must reject here.
9635 check_func_name(List *names)
9641 if (!IsA(lfirst(i), String))
9642 yyerror("syntax error");
9643 else if (strcmp(strVal(lfirst(i)), "*") == 0)
9644 yyerror("syntax error");
9649 /* extractArgTypes()
9650 * Given a list of FunctionParameter nodes, extract a list of just the
9651 * argument types (TypeNames) for input parameters only. This is what
9652 * is needed to look up an existing function, which is what is wanted by
9653 * the productions that use this call.
9656 extractArgTypes(List *parameters)
9661 foreach(i, parameters)
9663 FunctionParameter *p = (FunctionParameter *) lfirst(i);
9665 if (p->mode != FUNC_PARAM_OUT) /* keep if IN or INOUT */
9666 result = lappend(result, p->argType);
9671 /* findLeftmostSelect()
9672 * Find the leftmost component SelectStmt in a set-operation parsetree.
9675 findLeftmostSelect(SelectStmt *node)
9677 while (node && node->op != SETOP_NONE)
9679 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
9683 /* insertSelectOptions()
9684 * Insert ORDER BY, etc into an already-constructed SelectStmt.
9686 * This routine is just to avoid duplicating code in SelectStmt productions.
9689 insertSelectOptions(SelectStmt *stmt,
9690 List *sortClause, List *lockingClause,
9691 Node *limitOffset, Node *limitCount)
9694 * Tests here are to reject constructs like
9695 * (SELECT foo ORDER BY bar) ORDER BY baz
9699 if (stmt->sortClause)
9701 (errcode(ERRCODE_SYNTAX_ERROR),
9702 errmsg("multiple ORDER BY clauses not allowed")));
9703 stmt->sortClause = sortClause;
9705 /* We can handle multiple locking clauses, though */
9706 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
9709 if (stmt->limitOffset)
9711 (errcode(ERRCODE_SYNTAX_ERROR),
9712 errmsg("multiple OFFSET clauses not allowed")));
9713 stmt->limitOffset = limitOffset;
9717 if (stmt->limitCount)
9719 (errcode(ERRCODE_SYNTAX_ERROR),
9720 errmsg("multiple LIMIT clauses not allowed")));
9721 stmt->limitCount = limitCount;
9726 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
9728 SelectStmt *n = makeNode(SelectStmt);
9732 n->larg = (SelectStmt *) larg;
9733 n->rarg = (SelectStmt *) rarg;
9738 * Build a properly-qualified reference to a built-in function.
9741 SystemFuncName(char *name)
9743 return list_make2(makeString("pg_catalog"), makeString(name));
9747 * Build a properly-qualified reference to a built-in type.
9749 * typmod is defaulted, but may be changed afterwards by caller.
9752 SystemTypeName(char *name)
9754 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
9759 * Handle negation of a numeric constant.
9761 * Formerly, we did this here because the optimizer couldn't cope with
9762 * indexquals that looked like "var = -4" --- it wants "var = const"
9763 * and a unary minus operator applied to a constant didn't qualify.
9764 * As of Postgres 7.0, that problem doesn't exist anymore because there
9765 * is a constant-subexpression simplifier in the optimizer. However,
9766 * there's still a good reason for doing this here, which is that we can
9767 * postpone committing to a particular internal representation for simple
9768 * negative constants. It's better to leave "-123.456" in string form
9769 * until we know what the desired type is.
9772 doNegate(Node *n, int location)
9774 if (IsA(n, A_Const))
9776 A_Const *con = (A_Const *)n;
9778 if (con->val.type == T_Integer)
9780 con->val.val.ival = -con->val.val.ival;
9783 if (con->val.type == T_Float)
9785 doNegateFloat(&con->val);
9790 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
9794 doNegateFloat(Value *v)
9796 char *oldval = v->val.str;
9798 Assert(IsA(v, Float));
9802 v->val.str = oldval+1; /* just strip the '-' */
9805 char *newval = (char *) palloc(strlen(oldval) + 2);
9808 strcpy(newval+1, oldval);
9809 v->val.str = newval;
9814 makeAArrayExpr(List *elements)
9816 A_ArrayExpr *n = makeNode(A_ArrayExpr);
9818 n->elements = elements;
9823 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args)
9825 XmlExpr *x = makeNode(XmlExpr);
9830 * named_args is a list of ResTarget; it'll be split apart into separate
9831 * expression and name lists in transformXmlExpr().
9833 x->named_args = named_args;
9840 * Initialize to parse one query string
9845 QueryIsRule = FALSE;
9849 * Must undefine base_yylex before including scan.c, since we want it
9850 * to create the function base_yylex not filtered_base_yylex.