4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.626 2008/10/20 14:26:28 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 "commands/defrem.h"
57 #include "nodes/makefuncs.h"
58 #include "nodes/nodeFuncs.h"
59 #include "parser/gramparse.h"
60 #include "storage/lmgr.h"
61 #include "utils/date.h"
62 #include "utils/datetime.h"
63 #include "utils/numeric.h"
64 #include "utils/xml.h"
67 /* Location tracking support --- simpler than bison's default */
68 #define YYLLOC_DEFAULT(Current, Rhs, N) \
71 (Current) = (Rhs)[1]; \
73 (Current) = (Rhs)[0]; \
77 * The %name-prefix option below will make bison call base_yylex, but we
78 * really want it to call filtered_base_yylex (see parser.c).
80 #define base_yylex filtered_base_yylex
83 * Bison doesn't allocate anything that needs to live across parser calls,
84 * so we can easily have it use palloc instead of malloc. This prevents
85 * memory leaks if we error out during parsing. Note this only works with
86 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
87 * if possible, so there's not really much problem anyhow, at least if
88 * you're building with gcc.
90 #define YYMALLOC palloc
93 extern List *parsetree; /* final parse result is delivered here */
95 static bool QueryIsRule = FALSE;
98 * If you need access to certain yacc-generated variables and find that
99 * they're static by default, uncomment the next line. (this is not a
102 /*#define __YYSCLASS*/
104 static Node *makeColumnRef(char *colname, List *indirection, int location);
105 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
106 static Node *makeStringConst(char *str, int location);
107 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
108 static Node *makeIntConst(int val, int location);
109 static Node *makeFloatConst(char *str, int location);
110 static Node *makeBitStringConst(char *str, int location);
111 static Node *makeNullAConst(int location);
112 static Node *makeAConst(Value *v, int location);
113 static Node *makeBoolAConst(bool state, int location);
114 static FuncCall *makeOverlaps(List *largs, List *rargs, int location);
115 static void check_qualified_name(List *names);
116 static List *check_func_name(List *names);
117 static List *check_indirection(List *indirection);
118 static List *extractArgTypes(List *parameters);
119 static SelectStmt *findLeftmostSelect(SelectStmt *node);
120 static void insertSelectOptions(SelectStmt *stmt,
121 List *sortClause, List *lockingClause,
122 Node *limitOffset, Node *limitCount,
123 WithClause *withClause);
124 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
125 static Node *doNegate(Node *n, int location);
126 static void doNegateFloat(Value *v);
127 static Node *makeAArrayExpr(List *elements, int location);
128 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
129 List *args, int location);
130 static List *mergeTableFuncParameters(List *func_args, List *columns);
131 static TypeName *TableFuncTypeName(List *columns);
135 %name-prefix="base_yy"
146 DropBehavior dbehavior;
147 OnCommitAction oncommit;
154 FunctionParameter *fun_param;
155 FunctionParameterMode fun_param_mode;
156 FuncWithArgs *funwithargs;
167 PrivTarget *privtarget;
170 VariableSetStmt *vsetstmt;
173 %type <node> stmt schema_stmt
174 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
175 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
176 AlterUserStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
177 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
178 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
179 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
180 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
181 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
182 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
183 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
184 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
185 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
186 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
187 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
188 LockStmt NotifyStmt ExplainableStmt PreparableStmt
189 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
190 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
191 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
192 SelectStmt TransactionStmt TruncateStmt
193 UnlistenStmt UpdateStmt VacuumStmt
194 VariableResetStmt VariableSetStmt VariableShowStmt
195 ViewStmt CheckPointStmt CreateConversionStmt
196 DeallocateStmt PrepareStmt ExecuteStmt
197 DropOwnedStmt ReassignOwnedStmt
198 AlterTSConfigurationStmt AlterTSDictionaryStmt
200 %type <node> select_no_parens select_with_parens select_clause
201 simple_select values_clause
203 %type <node> alter_column_default opclass_item opclass_drop alter_using
204 %type <ival> add_drop opt_asc_desc opt_nulls_order
206 %type <node> alter_table_cmd
207 %type <list> alter_table_cmds
209 %type <dbehavior> opt_drop_behavior
211 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
212 transaction_mode_list
213 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
214 transaction_mode_item
216 %type <ival> opt_lock lock_type cast_context
217 %type <boolean> opt_force opt_or_replace
218 opt_grant_grant_option opt_grant_admin_option
219 opt_nowait opt_if_exists
221 %type <list> OptRoleList
222 %type <defelt> OptRoleElem
224 %type <str> OptSchemaName
225 %type <list> OptSchemaEltList
227 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
228 %type <str> opt_lancompiler
230 %type <str> TriggerEvents
231 %type <value> TriggerFuncArg
233 %type <str> relation_name copy_file_name
234 database_name access_method_clause access_method attr_name
235 index_name name file_name cluster_index_specification
237 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
238 opt_class opt_validator
240 %type <range> qualified_name OptConstrFromTable
242 %type <str> all_Op MathOp SpecialRuleRelation
244 %type <str> iso_level opt_encoding
246 %type <list> grantee_list
247 %type <str> privilege
248 %type <list> privileges privilege_list
249 %type <privtarget> privilege_target
250 %type <funwithargs> function_with_argtypes
251 %type <list> function_with_argtypes_list
252 %type <chr> TriggerOneEvent
254 %type <list> stmtblock stmtmulti
255 OptTableElementList TableElementList OptInherit definition
256 OptWith opt_distinct opt_definition func_args func_args_list
257 func_as createfunc_opt_list alterfunc_opt_list
258 aggr_args old_aggr_definition old_aggr_list
259 oper_argtypes RuleActionList RuleActionMulti
260 opt_column_list columnList opt_name_list
261 sort_clause opt_sort_clause sortby_list index_params
262 name_list from_clause from_list opt_array_bounds
263 qualified_name_list any_name any_name_list
264 any_operator expr_list attrs
265 target_list insert_column_list set_target_list
266 set_clause_list set_clause multiple_set_clause
267 ctext_expr_list ctext_row def_list indirection opt_indirection
268 group_clause TriggerFuncArgs select_limit
269 opt_select_limit opclass_item_list opclass_drop_list
270 opt_opfamily transaction_mode_list_or_empty
271 TableFuncElementList opt_type_modifiers
273 execute_param_clause using_clause returning_clause
274 enum_val_list table_func_column_list
276 %type <range> OptTempTableName
277 %type <into> into_clause create_as_target
279 %type <defelt> createfunc_opt_item common_func_opt_item
280 %type <fun_param> func_arg table_func_column
281 %type <fun_param_mode> arg_class
282 %type <typnam> func_return func_type
284 %type <boolean> TriggerForType OptTemp
285 %type <oncommit> OnCommitOption
287 %type <node> for_locking_item
288 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
289 %type <list> locked_rels_list
290 %type <boolean> opt_all
292 %type <node> join_outer join_qual
293 %type <jtype> join_type
295 %type <list> extract_list overlay_list position_list
296 %type <list> substr_list trim_list
297 %type <list> opt_interval interval_second
298 %type <node> overlay_placing substr_from substr_for
300 %type <boolean> opt_instead opt_analyze
301 %type <boolean> index_opt_unique opt_verbose opt_full
302 %type <boolean> opt_freeze opt_default opt_recheck
303 %type <defelt> opt_binary opt_oids copy_delimiter
305 %type <boolean> copy_from
307 %type <ival> opt_column event cursor_options opt_hold
308 %type <objtype> reindex_type drop_type comment_type
310 %type <node> fetch_direction select_limit_value select_offset_value
312 %type <list> OptSeqOptList SeqOptList
313 %type <defelt> SeqOptElem
315 %type <istmt> insert_rest
317 %type <vsetstmt> set_rest SetResetClause
319 %type <node> TableElement ConstraintElem TableFuncElement
320 %type <node> columnDef
321 %type <defelt> def_elem old_aggr_elem
322 %type <node> def_arg columnElem where_clause where_or_current_clause
323 a_expr b_expr c_expr func_expr AexprConst indirection_el
324 columnref in_expr having_clause func_table array_expr
325 %type <list> row type_list array_expr_list
326 %type <node> case_expr case_arg when_clause case_default
327 %type <list> when_clause_list
328 %type <ival> sub_type
329 %type <list> OptCreateAs CreateAsList
330 %type <node> CreateAsElement ctext_expr
331 %type <value> NumericOnly FloatOnly IntegerOnly
332 %type <alias> alias_clause
333 %type <sortby> sortby
334 %type <ielem> index_elem
335 %type <node> table_ref
336 %type <jexpr> joined_table
337 %type <range> relation_expr
338 %type <range> relation_expr_opt_alias
339 %type <target> target_el single_set_clause set_target insert_column_item
341 %type <typnam> Typename SimpleTypename ConstTypename
342 GenericType Numeric opt_float
343 Character ConstCharacter
344 CharacterWithLength CharacterWithoutLength
345 ConstDatetime ConstInterval
346 Bit ConstBit BitWithLength BitWithoutLength
347 %type <str> character
348 %type <str> extract_arg
349 %type <str> opt_charset
350 %type <boolean> opt_varying opt_timezone
352 %type <ival> Iconst SignedIconst
353 %type <str> Sconst comment_text
354 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
355 %type <list> var_list
356 %type <str> ColId ColLabel var_name type_function_name param_name
357 %type <node> var_value zone_value
359 %type <keyword> unreserved_keyword type_func_name_keyword
360 %type <keyword> col_name_keyword reserved_keyword
362 %type <node> TableConstraint TableLikeClause
363 %type <list> TableLikeOptionList
364 %type <ival> TableLikeOption
365 %type <list> ColQualList
366 %type <node> ColConstraint ColConstraintElem ConstraintAttr
367 %type <ival> key_actions key_delete key_match key_update key_action
368 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
371 %type <list> constraints_set_list
372 %type <boolean> constraints_set_mode
373 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
374 %type <list> opt_check_option
376 %type <target> xml_attribute_el
377 %type <list> xml_attribute_list xml_attributes
378 %type <node> xml_root_version opt_xml_root_standalone
379 %type <ival> document_or_content
380 %type <boolean> xml_whitespace_option
382 %type <node> common_table_expr
383 %type <with> with_clause
384 %type <list> cte_list
388 * If you make any token changes, update the keyword table in
389 * parser/keywords.c and add new keywords to the appropriate one of
390 * the reserved-or-not-so-reserved keyword lists, below; search
391 * this file for "Name classification hierarchy".
394 /* ordinary key words in alphabetical order */
395 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
396 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
397 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
399 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
402 CACHE CALLED CASCADE CASCADED CASE CAST CHAIN CHAR_P
403 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
404 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
405 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
406 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
407 CREATEROLE CREATEUSER CROSS CSV CTYPE CURRENT_P CURRENT_DATE CURRENT_ROLE
408 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
410 DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
411 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
412 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
414 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
415 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
417 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
418 FREEZE FROM FULL FUNCTION
420 GLOBAL GRANT GRANTED GREATEST GROUP_P
422 HANDLER HAVING HEADER_P HOLD HOUR_P
424 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
425 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
426 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
427 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
433 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
434 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
437 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
439 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
440 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
441 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
443 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
444 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
446 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
447 PRECISION PRESERVE PREPARE PREPARED PRIMARY
448 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
452 READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX RELATIVE_P RELEASE
453 RENAME REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS
454 REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
456 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
457 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
458 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
459 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
460 SYMMETRIC SYSID SYSTEM_P
462 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
463 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
464 TRUNCATE TRUSTED TYPE_P
466 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
469 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
470 VERBOSE VERSION_P VIEW VOLATILE
472 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRITE
474 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
475 XMLPI XMLROOT XMLSERIALIZE
481 /* The grammar thinks these are keywords, but they are not in the keywords.c
482 * list and so can never be entered directly. The filter in parser.c
483 * creates these tokens when required.
485 %token NULLS_FIRST NULLS_LAST WITH_CASCADED WITH_LOCAL WITH_CHECK
487 /* Special token types, not actually keywords - see the "lex" file */
488 %token <str> IDENT FCONST SCONST BCONST XCONST Op
489 %token <ival> ICONST PARAM
491 /* precedence: lowest to highest */
492 %nonassoc SET /* see relation_expr_opt_alias */
500 %nonassoc LIKE ILIKE SIMILAR
505 %left POSTFIXOP /* dummy for postfix Op rules */
506 %nonassoc IDENT /* to support target_el without AS */
507 %left Op OPERATOR /* multi-character ops and user-defined operators */
510 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
514 /* Unary Operators */
515 %left AT ZONE /* sets precedence for AT TIME ZONE */
522 * These might seem to be low-precedence, but actually they are not part
523 * of the arithmetic hierarchy at all in their use as JOIN operators.
524 * We make them high-precedence to support their use as function names.
525 * They wouldn't be given a precedence at all, were it not that we need
526 * left-associativity among the JOIN rules themselves.
528 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
529 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
530 %right PRESERVE STRIP_P
534 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
535 * psql already handles such cases, but other interfaces don't.
538 stmtblock: stmtmulti { parsetree = $1; }
541 /* the thrashing around here is to discard "empty" statements... */
542 stmtmulti: stmtmulti ';' stmt
544 $$ = lappend($1, $3);
558 | AlterDatabaseSetStmt
562 | AlterObjectSchemaStmt
568 | AlterTSConfigurationStmt
569 | AlterTSDictionaryStmt
582 | CreateConversionStmt
593 | CreateTableSpaceStmt
652 /*****************************************************************************
654 * Create a new Postgres DBMS role
656 *****************************************************************************/
659 CREATE ROLE RoleId opt_with OptRoleList
661 CreateRoleStmt *n = makeNode(CreateRoleStmt);
662 n->stmt_type = ROLESTMT_ROLE;
675 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
676 * for backwards compatibility). Note: the only option required by SQL99
677 * is "WITH ADMIN name".
680 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
681 | /* EMPTY */ { $$ = NIL; }
687 $$ = makeDefElem("password",
688 (Node *)makeString($2));
692 $$ = makeDefElem("password", NULL);
694 | ENCRYPTED PASSWORD Sconst
696 $$ = makeDefElem("encryptedPassword",
697 (Node *)makeString($3));
699 | UNENCRYPTED PASSWORD Sconst
701 $$ = makeDefElem("unencryptedPassword",
702 (Node *)makeString($3));
706 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
710 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
714 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
718 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
722 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
726 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
730 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
734 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
738 /* For backwards compatibility, synonym for SUPERUSER */
739 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
743 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
747 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
751 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
753 | CONNECTION LIMIT SignedIconst
755 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
759 $$ = makeDefElem("validUntil", (Node *)makeString($3));
761 /* Supported but not documented for roles, for use by ALTER GROUP. */
764 $$ = makeDefElem("rolemembers", (Node *)$2);
766 /* The following are not supported by ALTER ROLE/USER/GROUP */
769 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
773 $$ = makeDefElem("adminmembers", (Node *)$2);
777 $$ = makeDefElem("rolemembers", (Node *)$2);
779 | IN_P ROLE name_list
781 $$ = makeDefElem("addroleto", (Node *)$3);
783 | IN_P GROUP_P name_list
785 $$ = makeDefElem("addroleto", (Node *)$3);
790 /*****************************************************************************
792 * Create a new Postgres DBMS user (role with implied login ability)
794 *****************************************************************************/
797 CREATE USER RoleId opt_with OptRoleList
799 CreateRoleStmt *n = makeNode(CreateRoleStmt);
800 n->stmt_type = ROLESTMT_USER;
808 /*****************************************************************************
810 * Alter a postgresql DBMS role
812 *****************************************************************************/
815 ALTER ROLE RoleId opt_with OptRoleList
817 AlterRoleStmt *n = makeNode(AlterRoleStmt);
819 n->action = +1; /* add, if there are members */
826 ALTER ROLE RoleId SetResetClause
828 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
836 /*****************************************************************************
838 * Alter a postgresql DBMS user
840 *****************************************************************************/
843 ALTER USER RoleId opt_with OptRoleList
845 AlterRoleStmt *n = makeNode(AlterRoleStmt);
847 n->action = +1; /* add, if there are members */
855 ALTER USER RoleId SetResetClause
857 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
865 /*****************************************************************************
867 * Drop a postgresql DBMS role
869 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
870 * might own objects in multiple databases, there is presently no way to
871 * implement either cascading or restricting. Caveat DBA.
872 *****************************************************************************/
877 DropRoleStmt *n = makeNode(DropRoleStmt);
878 n->missing_ok = FALSE;
882 | DROP ROLE IF_P EXISTS name_list
884 DropRoleStmt *n = makeNode(DropRoleStmt);
885 n->missing_ok = TRUE;
891 /*****************************************************************************
893 * Drop a postgresql DBMS user
895 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
896 * might own objects in multiple databases, there is presently no way to
897 * implement either cascading or restricting. Caveat DBA.
898 *****************************************************************************/
903 DropRoleStmt *n = makeNode(DropRoleStmt);
904 n->missing_ok = FALSE;
908 | DROP USER IF_P EXISTS name_list
910 DropRoleStmt *n = makeNode(DropRoleStmt);
912 n->missing_ok = TRUE;
918 /*****************************************************************************
920 * Create a postgresql group (role without login ability)
922 *****************************************************************************/
925 CREATE GROUP_P RoleId opt_with OptRoleList
927 CreateRoleStmt *n = makeNode(CreateRoleStmt);
928 n->stmt_type = ROLESTMT_GROUP;
936 /*****************************************************************************
938 * Alter a postgresql group
940 *****************************************************************************/
943 ALTER GROUP_P RoleId add_drop USER name_list
945 AlterRoleStmt *n = makeNode(AlterRoleStmt);
948 n->options = list_make1(makeDefElem("rolemembers",
954 add_drop: ADD_P { $$ = +1; }
959 /*****************************************************************************
961 * Drop a postgresql group
963 * XXX see above notes about cascading DROP USER; groups have same problem.
964 *****************************************************************************/
967 DROP GROUP_P name_list
969 DropRoleStmt *n = makeNode(DropRoleStmt);
970 n->missing_ok = FALSE;
974 | DROP GROUP_P IF_P EXISTS name_list
976 DropRoleStmt *n = makeNode(DropRoleStmt);
977 n->missing_ok = TRUE;
984 /*****************************************************************************
986 * Manipulate a schema
988 *****************************************************************************/
991 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
993 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
994 /* One can omit the schema name or the authorization id. */
1003 | CREATE SCHEMA ColId OptSchemaEltList
1005 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1006 /* ...but not both */
1016 | /* EMPTY */ { $$ = NULL; }
1020 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1021 | /* EMPTY */ { $$ = NIL; }
1025 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1026 * statement (in addition to by themselves).
1038 /*****************************************************************************
1040 * Set PG internal variable
1041 * SET name TO 'var_value'
1042 * Include SQL92 syntax (thomas 1997-10-22):
1043 * SET TIME ZONE 'var_value'
1045 *****************************************************************************/
1050 VariableSetStmt *n = $2;
1051 n->is_local = false;
1054 | SET LOCAL set_rest
1056 VariableSetStmt *n = $3;
1060 | SET SESSION set_rest
1062 VariableSetStmt *n = $3;
1063 n->is_local = false;
1068 set_rest: /* Generic SET syntaxes: */
1069 var_name TO var_list
1071 VariableSetStmt *n = makeNode(VariableSetStmt);
1072 n->kind = VAR_SET_VALUE;
1077 | var_name '=' var_list
1079 VariableSetStmt *n = makeNode(VariableSetStmt);
1080 n->kind = VAR_SET_VALUE;
1085 | var_name TO DEFAULT
1087 VariableSetStmt *n = makeNode(VariableSetStmt);
1088 n->kind = VAR_SET_DEFAULT;
1092 | var_name '=' DEFAULT
1094 VariableSetStmt *n = makeNode(VariableSetStmt);
1095 n->kind = VAR_SET_DEFAULT;
1099 | var_name FROM CURRENT_P
1101 VariableSetStmt *n = makeNode(VariableSetStmt);
1102 n->kind = VAR_SET_CURRENT;
1106 /* Special syntaxes mandated by SQL standard: */
1107 | TIME ZONE zone_value
1109 VariableSetStmt *n = makeNode(VariableSetStmt);
1110 n->kind = VAR_SET_VALUE;
1111 n->name = "timezone";
1113 n->args = list_make1($3);
1115 n->kind = VAR_SET_DEFAULT;
1118 | TRANSACTION transaction_mode_list
1120 VariableSetStmt *n = makeNode(VariableSetStmt);
1121 n->kind = VAR_SET_MULTI;
1122 n->name = "TRANSACTION";
1126 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1128 VariableSetStmt *n = makeNode(VariableSetStmt);
1129 n->kind = VAR_SET_MULTI;
1130 n->name = "SESSION CHARACTERISTICS";
1134 | NAMES opt_encoding
1136 VariableSetStmt *n = makeNode(VariableSetStmt);
1137 n->kind = VAR_SET_VALUE;
1138 n->name = "client_encoding";
1140 n->args = list_make1(makeStringConst($2, @2));
1142 n->kind = VAR_SET_DEFAULT;
1145 | ROLE ColId_or_Sconst
1147 VariableSetStmt *n = makeNode(VariableSetStmt);
1148 n->kind = VAR_SET_VALUE;
1150 n->args = list_make1(makeStringConst($2, @2));
1153 | SESSION AUTHORIZATION ColId_or_Sconst
1155 VariableSetStmt *n = makeNode(VariableSetStmt);
1156 n->kind = VAR_SET_VALUE;
1157 n->name = "session_authorization";
1158 n->args = list_make1(makeStringConst($3, @3));
1161 | SESSION AUTHORIZATION DEFAULT
1163 VariableSetStmt *n = makeNode(VariableSetStmt);
1164 n->kind = VAR_SET_DEFAULT;
1165 n->name = "session_authorization";
1168 | XML_P OPTION document_or_content
1170 VariableSetStmt *n = makeNode(VariableSetStmt);
1171 n->kind = VAR_SET_VALUE;
1172 n->name = "xmloption";
1173 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1178 var_name: ColId { $$ = $1; }
1179 | var_name '.' ColId
1181 $$ = palloc(strlen($1) + strlen($3) + 2);
1182 sprintf($$, "%s.%s", $1, $3);
1186 var_list: var_value { $$ = list_make1($1); }
1187 | var_list ',' var_value { $$ = lappend($1, $3); }
1190 var_value: opt_boolean
1191 { $$ = makeStringConst($1, @1); }
1193 { $$ = makeStringConst($1, @1); }
1195 { $$ = makeAConst($1, @1); }
1198 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1199 | READ COMMITTED { $$ = "read committed"; }
1200 | REPEATABLE READ { $$ = "repeatable read"; }
1201 | SERIALIZABLE { $$ = "serializable"; }
1205 TRUE_P { $$ = "true"; }
1206 | FALSE_P { $$ = "false"; }
1208 | OFF { $$ = "off"; }
1211 /* Timezone values can be:
1212 * - a string such as 'pst8pdt'
1213 * - an identifier such as "pst8pdt"
1214 * - an integer or floating point number
1215 * - a time interval per SQL99
1216 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1217 * so use IDENT and reject anything which is a reserved word.
1222 $$ = makeStringConst($1, @1);
1226 $$ = makeStringConst($1, @1);
1228 | ConstInterval Sconst opt_interval
1233 A_Const *n = (A_Const *) linitial($3);
1234 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1236 (errcode(ERRCODE_SYNTAX_ERROR),
1237 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1238 scanner_errposition(@3)));
1241 $$ = makeStringConstCast($2, @2, t);
1243 | ConstInterval '(' Iconst ')' Sconst opt_interval
1248 A_Const *n = (A_Const *) linitial($6);
1249 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1251 (errcode(ERRCODE_SYNTAX_ERROR),
1252 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1253 scanner_errposition(@6)));
1254 if (list_length($6) != 1)
1256 (errcode(ERRCODE_SYNTAX_ERROR),
1257 errmsg("interval precision specified twice"),
1258 scanner_errposition(@1)));
1259 t->typmods = lappend($6, makeIntConst($3, @3));
1262 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1263 makeIntConst($3, @3));
1264 $$ = makeStringConstCast($5, @5, t);
1266 | NumericOnly { $$ = makeAConst($1, @1); }
1267 | DEFAULT { $$ = NULL; }
1268 | LOCAL { $$ = NULL; }
1273 | DEFAULT { $$ = NULL; }
1274 | /*EMPTY*/ { $$ = NULL; }
1279 | SCONST { $$ = $1; }
1285 VariableSetStmt *n = makeNode(VariableSetStmt);
1286 n->kind = VAR_RESET;
1292 VariableSetStmt *n = makeNode(VariableSetStmt);
1293 n->kind = VAR_RESET;
1294 n->name = "timezone";
1297 | RESET TRANSACTION ISOLATION LEVEL
1299 VariableSetStmt *n = makeNode(VariableSetStmt);
1300 n->kind = VAR_RESET;
1301 n->name = "transaction_isolation";
1304 | RESET SESSION AUTHORIZATION
1306 VariableSetStmt *n = makeNode(VariableSetStmt);
1307 n->kind = VAR_RESET;
1308 n->name = "session_authorization";
1313 VariableSetStmt *n = makeNode(VariableSetStmt);
1314 n->kind = VAR_RESET_ALL;
1319 /* SetResetClause allows SET or RESET without LOCAL */
1321 SET set_rest { $$ = $2; }
1322 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1329 VariableShowStmt *n = makeNode(VariableShowStmt);
1335 VariableShowStmt *n = makeNode(VariableShowStmt);
1336 n->name = "timezone";
1339 | SHOW TRANSACTION ISOLATION LEVEL
1341 VariableShowStmt *n = makeNode(VariableShowStmt);
1342 n->name = "transaction_isolation";
1345 | SHOW SESSION AUTHORIZATION
1347 VariableShowStmt *n = makeNode(VariableShowStmt);
1348 n->name = "session_authorization";
1353 VariableShowStmt *n = makeNode(VariableShowStmt);
1361 SET CONSTRAINTS constraints_set_list constraints_set_mode
1363 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1364 n->constraints = $3;
1370 constraints_set_list:
1372 | qualified_name_list { $$ = $1; }
1375 constraints_set_mode:
1376 DEFERRED { $$ = TRUE; }
1377 | IMMEDIATE { $$ = FALSE; }
1382 * Checkpoint statement
1387 CheckPointStmt *n = makeNode(CheckPointStmt);
1393 /*****************************************************************************
1395 * DISCARD { ALL | TEMP | PLANS }
1397 *****************************************************************************/
1402 DiscardStmt *n = makeNode(DiscardStmt);
1403 n->target = DISCARD_ALL;
1408 DiscardStmt *n = makeNode(DiscardStmt);
1409 n->target = DISCARD_TEMP;
1414 DiscardStmt *n = makeNode(DiscardStmt);
1415 n->target = DISCARD_TEMP;
1420 DiscardStmt *n = makeNode(DiscardStmt);
1421 n->target = DISCARD_PLANS;
1427 /*****************************************************************************
1429 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1431 * Note: we accept all subcommands for each of the four variants, and sort
1432 * out what's really legal at execution time.
1433 *****************************************************************************/
1436 ALTER TABLE relation_expr alter_table_cmds
1438 AlterTableStmt *n = makeNode(AlterTableStmt);
1441 n->relkind = OBJECT_TABLE;
1444 | ALTER INDEX relation_expr alter_table_cmds
1446 AlterTableStmt *n = makeNode(AlterTableStmt);
1449 n->relkind = OBJECT_INDEX;
1452 | ALTER SEQUENCE relation_expr alter_table_cmds
1454 AlterTableStmt *n = makeNode(AlterTableStmt);
1457 n->relkind = OBJECT_SEQUENCE;
1460 | ALTER VIEW relation_expr alter_table_cmds
1462 AlterTableStmt *n = makeNode(AlterTableStmt);
1465 n->relkind = OBJECT_VIEW;
1471 alter_table_cmd { $$ = list_make1($1); }
1472 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1476 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1477 ADD_P opt_column columnDef
1479 AlterTableCmd *n = makeNode(AlterTableCmd);
1480 n->subtype = AT_AddColumn;
1484 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1485 | ALTER opt_column ColId alter_column_default
1487 AlterTableCmd *n = makeNode(AlterTableCmd);
1488 n->subtype = AT_ColumnDefault;
1493 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1494 | ALTER opt_column ColId DROP NOT NULL_P
1496 AlterTableCmd *n = makeNode(AlterTableCmd);
1497 n->subtype = AT_DropNotNull;
1501 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1502 | ALTER opt_column ColId SET NOT NULL_P
1504 AlterTableCmd *n = makeNode(AlterTableCmd);
1505 n->subtype = AT_SetNotNull;
1509 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1510 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1512 AlterTableCmd *n = makeNode(AlterTableCmd);
1513 n->subtype = AT_SetStatistics;
1515 n->def = (Node *) $6;
1518 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1519 | ALTER opt_column ColId SET STORAGE ColId
1521 AlterTableCmd *n = makeNode(AlterTableCmd);
1522 n->subtype = AT_SetStorage;
1524 n->def = (Node *) makeString($6);
1527 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1528 | DROP opt_column ColId opt_drop_behavior
1530 AlterTableCmd *n = makeNode(AlterTableCmd);
1531 n->subtype = AT_DropColumn;
1537 * ALTER TABLE <name> ALTER [COLUMN] <colname> TYPE <typename>
1538 * [ USING <expression> ]
1540 | ALTER opt_column ColId TYPE_P Typename alter_using
1542 AlterTableCmd *n = makeNode(AlterTableCmd);
1543 n->subtype = AT_AlterColumnType;
1545 n->def = (Node *) $5;
1549 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1550 | ADD_P TableConstraint
1552 AlterTableCmd *n = makeNode(AlterTableCmd);
1553 n->subtype = AT_AddConstraint;
1557 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1558 | DROP CONSTRAINT name opt_drop_behavior
1560 AlterTableCmd *n = makeNode(AlterTableCmd);
1561 n->subtype = AT_DropConstraint;
1566 /* ALTER TABLE <name> SET WITHOUT OIDS */
1569 AlterTableCmd *n = makeNode(AlterTableCmd);
1570 n->subtype = AT_DropOids;
1573 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1576 AlterTableCmd *n = makeNode(AlterTableCmd);
1577 n->subtype = AT_ClusterOn;
1581 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1582 | SET WITHOUT CLUSTER
1584 AlterTableCmd *n = makeNode(AlterTableCmd);
1585 n->subtype = AT_DropCluster;
1589 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1590 | ENABLE_P TRIGGER name
1592 AlterTableCmd *n = makeNode(AlterTableCmd);
1593 n->subtype = AT_EnableTrig;
1597 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1598 | ENABLE_P ALWAYS TRIGGER name
1600 AlterTableCmd *n = makeNode(AlterTableCmd);
1601 n->subtype = AT_EnableAlwaysTrig;
1605 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1606 | ENABLE_P REPLICA TRIGGER name
1608 AlterTableCmd *n = makeNode(AlterTableCmd);
1609 n->subtype = AT_EnableReplicaTrig;
1613 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1614 | ENABLE_P TRIGGER ALL
1616 AlterTableCmd *n = makeNode(AlterTableCmd);
1617 n->subtype = AT_EnableTrigAll;
1620 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1621 | ENABLE_P TRIGGER USER
1623 AlterTableCmd *n = makeNode(AlterTableCmd);
1624 n->subtype = AT_EnableTrigUser;
1627 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1628 | DISABLE_P TRIGGER name
1630 AlterTableCmd *n = makeNode(AlterTableCmd);
1631 n->subtype = AT_DisableTrig;
1635 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1636 | DISABLE_P TRIGGER ALL
1638 AlterTableCmd *n = makeNode(AlterTableCmd);
1639 n->subtype = AT_DisableTrigAll;
1642 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1643 | DISABLE_P TRIGGER USER
1645 AlterTableCmd *n = makeNode(AlterTableCmd);
1646 n->subtype = AT_DisableTrigUser;
1649 /* ALTER TABLE <name> ENABLE RULE <rule> */
1650 | ENABLE_P RULE name
1652 AlterTableCmd *n = makeNode(AlterTableCmd);
1653 n->subtype = AT_EnableRule;
1657 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1658 | ENABLE_P ALWAYS RULE name
1660 AlterTableCmd *n = makeNode(AlterTableCmd);
1661 n->subtype = AT_EnableAlwaysRule;
1665 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1666 | ENABLE_P REPLICA RULE name
1668 AlterTableCmd *n = makeNode(AlterTableCmd);
1669 n->subtype = AT_EnableReplicaRule;
1673 /* ALTER TABLE <name> DISABLE RULE <rule> */
1674 | DISABLE_P RULE name
1676 AlterTableCmd *n = makeNode(AlterTableCmd);
1677 n->subtype = AT_DisableRule;
1681 /* ALTER TABLE <name> INHERIT <parent> */
1682 | INHERIT qualified_name
1684 AlterTableCmd *n = makeNode(AlterTableCmd);
1685 n->subtype = AT_AddInherit;
1686 n->def = (Node *) $2;
1689 /* ALTER TABLE <name> NO INHERIT <parent> */
1690 | NO INHERIT qualified_name
1692 AlterTableCmd *n = makeNode(AlterTableCmd);
1693 n->subtype = AT_DropInherit;
1694 n->def = (Node *) $3;
1697 /* ALTER TABLE <name> OWNER TO RoleId */
1700 AlterTableCmd *n = makeNode(AlterTableCmd);
1701 n->subtype = AT_ChangeOwner;
1705 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1706 | SET TABLESPACE name
1708 AlterTableCmd *n = makeNode(AlterTableCmd);
1709 n->subtype = AT_SetTableSpace;
1713 /* ALTER TABLE <name> SET (...) */
1716 AlterTableCmd *n = makeNode(AlterTableCmd);
1717 n->subtype = AT_SetRelOptions;
1718 n->def = (Node *)$2;
1721 /* ALTER TABLE <name> RESET (...) */
1724 AlterTableCmd *n = makeNode(AlterTableCmd);
1725 n->subtype = AT_ResetRelOptions;
1726 n->def = (Node *)$2;
1731 alter_column_default:
1732 SET DEFAULT a_expr { $$ = $3; }
1733 | DROP DEFAULT { $$ = NULL; }
1737 CASCADE { $$ = DROP_CASCADE; }
1738 | RESTRICT { $$ = DROP_RESTRICT; }
1739 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1743 USING a_expr { $$ = $2; }
1744 | /* EMPTY */ { $$ = NULL; }
1749 /*****************************************************************************
1752 * close <portalname>
1754 *****************************************************************************/
1759 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1765 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1766 n->portalname = NULL;
1772 /*****************************************************************************
1775 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1777 * BINARY, OIDS, and DELIMITERS kept in old locations
1778 * for backward compatibility. 2002-06-18
1780 * COPY ( SELECT ... ) TO file [WITH options]
1781 * This form doesn't have the backwards-compatible option
1784 *****************************************************************************/
1786 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1787 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1789 CopyStmt *n = makeNode(CopyStmt);
1797 /* Concatenate user-supplied flags */
1799 n->options = lappend(n->options, $2);
1801 n->options = lappend(n->options, $5);
1803 n->options = lappend(n->options, $8);
1805 n->options = list_concat(n->options, $10);
1808 | COPY select_with_parens TO copy_file_name opt_with
1811 CopyStmt *n = makeNode(CopyStmt);
1824 | TO { $$ = FALSE; }
1828 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1829 * used depends on the direction. (It really doesn't make sense to copy from
1830 * stdout. We silently correct the "typo".) - AY 9/94
1834 | STDIN { $$ = NULL; }
1835 | STDOUT { $$ = NULL; }
1841 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1842 | /* EMPTY */ { $$ = NIL; }
1849 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1853 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1855 | DELIMITER opt_as Sconst
1857 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1859 | NULL_P opt_as Sconst
1861 $$ = makeDefElem("null", (Node *)makeString($3));
1865 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1869 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1871 | QUOTE opt_as Sconst
1873 $$ = makeDefElem("quote", (Node *)makeString($3));
1875 | ESCAPE opt_as Sconst
1877 $$ = makeDefElem("escape", (Node *)makeString($3));
1879 | FORCE QUOTE columnList
1881 $$ = makeDefElem("force_quote", (Node *)$3);
1883 | FORCE NOT NULL_P columnList
1885 $$ = makeDefElem("force_notnull", (Node *)$4);
1889 /* The following exist for backward compatibility */
1894 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1896 | /*EMPTY*/ { $$ = NULL; }
1902 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1904 | /*EMPTY*/ { $$ = NULL; }
1908 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1909 opt_using DELIMITERS Sconst
1911 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1913 | /*EMPTY*/ { $$ = NULL; }
1922 /*****************************************************************************
1925 * CREATE TABLE relname
1927 *****************************************************************************/
1929 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1930 OptInherit OptWith OnCommitOption OptTableSpace
1932 CreateStmt *n = makeNode(CreateStmt);
1936 n->inhRelations = $8;
1937 n->constraints = NIL;
1940 n->tablespacename = $11;
1943 | CREATE OptTemp TABLE qualified_name OF qualified_name
1944 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1946 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1947 * by our inheritance capabilities. Let's try it...
1949 CreateStmt *n = makeNode(CreateStmt);
1953 n->inhRelations = list_make1($6);
1954 n->constraints = NIL;
1957 n->tablespacename = $12;
1963 * Redundancy here is needed to avoid shift/reduce conflicts,
1964 * since TEMP is not a reserved word. See also OptTempTableName.
1966 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1967 * the LOCAL keyword is really meaningless.
1969 OptTemp: TEMPORARY { $$ = TRUE; }
1970 | TEMP { $$ = TRUE; }
1971 | LOCAL TEMPORARY { $$ = TRUE; }
1972 | LOCAL TEMP { $$ = TRUE; }
1973 | GLOBAL TEMPORARY { $$ = TRUE; }
1974 | GLOBAL TEMP { $$ = TRUE; }
1975 | /*EMPTY*/ { $$ = FALSE; }
1978 OptTableElementList:
1979 TableElementList { $$ = $1; }
1980 | /*EMPTY*/ { $$ = NIL; }
1986 $$ = list_make1($1);
1988 | TableElementList ',' TableElement
1990 $$ = lappend($1, $3);
1995 columnDef { $$ = $1; }
1996 | TableLikeClause { $$ = $1; }
1997 | TableConstraint { $$ = $1; }
2000 columnDef: ColId Typename ColQualList
2002 ColumnDef *n = makeNode(ColumnDef);
2005 n->constraints = $3;
2012 ColQualList ColConstraint { $$ = lappend($1, $2); }
2013 | /*EMPTY*/ { $$ = NIL; }
2017 CONSTRAINT name ColConstraintElem
2019 switch (nodeTag($3))
2023 Constraint *n = (Constraint *)$3;
2027 case T_FkConstraint:
2029 FkConstraint *n = (FkConstraint *)$3;
2030 n->constr_name = $2;
2038 | ColConstraintElem { $$ = $1; }
2039 | ConstraintAttr { $$ = $1; }
2042 /* DEFAULT NULL is already the default for Postgres.
2043 * But define it here and carry it forward into the system
2044 * to make it explicit.
2045 * - thomas 1998-09-13
2047 * WITH NULL and NULL are not SQL92-standard syntax elements,
2048 * so leave them out. Use DEFAULT NULL to explicitly indicate
2049 * that a column may have that value. WITH NULL leads to
2050 * shift/reduce conflicts with WITH TIME ZONE anyway.
2051 * - thomas 1999-01-08
2053 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2054 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2055 * or be part of a_expr NOT LIKE or similar constructs).
2060 Constraint *n = makeNode(Constraint);
2061 n->contype = CONSTR_NOTNULL;
2064 n->cooked_expr = NULL;
2066 n->indexspace = NULL;
2071 Constraint *n = makeNode(Constraint);
2072 n->contype = CONSTR_NULL;
2075 n->cooked_expr = NULL;
2077 n->indexspace = NULL;
2080 | UNIQUE opt_definition OptConsTableSpace
2082 Constraint *n = makeNode(Constraint);
2083 n->contype = CONSTR_UNIQUE;
2086 n->cooked_expr = NULL;
2092 | PRIMARY KEY opt_definition OptConsTableSpace
2094 Constraint *n = makeNode(Constraint);
2095 n->contype = CONSTR_PRIMARY;
2098 n->cooked_expr = NULL;
2104 | CHECK '(' a_expr ')'
2106 Constraint *n = makeNode(Constraint);
2107 n->contype = CONSTR_CHECK;
2110 n->cooked_expr = NULL;
2112 n->indexspace = NULL;
2117 Constraint *n = makeNode(Constraint);
2118 n->contype = CONSTR_DEFAULT;
2121 n->cooked_expr = NULL;
2123 n->indexspace = NULL;
2126 | REFERENCES qualified_name opt_column_list key_match key_actions
2128 FkConstraint *n = makeNode(FkConstraint);
2129 n->constr_name = NULL;
2133 n->fk_matchtype = $4;
2134 n->fk_upd_action = (char) ($5 >> 8);
2135 n->fk_del_action = (char) ($5 & 0xFF);
2136 n->deferrable = FALSE;
2137 n->initdeferred = FALSE;
2143 * ConstraintAttr represents constraint attributes, which we parse as if
2144 * they were independent constraint clauses, in order to avoid shift/reduce
2145 * conflicts (since NOT might start either an independent NOT NULL clause
2146 * or an attribute). parse_utilcmd.c is responsible for attaching the
2147 * attribute information to the preceding "real" constraint node, and for
2148 * complaining if attribute clauses appear in the wrong place or wrong
2151 * See also ConstraintAttributeSpec, which can be used in places where
2152 * there is no parsing conflict.
2157 Constraint *n = makeNode(Constraint);
2158 n->contype = CONSTR_ATTR_DEFERRABLE;
2163 Constraint *n = makeNode(Constraint);
2164 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2167 | INITIALLY DEFERRED
2169 Constraint *n = makeNode(Constraint);
2170 n->contype = CONSTR_ATTR_DEFERRED;
2173 | INITIALLY IMMEDIATE
2175 Constraint *n = makeNode(Constraint);
2176 n->contype = CONSTR_ATTR_IMMEDIATE;
2183 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2184 * This seems to be a poor man's inheritance capability, with the resulting
2185 * tables completely decoupled except for the original commonality in definitions.
2187 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2188 * which is a part of SQL:2003.
2191 LIKE qualified_name TableLikeOptionList
2193 InhRelation *n = makeNode(InhRelation);
2200 TableLikeOptionList:
2201 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2202 | /* EMPTY */ { $$ = NIL; }
2206 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2207 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2208 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2209 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2210 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2211 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2215 /* ConstraintElem specifies constraint syntax which is not embedded into
2216 * a column definition. ColConstraintElem specifies the embedded form.
2217 * - thomas 1997-12-03
2220 CONSTRAINT name ConstraintElem
2222 switch (nodeTag($3))
2226 Constraint *n = (Constraint *)$3;
2230 case T_FkConstraint:
2232 FkConstraint *n = (FkConstraint *)$3;
2233 n->constr_name = $2;
2241 | ConstraintElem { $$ = $1; }
2245 CHECK '(' a_expr ')'
2247 Constraint *n = makeNode(Constraint);
2248 n->contype = CONSTR_CHECK;
2251 n->cooked_expr = NULL;
2252 n->indexspace = NULL;
2255 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2257 Constraint *n = makeNode(Constraint);
2258 n->contype = CONSTR_UNIQUE;
2261 n->cooked_expr = NULL;
2267 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2269 Constraint *n = makeNode(Constraint);
2270 n->contype = CONSTR_PRIMARY;
2273 n->cooked_expr = NULL;
2279 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2280 opt_column_list key_match key_actions ConstraintAttributeSpec
2282 FkConstraint *n = makeNode(FkConstraint);
2283 n->constr_name = NULL;
2287 n->fk_matchtype = $9;
2288 n->fk_upd_action = (char) ($10 >> 8);
2289 n->fk_del_action = (char) ($10 & 0xFF);
2290 n->deferrable = ($11 & 1) != 0;
2291 n->initdeferred = ($11 & 2) != 0;
2297 '(' columnList ')' { $$ = $2; }
2298 | /*EMPTY*/ { $$ = NIL; }
2302 columnElem { $$ = list_make1($1); }
2303 | columnList ',' columnElem { $$ = lappend($1, $3); }
2308 $$ = (Node *) makeString($1);
2312 key_match: MATCH FULL
2314 $$ = FKCONSTR_MATCH_FULL;
2319 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2320 errmsg("MATCH PARTIAL not yet implemented"),
2321 scanner_errposition(@1)));
2322 $$ = FKCONSTR_MATCH_PARTIAL;
2326 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2330 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2335 * We combine the update and delete actions into one value temporarily
2336 * for simplicity of parsing, and then break them down again in the
2337 * calling production. update is in the left 8 bits, delete in the right.
2338 * Note that NOACTION is the default.
2342 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2344 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2345 | key_update key_delete
2346 { $$ = ($1 << 8) | ($2 & 0xFF); }
2347 | key_delete key_update
2348 { $$ = ($2 << 8) | ($1 & 0xFF); }
2350 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2353 key_update: ON UPDATE key_action { $$ = $3; }
2356 key_delete: ON DELETE_P key_action { $$ = $3; }
2360 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2361 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2362 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2363 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2364 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2367 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2368 | /*EMPTY*/ { $$ = NIL; }
2371 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2373 WITH definition { $$ = $2; }
2374 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2375 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2376 | /*EMPTY*/ { $$ = NIL; }
2379 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2380 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2381 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2382 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2385 OptTableSpace: TABLESPACE name { $$ = $2; }
2386 | /*EMPTY*/ { $$ = NULL; }
2389 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2390 | /*EMPTY*/ { $$ = NULL; }
2395 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2400 CREATE OptTemp TABLE create_as_target AS SelectStmt
2403 * When the SelectStmt is a set-operation tree, we must
2404 * stuff the INTO information into the leftmost component
2405 * Select, because that's where analyze.c will expect
2406 * to find it. Similarly, the output column names must
2407 * be attached to that Select's target list.
2409 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2410 if (n->intoClause != NULL)
2412 (errcode(ERRCODE_SYNTAX_ERROR),
2413 errmsg("CREATE TABLE AS cannot specify INTO"),
2414 scanner_errposition(exprLocation((Node *) n->intoClause))));
2415 $4->rel->istemp = $2;
2422 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2424 $$ = makeNode(IntoClause);
2429 $$->tableSpaceName = $5;
2434 '(' CreateAsList ')' { $$ = $2; }
2435 | /*EMPTY*/ { $$ = NIL; }
2439 CreateAsElement { $$ = list_make1($1); }
2440 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2446 ColumnDef *n = makeNode(ColumnDef);
2451 n->is_not_null = false;
2452 n->raw_default = NULL;
2453 n->cooked_default = NULL;
2454 n->constraints = NIL;
2460 /*****************************************************************************
2463 * CREATE SEQUENCE seqname
2464 * ALTER SEQUENCE seqname
2466 *****************************************************************************/
2469 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2471 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2480 ALTER SEQUENCE relation_expr SeqOptList
2482 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2489 OptSeqOptList: SeqOptList { $$ = $1; }
2490 | /*EMPTY*/ { $$ = NIL; }
2493 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2494 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2497 SeqOptElem: CACHE NumericOnly
2499 $$ = makeDefElem("cache", (Node *)$2);
2503 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2507 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2509 | INCREMENT opt_by NumericOnly
2511 $$ = makeDefElem("increment", (Node *)$3);
2513 | MAXVALUE NumericOnly
2515 $$ = makeDefElem("maxvalue", (Node *)$2);
2517 | MINVALUE NumericOnly
2519 $$ = makeDefElem("minvalue", (Node *)$2);
2523 $$ = makeDefElem("maxvalue", NULL);
2527 $$ = makeDefElem("minvalue", NULL);
2531 $$ = makeDefElem("owned_by", (Node *)$3);
2533 | START opt_with NumericOnly
2535 $$ = makeDefElem("start", (Node *)$3);
2539 $$ = makeDefElem("restart", NULL);
2541 | RESTART opt_with NumericOnly
2543 $$ = makeDefElem("restart", (Node *)$3);
2552 FloatOnly { $$ = $1; }
2553 | IntegerOnly { $$ = $1; }
2556 FloatOnly: FCONST { $$ = makeFloat($1); }
2564 IntegerOnly: SignedIconst { $$ = makeInteger($1); };
2567 /*****************************************************************************
2570 * CREATE PROCEDURAL LANGUAGE ...
2571 * DROP PROCEDURAL LANGUAGE ...
2573 *****************************************************************************/
2576 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2578 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2580 /* parameters are all to be supplied by system */
2582 n->plvalidator = NIL;
2583 n->pltrusted = false;
2586 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2587 HANDLER handler_name opt_validator opt_lancompiler
2589 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2592 n->plvalidator = $8;
2594 /* LANCOMPILER is now ignored entirely */
2600 TRUSTED { $$ = TRUE; }
2601 | /*EMPTY*/ { $$ = FALSE; }
2604 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2605 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2606 * Work around by using simple names, instead.
2609 name { $$ = list_make1(makeString($1)); }
2610 | name attrs { $$ = lcons(makeString($1), $2); }
2614 VALIDATOR handler_name { $$ = $2; }
2615 | /*EMPTY*/ { $$ = NIL; }
2619 LANCOMPILER Sconst { $$ = $2; }
2620 | /*EMPTY*/ { $$ = NULL; }
2624 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2626 DropPLangStmt *n = makeNode(DropPLangStmt);
2629 n->missing_ok = false;
2632 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2634 DropPLangStmt *n = makeNode(DropPLangStmt);
2637 n->missing_ok = true;
2647 /*****************************************************************************
2650 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2652 *****************************************************************************/
2654 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2656 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2657 n->tablespacename = $3;
2664 OptTableSpaceOwner: OWNER name { $$ = $2; }
2665 | /*EMPTY */ { $$ = NULL; }
2668 /*****************************************************************************
2671 * DROP TABLESPACE <tablespace>
2673 * No need for drop behaviour as we cannot implement dependencies for
2674 * objects in other databases; we can only support RESTRICT.
2676 ****************************************************************************/
2678 DropTableSpaceStmt: DROP TABLESPACE name
2680 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2681 n->tablespacename = $3;
2682 n->missing_ok = false;
2685 | DROP TABLESPACE IF_P EXISTS name
2687 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2688 n->tablespacename = $5;
2689 n->missing_ok = true;
2694 /*****************************************************************************
2697 * CREATE TRIGGER ...
2700 *****************************************************************************/
2703 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2704 qualified_name TriggerForSpec EXECUTE PROCEDURE
2705 func_name '(' TriggerFuncArgs ')'
2707 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2714 memcpy(n->actions, $5, 4);
2715 n->isconstraint = FALSE;
2716 n->deferrable = FALSE;
2717 n->initdeferred = FALSE;
2718 n->constrrel = NULL;
2721 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2722 qualified_name OptConstrFromTable
2723 ConstraintAttributeSpec
2724 FOR EACH ROW EXECUTE PROCEDURE
2725 func_name '(' TriggerFuncArgs ')'
2727 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2734 memcpy(n->actions, $6, 4);
2735 n->isconstraint = TRUE;
2736 n->deferrable = ($10 & 1) != 0;
2737 n->initdeferred = ($10 & 2) != 0;
2745 BEFORE { $$ = TRUE; }
2746 | AFTER { $$ = FALSE; }
2752 char *e = palloc(4);
2753 e[0] = $1; e[1] = '\0';
2756 | TriggerOneEvent OR TriggerOneEvent
2758 char *e = palloc(4);
2759 e[0] = $1; e[1] = $3; e[2] = '\0';
2762 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2764 char *e = palloc(4);
2765 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2771 INSERT { $$ = 'i'; }
2772 | DELETE_P { $$ = 'd'; }
2773 | UPDATE { $$ = 'u'; }
2774 | TRUNCATE { $$ = 't'; }
2778 FOR TriggerForOpt TriggerForType
2785 * If ROW/STATEMENT not specified, default to
2786 * STATEMENT, per SQL
2799 | STATEMENT { $$ = FALSE; }
2803 TriggerFuncArg { $$ = list_make1($1); }
2804 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2805 | /*EMPTY*/ { $$ = NIL; }
2812 snprintf(buf, sizeof(buf), "%d", $1);
2813 $$ = makeString(pstrdup(buf));
2815 | FCONST { $$ = makeString($1); }
2816 | Sconst { $$ = makeString($1); }
2817 | BCONST { $$ = makeString($1); }
2818 | XCONST { $$ = makeString($1); }
2819 | ColId { $$ = makeString($1); }
2823 FROM qualified_name { $$ = $2; }
2824 | /*EMPTY*/ { $$ = NULL; }
2827 ConstraintAttributeSpec:
2828 ConstraintDeferrabilitySpec
2830 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2832 if ($1 == 0 && $2 != 0)
2834 (errcode(ERRCODE_SYNTAX_ERROR),
2835 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2836 scanner_errposition(@1)));
2839 | ConstraintTimeSpec
2846 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2848 if ($2 == 0 && $1 != 0)
2850 (errcode(ERRCODE_SYNTAX_ERROR),
2851 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2852 scanner_errposition(@1)));
2859 ConstraintDeferrabilitySpec:
2860 NOT DEFERRABLE { $$ = 0; }
2861 | DEFERRABLE { $$ = 1; }
2865 INITIALLY IMMEDIATE { $$ = 0; }
2866 | INITIALLY DEFERRED { $$ = 2; }
2871 DROP TRIGGER name ON qualified_name opt_drop_behavior
2873 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2877 n->removeType = OBJECT_TRIGGER;
2878 n->missing_ok = false;
2881 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
2883 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2887 n->removeType = OBJECT_TRIGGER;
2888 n->missing_ok = true;
2894 /*****************************************************************************
2897 * CREATE ASSERTION ...
2898 * DROP ASSERTION ...
2900 *****************************************************************************/
2903 CREATE ASSERTION name CHECK '(' a_expr ')'
2904 ConstraintAttributeSpec
2906 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2908 n->args = list_make1($6);
2909 n->isconstraint = TRUE;
2910 n->deferrable = ($8 & 1) != 0;
2911 n->initdeferred = ($8 & 2) != 0;
2914 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2915 errmsg("CREATE ASSERTION is not yet implemented")));
2922 DROP ASSERTION name opt_drop_behavior
2924 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2928 n->removeType = OBJECT_TRIGGER; /* XXX */
2930 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2931 errmsg("DROP ASSERTION is not yet implemented")));
2937 /*****************************************************************************
2940 * define (aggregate,operator,type)
2942 *****************************************************************************/
2945 CREATE AGGREGATE func_name aggr_args definition
2947 DefineStmt *n = makeNode(DefineStmt);
2948 n->kind = OBJECT_AGGREGATE;
2949 n->oldstyle = false;
2955 | CREATE AGGREGATE func_name old_aggr_definition
2957 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
2958 DefineStmt *n = makeNode(DefineStmt);
2959 n->kind = OBJECT_AGGREGATE;
2966 | CREATE OPERATOR any_operator definition
2968 DefineStmt *n = makeNode(DefineStmt);
2969 n->kind = OBJECT_OPERATOR;
2970 n->oldstyle = false;
2976 | CREATE TYPE_P any_name definition
2978 DefineStmt *n = makeNode(DefineStmt);
2979 n->kind = OBJECT_TYPE;
2980 n->oldstyle = false;
2986 | CREATE TYPE_P any_name
2988 /* Shell type (identified by lack of definition) */
2989 DefineStmt *n = makeNode(DefineStmt);
2990 n->kind = OBJECT_TYPE;
2991 n->oldstyle = false;
2994 n->definition = NIL;
2997 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2999 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3000 RangeVar *r = makeNode(RangeVar);
3002 /* can't use qualified_name, sigh */
3003 switch (list_length($3))
3006 r->catalogname = NULL;
3007 r->schemaname = NULL;
3008 r->relname = strVal(linitial($3));
3011 r->catalogname = NULL;
3012 r->schemaname = strVal(linitial($3));
3013 r->relname = strVal(lsecond($3));
3016 r->catalogname = strVal(linitial($3));
3017 r->schemaname = strVal(lsecond($3));
3018 r->relname = strVal(lthird($3));
3022 (errcode(ERRCODE_SYNTAX_ERROR),
3023 errmsg("improper qualified name (too many dotted names): %s",
3024 NameListToString($3)),
3025 scanner_errposition(@3)));
3033 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3035 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3040 | CREATE TEXT_P SEARCH PARSER any_name definition
3042 DefineStmt *n = makeNode(DefineStmt);
3043 n->kind = OBJECT_TSPARSER;
3049 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3051 DefineStmt *n = makeNode(DefineStmt);
3052 n->kind = OBJECT_TSDICTIONARY;
3058 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3060 DefineStmt *n = makeNode(DefineStmt);
3061 n->kind = OBJECT_TSTEMPLATE;
3067 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3069 DefineStmt *n = makeNode(DefineStmt);
3070 n->kind = OBJECT_TSCONFIGURATION;
3078 definition: '(' def_list ')' { $$ = $2; }
3081 def_list: def_elem { $$ = list_make1($1); }
3082 | def_list ',' def_elem { $$ = lappend($1, $3); }
3085 def_elem: ColLabel '=' def_arg
3087 $$ = makeDefElem($1, (Node *)$3);
3091 $$ = makeDefElem($1, NULL);
3095 /* Note: any simple identifier will be returned as a type name! */
3096 def_arg: func_type { $$ = (Node *)$1; }
3097 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3098 | qual_all_Op { $$ = (Node *)$1; }
3099 | NumericOnly { $$ = (Node *)$1; }
3100 | Sconst { $$ = (Node *)makeString($1); }
3103 aggr_args: '(' type_list ')' { $$ = $2; }
3104 | '(' '*' ')' { $$ = NIL; }
3107 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3110 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3111 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3114 old_aggr_elem: IDENT '=' def_arg
3116 $$ = makeDefElem($1, (Node *)$3);
3120 enum_val_list: Sconst
3121 { $$ = list_make1(makeString($1)); }
3122 | enum_val_list ',' Sconst
3123 { $$ = lappend($1, makeString($3)); }
3127 /*****************************************************************************
3130 * CREATE OPERATOR CLASS ...
3131 * CREATE OPERATOR FAMILY ...
3132 * ALTER OPERATOR FAMILY ...
3133 * DROP OPERATOR CLASS ...
3134 * DROP OPERATOR FAMILY ...
3136 *****************************************************************************/
3139 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3140 USING access_method opt_opfamily AS opclass_item_list
3142 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3143 n->opclassname = $4;
3147 n->opfamilyname = $11;
3154 opclass_item { $$ = list_make1($1); }
3155 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3159 OPERATOR Iconst any_operator opt_recheck
3161 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3162 n->itemtype = OPCLASS_ITEM_OPERATOR;
3168 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3170 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3171 n->itemtype = OPCLASS_ITEM_OPERATOR;
3177 | FUNCTION Iconst func_name func_args
3179 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3180 n->itemtype = OPCLASS_ITEM_FUNCTION;
3182 n->args = extractArgTypes($4);
3186 | FUNCTION Iconst '(' type_list ')' func_name func_args
3188 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3189 n->itemtype = OPCLASS_ITEM_FUNCTION;
3191 n->args = extractArgTypes($7);
3198 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3199 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3205 opt_default: DEFAULT { $$ = TRUE; }
3206 | /*EMPTY*/ { $$ = FALSE; }
3209 opt_opfamily: FAMILY any_name { $$ = $2; }
3210 | /*EMPTY*/ { $$ = NIL; }
3213 opt_recheck: RECHECK
3216 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3217 errmsg("RECHECK is no longer supported"),
3218 errhint("Update your data type."),
3219 scanner_errposition(@1)));
3222 | /*EMPTY*/ { $$ = FALSE; }
3227 CREATE OPERATOR FAMILY any_name USING access_method
3229 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3230 n->opfamilyname = $4;
3237 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3239 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3240 n->opfamilyname = $4;
3246 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3248 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3249 n->opfamilyname = $4;
3258 opclass_drop { $$ = list_make1($1); }
3259 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3263 OPERATOR Iconst '(' type_list ')'
3265 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3266 n->itemtype = OPCLASS_ITEM_OPERATOR;
3271 | FUNCTION Iconst '(' type_list ')'
3273 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3274 n->itemtype = OPCLASS_ITEM_FUNCTION;
3283 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3285 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3286 n->opclassname = $4;
3289 n->missing_ok = false;
3292 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3294 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3295 n->opclassname = $6;
3298 n->missing_ok = true;
3304 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3306 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3307 n->opfamilyname = $4;
3310 n->missing_ok = false;
3313 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3315 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3316 n->opfamilyname = $6;
3319 n->missing_ok = true;
3325 /*****************************************************************************
3329 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3330 * REASSIGN OWNED BY username [, username ...] TO username
3332 *****************************************************************************/
3334 DROP OWNED BY name_list opt_drop_behavior
3336 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3344 REASSIGN OWNED BY name_list TO name
3346 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3353 /*****************************************************************************
3357 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3358 * [ RESTRICT | CASCADE ]
3360 *****************************************************************************/
3362 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3364 DropStmt *n = makeNode(DropStmt);
3366 n->missing_ok = TRUE;
3371 | DROP drop_type any_name_list opt_drop_behavior
3373 DropStmt *n = makeNode(DropStmt);
3375 n->missing_ok = FALSE;
3383 drop_type: TABLE { $$ = OBJECT_TABLE; }
3384 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3385 | VIEW { $$ = OBJECT_VIEW; }
3386 | INDEX { $$ = OBJECT_INDEX; }
3387 | TYPE_P { $$ = OBJECT_TYPE; }
3388 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3389 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3390 | SCHEMA { $$ = OBJECT_SCHEMA; }
3391 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3392 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3393 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3394 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3398 any_name { $$ = list_make1($1); }
3399 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3402 any_name: ColId { $$ = list_make1(makeString($1)); }
3403 | ColId attrs { $$ = lcons(makeString($1), $2); }
3406 attrs: '.' attr_name
3407 { $$ = list_make1(makeString($2)); }
3408 | attrs '.' attr_name
3409 { $$ = lappend($1, makeString($3)); }
3413 /*****************************************************************************
3416 * truncate table relname1, relname2, ...
3418 *****************************************************************************/
3421 TRUNCATE opt_table qualified_name_list opt_restart_seqs opt_drop_behavior
3423 TruncateStmt *n = makeNode(TruncateStmt);
3425 n->restart_seqs = $4;
3432 CONTINUE_P IDENTITY_P { $$ = false; }
3433 | RESTART IDENTITY_P { $$ = true; }
3434 | /* EMPTY */ { $$ = false; }
3437 /*****************************************************************************
3439 * The COMMENT ON statement can take different forms based upon the type of
3440 * the object associated with the comment. The form of the statement is:
3442 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3443 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3444 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3445 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3446 * TEXT SEARCH TEMPLATE |
3447 * TEXT SEARCH CONFIGURATION ] <objname> |
3448 * AGGREGATE <aggname> (arg1, ...) |
3449 * FUNCTION <funcname> (arg1, arg2, ...) |
3450 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3451 * TRIGGER <triggername> ON <relname> |
3452 * CONSTRAINT <constraintname> ON <relname> |
3453 * RULE <rulename> ON <relname> ]
3456 *****************************************************************************/
3459 COMMENT ON comment_type any_name IS comment_text
3461 CommentStmt *n = makeNode(CommentStmt);
3468 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3470 CommentStmt *n = makeNode(CommentStmt);
3471 n->objtype = OBJECT_AGGREGATE;
3477 | COMMENT ON FUNCTION func_name func_args IS comment_text
3479 CommentStmt *n = makeNode(CommentStmt);
3480 n->objtype = OBJECT_FUNCTION;
3482 n->objargs = extractArgTypes($5);
3486 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3488 CommentStmt *n = makeNode(CommentStmt);
3489 n->objtype = OBJECT_OPERATOR;
3495 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3497 CommentStmt *n = makeNode(CommentStmt);
3498 n->objtype = OBJECT_CONSTRAINT;
3499 n->objname = lappend($6, makeString($4));
3504 | COMMENT ON RULE name ON any_name IS comment_text
3506 CommentStmt *n = makeNode(CommentStmt);
3507 n->objtype = OBJECT_RULE;
3508 n->objname = lappend($6, makeString($4));
3513 | COMMENT ON RULE name IS comment_text
3515 /* Obsolete syntax supported for awhile for compatibility */
3516 CommentStmt *n = makeNode(CommentStmt);
3517 n->objtype = OBJECT_RULE;
3518 n->objname = list_make1(makeString($4));
3523 | COMMENT ON TRIGGER name ON any_name IS comment_text
3525 CommentStmt *n = makeNode(CommentStmt);
3526 n->objtype = OBJECT_TRIGGER;
3527 n->objname = lappend($6, makeString($4));
3532 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3534 CommentStmt *n = makeNode(CommentStmt);
3535 n->objtype = OBJECT_OPCLASS;
3537 n->objargs = list_make1(makeString($7));
3541 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3543 CommentStmt *n = makeNode(CommentStmt);
3544 n->objtype = OBJECT_OPFAMILY;
3546 n->objargs = list_make1(makeString($7));
3550 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3552 CommentStmt *n = makeNode(CommentStmt);
3553 n->objtype = OBJECT_LARGEOBJECT;
3554 n->objname = list_make1($5);
3559 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3561 CommentStmt *n = makeNode(CommentStmt);
3562 n->objtype = OBJECT_CAST;
3563 n->objname = list_make1($5);
3564 n->objargs = list_make1($7);
3568 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3570 CommentStmt *n = makeNode(CommentStmt);
3571 n->objtype = OBJECT_LANGUAGE;
3577 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3579 CommentStmt *n = makeNode(CommentStmt);
3580 n->objtype = OBJECT_TSPARSER;
3585 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3587 CommentStmt *n = makeNode(CommentStmt);
3588 n->objtype = OBJECT_TSDICTIONARY;
3593 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3595 CommentStmt *n = makeNode(CommentStmt);
3596 n->objtype = OBJECT_TSTEMPLATE;
3601 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3603 CommentStmt *n = makeNode(CommentStmt);
3604 n->objtype = OBJECT_TSCONFIGURATION;
3612 COLUMN { $$ = OBJECT_COLUMN; }
3613 | DATABASE { $$ = OBJECT_DATABASE; }
3614 | SCHEMA { $$ = OBJECT_SCHEMA; }
3615 | INDEX { $$ = OBJECT_INDEX; }
3616 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3617 | TABLE { $$ = OBJECT_TABLE; }
3618 | DOMAIN_P { $$ = OBJECT_TYPE; }
3619 | TYPE_P { $$ = OBJECT_TYPE; }
3620 | VIEW { $$ = OBJECT_VIEW; }
3621 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3622 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3623 | ROLE { $$ = OBJECT_ROLE; }
3628 | NULL_P { $$ = NULL; }
3631 /*****************************************************************************
3636 *****************************************************************************/
3638 FetchStmt: FETCH fetch_direction from_in name
3640 FetchStmt *n = (FetchStmt *) $2;
3647 FetchStmt *n = makeNode(FetchStmt);
3648 n->direction = FETCH_FORWARD;
3654 | MOVE fetch_direction from_in name
3656 FetchStmt *n = (FetchStmt *) $2;
3663 FetchStmt *n = makeNode(FetchStmt);
3664 n->direction = FETCH_FORWARD;
3675 FetchStmt *n = makeNode(FetchStmt);
3676 n->direction = FETCH_FORWARD;
3682 FetchStmt *n = makeNode(FetchStmt);
3683 n->direction = FETCH_FORWARD;
3689 FetchStmt *n = makeNode(FetchStmt);
3690 n->direction = FETCH_BACKWARD;
3696 FetchStmt *n = makeNode(FetchStmt);
3697 n->direction = FETCH_ABSOLUTE;
3703 FetchStmt *n = makeNode(FetchStmt);
3704 n->direction = FETCH_ABSOLUTE;
3708 | ABSOLUTE_P SignedIconst
3710 FetchStmt *n = makeNode(FetchStmt);
3711 n->direction = FETCH_ABSOLUTE;
3715 | RELATIVE_P SignedIconst
3717 FetchStmt *n = makeNode(FetchStmt);
3718 n->direction = FETCH_RELATIVE;
3724 FetchStmt *n = makeNode(FetchStmt);
3725 n->direction = FETCH_FORWARD;
3731 FetchStmt *n = makeNode(FetchStmt);
3732 n->direction = FETCH_FORWARD;
3733 n->howMany = FETCH_ALL;
3738 FetchStmt *n = makeNode(FetchStmt);
3739 n->direction = FETCH_FORWARD;
3743 | FORWARD SignedIconst
3745 FetchStmt *n = makeNode(FetchStmt);
3746 n->direction = FETCH_FORWARD;
3752 FetchStmt *n = makeNode(FetchStmt);
3753 n->direction = FETCH_FORWARD;
3754 n->howMany = FETCH_ALL;
3759 FetchStmt *n = makeNode(FetchStmt);
3760 n->direction = FETCH_BACKWARD;
3764 | BACKWARD SignedIconst
3766 FetchStmt *n = makeNode(FetchStmt);
3767 n->direction = FETCH_BACKWARD;
3773 FetchStmt *n = makeNode(FetchStmt);
3774 n->direction = FETCH_BACKWARD;
3775 n->howMany = FETCH_ALL;
3785 /*****************************************************************************
3787 * GRANT and REVOKE statements
3789 *****************************************************************************/
3791 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3792 opt_grant_grant_option
3794 GrantStmt *n = makeNode(GrantStmt);
3797 n->objtype = ($4)->objtype;
3798 n->objects = ($4)->objs;
3800 n->grant_option = $7;
3806 REVOKE privileges ON privilege_target
3807 FROM grantee_list opt_drop_behavior
3809 GrantStmt *n = makeNode(GrantStmt);
3810 n->is_grant = false;
3811 n->grant_option = false;
3813 n->objtype = ($4)->objtype;
3814 n->objects = ($4)->objs;
3819 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3820 FROM grantee_list opt_drop_behavior
3822 GrantStmt *n = makeNode(GrantStmt);
3823 n->is_grant = false;
3824 n->grant_option = true;
3826 n->objtype = ($7)->objtype;
3827 n->objects = ($7)->objs;
3836 * A privilege list is represented as a list of strings; the validity of
3837 * the privilege names gets checked at execution. This is a bit annoying
3838 * but we have little choice because of the syntactic conflict with lists
3839 * of role names in GRANT/REVOKE. What's more, we have to call out in
3840 * the "privilege" production any reserved keywords that need to be usable
3841 * as privilege names.
3844 /* either ALL [PRIVILEGES] or a list of individual privileges */
3845 privileges: privilege_list
3853 privilege_list: privilege
3854 { $$ = list_make1(makeString($1)); }
3855 | privilege_list ',' privilege
3856 { $$ = lappend($1, makeString($3)); }
3859 privilege: SELECT { $$ = pstrdup($1); }
3860 | REFERENCES { $$ = pstrdup($1); }
3861 | CREATE { $$ = pstrdup($1); }
3862 | ColId { $$ = $1; }
3866 /* Don't bother trying to fold the first two rules into one using
3867 * opt_table. You're going to get conflicts.
3872 PrivTarget *n = makeNode(PrivTarget);
3873 n->objtype = ACL_OBJECT_RELATION;
3877 | TABLE qualified_name_list
3879 PrivTarget *n = makeNode(PrivTarget);
3880 n->objtype = ACL_OBJECT_RELATION;
3884 | SEQUENCE qualified_name_list
3886 PrivTarget *n = makeNode(PrivTarget);
3887 n->objtype = ACL_OBJECT_SEQUENCE;
3891 | FUNCTION function_with_argtypes_list
3893 PrivTarget *n = makeNode(PrivTarget);
3894 n->objtype = ACL_OBJECT_FUNCTION;
3898 | DATABASE name_list
3900 PrivTarget *n = makeNode(PrivTarget);
3901 n->objtype = ACL_OBJECT_DATABASE;
3905 | LANGUAGE name_list
3907 PrivTarget *n = makeNode(PrivTarget);
3908 n->objtype = ACL_OBJECT_LANGUAGE;
3914 PrivTarget *n = makeNode(PrivTarget);
3915 n->objtype = ACL_OBJECT_NAMESPACE;
3919 | TABLESPACE name_list
3921 PrivTarget *n = makeNode(PrivTarget);
3922 n->objtype = ACL_OBJECT_TABLESPACE;
3930 grantee { $$ = list_make1($1); }
3931 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3936 PrivGrantee *n = makeNode(PrivGrantee);
3937 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3938 if (strcmp($1, "public") == 0)
3946 PrivGrantee *n = makeNode(PrivGrantee);
3947 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3948 if (strcmp($2, "public") == 0)
3957 opt_grant_grant_option:
3958 WITH GRANT OPTION { $$ = TRUE; }
3959 | /*EMPTY*/ { $$ = FALSE; }
3962 function_with_argtypes_list:
3963 function_with_argtypes { $$ = list_make1($1); }
3964 | function_with_argtypes_list ',' function_with_argtypes
3965 { $$ = lappend($1, $3); }
3968 function_with_argtypes:
3971 FuncWithArgs *n = makeNode(FuncWithArgs);
3973 n->funcargs = extractArgTypes($2);
3978 /*****************************************************************************
3980 * GRANT and REVOKE ROLE statements
3982 *****************************************************************************/
3985 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
3987 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3989 n->granted_roles = $2;
3990 n->grantee_roles = $4;
3998 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4000 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4001 n->is_grant = false;
4002 n->admin_opt = false;
4003 n->granted_roles = $2;
4004 n->grantee_roles = $4;
4008 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4010 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4011 n->is_grant = false;
4012 n->admin_opt = true;
4013 n->granted_roles = $5;
4014 n->grantee_roles = $7;
4020 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4021 | /*EMPTY*/ { $$ = FALSE; }
4024 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4025 | /*EMPTY*/ { $$ = NULL; }
4029 /*****************************************************************************
4031 * QUERY: CREATE INDEX
4033 * Note: we can't factor CONCURRENTLY into a separate production without
4034 * making it a reserved word.
4036 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4037 * willing to make TABLESPACE a fully reserved word.
4038 *****************************************************************************/
4040 IndexStmt: CREATE index_opt_unique INDEX index_name
4041 ON qualified_name access_method_clause '(' index_params ')'
4042 opt_definition OptTableSpace where_clause
4044 IndexStmt *n = makeNode(IndexStmt);
4046 n->concurrent = false;
4049 n->accessMethod = $7;
4050 n->indexParams = $9;
4052 n->tableSpace = $12;
4053 n->whereClause = $13;
4056 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4057 ON qualified_name access_method_clause '(' index_params ')'
4058 opt_definition OptTableSpace where_clause
4060 IndexStmt *n = makeNode(IndexStmt);
4062 n->concurrent = true;
4065 n->accessMethod = $8;
4066 n->indexParams = $10;
4068 n->tableSpace = $13;
4069 n->whereClause = $14;
4075 UNIQUE { $$ = TRUE; }
4076 | /*EMPTY*/ { $$ = FALSE; }
4079 access_method_clause:
4080 USING access_method { $$ = $2; }
4081 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4084 index_params: index_elem { $$ = list_make1($1); }
4085 | index_params ',' index_elem { $$ = lappend($1, $3); }
4089 * Index attributes can be either simple column references, or arbitrary
4090 * expressions in parens. For backwards-compatibility reasons, we allow
4091 * an expression that's just a function call to be written without parens.
4093 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4095 $$ = makeNode(IndexElem);
4100 $$->nulls_ordering = $4;
4102 | func_expr opt_class opt_asc_desc opt_nulls_order
4104 $$ = makeNode(IndexElem);
4109 $$->nulls_ordering = $4;
4111 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4113 $$ = makeNode(IndexElem);
4118 $$->nulls_ordering = $6;
4122 opt_class: any_name { $$ = $1; }
4123 | USING any_name { $$ = $2; }
4124 | /*EMPTY*/ { $$ = NIL; }
4127 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4128 | DESC { $$ = SORTBY_DESC; }
4129 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4132 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4133 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4134 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4138 /*****************************************************************************
4141 * create [or replace] function <fname>
4142 * [(<type-1> { , <type-n>})]
4144 * as <filename or code in language as appropriate>
4145 * language <lang> [with parameters]
4147 *****************************************************************************/
4150 CREATE opt_or_replace FUNCTION func_name func_args
4151 RETURNS func_return createfunc_opt_list opt_definition
4153 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4162 | CREATE opt_or_replace FUNCTION func_name func_args
4163 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4165 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4168 n->parameters = mergeTableFuncParameters($5, $9);
4169 n->returnType = TableFuncTypeName($9);
4170 n->returnType->location = @7;
4172 n->withClause = $12;
4175 | CREATE opt_or_replace FUNCTION func_name func_args
4176 createfunc_opt_list opt_definition
4178 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4182 n->returnType = NULL;
4190 OR REPLACE { $$ = TRUE; }
4191 | /*EMPTY*/ { $$ = FALSE; }
4194 func_args: '(' func_args_list ')' { $$ = $2; }
4195 | '(' ')' { $$ = NIL; }
4199 func_arg { $$ = list_make1($1); }
4200 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4204 * The style with arg_class first is SQL99 standard, but Oracle puts
4205 * param_name first; accept both since it's likely people will try both
4206 * anyway. Don't bother trying to save productions by letting arg_class
4207 * have an empty alternative ... you'll get shift/reduce conflicts.
4209 * We can catch over-specified arguments here if we want to,
4210 * but for now better to silently swallow typmod, etc.
4211 * - thomas 2000-03-22
4214 arg_class param_name func_type
4216 FunctionParameter *n = makeNode(FunctionParameter);
4222 | param_name arg_class func_type
4224 FunctionParameter *n = makeNode(FunctionParameter);
4230 | param_name func_type
4232 FunctionParameter *n = makeNode(FunctionParameter);
4235 n->mode = FUNC_PARAM_IN;
4238 | arg_class func_type
4240 FunctionParameter *n = makeNode(FunctionParameter);
4248 FunctionParameter *n = makeNode(FunctionParameter);
4251 n->mode = FUNC_PARAM_IN;
4256 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4257 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4258 | OUT_P { $$ = FUNC_PARAM_OUT; }
4259 | INOUT { $$ = FUNC_PARAM_INOUT; }
4260 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4261 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4265 * Ideally param_name should be ColId, but that causes too many conflicts.
4267 param_name: type_function_name
4273 /* We can catch over-specified results here if we want to,
4274 * but for now better to silently swallow typmod, etc.
4275 * - thomas 2000-03-22
4282 * We would like to make the %TYPE productions here be ColId attrs etc,
4283 * but that causes reduce/reduce conflicts. type_function_name
4284 * is next best choice.
4286 func_type: Typename { $$ = $1; }
4287 | type_function_name attrs '%' TYPE_P
4289 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4290 $$->pct_type = true;
4293 | SETOF type_function_name attrs '%' TYPE_P
4295 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4296 $$->pct_type = true;
4303 createfunc_opt_list:
4304 /* Must be at least one to prevent conflict */
4305 createfunc_opt_item { $$ = list_make1($1); }
4306 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4310 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4312 common_func_opt_item:
4313 CALLED ON NULL_P INPUT_P
4315 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4317 | RETURNS NULL_P ON NULL_P INPUT_P
4319 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4323 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4327 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4331 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4335 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4337 | EXTERNAL SECURITY DEFINER
4339 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4341 | EXTERNAL SECURITY INVOKER
4343 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4347 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4351 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4355 $$ = makeDefElem("cost", (Node *)$2);
4359 $$ = makeDefElem("rows", (Node *)$2);
4363 /* we abuse the normal content of a DefElem here */
4364 $$ = makeDefElem("set", (Node *)$1);
4368 createfunc_opt_item:
4371 $$ = makeDefElem("as", (Node *)$2);
4373 | LANGUAGE ColId_or_Sconst
4375 $$ = makeDefElem("language", (Node *)makeString($2));
4377 | common_func_opt_item
4383 func_as: Sconst { $$ = list_make1(makeString($1)); }
4386 $$ = list_make2(makeString($1), makeString($3));
4391 WITH definition { $$ = $2; }
4392 | /*EMPTY*/ { $$ = NIL; }
4395 table_func_column: param_name func_type
4397 FunctionParameter *n = makeNode(FunctionParameter);
4400 n->mode = FUNC_PARAM_TABLE;
4405 table_func_column_list:
4408 $$ = list_make1($1);
4410 | table_func_column_list ',' table_func_column
4412 $$ = lappend($1, $3);
4416 /*****************************************************************************
4419 * RENAME and OWNER subcommands are already provided by the generic
4420 * ALTER infrastructure, here we just specify alterations that can
4421 * only be applied to functions.
4423 *****************************************************************************/
4425 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4427 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4435 /* At least one option must be specified */
4436 common_func_opt_item { $$ = list_make1($1); }
4437 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4440 /* Ignored, merely for SQL compliance */
4447 /*****************************************************************************
4451 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4452 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4453 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4455 *****************************************************************************/
4458 DROP FUNCTION func_name func_args opt_drop_behavior
4460 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4461 n->kind = OBJECT_FUNCTION;
4463 n->args = extractArgTypes($4);
4465 n->missing_ok = false;
4468 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4470 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4471 n->kind = OBJECT_FUNCTION;
4473 n->args = extractArgTypes($6);
4475 n->missing_ok = true;
4481 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4483 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4484 n->kind = OBJECT_AGGREGATE;
4488 n->missing_ok = false;
4491 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4493 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4494 n->kind = OBJECT_AGGREGATE;
4498 n->missing_ok = true;
4504 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
4506 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4507 n->kind = OBJECT_OPERATOR;
4511 n->missing_ok = false;
4514 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
4516 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4517 n->kind = OBJECT_OPERATOR;
4521 n->missing_ok = true;
4530 (errcode(ERRCODE_SYNTAX_ERROR),
4531 errmsg("missing argument"),
4532 errhint("Use NONE to denote the missing argument of a unary operator."),
4533 scanner_errposition(@3)));
4535 | '(' Typename ',' Typename ')'
4536 { $$ = list_make2($2, $4); }
4537 | '(' NONE ',' Typename ')' /* left unary */
4538 { $$ = list_make2(NULL, $4); }
4539 | '(' Typename ',' NONE ')' /* right unary */
4540 { $$ = list_make2($2, NULL); }
4545 { $$ = list_make1(makeString($1)); }
4546 | ColId '.' any_operator
4547 { $$ = lcons(makeString($1), $3); }
4551 /*****************************************************************************
4553 * CREATE CAST / DROP CAST
4555 *****************************************************************************/
4557 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4558 WITH FUNCTION function_with_argtypes cast_context
4560 CreateCastStmt *n = makeNode(CreateCastStmt);
4564 n->context = (CoercionContext) $11;
4567 | CREATE CAST '(' Typename AS Typename ')'
4568 WITHOUT FUNCTION cast_context
4570 CreateCastStmt *n = makeNode(CreateCastStmt);
4574 n->context = (CoercionContext) $10;
4579 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
4580 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
4581 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
4585 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
4587 DropCastStmt *n = makeNode(DropCastStmt);
4596 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
4597 | /*EMPTY*/ { $$ = FALSE; }
4601 /*****************************************************************************
4605 * REINDEX type <name> [FORCE]
4607 * FORCE no longer does anything, but we accept it for backwards compatibility
4608 *****************************************************************************/
4611 REINDEX reindex_type qualified_name opt_force
4613 ReindexStmt *n = makeNode(ReindexStmt);
4619 | REINDEX SYSTEM_P name opt_force
4621 ReindexStmt *n = makeNode(ReindexStmt);
4622 n->kind = OBJECT_DATABASE;
4625 n->do_system = true;
4629 | REINDEX DATABASE name opt_force
4631 ReindexStmt *n = makeNode(ReindexStmt);
4632 n->kind = OBJECT_DATABASE;
4635 n->do_system = true;
4642 INDEX { $$ = OBJECT_INDEX; }
4643 | TABLE { $$ = OBJECT_TABLE; }
4646 opt_force: FORCE { $$ = TRUE; }
4647 | /* EMPTY */ { $$ = FALSE; }
4651 /*****************************************************************************
4653 * ALTER THING name RENAME TO newname
4655 *****************************************************************************/
4657 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
4659 RenameStmt *n = makeNode(RenameStmt);
4660 n->renameType = OBJECT_AGGREGATE;
4666 | ALTER CONVERSION_P any_name RENAME TO name
4668 RenameStmt *n = makeNode(RenameStmt);
4669 n->renameType = OBJECT_CONVERSION;
4674 | ALTER DATABASE database_name RENAME TO database_name
4676 RenameStmt *n = makeNode(RenameStmt);
4677 n->renameType = OBJECT_DATABASE;
4682 | ALTER FUNCTION function_with_argtypes RENAME TO name
4684 RenameStmt *n = makeNode(RenameStmt);
4685 n->renameType = OBJECT_FUNCTION;
4686 n->object = $3->funcname;
4687 n->objarg = $3->funcargs;
4691 | ALTER GROUP_P RoleId RENAME TO RoleId
4693 RenameStmt *n = makeNode(RenameStmt);
4694 n->renameType = OBJECT_ROLE;
4699 | ALTER opt_procedural LANGUAGE name RENAME TO name
4701 RenameStmt *n = makeNode(RenameStmt);
4702 n->renameType = OBJECT_LANGUAGE;
4707 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
4709 RenameStmt *n = makeNode(RenameStmt);
4710 n->renameType = OBJECT_OPCLASS;
4716 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
4718 RenameStmt *n = makeNode(RenameStmt);
4719 n->renameType = OBJECT_OPFAMILY;
4725 | ALTER SCHEMA name RENAME TO name
4727 RenameStmt *n = makeNode(RenameStmt);
4728 n->renameType = OBJECT_SCHEMA;
4733 | ALTER TABLE relation_expr RENAME TO name
4735 RenameStmt *n = makeNode(RenameStmt);
4736 n->renameType = OBJECT_TABLE;
4742 | ALTER SEQUENCE relation_expr RENAME TO name
4744 RenameStmt *n = makeNode(RenameStmt);
4745 n->renameType = OBJECT_SEQUENCE;
4751 | ALTER VIEW relation_expr RENAME TO name
4753 RenameStmt *n = makeNode(RenameStmt);
4754 n->renameType = OBJECT_VIEW;
4760 | ALTER INDEX relation_expr RENAME TO name
4762 RenameStmt *n = makeNode(RenameStmt);
4763 n->renameType = OBJECT_INDEX;
4769 | ALTER TABLE relation_expr RENAME opt_column name TO name
4771 RenameStmt *n = makeNode(RenameStmt);
4772 n->renameType = OBJECT_COLUMN;
4778 | ALTER TRIGGER name ON relation_expr RENAME TO name
4780 RenameStmt *n = makeNode(RenameStmt);
4781 n->renameType = OBJECT_TRIGGER;
4787 | ALTER ROLE RoleId RENAME TO RoleId
4789 RenameStmt *n = makeNode(RenameStmt);
4790 n->renameType = OBJECT_ROLE;
4795 | ALTER USER RoleId RENAME TO RoleId
4797 RenameStmt *n = makeNode(RenameStmt);
4798 n->renameType = OBJECT_ROLE;
4803 | ALTER TABLESPACE name RENAME TO name
4805 RenameStmt *n = makeNode(RenameStmt);
4806 n->renameType = OBJECT_TABLESPACE;
4811 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
4813 RenameStmt *n = makeNode(RenameStmt);
4814 n->renameType = OBJECT_TSPARSER;
4819 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
4821 RenameStmt *n = makeNode(RenameStmt);
4822 n->renameType = OBJECT_TSDICTIONARY;
4827 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
4829 RenameStmt *n = makeNode(RenameStmt);
4830 n->renameType = OBJECT_TSTEMPLATE;
4835 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
4837 RenameStmt *n = makeNode(RenameStmt);
4838 n->renameType = OBJECT_TSCONFIGURATION;
4843 | ALTER TYPE_P any_name RENAME TO name
4845 RenameStmt *n = makeNode(RenameStmt);
4846 n->renameType = OBJECT_TYPE;
4853 opt_column: COLUMN { $$ = COLUMN; }
4854 | /*EMPTY*/ { $$ = 0; }
4857 /*****************************************************************************
4859 * ALTER THING name SET SCHEMA name
4861 *****************************************************************************/
4863 AlterObjectSchemaStmt:
4864 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
4866 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4867 n->objectType = OBJECT_AGGREGATE;
4873 | ALTER DOMAIN_P any_name SET SCHEMA name
4875 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4876 n->objectType = OBJECT_DOMAIN;
4881 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
4883 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4884 n->objectType = OBJECT_FUNCTION;
4885 n->object = $3->funcname;
4886 n->objarg = $3->funcargs;
4890 | ALTER TABLE relation_expr SET SCHEMA name
4892 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4893 n->objectType = OBJECT_TABLE;
4898 | ALTER SEQUENCE relation_expr SET SCHEMA name
4900 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4901 n->objectType = OBJECT_SEQUENCE;
4906 | ALTER VIEW relation_expr SET SCHEMA name
4908 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4909 n->objectType = OBJECT_VIEW;
4914 | ALTER TYPE_P any_name SET SCHEMA name
4916 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4917 n->objectType = OBJECT_TYPE;
4924 /*****************************************************************************
4926 * ALTER THING name OWNER TO newname
4928 *****************************************************************************/
4930 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
4932 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4933 n->objectType = OBJECT_AGGREGATE;
4939 | ALTER CONVERSION_P any_name OWNER TO RoleId
4941 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4942 n->objectType = OBJECT_CONVERSION;
4947 | ALTER DATABASE database_name OWNER TO RoleId
4949 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4950 n->objectType = OBJECT_DATABASE;
4951 n->object = list_make1(makeString($3));
4955 | ALTER DOMAIN_P any_name OWNER TO RoleId
4957 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4958 n->objectType = OBJECT_DOMAIN;
4963 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
4965 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4966 n->objectType = OBJECT_FUNCTION;
4967 n->object = $3->funcname;
4968 n->objarg = $3->funcargs;
4972 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
4974 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4975 n->objectType = OBJECT_LANGUAGE;
4976 n->object = list_make1(makeString($4));
4980 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
4982 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4983 n->objectType = OBJECT_OPERATOR;
4989 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
4991 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4992 n->objectType = OBJECT_OPCLASS;
4998 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5000 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5001 n->objectType = OBJECT_OPFAMILY;
5007 | ALTER SCHEMA name OWNER TO RoleId
5009 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5010 n->objectType = OBJECT_SCHEMA;
5011 n->object = list_make1(makeString($3));
5015 | ALTER TYPE_P any_name OWNER TO RoleId
5017 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5018 n->objectType = OBJECT_TYPE;
5023 | ALTER TABLESPACE name OWNER TO RoleId
5025 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5026 n->objectType = OBJECT_TABLESPACE;
5027 n->object = list_make1(makeString($3));
5031 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5033 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5034 n->objectType = OBJECT_TSDICTIONARY;
5039 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5041 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5042 n->objectType = OBJECT_TSCONFIGURATION;
5050 /*****************************************************************************
5052 * QUERY: Define Rewrite Rule
5054 *****************************************************************************/
5056 RuleStmt: CREATE opt_or_replace RULE name AS
5057 { QueryIsRule=TRUE; }
5058 ON event TO qualified_name where_clause
5059 DO opt_instead RuleActionList
5061 RuleStmt *n = makeNode(RuleStmt);
5065 n->whereClause = $11;
5075 NOTHING { $$ = NIL; }
5076 | RuleActionStmt { $$ = list_make1($1); }
5077 | '(' RuleActionMulti ')' { $$ = $2; }
5080 /* the thrashing around here is to discard "empty" statements... */
5082 RuleActionMulti ';' RuleActionStmtOrEmpty
5084 $$ = lappend($1, $3);
5088 | RuleActionStmtOrEmpty
5090 $$ = list_make1($1);
5104 RuleActionStmtOrEmpty:
5105 RuleActionStmt { $$ = $1; }
5106 | /*EMPTY*/ { $$ = NULL; }
5109 event: SELECT { $$ = CMD_SELECT; }
5110 | UPDATE { $$ = CMD_UPDATE; }
5111 | DELETE_P { $$ = CMD_DELETE; }
5112 | INSERT { $$ = CMD_INSERT; }
5116 INSTEAD { $$ = TRUE; }
5117 | ALSO { $$ = FALSE; }
5118 | /*EMPTY*/ { $$ = FALSE; }
5123 DROP RULE name ON qualified_name opt_drop_behavior
5125 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5129 n->removeType = OBJECT_RULE;
5130 n->missing_ok = false;
5133 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5135 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5139 n->removeType = OBJECT_RULE;
5140 n->missing_ok = true;
5146 /*****************************************************************************
5149 * NOTIFY <identifier> can appear both in rule bodies and
5150 * as a query-level command
5152 *****************************************************************************/
5154 NotifyStmt: NOTIFY ColId
5156 NotifyStmt *n = makeNode(NotifyStmt);
5157 n->conditionname = $2;
5162 ListenStmt: LISTEN ColId
5164 ListenStmt *n = makeNode(ListenStmt);
5165 n->conditionname = $2;
5173 UnlistenStmt *n = makeNode(UnlistenStmt);
5174 n->conditionname = $2;
5179 UnlistenStmt *n = makeNode(UnlistenStmt);
5180 n->conditionname = NULL;
5186 /*****************************************************************************
5190 * BEGIN / COMMIT / ROLLBACK
5191 * (also older versions END / ABORT)
5193 *****************************************************************************/
5196 ABORT_P opt_transaction
5198 TransactionStmt *n = makeNode(TransactionStmt);
5199 n->kind = TRANS_STMT_ROLLBACK;
5203 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5205 TransactionStmt *n = makeNode(TransactionStmt);
5206 n->kind = TRANS_STMT_BEGIN;
5210 | START TRANSACTION transaction_mode_list_or_empty
5212 TransactionStmt *n = makeNode(TransactionStmt);
5213 n->kind = TRANS_STMT_START;
5217 | COMMIT opt_transaction
5219 TransactionStmt *n = makeNode(TransactionStmt);
5220 n->kind = TRANS_STMT_COMMIT;
5224 | END_P opt_transaction
5226 TransactionStmt *n = makeNode(TransactionStmt);
5227 n->kind = TRANS_STMT_COMMIT;
5231 | ROLLBACK opt_transaction
5233 TransactionStmt *n = makeNode(TransactionStmt);
5234 n->kind = TRANS_STMT_ROLLBACK;
5240 TransactionStmt *n = makeNode(TransactionStmt);
5241 n->kind = TRANS_STMT_SAVEPOINT;
5242 n->options = list_make1(makeDefElem("savepoint_name",
5243 (Node *)makeString($2)));
5246 | RELEASE SAVEPOINT ColId
5248 TransactionStmt *n = makeNode(TransactionStmt);
5249 n->kind = TRANS_STMT_RELEASE;
5250 n->options = list_make1(makeDefElem("savepoint_name",
5251 (Node *)makeString($3)));
5256 TransactionStmt *n = makeNode(TransactionStmt);
5257 n->kind = TRANS_STMT_RELEASE;
5258 n->options = list_make1(makeDefElem("savepoint_name",
5259 (Node *)makeString($2)));
5262 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5264 TransactionStmt *n = makeNode(TransactionStmt);
5265 n->kind = TRANS_STMT_ROLLBACK_TO;
5266 n->options = list_make1(makeDefElem("savepoint_name",
5267 (Node *)makeString($5)));
5270 | ROLLBACK opt_transaction TO ColId
5272 TransactionStmt *n = makeNode(TransactionStmt);
5273 n->kind = TRANS_STMT_ROLLBACK_TO;
5274 n->options = list_make1(makeDefElem("savepoint_name",
5275 (Node *)makeString($4)));
5278 | PREPARE TRANSACTION Sconst
5280 TransactionStmt *n = makeNode(TransactionStmt);
5281 n->kind = TRANS_STMT_PREPARE;
5285 | COMMIT PREPARED Sconst
5287 TransactionStmt *n = makeNode(TransactionStmt);
5288 n->kind = TRANS_STMT_COMMIT_PREPARED;
5292 | ROLLBACK PREPARED Sconst
5294 TransactionStmt *n = makeNode(TransactionStmt);
5295 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5301 opt_transaction: WORK {}
5306 transaction_mode_item:
5307 ISOLATION LEVEL iso_level
5308 { $$ = makeDefElem("transaction_isolation",
5309 makeStringConst($3, @3)); }
5311 { $$ = makeDefElem("transaction_read_only",
5312 makeIntConst(TRUE, @1)); }
5314 { $$ = makeDefElem("transaction_read_only",
5315 makeIntConst(FALSE, @1)); }
5318 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5319 transaction_mode_list:
5320 transaction_mode_item
5321 { $$ = list_make1($1); }
5322 | transaction_mode_list ',' transaction_mode_item
5323 { $$ = lappend($1, $3); }
5324 | transaction_mode_list transaction_mode_item
5325 { $$ = lappend($1, $2); }
5328 transaction_mode_list_or_empty:
5329 transaction_mode_list
5335 /*****************************************************************************
5338 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5339 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5341 *****************************************************************************/
5343 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5344 AS SelectStmt opt_check_option
5346 ViewStmt *n = makeNode(ViewStmt);
5348 n->view->istemp = $2;
5354 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5355 AS SelectStmt opt_check_option
5357 ViewStmt *n = makeNode(ViewStmt);
5359 n->view->istemp = $4;
5368 * We use merged tokens here to avoid creating shift/reduce conflicts against
5369 * a whole lot of other uses of WITH.
5375 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5376 errmsg("WITH CHECK OPTION is not implemented")));
5378 | WITH_CASCADED CHECK OPTION
5381 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5382 errmsg("WITH CHECK OPTION is not implemented")));
5384 | WITH_LOCAL CHECK OPTION
5387 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5388 errmsg("WITH CHECK OPTION is not implemented")));
5390 | /* EMPTY */ { $$ = NIL; }
5393 /*****************************************************************************
5398 *****************************************************************************/
5400 LoadStmt: LOAD file_name
5402 LoadStmt *n = makeNode(LoadStmt);
5409 /*****************************************************************************
5413 *****************************************************************************/
5416 CREATE DATABASE database_name opt_with createdb_opt_list
5418 CreatedbStmt *n = makeNode(CreatedbStmt);
5426 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5427 | /* EMPTY */ { $$ = NIL; }
5431 TABLESPACE opt_equal name
5433 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5435 | TABLESPACE opt_equal DEFAULT
5437 $$ = makeDefElem("tablespace", NULL);
5439 | LOCATION opt_equal Sconst
5441 $$ = makeDefElem("location", (Node *)makeString($3));
5443 | LOCATION opt_equal DEFAULT
5445 $$ = makeDefElem("location", NULL);
5447 | TEMPLATE opt_equal name
5449 $$ = makeDefElem("template", (Node *)makeString($3));
5451 | TEMPLATE opt_equal DEFAULT
5453 $$ = makeDefElem("template", NULL);
5455 | ENCODING opt_equal Sconst
5457 $$ = makeDefElem("encoding", (Node *)makeString($3));
5459 | ENCODING opt_equal Iconst
5461 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5463 | ENCODING opt_equal DEFAULT
5465 $$ = makeDefElem("encoding", NULL);
5467 | COLLATE opt_equal Sconst
5469 $$ = makeDefElem("collate", (Node *)makeString($3));
5471 | COLLATE opt_equal DEFAULT
5473 $$ = makeDefElem("collate", NULL);
5475 | CTYPE opt_equal Sconst
5477 $$ = makeDefElem("ctype", (Node *)makeString($3));
5479 | CTYPE opt_equal DEFAULT
5481 $$ = makeDefElem("ctype", NULL);
5483 | CONNECTION LIMIT opt_equal SignedIconst
5485 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5487 | OWNER opt_equal name
5489 $$ = makeDefElem("owner", (Node *)makeString($3));
5491 | OWNER opt_equal DEFAULT
5493 $$ = makeDefElem("owner", NULL);
5498 * Though the equals sign doesn't match other WITH options, pg_dump uses
5499 * equals for backward compatibility, and it doesn't seem worth removing it.
5506 /*****************************************************************************
5510 *****************************************************************************/
5513 ALTER DATABASE database_name opt_with alterdb_opt_list
5515 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5522 AlterDatabaseSetStmt:
5523 ALTER DATABASE database_name SetResetClause
5525 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5534 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5535 | /* EMPTY */ { $$ = NIL; }
5539 CONNECTION LIMIT opt_equal SignedIconst
5541 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5546 /*****************************************************************************
5548 * DROP DATABASE [ IF EXISTS ]
5550 * This is implicitly CASCADE, no need for drop behavior
5551 *****************************************************************************/
5553 DropdbStmt: DROP DATABASE database_name
5555 DropdbStmt *n = makeNode(DropdbStmt);
5557 n->missing_ok = FALSE;
5560 | DROP DATABASE IF_P EXISTS database_name
5562 DropdbStmt *n = makeNode(DropdbStmt);
5564 n->missing_ok = TRUE;
5570 /*****************************************************************************
5572 * Manipulate a domain
5574 *****************************************************************************/
5577 CREATE DOMAIN_P any_name opt_as Typename ColQualList
5579 CreateDomainStmt *n = makeNode(CreateDomainStmt);
5582 n->constraints = $6;
5588 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
5589 ALTER DOMAIN_P any_name alter_column_default
5591 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5597 /* ALTER DOMAIN <domain> DROP NOT NULL */
5598 | ALTER DOMAIN_P any_name DROP NOT NULL_P
5600 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5605 /* ALTER DOMAIN <domain> SET NOT NULL */
5606 | ALTER DOMAIN_P any_name SET NOT NULL_P
5608 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5613 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
5614 | ALTER DOMAIN_P any_name ADD_P TableConstraint
5616 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5622 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
5623 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
5625 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5639 /*****************************************************************************
5641 * Manipulate a text search dictionary or configuration
5643 *****************************************************************************/
5645 AlterTSDictionaryStmt:
5646 ALTER TEXT_P SEARCH DICTIONARY any_name definition
5648 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
5655 AlterTSConfigurationStmt:
5656 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
5658 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5662 n->override = false;
5666 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
5668 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5676 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
5678 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5681 n->dicts = list_make2($9,$11);
5682 n->override = false;
5686 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
5688 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5691 n->dicts = list_make2($11,$13);
5692 n->override = false;
5696 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
5698 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5701 n->missing_ok = false;
5704 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
5706 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5709 n->missing_ok = true;
5715 /*****************************************************************************
5717 * Manipulate a conversion
5719 * CREATE [DEFAULT] CONVERSION <conversion_name>
5720 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
5722 *****************************************************************************/
5724 CreateConversionStmt:
5725 CREATE opt_default CONVERSION_P any_name FOR Sconst
5726 TO Sconst FROM any_name
5728 CreateConversionStmt *n = makeNode(CreateConversionStmt);
5729 n->conversion_name = $4;
5730 n->for_encoding_name = $6;
5731 n->to_encoding_name = $8;
5738 /*****************************************************************************
5741 * CLUSTER <qualified_name> [ USING <index_name> ]
5743 * CLUSTER <index_name> ON <qualified_name> (for pre-8.3)
5745 *****************************************************************************/
5748 CLUSTER qualified_name cluster_index_specification
5750 ClusterStmt *n = makeNode(ClusterStmt);
5757 ClusterStmt *n = makeNode(ClusterStmt);
5759 n->indexname = NULL;
5762 /* kept for pre-8.3 compatibility */
5763 | CLUSTER index_name ON qualified_name
5765 ClusterStmt *n = makeNode(ClusterStmt);
5772 cluster_index_specification:
5773 USING index_name { $$ = $2; }
5774 | /*EMPTY*/ { $$ = NULL; }
5778 /*****************************************************************************
5784 *****************************************************************************/
5786 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
5788 VacuumStmt *n = makeNode(VacuumStmt);
5792 n->freeze_min_age = $3 ? 0 : -1;
5798 | VACUUM opt_full opt_freeze opt_verbose qualified_name
5800 VacuumStmt *n = makeNode(VacuumStmt);
5804 n->freeze_min_age = $3 ? 0 : -1;
5810 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
5812 VacuumStmt *n = (VacuumStmt *) $5;
5815 n->freeze_min_age = $3 ? 0 : -1;
5822 analyze_keyword opt_verbose
5824 VacuumStmt *n = makeNode(VacuumStmt);
5828 n->freeze_min_age = -1;
5834 | analyze_keyword opt_verbose qualified_name opt_name_list
5836 VacuumStmt *n = makeNode(VacuumStmt);
5840 n->freeze_min_age = -1;
5850 | ANALYSE /* British */ {}
5854 VERBOSE { $$ = TRUE; }
5855 | /*EMPTY*/ { $$ = FALSE; }
5858 opt_full: FULL { $$ = TRUE; }
5859 | /*EMPTY*/ { $$ = FALSE; }
5862 opt_freeze: FREEZE { $$ = TRUE; }
5863 | /*EMPTY*/ { $$ = FALSE; }
5867 '(' name_list ')' { $$ = $2; }
5868 | /*EMPTY*/ { $$ = NIL; }
5872 /*****************************************************************************
5875 * EXPLAIN [ANALYZE] [VERBOSE] query
5877 *****************************************************************************/
5879 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
5881 ExplainStmt *n = makeNode(ExplainStmt);
5895 | ExecuteStmt /* by default all are $$=$1 */
5899 analyze_keyword { $$ = TRUE; }
5900 | /* EMPTY */ { $$ = FALSE; }
5903 /*****************************************************************************
5906 * PREPARE <plan_name> [(args, ...)] AS <query>
5908 *****************************************************************************/
5910 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
5912 PrepareStmt *n = makeNode(PrepareStmt);
5920 prep_type_clause: '(' type_list ')' { $$ = $2; }
5921 | /* EMPTY */ { $$ = NIL; }
5928 | DeleteStmt /* by default all are $$=$1 */
5931 /*****************************************************************************
5933 * EXECUTE <plan_name> [(params, ...)]
5934 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
5936 *****************************************************************************/
5938 ExecuteStmt: EXECUTE name execute_param_clause
5940 ExecuteStmt *n = makeNode(ExecuteStmt);
5946 | CREATE OptTemp TABLE create_as_target AS
5947 EXECUTE name execute_param_clause
5949 ExecuteStmt *n = makeNode(ExecuteStmt);
5952 $4->rel->istemp = $2;
5956 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5957 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
5958 /* ... because it's not implemented, but it could be */
5963 execute_param_clause: '(' expr_list ')' { $$ = $2; }
5964 | /* EMPTY */ { $$ = NIL; }
5967 /*****************************************************************************
5970 * DEALLOCATE [PREPARE] <plan_name>
5972 *****************************************************************************/
5974 DeallocateStmt: DEALLOCATE name
5976 DeallocateStmt *n = makeNode(DeallocateStmt);
5980 | DEALLOCATE PREPARE name
5982 DeallocateStmt *n = makeNode(DeallocateStmt);
5988 DeallocateStmt *n = makeNode(DeallocateStmt);
5992 | DEALLOCATE PREPARE ALL
5994 DeallocateStmt *n = makeNode(DeallocateStmt);
6000 /*****************************************************************************
6005 *****************************************************************************/
6008 INSERT INTO qualified_name insert_rest returning_clause
6011 $4->returningList = $5;
6019 $$ = makeNode(InsertStmt);
6021 $$->selectStmt = $1;
6023 | '(' insert_column_list ')' SelectStmt
6025 $$ = makeNode(InsertStmt);
6027 $$->selectStmt = $4;
6031 $$ = makeNode(InsertStmt);
6033 $$->selectStmt = NULL;
6039 { $$ = list_make1($1); }
6040 | insert_column_list ',' insert_column_item
6041 { $$ = lappend($1, $3); }
6045 ColId opt_indirection
6047 $$ = makeNode(ResTarget);
6049 $$->indirection = check_indirection($2);
6056 RETURNING target_list { $$ = $2; }
6057 | /* EMPTY */ { $$ = NIL; }
6061 /*****************************************************************************
6066 *****************************************************************************/
6068 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6069 using_clause where_or_current_clause returning_clause
6071 DeleteStmt *n = makeNode(DeleteStmt);
6073 n->usingClause = $4;
6074 n->whereClause = $5;
6075 n->returningList = $6;
6081 USING from_list { $$ = $2; }
6082 | /*EMPTY*/ { $$ = NIL; }
6085 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
6087 LockStmt *n = makeNode(LockStmt);
6096 opt_lock: IN_P lock_type MODE { $$ = $2; }
6097 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6100 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6101 | ROW SHARE { $$ = RowShareLock; }
6102 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6103 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6104 | SHARE { $$ = ShareLock; }
6105 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6106 | EXCLUSIVE { $$ = ExclusiveLock; }
6107 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6110 opt_nowait: NOWAIT { $$ = TRUE; }
6111 | /*EMPTY*/ { $$ = FALSE; }
6115 /*****************************************************************************
6118 * UpdateStmt (UPDATE)
6120 *****************************************************************************/
6122 UpdateStmt: UPDATE relation_expr_opt_alias
6125 where_or_current_clause
6128 UpdateStmt *n = makeNode(UpdateStmt);
6132 n->whereClause = $6;
6133 n->returningList = $7;
6139 set_clause { $$ = $1; }
6140 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6144 single_set_clause { $$ = list_make1($1); }
6145 | multiple_set_clause { $$ = $1; }
6149 set_target '=' ctext_expr
6152 $$->val = (Node *) $3;
6156 multiple_set_clause:
6157 '(' set_target_list ')' '=' ctext_row
6163 * Break the ctext_row apart, merge individual expressions
6164 * into the destination ResTargets. XXX this approach
6165 * cannot work for general row expressions as sources.
6167 if (list_length($2) != list_length($5))
6169 (errcode(ERRCODE_SYNTAX_ERROR),
6170 errmsg("number of columns does not match number of values"),
6171 scanner_errposition(@1)));
6172 forboth(col_cell, $2, val_cell, $5)
6174 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6175 Node *res_val = (Node *) lfirst(val_cell);
6177 res_col->val = res_val;
6185 ColId opt_indirection
6187 $$ = makeNode(ResTarget);
6189 $$->indirection = check_indirection($2);
6190 $$->val = NULL; /* upper production sets this */
6196 set_target { $$ = list_make1($1); }
6197 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6201 /*****************************************************************************
6206 *****************************************************************************/
6207 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6209 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6211 /* currently we always set FAST_PLAN option */
6212 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6218 cursor_options: /*EMPTY*/ { $$ = 0; }
6219 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6220 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6221 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6222 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6225 opt_hold: /* EMPTY */ { $$ = 0; }
6226 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6227 | WITHOUT HOLD { $$ = 0; }
6230 /*****************************************************************************
6235 *****************************************************************************/
6237 /* A complete SELECT statement looks like this.
6239 * The rule returns either a single SelectStmt node or a tree of them,
6240 * representing a set-operation tree.
6242 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6243 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6244 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6245 * To resolve the ambiguity, we are careful to define the grammar so that
6246 * the decision is staved off as long as possible: as long as we can keep
6247 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6248 * it's no longer possible to do that will we decide that parens belong to
6249 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6250 * parentheses are treated as part of the sub-select. The necessity of doing
6251 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6252 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6253 * SELECT viewpoint when we see the UNION.
6255 * This approach is implemented by defining a nonterminal select_with_parens,
6256 * which represents a SELECT with at least one outer layer of parentheses,
6257 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6258 * in the expression grammar. We will then have shift-reduce conflicts
6259 * which we can resolve in favor of always treating '(' <select> ')' as
6260 * a select_with_parens. To resolve the conflicts, the productions that
6261 * conflict with the select_with_parens productions are manually given
6262 * precedences lower than the precedence of ')', thereby ensuring that we
6263 * shift ')' (and then reduce to select_with_parens) rather than trying to
6264 * reduce the inner <select> nonterminal to something else. We use UMINUS
6265 * precedence for this, which is a fairly arbitrary choice.
6267 * To be able to define select_with_parens itself without ambiguity, we need
6268 * a nonterminal select_no_parens that represents a SELECT structure with no
6269 * outermost parentheses. This is a little bit tedious, but it works.
6271 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6272 * with or without outer parentheses.
6275 SelectStmt: select_no_parens %prec UMINUS
6276 | select_with_parens %prec UMINUS
6280 '(' select_no_parens ')' { $$ = $2; }
6281 | '(' select_with_parens ')' { $$ = $2; }
6285 * This rule parses the equivalent of the standard's <query expression>.
6286 * The duplicative productions are annoying, but hard to get rid of without
6287 * creating shift/reduce conflicts.
6289 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6290 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6291 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6295 simple_select { $$ = $1; }
6296 | select_clause sort_clause
6298 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6302 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6304 insertSelectOptions((SelectStmt *) $1, $2, $3,
6305 list_nth($4, 0), list_nth($4, 1),
6309 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6311 insertSelectOptions((SelectStmt *) $1, $2, $4,
6312 list_nth($3, 0), list_nth($3, 1),
6316 | with_clause simple_select
6318 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6323 | with_clause select_clause sort_clause
6325 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6330 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6332 insertSelectOptions((SelectStmt *) $2, $3, $4,
6333 list_nth($5, 0), list_nth($5, 1),
6337 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6339 insertSelectOptions((SelectStmt *) $2, $3, $5,
6340 list_nth($4, 0), list_nth($4, 1),
6347 simple_select { $$ = $1; }
6348 | select_with_parens { $$ = $1; }
6352 * This rule parses SELECT statements that can appear within set operations,
6353 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6354 * the ordering of the set operations. Without '(' and ')' we want the
6355 * operations to be ordered per the precedence specs at the head of this file.
6357 * As with select_no_parens, simple_select cannot have outer parentheses,
6358 * but can have parenthesized subclauses.
6360 * Note that sort clauses cannot be included at this level --- SQL92 requires
6361 * SELECT foo UNION SELECT bar ORDER BY baz
6363 * (SELECT foo UNION SELECT bar) ORDER BY baz
6365 * SELECT foo UNION (SELECT bar ORDER BY baz)
6366 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6367 * described as part of the select_no_parens production, not simple_select.
6368 * This does not limit functionality, because you can reintroduce these
6369 * clauses inside parentheses.
6371 * NOTE: only the leftmost component SelectStmt should have INTO.
6372 * However, this is not checked by the grammar; parse analysis must check it.
6375 SELECT opt_distinct target_list
6376 into_clause from_clause where_clause
6377 group_clause having_clause
6379 SelectStmt *n = makeNode(SelectStmt);
6380 n->distinctClause = $2;
6384 n->whereClause = $6;
6385 n->groupClause = $7;
6386 n->havingClause = $8;
6389 | values_clause { $$ = $1; }
6390 | select_clause UNION opt_all select_clause
6392 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6394 | select_clause INTERSECT opt_all select_clause
6396 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6398 | select_clause EXCEPT opt_all select_clause
6400 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6405 * SQL standard WITH clause looks like:
6407 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6408 * AS (query) [ SEARCH or CYCLE clause ]
6410 * We don't currently support the SEARCH or CYCLE clause.
6415 $$ = makeNode(WithClause);
6417 $$->recursive = false;
6420 | WITH RECURSIVE cte_list
6422 $$ = makeNode(WithClause);
6424 $$->recursive = true;
6430 common_table_expr { $$ = list_make1($1); }
6431 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
6434 common_table_expr: name opt_name_list AS select_with_parens
6436 CommonTableExpr *n = makeNode(CommonTableExpr);
6438 n->aliascolnames = $2;
6446 INTO OptTempTableName
6448 $$ = makeNode(IntoClause);
6452 $$->onCommit = ONCOMMIT_NOOP;
6453 $$->tableSpaceName = NULL;
6460 * Redundancy here is needed to avoid shift/reduce conflicts,
6461 * since TEMP is not a reserved word. See also OptTemp.
6464 TEMPORARY opt_table qualified_name
6469 | TEMP opt_table qualified_name
6474 | LOCAL TEMPORARY opt_table qualified_name
6479 | LOCAL TEMP opt_table qualified_name
6484 | GLOBAL TEMPORARY opt_table qualified_name
6489 | GLOBAL TEMP opt_table qualified_name
6494 | TABLE qualified_name
6510 opt_all: ALL { $$ = TRUE; }
6511 | DISTINCT { $$ = FALSE; }
6512 | /*EMPTY*/ { $$ = FALSE; }
6515 /* We use (NIL) as a placeholder to indicate that all target expressions
6516 * should be placed in the DISTINCT list during parsetree analysis.
6519 DISTINCT { $$ = list_make1(NIL); }
6520 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6522 | /*EMPTY*/ { $$ = NIL; }
6526 sort_clause { $$ = $1;}
6527 | /*EMPTY*/ { $$ = NIL; }
6531 ORDER BY sortby_list { $$ = $3; }
6535 sortby { $$ = list_make1($1); }
6536 | sortby_list ',' sortby { $$ = lappend($1, $3); }
6539 sortby: a_expr USING qual_all_Op opt_nulls_order
6541 $$ = makeNode(SortBy);
6543 $$->sortby_dir = SORTBY_USING;
6544 $$->sortby_nulls = $4;
6548 | a_expr opt_asc_desc opt_nulls_order
6550 $$ = makeNode(SortBy);
6552 $$->sortby_dir = $2;
6553 $$->sortby_nulls = $3;
6555 $$->location = -1; /* no operator */
6561 LIMIT select_limit_value OFFSET select_offset_value
6562 { $$ = list_make2($4, $2); }
6563 | OFFSET select_offset_value LIMIT select_limit_value
6564 { $$ = list_make2($2, $4); }
6565 | LIMIT select_limit_value
6566 { $$ = list_make2(NULL, $2); }
6567 | OFFSET select_offset_value
6568 { $$ = list_make2($2, NULL); }
6569 | LIMIT select_limit_value ',' select_offset_value
6571 /* Disabled because it was too confusing, bjm 2002-02-18 */
6573 (errcode(ERRCODE_SYNTAX_ERROR),
6574 errmsg("LIMIT #,# syntax is not supported"),
6575 errhint("Use separate LIMIT and OFFSET clauses."),
6576 scanner_errposition(@1)));
6581 select_limit { $$ = $1; }
6583 { $$ = list_make2(NULL,NULL); }
6590 /* LIMIT ALL is represented as a NULL constant */
6591 $$ = makeNullAConst(@1);
6595 select_offset_value:
6600 GROUP_P BY expr_list { $$ = $3; }
6601 | /*EMPTY*/ { $$ = NIL; }
6605 HAVING a_expr { $$ = $2; }
6606 | /*EMPTY*/ { $$ = NULL; }
6610 for_locking_items { $$ = $1; }
6611 | FOR READ ONLY { $$ = NIL; }
6614 opt_for_locking_clause:
6615 for_locking_clause { $$ = $1; }
6616 | /* EMPTY */ { $$ = NIL; }
6620 for_locking_item { $$ = list_make1($1); }
6621 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
6625 FOR UPDATE locked_rels_list opt_nowait
6627 LockingClause *n = makeNode(LockingClause);
6629 n->forUpdate = TRUE;
6633 | FOR SHARE locked_rels_list opt_nowait
6635 LockingClause *n = makeNode(LockingClause);
6637 n->forUpdate = FALSE;
6644 OF qualified_name_list { $$ = $2; }
6645 | /* EMPTY */ { $$ = NIL; }
6652 SelectStmt *n = makeNode(SelectStmt);
6653 n->valuesLists = list_make1($2);
6656 | values_clause ',' ctext_row
6658 SelectStmt *n = (SelectStmt *) $1;
6659 n->valuesLists = lappend(n->valuesLists, $3);
6665 /*****************************************************************************
6667 * clauses common to all Optimizable Stmts:
6668 * from_clause - allow list of both JOIN expressions and table names
6669 * where_clause - qualifications for joins or restrictions
6671 *****************************************************************************/
6674 FROM from_list { $$ = $2; }
6675 | /*EMPTY*/ { $$ = NIL; }
6679 table_ref { $$ = list_make1($1); }
6680 | from_list ',' table_ref { $$ = lappend($1, $3); }
6684 * table_ref is where an alias clause can be attached. Note we cannot make
6685 * alias_clause have an empty production because that causes parse conflicts
6686 * between table_ref := '(' joined_table ')' alias_clause
6687 * and joined_table := '(' joined_table ')'. So, we must have the
6688 * redundant-looking productions here instead.
6690 table_ref: relation_expr
6694 | relation_expr alias_clause
6701 RangeFunction *n = makeNode(RangeFunction);
6702 n->funccallnode = $1;
6703 n->coldeflist = NIL;
6706 | func_table alias_clause
6708 RangeFunction *n = makeNode(RangeFunction);
6709 n->funccallnode = $1;
6711 n->coldeflist = NIL;
6714 | func_table AS '(' TableFuncElementList ')'
6716 RangeFunction *n = makeNode(RangeFunction);
6717 n->funccallnode = $1;
6721 | func_table AS ColId '(' TableFuncElementList ')'
6723 RangeFunction *n = makeNode(RangeFunction);
6724 Alias *a = makeNode(Alias);
6725 n->funccallnode = $1;
6731 | func_table ColId '(' TableFuncElementList ')'
6733 RangeFunction *n = makeNode(RangeFunction);
6734 Alias *a = makeNode(Alias);
6735 n->funccallnode = $1;
6741 | select_with_parens
6744 * The SQL spec does not permit a subselect
6745 * (<derived_table>) without an alias clause,
6746 * so we don't either. This avoids the problem
6747 * of needing to invent a unique refname for it.
6748 * That could be surmounted if there's sufficient
6749 * popular demand, but for now let's just implement
6750 * the spec and see if anyone complains.
6751 * However, it does seem like a good idea to emit
6752 * an error message that's better than "syntax error".
6754 if (IsA($1, SelectStmt) &&
6755 ((SelectStmt *) $1)->valuesLists)
6757 (errcode(ERRCODE_SYNTAX_ERROR),
6758 errmsg("VALUES in FROM must have an alias"),
6759 errhint("For example, FROM (VALUES ...) [AS] foo."),
6760 scanner_errposition(@1)));
6763 (errcode(ERRCODE_SYNTAX_ERROR),
6764 errmsg("subquery in FROM must have an alias"),
6765 errhint("For example, FROM (SELECT ...) [AS] foo."),
6766 scanner_errposition(@1)));
6769 | select_with_parens alias_clause
6771 RangeSubselect *n = makeNode(RangeSubselect);
6780 | '(' joined_table ')' alias_clause
6789 * It may seem silly to separate joined_table from table_ref, but there is
6790 * method in SQL92's madness: if you don't do it this way you get reduce-
6791 * reduce conflicts, because it's not clear to the parser generator whether
6792 * to expect alias_clause after ')' or not. For the same reason we must
6793 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
6794 * join_type to expand to empty; if we try it, the parser generator can't
6795 * figure out when to reduce an empty join_type right after table_ref.
6797 * Note that a CROSS JOIN is the same as an unqualified
6798 * INNER JOIN, and an INNER JOIN/ON has the same shape
6799 * but a qualification expression to limit membership.
6800 * A NATURAL JOIN implicitly matches column names between
6801 * tables and the shape is determined by which columns are
6802 * in common. We'll collect columns during the later transformations.
6806 '(' joined_table ')'
6810 | table_ref CROSS JOIN table_ref
6812 /* CROSS JOIN is same as unqualified inner join */
6813 JoinExpr *n = makeNode(JoinExpr);
6814 n->jointype = JOIN_INNER;
6815 n->isNatural = FALSE;
6822 | table_ref join_type JOIN table_ref join_qual
6824 JoinExpr *n = makeNode(JoinExpr);
6826 n->isNatural = FALSE;
6829 if ($5 != NULL && IsA($5, List))
6830 n->using = (List *) $5; /* USING clause */
6832 n->quals = $5; /* ON clause */
6835 | table_ref JOIN table_ref join_qual
6837 /* letting join_type reduce to empty doesn't work */
6838 JoinExpr *n = makeNode(JoinExpr);
6839 n->jointype = JOIN_INNER;
6840 n->isNatural = FALSE;
6843 if ($4 != NULL && IsA($4, List))
6844 n->using = (List *) $4; /* USING clause */
6846 n->quals = $4; /* ON clause */
6849 | table_ref NATURAL join_type JOIN table_ref
6851 JoinExpr *n = makeNode(JoinExpr);
6853 n->isNatural = TRUE;
6856 n->using = NIL; /* figure out which columns later... */
6857 n->quals = NULL; /* fill later */
6860 | table_ref NATURAL JOIN table_ref
6862 /* letting join_type reduce to empty doesn't work */
6863 JoinExpr *n = makeNode(JoinExpr);
6864 n->jointype = JOIN_INNER;
6865 n->isNatural = TRUE;
6868 n->using = NIL; /* figure out which columns later... */
6869 n->quals = NULL; /* fill later */
6875 AS ColId '(' name_list ')'
6877 $$ = makeNode(Alias);
6883 $$ = makeNode(Alias);
6886 | ColId '(' name_list ')'
6888 $$ = makeNode(Alias);
6894 $$ = makeNode(Alias);
6899 join_type: FULL join_outer { $$ = JOIN_FULL; }
6900 | LEFT join_outer { $$ = JOIN_LEFT; }
6901 | RIGHT join_outer { $$ = JOIN_RIGHT; }
6902 | INNER_P { $$ = JOIN_INNER; }
6905 /* OUTER is just noise... */
6906 join_outer: OUTER_P { $$ = NULL; }
6907 | /*EMPTY*/ { $$ = NULL; }
6910 /* JOIN qualification clauses
6911 * Possibilities are:
6912 * USING ( column list ) allows only unqualified column names,
6913 * which must match between tables.
6914 * ON expr allows more general qualifications.
6916 * We return USING as a List node, while an ON-expr will not be a List.
6919 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
6920 | ON a_expr { $$ = $2; }
6927 /* default inheritance */
6929 $$->inhOpt = INH_DEFAULT;
6932 | qualified_name '*'
6934 /* inheritance query */
6936 $$->inhOpt = INH_YES;
6939 | ONLY qualified_name
6941 /* no inheritance */
6943 $$->inhOpt = INH_NO;
6946 | ONLY '(' qualified_name ')'
6948 /* no inheritance, SQL99-style syntax */
6950 $$->inhOpt = INH_NO;
6957 * Given "UPDATE foo set set ...", we have to decide without looking any
6958 * further ahead whether the first "set" is an alias or the UPDATE's SET
6959 * keyword. Since "set" is allowed as a column name both interpretations
6960 * are feasible. We resolve the shift/reduce conflict by giving the first
6961 * relation_expr_opt_alias production a higher precedence than the SET token
6962 * has, causing the parser to prefer to reduce, in effect assuming that the
6963 * SET is not an alias.
6965 relation_expr_opt_alias: relation_expr %prec UMINUS
6969 | relation_expr ColId
6971 Alias *alias = makeNode(Alias);
6972 alias->aliasname = $2;
6976 | relation_expr AS ColId
6978 Alias *alias = makeNode(Alias);
6979 alias->aliasname = $3;
6986 func_table: func_expr { $$ = $1; }
6991 WHERE a_expr { $$ = $2; }
6992 | /*EMPTY*/ { $$ = NULL; }
6995 /* variant for UPDATE and DELETE */
6996 where_or_current_clause:
6997 WHERE a_expr { $$ = $2; }
6998 | WHERE CURRENT_P OF name
7000 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7001 /* cvarno is filled in by parse analysis */
7002 n->cursor_name = $4;
7003 n->cursor_param = 0;
7006 | WHERE CURRENT_P OF PARAM
7008 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7009 /* cvarno is filled in by parse analysis */
7010 n->cursor_name = NULL;
7011 n->cursor_param = $4;
7014 | /*EMPTY*/ { $$ = NULL; }
7018 TableFuncElementList:
7021 $$ = list_make1($1);
7023 | TableFuncElementList ',' TableFuncElement
7025 $$ = lappend($1, $3);
7029 TableFuncElement: ColId Typename
7031 ColumnDef *n = makeNode(ColumnDef);
7034 n->constraints = NIL;
7040 /*****************************************************************************
7043 * SQL92 introduces a large amount of type-specific syntax.
7044 * Define individual clauses to handle these cases, and use
7045 * the generic case to handle regular type-extensible Postgres syntax.
7046 * - thomas 1997-10-10
7048 *****************************************************************************/
7050 Typename: SimpleTypename opt_array_bounds
7053 $$->arrayBounds = $2;
7055 | SETOF SimpleTypename opt_array_bounds
7058 $$->arrayBounds = $3;
7061 | SimpleTypename ARRAY '[' Iconst ']'
7063 /* SQL99's redundant syntax */
7065 $$->arrayBounds = list_make1(makeInteger($4));
7067 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7069 /* SQL99's redundant syntax */
7071 $$->arrayBounds = list_make1(makeInteger($5));
7077 opt_array_bounds '[' ']'
7078 { $$ = lappend($1, makeInteger(-1)); }
7079 | opt_array_bounds '[' Iconst ']'
7080 { $$ = lappend($1, makeInteger($3)); }
7086 GenericType { $$ = $1; }
7087 | Numeric { $$ = $1; }
7089 | Character { $$ = $1; }
7090 | ConstDatetime { $$ = $1; }
7091 | ConstInterval opt_interval
7096 | ConstInterval '(' Iconst ')' opt_interval
7101 if (list_length($5) != 1)
7103 (errcode(ERRCODE_SYNTAX_ERROR),
7104 errmsg("interval precision specified twice"),
7105 scanner_errposition(@1)));
7106 $$->typmods = lappend($5, makeIntConst($3, @3));
7109 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7110 makeIntConst($3, @3));
7114 /* We have a separate ConstTypename to allow defaulting fixed-length
7115 * types such as CHAR() and BIT() to an unspecified length.
7116 * SQL9x requires that these default to a length of one, but this
7117 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7118 * where there is an obvious better choice to make.
7119 * Note that ConstInterval is not included here since it must
7120 * be pushed up higher in the rules to accomodate the postfix
7121 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7122 * the generic-type-name case in AExprConst to avoid premature
7123 * reduce/reduce conflicts against function names.
7126 Numeric { $$ = $1; }
7127 | ConstBit { $$ = $1; }
7128 | ConstCharacter { $$ = $1; }
7129 | ConstDatetime { $$ = $1; }
7133 * GenericType covers all type names that don't have special syntax mandated
7134 * by the standard, including qualified names. We also allow type modifiers.
7135 * To avoid parsing conflicts against function invocations, the modifiers
7136 * have to be shown as expr_list here, but parse analysis will only accept
7137 * constants for them.
7140 type_function_name opt_type_modifiers
7142 $$ = makeTypeName($1);
7146 | type_function_name attrs opt_type_modifiers
7148 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7154 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7155 | /* EMPTY */ { $$ = NIL; }
7159 * SQL92 numeric data types
7163 $$ = SystemTypeName("int4");
7168 $$ = SystemTypeName("int4");
7173 $$ = SystemTypeName("int2");
7178 $$ = SystemTypeName("int8");
7183 $$ = SystemTypeName("float4");
7191 | DOUBLE_P PRECISION
7193 $$ = SystemTypeName("float8");
7196 | DECIMAL_P opt_type_modifiers
7198 $$ = SystemTypeName("numeric");
7202 | DEC opt_type_modifiers
7204 $$ = SystemTypeName("numeric");
7208 | NUMERIC opt_type_modifiers
7210 $$ = SystemTypeName("numeric");
7216 $$ = SystemTypeName("bool");
7221 opt_float: '(' Iconst ')'
7224 * Check FLOAT() precision limits assuming IEEE floating
7225 * types - thomas 1997-09-18
7229 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7230 errmsg("precision for type float must be at least 1 bit"),
7231 scanner_errposition(@2)));
7233 $$ = SystemTypeName("float4");
7235 $$ = SystemTypeName("float8");
7238 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7239 errmsg("precision for type float must be less than 54 bits"),
7240 scanner_errposition(@2)));
7244 $$ = SystemTypeName("float8");
7249 * SQL92 bit-field data types
7250 * The following implements BIT() and BIT VARYING().
7262 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7263 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7264 ConstBit: BitWithLength
7276 BIT opt_varying '(' expr_list ')'
7280 typname = $2 ? "varbit" : "bit";
7281 $$ = SystemTypeName(typname);
7290 /* bit defaults to bit(1), varbit to no limit */
7293 $$ = SystemTypeName("varbit");
7297 $$ = SystemTypeName("bit");
7298 $$->typmods = list_make1(makeIntConst(1, -1));
7306 * SQL92 character data types
7307 * The following implements CHAR() and VARCHAR().
7309 Character: CharacterWithLength
7313 | CharacterWithoutLength
7319 ConstCharacter: CharacterWithLength
7323 | CharacterWithoutLength
7325 /* Length was not specified so allow to be unrestricted.
7326 * This handles problems with fixed-length (bpchar) strings
7327 * which in column definitions must default to a length
7328 * of one, but should not be constrained if the length
7329 * was not specified.
7336 CharacterWithLength: character '(' Iconst ')' opt_charset
7338 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7342 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7349 $$ = SystemTypeName($1);
7350 $$->typmods = list_make1(makeIntConst($3, @3));
7355 CharacterWithoutLength: character opt_charset
7357 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7361 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7368 $$ = SystemTypeName($1);
7370 /* char defaults to char(1), varchar to no limit */
7371 if (strcmp($1, "bpchar") == 0)
7372 $$->typmods = list_make1(makeIntConst(1, -1));
7378 character: CHARACTER opt_varying
7379 { $$ = $2 ? "varchar": "bpchar"; }
7380 | CHAR_P opt_varying
7381 { $$ = $2 ? "varchar": "bpchar"; }
7384 | NATIONAL CHARACTER opt_varying
7385 { $$ = $3 ? "varchar": "bpchar"; }
7386 | NATIONAL CHAR_P opt_varying
7387 { $$ = $3 ? "varchar": "bpchar"; }
7389 { $$ = $2 ? "varchar": "bpchar"; }
7393 VARYING { $$ = TRUE; }
7394 | /*EMPTY*/ { $$ = FALSE; }
7398 CHARACTER SET ColId { $$ = $3; }
7399 | /*EMPTY*/ { $$ = NULL; }
7403 * SQL92 date/time types
7406 TIMESTAMP '(' Iconst ')' opt_timezone
7409 $$ = SystemTypeName("timestamptz");
7411 $$ = SystemTypeName("timestamp");
7412 $$->typmods = list_make1(makeIntConst($3, @3));
7415 | TIMESTAMP opt_timezone
7418 $$ = SystemTypeName("timestamptz");
7420 $$ = SystemTypeName("timestamp");
7423 | TIME '(' Iconst ')' opt_timezone
7426 $$ = SystemTypeName("timetz");
7428 $$ = SystemTypeName("time");
7429 $$->typmods = list_make1(makeIntConst($3, @3));
7435 $$ = SystemTypeName("timetz");
7437 $$ = SystemTypeName("time");
7445 $$ = SystemTypeName("interval");
7451 WITH TIME ZONE { $$ = TRUE; }
7452 | WITHOUT TIME ZONE { $$ = FALSE; }
7453 | /*EMPTY*/ { $$ = FALSE; }
7458 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
7460 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
7462 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
7464 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
7466 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
7471 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
7472 INTERVAL_MASK(MONTH), @1));
7476 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7477 INTERVAL_MASK(HOUR), @1));
7481 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7482 INTERVAL_MASK(HOUR) |
7483 INTERVAL_MASK(MINUTE), @1));
7485 | DAY_P TO interval_second
7488 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
7489 INTERVAL_MASK(HOUR) |
7490 INTERVAL_MASK(MINUTE) |
7491 INTERVAL_MASK(SECOND), @1);
7493 | HOUR_P TO MINUTE_P
7495 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
7496 INTERVAL_MASK(MINUTE), @1));
7498 | HOUR_P TO interval_second
7501 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
7502 INTERVAL_MASK(MINUTE) |
7503 INTERVAL_MASK(SECOND), @1);
7505 | MINUTE_P TO interval_second
7508 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
7509 INTERVAL_MASK(SECOND), @1);
7518 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
7520 | SECOND_P '(' Iconst ')'
7522 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
7523 makeIntConst($3, @3));
7528 /*****************************************************************************
7530 * expression grammar
7532 *****************************************************************************/
7535 * General expressions
7536 * This is the heart of the expression syntax.
7538 * We have two expression types: a_expr is the unrestricted kind, and
7539 * b_expr is a subset that must be used in some places to avoid shift/reduce
7540 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
7541 * because that use of AND conflicts with AND as a boolean operator. So,
7542 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
7544 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
7545 * always be used by surrounding it with parens.
7547 * c_expr is all the productions that are common to a_expr and b_expr;
7548 * it's factored out just to eliminate redundant coding.
7550 a_expr: c_expr { $$ = $1; }
7551 | a_expr TYPECAST Typename
7552 { $$ = makeTypeCast($1, $3, @2); }
7553 | a_expr AT TIME ZONE a_expr
7555 FuncCall *n = makeNode(FuncCall);
7556 n->funcname = SystemFuncName("timezone");
7557 n->args = list_make2($5, $1);
7558 n->agg_star = FALSE;
7559 n->agg_distinct = FALSE;
7560 n->func_variadic = FALSE;
7565 * These operators must be called out explicitly in order to make use
7566 * of yacc/bison's automatic operator-precedence handling. All other
7567 * operator names are handled by the generic productions using "Op",
7568 * below; and all those operators will have the same precedence.
7570 * If you add more explicitly-known operators, be sure to add them
7571 * also to b_expr and to the MathOp list above.
7573 | '+' a_expr %prec UMINUS
7574 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7575 | '-' a_expr %prec UMINUS
7576 { $$ = doNegate($2, @1); }
7578 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7580 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7582 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7584 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7586 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7588 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7590 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7592 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7594 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7596 | a_expr qual_Op a_expr %prec Op
7597 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7598 | qual_Op a_expr %prec Op
7599 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7600 | a_expr qual_Op %prec POSTFIXOP
7601 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7604 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
7606 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
7608 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
7610 | a_expr LIKE a_expr
7611 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
7612 | a_expr LIKE a_expr ESCAPE a_expr
7614 FuncCall *n = makeNode(FuncCall);
7615 n->funcname = SystemFuncName("like_escape");
7616 n->args = list_make2($3, $5);
7617 n->agg_star = FALSE;
7618 n->agg_distinct = FALSE;
7619 n->func_variadic = FALSE;
7621 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
7623 | a_expr NOT LIKE a_expr
7624 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
7625 | a_expr NOT LIKE a_expr ESCAPE a_expr
7627 FuncCall *n = makeNode(FuncCall);
7628 n->funcname = SystemFuncName("like_escape");
7629 n->args = list_make2($4, $6);
7630 n->agg_star = FALSE;
7631 n->agg_distinct = FALSE;
7632 n->func_variadic = FALSE;
7634 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
7636 | a_expr ILIKE a_expr
7637 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
7638 | a_expr ILIKE a_expr ESCAPE a_expr
7640 FuncCall *n = makeNode(FuncCall);
7641 n->funcname = SystemFuncName("like_escape");
7642 n->args = list_make2($3, $5);
7643 n->agg_star = FALSE;
7644 n->agg_distinct = FALSE;
7645 n->func_variadic = FALSE;
7647 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
7649 | a_expr NOT ILIKE a_expr
7650 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
7651 | a_expr NOT ILIKE a_expr ESCAPE a_expr
7653 FuncCall *n = makeNode(FuncCall);
7654 n->funcname = SystemFuncName("like_escape");
7655 n->args = list_make2($4, $6);
7656 n->agg_star = FALSE;
7657 n->agg_distinct = FALSE;
7658 n->func_variadic = FALSE;
7660 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
7663 | a_expr SIMILAR TO a_expr %prec SIMILAR
7665 FuncCall *n = makeNode(FuncCall);
7666 n->funcname = SystemFuncName("similar_escape");
7667 n->args = list_make2($4, makeNullAConst(-1));
7668 n->agg_star = FALSE;
7669 n->agg_distinct = FALSE;
7670 n->func_variadic = FALSE;
7672 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7674 | a_expr SIMILAR TO a_expr ESCAPE a_expr
7676 FuncCall *n = makeNode(FuncCall);
7677 n->funcname = SystemFuncName("similar_escape");
7678 n->args = list_make2($4, $6);
7679 n->agg_star = FALSE;
7680 n->agg_distinct = FALSE;
7681 n->func_variadic = FALSE;
7683 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7685 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
7687 FuncCall *n = makeNode(FuncCall);
7688 n->funcname = SystemFuncName("similar_escape");
7689 n->args = list_make2($5, makeNullAConst(-1));
7690 n->agg_star = FALSE;
7691 n->agg_distinct = FALSE;
7692 n->func_variadic = FALSE;
7694 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7696 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
7698 FuncCall *n = makeNode(FuncCall);
7699 n->funcname = SystemFuncName("similar_escape");
7700 n->args = list_make2($5, $7);
7701 n->agg_star = FALSE;
7702 n->agg_distinct = FALSE;
7703 n->func_variadic = FALSE;
7705 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7709 * Define SQL92-style Null test clause.
7710 * Allow two forms described in the standard:
7713 * Allow two SQL extensions
7719 NullTest *n = makeNode(NullTest);
7720 n->arg = (Expr *) $1;
7721 n->nulltesttype = IS_NULL;
7726 NullTest *n = makeNode(NullTest);
7727 n->arg = (Expr *) $1;
7728 n->nulltesttype = IS_NULL;
7731 | a_expr IS NOT NULL_P
7733 NullTest *n = makeNode(NullTest);
7734 n->arg = (Expr *) $1;
7735 n->nulltesttype = IS_NOT_NULL;
7740 NullTest *n = makeNode(NullTest);
7741 n->arg = (Expr *) $1;
7742 n->nulltesttype = IS_NOT_NULL;
7747 $$ = (Node *)makeOverlaps($1, $3, @2);
7751 BooleanTest *b = makeNode(BooleanTest);
7752 b->arg = (Expr *) $1;
7753 b->booltesttype = IS_TRUE;
7756 | a_expr IS NOT TRUE_P
7758 BooleanTest *b = makeNode(BooleanTest);
7759 b->arg = (Expr *) $1;
7760 b->booltesttype = IS_NOT_TRUE;
7765 BooleanTest *b = makeNode(BooleanTest);
7766 b->arg = (Expr *) $1;
7767 b->booltesttype = IS_FALSE;
7770 | a_expr IS NOT FALSE_P
7772 BooleanTest *b = makeNode(BooleanTest);
7773 b->arg = (Expr *) $1;
7774 b->booltesttype = IS_NOT_FALSE;
7779 BooleanTest *b = makeNode(BooleanTest);
7780 b->arg = (Expr *) $1;
7781 b->booltesttype = IS_UNKNOWN;
7784 | a_expr IS NOT UNKNOWN
7786 BooleanTest *b = makeNode(BooleanTest);
7787 b->arg = (Expr *) $1;
7788 b->booltesttype = IS_NOT_UNKNOWN;
7791 | a_expr IS DISTINCT FROM a_expr %prec IS
7793 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7795 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
7797 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7798 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
7803 | a_expr IS OF '(' type_list ')' %prec IS
7805 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7807 | a_expr IS NOT OF '(' type_list ')' %prec IS
7809 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7811 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7813 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7814 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7815 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7818 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7820 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7821 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7822 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7825 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7827 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7828 (Node *) makeA_Expr(AEXPR_AND, NIL,
7829 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7830 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7832 (Node *) makeA_Expr(AEXPR_AND, NIL,
7833 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
7834 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
7838 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7840 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7841 (Node *) makeA_Expr(AEXPR_OR, NIL,
7842 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7843 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7845 (Node *) makeA_Expr(AEXPR_OR, NIL,
7846 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
7847 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
7851 | a_expr IN_P in_expr
7853 /* in_expr returns a SubLink or a list of a_exprs */
7854 if (IsA($3, SubLink))
7856 /* generate foo = ANY (subquery) */
7857 SubLink *n = (SubLink *) $3;
7858 n->subLinkType = ANY_SUBLINK;
7860 n->operName = list_make1(makeString("="));
7866 /* generate scalar IN expression */
7867 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
7870 | a_expr NOT IN_P in_expr
7872 /* in_expr returns a SubLink or a list of a_exprs */
7873 if (IsA($4, SubLink))
7875 /* generate NOT (foo = ANY (subquery)) */
7876 /* Make an = ANY node */
7877 SubLink *n = (SubLink *) $4;
7878 n->subLinkType = ANY_SUBLINK;
7880 n->operName = list_make1(makeString("="));
7882 /* Stick a NOT on top */
7883 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
7887 /* generate scalar NOT IN expression */
7888 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
7891 | a_expr subquery_Op sub_type select_with_parens %prec Op
7893 SubLink *n = makeNode(SubLink);
7894 n->subLinkType = $3;
7901 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
7903 if ($3 == ANY_SUBLINK)
7904 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
7906 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
7908 | UNIQUE select_with_parens
7910 /* Not sure how to get rid of the parentheses
7911 * but there are lots of shift/reduce errors without them.
7913 * Should be able to implement this by plopping the entire
7914 * select into a node, then transforming the target expressions
7915 * from whatever they are into count(*), and testing the
7916 * entire result equal to one.
7917 * But, will probably implement a separate node in the executor.
7920 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7921 errmsg("UNIQUE predicate is not yet implemented"),
7922 scanner_errposition(@1)));
7924 | a_expr IS DOCUMENT_P %prec IS
7926 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
7927 list_make1($1), @2);
7929 | a_expr IS NOT DOCUMENT_P %prec IS
7931 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7932 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
7933 list_make1($1), @2),
7939 * Restricted expressions
7941 * b_expr is a subset of the complete expression syntax defined by a_expr.
7943 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
7944 * cause trouble in the places where b_expr is used. For simplicity, we
7945 * just eliminate all the boolean-keyword-operator productions from b_expr.
7949 | b_expr TYPECAST Typename
7950 { $$ = makeTypeCast($1, $3, @2); }
7951 | '+' b_expr %prec UMINUS
7952 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7953 | '-' b_expr %prec UMINUS
7954 { $$ = doNegate($2, @1); }
7956 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7958 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7960 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7962 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7964 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7966 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7968 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7970 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7972 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7973 | b_expr qual_Op b_expr %prec Op
7974 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7975 | qual_Op b_expr %prec Op
7976 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7977 | b_expr qual_Op %prec POSTFIXOP
7978 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7979 | b_expr IS DISTINCT FROM b_expr %prec IS
7981 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7983 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
7985 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
7986 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
7988 | b_expr IS OF '(' type_list ')' %prec IS
7990 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7992 | b_expr IS NOT OF '(' type_list ')' %prec IS
7994 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7996 | b_expr IS DOCUMENT_P %prec IS
7998 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
7999 list_make1($1), @2);
8001 | b_expr IS NOT DOCUMENT_P %prec IS
8003 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8004 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8005 list_make1($1), @2),
8011 * Productions that can be used in both a_expr and b_expr.
8013 * Note: productions that refer recursively to a_expr or b_expr mostly
8014 * cannot appear here. However, it's OK to refer to a_exprs that occur
8015 * inside parentheses, such as function arguments; that cannot introduce
8016 * ambiguity to the b_expr syntax.
8018 c_expr: columnref { $$ = $1; }
8019 | AexprConst { $$ = $1; }
8020 | PARAM opt_indirection
8022 ParamRef *p = makeNode(ParamRef);
8027 A_Indirection *n = makeNode(A_Indirection);
8028 n->arg = (Node *) p;
8029 n->indirection = check_indirection($2);
8035 | '(' a_expr ')' opt_indirection
8039 A_Indirection *n = makeNode(A_Indirection);
8041 n->indirection = check_indirection($4);
8051 | select_with_parens %prec UMINUS
8053 SubLink *n = makeNode(SubLink);
8054 n->subLinkType = EXPR_SUBLINK;
8061 | EXISTS select_with_parens
8063 SubLink *n = makeNode(SubLink);
8064 n->subLinkType = EXISTS_SUBLINK;
8071 | ARRAY select_with_parens
8073 SubLink *n = makeNode(SubLink);
8074 n->subLinkType = ARRAY_SUBLINK;
8083 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8084 Assert(IsA(n, A_ArrayExpr));
8085 /* point outermost A_ArrayExpr to the ARRAY keyword */
8091 RowExpr *r = makeNode(RowExpr);
8093 r->row_typeid = InvalidOid; /* not analyzed yet */
8100 * func_expr is split out from c_expr just so that we have a classification
8101 * for "everything that is a function call or looks like one". This isn't
8102 * very important, but it saves us having to document which variants are
8103 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8104 * (Note that many of the special SQL functions wouldn't actually make any
8105 * sense as functional index entries, but we ignore that consideration here.)
8107 func_expr: func_name '(' ')'
8109 FuncCall *n = makeNode(FuncCall);
8112 n->agg_star = FALSE;
8113 n->agg_distinct = FALSE;
8114 n->func_variadic = FALSE;
8118 | func_name '(' expr_list ')'
8120 FuncCall *n = makeNode(FuncCall);
8123 n->agg_star = FALSE;
8124 n->agg_distinct = FALSE;
8125 n->func_variadic = FALSE;
8129 | func_name '(' VARIADIC a_expr ')'
8131 FuncCall *n = makeNode(FuncCall);
8133 n->args = list_make1($4);
8134 n->agg_star = FALSE;
8135 n->agg_distinct = FALSE;
8136 n->func_variadic = TRUE;
8140 | func_name '(' expr_list ',' VARIADIC a_expr ')'
8142 FuncCall *n = makeNode(FuncCall);
8144 n->args = lappend($3, $6);
8145 n->agg_star = FALSE;
8146 n->agg_distinct = FALSE;
8147 n->func_variadic = TRUE;
8151 | func_name '(' ALL expr_list ')'
8153 FuncCall *n = makeNode(FuncCall);
8156 n->agg_star = FALSE;
8157 n->agg_distinct = FALSE;
8158 /* Ideally we'd mark the FuncCall node to indicate
8159 * "must be an aggregate", but there's no provision
8160 * for that in FuncCall at the moment.
8162 n->func_variadic = FALSE;
8166 | func_name '(' DISTINCT expr_list ')'
8168 FuncCall *n = makeNode(FuncCall);
8171 n->agg_star = FALSE;
8172 n->agg_distinct = TRUE;
8173 n->func_variadic = FALSE;
8177 | func_name '(' '*' ')'
8180 * We consider AGGREGATE(*) to invoke a parameterless
8181 * aggregate. This does the right thing for COUNT(*),
8182 * and there are no other aggregates in SQL92 that accept
8185 * The FuncCall node is also marked agg_star = true,
8186 * so that later processing can detect what the argument
8189 FuncCall *n = makeNode(FuncCall);
8193 n->agg_distinct = FALSE;
8194 n->func_variadic = FALSE;
8201 * Translate as "'now'::text::date".
8203 * We cannot use "'now'::date" because coerce_type() will
8204 * immediately reduce that to a constant representing
8205 * today's date. We need to delay the conversion until
8206 * runtime, else the wrong things will happen when
8207 * CURRENT_DATE is used in a column default value or rule.
8209 * This could be simplified if we had a way to generate
8210 * an expression tree representing runtime application
8211 * of type-input conversion functions. (As of PG 7.3
8212 * that is actually possible, but not clear that we want
8216 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8217 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8222 * Translate as "'now'::text::timetz".
8223 * See comments for CURRENT_DATE.
8226 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8227 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8229 | CURRENT_TIME '(' Iconst ')'
8232 * Translate as "'now'::text::timetz(n)".
8233 * See comments for CURRENT_DATE.
8237 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8238 d = SystemTypeName("timetz");
8239 d->typmods = list_make1(makeIntConst($3, @3));
8240 $$ = makeTypeCast(n, d, -1);
8245 * Translate as "now()", since we have a function that
8246 * does exactly what is needed.
8248 FuncCall *n = makeNode(FuncCall);
8249 n->funcname = SystemFuncName("now");
8251 n->agg_star = FALSE;
8252 n->agg_distinct = FALSE;
8253 n->func_variadic = FALSE;
8257 | CURRENT_TIMESTAMP '(' Iconst ')'
8260 * Translate as "'now'::text::timestamptz(n)".
8261 * See comments for CURRENT_DATE.
8265 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8266 d = SystemTypeName("timestamptz");
8267 d->typmods = list_make1(makeIntConst($3, @3));
8268 $$ = makeTypeCast(n, d, -1);
8273 * Translate as "'now'::text::time".
8274 * See comments for CURRENT_DATE.
8277 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8278 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8280 | LOCALTIME '(' Iconst ')'
8283 * Translate as "'now'::text::time(n)".
8284 * See comments for CURRENT_DATE.
8288 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8289 d = SystemTypeName("time");
8290 d->typmods = list_make1(makeIntConst($3, @3));
8291 $$ = makeTypeCast((Node *)n, d, -1);
8296 * Translate as "'now'::text::timestamp".
8297 * See comments for CURRENT_DATE.
8300 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8301 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8303 | LOCALTIMESTAMP '(' Iconst ')'
8306 * Translate as "'now'::text::timestamp(n)".
8307 * See comments for CURRENT_DATE.
8311 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8312 d = SystemTypeName("timestamp");
8313 d->typmods = list_make1(makeIntConst($3, @3));
8314 $$ = makeTypeCast(n, d, -1);
8318 FuncCall *n = makeNode(FuncCall);
8319 n->funcname = SystemFuncName("current_user");
8321 n->agg_star = FALSE;
8322 n->agg_distinct = FALSE;
8323 n->func_variadic = FALSE;
8329 FuncCall *n = makeNode(FuncCall);
8330 n->funcname = SystemFuncName("current_user");
8332 n->agg_star = FALSE;
8333 n->agg_distinct = FALSE;
8334 n->func_variadic = FALSE;
8340 FuncCall *n = makeNode(FuncCall);
8341 n->funcname = SystemFuncName("session_user");
8343 n->agg_star = FALSE;
8344 n->agg_distinct = FALSE;
8345 n->func_variadic = FALSE;
8351 FuncCall *n = makeNode(FuncCall);
8352 n->funcname = SystemFuncName("current_user");
8354 n->agg_star = FALSE;
8355 n->agg_distinct = FALSE;
8356 n->func_variadic = FALSE;
8360 | CAST '(' a_expr AS Typename ')'
8361 { $$ = makeTypeCast($3, $5, @1); }
8362 | EXTRACT '(' extract_list ')'
8364 FuncCall *n = makeNode(FuncCall);
8365 n->funcname = SystemFuncName("date_part");
8367 n->agg_star = FALSE;
8368 n->agg_distinct = FALSE;
8369 n->func_variadic = FALSE;
8373 | OVERLAY '(' overlay_list ')'
8375 /* overlay(A PLACING B FROM C FOR D) is converted to
8376 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8377 * overlay(A PLACING B FROM C) is converted to
8378 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8380 FuncCall *n = makeNode(FuncCall);
8381 n->funcname = SystemFuncName("overlay");
8383 n->agg_star = FALSE;
8384 n->agg_distinct = FALSE;
8385 n->func_variadic = FALSE;
8389 | POSITION '(' position_list ')'
8391 /* position(A in B) is converted to position(B, A) */
8392 FuncCall *n = makeNode(FuncCall);
8393 n->funcname = SystemFuncName("position");
8395 n->agg_star = FALSE;
8396 n->agg_distinct = FALSE;
8397 n->func_variadic = FALSE;
8401 | SUBSTRING '(' substr_list ')'
8403 /* substring(A from B for C) is converted to
8404 * substring(A, B, C) - thomas 2000-11-28
8406 FuncCall *n = makeNode(FuncCall);
8407 n->funcname = SystemFuncName("substring");
8409 n->agg_star = FALSE;
8410 n->agg_distinct = FALSE;
8411 n->func_variadic = FALSE;
8415 | TREAT '(' a_expr AS Typename ')'
8417 /* TREAT(expr AS target) converts expr of a particular type to target,
8418 * which is defined to be a subtype of the original expression.
8419 * In SQL99, this is intended for use with structured UDTs,
8420 * but let's make this a generally useful form allowing stronger
8421 * coercions than are handled by implicit casting.
8423 FuncCall *n = makeNode(FuncCall);
8424 /* Convert SystemTypeName() to SystemFuncName() even though
8425 * at the moment they result in the same thing.
8427 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8428 n->args = list_make1($3);
8429 n->agg_star = FALSE;
8430 n->agg_distinct = FALSE;
8431 n->func_variadic = FALSE;
8435 | TRIM '(' BOTH trim_list ')'
8437 /* various trim expressions are defined in SQL92
8438 * - thomas 1997-07-19
8440 FuncCall *n = makeNode(FuncCall);
8441 n->funcname = SystemFuncName("btrim");
8443 n->agg_star = FALSE;
8444 n->agg_distinct = FALSE;
8445 n->func_variadic = FALSE;
8449 | TRIM '(' LEADING trim_list ')'
8451 FuncCall *n = makeNode(FuncCall);
8452 n->funcname = SystemFuncName("ltrim");
8454 n->agg_star = FALSE;
8455 n->agg_distinct = FALSE;
8456 n->func_variadic = FALSE;
8460 | TRIM '(' TRAILING trim_list ')'
8462 FuncCall *n = makeNode(FuncCall);
8463 n->funcname = SystemFuncName("rtrim");
8465 n->agg_star = FALSE;
8466 n->agg_distinct = FALSE;
8467 n->func_variadic = FALSE;
8471 | TRIM '(' trim_list ')'
8473 FuncCall *n = makeNode(FuncCall);
8474 n->funcname = SystemFuncName("btrim");
8476 n->agg_star = FALSE;
8477 n->agg_distinct = FALSE;
8478 n->func_variadic = FALSE;
8482 | NULLIF '(' a_expr ',' a_expr ')'
8484 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
8486 | COALESCE '(' expr_list ')'
8488 CoalesceExpr *c = makeNode(CoalesceExpr);
8493 | GREATEST '(' expr_list ')'
8495 MinMaxExpr *v = makeNode(MinMaxExpr);
8497 v->op = IS_GREATEST;
8501 | LEAST '(' expr_list ')'
8503 MinMaxExpr *v = makeNode(MinMaxExpr);
8509 | XMLCONCAT '(' expr_list ')'
8511 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
8513 | XMLELEMENT '(' NAME_P ColLabel ')'
8515 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
8517 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
8519 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
8521 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
8523 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
8525 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
8527 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
8529 | XMLFOREST '(' xml_attribute_list ')'
8531 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
8533 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
8535 XmlExpr *x = (XmlExpr *)
8536 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
8537 list_make2($4, makeBoolAConst($5, -1)),
8542 | XMLPI '(' NAME_P ColLabel ')'
8544 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
8546 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
8548 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
8550 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
8552 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
8553 list_make3($3, $5, $6), @1);
8555 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
8557 XmlSerialize *n = makeNode(XmlSerialize);
8569 xml_root_version: VERSION_P a_expr
8571 | VERSION_P NO VALUE_P
8572 { $$ = makeNullAConst(-1); }
8575 opt_xml_root_standalone: ',' STANDALONE_P YES_P
8576 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
8577 | ',' STANDALONE_P NO
8578 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
8579 | ',' STANDALONE_P NO VALUE_P
8580 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
8582 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
8585 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
8588 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
8589 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
8592 xml_attribute_el: a_expr AS ColLabel
8594 $$ = makeNode(ResTarget);
8596 $$->indirection = NIL;
8597 $$->val = (Node *) $1;
8602 $$ = makeNode(ResTarget);
8604 $$->indirection = NIL;
8605 $$->val = (Node *) $1;
8610 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
8611 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
8614 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
8615 | STRIP_P WHITESPACE_P { $$ = FALSE; }
8616 | /*EMPTY*/ { $$ = FALSE; }
8620 * Supporting nonterminals for expressions.
8623 /* Explicit row production.
8625 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
8626 * without conflicting with the parenthesized a_expr production. Without the
8627 * ROW keyword, there must be more than one a_expr inside the parens.
8629 row: ROW '(' expr_list ')' { $$ = $3; }
8630 | ROW '(' ')' { $$ = NIL; }
8631 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
8634 sub_type: ANY { $$ = ANY_SUBLINK; }
8635 | SOME { $$ = ANY_SUBLINK; }
8636 | ALL { $$ = ALL_SUBLINK; }
8639 all_Op: Op { $$ = $1; }
8640 | MathOp { $$ = $1; }
8643 MathOp: '+' { $$ = "+"; }
8655 { $$ = list_make1(makeString($1)); }
8656 | OPERATOR '(' any_operator ')'
8662 { $$ = list_make1(makeString($1)); }
8663 | OPERATOR '(' any_operator ')'
8669 { $$ = list_make1(makeString($1)); }
8670 | OPERATOR '(' any_operator ')'
8673 { $$ = list_make1(makeString("~~")); }
8675 { $$ = list_make1(makeString("!~~")); }
8677 { $$ = list_make1(makeString("~~*")); }
8679 { $$ = list_make1(makeString("!~~*")); }
8680 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
8681 * the regular expression is preprocessed by a function (similar_escape),
8682 * and the ~ operator for posix regular expressions is used.
8683 * x SIMILAR TO y -> x ~ similar_escape(y)
8684 * this transformation is made on the fly by the parser upwards.
8685 * however the SubLink structure which handles any/some/all stuff
8686 * is not ready for such a thing.
8692 $$ = list_make1($1);
8694 | expr_list ',' a_expr
8696 $$ = lappend($1, $3);
8700 type_list: Typename { $$ = list_make1($1); }
8701 | type_list ',' Typename { $$ = lappend($1, $3); }
8704 array_expr: '[' expr_list ']'
8706 $$ = makeAArrayExpr($2, @1);
8708 | '[' array_expr_list ']'
8710 $$ = makeAArrayExpr($2, @1);
8714 $$ = makeAArrayExpr(NIL, @1);
8718 array_expr_list: array_expr { $$ = list_make1($1); }
8719 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
8724 extract_arg FROM a_expr
8726 $$ = list_make2(makeStringConst($1, @1), $3);
8728 | /*EMPTY*/ { $$ = NIL; }
8731 /* Allow delimited string SCONST in extract_arg as an SQL extension.
8732 * - thomas 2001-04-12
8736 | YEAR_P { $$ = "year"; }
8737 | MONTH_P { $$ = "month"; }
8738 | DAY_P { $$ = "day"; }
8739 | HOUR_P { $$ = "hour"; }
8740 | MINUTE_P { $$ = "minute"; }
8741 | SECOND_P { $$ = "second"; }
8742 | SCONST { $$ = $1; }
8745 /* OVERLAY() arguments
8746 * SQL99 defines the OVERLAY() function:
8747 * o overlay(text placing text from int for int)
8748 * o overlay(text placing text from int)
8751 a_expr overlay_placing substr_from substr_for
8753 $$ = list_make4($1, $2, $3, $4);
8755 | a_expr overlay_placing substr_from
8757 $$ = list_make3($1, $2, $3);
8766 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
8769 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
8770 | /*EMPTY*/ { $$ = NIL; }
8773 /* SUBSTRING() arguments
8774 * SQL9x defines a specific syntax for arguments to SUBSTRING():
8775 * o substring(text from int for int)
8776 * o substring(text from int) get entire string from starting point "int"
8777 * o substring(text for int) get first "int" characters of string
8778 * o substring(text from pattern) get entire string matching pattern
8779 * o substring(text from pattern for escape) same with specified escape char
8780 * We also want to support generic substring functions which accept
8781 * the usual generic list of arguments. So we will accept both styles
8782 * here, and convert the SQL9x style to the generic list for further
8783 * processing. - thomas 2000-11-28
8786 a_expr substr_from substr_for
8788 $$ = list_make3($1, $2, $3);
8790 | a_expr substr_for substr_from
8792 /* not legal per SQL99, but might as well allow it */
8793 $$ = list_make3($1, $3, $2);
8795 | a_expr substr_from
8797 $$ = list_make2($1, $2);
8802 * Since there are no cases where this syntax allows
8803 * a textual FOR value, we forcibly cast the argument
8804 * to int4. The possible matches in pg_proc are
8805 * substring(text,int4) and substring(text,text),
8806 * and we don't want the parser to choose the latter,
8807 * which it is likely to do if the second argument
8808 * is unknown or doesn't have an implicit cast to int4.
8810 $$ = list_make3($1, makeIntConst(1, -1),
8812 SystemTypeName("int4"), -1));
8823 FROM a_expr { $$ = $2; }
8826 substr_for: FOR a_expr { $$ = $2; }
8829 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
8830 | FROM expr_list { $$ = $2; }
8831 | expr_list { $$ = $1; }
8834 in_expr: select_with_parens
8836 SubLink *n = makeNode(SubLink);
8838 /* other fields will be filled later */
8841 | '(' expr_list ')' { $$ = (Node *)$2; }
8845 * Define SQL92-style case clause.
8846 * - Full specification
8847 * CASE WHEN a = b THEN c ... ELSE d END
8848 * - Implicit argument
8849 * CASE a WHEN b THEN c ... ELSE d END
8851 case_expr: CASE case_arg when_clause_list case_default END_P
8853 CaseExpr *c = makeNode(CaseExpr);
8854 c->casetype = InvalidOid; /* not analyzed yet */
8855 c->arg = (Expr *) $2;
8857 c->defresult = (Expr *) $4;
8864 /* There must be at least one */
8865 when_clause { $$ = list_make1($1); }
8866 | when_clause_list when_clause { $$ = lappend($1, $2); }
8870 WHEN a_expr THEN a_expr
8872 CaseWhen *w = makeNode(CaseWhen);
8873 w->expr = (Expr *) $2;
8874 w->result = (Expr *) $4;
8881 ELSE a_expr { $$ = $2; }
8882 | /*EMPTY*/ { $$ = NULL; }
8885 case_arg: a_expr { $$ = $1; }
8886 | /*EMPTY*/ { $$ = NULL; }
8890 * columnref starts with relation_name not ColId, so that OLD and NEW
8891 * references can be accepted. Note that when there are more than two
8892 * dotted names, the first name is not actually a relation name...
8894 columnref: relation_name
8896 $$ = makeColumnRef($1, NIL, @1);
8898 | relation_name indirection
8900 $$ = makeColumnRef($1, $2, @1);
8907 $$ = (Node *) makeString($2);
8911 $$ = (Node *) makeNode(A_Star);
8915 A_Indices *ai = makeNode(A_Indices);
8920 | '[' a_expr ':' a_expr ']'
8922 A_Indices *ai = makeNode(A_Indices);
8930 indirection_el { $$ = list_make1($1); }
8931 | indirection indirection_el { $$ = lappend($1, $2); }
8935 /*EMPTY*/ { $$ = NIL; }
8936 | opt_indirection indirection_el { $$ = lappend($1, $2); }
8939 opt_asymmetric: ASYMMETRIC
8944 * The SQL spec defines "contextually typed value expressions" and
8945 * "contextually typed row value constructors", which for our purposes
8946 * are the same as "a_expr" and "row" except that DEFAULT can appear at
8951 a_expr { $$ = (Node *) $1; }
8954 SetToDefault *n = makeNode(SetToDefault);
8961 ctext_expr { $$ = list_make1($1); }
8962 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
8966 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
8967 * making VALUES a fully reserved word, which will probably break more apps
8968 * than allowing the noise-word is worth.
8970 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
8974 /*****************************************************************************
8976 * target list for SELECT
8978 *****************************************************************************/
8981 target_el { $$ = list_make1($1); }
8982 | target_list ',' target_el { $$ = lappend($1, $3); }
8985 target_el: a_expr AS ColLabel
8987 $$ = makeNode(ResTarget);
8989 $$->indirection = NIL;
8990 $$->val = (Node *)$1;
8994 * We support omitting AS only for column labels that aren't
8995 * any known keyword. There is an ambiguity against postfix
8996 * operators: is "a ! b" an infix expression, or a postfix
8997 * expression and a column label? We prefer to resolve this
8998 * as an infix expression, which we accomplish by assigning
8999 * IDENT a precedence higher than POSTFIXOP.
9003 $$ = makeNode(ResTarget);
9005 $$->indirection = NIL;
9006 $$->val = (Node *)$1;
9011 $$ = makeNode(ResTarget);
9013 $$->indirection = NIL;
9014 $$->val = (Node *)$1;
9019 ColumnRef *n = makeNode(ColumnRef);
9020 n->fields = list_make1(makeNode(A_Star));
9023 $$ = makeNode(ResTarget);
9025 $$->indirection = NIL;
9026 $$->val = (Node *)n;
9032 /*****************************************************************************
9034 * Names and constants
9036 *****************************************************************************/
9039 SpecialRuleRelation { $$ = $1; }
9040 | ColId { $$ = $1; }
9043 qualified_name_list:
9044 qualified_name { $$ = list_make1($1); }
9045 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9049 * The production for a qualified relation name has to exactly match the
9050 * production for a qualified func_name, because in a FROM clause we cannot
9051 * tell which we are parsing until we see what comes after it ('(' for a
9052 * func_name, something else for a relation). Therefore we allow 'indirection'
9053 * which may contain subscripts, and reject that case in the C code.
9058 $$ = makeNode(RangeVar);
9059 $$->catalogname = NULL;
9060 $$->schemaname = NULL;
9064 | relation_name indirection
9066 check_qualified_name($2);
9067 $$ = makeNode(RangeVar);
9068 switch (list_length($2))
9071 $$->catalogname = NULL;
9072 $$->schemaname = $1;
9073 $$->relname = strVal(linitial($2));
9076 $$->catalogname = $1;
9077 $$->schemaname = strVal(linitial($2));
9078 $$->relname = strVal(lsecond($2));
9082 (errcode(ERRCODE_SYNTAX_ERROR),
9083 errmsg("improper qualified name (too many dotted names): %s",
9084 NameListToString(lcons(makeString($1), $2))),
9085 scanner_errposition(@1)));
9093 { $$ = list_make1(makeString($1)); }
9094 | name_list ',' name
9095 { $$ = lappend($1, makeString($3)); }
9099 name: ColId { $$ = $1; };
9107 attr_name: ColLabel { $$ = $1; };
9109 index_name: ColId { $$ = $1; };
9111 file_name: Sconst { $$ = $1; };
9114 * The production for a qualified func_name has to exactly match the
9115 * production for a qualified columnref, because we cannot tell which we
9116 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9117 * anything else for a columnref). Therefore we allow 'indirection' which
9118 * may contain subscripts, and reject that case in the C code. (If we
9119 * ever implement SQL99-like methods, such syntax may actually become legal!)
9121 func_name: type_function_name
9122 { $$ = list_make1(makeString($1)); }
9123 | relation_name indirection
9124 { $$ = check_func_name(lcons(makeString($1), $2)); }
9133 $$ = makeIntConst($1, @1);
9137 $$ = makeFloatConst($1, @1);
9141 $$ = makeStringConst($1, @1);
9145 $$ = makeBitStringConst($1, @1);
9149 /* This is a bit constant per SQL99:
9150 * Without Feature F511, "BIT data type",
9151 * a <general literal> shall not be a
9152 * <bit string literal> or a <hex string literal>.
9154 $$ = makeBitStringConst($1, @1);
9158 /* generic type 'literal' syntax */
9159 TypeName *t = makeTypeNameFromNameList($1);
9161 $$ = makeStringConstCast($2, @2, t);
9163 | func_name '(' expr_list ')' Sconst
9165 /* generic syntax with a type modifier */
9166 TypeName *t = makeTypeNameFromNameList($1);
9169 $$ = makeStringConstCast($5, @5, t);
9171 | ConstTypename Sconst
9173 $$ = makeStringConstCast($2, @2, $1);
9175 | ConstInterval Sconst opt_interval
9179 $$ = makeStringConstCast($2, @2, t);
9181 | ConstInterval '(' Iconst ')' Sconst opt_interval
9186 if (list_length($6) != 1)
9188 (errcode(ERRCODE_SYNTAX_ERROR),
9189 errmsg("interval precision specified twice"),
9190 scanner_errposition(@1)));
9191 t->typmods = lappend($6, makeIntConst($3, @3));
9194 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9195 makeIntConst($3, @3));
9196 $$ = makeStringConstCast($5, @5, t);
9200 $$ = makeBoolAConst(TRUE, @1);
9204 $$ = makeBoolAConst(FALSE, @1);
9208 $$ = makeNullAConst(@1);
9212 Iconst: ICONST { $$ = $1; };
9213 Sconst: SCONST { $$ = $1; };
9214 RoleId: ColId { $$ = $1; };
9216 SignedIconst: ICONST { $$ = $1; }
9217 | '-' ICONST { $$ = - $2; }
9221 * Name classification hierarchy.
9223 * IDENT is the lexeme returned by the lexer for identifiers that match
9224 * no known keyword. In most cases, we can accept certain keywords as
9225 * names, not only IDENTs. We prefer to accept as many such keywords
9226 * as possible to minimize the impact of "reserved words" on programmers.
9227 * So, we divide names into several possible classes. The classification
9228 * is chosen in part to make keywords acceptable as names wherever possible.
9231 /* Column identifier --- names that can be column, table, etc names.
9233 ColId: IDENT { $$ = $1; }
9234 | unreserved_keyword { $$ = pstrdup($1); }
9235 | col_name_keyword { $$ = pstrdup($1); }
9238 /* Type/function identifier --- names that can be type or function names.
9240 type_function_name: IDENT { $$ = $1; }
9241 | unreserved_keyword { $$ = pstrdup($1); }
9242 | type_func_name_keyword { $$ = pstrdup($1); }
9245 /* Column label --- allowed labels in "AS" clauses.
9246 * This presently includes *all* Postgres keywords.
9248 ColLabel: IDENT { $$ = $1; }
9249 | unreserved_keyword { $$ = pstrdup($1); }
9250 | col_name_keyword { $$ = pstrdup($1); }
9251 | type_func_name_keyword { $$ = pstrdup($1); }
9252 | reserved_keyword { $$ = pstrdup($1); }
9257 * Keyword category lists. Generally, every keyword present in
9258 * the Postgres grammar should appear in exactly one of these lists.
9260 * Put a new keyword into the first list that it can go into without causing
9261 * shift or reduce conflicts. The earlier lists define "less reserved"
9262 * categories of keywords.
9264 * Make sure that each keyword's category in keywords.c matches where
9265 * it is listed here. (Someday we may be able to generate these lists and
9266 * keywords.c's table from a common master list.)
9269 /* "Unreserved" keywords --- available for use as any kind of name.
9510 /* Column identifier --- keywords that can be column, table, etc names.
9512 * Many of these keywords will in fact be recognized as type or function
9513 * names too; but they have special productions for the purpose, and so
9514 * can't be treated as "generic" type or function names.
9516 * The type names appearing here are not usable as function names
9517 * because they can be followed by '(' in typename productions, which
9518 * looks too much like a function call for an LR(1) parser.
9568 /* Type/function identifier --- keywords that can be type or function names.
9570 * Most of these are keywords that are used as operators in expressions;
9571 * in general such keywords can't be column names because they would be
9572 * ambiguous with variables, but they are unambiguous as function identifiers.
9574 * Do not include POSITION, SUBSTRING, etc here since they have explicit
9575 * productions in a_expr to support the goofy SQL9x argument syntax.
9576 * - thomas 2000-11-28
9578 type_func_name_keyword:
9601 /* Reserved keyword --- these keywords are usable only as a ColLabel.
9603 * Keywords appear here if they could not be distinguished from variable,
9604 * type, or function names in some contexts. Don't put things here unless
9687 SpecialRuleRelation:
9694 (errcode(ERRCODE_SYNTAX_ERROR),
9695 errmsg("OLD used in query that is not in a rule"),
9696 scanner_errposition(@1)));
9704 (errcode(ERRCODE_SYNTAX_ERROR),
9705 errmsg("NEW used in query that is not in a rule"),
9706 scanner_errposition(@1)));
9713 makeColumnRef(char *colname, List *indirection, int location)
9716 * Generate a ColumnRef node, with an A_Indirection node added if there
9717 * is any subscripting in the specified indirection list. However,
9718 * any field selection at the start of the indirection list must be
9719 * transposed into the "fields" part of the ColumnRef node.
9721 ColumnRef *c = makeNode(ColumnRef);
9725 c->location = location;
9726 foreach(l, indirection)
9728 if (IsA(lfirst(l), A_Indices))
9730 A_Indirection *i = makeNode(A_Indirection);
9734 /* easy case - all indirection goes to A_Indirection */
9735 c->fields = list_make1(makeString(colname));
9736 i->indirection = check_indirection(indirection);
9740 /* got to split the list in two */
9741 i->indirection = check_indirection(list_copy_tail(indirection,
9743 indirection = list_truncate(indirection, nfields);
9744 c->fields = lcons(makeString(colname), indirection);
9746 i->arg = (Node *) c;
9749 else if (IsA(lfirst(l), A_Star))
9751 /* We only allow '*' at the end of a ColumnRef */
9752 if (lnext(l) != NULL)
9753 yyerror("improper use of \"*\"");
9757 /* No subscripting, so all indirection gets added to field list */
9758 c->fields = lcons(makeString(colname), indirection);
9763 makeTypeCast(Node *arg, TypeName *typename, int location)
9765 TypeCast *n = makeNode(TypeCast);
9767 n->typename = typename;
9768 n->location = location;
9773 makeStringConst(char *str, int location)
9775 A_Const *n = makeNode(A_Const);
9777 n->val.type = T_String;
9778 n->val.val.str = str;
9779 n->location = location;
9785 makeStringConstCast(char *str, int location, TypeName *typename)
9787 Node *s = makeStringConst(str, location);
9789 return makeTypeCast(s, typename, -1);
9793 makeIntConst(int val, int location)
9795 A_Const *n = makeNode(A_Const);
9797 n->val.type = T_Integer;
9798 n->val.val.ival = val;
9799 n->location = location;
9805 makeFloatConst(char *str, int location)
9807 A_Const *n = makeNode(A_Const);
9809 n->val.type = T_Float;
9810 n->val.val.str = str;
9811 n->location = location;
9817 makeBitStringConst(char *str, int location)
9819 A_Const *n = makeNode(A_Const);
9821 n->val.type = T_BitString;
9822 n->val.val.str = str;
9823 n->location = location;
9829 makeNullAConst(int location)
9831 A_Const *n = makeNode(A_Const);
9833 n->val.type = T_Null;
9834 n->location = location;
9840 makeAConst(Value *v, int location)
9847 n = makeFloatConst(v->val.str, location);
9851 n = makeIntConst(v->val.ival, location);
9856 n = makeStringConst(v->val.str, location);
9864 * Create an A_Const string node and put it inside a boolean cast.
9867 makeBoolAConst(bool state, int location)
9869 A_Const *n = makeNode(A_Const);
9871 n->val.type = T_String;
9872 n->val.val.str = (state ? "t" : "f");
9873 n->location = location;
9875 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
9879 * Create and populate a FuncCall node to support the OVERLAPS operator.
9882 makeOverlaps(List *largs, List *rargs, int location)
9884 FuncCall *n = makeNode(FuncCall);
9886 n->funcname = SystemFuncName("overlaps");
9887 if (list_length(largs) == 1)
9888 largs = lappend(largs, largs);
9889 else if (list_length(largs) != 2)
9891 (errcode(ERRCODE_SYNTAX_ERROR),
9892 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
9893 scanner_errposition(location)));
9894 if (list_length(rargs) == 1)
9895 rargs = lappend(rargs, rargs);
9896 else if (list_length(rargs) != 2)
9898 (errcode(ERRCODE_SYNTAX_ERROR),
9899 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
9900 scanner_errposition(location)));
9901 n->args = list_concat(largs, rargs);
9902 n->agg_star = FALSE;
9903 n->agg_distinct = FALSE;
9904 n->func_variadic = FALSE;
9905 n->location = location;
9909 /* check_qualified_name --- check the result of qualified_name production
9911 * It's easiest to let the grammar production for qualified_name allow
9912 * subscripts and '*', which we then must reject here.
9915 check_qualified_name(List *names)
9921 if (!IsA(lfirst(i), String))
9922 yyerror("syntax error");
9926 /* check_func_name --- check the result of func_name production
9928 * It's easiest to let the grammar production for func_name allow subscripts
9929 * and '*', which we then must reject here.
9932 check_func_name(List *names)
9938 if (!IsA(lfirst(i), String))
9939 yyerror("syntax error");
9944 /* check_indirection --- check the result of indirection production
9946 * We only allow '*' at the end of the list, but it's hard to enforce that
9947 * in the grammar, so do it here.
9950 check_indirection(List *indirection)
9954 foreach(l, indirection)
9956 if (IsA(lfirst(l), A_Star))
9958 if (lnext(l) != NULL)
9959 yyerror("improper use of \"*\"");
9965 /* extractArgTypes()
9966 * Given a list of FunctionParameter nodes, extract a list of just the
9967 * argument types (TypeNames) for input parameters only. This is what
9968 * is needed to look up an existing function, which is what is wanted by
9969 * the productions that use this call.
9972 extractArgTypes(List *parameters)
9977 foreach(i, parameters)
9979 FunctionParameter *p = (FunctionParameter *) lfirst(i);
9981 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
9982 result = lappend(result, p->argType);
9987 /* findLeftmostSelect()
9988 * Find the leftmost component SelectStmt in a set-operation parsetree.
9991 findLeftmostSelect(SelectStmt *node)
9993 while (node && node->op != SETOP_NONE)
9995 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
9999 /* insertSelectOptions()
10000 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10002 * This routine is just to avoid duplicating code in SelectStmt productions.
10005 insertSelectOptions(SelectStmt *stmt,
10006 List *sortClause, List *lockingClause,
10007 Node *limitOffset, Node *limitCount,
10008 WithClause *withClause)
10010 Assert(IsA(stmt, SelectStmt));
10013 * Tests here are to reject constructs like
10014 * (SELECT foo ORDER BY bar) ORDER BY baz
10018 if (stmt->sortClause)
10020 (errcode(ERRCODE_SYNTAX_ERROR),
10021 errmsg("multiple ORDER BY clauses not allowed"),
10022 scanner_errposition(exprLocation((Node *) sortClause))));
10023 stmt->sortClause = sortClause;
10025 /* We can handle multiple locking clauses, though */
10026 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10029 if (stmt->limitOffset)
10031 (errcode(ERRCODE_SYNTAX_ERROR),
10032 errmsg("multiple OFFSET clauses not allowed"),
10033 scanner_errposition(exprLocation(limitOffset))));
10034 stmt->limitOffset = limitOffset;
10038 if (stmt->limitCount)
10040 (errcode(ERRCODE_SYNTAX_ERROR),
10041 errmsg("multiple LIMIT clauses not allowed"),
10042 scanner_errposition(exprLocation(limitCount))));
10043 stmt->limitCount = limitCount;
10047 if (stmt->withClause)
10049 (errcode(ERRCODE_SYNTAX_ERROR),
10050 errmsg("multiple WITH clauses not allowed"),
10051 scanner_errposition(exprLocation((Node *) withClause))));
10052 stmt->withClause = withClause;
10057 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10059 SelectStmt *n = makeNode(SelectStmt);
10063 n->larg = (SelectStmt *) larg;
10064 n->rarg = (SelectStmt *) rarg;
10068 /* SystemFuncName()
10069 * Build a properly-qualified reference to a built-in function.
10072 SystemFuncName(char *name)
10074 return list_make2(makeString("pg_catalog"), makeString(name));
10077 /* SystemTypeName()
10078 * Build a properly-qualified reference to a built-in type.
10080 * typmod is defaulted, but may be changed afterwards by caller.
10081 * Likewise for the location.
10084 SystemTypeName(char *name)
10086 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10087 makeString(name)));
10091 * Handle negation of a numeric constant.
10093 * Formerly, we did this here because the optimizer couldn't cope with
10094 * indexquals that looked like "var = -4" --- it wants "var = const"
10095 * and a unary minus operator applied to a constant didn't qualify.
10096 * As of Postgres 7.0, that problem doesn't exist anymore because there
10097 * is a constant-subexpression simplifier in the optimizer. However,
10098 * there's still a good reason for doing this here, which is that we can
10099 * postpone committing to a particular internal representation for simple
10100 * negative constants. It's better to leave "-123.456" in string form
10101 * until we know what the desired type is.
10104 doNegate(Node *n, int location)
10106 if (IsA(n, A_Const))
10108 A_Const *con = (A_Const *)n;
10110 /* report the constant's location as that of the '-' sign */
10111 con->location = location;
10113 if (con->val.type == T_Integer)
10115 con->val.val.ival = -con->val.val.ival;
10118 if (con->val.type == T_Float)
10120 doNegateFloat(&con->val);
10125 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10129 doNegateFloat(Value *v)
10131 char *oldval = v->val.str;
10133 Assert(IsA(v, Float));
10134 if (*oldval == '+')
10136 if (*oldval == '-')
10137 v->val.str = oldval+1; /* just strip the '-' */
10140 char *newval = (char *) palloc(strlen(oldval) + 2);
10143 strcpy(newval+1, oldval);
10144 v->val.str = newval;
10149 makeAArrayExpr(List *elements, int location)
10151 A_ArrayExpr *n = makeNode(A_ArrayExpr);
10153 n->elements = elements;
10154 n->location = location;
10159 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
10162 XmlExpr *x = makeNode(XmlExpr);
10167 * named_args is a list of ResTarget; it'll be split apart into separate
10168 * expression and name lists in transformXmlExpr().
10170 x->named_args = named_args;
10171 x->arg_names = NIL;
10173 /* xmloption, if relevant, must be filled in by caller */
10174 /* type and typmod will be filled in during parse analysis */
10175 x->location = location;
10180 * Initialize to parse one query string
10185 QueryIsRule = FALSE;
10189 * Merge the input and output parameters of a table function.
10192 mergeTableFuncParameters(List *func_args, List *columns)
10196 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
10197 foreach(lc, func_args)
10199 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
10201 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
10203 (errcode(ERRCODE_SYNTAX_ERROR),
10204 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
10207 return list_concat(func_args, columns);
10211 * Determine return type of a TABLE function. A single result column
10212 * returns setof that column's type; otherwise return setof record.
10215 TableFuncTypeName(List *columns)
10219 if (list_length(columns) == 1)
10221 FunctionParameter *p = (FunctionParameter *) linitial(columns);
10223 result = (TypeName *) copyObject(p->argType);
10226 result = SystemTypeName("record");
10228 result->setof = true;
10234 * Must undefine base_yylex before including scan.c, since we want it
10235 * to create the function base_yylex not filtered_base_yylex.