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.647 2008/12/20 16:02:55 tgl Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled during parse analysis so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "commands/defrem.h"
57 #include "nodes/makefuncs.h"
58 #include "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);
136 %name-prefix="base_yy"
147 DropBehavior dbehavior;
148 OnCommitAction oncommit;
155 FunctionParameter *fun_param;
156 FunctionParameterMode fun_param_mode;
157 FuncWithArgs *funwithargs;
159 OptionDefElem *optdef;
169 PrivTarget *privtarget;
172 VariableSetStmt *vsetstmt;
175 %type <node> stmt schema_stmt
176 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterFdwStmt
177 AlterForeignServerStmt AlterGroupStmt
178 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
179 AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
180 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
181 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
182 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
183 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
184 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
185 CreateFdwStmt CreateForeignServerStmt CreateAssertStmt CreateTrigStmt
186 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
187 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
188 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
189 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
190 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
191 DropForeignServerStmt DropUserMappingStmt ExplainStmt FetchStmt
192 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
193 LockStmt NotifyStmt ExplainableStmt PreparableStmt
194 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
195 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
196 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
197 SelectStmt TransactionStmt TruncateStmt
198 UnlistenStmt UpdateStmt VacuumStmt
199 VariableResetStmt VariableSetStmt VariableShowStmt
200 ViewStmt CheckPointStmt CreateConversionStmt
201 DeallocateStmt PrepareStmt ExecuteStmt
202 DropOwnedStmt ReassignOwnedStmt
203 AlterTSConfigurationStmt AlterTSDictionaryStmt
205 %type <node> select_no_parens select_with_parens select_clause
206 simple_select values_clause
208 %type <node> alter_column_default opclass_item opclass_drop alter_using
209 %type <ival> add_drop opt_asc_desc opt_nulls_order
211 %type <node> alter_table_cmd
212 %type <list> alter_table_cmds
214 %type <dbehavior> opt_drop_behavior
216 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
217 transaction_mode_list
218 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
219 transaction_mode_item
221 %type <ival> opt_lock lock_type cast_context
222 %type <boolean> opt_force opt_or_replace
223 opt_grant_grant_option opt_grant_admin_option
224 opt_nowait opt_if_exists opt_with_data
226 %type <list> OptRoleList
227 %type <defelt> OptRoleElem
230 %type <str> foreign_server_version opt_foreign_server_version
231 %type <str> auth_ident
233 %type <str> OptSchemaName
234 %type <list> OptSchemaEltList
236 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
237 %type <str> opt_lancompiler
239 %type <str> TriggerEvents
240 %type <value> TriggerFuncArg
242 %type <str> relation_name copy_file_name
243 database_name access_method_clause access_method attr_name
244 index_name name file_name cluster_index_specification
246 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
247 opt_class opt_validator
249 %type <range> qualified_name OptConstrFromTable
251 %type <str> all_Op MathOp SpecialRuleRelation
253 %type <str> iso_level opt_encoding
255 %type <list> grantee_list
256 %type <str> privilege
257 %type <list> privileges privilege_list
258 %type <privtarget> privilege_target
259 %type <funwithargs> function_with_argtypes
260 %type <list> function_with_argtypes_list
261 %type <chr> TriggerOneEvent
263 %type <list> stmtblock stmtmulti
264 OptTableElementList TableElementList OptInherit definition
265 OptWith opt_distinct opt_definition func_args func_args_list
266 func_args_with_defaults func_args_with_defaults_list
267 func_as createfunc_opt_list alterfunc_opt_list
268 aggr_args old_aggr_definition old_aggr_list
269 oper_argtypes RuleActionList RuleActionMulti
270 opt_column_list columnList opt_name_list
271 sort_clause opt_sort_clause sortby_list index_params
272 name_list from_clause from_list opt_array_bounds
273 qualified_name_list any_name any_name_list
274 any_operator expr_list attrs
275 target_list insert_column_list set_target_list
276 set_clause_list set_clause multiple_set_clause
277 ctext_expr_list ctext_row def_list indirection opt_indirection
278 group_clause TriggerFuncArgs select_limit
279 opt_select_limit opclass_item_list opclass_drop_list
280 opt_opfamily transaction_mode_list_or_empty
281 TableFuncElementList opt_type_modifiers
283 execute_param_clause using_clause returning_clause
284 enum_val_list table_func_column_list
285 create_generic_options alter_generic_options
287 %type <range> OptTempTableName
288 %type <into> into_clause create_as_target
290 %type <defelt> createfunc_opt_item common_func_opt_item
291 %type <fun_param> func_arg func_arg_with_default table_func_column
292 %type <fun_param_mode> arg_class
293 %type <typnam> func_return func_type
295 %type <boolean> TriggerForType OptTemp
296 %type <oncommit> OnCommitOption
298 %type <node> for_locking_item
299 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
300 %type <list> locked_rels_list
301 %type <boolean> opt_all
303 %type <node> join_outer join_qual
304 %type <jtype> join_type
306 %type <list> extract_list overlay_list position_list
307 %type <list> substr_list trim_list
308 %type <list> opt_interval interval_second
309 %type <node> overlay_placing substr_from substr_for
311 %type <boolean> opt_instead opt_analyze
312 %type <boolean> index_opt_unique opt_verbose opt_full
313 %type <boolean> opt_freeze opt_default opt_recheck
314 %type <defelt> opt_binary opt_oids copy_delimiter
316 %type <boolean> copy_from
318 %type <ival> opt_column event cursor_options opt_hold opt_set_data
319 %type <objtype> reindex_type drop_type comment_type
321 %type <node> fetch_direction select_limit_value select_offset_value
322 select_offset_value2 opt_select_fetch_first_value
323 %type <ival> row_or_rows first_or_next
325 %type <list> OptSeqOptList SeqOptList
326 %type <defelt> SeqOptElem
328 %type <istmt> insert_rest
330 %type <vsetstmt> set_rest SetResetClause
332 %type <node> TableElement ConstraintElem TableFuncElement
333 %type <node> columnDef
334 %type <defelt> def_elem old_aggr_elem
335 %type <node> def_arg columnElem where_clause where_or_current_clause
336 a_expr b_expr c_expr func_expr AexprConst indirection_el
337 columnref in_expr having_clause func_table array_expr
338 %type <list> row type_list array_expr_list
339 %type <node> case_expr case_arg when_clause case_default
340 %type <list> when_clause_list
341 %type <ival> sub_type
342 %type <list> OptCreateAs CreateAsList
343 %type <node> CreateAsElement ctext_expr
344 %type <value> NumericOnly
345 %type <alias> alias_clause
346 %type <sortby> sortby
347 %type <ielem> index_elem
348 %type <node> table_ref
349 %type <jexpr> joined_table
350 %type <range> relation_expr
351 %type <range> relation_expr_opt_alias
352 %type <target> target_el single_set_clause set_target insert_column_item
354 %type <str> generic_option_name
355 %type <node> generic_option_arg
356 %type <defelt> generic_option_elem
357 %type <optdef> alter_generic_option_elem
358 %type <list> generic_option_list alter_generic_option_list
360 %type <typnam> Typename SimpleTypename ConstTypename
361 GenericType Numeric opt_float
362 Character ConstCharacter
363 CharacterWithLength CharacterWithoutLength
364 ConstDatetime ConstInterval
365 Bit ConstBit BitWithLength BitWithoutLength
366 %type <str> character
367 %type <str> extract_arg
368 %type <str> opt_charset
369 %type <boolean> opt_varying opt_timezone
371 %type <ival> Iconst SignedIconst
372 %type <str> Sconst comment_text
373 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
374 %type <list> var_list
375 %type <str> ColId ColLabel var_name type_function_name param_name
376 %type <node> var_value zone_value
378 %type <keyword> unreserved_keyword type_func_name_keyword
379 %type <keyword> col_name_keyword reserved_keyword
381 %type <node> TableConstraint TableLikeClause
382 %type <list> TableLikeOptionList
383 %type <ival> TableLikeOption
384 %type <list> ColQualList
385 %type <node> ColConstraint ColConstraintElem ConstraintAttr
386 %type <ival> key_actions key_delete key_match key_update key_action
387 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
390 %type <list> constraints_set_list
391 %type <boolean> constraints_set_mode
392 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
393 %type <list> opt_check_option
395 %type <target> xml_attribute_el
396 %type <list> xml_attribute_list xml_attributes
397 %type <node> xml_root_version opt_xml_root_standalone
398 %type <ival> document_or_content
399 %type <boolean> xml_whitespace_option
401 %type <node> common_table_expr
402 %type <with> with_clause
403 %type <list> cte_list
407 * If you make any token changes, update the keyword table in
408 * parser/keywords.c and add new keywords to the appropriate one of
409 * the reserved-or-not-so-reserved keyword lists, below; search
410 * this file for "Name classification hierarchy".
413 /* ordinary key words in alphabetical order */
414 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
415 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
416 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
418 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
421 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
422 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
423 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
424 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
425 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
426 CREATEROLE CREATEUSER CROSS CSV CTYPE CURRENT_P
427 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
428 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
430 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
431 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
432 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
434 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
435 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
437 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
438 FREEZE FROM FULL FUNCTION
440 GLOBAL GRANT GRANTED GREATEST GROUP_P
442 HANDLER HAVING HEADER_P HOLD HOUR_P
444 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
445 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
446 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
447 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
453 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
454 LIBRARY LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
457 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
459 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
460 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
461 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
463 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
464 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
466 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
467 PRECISION PRESERVE PREPARE PREPARED PRIMARY
468 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
472 READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX RELATIVE_P RELEASE
473 RENAME REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS
474 REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
476 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
477 SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
478 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
479 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
480 SYMMETRIC SYSID SYSTEM_P
482 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
483 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
484 TRUNCATE TRUSTED TYPE_P
486 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
489 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
490 VERBOSE VERSION_P VIEW VOLATILE
492 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRAPPER WRITE
494 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
495 XMLPI XMLROOT XMLSERIALIZE
501 /* The grammar thinks these are keywords, but they are not in the keywords.c
502 * list and so can never be entered directly. The filter in parser.c
503 * creates these tokens when required.
505 %token NULLS_FIRST NULLS_LAST WITH_TIME
507 /* Special token types, not actually keywords - see the "lex" file */
508 %token <str> IDENT FCONST SCONST BCONST XCONST Op
509 %token <ival> ICONST PARAM
511 /* precedence: lowest to highest */
512 %nonassoc SET /* see relation_expr_opt_alias */
520 %nonassoc LIKE ILIKE SIMILAR
525 %left POSTFIXOP /* dummy for postfix Op rules */
526 %nonassoc IDENT /* to support target_el without AS */
527 %left Op OPERATOR /* multi-character ops and user-defined operators */
530 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
534 /* Unary Operators */
535 %left AT ZONE /* sets precedence for AT TIME ZONE */
542 * These might seem to be low-precedence, but actually they are not part
543 * of the arithmetic hierarchy at all in their use as JOIN operators.
544 * We make them high-precedence to support their use as function names.
545 * They wouldn't be given a precedence at all, were it not that we need
546 * left-associativity among the JOIN rules themselves.
548 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
549 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
550 %right PRESERVE STRIP_P
554 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
555 * psql already handles such cases, but other interfaces don't.
558 stmtblock: stmtmulti { parsetree = $1; }
561 /* the thrashing around here is to discard "empty" statements... */
562 stmtmulti: stmtmulti ';' stmt
564 $$ = lappend($1, $3);
578 | AlterDatabaseSetStmt
581 | AlterForeignServerStmt
584 | AlterObjectSchemaStmt
590 | AlterTSConfigurationStmt
591 | AlterTSDictionaryStmt
592 | AlterUserMappingStmt
605 | CreateConversionStmt
608 | CreateForeignServerStmt
618 | CreateTableSpaceStmt
622 | CreateUserMappingStmt
632 | DropForeignServerStmt
644 | DropUserMappingStmt
681 /*****************************************************************************
683 * Create a new Postgres DBMS role
685 *****************************************************************************/
688 CREATE ROLE RoleId opt_with OptRoleList
690 CreateRoleStmt *n = makeNode(CreateRoleStmt);
691 n->stmt_type = ROLESTMT_ROLE;
704 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
705 * for backwards compatibility). Note: the only option required by SQL99
706 * is "WITH ADMIN name".
709 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
710 | /* EMPTY */ { $$ = NIL; }
716 $$ = makeDefElem("password",
717 (Node *)makeString($2));
721 $$ = makeDefElem("password", NULL);
723 | ENCRYPTED PASSWORD Sconst
725 $$ = makeDefElem("encryptedPassword",
726 (Node *)makeString($3));
728 | UNENCRYPTED PASSWORD Sconst
730 $$ = makeDefElem("unencryptedPassword",
731 (Node *)makeString($3));
735 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
739 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
743 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
747 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
751 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
755 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
759 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
763 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
767 /* For backwards compatibility, synonym for SUPERUSER */
768 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
772 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
776 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
780 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
782 | CONNECTION LIMIT SignedIconst
784 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
788 $$ = makeDefElem("validUntil", (Node *)makeString($3));
790 /* Supported but not documented for roles, for use by ALTER GROUP. */
793 $$ = makeDefElem("rolemembers", (Node *)$2);
795 /* The following are not supported by ALTER ROLE/USER/GROUP */
798 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
802 $$ = makeDefElem("adminmembers", (Node *)$2);
806 $$ = makeDefElem("rolemembers", (Node *)$2);
808 | IN_P ROLE name_list
810 $$ = makeDefElem("addroleto", (Node *)$3);
812 | IN_P GROUP_P name_list
814 $$ = makeDefElem("addroleto", (Node *)$3);
819 /*****************************************************************************
821 * Create a new Postgres DBMS user (role with implied login ability)
823 *****************************************************************************/
826 CREATE USER RoleId opt_with OptRoleList
828 CreateRoleStmt *n = makeNode(CreateRoleStmt);
829 n->stmt_type = ROLESTMT_USER;
837 /*****************************************************************************
839 * Alter a postgresql DBMS role
841 *****************************************************************************/
844 ALTER ROLE RoleId opt_with OptRoleList
846 AlterRoleStmt *n = makeNode(AlterRoleStmt);
848 n->action = +1; /* add, if there are members */
855 ALTER ROLE RoleId SetResetClause
857 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
865 /*****************************************************************************
867 * Alter a postgresql DBMS user
869 *****************************************************************************/
872 ALTER USER RoleId opt_with OptRoleList
874 AlterRoleStmt *n = makeNode(AlterRoleStmt);
876 n->action = +1; /* add, if there are members */
884 ALTER USER RoleId SetResetClause
886 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
894 /*****************************************************************************
896 * Drop a postgresql DBMS role
898 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
899 * might own objects in multiple databases, there is presently no way to
900 * implement either cascading or restricting. Caveat DBA.
901 *****************************************************************************/
906 DropRoleStmt *n = makeNode(DropRoleStmt);
907 n->missing_ok = FALSE;
911 | DROP ROLE IF_P EXISTS name_list
913 DropRoleStmt *n = makeNode(DropRoleStmt);
914 n->missing_ok = TRUE;
920 /*****************************************************************************
922 * Drop a postgresql DBMS user
924 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
925 * might own objects in multiple databases, there is presently no way to
926 * implement either cascading or restricting. Caveat DBA.
927 *****************************************************************************/
932 DropRoleStmt *n = makeNode(DropRoleStmt);
933 n->missing_ok = FALSE;
937 | DROP USER IF_P EXISTS name_list
939 DropRoleStmt *n = makeNode(DropRoleStmt);
941 n->missing_ok = TRUE;
947 /*****************************************************************************
949 * Create a postgresql group (role without login ability)
951 *****************************************************************************/
954 CREATE GROUP_P RoleId opt_with OptRoleList
956 CreateRoleStmt *n = makeNode(CreateRoleStmt);
957 n->stmt_type = ROLESTMT_GROUP;
965 /*****************************************************************************
967 * Alter a postgresql group
969 *****************************************************************************/
972 ALTER GROUP_P RoleId add_drop USER name_list
974 AlterRoleStmt *n = makeNode(AlterRoleStmt);
977 n->options = list_make1(makeDefElem("rolemembers",
983 add_drop: ADD_P { $$ = +1; }
988 /*****************************************************************************
990 * Drop a postgresql group
992 * XXX see above notes about cascading DROP USER; groups have same problem.
993 *****************************************************************************/
996 DROP GROUP_P name_list
998 DropRoleStmt *n = makeNode(DropRoleStmt);
999 n->missing_ok = FALSE;
1003 | DROP GROUP_P IF_P EXISTS name_list
1005 DropRoleStmt *n = makeNode(DropRoleStmt);
1006 n->missing_ok = TRUE;
1013 /*****************************************************************************
1015 * Manipulate a schema
1017 *****************************************************************************/
1020 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1022 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1023 /* One can omit the schema name or the authorization id. */
1032 | CREATE SCHEMA ColId OptSchemaEltList
1034 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1035 /* ...but not both */
1045 | /* EMPTY */ { $$ = NULL; }
1049 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1050 | /* EMPTY */ { $$ = NIL; }
1054 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1055 * statement (in addition to by themselves).
1067 /*****************************************************************************
1069 * Set PG internal variable
1070 * SET name TO 'var_value'
1071 * Include SQL92 syntax (thomas 1997-10-22):
1072 * SET TIME ZONE 'var_value'
1074 *****************************************************************************/
1079 VariableSetStmt *n = $2;
1080 n->is_local = false;
1083 | SET LOCAL set_rest
1085 VariableSetStmt *n = $3;
1089 | SET SESSION set_rest
1091 VariableSetStmt *n = $3;
1092 n->is_local = false;
1097 set_rest: /* Generic SET syntaxes: */
1098 var_name TO var_list
1100 VariableSetStmt *n = makeNode(VariableSetStmt);
1101 n->kind = VAR_SET_VALUE;
1106 | var_name '=' var_list
1108 VariableSetStmt *n = makeNode(VariableSetStmt);
1109 n->kind = VAR_SET_VALUE;
1114 | var_name TO DEFAULT
1116 VariableSetStmt *n = makeNode(VariableSetStmt);
1117 n->kind = VAR_SET_DEFAULT;
1121 | var_name '=' DEFAULT
1123 VariableSetStmt *n = makeNode(VariableSetStmt);
1124 n->kind = VAR_SET_DEFAULT;
1128 | var_name FROM CURRENT_P
1130 VariableSetStmt *n = makeNode(VariableSetStmt);
1131 n->kind = VAR_SET_CURRENT;
1135 /* Special syntaxes mandated by SQL standard: */
1136 | TIME ZONE zone_value
1138 VariableSetStmt *n = makeNode(VariableSetStmt);
1139 n->kind = VAR_SET_VALUE;
1140 n->name = "timezone";
1142 n->args = list_make1($3);
1144 n->kind = VAR_SET_DEFAULT;
1147 | TRANSACTION transaction_mode_list
1149 VariableSetStmt *n = makeNode(VariableSetStmt);
1150 n->kind = VAR_SET_MULTI;
1151 n->name = "TRANSACTION";
1155 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1157 VariableSetStmt *n = makeNode(VariableSetStmt);
1158 n->kind = VAR_SET_MULTI;
1159 n->name = "SESSION CHARACTERISTICS";
1166 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1167 errmsg("current database cannot be changed"),
1168 scanner_errposition(@2)));
1169 $$ = NULL; /*not reached*/
1173 VariableSetStmt *n = makeNode(VariableSetStmt);
1174 n->kind = VAR_SET_VALUE;
1175 n->name = "search_path";
1176 n->args = list_make1(makeStringConst($2, @2));
1179 | NAMES opt_encoding
1181 VariableSetStmt *n = makeNode(VariableSetStmt);
1182 n->kind = VAR_SET_VALUE;
1183 n->name = "client_encoding";
1185 n->args = list_make1(makeStringConst($2, @2));
1187 n->kind = VAR_SET_DEFAULT;
1190 | ROLE ColId_or_Sconst
1192 VariableSetStmt *n = makeNode(VariableSetStmt);
1193 n->kind = VAR_SET_VALUE;
1195 n->args = list_make1(makeStringConst($2, @2));
1198 | SESSION AUTHORIZATION ColId_or_Sconst
1200 VariableSetStmt *n = makeNode(VariableSetStmt);
1201 n->kind = VAR_SET_VALUE;
1202 n->name = "session_authorization";
1203 n->args = list_make1(makeStringConst($3, @3));
1206 | SESSION AUTHORIZATION DEFAULT
1208 VariableSetStmt *n = makeNode(VariableSetStmt);
1209 n->kind = VAR_SET_DEFAULT;
1210 n->name = "session_authorization";
1213 | XML_P OPTION document_or_content
1215 VariableSetStmt *n = makeNode(VariableSetStmt);
1216 n->kind = VAR_SET_VALUE;
1217 n->name = "xmloption";
1218 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1223 var_name: ColId { $$ = $1; }
1224 | var_name '.' ColId
1226 $$ = palloc(strlen($1) + strlen($3) + 2);
1227 sprintf($$, "%s.%s", $1, $3);
1231 var_list: var_value { $$ = list_make1($1); }
1232 | var_list ',' var_value { $$ = lappend($1, $3); }
1235 var_value: opt_boolean
1236 { $$ = makeStringConst($1, @1); }
1238 { $$ = makeStringConst($1, @1); }
1240 { $$ = makeAConst($1, @1); }
1243 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1244 | READ COMMITTED { $$ = "read committed"; }
1245 | REPEATABLE READ { $$ = "repeatable read"; }
1246 | SERIALIZABLE { $$ = "serializable"; }
1250 TRUE_P { $$ = "true"; }
1251 | FALSE_P { $$ = "false"; }
1253 | OFF { $$ = "off"; }
1256 /* Timezone values can be:
1257 * - a string such as 'pst8pdt'
1258 * - an identifier such as "pst8pdt"
1259 * - an integer or floating point number
1260 * - a time interval per SQL99
1261 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1262 * so use IDENT and reject anything which is a reserved word.
1267 $$ = makeStringConst($1, @1);
1271 $$ = makeStringConst($1, @1);
1273 | ConstInterval Sconst opt_interval
1278 A_Const *n = (A_Const *) linitial($3);
1279 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1281 (errcode(ERRCODE_SYNTAX_ERROR),
1282 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1283 scanner_errposition(@3)));
1286 $$ = makeStringConstCast($2, @2, t);
1288 | ConstInterval '(' Iconst ')' Sconst opt_interval
1293 A_Const *n = (A_Const *) linitial($6);
1294 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1296 (errcode(ERRCODE_SYNTAX_ERROR),
1297 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1298 scanner_errposition(@6)));
1299 if (list_length($6) != 1)
1301 (errcode(ERRCODE_SYNTAX_ERROR),
1302 errmsg("interval precision specified twice"),
1303 scanner_errposition(@1)));
1304 t->typmods = lappend($6, makeIntConst($3, @3));
1307 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1308 makeIntConst($3, @3));
1309 $$ = makeStringConstCast($5, @5, t);
1311 | NumericOnly { $$ = makeAConst($1, @1); }
1312 | DEFAULT { $$ = NULL; }
1313 | LOCAL { $$ = NULL; }
1318 | DEFAULT { $$ = NULL; }
1319 | /*EMPTY*/ { $$ = NULL; }
1324 | Sconst { $$ = $1; }
1330 VariableSetStmt *n = makeNode(VariableSetStmt);
1331 n->kind = VAR_RESET;
1337 VariableSetStmt *n = makeNode(VariableSetStmt);
1338 n->kind = VAR_RESET;
1339 n->name = "timezone";
1342 | RESET TRANSACTION ISOLATION LEVEL
1344 VariableSetStmt *n = makeNode(VariableSetStmt);
1345 n->kind = VAR_RESET;
1346 n->name = "transaction_isolation";
1349 | RESET SESSION AUTHORIZATION
1351 VariableSetStmt *n = makeNode(VariableSetStmt);
1352 n->kind = VAR_RESET;
1353 n->name = "session_authorization";
1358 VariableSetStmt *n = makeNode(VariableSetStmt);
1359 n->kind = VAR_RESET_ALL;
1364 /* SetResetClause allows SET or RESET without LOCAL */
1366 SET set_rest { $$ = $2; }
1367 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1374 VariableShowStmt *n = makeNode(VariableShowStmt);
1380 VariableShowStmt *n = makeNode(VariableShowStmt);
1381 n->name = "timezone";
1384 | SHOW TRANSACTION ISOLATION LEVEL
1386 VariableShowStmt *n = makeNode(VariableShowStmt);
1387 n->name = "transaction_isolation";
1390 | SHOW SESSION AUTHORIZATION
1392 VariableShowStmt *n = makeNode(VariableShowStmt);
1393 n->name = "session_authorization";
1398 VariableShowStmt *n = makeNode(VariableShowStmt);
1406 SET CONSTRAINTS constraints_set_list constraints_set_mode
1408 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1409 n->constraints = $3;
1415 constraints_set_list:
1417 | qualified_name_list { $$ = $1; }
1420 constraints_set_mode:
1421 DEFERRED { $$ = TRUE; }
1422 | IMMEDIATE { $$ = FALSE; }
1427 * Checkpoint statement
1432 CheckPointStmt *n = makeNode(CheckPointStmt);
1438 /*****************************************************************************
1440 * DISCARD { ALL | TEMP | PLANS }
1442 *****************************************************************************/
1447 DiscardStmt *n = makeNode(DiscardStmt);
1448 n->target = DISCARD_ALL;
1453 DiscardStmt *n = makeNode(DiscardStmt);
1454 n->target = DISCARD_TEMP;
1459 DiscardStmt *n = makeNode(DiscardStmt);
1460 n->target = DISCARD_TEMP;
1465 DiscardStmt *n = makeNode(DiscardStmt);
1466 n->target = DISCARD_PLANS;
1472 /*****************************************************************************
1474 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1476 * Note: we accept all subcommands for each of the four variants, and sort
1477 * out what's really legal at execution time.
1478 *****************************************************************************/
1481 ALTER TABLE relation_expr alter_table_cmds
1483 AlterTableStmt *n = makeNode(AlterTableStmt);
1486 n->relkind = OBJECT_TABLE;
1489 | ALTER INDEX relation_expr alter_table_cmds
1491 AlterTableStmt *n = makeNode(AlterTableStmt);
1494 n->relkind = OBJECT_INDEX;
1497 | ALTER SEQUENCE relation_expr alter_table_cmds
1499 AlterTableStmt *n = makeNode(AlterTableStmt);
1502 n->relkind = OBJECT_SEQUENCE;
1505 | ALTER VIEW relation_expr alter_table_cmds
1507 AlterTableStmt *n = makeNode(AlterTableStmt);
1510 n->relkind = OBJECT_VIEW;
1516 alter_table_cmd { $$ = list_make1($1); }
1517 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1521 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1522 ADD_P opt_column columnDef
1524 AlterTableCmd *n = makeNode(AlterTableCmd);
1525 n->subtype = AT_AddColumn;
1529 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1530 | ALTER opt_column ColId alter_column_default
1532 AlterTableCmd *n = makeNode(AlterTableCmd);
1533 n->subtype = AT_ColumnDefault;
1538 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1539 | ALTER opt_column ColId DROP NOT NULL_P
1541 AlterTableCmd *n = makeNode(AlterTableCmd);
1542 n->subtype = AT_DropNotNull;
1546 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1547 | ALTER opt_column ColId SET NOT NULL_P
1549 AlterTableCmd *n = makeNode(AlterTableCmd);
1550 n->subtype = AT_SetNotNull;
1554 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1555 | ALTER opt_column ColId SET STATISTICS SignedIconst
1557 AlterTableCmd *n = makeNode(AlterTableCmd);
1558 n->subtype = AT_SetStatistics;
1560 n->def = (Node *) makeInteger($6);
1563 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1564 | ALTER opt_column ColId SET STORAGE ColId
1566 AlterTableCmd *n = makeNode(AlterTableCmd);
1567 n->subtype = AT_SetStorage;
1569 n->def = (Node *) makeString($6);
1572 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1573 | DROP opt_column ColId opt_drop_behavior
1575 AlterTableCmd *n = makeNode(AlterTableCmd);
1576 n->subtype = AT_DropColumn;
1582 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1583 * [ USING <expression> ]
1585 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1587 AlterTableCmd *n = makeNode(AlterTableCmd);
1588 n->subtype = AT_AlterColumnType;
1590 n->def = (Node *) $6;
1594 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1595 | ADD_P TableConstraint
1597 AlterTableCmd *n = makeNode(AlterTableCmd);
1598 n->subtype = AT_AddConstraint;
1602 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1603 | DROP CONSTRAINT name opt_drop_behavior
1605 AlterTableCmd *n = makeNode(AlterTableCmd);
1606 n->subtype = AT_DropConstraint;
1611 /* ALTER TABLE <name> SET WITHOUT OIDS */
1614 AlterTableCmd *n = makeNode(AlterTableCmd);
1615 n->subtype = AT_DropOids;
1618 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1621 AlterTableCmd *n = makeNode(AlterTableCmd);
1622 n->subtype = AT_ClusterOn;
1626 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1627 | SET WITHOUT CLUSTER
1629 AlterTableCmd *n = makeNode(AlterTableCmd);
1630 n->subtype = AT_DropCluster;
1634 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1635 | ENABLE_P TRIGGER name
1637 AlterTableCmd *n = makeNode(AlterTableCmd);
1638 n->subtype = AT_EnableTrig;
1642 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1643 | ENABLE_P ALWAYS TRIGGER name
1645 AlterTableCmd *n = makeNode(AlterTableCmd);
1646 n->subtype = AT_EnableAlwaysTrig;
1650 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1651 | ENABLE_P REPLICA TRIGGER name
1653 AlterTableCmd *n = makeNode(AlterTableCmd);
1654 n->subtype = AT_EnableReplicaTrig;
1658 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1659 | ENABLE_P TRIGGER ALL
1661 AlterTableCmd *n = makeNode(AlterTableCmd);
1662 n->subtype = AT_EnableTrigAll;
1665 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1666 | ENABLE_P TRIGGER USER
1668 AlterTableCmd *n = makeNode(AlterTableCmd);
1669 n->subtype = AT_EnableTrigUser;
1672 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1673 | DISABLE_P TRIGGER name
1675 AlterTableCmd *n = makeNode(AlterTableCmd);
1676 n->subtype = AT_DisableTrig;
1680 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1681 | DISABLE_P TRIGGER ALL
1683 AlterTableCmd *n = makeNode(AlterTableCmd);
1684 n->subtype = AT_DisableTrigAll;
1687 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1688 | DISABLE_P TRIGGER USER
1690 AlterTableCmd *n = makeNode(AlterTableCmd);
1691 n->subtype = AT_DisableTrigUser;
1694 /* ALTER TABLE <name> ENABLE RULE <rule> */
1695 | ENABLE_P RULE name
1697 AlterTableCmd *n = makeNode(AlterTableCmd);
1698 n->subtype = AT_EnableRule;
1702 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1703 | ENABLE_P ALWAYS RULE name
1705 AlterTableCmd *n = makeNode(AlterTableCmd);
1706 n->subtype = AT_EnableAlwaysRule;
1710 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1711 | ENABLE_P REPLICA RULE name
1713 AlterTableCmd *n = makeNode(AlterTableCmd);
1714 n->subtype = AT_EnableReplicaRule;
1718 /* ALTER TABLE <name> DISABLE RULE <rule> */
1719 | DISABLE_P RULE name
1721 AlterTableCmd *n = makeNode(AlterTableCmd);
1722 n->subtype = AT_DisableRule;
1726 /* ALTER TABLE <name> INHERIT <parent> */
1727 | INHERIT qualified_name
1729 AlterTableCmd *n = makeNode(AlterTableCmd);
1730 n->subtype = AT_AddInherit;
1731 n->def = (Node *) $2;
1734 /* ALTER TABLE <name> NO INHERIT <parent> */
1735 | NO INHERIT qualified_name
1737 AlterTableCmd *n = makeNode(AlterTableCmd);
1738 n->subtype = AT_DropInherit;
1739 n->def = (Node *) $3;
1742 /* ALTER TABLE <name> OWNER TO RoleId */
1745 AlterTableCmd *n = makeNode(AlterTableCmd);
1746 n->subtype = AT_ChangeOwner;
1750 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1751 | SET TABLESPACE name
1753 AlterTableCmd *n = makeNode(AlterTableCmd);
1754 n->subtype = AT_SetTableSpace;
1758 /* ALTER TABLE <name> SET (...) */
1761 AlterTableCmd *n = makeNode(AlterTableCmd);
1762 n->subtype = AT_SetRelOptions;
1763 n->def = (Node *)$2;
1766 /* ALTER TABLE <name> RESET (...) */
1769 AlterTableCmd *n = makeNode(AlterTableCmd);
1770 n->subtype = AT_ResetRelOptions;
1771 n->def = (Node *)$2;
1776 alter_column_default:
1777 SET DEFAULT a_expr { $$ = $3; }
1778 | DROP DEFAULT { $$ = NULL; }
1782 CASCADE { $$ = DROP_CASCADE; }
1783 | RESTRICT { $$ = DROP_RESTRICT; }
1784 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1788 USING a_expr { $$ = $2; }
1789 | /* EMPTY */ { $$ = NULL; }
1794 /*****************************************************************************
1797 * close <portalname>
1799 *****************************************************************************/
1804 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1810 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1811 n->portalname = NULL;
1817 /*****************************************************************************
1820 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1822 * BINARY, OIDS, and DELIMITERS kept in old locations
1823 * for backward compatibility. 2002-06-18
1825 * COPY ( SELECT ... ) TO file [WITH options]
1826 * This form doesn't have the backwards-compatible option
1829 *****************************************************************************/
1831 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1832 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1834 CopyStmt *n = makeNode(CopyStmt);
1842 /* Concatenate user-supplied flags */
1844 n->options = lappend(n->options, $2);
1846 n->options = lappend(n->options, $5);
1848 n->options = lappend(n->options, $8);
1850 n->options = list_concat(n->options, $10);
1853 | COPY select_with_parens TO copy_file_name opt_with
1856 CopyStmt *n = makeNode(CopyStmt);
1869 | TO { $$ = FALSE; }
1873 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1874 * used depends on the direction. (It really doesn't make sense to copy from
1875 * stdout. We silently correct the "typo".) - AY 9/94
1879 | STDIN { $$ = NULL; }
1880 | STDOUT { $$ = NULL; }
1886 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1887 | /* EMPTY */ { $$ = NIL; }
1894 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1898 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1900 | DELIMITER opt_as Sconst
1902 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1904 | NULL_P opt_as Sconst
1906 $$ = makeDefElem("null", (Node *)makeString($3));
1910 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1914 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1916 | QUOTE opt_as Sconst
1918 $$ = makeDefElem("quote", (Node *)makeString($3));
1920 | ESCAPE opt_as Sconst
1922 $$ = makeDefElem("escape", (Node *)makeString($3));
1924 | FORCE QUOTE columnList
1926 $$ = makeDefElem("force_quote", (Node *)$3);
1928 | FORCE NOT NULL_P columnList
1930 $$ = makeDefElem("force_notnull", (Node *)$4);
1934 /* The following exist for backward compatibility */
1939 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1941 | /*EMPTY*/ { $$ = NULL; }
1947 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1949 | /*EMPTY*/ { $$ = NULL; }
1953 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1954 opt_using DELIMITERS Sconst
1956 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1958 | /*EMPTY*/ { $$ = NULL; }
1967 /*****************************************************************************
1970 * CREATE TABLE relname
1972 *****************************************************************************/
1974 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1975 OptInherit OptWith OnCommitOption OptTableSpace
1977 CreateStmt *n = makeNode(CreateStmt);
1981 n->inhRelations = $8;
1982 n->constraints = NIL;
1985 n->tablespacename = $11;
1988 | CREATE OptTemp TABLE qualified_name OF qualified_name
1989 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1991 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1992 * by our inheritance capabilities. Let's try it...
1994 CreateStmt *n = makeNode(CreateStmt);
1998 n->inhRelations = list_make1($6);
1999 n->constraints = NIL;
2002 n->tablespacename = $12;
2008 * Redundancy here is needed to avoid shift/reduce conflicts,
2009 * since TEMP is not a reserved word. See also OptTempTableName.
2011 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2012 * the LOCAL keyword is really meaningless.
2014 OptTemp: TEMPORARY { $$ = TRUE; }
2015 | TEMP { $$ = TRUE; }
2016 | LOCAL TEMPORARY { $$ = TRUE; }
2017 | LOCAL TEMP { $$ = TRUE; }
2018 | GLOBAL TEMPORARY { $$ = TRUE; }
2019 | GLOBAL TEMP { $$ = TRUE; }
2020 | /*EMPTY*/ { $$ = FALSE; }
2023 OptTableElementList:
2024 TableElementList { $$ = $1; }
2025 | /*EMPTY*/ { $$ = NIL; }
2031 $$ = list_make1($1);
2033 | TableElementList ',' TableElement
2035 $$ = lappend($1, $3);
2040 columnDef { $$ = $1; }
2041 | TableLikeClause { $$ = $1; }
2042 | TableConstraint { $$ = $1; }
2045 columnDef: ColId Typename ColQualList
2047 ColumnDef *n = makeNode(ColumnDef);
2050 n->constraints = $3;
2057 ColQualList ColConstraint { $$ = lappend($1, $2); }
2058 | /*EMPTY*/ { $$ = NIL; }
2062 CONSTRAINT name ColConstraintElem
2064 switch (nodeTag($3))
2068 Constraint *n = (Constraint *)$3;
2072 case T_FkConstraint:
2074 FkConstraint *n = (FkConstraint *)$3;
2075 n->constr_name = $2;
2083 | ColConstraintElem { $$ = $1; }
2084 | ConstraintAttr { $$ = $1; }
2087 /* DEFAULT NULL is already the default for Postgres.
2088 * But define it here and carry it forward into the system
2089 * to make it explicit.
2090 * - thomas 1998-09-13
2092 * WITH NULL and NULL are not SQL92-standard syntax elements,
2093 * so leave them out. Use DEFAULT NULL to explicitly indicate
2094 * that a column may have that value. WITH NULL leads to
2095 * shift/reduce conflicts with WITH TIME ZONE anyway.
2096 * - thomas 1999-01-08
2098 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2099 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2100 * or be part of a_expr NOT LIKE or similar constructs).
2105 Constraint *n = makeNode(Constraint);
2106 n->contype = CONSTR_NOTNULL;
2109 n->cooked_expr = NULL;
2111 n->indexspace = NULL;
2116 Constraint *n = makeNode(Constraint);
2117 n->contype = CONSTR_NULL;
2120 n->cooked_expr = NULL;
2122 n->indexspace = NULL;
2125 | UNIQUE opt_definition OptConsTableSpace
2127 Constraint *n = makeNode(Constraint);
2128 n->contype = CONSTR_UNIQUE;
2131 n->cooked_expr = NULL;
2137 | PRIMARY KEY opt_definition OptConsTableSpace
2139 Constraint *n = makeNode(Constraint);
2140 n->contype = CONSTR_PRIMARY;
2143 n->cooked_expr = NULL;
2149 | CHECK '(' a_expr ')'
2151 Constraint *n = makeNode(Constraint);
2152 n->contype = CONSTR_CHECK;
2155 n->cooked_expr = NULL;
2157 n->indexspace = NULL;
2162 Constraint *n = makeNode(Constraint);
2163 n->contype = CONSTR_DEFAULT;
2166 n->cooked_expr = NULL;
2168 n->indexspace = NULL;
2171 | REFERENCES qualified_name opt_column_list key_match key_actions
2173 FkConstraint *n = makeNode(FkConstraint);
2174 n->constr_name = NULL;
2178 n->fk_matchtype = $4;
2179 n->fk_upd_action = (char) ($5 >> 8);
2180 n->fk_del_action = (char) ($5 & 0xFF);
2181 n->deferrable = FALSE;
2182 n->initdeferred = FALSE;
2188 * ConstraintAttr represents constraint attributes, which we parse as if
2189 * they were independent constraint clauses, in order to avoid shift/reduce
2190 * conflicts (since NOT might start either an independent NOT NULL clause
2191 * or an attribute). parse_utilcmd.c is responsible for attaching the
2192 * attribute information to the preceding "real" constraint node, and for
2193 * complaining if attribute clauses appear in the wrong place or wrong
2196 * See also ConstraintAttributeSpec, which can be used in places where
2197 * there is no parsing conflict.
2202 Constraint *n = makeNode(Constraint);
2203 n->contype = CONSTR_ATTR_DEFERRABLE;
2208 Constraint *n = makeNode(Constraint);
2209 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2212 | INITIALLY DEFERRED
2214 Constraint *n = makeNode(Constraint);
2215 n->contype = CONSTR_ATTR_DEFERRED;
2218 | INITIALLY IMMEDIATE
2220 Constraint *n = makeNode(Constraint);
2221 n->contype = CONSTR_ATTR_IMMEDIATE;
2228 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2229 * This seems to be a poor man's inheritance capability, with the resulting
2230 * tables completely decoupled except for the original commonality in definitions.
2232 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2233 * which is a part of SQL:2003.
2236 LIKE qualified_name TableLikeOptionList
2238 InhRelation *n = makeNode(InhRelation);
2245 TableLikeOptionList:
2246 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2247 | /* EMPTY */ { $$ = NIL; }
2251 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2252 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2253 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2254 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2255 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2256 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2260 /* ConstraintElem specifies constraint syntax which is not embedded into
2261 * a column definition. ColConstraintElem specifies the embedded form.
2262 * - thomas 1997-12-03
2265 CONSTRAINT name ConstraintElem
2267 switch (nodeTag($3))
2271 Constraint *n = (Constraint *)$3;
2275 case T_FkConstraint:
2277 FkConstraint *n = (FkConstraint *)$3;
2278 n->constr_name = $2;
2286 | ConstraintElem { $$ = $1; }
2290 CHECK '(' a_expr ')'
2292 Constraint *n = makeNode(Constraint);
2293 n->contype = CONSTR_CHECK;
2296 n->cooked_expr = NULL;
2297 n->indexspace = NULL;
2300 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2302 Constraint *n = makeNode(Constraint);
2303 n->contype = CONSTR_UNIQUE;
2306 n->cooked_expr = NULL;
2312 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2314 Constraint *n = makeNode(Constraint);
2315 n->contype = CONSTR_PRIMARY;
2318 n->cooked_expr = NULL;
2324 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2325 opt_column_list key_match key_actions ConstraintAttributeSpec
2327 FkConstraint *n = makeNode(FkConstraint);
2328 n->constr_name = NULL;
2332 n->fk_matchtype = $9;
2333 n->fk_upd_action = (char) ($10 >> 8);
2334 n->fk_del_action = (char) ($10 & 0xFF);
2335 n->deferrable = ($11 & 1) != 0;
2336 n->initdeferred = ($11 & 2) != 0;
2342 '(' columnList ')' { $$ = $2; }
2343 | /*EMPTY*/ { $$ = NIL; }
2347 columnElem { $$ = list_make1($1); }
2348 | columnList ',' columnElem { $$ = lappend($1, $3); }
2353 $$ = (Node *) makeString($1);
2357 key_match: MATCH FULL
2359 $$ = FKCONSTR_MATCH_FULL;
2364 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2365 errmsg("MATCH PARTIAL not yet implemented"),
2366 scanner_errposition(@1)));
2367 $$ = FKCONSTR_MATCH_PARTIAL;
2371 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2375 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2380 * We combine the update and delete actions into one value temporarily
2381 * for simplicity of parsing, and then break them down again in the
2382 * calling production. update is in the left 8 bits, delete in the right.
2383 * Note that NOACTION is the default.
2387 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2389 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2390 | key_update key_delete
2391 { $$ = ($1 << 8) | ($2 & 0xFF); }
2392 | key_delete key_update
2393 { $$ = ($2 << 8) | ($1 & 0xFF); }
2395 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2398 key_update: ON UPDATE key_action { $$ = $3; }
2401 key_delete: ON DELETE_P key_action { $$ = $3; }
2405 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2406 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2407 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2408 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2409 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2412 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2413 | /*EMPTY*/ { $$ = NIL; }
2416 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2418 WITH definition { $$ = $2; }
2419 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2420 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2421 | /*EMPTY*/ { $$ = NIL; }
2424 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2425 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2426 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2427 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2430 OptTableSpace: TABLESPACE name { $$ = $2; }
2431 | /*EMPTY*/ { $$ = NULL; }
2434 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2435 | /*EMPTY*/ { $$ = NULL; }
2440 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2445 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2448 * When the SelectStmt is a set-operation tree, we must
2449 * stuff the INTO information into the leftmost component
2450 * Select, because that's where analyze.c will expect
2451 * to find it. Similarly, the output column names must
2452 * be attached to that Select's target list.
2454 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2455 if (n->intoClause != NULL)
2457 (errcode(ERRCODE_SYNTAX_ERROR),
2458 errmsg("CREATE TABLE AS cannot specify INTO"),
2459 scanner_errposition(exprLocation((Node *) n->intoClause))));
2460 $4->rel->istemp = $2;
2462 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2464 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2470 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2472 $$ = makeNode(IntoClause);
2477 $$->tableSpaceName = $5;
2482 '(' CreateAsList ')' { $$ = $2; }
2483 | /*EMPTY*/ { $$ = NIL; }
2487 CreateAsElement { $$ = list_make1($1); }
2488 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2494 ColumnDef *n = makeNode(ColumnDef);
2499 n->is_not_null = false;
2500 n->raw_default = NULL;
2501 n->cooked_default = NULL;
2502 n->constraints = NIL;
2508 WITH DATA_P { $$ = TRUE; }
2509 | WITH NO DATA_P { $$ = FALSE; }
2510 | /*EMPTY*/ { $$ = TRUE; }
2514 /*****************************************************************************
2517 * CREATE SEQUENCE seqname
2518 * ALTER SEQUENCE seqname
2520 *****************************************************************************/
2523 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2525 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2534 ALTER SEQUENCE relation_expr SeqOptList
2536 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2543 OptSeqOptList: SeqOptList { $$ = $1; }
2544 | /*EMPTY*/ { $$ = NIL; }
2547 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2548 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2551 SeqOptElem: CACHE NumericOnly
2553 $$ = makeDefElem("cache", (Node *)$2);
2557 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2561 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2563 | INCREMENT opt_by NumericOnly
2565 $$ = makeDefElem("increment", (Node *)$3);
2567 | MAXVALUE NumericOnly
2569 $$ = makeDefElem("maxvalue", (Node *)$2);
2571 | MINVALUE NumericOnly
2573 $$ = makeDefElem("minvalue", (Node *)$2);
2577 $$ = makeDefElem("maxvalue", NULL);
2581 $$ = makeDefElem("minvalue", NULL);
2585 $$ = makeDefElem("owned_by", (Node *)$3);
2587 | START opt_with NumericOnly
2589 $$ = makeDefElem("start", (Node *)$3);
2593 $$ = makeDefElem("restart", NULL);
2595 | RESTART opt_with NumericOnly
2597 $$ = makeDefElem("restart", (Node *)$3);
2606 FCONST { $$ = makeFloat($1); }
2612 | SignedIconst { $$ = makeInteger($1); };
2615 /*****************************************************************************
2618 * CREATE PROCEDURAL LANGUAGE ...
2619 * DROP PROCEDURAL LANGUAGE ...
2621 *****************************************************************************/
2624 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2626 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2628 /* parameters are all to be supplied by system */
2630 n->plvalidator = NIL;
2631 n->pltrusted = false;
2634 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2635 HANDLER handler_name opt_validator opt_lancompiler
2637 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2640 n->plvalidator = $8;
2642 /* LANCOMPILER is now ignored entirely */
2648 TRUSTED { $$ = TRUE; }
2649 | /*EMPTY*/ { $$ = FALSE; }
2652 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2653 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2654 * Work around by using simple names, instead.
2657 name { $$ = list_make1(makeString($1)); }
2658 | name attrs { $$ = lcons(makeString($1), $2); }
2662 VALIDATOR handler_name { $$ = $2; }
2663 | /*EMPTY*/ { $$ = NIL; }
2667 LANCOMPILER Sconst { $$ = $2; }
2668 | /*EMPTY*/ { $$ = NULL; }
2672 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2674 DropPLangStmt *n = makeNode(DropPLangStmt);
2677 n->missing_ok = false;
2680 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2682 DropPLangStmt *n = makeNode(DropPLangStmt);
2685 n->missing_ok = true;
2695 /*****************************************************************************
2698 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2700 *****************************************************************************/
2702 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2704 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2705 n->tablespacename = $3;
2712 OptTableSpaceOwner: OWNER name { $$ = $2; }
2713 | /*EMPTY */ { $$ = NULL; }
2716 /*****************************************************************************
2719 * DROP TABLESPACE <tablespace>
2721 * No need for drop behaviour as we cannot implement dependencies for
2722 * objects in other databases; we can only support RESTRICT.
2724 ****************************************************************************/
2726 DropTableSpaceStmt: DROP TABLESPACE name
2728 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2729 n->tablespacename = $3;
2730 n->missing_ok = false;
2733 | DROP TABLESPACE IF_P EXISTS name
2735 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2736 n->tablespacename = $5;
2737 n->missing_ok = true;
2742 /*****************************************************************************
2745 * CREATE FOREIGN DATA WRAPPER name LIBRARY 'library_name' LANGUAGE C
2747 *****************************************************************************/
2749 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name LIBRARY Sconst LANGUAGE ColId create_generic_options
2751 CreateFdwStmt *n = makeNode(CreateFdwStmt);
2757 if (pg_strcasecmp($9, "C") != 0)
2759 (errcode(ERRCODE_SYNTAX_ERROR),
2760 errmsg("language for foreign-data wrapper must be C"),
2761 scanner_errposition(@9)));
2765 /*****************************************************************************
2768 * DROP FOREIGN DATA WRAPPER name
2770 ****************************************************************************/
2772 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
2774 DropFdwStmt *n = makeNode(DropFdwStmt);
2776 n->missing_ok = false;
2780 | DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
2782 DropFdwStmt *n = makeNode(DropFdwStmt);
2784 n->missing_ok = true;
2790 /*****************************************************************************
2793 * ALTER FOREIGN DATA WRAPPER name
2795 ****************************************************************************/
2797 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name LIBRARY Sconst alter_generic_options
2799 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2805 | ALTER FOREIGN DATA_P WRAPPER name LIBRARY Sconst
2807 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2812 | ALTER FOREIGN DATA_P WRAPPER name alter_generic_options
2814 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2821 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
2822 create_generic_options:
2823 OPTIONS '(' generic_option_list ')' { $$ = $3; }
2824 | /*EMPTY*/ { $$ = NIL; }
2827 generic_option_list: generic_option_elem
2829 $$ = list_make1(makeOptionDefElem(ALTER_OPT_ADD, $1));
2831 | generic_option_list ',' generic_option_elem
2833 $$ = lappend($1, makeOptionDefElem(ALTER_OPT_ADD, $3));
2837 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
2838 alter_generic_options:
2839 OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
2842 alter_generic_option_list:
2843 alter_generic_option_elem
2845 $$ = list_make1($1);
2847 | generic_option_elem
2849 $$ = list_make1(makeOptionDefElem(ALTER_OPT_ADD, $1));
2851 | alter_generic_option_list ',' alter_generic_option_elem
2853 $$ = lappend($1, $3);
2855 | alter_generic_option_list ',' generic_option_elem
2857 $$ = lappend($1, makeOptionDefElem(ALTER_OPT_ADD, $3));
2861 alter_generic_option_elem:
2862 ADD_P generic_option_elem
2864 $$ = makeOptionDefElem(ALTER_OPT_ADD, $2);
2866 | SET generic_option_elem
2868 $$ = makeOptionDefElem(ALTER_OPT_SET, $2);
2870 | DROP generic_option_name
2872 $$ = makeOptionDefElem(ALTER_OPT_DROP,
2873 makeDefElem($2, NULL));
2877 generic_option_elem:
2878 generic_option_name generic_option_arg { $$ = makeDefElem($1, $2); }
2881 generic_option_name:
2882 attr_name { $$ = $1; }
2886 Sconst { $$ = (Node *)makeString($1); }
2889 /*****************************************************************************
2892 * CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
2894 *****************************************************************************/
2896 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
2897 FOREIGN DATA_P WRAPPER name create_generic_options
2899 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
2910 TYPE_P Sconst { $$ = $2; }
2911 | /*EMPTY*/ { $$ = NULL; }
2915 foreign_server_version:
2916 VERSION_P Sconst { $$ = $2; }
2917 | VERSION_P NULL_P { $$ = NULL; }
2920 opt_foreign_server_version:
2921 foreign_server_version { $$ = $1; }
2922 | /*EMPTY*/ { $$ = NULL; }
2925 /*****************************************************************************
2930 ****************************************************************************/
2932 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
2934 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
2936 n->missing_ok = false;
2940 | DROP SERVER IF_P EXISTS name opt_drop_behavior
2942 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
2944 n->missing_ok = true;
2950 /*****************************************************************************
2953 * ALTER SERVER name [VERSION] [OPTIONS]
2955 ****************************************************************************/
2957 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
2959 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2963 n->has_version = true;
2966 | ALTER SERVER name foreign_server_version
2968 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2971 n->has_version = true;
2974 | ALTER SERVER name alter_generic_options
2976 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2983 /*****************************************************************************
2986 * CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
2988 *****************************************************************************/
2990 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
2992 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3000 /* User mapping authorization identifier */
3002 CURRENT_USER { $$ = "current_user"; }
3003 | USER { $$ = "current_user"; }
3004 | RoleId { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3007 /*****************************************************************************
3010 * DROP USER MAPPING FOR auth_ident SERVER name
3012 ****************************************************************************/
3014 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3016 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3019 n->missing_ok = false;
3022 | DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3024 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3027 n->missing_ok = true;
3032 /*****************************************************************************
3035 * ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3037 ****************************************************************************/
3039 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3041 AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3049 /*****************************************************************************
3052 * CREATE TRIGGER ...
3055 *****************************************************************************/
3058 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3059 qualified_name TriggerForSpec EXECUTE PROCEDURE
3060 func_name '(' TriggerFuncArgs ')'
3062 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3069 memcpy(n->actions, $5, 4);
3070 n->isconstraint = FALSE;
3071 n->deferrable = FALSE;
3072 n->initdeferred = FALSE;
3073 n->constrrel = NULL;
3076 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
3077 qualified_name OptConstrFromTable
3078 ConstraintAttributeSpec
3079 FOR EACH ROW EXECUTE PROCEDURE
3080 func_name '(' TriggerFuncArgs ')'
3082 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3089 memcpy(n->actions, $6, 4);
3090 n->isconstraint = TRUE;
3091 n->deferrable = ($10 & 1) != 0;
3092 n->initdeferred = ($10 & 2) != 0;
3100 BEFORE { $$ = TRUE; }
3101 | AFTER { $$ = FALSE; }
3107 char *e = palloc(4);
3108 e[0] = $1; e[1] = '\0';
3111 | TriggerOneEvent OR TriggerOneEvent
3113 char *e = palloc(4);
3114 e[0] = $1; e[1] = $3; e[2] = '\0';
3117 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
3119 char *e = palloc(4);
3120 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
3126 INSERT { $$ = 'i'; }
3127 | DELETE_P { $$ = 'd'; }
3128 | UPDATE { $$ = 'u'; }
3129 | TRUNCATE { $$ = 't'; }
3133 FOR TriggerForOpt TriggerForType
3140 * If ROW/STATEMENT not specified, default to
3141 * STATEMENT, per SQL
3154 | STATEMENT { $$ = FALSE; }
3158 TriggerFuncArg { $$ = list_make1($1); }
3159 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
3160 | /*EMPTY*/ { $$ = NIL; }
3167 snprintf(buf, sizeof(buf), "%d", $1);
3168 $$ = makeString(pstrdup(buf));
3170 | FCONST { $$ = makeString($1); }
3171 | Sconst { $$ = makeString($1); }
3172 | BCONST { $$ = makeString($1); }
3173 | XCONST { $$ = makeString($1); }
3174 | ColId { $$ = makeString($1); }
3178 FROM qualified_name { $$ = $2; }
3179 | /*EMPTY*/ { $$ = NULL; }
3182 ConstraintAttributeSpec:
3183 ConstraintDeferrabilitySpec
3185 | ConstraintDeferrabilitySpec ConstraintTimeSpec
3187 if ($1 == 0 && $2 != 0)
3189 (errcode(ERRCODE_SYNTAX_ERROR),
3190 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3191 scanner_errposition(@1)));
3194 | ConstraintTimeSpec
3201 | ConstraintTimeSpec ConstraintDeferrabilitySpec
3203 if ($2 == 0 && $1 != 0)
3205 (errcode(ERRCODE_SYNTAX_ERROR),
3206 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3207 scanner_errposition(@1)));
3214 ConstraintDeferrabilitySpec:
3215 NOT DEFERRABLE { $$ = 0; }
3216 | DEFERRABLE { $$ = 1; }
3220 INITIALLY IMMEDIATE { $$ = 0; }
3221 | INITIALLY DEFERRED { $$ = 2; }
3226 DROP TRIGGER name ON qualified_name opt_drop_behavior
3228 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3232 n->removeType = OBJECT_TRIGGER;
3233 n->missing_ok = false;
3236 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
3238 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3242 n->removeType = OBJECT_TRIGGER;
3243 n->missing_ok = true;
3249 /*****************************************************************************
3252 * CREATE ASSERTION ...
3253 * DROP ASSERTION ...
3255 *****************************************************************************/
3258 CREATE ASSERTION name CHECK '(' a_expr ')'
3259 ConstraintAttributeSpec
3261 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3263 n->args = list_make1($6);
3264 n->isconstraint = TRUE;
3265 n->deferrable = ($8 & 1) != 0;
3266 n->initdeferred = ($8 & 2) != 0;
3269 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3270 errmsg("CREATE ASSERTION is not yet implemented")));
3277 DROP ASSERTION name opt_drop_behavior
3279 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3283 n->removeType = OBJECT_TRIGGER; /* XXX */
3285 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3286 errmsg("DROP ASSERTION is not yet implemented")));
3292 /*****************************************************************************
3295 * define (aggregate,operator,type)
3297 *****************************************************************************/
3300 CREATE AGGREGATE func_name aggr_args definition
3302 DefineStmt *n = makeNode(DefineStmt);
3303 n->kind = OBJECT_AGGREGATE;
3304 n->oldstyle = false;
3310 | CREATE AGGREGATE func_name old_aggr_definition
3312 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
3313 DefineStmt *n = makeNode(DefineStmt);
3314 n->kind = OBJECT_AGGREGATE;
3321 | CREATE OPERATOR any_operator definition
3323 DefineStmt *n = makeNode(DefineStmt);
3324 n->kind = OBJECT_OPERATOR;
3325 n->oldstyle = false;
3331 | CREATE TYPE_P any_name definition
3333 DefineStmt *n = makeNode(DefineStmt);
3334 n->kind = OBJECT_TYPE;
3335 n->oldstyle = false;
3341 | CREATE TYPE_P any_name
3343 /* Shell type (identified by lack of definition) */
3344 DefineStmt *n = makeNode(DefineStmt);
3345 n->kind = OBJECT_TYPE;
3346 n->oldstyle = false;
3349 n->definition = NIL;
3352 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3354 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3355 RangeVar *r = makeNode(RangeVar);
3357 /* can't use qualified_name, sigh */
3358 switch (list_length($3))
3361 r->catalogname = NULL;
3362 r->schemaname = NULL;
3363 r->relname = strVal(linitial($3));
3366 r->catalogname = NULL;
3367 r->schemaname = strVal(linitial($3));
3368 r->relname = strVal(lsecond($3));
3371 r->catalogname = strVal(linitial($3));
3372 r->schemaname = strVal(lsecond($3));
3373 r->relname = strVal(lthird($3));
3377 (errcode(ERRCODE_SYNTAX_ERROR),
3378 errmsg("improper qualified name (too many dotted names): %s",
3379 NameListToString($3)),
3380 scanner_errposition(@3)));
3388 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3390 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3395 | CREATE TEXT_P SEARCH PARSER any_name definition
3397 DefineStmt *n = makeNode(DefineStmt);
3398 n->kind = OBJECT_TSPARSER;
3404 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3406 DefineStmt *n = makeNode(DefineStmt);
3407 n->kind = OBJECT_TSDICTIONARY;
3413 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3415 DefineStmt *n = makeNode(DefineStmt);
3416 n->kind = OBJECT_TSTEMPLATE;
3422 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3424 DefineStmt *n = makeNode(DefineStmt);
3425 n->kind = OBJECT_TSCONFIGURATION;
3433 definition: '(' def_list ')' { $$ = $2; }
3436 def_list: def_elem { $$ = list_make1($1); }
3437 | def_list ',' def_elem { $$ = lappend($1, $3); }
3440 def_elem: ColLabel '=' def_arg
3442 $$ = makeDefElem($1, (Node *)$3);
3446 $$ = makeDefElem($1, NULL);
3450 /* Note: any simple identifier will be returned as a type name! */
3451 def_arg: func_type { $$ = (Node *)$1; }
3452 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3453 | qual_all_Op { $$ = (Node *)$1; }
3454 | NumericOnly { $$ = (Node *)$1; }
3455 | Sconst { $$ = (Node *)makeString($1); }
3458 aggr_args: '(' type_list ')' { $$ = $2; }
3459 | '(' '*' ')' { $$ = NIL; }
3462 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3465 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3466 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3469 old_aggr_elem: IDENT '=' def_arg
3471 $$ = makeDefElem($1, (Node *)$3);
3475 enum_val_list: Sconst
3476 { $$ = list_make1(makeString($1)); }
3477 | enum_val_list ',' Sconst
3478 { $$ = lappend($1, makeString($3)); }
3482 /*****************************************************************************
3485 * CREATE OPERATOR CLASS ...
3486 * CREATE OPERATOR FAMILY ...
3487 * ALTER OPERATOR FAMILY ...
3488 * DROP OPERATOR CLASS ...
3489 * DROP OPERATOR FAMILY ...
3491 *****************************************************************************/
3494 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3495 USING access_method opt_opfamily AS opclass_item_list
3497 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3498 n->opclassname = $4;
3502 n->opfamilyname = $11;
3509 opclass_item { $$ = list_make1($1); }
3510 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3514 OPERATOR Iconst any_operator opt_recheck
3516 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3517 n->itemtype = OPCLASS_ITEM_OPERATOR;
3523 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3525 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3526 n->itemtype = OPCLASS_ITEM_OPERATOR;
3532 | FUNCTION Iconst func_name func_args
3534 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3535 n->itemtype = OPCLASS_ITEM_FUNCTION;
3537 n->args = extractArgTypes($4);
3541 | FUNCTION Iconst '(' type_list ')' func_name func_args
3543 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3544 n->itemtype = OPCLASS_ITEM_FUNCTION;
3546 n->args = extractArgTypes($7);
3553 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3554 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3560 opt_default: DEFAULT { $$ = TRUE; }
3561 | /*EMPTY*/ { $$ = FALSE; }
3564 opt_opfamily: FAMILY any_name { $$ = $2; }
3565 | /*EMPTY*/ { $$ = NIL; }
3568 opt_recheck: RECHECK
3571 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3572 errmsg("RECHECK is no longer supported"),
3573 errhint("Update your data type."),
3574 scanner_errposition(@1)));
3577 | /*EMPTY*/ { $$ = FALSE; }
3582 CREATE OPERATOR FAMILY any_name USING access_method
3584 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3585 n->opfamilyname = $4;
3592 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3594 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3595 n->opfamilyname = $4;
3601 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3603 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3604 n->opfamilyname = $4;
3613 opclass_drop { $$ = list_make1($1); }
3614 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3618 OPERATOR Iconst '(' type_list ')'
3620 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3621 n->itemtype = OPCLASS_ITEM_OPERATOR;
3626 | FUNCTION Iconst '(' type_list ')'
3628 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3629 n->itemtype = OPCLASS_ITEM_FUNCTION;
3638 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3640 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3641 n->opclassname = $4;
3644 n->missing_ok = false;
3647 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3649 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3650 n->opclassname = $6;
3653 n->missing_ok = true;
3659 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3661 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3662 n->opfamilyname = $4;
3665 n->missing_ok = false;
3668 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3670 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3671 n->opfamilyname = $6;
3674 n->missing_ok = true;
3680 /*****************************************************************************
3684 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3685 * REASSIGN OWNED BY username [, username ...] TO username
3687 *****************************************************************************/
3689 DROP OWNED BY name_list opt_drop_behavior
3691 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3699 REASSIGN OWNED BY name_list TO name
3701 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3708 /*****************************************************************************
3712 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3713 * [ RESTRICT | CASCADE ]
3715 *****************************************************************************/
3717 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3719 DropStmt *n = makeNode(DropStmt);
3721 n->missing_ok = TRUE;
3726 | DROP drop_type any_name_list opt_drop_behavior
3728 DropStmt *n = makeNode(DropStmt);
3730 n->missing_ok = FALSE;
3738 drop_type: TABLE { $$ = OBJECT_TABLE; }
3739 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3740 | VIEW { $$ = OBJECT_VIEW; }
3741 | INDEX { $$ = OBJECT_INDEX; }
3742 | TYPE_P { $$ = OBJECT_TYPE; }
3743 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3744 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3745 | SCHEMA { $$ = OBJECT_SCHEMA; }
3746 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3747 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3748 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3749 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3753 any_name { $$ = list_make1($1); }
3754 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3757 any_name: ColId { $$ = list_make1(makeString($1)); }
3758 | ColId attrs { $$ = lcons(makeString($1), $2); }
3761 attrs: '.' attr_name
3762 { $$ = list_make1(makeString($2)); }
3763 | attrs '.' attr_name
3764 { $$ = lappend($1, makeString($3)); }
3768 /*****************************************************************************
3771 * truncate table relname1, relname2, ...
3773 *****************************************************************************/
3776 TRUNCATE opt_table qualified_name_list opt_restart_seqs opt_drop_behavior
3778 TruncateStmt *n = makeNode(TruncateStmt);
3780 n->restart_seqs = $4;
3787 CONTINUE_P IDENTITY_P { $$ = false; }
3788 | RESTART IDENTITY_P { $$ = true; }
3789 | /* EMPTY */ { $$ = false; }
3792 /*****************************************************************************
3794 * The COMMENT ON statement can take different forms based upon the type of
3795 * the object associated with the comment. The form of the statement is:
3797 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3798 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3799 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3800 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3801 * TEXT SEARCH TEMPLATE |
3802 * TEXT SEARCH CONFIGURATION ] <objname> |
3803 * AGGREGATE <aggname> (arg1, ...) |
3804 * FUNCTION <funcname> (arg1, arg2, ...) |
3805 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3806 * TRIGGER <triggername> ON <relname> |
3807 * CONSTRAINT <constraintname> ON <relname> |
3808 * RULE <rulename> ON <relname> ]
3811 *****************************************************************************/
3814 COMMENT ON comment_type any_name IS comment_text
3816 CommentStmt *n = makeNode(CommentStmt);
3823 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3825 CommentStmt *n = makeNode(CommentStmt);
3826 n->objtype = OBJECT_AGGREGATE;
3832 | COMMENT ON FUNCTION func_name func_args IS comment_text
3834 CommentStmt *n = makeNode(CommentStmt);
3835 n->objtype = OBJECT_FUNCTION;
3837 n->objargs = extractArgTypes($5);
3841 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3843 CommentStmt *n = makeNode(CommentStmt);
3844 n->objtype = OBJECT_OPERATOR;
3850 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3852 CommentStmt *n = makeNode(CommentStmt);
3853 n->objtype = OBJECT_CONSTRAINT;
3854 n->objname = lappend($6, makeString($4));
3859 | COMMENT ON RULE name ON any_name IS comment_text
3861 CommentStmt *n = makeNode(CommentStmt);
3862 n->objtype = OBJECT_RULE;
3863 n->objname = lappend($6, makeString($4));
3868 | COMMENT ON RULE name IS comment_text
3870 /* Obsolete syntax supported for awhile for compatibility */
3871 CommentStmt *n = makeNode(CommentStmt);
3872 n->objtype = OBJECT_RULE;
3873 n->objname = list_make1(makeString($4));
3878 | COMMENT ON TRIGGER name ON any_name IS comment_text
3880 CommentStmt *n = makeNode(CommentStmt);
3881 n->objtype = OBJECT_TRIGGER;
3882 n->objname = lappend($6, makeString($4));
3887 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3889 CommentStmt *n = makeNode(CommentStmt);
3890 n->objtype = OBJECT_OPCLASS;
3892 n->objargs = list_make1(makeString($7));
3896 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3898 CommentStmt *n = makeNode(CommentStmt);
3899 n->objtype = OBJECT_OPFAMILY;
3901 n->objargs = list_make1(makeString($7));
3905 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3907 CommentStmt *n = makeNode(CommentStmt);
3908 n->objtype = OBJECT_LARGEOBJECT;
3909 n->objname = list_make1($5);
3914 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3916 CommentStmt *n = makeNode(CommentStmt);
3917 n->objtype = OBJECT_CAST;
3918 n->objname = list_make1($5);
3919 n->objargs = list_make1($7);
3923 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3925 CommentStmt *n = makeNode(CommentStmt);
3926 n->objtype = OBJECT_LANGUAGE;
3932 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3934 CommentStmt *n = makeNode(CommentStmt);
3935 n->objtype = OBJECT_TSPARSER;
3940 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3942 CommentStmt *n = makeNode(CommentStmt);
3943 n->objtype = OBJECT_TSDICTIONARY;
3948 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3950 CommentStmt *n = makeNode(CommentStmt);
3951 n->objtype = OBJECT_TSTEMPLATE;
3956 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3958 CommentStmt *n = makeNode(CommentStmt);
3959 n->objtype = OBJECT_TSCONFIGURATION;
3967 COLUMN { $$ = OBJECT_COLUMN; }
3968 | DATABASE { $$ = OBJECT_DATABASE; }
3969 | SCHEMA { $$ = OBJECT_SCHEMA; }
3970 | INDEX { $$ = OBJECT_INDEX; }
3971 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3972 | TABLE { $$ = OBJECT_TABLE; }
3973 | DOMAIN_P { $$ = OBJECT_TYPE; }
3974 | TYPE_P { $$ = OBJECT_TYPE; }
3975 | VIEW { $$ = OBJECT_VIEW; }
3976 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3977 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3978 | ROLE { $$ = OBJECT_ROLE; }
3983 | NULL_P { $$ = NULL; }
3986 /*****************************************************************************
3991 *****************************************************************************/
3993 FetchStmt: FETCH fetch_direction from_in name
3995 FetchStmt *n = (FetchStmt *) $2;
4002 FetchStmt *n = makeNode(FetchStmt);
4003 n->direction = FETCH_FORWARD;
4009 | MOVE fetch_direction from_in name
4011 FetchStmt *n = (FetchStmt *) $2;
4018 FetchStmt *n = makeNode(FetchStmt);
4019 n->direction = FETCH_FORWARD;
4030 FetchStmt *n = makeNode(FetchStmt);
4031 n->direction = FETCH_FORWARD;
4037 FetchStmt *n = makeNode(FetchStmt);
4038 n->direction = FETCH_FORWARD;
4044 FetchStmt *n = makeNode(FetchStmt);
4045 n->direction = FETCH_BACKWARD;
4051 FetchStmt *n = makeNode(FetchStmt);
4052 n->direction = FETCH_ABSOLUTE;
4058 FetchStmt *n = makeNode(FetchStmt);
4059 n->direction = FETCH_ABSOLUTE;
4063 | ABSOLUTE_P SignedIconst
4065 FetchStmt *n = makeNode(FetchStmt);
4066 n->direction = FETCH_ABSOLUTE;
4070 | RELATIVE_P SignedIconst
4072 FetchStmt *n = makeNode(FetchStmt);
4073 n->direction = FETCH_RELATIVE;
4079 FetchStmt *n = makeNode(FetchStmt);
4080 n->direction = FETCH_FORWARD;
4086 FetchStmt *n = makeNode(FetchStmt);
4087 n->direction = FETCH_FORWARD;
4088 n->howMany = FETCH_ALL;
4093 FetchStmt *n = makeNode(FetchStmt);
4094 n->direction = FETCH_FORWARD;
4098 | FORWARD SignedIconst
4100 FetchStmt *n = makeNode(FetchStmt);
4101 n->direction = FETCH_FORWARD;
4107 FetchStmt *n = makeNode(FetchStmt);
4108 n->direction = FETCH_FORWARD;
4109 n->howMany = FETCH_ALL;
4114 FetchStmt *n = makeNode(FetchStmt);
4115 n->direction = FETCH_BACKWARD;
4119 | BACKWARD SignedIconst
4121 FetchStmt *n = makeNode(FetchStmt);
4122 n->direction = FETCH_BACKWARD;
4128 FetchStmt *n = makeNode(FetchStmt);
4129 n->direction = FETCH_BACKWARD;
4130 n->howMany = FETCH_ALL;
4140 /*****************************************************************************
4142 * GRANT and REVOKE statements
4144 *****************************************************************************/
4146 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
4147 opt_grant_grant_option
4149 GrantStmt *n = makeNode(GrantStmt);
4152 n->objtype = ($4)->objtype;
4153 n->objects = ($4)->objs;
4155 n->grant_option = $7;
4161 REVOKE privileges ON privilege_target
4162 FROM grantee_list opt_drop_behavior
4164 GrantStmt *n = makeNode(GrantStmt);
4165 n->is_grant = false;
4166 n->grant_option = false;
4168 n->objtype = ($4)->objtype;
4169 n->objects = ($4)->objs;
4174 | REVOKE GRANT OPTION FOR privileges ON privilege_target
4175 FROM grantee_list opt_drop_behavior
4177 GrantStmt *n = makeNode(GrantStmt);
4178 n->is_grant = false;
4179 n->grant_option = true;
4181 n->objtype = ($7)->objtype;
4182 n->objects = ($7)->objs;
4191 * A privilege list is represented as a list of strings; the validity of
4192 * the privilege names gets checked at execution. This is a bit annoying
4193 * but we have little choice because of the syntactic conflict with lists
4194 * of role names in GRANT/REVOKE. What's more, we have to call out in
4195 * the "privilege" production any reserved keywords that need to be usable
4196 * as privilege names.
4199 /* either ALL [PRIVILEGES] or a list of individual privileges */
4200 privileges: privilege_list
4208 privilege_list: privilege
4209 { $$ = list_make1(makeString($1)); }
4210 | privilege_list ',' privilege
4211 { $$ = lappend($1, makeString($3)); }
4214 privilege: SELECT { $$ = pstrdup($1); }
4215 | REFERENCES { $$ = pstrdup($1); }
4216 | CREATE { $$ = pstrdup($1); }
4217 | ColId { $$ = $1; }
4221 /* Don't bother trying to fold the first two rules into one using
4222 * opt_table. You're going to get conflicts.
4227 PrivTarget *n = makeNode(PrivTarget);
4228 n->objtype = ACL_OBJECT_RELATION;
4232 | TABLE qualified_name_list
4234 PrivTarget *n = makeNode(PrivTarget);
4235 n->objtype = ACL_OBJECT_RELATION;
4239 | SEQUENCE qualified_name_list
4241 PrivTarget *n = makeNode(PrivTarget);
4242 n->objtype = ACL_OBJECT_SEQUENCE;
4246 | FOREIGN DATA_P WRAPPER name_list
4248 PrivTarget *n = makeNode(PrivTarget);
4249 n->objtype = ACL_OBJECT_FDW;
4253 | FOREIGN SERVER name_list
4255 PrivTarget *n = makeNode(PrivTarget);
4256 n->objtype = ACL_OBJECT_FOREIGN_SERVER;
4260 | FUNCTION function_with_argtypes_list
4262 PrivTarget *n = makeNode(PrivTarget);
4263 n->objtype = ACL_OBJECT_FUNCTION;
4267 | DATABASE name_list
4269 PrivTarget *n = makeNode(PrivTarget);
4270 n->objtype = ACL_OBJECT_DATABASE;
4274 | LANGUAGE name_list
4276 PrivTarget *n = makeNode(PrivTarget);
4277 n->objtype = ACL_OBJECT_LANGUAGE;
4283 PrivTarget *n = makeNode(PrivTarget);
4284 n->objtype = ACL_OBJECT_NAMESPACE;
4288 | TABLESPACE name_list
4290 PrivTarget *n = makeNode(PrivTarget);
4291 n->objtype = ACL_OBJECT_TABLESPACE;
4299 grantee { $$ = list_make1($1); }
4300 | grantee_list ',' grantee { $$ = lappend($1, $3); }
4305 PrivGrantee *n = makeNode(PrivGrantee);
4306 /* This hack lets us avoid reserving PUBLIC as a keyword*/
4307 if (strcmp($1, "public") == 0)
4315 PrivGrantee *n = makeNode(PrivGrantee);
4316 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
4317 if (strcmp($2, "public") == 0)
4326 opt_grant_grant_option:
4327 WITH GRANT OPTION { $$ = TRUE; }
4328 | /*EMPTY*/ { $$ = FALSE; }
4331 function_with_argtypes_list:
4332 function_with_argtypes { $$ = list_make1($1); }
4333 | function_with_argtypes_list ',' function_with_argtypes
4334 { $$ = lappend($1, $3); }
4337 function_with_argtypes:
4340 FuncWithArgs *n = makeNode(FuncWithArgs);
4342 n->funcargs = extractArgTypes($2);
4347 /*****************************************************************************
4349 * GRANT and REVOKE ROLE statements
4351 *****************************************************************************/
4354 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4356 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4358 n->granted_roles = $2;
4359 n->grantee_roles = $4;
4367 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4369 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4370 n->is_grant = false;
4371 n->admin_opt = false;
4372 n->granted_roles = $2;
4373 n->grantee_roles = $4;
4377 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4379 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4380 n->is_grant = false;
4381 n->admin_opt = true;
4382 n->granted_roles = $5;
4383 n->grantee_roles = $7;
4389 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4390 | /*EMPTY*/ { $$ = FALSE; }
4393 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4394 | /*EMPTY*/ { $$ = NULL; }
4398 /*****************************************************************************
4400 * QUERY: CREATE INDEX
4402 * Note: we can't factor CONCURRENTLY into a separate production without
4403 * making it a reserved word.
4405 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4406 * willing to make TABLESPACE a fully reserved word.
4407 *****************************************************************************/
4409 IndexStmt: CREATE index_opt_unique INDEX index_name
4410 ON qualified_name access_method_clause '(' index_params ')'
4411 opt_definition OptTableSpace where_clause
4413 IndexStmt *n = makeNode(IndexStmt);
4415 n->concurrent = false;
4418 n->accessMethod = $7;
4419 n->indexParams = $9;
4421 n->tableSpace = $12;
4422 n->whereClause = $13;
4425 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4426 ON qualified_name access_method_clause '(' index_params ')'
4427 opt_definition OptTableSpace where_clause
4429 IndexStmt *n = makeNode(IndexStmt);
4431 n->concurrent = true;
4434 n->accessMethod = $8;
4435 n->indexParams = $10;
4437 n->tableSpace = $13;
4438 n->whereClause = $14;
4444 UNIQUE { $$ = TRUE; }
4445 | /*EMPTY*/ { $$ = FALSE; }
4448 access_method_clause:
4449 USING access_method { $$ = $2; }
4450 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4453 index_params: index_elem { $$ = list_make1($1); }
4454 | index_params ',' index_elem { $$ = lappend($1, $3); }
4458 * Index attributes can be either simple column references, or arbitrary
4459 * expressions in parens. For backwards-compatibility reasons, we allow
4460 * an expression that's just a function call to be written without parens.
4462 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4464 $$ = makeNode(IndexElem);
4469 $$->nulls_ordering = $4;
4471 | func_expr opt_class opt_asc_desc opt_nulls_order
4473 $$ = makeNode(IndexElem);
4478 $$->nulls_ordering = $4;
4480 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4482 $$ = makeNode(IndexElem);
4487 $$->nulls_ordering = $6;
4491 opt_class: any_name { $$ = $1; }
4492 | USING any_name { $$ = $2; }
4493 | /*EMPTY*/ { $$ = NIL; }
4496 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4497 | DESC { $$ = SORTBY_DESC; }
4498 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4501 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4502 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4503 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4507 /*****************************************************************************
4510 * create [or replace] function <fname>
4511 * [(<type-1> { , <type-n>})]
4513 * as <filename or code in language as appropriate>
4514 * language <lang> [with parameters]
4516 *****************************************************************************/
4519 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4520 RETURNS func_return createfunc_opt_list opt_definition
4522 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4531 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4532 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4534 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4537 n->parameters = mergeTableFuncParameters($5, $9);
4538 n->returnType = TableFuncTypeName($9);
4539 n->returnType->location = @7;
4541 n->withClause = $12;
4544 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4545 createfunc_opt_list opt_definition
4547 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4551 n->returnType = NULL;
4559 OR REPLACE { $$ = TRUE; }
4560 | /*EMPTY*/ { $$ = FALSE; }
4563 func_args: '(' func_args_list ')' { $$ = $2; }
4564 | '(' ')' { $$ = NIL; }
4568 func_arg { $$ = list_make1($1); }
4569 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4573 * func_args_with_defaults is separate because we only want to accept
4574 * defaults in CREATE FUNCTION, not in ALTER etc.
4576 func_args_with_defaults:
4577 '(' func_args_with_defaults_list ')' { $$ = $2; }
4578 | '(' ')' { $$ = NIL; }
4581 func_args_with_defaults_list:
4582 func_arg_with_default { $$ = list_make1($1); }
4583 | func_args_with_defaults_list ',' func_arg_with_default
4584 { $$ = lappend($1, $3); }
4588 * The style with arg_class first is SQL99 standard, but Oracle puts
4589 * param_name first; accept both since it's likely people will try both
4590 * anyway. Don't bother trying to save productions by letting arg_class
4591 * have an empty alternative ... you'll get shift/reduce conflicts.
4593 * We can catch over-specified arguments here if we want to,
4594 * but for now better to silently swallow typmod, etc.
4595 * - thomas 2000-03-22
4598 arg_class param_name func_type
4600 FunctionParameter *n = makeNode(FunctionParameter);
4607 | param_name arg_class func_type
4609 FunctionParameter *n = makeNode(FunctionParameter);
4616 | param_name func_type
4618 FunctionParameter *n = makeNode(FunctionParameter);
4621 n->mode = FUNC_PARAM_IN;
4625 | arg_class func_type
4627 FunctionParameter *n = makeNode(FunctionParameter);
4636 FunctionParameter *n = makeNode(FunctionParameter);
4639 n->mode = FUNC_PARAM_IN;
4645 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4646 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4647 | OUT_P { $$ = FUNC_PARAM_OUT; }
4648 | INOUT { $$ = FUNC_PARAM_INOUT; }
4649 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4650 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4654 * Ideally param_name should be ColId, but that causes too many conflicts.
4656 param_name: type_function_name
4662 /* We can catch over-specified results here if we want to,
4663 * but for now better to silently swallow typmod, etc.
4664 * - thomas 2000-03-22
4671 * We would like to make the %TYPE productions here be ColId attrs etc,
4672 * but that causes reduce/reduce conflicts. type_function_name
4673 * is next best choice.
4675 func_type: Typename { $$ = $1; }
4676 | type_function_name attrs '%' TYPE_P
4678 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4679 $$->pct_type = true;
4682 | SETOF type_function_name attrs '%' TYPE_P
4684 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4685 $$->pct_type = true;
4691 func_arg_with_default:
4696 | func_arg DEFAULT a_expr
4701 | func_arg '=' a_expr
4709 createfunc_opt_list:
4710 /* Must be at least one to prevent conflict */
4711 createfunc_opt_item { $$ = list_make1($1); }
4712 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4716 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4718 common_func_opt_item:
4719 CALLED ON NULL_P INPUT_P
4721 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4723 | RETURNS NULL_P ON NULL_P INPUT_P
4725 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4729 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4733 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4737 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4741 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4743 | EXTERNAL SECURITY DEFINER
4745 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4747 | EXTERNAL SECURITY INVOKER
4749 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4753 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4757 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4761 $$ = makeDefElem("cost", (Node *)$2);
4765 $$ = makeDefElem("rows", (Node *)$2);
4769 /* we abuse the normal content of a DefElem here */
4770 $$ = makeDefElem("set", (Node *)$1);
4774 createfunc_opt_item:
4777 $$ = makeDefElem("as", (Node *)$2);
4779 | LANGUAGE ColId_or_Sconst
4781 $$ = makeDefElem("language", (Node *)makeString($2));
4783 | common_func_opt_item
4789 func_as: Sconst { $$ = list_make1(makeString($1)); }
4792 $$ = list_make2(makeString($1), makeString($3));
4797 WITH definition { $$ = $2; }
4798 | /*EMPTY*/ { $$ = NIL; }
4801 table_func_column: param_name func_type
4803 FunctionParameter *n = makeNode(FunctionParameter);
4806 n->mode = FUNC_PARAM_TABLE;
4812 table_func_column_list:
4815 $$ = list_make1($1);
4817 | table_func_column_list ',' table_func_column
4819 $$ = lappend($1, $3);
4823 /*****************************************************************************
4826 * RENAME and OWNER subcommands are already provided by the generic
4827 * ALTER infrastructure, here we just specify alterations that can
4828 * only be applied to functions.
4830 *****************************************************************************/
4832 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4834 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4842 /* At least one option must be specified */
4843 common_func_opt_item { $$ = list_make1($1); }
4844 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4847 /* Ignored, merely for SQL compliance */
4854 /*****************************************************************************
4858 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4859 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4860 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4862 *****************************************************************************/
4865 DROP FUNCTION func_name func_args opt_drop_behavior
4867 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4868 n->kind = OBJECT_FUNCTION;
4870 n->args = extractArgTypes($4);
4872 n->missing_ok = false;
4875 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4877 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4878 n->kind = OBJECT_FUNCTION;
4880 n->args = extractArgTypes($6);
4882 n->missing_ok = true;
4888 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4890 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4891 n->kind = OBJECT_AGGREGATE;
4895 n->missing_ok = false;
4898 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4900 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4901 n->kind = OBJECT_AGGREGATE;
4905 n->missing_ok = true;
4911 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
4913 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4914 n->kind = OBJECT_OPERATOR;
4918 n->missing_ok = false;
4921 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
4923 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4924 n->kind = OBJECT_OPERATOR;
4928 n->missing_ok = true;
4937 (errcode(ERRCODE_SYNTAX_ERROR),
4938 errmsg("missing argument"),
4939 errhint("Use NONE to denote the missing argument of a unary operator."),
4940 scanner_errposition(@3)));
4942 | '(' Typename ',' Typename ')'
4943 { $$ = list_make2($2, $4); }
4944 | '(' NONE ',' Typename ')' /* left unary */
4945 { $$ = list_make2(NULL, $4); }
4946 | '(' Typename ',' NONE ')' /* right unary */
4947 { $$ = list_make2($2, NULL); }
4952 { $$ = list_make1(makeString($1)); }
4953 | ColId '.' any_operator
4954 { $$ = lcons(makeString($1), $3); }
4958 /*****************************************************************************
4960 * CREATE CAST / DROP CAST
4962 *****************************************************************************/
4964 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4965 WITH FUNCTION function_with_argtypes cast_context
4967 CreateCastStmt *n = makeNode(CreateCastStmt);
4971 n->context = (CoercionContext) $11;
4975 | CREATE CAST '(' Typename AS Typename ')'
4976 WITHOUT FUNCTION cast_context
4978 CreateCastStmt *n = makeNode(CreateCastStmt);
4982 n->context = (CoercionContext) $10;
4986 | CREATE CAST '(' Typename AS Typename ')'
4987 WITH INOUT cast_context
4989 CreateCastStmt *n = makeNode(CreateCastStmt);
4993 n->context = (CoercionContext) $10;
4999 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
5000 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
5001 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
5005 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
5007 DropCastStmt *n = makeNode(DropCastStmt);
5016 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
5017 | /*EMPTY*/ { $$ = FALSE; }
5021 /*****************************************************************************
5025 * REINDEX type <name> [FORCE]
5027 * FORCE no longer does anything, but we accept it for backwards compatibility
5028 *****************************************************************************/
5031 REINDEX reindex_type qualified_name opt_force
5033 ReindexStmt *n = makeNode(ReindexStmt);
5039 | REINDEX SYSTEM_P name opt_force
5041 ReindexStmt *n = makeNode(ReindexStmt);
5042 n->kind = OBJECT_DATABASE;
5045 n->do_system = true;
5049 | REINDEX DATABASE name opt_force
5051 ReindexStmt *n = makeNode(ReindexStmt);
5052 n->kind = OBJECT_DATABASE;
5055 n->do_system = true;
5062 INDEX { $$ = OBJECT_INDEX; }
5063 | TABLE { $$ = OBJECT_TABLE; }
5066 opt_force: FORCE { $$ = TRUE; }
5067 | /* EMPTY */ { $$ = FALSE; }
5071 /*****************************************************************************
5073 * ALTER THING name RENAME TO newname
5075 *****************************************************************************/
5077 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
5079 RenameStmt *n = makeNode(RenameStmt);
5080 n->renameType = OBJECT_AGGREGATE;
5086 | ALTER CONVERSION_P any_name RENAME TO name
5088 RenameStmt *n = makeNode(RenameStmt);
5089 n->renameType = OBJECT_CONVERSION;
5094 | ALTER DATABASE database_name RENAME TO database_name
5096 RenameStmt *n = makeNode(RenameStmt);
5097 n->renameType = OBJECT_DATABASE;
5102 | ALTER FUNCTION function_with_argtypes RENAME TO name
5104 RenameStmt *n = makeNode(RenameStmt);
5105 n->renameType = OBJECT_FUNCTION;
5106 n->object = $3->funcname;
5107 n->objarg = $3->funcargs;
5111 | ALTER GROUP_P RoleId RENAME TO RoleId
5113 RenameStmt *n = makeNode(RenameStmt);
5114 n->renameType = OBJECT_ROLE;
5119 | ALTER opt_procedural LANGUAGE name RENAME TO name
5121 RenameStmt *n = makeNode(RenameStmt);
5122 n->renameType = OBJECT_LANGUAGE;
5127 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
5129 RenameStmt *n = makeNode(RenameStmt);
5130 n->renameType = OBJECT_OPCLASS;
5136 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
5138 RenameStmt *n = makeNode(RenameStmt);
5139 n->renameType = OBJECT_OPFAMILY;
5145 | ALTER SCHEMA name RENAME TO name
5147 RenameStmt *n = makeNode(RenameStmt);
5148 n->renameType = OBJECT_SCHEMA;
5153 | ALTER TABLE relation_expr RENAME TO name
5155 RenameStmt *n = makeNode(RenameStmt);
5156 n->renameType = OBJECT_TABLE;
5162 | ALTER SEQUENCE relation_expr RENAME TO name
5164 RenameStmt *n = makeNode(RenameStmt);
5165 n->renameType = OBJECT_SEQUENCE;
5171 | ALTER VIEW relation_expr RENAME TO name
5173 RenameStmt *n = makeNode(RenameStmt);
5174 n->renameType = OBJECT_VIEW;
5180 | ALTER INDEX relation_expr RENAME TO name
5182 RenameStmt *n = makeNode(RenameStmt);
5183 n->renameType = OBJECT_INDEX;
5189 | ALTER TABLE relation_expr RENAME opt_column name TO name
5191 RenameStmt *n = makeNode(RenameStmt);
5192 n->renameType = OBJECT_COLUMN;
5198 | ALTER TRIGGER name ON relation_expr RENAME TO name
5200 RenameStmt *n = makeNode(RenameStmt);
5201 n->renameType = OBJECT_TRIGGER;
5207 | ALTER ROLE RoleId RENAME TO RoleId
5209 RenameStmt *n = makeNode(RenameStmt);
5210 n->renameType = OBJECT_ROLE;
5215 | ALTER USER RoleId RENAME TO RoleId
5217 RenameStmt *n = makeNode(RenameStmt);
5218 n->renameType = OBJECT_ROLE;
5223 | ALTER TABLESPACE name RENAME TO name
5225 RenameStmt *n = makeNode(RenameStmt);
5226 n->renameType = OBJECT_TABLESPACE;
5231 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
5233 RenameStmt *n = makeNode(RenameStmt);
5234 n->renameType = OBJECT_TSPARSER;
5239 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
5241 RenameStmt *n = makeNode(RenameStmt);
5242 n->renameType = OBJECT_TSDICTIONARY;
5247 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
5249 RenameStmt *n = makeNode(RenameStmt);
5250 n->renameType = OBJECT_TSTEMPLATE;
5255 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
5257 RenameStmt *n = makeNode(RenameStmt);
5258 n->renameType = OBJECT_TSCONFIGURATION;
5263 | ALTER TYPE_P any_name RENAME TO name
5265 RenameStmt *n = makeNode(RenameStmt);
5266 n->renameType = OBJECT_TYPE;
5273 opt_column: COLUMN { $$ = COLUMN; }
5274 | /*EMPTY*/ { $$ = 0; }
5277 opt_set_data: SET DATA_P { $$ = 1; }
5278 | /*EMPTY*/ { $$ = 0; }
5281 /*****************************************************************************
5283 * ALTER THING name SET SCHEMA name
5285 *****************************************************************************/
5287 AlterObjectSchemaStmt:
5288 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
5290 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5291 n->objectType = OBJECT_AGGREGATE;
5297 | ALTER DOMAIN_P any_name SET SCHEMA name
5299 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5300 n->objectType = OBJECT_DOMAIN;
5305 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
5307 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5308 n->objectType = OBJECT_FUNCTION;
5309 n->object = $3->funcname;
5310 n->objarg = $3->funcargs;
5314 | ALTER TABLE relation_expr SET SCHEMA name
5316 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5317 n->objectType = OBJECT_TABLE;
5322 | ALTER SEQUENCE relation_expr SET SCHEMA name
5324 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5325 n->objectType = OBJECT_SEQUENCE;
5330 | ALTER VIEW relation_expr SET SCHEMA name
5332 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5333 n->objectType = OBJECT_VIEW;
5338 | ALTER TYPE_P any_name SET SCHEMA name
5340 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5341 n->objectType = OBJECT_TYPE;
5348 /*****************************************************************************
5350 * ALTER THING name OWNER TO newname
5352 *****************************************************************************/
5354 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5356 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5357 n->objectType = OBJECT_AGGREGATE;
5363 | ALTER CONVERSION_P any_name OWNER TO RoleId
5365 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5366 n->objectType = OBJECT_CONVERSION;
5371 | ALTER DATABASE database_name OWNER TO RoleId
5373 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5374 n->objectType = OBJECT_DATABASE;
5375 n->object = list_make1(makeString($3));
5379 | ALTER DOMAIN_P any_name OWNER TO RoleId
5381 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5382 n->objectType = OBJECT_DOMAIN;
5387 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5389 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5390 n->objectType = OBJECT_FUNCTION;
5391 n->object = $3->funcname;
5392 n->objarg = $3->funcargs;
5396 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5398 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5399 n->objectType = OBJECT_LANGUAGE;
5400 n->object = list_make1(makeString($4));
5404 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5406 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5407 n->objectType = OBJECT_OPERATOR;
5413 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5415 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5416 n->objectType = OBJECT_OPCLASS;
5422 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5424 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5425 n->objectType = OBJECT_OPFAMILY;
5431 | ALTER SCHEMA name OWNER TO RoleId
5433 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5434 n->objectType = OBJECT_SCHEMA;
5435 n->object = list_make1(makeString($3));
5439 | ALTER TYPE_P any_name OWNER TO RoleId
5441 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5442 n->objectType = OBJECT_TYPE;
5447 | ALTER TABLESPACE name OWNER TO RoleId
5449 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5450 n->objectType = OBJECT_TABLESPACE;
5451 n->object = list_make1(makeString($3));
5455 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5457 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5458 n->objectType = OBJECT_TSDICTIONARY;
5463 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5465 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5466 n->objectType = OBJECT_TSCONFIGURATION;
5471 | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
5473 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5474 n->objectType = OBJECT_FDW;
5475 n->object = list_make1(makeString($5));
5479 | ALTER SERVER name OWNER TO RoleId
5481 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5482 n->objectType = OBJECT_FOREIGN_SERVER;
5483 n->object = list_make1(makeString($3));
5490 /*****************************************************************************
5492 * QUERY: Define Rewrite Rule
5494 *****************************************************************************/
5496 RuleStmt: CREATE opt_or_replace RULE name AS
5497 { QueryIsRule=TRUE; }
5498 ON event TO qualified_name where_clause
5499 DO opt_instead RuleActionList
5501 RuleStmt *n = makeNode(RuleStmt);
5505 n->whereClause = $11;
5515 NOTHING { $$ = NIL; }
5516 | RuleActionStmt { $$ = list_make1($1); }
5517 | '(' RuleActionMulti ')' { $$ = $2; }
5520 /* the thrashing around here is to discard "empty" statements... */
5522 RuleActionMulti ';' RuleActionStmtOrEmpty
5524 $$ = lappend($1, $3);
5528 | RuleActionStmtOrEmpty
5530 $$ = list_make1($1);
5544 RuleActionStmtOrEmpty:
5545 RuleActionStmt { $$ = $1; }
5546 | /*EMPTY*/ { $$ = NULL; }
5549 event: SELECT { $$ = CMD_SELECT; }
5550 | UPDATE { $$ = CMD_UPDATE; }
5551 | DELETE_P { $$ = CMD_DELETE; }
5552 | INSERT { $$ = CMD_INSERT; }
5556 INSTEAD { $$ = TRUE; }
5557 | ALSO { $$ = FALSE; }
5558 | /*EMPTY*/ { $$ = FALSE; }
5563 DROP RULE name ON qualified_name opt_drop_behavior
5565 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5569 n->removeType = OBJECT_RULE;
5570 n->missing_ok = false;
5573 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5575 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5579 n->removeType = OBJECT_RULE;
5580 n->missing_ok = true;
5586 /*****************************************************************************
5589 * NOTIFY <identifier> can appear both in rule bodies and
5590 * as a query-level command
5592 *****************************************************************************/
5594 NotifyStmt: NOTIFY ColId
5596 NotifyStmt *n = makeNode(NotifyStmt);
5597 n->conditionname = $2;
5602 ListenStmt: LISTEN ColId
5604 ListenStmt *n = makeNode(ListenStmt);
5605 n->conditionname = $2;
5613 UnlistenStmt *n = makeNode(UnlistenStmt);
5614 n->conditionname = $2;
5619 UnlistenStmt *n = makeNode(UnlistenStmt);
5620 n->conditionname = NULL;
5626 /*****************************************************************************
5630 * BEGIN / COMMIT / ROLLBACK
5631 * (also older versions END / ABORT)
5633 *****************************************************************************/
5636 ABORT_P opt_transaction
5638 TransactionStmt *n = makeNode(TransactionStmt);
5639 n->kind = TRANS_STMT_ROLLBACK;
5643 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5645 TransactionStmt *n = makeNode(TransactionStmt);
5646 n->kind = TRANS_STMT_BEGIN;
5650 | START TRANSACTION transaction_mode_list_or_empty
5652 TransactionStmt *n = makeNode(TransactionStmt);
5653 n->kind = TRANS_STMT_START;
5657 | COMMIT opt_transaction
5659 TransactionStmt *n = makeNode(TransactionStmt);
5660 n->kind = TRANS_STMT_COMMIT;
5664 | END_P opt_transaction
5666 TransactionStmt *n = makeNode(TransactionStmt);
5667 n->kind = TRANS_STMT_COMMIT;
5671 | ROLLBACK opt_transaction
5673 TransactionStmt *n = makeNode(TransactionStmt);
5674 n->kind = TRANS_STMT_ROLLBACK;
5680 TransactionStmt *n = makeNode(TransactionStmt);
5681 n->kind = TRANS_STMT_SAVEPOINT;
5682 n->options = list_make1(makeDefElem("savepoint_name",
5683 (Node *)makeString($2)));
5686 | RELEASE SAVEPOINT ColId
5688 TransactionStmt *n = makeNode(TransactionStmt);
5689 n->kind = TRANS_STMT_RELEASE;
5690 n->options = list_make1(makeDefElem("savepoint_name",
5691 (Node *)makeString($3)));
5696 TransactionStmt *n = makeNode(TransactionStmt);
5697 n->kind = TRANS_STMT_RELEASE;
5698 n->options = list_make1(makeDefElem("savepoint_name",
5699 (Node *)makeString($2)));
5702 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5704 TransactionStmt *n = makeNode(TransactionStmt);
5705 n->kind = TRANS_STMT_ROLLBACK_TO;
5706 n->options = list_make1(makeDefElem("savepoint_name",
5707 (Node *)makeString($5)));
5710 | ROLLBACK opt_transaction TO ColId
5712 TransactionStmt *n = makeNode(TransactionStmt);
5713 n->kind = TRANS_STMT_ROLLBACK_TO;
5714 n->options = list_make1(makeDefElem("savepoint_name",
5715 (Node *)makeString($4)));
5718 | PREPARE TRANSACTION Sconst
5720 TransactionStmt *n = makeNode(TransactionStmt);
5721 n->kind = TRANS_STMT_PREPARE;
5725 | COMMIT PREPARED Sconst
5727 TransactionStmt *n = makeNode(TransactionStmt);
5728 n->kind = TRANS_STMT_COMMIT_PREPARED;
5732 | ROLLBACK PREPARED Sconst
5734 TransactionStmt *n = makeNode(TransactionStmt);
5735 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5741 opt_transaction: WORK {}
5746 transaction_mode_item:
5747 ISOLATION LEVEL iso_level
5748 { $$ = makeDefElem("transaction_isolation",
5749 makeStringConst($3, @3)); }
5751 { $$ = makeDefElem("transaction_read_only",
5752 makeIntConst(TRUE, @1)); }
5754 { $$ = makeDefElem("transaction_read_only",
5755 makeIntConst(FALSE, @1)); }
5758 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5759 transaction_mode_list:
5760 transaction_mode_item
5761 { $$ = list_make1($1); }
5762 | transaction_mode_list ',' transaction_mode_item
5763 { $$ = lappend($1, $3); }
5764 | transaction_mode_list transaction_mode_item
5765 { $$ = lappend($1, $2); }
5768 transaction_mode_list_or_empty:
5769 transaction_mode_list
5775 /*****************************************************************************
5778 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5779 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5781 *****************************************************************************/
5783 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5784 AS SelectStmt opt_check_option
5786 ViewStmt *n = makeNode(ViewStmt);
5788 n->view->istemp = $2;
5794 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5795 AS SelectStmt opt_check_option
5797 ViewStmt *n = makeNode(ViewStmt);
5799 n->view->istemp = $4;
5811 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5812 errmsg("WITH CHECK OPTION is not implemented")));
5814 | WITH CASCADED CHECK OPTION
5817 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5818 errmsg("WITH CHECK OPTION is not implemented")));
5820 | WITH LOCAL CHECK OPTION
5823 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5824 errmsg("WITH CHECK OPTION is not implemented")));
5826 | /* EMPTY */ { $$ = NIL; }
5829 /*****************************************************************************
5834 *****************************************************************************/
5836 LoadStmt: LOAD file_name
5838 LoadStmt *n = makeNode(LoadStmt);
5845 /*****************************************************************************
5849 *****************************************************************************/
5852 CREATE DATABASE database_name opt_with createdb_opt_list
5854 CreatedbStmt *n = makeNode(CreatedbStmt);
5862 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5863 | /* EMPTY */ { $$ = NIL; }
5867 TABLESPACE opt_equal name
5869 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5871 | TABLESPACE opt_equal DEFAULT
5873 $$ = makeDefElem("tablespace", NULL);
5875 | LOCATION opt_equal Sconst
5877 $$ = makeDefElem("location", (Node *)makeString($3));
5879 | LOCATION opt_equal DEFAULT
5881 $$ = makeDefElem("location", NULL);
5883 | TEMPLATE opt_equal name
5885 $$ = makeDefElem("template", (Node *)makeString($3));
5887 | TEMPLATE opt_equal DEFAULT
5889 $$ = makeDefElem("template", NULL);
5891 | ENCODING opt_equal Sconst
5893 $$ = makeDefElem("encoding", (Node *)makeString($3));
5895 | ENCODING opt_equal Iconst
5897 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5899 | ENCODING opt_equal DEFAULT
5901 $$ = makeDefElem("encoding", NULL);
5903 | COLLATE opt_equal Sconst
5905 $$ = makeDefElem("collate", (Node *)makeString($3));
5907 | COLLATE opt_equal DEFAULT
5909 $$ = makeDefElem("collate", NULL);
5911 | CTYPE opt_equal Sconst
5913 $$ = makeDefElem("ctype", (Node *)makeString($3));
5915 | CTYPE opt_equal DEFAULT
5917 $$ = makeDefElem("ctype", NULL);
5919 | CONNECTION LIMIT opt_equal SignedIconst
5921 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5923 | OWNER opt_equal name
5925 $$ = makeDefElem("owner", (Node *)makeString($3));
5927 | OWNER opt_equal DEFAULT
5929 $$ = makeDefElem("owner", NULL);
5934 * Though the equals sign doesn't match other WITH options, pg_dump uses
5935 * equals for backward compatibility, and it doesn't seem worth removing it.
5942 /*****************************************************************************
5946 *****************************************************************************/
5949 ALTER DATABASE database_name opt_with alterdb_opt_list
5951 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5956 | ALTER DATABASE database_name SET TABLESPACE name
5958 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5960 n->options = list_make1(makeDefElem("tablespace",
5961 (Node *)makeString($6)));
5966 AlterDatabaseSetStmt:
5967 ALTER DATABASE database_name SetResetClause
5969 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5978 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5979 | /* EMPTY */ { $$ = NIL; }
5983 CONNECTION LIMIT opt_equal SignedIconst
5985 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5990 /*****************************************************************************
5992 * DROP DATABASE [ IF EXISTS ]
5994 * This is implicitly CASCADE, no need for drop behavior
5995 *****************************************************************************/
5997 DropdbStmt: DROP DATABASE database_name
5999 DropdbStmt *n = makeNode(DropdbStmt);
6001 n->missing_ok = FALSE;
6004 | DROP DATABASE IF_P EXISTS database_name
6006 DropdbStmt *n = makeNode(DropdbStmt);
6008 n->missing_ok = TRUE;
6014 /*****************************************************************************
6016 * Manipulate a domain
6018 *****************************************************************************/
6021 CREATE DOMAIN_P any_name opt_as Typename ColQualList
6023 CreateDomainStmt *n = makeNode(CreateDomainStmt);
6026 n->constraints = $6;
6032 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
6033 ALTER DOMAIN_P any_name alter_column_default
6035 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6041 /* ALTER DOMAIN <domain> DROP NOT NULL */
6042 | ALTER DOMAIN_P any_name DROP NOT NULL_P
6044 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6049 /* ALTER DOMAIN <domain> SET NOT NULL */
6050 | ALTER DOMAIN_P any_name SET NOT NULL_P
6052 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6057 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
6058 | ALTER DOMAIN_P any_name ADD_P TableConstraint
6060 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6066 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
6067 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
6069 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6083 /*****************************************************************************
6085 * Manipulate a text search dictionary or configuration
6087 *****************************************************************************/
6089 AlterTSDictionaryStmt:
6090 ALTER TEXT_P SEARCH DICTIONARY any_name definition
6092 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
6099 AlterTSConfigurationStmt:
6100 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
6102 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6106 n->override = false;
6110 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
6112 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6120 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
6122 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6125 n->dicts = list_make2($9,$11);
6126 n->override = false;
6130 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
6132 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6135 n->dicts = list_make2($11,$13);
6136 n->override = false;
6140 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
6142 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6145 n->missing_ok = false;
6148 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
6150 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6153 n->missing_ok = true;
6159 /*****************************************************************************
6161 * Manipulate a conversion
6163 * CREATE [DEFAULT] CONVERSION <conversion_name>
6164 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
6166 *****************************************************************************/
6168 CreateConversionStmt:
6169 CREATE opt_default CONVERSION_P any_name FOR Sconst
6170 TO Sconst FROM any_name
6172 CreateConversionStmt *n = makeNode(CreateConversionStmt);
6173 n->conversion_name = $4;
6174 n->for_encoding_name = $6;
6175 n->to_encoding_name = $8;
6182 /*****************************************************************************
6185 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
6187 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
6189 *****************************************************************************/
6192 CLUSTER opt_verbose qualified_name cluster_index_specification
6194 ClusterStmt *n = makeNode(ClusterStmt);
6200 | CLUSTER opt_verbose
6202 ClusterStmt *n = makeNode(ClusterStmt);
6204 n->indexname = NULL;
6208 /* kept for pre-8.3 compatibility */
6209 | CLUSTER opt_verbose index_name ON qualified_name
6211 ClusterStmt *n = makeNode(ClusterStmt);
6219 cluster_index_specification:
6220 USING index_name { $$ = $2; }
6221 | /*EMPTY*/ { $$ = NULL; }
6225 /*****************************************************************************
6231 *****************************************************************************/
6233 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
6235 VacuumStmt *n = makeNode(VacuumStmt);
6239 n->freeze_min_age = $3 ? 0 : -1;
6240 n->scan_all = $2 || $3;
6246 | VACUUM opt_full opt_freeze opt_verbose qualified_name
6248 VacuumStmt *n = makeNode(VacuumStmt);
6252 n->freeze_min_age = $3 ? 0 : -1;
6253 n->scan_all = $2 || $3;
6259 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
6261 VacuumStmt *n = (VacuumStmt *) $5;
6264 n->freeze_min_age = $3 ? 0 : -1;
6265 n->scan_all = $2 || $3;
6272 analyze_keyword opt_verbose
6274 VacuumStmt *n = makeNode(VacuumStmt);
6278 n->freeze_min_age = -1;
6284 | analyze_keyword opt_verbose qualified_name opt_name_list
6286 VacuumStmt *n = makeNode(VacuumStmt);
6290 n->freeze_min_age = -1;
6300 | ANALYSE /* British */ {}
6304 VERBOSE { $$ = TRUE; }
6305 | /*EMPTY*/ { $$ = FALSE; }
6308 opt_full: FULL { $$ = TRUE; }
6309 | /*EMPTY*/ { $$ = FALSE; }
6312 opt_freeze: FREEZE { $$ = TRUE; }
6313 | /*EMPTY*/ { $$ = FALSE; }
6317 '(' name_list ')' { $$ = $2; }
6318 | /*EMPTY*/ { $$ = NIL; }
6322 /*****************************************************************************
6325 * EXPLAIN [ANALYZE] [VERBOSE] query
6327 *****************************************************************************/
6329 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
6331 ExplainStmt *n = makeNode(ExplainStmt);
6346 | ExecuteStmt /* by default all are $$=$1 */
6350 analyze_keyword { $$ = TRUE; }
6351 | /* EMPTY */ { $$ = FALSE; }
6354 /*****************************************************************************
6357 * PREPARE <plan_name> [(args, ...)] AS <query>
6359 *****************************************************************************/
6361 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
6363 PrepareStmt *n = makeNode(PrepareStmt);
6371 prep_type_clause: '(' type_list ')' { $$ = $2; }
6372 | /* EMPTY */ { $$ = NIL; }
6379 | DeleteStmt /* by default all are $$=$1 */
6382 /*****************************************************************************
6384 * EXECUTE <plan_name> [(params, ...)]
6385 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
6387 *****************************************************************************/
6389 ExecuteStmt: EXECUTE name execute_param_clause
6391 ExecuteStmt *n = makeNode(ExecuteStmt);
6397 | CREATE OptTemp TABLE create_as_target AS
6398 EXECUTE name execute_param_clause
6400 ExecuteStmt *n = makeNode(ExecuteStmt);
6403 $4->rel->istemp = $2;
6407 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6408 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6409 /* ... because it's not implemented, but it could be */
6414 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6415 | /* EMPTY */ { $$ = NIL; }
6418 /*****************************************************************************
6421 * DEALLOCATE [PREPARE] <plan_name>
6423 *****************************************************************************/
6425 DeallocateStmt: DEALLOCATE name
6427 DeallocateStmt *n = makeNode(DeallocateStmt);
6431 | DEALLOCATE PREPARE name
6433 DeallocateStmt *n = makeNode(DeallocateStmt);
6439 DeallocateStmt *n = makeNode(DeallocateStmt);
6443 | DEALLOCATE PREPARE ALL
6445 DeallocateStmt *n = makeNode(DeallocateStmt);
6451 /*****************************************************************************
6456 *****************************************************************************/
6459 INSERT INTO qualified_name insert_rest returning_clause
6462 $4->returningList = $5;
6470 $$ = makeNode(InsertStmt);
6472 $$->selectStmt = $1;
6474 | '(' insert_column_list ')' SelectStmt
6476 $$ = makeNode(InsertStmt);
6478 $$->selectStmt = $4;
6482 $$ = makeNode(InsertStmt);
6484 $$->selectStmt = NULL;
6490 { $$ = list_make1($1); }
6491 | insert_column_list ',' insert_column_item
6492 { $$ = lappend($1, $3); }
6496 ColId opt_indirection
6498 $$ = makeNode(ResTarget);
6500 $$->indirection = check_indirection($2);
6507 RETURNING target_list { $$ = $2; }
6508 | /* EMPTY */ { $$ = NIL; }
6512 /*****************************************************************************
6517 *****************************************************************************/
6519 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6520 using_clause where_or_current_clause returning_clause
6522 DeleteStmt *n = makeNode(DeleteStmt);
6524 n->usingClause = $4;
6525 n->whereClause = $5;
6526 n->returningList = $6;
6532 USING from_list { $$ = $2; }
6533 | /*EMPTY*/ { $$ = NIL; }
6536 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
6538 LockStmt *n = makeNode(LockStmt);
6547 opt_lock: IN_P lock_type MODE { $$ = $2; }
6548 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6551 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6552 | ROW SHARE { $$ = RowShareLock; }
6553 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6554 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6555 | SHARE { $$ = ShareLock; }
6556 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6557 | EXCLUSIVE { $$ = ExclusiveLock; }
6558 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6561 opt_nowait: NOWAIT { $$ = TRUE; }
6562 | /*EMPTY*/ { $$ = FALSE; }
6566 /*****************************************************************************
6569 * UpdateStmt (UPDATE)
6571 *****************************************************************************/
6573 UpdateStmt: UPDATE relation_expr_opt_alias
6576 where_or_current_clause
6579 UpdateStmt *n = makeNode(UpdateStmt);
6583 n->whereClause = $6;
6584 n->returningList = $7;
6590 set_clause { $$ = $1; }
6591 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6595 single_set_clause { $$ = list_make1($1); }
6596 | multiple_set_clause { $$ = $1; }
6600 set_target '=' ctext_expr
6603 $$->val = (Node *) $3;
6607 multiple_set_clause:
6608 '(' set_target_list ')' '=' ctext_row
6614 * Break the ctext_row apart, merge individual expressions
6615 * into the destination ResTargets. XXX this approach
6616 * cannot work for general row expressions as sources.
6618 if (list_length($2) != list_length($5))
6620 (errcode(ERRCODE_SYNTAX_ERROR),
6621 errmsg("number of columns does not match number of values"),
6622 scanner_errposition(@1)));
6623 forboth(col_cell, $2, val_cell, $5)
6625 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6626 Node *res_val = (Node *) lfirst(val_cell);
6628 res_col->val = res_val;
6636 ColId opt_indirection
6638 $$ = makeNode(ResTarget);
6640 $$->indirection = check_indirection($2);
6641 $$->val = NULL; /* upper production sets this */
6647 set_target { $$ = list_make1($1); }
6648 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6652 /*****************************************************************************
6657 *****************************************************************************/
6658 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6660 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6662 /* currently we always set FAST_PLAN option */
6663 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6669 cursor_options: /*EMPTY*/ { $$ = 0; }
6670 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6671 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6672 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6673 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6676 opt_hold: /* EMPTY */ { $$ = 0; }
6677 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6678 | WITHOUT HOLD { $$ = 0; }
6681 /*****************************************************************************
6686 *****************************************************************************/
6688 /* A complete SELECT statement looks like this.
6690 * The rule returns either a single SelectStmt node or a tree of them,
6691 * representing a set-operation tree.
6693 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6694 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6695 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6696 * To resolve the ambiguity, we are careful to define the grammar so that
6697 * the decision is staved off as long as possible: as long as we can keep
6698 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6699 * it's no longer possible to do that will we decide that parens belong to
6700 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6701 * parentheses are treated as part of the sub-select. The necessity of doing
6702 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6703 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6704 * SELECT viewpoint when we see the UNION.
6706 * This approach is implemented by defining a nonterminal select_with_parens,
6707 * which represents a SELECT with at least one outer layer of parentheses,
6708 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6709 * in the expression grammar. We will then have shift-reduce conflicts
6710 * which we can resolve in favor of always treating '(' <select> ')' as
6711 * a select_with_parens. To resolve the conflicts, the productions that
6712 * conflict with the select_with_parens productions are manually given
6713 * precedences lower than the precedence of ')', thereby ensuring that we
6714 * shift ')' (and then reduce to select_with_parens) rather than trying to
6715 * reduce the inner <select> nonterminal to something else. We use UMINUS
6716 * precedence for this, which is a fairly arbitrary choice.
6718 * To be able to define select_with_parens itself without ambiguity, we need
6719 * a nonterminal select_no_parens that represents a SELECT structure with no
6720 * outermost parentheses. This is a little bit tedious, but it works.
6722 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6723 * with or without outer parentheses.
6726 SelectStmt: select_no_parens %prec UMINUS
6727 | select_with_parens %prec UMINUS
6731 '(' select_no_parens ')' { $$ = $2; }
6732 | '(' select_with_parens ')' { $$ = $2; }
6736 * This rule parses the equivalent of the standard's <query expression>.
6737 * The duplicative productions are annoying, but hard to get rid of without
6738 * creating shift/reduce conflicts.
6740 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6741 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6742 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6746 simple_select { $$ = $1; }
6747 | select_clause sort_clause
6749 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6753 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6755 insertSelectOptions((SelectStmt *) $1, $2, $3,
6756 list_nth($4, 0), list_nth($4, 1),
6760 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6762 insertSelectOptions((SelectStmt *) $1, $2, $4,
6763 list_nth($3, 0), list_nth($3, 1),
6767 | with_clause simple_select
6769 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6774 | with_clause select_clause sort_clause
6776 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6781 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6783 insertSelectOptions((SelectStmt *) $2, $3, $4,
6784 list_nth($5, 0), list_nth($5, 1),
6788 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6790 insertSelectOptions((SelectStmt *) $2, $3, $5,
6791 list_nth($4, 0), list_nth($4, 1),
6798 simple_select { $$ = $1; }
6799 | select_with_parens { $$ = $1; }
6803 * This rule parses SELECT statements that can appear within set operations,
6804 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6805 * the ordering of the set operations. Without '(' and ')' we want the
6806 * operations to be ordered per the precedence specs at the head of this file.
6808 * As with select_no_parens, simple_select cannot have outer parentheses,
6809 * but can have parenthesized subclauses.
6811 * Note that sort clauses cannot be included at this level --- SQL92 requires
6812 * SELECT foo UNION SELECT bar ORDER BY baz
6814 * (SELECT foo UNION SELECT bar) ORDER BY baz
6816 * SELECT foo UNION (SELECT bar ORDER BY baz)
6817 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6818 * described as part of the select_no_parens production, not simple_select.
6819 * This does not limit functionality, because you can reintroduce these
6820 * clauses inside parentheses.
6822 * NOTE: only the leftmost component SelectStmt should have INTO.
6823 * However, this is not checked by the grammar; parse analysis must check it.
6826 SELECT opt_distinct target_list
6827 into_clause from_clause where_clause
6828 group_clause having_clause
6830 SelectStmt *n = makeNode(SelectStmt);
6831 n->distinctClause = $2;
6835 n->whereClause = $6;
6836 n->groupClause = $7;
6837 n->havingClause = $8;
6840 | values_clause { $$ = $1; }
6841 | TABLE relation_expr
6843 /* same as SELECT * FROM relation_expr */
6844 ColumnRef *cr = makeNode(ColumnRef);
6845 ResTarget *rt = makeNode(ResTarget);
6846 SelectStmt *n = makeNode(SelectStmt);
6848 cr->fields = list_make1(makeNode(A_Star));
6852 rt->indirection = NIL;
6853 rt->val = (Node *)cr;
6856 n->targetList = list_make1(rt);
6857 n->fromClause = list_make1($2);
6860 | select_clause UNION opt_all select_clause
6862 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6864 | select_clause INTERSECT opt_all select_clause
6866 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6868 | select_clause EXCEPT opt_all select_clause
6870 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6875 * SQL standard WITH clause looks like:
6877 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6878 * AS (query) [ SEARCH or CYCLE clause ]
6880 * We don't currently support the SEARCH or CYCLE clause.
6885 $$ = makeNode(WithClause);
6887 $$->recursive = false;
6890 | WITH RECURSIVE cte_list
6892 $$ = makeNode(WithClause);
6894 $$->recursive = true;
6900 common_table_expr { $$ = list_make1($1); }
6901 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
6904 common_table_expr: name opt_name_list AS select_with_parens
6906 CommonTableExpr *n = makeNode(CommonTableExpr);
6908 n->aliascolnames = $2;
6916 INTO OptTempTableName
6918 $$ = makeNode(IntoClause);
6922 $$->onCommit = ONCOMMIT_NOOP;
6923 $$->tableSpaceName = NULL;
6930 * Redundancy here is needed to avoid shift/reduce conflicts,
6931 * since TEMP is not a reserved word. See also OptTemp.
6934 TEMPORARY opt_table qualified_name
6939 | TEMP opt_table qualified_name
6944 | LOCAL TEMPORARY opt_table qualified_name
6949 | LOCAL TEMP opt_table qualified_name
6954 | GLOBAL TEMPORARY opt_table qualified_name
6959 | GLOBAL TEMP opt_table qualified_name
6964 | TABLE qualified_name
6980 opt_all: ALL { $$ = TRUE; }
6981 | DISTINCT { $$ = FALSE; }
6982 | /*EMPTY*/ { $$ = FALSE; }
6985 /* We use (NIL) as a placeholder to indicate that all target expressions
6986 * should be placed in the DISTINCT list during parsetree analysis.
6989 DISTINCT { $$ = list_make1(NIL); }
6990 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6992 | /*EMPTY*/ { $$ = NIL; }
6996 sort_clause { $$ = $1;}
6997 | /*EMPTY*/ { $$ = NIL; }
7001 ORDER BY sortby_list { $$ = $3; }
7005 sortby { $$ = list_make1($1); }
7006 | sortby_list ',' sortby { $$ = lappend($1, $3); }
7009 sortby: a_expr USING qual_all_Op opt_nulls_order
7011 $$ = makeNode(SortBy);
7013 $$->sortby_dir = SORTBY_USING;
7014 $$->sortby_nulls = $4;
7018 | a_expr opt_asc_desc opt_nulls_order
7020 $$ = makeNode(SortBy);
7022 $$->sortby_dir = $2;
7023 $$->sortby_nulls = $3;
7025 $$->location = -1; /* no operator */
7031 LIMIT select_limit_value OFFSET select_offset_value
7032 { $$ = list_make2($4, $2); }
7033 | OFFSET select_offset_value LIMIT select_limit_value
7034 { $$ = list_make2($2, $4); }
7035 | LIMIT select_limit_value
7036 { $$ = list_make2(NULL, $2); }
7037 | OFFSET select_offset_value
7038 { $$ = list_make2($2, NULL); }
7039 | LIMIT select_limit_value ',' select_offset_value
7041 /* Disabled because it was too confusing, bjm 2002-02-18 */
7043 (errcode(ERRCODE_SYNTAX_ERROR),
7044 errmsg("LIMIT #,# syntax is not supported"),
7045 errhint("Use separate LIMIT and OFFSET clauses."),
7046 scanner_errposition(@1)));
7048 /* SQL:2008 syntax variants */
7049 | OFFSET select_offset_value2 row_or_rows
7050 { $$ = list_make2($2, NULL); }
7051 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7052 { $$ = list_make2(NULL, $3); }
7053 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7054 { $$ = list_make2($2, $6); }
7058 select_limit { $$ = $1; }
7060 { $$ = list_make2(NULL,NULL); }
7067 /* LIMIT ALL is represented as a NULL constant */
7068 $$ = makeNullAConst(@1);
7073 * Allowing full expressions without parentheses causes various parsing
7074 * problems with the trailing ROW/ROWS key words. SQL only calls for
7075 * constants, so we allow the rest only with parentheses.
7077 opt_select_fetch_first_value:
7078 SignedIconst { $$ = makeIntConst($1, @1); }
7079 | '(' a_expr ')' { $$ = $2; }
7080 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
7083 select_offset_value:
7088 * Again, the trailing ROW/ROWS in this case prevent the full expression
7089 * syntax. c_expr is the best we can do.
7091 select_offset_value2:
7108 GROUP_P BY expr_list { $$ = $3; }
7109 | /*EMPTY*/ { $$ = NIL; }
7113 HAVING a_expr { $$ = $2; }
7114 | /*EMPTY*/ { $$ = NULL; }
7118 for_locking_items { $$ = $1; }
7119 | FOR READ ONLY { $$ = NIL; }
7122 opt_for_locking_clause:
7123 for_locking_clause { $$ = $1; }
7124 | /* EMPTY */ { $$ = NIL; }
7128 for_locking_item { $$ = list_make1($1); }
7129 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
7133 FOR UPDATE locked_rels_list opt_nowait
7135 LockingClause *n = makeNode(LockingClause);
7137 n->forUpdate = TRUE;
7141 | FOR SHARE locked_rels_list opt_nowait
7143 LockingClause *n = makeNode(LockingClause);
7145 n->forUpdate = FALSE;
7152 OF qualified_name_list { $$ = $2; }
7153 | /* EMPTY */ { $$ = NIL; }
7160 SelectStmt *n = makeNode(SelectStmt);
7161 n->valuesLists = list_make1($2);
7164 | values_clause ',' ctext_row
7166 SelectStmt *n = (SelectStmt *) $1;
7167 n->valuesLists = lappend(n->valuesLists, $3);
7173 /*****************************************************************************
7175 * clauses common to all Optimizable Stmts:
7176 * from_clause - allow list of both JOIN expressions and table names
7177 * where_clause - qualifications for joins or restrictions
7179 *****************************************************************************/
7182 FROM from_list { $$ = $2; }
7183 | /*EMPTY*/ { $$ = NIL; }
7187 table_ref { $$ = list_make1($1); }
7188 | from_list ',' table_ref { $$ = lappend($1, $3); }
7192 * table_ref is where an alias clause can be attached. Note we cannot make
7193 * alias_clause have an empty production because that causes parse conflicts
7194 * between table_ref := '(' joined_table ')' alias_clause
7195 * and joined_table := '(' joined_table ')'. So, we must have the
7196 * redundant-looking productions here instead.
7198 table_ref: relation_expr
7202 | relation_expr alias_clause
7209 RangeFunction *n = makeNode(RangeFunction);
7210 n->funccallnode = $1;
7211 n->coldeflist = NIL;
7214 | func_table alias_clause
7216 RangeFunction *n = makeNode(RangeFunction);
7217 n->funccallnode = $1;
7219 n->coldeflist = NIL;
7222 | func_table AS '(' TableFuncElementList ')'
7224 RangeFunction *n = makeNode(RangeFunction);
7225 n->funccallnode = $1;
7229 | func_table AS ColId '(' TableFuncElementList ')'
7231 RangeFunction *n = makeNode(RangeFunction);
7232 Alias *a = makeNode(Alias);
7233 n->funccallnode = $1;
7239 | func_table ColId '(' TableFuncElementList ')'
7241 RangeFunction *n = makeNode(RangeFunction);
7242 Alias *a = makeNode(Alias);
7243 n->funccallnode = $1;
7249 | select_with_parens
7252 * The SQL spec does not permit a subselect
7253 * (<derived_table>) without an alias clause,
7254 * so we don't either. This avoids the problem
7255 * of needing to invent a unique refname for it.
7256 * That could be surmounted if there's sufficient
7257 * popular demand, but for now let's just implement
7258 * the spec and see if anyone complains.
7259 * However, it does seem like a good idea to emit
7260 * an error message that's better than "syntax error".
7262 if (IsA($1, SelectStmt) &&
7263 ((SelectStmt *) $1)->valuesLists)
7265 (errcode(ERRCODE_SYNTAX_ERROR),
7266 errmsg("VALUES in FROM must have an alias"),
7267 errhint("For example, FROM (VALUES ...) [AS] foo."),
7268 scanner_errposition(@1)));
7271 (errcode(ERRCODE_SYNTAX_ERROR),
7272 errmsg("subquery in FROM must have an alias"),
7273 errhint("For example, FROM (SELECT ...) [AS] foo."),
7274 scanner_errposition(@1)));
7277 | select_with_parens alias_clause
7279 RangeSubselect *n = makeNode(RangeSubselect);
7288 | '(' joined_table ')' alias_clause
7297 * It may seem silly to separate joined_table from table_ref, but there is
7298 * method in SQL92's madness: if you don't do it this way you get reduce-
7299 * reduce conflicts, because it's not clear to the parser generator whether
7300 * to expect alias_clause after ')' or not. For the same reason we must
7301 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
7302 * join_type to expand to empty; if we try it, the parser generator can't
7303 * figure out when to reduce an empty join_type right after table_ref.
7305 * Note that a CROSS JOIN is the same as an unqualified
7306 * INNER JOIN, and an INNER JOIN/ON has the same shape
7307 * but a qualification expression to limit membership.
7308 * A NATURAL JOIN implicitly matches column names between
7309 * tables and the shape is determined by which columns are
7310 * in common. We'll collect columns during the later transformations.
7314 '(' joined_table ')'
7318 | table_ref CROSS JOIN table_ref
7320 /* CROSS JOIN is same as unqualified inner join */
7321 JoinExpr *n = makeNode(JoinExpr);
7322 n->jointype = JOIN_INNER;
7323 n->isNatural = FALSE;
7330 | table_ref join_type JOIN table_ref join_qual
7332 JoinExpr *n = makeNode(JoinExpr);
7334 n->isNatural = FALSE;
7337 if ($5 != NULL && IsA($5, List))
7338 n->using = (List *) $5; /* USING clause */
7340 n->quals = $5; /* ON clause */
7343 | table_ref JOIN table_ref join_qual
7345 /* letting join_type reduce to empty doesn't work */
7346 JoinExpr *n = makeNode(JoinExpr);
7347 n->jointype = JOIN_INNER;
7348 n->isNatural = FALSE;
7351 if ($4 != NULL && IsA($4, List))
7352 n->using = (List *) $4; /* USING clause */
7354 n->quals = $4; /* ON clause */
7357 | table_ref NATURAL join_type JOIN table_ref
7359 JoinExpr *n = makeNode(JoinExpr);
7361 n->isNatural = TRUE;
7364 n->using = NIL; /* figure out which columns later... */
7365 n->quals = NULL; /* fill later */
7368 | table_ref NATURAL JOIN table_ref
7370 /* letting join_type reduce to empty doesn't work */
7371 JoinExpr *n = makeNode(JoinExpr);
7372 n->jointype = JOIN_INNER;
7373 n->isNatural = TRUE;
7376 n->using = NIL; /* figure out which columns later... */
7377 n->quals = NULL; /* fill later */
7383 AS ColId '(' name_list ')'
7385 $$ = makeNode(Alias);
7391 $$ = makeNode(Alias);
7394 | ColId '(' name_list ')'
7396 $$ = makeNode(Alias);
7402 $$ = makeNode(Alias);
7407 join_type: FULL join_outer { $$ = JOIN_FULL; }
7408 | LEFT join_outer { $$ = JOIN_LEFT; }
7409 | RIGHT join_outer { $$ = JOIN_RIGHT; }
7410 | INNER_P { $$ = JOIN_INNER; }
7413 /* OUTER is just noise... */
7414 join_outer: OUTER_P { $$ = NULL; }
7415 | /*EMPTY*/ { $$ = NULL; }
7418 /* JOIN qualification clauses
7419 * Possibilities are:
7420 * USING ( column list ) allows only unqualified column names,
7421 * which must match between tables.
7422 * ON expr allows more general qualifications.
7424 * We return USING as a List node, while an ON-expr will not be a List.
7427 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
7428 | ON a_expr { $$ = $2; }
7435 /* default inheritance */
7437 $$->inhOpt = INH_DEFAULT;
7440 | qualified_name '*'
7442 /* inheritance query */
7444 $$->inhOpt = INH_YES;
7447 | ONLY qualified_name
7449 /* no inheritance */
7451 $$->inhOpt = INH_NO;
7454 | ONLY '(' qualified_name ')'
7456 /* no inheritance, SQL99-style syntax */
7458 $$->inhOpt = INH_NO;
7465 * Given "UPDATE foo set set ...", we have to decide without looking any
7466 * further ahead whether the first "set" is an alias or the UPDATE's SET
7467 * keyword. Since "set" is allowed as a column name both interpretations
7468 * are feasible. We resolve the shift/reduce conflict by giving the first
7469 * relation_expr_opt_alias production a higher precedence than the SET token
7470 * has, causing the parser to prefer to reduce, in effect assuming that the
7471 * SET is not an alias.
7473 relation_expr_opt_alias: relation_expr %prec UMINUS
7477 | relation_expr ColId
7479 Alias *alias = makeNode(Alias);
7480 alias->aliasname = $2;
7484 | relation_expr AS ColId
7486 Alias *alias = makeNode(Alias);
7487 alias->aliasname = $3;
7494 func_table: func_expr { $$ = $1; }
7499 WHERE a_expr { $$ = $2; }
7500 | /*EMPTY*/ { $$ = NULL; }
7503 /* variant for UPDATE and DELETE */
7504 where_or_current_clause:
7505 WHERE a_expr { $$ = $2; }
7506 | WHERE CURRENT_P OF name
7508 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7509 /* cvarno is filled in by parse analysis */
7510 n->cursor_name = $4;
7511 n->cursor_param = 0;
7514 | WHERE CURRENT_P OF PARAM
7516 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7517 /* cvarno is filled in by parse analysis */
7518 n->cursor_name = NULL;
7519 n->cursor_param = $4;
7522 | /*EMPTY*/ { $$ = NULL; }
7526 TableFuncElementList:
7529 $$ = list_make1($1);
7531 | TableFuncElementList ',' TableFuncElement
7533 $$ = lappend($1, $3);
7537 TableFuncElement: ColId Typename
7539 ColumnDef *n = makeNode(ColumnDef);
7542 n->constraints = NIL;
7548 /*****************************************************************************
7551 * SQL92 introduces a large amount of type-specific syntax.
7552 * Define individual clauses to handle these cases, and use
7553 * the generic case to handle regular type-extensible Postgres syntax.
7554 * - thomas 1997-10-10
7556 *****************************************************************************/
7558 Typename: SimpleTypename opt_array_bounds
7561 $$->arrayBounds = $2;
7563 | SETOF SimpleTypename opt_array_bounds
7566 $$->arrayBounds = $3;
7569 /* SQL standard syntax, currently only one-dimensional */
7570 | SimpleTypename ARRAY '[' Iconst ']'
7573 $$->arrayBounds = list_make1(makeInteger($4));
7575 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7578 $$->arrayBounds = list_make1(makeInteger($5));
7581 | SimpleTypename ARRAY
7584 $$->arrayBounds = list_make1(makeInteger(-1));
7586 | SETOF SimpleTypename ARRAY
7589 $$->arrayBounds = list_make1(makeInteger(-1));
7595 opt_array_bounds '[' ']'
7596 { $$ = lappend($1, makeInteger(-1)); }
7597 | opt_array_bounds '[' Iconst ']'
7598 { $$ = lappend($1, makeInteger($3)); }
7604 GenericType { $$ = $1; }
7605 | Numeric { $$ = $1; }
7607 | Character { $$ = $1; }
7608 | ConstDatetime { $$ = $1; }
7609 | ConstInterval opt_interval
7614 | ConstInterval '(' Iconst ')' opt_interval
7619 if (list_length($5) != 1)
7621 (errcode(ERRCODE_SYNTAX_ERROR),
7622 errmsg("interval precision specified twice"),
7623 scanner_errposition(@1)));
7624 $$->typmods = lappend($5, makeIntConst($3, @3));
7627 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7628 makeIntConst($3, @3));
7632 /* We have a separate ConstTypename to allow defaulting fixed-length
7633 * types such as CHAR() and BIT() to an unspecified length.
7634 * SQL9x requires that these default to a length of one, but this
7635 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7636 * where there is an obvious better choice to make.
7637 * Note that ConstInterval is not included here since it must
7638 * be pushed up higher in the rules to accomodate the postfix
7639 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7640 * the generic-type-name case in AExprConst to avoid premature
7641 * reduce/reduce conflicts against function names.
7644 Numeric { $$ = $1; }
7645 | ConstBit { $$ = $1; }
7646 | ConstCharacter { $$ = $1; }
7647 | ConstDatetime { $$ = $1; }
7651 * GenericType covers all type names that don't have special syntax mandated
7652 * by the standard, including qualified names. We also allow type modifiers.
7653 * To avoid parsing conflicts against function invocations, the modifiers
7654 * have to be shown as expr_list here, but parse analysis will only accept
7655 * constants for them.
7658 type_function_name opt_type_modifiers
7660 $$ = makeTypeName($1);
7664 | type_function_name attrs opt_type_modifiers
7666 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7672 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7673 | /* EMPTY */ { $$ = NIL; }
7677 * SQL92 numeric data types
7681 $$ = SystemTypeName("int4");
7686 $$ = SystemTypeName("int4");
7691 $$ = SystemTypeName("int2");
7696 $$ = SystemTypeName("int8");
7701 $$ = SystemTypeName("float4");
7709 | DOUBLE_P PRECISION
7711 $$ = SystemTypeName("float8");
7714 | DECIMAL_P opt_type_modifiers
7716 $$ = SystemTypeName("numeric");
7720 | DEC opt_type_modifiers
7722 $$ = SystemTypeName("numeric");
7726 | NUMERIC opt_type_modifiers
7728 $$ = SystemTypeName("numeric");
7734 $$ = SystemTypeName("bool");
7739 opt_float: '(' Iconst ')'
7742 * Check FLOAT() precision limits assuming IEEE floating
7743 * types - thomas 1997-09-18
7747 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7748 errmsg("precision for type float must be at least 1 bit"),
7749 scanner_errposition(@2)));
7751 $$ = SystemTypeName("float4");
7753 $$ = SystemTypeName("float8");
7756 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7757 errmsg("precision for type float must be less than 54 bits"),
7758 scanner_errposition(@2)));
7762 $$ = SystemTypeName("float8");
7767 * SQL92 bit-field data types
7768 * The following implements BIT() and BIT VARYING().
7780 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7781 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7782 ConstBit: BitWithLength
7794 BIT opt_varying '(' expr_list ')'
7798 typname = $2 ? "varbit" : "bit";
7799 $$ = SystemTypeName(typname);
7808 /* bit defaults to bit(1), varbit to no limit */
7811 $$ = SystemTypeName("varbit");
7815 $$ = SystemTypeName("bit");
7816 $$->typmods = list_make1(makeIntConst(1, -1));
7824 * SQL92 character data types
7825 * The following implements CHAR() and VARCHAR().
7827 Character: CharacterWithLength
7831 | CharacterWithoutLength
7837 ConstCharacter: CharacterWithLength
7841 | CharacterWithoutLength
7843 /* Length was not specified so allow to be unrestricted.
7844 * This handles problems with fixed-length (bpchar) strings
7845 * which in column definitions must default to a length
7846 * of one, but should not be constrained if the length
7847 * was not specified.
7854 CharacterWithLength: character '(' Iconst ')' opt_charset
7856 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7860 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7867 $$ = SystemTypeName($1);
7868 $$->typmods = list_make1(makeIntConst($3, @3));
7873 CharacterWithoutLength: character opt_charset
7875 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7879 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7886 $$ = SystemTypeName($1);
7888 /* char defaults to char(1), varchar to no limit */
7889 if (strcmp($1, "bpchar") == 0)
7890 $$->typmods = list_make1(makeIntConst(1, -1));
7896 character: CHARACTER opt_varying
7897 { $$ = $2 ? "varchar": "bpchar"; }
7898 | CHAR_P opt_varying
7899 { $$ = $2 ? "varchar": "bpchar"; }
7902 | NATIONAL CHARACTER opt_varying
7903 { $$ = $3 ? "varchar": "bpchar"; }
7904 | NATIONAL CHAR_P opt_varying
7905 { $$ = $3 ? "varchar": "bpchar"; }
7907 { $$ = $2 ? "varchar": "bpchar"; }
7911 VARYING { $$ = TRUE; }
7912 | /*EMPTY*/ { $$ = FALSE; }
7916 CHARACTER SET ColId { $$ = $3; }
7917 | /*EMPTY*/ { $$ = NULL; }
7921 * SQL92 date/time types
7924 TIMESTAMP '(' Iconst ')' opt_timezone
7927 $$ = SystemTypeName("timestamptz");
7929 $$ = SystemTypeName("timestamp");
7930 $$->typmods = list_make1(makeIntConst($3, @3));
7933 | TIMESTAMP opt_timezone
7936 $$ = SystemTypeName("timestamptz");
7938 $$ = SystemTypeName("timestamp");
7941 | TIME '(' Iconst ')' opt_timezone
7944 $$ = SystemTypeName("timetz");
7946 $$ = SystemTypeName("time");
7947 $$->typmods = list_make1(makeIntConst($3, @3));
7953 $$ = SystemTypeName("timetz");
7955 $$ = SystemTypeName("time");
7963 $$ = SystemTypeName("interval");
7969 WITH_TIME ZONE { $$ = TRUE; }
7970 | WITHOUT TIME ZONE { $$ = FALSE; }
7971 | /*EMPTY*/ { $$ = FALSE; }
7976 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
7978 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
7980 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
7982 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
7984 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
7989 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
7990 INTERVAL_MASK(MONTH), @1));
7994 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7995 INTERVAL_MASK(HOUR), @1));
7999 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8000 INTERVAL_MASK(HOUR) |
8001 INTERVAL_MASK(MINUTE), @1));
8003 | DAY_P TO interval_second
8006 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
8007 INTERVAL_MASK(HOUR) |
8008 INTERVAL_MASK(MINUTE) |
8009 INTERVAL_MASK(SECOND), @1);
8011 | HOUR_P TO MINUTE_P
8013 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
8014 INTERVAL_MASK(MINUTE), @1));
8016 | HOUR_P TO interval_second
8019 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
8020 INTERVAL_MASK(MINUTE) |
8021 INTERVAL_MASK(SECOND), @1);
8023 | MINUTE_P TO interval_second
8026 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
8027 INTERVAL_MASK(SECOND), @1);
8036 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
8038 | SECOND_P '(' Iconst ')'
8040 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
8041 makeIntConst($3, @3));
8046 /*****************************************************************************
8048 * expression grammar
8050 *****************************************************************************/
8053 * General expressions
8054 * This is the heart of the expression syntax.
8056 * We have two expression types: a_expr is the unrestricted kind, and
8057 * b_expr is a subset that must be used in some places to avoid shift/reduce
8058 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
8059 * because that use of AND conflicts with AND as a boolean operator. So,
8060 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
8062 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
8063 * always be used by surrounding it with parens.
8065 * c_expr is all the productions that are common to a_expr and b_expr;
8066 * it's factored out just to eliminate redundant coding.
8068 a_expr: c_expr { $$ = $1; }
8069 | a_expr TYPECAST Typename
8070 { $$ = makeTypeCast($1, $3, @2); }
8071 | a_expr AT TIME ZONE a_expr
8073 FuncCall *n = makeNode(FuncCall);
8074 n->funcname = SystemFuncName("timezone");
8075 n->args = list_make2($5, $1);
8076 n->agg_star = FALSE;
8077 n->agg_distinct = FALSE;
8078 n->func_variadic = FALSE;
8083 * These operators must be called out explicitly in order to make use
8084 * of yacc/bison's automatic operator-precedence handling. All other
8085 * operator names are handled by the generic productions using "Op",
8086 * below; and all those operators will have the same precedence.
8088 * If you add more explicitly-known operators, be sure to add them
8089 * also to b_expr and to the MathOp list above.
8091 | '+' a_expr %prec UMINUS
8092 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8093 | '-' a_expr %prec UMINUS
8094 { $$ = doNegate($2, @1); }
8096 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8098 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8100 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8102 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8104 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8106 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8108 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8110 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8112 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8114 | a_expr qual_Op a_expr %prec Op
8115 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8116 | qual_Op a_expr %prec Op
8117 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8118 | a_expr qual_Op %prec POSTFIXOP
8119 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8122 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
8124 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
8126 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
8128 | a_expr LIKE a_expr
8129 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
8130 | a_expr LIKE a_expr ESCAPE a_expr
8132 FuncCall *n = makeNode(FuncCall);
8133 n->funcname = SystemFuncName("like_escape");
8134 n->args = list_make2($3, $5);
8135 n->agg_star = FALSE;
8136 n->agg_distinct = FALSE;
8137 n->func_variadic = FALSE;
8139 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
8141 | a_expr NOT LIKE a_expr
8142 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
8143 | a_expr NOT LIKE a_expr ESCAPE a_expr
8145 FuncCall *n = makeNode(FuncCall);
8146 n->funcname = SystemFuncName("like_escape");
8147 n->args = list_make2($4, $6);
8148 n->agg_star = FALSE;
8149 n->agg_distinct = FALSE;
8150 n->func_variadic = FALSE;
8152 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
8154 | a_expr ILIKE a_expr
8155 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
8156 | a_expr ILIKE a_expr ESCAPE a_expr
8158 FuncCall *n = makeNode(FuncCall);
8159 n->funcname = SystemFuncName("like_escape");
8160 n->args = list_make2($3, $5);
8161 n->agg_star = FALSE;
8162 n->agg_distinct = FALSE;
8163 n->func_variadic = FALSE;
8165 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
8167 | a_expr NOT ILIKE a_expr
8168 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
8169 | a_expr NOT ILIKE a_expr ESCAPE a_expr
8171 FuncCall *n = makeNode(FuncCall);
8172 n->funcname = SystemFuncName("like_escape");
8173 n->args = list_make2($4, $6);
8174 n->agg_star = FALSE;
8175 n->agg_distinct = FALSE;
8176 n->func_variadic = FALSE;
8178 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
8181 | a_expr SIMILAR TO a_expr %prec SIMILAR
8183 FuncCall *n = makeNode(FuncCall);
8184 n->funcname = SystemFuncName("similar_escape");
8185 n->args = list_make2($4, makeNullAConst(-1));
8186 n->agg_star = FALSE;
8187 n->agg_distinct = FALSE;
8188 n->func_variadic = FALSE;
8190 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8192 | a_expr SIMILAR TO a_expr ESCAPE a_expr
8194 FuncCall *n = makeNode(FuncCall);
8195 n->funcname = SystemFuncName("similar_escape");
8196 n->args = list_make2($4, $6);
8197 n->agg_star = FALSE;
8198 n->agg_distinct = FALSE;
8199 n->func_variadic = FALSE;
8201 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8203 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
8205 FuncCall *n = makeNode(FuncCall);
8206 n->funcname = SystemFuncName("similar_escape");
8207 n->args = list_make2($5, makeNullAConst(-1));
8208 n->agg_star = FALSE;
8209 n->agg_distinct = FALSE;
8210 n->func_variadic = FALSE;
8212 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8214 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
8216 FuncCall *n = makeNode(FuncCall);
8217 n->funcname = SystemFuncName("similar_escape");
8218 n->args = list_make2($5, $7);
8219 n->agg_star = FALSE;
8220 n->agg_distinct = FALSE;
8221 n->func_variadic = FALSE;
8223 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8227 * Define SQL92-style Null test clause.
8228 * Allow two forms described in the standard:
8231 * Allow two SQL extensions
8237 NullTest *n = makeNode(NullTest);
8238 n->arg = (Expr *) $1;
8239 n->nulltesttype = IS_NULL;
8244 NullTest *n = makeNode(NullTest);
8245 n->arg = (Expr *) $1;
8246 n->nulltesttype = IS_NULL;
8249 | a_expr IS NOT NULL_P
8251 NullTest *n = makeNode(NullTest);
8252 n->arg = (Expr *) $1;
8253 n->nulltesttype = IS_NOT_NULL;
8258 NullTest *n = makeNode(NullTest);
8259 n->arg = (Expr *) $1;
8260 n->nulltesttype = IS_NOT_NULL;
8265 $$ = (Node *)makeOverlaps($1, $3, @2);
8269 BooleanTest *b = makeNode(BooleanTest);
8270 b->arg = (Expr *) $1;
8271 b->booltesttype = IS_TRUE;
8274 | a_expr IS NOT TRUE_P
8276 BooleanTest *b = makeNode(BooleanTest);
8277 b->arg = (Expr *) $1;
8278 b->booltesttype = IS_NOT_TRUE;
8283 BooleanTest *b = makeNode(BooleanTest);
8284 b->arg = (Expr *) $1;
8285 b->booltesttype = IS_FALSE;
8288 | a_expr IS NOT FALSE_P
8290 BooleanTest *b = makeNode(BooleanTest);
8291 b->arg = (Expr *) $1;
8292 b->booltesttype = IS_NOT_FALSE;
8297 BooleanTest *b = makeNode(BooleanTest);
8298 b->arg = (Expr *) $1;
8299 b->booltesttype = IS_UNKNOWN;
8302 | a_expr IS NOT UNKNOWN
8304 BooleanTest *b = makeNode(BooleanTest);
8305 b->arg = (Expr *) $1;
8306 b->booltesttype = IS_NOT_UNKNOWN;
8309 | a_expr IS DISTINCT FROM a_expr %prec IS
8311 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8313 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
8315 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8316 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
8321 | a_expr IS OF '(' type_list ')' %prec IS
8323 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8325 | a_expr IS NOT OF '(' type_list ')' %prec IS
8327 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8329 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8331 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8332 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8333 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8336 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8338 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8339 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8340 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8343 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8345 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8346 (Node *) makeA_Expr(AEXPR_AND, NIL,
8347 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8348 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8350 (Node *) makeA_Expr(AEXPR_AND, NIL,
8351 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
8352 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
8356 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8358 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8359 (Node *) makeA_Expr(AEXPR_OR, NIL,
8360 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8361 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8363 (Node *) makeA_Expr(AEXPR_OR, NIL,
8364 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
8365 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
8369 | a_expr IN_P in_expr
8371 /* in_expr returns a SubLink or a list of a_exprs */
8372 if (IsA($3, SubLink))
8374 /* generate foo = ANY (subquery) */
8375 SubLink *n = (SubLink *) $3;
8376 n->subLinkType = ANY_SUBLINK;
8378 n->operName = list_make1(makeString("="));
8384 /* generate scalar IN expression */
8385 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
8388 | a_expr NOT IN_P in_expr
8390 /* in_expr returns a SubLink or a list of a_exprs */
8391 if (IsA($4, SubLink))
8393 /* generate NOT (foo = ANY (subquery)) */
8394 /* Make an = ANY node */
8395 SubLink *n = (SubLink *) $4;
8396 n->subLinkType = ANY_SUBLINK;
8398 n->operName = list_make1(makeString("="));
8400 /* Stick a NOT on top */
8401 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
8405 /* generate scalar NOT IN expression */
8406 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
8409 | a_expr subquery_Op sub_type select_with_parens %prec Op
8411 SubLink *n = makeNode(SubLink);
8412 n->subLinkType = $3;
8419 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
8421 if ($3 == ANY_SUBLINK)
8422 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
8424 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
8426 | UNIQUE select_with_parens
8428 /* Not sure how to get rid of the parentheses
8429 * but there are lots of shift/reduce errors without them.
8431 * Should be able to implement this by plopping the entire
8432 * select into a node, then transforming the target expressions
8433 * from whatever they are into count(*), and testing the
8434 * entire result equal to one.
8435 * But, will probably implement a separate node in the executor.
8438 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8439 errmsg("UNIQUE predicate is not yet implemented"),
8440 scanner_errposition(@1)));
8442 | a_expr IS DOCUMENT_P %prec IS
8444 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8445 list_make1($1), @2);
8447 | a_expr IS NOT DOCUMENT_P %prec IS
8449 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8450 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8451 list_make1($1), @2),
8457 * Restricted expressions
8459 * b_expr is a subset of the complete expression syntax defined by a_expr.
8461 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8462 * cause trouble in the places where b_expr is used. For simplicity, we
8463 * just eliminate all the boolean-keyword-operator productions from b_expr.
8467 | b_expr TYPECAST Typename
8468 { $$ = makeTypeCast($1, $3, @2); }
8469 | '+' b_expr %prec UMINUS
8470 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8471 | '-' b_expr %prec UMINUS
8472 { $$ = doNegate($2, @1); }
8474 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8476 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8478 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8480 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8482 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8484 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8486 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8488 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8490 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8491 | b_expr qual_Op b_expr %prec Op
8492 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8493 | qual_Op b_expr %prec Op
8494 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8495 | b_expr qual_Op %prec POSTFIXOP
8496 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8497 | b_expr IS DISTINCT FROM b_expr %prec IS
8499 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8501 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8503 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8504 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8506 | b_expr IS OF '(' type_list ')' %prec IS
8508 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8510 | b_expr IS NOT OF '(' type_list ')' %prec IS
8512 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8514 | b_expr IS DOCUMENT_P %prec IS
8516 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8517 list_make1($1), @2);
8519 | b_expr IS NOT DOCUMENT_P %prec IS
8521 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8522 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8523 list_make1($1), @2),
8529 * Productions that can be used in both a_expr and b_expr.
8531 * Note: productions that refer recursively to a_expr or b_expr mostly
8532 * cannot appear here. However, it's OK to refer to a_exprs that occur
8533 * inside parentheses, such as function arguments; that cannot introduce
8534 * ambiguity to the b_expr syntax.
8536 c_expr: columnref { $$ = $1; }
8537 | AexprConst { $$ = $1; }
8538 | PARAM opt_indirection
8540 ParamRef *p = makeNode(ParamRef);
8545 A_Indirection *n = makeNode(A_Indirection);
8546 n->arg = (Node *) p;
8547 n->indirection = check_indirection($2);
8553 | '(' a_expr ')' opt_indirection
8557 A_Indirection *n = makeNode(A_Indirection);
8559 n->indirection = check_indirection($4);
8569 | select_with_parens %prec UMINUS
8571 SubLink *n = makeNode(SubLink);
8572 n->subLinkType = EXPR_SUBLINK;
8579 | EXISTS select_with_parens
8581 SubLink *n = makeNode(SubLink);
8582 n->subLinkType = EXISTS_SUBLINK;
8589 | ARRAY select_with_parens
8591 SubLink *n = makeNode(SubLink);
8592 n->subLinkType = ARRAY_SUBLINK;
8601 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8602 Assert(IsA(n, A_ArrayExpr));
8603 /* point outermost A_ArrayExpr to the ARRAY keyword */
8609 RowExpr *r = makeNode(RowExpr);
8611 r->row_typeid = InvalidOid; /* not analyzed yet */
8618 * func_expr is split out from c_expr just so that we have a classification
8619 * for "everything that is a function call or looks like one". This isn't
8620 * very important, but it saves us having to document which variants are
8621 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8622 * (Note that many of the special SQL functions wouldn't actually make any
8623 * sense as functional index entries, but we ignore that consideration here.)
8625 func_expr: func_name '(' ')'
8627 FuncCall *n = makeNode(FuncCall);
8630 n->agg_star = FALSE;
8631 n->agg_distinct = FALSE;
8632 n->func_variadic = FALSE;
8636 | func_name '(' expr_list ')'
8638 FuncCall *n = makeNode(FuncCall);
8641 n->agg_star = FALSE;
8642 n->agg_distinct = FALSE;
8643 n->func_variadic = FALSE;
8647 | func_name '(' VARIADIC a_expr ')'
8649 FuncCall *n = makeNode(FuncCall);
8651 n->args = list_make1($4);
8652 n->agg_star = FALSE;
8653 n->agg_distinct = FALSE;
8654 n->func_variadic = TRUE;
8658 | func_name '(' expr_list ',' VARIADIC a_expr ')'
8660 FuncCall *n = makeNode(FuncCall);
8662 n->args = lappend($3, $6);
8663 n->agg_star = FALSE;
8664 n->agg_distinct = FALSE;
8665 n->func_variadic = TRUE;
8669 | func_name '(' ALL expr_list ')'
8671 FuncCall *n = makeNode(FuncCall);
8674 n->agg_star = FALSE;
8675 n->agg_distinct = FALSE;
8676 /* Ideally we'd mark the FuncCall node to indicate
8677 * "must be an aggregate", but there's no provision
8678 * for that in FuncCall at the moment.
8680 n->func_variadic = FALSE;
8684 | func_name '(' DISTINCT expr_list ')'
8686 FuncCall *n = makeNode(FuncCall);
8689 n->agg_star = FALSE;
8690 n->agg_distinct = TRUE;
8691 n->func_variadic = FALSE;
8695 | func_name '(' '*' ')'
8698 * We consider AGGREGATE(*) to invoke a parameterless
8699 * aggregate. This does the right thing for COUNT(*),
8700 * and there are no other aggregates in SQL92 that accept
8703 * The FuncCall node is also marked agg_star = true,
8704 * so that later processing can detect what the argument
8707 FuncCall *n = makeNode(FuncCall);
8711 n->agg_distinct = FALSE;
8712 n->func_variadic = FALSE;
8719 * Translate as "'now'::text::date".
8721 * We cannot use "'now'::date" because coerce_type() will
8722 * immediately reduce that to a constant representing
8723 * today's date. We need to delay the conversion until
8724 * runtime, else the wrong things will happen when
8725 * CURRENT_DATE is used in a column default value or rule.
8727 * This could be simplified if we had a way to generate
8728 * an expression tree representing runtime application
8729 * of type-input conversion functions. (As of PG 7.3
8730 * that is actually possible, but not clear that we want
8734 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8735 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8740 * Translate as "'now'::text::timetz".
8741 * See comments for CURRENT_DATE.
8744 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8745 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8747 | CURRENT_TIME '(' Iconst ')'
8750 * Translate as "'now'::text::timetz(n)".
8751 * See comments for CURRENT_DATE.
8755 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8756 d = SystemTypeName("timetz");
8757 d->typmods = list_make1(makeIntConst($3, @3));
8758 $$ = makeTypeCast(n, d, -1);
8763 * Translate as "now()", since we have a function that
8764 * does exactly what is needed.
8766 FuncCall *n = makeNode(FuncCall);
8767 n->funcname = SystemFuncName("now");
8769 n->agg_star = FALSE;
8770 n->agg_distinct = FALSE;
8771 n->func_variadic = FALSE;
8775 | CURRENT_TIMESTAMP '(' Iconst ')'
8778 * Translate as "'now'::text::timestamptz(n)".
8779 * See comments for CURRENT_DATE.
8783 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8784 d = SystemTypeName("timestamptz");
8785 d->typmods = list_make1(makeIntConst($3, @3));
8786 $$ = makeTypeCast(n, d, -1);
8791 * Translate as "'now'::text::time".
8792 * See comments for CURRENT_DATE.
8795 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8796 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8798 | LOCALTIME '(' Iconst ')'
8801 * Translate as "'now'::text::time(n)".
8802 * See comments for CURRENT_DATE.
8806 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8807 d = SystemTypeName("time");
8808 d->typmods = list_make1(makeIntConst($3, @3));
8809 $$ = makeTypeCast((Node *)n, d, -1);
8814 * Translate as "'now'::text::timestamp".
8815 * See comments for CURRENT_DATE.
8818 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8819 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8821 | LOCALTIMESTAMP '(' Iconst ')'
8824 * Translate as "'now'::text::timestamp(n)".
8825 * See comments for CURRENT_DATE.
8829 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8830 d = SystemTypeName("timestamp");
8831 d->typmods = list_make1(makeIntConst($3, @3));
8832 $$ = makeTypeCast(n, d, -1);
8836 FuncCall *n = makeNode(FuncCall);
8837 n->funcname = SystemFuncName("current_user");
8839 n->agg_star = FALSE;
8840 n->agg_distinct = FALSE;
8841 n->func_variadic = FALSE;
8847 FuncCall *n = makeNode(FuncCall);
8848 n->funcname = SystemFuncName("current_user");
8850 n->agg_star = FALSE;
8851 n->agg_distinct = FALSE;
8852 n->func_variadic = FALSE;
8858 FuncCall *n = makeNode(FuncCall);
8859 n->funcname = SystemFuncName("session_user");
8861 n->agg_star = FALSE;
8862 n->agg_distinct = FALSE;
8863 n->func_variadic = FALSE;
8869 FuncCall *n = makeNode(FuncCall);
8870 n->funcname = SystemFuncName("current_user");
8872 n->agg_star = FALSE;
8873 n->agg_distinct = FALSE;
8874 n->func_variadic = FALSE;
8880 FuncCall *n = makeNode(FuncCall);
8881 n->funcname = SystemFuncName("current_database");
8883 n->agg_star = FALSE;
8884 n->agg_distinct = FALSE;
8885 n->func_variadic = FALSE;
8891 FuncCall *n = makeNode(FuncCall);
8892 n->funcname = SystemFuncName("current_schema");
8894 n->agg_star = FALSE;
8895 n->agg_distinct = FALSE;
8896 n->func_variadic = FALSE;
8900 | CAST '(' a_expr AS Typename ')'
8901 { $$ = makeTypeCast($3, $5, @1); }
8902 | EXTRACT '(' extract_list ')'
8904 FuncCall *n = makeNode(FuncCall);
8905 n->funcname = SystemFuncName("date_part");
8907 n->agg_star = FALSE;
8908 n->agg_distinct = FALSE;
8909 n->func_variadic = FALSE;
8913 | OVERLAY '(' overlay_list ')'
8915 /* overlay(A PLACING B FROM C FOR D) is converted to
8916 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8917 * overlay(A PLACING B FROM C) is converted to
8918 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8920 FuncCall *n = makeNode(FuncCall);
8921 n->funcname = SystemFuncName("overlay");
8923 n->agg_star = FALSE;
8924 n->agg_distinct = FALSE;
8925 n->func_variadic = FALSE;
8929 | POSITION '(' position_list ')'
8931 /* position(A in B) is converted to position(B, A) */
8932 FuncCall *n = makeNode(FuncCall);
8933 n->funcname = SystemFuncName("position");
8935 n->agg_star = FALSE;
8936 n->agg_distinct = FALSE;
8937 n->func_variadic = FALSE;
8941 | SUBSTRING '(' substr_list ')'
8943 /* substring(A from B for C) is converted to
8944 * substring(A, B, C) - thomas 2000-11-28
8946 FuncCall *n = makeNode(FuncCall);
8947 n->funcname = SystemFuncName("substring");
8949 n->agg_star = FALSE;
8950 n->agg_distinct = FALSE;
8951 n->func_variadic = FALSE;
8955 | TREAT '(' a_expr AS Typename ')'
8957 /* TREAT(expr AS target) converts expr of a particular type to target,
8958 * which is defined to be a subtype of the original expression.
8959 * In SQL99, this is intended for use with structured UDTs,
8960 * but let's make this a generally useful form allowing stronger
8961 * coercions than are handled by implicit casting.
8963 FuncCall *n = makeNode(FuncCall);
8964 /* Convert SystemTypeName() to SystemFuncName() even though
8965 * at the moment they result in the same thing.
8967 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8968 n->args = list_make1($3);
8969 n->agg_star = FALSE;
8970 n->agg_distinct = FALSE;
8971 n->func_variadic = FALSE;
8975 | TRIM '(' BOTH trim_list ')'
8977 /* various trim expressions are defined in SQL92
8978 * - thomas 1997-07-19
8980 FuncCall *n = makeNode(FuncCall);
8981 n->funcname = SystemFuncName("btrim");
8983 n->agg_star = FALSE;
8984 n->agg_distinct = FALSE;
8985 n->func_variadic = FALSE;
8989 | TRIM '(' LEADING trim_list ')'
8991 FuncCall *n = makeNode(FuncCall);
8992 n->funcname = SystemFuncName("ltrim");
8994 n->agg_star = FALSE;
8995 n->agg_distinct = FALSE;
8996 n->func_variadic = FALSE;
9000 | TRIM '(' TRAILING trim_list ')'
9002 FuncCall *n = makeNode(FuncCall);
9003 n->funcname = SystemFuncName("rtrim");
9005 n->agg_star = FALSE;
9006 n->agg_distinct = FALSE;
9007 n->func_variadic = FALSE;
9011 | TRIM '(' trim_list ')'
9013 FuncCall *n = makeNode(FuncCall);
9014 n->funcname = SystemFuncName("btrim");
9016 n->agg_star = FALSE;
9017 n->agg_distinct = FALSE;
9018 n->func_variadic = FALSE;
9022 | NULLIF '(' a_expr ',' a_expr ')'
9024 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
9026 | COALESCE '(' expr_list ')'
9028 CoalesceExpr *c = makeNode(CoalesceExpr);
9033 | GREATEST '(' expr_list ')'
9035 MinMaxExpr *v = makeNode(MinMaxExpr);
9037 v->op = IS_GREATEST;
9041 | LEAST '(' expr_list ')'
9043 MinMaxExpr *v = makeNode(MinMaxExpr);
9049 | XMLCONCAT '(' expr_list ')'
9051 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
9053 | XMLELEMENT '(' NAME_P ColLabel ')'
9055 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
9057 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
9059 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
9061 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
9063 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
9065 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
9067 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
9069 | XMLFOREST '(' xml_attribute_list ')'
9071 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
9073 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
9075 XmlExpr *x = (XmlExpr *)
9076 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
9077 list_make2($4, makeBoolAConst($5, -1)),
9082 | XMLPI '(' NAME_P ColLabel ')'
9084 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
9086 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
9088 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
9090 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
9092 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
9093 list_make3($3, $5, $6), @1);
9095 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
9097 XmlSerialize *n = makeNode(XmlSerialize);
9109 xml_root_version: VERSION_P a_expr
9111 | VERSION_P NO VALUE_P
9112 { $$ = makeNullAConst(-1); }
9115 opt_xml_root_standalone: ',' STANDALONE_P YES_P
9116 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
9117 | ',' STANDALONE_P NO
9118 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
9119 | ',' STANDALONE_P NO VALUE_P
9120 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
9122 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
9125 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
9128 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
9129 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
9132 xml_attribute_el: a_expr AS ColLabel
9134 $$ = makeNode(ResTarget);
9136 $$->indirection = NIL;
9137 $$->val = (Node *) $1;
9142 $$ = makeNode(ResTarget);
9144 $$->indirection = NIL;
9145 $$->val = (Node *) $1;
9150 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
9151 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
9154 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
9155 | STRIP_P WHITESPACE_P { $$ = FALSE; }
9156 | /*EMPTY*/ { $$ = FALSE; }
9160 * Supporting nonterminals for expressions.
9163 /* Explicit row production.
9165 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
9166 * without conflicting with the parenthesized a_expr production. Without the
9167 * ROW keyword, there must be more than one a_expr inside the parens.
9169 row: ROW '(' expr_list ')' { $$ = $3; }
9170 | ROW '(' ')' { $$ = NIL; }
9171 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
9174 sub_type: ANY { $$ = ANY_SUBLINK; }
9175 | SOME { $$ = ANY_SUBLINK; }
9176 | ALL { $$ = ALL_SUBLINK; }
9179 all_Op: Op { $$ = $1; }
9180 | MathOp { $$ = $1; }
9183 MathOp: '+' { $$ = "+"; }
9195 { $$ = list_make1(makeString($1)); }
9196 | OPERATOR '(' any_operator ')'
9202 { $$ = list_make1(makeString($1)); }
9203 | OPERATOR '(' any_operator ')'
9209 { $$ = list_make1(makeString($1)); }
9210 | OPERATOR '(' any_operator ')'
9213 { $$ = list_make1(makeString("~~")); }
9215 { $$ = list_make1(makeString("!~~")); }
9217 { $$ = list_make1(makeString("~~*")); }
9219 { $$ = list_make1(makeString("!~~*")); }
9220 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
9221 * the regular expression is preprocessed by a function (similar_escape),
9222 * and the ~ operator for posix regular expressions is used.
9223 * x SIMILAR TO y -> x ~ similar_escape(y)
9224 * this transformation is made on the fly by the parser upwards.
9225 * however the SubLink structure which handles any/some/all stuff
9226 * is not ready for such a thing.
9232 $$ = list_make1($1);
9234 | expr_list ',' a_expr
9236 $$ = lappend($1, $3);
9240 type_list: Typename { $$ = list_make1($1); }
9241 | type_list ',' Typename { $$ = lappend($1, $3); }
9244 array_expr: '[' expr_list ']'
9246 $$ = makeAArrayExpr($2, @1);
9248 | '[' array_expr_list ']'
9250 $$ = makeAArrayExpr($2, @1);
9254 $$ = makeAArrayExpr(NIL, @1);
9258 array_expr_list: array_expr { $$ = list_make1($1); }
9259 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
9264 extract_arg FROM a_expr
9266 $$ = list_make2(makeStringConst($1, @1), $3);
9268 | /*EMPTY*/ { $$ = NIL; }
9271 /* Allow delimited string Sconst in extract_arg as an SQL extension.
9272 * - thomas 2001-04-12
9276 | YEAR_P { $$ = "year"; }
9277 | MONTH_P { $$ = "month"; }
9278 | DAY_P { $$ = "day"; }
9279 | HOUR_P { $$ = "hour"; }
9280 | MINUTE_P { $$ = "minute"; }
9281 | SECOND_P { $$ = "second"; }
9282 | Sconst { $$ = $1; }
9285 /* OVERLAY() arguments
9286 * SQL99 defines the OVERLAY() function:
9287 * o overlay(text placing text from int for int)
9288 * o overlay(text placing text from int)
9291 a_expr overlay_placing substr_from substr_for
9293 $$ = list_make4($1, $2, $3, $4);
9295 | a_expr overlay_placing substr_from
9297 $$ = list_make3($1, $2, $3);
9306 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
9309 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
9310 | /*EMPTY*/ { $$ = NIL; }
9313 /* SUBSTRING() arguments
9314 * SQL9x defines a specific syntax for arguments to SUBSTRING():
9315 * o substring(text from int for int)
9316 * o substring(text from int) get entire string from starting point "int"
9317 * o substring(text for int) get first "int" characters of string
9318 * o substring(text from pattern) get entire string matching pattern
9319 * o substring(text from pattern for escape) same with specified escape char
9320 * We also want to support generic substring functions which accept
9321 * the usual generic list of arguments. So we will accept both styles
9322 * here, and convert the SQL9x style to the generic list for further
9323 * processing. - thomas 2000-11-28
9326 a_expr substr_from substr_for
9328 $$ = list_make3($1, $2, $3);
9330 | a_expr substr_for substr_from
9332 /* not legal per SQL99, but might as well allow it */
9333 $$ = list_make3($1, $3, $2);
9335 | a_expr substr_from
9337 $$ = list_make2($1, $2);
9342 * Since there are no cases where this syntax allows
9343 * a textual FOR value, we forcibly cast the argument
9344 * to int4. The possible matches in pg_proc are
9345 * substring(text,int4) and substring(text,text),
9346 * and we don't want the parser to choose the latter,
9347 * which it is likely to do if the second argument
9348 * is unknown or doesn't have an implicit cast to int4.
9350 $$ = list_make3($1, makeIntConst(1, -1),
9352 SystemTypeName("int4"), -1));
9363 FROM a_expr { $$ = $2; }
9366 substr_for: FOR a_expr { $$ = $2; }
9369 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
9370 | FROM expr_list { $$ = $2; }
9371 | expr_list { $$ = $1; }
9374 in_expr: select_with_parens
9376 SubLink *n = makeNode(SubLink);
9378 /* other fields will be filled later */
9381 | '(' expr_list ')' { $$ = (Node *)$2; }
9385 * Define SQL92-style case clause.
9386 * - Full specification
9387 * CASE WHEN a = b THEN c ... ELSE d END
9388 * - Implicit argument
9389 * CASE a WHEN b THEN c ... ELSE d END
9391 case_expr: CASE case_arg when_clause_list case_default END_P
9393 CaseExpr *c = makeNode(CaseExpr);
9394 c->casetype = InvalidOid; /* not analyzed yet */
9395 c->arg = (Expr *) $2;
9397 c->defresult = (Expr *) $4;
9404 /* There must be at least one */
9405 when_clause { $$ = list_make1($1); }
9406 | when_clause_list when_clause { $$ = lappend($1, $2); }
9410 WHEN a_expr THEN a_expr
9412 CaseWhen *w = makeNode(CaseWhen);
9413 w->expr = (Expr *) $2;
9414 w->result = (Expr *) $4;
9421 ELSE a_expr { $$ = $2; }
9422 | /*EMPTY*/ { $$ = NULL; }
9425 case_arg: a_expr { $$ = $1; }
9426 | /*EMPTY*/ { $$ = NULL; }
9430 * columnref starts with relation_name not ColId, so that OLD and NEW
9431 * references can be accepted. Note that when there are more than two
9432 * dotted names, the first name is not actually a relation name...
9434 columnref: relation_name
9436 $$ = makeColumnRef($1, NIL, @1);
9438 | relation_name indirection
9440 $$ = makeColumnRef($1, $2, @1);
9447 $$ = (Node *) makeString($2);
9451 $$ = (Node *) makeNode(A_Star);
9455 A_Indices *ai = makeNode(A_Indices);
9460 | '[' a_expr ':' a_expr ']'
9462 A_Indices *ai = makeNode(A_Indices);
9470 indirection_el { $$ = list_make1($1); }
9471 | indirection indirection_el { $$ = lappend($1, $2); }
9475 /*EMPTY*/ { $$ = NIL; }
9476 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9479 opt_asymmetric: ASYMMETRIC
9484 * The SQL spec defines "contextually typed value expressions" and
9485 * "contextually typed row value constructors", which for our purposes
9486 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9491 a_expr { $$ = (Node *) $1; }
9494 SetToDefault *n = makeNode(SetToDefault);
9501 ctext_expr { $$ = list_make1($1); }
9502 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9506 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9507 * making VALUES a fully reserved word, which will probably break more apps
9508 * than allowing the noise-word is worth.
9510 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9514 /*****************************************************************************
9516 * target list for SELECT
9518 *****************************************************************************/
9521 target_el { $$ = list_make1($1); }
9522 | target_list ',' target_el { $$ = lappend($1, $3); }
9525 target_el: a_expr AS ColLabel
9527 $$ = makeNode(ResTarget);
9529 $$->indirection = NIL;
9530 $$->val = (Node *)$1;
9534 * We support omitting AS only for column labels that aren't
9535 * any known keyword. There is an ambiguity against postfix
9536 * operators: is "a ! b" an infix expression, or a postfix
9537 * expression and a column label? We prefer to resolve this
9538 * as an infix expression, which we accomplish by assigning
9539 * IDENT a precedence higher than POSTFIXOP.
9543 $$ = makeNode(ResTarget);
9545 $$->indirection = NIL;
9546 $$->val = (Node *)$1;
9551 $$ = makeNode(ResTarget);
9553 $$->indirection = NIL;
9554 $$->val = (Node *)$1;
9559 ColumnRef *n = makeNode(ColumnRef);
9560 n->fields = list_make1(makeNode(A_Star));
9563 $$ = makeNode(ResTarget);
9565 $$->indirection = NIL;
9566 $$->val = (Node *)n;
9572 /*****************************************************************************
9574 * Names and constants
9576 *****************************************************************************/
9579 SpecialRuleRelation { $$ = $1; }
9580 | ColId { $$ = $1; }
9583 qualified_name_list:
9584 qualified_name { $$ = list_make1($1); }
9585 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9589 * The production for a qualified relation name has to exactly match the
9590 * production for a qualified func_name, because in a FROM clause we cannot
9591 * tell which we are parsing until we see what comes after it ('(' for a
9592 * func_name, something else for a relation). Therefore we allow 'indirection'
9593 * which may contain subscripts, and reject that case in the C code.
9598 $$ = makeNode(RangeVar);
9599 $$->catalogname = NULL;
9600 $$->schemaname = NULL;
9604 | relation_name indirection
9606 check_qualified_name($2);
9607 $$ = makeNode(RangeVar);
9608 switch (list_length($2))
9611 $$->catalogname = NULL;
9612 $$->schemaname = $1;
9613 $$->relname = strVal(linitial($2));
9616 $$->catalogname = $1;
9617 $$->schemaname = strVal(linitial($2));
9618 $$->relname = strVal(lsecond($2));
9622 (errcode(ERRCODE_SYNTAX_ERROR),
9623 errmsg("improper qualified name (too many dotted names): %s",
9624 NameListToString(lcons(makeString($1), $2))),
9625 scanner_errposition(@1)));
9633 { $$ = list_make1(makeString($1)); }
9634 | name_list ',' name
9635 { $$ = lappend($1, makeString($3)); }
9639 name: ColId { $$ = $1; };
9647 attr_name: ColLabel { $$ = $1; };
9649 index_name: ColId { $$ = $1; };
9651 file_name: Sconst { $$ = $1; };
9654 * The production for a qualified func_name has to exactly match the
9655 * production for a qualified columnref, because we cannot tell which we
9656 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9657 * anything else for a columnref). Therefore we allow 'indirection' which
9658 * may contain subscripts, and reject that case in the C code. (If we
9659 * ever implement SQL99-like methods, such syntax may actually become legal!)
9661 func_name: type_function_name
9662 { $$ = list_make1(makeString($1)); }
9663 | relation_name indirection
9664 { $$ = check_func_name(lcons(makeString($1), $2)); }
9673 $$ = makeIntConst($1, @1);
9677 $$ = makeFloatConst($1, @1);
9681 $$ = makeStringConst($1, @1);
9685 $$ = makeBitStringConst($1, @1);
9689 /* This is a bit constant per SQL99:
9690 * Without Feature F511, "BIT data type",
9691 * a <general literal> shall not be a
9692 * <bit string literal> or a <hex string literal>.
9694 $$ = makeBitStringConst($1, @1);
9698 /* generic type 'literal' syntax */
9699 TypeName *t = makeTypeNameFromNameList($1);
9701 $$ = makeStringConstCast($2, @2, t);
9703 | func_name '(' expr_list ')' Sconst
9705 /* generic syntax with a type modifier */
9706 TypeName *t = makeTypeNameFromNameList($1);
9709 $$ = makeStringConstCast($5, @5, t);
9711 | ConstTypename Sconst
9713 $$ = makeStringConstCast($2, @2, $1);
9715 | ConstInterval Sconst opt_interval
9719 $$ = makeStringConstCast($2, @2, t);
9721 | ConstInterval '(' Iconst ')' Sconst opt_interval
9726 if (list_length($6) != 1)
9728 (errcode(ERRCODE_SYNTAX_ERROR),
9729 errmsg("interval precision specified twice"),
9730 scanner_errposition(@1)));
9731 t->typmods = lappend($6, makeIntConst($3, @3));
9734 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9735 makeIntConst($3, @3));
9736 $$ = makeStringConstCast($5, @5, t);
9740 $$ = makeBoolAConst(TRUE, @1);
9744 $$ = makeBoolAConst(FALSE, @1);
9748 $$ = makeNullAConst(@1);
9752 Iconst: ICONST { $$ = $1; };
9753 Sconst: SCONST { $$ = $1; };
9754 RoleId: ColId { $$ = $1; };
9756 SignedIconst: Iconst { $$ = $1; }
9757 | '+' Iconst { $$ = + $2; }
9758 | '-' Iconst { $$ = - $2; }
9762 * Name classification hierarchy.
9764 * IDENT is the lexeme returned by the lexer for identifiers that match
9765 * no known keyword. In most cases, we can accept certain keywords as
9766 * names, not only IDENTs. We prefer to accept as many such keywords
9767 * as possible to minimize the impact of "reserved words" on programmers.
9768 * So, we divide names into several possible classes. The classification
9769 * is chosen in part to make keywords acceptable as names wherever possible.
9772 /* Column identifier --- names that can be column, table, etc names.
9774 ColId: IDENT { $$ = $1; }
9775 | unreserved_keyword { $$ = pstrdup($1); }
9776 | col_name_keyword { $$ = pstrdup($1); }
9779 /* Type/function identifier --- names that can be type or function names.
9781 type_function_name: IDENT { $$ = $1; }
9782 | unreserved_keyword { $$ = pstrdup($1); }
9783 | type_func_name_keyword { $$ = pstrdup($1); }
9786 /* Column label --- allowed labels in "AS" clauses.
9787 * This presently includes *all* Postgres keywords.
9789 ColLabel: IDENT { $$ = $1; }
9790 | unreserved_keyword { $$ = pstrdup($1); }
9791 | col_name_keyword { $$ = pstrdup($1); }
9792 | type_func_name_keyword { $$ = pstrdup($1); }
9793 | reserved_keyword { $$ = pstrdup($1); }
9798 * Keyword category lists. Generally, every keyword present in
9799 * the Postgres grammar should appear in exactly one of these lists.
9801 * Put a new keyword into the first list that it can go into without causing
9802 * shift or reduce conflicts. The earlier lists define "less reserved"
9803 * categories of keywords.
9805 * Make sure that each keyword's category in keywords.c matches where
9806 * it is listed here. (Someday we may be able to generate these lists and
9807 * keywords.c's table from a common master list.)
9810 /* "Unreserved" keywords --- available for use as any kind of name.
10056 /* Column identifier --- keywords that can be column, table, etc names.
10058 * Many of these keywords will in fact be recognized as type or function
10059 * names too; but they have special productions for the purpose, and so
10060 * can't be treated as "generic" type or function names.
10062 * The type names appearing here are not usable as function names
10063 * because they can be followed by '(' in typename productions, which
10064 * looks too much like a function call for an LR(1) parser.
10114 /* Type/function identifier --- keywords that can be type or function names.
10116 * Most of these are keywords that are used as operators in expressions;
10117 * in general such keywords can't be column names because they would be
10118 * ambiguous with variables, but they are unambiguous as function identifiers.
10120 * Do not include POSITION, SUBSTRING, etc here since they have explicit
10121 * productions in a_expr to support the goofy SQL9x argument syntax.
10122 * - thomas 2000-11-28
10124 type_func_name_keyword:
10148 /* Reserved keyword --- these keywords are usable only as a ColLabel.
10150 * Keywords appear here if they could not be distinguished from variable,
10151 * type, or function names in some contexts. Don't put things here unless
10176 | CURRENT_TIMESTAMP
10236 SpecialRuleRelation:
10243 (errcode(ERRCODE_SYNTAX_ERROR),
10244 errmsg("OLD used in query that is not in a rule"),
10245 scanner_errposition(@1)));
10253 (errcode(ERRCODE_SYNTAX_ERROR),
10254 errmsg("NEW used in query that is not in a rule"),
10255 scanner_errposition(@1)));
10262 makeColumnRef(char *colname, List *indirection, int location)
10265 * Generate a ColumnRef node, with an A_Indirection node added if there
10266 * is any subscripting in the specified indirection list. However,
10267 * any field selection at the start of the indirection list must be
10268 * transposed into the "fields" part of the ColumnRef node.
10270 ColumnRef *c = makeNode(ColumnRef);
10274 c->location = location;
10275 foreach(l, indirection)
10277 if (IsA(lfirst(l), A_Indices))
10279 A_Indirection *i = makeNode(A_Indirection);
10283 /* easy case - all indirection goes to A_Indirection */
10284 c->fields = list_make1(makeString(colname));
10285 i->indirection = check_indirection(indirection);
10289 /* got to split the list in two */
10290 i->indirection = check_indirection(list_copy_tail(indirection,
10292 indirection = list_truncate(indirection, nfields);
10293 c->fields = lcons(makeString(colname), indirection);
10295 i->arg = (Node *) c;
10298 else if (IsA(lfirst(l), A_Star))
10300 /* We only allow '*' at the end of a ColumnRef */
10301 if (lnext(l) != NULL)
10302 yyerror("improper use of \"*\"");
10306 /* No subscripting, so all indirection gets added to field list */
10307 c->fields = lcons(makeString(colname), indirection);
10312 makeTypeCast(Node *arg, TypeName *typename, int location)
10314 TypeCast *n = makeNode(TypeCast);
10316 n->typename = typename;
10317 n->location = location;
10322 makeStringConst(char *str, int location)
10324 A_Const *n = makeNode(A_Const);
10326 n->val.type = T_String;
10327 n->val.val.str = str;
10328 n->location = location;
10334 makeStringConstCast(char *str, int location, TypeName *typename)
10336 Node *s = makeStringConst(str, location);
10338 return makeTypeCast(s, typename, -1);
10342 makeIntConst(int val, int location)
10344 A_Const *n = makeNode(A_Const);
10346 n->val.type = T_Integer;
10347 n->val.val.ival = val;
10348 n->location = location;
10354 makeFloatConst(char *str, int location)
10356 A_Const *n = makeNode(A_Const);
10358 n->val.type = T_Float;
10359 n->val.val.str = str;
10360 n->location = location;
10366 makeBitStringConst(char *str, int location)
10368 A_Const *n = makeNode(A_Const);
10370 n->val.type = T_BitString;
10371 n->val.val.str = str;
10372 n->location = location;
10378 makeNullAConst(int location)
10380 A_Const *n = makeNode(A_Const);
10382 n->val.type = T_Null;
10383 n->location = location;
10389 makeAConst(Value *v, int location)
10396 n = makeFloatConst(v->val.str, location);
10400 n = makeIntConst(v->val.ival, location);
10405 n = makeStringConst(v->val.str, location);
10412 /* makeBoolAConst()
10413 * Create an A_Const string node and put it inside a boolean cast.
10416 makeBoolAConst(bool state, int location)
10418 A_Const *n = makeNode(A_Const);
10420 n->val.type = T_String;
10421 n->val.val.str = (state ? "t" : "f");
10422 n->location = location;
10424 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
10428 * Create and populate a FuncCall node to support the OVERLAPS operator.
10431 makeOverlaps(List *largs, List *rargs, int location)
10433 FuncCall *n = makeNode(FuncCall);
10435 n->funcname = SystemFuncName("overlaps");
10436 if (list_length(largs) == 1)
10437 largs = lappend(largs, largs);
10438 else if (list_length(largs) != 2)
10440 (errcode(ERRCODE_SYNTAX_ERROR),
10441 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10442 scanner_errposition(location)));
10443 if (list_length(rargs) == 1)
10444 rargs = lappend(rargs, rargs);
10445 else if (list_length(rargs) != 2)
10447 (errcode(ERRCODE_SYNTAX_ERROR),
10448 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10449 scanner_errposition(location)));
10450 n->args = list_concat(largs, rargs);
10451 n->agg_star = FALSE;
10452 n->agg_distinct = FALSE;
10453 n->func_variadic = FALSE;
10454 n->location = location;
10458 /* check_qualified_name --- check the result of qualified_name production
10460 * It's easiest to let the grammar production for qualified_name allow
10461 * subscripts and '*', which we then must reject here.
10464 check_qualified_name(List *names)
10470 if (!IsA(lfirst(i), String))
10471 yyerror("syntax error");
10475 /* check_func_name --- check the result of func_name production
10477 * It's easiest to let the grammar production for func_name allow subscripts
10478 * and '*', which we then must reject here.
10481 check_func_name(List *names)
10487 if (!IsA(lfirst(i), String))
10488 yyerror("syntax error");
10493 /* check_indirection --- check the result of indirection production
10495 * We only allow '*' at the end of the list, but it's hard to enforce that
10496 * in the grammar, so do it here.
10499 check_indirection(List *indirection)
10503 foreach(l, indirection)
10505 if (IsA(lfirst(l), A_Star))
10507 if (lnext(l) != NULL)
10508 yyerror("improper use of \"*\"");
10511 return indirection;
10514 /* extractArgTypes()
10515 * Given a list of FunctionParameter nodes, extract a list of just the
10516 * argument types (TypeNames) for input parameters only. This is what
10517 * is needed to look up an existing function, which is what is wanted by
10518 * the productions that use this call.
10521 extractArgTypes(List *parameters)
10523 List *result = NIL;
10526 foreach(i, parameters)
10528 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10530 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10531 result = lappend(result, p->argType);
10536 /* findLeftmostSelect()
10537 * Find the leftmost component SelectStmt in a set-operation parsetree.
10539 static SelectStmt *
10540 findLeftmostSelect(SelectStmt *node)
10542 while (node && node->op != SETOP_NONE)
10544 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10548 /* insertSelectOptions()
10549 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10551 * This routine is just to avoid duplicating code in SelectStmt productions.
10554 insertSelectOptions(SelectStmt *stmt,
10555 List *sortClause, List *lockingClause,
10556 Node *limitOffset, Node *limitCount,
10557 WithClause *withClause)
10559 Assert(IsA(stmt, SelectStmt));
10562 * Tests here are to reject constructs like
10563 * (SELECT foo ORDER BY bar) ORDER BY baz
10567 if (stmt->sortClause)
10569 (errcode(ERRCODE_SYNTAX_ERROR),
10570 errmsg("multiple ORDER BY clauses not allowed"),
10571 scanner_errposition(exprLocation((Node *) sortClause))));
10572 stmt->sortClause = sortClause;
10574 /* We can handle multiple locking clauses, though */
10575 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10578 if (stmt->limitOffset)
10580 (errcode(ERRCODE_SYNTAX_ERROR),
10581 errmsg("multiple OFFSET clauses not allowed"),
10582 scanner_errposition(exprLocation(limitOffset))));
10583 stmt->limitOffset = limitOffset;
10587 if (stmt->limitCount)
10589 (errcode(ERRCODE_SYNTAX_ERROR),
10590 errmsg("multiple LIMIT clauses not allowed"),
10591 scanner_errposition(exprLocation(limitCount))));
10592 stmt->limitCount = limitCount;
10596 if (stmt->withClause)
10598 (errcode(ERRCODE_SYNTAX_ERROR),
10599 errmsg("multiple WITH clauses not allowed"),
10600 scanner_errposition(exprLocation((Node *) withClause))));
10601 stmt->withClause = withClause;
10606 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10608 SelectStmt *n = makeNode(SelectStmt);
10612 n->larg = (SelectStmt *) larg;
10613 n->rarg = (SelectStmt *) rarg;
10617 /* SystemFuncName()
10618 * Build a properly-qualified reference to a built-in function.
10621 SystemFuncName(char *name)
10623 return list_make2(makeString("pg_catalog"), makeString(name));
10626 /* SystemTypeName()
10627 * Build a properly-qualified reference to a built-in type.
10629 * typmod is defaulted, but may be changed afterwards by caller.
10630 * Likewise for the location.
10633 SystemTypeName(char *name)
10635 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10636 makeString(name)));
10640 * Handle negation of a numeric constant.
10642 * Formerly, we did this here because the optimizer couldn't cope with
10643 * indexquals that looked like "var = -4" --- it wants "var = const"
10644 * and a unary minus operator applied to a constant didn't qualify.
10645 * As of Postgres 7.0, that problem doesn't exist anymore because there
10646 * is a constant-subexpression simplifier in the optimizer. However,
10647 * there's still a good reason for doing this here, which is that we can
10648 * postpone committing to a particular internal representation for simple
10649 * negative constants. It's better to leave "-123.456" in string form
10650 * until we know what the desired type is.
10653 doNegate(Node *n, int location)
10655 if (IsA(n, A_Const))
10657 A_Const *con = (A_Const *)n;
10659 /* report the constant's location as that of the '-' sign */
10660 con->location = location;
10662 if (con->val.type == T_Integer)
10664 con->val.val.ival = -con->val.val.ival;
10667 if (con->val.type == T_Float)
10669 doNegateFloat(&con->val);
10674 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10678 doNegateFloat(Value *v)
10680 char *oldval = v->val.str;
10682 Assert(IsA(v, Float));
10683 if (*oldval == '+')
10685 if (*oldval == '-')
10686 v->val.str = oldval+1; /* just strip the '-' */
10689 char *newval = (char *) palloc(strlen(oldval) + 2);
10692 strcpy(newval+1, oldval);
10693 v->val.str = newval;
10698 makeAArrayExpr(List *elements, int location)
10700 A_ArrayExpr *n = makeNode(A_ArrayExpr);
10702 n->elements = elements;
10703 n->location = location;
10708 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
10711 XmlExpr *x = makeNode(XmlExpr);
10716 * named_args is a list of ResTarget; it'll be split apart into separate
10717 * expression and name lists in transformXmlExpr().
10719 x->named_args = named_args;
10720 x->arg_names = NIL;
10722 /* xmloption, if relevant, must be filled in by caller */
10723 /* type and typmod will be filled in during parse analysis */
10724 x->location = location;
10729 * Initialize to parse one query string
10734 QueryIsRule = FALSE;
10738 * Merge the input and output parameters of a table function.
10741 mergeTableFuncParameters(List *func_args, List *columns)
10745 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
10746 foreach(lc, func_args)
10748 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
10750 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
10752 (errcode(ERRCODE_SYNTAX_ERROR),
10753 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
10756 return list_concat(func_args, columns);
10760 * Determine return type of a TABLE function. A single result column
10761 * returns setof that column's type; otherwise return setof record.
10764 TableFuncTypeName(List *columns)
10768 if (list_length(columns) == 1)
10770 FunctionParameter *p = (FunctionParameter *) linitial(columns);
10772 result = (TypeName *) copyObject(p->argType);
10775 result = SystemTypeName("record");
10777 result->setof = true;
10783 * Must undefine base_yylex before including scan.c, since we want it
10784 * to create the function base_yylex not filtered_base_yylex.