4 /*-------------------------------------------------------------------------
7 * POSTGRESQL BISON rules/actions
9 * Portions Copyright (c) 1996-2009, 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.670 2009/07/16 06:33:43 petere 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 "catalog/pg_trigger.h"
57 #include "commands/defrem.h"
58 #include "nodes/makefuncs.h"
59 #include "nodes/nodeFuncs.h"
60 #include "parser/gramparse.h"
61 #include "parser/parser.h"
62 #include "storage/lmgr.h"
63 #include "utils/date.h"
64 #include "utils/datetime.h"
65 #include "utils/numeric.h"
66 #include "utils/xml.h"
69 /* Location tracking support --- simpler than bison's default */
70 #define YYLLOC_DEFAULT(Current, Rhs, N) \
73 (Current) = (Rhs)[1]; \
75 (Current) = (Rhs)[0]; \
79 * The %name-prefix option below will make bison call base_yylex, but we
80 * really want it to call filtered_base_yylex (see parser.c).
82 #define base_yylex filtered_base_yylex
85 * Bison doesn't allocate anything that needs to live across parser calls,
86 * so we can easily have it use palloc instead of malloc. This prevents
87 * memory leaks if we error out during parsing. Note this only works with
88 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
89 * if possible, so there's not really much problem anyhow, at least if
90 * you're building with gcc.
92 #define YYMALLOC palloc
95 /* Private struct for the result of privilege_target production */
96 typedef struct PrivTarget
98 GrantObjectType objtype;
103 #define parser_yyerror(msg) scanner_yyerror(msg, yyscanner)
104 #define parser_errposition(pos) scanner_errposition(pos, yyscanner)
106 static void base_yyerror(YYLTYPE *yylloc, base_yyscan_t yyscanner,
108 static Node *makeColumnRef(char *colname, List *indirection,
109 int location, base_yyscan_t yyscanner);
110 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
111 static Node *makeStringConst(char *str, int location);
112 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
113 static Node *makeIntConst(int val, int location);
114 static Node *makeFloatConst(char *str, int location);
115 static Node *makeBitStringConst(char *str, int location);
116 static Node *makeNullAConst(int location);
117 static Node *makeAConst(Value *v, int location);
118 static Node *makeBoolAConst(bool state, int location);
119 static FuncCall *makeOverlaps(List *largs, List *rargs,
120 int location, base_yyscan_t yyscanner);
121 static void check_qualified_name(List *names, base_yyscan_t yyscanner);
122 static List *check_func_name(List *names, base_yyscan_t yyscanner);
123 static List *check_indirection(List *indirection, base_yyscan_t yyscanner);
124 static List *extractArgTypes(List *parameters);
125 static SelectStmt *findLeftmostSelect(SelectStmt *node);
126 static void insertSelectOptions(SelectStmt *stmt,
127 List *sortClause, List *lockingClause,
128 Node *limitOffset, Node *limitCount,
129 WithClause *withClause,
130 base_yyscan_t yyscanner);
131 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
132 static Node *doNegate(Node *n, int location);
133 static void doNegateFloat(Value *v);
134 static Node *makeAArrayExpr(List *elements, int location);
135 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
136 List *args, int location);
137 static List *mergeTableFuncParameters(List *func_args, List *columns);
138 static TypeName *TableFuncTypeName(List *columns);
144 %name-prefix="base_yy"
147 %parse-param {base_yyscan_t yyscanner}
148 %lex-param {base_yyscan_t yyscanner}
158 DropBehavior dbehavior;
159 OnCommitAction oncommit;
166 FunctionParameter *fun_param;
167 FunctionParameterMode fun_param_mode;
168 FuncWithArgs *funwithargs;
180 struct PrivTarget *privtarget;
181 AccessPriv *accesspriv;
184 VariableSetStmt *vsetstmt;
187 %type <node> stmt schema_stmt
188 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterFdwStmt
189 AlterForeignServerStmt AlterGroupStmt
190 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
191 AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
192 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
193 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
194 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
195 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
196 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
197 CreateFdwStmt CreateForeignServerStmt CreateAssertStmt CreateTrigStmt
198 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
199 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
200 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
201 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
202 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
203 DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
204 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
205 LockStmt NotifyStmt ExplainableStmt PreparableStmt
206 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
207 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
208 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
209 SelectStmt TransactionStmt TruncateStmt
210 UnlistenStmt UpdateStmt VacuumStmt
211 VariableResetStmt VariableSetStmt VariableShowStmt
212 ViewStmt CheckPointStmt CreateConversionStmt
213 DeallocateStmt PrepareStmt ExecuteStmt
214 DropOwnedStmt ReassignOwnedStmt
215 AlterTSConfigurationStmt AlterTSDictionaryStmt
217 %type <node> select_no_parens select_with_parens select_clause
218 simple_select values_clause
220 %type <node> alter_column_default opclass_item opclass_drop alter_using
221 %type <ival> add_drop opt_asc_desc opt_nulls_order
223 %type <node> alter_table_cmd
224 %type <list> alter_table_cmds
226 %type <dbehavior> opt_drop_behavior
228 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
229 transaction_mode_list
230 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
231 transaction_mode_item
233 %type <ival> opt_lock lock_type cast_context
234 %type <boolean> opt_force opt_or_replace
235 opt_grant_grant_option opt_grant_admin_option
236 opt_nowait opt_if_exists opt_with_data
238 %type <list> OptRoleList
239 %type <defelt> OptRoleElem
242 %type <str> foreign_server_version opt_foreign_server_version
243 %type <str> auth_ident
245 %type <str> OptSchemaName
246 %type <list> OptSchemaEltList
248 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
249 %type <str> opt_lancompiler
251 %type <ival> TriggerEvents TriggerOneEvent
252 %type <value> TriggerFuncArg
254 %type <str> relation_name copy_file_name
255 database_name access_method_clause access_method attr_name
256 index_name name file_name cluster_index_specification
258 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
259 opt_class opt_validator validator_clause
261 %type <range> qualified_name OptConstrFromTable
263 %type <str> all_Op MathOp SpecialRuleRelation
265 %type <str> iso_level opt_encoding
267 %type <list> grantee_list
268 %type <accesspriv> privilege
269 %type <list> privileges privilege_list
270 %type <privtarget> privilege_target
271 %type <funwithargs> function_with_argtypes
272 %type <list> function_with_argtypes_list
274 %type <list> stmtblock stmtmulti
275 OptTableElementList TableElementList OptInherit definition
276 reloptions opt_reloptions
277 OptWith opt_distinct opt_definition func_args func_args_list
278 func_args_with_defaults func_args_with_defaults_list
279 func_as createfunc_opt_list alterfunc_opt_list
280 aggr_args old_aggr_definition old_aggr_list
281 oper_argtypes RuleActionList RuleActionMulti
282 opt_column_list columnList opt_name_list
283 sort_clause opt_sort_clause sortby_list index_params
284 name_list from_clause from_list opt_array_bounds
285 qualified_name_list any_name any_name_list
286 any_operator expr_list attrs
287 target_list insert_column_list set_target_list
288 set_clause_list set_clause multiple_set_clause
289 ctext_expr_list ctext_row def_list indirection opt_indirection
290 reloption_list group_clause TriggerFuncArgs select_limit
291 opt_select_limit opclass_item_list opclass_drop_list
292 opt_opfamily transaction_mode_list_or_empty
293 TableFuncElementList opt_type_modifiers
295 execute_param_clause using_clause returning_clause
296 enum_val_list table_func_column_list
297 create_generic_options alter_generic_options
300 %type <range> OptTempTableName
301 %type <into> into_clause create_as_target
303 %type <defelt> createfunc_opt_item common_func_opt_item
304 %type <fun_param> func_arg func_arg_with_default table_func_column
305 %type <fun_param_mode> arg_class
306 %type <typnam> func_return func_type
308 %type <boolean> TriggerForType OptTemp
309 %type <oncommit> OnCommitOption
311 %type <node> for_locking_item
312 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
313 %type <list> locked_rels_list
314 %type <boolean> opt_all
316 %type <node> join_outer join_qual
317 %type <jtype> join_type
319 %type <list> extract_list overlay_list position_list
320 %type <list> substr_list trim_list
321 %type <list> opt_interval interval_second
322 %type <node> overlay_placing substr_from substr_for
324 %type <boolean> opt_instead opt_analyze
325 %type <boolean> index_opt_unique opt_verbose opt_full
326 %type <boolean> opt_freeze opt_default opt_recheck
327 %type <defelt> opt_binary opt_oids copy_delimiter
329 %type <boolean> copy_from
331 %type <ival> opt_column event cursor_options opt_hold opt_set_data
332 %type <objtype> reindex_type drop_type comment_type
334 %type <node> fetch_direction select_limit_value select_offset_value
335 select_offset_value2 opt_select_fetch_first_value
336 %type <ival> row_or_rows first_or_next
338 %type <list> OptSeqOptList SeqOptList
339 %type <defelt> SeqOptElem
341 %type <istmt> insert_rest
343 %type <vsetstmt> set_rest SetResetClause
345 %type <node> TableElement ConstraintElem TableFuncElement
346 %type <node> columnDef
347 %type <defelt> def_elem reloption_elem old_aggr_elem
348 %type <node> def_arg columnElem where_clause where_or_current_clause
349 a_expr b_expr c_expr func_expr AexprConst indirection_el
350 columnref in_expr having_clause func_table array_expr
351 %type <list> row type_list array_expr_list
352 %type <node> case_expr case_arg when_clause case_default
353 %type <list> when_clause_list
354 %type <ival> sub_type
355 %type <list> OptCreateAs CreateAsList
356 %type <node> CreateAsElement ctext_expr
357 %type <value> NumericOnly
358 %type <alias> alias_clause
359 %type <sortby> sortby
360 %type <ielem> index_elem
361 %type <node> table_ref
362 %type <jexpr> joined_table
363 %type <range> relation_expr
364 %type <range> relation_expr_opt_alias
365 %type <target> target_el single_set_clause set_target insert_column_item
367 %type <str> generic_option_name
368 %type <node> generic_option_arg
369 %type <defelt> generic_option_elem alter_generic_option_elem
370 %type <list> generic_option_list alter_generic_option_list
372 %type <typnam> Typename SimpleTypename ConstTypename
373 GenericType Numeric opt_float
374 Character ConstCharacter
375 CharacterWithLength CharacterWithoutLength
376 ConstDatetime ConstInterval
377 Bit ConstBit BitWithLength BitWithoutLength
378 %type <str> character
379 %type <str> extract_arg
380 %type <str> opt_charset
381 %type <boolean> opt_varying opt_timezone
383 %type <ival> Iconst SignedIconst
384 %type <str> Sconst comment_text
385 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
386 %type <list> var_list
387 %type <str> ColId ColLabel var_name type_function_name param_name
388 %type <node> var_value zone_value
390 %type <keyword> unreserved_keyword type_func_name_keyword
391 %type <keyword> col_name_keyword reserved_keyword
393 %type <node> TableConstraint TableLikeClause
394 %type <list> TableLikeOptionList
395 %type <ival> TableLikeOption
396 %type <list> ColQualList
397 %type <node> ColConstraint ColConstraintElem ConstraintAttr
398 %type <ival> key_actions key_delete key_match key_update key_action
399 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
402 %type <list> constraints_set_list
403 %type <boolean> constraints_set_mode
404 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
405 %type <list> opt_check_option
407 %type <target> xml_attribute_el
408 %type <list> xml_attribute_list xml_attributes
409 %type <node> xml_root_version opt_xml_root_standalone
410 %type <ival> document_or_content
411 %type <boolean> xml_whitespace_option
413 %type <node> common_table_expr
414 %type <with> with_clause
415 %type <list> cte_list
417 %type <list> window_clause window_definition_list opt_partition_clause
418 %type <windef> window_definition over_clause window_specification
419 %type <str> opt_existing_window_name
420 %type <ival> opt_frame_clause frame_extent frame_bound
424 * Non-keyword token types. These are hard-wired into the "flex" lexer.
425 * They must be listed first so that their numeric codes do not depend on
426 * the set of keywords. PL/pgsql depends on this so that it can share the
427 * same lexer. If you add/change tokens here, fix PL/pgsql to match!
429 * DOT_DOT and COLON_EQUALS are unused in the core SQL grammar, and so will
430 * always provoke parse errors. They are needed by PL/pgsql.
432 %token <str> IDENT FCONST SCONST BCONST XCONST Op
433 %token <ival> ICONST PARAM
434 %token TYPECAST DOT_DOT COLON_EQUALS
437 * If you want to make any keyword changes, update the keyword table in
438 * src/include/parser/kwlist.h and add new keywords to the appropriate one
439 * of the reserved-or-not-so-reserved keyword lists, below; search
440 * this file for "Keyword category lists".
443 /* ordinary key words in alphabetical order */
444 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
445 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
446 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
448 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
451 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
452 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
453 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
454 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
455 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
456 CREATEROLE CREATEUSER CROSS CSV CURRENT_P
457 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
458 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
460 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
461 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
462 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
464 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
465 EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
467 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
468 FREEZE FROM FULL FUNCTION
470 GLOBAL GRANT GRANTED GREATEST GROUP_P
472 HANDLER HAVING HEADER_P HOLD HOUR_P
474 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
475 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
476 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
477 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
483 LANCOMPILER LANGUAGE LARGE_P LAST_P LC_COLLATE_P LC_CTYPE_P LEADING
484 LEAST LEFT LEVEL LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP
485 LOCATION LOCK_P LOGIN_P
487 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
489 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
490 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
491 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
493 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
494 ORDER OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
496 PARSER PARTIAL PARTITION PASSWORD PLACING PLANS POSITION
497 PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
498 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
502 RANGE READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX
503 RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA RESET RESTART
504 RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
506 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
507 SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
508 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
509 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
510 SYMMETRIC SYSID SYSTEM_P
512 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
513 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
514 TRUNCATE TRUSTED TYPE_P
516 UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
519 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
520 VERBOSE VERSION_P VIEW VOLATILE
522 WHEN WHERE WHITESPACE_P WINDOW WITH WITHOUT WORK WRAPPER WRITE
524 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
525 XMLPI XMLROOT XMLSERIALIZE
532 * The grammar thinks these are keywords, but they are not in the kwlist.h
533 * list and so can never be entered directly. The filter in parser.c
534 * creates these tokens when required.
536 %token NULLS_FIRST NULLS_LAST WITH_TIME
539 /* Precedence: lowest to highest */
540 %nonassoc SET /* see relation_expr_opt_alias */
548 %nonassoc LIKE ILIKE SIMILAR
553 %left POSTFIXOP /* dummy for postfix Op rules */
555 * To support target_el without AS, we must give IDENT an explicit priority
556 * between POSTFIXOP and Op. We can safely assign the same priority to
557 * various unreserved keywords as needed to resolve ambiguities (this can't
558 * have any bad effects since obviously the keywords will still behave the
559 * same as if they weren't keywords). We need to do this for PARTITION,
560 * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
561 * so that they can follow a_expr without creating
562 * postfix-operator problems.
564 %nonassoc IDENT PARTITION RANGE ROWS
565 %left Op OPERATOR /* multi-character ops and user-defined operators */
568 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
572 /* Unary Operators */
573 %left AT ZONE /* sets precedence for AT TIME ZONE */
580 * These might seem to be low-precedence, but actually they are not part
581 * of the arithmetic hierarchy at all in their use as JOIN operators.
582 * We make them high-precedence to support their use as function names.
583 * They wouldn't be given a precedence at all, were it not that we need
584 * left-associativity among the JOIN rules themselves.
586 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
587 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
588 %right PRESERVE STRIP_P
592 * The target production for the whole parse.
596 pg_yyget_extra(yyscanner)->parsetree = $1;
600 /* the thrashing around here is to discard "empty" statements... */
601 stmtmulti: stmtmulti ';' stmt
604 $$ = lappend($1, $3);
619 | AlterDatabaseSetStmt
622 | AlterForeignServerStmt
625 | AlterObjectSchemaStmt
631 | AlterTSConfigurationStmt
632 | AlterTSDictionaryStmt
633 | AlterUserMappingStmt
646 | CreateConversionStmt
649 | CreateForeignServerStmt
659 | CreateTableSpaceStmt
663 | CreateUserMappingStmt
673 | DropForeignServerStmt
685 | DropUserMappingStmt
722 /*****************************************************************************
724 * Create a new Postgres DBMS role
726 *****************************************************************************/
729 CREATE ROLE RoleId opt_with OptRoleList
731 CreateRoleStmt *n = makeNode(CreateRoleStmt);
732 n->stmt_type = ROLESTMT_ROLE;
745 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
746 * for backwards compatibility). Note: the only option required by SQL99
747 * is "WITH ADMIN name".
750 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
751 | /* EMPTY */ { $$ = NIL; }
757 $$ = makeDefElem("password",
758 (Node *)makeString($2));
762 $$ = makeDefElem("password", NULL);
764 | ENCRYPTED PASSWORD Sconst
766 $$ = makeDefElem("encryptedPassword",
767 (Node *)makeString($3));
769 | UNENCRYPTED PASSWORD Sconst
771 $$ = makeDefElem("unencryptedPassword",
772 (Node *)makeString($3));
776 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
780 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
784 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
788 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
792 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
796 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
800 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
804 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
808 /* For backwards compatibility, synonym for SUPERUSER */
809 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
813 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
817 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
821 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
823 | CONNECTION LIMIT SignedIconst
825 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
829 $$ = makeDefElem("validUntil", (Node *)makeString($3));
831 /* Supported but not documented for roles, for use by ALTER GROUP. */
834 $$ = makeDefElem("rolemembers", (Node *)$2);
836 /* The following are not supported by ALTER ROLE/USER/GROUP */
839 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
843 $$ = makeDefElem("adminmembers", (Node *)$2);
847 $$ = makeDefElem("rolemembers", (Node *)$2);
849 | IN_P ROLE name_list
851 $$ = makeDefElem("addroleto", (Node *)$3);
853 | IN_P GROUP_P name_list
855 $$ = makeDefElem("addroleto", (Node *)$3);
860 /*****************************************************************************
862 * Create a new Postgres DBMS user (role with implied login ability)
864 *****************************************************************************/
867 CREATE USER RoleId opt_with OptRoleList
869 CreateRoleStmt *n = makeNode(CreateRoleStmt);
870 n->stmt_type = ROLESTMT_USER;
878 /*****************************************************************************
880 * Alter a postgresql DBMS role
882 *****************************************************************************/
885 ALTER ROLE RoleId opt_with OptRoleList
887 AlterRoleStmt *n = makeNode(AlterRoleStmt);
889 n->action = +1; /* add, if there are members */
896 ALTER ROLE RoleId SetResetClause
898 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
906 /*****************************************************************************
908 * Alter a postgresql DBMS user
910 *****************************************************************************/
913 ALTER USER RoleId opt_with OptRoleList
915 AlterRoleStmt *n = makeNode(AlterRoleStmt);
917 n->action = +1; /* add, if there are members */
925 ALTER USER RoleId SetResetClause
927 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
935 /*****************************************************************************
937 * Drop a postgresql DBMS role
939 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
940 * might own objects in multiple databases, there is presently no way to
941 * implement either cascading or restricting. Caveat DBA.
942 *****************************************************************************/
947 DropRoleStmt *n = makeNode(DropRoleStmt);
948 n->missing_ok = FALSE;
952 | DROP ROLE IF_P EXISTS name_list
954 DropRoleStmt *n = makeNode(DropRoleStmt);
955 n->missing_ok = TRUE;
961 /*****************************************************************************
963 * Drop a postgresql DBMS user
965 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
966 * might own objects in multiple databases, there is presently no way to
967 * implement either cascading or restricting. Caveat DBA.
968 *****************************************************************************/
973 DropRoleStmt *n = makeNode(DropRoleStmt);
974 n->missing_ok = FALSE;
978 | DROP USER IF_P EXISTS name_list
980 DropRoleStmt *n = makeNode(DropRoleStmt);
982 n->missing_ok = TRUE;
988 /*****************************************************************************
990 * Create a postgresql group (role without login ability)
992 *****************************************************************************/
995 CREATE GROUP_P RoleId opt_with OptRoleList
997 CreateRoleStmt *n = makeNode(CreateRoleStmt);
998 n->stmt_type = ROLESTMT_GROUP;
1006 /*****************************************************************************
1008 * Alter a postgresql group
1010 *****************************************************************************/
1013 ALTER GROUP_P RoleId add_drop USER name_list
1015 AlterRoleStmt *n = makeNode(AlterRoleStmt);
1018 n->options = list_make1(makeDefElem("rolemembers",
1024 add_drop: ADD_P { $$ = +1; }
1029 /*****************************************************************************
1031 * Drop a postgresql group
1033 * XXX see above notes about cascading DROP USER; groups have same problem.
1034 *****************************************************************************/
1037 DROP GROUP_P name_list
1039 DropRoleStmt *n = makeNode(DropRoleStmt);
1040 n->missing_ok = FALSE;
1044 | DROP GROUP_P IF_P EXISTS name_list
1046 DropRoleStmt *n = makeNode(DropRoleStmt);
1047 n->missing_ok = TRUE;
1054 /*****************************************************************************
1056 * Manipulate a schema
1058 *****************************************************************************/
1061 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1063 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1064 /* One can omit the schema name or the authorization id. */
1073 | CREATE SCHEMA ColId OptSchemaEltList
1075 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1076 /* ...but not both */
1086 | /* EMPTY */ { $$ = NULL; }
1090 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1091 | /* EMPTY */ { $$ = NIL; }
1095 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1096 * statement (in addition to by themselves).
1108 /*****************************************************************************
1110 * Set PG internal variable
1111 * SET name TO 'var_value'
1112 * Include SQL92 syntax (thomas 1997-10-22):
1113 * SET TIME ZONE 'var_value'
1115 *****************************************************************************/
1120 VariableSetStmt *n = $2;
1121 n->is_local = false;
1124 | SET LOCAL set_rest
1126 VariableSetStmt *n = $3;
1130 | SET SESSION set_rest
1132 VariableSetStmt *n = $3;
1133 n->is_local = false;
1138 set_rest: /* Generic SET syntaxes: */
1139 var_name TO var_list
1141 VariableSetStmt *n = makeNode(VariableSetStmt);
1142 n->kind = VAR_SET_VALUE;
1147 | var_name '=' var_list
1149 VariableSetStmt *n = makeNode(VariableSetStmt);
1150 n->kind = VAR_SET_VALUE;
1155 | var_name TO DEFAULT
1157 VariableSetStmt *n = makeNode(VariableSetStmt);
1158 n->kind = VAR_SET_DEFAULT;
1162 | var_name '=' DEFAULT
1164 VariableSetStmt *n = makeNode(VariableSetStmt);
1165 n->kind = VAR_SET_DEFAULT;
1169 | var_name FROM CURRENT_P
1171 VariableSetStmt *n = makeNode(VariableSetStmt);
1172 n->kind = VAR_SET_CURRENT;
1176 /* Special syntaxes mandated by SQL standard: */
1177 | TIME ZONE zone_value
1179 VariableSetStmt *n = makeNode(VariableSetStmt);
1180 n->kind = VAR_SET_VALUE;
1181 n->name = "timezone";
1183 n->args = list_make1($3);
1185 n->kind = VAR_SET_DEFAULT;
1188 | TRANSACTION transaction_mode_list
1190 VariableSetStmt *n = makeNode(VariableSetStmt);
1191 n->kind = VAR_SET_MULTI;
1192 n->name = "TRANSACTION";
1196 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1198 VariableSetStmt *n = makeNode(VariableSetStmt);
1199 n->kind = VAR_SET_MULTI;
1200 n->name = "SESSION CHARACTERISTICS";
1207 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1208 errmsg("current database cannot be changed"),
1209 parser_errposition(@2)));
1210 $$ = NULL; /*not reached*/
1214 VariableSetStmt *n = makeNode(VariableSetStmt);
1215 n->kind = VAR_SET_VALUE;
1216 n->name = "search_path";
1217 n->args = list_make1(makeStringConst($2, @2));
1220 | NAMES opt_encoding
1222 VariableSetStmt *n = makeNode(VariableSetStmt);
1223 n->kind = VAR_SET_VALUE;
1224 n->name = "client_encoding";
1226 n->args = list_make1(makeStringConst($2, @2));
1228 n->kind = VAR_SET_DEFAULT;
1231 | ROLE ColId_or_Sconst
1233 VariableSetStmt *n = makeNode(VariableSetStmt);
1234 n->kind = VAR_SET_VALUE;
1236 n->args = list_make1(makeStringConst($2, @2));
1239 | SESSION AUTHORIZATION ColId_or_Sconst
1241 VariableSetStmt *n = makeNode(VariableSetStmt);
1242 n->kind = VAR_SET_VALUE;
1243 n->name = "session_authorization";
1244 n->args = list_make1(makeStringConst($3, @3));
1247 | SESSION AUTHORIZATION DEFAULT
1249 VariableSetStmt *n = makeNode(VariableSetStmt);
1250 n->kind = VAR_SET_DEFAULT;
1251 n->name = "session_authorization";
1254 | XML_P OPTION document_or_content
1256 VariableSetStmt *n = makeNode(VariableSetStmt);
1257 n->kind = VAR_SET_VALUE;
1258 n->name = "xmloption";
1259 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1264 var_name: ColId { $$ = $1; }
1265 | var_name '.' ColId
1267 $$ = palloc(strlen($1) + strlen($3) + 2);
1268 sprintf($$, "%s.%s", $1, $3);
1272 var_list: var_value { $$ = list_make1($1); }
1273 | var_list ',' var_value { $$ = lappend($1, $3); }
1276 var_value: opt_boolean
1277 { $$ = makeStringConst($1, @1); }
1279 { $$ = makeStringConst($1, @1); }
1281 { $$ = makeAConst($1, @1); }
1284 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1285 | READ COMMITTED { $$ = "read committed"; }
1286 | REPEATABLE READ { $$ = "repeatable read"; }
1287 | SERIALIZABLE { $$ = "serializable"; }
1291 TRUE_P { $$ = "true"; }
1292 | FALSE_P { $$ = "false"; }
1294 | OFF { $$ = "off"; }
1297 /* Timezone values can be:
1298 * - a string such as 'pst8pdt'
1299 * - an identifier such as "pst8pdt"
1300 * - an integer or floating point number
1301 * - a time interval per SQL99
1302 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1303 * so use IDENT (meaning we reject anything that is a key word).
1308 $$ = makeStringConst($1, @1);
1312 $$ = makeStringConst($1, @1);
1314 | ConstInterval Sconst opt_interval
1319 A_Const *n = (A_Const *) linitial($3);
1320 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1322 (errcode(ERRCODE_SYNTAX_ERROR),
1323 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1324 parser_errposition(@3)));
1327 $$ = makeStringConstCast($2, @2, t);
1329 | ConstInterval '(' Iconst ')' Sconst opt_interval
1334 A_Const *n = (A_Const *) linitial($6);
1335 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1337 (errcode(ERRCODE_SYNTAX_ERROR),
1338 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1339 parser_errposition(@6)));
1340 if (list_length($6) != 1)
1342 (errcode(ERRCODE_SYNTAX_ERROR),
1343 errmsg("interval precision specified twice"),
1344 parser_errposition(@1)));
1345 t->typmods = lappend($6, makeIntConst($3, @3));
1348 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1349 makeIntConst($3, @3));
1350 $$ = makeStringConstCast($5, @5, t);
1352 | NumericOnly { $$ = makeAConst($1, @1); }
1353 | DEFAULT { $$ = NULL; }
1354 | LOCAL { $$ = NULL; }
1359 | DEFAULT { $$ = NULL; }
1360 | /*EMPTY*/ { $$ = NULL; }
1365 | Sconst { $$ = $1; }
1371 VariableSetStmt *n = makeNode(VariableSetStmt);
1372 n->kind = VAR_RESET;
1378 VariableSetStmt *n = makeNode(VariableSetStmt);
1379 n->kind = VAR_RESET;
1380 n->name = "timezone";
1383 | RESET TRANSACTION ISOLATION LEVEL
1385 VariableSetStmt *n = makeNode(VariableSetStmt);
1386 n->kind = VAR_RESET;
1387 n->name = "transaction_isolation";
1390 | RESET SESSION AUTHORIZATION
1392 VariableSetStmt *n = makeNode(VariableSetStmt);
1393 n->kind = VAR_RESET;
1394 n->name = "session_authorization";
1399 VariableSetStmt *n = makeNode(VariableSetStmt);
1400 n->kind = VAR_RESET_ALL;
1405 /* SetResetClause allows SET or RESET without LOCAL */
1407 SET set_rest { $$ = $2; }
1408 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1415 VariableShowStmt *n = makeNode(VariableShowStmt);
1421 VariableShowStmt *n = makeNode(VariableShowStmt);
1422 n->name = "timezone";
1425 | SHOW TRANSACTION ISOLATION LEVEL
1427 VariableShowStmt *n = makeNode(VariableShowStmt);
1428 n->name = "transaction_isolation";
1431 | SHOW SESSION AUTHORIZATION
1433 VariableShowStmt *n = makeNode(VariableShowStmt);
1434 n->name = "session_authorization";
1439 VariableShowStmt *n = makeNode(VariableShowStmt);
1447 SET CONSTRAINTS constraints_set_list constraints_set_mode
1449 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1450 n->constraints = $3;
1456 constraints_set_list:
1458 | qualified_name_list { $$ = $1; }
1461 constraints_set_mode:
1462 DEFERRED { $$ = TRUE; }
1463 | IMMEDIATE { $$ = FALSE; }
1468 * Checkpoint statement
1473 CheckPointStmt *n = makeNode(CheckPointStmt);
1479 /*****************************************************************************
1481 * DISCARD { ALL | TEMP | PLANS }
1483 *****************************************************************************/
1488 DiscardStmt *n = makeNode(DiscardStmt);
1489 n->target = DISCARD_ALL;
1494 DiscardStmt *n = makeNode(DiscardStmt);
1495 n->target = DISCARD_TEMP;
1500 DiscardStmt *n = makeNode(DiscardStmt);
1501 n->target = DISCARD_TEMP;
1506 DiscardStmt *n = makeNode(DiscardStmt);
1507 n->target = DISCARD_PLANS;
1513 /*****************************************************************************
1515 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1517 * Note: we accept all subcommands for each of the four variants, and sort
1518 * out what's really legal at execution time.
1519 *****************************************************************************/
1522 ALTER TABLE relation_expr alter_table_cmds
1524 AlterTableStmt *n = makeNode(AlterTableStmt);
1527 n->relkind = OBJECT_TABLE;
1530 | ALTER INDEX qualified_name alter_table_cmds
1532 AlterTableStmt *n = makeNode(AlterTableStmt);
1535 n->relkind = OBJECT_INDEX;
1538 | ALTER SEQUENCE qualified_name alter_table_cmds
1540 AlterTableStmt *n = makeNode(AlterTableStmt);
1543 n->relkind = OBJECT_SEQUENCE;
1546 | ALTER VIEW qualified_name alter_table_cmds
1548 AlterTableStmt *n = makeNode(AlterTableStmt);
1551 n->relkind = OBJECT_VIEW;
1557 alter_table_cmd { $$ = list_make1($1); }
1558 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1562 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1563 ADD_P opt_column columnDef
1565 AlterTableCmd *n = makeNode(AlterTableCmd);
1566 n->subtype = AT_AddColumn;
1570 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1571 | ALTER opt_column ColId alter_column_default
1573 AlterTableCmd *n = makeNode(AlterTableCmd);
1574 n->subtype = AT_ColumnDefault;
1579 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1580 | ALTER opt_column ColId DROP NOT NULL_P
1582 AlterTableCmd *n = makeNode(AlterTableCmd);
1583 n->subtype = AT_DropNotNull;
1587 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1588 | ALTER opt_column ColId SET NOT NULL_P
1590 AlterTableCmd *n = makeNode(AlterTableCmd);
1591 n->subtype = AT_SetNotNull;
1595 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1596 | ALTER opt_column ColId SET STATISTICS SignedIconst
1598 AlterTableCmd *n = makeNode(AlterTableCmd);
1599 n->subtype = AT_SetStatistics;
1601 n->def = (Node *) makeInteger($6);
1604 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1605 | ALTER opt_column ColId SET STORAGE ColId
1607 AlterTableCmd *n = makeNode(AlterTableCmd);
1608 n->subtype = AT_SetStorage;
1610 n->def = (Node *) makeString($6);
1613 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1614 | DROP opt_column ColId opt_drop_behavior
1616 AlterTableCmd *n = makeNode(AlterTableCmd);
1617 n->subtype = AT_DropColumn;
1623 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1624 * [ USING <expression> ]
1626 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1628 AlterTableCmd *n = makeNode(AlterTableCmd);
1629 n->subtype = AT_AlterColumnType;
1631 n->def = (Node *) $6;
1635 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1636 | ADD_P TableConstraint
1638 AlterTableCmd *n = makeNode(AlterTableCmd);
1639 n->subtype = AT_AddConstraint;
1643 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1644 | DROP CONSTRAINT name opt_drop_behavior
1646 AlterTableCmd *n = makeNode(AlterTableCmd);
1647 n->subtype = AT_DropConstraint;
1652 /* ALTER TABLE <name> SET WITH OIDS */
1655 AlterTableCmd *n = makeNode(AlterTableCmd);
1656 n->subtype = AT_AddOids;
1659 /* ALTER TABLE <name> SET WITHOUT OIDS */
1662 AlterTableCmd *n = makeNode(AlterTableCmd);
1663 n->subtype = AT_DropOids;
1666 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1669 AlterTableCmd *n = makeNode(AlterTableCmd);
1670 n->subtype = AT_ClusterOn;
1674 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1675 | SET WITHOUT CLUSTER
1677 AlterTableCmd *n = makeNode(AlterTableCmd);
1678 n->subtype = AT_DropCluster;
1682 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1683 | ENABLE_P TRIGGER name
1685 AlterTableCmd *n = makeNode(AlterTableCmd);
1686 n->subtype = AT_EnableTrig;
1690 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1691 | ENABLE_P ALWAYS TRIGGER name
1693 AlterTableCmd *n = makeNode(AlterTableCmd);
1694 n->subtype = AT_EnableAlwaysTrig;
1698 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1699 | ENABLE_P REPLICA TRIGGER name
1701 AlterTableCmd *n = makeNode(AlterTableCmd);
1702 n->subtype = AT_EnableReplicaTrig;
1706 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1707 | ENABLE_P TRIGGER ALL
1709 AlterTableCmd *n = makeNode(AlterTableCmd);
1710 n->subtype = AT_EnableTrigAll;
1713 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1714 | ENABLE_P TRIGGER USER
1716 AlterTableCmd *n = makeNode(AlterTableCmd);
1717 n->subtype = AT_EnableTrigUser;
1720 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1721 | DISABLE_P TRIGGER name
1723 AlterTableCmd *n = makeNode(AlterTableCmd);
1724 n->subtype = AT_DisableTrig;
1728 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1729 | DISABLE_P TRIGGER ALL
1731 AlterTableCmd *n = makeNode(AlterTableCmd);
1732 n->subtype = AT_DisableTrigAll;
1735 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1736 | DISABLE_P TRIGGER USER
1738 AlterTableCmd *n = makeNode(AlterTableCmd);
1739 n->subtype = AT_DisableTrigUser;
1742 /* ALTER TABLE <name> ENABLE RULE <rule> */
1743 | ENABLE_P RULE name
1745 AlterTableCmd *n = makeNode(AlterTableCmd);
1746 n->subtype = AT_EnableRule;
1750 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1751 | ENABLE_P ALWAYS RULE name
1753 AlterTableCmd *n = makeNode(AlterTableCmd);
1754 n->subtype = AT_EnableAlwaysRule;
1758 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1759 | ENABLE_P REPLICA RULE name
1761 AlterTableCmd *n = makeNode(AlterTableCmd);
1762 n->subtype = AT_EnableReplicaRule;
1766 /* ALTER TABLE <name> DISABLE RULE <rule> */
1767 | DISABLE_P RULE name
1769 AlterTableCmd *n = makeNode(AlterTableCmd);
1770 n->subtype = AT_DisableRule;
1774 /* ALTER TABLE <name> INHERIT <parent> */
1775 | INHERIT qualified_name
1777 AlterTableCmd *n = makeNode(AlterTableCmd);
1778 n->subtype = AT_AddInherit;
1779 n->def = (Node *) $2;
1782 /* ALTER TABLE <name> NO INHERIT <parent> */
1783 | NO INHERIT qualified_name
1785 AlterTableCmd *n = makeNode(AlterTableCmd);
1786 n->subtype = AT_DropInherit;
1787 n->def = (Node *) $3;
1790 /* ALTER TABLE <name> OWNER TO RoleId */
1793 AlterTableCmd *n = makeNode(AlterTableCmd);
1794 n->subtype = AT_ChangeOwner;
1798 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1799 | SET TABLESPACE name
1801 AlterTableCmd *n = makeNode(AlterTableCmd);
1802 n->subtype = AT_SetTableSpace;
1806 /* ALTER TABLE <name> SET (...) */
1809 AlterTableCmd *n = makeNode(AlterTableCmd);
1810 n->subtype = AT_SetRelOptions;
1811 n->def = (Node *)$2;
1814 /* ALTER TABLE <name> RESET (...) */
1817 AlterTableCmd *n = makeNode(AlterTableCmd);
1818 n->subtype = AT_ResetRelOptions;
1819 n->def = (Node *)$2;
1824 alter_column_default:
1825 SET DEFAULT a_expr { $$ = $3; }
1826 | DROP DEFAULT { $$ = NULL; }
1830 CASCADE { $$ = DROP_CASCADE; }
1831 | RESTRICT { $$ = DROP_RESTRICT; }
1832 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1836 USING a_expr { $$ = $2; }
1837 | /* EMPTY */ { $$ = NULL; }
1841 '(' reloption_list ')' { $$ = $2; }
1844 opt_reloptions: WITH reloptions { $$ = $2; }
1845 | /* EMPTY */ { $$ = NIL; }
1849 reloption_elem { $$ = list_make1($1); }
1850 | reloption_list ',' reloption_elem { $$ = lappend($1, $3); }
1853 /* This should match def_elem and also allow qualified names */
1855 ColLabel '=' def_arg
1857 $$ = makeDefElem($1, (Node *) $3);
1861 $$ = makeDefElem($1, NULL);
1863 | ColLabel '.' ColLabel '=' def_arg
1865 $$ = makeDefElemExtended($1, $3, (Node *) $5,
1868 | ColLabel '.' ColLabel
1870 $$ = makeDefElemExtended($1, $3, NULL, DEFELEM_UNSPEC);
1875 /*****************************************************************************
1878 * close <portalname>
1880 *****************************************************************************/
1885 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1891 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1892 n->portalname = NULL;
1898 /*****************************************************************************
1901 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1903 * BINARY, OIDS, and DELIMITERS kept in old locations
1904 * for backward compatibility. 2002-06-18
1906 * COPY ( SELECT ... ) TO file [WITH options]
1907 * This form doesn't have the backwards-compatible option
1910 *****************************************************************************/
1912 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1913 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1915 CopyStmt *n = makeNode(CopyStmt);
1923 /* Concatenate user-supplied flags */
1925 n->options = lappend(n->options, $2);
1927 n->options = lappend(n->options, $5);
1929 n->options = lappend(n->options, $8);
1931 n->options = list_concat(n->options, $10);
1934 | COPY select_with_parens TO copy_file_name opt_with
1937 CopyStmt *n = makeNode(CopyStmt);
1950 | TO { $$ = FALSE; }
1954 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1955 * used depends on the direction. (It really doesn't make sense to copy from
1956 * stdout. We silently correct the "typo".) - AY 9/94
1960 | STDIN { $$ = NULL; }
1961 | STDOUT { $$ = NULL; }
1967 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1968 | /* EMPTY */ { $$ = NIL; }
1975 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1979 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1981 | DELIMITER opt_as Sconst
1983 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1985 | NULL_P opt_as Sconst
1987 $$ = makeDefElem("null", (Node *)makeString($3));
1991 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1995 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1997 | QUOTE opt_as Sconst
1999 $$ = makeDefElem("quote", (Node *)makeString($3));
2001 | ESCAPE opt_as Sconst
2003 $$ = makeDefElem("escape", (Node *)makeString($3));
2005 | FORCE QUOTE columnList
2007 $$ = makeDefElem("force_quote", (Node *)$3);
2009 | FORCE NOT NULL_P columnList
2011 $$ = makeDefElem("force_notnull", (Node *)$4);
2015 /* The following exist for backward compatibility */
2020 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
2022 | /*EMPTY*/ { $$ = NULL; }
2028 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
2030 | /*EMPTY*/ { $$ = NULL; }
2034 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
2035 opt_using DELIMITERS Sconst
2037 $$ = makeDefElem("delimiter", (Node *)makeString($3));
2039 | /*EMPTY*/ { $$ = NULL; }
2048 /*****************************************************************************
2051 * CREATE TABLE relname
2053 *****************************************************************************/
2055 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
2056 OptInherit OptWith OnCommitOption OptTableSpace
2058 CreateStmt *n = makeNode(CreateStmt);
2062 n->inhRelations = $8;
2063 n->constraints = NIL;
2066 n->tablespacename = $11;
2069 | CREATE OptTemp TABLE qualified_name OF qualified_name
2070 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
2072 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
2073 * by our inheritance capabilities. Let's try it...
2075 CreateStmt *n = makeNode(CreateStmt);
2079 n->inhRelations = list_make1($6);
2080 n->constraints = NIL;
2083 n->tablespacename = $12;
2089 * Redundancy here is needed to avoid shift/reduce conflicts,
2090 * since TEMP is not a reserved word. See also OptTempTableName.
2092 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2093 * the LOCAL keyword is really meaningless.
2095 OptTemp: TEMPORARY { $$ = TRUE; }
2096 | TEMP { $$ = TRUE; }
2097 | LOCAL TEMPORARY { $$ = TRUE; }
2098 | LOCAL TEMP { $$ = TRUE; }
2099 | GLOBAL TEMPORARY { $$ = TRUE; }
2100 | GLOBAL TEMP { $$ = TRUE; }
2101 | /*EMPTY*/ { $$ = FALSE; }
2104 OptTableElementList:
2105 TableElementList { $$ = $1; }
2106 | /*EMPTY*/ { $$ = NIL; }
2112 $$ = list_make1($1);
2114 | TableElementList ',' TableElement
2116 $$ = lappend($1, $3);
2121 columnDef { $$ = $1; }
2122 | TableLikeClause { $$ = $1; }
2123 | TableConstraint { $$ = $1; }
2126 columnDef: ColId Typename ColQualList
2128 ColumnDef *n = makeNode(ColumnDef);
2131 n->constraints = $3;
2138 ColQualList ColConstraint { $$ = lappend($1, $2); }
2139 | /*EMPTY*/ { $$ = NIL; }
2143 CONSTRAINT name ColConstraintElem
2145 switch (nodeTag($3))
2149 Constraint *n = (Constraint *)$3;
2153 case T_FkConstraint:
2155 FkConstraint *n = (FkConstraint *)$3;
2156 n->constr_name = $2;
2164 | ColConstraintElem { $$ = $1; }
2165 | ConstraintAttr { $$ = $1; }
2168 /* DEFAULT NULL is already the default for Postgres.
2169 * But define it here and carry it forward into the system
2170 * to make it explicit.
2171 * - thomas 1998-09-13
2173 * WITH NULL and NULL are not SQL92-standard syntax elements,
2174 * so leave them out. Use DEFAULT NULL to explicitly indicate
2175 * that a column may have that value. WITH NULL leads to
2176 * shift/reduce conflicts with WITH TIME ZONE anyway.
2177 * - thomas 1999-01-08
2179 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2180 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2181 * or be part of a_expr NOT LIKE or similar constructs).
2186 Constraint *n = makeNode(Constraint);
2187 n->contype = CONSTR_NOTNULL;
2190 n->cooked_expr = NULL;
2192 n->indexspace = NULL;
2197 Constraint *n = makeNode(Constraint);
2198 n->contype = CONSTR_NULL;
2201 n->cooked_expr = NULL;
2203 n->indexspace = NULL;
2206 | UNIQUE opt_definition OptConsTableSpace
2208 Constraint *n = makeNode(Constraint);
2209 n->contype = CONSTR_UNIQUE;
2212 n->cooked_expr = NULL;
2218 | PRIMARY KEY opt_definition OptConsTableSpace
2220 Constraint *n = makeNode(Constraint);
2221 n->contype = CONSTR_PRIMARY;
2224 n->cooked_expr = NULL;
2230 | CHECK '(' a_expr ')'
2232 Constraint *n = makeNode(Constraint);
2233 n->contype = CONSTR_CHECK;
2236 n->cooked_expr = NULL;
2238 n->indexspace = NULL;
2243 Constraint *n = makeNode(Constraint);
2244 n->contype = CONSTR_DEFAULT;
2247 n->cooked_expr = NULL;
2249 n->indexspace = NULL;
2252 | REFERENCES qualified_name opt_column_list key_match key_actions
2254 FkConstraint *n = makeNode(FkConstraint);
2255 n->constr_name = NULL;
2259 n->fk_matchtype = $4;
2260 n->fk_upd_action = (char) ($5 >> 8);
2261 n->fk_del_action = (char) ($5 & 0xFF);
2262 n->deferrable = FALSE;
2263 n->initdeferred = FALSE;
2269 * ConstraintAttr represents constraint attributes, which we parse as if
2270 * they were independent constraint clauses, in order to avoid shift/reduce
2271 * conflicts (since NOT might start either an independent NOT NULL clause
2272 * or an attribute). parse_utilcmd.c is responsible for attaching the
2273 * attribute information to the preceding "real" constraint node, and for
2274 * complaining if attribute clauses appear in the wrong place or wrong
2277 * See also ConstraintAttributeSpec, which can be used in places where
2278 * there is no parsing conflict.
2283 Constraint *n = makeNode(Constraint);
2284 n->contype = CONSTR_ATTR_DEFERRABLE;
2289 Constraint *n = makeNode(Constraint);
2290 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2293 | INITIALLY DEFERRED
2295 Constraint *n = makeNode(Constraint);
2296 n->contype = CONSTR_ATTR_DEFERRED;
2299 | INITIALLY IMMEDIATE
2301 Constraint *n = makeNode(Constraint);
2302 n->contype = CONSTR_ATTR_IMMEDIATE;
2309 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2310 * This seems to be a poor man's inheritance capability, with the resulting
2311 * tables completely decoupled except for the original commonality in definitions.
2313 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2314 * which is a part of SQL:2003.
2317 LIKE qualified_name TableLikeOptionList
2319 InhRelation *n = makeNode(InhRelation);
2326 TableLikeOptionList:
2327 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2328 | /* EMPTY */ { $$ = NIL; }
2332 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2333 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2334 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2335 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2336 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2337 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2341 /* ConstraintElem specifies constraint syntax which is not embedded into
2342 * a column definition. ColConstraintElem specifies the embedded form.
2343 * - thomas 1997-12-03
2346 CONSTRAINT name ConstraintElem
2348 switch (nodeTag($3))
2352 Constraint *n = (Constraint *)$3;
2356 case T_FkConstraint:
2358 FkConstraint *n = (FkConstraint *)$3;
2359 n->constr_name = $2;
2367 | ConstraintElem { $$ = $1; }
2371 CHECK '(' a_expr ')'
2373 Constraint *n = makeNode(Constraint);
2374 n->contype = CONSTR_CHECK;
2377 n->cooked_expr = NULL;
2378 n->indexspace = NULL;
2381 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2383 Constraint *n = makeNode(Constraint);
2384 n->contype = CONSTR_UNIQUE;
2387 n->cooked_expr = NULL;
2393 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2395 Constraint *n = makeNode(Constraint);
2396 n->contype = CONSTR_PRIMARY;
2399 n->cooked_expr = NULL;
2405 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2406 opt_column_list key_match key_actions ConstraintAttributeSpec
2408 FkConstraint *n = makeNode(FkConstraint);
2409 n->constr_name = NULL;
2413 n->fk_matchtype = $9;
2414 n->fk_upd_action = (char) ($10 >> 8);
2415 n->fk_del_action = (char) ($10 & 0xFF);
2416 n->deferrable = ($11 & 1) != 0;
2417 n->initdeferred = ($11 & 2) != 0;
2423 '(' columnList ')' { $$ = $2; }
2424 | /*EMPTY*/ { $$ = NIL; }
2428 columnElem { $$ = list_make1($1); }
2429 | columnList ',' columnElem { $$ = lappend($1, $3); }
2434 $$ = (Node *) makeString($1);
2438 key_match: MATCH FULL
2440 $$ = FKCONSTR_MATCH_FULL;
2445 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2446 errmsg("MATCH PARTIAL not yet implemented"),
2447 parser_errposition(@1)));
2448 $$ = FKCONSTR_MATCH_PARTIAL;
2452 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2456 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2461 * We combine the update and delete actions into one value temporarily
2462 * for simplicity of parsing, and then break them down again in the
2463 * calling production. update is in the left 8 bits, delete in the right.
2464 * Note that NOACTION is the default.
2468 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2470 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2471 | key_update key_delete
2472 { $$ = ($1 << 8) | ($2 & 0xFF); }
2473 | key_delete key_update
2474 { $$ = ($2 << 8) | ($1 & 0xFF); }
2476 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2479 key_update: ON UPDATE key_action { $$ = $3; }
2482 key_delete: ON DELETE_P key_action { $$ = $3; }
2486 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2487 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2488 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2489 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2490 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2493 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2494 | /*EMPTY*/ { $$ = NIL; }
2497 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2499 WITH reloptions { $$ = $2; }
2500 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2501 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2502 | /*EMPTY*/ { $$ = NIL; }
2505 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2506 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2507 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2508 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2511 OptTableSpace: TABLESPACE name { $$ = $2; }
2512 | /*EMPTY*/ { $$ = NULL; }
2515 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2516 | /*EMPTY*/ { $$ = NULL; }
2521 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2526 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2529 * When the SelectStmt is a set-operation tree, we must
2530 * stuff the INTO information into the leftmost component
2531 * Select, because that's where analyze.c will expect
2532 * to find it. Similarly, the output column names must
2533 * be attached to that Select's target list.
2535 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2536 if (n->intoClause != NULL)
2538 (errcode(ERRCODE_SYNTAX_ERROR),
2539 errmsg("CREATE TABLE AS cannot specify INTO"),
2540 parser_errposition(exprLocation((Node *) n->intoClause))));
2541 $4->rel->istemp = $2;
2543 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2545 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2551 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2553 $$ = makeNode(IntoClause);
2558 $$->tableSpaceName = $5;
2563 '(' CreateAsList ')' { $$ = $2; }
2564 | /*EMPTY*/ { $$ = NIL; }
2568 CreateAsElement { $$ = list_make1($1); }
2569 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2575 ColumnDef *n = makeNode(ColumnDef);
2580 n->is_not_null = false;
2581 n->raw_default = NULL;
2582 n->cooked_default = NULL;
2583 n->constraints = NIL;
2589 WITH DATA_P { $$ = TRUE; }
2590 | WITH NO DATA_P { $$ = FALSE; }
2591 | /*EMPTY*/ { $$ = TRUE; }
2595 /*****************************************************************************
2598 * CREATE SEQUENCE seqname
2599 * ALTER SEQUENCE seqname
2601 *****************************************************************************/
2604 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2606 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2615 ALTER SEQUENCE qualified_name SeqOptList
2617 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2624 OptSeqOptList: SeqOptList { $$ = $1; }
2625 | /*EMPTY*/ { $$ = NIL; }
2628 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2629 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2632 SeqOptElem: CACHE NumericOnly
2634 $$ = makeDefElem("cache", (Node *)$2);
2638 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2642 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2644 | INCREMENT opt_by NumericOnly
2646 $$ = makeDefElem("increment", (Node *)$3);
2648 | MAXVALUE NumericOnly
2650 $$ = makeDefElem("maxvalue", (Node *)$2);
2652 | MINVALUE NumericOnly
2654 $$ = makeDefElem("minvalue", (Node *)$2);
2658 $$ = makeDefElem("maxvalue", NULL);
2662 $$ = makeDefElem("minvalue", NULL);
2666 $$ = makeDefElem("owned_by", (Node *)$3);
2668 | START opt_with NumericOnly
2670 $$ = makeDefElem("start", (Node *)$3);
2674 $$ = makeDefElem("restart", NULL);
2676 | RESTART opt_with NumericOnly
2678 $$ = makeDefElem("restart", (Node *)$3);
2687 FCONST { $$ = makeFloat($1); }
2693 | SignedIconst { $$ = makeInteger($1); };
2696 /*****************************************************************************
2699 * CREATE PROCEDURAL LANGUAGE ...
2700 * DROP PROCEDURAL LANGUAGE ...
2702 *****************************************************************************/
2705 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2707 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2709 /* parameters are all to be supplied by system */
2711 n->plvalidator = NIL;
2712 n->pltrusted = false;
2715 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2716 HANDLER handler_name opt_validator opt_lancompiler
2718 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2721 n->plvalidator = $8;
2723 /* LANCOMPILER is now ignored entirely */
2729 TRUSTED { $$ = TRUE; }
2730 | /*EMPTY*/ { $$ = FALSE; }
2733 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2734 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2735 * Work around by using simple names, instead.
2738 name { $$ = list_make1(makeString($1)); }
2739 | name attrs { $$ = lcons(makeString($1), $2); }
2743 VALIDATOR handler_name { $$ = $2; }
2744 | NO VALIDATOR { $$ = NIL; }
2748 validator_clause { $$ = $1; }
2749 | /*EMPTY*/ { $$ = NIL; }
2753 LANCOMPILER Sconst { $$ = $2; }
2754 | /*EMPTY*/ { $$ = NULL; }
2758 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2760 DropPLangStmt *n = makeNode(DropPLangStmt);
2763 n->missing_ok = false;
2766 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2768 DropPLangStmt *n = makeNode(DropPLangStmt);
2771 n->missing_ok = true;
2781 /*****************************************************************************
2784 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2786 *****************************************************************************/
2788 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2790 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2791 n->tablespacename = $3;
2798 OptTableSpaceOwner: OWNER name { $$ = $2; }
2799 | /*EMPTY */ { $$ = NULL; }
2802 /*****************************************************************************
2805 * DROP TABLESPACE <tablespace>
2807 * No need for drop behaviour as we cannot implement dependencies for
2808 * objects in other databases; we can only support RESTRICT.
2810 ****************************************************************************/
2812 DropTableSpaceStmt: DROP TABLESPACE name
2814 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2815 n->tablespacename = $3;
2816 n->missing_ok = false;
2819 | DROP TABLESPACE IF_P EXISTS name
2821 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2822 n->tablespacename = $5;
2823 n->missing_ok = true;
2828 /*****************************************************************************
2831 * CREATE FOREIGN DATA WRAPPER name [ VALIDATOR name ]
2833 *****************************************************************************/
2835 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name opt_validator create_generic_options
2837 CreateFdwStmt *n = makeNode(CreateFdwStmt);
2845 /*****************************************************************************
2848 * DROP FOREIGN DATA WRAPPER name
2850 ****************************************************************************/
2852 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
2854 DropFdwStmt *n = makeNode(DropFdwStmt);
2856 n->missing_ok = false;
2860 | DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
2862 DropFdwStmt *n = makeNode(DropFdwStmt);
2864 n->missing_ok = true;
2870 /*****************************************************************************
2873 * ALTER FOREIGN DATA WRAPPER name
2875 ****************************************************************************/
2877 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name validator_clause alter_generic_options
2879 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2882 n->change_validator = true;
2886 | ALTER FOREIGN DATA_P WRAPPER name validator_clause
2888 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2891 n->change_validator = true;
2894 | ALTER FOREIGN DATA_P WRAPPER name alter_generic_options
2896 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2903 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
2904 create_generic_options:
2905 OPTIONS '(' generic_option_list ')' { $$ = $3; }
2906 | /*EMPTY*/ { $$ = NIL; }
2909 generic_option_list:
2912 $$ = list_make1($1);
2914 | generic_option_list ',' generic_option_elem
2916 $$ = lappend($1, $3);
2920 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
2921 alter_generic_options:
2922 OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
2925 alter_generic_option_list:
2926 alter_generic_option_elem
2928 $$ = list_make1($1);
2930 | alter_generic_option_list ',' alter_generic_option_elem
2932 $$ = lappend($1, $3);
2936 alter_generic_option_elem:
2941 | SET generic_option_elem
2944 $$->defaction = DEFELEM_SET;
2946 | ADD_P generic_option_elem
2949 $$->defaction = DEFELEM_ADD;
2951 | DROP generic_option_name
2953 $$ = makeDefElemExtended(NULL, $2, NULL, DEFELEM_DROP);
2957 generic_option_elem:
2958 generic_option_name generic_option_arg
2960 $$ = makeDefElem($1, $2);
2964 generic_option_name:
2965 ColLabel { $$ = $1; }
2968 /* We could use def_arg here, but the spec only requires string literals */
2970 Sconst { $$ = (Node *) makeString($1); }
2973 /*****************************************************************************
2976 * CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
2978 *****************************************************************************/
2980 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
2981 FOREIGN DATA_P WRAPPER name create_generic_options
2983 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
2994 TYPE_P Sconst { $$ = $2; }
2995 | /*EMPTY*/ { $$ = NULL; }
2999 foreign_server_version:
3000 VERSION_P Sconst { $$ = $2; }
3001 | VERSION_P NULL_P { $$ = NULL; }
3004 opt_foreign_server_version:
3005 foreign_server_version { $$ = $1; }
3006 | /*EMPTY*/ { $$ = NULL; }
3009 /*****************************************************************************
3014 ****************************************************************************/
3016 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
3018 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3020 n->missing_ok = false;
3024 | DROP SERVER IF_P EXISTS name opt_drop_behavior
3026 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
3028 n->missing_ok = true;
3034 /*****************************************************************************
3037 * ALTER SERVER name [VERSION] [OPTIONS]
3039 ****************************************************************************/
3041 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
3043 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3047 n->has_version = true;
3050 | ALTER SERVER name foreign_server_version
3052 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3055 n->has_version = true;
3058 | ALTER SERVER name alter_generic_options
3060 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
3067 /*****************************************************************************
3070 * CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
3072 *****************************************************************************/
3074 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
3076 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3084 /* User mapping authorization identifier */
3086 CURRENT_USER { $$ = "current_user"; }
3087 | USER { $$ = "current_user"; }
3088 | RoleId { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3091 /*****************************************************************************
3094 * DROP USER MAPPING FOR auth_ident SERVER name
3096 ****************************************************************************/
3098 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3100 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3103 n->missing_ok = false;
3106 | DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3108 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3111 n->missing_ok = true;
3116 /*****************************************************************************
3119 * ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3121 ****************************************************************************/
3123 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3125 AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3133 /*****************************************************************************
3136 * CREATE TRIGGER ...
3139 *****************************************************************************/
3142 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3143 qualified_name TriggerForSpec EXECUTE PROCEDURE
3144 func_name '(' TriggerFuncArgs ')'
3146 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3154 n->isconstraint = FALSE;
3155 n->deferrable = FALSE;
3156 n->initdeferred = FALSE;
3157 n->constrrel = NULL;
3160 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
3161 qualified_name OptConstrFromTable
3162 ConstraintAttributeSpec
3163 FOR EACH ROW EXECUTE PROCEDURE
3164 func_name '(' TriggerFuncArgs ')'
3166 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3174 n->isconstraint = TRUE;
3175 n->deferrable = ($10 & 1) != 0;
3176 n->initdeferred = ($10 & 2) != 0;
3183 BEFORE { $$ = TRUE; }
3184 | AFTER { $$ = FALSE; }
3190 | TriggerEvents OR TriggerOneEvent
3193 parser_yyerror("duplicate trigger events specified");
3199 INSERT { $$ = TRIGGER_TYPE_INSERT; }
3200 | DELETE_P { $$ = TRIGGER_TYPE_DELETE; }
3201 | UPDATE { $$ = TRIGGER_TYPE_UPDATE; }
3202 | TRUNCATE { $$ = TRIGGER_TYPE_TRUNCATE; }
3206 FOR TriggerForOpt TriggerForType
3213 * If ROW/STATEMENT not specified, default to
3214 * STATEMENT, per SQL
3227 | STATEMENT { $$ = FALSE; }
3231 TriggerFuncArg { $$ = list_make1($1); }
3232 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
3233 | /*EMPTY*/ { $$ = NIL; }
3240 snprintf(buf, sizeof(buf), "%d", $1);
3241 $$ = makeString(pstrdup(buf));
3243 | FCONST { $$ = makeString($1); }
3244 | Sconst { $$ = makeString($1); }
3245 | BCONST { $$ = makeString($1); }
3246 | XCONST { $$ = makeString($1); }
3247 | ColId { $$ = makeString($1); }
3251 FROM qualified_name { $$ = $2; }
3252 | /*EMPTY*/ { $$ = NULL; }
3255 ConstraintAttributeSpec:
3256 ConstraintDeferrabilitySpec
3258 | ConstraintDeferrabilitySpec ConstraintTimeSpec
3260 if ($1 == 0 && $2 != 0)
3262 (errcode(ERRCODE_SYNTAX_ERROR),
3263 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3264 parser_errposition(@1)));
3267 | ConstraintTimeSpec
3274 | ConstraintTimeSpec ConstraintDeferrabilitySpec
3276 if ($2 == 0 && $1 != 0)
3278 (errcode(ERRCODE_SYNTAX_ERROR),
3279 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3280 parser_errposition(@1)));
3287 ConstraintDeferrabilitySpec:
3288 NOT DEFERRABLE { $$ = 0; }
3289 | DEFERRABLE { $$ = 1; }
3293 INITIALLY IMMEDIATE { $$ = 0; }
3294 | INITIALLY DEFERRED { $$ = 2; }
3299 DROP TRIGGER name ON qualified_name opt_drop_behavior
3301 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3305 n->removeType = OBJECT_TRIGGER;
3306 n->missing_ok = false;
3309 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
3311 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3315 n->removeType = OBJECT_TRIGGER;
3316 n->missing_ok = true;
3322 /*****************************************************************************
3325 * CREATE ASSERTION ...
3326 * DROP ASSERTION ...
3328 *****************************************************************************/
3331 CREATE ASSERTION name CHECK '(' a_expr ')'
3332 ConstraintAttributeSpec
3334 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3336 n->args = list_make1($6);
3337 n->isconstraint = TRUE;
3338 n->deferrable = ($8 & 1) != 0;
3339 n->initdeferred = ($8 & 2) != 0;
3342 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3343 errmsg("CREATE ASSERTION is not yet implemented")));
3350 DROP ASSERTION name opt_drop_behavior
3352 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3356 n->removeType = OBJECT_TRIGGER; /* XXX */
3358 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3359 errmsg("DROP ASSERTION is not yet implemented")));
3365 /*****************************************************************************
3368 * define (aggregate,operator,type)
3370 *****************************************************************************/
3373 CREATE AGGREGATE func_name aggr_args definition
3375 DefineStmt *n = makeNode(DefineStmt);
3376 n->kind = OBJECT_AGGREGATE;
3377 n->oldstyle = false;
3383 | CREATE AGGREGATE func_name old_aggr_definition
3385 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
3386 DefineStmt *n = makeNode(DefineStmt);
3387 n->kind = OBJECT_AGGREGATE;
3394 | CREATE OPERATOR any_operator definition
3396 DefineStmt *n = makeNode(DefineStmt);
3397 n->kind = OBJECT_OPERATOR;
3398 n->oldstyle = false;
3404 | CREATE TYPE_P any_name definition
3406 DefineStmt *n = makeNode(DefineStmt);
3407 n->kind = OBJECT_TYPE;
3408 n->oldstyle = false;
3414 | CREATE TYPE_P any_name
3416 /* Shell type (identified by lack of definition) */
3417 DefineStmt *n = makeNode(DefineStmt);
3418 n->kind = OBJECT_TYPE;
3419 n->oldstyle = false;
3422 n->definition = NIL;
3425 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3427 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3428 RangeVar *r = makeNode(RangeVar);
3430 /* can't use qualified_name, sigh */
3431 switch (list_length($3))
3434 r->catalogname = NULL;
3435 r->schemaname = NULL;
3436 r->relname = strVal(linitial($3));
3439 r->catalogname = NULL;
3440 r->schemaname = strVal(linitial($3));
3441 r->relname = strVal(lsecond($3));
3444 r->catalogname = strVal(linitial($3));
3445 r->schemaname = strVal(lsecond($3));
3446 r->relname = strVal(lthird($3));
3450 (errcode(ERRCODE_SYNTAX_ERROR),
3451 errmsg("improper qualified name (too many dotted names): %s",
3452 NameListToString($3)),
3453 parser_errposition(@3)));
3461 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3463 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3468 | CREATE TEXT_P SEARCH PARSER any_name definition
3470 DefineStmt *n = makeNode(DefineStmt);
3471 n->kind = OBJECT_TSPARSER;
3477 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3479 DefineStmt *n = makeNode(DefineStmt);
3480 n->kind = OBJECT_TSDICTIONARY;
3486 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3488 DefineStmt *n = makeNode(DefineStmt);
3489 n->kind = OBJECT_TSTEMPLATE;
3495 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3497 DefineStmt *n = makeNode(DefineStmt);
3498 n->kind = OBJECT_TSCONFIGURATION;
3506 definition: '(' def_list ')' { $$ = $2; }
3509 def_list: def_elem { $$ = list_make1($1); }
3510 | def_list ',' def_elem { $$ = lappend($1, $3); }
3513 def_elem: ColLabel '=' def_arg
3515 $$ = makeDefElem($1, (Node *) $3);
3519 $$ = makeDefElem($1, NULL);
3523 /* Note: any simple identifier will be returned as a type name! */
3524 def_arg: func_type { $$ = (Node *)$1; }
3525 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3526 | qual_all_Op { $$ = (Node *)$1; }
3527 | NumericOnly { $$ = (Node *)$1; }
3528 | Sconst { $$ = (Node *)makeString($1); }
3531 aggr_args: '(' type_list ')' { $$ = $2; }
3532 | '(' '*' ')' { $$ = NIL; }
3535 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3538 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3539 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3543 * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
3544 * the item names needed in old aggregate definitions are likely to become
3547 old_aggr_elem: IDENT '=' def_arg
3549 $$ = makeDefElem($1, (Node *)$3);
3553 enum_val_list: Sconst
3554 { $$ = list_make1(makeString($1)); }
3555 | enum_val_list ',' Sconst
3556 { $$ = lappend($1, makeString($3)); }
3560 /*****************************************************************************
3563 * CREATE OPERATOR CLASS ...
3564 * CREATE OPERATOR FAMILY ...
3565 * ALTER OPERATOR FAMILY ...
3566 * DROP OPERATOR CLASS ...
3567 * DROP OPERATOR FAMILY ...
3569 *****************************************************************************/
3572 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3573 USING access_method opt_opfamily AS opclass_item_list
3575 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3576 n->opclassname = $4;
3580 n->opfamilyname = $11;
3587 opclass_item { $$ = list_make1($1); }
3588 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3592 OPERATOR Iconst any_operator opt_recheck
3594 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3595 n->itemtype = OPCLASS_ITEM_OPERATOR;
3601 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3603 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3604 n->itemtype = OPCLASS_ITEM_OPERATOR;
3610 | FUNCTION Iconst func_name func_args
3612 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3613 n->itemtype = OPCLASS_ITEM_FUNCTION;
3615 n->args = extractArgTypes($4);
3619 | FUNCTION Iconst '(' type_list ')' func_name func_args
3621 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3622 n->itemtype = OPCLASS_ITEM_FUNCTION;
3624 n->args = extractArgTypes($7);
3631 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3632 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3638 opt_default: DEFAULT { $$ = TRUE; }
3639 | /*EMPTY*/ { $$ = FALSE; }
3642 opt_opfamily: FAMILY any_name { $$ = $2; }
3643 | /*EMPTY*/ { $$ = NIL; }
3646 opt_recheck: RECHECK
3649 * RECHECK no longer does anything in opclass definitions,
3650 * but we still accept it to ease porting of old database
3654 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3655 errmsg("RECHECK is no longer required"),
3656 errhint("Update your data type."),
3657 parser_errposition(@1)));
3660 | /*EMPTY*/ { $$ = FALSE; }
3665 CREATE OPERATOR FAMILY any_name USING access_method
3667 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3668 n->opfamilyname = $4;
3675 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3677 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3678 n->opfamilyname = $4;
3684 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3686 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3687 n->opfamilyname = $4;
3696 opclass_drop { $$ = list_make1($1); }
3697 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3701 OPERATOR Iconst '(' type_list ')'
3703 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3704 n->itemtype = OPCLASS_ITEM_OPERATOR;
3709 | FUNCTION Iconst '(' type_list ')'
3711 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3712 n->itemtype = OPCLASS_ITEM_FUNCTION;
3721 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3723 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3724 n->opclassname = $4;
3727 n->missing_ok = false;
3730 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3732 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3733 n->opclassname = $6;
3736 n->missing_ok = true;
3742 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3744 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3745 n->opfamilyname = $4;
3748 n->missing_ok = false;
3751 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3753 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3754 n->opfamilyname = $6;
3757 n->missing_ok = true;
3763 /*****************************************************************************
3767 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3768 * REASSIGN OWNED BY username [, username ...] TO username
3770 *****************************************************************************/
3772 DROP OWNED BY name_list opt_drop_behavior
3774 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3782 REASSIGN OWNED BY name_list TO name
3784 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3791 /*****************************************************************************
3795 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3796 * [ RESTRICT | CASCADE ]
3798 *****************************************************************************/
3800 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3802 DropStmt *n = makeNode(DropStmt);
3804 n->missing_ok = TRUE;
3809 | DROP drop_type any_name_list opt_drop_behavior
3811 DropStmt *n = makeNode(DropStmt);
3813 n->missing_ok = FALSE;
3821 drop_type: TABLE { $$ = OBJECT_TABLE; }
3822 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3823 | VIEW { $$ = OBJECT_VIEW; }
3824 | INDEX { $$ = OBJECT_INDEX; }
3825 | TYPE_P { $$ = OBJECT_TYPE; }
3826 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3827 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3828 | SCHEMA { $$ = OBJECT_SCHEMA; }
3829 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3830 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3831 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3832 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3836 any_name { $$ = list_make1($1); }
3837 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3840 any_name: ColId { $$ = list_make1(makeString($1)); }
3841 | ColId attrs { $$ = lcons(makeString($1), $2); }
3844 attrs: '.' attr_name
3845 { $$ = list_make1(makeString($2)); }
3846 | attrs '.' attr_name
3847 { $$ = lappend($1, makeString($3)); }
3851 /*****************************************************************************
3854 * truncate table relname1, relname2, ...
3856 *****************************************************************************/
3859 TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
3861 TruncateStmt *n = makeNode(TruncateStmt);
3863 n->restart_seqs = $4;
3870 CONTINUE_P IDENTITY_P { $$ = false; }
3871 | RESTART IDENTITY_P { $$ = true; }
3872 | /* EMPTY */ { $$ = false; }
3875 /*****************************************************************************
3877 * The COMMENT ON statement can take different forms based upon the type of
3878 * the object associated with the comment. The form of the statement is:
3880 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3881 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3882 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3883 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3884 * TEXT SEARCH TEMPLATE |
3885 * TEXT SEARCH CONFIGURATION ] <objname> |
3886 * AGGREGATE <aggname> (arg1, ...) |
3887 * FUNCTION <funcname> (arg1, arg2, ...) |
3888 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3889 * TRIGGER <triggername> ON <relname> |
3890 * CONSTRAINT <constraintname> ON <relname> |
3891 * RULE <rulename> ON <relname> ]
3894 *****************************************************************************/
3897 COMMENT ON comment_type any_name IS comment_text
3899 CommentStmt *n = makeNode(CommentStmt);
3906 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3908 CommentStmt *n = makeNode(CommentStmt);
3909 n->objtype = OBJECT_AGGREGATE;
3915 | COMMENT ON FUNCTION func_name func_args IS comment_text
3917 CommentStmt *n = makeNode(CommentStmt);
3918 n->objtype = OBJECT_FUNCTION;
3920 n->objargs = extractArgTypes($5);
3924 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3926 CommentStmt *n = makeNode(CommentStmt);
3927 n->objtype = OBJECT_OPERATOR;
3933 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3935 CommentStmt *n = makeNode(CommentStmt);
3936 n->objtype = OBJECT_CONSTRAINT;
3937 n->objname = lappend($6, makeString($4));
3942 | COMMENT ON RULE name ON any_name IS comment_text
3944 CommentStmt *n = makeNode(CommentStmt);
3945 n->objtype = OBJECT_RULE;
3946 n->objname = lappend($6, makeString($4));
3951 | COMMENT ON RULE name IS comment_text
3953 /* Obsolete syntax supported for awhile for compatibility */
3954 CommentStmt *n = makeNode(CommentStmt);
3955 n->objtype = OBJECT_RULE;
3956 n->objname = list_make1(makeString($4));
3961 | COMMENT ON TRIGGER name ON any_name IS comment_text
3963 CommentStmt *n = makeNode(CommentStmt);
3964 n->objtype = OBJECT_TRIGGER;
3965 n->objname = lappend($6, makeString($4));
3970 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3972 CommentStmt *n = makeNode(CommentStmt);
3973 n->objtype = OBJECT_OPCLASS;
3975 n->objargs = list_make1(makeString($7));
3979 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3981 CommentStmt *n = makeNode(CommentStmt);
3982 n->objtype = OBJECT_OPFAMILY;
3984 n->objargs = list_make1(makeString($7));
3988 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3990 CommentStmt *n = makeNode(CommentStmt);
3991 n->objtype = OBJECT_LARGEOBJECT;
3992 n->objname = list_make1($5);
3997 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3999 CommentStmt *n = makeNode(CommentStmt);
4000 n->objtype = OBJECT_CAST;
4001 n->objname = list_make1($5);
4002 n->objargs = list_make1($7);
4006 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
4008 CommentStmt *n = makeNode(CommentStmt);
4009 n->objtype = OBJECT_LANGUAGE;
4015 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
4017 CommentStmt *n = makeNode(CommentStmt);
4018 n->objtype = OBJECT_TSPARSER;
4023 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
4025 CommentStmt *n = makeNode(CommentStmt);
4026 n->objtype = OBJECT_TSDICTIONARY;
4031 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
4033 CommentStmt *n = makeNode(CommentStmt);
4034 n->objtype = OBJECT_TSTEMPLATE;
4039 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
4041 CommentStmt *n = makeNode(CommentStmt);
4042 n->objtype = OBJECT_TSCONFIGURATION;
4050 COLUMN { $$ = OBJECT_COLUMN; }
4051 | DATABASE { $$ = OBJECT_DATABASE; }
4052 | SCHEMA { $$ = OBJECT_SCHEMA; }
4053 | INDEX { $$ = OBJECT_INDEX; }
4054 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
4055 | TABLE { $$ = OBJECT_TABLE; }
4056 | DOMAIN_P { $$ = OBJECT_TYPE; }
4057 | TYPE_P { $$ = OBJECT_TYPE; }
4058 | VIEW { $$ = OBJECT_VIEW; }
4059 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
4060 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
4061 | ROLE { $$ = OBJECT_ROLE; }
4066 | NULL_P { $$ = NULL; }
4069 /*****************************************************************************
4074 *****************************************************************************/
4076 FetchStmt: FETCH fetch_direction from_in name
4078 FetchStmt *n = (FetchStmt *) $2;
4085 FetchStmt *n = makeNode(FetchStmt);
4086 n->direction = FETCH_FORWARD;
4092 | MOVE fetch_direction from_in name
4094 FetchStmt *n = (FetchStmt *) $2;
4101 FetchStmt *n = makeNode(FetchStmt);
4102 n->direction = FETCH_FORWARD;
4113 FetchStmt *n = makeNode(FetchStmt);
4114 n->direction = FETCH_FORWARD;
4120 FetchStmt *n = makeNode(FetchStmt);
4121 n->direction = FETCH_FORWARD;
4127 FetchStmt *n = makeNode(FetchStmt);
4128 n->direction = FETCH_BACKWARD;
4134 FetchStmt *n = makeNode(FetchStmt);
4135 n->direction = FETCH_ABSOLUTE;
4141 FetchStmt *n = makeNode(FetchStmt);
4142 n->direction = FETCH_ABSOLUTE;
4146 | ABSOLUTE_P SignedIconst
4148 FetchStmt *n = makeNode(FetchStmt);
4149 n->direction = FETCH_ABSOLUTE;
4153 | RELATIVE_P SignedIconst
4155 FetchStmt *n = makeNode(FetchStmt);
4156 n->direction = FETCH_RELATIVE;
4162 FetchStmt *n = makeNode(FetchStmt);
4163 n->direction = FETCH_FORWARD;
4169 FetchStmt *n = makeNode(FetchStmt);
4170 n->direction = FETCH_FORWARD;
4171 n->howMany = FETCH_ALL;
4176 FetchStmt *n = makeNode(FetchStmt);
4177 n->direction = FETCH_FORWARD;
4181 | FORWARD SignedIconst
4183 FetchStmt *n = makeNode(FetchStmt);
4184 n->direction = FETCH_FORWARD;
4190 FetchStmt *n = makeNode(FetchStmt);
4191 n->direction = FETCH_FORWARD;
4192 n->howMany = FETCH_ALL;
4197 FetchStmt *n = makeNode(FetchStmt);
4198 n->direction = FETCH_BACKWARD;
4202 | BACKWARD SignedIconst
4204 FetchStmt *n = makeNode(FetchStmt);
4205 n->direction = FETCH_BACKWARD;
4211 FetchStmt *n = makeNode(FetchStmt);
4212 n->direction = FETCH_BACKWARD;
4213 n->howMany = FETCH_ALL;
4223 /*****************************************************************************
4225 * GRANT and REVOKE statements
4227 *****************************************************************************/
4229 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
4230 opt_grant_grant_option
4232 GrantStmt *n = makeNode(GrantStmt);
4235 n->objtype = ($4)->objtype;
4236 n->objects = ($4)->objs;
4238 n->grant_option = $7;
4244 REVOKE privileges ON privilege_target
4245 FROM grantee_list opt_drop_behavior
4247 GrantStmt *n = makeNode(GrantStmt);
4248 n->is_grant = false;
4249 n->grant_option = false;
4251 n->objtype = ($4)->objtype;
4252 n->objects = ($4)->objs;
4257 | REVOKE GRANT OPTION FOR privileges ON privilege_target
4258 FROM grantee_list opt_drop_behavior
4260 GrantStmt *n = makeNode(GrantStmt);
4261 n->is_grant = false;
4262 n->grant_option = true;
4264 n->objtype = ($7)->objtype;
4265 n->objects = ($7)->objs;
4274 * Privilege names are represented as strings; the validity of the privilege
4275 * names gets checked at execution. This is a bit annoying but we have little
4276 * choice because of the syntactic conflict with lists of role names in
4277 * GRANT/REVOKE. What's more, we have to call out in the "privilege"
4278 * production any reserved keywords that need to be usable as privilege names.
4281 /* either ALL [PRIVILEGES] or a list of individual privileges */
4282 privileges: privilege_list
4288 | ALL '(' columnList ')'
4290 AccessPriv *n = makeNode(AccessPriv);
4291 n->priv_name = NULL;
4295 | ALL PRIVILEGES '(' columnList ')'
4297 AccessPriv *n = makeNode(AccessPriv);
4298 n->priv_name = NULL;
4304 privilege_list: privilege { $$ = list_make1($1); }
4305 | privilege_list ',' privilege { $$ = lappend($1, $3); }
4308 privilege: SELECT opt_column_list
4310 AccessPriv *n = makeNode(AccessPriv);
4311 n->priv_name = pstrdup($1);
4315 | REFERENCES opt_column_list
4317 AccessPriv *n = makeNode(AccessPriv);
4318 n->priv_name = pstrdup($1);
4322 | CREATE opt_column_list
4324 AccessPriv *n = makeNode(AccessPriv);
4325 n->priv_name = pstrdup($1);
4329 | ColId opt_column_list
4331 AccessPriv *n = makeNode(AccessPriv);
4339 /* Don't bother trying to fold the first two rules into one using
4340 * opt_table. You're going to get conflicts.
4345 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4346 n->objtype = ACL_OBJECT_RELATION;
4350 | TABLE qualified_name_list
4352 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4353 n->objtype = ACL_OBJECT_RELATION;
4357 | SEQUENCE qualified_name_list
4359 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4360 n->objtype = ACL_OBJECT_SEQUENCE;
4364 | FOREIGN DATA_P WRAPPER name_list
4366 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4367 n->objtype = ACL_OBJECT_FDW;
4371 | FOREIGN SERVER name_list
4373 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4374 n->objtype = ACL_OBJECT_FOREIGN_SERVER;
4378 | FUNCTION function_with_argtypes_list
4380 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4381 n->objtype = ACL_OBJECT_FUNCTION;
4385 | DATABASE name_list
4387 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4388 n->objtype = ACL_OBJECT_DATABASE;
4392 | LANGUAGE name_list
4394 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4395 n->objtype = ACL_OBJECT_LANGUAGE;
4401 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4402 n->objtype = ACL_OBJECT_NAMESPACE;
4406 | TABLESPACE name_list
4408 PrivTarget *n = (PrivTarget *) palloc(sizeof(PrivTarget));
4409 n->objtype = ACL_OBJECT_TABLESPACE;
4417 grantee { $$ = list_make1($1); }
4418 | grantee_list ',' grantee { $$ = lappend($1, $3); }
4423 PrivGrantee *n = makeNode(PrivGrantee);
4424 /* This hack lets us avoid reserving PUBLIC as a keyword*/
4425 if (strcmp($1, "public") == 0)
4433 PrivGrantee *n = makeNode(PrivGrantee);
4434 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
4435 if (strcmp($2, "public") == 0)
4444 opt_grant_grant_option:
4445 WITH GRANT OPTION { $$ = TRUE; }
4446 | /*EMPTY*/ { $$ = FALSE; }
4449 function_with_argtypes_list:
4450 function_with_argtypes { $$ = list_make1($1); }
4451 | function_with_argtypes_list ',' function_with_argtypes
4452 { $$ = lappend($1, $3); }
4455 function_with_argtypes:
4458 FuncWithArgs *n = makeNode(FuncWithArgs);
4460 n->funcargs = extractArgTypes($2);
4465 /*****************************************************************************
4467 * GRANT and REVOKE ROLE statements
4469 *****************************************************************************/
4472 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4474 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4476 n->granted_roles = $2;
4477 n->grantee_roles = $4;
4485 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4487 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4488 n->is_grant = false;
4489 n->admin_opt = false;
4490 n->granted_roles = $2;
4491 n->grantee_roles = $4;
4495 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4497 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4498 n->is_grant = false;
4499 n->admin_opt = true;
4500 n->granted_roles = $5;
4501 n->grantee_roles = $7;
4507 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4508 | /*EMPTY*/ { $$ = FALSE; }
4511 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4512 | /*EMPTY*/ { $$ = NULL; }
4516 /*****************************************************************************
4518 * QUERY: CREATE INDEX
4520 * Note: we can't factor CONCURRENTLY into a separate production without
4521 * making it a reserved word.
4523 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4524 * willing to make TABLESPACE a fully reserved word.
4525 *****************************************************************************/
4527 IndexStmt: CREATE index_opt_unique INDEX index_name
4528 ON qualified_name access_method_clause '(' index_params ')'
4529 opt_reloptions OptTableSpace where_clause
4531 IndexStmt *n = makeNode(IndexStmt);
4533 n->concurrent = false;
4536 n->accessMethod = $7;
4537 n->indexParams = $9;
4539 n->tableSpace = $12;
4540 n->whereClause = $13;
4543 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4544 ON qualified_name access_method_clause '(' index_params ')'
4545 opt_reloptions OptTableSpace where_clause
4547 IndexStmt *n = makeNode(IndexStmt);
4549 n->concurrent = true;
4552 n->accessMethod = $8;
4553 n->indexParams = $10;
4555 n->tableSpace = $13;
4556 n->whereClause = $14;
4562 UNIQUE { $$ = TRUE; }
4563 | /*EMPTY*/ { $$ = FALSE; }
4566 access_method_clause:
4567 USING access_method { $$ = $2; }
4568 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4571 index_params: index_elem { $$ = list_make1($1); }
4572 | index_params ',' index_elem { $$ = lappend($1, $3); }
4576 * Index attributes can be either simple column references, or arbitrary
4577 * expressions in parens. For backwards-compatibility reasons, we allow
4578 * an expression that's just a function call to be written without parens.
4580 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4582 $$ = makeNode(IndexElem);
4587 $$->nulls_ordering = $4;
4589 | func_expr opt_class opt_asc_desc opt_nulls_order
4591 $$ = makeNode(IndexElem);
4596 $$->nulls_ordering = $4;
4598 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4600 $$ = makeNode(IndexElem);
4605 $$->nulls_ordering = $6;
4609 opt_class: any_name { $$ = $1; }
4610 | USING any_name { $$ = $2; }
4611 | /*EMPTY*/ { $$ = NIL; }
4614 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4615 | DESC { $$ = SORTBY_DESC; }
4616 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4619 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4620 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4621 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4625 /*****************************************************************************
4628 * create [or replace] function <fname>
4629 * [(<type-1> { , <type-n>})]
4631 * as <filename or code in language as appropriate>
4632 * language <lang> [with parameters]
4634 *****************************************************************************/
4637 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4638 RETURNS func_return createfunc_opt_list opt_definition
4640 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4649 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4650 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4652 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4655 n->parameters = mergeTableFuncParameters($5, $9);
4656 n->returnType = TableFuncTypeName($9);
4657 n->returnType->location = @7;
4659 n->withClause = $12;
4662 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4663 createfunc_opt_list opt_definition
4665 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4669 n->returnType = NULL;
4677 OR REPLACE { $$ = TRUE; }
4678 | /*EMPTY*/ { $$ = FALSE; }
4681 func_args: '(' func_args_list ')' { $$ = $2; }
4682 | '(' ')' { $$ = NIL; }
4686 func_arg { $$ = list_make1($1); }
4687 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4691 * func_args_with_defaults is separate because we only want to accept
4692 * defaults in CREATE FUNCTION, not in ALTER etc.
4694 func_args_with_defaults:
4695 '(' func_args_with_defaults_list ')' { $$ = $2; }
4696 | '(' ')' { $$ = NIL; }
4699 func_args_with_defaults_list:
4700 func_arg_with_default { $$ = list_make1($1); }
4701 | func_args_with_defaults_list ',' func_arg_with_default
4702 { $$ = lappend($1, $3); }
4706 * The style with arg_class first is SQL99 standard, but Oracle puts
4707 * param_name first; accept both since it's likely people will try both
4708 * anyway. Don't bother trying to save productions by letting arg_class
4709 * have an empty alternative ... you'll get shift/reduce conflicts.
4711 * We can catch over-specified arguments here if we want to,
4712 * but for now better to silently swallow typmod, etc.
4713 * - thomas 2000-03-22
4716 arg_class param_name func_type
4718 FunctionParameter *n = makeNode(FunctionParameter);
4725 | param_name arg_class func_type
4727 FunctionParameter *n = makeNode(FunctionParameter);
4734 | param_name func_type
4736 FunctionParameter *n = makeNode(FunctionParameter);
4739 n->mode = FUNC_PARAM_IN;
4743 | arg_class func_type
4745 FunctionParameter *n = makeNode(FunctionParameter);
4754 FunctionParameter *n = makeNode(FunctionParameter);
4757 n->mode = FUNC_PARAM_IN;
4763 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4764 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4765 | OUT_P { $$ = FUNC_PARAM_OUT; }
4766 | INOUT { $$ = FUNC_PARAM_INOUT; }
4767 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4768 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4772 * Ideally param_name should be ColId, but that causes too many conflicts.
4774 param_name: type_function_name
4780 /* We can catch over-specified results here if we want to,
4781 * but for now better to silently swallow typmod, etc.
4782 * - thomas 2000-03-22
4789 * We would like to make the %TYPE productions here be ColId attrs etc,
4790 * but that causes reduce/reduce conflicts. type_function_name
4791 * is next best choice.
4793 func_type: Typename { $$ = $1; }
4794 | type_function_name attrs '%' TYPE_P
4796 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4797 $$->pct_type = true;
4800 | SETOF type_function_name attrs '%' TYPE_P
4802 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4803 $$->pct_type = true;
4809 func_arg_with_default:
4814 | func_arg DEFAULT a_expr
4819 | func_arg '=' a_expr
4827 createfunc_opt_list:
4828 /* Must be at least one to prevent conflict */
4829 createfunc_opt_item { $$ = list_make1($1); }
4830 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4834 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4836 common_func_opt_item:
4837 CALLED ON NULL_P INPUT_P
4839 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4841 | RETURNS NULL_P ON NULL_P INPUT_P
4843 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4847 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4851 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4855 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4859 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4861 | EXTERNAL SECURITY DEFINER
4863 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4865 | EXTERNAL SECURITY INVOKER
4867 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4871 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4875 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4879 $$ = makeDefElem("cost", (Node *)$2);
4883 $$ = makeDefElem("rows", (Node *)$2);
4887 /* we abuse the normal content of a DefElem here */
4888 $$ = makeDefElem("set", (Node *)$1);
4892 createfunc_opt_item:
4895 $$ = makeDefElem("as", (Node *)$2);
4897 | LANGUAGE ColId_or_Sconst
4899 $$ = makeDefElem("language", (Node *)makeString($2));
4903 $$ = makeDefElem("window", (Node *)makeInteger(TRUE));
4905 | common_func_opt_item
4911 func_as: Sconst { $$ = list_make1(makeString($1)); }
4914 $$ = list_make2(makeString($1), makeString($3));
4919 WITH definition { $$ = $2; }
4920 | /*EMPTY*/ { $$ = NIL; }
4923 table_func_column: param_name func_type
4925 FunctionParameter *n = makeNode(FunctionParameter);
4928 n->mode = FUNC_PARAM_TABLE;
4934 table_func_column_list:
4937 $$ = list_make1($1);
4939 | table_func_column_list ',' table_func_column
4941 $$ = lappend($1, $3);
4945 /*****************************************************************************
4948 * RENAME and OWNER subcommands are already provided by the generic
4949 * ALTER infrastructure, here we just specify alterations that can
4950 * only be applied to functions.
4952 *****************************************************************************/
4954 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4956 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4964 /* At least one option must be specified */
4965 common_func_opt_item { $$ = list_make1($1); }
4966 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4969 /* Ignored, merely for SQL compliance */
4976 /*****************************************************************************
4980 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4981 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4982 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4984 *****************************************************************************/
4987 DROP FUNCTION func_name func_args opt_drop_behavior
4989 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4990 n->kind = OBJECT_FUNCTION;
4992 n->args = extractArgTypes($4);
4994 n->missing_ok = false;
4997 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4999 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5000 n->kind = OBJECT_FUNCTION;
5002 n->args = extractArgTypes($6);
5004 n->missing_ok = true;
5010 DROP AGGREGATE func_name aggr_args opt_drop_behavior
5012 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5013 n->kind = OBJECT_AGGREGATE;
5017 n->missing_ok = false;
5020 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
5022 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5023 n->kind = OBJECT_AGGREGATE;
5027 n->missing_ok = true;
5033 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
5035 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5036 n->kind = OBJECT_OPERATOR;
5040 n->missing_ok = false;
5043 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
5045 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
5046 n->kind = OBJECT_OPERATOR;
5050 n->missing_ok = true;
5059 (errcode(ERRCODE_SYNTAX_ERROR),
5060 errmsg("missing argument"),
5061 errhint("Use NONE to denote the missing argument of a unary operator."),
5062 parser_errposition(@3)));
5064 | '(' Typename ',' Typename ')'
5065 { $$ = list_make2($2, $4); }
5066 | '(' NONE ',' Typename ')' /* left unary */
5067 { $$ = list_make2(NULL, $4); }
5068 | '(' Typename ',' NONE ')' /* right unary */
5069 { $$ = list_make2($2, NULL); }
5074 { $$ = list_make1(makeString($1)); }
5075 | ColId '.' any_operator
5076 { $$ = lcons(makeString($1), $3); }
5080 /*****************************************************************************
5082 * CREATE CAST / DROP CAST
5084 *****************************************************************************/
5086 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
5087 WITH FUNCTION function_with_argtypes cast_context
5089 CreateCastStmt *n = makeNode(CreateCastStmt);
5093 n->context = (CoercionContext) $11;
5097 | CREATE CAST '(' Typename AS Typename ')'
5098 WITHOUT FUNCTION cast_context
5100 CreateCastStmt *n = makeNode(CreateCastStmt);
5104 n->context = (CoercionContext) $10;
5108 | CREATE CAST '(' Typename AS Typename ')'
5109 WITH INOUT cast_context
5111 CreateCastStmt *n = makeNode(CreateCastStmt);
5115 n->context = (CoercionContext) $10;
5121 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
5122 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
5123 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
5127 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
5129 DropCastStmt *n = makeNode(DropCastStmt);
5138 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
5139 | /*EMPTY*/ { $$ = FALSE; }
5143 /*****************************************************************************
5147 * REINDEX type <name> [FORCE]
5149 * FORCE no longer does anything, but we accept it for backwards compatibility
5150 *****************************************************************************/
5153 REINDEX reindex_type qualified_name opt_force
5155 ReindexStmt *n = makeNode(ReindexStmt);
5161 | REINDEX SYSTEM_P name opt_force
5163 ReindexStmt *n = makeNode(ReindexStmt);
5164 n->kind = OBJECT_DATABASE;
5167 n->do_system = true;
5171 | REINDEX DATABASE name opt_force
5173 ReindexStmt *n = makeNode(ReindexStmt);
5174 n->kind = OBJECT_DATABASE;
5177 n->do_system = true;
5184 INDEX { $$ = OBJECT_INDEX; }
5185 | TABLE { $$ = OBJECT_TABLE; }
5188 opt_force: FORCE { $$ = TRUE; }
5189 | /* EMPTY */ { $$ = FALSE; }
5193 /*****************************************************************************
5195 * ALTER THING name RENAME TO newname
5197 *****************************************************************************/
5199 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
5201 RenameStmt *n = makeNode(RenameStmt);
5202 n->renameType = OBJECT_AGGREGATE;
5208 | ALTER CONVERSION_P any_name RENAME TO name
5210 RenameStmt *n = makeNode(RenameStmt);
5211 n->renameType = OBJECT_CONVERSION;
5216 | ALTER DATABASE database_name RENAME TO database_name
5218 RenameStmt *n = makeNode(RenameStmt);
5219 n->renameType = OBJECT_DATABASE;
5224 | ALTER FUNCTION function_with_argtypes RENAME TO name
5226 RenameStmt *n = makeNode(RenameStmt);
5227 n->renameType = OBJECT_FUNCTION;
5228 n->object = $3->funcname;
5229 n->objarg = $3->funcargs;
5233 | ALTER GROUP_P RoleId RENAME TO RoleId
5235 RenameStmt *n = makeNode(RenameStmt);
5236 n->renameType = OBJECT_ROLE;
5241 | ALTER opt_procedural LANGUAGE name RENAME TO name
5243 RenameStmt *n = makeNode(RenameStmt);
5244 n->renameType = OBJECT_LANGUAGE;
5249 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
5251 RenameStmt *n = makeNode(RenameStmt);
5252 n->renameType = OBJECT_OPCLASS;
5258 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
5260 RenameStmt *n = makeNode(RenameStmt);
5261 n->renameType = OBJECT_OPFAMILY;
5267 | ALTER SCHEMA name RENAME TO name
5269 RenameStmt *n = makeNode(RenameStmt);
5270 n->renameType = OBJECT_SCHEMA;
5275 | ALTER TABLE relation_expr RENAME TO name
5277 RenameStmt *n = makeNode(RenameStmt);
5278 n->renameType = OBJECT_TABLE;
5284 | ALTER SEQUENCE qualified_name RENAME TO name
5286 RenameStmt *n = makeNode(RenameStmt);
5287 n->renameType = OBJECT_SEQUENCE;
5293 | ALTER VIEW qualified_name RENAME TO name
5295 RenameStmt *n = makeNode(RenameStmt);
5296 n->renameType = OBJECT_VIEW;
5302 | ALTER INDEX qualified_name RENAME TO name
5304 RenameStmt *n = makeNode(RenameStmt);
5305 n->renameType = OBJECT_INDEX;
5311 | ALTER TABLE relation_expr RENAME opt_column name TO name
5313 RenameStmt *n = makeNode(RenameStmt);
5314 n->renameType = OBJECT_COLUMN;
5320 | ALTER TRIGGER name ON qualified_name RENAME TO name
5322 RenameStmt *n = makeNode(RenameStmt);
5323 n->renameType = OBJECT_TRIGGER;
5329 | ALTER ROLE RoleId RENAME TO RoleId
5331 RenameStmt *n = makeNode(RenameStmt);
5332 n->renameType = OBJECT_ROLE;
5337 | ALTER USER RoleId RENAME TO RoleId
5339 RenameStmt *n = makeNode(RenameStmt);
5340 n->renameType = OBJECT_ROLE;
5345 | ALTER TABLESPACE name RENAME TO name
5347 RenameStmt *n = makeNode(RenameStmt);
5348 n->renameType = OBJECT_TABLESPACE;
5353 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
5355 RenameStmt *n = makeNode(RenameStmt);
5356 n->renameType = OBJECT_TSPARSER;
5361 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
5363 RenameStmt *n = makeNode(RenameStmt);
5364 n->renameType = OBJECT_TSDICTIONARY;
5369 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
5371 RenameStmt *n = makeNode(RenameStmt);
5372 n->renameType = OBJECT_TSTEMPLATE;
5377 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
5379 RenameStmt *n = makeNode(RenameStmt);
5380 n->renameType = OBJECT_TSCONFIGURATION;
5385 | ALTER TYPE_P any_name RENAME TO name
5387 RenameStmt *n = makeNode(RenameStmt);
5388 n->renameType = OBJECT_TYPE;
5395 opt_column: COLUMN { $$ = COLUMN; }
5396 | /*EMPTY*/ { $$ = 0; }
5399 opt_set_data: SET DATA_P { $$ = 1; }
5400 | /*EMPTY*/ { $$ = 0; }
5403 /*****************************************************************************
5405 * ALTER THING name SET SCHEMA name
5407 *****************************************************************************/
5409 AlterObjectSchemaStmt:
5410 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
5412 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5413 n->objectType = OBJECT_AGGREGATE;
5419 | ALTER DOMAIN_P any_name SET SCHEMA name
5421 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5422 n->objectType = OBJECT_DOMAIN;
5427 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
5429 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5430 n->objectType = OBJECT_FUNCTION;
5431 n->object = $3->funcname;
5432 n->objarg = $3->funcargs;
5436 | ALTER TABLE relation_expr SET SCHEMA name
5438 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5439 n->objectType = OBJECT_TABLE;
5444 | ALTER SEQUENCE qualified_name SET SCHEMA name
5446 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5447 n->objectType = OBJECT_SEQUENCE;
5452 | ALTER VIEW qualified_name SET SCHEMA name
5454 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5455 n->objectType = OBJECT_VIEW;
5460 | ALTER TYPE_P any_name SET SCHEMA name
5462 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5463 n->objectType = OBJECT_TYPE;
5470 /*****************************************************************************
5472 * ALTER THING name OWNER TO newname
5474 *****************************************************************************/
5476 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5478 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5479 n->objectType = OBJECT_AGGREGATE;
5485 | ALTER CONVERSION_P any_name OWNER TO RoleId
5487 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5488 n->objectType = OBJECT_CONVERSION;
5493 | ALTER DATABASE database_name OWNER TO RoleId
5495 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5496 n->objectType = OBJECT_DATABASE;
5497 n->object = list_make1(makeString($3));
5501 | ALTER DOMAIN_P any_name OWNER TO RoleId
5503 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5504 n->objectType = OBJECT_DOMAIN;
5509 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5511 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5512 n->objectType = OBJECT_FUNCTION;
5513 n->object = $3->funcname;
5514 n->objarg = $3->funcargs;
5518 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5520 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5521 n->objectType = OBJECT_LANGUAGE;
5522 n->object = list_make1(makeString($4));
5526 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5528 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5529 n->objectType = OBJECT_OPERATOR;
5535 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5537 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5538 n->objectType = OBJECT_OPCLASS;
5544 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5546 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5547 n->objectType = OBJECT_OPFAMILY;
5553 | ALTER SCHEMA name OWNER TO RoleId
5555 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5556 n->objectType = OBJECT_SCHEMA;
5557 n->object = list_make1(makeString($3));
5561 | ALTER TYPE_P any_name OWNER TO RoleId
5563 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5564 n->objectType = OBJECT_TYPE;
5569 | ALTER TABLESPACE name OWNER TO RoleId
5571 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5572 n->objectType = OBJECT_TABLESPACE;
5573 n->object = list_make1(makeString($3));
5577 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5579 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5580 n->objectType = OBJECT_TSDICTIONARY;
5585 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5587 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5588 n->objectType = OBJECT_TSCONFIGURATION;
5593 | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
5595 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5596 n->objectType = OBJECT_FDW;
5597 n->object = list_make1(makeString($5));
5601 | ALTER SERVER name OWNER TO RoleId
5603 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5604 n->objectType = OBJECT_FOREIGN_SERVER;
5605 n->object = list_make1(makeString($3));
5612 /*****************************************************************************
5614 * QUERY: Define Rewrite Rule
5616 *****************************************************************************/
5618 RuleStmt: CREATE opt_or_replace RULE name AS
5619 { pg_yyget_extra(yyscanner)->QueryIsRule = TRUE; }
5620 ON event TO qualified_name where_clause
5621 DO opt_instead RuleActionList
5623 RuleStmt *n = makeNode(RuleStmt);
5627 n->whereClause = $11;
5632 pg_yyget_extra(yyscanner)->QueryIsRule = FALSE;
5637 NOTHING { $$ = NIL; }
5638 | RuleActionStmt { $$ = list_make1($1); }
5639 | '(' RuleActionMulti ')' { $$ = $2; }
5642 /* the thrashing around here is to discard "empty" statements... */
5644 RuleActionMulti ';' RuleActionStmtOrEmpty
5646 $$ = lappend($1, $3);
5650 | RuleActionStmtOrEmpty
5652 $$ = list_make1($1);
5666 RuleActionStmtOrEmpty:
5667 RuleActionStmt { $$ = $1; }
5668 | /*EMPTY*/ { $$ = NULL; }
5671 event: SELECT { $$ = CMD_SELECT; }
5672 | UPDATE { $$ = CMD_UPDATE; }
5673 | DELETE_P { $$ = CMD_DELETE; }
5674 | INSERT { $$ = CMD_INSERT; }
5678 INSTEAD { $$ = TRUE; }
5679 | ALSO { $$ = FALSE; }
5680 | /*EMPTY*/ { $$ = FALSE; }
5685 DROP RULE name ON qualified_name opt_drop_behavior
5687 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5691 n->removeType = OBJECT_RULE;
5692 n->missing_ok = false;
5695 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5697 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5701 n->removeType = OBJECT_RULE;
5702 n->missing_ok = true;
5708 /*****************************************************************************
5711 * NOTIFY <identifier> can appear both in rule bodies and
5712 * as a query-level command
5714 *****************************************************************************/
5716 NotifyStmt: NOTIFY ColId
5718 NotifyStmt *n = makeNode(NotifyStmt);
5719 n->conditionname = $2;
5724 ListenStmt: LISTEN ColId
5726 ListenStmt *n = makeNode(ListenStmt);
5727 n->conditionname = $2;
5735 UnlistenStmt *n = makeNode(UnlistenStmt);
5736 n->conditionname = $2;
5741 UnlistenStmt *n = makeNode(UnlistenStmt);
5742 n->conditionname = NULL;
5748 /*****************************************************************************
5752 * BEGIN / COMMIT / ROLLBACK
5753 * (also older versions END / ABORT)
5755 *****************************************************************************/
5758 ABORT_P opt_transaction
5760 TransactionStmt *n = makeNode(TransactionStmt);
5761 n->kind = TRANS_STMT_ROLLBACK;
5765 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5767 TransactionStmt *n = makeNode(TransactionStmt);
5768 n->kind = TRANS_STMT_BEGIN;
5772 | START TRANSACTION transaction_mode_list_or_empty
5774 TransactionStmt *n = makeNode(TransactionStmt);
5775 n->kind = TRANS_STMT_START;
5779 | COMMIT opt_transaction
5781 TransactionStmt *n = makeNode(TransactionStmt);
5782 n->kind = TRANS_STMT_COMMIT;
5786 | END_P opt_transaction
5788 TransactionStmt *n = makeNode(TransactionStmt);
5789 n->kind = TRANS_STMT_COMMIT;
5793 | ROLLBACK opt_transaction
5795 TransactionStmt *n = makeNode(TransactionStmt);
5796 n->kind = TRANS_STMT_ROLLBACK;
5802 TransactionStmt *n = makeNode(TransactionStmt);
5803 n->kind = TRANS_STMT_SAVEPOINT;
5804 n->options = list_make1(makeDefElem("savepoint_name",
5805 (Node *)makeString($2)));
5808 | RELEASE SAVEPOINT ColId
5810 TransactionStmt *n = makeNode(TransactionStmt);
5811 n->kind = TRANS_STMT_RELEASE;
5812 n->options = list_make1(makeDefElem("savepoint_name",
5813 (Node *)makeString($3)));
5818 TransactionStmt *n = makeNode(TransactionStmt);
5819 n->kind = TRANS_STMT_RELEASE;
5820 n->options = list_make1(makeDefElem("savepoint_name",
5821 (Node *)makeString($2)));
5824 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5826 TransactionStmt *n = makeNode(TransactionStmt);
5827 n->kind = TRANS_STMT_ROLLBACK_TO;
5828 n->options = list_make1(makeDefElem("savepoint_name",
5829 (Node *)makeString($5)));
5832 | ROLLBACK opt_transaction TO ColId
5834 TransactionStmt *n = makeNode(TransactionStmt);
5835 n->kind = TRANS_STMT_ROLLBACK_TO;
5836 n->options = list_make1(makeDefElem("savepoint_name",
5837 (Node *)makeString($4)));
5840 | PREPARE TRANSACTION Sconst
5842 TransactionStmt *n = makeNode(TransactionStmt);
5843 n->kind = TRANS_STMT_PREPARE;
5847 | COMMIT PREPARED Sconst
5849 TransactionStmt *n = makeNode(TransactionStmt);
5850 n->kind = TRANS_STMT_COMMIT_PREPARED;
5854 | ROLLBACK PREPARED Sconst
5856 TransactionStmt *n = makeNode(TransactionStmt);
5857 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5863 opt_transaction: WORK {}
5868 transaction_mode_item:
5869 ISOLATION LEVEL iso_level
5870 { $$ = makeDefElem("transaction_isolation",
5871 makeStringConst($3, @3)); }
5873 { $$ = makeDefElem("transaction_read_only",
5874 makeIntConst(TRUE, @1)); }
5876 { $$ = makeDefElem("transaction_read_only",
5877 makeIntConst(FALSE, @1)); }
5880 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5881 transaction_mode_list:
5882 transaction_mode_item
5883 { $$ = list_make1($1); }
5884 | transaction_mode_list ',' transaction_mode_item
5885 { $$ = lappend($1, $3); }
5886 | transaction_mode_list transaction_mode_item
5887 { $$ = lappend($1, $2); }
5890 transaction_mode_list_or_empty:
5891 transaction_mode_list
5897 /*****************************************************************************
5900 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5901 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5903 *****************************************************************************/
5905 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5906 AS SelectStmt opt_check_option
5908 ViewStmt *n = makeNode(ViewStmt);
5910 n->view->istemp = $2;
5916 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5917 AS SelectStmt opt_check_option
5919 ViewStmt *n = makeNode(ViewStmt);
5921 n->view->istemp = $4;
5933 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5934 errmsg("WITH CHECK OPTION is not implemented")));
5936 | WITH CASCADED CHECK OPTION
5939 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5940 errmsg("WITH CHECK OPTION is not implemented")));
5942 | WITH LOCAL CHECK OPTION
5945 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5946 errmsg("WITH CHECK OPTION is not implemented")));
5948 | /* EMPTY */ { $$ = NIL; }
5951 /*****************************************************************************
5956 *****************************************************************************/
5958 LoadStmt: LOAD file_name
5960 LoadStmt *n = makeNode(LoadStmt);
5967 /*****************************************************************************
5971 *****************************************************************************/
5974 CREATE DATABASE database_name opt_with createdb_opt_list
5976 CreatedbStmt *n = makeNode(CreatedbStmt);
5984 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5985 | /* EMPTY */ { $$ = NIL; }
5989 TABLESPACE opt_equal name
5991 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5993 | TABLESPACE opt_equal DEFAULT
5995 $$ = makeDefElem("tablespace", NULL);
5997 | LOCATION opt_equal Sconst
5999 $$ = makeDefElem("location", (Node *)makeString($3));
6001 | LOCATION opt_equal DEFAULT
6003 $$ = makeDefElem("location", NULL);
6005 | TEMPLATE opt_equal name
6007 $$ = makeDefElem("template", (Node *)makeString($3));
6009 | TEMPLATE opt_equal DEFAULT
6011 $$ = makeDefElem("template", NULL);
6013 | ENCODING opt_equal Sconst
6015 $$ = makeDefElem("encoding", (Node *)makeString($3));
6017 | ENCODING opt_equal Iconst
6019 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
6021 | ENCODING opt_equal DEFAULT
6023 $$ = makeDefElem("encoding", NULL);
6025 | LC_COLLATE_P opt_equal Sconst
6027 $$ = makeDefElem("lc_collate", (Node *)makeString($3));
6029 | LC_COLLATE_P opt_equal DEFAULT
6031 $$ = makeDefElem("lc_collate", NULL);
6033 | LC_CTYPE_P opt_equal Sconst
6035 $$ = makeDefElem("lc_ctype", (Node *)makeString($3));
6037 | LC_CTYPE_P opt_equal DEFAULT
6039 $$ = makeDefElem("lc_ctype", NULL);
6041 | CONNECTION LIMIT opt_equal SignedIconst
6043 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6045 | OWNER opt_equal name
6047 $$ = makeDefElem("owner", (Node *)makeString($3));
6049 | OWNER opt_equal DEFAULT
6051 $$ = makeDefElem("owner", NULL);
6056 * Though the equals sign doesn't match other WITH options, pg_dump uses
6057 * equals for backward compatibility, and it doesn't seem worth removing it.
6064 /*****************************************************************************
6068 *****************************************************************************/
6071 ALTER DATABASE database_name opt_with alterdb_opt_list
6073 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6078 | ALTER DATABASE database_name SET TABLESPACE name
6080 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
6082 n->options = list_make1(makeDefElem("tablespace",
6083 (Node *)makeString($6)));
6088 AlterDatabaseSetStmt:
6089 ALTER DATABASE database_name SetResetClause
6091 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
6100 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
6101 | /* EMPTY */ { $$ = NIL; }
6105 CONNECTION LIMIT opt_equal SignedIconst
6107 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6112 /*****************************************************************************
6114 * DROP DATABASE [ IF EXISTS ]
6116 * This is implicitly CASCADE, no need for drop behavior
6117 *****************************************************************************/
6119 DropdbStmt: DROP DATABASE database_name
6121 DropdbStmt *n = makeNode(DropdbStmt);
6123 n->missing_ok = FALSE;
6126 | DROP DATABASE IF_P EXISTS database_name
6128 DropdbStmt *n = makeNode(DropdbStmt);
6130 n->missing_ok = TRUE;
6136 /*****************************************************************************
6138 * Manipulate a domain
6140 *****************************************************************************/
6143 CREATE DOMAIN_P any_name opt_as Typename ColQualList
6145 CreateDomainStmt *n = makeNode(CreateDomainStmt);
6148 n->constraints = $6;
6154 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
6155 ALTER DOMAIN_P any_name alter_column_default
6157 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6163 /* ALTER DOMAIN <domain> DROP NOT NULL */
6164 | ALTER DOMAIN_P any_name DROP NOT NULL_P
6166 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6171 /* ALTER DOMAIN <domain> SET NOT NULL */
6172 | ALTER DOMAIN_P any_name SET NOT NULL_P
6174 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6179 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
6180 | ALTER DOMAIN_P any_name ADD_P TableConstraint
6182 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6188 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
6189 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
6191 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6205 /*****************************************************************************
6207 * Manipulate a text search dictionary or configuration
6209 *****************************************************************************/
6211 AlterTSDictionaryStmt:
6212 ALTER TEXT_P SEARCH DICTIONARY any_name definition
6214 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
6221 AlterTSConfigurationStmt:
6222 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
6224 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6228 n->override = false;
6232 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
6234 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6242 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
6244 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6247 n->dicts = list_make2($9,$11);
6248 n->override = false;
6252 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
6254 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6257 n->dicts = list_make2($11,$13);
6258 n->override = false;
6262 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
6264 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6267 n->missing_ok = false;
6270 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
6272 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6275 n->missing_ok = true;
6281 /*****************************************************************************
6283 * Manipulate a conversion
6285 * CREATE [DEFAULT] CONVERSION <conversion_name>
6286 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
6288 *****************************************************************************/
6290 CreateConversionStmt:
6291 CREATE opt_default CONVERSION_P any_name FOR Sconst
6292 TO Sconst FROM any_name
6294 CreateConversionStmt *n = makeNode(CreateConversionStmt);
6295 n->conversion_name = $4;
6296 n->for_encoding_name = $6;
6297 n->to_encoding_name = $8;
6304 /*****************************************************************************
6307 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
6309 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
6311 *****************************************************************************/
6314 CLUSTER opt_verbose qualified_name cluster_index_specification
6316 ClusterStmt *n = makeNode(ClusterStmt);
6322 | CLUSTER opt_verbose
6324 ClusterStmt *n = makeNode(ClusterStmt);
6326 n->indexname = NULL;
6330 /* kept for pre-8.3 compatibility */
6331 | CLUSTER opt_verbose index_name ON qualified_name
6333 ClusterStmt *n = makeNode(ClusterStmt);
6341 cluster_index_specification:
6342 USING index_name { $$ = $2; }
6343 | /*EMPTY*/ { $$ = NULL; }
6347 /*****************************************************************************
6353 *****************************************************************************/
6355 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
6357 VacuumStmt *n = makeNode(VacuumStmt);
6361 n->freeze_min_age = $3 ? 0 : -1;
6362 n->freeze_table_age = $3 ? 0 : -1;
6368 | VACUUM opt_full opt_freeze opt_verbose qualified_name
6370 VacuumStmt *n = makeNode(VacuumStmt);
6374 n->freeze_min_age = $3 ? 0 : -1;
6375 n->freeze_table_age = $3 ? 0 : -1;
6381 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
6383 VacuumStmt *n = (VacuumStmt *) $5;
6386 n->freeze_min_age = $3 ? 0 : -1;
6387 n->freeze_table_age = $3 ? 0 : -1;
6394 analyze_keyword opt_verbose
6396 VacuumStmt *n = makeNode(VacuumStmt);
6400 n->freeze_min_age = -1;
6401 n->freeze_table_age = -1;
6407 | analyze_keyword opt_verbose qualified_name opt_name_list
6409 VacuumStmt *n = makeNode(VacuumStmt);
6413 n->freeze_min_age = -1;
6414 n->freeze_table_age = -1;
6424 | ANALYSE /* British */ {}
6428 VERBOSE { $$ = TRUE; }
6429 | /*EMPTY*/ { $$ = FALSE; }
6432 opt_full: FULL { $$ = TRUE; }
6433 | /*EMPTY*/ { $$ = FALSE; }
6436 opt_freeze: FREEZE { $$ = TRUE; }
6437 | /*EMPTY*/ { $$ = FALSE; }
6441 '(' name_list ')' { $$ = $2; }
6442 | /*EMPTY*/ { $$ = NIL; }
6446 /*****************************************************************************
6449 * EXPLAIN [ANALYZE] [VERBOSE] query
6451 *****************************************************************************/
6453 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
6455 ExplainStmt *n = makeNode(ExplainStmt);
6470 | ExecuteStmt /* by default all are $$=$1 */
6474 analyze_keyword { $$ = TRUE; }
6475 | /* EMPTY */ { $$ = FALSE; }
6478 /*****************************************************************************
6481 * PREPARE <plan_name> [(args, ...)] AS <query>
6483 *****************************************************************************/
6485 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
6487 PrepareStmt *n = makeNode(PrepareStmt);
6495 prep_type_clause: '(' type_list ')' { $$ = $2; }
6496 | /* EMPTY */ { $$ = NIL; }
6503 | DeleteStmt /* by default all are $$=$1 */
6506 /*****************************************************************************
6508 * EXECUTE <plan_name> [(params, ...)]
6509 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
6511 *****************************************************************************/
6513 ExecuteStmt: EXECUTE name execute_param_clause
6515 ExecuteStmt *n = makeNode(ExecuteStmt);
6521 | CREATE OptTemp TABLE create_as_target AS
6522 EXECUTE name execute_param_clause
6524 ExecuteStmt *n = makeNode(ExecuteStmt);
6527 $4->rel->istemp = $2;
6531 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6532 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6533 /* ... because it's not implemented, but it could be */
6538 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6539 | /* EMPTY */ { $$ = NIL; }
6542 /*****************************************************************************
6545 * DEALLOCATE [PREPARE] <plan_name>
6547 *****************************************************************************/
6549 DeallocateStmt: DEALLOCATE name
6551 DeallocateStmt *n = makeNode(DeallocateStmt);
6555 | DEALLOCATE PREPARE name
6557 DeallocateStmt *n = makeNode(DeallocateStmt);
6563 DeallocateStmt *n = makeNode(DeallocateStmt);
6567 | DEALLOCATE PREPARE ALL
6569 DeallocateStmt *n = makeNode(DeallocateStmt);
6575 /*****************************************************************************
6580 *****************************************************************************/
6583 INSERT INTO qualified_name insert_rest returning_clause
6586 $4->returningList = $5;
6594 $$ = makeNode(InsertStmt);
6596 $$->selectStmt = $1;
6598 | '(' insert_column_list ')' SelectStmt
6600 $$ = makeNode(InsertStmt);
6602 $$->selectStmt = $4;
6606 $$ = makeNode(InsertStmt);
6608 $$->selectStmt = NULL;
6614 { $$ = list_make1($1); }
6615 | insert_column_list ',' insert_column_item
6616 { $$ = lappend($1, $3); }
6620 ColId opt_indirection
6622 $$ = makeNode(ResTarget);
6624 $$->indirection = check_indirection($2, yyscanner);
6631 RETURNING target_list { $$ = $2; }
6632 | /* EMPTY */ { $$ = NIL; }
6636 /*****************************************************************************
6641 *****************************************************************************/
6643 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6644 using_clause where_or_current_clause returning_clause
6646 DeleteStmt *n = makeNode(DeleteStmt);
6648 n->usingClause = $4;
6649 n->whereClause = $5;
6650 n->returningList = $6;
6656 USING from_list { $$ = $2; }
6657 | /*EMPTY*/ { $$ = NIL; }
6661 /*****************************************************************************
6666 *****************************************************************************/
6668 LockStmt: LOCK_P opt_table relation_expr_list opt_lock opt_nowait
6670 LockStmt *n = makeNode(LockStmt);
6679 opt_lock: IN_P lock_type MODE { $$ = $2; }
6680 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6683 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6684 | ROW SHARE { $$ = RowShareLock; }
6685 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6686 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6687 | SHARE { $$ = ShareLock; }
6688 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6689 | EXCLUSIVE { $$ = ExclusiveLock; }
6690 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6693 opt_nowait: NOWAIT { $$ = TRUE; }
6694 | /*EMPTY*/ { $$ = FALSE; }
6698 /*****************************************************************************
6701 * UpdateStmt (UPDATE)
6703 *****************************************************************************/
6705 UpdateStmt: UPDATE relation_expr_opt_alias
6708 where_or_current_clause
6711 UpdateStmt *n = makeNode(UpdateStmt);
6715 n->whereClause = $6;
6716 n->returningList = $7;
6722 set_clause { $$ = $1; }
6723 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6727 single_set_clause { $$ = list_make1($1); }
6728 | multiple_set_clause { $$ = $1; }
6732 set_target '=' ctext_expr
6735 $$->val = (Node *) $3;
6739 multiple_set_clause:
6740 '(' set_target_list ')' '=' ctext_row
6746 * Break the ctext_row apart, merge individual expressions
6747 * into the destination ResTargets. XXX this approach
6748 * cannot work for general row expressions as sources.
6750 if (list_length($2) != list_length($5))
6752 (errcode(ERRCODE_SYNTAX_ERROR),
6753 errmsg("number of columns does not match number of values"),
6754 parser_errposition(@1)));
6755 forboth(col_cell, $2, val_cell, $5)
6757 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6758 Node *res_val = (Node *) lfirst(val_cell);
6760 res_col->val = res_val;
6768 ColId opt_indirection
6770 $$ = makeNode(ResTarget);
6772 $$->indirection = check_indirection($2, yyscanner);
6773 $$->val = NULL; /* upper production sets this */
6779 set_target { $$ = list_make1($1); }
6780 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6784 /*****************************************************************************
6789 *****************************************************************************/
6790 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6792 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6794 /* currently we always set FAST_PLAN option */
6795 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6801 cursor_options: /*EMPTY*/ { $$ = 0; }
6802 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6803 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6804 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6805 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6808 opt_hold: /* EMPTY */ { $$ = 0; }
6809 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6810 | WITHOUT HOLD { $$ = 0; }
6813 /*****************************************************************************
6818 *****************************************************************************/
6820 /* A complete SELECT statement looks like this.
6822 * The rule returns either a single SelectStmt node or a tree of them,
6823 * representing a set-operation tree.
6825 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6826 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6827 * to the surrounding a_expr? We don't really care, but bison wants to know.
6828 * To resolve the ambiguity, we are careful to define the grammar so that
6829 * the decision is staved off as long as possible: as long as we can keep
6830 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6831 * it's no longer possible to do that will we decide that parens belong to
6832 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6833 * parentheses are treated as part of the sub-select. The necessity of doing
6834 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6835 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6836 * SELECT viewpoint when we see the UNION.
6838 * This approach is implemented by defining a nonterminal select_with_parens,
6839 * which represents a SELECT with at least one outer layer of parentheses,
6840 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6841 * in the expression grammar. We will then have shift-reduce conflicts
6842 * which we can resolve in favor of always treating '(' <select> ')' as
6843 * a select_with_parens. To resolve the conflicts, the productions that
6844 * conflict with the select_with_parens productions are manually given
6845 * precedences lower than the precedence of ')', thereby ensuring that we
6846 * shift ')' (and then reduce to select_with_parens) rather than trying to
6847 * reduce the inner <select> nonterminal to something else. We use UMINUS
6848 * precedence for this, which is a fairly arbitrary choice.
6850 * To be able to define select_with_parens itself without ambiguity, we need
6851 * a nonterminal select_no_parens that represents a SELECT structure with no
6852 * outermost parentheses. This is a little bit tedious, but it works.
6854 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6855 * with or without outer parentheses.
6858 SelectStmt: select_no_parens %prec UMINUS
6859 | select_with_parens %prec UMINUS
6863 '(' select_no_parens ')' { $$ = $2; }
6864 | '(' select_with_parens ')' { $$ = $2; }
6868 * This rule parses the equivalent of the standard's <query expression>.
6869 * The duplicative productions are annoying, but hard to get rid of without
6870 * creating shift/reduce conflicts.
6872 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6873 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6874 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6878 simple_select { $$ = $1; }
6879 | select_clause sort_clause
6881 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6886 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6888 insertSelectOptions((SelectStmt *) $1, $2, $3,
6889 list_nth($4, 0), list_nth($4, 1),
6894 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6896 insertSelectOptions((SelectStmt *) $1, $2, $4,
6897 list_nth($3, 0), list_nth($3, 1),
6902 | with_clause select_clause
6904 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6910 | with_clause select_clause sort_clause
6912 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6918 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6920 insertSelectOptions((SelectStmt *) $2, $3, $4,
6921 list_nth($5, 0), list_nth($5, 1),
6926 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6928 insertSelectOptions((SelectStmt *) $2, $3, $5,
6929 list_nth($4, 0), list_nth($4, 1),
6937 simple_select { $$ = $1; }
6938 | select_with_parens { $$ = $1; }
6942 * This rule parses SELECT statements that can appear within set operations,
6943 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6944 * the ordering of the set operations. Without '(' and ')' we want the
6945 * operations to be ordered per the precedence specs at the head of this file.
6947 * As with select_no_parens, simple_select cannot have outer parentheses,
6948 * but can have parenthesized subclauses.
6950 * Note that sort clauses cannot be included at this level --- SQL92 requires
6951 * SELECT foo UNION SELECT bar ORDER BY baz
6953 * (SELECT foo UNION SELECT bar) ORDER BY baz
6955 * SELECT foo UNION (SELECT bar ORDER BY baz)
6956 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6957 * described as part of the select_no_parens production, not simple_select.
6958 * This does not limit functionality, because you can reintroduce these
6959 * clauses inside parentheses.
6961 * NOTE: only the leftmost component SelectStmt should have INTO.
6962 * However, this is not checked by the grammar; parse analysis must check it.
6965 SELECT opt_distinct target_list
6966 into_clause from_clause where_clause
6967 group_clause having_clause window_clause
6969 SelectStmt *n = makeNode(SelectStmt);
6970 n->distinctClause = $2;
6974 n->whereClause = $6;
6975 n->groupClause = $7;
6976 n->havingClause = $8;
6977 n->windowClause = $9;
6980 | values_clause { $$ = $1; }
6981 | TABLE relation_expr
6983 /* same as SELECT * FROM relation_expr */
6984 ColumnRef *cr = makeNode(ColumnRef);
6985 ResTarget *rt = makeNode(ResTarget);
6986 SelectStmt *n = makeNode(SelectStmt);
6988 cr->fields = list_make1(makeNode(A_Star));
6992 rt->indirection = NIL;
6993 rt->val = (Node *)cr;
6996 n->targetList = list_make1(rt);
6997 n->fromClause = list_make1($2);
7000 | select_clause UNION opt_all select_clause
7002 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
7004 | select_clause INTERSECT opt_all select_clause
7006 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
7008 | select_clause EXCEPT opt_all select_clause
7010 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
7015 * SQL standard WITH clause looks like:
7017 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
7018 * AS (query) [ SEARCH or CYCLE clause ]
7020 * We don't currently support the SEARCH or CYCLE clause.
7025 $$ = makeNode(WithClause);
7027 $$->recursive = false;
7030 | WITH RECURSIVE cte_list
7032 $$ = makeNode(WithClause);
7034 $$->recursive = true;
7040 common_table_expr { $$ = list_make1($1); }
7041 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
7044 common_table_expr: name opt_name_list AS select_with_parens
7046 CommonTableExpr *n = makeNode(CommonTableExpr);
7048 n->aliascolnames = $2;
7056 INTO OptTempTableName
7058 $$ = makeNode(IntoClause);
7062 $$->onCommit = ONCOMMIT_NOOP;
7063 $$->tableSpaceName = NULL;
7070 * Redundancy here is needed to avoid shift/reduce conflicts,
7071 * since TEMP is not a reserved word. See also OptTemp.
7074 TEMPORARY opt_table qualified_name
7079 | TEMP opt_table qualified_name
7084 | LOCAL TEMPORARY opt_table qualified_name
7089 | LOCAL TEMP opt_table qualified_name
7094 | GLOBAL TEMPORARY opt_table qualified_name
7099 | GLOBAL TEMP opt_table qualified_name
7104 | TABLE qualified_name
7120 opt_all: ALL { $$ = TRUE; }
7121 | DISTINCT { $$ = FALSE; }
7122 | /*EMPTY*/ { $$ = FALSE; }
7125 /* We use (NIL) as a placeholder to indicate that all target expressions
7126 * should be placed in the DISTINCT list during parsetree analysis.
7129 DISTINCT { $$ = list_make1(NIL); }
7130 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
7132 | /*EMPTY*/ { $$ = NIL; }
7136 sort_clause { $$ = $1;}
7137 | /*EMPTY*/ { $$ = NIL; }
7141 ORDER BY sortby_list { $$ = $3; }
7145 sortby { $$ = list_make1($1); }
7146 | sortby_list ',' sortby { $$ = lappend($1, $3); }
7149 sortby: a_expr USING qual_all_Op opt_nulls_order
7151 $$ = makeNode(SortBy);
7153 $$->sortby_dir = SORTBY_USING;
7154 $$->sortby_nulls = $4;
7158 | a_expr opt_asc_desc opt_nulls_order
7160 $$ = makeNode(SortBy);
7162 $$->sortby_dir = $2;
7163 $$->sortby_nulls = $3;
7165 $$->location = -1; /* no operator */
7171 LIMIT select_limit_value OFFSET select_offset_value
7172 { $$ = list_make2($4, $2); }
7173 | OFFSET select_offset_value LIMIT select_limit_value
7174 { $$ = list_make2($2, $4); }
7175 | LIMIT select_limit_value
7176 { $$ = list_make2(NULL, $2); }
7177 | OFFSET select_offset_value
7178 { $$ = list_make2($2, NULL); }
7179 | LIMIT select_limit_value ',' select_offset_value
7181 /* Disabled because it was too confusing, bjm 2002-02-18 */
7183 (errcode(ERRCODE_SYNTAX_ERROR),
7184 errmsg("LIMIT #,# syntax is not supported"),
7185 errhint("Use separate LIMIT and OFFSET clauses."),
7186 parser_errposition(@1)));
7188 /* SQL:2008 syntax variants */
7189 | OFFSET select_offset_value2 row_or_rows
7190 { $$ = list_make2($2, NULL); }
7191 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7192 { $$ = list_make2(NULL, $3); }
7193 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7194 { $$ = list_make2($2, $6); }
7198 select_limit { $$ = $1; }
7200 { $$ = list_make2(NULL,NULL); }
7207 /* LIMIT ALL is represented as a NULL constant */
7208 $$ = makeNullAConst(@1);
7213 * Allowing full expressions without parentheses causes various parsing
7214 * problems with the trailing ROW/ROWS key words. SQL only calls for
7215 * constants, so we allow the rest only with parentheses.
7217 opt_select_fetch_first_value:
7218 SignedIconst { $$ = makeIntConst($1, @1); }
7219 | '(' a_expr ')' { $$ = $2; }
7220 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
7223 select_offset_value:
7228 * Again, the trailing ROW/ROWS in this case prevent the full expression
7229 * syntax. c_expr is the best we can do.
7231 select_offset_value2:
7248 GROUP_P BY expr_list { $$ = $3; }
7249 | /*EMPTY*/ { $$ = NIL; }
7253 HAVING a_expr { $$ = $2; }
7254 | /*EMPTY*/ { $$ = NULL; }
7258 for_locking_items { $$ = $1; }
7259 | FOR READ ONLY { $$ = NIL; }
7262 opt_for_locking_clause:
7263 for_locking_clause { $$ = $1; }
7264 | /* EMPTY */ { $$ = NIL; }
7268 for_locking_item { $$ = list_make1($1); }
7269 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
7273 FOR UPDATE locked_rels_list opt_nowait
7275 LockingClause *n = makeNode(LockingClause);
7277 n->forUpdate = TRUE;
7281 | FOR SHARE locked_rels_list opt_nowait
7283 LockingClause *n = makeNode(LockingClause);
7285 n->forUpdate = FALSE;
7292 OF qualified_name_list { $$ = $2; }
7293 | /* EMPTY */ { $$ = NIL; }
7300 SelectStmt *n = makeNode(SelectStmt);
7301 n->valuesLists = list_make1($2);
7304 | values_clause ',' ctext_row
7306 SelectStmt *n = (SelectStmt *) $1;
7307 n->valuesLists = lappend(n->valuesLists, $3);
7313 /*****************************************************************************
7315 * clauses common to all Optimizable Stmts:
7316 * from_clause - allow list of both JOIN expressions and table names
7317 * where_clause - qualifications for joins or restrictions
7319 *****************************************************************************/
7322 FROM from_list { $$ = $2; }
7323 | /*EMPTY*/ { $$ = NIL; }
7327 table_ref { $$ = list_make1($1); }
7328 | from_list ',' table_ref { $$ = lappend($1, $3); }
7332 * table_ref is where an alias clause can be attached. Note we cannot make
7333 * alias_clause have an empty production because that causes parse conflicts
7334 * between table_ref := '(' joined_table ')' alias_clause
7335 * and joined_table := '(' joined_table ')'. So, we must have the
7336 * redundant-looking productions here instead.
7338 table_ref: relation_expr
7342 | relation_expr alias_clause
7349 RangeFunction *n = makeNode(RangeFunction);
7350 n->funccallnode = $1;
7351 n->coldeflist = NIL;
7354 | func_table alias_clause
7356 RangeFunction *n = makeNode(RangeFunction);
7357 n->funccallnode = $1;
7359 n->coldeflist = NIL;
7362 | func_table AS '(' TableFuncElementList ')'
7364 RangeFunction *n = makeNode(RangeFunction);
7365 n->funccallnode = $1;
7369 | func_table AS ColId '(' TableFuncElementList ')'
7371 RangeFunction *n = makeNode(RangeFunction);
7372 Alias *a = makeNode(Alias);
7373 n->funccallnode = $1;
7379 | func_table ColId '(' TableFuncElementList ')'
7381 RangeFunction *n = makeNode(RangeFunction);
7382 Alias *a = makeNode(Alias);
7383 n->funccallnode = $1;
7389 | select_with_parens
7392 * The SQL spec does not permit a subselect
7393 * (<derived_table>) without an alias clause,
7394 * so we don't either. This avoids the problem
7395 * of needing to invent a unique refname for it.
7396 * That could be surmounted if there's sufficient
7397 * popular demand, but for now let's just implement
7398 * the spec and see if anyone complains.
7399 * However, it does seem like a good idea to emit
7400 * an error message that's better than "syntax error".
7402 if (IsA($1, SelectStmt) &&
7403 ((SelectStmt *) $1)->valuesLists)
7405 (errcode(ERRCODE_SYNTAX_ERROR),
7406 errmsg("VALUES in FROM must have an alias"),
7407 errhint("For example, FROM (VALUES ...) [AS] foo."),
7408 parser_errposition(@1)));
7411 (errcode(ERRCODE_SYNTAX_ERROR),
7412 errmsg("subquery in FROM must have an alias"),
7413 errhint("For example, FROM (SELECT ...) [AS] foo."),
7414 parser_errposition(@1)));
7417 | select_with_parens alias_clause
7419 RangeSubselect *n = makeNode(RangeSubselect);
7428 | '(' joined_table ')' alias_clause
7437 * It may seem silly to separate joined_table from table_ref, but there is
7438 * method in SQL92's madness: if you don't do it this way you get reduce-
7439 * reduce conflicts, because it's not clear to the parser generator whether
7440 * to expect alias_clause after ')' or not. For the same reason we must
7441 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
7442 * join_type to expand to empty; if we try it, the parser generator can't
7443 * figure out when to reduce an empty join_type right after table_ref.
7445 * Note that a CROSS JOIN is the same as an unqualified
7446 * INNER JOIN, and an INNER JOIN/ON has the same shape
7447 * but a qualification expression to limit membership.
7448 * A NATURAL JOIN implicitly matches column names between
7449 * tables and the shape is determined by which columns are
7450 * in common. We'll collect columns during the later transformations.
7454 '(' joined_table ')'
7458 | table_ref CROSS JOIN table_ref
7460 /* CROSS JOIN is same as unqualified inner join */
7461 JoinExpr *n = makeNode(JoinExpr);
7462 n->jointype = JOIN_INNER;
7463 n->isNatural = FALSE;
7466 n->usingClause = NIL;
7470 | table_ref join_type JOIN table_ref join_qual
7472 JoinExpr *n = makeNode(JoinExpr);
7474 n->isNatural = FALSE;
7477 if ($5 != NULL && IsA($5, List))
7478 n->usingClause = (List *) $5; /* USING clause */
7480 n->quals = $5; /* ON clause */
7483 | table_ref JOIN table_ref join_qual
7485 /* letting join_type reduce to empty doesn't work */
7486 JoinExpr *n = makeNode(JoinExpr);
7487 n->jointype = JOIN_INNER;
7488 n->isNatural = FALSE;
7491 if ($4 != NULL && IsA($4, List))
7492 n->usingClause = (List *) $4; /* USING clause */
7494 n->quals = $4; /* ON clause */
7497 | table_ref NATURAL join_type JOIN table_ref
7499 JoinExpr *n = makeNode(JoinExpr);
7501 n->isNatural = TRUE;
7504 n->usingClause = NIL; /* figure out which columns later... */
7505 n->quals = NULL; /* fill later */
7508 | table_ref NATURAL JOIN table_ref
7510 /* letting join_type reduce to empty doesn't work */
7511 JoinExpr *n = makeNode(JoinExpr);
7512 n->jointype = JOIN_INNER;
7513 n->isNatural = TRUE;
7516 n->usingClause = NIL; /* figure out which columns later... */
7517 n->quals = NULL; /* fill later */
7523 AS ColId '(' name_list ')'
7525 $$ = makeNode(Alias);
7531 $$ = makeNode(Alias);
7534 | ColId '(' name_list ')'
7536 $$ = makeNode(Alias);
7542 $$ = makeNode(Alias);
7547 join_type: FULL join_outer { $$ = JOIN_FULL; }
7548 | LEFT join_outer { $$ = JOIN_LEFT; }
7549 | RIGHT join_outer { $$ = JOIN_RIGHT; }
7550 | INNER_P { $$ = JOIN_INNER; }
7553 /* OUTER is just noise... */
7554 join_outer: OUTER_P { $$ = NULL; }
7555 | /*EMPTY*/ { $$ = NULL; }
7558 /* JOIN qualification clauses
7559 * Possibilities are:
7560 * USING ( column list ) allows only unqualified column names,
7561 * which must match between tables.
7562 * ON expr allows more general qualifications.
7564 * We return USING as a List node, while an ON-expr will not be a List.
7567 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
7568 | ON a_expr { $$ = $2; }
7575 /* default inheritance */
7577 $$->inhOpt = INH_DEFAULT;
7580 | qualified_name '*'
7582 /* inheritance query */
7584 $$->inhOpt = INH_YES;
7587 | ONLY qualified_name
7589 /* no inheritance */
7591 $$->inhOpt = INH_NO;
7594 | ONLY '(' qualified_name ')'
7596 /* no inheritance, SQL99-style syntax */
7598 $$->inhOpt = INH_NO;
7605 relation_expr { $$ = list_make1($1); }
7606 | relation_expr_list ',' relation_expr { $$ = lappend($1, $3); }
7611 * Given "UPDATE foo set set ...", we have to decide without looking any
7612 * further ahead whether the first "set" is an alias or the UPDATE's SET
7613 * keyword. Since "set" is allowed as a column name both interpretations
7614 * are feasible. We resolve the shift/reduce conflict by giving the first
7615 * relation_expr_opt_alias production a higher precedence than the SET token
7616 * has, causing the parser to prefer to reduce, in effect assuming that the
7617 * SET is not an alias.
7619 relation_expr_opt_alias: relation_expr %prec UMINUS
7623 | relation_expr ColId
7625 Alias *alias = makeNode(Alias);
7626 alias->aliasname = $2;
7630 | relation_expr AS ColId
7632 Alias *alias = makeNode(Alias);
7633 alias->aliasname = $3;
7640 func_table: func_expr { $$ = $1; }
7645 WHERE a_expr { $$ = $2; }
7646 | /*EMPTY*/ { $$ = NULL; }
7649 /* variant for UPDATE and DELETE */
7650 where_or_current_clause:
7651 WHERE a_expr { $$ = $2; }
7652 | WHERE CURRENT_P OF name
7654 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7655 /* cvarno is filled in by parse analysis */
7656 n->cursor_name = $4;
7657 n->cursor_param = 0;
7660 | WHERE CURRENT_P OF PARAM
7662 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7663 /* cvarno is filled in by parse analysis */
7664 n->cursor_name = NULL;
7665 n->cursor_param = $4;
7668 | /*EMPTY*/ { $$ = NULL; }
7672 TableFuncElementList:
7675 $$ = list_make1($1);
7677 | TableFuncElementList ',' TableFuncElement
7679 $$ = lappend($1, $3);
7683 TableFuncElement: ColId Typename
7685 ColumnDef *n = makeNode(ColumnDef);
7688 n->constraints = NIL;
7694 /*****************************************************************************
7697 * SQL92 introduces a large amount of type-specific syntax.
7698 * Define individual clauses to handle these cases, and use
7699 * the generic case to handle regular type-extensible Postgres syntax.
7700 * - thomas 1997-10-10
7702 *****************************************************************************/
7704 Typename: SimpleTypename opt_array_bounds
7707 $$->arrayBounds = $2;
7709 | SETOF SimpleTypename opt_array_bounds
7712 $$->arrayBounds = $3;
7715 /* SQL standard syntax, currently only one-dimensional */
7716 | SimpleTypename ARRAY '[' Iconst ']'
7719 $$->arrayBounds = list_make1(makeInteger($4));
7721 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7724 $$->arrayBounds = list_make1(makeInteger($5));
7727 | SimpleTypename ARRAY
7730 $$->arrayBounds = list_make1(makeInteger(-1));
7732 | SETOF SimpleTypename ARRAY
7735 $$->arrayBounds = list_make1(makeInteger(-1));
7741 opt_array_bounds '[' ']'
7742 { $$ = lappend($1, makeInteger(-1)); }
7743 | opt_array_bounds '[' Iconst ']'
7744 { $$ = lappend($1, makeInteger($3)); }
7750 GenericType { $$ = $1; }
7751 | Numeric { $$ = $1; }
7753 | Character { $$ = $1; }
7754 | ConstDatetime { $$ = $1; }
7755 | ConstInterval opt_interval
7760 | ConstInterval '(' Iconst ')' opt_interval
7765 if (list_length($5) != 1)
7767 (errcode(ERRCODE_SYNTAX_ERROR),
7768 errmsg("interval precision specified twice"),
7769 parser_errposition(@1)));
7770 $$->typmods = lappend($5, makeIntConst($3, @3));
7773 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7774 makeIntConst($3, @3));
7778 /* We have a separate ConstTypename to allow defaulting fixed-length
7779 * types such as CHAR() and BIT() to an unspecified length.
7780 * SQL9x requires that these default to a length of one, but this
7781 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7782 * where there is an obvious better choice to make.
7783 * Note that ConstInterval is not included here since it must
7784 * be pushed up higher in the rules to accomodate the postfix
7785 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7786 * the generic-type-name case in AExprConst to avoid premature
7787 * reduce/reduce conflicts against function names.
7790 Numeric { $$ = $1; }
7791 | ConstBit { $$ = $1; }
7792 | ConstCharacter { $$ = $1; }
7793 | ConstDatetime { $$ = $1; }
7797 * GenericType covers all type names that don't have special syntax mandated
7798 * by the standard, including qualified names. We also allow type modifiers.
7799 * To avoid parsing conflicts against function invocations, the modifiers
7800 * have to be shown as expr_list here, but parse analysis will only accept
7801 * constants for them.
7804 type_function_name opt_type_modifiers
7806 $$ = makeTypeName($1);
7810 | type_function_name attrs opt_type_modifiers
7812 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7818 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7819 | /* EMPTY */ { $$ = NIL; }
7823 * SQL92 numeric data types
7827 $$ = SystemTypeName("int4");
7832 $$ = SystemTypeName("int4");
7837 $$ = SystemTypeName("int2");
7842 $$ = SystemTypeName("int8");
7847 $$ = SystemTypeName("float4");
7855 | DOUBLE_P PRECISION
7857 $$ = SystemTypeName("float8");
7860 | DECIMAL_P opt_type_modifiers
7862 $$ = SystemTypeName("numeric");
7866 | DEC opt_type_modifiers
7868 $$ = SystemTypeName("numeric");
7872 | NUMERIC opt_type_modifiers
7874 $$ = SystemTypeName("numeric");
7880 $$ = SystemTypeName("bool");
7885 opt_float: '(' Iconst ')'
7888 * Check FLOAT() precision limits assuming IEEE floating
7889 * types - thomas 1997-09-18
7893 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7894 errmsg("precision for type float must be at least 1 bit"),
7895 parser_errposition(@2)));
7897 $$ = SystemTypeName("float4");
7899 $$ = SystemTypeName("float8");
7902 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7903 errmsg("precision for type float must be less than 54 bits"),
7904 parser_errposition(@2)));
7908 $$ = SystemTypeName("float8");
7913 * SQL92 bit-field data types
7914 * The following implements BIT() and BIT VARYING().
7926 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7927 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7928 ConstBit: BitWithLength
7940 BIT opt_varying '(' expr_list ')'
7944 typname = $2 ? "varbit" : "bit";
7945 $$ = SystemTypeName(typname);
7954 /* bit defaults to bit(1), varbit to no limit */
7957 $$ = SystemTypeName("varbit");
7961 $$ = SystemTypeName("bit");
7962 $$->typmods = list_make1(makeIntConst(1, -1));
7970 * SQL92 character data types
7971 * The following implements CHAR() and VARCHAR().
7973 Character: CharacterWithLength
7977 | CharacterWithoutLength
7983 ConstCharacter: CharacterWithLength
7987 | CharacterWithoutLength
7989 /* Length was not specified so allow to be unrestricted.
7990 * This handles problems with fixed-length (bpchar) strings
7991 * which in column definitions must default to a length
7992 * of one, but should not be constrained if the length
7993 * was not specified.
8000 CharacterWithLength: character '(' Iconst ')' opt_charset
8002 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
8006 type = palloc(strlen($1) + 1 + strlen($5) + 1);
8013 $$ = SystemTypeName($1);
8014 $$->typmods = list_make1(makeIntConst($3, @3));
8019 CharacterWithoutLength: character opt_charset
8021 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
8025 type = palloc(strlen($1) + 1 + strlen($2) + 1);
8032 $$ = SystemTypeName($1);
8034 /* char defaults to char(1), varchar to no limit */
8035 if (strcmp($1, "bpchar") == 0)
8036 $$->typmods = list_make1(makeIntConst(1, -1));
8042 character: CHARACTER opt_varying
8043 { $$ = $2 ? "varchar": "bpchar"; }
8044 | CHAR_P opt_varying
8045 { $$ = $2 ? "varchar": "bpchar"; }
8048 | NATIONAL CHARACTER opt_varying
8049 { $$ = $3 ? "varchar": "bpchar"; }
8050 | NATIONAL CHAR_P opt_varying
8051 { $$ = $3 ? "varchar": "bpchar"; }
8053 { $$ = $2 ? "varchar": "bpchar"; }
8057 VARYING { $$ = TRUE; }
8058 | /*EMPTY*/ { $$ = FALSE; }
8062 CHARACTER SET ColId { $$ = $3; }
8063 | /*EMPTY*/ { $$ = NULL; }
8067 * SQL92 date/time types
8070 TIMESTAMP '(' Iconst ')' opt_timezone
8073 $$ = SystemTypeName("timestamptz");
8075 $$ = SystemTypeName("timestamp");
8076 $$->typmods = list_make1(makeIntConst($3, @3));
8079 | TIMESTAMP opt_timezone
8082 $$ = SystemTypeName("timestamptz");
8084 $$ = SystemTypeName("timestamp");
8087 | TIME '(' Iconst ')' opt_timezone
8090 $$ = SystemTypeName("timetz");
8092 $$ = SystemTypeName("time");
8093 $$->typmods = list_make1(makeIntConst($3, @3));
8099 $$ = SystemTypeName("timetz");
8101 $$ = SystemTypeName("time");
8109 $$ = SystemTypeName("interval");
8115 WITH_TIME ZONE { $$ = TRUE; }
8116 | WITHOUT TIME ZONE { $$ = FALSE; }
8117 | /*EMPTY*/ { $$ = FALSE; }
8122 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
8124 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
8126 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
8128 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
8130 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
8135 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
8136 INTERVAL_MASK(MONTH), @1));
8140 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8141 INTERVAL_MASK(HOUR), @1));
8145 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8146 INTERVAL_MASK(HOUR) |
8147 INTERVAL_MASK(MINUTE), @1));
8149 | DAY_P TO interval_second
8152 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
8153 INTERVAL_MASK(HOUR) |
8154 INTERVAL_MASK(MINUTE) |
8155 INTERVAL_MASK(SECOND), @1);
8157 | HOUR_P TO MINUTE_P
8159 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
8160 INTERVAL_MASK(MINUTE), @1));
8162 | HOUR_P TO interval_second
8165 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
8166 INTERVAL_MASK(MINUTE) |
8167 INTERVAL_MASK(SECOND), @1);
8169 | MINUTE_P TO interval_second
8172 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
8173 INTERVAL_MASK(SECOND), @1);
8182 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
8184 | SECOND_P '(' Iconst ')'
8186 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
8187 makeIntConst($3, @3));
8192 /*****************************************************************************
8194 * expression grammar
8196 *****************************************************************************/
8199 * General expressions
8200 * This is the heart of the expression syntax.
8202 * We have two expression types: a_expr is the unrestricted kind, and
8203 * b_expr is a subset that must be used in some places to avoid shift/reduce
8204 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
8205 * because that use of AND conflicts with AND as a boolean operator. So,
8206 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
8208 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
8209 * always be used by surrounding it with parens.
8211 * c_expr is all the productions that are common to a_expr and b_expr;
8212 * it's factored out just to eliminate redundant coding.
8214 a_expr: c_expr { $$ = $1; }
8215 | a_expr TYPECAST Typename
8216 { $$ = makeTypeCast($1, $3, @2); }
8217 | a_expr AT TIME ZONE a_expr
8219 FuncCall *n = makeNode(FuncCall);
8220 n->funcname = SystemFuncName("timezone");
8221 n->args = list_make2($5, $1);
8222 n->agg_star = FALSE;
8223 n->agg_distinct = FALSE;
8224 n->func_variadic = FALSE;
8230 * These operators must be called out explicitly in order to make use
8231 * of bison's automatic operator-precedence handling. All other
8232 * operator names are handled by the generic productions using "Op",
8233 * below; and all those operators will have the same precedence.
8235 * If you add more explicitly-known operators, be sure to add them
8236 * also to b_expr and to the MathOp list above.
8238 | '+' a_expr %prec UMINUS
8239 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8240 | '-' a_expr %prec UMINUS
8241 { $$ = doNegate($2, @1); }
8243 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8245 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8247 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8249 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8251 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8253 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8255 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8257 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8259 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8261 | a_expr qual_Op a_expr %prec Op
8262 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8263 | qual_Op a_expr %prec Op
8264 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8265 | a_expr qual_Op %prec POSTFIXOP
8266 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8269 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
8271 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
8273 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
8275 | a_expr LIKE a_expr
8276 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
8277 | a_expr LIKE a_expr ESCAPE a_expr
8279 FuncCall *n = makeNode(FuncCall);
8280 n->funcname = SystemFuncName("like_escape");
8281 n->args = list_make2($3, $5);
8282 n->agg_star = FALSE;
8283 n->agg_distinct = FALSE;
8284 n->func_variadic = FALSE;
8287 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
8289 | a_expr NOT LIKE a_expr
8290 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
8291 | a_expr NOT LIKE a_expr ESCAPE a_expr
8293 FuncCall *n = makeNode(FuncCall);
8294 n->funcname = SystemFuncName("like_escape");
8295 n->args = list_make2($4, $6);
8296 n->agg_star = FALSE;
8297 n->agg_distinct = FALSE;
8298 n->func_variadic = FALSE;
8301 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
8303 | a_expr ILIKE a_expr
8304 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
8305 | a_expr ILIKE a_expr ESCAPE a_expr
8307 FuncCall *n = makeNode(FuncCall);
8308 n->funcname = SystemFuncName("like_escape");
8309 n->args = list_make2($3, $5);
8310 n->agg_star = FALSE;
8311 n->agg_distinct = FALSE;
8312 n->func_variadic = FALSE;
8315 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
8317 | a_expr NOT ILIKE a_expr
8318 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
8319 | a_expr NOT ILIKE a_expr ESCAPE a_expr
8321 FuncCall *n = makeNode(FuncCall);
8322 n->funcname = SystemFuncName("like_escape");
8323 n->args = list_make2($4, $6);
8324 n->agg_star = FALSE;
8325 n->agg_distinct = FALSE;
8326 n->func_variadic = FALSE;
8329 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
8332 | a_expr SIMILAR TO a_expr %prec SIMILAR
8334 FuncCall *n = makeNode(FuncCall);
8335 n->funcname = SystemFuncName("similar_escape");
8336 n->args = list_make2($4, makeNullAConst(-1));
8337 n->agg_star = FALSE;
8338 n->agg_distinct = FALSE;
8339 n->func_variadic = FALSE;
8342 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8344 | a_expr SIMILAR TO a_expr ESCAPE a_expr
8346 FuncCall *n = makeNode(FuncCall);
8347 n->funcname = SystemFuncName("similar_escape");
8348 n->args = list_make2($4, $6);
8349 n->agg_star = FALSE;
8350 n->agg_distinct = FALSE;
8351 n->func_variadic = FALSE;
8354 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8356 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
8358 FuncCall *n = makeNode(FuncCall);
8359 n->funcname = SystemFuncName("similar_escape");
8360 n->args = list_make2($5, makeNullAConst(-1));
8361 n->agg_star = FALSE;
8362 n->agg_distinct = FALSE;
8363 n->func_variadic = FALSE;
8366 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8368 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
8370 FuncCall *n = makeNode(FuncCall);
8371 n->funcname = SystemFuncName("similar_escape");
8372 n->args = list_make2($5, $7);
8373 n->agg_star = FALSE;
8374 n->agg_distinct = FALSE;
8375 n->func_variadic = FALSE;
8378 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8382 * Define SQL92-style Null test clause.
8383 * Allow two forms described in the standard:
8386 * Allow two SQL extensions
8392 NullTest *n = makeNode(NullTest);
8393 n->arg = (Expr *) $1;
8394 n->nulltesttype = IS_NULL;
8399 NullTest *n = makeNode(NullTest);
8400 n->arg = (Expr *) $1;
8401 n->nulltesttype = IS_NULL;
8404 | a_expr IS NOT NULL_P
8406 NullTest *n = makeNode(NullTest);
8407 n->arg = (Expr *) $1;
8408 n->nulltesttype = IS_NOT_NULL;
8413 NullTest *n = makeNode(NullTest);
8414 n->arg = (Expr *) $1;
8415 n->nulltesttype = IS_NOT_NULL;
8420 $$ = (Node *)makeOverlaps($1, $3, @2, yyscanner);
8424 BooleanTest *b = makeNode(BooleanTest);
8425 b->arg = (Expr *) $1;
8426 b->booltesttype = IS_TRUE;
8429 | a_expr IS NOT TRUE_P
8431 BooleanTest *b = makeNode(BooleanTest);
8432 b->arg = (Expr *) $1;
8433 b->booltesttype = IS_NOT_TRUE;
8438 BooleanTest *b = makeNode(BooleanTest);
8439 b->arg = (Expr *) $1;
8440 b->booltesttype = IS_FALSE;
8443 | a_expr IS NOT FALSE_P
8445 BooleanTest *b = makeNode(BooleanTest);
8446 b->arg = (Expr *) $1;
8447 b->booltesttype = IS_NOT_FALSE;
8452 BooleanTest *b = makeNode(BooleanTest);
8453 b->arg = (Expr *) $1;
8454 b->booltesttype = IS_UNKNOWN;
8457 | a_expr IS NOT UNKNOWN
8459 BooleanTest *b = makeNode(BooleanTest);
8460 b->arg = (Expr *) $1;
8461 b->booltesttype = IS_NOT_UNKNOWN;
8464 | a_expr IS DISTINCT FROM a_expr %prec IS
8466 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8468 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
8470 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8471 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
8476 | a_expr IS OF '(' type_list ')' %prec IS
8478 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8480 | a_expr IS NOT OF '(' type_list ')' %prec IS
8482 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8485 * Ideally we would not use hard-wired operators below but instead use
8486 * opclasses. However, mixed data types and other issues make this
8487 * difficult: http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
8489 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8491 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8492 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8493 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8496 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8498 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8499 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8500 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8503 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8505 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8506 (Node *) makeA_Expr(AEXPR_AND, NIL,
8507 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8508 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8510 (Node *) makeA_Expr(AEXPR_AND, NIL,
8511 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
8512 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
8516 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8518 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8519 (Node *) makeA_Expr(AEXPR_OR, NIL,
8520 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8521 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8523 (Node *) makeA_Expr(AEXPR_OR, NIL,
8524 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
8525 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
8529 | a_expr IN_P in_expr
8531 /* in_expr returns a SubLink or a list of a_exprs */
8532 if (IsA($3, SubLink))
8534 /* generate foo = ANY (subquery) */
8535 SubLink *n = (SubLink *) $3;
8536 n->subLinkType = ANY_SUBLINK;
8538 n->operName = list_make1(makeString("="));
8544 /* generate scalar IN expression */
8545 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
8548 | a_expr NOT IN_P in_expr
8550 /* in_expr returns a SubLink or a list of a_exprs */
8551 if (IsA($4, SubLink))
8553 /* generate NOT (foo = ANY (subquery)) */
8554 /* Make an = ANY node */
8555 SubLink *n = (SubLink *) $4;
8556 n->subLinkType = ANY_SUBLINK;
8558 n->operName = list_make1(makeString("="));
8560 /* Stick a NOT on top */
8561 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
8565 /* generate scalar NOT IN expression */
8566 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
8569 | a_expr subquery_Op sub_type select_with_parens %prec Op
8571 SubLink *n = makeNode(SubLink);
8572 n->subLinkType = $3;
8579 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
8581 if ($3 == ANY_SUBLINK)
8582 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
8584 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
8586 | UNIQUE select_with_parens
8588 /* Not sure how to get rid of the parentheses
8589 * but there are lots of shift/reduce errors without them.
8591 * Should be able to implement this by plopping the entire
8592 * select into a node, then transforming the target expressions
8593 * from whatever they are into count(*), and testing the
8594 * entire result equal to one.
8595 * But, will probably implement a separate node in the executor.
8598 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8599 errmsg("UNIQUE predicate is not yet implemented"),
8600 parser_errposition(@1)));
8602 | a_expr IS DOCUMENT_P %prec IS
8604 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8605 list_make1($1), @2);
8607 | a_expr IS NOT DOCUMENT_P %prec IS
8609 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8610 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8611 list_make1($1), @2),
8617 * Restricted expressions
8619 * b_expr is a subset of the complete expression syntax defined by a_expr.
8621 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8622 * cause trouble in the places where b_expr is used. For simplicity, we
8623 * just eliminate all the boolean-keyword-operator productions from b_expr.
8627 | b_expr TYPECAST Typename
8628 { $$ = makeTypeCast($1, $3, @2); }
8629 | '+' b_expr %prec UMINUS
8630 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8631 | '-' b_expr %prec UMINUS
8632 { $$ = doNegate($2, @1); }
8634 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8636 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8638 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8640 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8642 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8644 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8646 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8648 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8650 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8651 | b_expr qual_Op b_expr %prec Op
8652 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8653 | qual_Op b_expr %prec Op
8654 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8655 | b_expr qual_Op %prec POSTFIXOP
8656 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8657 | b_expr IS DISTINCT FROM b_expr %prec IS
8659 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8661 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8663 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8664 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8666 | b_expr IS OF '(' type_list ')' %prec IS
8668 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8670 | b_expr IS NOT OF '(' type_list ')' %prec IS
8672 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8674 | b_expr IS DOCUMENT_P %prec IS
8676 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8677 list_make1($1), @2);
8679 | b_expr IS NOT DOCUMENT_P %prec IS
8681 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8682 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8683 list_make1($1), @2),
8689 * Productions that can be used in both a_expr and b_expr.
8691 * Note: productions that refer recursively to a_expr or b_expr mostly
8692 * cannot appear here. However, it's OK to refer to a_exprs that occur
8693 * inside parentheses, such as function arguments; that cannot introduce
8694 * ambiguity to the b_expr syntax.
8696 c_expr: columnref { $$ = $1; }
8697 | AexprConst { $$ = $1; }
8698 | PARAM opt_indirection
8700 ParamRef *p = makeNode(ParamRef);
8705 A_Indirection *n = makeNode(A_Indirection);
8706 n->arg = (Node *) p;
8707 n->indirection = check_indirection($2, yyscanner);
8713 | '(' a_expr ')' opt_indirection
8717 A_Indirection *n = makeNode(A_Indirection);
8719 n->indirection = check_indirection($4, yyscanner);
8729 | select_with_parens %prec UMINUS
8731 SubLink *n = makeNode(SubLink);
8732 n->subLinkType = EXPR_SUBLINK;
8739 | EXISTS select_with_parens
8741 SubLink *n = makeNode(SubLink);
8742 n->subLinkType = EXISTS_SUBLINK;
8749 | ARRAY select_with_parens
8751 SubLink *n = makeNode(SubLink);
8752 n->subLinkType = ARRAY_SUBLINK;
8761 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8762 Assert(IsA(n, A_ArrayExpr));
8763 /* point outermost A_ArrayExpr to the ARRAY keyword */
8769 RowExpr *r = makeNode(RowExpr);
8771 r->row_typeid = InvalidOid; /* not analyzed yet */
8778 * func_expr is split out from c_expr just so that we have a classification
8779 * for "everything that is a function call or looks like one". This isn't
8780 * very important, but it saves us having to document which variants are
8781 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8782 * (Note that many of the special SQL functions wouldn't actually make any
8783 * sense as functional index entries, but we ignore that consideration here.)
8785 func_expr: func_name '(' ')' over_clause
8787 FuncCall *n = makeNode(FuncCall);
8790 n->agg_star = FALSE;
8791 n->agg_distinct = FALSE;
8792 n->func_variadic = FALSE;
8797 | func_name '(' expr_list ')' over_clause
8799 FuncCall *n = makeNode(FuncCall);
8802 n->agg_star = FALSE;
8803 n->agg_distinct = FALSE;
8804 n->func_variadic = FALSE;
8809 | func_name '(' VARIADIC a_expr ')' over_clause
8811 FuncCall *n = makeNode(FuncCall);
8813 n->args = list_make1($4);
8814 n->agg_star = FALSE;
8815 n->agg_distinct = FALSE;
8816 n->func_variadic = TRUE;
8821 | func_name '(' expr_list ',' VARIADIC a_expr ')' over_clause
8823 FuncCall *n = makeNode(FuncCall);
8825 n->args = lappend($3, $6);
8826 n->agg_star = FALSE;
8827 n->agg_distinct = FALSE;
8828 n->func_variadic = TRUE;
8833 | func_name '(' ALL expr_list ')' over_clause
8835 FuncCall *n = makeNode(FuncCall);
8838 n->agg_star = FALSE;
8839 n->agg_distinct = FALSE;
8840 /* Ideally we'd mark the FuncCall node to indicate
8841 * "must be an aggregate", but there's no provision
8842 * for that in FuncCall at the moment.
8844 n->func_variadic = FALSE;
8849 | func_name '(' DISTINCT expr_list ')' over_clause
8851 FuncCall *n = makeNode(FuncCall);
8854 n->agg_star = FALSE;
8855 n->agg_distinct = TRUE;
8856 n->func_variadic = FALSE;
8861 | func_name '(' '*' ')' over_clause
8864 * We consider AGGREGATE(*) to invoke a parameterless
8865 * aggregate. This does the right thing for COUNT(*),
8866 * and there are no other aggregates in SQL92 that accept
8869 * The FuncCall node is also marked agg_star = true,
8870 * so that later processing can detect what the argument
8873 FuncCall *n = makeNode(FuncCall);
8877 n->agg_distinct = FALSE;
8878 n->func_variadic = FALSE;
8886 * Translate as "'now'::text::date".
8888 * We cannot use "'now'::date" because coerce_type() will
8889 * immediately reduce that to a constant representing
8890 * today's date. We need to delay the conversion until
8891 * runtime, else the wrong things will happen when
8892 * CURRENT_DATE is used in a column default value or rule.
8894 * This could be simplified if we had a way to generate
8895 * an expression tree representing runtime application
8896 * of type-input conversion functions. (As of PG 7.3
8897 * that is actually possible, but not clear that we want
8901 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8902 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8907 * Translate as "'now'::text::timetz".
8908 * See comments for CURRENT_DATE.
8911 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8912 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8914 | CURRENT_TIME '(' Iconst ')'
8917 * Translate as "'now'::text::timetz(n)".
8918 * See comments for CURRENT_DATE.
8922 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8923 d = SystemTypeName("timetz");
8924 d->typmods = list_make1(makeIntConst($3, @3));
8925 $$ = makeTypeCast(n, d, -1);
8930 * Translate as "now()", since we have a function that
8931 * does exactly what is needed.
8933 FuncCall *n = makeNode(FuncCall);
8934 n->funcname = SystemFuncName("now");
8936 n->agg_star = FALSE;
8937 n->agg_distinct = FALSE;
8938 n->func_variadic = FALSE;
8943 | CURRENT_TIMESTAMP '(' Iconst ')'
8946 * Translate as "'now'::text::timestamptz(n)".
8947 * See comments for CURRENT_DATE.
8951 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8952 d = SystemTypeName("timestamptz");
8953 d->typmods = list_make1(makeIntConst($3, @3));
8954 $$ = makeTypeCast(n, d, -1);
8959 * Translate as "'now'::text::time".
8960 * See comments for CURRENT_DATE.
8963 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8964 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8966 | LOCALTIME '(' Iconst ')'
8969 * Translate as "'now'::text::time(n)".
8970 * See comments for CURRENT_DATE.
8974 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8975 d = SystemTypeName("time");
8976 d->typmods = list_make1(makeIntConst($3, @3));
8977 $$ = makeTypeCast((Node *)n, d, -1);
8982 * Translate as "'now'::text::timestamp".
8983 * See comments for CURRENT_DATE.
8986 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8987 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8989 | LOCALTIMESTAMP '(' Iconst ')'
8992 * Translate as "'now'::text::timestamp(n)".
8993 * See comments for CURRENT_DATE.
8997 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8998 d = SystemTypeName("timestamp");
8999 d->typmods = list_make1(makeIntConst($3, @3));
9000 $$ = makeTypeCast(n, d, -1);
9004 FuncCall *n = makeNode(FuncCall);
9005 n->funcname = SystemFuncName("current_user");
9007 n->agg_star = FALSE;
9008 n->agg_distinct = FALSE;
9009 n->func_variadic = FALSE;
9016 FuncCall *n = makeNode(FuncCall);
9017 n->funcname = SystemFuncName("current_user");
9019 n->agg_star = FALSE;
9020 n->agg_distinct = FALSE;
9021 n->func_variadic = FALSE;
9028 FuncCall *n = makeNode(FuncCall);
9029 n->funcname = SystemFuncName("session_user");
9031 n->agg_star = FALSE;
9032 n->agg_distinct = FALSE;
9033 n->func_variadic = FALSE;
9040 FuncCall *n = makeNode(FuncCall);
9041 n->funcname = SystemFuncName("current_user");
9043 n->agg_star = FALSE;
9044 n->agg_distinct = FALSE;
9045 n->func_variadic = FALSE;
9052 FuncCall *n = makeNode(FuncCall);
9053 n->funcname = SystemFuncName("current_database");
9055 n->agg_star = FALSE;
9056 n->agg_distinct = FALSE;
9057 n->func_variadic = FALSE;
9064 FuncCall *n = makeNode(FuncCall);
9065 n->funcname = SystemFuncName("current_schema");
9067 n->agg_star = FALSE;
9068 n->agg_distinct = FALSE;
9069 n->func_variadic = FALSE;
9074 | CAST '(' a_expr AS Typename ')'
9075 { $$ = makeTypeCast($3, $5, @1); }
9076 | EXTRACT '(' extract_list ')'
9078 FuncCall *n = makeNode(FuncCall);
9079 n->funcname = SystemFuncName("date_part");
9081 n->agg_star = FALSE;
9082 n->agg_distinct = FALSE;
9083 n->func_variadic = FALSE;
9088 | OVERLAY '(' overlay_list ')'
9090 /* overlay(A PLACING B FROM C FOR D) is converted to
9091 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
9092 * overlay(A PLACING B FROM C) is converted to
9093 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
9095 FuncCall *n = makeNode(FuncCall);
9096 n->funcname = SystemFuncName("overlay");
9098 n->agg_star = FALSE;
9099 n->agg_distinct = FALSE;
9100 n->func_variadic = FALSE;
9105 | POSITION '(' position_list ')'
9107 /* position(A in B) is converted to position(B, A) */
9108 FuncCall *n = makeNode(FuncCall);
9109 n->funcname = SystemFuncName("position");
9111 n->agg_star = FALSE;
9112 n->agg_distinct = FALSE;
9113 n->func_variadic = FALSE;
9118 | SUBSTRING '(' substr_list ')'
9120 /* substring(A from B for C) is converted to
9121 * substring(A, B, C) - thomas 2000-11-28
9123 FuncCall *n = makeNode(FuncCall);
9124 n->funcname = SystemFuncName("substring");
9126 n->agg_star = FALSE;
9127 n->agg_distinct = FALSE;
9128 n->func_variadic = FALSE;
9133 | TREAT '(' a_expr AS Typename ')'
9135 /* TREAT(expr AS target) converts expr of a particular type to target,
9136 * which is defined to be a subtype of the original expression.
9137 * In SQL99, this is intended for use with structured UDTs,
9138 * but let's make this a generally useful form allowing stronger
9139 * coercions than are handled by implicit casting.
9141 FuncCall *n = makeNode(FuncCall);
9142 /* Convert SystemTypeName() to SystemFuncName() even though
9143 * at the moment they result in the same thing.
9145 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
9146 n->args = list_make1($3);
9147 n->agg_star = FALSE;
9148 n->agg_distinct = FALSE;
9149 n->func_variadic = FALSE;
9154 | TRIM '(' BOTH trim_list ')'
9156 /* various trim expressions are defined in SQL92
9157 * - thomas 1997-07-19
9159 FuncCall *n = makeNode(FuncCall);
9160 n->funcname = SystemFuncName("btrim");
9162 n->agg_star = FALSE;
9163 n->agg_distinct = FALSE;
9164 n->func_variadic = FALSE;
9169 | TRIM '(' LEADING trim_list ')'
9171 FuncCall *n = makeNode(FuncCall);
9172 n->funcname = SystemFuncName("ltrim");
9174 n->agg_star = FALSE;
9175 n->agg_distinct = FALSE;
9176 n->func_variadic = FALSE;
9181 | TRIM '(' TRAILING trim_list ')'
9183 FuncCall *n = makeNode(FuncCall);
9184 n->funcname = SystemFuncName("rtrim");
9186 n->agg_star = FALSE;
9187 n->agg_distinct = FALSE;
9188 n->func_variadic = FALSE;
9193 | TRIM '(' trim_list ')'
9195 FuncCall *n = makeNode(FuncCall);
9196 n->funcname = SystemFuncName("btrim");
9198 n->agg_star = FALSE;
9199 n->agg_distinct = FALSE;
9200 n->func_variadic = FALSE;
9205 | NULLIF '(' a_expr ',' a_expr ')'
9207 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
9209 | COALESCE '(' expr_list ')'
9211 CoalesceExpr *c = makeNode(CoalesceExpr);
9216 | GREATEST '(' expr_list ')'
9218 MinMaxExpr *v = makeNode(MinMaxExpr);
9220 v->op = IS_GREATEST;
9224 | LEAST '(' expr_list ')'
9226 MinMaxExpr *v = makeNode(MinMaxExpr);
9232 | XMLCONCAT '(' expr_list ')'
9234 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
9236 | XMLELEMENT '(' NAME_P ColLabel ')'
9238 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
9240 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
9242 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
9244 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
9246 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
9248 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
9250 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
9252 | XMLFOREST '(' xml_attribute_list ')'
9254 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
9256 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
9258 XmlExpr *x = (XmlExpr *)
9259 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
9260 list_make2($4, makeBoolAConst($5, -1)),
9265 | XMLPI '(' NAME_P ColLabel ')'
9267 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
9269 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
9271 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
9273 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
9275 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
9276 list_make3($3, $5, $6), @1);
9278 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
9280 XmlSerialize *n = makeNode(XmlSerialize);
9292 xml_root_version: VERSION_P a_expr
9294 | VERSION_P NO VALUE_P
9295 { $$ = makeNullAConst(-1); }
9298 opt_xml_root_standalone: ',' STANDALONE_P YES_P
9299 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
9300 | ',' STANDALONE_P NO
9301 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
9302 | ',' STANDALONE_P NO VALUE_P
9303 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
9305 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
9308 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
9311 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
9312 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
9315 xml_attribute_el: a_expr AS ColLabel
9317 $$ = makeNode(ResTarget);
9319 $$->indirection = NIL;
9320 $$->val = (Node *) $1;
9325 $$ = makeNode(ResTarget);
9327 $$->indirection = NIL;
9328 $$->val = (Node *) $1;
9333 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
9334 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
9337 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
9338 | STRIP_P WHITESPACE_P { $$ = FALSE; }
9339 | /*EMPTY*/ { $$ = FALSE; }
9343 * Window Definitions
9346 WINDOW window_definition_list { $$ = $2; }
9347 | /*EMPTY*/ { $$ = NIL; }
9350 window_definition_list:
9351 window_definition { $$ = list_make1($1); }
9352 | window_definition_list ',' window_definition
9353 { $$ = lappend($1, $3); }
9357 ColId AS window_specification
9365 over_clause: OVER window_specification
9369 WindowDef *n = makeNode(WindowDef);
9372 n->partitionClause = NIL;
9373 n->orderClause = NIL;
9374 n->frameOptions = FRAMEOPTION_DEFAULTS;
9382 window_specification: '(' opt_existing_window_name opt_partition_clause
9383 opt_sort_clause opt_frame_clause ')'
9385 WindowDef *n = makeNode(WindowDef);
9388 n->partitionClause = $3;
9389 n->orderClause = $4;
9390 n->frameOptions = $5;
9397 * If we see PARTITION, RANGE, or ROWS as the first token after the '('
9398 * of a window_specification, we want the assumption to be that there is
9399 * no existing_window_name; but those keywords are unreserved and so could
9400 * be ColIds. We fix this by making them have the same precedence as IDENT
9401 * and giving the empty production here a slightly higher precedence, so
9402 * that the shift/reduce conflict is resolved in favor of reducing the rule.
9403 * These keywords are thus precluded from being an existing_window_name but
9404 * are not reserved for any other purpose.
9406 opt_existing_window_name: ColId { $$ = $1; }
9407 | /*EMPTY*/ %prec Op { $$ = NULL; }
9410 opt_partition_clause: PARTITION BY expr_list { $$ = $3; }
9411 | /*EMPTY*/ { $$ = NIL; }
9415 * This is only a subset of the full SQL:2008 frame_clause grammar.
9416 * We don't support <expression> PRECEDING, <expression> FOLLOWING,
9417 * nor <window frame exclusion> yet.
9422 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE | $2;
9426 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS | $2;
9429 { $$ = FRAMEOPTION_DEFAULTS; }
9432 frame_extent: frame_bound
9434 /* reject invalid cases */
9435 if ($1 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9437 (errcode(ERRCODE_WINDOWING_ERROR),
9438 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9439 parser_errposition(@1)));
9440 if ($1 & FRAMEOPTION_START_CURRENT_ROW)
9442 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9443 errmsg("frame start at CURRENT ROW is not implemented"),
9444 parser_errposition(@1)));
9445 $$ = $1 | FRAMEOPTION_END_CURRENT_ROW;
9447 | BETWEEN frame_bound AND frame_bound
9449 /* reject invalid cases */
9450 if ($2 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9452 (errcode(ERRCODE_WINDOWING_ERROR),
9453 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9454 parser_errposition(@2)));
9455 if ($2 & FRAMEOPTION_START_CURRENT_ROW)
9457 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9458 errmsg("frame start at CURRENT ROW is not implemented"),
9459 parser_errposition(@2)));
9460 if ($4 & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
9462 (errcode(ERRCODE_WINDOWING_ERROR),
9463 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
9464 parser_errposition(@4)));
9465 /* shift converts START_ options to END_ options */
9466 $$ = FRAMEOPTION_BETWEEN | $2 | ($4 << 1);
9471 * This is used for both frame start and frame end, with output set up on
9472 * the assumption it's frame start; the frame_extent productions must reject
9478 $$ = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
9480 | UNBOUNDED FOLLOWING
9482 $$ = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
9486 $$ = FRAMEOPTION_START_CURRENT_ROW;
9492 * Supporting nonterminals for expressions.
9495 /* Explicit row production.
9497 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
9498 * without conflicting with the parenthesized a_expr production. Without the
9499 * ROW keyword, there must be more than one a_expr inside the parens.
9501 row: ROW '(' expr_list ')' { $$ = $3; }
9502 | ROW '(' ')' { $$ = NIL; }
9503 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
9506 sub_type: ANY { $$ = ANY_SUBLINK; }
9507 | SOME { $$ = ANY_SUBLINK; }
9508 | ALL { $$ = ALL_SUBLINK; }
9511 all_Op: Op { $$ = $1; }
9512 | MathOp { $$ = $1; }
9515 MathOp: '+' { $$ = "+"; }
9527 { $$ = list_make1(makeString($1)); }
9528 | OPERATOR '(' any_operator ')'
9534 { $$ = list_make1(makeString($1)); }
9535 | OPERATOR '(' any_operator ')'
9541 { $$ = list_make1(makeString($1)); }
9542 | OPERATOR '(' any_operator ')'
9545 { $$ = list_make1(makeString("~~")); }
9547 { $$ = list_make1(makeString("!~~")); }
9549 { $$ = list_make1(makeString("~~*")); }
9551 { $$ = list_make1(makeString("!~~*")); }
9552 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
9553 * the regular expression is preprocessed by a function (similar_escape),
9554 * and the ~ operator for posix regular expressions is used.
9555 * x SIMILAR TO y -> x ~ similar_escape(y)
9556 * this transformation is made on the fly by the parser upwards.
9557 * however the SubLink structure which handles any/some/all stuff
9558 * is not ready for such a thing.
9564 $$ = list_make1($1);
9566 | expr_list ',' a_expr
9568 $$ = lappend($1, $3);
9572 type_list: Typename { $$ = list_make1($1); }
9573 | type_list ',' Typename { $$ = lappend($1, $3); }
9576 array_expr: '[' expr_list ']'
9578 $$ = makeAArrayExpr($2, @1);
9580 | '[' array_expr_list ']'
9582 $$ = makeAArrayExpr($2, @1);
9586 $$ = makeAArrayExpr(NIL, @1);
9590 array_expr_list: array_expr { $$ = list_make1($1); }
9591 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
9596 extract_arg FROM a_expr
9598 $$ = list_make2(makeStringConst($1, @1), $3);
9600 | /*EMPTY*/ { $$ = NIL; }
9603 /* Allow delimited string Sconst in extract_arg as an SQL extension.
9604 * - thomas 2001-04-12
9608 | YEAR_P { $$ = "year"; }
9609 | MONTH_P { $$ = "month"; }
9610 | DAY_P { $$ = "day"; }
9611 | HOUR_P { $$ = "hour"; }
9612 | MINUTE_P { $$ = "minute"; }
9613 | SECOND_P { $$ = "second"; }
9614 | Sconst { $$ = $1; }
9617 /* OVERLAY() arguments
9618 * SQL99 defines the OVERLAY() function:
9619 * o overlay(text placing text from int for int)
9620 * o overlay(text placing text from int)
9623 a_expr overlay_placing substr_from substr_for
9625 $$ = list_make4($1, $2, $3, $4);
9627 | a_expr overlay_placing substr_from
9629 $$ = list_make3($1, $2, $3);
9638 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
9641 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
9642 | /*EMPTY*/ { $$ = NIL; }
9645 /* SUBSTRING() arguments
9646 * SQL9x defines a specific syntax for arguments to SUBSTRING():
9647 * o substring(text from int for int)
9648 * o substring(text from int) get entire string from starting point "int"
9649 * o substring(text for int) get first "int" characters of string
9650 * o substring(text from pattern) get entire string matching pattern
9651 * o substring(text from pattern for escape) same with specified escape char
9652 * We also want to support generic substring functions which accept
9653 * the usual generic list of arguments. So we will accept both styles
9654 * here, and convert the SQL9x style to the generic list for further
9655 * processing. - thomas 2000-11-28
9658 a_expr substr_from substr_for
9660 $$ = list_make3($1, $2, $3);
9662 | a_expr substr_for substr_from
9664 /* not legal per SQL99, but might as well allow it */
9665 $$ = list_make3($1, $3, $2);
9667 | a_expr substr_from
9669 $$ = list_make2($1, $2);
9674 * Since there are no cases where this syntax allows
9675 * a textual FOR value, we forcibly cast the argument
9676 * to int4. The possible matches in pg_proc are
9677 * substring(text,int4) and substring(text,text),
9678 * and we don't want the parser to choose the latter,
9679 * which it is likely to do if the second argument
9680 * is unknown or doesn't have an implicit cast to int4.
9682 $$ = list_make3($1, makeIntConst(1, -1),
9684 SystemTypeName("int4"), -1));
9695 FROM a_expr { $$ = $2; }
9698 substr_for: FOR a_expr { $$ = $2; }
9701 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
9702 | FROM expr_list { $$ = $2; }
9703 | expr_list { $$ = $1; }
9706 in_expr: select_with_parens
9708 SubLink *n = makeNode(SubLink);
9710 /* other fields will be filled later */
9713 | '(' expr_list ')' { $$ = (Node *)$2; }
9717 * Define SQL92-style case clause.
9718 * - Full specification
9719 * CASE WHEN a = b THEN c ... ELSE d END
9720 * - Implicit argument
9721 * CASE a WHEN b THEN c ... ELSE d END
9723 case_expr: CASE case_arg when_clause_list case_default END_P
9725 CaseExpr *c = makeNode(CaseExpr);
9726 c->casetype = InvalidOid; /* not analyzed yet */
9727 c->arg = (Expr *) $2;
9729 c->defresult = (Expr *) $4;
9736 /* There must be at least one */
9737 when_clause { $$ = list_make1($1); }
9738 | when_clause_list when_clause { $$ = lappend($1, $2); }
9742 WHEN a_expr THEN a_expr
9744 CaseWhen *w = makeNode(CaseWhen);
9745 w->expr = (Expr *) $2;
9746 w->result = (Expr *) $4;
9753 ELSE a_expr { $$ = $2; }
9754 | /*EMPTY*/ { $$ = NULL; }
9757 case_arg: a_expr { $$ = $1; }
9758 | /*EMPTY*/ { $$ = NULL; }
9762 * columnref starts with relation_name not ColId, so that OLD and NEW
9763 * references can be accepted. Note that when there are more than two
9764 * dotted names, the first name is not actually a relation name...
9766 columnref: relation_name
9768 $$ = makeColumnRef($1, NIL, @1, yyscanner);
9770 | relation_name indirection
9772 $$ = makeColumnRef($1, $2, @1, yyscanner);
9779 $$ = (Node *) makeString($2);
9783 $$ = (Node *) makeNode(A_Star);
9787 A_Indices *ai = makeNode(A_Indices);
9792 | '[' a_expr ':' a_expr ']'
9794 A_Indices *ai = makeNode(A_Indices);
9802 indirection_el { $$ = list_make1($1); }
9803 | indirection indirection_el { $$ = lappend($1, $2); }
9807 /*EMPTY*/ { $$ = NIL; }
9808 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9811 opt_asymmetric: ASYMMETRIC
9816 * The SQL spec defines "contextually typed value expressions" and
9817 * "contextually typed row value constructors", which for our purposes
9818 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9823 a_expr { $$ = (Node *) $1; }
9826 SetToDefault *n = makeNode(SetToDefault);
9833 ctext_expr { $$ = list_make1($1); }
9834 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9838 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9839 * making VALUES a fully reserved word, which will probably break more apps
9840 * than allowing the noise-word is worth.
9842 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9846 /*****************************************************************************
9848 * target list for SELECT
9850 *****************************************************************************/
9853 target_el { $$ = list_make1($1); }
9854 | target_list ',' target_el { $$ = lappend($1, $3); }
9857 target_el: a_expr AS ColLabel
9859 $$ = makeNode(ResTarget);
9861 $$->indirection = NIL;
9862 $$->val = (Node *)$1;
9866 * We support omitting AS only for column labels that aren't
9867 * any known keyword. There is an ambiguity against postfix
9868 * operators: is "a ! b" an infix expression, or a postfix
9869 * expression and a column label? We prefer to resolve this
9870 * as an infix expression, which we accomplish by assigning
9871 * IDENT a precedence higher than POSTFIXOP.
9875 $$ = makeNode(ResTarget);
9877 $$->indirection = NIL;
9878 $$->val = (Node *)$1;
9883 $$ = makeNode(ResTarget);
9885 $$->indirection = NIL;
9886 $$->val = (Node *)$1;
9891 ColumnRef *n = makeNode(ColumnRef);
9892 n->fields = list_make1(makeNode(A_Star));
9895 $$ = makeNode(ResTarget);
9897 $$->indirection = NIL;
9898 $$->val = (Node *)n;
9904 /*****************************************************************************
9906 * Names and constants
9908 *****************************************************************************/
9911 SpecialRuleRelation { $$ = $1; }
9912 | ColId { $$ = $1; }
9915 qualified_name_list:
9916 qualified_name { $$ = list_make1($1); }
9917 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9921 * The production for a qualified relation name has to exactly match the
9922 * production for a qualified func_name, because in a FROM clause we cannot
9923 * tell which we are parsing until we see what comes after it ('(' for a
9924 * func_name, something else for a relation). Therefore we allow 'indirection'
9925 * which may contain subscripts, and reject that case in the C code.
9930 $$ = makeNode(RangeVar);
9931 $$->catalogname = NULL;
9932 $$->schemaname = NULL;
9936 | relation_name indirection
9938 check_qualified_name($2, yyscanner);
9939 $$ = makeNode(RangeVar);
9940 switch (list_length($2))
9943 $$->catalogname = NULL;
9944 $$->schemaname = $1;
9945 $$->relname = strVal(linitial($2));
9948 $$->catalogname = $1;
9949 $$->schemaname = strVal(linitial($2));
9950 $$->relname = strVal(lsecond($2));
9954 (errcode(ERRCODE_SYNTAX_ERROR),
9955 errmsg("improper qualified name (too many dotted names): %s",
9956 NameListToString(lcons(makeString($1), $2))),
9957 parser_errposition(@1)));
9965 { $$ = list_make1(makeString($1)); }
9966 | name_list ',' name
9967 { $$ = lappend($1, makeString($3)); }
9971 name: ColId { $$ = $1; };
9979 attr_name: ColLabel { $$ = $1; };
9981 index_name: ColId { $$ = $1; };
9983 file_name: Sconst { $$ = $1; };
9986 * The production for a qualified func_name has to exactly match the
9987 * production for a qualified columnref, because we cannot tell which we
9988 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9989 * anything else for a columnref). Therefore we allow 'indirection' which
9990 * may contain subscripts, and reject that case in the C code. (If we
9991 * ever implement SQL99-like methods, such syntax may actually become legal!)
9993 func_name: type_function_name
9994 { $$ = list_make1(makeString($1)); }
9995 | relation_name indirection
9997 $$ = check_func_name(lcons(makeString($1), $2),
10008 $$ = makeIntConst($1, @1);
10012 $$ = makeFloatConst($1, @1);
10016 $$ = makeStringConst($1, @1);
10020 $$ = makeBitStringConst($1, @1);
10024 /* This is a bit constant per SQL99:
10025 * Without Feature F511, "BIT data type",
10026 * a <general literal> shall not be a
10027 * <bit string literal> or a <hex string literal>.
10029 $$ = makeBitStringConst($1, @1);
10033 /* generic type 'literal' syntax */
10034 TypeName *t = makeTypeNameFromNameList($1);
10036 $$ = makeStringConstCast($2, @2, t);
10038 | func_name '(' expr_list ')' Sconst
10040 /* generic syntax with a type modifier */
10041 TypeName *t = makeTypeNameFromNameList($1);
10044 $$ = makeStringConstCast($5, @5, t);
10046 | ConstTypename Sconst
10048 $$ = makeStringConstCast($2, @2, $1);
10050 | ConstInterval Sconst opt_interval
10054 $$ = makeStringConstCast($2, @2, t);
10056 | ConstInterval '(' Iconst ')' Sconst opt_interval
10061 if (list_length($6) != 1)
10063 (errcode(ERRCODE_SYNTAX_ERROR),
10064 errmsg("interval precision specified twice"),
10065 parser_errposition(@1)));
10066 t->typmods = lappend($6, makeIntConst($3, @3));
10069 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
10070 makeIntConst($3, @3));
10071 $$ = makeStringConstCast($5, @5, t);
10075 $$ = makeBoolAConst(TRUE, @1);
10079 $$ = makeBoolAConst(FALSE, @1);
10083 $$ = makeNullAConst(@1);
10087 Iconst: ICONST { $$ = $1; };
10088 Sconst: SCONST { $$ = $1; };
10089 RoleId: ColId { $$ = $1; };
10091 SignedIconst: Iconst { $$ = $1; }
10092 | '+' Iconst { $$ = + $2; }
10093 | '-' Iconst { $$ = - $2; }
10097 * Name classification hierarchy.
10099 * IDENT is the lexeme returned by the lexer for identifiers that match
10100 * no known keyword. In most cases, we can accept certain keywords as
10101 * names, not only IDENTs. We prefer to accept as many such keywords
10102 * as possible to minimize the impact of "reserved words" on programmers.
10103 * So, we divide names into several possible classes. The classification
10104 * is chosen in part to make keywords acceptable as names wherever possible.
10107 /* Column identifier --- names that can be column, table, etc names.
10109 ColId: IDENT { $$ = $1; }
10110 | unreserved_keyword { $$ = pstrdup($1); }
10111 | col_name_keyword { $$ = pstrdup($1); }
10114 /* Type/function identifier --- names that can be type or function names.
10116 type_function_name: IDENT { $$ = $1; }
10117 | unreserved_keyword { $$ = pstrdup($1); }
10118 | type_func_name_keyword { $$ = pstrdup($1); }
10121 /* Column label --- allowed labels in "AS" clauses.
10122 * This presently includes *all* Postgres keywords.
10124 ColLabel: IDENT { $$ = $1; }
10125 | unreserved_keyword { $$ = pstrdup($1); }
10126 | col_name_keyword { $$ = pstrdup($1); }
10127 | type_func_name_keyword { $$ = pstrdup($1); }
10128 | reserved_keyword { $$ = pstrdup($1); }
10133 * Keyword category lists. Generally, every keyword present in
10134 * the Postgres grammar should appear in exactly one of these lists.
10136 * Put a new keyword into the first list that it can go into without causing
10137 * shift or reduce conflicts. The earlier lists define "less reserved"
10138 * categories of keywords.
10140 * Make sure that each keyword's category in keywords.c matches where
10141 * it is listed here. (Someday we may be able to generate these lists and
10142 * keywords.c's table from a common master list.)
10145 /* "Unreserved" keywords --- available for use as any kind of name.
10147 unreserved_keyword:
10396 /* Column identifier --- keywords that can be column, table, etc names.
10398 * Many of these keywords will in fact be recognized as type or function
10399 * names too; but they have special productions for the purpose, and so
10400 * can't be treated as "generic" type or function names.
10402 * The type names appearing here are not usable as function names
10403 * because they can be followed by '(' in typename productions, which
10404 * looks too much like a function call for an LR(1) parser.
10454 /* Type/function identifier --- keywords that can be type or function names.
10456 * Most of these are keywords that are used as operators in expressions;
10457 * in general such keywords can't be column names because they would be
10458 * ambiguous with variables, but they are unambiguous as function identifiers.
10460 * Do not include POSITION, SUBSTRING, etc here since they have explicit
10461 * productions in a_expr to support the goofy SQL9x argument syntax.
10462 * - thomas 2000-11-28
10464 type_func_name_keyword:
10489 /* Reserved keyword --- these keywords are usable only as a ColLabel.
10491 * Keywords appear here if they could not be distinguished from variable,
10492 * type, or function names in some contexts. Don't put things here unless
10517 | CURRENT_TIMESTAMP
10578 SpecialRuleRelation:
10581 if (pg_yyget_extra(yyscanner)->QueryIsRule)
10585 (errcode(ERRCODE_SYNTAX_ERROR),
10586 errmsg("OLD used in query that is not in a rule"),
10587 parser_errposition(@1)));
10591 if (pg_yyget_extra(yyscanner)->QueryIsRule)
10595 (errcode(ERRCODE_SYNTAX_ERROR),
10596 errmsg("NEW used in query that is not in a rule"),
10597 parser_errposition(@1)));
10604 * The signature of this function is required by bison. However, we
10605 * ignore the passed yylloc and instead use the last token position
10606 * available from the scanner.
10609 base_yyerror(YYLTYPE *yylloc, base_yyscan_t yyscanner, const char *msg)
10611 parser_yyerror(msg);
10615 makeColumnRef(char *colname, List *indirection,
10616 int location, base_yyscan_t yyscanner)
10619 * Generate a ColumnRef node, with an A_Indirection node added if there
10620 * is any subscripting in the specified indirection list. However,
10621 * any field selection at the start of the indirection list must be
10622 * transposed into the "fields" part of the ColumnRef node.
10624 ColumnRef *c = makeNode(ColumnRef);
10628 c->location = location;
10629 foreach(l, indirection)
10631 if (IsA(lfirst(l), A_Indices))
10633 A_Indirection *i = makeNode(A_Indirection);
10637 /* easy case - all indirection goes to A_Indirection */
10638 c->fields = list_make1(makeString(colname));
10639 i->indirection = check_indirection(indirection, yyscanner);
10643 /* got to split the list in two */
10644 i->indirection = check_indirection(list_copy_tail(indirection,
10647 indirection = list_truncate(indirection, nfields);
10648 c->fields = lcons(makeString(colname), indirection);
10650 i->arg = (Node *) c;
10653 else if (IsA(lfirst(l), A_Star))
10655 /* We only allow '*' at the end of a ColumnRef */
10656 if (lnext(l) != NULL)
10657 parser_yyerror("improper use of \"*\"");
10661 /* No subscripting, so all indirection gets added to field list */
10662 c->fields = lcons(makeString(colname), indirection);
10667 makeTypeCast(Node *arg, TypeName *typename, int location)
10669 TypeCast *n = makeNode(TypeCast);
10671 n->typeName = typename;
10672 n->location = location;
10677 makeStringConst(char *str, int location)
10679 A_Const *n = makeNode(A_Const);
10681 n->val.type = T_String;
10682 n->val.val.str = str;
10683 n->location = location;
10689 makeStringConstCast(char *str, int location, TypeName *typename)
10691 Node *s = makeStringConst(str, location);
10693 return makeTypeCast(s, typename, -1);
10697 makeIntConst(int val, int location)
10699 A_Const *n = makeNode(A_Const);
10701 n->val.type = T_Integer;
10702 n->val.val.ival = val;
10703 n->location = location;
10709 makeFloatConst(char *str, int location)
10711 A_Const *n = makeNode(A_Const);
10713 n->val.type = T_Float;
10714 n->val.val.str = str;
10715 n->location = location;
10721 makeBitStringConst(char *str, int location)
10723 A_Const *n = makeNode(A_Const);
10725 n->val.type = T_BitString;
10726 n->val.val.str = str;
10727 n->location = location;
10733 makeNullAConst(int location)
10735 A_Const *n = makeNode(A_Const);
10737 n->val.type = T_Null;
10738 n->location = location;
10744 makeAConst(Value *v, int location)
10751 n = makeFloatConst(v->val.str, location);
10755 n = makeIntConst(v->val.ival, location);
10760 n = makeStringConst(v->val.str, location);
10767 /* makeBoolAConst()
10768 * Create an A_Const string node and put it inside a boolean cast.
10771 makeBoolAConst(bool state, int location)
10773 A_Const *n = makeNode(A_Const);
10775 n->val.type = T_String;
10776 n->val.val.str = (state ? "t" : "f");
10777 n->location = location;
10779 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
10783 * Create and populate a FuncCall node to support the OVERLAPS operator.
10786 makeOverlaps(List *largs, List *rargs, int location, base_yyscan_t yyscanner)
10788 FuncCall *n = makeNode(FuncCall);
10790 n->funcname = SystemFuncName("overlaps");
10791 if (list_length(largs) == 1)
10792 largs = lappend(largs, largs);
10793 else if (list_length(largs) != 2)
10795 (errcode(ERRCODE_SYNTAX_ERROR),
10796 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10797 parser_errposition(location)));
10798 if (list_length(rargs) == 1)
10799 rargs = lappend(rargs, rargs);
10800 else if (list_length(rargs) != 2)
10802 (errcode(ERRCODE_SYNTAX_ERROR),
10803 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10804 parser_errposition(location)));
10805 n->args = list_concat(largs, rargs);
10806 n->agg_star = FALSE;
10807 n->agg_distinct = FALSE;
10808 n->func_variadic = FALSE;
10810 n->location = location;
10814 /* check_qualified_name --- check the result of qualified_name production
10816 * It's easiest to let the grammar production for qualified_name allow
10817 * subscripts and '*', which we then must reject here.
10820 check_qualified_name(List *names, base_yyscan_t yyscanner)
10826 if (!IsA(lfirst(i), String))
10827 parser_yyerror("syntax error");
10831 /* check_func_name --- check the result of func_name production
10833 * It's easiest to let the grammar production for func_name allow subscripts
10834 * and '*', which we then must reject here.
10837 check_func_name(List *names, base_yyscan_t yyscanner)
10843 if (!IsA(lfirst(i), String))
10844 parser_yyerror("syntax error");
10849 /* check_indirection --- check the result of indirection production
10851 * We only allow '*' at the end of the list, but it's hard to enforce that
10852 * in the grammar, so do it here.
10855 check_indirection(List *indirection, base_yyscan_t yyscanner)
10859 foreach(l, indirection)
10861 if (IsA(lfirst(l), A_Star))
10863 if (lnext(l) != NULL)
10864 parser_yyerror("improper use of \"*\"");
10867 return indirection;
10870 /* extractArgTypes()
10871 * Given a list of FunctionParameter nodes, extract a list of just the
10872 * argument types (TypeNames) for input parameters only. This is what
10873 * is needed to look up an existing function, which is what is wanted by
10874 * the productions that use this call.
10877 extractArgTypes(List *parameters)
10879 List *result = NIL;
10882 foreach(i, parameters)
10884 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10886 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10887 result = lappend(result, p->argType);
10892 /* findLeftmostSelect()
10893 * Find the leftmost component SelectStmt in a set-operation parsetree.
10895 static SelectStmt *
10896 findLeftmostSelect(SelectStmt *node)
10898 while (node && node->op != SETOP_NONE)
10900 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10904 /* insertSelectOptions()
10905 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10907 * This routine is just to avoid duplicating code in SelectStmt productions.
10910 insertSelectOptions(SelectStmt *stmt,
10911 List *sortClause, List *lockingClause,
10912 Node *limitOffset, Node *limitCount,
10913 WithClause *withClause,
10914 base_yyscan_t yyscanner)
10916 Assert(IsA(stmt, SelectStmt));
10919 * Tests here are to reject constructs like
10920 * (SELECT foo ORDER BY bar) ORDER BY baz
10924 if (stmt->sortClause)
10926 (errcode(ERRCODE_SYNTAX_ERROR),
10927 errmsg("multiple ORDER BY clauses not allowed"),
10928 parser_errposition(exprLocation((Node *) sortClause))));
10929 stmt->sortClause = sortClause;
10931 /* We can handle multiple locking clauses, though */
10932 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10935 if (stmt->limitOffset)
10937 (errcode(ERRCODE_SYNTAX_ERROR),
10938 errmsg("multiple OFFSET clauses not allowed"),
10939 parser_errposition(exprLocation(limitOffset))));
10940 stmt->limitOffset = limitOffset;
10944 if (stmt->limitCount)
10946 (errcode(ERRCODE_SYNTAX_ERROR),
10947 errmsg("multiple LIMIT clauses not allowed"),
10948 parser_errposition(exprLocation(limitCount))));
10949 stmt->limitCount = limitCount;
10953 if (stmt->withClause)
10955 (errcode(ERRCODE_SYNTAX_ERROR),
10956 errmsg("multiple WITH clauses not allowed"),
10957 parser_errposition(exprLocation((Node *) withClause))));
10958 stmt->withClause = withClause;
10963 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10965 SelectStmt *n = makeNode(SelectStmt);
10969 n->larg = (SelectStmt *) larg;
10970 n->rarg = (SelectStmt *) rarg;
10974 /* SystemFuncName()
10975 * Build a properly-qualified reference to a built-in function.
10978 SystemFuncName(char *name)
10980 return list_make2(makeString("pg_catalog"), makeString(name));
10983 /* SystemTypeName()
10984 * Build a properly-qualified reference to a built-in type.
10986 * typmod is defaulted, but may be changed afterwards by caller.
10987 * Likewise for the location.
10990 SystemTypeName(char *name)
10992 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10993 makeString(name)));
10997 * Handle negation of a numeric constant.
10999 * Formerly, we did this here because the optimizer couldn't cope with
11000 * indexquals that looked like "var = -4" --- it wants "var = const"
11001 * and a unary minus operator applied to a constant didn't qualify.
11002 * As of Postgres 7.0, that problem doesn't exist anymore because there
11003 * is a constant-subexpression simplifier in the optimizer. However,
11004 * there's still a good reason for doing this here, which is that we can
11005 * postpone committing to a particular internal representation for simple
11006 * negative constants. It's better to leave "-123.456" in string form
11007 * until we know what the desired type is.
11010 doNegate(Node *n, int location)
11012 if (IsA(n, A_Const))
11014 A_Const *con = (A_Const *)n;
11016 /* report the constant's location as that of the '-' sign */
11017 con->location = location;
11019 if (con->val.type == T_Integer)
11021 con->val.val.ival = -con->val.val.ival;
11024 if (con->val.type == T_Float)
11026 doNegateFloat(&con->val);
11031 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
11035 doNegateFloat(Value *v)
11037 char *oldval = v->val.str;
11039 Assert(IsA(v, Float));
11040 if (*oldval == '+')
11042 if (*oldval == '-')
11043 v->val.str = oldval+1; /* just strip the '-' */
11046 char *newval = (char *) palloc(strlen(oldval) + 2);
11049 strcpy(newval+1, oldval);
11050 v->val.str = newval;
11055 makeAArrayExpr(List *elements, int location)
11057 A_ArrayExpr *n = makeNode(A_ArrayExpr);
11059 n->elements = elements;
11060 n->location = location;
11065 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
11068 XmlExpr *x = makeNode(XmlExpr);
11073 * named_args is a list of ResTarget; it'll be split apart into separate
11074 * expression and name lists in transformXmlExpr().
11076 x->named_args = named_args;
11077 x->arg_names = NIL;
11079 /* xmloption, if relevant, must be filled in by caller */
11080 /* type and typmod will be filled in during parse analysis */
11081 x->location = location;
11086 * Initialize to parse one query string
11089 parser_init(base_yy_extra_type *yyext)
11091 yyext->parsetree = NIL; /* in case grammar forgets to set it */
11092 yyext->QueryIsRule = FALSE;
11096 * Merge the input and output parameters of a table function.
11099 mergeTableFuncParameters(List *func_args, List *columns)
11103 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
11104 foreach(lc, func_args)
11106 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
11108 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
11110 (errcode(ERRCODE_SYNTAX_ERROR),
11111 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
11114 return list_concat(func_args, columns);
11118 * Determine return type of a TABLE function. A single result column
11119 * returns setof that column's type; otherwise return setof record.
11122 TableFuncTypeName(List *columns)
11126 if (list_length(columns) == 1)
11128 FunctionParameter *p = (FunctionParameter *) linitial(columns);
11130 result = (TypeName *) copyObject(p->argType);
11133 result = SystemTypeName("record");
11135 result->setof = true;
11141 * Must undefine base_yylex before including scan.c, since we want it
11142 * to create the function base_yylex not filtered_base_yylex.
11146 /* Undefine some other stuff that would conflict in scan.c, too */