4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 2003-2008, PgPool Global Development Group
10 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
15 * $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.603 2007/09/24 01:29:28 adunstan Exp $
18 * AUTHOR DATE MAJOR EVENT
19 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
20 * Andrew Yu Oct, 1994 lispy code conversion
23 * CAPITALS are used to represent terminal symbols.
24 * non-capitals are used to represent non-terminals.
25 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
26 * to help isolate the non-extensible portions of the parser.
28 * In general, nothing in this file should initiate database accesses
29 * nor depend on changeable state (such as SET variables). If you do
30 * database accesses, your code will fail when we have aborted the
31 * current transaction and are just parsing commands to find the next
32 * ROLLBACK or COMMIT. If you make use of SET variables, then you
33 * will do the wrong thing in multi-query strings like this:
34 * SET SQL_inheritance TO off; SELECT * FROM foo;
35 * because the entire string is parsed by gram.y before the SET gets
36 * executed. Anything that depends on the database or changeable state
37 * should be handled during parse analysis so that it happens at the
38 * right time not the wrong time. The handling of SQL_inheritance is
42 * If you use a list, make sure the datum is a node so that the printing
45 * Sometimes we assign constants to makeStrings. Make sure we don't free
48 *-------------------------------------------------------------------------
50 #include "pool_parser.h"
60 #include "pool_memory.h"
61 #include "gramparse.h"
62 #include "makefuncs.h"
63 #include "pool_string.h"
66 /* Location tracking support --- simpler than bison's default */
67 #define YYLLOC_DEFAULT(Current, Rhs, N) \
70 (Current) = (Rhs)[1]; \
72 (Current) = (Rhs)[0]; \
76 * We track token locations in terms of byte offsets from the start of the
77 * source string, not the column number/line number representation that
78 * bison uses by default. Also, to minimize overhead we track only one
79 * location (usually the first token location) for each construct, not
80 * the beginning and ending locations as bison does by default. It's
81 * therefore sufficient to make YYLTYPE an int.
85 /* for XML data type */
90 XML_STANDALONE_NO_VALUE,
91 XML_STANDALONE_OMITTED
96 * The %name-prefix option below will make bison call base_yylex, but we
97 * really want it to call filtered_base_yylex (see parser.c).
99 #define base_yylex filtered_base_yylex
101 #define ereport(a,b) yyerror("")
103 List *parsetree; /* final parse result is delivered here */
105 static bool QueryIsRule = FALSE;
107 extern TypeName *SystemTypeName(char *name);
108 extern List *SystemFuncName(char *name);
109 extern void yyerror(const char *s);
112 * If you need access to certain yacc-generated variables and find that
113 * they're static by default, uncomment the next line. (this is not a
116 /*#define __YYSCLASS*/
118 static Node *makeColumnRef(char *relname, List *indirection, int location);
119 static Node *makeTypeCast(Node *arg, TypeName *typename);
120 static Node *makeStringConst(char *str, TypeName *typename);
121 static Node *makeIntConst(int val);
122 static Node *makeFloatConst(char *str);
123 static Node *makeAConst(Value *v);
124 static A_Const *makeBoolAConst(bool state);
125 static FuncCall *makeOverlaps(List *largs, List *rargs, int location);
126 static void check_qualified_name(List *names);
127 static List *check_func_name(List *names);
128 static List *extractArgTypes(List *parameters);
129 static SelectStmt *findLeftmostSelect(SelectStmt *node);
130 static void insertSelectOptions(SelectStmt *stmt,
131 List *sortClause, List *lockingClause,
132 Node *limitOffset, Node *limitCount);
133 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
134 static Node *doNegate(Node *n, int location);
135 static void doNegateFloat(Value *v);
136 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args);
137 static DefElem *defWithOids(bool value);
142 %name-prefix="base_yy"
153 DropBehavior dbehavior;
154 OnCommitAction oncommit;
161 FunctionParameter *fun_param;
162 FunctionParameterMode fun_param_mode;
163 FuncWithArgs *funwithargs;
173 PrivTarget *privtarget;
176 VariableSetStmt *vsetstmt;
179 %type <node> stmt schema_stmt
180 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
181 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
182 AlterUserStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
183 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
184 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
185 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
186 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
187 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
188 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
189 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
190 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
191 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
192 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
193 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
194 LockStmt NotifyStmt ExplainableStmt PreparableStmt
195 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
196 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
197 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
198 SelectStmt TransactionStmt TruncateStmt
199 UnlistenStmt UpdateStmt VacuumStmt
200 VariableResetStmt VariableSetStmt VariableShowStmt
201 ViewStmt CheckPointStmt CreateConversionStmt
202 DeallocateStmt PrepareStmt ExecuteStmt
203 DropOwnedStmt ReassignOwnedStmt
204 AlterTSConfigurationStmt AlterTSDictionaryStmt
206 %type <node> select_no_parens select_with_parens select_clause
207 simple_select values_clause
209 %type <node> alter_column_default opclass_item opclass_drop alter_using
210 %type <ival> add_drop opt_asc_desc opt_nulls_order
212 %type <node> alter_table_cmd alter_rel_cmd
213 %type <list> alter_table_cmds alter_rel_cmds
215 %type <dbehavior> opt_drop_behavior
217 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
218 transaction_mode_list
219 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
220 transaction_mode_item
222 %type <ival> opt_lock lock_type cast_context
223 %type <boolean> opt_force opt_or_replace
224 opt_grant_grant_option opt_grant_admin_option
225 opt_nowait opt_if_exists
227 %type <list> OptRoleList
228 %type <defelt> OptRoleElem
230 %type <str> OptSchemaName
231 %type <list> OptSchemaEltList
233 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted
234 %type <str> opt_lancompiler
236 %type <str> TriggerEvents
237 %type <value> TriggerFuncArg
239 %type <str> relation_name copy_file_name
240 database_name access_method_clause access_method attr_name
241 index_name name file_name cluster_index_specification
243 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
244 opt_class opt_validator
246 %type <range> qualified_name OptConstrFromTable
248 %type <str> all_Op MathOp SpecialRuleRelation
250 %type <str> iso_level opt_encoding
252 %type <list> grantee_list
253 %type <str> privilege
254 %type <list> privileges privilege_list
255 %type <privtarget> privilege_target
256 %type <funwithargs> function_with_argtypes
257 %type <list> function_with_argtypes_list
258 %type <chr> TriggerOneEvent
260 %type <list> stmtblock stmtmulti
261 OptTableElementList TableElementList OptInherit definition
262 OptWith opt_distinct opt_definition func_args func_args_list
263 func_as createfunc_opt_list alterfunc_opt_list
264 aggr_args old_aggr_definition old_aggr_list
265 oper_argtypes RuleActionList RuleActionMulti
266 opt_column_list columnList opt_name_list
267 sort_clause opt_sort_clause sortby_list index_params
268 name_list from_clause from_list opt_array_bounds
269 qualified_name_list any_name any_name_list
270 any_operator expr_list attrs
271 target_list insert_column_list set_target_list
272 set_clause_list set_clause multiple_set_clause
273 ctext_expr_list ctext_row def_list indirection opt_indirection
274 group_clause TriggerFuncArgs select_limit
275 opt_select_limit opclass_item_list opclass_drop_list
276 opt_opfamily transaction_mode_list_or_empty
277 TableFuncElementList opt_type_modifiers
279 execute_param_clause using_clause returning_clause
282 %type <range> OptTempTableName
283 %type <into> into_clause create_as_target
285 %type <defelt> createfunc_opt_item common_func_opt_item
286 %type <fun_param> func_arg
287 %type <fun_param_mode> arg_class
288 %type <typnam> func_return func_type
290 %type <boolean> TriggerForType OptTemp
291 %type <oncommit> OnCommitOption
293 %type <node> for_locking_item
294 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
295 %type <list> locked_rels_list
296 %type <boolean> opt_all
298 %type <node> join_outer join_qual
299 %type <jtype> join_type
301 %type <list> extract_list overlay_list position_list
302 %type <list> substr_list trim_list
303 %type <ival> opt_interval
304 %type <node> overlay_placing substr_from substr_for
306 %type <boolean> opt_instead opt_analyze
307 %type <boolean> index_opt_unique opt_verbose opt_full
308 %type <boolean> opt_freeze opt_default opt_recheck
309 %type <defelt> opt_binary opt_oids copy_delimiter
311 %type <boolean> copy_from
313 %type <ival> opt_column event cursor_options opt_hold
314 %type <objtype> reindex_type drop_type comment_type
316 %type <node> fetch_direction select_limit_value select_offset_value
318 %type <list> OptSeqList
319 %type <defelt> OptSeqElem
321 %type <istmt> insert_rest
323 %type <vsetstmt> set_rest SetResetClause
325 %type <node> TableElement ConstraintElem TableFuncElement
326 %type <node> columnDef
327 %type <defelt> def_elem old_aggr_elem
328 %type <node> def_arg columnElem where_clause where_or_current_clause
329 a_expr b_expr c_expr func_expr AexprConst indirection_el
330 columnref in_expr having_clause func_table array_expr
331 %type <list> row type_list array_expr_list
332 %type <node> case_expr case_arg when_clause case_default
333 %type <list> when_clause_list
334 %type <ival> sub_type
335 %type <list> OptCreateAs CreateAsList
336 %type <node> CreateAsElement ctext_expr
337 %type <value> NumericOnly FloatOnly IntegerOnly
338 %type <alias> alias_clause
339 %type <sortby> sortby
340 %type <ielem> index_elem
341 %type <node> table_ref
342 %type <jexpr> joined_table
343 %type <range> relation_expr
344 %type <range> relation_expr_opt_alias
345 %type <target> target_el single_set_clause set_target insert_column_item
347 %type <typnam> Typename SimpleTypename ConstTypename
348 GenericType Numeric opt_float
349 Character ConstCharacter
350 CharacterWithLength CharacterWithoutLength
351 ConstDatetime ConstInterval
352 Bit ConstBit BitWithLength BitWithoutLength
353 %type <str> character
354 %type <str> extract_arg
355 %type <str> opt_charset
356 %type <boolean> opt_varying opt_timezone
358 %type <ival> Iconst SignedIconst
359 %type <str> Sconst comment_text
360 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
361 %type <list> var_list
362 %type <str> ColId ColLabel var_name type_function_name param_name
363 %type <node> var_value zone_value
365 %type <keyword> unreserved_keyword type_func_name_keyword
366 %type <keyword> col_name_keyword reserved_keyword
368 %type <node> TableConstraint TableLikeClause
369 %type <list> TableLikeOptionList
370 %type <ival> TableLikeOption
371 %type <list> ColQualList
372 %type <node> ColConstraint ColConstraintElem ConstraintAttr
373 %type <ival> key_actions key_delete key_match key_update key_action
374 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
377 %type <list> constraints_set_list
378 %type <boolean> constraints_set_mode
379 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
380 %type <list> opt_check_option
382 %type <target> xml_attribute_el
383 %type <list> xml_attribute_list xml_attributes
384 %type <node> xml_root_version opt_xml_root_standalone
385 %type <ival> document_or_content
386 %type <boolean> xml_whitespace_option
390 * If you make any token changes, update the keyword table in
391 * parser/keywords.c and add new keywords to the appropriate one of
392 * the reserved-or-not-so-reserved keyword lists, below; search
393 * this file for "Name classification hierarchy".
396 /* ordinary key words in alphabetical order */
397 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
398 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
399 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
401 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
404 CACHE CALLED CASCADE CASCADED CASE CAST CHAIN CHAR_P
405 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
406 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
407 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
408 CONTENT_P CONVERSION_P COPY COST CREATE CREATEDB
409 CREATEROLE CREATEUSER CROSS CSV CURRENT_P CURRENT_DATE CURRENT_ROLE
410 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
412 DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
413 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
414 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
416 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
417 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
419 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
420 FREEZE FROM FULL FUNCTION
422 GLOBAL GRANT GRANTED GREATEST GROUP_P
424 HANDLER HAVING HEADER_P HOLD HOUR_P
426 IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCLUDING INCREMENT
427 INDEX INDEXES INHERIT INHERITS INITIALLY INNER_P INOUT INPUT_P
428 INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT
429 INTERVAL INTO INVOKER IS ISNULL ISOLATION
435 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
436 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
439 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
441 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
442 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
443 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
445 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
446 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
448 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
449 PRECISION PRESERVE PREPARE PREPARED PRIMARY
450 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
454 READ REAL REASSIGN RECHECK REFERENCES REINDEX RELATIVE_P RELEASE RENAME
455 REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS REVOKE
456 RIGHT ROLE ROLLBACK ROW ROWS RULE
458 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
459 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
460 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
461 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
462 SYMMETRIC SYSID SYSTEM_P
464 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
465 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
466 TRUNCATE TRUSTED TYPE_P
468 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
471 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARYING
472 VERBOSE VERSION_P VIEW VOLATILE
474 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRITE
476 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
477 XMLPI XMLROOT XMLSERIALIZE
483 /* The grammar thinks these are keywords, but they are not in the keywords.c
484 * list and so can never be entered directly. The filter in parser.c
485 * creates these tokens when required.
487 %token NULLS_FIRST NULLS_LAST WITH_CASCADED WITH_LOCAL WITH_CHECK
489 /* Special token types, not actually keywords - see the "lex" file */
490 %token <str> IDENT FCONST SCONST BCONST XCONST Op
491 %token <ival> ICONST PARAM
493 /* precedence: lowest to highest */
494 %nonassoc SET /* see relation_expr_opt_alias */
502 %nonassoc LIKE ILIKE SIMILAR
507 %left POSTFIXOP /* dummy for postfix Op rules */
508 %left Op OPERATOR /* multi-character ops and user-defined operators */
511 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
515 /* Unary Operators */
516 %left AT ZONE /* sets precedence for AT TIME ZONE */
523 * These might seem to be low-precedence, but actually they are not part
524 * of the arithmetic hierarchy at all in their use as JOIN operators.
525 * We make them high-precedence to support their use as function names.
526 * They wouldn't be given a precedence at all, were it not that we need
527 * left-associativity among the JOIN rules themselves.
529 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
530 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
531 %right PRESERVE STRIP_P
535 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
536 * psql already handles such cases, but other interfaces don't.
539 stmtblock: stmtmulti { parsetree = $1; }
542 /* the thrashing around here is to discard "empty" statements... */
543 stmtmulti: stmtmulti ';' stmt
545 $$ = lappend($1, $3);
559 | AlterDatabaseSetStmt
563 | AlterObjectSchemaStmt
569 | AlterTSConfigurationStmt
570 | AlterTSDictionaryStmt
583 | CreateConversionStmt
594 | CreateTableSpaceStmt
653 /*****************************************************************************
655 * Create a new Postgres DBMS role
657 *****************************************************************************/
660 CREATE ROLE RoleId opt_with OptRoleList
662 CreateRoleStmt *n = makeNode(CreateRoleStmt);
663 n->stmt_type = ROLESTMT_ROLE;
676 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
677 * for backwards compatibility). Note: the only option required by SQL99
678 * is "WITH ADMIN name".
681 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
682 | /* EMPTY */ { $$ = NIL; }
688 $$ = makeDefElem("password",
689 (Node *)makeString($2));
693 $$ = makeDefElem("password", NULL);
695 | ENCRYPTED PASSWORD Sconst
697 $$ = makeDefElem("encryptedPassword",
698 (Node *)makeString($3));
700 | UNENCRYPTED PASSWORD Sconst
702 $$ = makeDefElem("unencryptedPassword",
703 (Node *)makeString($3));
707 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
711 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
715 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
719 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
723 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
727 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
731 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
735 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
739 /* For backwards compatibility, synonym for SUPERUSER */
740 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
744 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
748 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
752 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
754 | CONNECTION LIMIT SignedIconst
756 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
760 $$ = makeDefElem("validUntil", (Node *)makeString($3));
762 /* Supported but not documented for roles, for use by ALTER GROUP. */
765 $$ = makeDefElem("rolemembers", (Node *)$2);
767 /* The following are not supported by ALTER ROLE/USER/GROUP */
770 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
774 $$ = makeDefElem("adminmembers", (Node *)$2);
778 $$ = makeDefElem("rolemembers", (Node *)$2);
780 | IN_P ROLE name_list
782 $$ = makeDefElem("addroleto", (Node *)$3);
784 | IN_P GROUP_P name_list
786 $$ = makeDefElem("addroleto", (Node *)$3);
791 /*****************************************************************************
793 * Create a new Postgres DBMS user (role with implied login ability)
795 *****************************************************************************/
798 CREATE USER RoleId opt_with OptRoleList
800 CreateRoleStmt *n = makeNode(CreateRoleStmt);
801 n->stmt_type = ROLESTMT_USER;
809 /*****************************************************************************
811 * Alter a postgresql DBMS role
813 *****************************************************************************/
816 ALTER ROLE RoleId opt_with OptRoleList
818 AlterRoleStmt *n = makeNode(AlterRoleStmt);
820 n->action = +1; /* add, if there are members */
827 ALTER ROLE RoleId SetResetClause
829 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
837 /*****************************************************************************
839 * Alter a postgresql DBMS user
841 *****************************************************************************/
844 ALTER USER RoleId opt_with OptRoleList
846 AlterRoleStmt *n = makeNode(AlterRoleStmt);
848 n->action = +1; /* add, if there are members */
856 ALTER USER RoleId SetResetClause
858 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
866 /*****************************************************************************
868 * Drop a postgresql DBMS role
870 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
871 * might own objects in multiple databases, there is presently no way to
872 * implement either cascading or restricting. Caveat DBA.
873 *****************************************************************************/
878 DropRoleStmt *n = makeNode(DropRoleStmt);
879 n->missing_ok = FALSE;
883 | DROP ROLE IF_P EXISTS name_list
885 DropRoleStmt *n = makeNode(DropRoleStmt);
886 n->missing_ok = TRUE;
892 /*****************************************************************************
894 * Drop a postgresql DBMS user
896 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
897 * might own objects in multiple databases, there is presently no way to
898 * implement either cascading or restricting. Caveat DBA.
899 *****************************************************************************/
904 DropRoleStmt *n = makeNode(DropRoleStmt);
905 n->missing_ok = FALSE;
909 | DROP USER IF_P EXISTS name_list
911 DropRoleStmt *n = makeNode(DropRoleStmt);
913 n->missing_ok = TRUE;
919 /*****************************************************************************
921 * Create a postgresql group (role without login ability)
923 *****************************************************************************/
926 CREATE GROUP_P RoleId opt_with OptRoleList
928 CreateRoleStmt *n = makeNode(CreateRoleStmt);
929 n->stmt_type = ROLESTMT_GROUP;
937 /*****************************************************************************
939 * Alter a postgresql group
941 *****************************************************************************/
944 ALTER GROUP_P RoleId add_drop USER name_list
946 AlterRoleStmt *n = makeNode(AlterRoleStmt);
949 n->options = list_make1(makeDefElem("rolemembers",
955 add_drop: ADD_P { $$ = +1; }
960 /*****************************************************************************
962 * Drop a postgresql group
964 * XXX see above notes about cascading DROP USER; groups have same problem.
965 *****************************************************************************/
968 DROP GROUP_P name_list
970 DropRoleStmt *n = makeNode(DropRoleStmt);
971 n->missing_ok = FALSE;
975 | DROP GROUP_P IF_P EXISTS name_list
977 DropRoleStmt *n = makeNode(DropRoleStmt);
978 n->missing_ok = TRUE;
985 /*****************************************************************************
987 * Manipulate a schema
989 *****************************************************************************/
992 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
994 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
995 /* One can omit the schema name or the authorization id. */
1004 | CREATE SCHEMA ColId OptSchemaEltList
1006 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1007 /* ...but not both */
1017 | /* EMPTY */ { $$ = NULL; }
1021 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1022 | /* EMPTY */ { $$ = NIL; }
1026 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1027 * statement (in addition to by themselves).
1039 /*****************************************************************************
1041 * Set PG internal variable
1042 * SET name TO 'var_value'
1043 * Include SQL92 syntax (thomas 1997-10-22):
1044 * SET TIME ZONE 'var_value'
1046 *****************************************************************************/
1051 VariableSetStmt *n = $2;
1052 n->is_local = false;
1055 | SET LOCAL set_rest
1057 VariableSetStmt *n = $3;
1061 | SET SESSION set_rest
1063 VariableSetStmt *n = $3;
1064 n->is_local = false;
1069 set_rest: /* Generic SET syntaxes: */
1070 var_name TO var_list
1072 VariableSetStmt *n = makeNode(VariableSetStmt);
1073 n->kind = VAR_SET_VALUE;
1078 | var_name '=' var_list
1080 VariableSetStmt *n = makeNode(VariableSetStmt);
1081 n->kind = VAR_SET_VALUE;
1086 | var_name TO DEFAULT
1088 VariableSetStmt *n = makeNode(VariableSetStmt);
1089 n->kind = VAR_SET_DEFAULT;
1093 | var_name '=' DEFAULT
1095 VariableSetStmt *n = makeNode(VariableSetStmt);
1096 n->kind = VAR_SET_DEFAULT;
1100 | var_name FROM CURRENT_P
1102 VariableSetStmt *n = makeNode(VariableSetStmt);
1103 n->kind = VAR_SET_CURRENT;
1107 /* Special syntaxes mandated by SQL standard: */
1108 | TIME ZONE zone_value
1110 VariableSetStmt *n = makeNode(VariableSetStmt);
1111 n->kind = VAR_SET_VALUE;
1112 n->name = "timezone";
1114 n->args = list_make1($3);
1116 n->kind = VAR_SET_DEFAULT;
1119 | TRANSACTION transaction_mode_list
1121 VariableSetStmt *n = makeNode(VariableSetStmt);
1122 n->kind = VAR_SET_MULTI;
1123 n->name = "TRANSACTION";
1127 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1129 VariableSetStmt *n = makeNode(VariableSetStmt);
1130 n->kind = VAR_SET_MULTI;
1131 n->name = "SESSION CHARACTERISTICS";
1135 | NAMES opt_encoding
1137 VariableSetStmt *n = makeNode(VariableSetStmt);
1138 n->kind = VAR_SET_VALUE;
1139 n->name = "client_encoding";
1141 n->args = list_make1(makeStringConst($2, NULL));
1143 n->kind = VAR_SET_DEFAULT;
1146 | ROLE ColId_or_Sconst
1148 VariableSetStmt *n = makeNode(VariableSetStmt);
1149 n->kind = VAR_SET_VALUE;
1151 n->args = list_make1(makeStringConst($2, NULL));
1154 | SESSION AUTHORIZATION ColId_or_Sconst
1156 VariableSetStmt *n = makeNode(VariableSetStmt);
1157 n->kind = VAR_SET_VALUE;
1158 n->name = "session_authorization";
1159 n->args = list_make1(makeStringConst($3, NULL));
1162 | SESSION AUTHORIZATION DEFAULT
1164 VariableSetStmt *n = makeNode(VariableSetStmt);
1165 n->kind = VAR_SET_DEFAULT;
1166 n->name = "session_authorization";
1169 | XML_P OPTION document_or_content
1171 VariableSetStmt *n = makeNode(VariableSetStmt);
1172 n->kind = VAR_SET_VALUE;
1173 n->name = "xmloption";
1174 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", NULL));
1179 var_name: ColId { $$ = $1; }
1180 | var_name '.' ColId
1182 $$ = palloc(strlen($1) + strlen($3) + 2);
1183 sprintf($$, "%s.%s", $1, $3);
1187 var_list: var_value { $$ = list_make1($1); }
1188 | var_list ',' var_value { $$ = lappend($1, $3); }
1191 var_value: opt_boolean
1192 { $$ = makeStringConst($1, NULL); }
1194 { $$ = makeStringConst($1, NULL); }
1196 { $$ = makeAConst($1); }
1199 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1200 | READ COMMITTED { $$ = "read committed"; }
1201 | REPEATABLE READ { $$ = "repeatable read"; }
1202 | SERIALIZABLE { $$ = "serializable"; }
1206 TRUE_P { $$ = "true"; }
1207 | FALSE_P { $$ = "false"; }
1209 | OFF { $$ = "off"; }
1212 /* Timezone values can be:
1213 * - a string such as 'pst8pdt'
1214 * - an identifier such as "pst8pdt"
1215 * - an integer or floating point number
1216 * - a time interval per SQL99
1217 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1218 * so use IDENT and reject anything which is a reserved word.
1223 $$ = makeStringConst($1, NULL);
1227 $$ = makeStringConst($1, NULL);
1229 | ConstInterval Sconst opt_interval
1231 A_Const *n = (A_Const *) makeStringConst($2, $1);
1232 if ($3 != INTERVAL_FULL_RANGE)
1234 if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1236 (errcode(ERRCODE_SYNTAX_ERROR),
1237 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1238 n->typename->typmods = list_make1(makeIntConst($3));
1242 | ConstInterval '(' Iconst ')' Sconst opt_interval
1244 A_Const *n = (A_Const *) makeStringConst($5, $1);
1245 if (($6 != INTERVAL_FULL_RANGE)
1246 && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
1248 (errcode(ERRCODE_SYNTAX_ERROR),
1249 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
1250 n->typename->typmods = list_make2(makeIntConst($6),
1254 | NumericOnly { $$ = makeAConst($1); }
1255 | DEFAULT { $$ = NULL; }
1256 | LOCAL { $$ = NULL; }
1261 | DEFAULT { $$ = NULL; }
1262 | /*EMPTY*/ { $$ = NULL; }
1267 | SCONST { $$ = $1; }
1273 VariableSetStmt *n = makeNode(VariableSetStmt);
1274 n->kind = VAR_RESET;
1280 VariableSetStmt *n = makeNode(VariableSetStmt);
1281 n->kind = VAR_RESET;
1282 n->name = "timezone";
1285 | RESET TRANSACTION ISOLATION LEVEL
1287 VariableSetStmt *n = makeNode(VariableSetStmt);
1288 n->kind = VAR_RESET;
1289 n->name = "transaction_isolation";
1292 | RESET SESSION AUTHORIZATION
1294 VariableSetStmt *n = makeNode(VariableSetStmt);
1295 n->kind = VAR_RESET;
1296 n->name = "session_authorization";
1301 VariableSetStmt *n = makeNode(VariableSetStmt);
1302 n->kind = VAR_RESET_ALL;
1307 /* SetResetClause allows SET or RESET without LOCAL */
1309 SET set_rest { $$ = $2; }
1310 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1317 VariableShowStmt *n = makeNode(VariableShowStmt);
1323 VariableShowStmt *n = makeNode(VariableShowStmt);
1324 n->name = "timezone";
1327 | SHOW TRANSACTION ISOLATION LEVEL
1329 VariableShowStmt *n = makeNode(VariableShowStmt);
1330 n->name = "transaction_isolation";
1333 | SHOW SESSION AUTHORIZATION
1335 VariableShowStmt *n = makeNode(VariableShowStmt);
1336 n->name = "session_authorization";
1341 VariableShowStmt *n = makeNode(VariableShowStmt);
1349 SET CONSTRAINTS constraints_set_list constraints_set_mode
1351 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1352 n->constraints = $3;
1358 constraints_set_list:
1360 | qualified_name_list { $$ = $1; }
1363 constraints_set_mode:
1364 DEFERRED { $$ = TRUE; }
1365 | IMMEDIATE { $$ = FALSE; }
1370 * Checkpoint statement
1375 CheckPointStmt *n = makeNode(CheckPointStmt);
1381 /*****************************************************************************
1383 * DISCARD { ALL | TEMP | PLANS }
1385 *****************************************************************************/
1390 DiscardStmt *n = makeNode(DiscardStmt);
1391 n->target = DISCARD_ALL;
1396 DiscardStmt *n = makeNode(DiscardStmt);
1397 n->target = DISCARD_TEMP;
1402 DiscardStmt *n = makeNode(DiscardStmt);
1403 n->target = DISCARD_TEMP;
1408 DiscardStmt *n = makeNode(DiscardStmt);
1409 n->target = DISCARD_PLANS;
1415 /*****************************************************************************
1417 * ALTER [ TABLE | INDEX ] variations
1419 *****************************************************************************/
1422 ALTER TABLE relation_expr alter_table_cmds
1424 AlterTableStmt *n = makeNode(AlterTableStmt);
1427 n->relkind = OBJECT_TABLE;
1430 | ALTER INDEX relation_expr alter_rel_cmds
1432 AlterTableStmt *n = makeNode(AlterTableStmt);
1435 n->relkind = OBJECT_INDEX;
1441 alter_table_cmd { $$ = list_make1($1); }
1442 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1445 /* Subcommands that are for ALTER TABLE only */
1447 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
1448 ADD_P opt_column columnDef
1450 AlterTableCmd *n = makeNode(AlterTableCmd);
1451 n->subtype = AT_AddColumn;
1455 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1456 | ALTER opt_column ColId alter_column_default
1458 AlterTableCmd *n = makeNode(AlterTableCmd);
1459 n->subtype = AT_ColumnDefault;
1464 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
1465 | ALTER opt_column ColId DROP NOT NULL_P
1467 AlterTableCmd *n = makeNode(AlterTableCmd);
1468 n->subtype = AT_DropNotNull;
1472 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
1473 | ALTER opt_column ColId SET NOT NULL_P
1475 AlterTableCmd *n = makeNode(AlterTableCmd);
1476 n->subtype = AT_SetNotNull;
1480 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1481 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1483 AlterTableCmd *n = makeNode(AlterTableCmd);
1484 n->subtype = AT_SetStatistics;
1486 n->def = (Node *) $6;
1489 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1490 | ALTER opt_column ColId SET STORAGE ColId
1492 AlterTableCmd *n = makeNode(AlterTableCmd);
1493 n->subtype = AT_SetStorage;
1495 n->def = (Node *) makeString($6);
1498 /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1499 | DROP opt_column ColId opt_drop_behavior
1501 AlterTableCmd *n = makeNode(AlterTableCmd);
1502 n->subtype = AT_DropColumn;
1508 * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
1509 * [ USING <expression> ]
1511 | ALTER opt_column ColId TYPE_P Typename alter_using
1513 AlterTableCmd *n = makeNode(AlterTableCmd);
1514 n->subtype = AT_AlterColumnType;
1516 n->def = (Node *) $5;
1520 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1521 | ADD_P TableConstraint
1523 AlterTableCmd *n = makeNode(AlterTableCmd);
1524 n->subtype = AT_AddConstraint;
1528 /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1529 | DROP CONSTRAINT name opt_drop_behavior
1531 AlterTableCmd *n = makeNode(AlterTableCmd);
1532 n->subtype = AT_DropConstraint;
1537 /* ALTER TABLE <relation> SET WITHOUT OIDS */
1540 AlterTableCmd *n = makeNode(AlterTableCmd);
1541 n->subtype = AT_DropOids;
1544 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1547 AlterTableCmd *n = makeNode(AlterTableCmd);
1548 n->subtype = AT_ClusterOn;
1552 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1553 | SET WITHOUT CLUSTER
1555 AlterTableCmd *n = makeNode(AlterTableCmd);
1556 n->subtype = AT_DropCluster;
1560 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1561 | ENABLE_P TRIGGER name
1563 AlterTableCmd *n = makeNode(AlterTableCmd);
1564 n->subtype = AT_EnableTrig;
1568 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1569 | ENABLE_P ALWAYS TRIGGER name
1571 AlterTableCmd *n = makeNode(AlterTableCmd);
1572 n->subtype = AT_EnableAlwaysTrig;
1576 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1577 | ENABLE_P REPLICA TRIGGER name
1579 AlterTableCmd *n = makeNode(AlterTableCmd);
1580 n->subtype = AT_EnableReplicaTrig;
1584 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1585 | ENABLE_P TRIGGER ALL
1587 AlterTableCmd *n = makeNode(AlterTableCmd);
1588 n->subtype = AT_EnableTrigAll;
1591 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1592 | ENABLE_P TRIGGER USER
1594 AlterTableCmd *n = makeNode(AlterTableCmd);
1595 n->subtype = AT_EnableTrigUser;
1598 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1599 | DISABLE_P TRIGGER name
1601 AlterTableCmd *n = makeNode(AlterTableCmd);
1602 n->subtype = AT_DisableTrig;
1606 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1607 | DISABLE_P TRIGGER ALL
1609 AlterTableCmd *n = makeNode(AlterTableCmd);
1610 n->subtype = AT_DisableTrigAll;
1613 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1614 | DISABLE_P TRIGGER USER
1616 AlterTableCmd *n = makeNode(AlterTableCmd);
1617 n->subtype = AT_DisableTrigUser;
1620 /* ALTER TABLE <name> ENABLE RULE <rule> */
1621 | ENABLE_P RULE name
1623 AlterTableCmd *n = makeNode(AlterTableCmd);
1624 n->subtype = AT_EnableRule;
1628 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1629 | ENABLE_P ALWAYS RULE name
1631 AlterTableCmd *n = makeNode(AlterTableCmd);
1632 n->subtype = AT_EnableAlwaysRule;
1636 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1637 | ENABLE_P REPLICA RULE name
1639 AlterTableCmd *n = makeNode(AlterTableCmd);
1640 n->subtype = AT_EnableReplicaRule;
1644 /* ALTER TABLE <name> DISABLE RULE <rule> */
1645 | DISABLE_P RULE name
1647 AlterTableCmd *n = makeNode(AlterTableCmd);
1648 n->subtype = AT_DisableRule;
1652 /* ALTER TABLE <name> INHERIT <parent> */
1653 | INHERIT qualified_name
1655 AlterTableCmd *n = makeNode(AlterTableCmd);
1656 n->subtype = AT_AddInherit;
1657 n->def = (Node *) $2;
1660 /* ALTER TABLE <name> NO INHERIT <parent> */
1661 | NO INHERIT qualified_name
1663 AlterTableCmd *n = makeNode(AlterTableCmd);
1664 n->subtype = AT_DropInherit;
1665 n->def = (Node *) $3;
1675 alter_rel_cmd { $$ = list_make1($1); }
1676 | alter_rel_cmds ',' alter_rel_cmd { $$ = lappend($1, $3); }
1679 /* Subcommands that are for ALTER TABLE or ALTER INDEX */
1681 /* ALTER [TABLE|INDEX] <name> OWNER TO RoleId */
1684 AlterTableCmd *n = makeNode(AlterTableCmd);
1685 n->subtype = AT_ChangeOwner;
1689 /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
1690 | SET TABLESPACE name
1692 AlterTableCmd *n = makeNode(AlterTableCmd);
1693 n->subtype = AT_SetTableSpace;
1697 /* ALTER [TABLE|INDEX] <name> SET (...) */
1700 AlterTableCmd *n = makeNode(AlterTableCmd);
1701 n->subtype = AT_SetRelOptions;
1702 n->def = (Node *)$2;
1705 /* ALTER [TABLE|INDEX] <name> RESET (...) */
1708 AlterTableCmd *n = makeNode(AlterTableCmd);
1709 n->subtype = AT_ResetRelOptions;
1710 n->def = (Node *)$2;
1715 alter_column_default:
1718 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1719 if (exprIsNullConstant($3))
1724 | DROP DEFAULT { $$ = NULL; }
1728 CASCADE { $$ = DROP_CASCADE; }
1729 | RESTRICT { $$ = DROP_RESTRICT; }
1730 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1734 USING a_expr { $$ = $2; }
1735 | /* EMPTY */ { $$ = NULL; }
1740 /*****************************************************************************
1743 * close <portalname>
1745 *****************************************************************************/
1750 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1756 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1757 n->portalname = NULL;
1763 /*****************************************************************************
1766 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1768 * BINARY, OIDS, and DELIMITERS kept in old locations
1769 * for backward compatibility. 2002-06-18
1771 * COPY ( SELECT ... ) TO file [WITH options]
1772 * This form doesn't have the backwards-compatible option
1775 *****************************************************************************/
1777 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1778 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1780 CopyStmt *n = makeNode(CopyStmt);
1788 /* Concatenate user-supplied flags */
1790 n->options = lappend(n->options, $2);
1792 n->options = lappend(n->options, $5);
1794 n->options = lappend(n->options, $8);
1796 n->options = list_concat(n->options, $10);
1799 | COPY select_with_parens TO copy_file_name opt_with
1802 CopyStmt *n = makeNode(CopyStmt);
1815 | TO { $$ = FALSE; }
1819 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1820 * used depends on the direction. (It really doesn't make sense to copy from
1821 * stdout. We silently correct the "typo".) - AY 9/94
1825 | STDIN { $$ = NULL; }
1826 | STDOUT { $$ = NULL; }
1832 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1833 | /* EMPTY */ { $$ = NIL; }
1840 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1844 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1846 | DELIMITER opt_as Sconst
1848 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1850 | NULL_P opt_as Sconst
1852 $$ = makeDefElem("null", (Node *)makeString($3));
1856 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1860 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1862 | QUOTE opt_as Sconst
1864 $$ = makeDefElem("quote", (Node *)makeString($3));
1866 | ESCAPE opt_as Sconst
1868 $$ = makeDefElem("escape", (Node *)makeString($3));
1870 | FORCE QUOTE columnList
1872 $$ = makeDefElem("force_quote", (Node *)$3);
1874 | FORCE NOT NULL_P columnList
1876 $$ = makeDefElem("force_notnull", (Node *)$4);
1880 /* The following exist for backward compatibility */
1885 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1887 | /*EMPTY*/ { $$ = NULL; }
1893 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1895 | /*EMPTY*/ { $$ = NULL; }
1899 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1900 opt_using DELIMITERS Sconst
1902 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1904 | /*EMPTY*/ { $$ = NULL; }
1913 /*****************************************************************************
1916 * CREATE TABLE relname
1918 *****************************************************************************/
1920 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1921 OptInherit OptWith OnCommitOption OptTableSpace
1923 CreateStmt *n = makeNode(CreateStmt);
1927 n->inhRelations = $8;
1928 n->constraints = NIL;
1931 n->tablespacename = $11;
1934 | CREATE OptTemp TABLE qualified_name OF qualified_name
1935 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1937 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1938 * by our inheritance capabilities. Let's try it...
1940 CreateStmt *n = makeNode(CreateStmt);
1944 n->inhRelations = list_make1($6);
1945 n->constraints = NIL;
1948 n->tablespacename = $12;
1954 * Redundancy here is needed to avoid shift/reduce conflicts,
1955 * since TEMP is not a reserved word. See also OptTempTableName.
1957 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1958 * the LOCAL keyword is really meaningless.
1960 OptTemp: TEMPORARY { $$ = TRUE; }
1961 | TEMP { $$ = TRUE; }
1962 | LOCAL TEMPORARY { $$ = TRUE; }
1963 | LOCAL TEMP { $$ = TRUE; }
1964 | GLOBAL TEMPORARY { $$ = TRUE; }
1965 | GLOBAL TEMP { $$ = TRUE; }
1966 | /*EMPTY*/ { $$ = FALSE; }
1969 OptTableElementList:
1970 TableElementList { $$ = $1; }
1971 | /*EMPTY*/ { $$ = NIL; }
1977 $$ = list_make1($1);
1979 | TableElementList ',' TableElement
1981 $$ = lappend($1, $3);
1986 columnDef { $$ = $1; }
1987 | TableLikeClause { $$ = $1; }
1988 | TableConstraint { $$ = $1; }
1991 columnDef: ColId Typename ColQualList
1993 ColumnDef *n = makeNode(ColumnDef);
1996 n->constraints = $3;
2003 ColQualList ColConstraint { $$ = lappend($1, $2); }
2004 | /*EMPTY*/ { $$ = NIL; }
2008 CONSTRAINT name ColConstraintElem
2010 switch (nodeTag($3))
2014 Constraint *n = (Constraint *)$3;
2018 case T_FkConstraint:
2020 FkConstraint *n = (FkConstraint *)$3;
2021 n->constr_name = $2;
2029 | ColConstraintElem { $$ = $1; }
2030 | ConstraintAttr { $$ = $1; }
2033 /* DEFAULT NULL is already the default for Postgres.
2034 * But define it here and carry it forward into the system
2035 * to make it explicit.
2036 * - thomas 1998-09-13
2038 * WITH NULL and NULL are not SQL92-standard syntax elements,
2039 * so leave them out. Use DEFAULT NULL to explicitly indicate
2040 * that a column may have that value. WITH NULL leads to
2041 * shift/reduce conflicts with WITH TIME ZONE anyway.
2042 * - thomas 1999-01-08
2044 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2045 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2046 * or be part of a_expr NOT LIKE or similar constructs).
2051 Constraint *n = makeNode(Constraint);
2052 n->contype = CONSTR_NOTNULL;
2055 n->cooked_expr = NULL;
2057 n->indexspace = NULL;
2062 Constraint *n = makeNode(Constraint);
2063 n->contype = CONSTR_NULL;
2066 n->cooked_expr = NULL;
2068 n->indexspace = NULL;
2071 | UNIQUE opt_definition OptConsTableSpace
2073 Constraint *n = makeNode(Constraint);
2074 n->contype = CONSTR_UNIQUE;
2077 n->cooked_expr = NULL;
2083 | PRIMARY KEY opt_definition OptConsTableSpace
2085 Constraint *n = makeNode(Constraint);
2086 n->contype = CONSTR_PRIMARY;
2089 n->cooked_expr = NULL;
2095 | CHECK '(' a_expr ')'
2097 Constraint *n = makeNode(Constraint);
2098 n->contype = CONSTR_CHECK;
2101 n->cooked_expr = NULL;
2103 n->indexspace = NULL;
2108 Constraint *n = makeNode(Constraint);
2109 n->contype = CONSTR_DEFAULT;
2111 if (exprIsNullConstant($2))
2113 /* DEFAULT NULL should be reported as empty expr */
2120 n->cooked_expr = NULL;
2122 n->indexspace = NULL;
2125 | REFERENCES qualified_name opt_column_list key_match key_actions
2127 FkConstraint *n = makeNode(FkConstraint);
2128 n->constr_name = NULL;
2132 n->fk_matchtype = $4;
2133 n->fk_upd_action = (char) ($5 >> 8);
2134 n->fk_del_action = (char) ($5 & 0xFF);
2135 n->deferrable = FALSE;
2136 n->initdeferred = FALSE;
2142 * ConstraintAttr represents constraint attributes, which we parse as if
2143 * they were independent constraint clauses, in order to avoid shift/reduce
2144 * conflicts (since NOT might start either an independent NOT NULL clause
2145 * or an attribute). parse_utilcmd.c is responsible for attaching the
2146 * attribute information to the preceding "real" constraint node, and for
2147 * complaining if attribute clauses appear in the wrong place or wrong
2150 * See also ConstraintAttributeSpec, which can be used in places where
2151 * there is no parsing conflict.
2156 Constraint *n = makeNode(Constraint);
2157 n->contype = CONSTR_ATTR_DEFERRABLE;
2162 Constraint *n = makeNode(Constraint);
2163 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2166 | INITIALLY DEFERRED
2168 Constraint *n = makeNode(Constraint);
2169 n->contype = CONSTR_ATTR_DEFERRED;
2172 | INITIALLY IMMEDIATE
2174 Constraint *n = makeNode(Constraint);
2175 n->contype = CONSTR_ATTR_IMMEDIATE;
2182 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2183 * This seems to be a poor man's inheritance capability, with the resulting
2184 * tables completely decoupled except for the original commonality in definitions.
2186 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2187 * which is a part of SQL 200N
2190 LIKE qualified_name TableLikeOptionList
2192 InhRelation *n = makeNode(InhRelation);
2199 TableLikeOptionList:
2200 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2201 | /* EMPTY */ { $$ = NIL; }
2205 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2206 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2207 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2208 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2209 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2210 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2214 /* ConstraintElem specifies constraint syntax which is not embedded into
2215 * a column definition. ColConstraintElem specifies the embedded form.
2216 * - thomas 1997-12-03
2219 CONSTRAINT name ConstraintElem
2221 switch (nodeTag($3))
2225 Constraint *n = (Constraint *)$3;
2229 case T_FkConstraint:
2231 FkConstraint *n = (FkConstraint *)$3;
2232 n->constr_name = $2;
2240 | ConstraintElem { $$ = $1; }
2244 CHECK '(' a_expr ')'
2246 Constraint *n = makeNode(Constraint);
2247 n->contype = CONSTR_CHECK;
2250 n->cooked_expr = NULL;
2251 n->indexspace = NULL;
2254 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2256 Constraint *n = makeNode(Constraint);
2257 n->contype = CONSTR_UNIQUE;
2260 n->cooked_expr = NULL;
2266 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2268 Constraint *n = makeNode(Constraint);
2269 n->contype = CONSTR_PRIMARY;
2272 n->cooked_expr = NULL;
2278 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2279 opt_column_list key_match key_actions ConstraintAttributeSpec
2281 FkConstraint *n = makeNode(FkConstraint);
2282 n->constr_name = NULL;
2286 n->fk_matchtype = $9;
2287 n->fk_upd_action = (char) ($10 >> 8);
2288 n->fk_del_action = (char) ($10 & 0xFF);
2289 n->deferrable = ($11 & 1) != 0;
2290 n->initdeferred = ($11 & 2) != 0;
2296 '(' columnList ')' { $$ = $2; }
2297 | /*EMPTY*/ { $$ = NIL; }
2301 columnElem { $$ = list_make1($1); }
2302 | columnList ',' columnElem { $$ = lappend($1, $3); }
2307 $$ = (Node *) makeString($1);
2311 key_match: MATCH FULL
2313 $$ = FKCONSTR_MATCH_FULL;
2318 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2319 errmsg("MATCH PARTIAL not yet implemented")));
2320 $$ = FKCONSTR_MATCH_PARTIAL;
2324 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2328 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2333 * We combine the update and delete actions into one value temporarily
2334 * for simplicity of parsing, and then break them down again in the
2335 * calling production. update is in the left 8 bits, delete in the right.
2336 * Note that NOACTION is the default.
2340 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2342 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2343 | key_update key_delete
2344 { $$ = ($1 << 8) | ($2 & 0xFF); }
2345 | key_delete key_update
2346 { $$ = ($2 << 8) | ($1 & 0xFF); }
2348 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2351 key_update: ON UPDATE key_action { $$ = $3; }
2354 key_delete: ON DELETE_P key_action { $$ = $3; }
2358 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2359 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2360 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2361 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2362 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2365 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2366 | /*EMPTY*/ { $$ = NIL; }
2369 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2371 WITH definition { $$ = $2; }
2372 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2373 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2374 | /*EMPTY*/ { $$ = NIL; }
2377 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2378 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2379 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2380 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2383 OptTableSpace: TABLESPACE name { $$ = $2; }
2384 | /*EMPTY*/ { $$ = NULL; }
2387 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2388 | /*EMPTY*/ { $$ = NULL; }
2393 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2398 CREATE OptTemp TABLE create_as_target AS SelectStmt
2401 * When the SelectStmt is a set-operation tree, we must
2402 * stuff the INTO information into the leftmost component
2403 * Select, because that's where analyze.c will expect
2404 * to find it. Similarly, the output column names must
2405 * be attached to that Select's target list.
2407 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2408 if (n->intoClause != NULL)
2410 (errcode(ERRCODE_SYNTAX_ERROR),
2411 errmsg("CREATE TABLE AS cannot specify INTO")));
2412 $4->rel->istemp = $2;
2419 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2421 $$ = makeNode(IntoClause);
2426 $$->tableSpaceName = $5;
2431 '(' CreateAsList ')' { $$ = $2; }
2432 | /*EMPTY*/ { $$ = NIL; }
2436 CreateAsElement { $$ = list_make1($1); }
2437 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2443 ColumnDef *n = makeNode(ColumnDef);
2448 n->is_not_null = false;
2449 n->raw_default = NULL;
2450 n->cooked_default = NULL;
2451 n->constraints = NIL;
2457 /*****************************************************************************
2460 * CREATE SEQUENCE seqname
2461 * ALTER SEQUENCE seqname
2463 *****************************************************************************/
2466 CREATE OptTemp SEQUENCE qualified_name OptSeqList
2468 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2477 ALTER SEQUENCE qualified_name OptSeqList
2479 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2486 OptSeqList: OptSeqList OptSeqElem { $$ = lappend($1, $2); }
2487 | /*EMPTY*/ { $$ = NIL; }
2490 OptSeqElem: CACHE NumericOnly
2492 $$ = makeDefElem("cache", (Node *)$2);
2496 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2500 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2502 | INCREMENT opt_by NumericOnly
2504 $$ = makeDefElem("increment", (Node *)$3);
2506 | MAXVALUE NumericOnly
2508 $$ = makeDefElem("maxvalue", (Node *)$2);
2510 | MINVALUE NumericOnly
2512 $$ = makeDefElem("minvalue", (Node *)$2);
2516 $$ = makeDefElem("maxvalue", NULL);
2520 $$ = makeDefElem("minvalue", NULL);
2524 $$ = makeDefElem("owned_by", (Node *)$3);
2526 | START opt_with NumericOnly
2528 $$ = makeDefElem("start", (Node *)$3);
2530 | RESTART opt_with NumericOnly
2532 $$ = makeDefElem("restart", (Node *)$3);
2541 FloatOnly { $$ = $1; }
2542 | IntegerOnly { $$ = $1; }
2545 FloatOnly: FCONST { $$ = makeFloat($1); }
2553 IntegerOnly: SignedIconst { $$ = makeInteger($1); };
2556 /*****************************************************************************
2559 * CREATE PROCEDURAL LANGUAGE ...
2560 * DROP PROCEDURAL LANGUAGE ...
2562 *****************************************************************************/
2565 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2567 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2569 /* parameters are all to be supplied by system */
2571 n->plvalidator = NIL;
2572 n->pltrusted = false;
2575 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2576 HANDLER handler_name opt_validator opt_lancompiler
2578 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2581 n->plvalidator = $8;
2583 /* LANCOMPILER is now ignored entirely */
2589 TRUSTED { $$ = TRUE; }
2590 | /*EMPTY*/ { $$ = FALSE; }
2593 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2594 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2595 * Work around by using simple names, instead.
2598 name { $$ = list_make1(makeString($1)); }
2599 | name attrs { $$ = lcons(makeString($1), $2); }
2603 VALIDATOR handler_name { $$ = $2; }
2604 | /*EMPTY*/ { $$ = NIL; }
2608 LANCOMPILER Sconst { $$ = $2; }
2609 | /*EMPTY*/ { $$ = NULL; }
2613 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2615 DropPLangStmt *n = makeNode(DropPLangStmt);
2618 n->missing_ok = false;
2621 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2623 DropPLangStmt *n = makeNode(DropPLangStmt);
2626 n->missing_ok = true;
2636 /*****************************************************************************
2639 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2641 *****************************************************************************/
2643 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2645 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2646 n->tablespacename = $3;
2653 OptTableSpaceOwner: OWNER name { $$ = $2; }
2654 | /*EMPTY */ { $$ = NULL; }
2657 /*****************************************************************************
2660 * DROP TABLESPACE <tablespace>
2662 * No need for drop behaviour as we cannot implement dependencies for
2663 * objects in other databases; we can only support RESTRICT.
2665 ****************************************************************************/
2667 DropTableSpaceStmt: DROP TABLESPACE name
2669 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2670 n->tablespacename = $3;
2671 n->missing_ok = false;
2674 | DROP TABLESPACE IF_P EXISTS name
2676 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2677 n->tablespacename = $5;
2678 n->missing_ok = true;
2683 /*****************************************************************************
2686 * CREATE TRIGGER ...
2689 *****************************************************************************/
2692 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2693 qualified_name TriggerForSpec EXECUTE PROCEDURE
2694 func_name '(' TriggerFuncArgs ')'
2696 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2703 memcpy(n->actions, $5, 4);
2704 n->isconstraint = FALSE;
2705 n->deferrable = FALSE;
2706 n->initdeferred = FALSE;
2707 n->constrrel = NULL;
2710 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2711 qualified_name OptConstrFromTable
2712 ConstraintAttributeSpec
2713 FOR EACH ROW EXECUTE PROCEDURE
2714 func_name '(' TriggerFuncArgs ')'
2716 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2723 memcpy(n->actions, $6, 4);
2724 n->isconstraint = TRUE;
2725 n->deferrable = ($10 & 1) != 0;
2726 n->initdeferred = ($10 & 2) != 0;
2734 BEFORE { $$ = TRUE; }
2735 | AFTER { $$ = FALSE; }
2741 char *e = palloc(4);
2742 e[0] = $1; e[1] = '\0';
2745 | TriggerOneEvent OR TriggerOneEvent
2747 char *e = palloc(4);
2748 e[0] = $1; e[1] = $3; e[2] = '\0';
2751 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2753 char *e = palloc(4);
2754 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2760 INSERT { $$ = 'i'; }
2761 | DELETE_P { $$ = 'd'; }
2762 | UPDATE { $$ = 'u'; }
2766 FOR TriggerForOpt TriggerForType
2773 * If ROW/STATEMENT not specified, default to
2774 * STATEMENT, per SQL
2787 | STATEMENT { $$ = FALSE; }
2791 TriggerFuncArg { $$ = list_make1($1); }
2792 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2793 | /*EMPTY*/ { $$ = NIL; }
2800 snprintf(buf, sizeof(buf), "%d", $1);
2801 $$ = makeString(pstrdup(buf));
2803 | FCONST { $$ = makeString($1); }
2804 | Sconst { $$ = makeString($1); }
2805 | BCONST { $$ = makeString($1); }
2806 | XCONST { $$ = makeString($1); }
2807 | ColId { $$ = makeString($1); }
2811 FROM qualified_name { $$ = $2; }
2812 | /*EMPTY*/ { $$ = NULL; }
2815 ConstraintAttributeSpec:
2816 ConstraintDeferrabilitySpec
2818 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2820 if ($1 == 0 && $2 != 0)
2822 (errcode(ERRCODE_SYNTAX_ERROR),
2823 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2826 | ConstraintTimeSpec
2833 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2835 if ($2 == 0 && $1 != 0)
2837 (errcode(ERRCODE_SYNTAX_ERROR),
2838 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
2845 ConstraintDeferrabilitySpec:
2846 NOT DEFERRABLE { $$ = 0; }
2847 | DEFERRABLE { $$ = 1; }
2851 INITIALLY IMMEDIATE { $$ = 0; }
2852 | INITIALLY DEFERRED { $$ = 2; }
2857 DROP TRIGGER name ON qualified_name opt_drop_behavior
2859 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2863 n->removeType = OBJECT_TRIGGER;
2864 n->missing_ok = false;
2867 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
2869 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2873 n->removeType = OBJECT_TRIGGER;
2874 n->missing_ok = true;
2880 /*****************************************************************************
2883 * CREATE ASSERTION ...
2884 * DROP ASSERTION ...
2886 *****************************************************************************/
2889 CREATE ASSERTION name CHECK '(' a_expr ')'
2890 ConstraintAttributeSpec
2892 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2894 n->args = list_make1($6);
2895 n->isconstraint = TRUE;
2896 n->deferrable = ($8 & 1) != 0;
2897 n->initdeferred = ($8 & 2) != 0;
2900 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2901 errmsg("CREATE ASSERTION is not yet implemented")));
2908 DROP ASSERTION name opt_drop_behavior
2910 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2914 n->removeType = OBJECT_TRIGGER; /* XXX */
2916 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2917 errmsg("DROP ASSERTION is not yet implemented")));
2923 /*****************************************************************************
2926 * define (aggregate,operator,type)
2928 *****************************************************************************/
2931 CREATE AGGREGATE func_name aggr_args definition
2933 DefineStmt *n = makeNode(DefineStmt);
2934 n->kind = OBJECT_AGGREGATE;
2935 n->oldstyle = false;
2941 | CREATE AGGREGATE func_name old_aggr_definition
2943 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
2944 DefineStmt *n = makeNode(DefineStmt);
2945 n->kind = OBJECT_AGGREGATE;
2952 | CREATE OPERATOR any_operator definition
2954 DefineStmt *n = makeNode(DefineStmt);
2955 n->kind = OBJECT_OPERATOR;
2956 n->oldstyle = false;
2962 | CREATE TYPE_P any_name definition
2964 DefineStmt *n = makeNode(DefineStmt);
2965 n->kind = OBJECT_TYPE;
2966 n->oldstyle = false;
2972 | CREATE TYPE_P any_name
2974 /* Shell type (identified by lack of definition) */
2975 DefineStmt *n = makeNode(DefineStmt);
2976 n->kind = OBJECT_TYPE;
2977 n->oldstyle = false;
2980 n->definition = NIL;
2983 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
2985 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
2986 RangeVar *r = makeNode(RangeVar);
2988 /* can't use qualified_name, sigh */
2989 switch (list_length($3))
2992 r->catalogname = NULL;
2993 r->schemaname = NULL;
2994 r->relname = strVal(linitial($3));
2997 r->catalogname = NULL;
2998 r->schemaname = strVal(linitial($3));
2999 r->relname = strVal(lsecond($3));
3002 r->catalogname = strVal(linitial($3));
3003 r->schemaname = strVal(lsecond($3));
3004 r->relname = strVal(lthird($3));
3008 (errcode(ERRCODE_SYNTAX_ERROR),
3009 errmsg("improper qualified name (too many dotted names): %s",
3010 NameListToString($3))));
3017 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3019 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3024 | CREATE TEXT_P SEARCH PARSER any_name definition
3026 DefineStmt *n = makeNode(DefineStmt);
3027 n->kind = OBJECT_TSPARSER;
3033 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3035 DefineStmt *n = makeNode(DefineStmt);
3036 n->kind = OBJECT_TSDICTIONARY;
3042 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3044 DefineStmt *n = makeNode(DefineStmt);
3045 n->kind = OBJECT_TSTEMPLATE;
3051 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3053 DefineStmt *n = makeNode(DefineStmt);
3054 n->kind = OBJECT_TSCONFIGURATION;
3062 definition: '(' def_list ')' { $$ = $2; }
3065 def_list: def_elem { $$ = list_make1($1); }
3066 | def_list ',' def_elem { $$ = lappend($1, $3); }
3069 def_elem: ColLabel '=' def_arg
3071 $$ = makeDefElem($1, (Node *)$3);
3075 $$ = makeDefElem($1, NULL);
3079 /* Note: any simple identifier will be returned as a type name! */
3080 def_arg: func_type { $$ = (Node *)$1; }
3081 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3082 | qual_all_Op { $$ = (Node *)$1; }
3083 | NumericOnly { $$ = (Node *)$1; }
3084 | Sconst { $$ = (Node *)makeString($1); }
3087 aggr_args: '(' type_list ')' { $$ = $2; }
3088 | '(' '*' ')' { $$ = NIL; }
3091 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3094 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3095 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3098 old_aggr_elem: IDENT '=' def_arg
3100 $$ = makeDefElem($1, (Node *)$3);
3104 enum_val_list: Sconst
3105 { $$ = list_make1(makeString($1)); }
3106 | enum_val_list ',' Sconst
3107 { $$ = lappend($1, makeString($3)); }
3111 /*****************************************************************************
3114 * CREATE OPERATOR CLASS ...
3115 * CREATE OPERATOR FAMILY ...
3116 * ALTER OPERATOR FAMILY ...
3117 * DROP OPERATOR CLASS ...
3118 * DROP OPERATOR FAMILY ...
3120 *****************************************************************************/
3123 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3124 USING access_method opt_opfamily AS opclass_item_list
3126 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3127 n->opclassname = $4;
3131 n->opfamilyname = $11;
3138 opclass_item { $$ = list_make1($1); }
3139 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3143 OPERATOR Iconst any_operator opt_recheck
3145 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3146 n->itemtype = OPCLASS_ITEM_OPERATOR;
3153 | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
3155 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3156 n->itemtype = OPCLASS_ITEM_OPERATOR;
3163 | FUNCTION Iconst func_name func_args
3165 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3166 n->itemtype = OPCLASS_ITEM_FUNCTION;
3168 n->args = extractArgTypes($4);
3172 | FUNCTION Iconst '(' type_list ')' func_name func_args
3174 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3175 n->itemtype = OPCLASS_ITEM_FUNCTION;
3177 n->args = extractArgTypes($7);
3184 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3185 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3191 opt_default: DEFAULT { $$ = TRUE; }
3192 | /*EMPTY*/ { $$ = FALSE; }
3195 opt_opfamily: FAMILY any_name { $$ = $2; }
3196 | /*EMPTY*/ { $$ = NIL; }
3199 opt_recheck: RECHECK { $$ = TRUE; }
3200 | /*EMPTY*/ { $$ = FALSE; }
3205 CREATE OPERATOR FAMILY any_name USING access_method
3207 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3208 n->opfamilyname = $4;
3215 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3217 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3218 n->opfamilyname = $4;
3224 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3226 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3227 n->opfamilyname = $4;
3236 opclass_drop { $$ = list_make1($1); }
3237 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3241 OPERATOR Iconst '(' type_list ')'
3243 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3244 n->itemtype = OPCLASS_ITEM_OPERATOR;
3249 | FUNCTION Iconst '(' type_list ')'
3251 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3252 n->itemtype = OPCLASS_ITEM_FUNCTION;
3261 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3263 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3264 n->opclassname = $4;
3267 n->missing_ok = false;
3270 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3272 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3273 n->opclassname = $6;
3276 n->missing_ok = true;
3282 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3284 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3285 n->opfamilyname = $4;
3288 n->missing_ok = false;
3291 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3293 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3294 n->opfamilyname = $6;
3297 n->missing_ok = true;
3303 /*****************************************************************************
3307 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3308 * REASSIGN OWNED BY username [, username ...] TO username
3310 *****************************************************************************/
3312 DROP OWNED BY name_list opt_drop_behavior
3314 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3322 REASSIGN OWNED BY name_list TO name
3324 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3331 /*****************************************************************************
3335 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3336 * [ RESTRICT | CASCADE ]
3338 *****************************************************************************/
3340 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3342 DropStmt *n = makeNode(DropStmt);
3344 n->missing_ok = TRUE;
3349 | DROP drop_type any_name_list opt_drop_behavior
3351 DropStmt *n = makeNode(DropStmt);
3353 n->missing_ok = FALSE;
3361 drop_type: TABLE { $$ = OBJECT_TABLE; }
3362 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3363 | VIEW { $$ = OBJECT_VIEW; }
3364 | INDEX { $$ = OBJECT_INDEX; }
3365 | TYPE_P { $$ = OBJECT_TYPE; }
3366 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3367 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3368 | SCHEMA { $$ = OBJECT_SCHEMA; }
3369 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3370 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3371 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3372 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3376 any_name { $$ = list_make1($1); }
3377 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3380 any_name: ColId { $$ = list_make1(makeString($1)); }
3381 | ColId attrs { $$ = lcons(makeString($1), $2); }
3384 attrs: '.' attr_name
3385 { $$ = list_make1(makeString($2)); }
3386 | attrs '.' attr_name
3387 { $$ = lappend($1, makeString($3)); }
3391 /*****************************************************************************
3394 * truncate table relname1, relname2, ...
3396 *****************************************************************************/
3399 TRUNCATE opt_table qualified_name_list opt_drop_behavior
3401 TruncateStmt *n = makeNode(TruncateStmt);
3408 /*****************************************************************************
3410 * The COMMENT ON statement can take different forms based upon the type of
3411 * the object associated with the comment. The form of the statement is:
3413 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3414 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3415 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3416 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3417 * TEXT SEARCH TEMPLATE |
3418 * TEXT SEARCH CONFIGURATION ] <objname> |
3419 * AGGREGATE <aggname> (arg1, ...) |
3420 * FUNCTION <funcname> (arg1, arg2, ...) |
3421 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3422 * TRIGGER <triggername> ON <relname> |
3423 * CONSTRAINT <constraintname> ON <relname> |
3424 * RULE <rulename> ON <relname> ]
3427 *****************************************************************************/
3430 COMMENT ON comment_type any_name IS comment_text
3432 CommentStmt *n = makeNode(CommentStmt);
3439 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3441 CommentStmt *n = makeNode(CommentStmt);
3442 n->objtype = OBJECT_AGGREGATE;
3448 | COMMENT ON FUNCTION func_name func_args IS comment_text
3450 CommentStmt *n = makeNode(CommentStmt);
3451 n->objtype = OBJECT_FUNCTION;
3453 n->objargs = extractArgTypes($5);
3457 | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
3460 CommentStmt *n = makeNode(CommentStmt);
3461 n->objtype = OBJECT_OPERATOR;
3467 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3469 CommentStmt *n = makeNode(CommentStmt);
3470 n->objtype = OBJECT_CONSTRAINT;
3471 n->objname = lappend($6, makeString($4));
3476 | COMMENT ON RULE name ON any_name IS comment_text
3478 CommentStmt *n = makeNode(CommentStmt);
3479 n->objtype = OBJECT_RULE;
3480 n->objname = lappend($6, makeString($4));
3485 | COMMENT ON RULE name IS comment_text
3487 /* Obsolete syntax supported for awhile for compatibility */
3488 CommentStmt *n = makeNode(CommentStmt);
3489 n->objtype = OBJECT_RULE;
3490 n->objname = list_make1(makeString($4));
3495 | COMMENT ON TRIGGER name ON any_name IS comment_text
3497 CommentStmt *n = makeNode(CommentStmt);
3498 n->objtype = OBJECT_TRIGGER;
3499 n->objname = lappend($6, makeString($4));
3504 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3506 CommentStmt *n = makeNode(CommentStmt);
3507 n->objtype = OBJECT_OPCLASS;
3509 n->objargs = list_make1(makeString($7));
3513 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3515 CommentStmt *n = makeNode(CommentStmt);
3516 n->objtype = OBJECT_OPFAMILY;
3518 n->objargs = list_make1(makeString($7));
3522 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3524 CommentStmt *n = makeNode(CommentStmt);
3525 n->objtype = OBJECT_LARGEOBJECT;
3526 n->objname = list_make1($5);
3531 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3533 CommentStmt *n = makeNode(CommentStmt);
3534 n->objtype = OBJECT_CAST;
3535 n->objname = list_make1($5);
3536 n->objargs = list_make1($7);
3540 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3542 CommentStmt *n = makeNode(CommentStmt);
3543 n->objtype = OBJECT_LANGUAGE;
3549 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3551 CommentStmt *n = makeNode(CommentStmt);
3552 n->objtype = OBJECT_TSPARSER;
3557 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3559 CommentStmt *n = makeNode(CommentStmt);
3560 n->objtype = OBJECT_TSDICTIONARY;
3565 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3567 CommentStmt *n = makeNode(CommentStmt);
3568 n->objtype = OBJECT_TSTEMPLATE;
3573 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3575 CommentStmt *n = makeNode(CommentStmt);
3576 n->objtype = OBJECT_TSCONFIGURATION;
3584 COLUMN { $$ = OBJECT_COLUMN; }
3585 | DATABASE { $$ = OBJECT_DATABASE; }
3586 | SCHEMA { $$ = OBJECT_SCHEMA; }
3587 | INDEX { $$ = OBJECT_INDEX; }
3588 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3589 | TABLE { $$ = OBJECT_TABLE; }
3590 | DOMAIN_P { $$ = OBJECT_TYPE; }
3591 | TYPE_P { $$ = OBJECT_TYPE; }
3592 | VIEW { $$ = OBJECT_VIEW; }
3593 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3594 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3595 | ROLE { $$ = OBJECT_ROLE; }
3600 | NULL_P { $$ = NULL; }
3603 /*****************************************************************************
3608 *****************************************************************************/
3610 FetchStmt: FETCH fetch_direction from_in name
3612 FetchStmt *n = (FetchStmt *) $2;
3619 FetchStmt *n = makeNode(FetchStmt);
3620 n->direction = FETCH_FORWARD;
3626 | MOVE fetch_direction from_in name
3628 FetchStmt *n = (FetchStmt *) $2;
3635 FetchStmt *n = makeNode(FetchStmt);
3636 n->direction = FETCH_FORWARD;
3647 FetchStmt *n = makeNode(FetchStmt);
3648 n->direction = FETCH_FORWARD;
3654 FetchStmt *n = makeNode(FetchStmt);
3655 n->direction = FETCH_FORWARD;
3661 FetchStmt *n = makeNode(FetchStmt);
3662 n->direction = FETCH_BACKWARD;
3668 FetchStmt *n = makeNode(FetchStmt);
3669 n->direction = FETCH_ABSOLUTE;
3675 FetchStmt *n = makeNode(FetchStmt);
3676 n->direction = FETCH_ABSOLUTE;
3680 | ABSOLUTE_P SignedIconst
3682 FetchStmt *n = makeNode(FetchStmt);
3683 n->direction = FETCH_ABSOLUTE;
3687 | RELATIVE_P SignedIconst
3689 FetchStmt *n = makeNode(FetchStmt);
3690 n->direction = FETCH_RELATIVE;
3696 FetchStmt *n = makeNode(FetchStmt);
3697 n->direction = FETCH_FORWARD;
3703 FetchStmt *n = makeNode(FetchStmt);
3704 n->direction = FETCH_FORWARD;
3705 n->howMany = FETCH_ALL;
3710 FetchStmt *n = makeNode(FetchStmt);
3711 n->direction = FETCH_FORWARD;
3715 | FORWARD SignedIconst
3717 FetchStmt *n = makeNode(FetchStmt);
3718 n->direction = FETCH_FORWARD;
3724 FetchStmt *n = makeNode(FetchStmt);
3725 n->direction = FETCH_FORWARD;
3726 n->howMany = FETCH_ALL;
3731 FetchStmt *n = makeNode(FetchStmt);
3732 n->direction = FETCH_BACKWARD;
3736 | BACKWARD SignedIconst
3738 FetchStmt *n = makeNode(FetchStmt);
3739 n->direction = FETCH_BACKWARD;
3745 FetchStmt *n = makeNode(FetchStmt);
3746 n->direction = FETCH_BACKWARD;
3747 n->howMany = FETCH_ALL;
3757 /*****************************************************************************
3759 * GRANT and REVOKE statements
3761 *****************************************************************************/
3763 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3764 opt_grant_grant_option
3766 GrantStmt *n = makeNode(GrantStmt);
3769 n->objtype = ($4)->objtype;
3770 n->objects = ($4)->objs;
3772 n->grant_option = $7;
3778 REVOKE privileges ON privilege_target
3779 FROM grantee_list opt_drop_behavior
3781 GrantStmt *n = makeNode(GrantStmt);
3782 n->is_grant = false;
3783 n->grant_option = false;
3785 n->objtype = ($4)->objtype;
3786 n->objects = ($4)->objs;
3791 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3792 FROM grantee_list opt_drop_behavior
3794 GrantStmt *n = makeNode(GrantStmt);
3795 n->is_grant = false;
3796 n->grant_option = true;
3798 n->objtype = ($7)->objtype;
3799 n->objects = ($7)->objs;
3808 * A privilege list is represented as a list of strings; the validity of
3809 * the privilege names gets checked at execution. This is a bit annoying
3810 * but we have little choice because of the syntactic conflict with lists
3811 * of role names in GRANT/REVOKE. What's more, we have to call out in
3812 * the "privilege" production any reserved keywords that need to be usable
3813 * as privilege names.
3816 /* either ALL [PRIVILEGES] or a list of individual privileges */
3817 privileges: privilege_list
3825 privilege_list: privilege
3826 { $$ = list_make1(makeString($1)); }
3827 | privilege_list ',' privilege
3828 { $$ = lappend($1, makeString($3)); }
3831 privilege: SELECT { $$ = pstrdup($1); }
3832 | REFERENCES { $$ = pstrdup($1); }
3833 | CREATE { $$ = pstrdup($1); }
3834 | ColId { $$ = $1; }
3838 /* Don't bother trying to fold the first two rules into one using
3839 * opt_table. You're going to get conflicts.
3844 PrivTarget *n = makeNode(PrivTarget);
3845 n->objtype = ACL_OBJECT_RELATION;
3849 | TABLE qualified_name_list
3851 PrivTarget *n = makeNode(PrivTarget);
3852 n->objtype = ACL_OBJECT_RELATION;
3856 | SEQUENCE qualified_name_list
3858 PrivTarget *n = makeNode(PrivTarget);
3859 n->objtype = ACL_OBJECT_SEQUENCE;
3863 | FUNCTION function_with_argtypes_list
3865 PrivTarget *n = makeNode(PrivTarget);
3866 n->objtype = ACL_OBJECT_FUNCTION;
3870 | DATABASE name_list
3872 PrivTarget *n = makeNode(PrivTarget);
3873 n->objtype = ACL_OBJECT_DATABASE;
3877 | LANGUAGE name_list
3879 PrivTarget *n = makeNode(PrivTarget);
3880 n->objtype = ACL_OBJECT_LANGUAGE;
3886 PrivTarget *n = makeNode(PrivTarget);
3887 n->objtype = ACL_OBJECT_NAMESPACE;
3891 | TABLESPACE name_list
3893 PrivTarget *n = makeNode(PrivTarget);
3894 n->objtype = ACL_OBJECT_TABLESPACE;
3902 grantee { $$ = list_make1($1); }
3903 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3908 PrivGrantee *n = makeNode(PrivGrantee);
3909 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3910 if (strcmp($1, "public") == 0)
3918 PrivGrantee *n = makeNode(PrivGrantee);
3919 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3920 if (strcmp($2, "public") == 0)
3929 opt_grant_grant_option:
3930 WITH GRANT OPTION { $$ = TRUE; }
3931 | /*EMPTY*/ { $$ = FALSE; }
3934 function_with_argtypes_list:
3935 function_with_argtypes { $$ = list_make1($1); }
3936 | function_with_argtypes_list ',' function_with_argtypes
3937 { $$ = lappend($1, $3); }
3940 function_with_argtypes:
3943 FuncWithArgs *n = makeNode(FuncWithArgs);
3945 n->funcargs = extractArgTypes($2);
3950 /*****************************************************************************
3952 * GRANT and REVOKE ROLE statements
3954 *****************************************************************************/
3957 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
3959 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3961 n->granted_roles = $2;
3962 n->grantee_roles = $4;
3970 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
3972 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3973 n->is_grant = false;
3974 n->admin_opt = false;
3975 n->granted_roles = $2;
3976 n->grantee_roles = $4;
3980 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
3982 GrantRoleStmt *n = makeNode(GrantRoleStmt);
3983 n->is_grant = false;
3984 n->admin_opt = true;
3985 n->granted_roles = $5;
3986 n->grantee_roles = $7;
3992 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
3993 | /*EMPTY*/ { $$ = FALSE; }
3996 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
3997 | /*EMPTY*/ { $$ = NULL; }
4001 /*****************************************************************************
4003 * QUERY: CREATE INDEX
4005 * Note: we can't factor CONCURRENTLY into a separate production without
4006 * making it a reserved word.
4008 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4009 * willing to make TABLESPACE a fully reserved word.
4010 *****************************************************************************/
4012 IndexStmt: CREATE index_opt_unique INDEX index_name
4013 ON qualified_name access_method_clause '(' index_params ')'
4014 opt_definition OptTableSpace where_clause
4016 IndexStmt *n = makeNode(IndexStmt);
4018 n->concurrent = false;
4021 n->accessMethod = $7;
4022 n->indexParams = $9;
4024 n->tableSpace = $12;
4025 n->whereClause = $13;
4028 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4029 ON qualified_name access_method_clause '(' index_params ')'
4030 opt_definition OptTableSpace where_clause
4032 IndexStmt *n = makeNode(IndexStmt);
4034 n->concurrent = true;
4037 n->accessMethod = $8;
4038 n->indexParams = $10;
4040 n->tableSpace = $13;
4041 n->whereClause = $14;
4047 UNIQUE { $$ = TRUE; }
4048 | /*EMPTY*/ { $$ = FALSE; }
4051 access_method_clause:
4052 USING access_method { $$ = $2; }
4053 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4056 index_params: index_elem { $$ = list_make1($1); }
4057 | index_params ',' index_elem { $$ = lappend($1, $3); }
4061 * Index attributes can be either simple column references, or arbitrary
4062 * expressions in parens. For backwards-compatibility reasons, we allow
4063 * an expression that's just a function call to be written without parens.
4065 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4067 $$ = makeNode(IndexElem);
4072 $$->nulls_ordering = $4;
4074 | func_expr opt_class opt_asc_desc opt_nulls_order
4076 $$ = makeNode(IndexElem);
4081 $$->nulls_ordering = $4;
4083 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4085 $$ = makeNode(IndexElem);
4090 $$->nulls_ordering = $6;
4094 opt_class: any_name { $$ = $1; }
4095 | USING any_name { $$ = $2; }
4096 | /*EMPTY*/ { $$ = NIL; }
4099 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4100 | DESC { $$ = SORTBY_DESC; }
4101 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4104 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4105 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4106 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4110 /*****************************************************************************
4113 * create [or replace] function <fname>
4114 * [(<type-1> { , <type-n>})]
4116 * as <filename or code in language as appropriate>
4117 * language <lang> [with parameters]
4119 *****************************************************************************/
4122 CREATE opt_or_replace FUNCTION func_name func_args
4123 RETURNS func_return createfunc_opt_list opt_definition
4125 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4134 | CREATE opt_or_replace FUNCTION func_name func_args
4135 createfunc_opt_list opt_definition
4137 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4141 n->returnType = NULL;
4149 OR REPLACE { $$ = TRUE; }
4150 | /*EMPTY*/ { $$ = FALSE; }
4153 func_args: '(' func_args_list ')' { $$ = $2; }
4154 | '(' ')' { $$ = NIL; }
4158 func_arg { $$ = list_make1($1); }
4159 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4163 * The style with arg_class first is SQL99 standard, but Oracle puts
4164 * param_name first; accept both since it's likely people will try both
4165 * anyway. Don't bother trying to save productions by letting arg_class
4166 * have an empty alternative ... you'll get shift/reduce conflicts.
4168 * We can catch over-specified arguments here if we want to,
4169 * but for now better to silently swallow typmod, etc.
4170 * - thomas 2000-03-22
4173 arg_class param_name func_type
4175 FunctionParameter *n = makeNode(FunctionParameter);
4181 | param_name arg_class func_type
4183 FunctionParameter *n = makeNode(FunctionParameter);
4189 | param_name func_type
4191 FunctionParameter *n = makeNode(FunctionParameter);
4194 n->mode = FUNC_PARAM_IN;
4197 | arg_class func_type
4199 FunctionParameter *n = makeNode(FunctionParameter);
4207 FunctionParameter *n = makeNode(FunctionParameter);
4210 n->mode = FUNC_PARAM_IN;
4215 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4216 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4217 | OUT_P { $$ = FUNC_PARAM_OUT; }
4218 | INOUT { $$ = FUNC_PARAM_INOUT; }
4219 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4223 * Ideally param_name should be ColId, but that causes too many conflicts.
4225 param_name: type_function_name
4231 /* We can catch over-specified results here if we want to,
4232 * but for now better to silently swallow typmod, etc.
4233 * - thomas 2000-03-22
4240 * We would like to make the %TYPE productions here be ColId attrs etc,
4241 * but that causes reduce/reduce conflicts. type_function_name
4242 * is next best choice.
4244 func_type: Typename { $$ = $1; }
4245 | type_function_name attrs '%' TYPE_P
4247 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4248 $$->pct_type = true;
4251 | SETOF type_function_name attrs '%' TYPE_P
4253 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4254 $$->pct_type = true;
4261 createfunc_opt_list:
4262 /* Must be at least one to prevent conflict */
4263 createfunc_opt_item { $$ = list_make1($1); }
4264 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4268 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4270 common_func_opt_item:
4271 CALLED ON NULL_P INPUT_P
4273 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4275 | RETURNS NULL_P ON NULL_P INPUT_P
4277 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4281 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4285 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4289 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4293 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4295 | EXTERNAL SECURITY DEFINER
4297 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4299 | EXTERNAL SECURITY INVOKER
4301 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4305 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4309 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4313 $$ = makeDefElem("cost", (Node *)$2);
4317 $$ = makeDefElem("rows", (Node *)$2);
4321 /* we abuse the normal content of a DefElem here */
4322 $$ = makeDefElem("set", (Node *)$1);
4326 createfunc_opt_item:
4329 $$ = makeDefElem("as", (Node *)$2);
4331 | LANGUAGE ColId_or_Sconst
4333 $$ = makeDefElem("language", (Node *)makeString($2));
4335 | common_func_opt_item
4341 func_as: Sconst { $$ = list_make1(makeString($1)); }
4344 $$ = list_make2(makeString($1), makeString($3));
4349 WITH definition { $$ = $2; }
4350 | /*EMPTY*/ { $$ = NIL; }
4353 /*****************************************************************************
4356 * RENAME and OWNER subcommands are already provided by the generic
4357 * ALTER infrastructure, here we just specify alterations that can
4358 * only be applied to functions.
4360 *****************************************************************************/
4362 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4364 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4372 /* At least one option must be specified */
4373 common_func_opt_item { $$ = list_make1($1); }
4374 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4377 /* Ignored, merely for SQL compliance */
4384 /*****************************************************************************
4388 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4389 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4390 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4392 *****************************************************************************/
4395 DROP FUNCTION func_name func_args opt_drop_behavior
4397 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4398 n->kind = OBJECT_FUNCTION;
4400 n->args = extractArgTypes($4);
4402 n->missing_ok = false;
4405 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4407 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4408 n->kind = OBJECT_FUNCTION;
4410 n->args = extractArgTypes($6);
4412 n->missing_ok = true;
4418 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4420 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4421 n->kind = OBJECT_AGGREGATE;
4425 n->missing_ok = false;
4428 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4430 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4431 n->kind = OBJECT_AGGREGATE;
4435 n->missing_ok = true;
4441 DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
4443 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4444 n->kind = OBJECT_OPERATOR;
4448 n->missing_ok = false;
4451 | DROP OPERATOR IF_P EXISTS any_operator '(' oper_argtypes ')' opt_drop_behavior
4453 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4454 n->kind = OBJECT_OPERATOR;
4458 n->missing_ok = true;
4467 (errcode(ERRCODE_SYNTAX_ERROR),
4468 errmsg("missing argument"),
4469 errhint("Use NONE to denote the missing argument of a unary operator.")));
4471 | Typename ',' Typename
4472 { $$ = list_make2($1, $3); }
4473 | NONE ',' Typename /* left unary */
4474 { $$ = list_make2(NULL, $3); }
4475 | Typename ',' NONE /* right unary */
4476 { $$ = list_make2($1, NULL); }
4481 { $$ = list_make1(makeString($1)); }
4482 | ColId '.' any_operator
4483 { $$ = lcons(makeString($1), $3); }
4487 /*****************************************************************************
4489 * CREATE CAST / DROP CAST
4491 *****************************************************************************/
4493 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4494 WITH FUNCTION function_with_argtypes cast_context
4496 CreateCastStmt *n = makeNode(CreateCastStmt);
4500 n->context = (CoercionContext) $11;
4503 | CREATE CAST '(' Typename AS Typename ')'
4504 WITHOUT FUNCTION cast_context
4506 CreateCastStmt *n = makeNode(CreateCastStmt);
4510 n->context = (CoercionContext) $10;
4515 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
4516 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
4517 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
4521 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
4523 DropCastStmt *n = makeNode(DropCastStmt);
4532 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
4533 | /*EMPTY*/ { $$ = FALSE; }
4537 /*****************************************************************************
4541 * REINDEX type <name> [FORCE]
4543 * FORCE no longer does anything, but we accept it for backwards compatibility
4544 *****************************************************************************/
4547 REINDEX reindex_type qualified_name opt_force
4549 ReindexStmt *n = makeNode(ReindexStmt);
4555 | REINDEX SYSTEM_P name opt_force
4557 ReindexStmt *n = makeNode(ReindexStmt);
4558 n->kind = OBJECT_DATABASE;
4561 n->do_system = true;
4565 | REINDEX DATABASE name opt_force
4567 ReindexStmt *n = makeNode(ReindexStmt);
4568 n->kind = OBJECT_DATABASE;
4571 n->do_system = true;
4578 INDEX { $$ = OBJECT_INDEX; }
4579 | TABLE { $$ = OBJECT_TABLE; }
4582 opt_force: FORCE { $$ = TRUE; }
4583 | /* EMPTY */ { $$ = FALSE; }
4587 /*****************************************************************************
4589 * ALTER THING name RENAME TO newname
4591 *****************************************************************************/
4593 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
4595 RenameStmt *n = makeNode(RenameStmt);
4596 n->renameType = OBJECT_AGGREGATE;
4602 | ALTER CONVERSION_P any_name RENAME TO name
4604 RenameStmt *n = makeNode(RenameStmt);
4605 n->renameType = OBJECT_CONVERSION;
4610 | ALTER DATABASE database_name RENAME TO database_name
4612 RenameStmt *n = makeNode(RenameStmt);
4613 n->renameType = OBJECT_DATABASE;
4618 | ALTER FUNCTION function_with_argtypes RENAME TO name
4620 RenameStmt *n = makeNode(RenameStmt);
4621 n->renameType = OBJECT_FUNCTION;
4622 n->object = $3->funcname;
4623 n->objarg = $3->funcargs;
4627 | ALTER GROUP_P RoleId RENAME TO RoleId
4629 RenameStmt *n = makeNode(RenameStmt);
4630 n->renameType = OBJECT_ROLE;
4635 | ALTER opt_procedural LANGUAGE name RENAME TO name
4637 RenameStmt *n = makeNode(RenameStmt);
4638 n->renameType = OBJECT_LANGUAGE;
4643 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
4645 RenameStmt *n = makeNode(RenameStmt);
4646 n->renameType = OBJECT_OPCLASS;
4652 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
4654 RenameStmt *n = makeNode(RenameStmt);
4655 n->renameType = OBJECT_OPFAMILY;
4661 | ALTER SCHEMA name RENAME TO name
4663 RenameStmt *n = makeNode(RenameStmt);
4664 n->renameType = OBJECT_SCHEMA;
4669 | ALTER TABLE relation_expr RENAME TO name
4671 RenameStmt *n = makeNode(RenameStmt);
4672 n->renameType = OBJECT_TABLE;
4678 | ALTER SEQUENCE relation_expr RENAME TO name
4680 RenameStmt *n = makeNode(RenameStmt);
4681 n->renameType = OBJECT_SEQUENCE;
4687 | ALTER VIEW relation_expr RENAME TO name
4689 RenameStmt *n = makeNode(RenameStmt);
4690 n->renameType = OBJECT_VIEW;
4696 | ALTER INDEX relation_expr RENAME TO name
4698 RenameStmt *n = makeNode(RenameStmt);
4699 n->renameType = OBJECT_INDEX;
4705 | ALTER TABLE relation_expr RENAME opt_column name TO name
4707 RenameStmt *n = makeNode(RenameStmt);
4708 n->renameType = OBJECT_COLUMN;
4714 | ALTER TRIGGER name ON relation_expr RENAME TO name
4716 RenameStmt *n = makeNode(RenameStmt);
4717 n->renameType = OBJECT_TRIGGER;
4723 | ALTER ROLE RoleId RENAME TO RoleId
4725 RenameStmt *n = makeNode(RenameStmt);
4726 n->renameType = OBJECT_ROLE;
4731 | ALTER USER RoleId RENAME TO RoleId
4733 RenameStmt *n = makeNode(RenameStmt);
4734 n->renameType = OBJECT_ROLE;
4739 | ALTER TABLESPACE name RENAME TO name
4741 RenameStmt *n = makeNode(RenameStmt);
4742 n->renameType = OBJECT_TABLESPACE;
4747 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
4749 RenameStmt *n = makeNode(RenameStmt);
4750 n->renameType = OBJECT_TSPARSER;
4755 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
4757 RenameStmt *n = makeNode(RenameStmt);
4758 n->renameType = OBJECT_TSDICTIONARY;
4763 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
4765 RenameStmt *n = makeNode(RenameStmt);
4766 n->renameType = OBJECT_TSTEMPLATE;
4771 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
4773 RenameStmt *n = makeNode(RenameStmt);
4774 n->renameType = OBJECT_TSCONFIGURATION;
4781 opt_column: COLUMN { $$ = COLUMN; }
4782 | /*EMPTY*/ { $$ = 0; }
4785 /*****************************************************************************
4787 * ALTER THING name SET SCHEMA name
4789 *****************************************************************************/
4791 AlterObjectSchemaStmt:
4792 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
4794 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4795 n->objectType = OBJECT_AGGREGATE;
4801 | ALTER DOMAIN_P any_name SET SCHEMA name
4803 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4804 n->objectType = OBJECT_DOMAIN;
4809 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
4811 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4812 n->objectType = OBJECT_FUNCTION;
4813 n->object = $3->funcname;
4814 n->objarg = $3->funcargs;
4818 | ALTER SEQUENCE relation_expr SET SCHEMA name
4820 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4821 n->objectType = OBJECT_SEQUENCE;
4826 | ALTER TABLE relation_expr SET SCHEMA name
4828 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4829 n->objectType = OBJECT_TABLE;
4834 | ALTER TYPE_P any_name SET SCHEMA name
4836 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4837 n->objectType = OBJECT_TYPE;
4844 /*****************************************************************************
4846 * ALTER THING name OWNER TO newname
4848 *****************************************************************************/
4850 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
4852 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4853 n->objectType = OBJECT_AGGREGATE;
4859 | ALTER CONVERSION_P any_name OWNER TO RoleId
4861 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4862 n->objectType = OBJECT_CONVERSION;
4867 | ALTER DATABASE database_name OWNER TO RoleId
4869 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4870 n->objectType = OBJECT_DATABASE;
4871 n->object = list_make1($3);
4875 | ALTER DOMAIN_P any_name OWNER TO RoleId
4877 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4878 n->objectType = OBJECT_DOMAIN;
4883 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
4885 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4886 n->objectType = OBJECT_FUNCTION;
4887 n->object = $3->funcname;
4888 n->objarg = $3->funcargs;
4892 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
4894 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4895 n->objectType = OBJECT_LANGUAGE;
4896 n->object = list_make1($4);
4900 | ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO RoleId
4902 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4903 n->objectType = OBJECT_OPERATOR;
4909 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
4911 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4912 n->objectType = OBJECT_OPCLASS;
4918 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
4920 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4921 n->objectType = OBJECT_OPFAMILY;
4927 | ALTER SCHEMA name OWNER TO RoleId
4929 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4930 n->objectType = OBJECT_SCHEMA;
4931 n->object = list_make1($3);
4935 | ALTER TYPE_P any_name OWNER TO RoleId
4937 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4938 n->objectType = OBJECT_TYPE;
4943 | ALTER TABLESPACE name OWNER TO RoleId
4945 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4946 n->objectType = OBJECT_TABLESPACE;
4947 n->object = list_make1($3);
4951 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
4953 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4954 n->objectType = OBJECT_TSDICTIONARY;
4959 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
4961 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4962 n->objectType = OBJECT_TSCONFIGURATION;
4970 /*****************************************************************************
4972 * QUERY: Define Rewrite Rule
4974 *****************************************************************************/
4976 RuleStmt: CREATE opt_or_replace RULE name AS
4977 { QueryIsRule=TRUE; }
4978 ON event TO qualified_name where_clause
4979 DO opt_instead RuleActionList
4981 RuleStmt *n = makeNode(RuleStmt);
4985 n->whereClause = $11;
4995 NOTHING { $$ = NIL; }
4996 | RuleActionStmt { $$ = list_make1($1); }
4997 | '(' RuleActionMulti ')' { $$ = $2; }
5000 /* the thrashing around here is to discard "empty" statements... */
5002 RuleActionMulti ';' RuleActionStmtOrEmpty
5004 $$ = lappend($1, $3);
5008 | RuleActionStmtOrEmpty
5010 $$ = list_make1($1);
5024 RuleActionStmtOrEmpty:
5025 RuleActionStmt { $$ = $1; }
5026 | /*EMPTY*/ { $$ = NULL; }
5029 event: SELECT { $$ = CMD_SELECT; }
5030 | UPDATE { $$ = CMD_UPDATE; }
5031 | DELETE_P { $$ = CMD_DELETE; }
5032 | INSERT { $$ = CMD_INSERT; }
5036 INSTEAD { $$ = TRUE; }
5037 | ALSO { $$ = FALSE; }
5038 | /*EMPTY*/ { $$ = FALSE; }
5043 DROP RULE name ON qualified_name opt_drop_behavior
5045 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5049 n->removeType = OBJECT_RULE;
5050 n->missing_ok = false;
5053 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5055 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5059 n->removeType = OBJECT_RULE;
5060 n->missing_ok = true;
5066 /*****************************************************************************
5069 * NOTIFY <qualified_name> can appear both in rule bodies and
5070 * as a query-level command
5072 *****************************************************************************/
5074 NotifyStmt: NOTIFY ColId
5076 NotifyStmt *n = makeNode(NotifyStmt);
5077 n->relation = makeNode(RangeVar);
5078 n->relation->relname = $2;
5079 n->relation->schemaname = NULL;
5084 ListenStmt: LISTEN ColId
5086 ListenStmt *n = makeNode(ListenStmt);
5087 n->relation = makeNode(RangeVar);
5088 n->relation->relname = $2;
5089 n->relation->schemaname = NULL;
5097 UnlistenStmt *n = makeNode(UnlistenStmt);
5098 n->relation = makeNode(RangeVar);
5099 n->relation->relname = $2;
5100 n->relation->schemaname = NULL;
5105 UnlistenStmt *n = makeNode(UnlistenStmt);
5106 n->relation = makeNode(RangeVar);
5107 n->relation->relname = "*";
5108 n->relation->schemaname = NULL;
5114 /*****************************************************************************
5118 * BEGIN / COMMIT / ROLLBACK
5119 * (also older versions END / ABORT)
5121 *****************************************************************************/
5124 ABORT_P opt_transaction
5126 TransactionStmt *n = makeNode(TransactionStmt);
5127 n->kind = TRANS_STMT_ROLLBACK;
5131 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5133 TransactionStmt *n = makeNode(TransactionStmt);
5134 n->kind = TRANS_STMT_BEGIN;
5138 | START TRANSACTION transaction_mode_list_or_empty
5140 TransactionStmt *n = makeNode(TransactionStmt);
5141 n->kind = TRANS_STMT_START;
5145 | COMMIT opt_transaction
5147 TransactionStmt *n = makeNode(TransactionStmt);
5148 n->kind = TRANS_STMT_COMMIT;
5152 | END_P opt_transaction
5154 TransactionStmt *n = makeNode(TransactionStmt);
5155 n->kind = TRANS_STMT_COMMIT;
5159 | ROLLBACK opt_transaction
5161 TransactionStmt *n = makeNode(TransactionStmt);
5162 n->kind = TRANS_STMT_ROLLBACK;
5168 TransactionStmt *n = makeNode(TransactionStmt);
5169 n->kind = TRANS_STMT_SAVEPOINT;
5170 n->options = list_make1(makeDefElem("savepoint_name",
5171 (Node *)makeString($2)));
5174 | RELEASE SAVEPOINT ColId
5176 TransactionStmt *n = makeNode(TransactionStmt);
5177 n->kind = TRANS_STMT_RELEASE;
5178 n->options = list_make1(makeDefElem("savepoint_name",
5179 (Node *)makeString($3)));
5184 TransactionStmt *n = makeNode(TransactionStmt);
5185 n->kind = TRANS_STMT_RELEASE;
5186 n->options = list_make1(makeDefElem("savepoint_name",
5187 (Node *)makeString($2)));
5190 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5192 TransactionStmt *n = makeNode(TransactionStmt);
5193 n->kind = TRANS_STMT_ROLLBACK_TO;
5194 n->options = list_make1(makeDefElem("savepoint_name",
5195 (Node *)makeString($5)));
5198 | ROLLBACK opt_transaction TO ColId
5200 TransactionStmt *n = makeNode(TransactionStmt);
5201 n->kind = TRANS_STMT_ROLLBACK_TO;
5202 n->options = list_make1(makeDefElem("savepoint_name",
5203 (Node *)makeString($4)));
5206 | PREPARE TRANSACTION Sconst
5208 TransactionStmt *n = makeNode(TransactionStmt);
5209 n->kind = TRANS_STMT_PREPARE;
5213 | COMMIT PREPARED Sconst
5215 TransactionStmt *n = makeNode(TransactionStmt);
5216 n->kind = TRANS_STMT_COMMIT_PREPARED;
5220 | ROLLBACK PREPARED Sconst
5222 TransactionStmt *n = makeNode(TransactionStmt);
5223 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5229 opt_transaction: WORK {}
5234 transaction_mode_item:
5235 ISOLATION LEVEL iso_level
5236 { $$ = makeDefElem("transaction_isolation",
5237 makeStringConst($3, NULL)); }
5239 { $$ = makeDefElem("transaction_read_only",
5240 makeIntConst(TRUE)); }
5242 { $$ = makeDefElem("transaction_read_only",
5243 makeIntConst(FALSE)); }
5246 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5247 transaction_mode_list:
5248 transaction_mode_item
5249 { $$ = list_make1($1); }
5250 | transaction_mode_list ',' transaction_mode_item
5251 { $$ = lappend($1, $3); }
5252 | transaction_mode_list transaction_mode_item
5253 { $$ = lappend($1, $2); }
5256 transaction_mode_list_or_empty:
5257 transaction_mode_list
5263 /*****************************************************************************
5266 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5267 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5269 *****************************************************************************/
5271 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5272 AS SelectStmt opt_check_option
5274 ViewStmt *n = makeNode(ViewStmt);
5276 n->view->istemp = $2;
5282 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5283 AS SelectStmt opt_check_option
5285 ViewStmt *n = makeNode(ViewStmt);
5287 n->view->istemp = $4;
5296 * We use merged tokens here to avoid creating shift/reduce conflicts against
5297 * a whole lot of other uses of WITH.
5303 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5304 errmsg("WITH CHECK OPTION is not implemented")));
5306 | WITH_CASCADED CHECK OPTION
5309 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5310 errmsg("WITH CHECK OPTION is not implemented")));
5312 | WITH_LOCAL CHECK OPTION
5315 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5316 errmsg("WITH CHECK OPTION is not implemented")));
5318 | /* EMPTY */ { $$ = NIL; }
5321 /*****************************************************************************
5326 *****************************************************************************/
5328 LoadStmt: LOAD file_name
5330 LoadStmt *n = makeNode(LoadStmt);
5337 /*****************************************************************************
5341 *****************************************************************************/
5344 CREATE DATABASE database_name opt_with createdb_opt_list
5346 CreatedbStmt *n = makeNode(CreatedbStmt);
5354 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5355 | /* EMPTY */ { $$ = NIL; }
5359 TABLESPACE opt_equal name
5361 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5363 | TABLESPACE opt_equal DEFAULT
5365 $$ = makeDefElem("tablespace", NULL);
5367 | LOCATION opt_equal Sconst
5369 $$ = makeDefElem("location", (Node *)makeString($3));
5371 | LOCATION opt_equal DEFAULT
5373 $$ = makeDefElem("location", NULL);
5375 | TEMPLATE opt_equal name
5377 $$ = makeDefElem("template", (Node *)makeString($3));
5379 | TEMPLATE opt_equal DEFAULT
5381 $$ = makeDefElem("template", NULL);
5383 | ENCODING opt_equal Sconst
5385 $$ = makeDefElem("encoding", (Node *)makeString($3));
5387 | ENCODING opt_equal Iconst
5389 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5391 | ENCODING opt_equal DEFAULT
5393 $$ = makeDefElem("encoding", NULL);
5395 | CONNECTION LIMIT opt_equal SignedIconst
5397 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5399 | OWNER opt_equal name
5401 $$ = makeDefElem("owner", (Node *)makeString($3));
5403 | OWNER opt_equal DEFAULT
5405 $$ = makeDefElem("owner", NULL);
5410 * Though the equals sign doesn't match other WITH options, pg_dump uses
5411 * equals for backward compatibility, and it doesn't seem worth removing it.
5418 /*****************************************************************************
5422 *****************************************************************************/
5425 ALTER DATABASE database_name opt_with alterdb_opt_list
5427 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5434 AlterDatabaseSetStmt:
5435 ALTER DATABASE database_name SetResetClause
5437 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5446 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5447 | /* EMPTY */ { $$ = NIL; }
5451 CONNECTION LIMIT opt_equal SignedIconst
5453 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5458 /*****************************************************************************
5460 * DROP DATABASE [ IF EXISTS ]
5462 * This is implicitly CASCADE, no need for drop behavior
5463 *****************************************************************************/
5465 DropdbStmt: DROP DATABASE database_name
5467 DropdbStmt *n = makeNode(DropdbStmt);
5469 n->missing_ok = FALSE;
5472 | DROP DATABASE IF_P EXISTS database_name
5474 DropdbStmt *n = makeNode(DropdbStmt);
5476 n->missing_ok = TRUE;
5482 /*****************************************************************************
5484 * Manipulate a domain
5486 *****************************************************************************/
5489 CREATE DOMAIN_P any_name opt_as Typename ColQualList
5491 CreateDomainStmt *n = makeNode(CreateDomainStmt);
5494 n->constraints = $6;
5500 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
5501 ALTER DOMAIN_P any_name alter_column_default
5503 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5509 /* ALTER DOMAIN <domain> DROP NOT NULL */
5510 | ALTER DOMAIN_P any_name DROP NOT NULL_P
5512 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5517 /* ALTER DOMAIN <domain> SET NOT NULL */
5518 | ALTER DOMAIN_P any_name SET NOT NULL_P
5520 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5525 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
5526 | ALTER DOMAIN_P any_name ADD_P TableConstraint
5528 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5534 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
5535 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
5537 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5551 /*****************************************************************************
5553 * Manipulate a text search dictionary or configuration
5555 *****************************************************************************/
5557 AlterTSDictionaryStmt:
5558 ALTER TEXT_P SEARCH DICTIONARY any_name definition
5560 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
5567 AlterTSConfigurationStmt:
5568 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
5570 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5574 n->override = false;
5578 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
5580 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5588 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
5590 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5593 n->dicts = list_make2($9,$11);
5594 n->override = false;
5598 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
5600 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5603 n->dicts = list_make2($11,$13);
5604 n->override = false;
5608 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
5610 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5613 n->missing_ok = false;
5616 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
5618 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5621 n->missing_ok = true;
5627 /*****************************************************************************
5629 * Manipulate a conversion
5631 * CREATE [DEFAULT] CONVERSION <conversion_name>
5632 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
5634 *****************************************************************************/
5636 CreateConversionStmt:
5637 CREATE opt_default CONVERSION_P any_name FOR Sconst
5638 TO Sconst FROM any_name
5640 CreateConversionStmt *n = makeNode(CreateConversionStmt);
5641 n->conversion_name = $4;
5642 n->for_encoding_name = $6;
5643 n->to_encoding_name = $8;
5650 /*****************************************************************************
5653 * CLUSTER <qualified_name> [ USING <index_name> ]
5655 * CLUSTER <index_name> ON <qualified_name> (for pre-8.3)
5657 *****************************************************************************/
5660 CLUSTER qualified_name cluster_index_specification
5662 ClusterStmt *n = makeNode(ClusterStmt);
5669 ClusterStmt *n = makeNode(ClusterStmt);
5671 n->indexname = NULL;
5674 /* kept for pre-8.3 compatibility */
5675 | CLUSTER index_name ON qualified_name
5677 ClusterStmt *n = makeNode(ClusterStmt);
5684 cluster_index_specification:
5685 USING index_name { $$ = $2; }
5686 | /*EMPTY*/ { $$ = NULL; }
5690 /*****************************************************************************
5696 *****************************************************************************/
5698 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
5700 VacuumStmt *n = makeNode(VacuumStmt);
5704 n->freeze_min_age = $3 ? 0 : -1;
5710 | VACUUM opt_full opt_freeze opt_verbose qualified_name
5712 VacuumStmt *n = makeNode(VacuumStmt);
5716 n->freeze_min_age = $3 ? 0 : -1;
5722 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
5724 VacuumStmt *n = (VacuumStmt *) $5;
5727 n->freeze_min_age = $3 ? 0 : -1;
5734 analyze_keyword opt_verbose
5736 VacuumStmt *n = makeNode(VacuumStmt);
5740 n->freeze_min_age = -1;
5746 | analyze_keyword opt_verbose qualified_name opt_name_list
5748 VacuumStmt *n = makeNode(VacuumStmt);
5752 n->freeze_min_age = -1;
5762 | ANALYSE /* British */ {}
5766 VERBOSE { $$ = TRUE; }
5767 | /*EMPTY*/ { $$ = FALSE; }
5770 opt_full: FULL { $$ = TRUE; }
5771 | /*EMPTY*/ { $$ = FALSE; }
5774 opt_freeze: FREEZE { $$ = TRUE; }
5775 | /*EMPTY*/ { $$ = FALSE; }
5779 '(' name_list ')' { $$ = $2; }
5780 | /*EMPTY*/ { $$ = NIL; }
5784 /*****************************************************************************
5787 * EXPLAIN [ANALYZE] [VERBOSE] query
5789 *****************************************************************************/
5791 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
5793 ExplainStmt *n = makeNode(ExplainStmt);
5807 | ExecuteStmt /* by default all are $$=$1 */
5811 analyze_keyword { $$ = TRUE; }
5812 | /* EMPTY */ { $$ = FALSE; }
5815 /*****************************************************************************
5818 * PREPARE <plan_name> [(args, ...)] AS <query>
5820 *****************************************************************************/
5822 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
5824 PrepareStmt *n = makeNode(PrepareStmt);
5832 prep_type_clause: '(' type_list ')' { $$ = $2; }
5833 | /* EMPTY */ { $$ = NIL; }
5840 | DeleteStmt /* by default all are $$=$1 */
5843 /*****************************************************************************
5845 * EXECUTE <plan_name> [(params, ...)]
5846 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
5848 *****************************************************************************/
5850 ExecuteStmt: EXECUTE name execute_param_clause
5852 ExecuteStmt *n = makeNode(ExecuteStmt);
5858 | CREATE OptTemp TABLE create_as_target AS
5859 EXECUTE name execute_param_clause
5861 ExecuteStmt *n = makeNode(ExecuteStmt);
5864 $4->rel->istemp = $2;
5868 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5869 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
5870 /* ... because it's not implemented, but it could be */
5875 execute_param_clause: '(' expr_list ')' { $$ = $2; }
5876 | /* EMPTY */ { $$ = NIL; }
5879 /*****************************************************************************
5882 * DEALLOCATE [PREPARE] <plan_name>
5884 *****************************************************************************/
5886 DeallocateStmt: DEALLOCATE name
5888 DeallocateStmt *n = makeNode(DeallocateStmt);
5892 | DEALLOCATE PREPARE name
5894 DeallocateStmt *n = makeNode(DeallocateStmt);
5900 DeallocateStmt *n = makeNode(DeallocateStmt);
5904 | DEALLOCATE PREPARE ALL
5906 DeallocateStmt *n = makeNode(DeallocateStmt);
5912 /*****************************************************************************
5917 *****************************************************************************/
5920 INSERT INTO qualified_name insert_rest returning_clause
5923 $4->returningList = $5;
5931 $$ = makeNode(InsertStmt);
5933 $$->selectStmt = $1;
5935 | '(' insert_column_list ')' SelectStmt
5937 $$ = makeNode(InsertStmt);
5939 $$->selectStmt = $4;
5943 $$ = makeNode(InsertStmt);
5945 $$->selectStmt = NULL;
5951 { $$ = list_make1($1); }
5952 | insert_column_list ',' insert_column_item
5953 { $$ = lappend($1, $3); }
5957 ColId opt_indirection
5959 $$ = makeNode(ResTarget);
5961 $$->indirection = $2;
5968 RETURNING target_list { $$ = $2; }
5969 | /* EMPTY */ { $$ = NIL; }
5973 /*****************************************************************************
5978 *****************************************************************************/
5980 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
5981 using_clause where_or_current_clause returning_clause
5983 DeleteStmt *n = makeNode(DeleteStmt);
5985 n->usingClause = $4;
5986 n->whereClause = $5;
5987 n->returningList = $6;
5993 USING from_list { $$ = $2; }
5994 | /*EMPTY*/ { $$ = NIL; }
5997 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
5999 LockStmt *n = makeNode(LockStmt);
6008 opt_lock: IN_P lock_type MODE { $$ = $2; }
6009 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6012 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6013 | ROW SHARE { $$ = RowShareLock; }
6014 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6015 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6016 | SHARE { $$ = ShareLock; }
6017 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6018 | EXCLUSIVE { $$ = ExclusiveLock; }
6019 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6022 opt_nowait: NOWAIT { $$ = TRUE; }
6023 | /*EMPTY*/ { $$ = FALSE; }
6027 /*****************************************************************************
6030 * UpdateStmt (UPDATE)
6032 *****************************************************************************/
6034 UpdateStmt: UPDATE relation_expr_opt_alias
6037 where_or_current_clause
6040 UpdateStmt *n = makeNode(UpdateStmt);
6044 n->whereClause = $6;
6045 n->returningList = $7;
6051 set_clause { $$ = $1; }
6052 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6056 single_set_clause { $$ = list_make1($1); }
6057 | multiple_set_clause { $$ = $1; }
6061 set_target '=' ctext_expr
6064 $$->val = (Node *) $3;
6068 multiple_set_clause:
6069 '(' set_target_list ')' '=' ctext_row
6075 * Break the ctext_row apart, merge individual expressions
6076 * into the destination ResTargets. XXX this approach
6077 * cannot work for general row expressions as sources.
6079 if (list_length($2) != list_length($5))
6081 (errcode(ERRCODE_SYNTAX_ERROR),
6082 errmsg("number of columns does not match number of values")));
6083 forboth(col_cell, $2, val_cell, $5)
6085 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6086 Node *res_val = (Node *) lfirst(val_cell);
6088 res_col->val = res_val;
6096 ColId opt_indirection
6098 $$ = makeNode(ResTarget);
6100 $$->indirection = $2;
6101 $$->val = NULL; /* upper production sets this */
6107 set_target { $$ = list_make1($1); }
6108 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6112 /*****************************************************************************
6117 *****************************************************************************/
6118 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6120 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6122 /* currently we always set FAST_PLAN option */
6123 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6129 cursor_options: /*EMPTY*/ { $$ = 0; }
6130 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6131 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6132 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6133 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6136 opt_hold: /* EMPTY */ { $$ = 0; }
6137 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6138 | WITHOUT HOLD { $$ = 0; }
6141 /*****************************************************************************
6146 *****************************************************************************/
6148 /* A complete SELECT statement looks like this.
6150 * The rule returns either a single SelectStmt node or a tree of them,
6151 * representing a set-operation tree.
6153 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6154 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6155 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6156 * To resolve the ambiguity, we are careful to define the grammar so that
6157 * the decision is staved off as long as possible: as long as we can keep
6158 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6159 * it's no longer possible to do that will we decide that parens belong to
6160 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6161 * parentheses are treated as part of the sub-select. The necessity of doing
6162 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6163 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6164 * SELECT viewpoint when we see the UNION.
6166 * This approach is implemented by defining a nonterminal select_with_parens,
6167 * which represents a SELECT with at least one outer layer of parentheses,
6168 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6169 * in the expression grammar. We will then have shift-reduce conflicts
6170 * which we can resolve in favor of always treating '(' <select> ')' as
6171 * a select_with_parens. To resolve the conflicts, the productions that
6172 * conflict with the select_with_parens productions are manually given
6173 * precedences lower than the precedence of ')', thereby ensuring that we
6174 * shift ')' (and then reduce to select_with_parens) rather than trying to
6175 * reduce the inner <select> nonterminal to something else. We use UMINUS
6176 * precedence for this, which is a fairly arbitrary choice.
6178 * To be able to define select_with_parens itself without ambiguity, we need
6179 * a nonterminal select_no_parens that represents a SELECT structure with no
6180 * outermost parentheses. This is a little bit tedious, but it works.
6182 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6183 * with or without outer parentheses.
6186 SelectStmt: select_no_parens %prec UMINUS
6187 | select_with_parens %prec UMINUS
6191 '(' select_no_parens ')' { $$ = $2; }
6192 | '(' select_with_parens ')' { $$ = $2; }
6196 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6197 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6198 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6202 simple_select { $$ = $1; }
6203 | select_clause sort_clause
6205 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6209 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6211 insertSelectOptions((SelectStmt *) $1, $2, $3,
6212 list_nth($4, 0), list_nth($4, 1));
6215 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6217 insertSelectOptions((SelectStmt *) $1, $2, $4,
6218 list_nth($3, 0), list_nth($3, 1));
6224 simple_select { $$ = $1; }
6225 | select_with_parens { $$ = $1; }
6229 * This rule parses SELECT statements that can appear within set operations,
6230 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6231 * the ordering of the set operations. Without '(' and ')' we want the
6232 * operations to be ordered per the precedence specs at the head of this file.
6234 * As with select_no_parens, simple_select cannot have outer parentheses,
6235 * but can have parenthesized subclauses.
6237 * Note that sort clauses cannot be included at this level --- SQL92 requires
6238 * SELECT foo UNION SELECT bar ORDER BY baz
6240 * (SELECT foo UNION SELECT bar) ORDER BY baz
6242 * SELECT foo UNION (SELECT bar ORDER BY baz)
6243 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
6244 * as part of the select_no_parens production, not simple_select.
6245 * This does not limit functionality, because you can reintroduce sort and
6246 * limit clauses inside parentheses.
6248 * NOTE: only the leftmost component SelectStmt should have INTO.
6249 * However, this is not checked by the grammar; parse analysis must check it.
6252 SELECT opt_distinct target_list
6253 into_clause from_clause where_clause
6254 group_clause having_clause
6256 SelectStmt *n = makeNode(SelectStmt);
6257 n->distinctClause = $2;
6261 n->whereClause = $6;
6262 n->groupClause = $7;
6263 n->havingClause = $8;
6266 | values_clause { $$ = $1; }
6267 | select_clause UNION opt_all select_clause
6269 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6271 | select_clause INTERSECT opt_all select_clause
6273 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6275 | select_clause EXCEPT opt_all select_clause
6277 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6282 INTO OptTempTableName
6284 $$ = makeNode(IntoClause);
6288 $$->onCommit = ONCOMMIT_NOOP;
6289 $$->tableSpaceName = NULL;
6296 * Redundancy here is needed to avoid shift/reduce conflicts,
6297 * since TEMP is not a reserved word. See also OptTemp.
6300 TEMPORARY opt_table qualified_name
6305 | TEMP opt_table qualified_name
6310 | LOCAL TEMPORARY opt_table qualified_name
6315 | LOCAL TEMP opt_table qualified_name
6320 | GLOBAL TEMPORARY opt_table qualified_name
6325 | GLOBAL TEMP opt_table qualified_name
6330 | TABLE qualified_name
6346 opt_all: ALL { $$ = TRUE; }
6347 | DISTINCT { $$ = FALSE; }
6348 | /*EMPTY*/ { $$ = FALSE; }
6351 /* We use (NIL) as a placeholder to indicate that all target expressions
6352 * should be placed in the DISTINCT list during parsetree analysis.
6355 DISTINCT { $$ = list_make1(NIL); }
6356 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6358 | /*EMPTY*/ { $$ = NIL; }
6362 sort_clause { $$ = $1;}
6363 | /*EMPTY*/ { $$ = NIL; }
6367 ORDER BY sortby_list { $$ = $3; }
6371 sortby { $$ = list_make1($1); }
6372 | sortby_list ',' sortby { $$ = lappend($1, $3); }
6375 sortby: a_expr USING qual_all_Op opt_nulls_order
6377 $$ = makeNode(SortBy);
6379 $$->sortby_dir = SORTBY_USING;
6380 $$->sortby_nulls = $4;
6383 | a_expr opt_asc_desc opt_nulls_order
6385 $$ = makeNode(SortBy);
6387 $$->sortby_dir = $2;
6388 $$->sortby_nulls = $3;
6395 LIMIT select_limit_value OFFSET select_offset_value
6396 { $$ = list_make2($4, $2); }
6397 | OFFSET select_offset_value LIMIT select_limit_value
6398 { $$ = list_make2($2, $4); }
6399 | LIMIT select_limit_value
6400 { $$ = list_make2(NULL, $2); }
6401 | OFFSET select_offset_value
6402 { $$ = list_make2($2, NULL); }
6403 | LIMIT select_limit_value ',' select_offset_value
6405 /* Disabled because it was too confusing, bjm 2002-02-18 */
6407 (errcode(ERRCODE_SYNTAX_ERROR),
6408 errmsg("LIMIT #,# syntax is not supported"),
6409 errhint("Use separate LIMIT and OFFSET clauses.")));
6414 select_limit { $$ = $1; }
6416 { $$ = list_make2(NULL,NULL); }
6423 /* LIMIT ALL is represented as a NULL constant */
6424 A_Const *n = makeNode(A_Const);
6425 n->val.type = T_Null;
6430 select_offset_value:
6435 GROUP_P BY expr_list { $$ = $3; }
6436 | /*EMPTY*/ { $$ = NIL; }
6440 HAVING a_expr { $$ = $2; }
6441 | /*EMPTY*/ { $$ = NULL; }
6445 for_locking_items { $$ = $1; }
6446 | FOR READ ONLY { $$ = NIL; }
6449 opt_for_locking_clause:
6450 for_locking_clause { $$ = $1; }
6451 | /* EMPTY */ { $$ = NIL; }
6455 for_locking_item { $$ = list_make1($1); }
6456 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
6460 FOR UPDATE locked_rels_list opt_nowait
6462 LockingClause *n = makeNode(LockingClause);
6464 n->forUpdate = TRUE;
6468 | FOR SHARE locked_rels_list opt_nowait
6470 LockingClause *n = makeNode(LockingClause);
6472 n->forUpdate = FALSE;
6479 OF name_list { $$ = $2; }
6480 | /* EMPTY */ { $$ = NIL; }
6487 SelectStmt *n = makeNode(SelectStmt);
6488 n->valuesLists = list_make1($2);
6491 | values_clause ',' ctext_row
6493 SelectStmt *n = (SelectStmt *) $1;
6494 n->valuesLists = lappend(n->valuesLists, $3);
6500 /*****************************************************************************
6502 * clauses common to all Optimizable Stmts:
6503 * from_clause - allow list of both JOIN expressions and table names
6504 * where_clause - qualifications for joins or restrictions
6506 *****************************************************************************/
6509 FROM from_list { $$ = $2; }
6510 | /*EMPTY*/ { $$ = NIL; }
6514 table_ref { $$ = list_make1($1); }
6515 | from_list ',' table_ref { $$ = lappend($1, $3); }
6519 * table_ref is where an alias clause can be attached. Note we cannot make
6520 * alias_clause have an empty production because that causes parse conflicts
6521 * between table_ref := '(' joined_table ')' alias_clause
6522 * and joined_table := '(' joined_table ')'. So, we must have the
6523 * redundant-looking productions here instead.
6525 table_ref: relation_expr
6529 | relation_expr alias_clause
6536 RangeFunction *n = makeNode(RangeFunction);
6537 n->funccallnode = $1;
6538 n->coldeflist = NIL;
6541 | func_table alias_clause
6543 RangeFunction *n = makeNode(RangeFunction);
6544 n->funccallnode = $1;
6546 n->coldeflist = NIL;
6549 | func_table AS '(' TableFuncElementList ')'
6551 RangeFunction *n = makeNode(RangeFunction);
6552 n->funccallnode = $1;
6556 | func_table AS ColId '(' TableFuncElementList ')'
6558 RangeFunction *n = makeNode(RangeFunction);
6559 Alias *a = makeNode(Alias);
6560 n->funccallnode = $1;
6566 | func_table ColId '(' TableFuncElementList ')'
6568 RangeFunction *n = makeNode(RangeFunction);
6569 Alias *a = makeNode(Alias);
6570 n->funccallnode = $1;
6576 | select_with_parens
6579 * The SQL spec does not permit a subselect
6580 * (<derived_table>) without an alias clause,
6581 * so we don't either. This avoids the problem
6582 * of needing to invent a unique refname for it.
6583 * That could be surmounted if there's sufficient
6584 * popular demand, but for now let's just implement
6585 * the spec and see if anyone complains.
6586 * However, it does seem like a good idea to emit
6587 * an error message that's better than "syntax error".
6589 if (IsA($1, SelectStmt) &&
6590 ((SelectStmt *) $1)->valuesLists)
6592 (errcode(ERRCODE_SYNTAX_ERROR),
6593 errmsg("VALUES in FROM must have an alias"),
6594 errhint("For example, FROM (VALUES ...) [AS] foo.")));
6597 (errcode(ERRCODE_SYNTAX_ERROR),
6598 errmsg("subquery in FROM must have an alias"),
6599 errhint("For example, FROM (SELECT ...) [AS] foo.")));
6602 | select_with_parens alias_clause
6604 RangeSubselect *n = makeNode(RangeSubselect);
6613 | '(' joined_table ')' alias_clause
6622 * It may seem silly to separate joined_table from table_ref, but there is
6623 * method in SQL92's madness: if you don't do it this way you get reduce-
6624 * reduce conflicts, because it's not clear to the parser generator whether
6625 * to expect alias_clause after ')' or not. For the same reason we must
6626 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
6627 * join_type to expand to empty; if we try it, the parser generator can't
6628 * figure out when to reduce an empty join_type right after table_ref.
6630 * Note that a CROSS JOIN is the same as an unqualified
6631 * INNER JOIN, and an INNER JOIN/ON has the same shape
6632 * but a qualification expression to limit membership.
6633 * A NATURAL JOIN implicitly matches column names between
6634 * tables and the shape is determined by which columns are
6635 * in common. We'll collect columns during the later transformations.
6639 '(' joined_table ')'
6643 | table_ref CROSS JOIN table_ref
6645 /* CROSS JOIN is same as unqualified inner join */
6646 JoinExpr *n = makeNode(JoinExpr);
6647 n->jointype = JOIN_INNER;
6648 n->isNatural = FALSE;
6655 | table_ref join_type JOIN table_ref join_qual
6657 JoinExpr *n = makeNode(JoinExpr);
6659 n->isNatural = FALSE;
6662 if ($5 != NULL && IsA($5, List))
6663 n->using = (List *) $5; /* USING clause */
6665 n->quals = $5; /* ON clause */
6668 | table_ref JOIN table_ref join_qual
6670 /* letting join_type reduce to empty doesn't work */
6671 JoinExpr *n = makeNode(JoinExpr);
6672 n->jointype = JOIN_INNER;
6673 n->isNatural = FALSE;
6676 if ($4 != NULL && IsA($4, List))
6677 n->using = (List *) $4; /* USING clause */
6679 n->quals = $4; /* ON clause */
6682 | table_ref NATURAL join_type JOIN table_ref
6684 JoinExpr *n = makeNode(JoinExpr);
6686 n->isNatural = TRUE;
6689 n->using = NIL; /* figure out which columns later... */
6690 n->quals = NULL; /* fill later */
6693 | table_ref NATURAL JOIN table_ref
6695 /* letting join_type reduce to empty doesn't work */
6696 JoinExpr *n = makeNode(JoinExpr);
6697 n->jointype = JOIN_INNER;
6698 n->isNatural = TRUE;
6701 n->using = NIL; /* figure out which columns later... */
6702 n->quals = NULL; /* fill later */
6708 AS ColId '(' name_list ')'
6710 $$ = makeNode(Alias);
6716 $$ = makeNode(Alias);
6719 | ColId '(' name_list ')'
6721 $$ = makeNode(Alias);
6727 $$ = makeNode(Alias);
6732 join_type: FULL join_outer { $$ = JOIN_FULL; }
6733 | LEFT join_outer { $$ = JOIN_LEFT; }
6734 | RIGHT join_outer { $$ = JOIN_RIGHT; }
6735 | INNER_P { $$ = JOIN_INNER; }
6738 /* OUTER is just noise... */
6739 join_outer: OUTER_P { $$ = NULL; }
6740 | /*EMPTY*/ { $$ = NULL; }
6743 /* JOIN qualification clauses
6744 * Possibilities are:
6745 * USING ( column list ) allows only unqualified column names,
6746 * which must match between tables.
6747 * ON expr allows more general qualifications.
6749 * We return USING as a List node, while an ON-expr will not be a List.
6752 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
6753 | ON a_expr { $$ = $2; }
6760 /* default inheritance */
6762 $$->inhOpt = INH_DEFAULT;
6765 | qualified_name '*'
6767 /* inheritance query */
6769 $$->inhOpt = INH_YES;
6772 | ONLY qualified_name
6774 /* no inheritance */
6776 $$->inhOpt = INH_NO;
6779 | ONLY '(' qualified_name ')'
6781 /* no inheritance, SQL99-style syntax */
6783 $$->inhOpt = INH_NO;
6790 * Given "UPDATE foo set set ...", we have to decide without looking any
6791 * further ahead whether the first "set" is an alias or the UPDATE's SET
6792 * keyword. Since "set" is allowed as a column name both interpretations
6793 * are feasible. We resolve the shift/reduce conflict by giving the first
6794 * relation_expr_opt_alias production a higher precedence than the SET token
6795 * has, causing the parser to prefer to reduce, in effect assuming that the
6796 * SET is not an alias.
6798 relation_expr_opt_alias: relation_expr %prec UMINUS
6802 | relation_expr ColId
6804 Alias *alias = makeNode(Alias);
6805 alias->aliasname = $2;
6809 | relation_expr AS ColId
6811 Alias *alias = makeNode(Alias);
6812 alias->aliasname = $3;
6819 func_table: func_expr { $$ = $1; }
6824 WHERE a_expr { $$ = $2; }
6825 | /*EMPTY*/ { $$ = NULL; }
6828 /* variant for UPDATE and DELETE */
6829 where_or_current_clause:
6830 WHERE a_expr { $$ = $2; }
6831 | WHERE CURRENT_P OF name
6833 CurrentOfExpr *n = makeNode(CurrentOfExpr);
6834 /* cvarno is filled in by parse analysis */
6835 n->cursor_name = $4;
6836 n->cursor_param = 0;
6839 | WHERE CURRENT_P OF PARAM
6841 CurrentOfExpr *n = makeNode(CurrentOfExpr);
6842 /* cvarno is filled in by parse analysis */
6843 n->cursor_name = NULL;
6844 n->cursor_param = $4;
6847 | /*EMPTY*/ { $$ = NULL; }
6851 TableFuncElementList:
6854 $$ = list_make1($1);
6856 | TableFuncElementList ',' TableFuncElement
6858 $$ = lappend($1, $3);
6862 TableFuncElement: ColId Typename
6864 ColumnDef *n = makeNode(ColumnDef);
6867 n->constraints = NIL;
6873 /*****************************************************************************
6876 * SQL92 introduces a large amount of type-specific syntax.
6877 * Define individual clauses to handle these cases, and use
6878 * the generic case to handle regular type-extensible Postgres syntax.
6879 * - thomas 1997-10-10
6881 *****************************************************************************/
6883 Typename: SimpleTypename opt_array_bounds
6886 $$->arrayBounds = $2;
6888 | SETOF SimpleTypename opt_array_bounds
6891 $$->arrayBounds = $3;
6894 | SimpleTypename ARRAY '[' Iconst ']'
6896 /* SQL99's redundant syntax */
6898 $$->arrayBounds = list_make1(makeInteger($4));
6900 | SETOF SimpleTypename ARRAY '[' Iconst ']'
6902 /* SQL99's redundant syntax */
6904 $$->arrayBounds = list_make1(makeInteger($5));
6910 opt_array_bounds '[' ']'
6911 { $$ = lappend($1, makeInteger(-1)); }
6912 | opt_array_bounds '[' Iconst ']'
6913 { $$ = lappend($1, makeInteger($3)); }
6919 GenericType { $$ = $1; }
6920 | Numeric { $$ = $1; }
6922 | Character { $$ = $1; }
6923 | ConstDatetime { $$ = $1; }
6924 | ConstInterval opt_interval
6927 if ($2 != INTERVAL_FULL_RANGE)
6928 $$->typmods = list_make1(makeIntConst($2));
6930 | ConstInterval '(' Iconst ')' opt_interval
6933 $$->typmods = list_make2(makeIntConst($5),
6938 /* We have a separate ConstTypename to allow defaulting fixed-length
6939 * types such as CHAR() and BIT() to an unspecified length.
6940 * SQL9x requires that these default to a length of one, but this
6941 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
6942 * where there is an obvious better choice to make.
6943 * Note that ConstInterval is not included here since it must
6944 * be pushed up higher in the rules to accomodate the postfix
6945 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
6946 * the generic-type-name case in AExprConst to avoid premature
6947 * reduce/reduce conflicts against function names.
6950 Numeric { $$ = $1; }
6951 | ConstBit { $$ = $1; }
6952 | ConstCharacter { $$ = $1; }
6953 | ConstDatetime { $$ = $1; }
6957 * GenericType covers all type names that don't have special syntax mandated
6958 * by the standard, including qualified names. We also allow type modifiers.
6959 * To avoid parsing conflicts against function invocations, the modifiers
6960 * have to be shown as expr_list here, but parse analysis will only accept
6961 * constants for them.
6964 type_function_name opt_type_modifiers
6966 $$ = makeTypeName($1);
6970 | type_function_name attrs opt_type_modifiers
6972 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
6978 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
6979 | /* EMPTY */ { $$ = NIL; }
6983 * SQL92 numeric data types
6987 $$ = SystemTypeName("int4");
6992 $$ = SystemTypeName("int4");
6997 $$ = SystemTypeName("int2");
7002 $$ = SystemTypeName("int8");
7007 $$ = SystemTypeName("float4");
7015 | DOUBLE_P PRECISION
7017 $$ = SystemTypeName("float8");
7020 | DECIMAL_P opt_type_modifiers
7022 $$ = SystemTypeName("numeric");
7026 | DEC opt_type_modifiers
7028 $$ = SystemTypeName("numeric");
7032 | NUMERIC opt_type_modifiers
7034 $$ = SystemTypeName("numeric");
7040 $$ = SystemTypeName("bool");
7045 opt_float: '(' Iconst ')'
7048 * Check FLOAT() precision limits assuming IEEE floating
7049 * types - thomas 1997-09-18
7053 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7054 errmsg("precision for type float must be at least 1 bit")));
7056 $$ = SystemTypeName("float4");
7058 $$ = SystemTypeName("float8");
7061 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7062 errmsg("precision for type float must be less than 54 bits")));
7066 $$ = SystemTypeName("float8");
7071 * SQL92 bit-field data types
7072 * The following implements BIT() and BIT VARYING().
7084 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7085 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7086 ConstBit: BitWithLength
7098 BIT opt_varying '(' expr_list ')'
7102 typname = $2 ? "varbit" : "bit";
7103 $$ = SystemTypeName(typname);
7112 /* bit defaults to bit(1), varbit to no limit */
7115 $$ = SystemTypeName("varbit");
7119 $$ = SystemTypeName("bit");
7120 $$->typmods = list_make1(makeIntConst(1));
7128 * SQL92 character data types
7129 * The following implements CHAR() and VARCHAR().
7131 Character: CharacterWithLength
7135 | CharacterWithoutLength
7141 ConstCharacter: CharacterWithLength
7145 | CharacterWithoutLength
7147 /* Length was not specified so allow to be unrestricted.
7148 * This handles problems with fixed-length (bpchar) strings
7149 * which in column definitions must default to a length
7150 * of one, but should not be constrained if the length
7151 * was not specified.
7158 CharacterWithLength: character '(' Iconst ')' opt_charset
7160 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7164 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7171 $$ = SystemTypeName($1);
7172 $$->typmods = list_make1(makeIntConst($3));
7177 CharacterWithoutLength: character opt_charset
7179 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7183 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7190 $$ = SystemTypeName($1);
7192 /* char defaults to char(1), varchar to no limit */
7193 if (strcmp($1, "char") == 0)
7194 $$->typmods = list_make1(makeIntConst(1));
7200 character: CHARACTER opt_varying
7201 { $$ = $2 ? "varchar": "char"; }
7202 | CHAR_P opt_varying
7203 { $$ = $2 ? "varchar": "char"; }
7206 | NATIONAL CHARACTER opt_varying
7207 { $$ = $3 ? "varchar": "char"; }
7208 | NATIONAL CHAR_P opt_varying
7209 { $$ = $3 ? "varchar": "char"; }
7211 { $$ = $2 ? "varchar": "char"; }
7215 VARYING { $$ = TRUE; }
7216 | /*EMPTY*/ { $$ = FALSE; }
7220 CHARACTER SET ColId { $$ = $3; }
7221 | /*EMPTY*/ { $$ = NULL; }
7225 * SQL92 date/time types
7228 TIMESTAMP '(' Iconst ')' opt_timezone
7231 $$ = SystemTypeName("timestamptz");
7233 $$ = SystemTypeName("timestamp");
7234 /* XXX the timezone field seems to be unused
7235 * - thomas 2001-09-06
7238 $$->typmods = list_make1(makeIntConst($3));
7241 | TIMESTAMP opt_timezone
7244 $$ = SystemTypeName("timestamptz");
7246 $$ = SystemTypeName("timestamp");
7247 /* XXX the timezone field seems to be unused
7248 * - thomas 2001-09-06
7253 | TIME '(' Iconst ')' opt_timezone
7256 $$ = SystemTypeName("timetz");
7258 $$ = SystemTypeName("time");
7259 $$->typmods = list_make1(makeIntConst($3));
7265 $$ = SystemTypeName("timetz");
7267 $$ = SystemTypeName("time");
7275 $$ = SystemTypeName("interval");
7281 WITH TIME ZONE { $$ = TRUE; }
7282 | WITHOUT TIME ZONE { $$ = FALSE; }
7283 | /*EMPTY*/ { $$ = FALSE; }
7287 YEAR_P { $$ = INTERVAL_MASK(YEAR); }
7288 | MONTH_P { $$ = INTERVAL_MASK(MONTH); }
7289 | DAY_P { $$ = INTERVAL_MASK(DAY); }
7290 | HOUR_P { $$ = INTERVAL_MASK(HOUR); }
7291 | MINUTE_P { $$ = INTERVAL_MASK(MINUTE); }
7292 | SECOND_P { $$ = INTERVAL_MASK(SECOND); }
7294 { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
7296 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
7298 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
7299 | INTERVAL_MASK(MINUTE); }
7301 { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
7302 | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
7303 | HOUR_P TO MINUTE_P
7304 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
7305 | HOUR_P TO SECOND_P
7306 { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
7307 | INTERVAL_MASK(SECOND); }
7308 | MINUTE_P TO SECOND_P
7309 { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
7310 | /*EMPTY*/ { $$ = INTERVAL_FULL_RANGE; }
7314 /*****************************************************************************
7316 * expression grammar
7318 *****************************************************************************/
7321 * General expressions
7322 * This is the heart of the expression syntax.
7324 * We have two expression types: a_expr is the unrestricted kind, and
7325 * b_expr is a subset that must be used in some places to avoid shift/reduce
7326 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
7327 * because that use of AND conflicts with AND as a boolean operator. So,
7328 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
7330 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
7331 * always be used by surrounding it with parens.
7333 * c_expr is all the productions that are common to a_expr and b_expr;
7334 * it's factored out just to eliminate redundant coding.
7336 a_expr: c_expr { $$ = $1; }
7337 | a_expr TYPECAST Typename
7338 { $$ = makeTypeCast($1, $3); }
7339 | a_expr AT TIME ZONE a_expr
7341 FuncCall *n = makeNode(FuncCall);
7342 n->funcname = SystemFuncName("timezone");
7343 n->args = list_make2($5, $1);
7344 n->agg_star = FALSE;
7345 n->agg_distinct = FALSE;
7350 * These operators must be called out explicitly in order to make use
7351 * of yacc/bison's automatic operator-precedence handling. All other
7352 * operator names are handled by the generic productions using "Op",
7353 * below; and all those operators will have the same precedence.
7355 * If you add more explicitly-known operators, be sure to add them
7356 * also to b_expr and to the MathOp list above.
7358 | '+' a_expr %prec UMINUS
7359 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7360 | '-' a_expr %prec UMINUS
7361 { $$ = doNegate($2, @1); }
7363 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7365 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7367 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7369 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7371 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7373 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7375 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7377 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7379 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7381 | a_expr qual_Op a_expr %prec Op
7382 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7383 | qual_Op a_expr %prec Op
7384 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7385 | a_expr qual_Op %prec POSTFIXOP
7386 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7389 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
7391 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
7393 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
7395 | a_expr LIKE a_expr
7396 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
7397 | a_expr LIKE a_expr ESCAPE a_expr
7399 FuncCall *n = makeNode(FuncCall);
7400 n->funcname = SystemFuncName("like_escape");
7401 n->args = list_make2($3, $5);
7402 n->agg_star = FALSE;
7403 n->agg_distinct = FALSE;
7405 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
7407 | a_expr NOT LIKE a_expr
7408 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
7409 | a_expr NOT LIKE a_expr ESCAPE a_expr
7411 FuncCall *n = makeNode(FuncCall);
7412 n->funcname = SystemFuncName("like_escape");
7413 n->args = list_make2($4, $6);
7414 n->agg_star = FALSE;
7415 n->agg_distinct = FALSE;
7417 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
7419 | a_expr ILIKE a_expr
7420 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
7421 | a_expr ILIKE a_expr ESCAPE a_expr
7423 FuncCall *n = makeNode(FuncCall);
7424 n->funcname = SystemFuncName("like_escape");
7425 n->args = list_make2($3, $5);
7426 n->agg_star = FALSE;
7427 n->agg_distinct = FALSE;
7429 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
7431 | a_expr NOT ILIKE a_expr
7432 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
7433 | a_expr NOT ILIKE a_expr ESCAPE a_expr
7435 FuncCall *n = makeNode(FuncCall);
7436 n->funcname = SystemFuncName("like_escape");
7437 n->args = list_make2($4, $6);
7438 n->agg_star = FALSE;
7439 n->agg_distinct = FALSE;
7441 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
7444 | a_expr SIMILAR TO a_expr %prec SIMILAR
7446 A_Const *c = makeNode(A_Const);
7447 FuncCall *n = makeNode(FuncCall);
7448 c->val.type = T_Null;
7449 n->funcname = SystemFuncName("similar_escape");
7450 n->args = list_make2($4, (Node *) c);
7451 n->agg_star = FALSE;
7452 n->agg_distinct = FALSE;
7454 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7456 | a_expr SIMILAR TO a_expr ESCAPE a_expr
7458 FuncCall *n = makeNode(FuncCall);
7459 n->funcname = SystemFuncName("similar_escape");
7460 n->args = list_make2($4, $6);
7461 n->agg_star = FALSE;
7462 n->agg_distinct = FALSE;
7464 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7466 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
7468 A_Const *c = makeNode(A_Const);
7469 FuncCall *n = makeNode(FuncCall);
7470 c->val.type = T_Null;
7471 n->funcname = SystemFuncName("similar_escape");
7472 n->args = list_make2($5, (Node *) c);
7473 n->agg_star = FALSE;
7474 n->agg_distinct = FALSE;
7476 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7478 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
7480 FuncCall *n = makeNode(FuncCall);
7481 n->funcname = SystemFuncName("similar_escape");
7482 n->args = list_make2($5, $7);
7483 n->agg_star = FALSE;
7484 n->agg_distinct = FALSE;
7486 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7490 * Define SQL92-style Null test clause.
7491 * Allow two forms described in the standard:
7494 * Allow two SQL extensions
7500 NullTest *n = makeNode(NullTest);
7501 n->arg = (Expr *) $1;
7502 n->nulltesttype = IS_NULL;
7507 NullTest *n = makeNode(NullTest);
7508 n->arg = (Expr *) $1;
7509 n->nulltesttype = IS_NULL;
7512 | a_expr IS NOT NULL_P
7514 NullTest *n = makeNode(NullTest);
7515 n->arg = (Expr *) $1;
7516 n->nulltesttype = IS_NOT_NULL;
7521 NullTest *n = makeNode(NullTest);
7522 n->arg = (Expr *) $1;
7523 n->nulltesttype = IS_NOT_NULL;
7528 $$ = (Node *)makeOverlaps($1, $3, @2);
7532 BooleanTest *b = makeNode(BooleanTest);
7533 b->arg = (Expr *) $1;
7534 b->booltesttype = IS_TRUE;
7537 | a_expr IS NOT TRUE_P
7539 BooleanTest *b = makeNode(BooleanTest);
7540 b->arg = (Expr *) $1;
7541 b->booltesttype = IS_NOT_TRUE;
7546 BooleanTest *b = makeNode(BooleanTest);
7547 b->arg = (Expr *) $1;
7548 b->booltesttype = IS_FALSE;
7551 | a_expr IS NOT FALSE_P
7553 BooleanTest *b = makeNode(BooleanTest);
7554 b->arg = (Expr *) $1;
7555 b->booltesttype = IS_NOT_FALSE;
7560 BooleanTest *b = makeNode(BooleanTest);
7561 b->arg = (Expr *) $1;
7562 b->booltesttype = IS_UNKNOWN;
7565 | a_expr IS NOT UNKNOWN
7567 BooleanTest *b = makeNode(BooleanTest);
7568 b->arg = (Expr *) $1;
7569 b->booltesttype = IS_NOT_UNKNOWN;
7572 | a_expr IS DISTINCT FROM a_expr %prec IS
7574 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7576 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
7578 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7579 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
7584 | a_expr IS OF '(' type_list ')' %prec IS
7586 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7588 | a_expr IS NOT OF '(' type_list ')' %prec IS
7590 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7592 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7594 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7595 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7596 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7599 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7601 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7602 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7603 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7606 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7608 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7609 (Node *) makeA_Expr(AEXPR_AND, NIL,
7610 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7611 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7613 (Node *) makeA_Expr(AEXPR_AND, NIL,
7614 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
7615 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
7619 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7621 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7622 (Node *) makeA_Expr(AEXPR_OR, NIL,
7623 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7624 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7626 (Node *) makeA_Expr(AEXPR_OR, NIL,
7627 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
7628 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
7632 | a_expr IN_P in_expr
7634 /* in_expr returns a SubLink or a list of a_exprs */
7635 if (IsA($3, SubLink))
7637 /* generate foo = ANY (subquery) */
7638 SubLink *n = (SubLink *) $3;
7639 n->subLinkType = ANY_SUBLINK;
7641 n->operName = list_make1(makeString("="));
7646 /* generate scalar IN expression */
7647 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
7650 | a_expr NOT IN_P in_expr
7652 /* in_expr returns a SubLink or a list of a_exprs */
7653 if (IsA($4, SubLink))
7655 /* generate NOT (foo = ANY (subquery)) */
7656 /* Make an = ANY node */
7657 SubLink *n = (SubLink *) $4;
7658 n->subLinkType = ANY_SUBLINK;
7660 n->operName = list_make1(makeString("="));
7661 /* Stick a NOT on top */
7662 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
7666 /* generate scalar NOT IN expression */
7667 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
7670 | a_expr subquery_Op sub_type select_with_parens %prec Op
7672 SubLink *n = makeNode(SubLink);
7673 n->subLinkType = $3;
7679 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
7681 if ($3 == ANY_SUBLINK)
7682 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
7684 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
7686 | UNIQUE select_with_parens
7688 /* Not sure how to get rid of the parentheses
7689 * but there are lots of shift/reduce errors without them.
7691 * Should be able to implement this by plopping the entire
7692 * select into a node, then transforming the target expressions
7693 * from whatever they are into count(*), and testing the
7694 * entire result equal to one.
7695 * But, will probably implement a separate node in the executor.
7698 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
7699 errmsg("UNIQUE predicate is not yet implemented")));
7701 | a_expr IS DOCUMENT_P %prec IS
7703 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1));
7705 | a_expr IS NOT DOCUMENT_P %prec IS
7707 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7708 makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1)),
7714 * Restricted expressions
7716 * b_expr is a subset of the complete expression syntax defined by a_expr.
7718 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
7719 * cause trouble in the places where b_expr is used. For simplicity, we
7720 * just eliminate all the boolean-keyword-operator productions from b_expr.
7724 | b_expr TYPECAST Typename
7725 { $$ = makeTypeCast($1, $3); }
7726 | '+' b_expr %prec UMINUS
7727 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7728 | '-' b_expr %prec UMINUS
7729 { $$ = doNegate($2, @1); }
7731 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7733 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7735 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7737 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7739 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7741 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7743 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7745 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7747 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7748 | b_expr qual_Op b_expr %prec Op
7749 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7750 | qual_Op b_expr %prec Op
7751 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7752 | b_expr qual_Op %prec POSTFIXOP
7753 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7754 | b_expr IS DISTINCT FROM b_expr %prec IS
7756 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7758 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
7760 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
7761 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
7763 | b_expr IS OF '(' type_list ')' %prec IS
7765 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7767 | b_expr IS NOT OF '(' type_list ')' %prec IS
7769 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7771 | b_expr IS DOCUMENT_P %prec IS
7773 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1));
7775 | b_expr IS NOT DOCUMENT_P %prec IS
7777 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7778 makeXmlExpr(IS_DOCUMENT, NULL, NIL, list_make1($1)),
7784 * Productions that can be used in both a_expr and b_expr.
7786 * Note: productions that refer recursively to a_expr or b_expr mostly
7787 * cannot appear here. However, it's OK to refer to a_exprs that occur
7788 * inside parentheses, such as function arguments; that cannot introduce
7789 * ambiguity to the b_expr syntax.
7791 c_expr: columnref { $$ = $1; }
7792 | AexprConst { $$ = $1; }
7793 | PARAM opt_indirection
7795 ParamRef *p = makeNode(ParamRef);
7799 A_Indirection *n = makeNode(A_Indirection);
7800 n->arg = (Node *) p;
7801 n->indirection = $2;
7807 | '(' a_expr ')' opt_indirection
7811 A_Indirection *n = makeNode(A_Indirection);
7813 n->indirection = $4;
7823 | select_with_parens %prec UMINUS
7825 SubLink *n = makeNode(SubLink);
7826 n->subLinkType = EXPR_SUBLINK;
7832 | EXISTS select_with_parens
7834 SubLink *n = makeNode(SubLink);
7835 n->subLinkType = EXISTS_SUBLINK;
7841 | ARRAY select_with_parens
7843 SubLink *n = makeNode(SubLink);
7844 n->subLinkType = ARRAY_SUBLINK;
7854 RowExpr *r = makeNode(RowExpr);
7856 r->row_typeid = 0; /* not analyzed yet */
7862 * func_expr is split out from c_expr just so that we have a classification
7863 * for "everything that is a function call or looks like one". This isn't
7864 * very important, but it saves us having to document which variants are
7865 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
7866 * (Note that many of the special SQL functions wouldn't actually make any
7867 * sense as functional index entries, but we ignore that consideration here.)
7869 func_expr: func_name '(' ')'
7871 FuncCall *n = makeNode(FuncCall);
7874 n->agg_star = FALSE;
7875 n->agg_distinct = FALSE;
7879 | func_name '(' expr_list ')'
7881 FuncCall *n = makeNode(FuncCall);
7884 n->agg_star = FALSE;
7885 n->agg_distinct = FALSE;
7889 | func_name '(' ALL expr_list ')'
7891 FuncCall *n = makeNode(FuncCall);
7894 n->agg_star = FALSE;
7895 n->agg_distinct = FALSE;
7896 /* Ideally we'd mark the FuncCall node to indicate
7897 * "must be an aggregate", but there's no provision
7898 * for that in FuncCall at the moment.
7903 | func_name '(' DISTINCT expr_list ')'
7905 FuncCall *n = makeNode(FuncCall);
7908 n->agg_star = FALSE;
7909 n->agg_distinct = TRUE;
7913 | func_name '(' '*' ')'
7916 * We consider AGGREGATE(*) to invoke a parameterless
7917 * aggregate. This does the right thing for COUNT(*),
7918 * and there are no other aggregates in SQL92 that accept
7921 * The FuncCall node is also marked agg_star = true,
7922 * so that later processing can detect what the argument
7925 FuncCall *n = makeNode(FuncCall);
7929 n->agg_distinct = FALSE;
7936 * Translate as "'now'::text::date".
7938 * We cannot use "'now'::date" because coerce_type() will
7939 * immediately reduce that to a constant representing
7940 * today's date. We need to delay the conversion until
7941 * runtime, else the wrong things will happen when
7942 * CURRENT_DATE is used in a column default value or rule.
7944 * This could be simplified if we had a way to generate
7945 * an expression tree representing runtime application
7946 * of type-input conversion functions. (As of PG 7.3
7947 * that is actually possible, but not clear that we want
7950 A_Const *s = makeNode(A_Const);
7953 s->val.type = T_String;
7954 s->val.val.str = "now";
7955 s->typename = SystemTypeName("text");
7957 d = SystemTypeName("date");
7959 $$ = (Node *)makeTypeCast((Node *)s, d);
7964 * Translate as "'now'::text::timetz".
7965 * See comments for CURRENT_DATE.
7967 A_Const *s = makeNode(A_Const);
7970 s->val.type = T_String;
7971 s->val.val.str = "now";
7972 s->typename = SystemTypeName("text");
7974 d = SystemTypeName("timetz");
7976 $$ = (Node *)makeTypeCast((Node *)s, d);
7978 | CURRENT_TIME '(' Iconst ')'
7981 * Translate as "'now'::text::timetz(n)".
7982 * See comments for CURRENT_DATE.
7984 A_Const *s = makeNode(A_Const);
7987 s->val.type = T_String;
7988 s->val.val.str = "now";
7989 s->typename = SystemTypeName("text");
7990 d = SystemTypeName("timetz");
7991 d->typmods = list_make1(makeIntConst($3));
7993 $$ = (Node *)makeTypeCast((Node *)s, d);
7998 * Translate as "now()", since we have a function that
7999 * does exactly what is needed.
8001 FuncCall *n = makeNode(FuncCall);
8002 n->funcname = SystemFuncName("now");
8004 n->agg_star = FALSE;
8005 n->agg_distinct = FALSE;
8009 | CURRENT_TIMESTAMP '(' Iconst ')'
8012 * Translate as "'now'::text::timestamptz(n)".
8013 * See comments for CURRENT_DATE.
8015 A_Const *s = makeNode(A_Const);
8018 s->val.type = T_String;
8019 s->val.val.str = "now";
8020 s->typename = SystemTypeName("text");
8022 d = SystemTypeName("timestamptz");
8023 d->typmods = list_make1(makeIntConst($3));
8025 $$ = (Node *)makeTypeCast((Node *)s, d);
8030 * Translate as "'now'::text::time".
8031 * See comments for CURRENT_DATE.
8033 A_Const *s = makeNode(A_Const);
8036 s->val.type = T_String;
8037 s->val.val.str = "now";
8038 s->typename = SystemTypeName("text");
8040 d = SystemTypeName("time");
8042 $$ = (Node *)makeTypeCast((Node *)s, d);
8044 | LOCALTIME '(' Iconst ')'
8047 * Translate as "'now'::text::time(n)".
8048 * See comments for CURRENT_DATE.
8050 A_Const *s = makeNode(A_Const);
8053 s->val.type = T_String;
8054 s->val.val.str = "now";
8055 s->typename = SystemTypeName("text");
8056 d = SystemTypeName("time");
8057 d->typmods = list_make1(makeIntConst($3));
8059 $$ = (Node *)makeTypeCast((Node *)s, d);
8064 * Translate as "'now'::text::timestamp".
8065 * See comments for CURRENT_DATE.
8067 A_Const *s = makeNode(A_Const);
8070 s->val.type = T_String;
8071 s->val.val.str = "now";
8072 s->typename = SystemTypeName("text");
8074 d = SystemTypeName("timestamp");
8076 $$ = (Node *)makeTypeCast((Node *)s, d);
8078 | LOCALTIMESTAMP '(' Iconst ')'
8081 * Translate as "'now'::text::timestamp(n)".
8082 * See comments for CURRENT_DATE.
8084 A_Const *s = makeNode(A_Const);
8087 s->val.type = T_String;
8088 s->val.val.str = "now";
8089 s->typename = SystemTypeName("text");
8091 d = SystemTypeName("timestamp");
8092 d->typmods = list_make1(makeIntConst($3));
8094 $$ = (Node *)makeTypeCast((Node *)s, d);
8098 FuncCall *n = makeNode(FuncCall);
8099 n->funcname = SystemFuncName("current_user");
8101 n->agg_star = FALSE;
8102 n->agg_distinct = FALSE;
8108 FuncCall *n = makeNode(FuncCall);
8109 n->funcname = SystemFuncName("current_user");
8111 n->agg_star = FALSE;
8112 n->agg_distinct = FALSE;
8118 FuncCall *n = makeNode(FuncCall);
8119 n->funcname = SystemFuncName("session_user");
8121 n->agg_star = FALSE;
8122 n->agg_distinct = FALSE;
8128 FuncCall *n = makeNode(FuncCall);
8129 n->funcname = SystemFuncName("current_user");
8131 n->agg_star = FALSE;
8132 n->agg_distinct = FALSE;
8136 | CAST '(' a_expr AS Typename ')'
8137 { $$ = makeTypeCast($3, $5); }
8138 | EXTRACT '(' extract_list ')'
8140 FuncCall *n = makeNode(FuncCall);
8141 n->funcname = SystemFuncName("date_part");
8143 n->agg_star = FALSE;
8144 n->agg_distinct = FALSE;
8148 | OVERLAY '(' overlay_list ')'
8150 /* overlay(A PLACING B FROM C FOR D) is converted to
8151 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8152 * overlay(A PLACING B FROM C) is converted to
8153 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8155 FuncCall *n = makeNode(FuncCall);
8156 n->funcname = SystemFuncName("overlay");
8158 n->agg_star = FALSE;
8159 n->agg_distinct = FALSE;
8163 | POSITION '(' position_list ')'
8165 /* position(A in B) is converted to position(B, A) */
8166 FuncCall *n = makeNode(FuncCall);
8167 n->funcname = SystemFuncName("position");
8169 n->agg_star = FALSE;
8170 n->agg_distinct = FALSE;
8174 | SUBSTRING '(' substr_list ')'
8176 /* substring(A from B for C) is converted to
8177 * substring(A, B, C) - thomas 2000-11-28
8179 FuncCall *n = makeNode(FuncCall);
8180 n->funcname = SystemFuncName("substring");
8182 n->agg_star = FALSE;
8183 n->agg_distinct = FALSE;
8187 | TREAT '(' a_expr AS Typename ')'
8189 /* TREAT(expr AS target) converts expr of a particular type to target,
8190 * which is defined to be a subtype of the original expression.
8191 * In SQL99, this is intended for use with structured UDTs,
8192 * but let's make this a generally useful form allowing stronger
8193 * coercions than are handled by implicit casting.
8195 FuncCall *n = makeNode(FuncCall);
8196 /* Convert SystemTypeName() to SystemFuncName() even though
8197 * at the moment they result in the same thing.
8199 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8200 n->args = list_make1($3);
8201 n->agg_star = FALSE;
8202 n->agg_distinct = FALSE;
8206 | TRIM '(' BOTH trim_list ')'
8208 /* various trim expressions are defined in SQL92
8209 * - thomas 1997-07-19
8211 FuncCall *n = makeNode(FuncCall);
8212 n->funcname = SystemFuncName("btrim");
8214 n->agg_star = FALSE;
8215 n->agg_distinct = FALSE;
8219 | TRIM '(' LEADING trim_list ')'
8221 FuncCall *n = makeNode(FuncCall);
8222 n->funcname = SystemFuncName("ltrim");
8224 n->agg_star = FALSE;
8225 n->agg_distinct = FALSE;
8229 | TRIM '(' TRAILING trim_list ')'
8231 FuncCall *n = makeNode(FuncCall);
8232 n->funcname = SystemFuncName("rtrim");
8234 n->agg_star = FALSE;
8235 n->agg_distinct = FALSE;
8239 | TRIM '(' trim_list ')'
8241 FuncCall *n = makeNode(FuncCall);
8242 n->funcname = SystemFuncName("btrim");
8244 n->agg_star = FALSE;
8245 n->agg_distinct = FALSE;
8249 | NULLIF '(' a_expr ',' a_expr ')'
8251 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
8253 | COALESCE '(' expr_list ')'
8255 CoalesceExpr *c = makeNode(CoalesceExpr);
8259 | GREATEST '(' expr_list ')'
8261 MinMaxExpr *v = makeNode(MinMaxExpr);
8263 v->op = IS_GREATEST;
8266 | LEAST '(' expr_list ')'
8268 MinMaxExpr *v = makeNode(MinMaxExpr);
8273 | XMLCONCAT '(' expr_list ')'
8275 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3);
8277 | XMLELEMENT '(' NAME_P ColLabel ')'
8279 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL);
8281 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
8283 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL);
8285 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
8287 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6);
8289 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
8291 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8);
8293 | XMLFOREST '(' xml_attribute_list ')'
8295 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL);
8297 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
8299 XmlExpr *x = (XmlExpr *) makeXmlExpr(IS_XMLPARSE, NULL, NIL,
8301 makeBoolAConst($5)));
8305 | XMLPI '(' NAME_P ColLabel ')'
8307 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL);
8309 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
8311 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6));
8313 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
8315 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
8316 list_make3($3, $5, $6));
8318 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
8320 XmlSerialize *n = makeNode(XmlSerialize);
8331 xml_root_version: VERSION_P a_expr
8333 | VERSION_P NO VALUE_P
8335 A_Const *val = makeNode(A_Const);
8336 val->val.type = T_Null;
8341 opt_xml_root_standalone: ',' STANDALONE_P YES_P
8342 { $$ = (Node *) makeIntConst(XML_STANDALONE_YES); }
8343 | ',' STANDALONE_P NO
8344 { $$ = (Node *) makeIntConst(XML_STANDALONE_NO); }
8345 | ',' STANDALONE_P NO VALUE_P
8346 { $$ = (Node *) makeIntConst(XML_STANDALONE_NO_VALUE); }
8348 { $$ = (Node *) makeIntConst(XML_STANDALONE_OMITTED); }
8351 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
8354 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
8355 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
8358 xml_attribute_el: a_expr AS ColLabel
8360 $$ = makeNode(ResTarget);
8362 $$->indirection = NULL;
8363 $$->val = (Node *) $1;
8368 $$ = makeNode(ResTarget);
8370 $$->indirection = NULL;
8371 $$->val = (Node *) $1;
8376 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
8377 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
8380 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
8381 | STRIP_P WHITESPACE_P { $$ = FALSE; }
8382 | /*EMPTY*/ { $$ = FALSE; }
8386 * Supporting nonterminals for expressions.
8389 /* Explicit row production.
8391 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
8392 * without conflicting with the parenthesized a_expr production. Without the
8393 * ROW keyword, there must be more than one a_expr inside the parens.
8395 row: ROW '(' expr_list ')' { $$ = $3; }
8396 | ROW '(' ')' { $$ = NIL; }
8397 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
8400 sub_type: ANY { $$ = ANY_SUBLINK; }
8401 | SOME { $$ = ANY_SUBLINK; }
8402 | ALL { $$ = ALL_SUBLINK; }
8405 all_Op: Op { $$ = $1; }
8406 | MathOp { $$ = $1; }
8409 MathOp: '+' { $$ = "+"; }
8421 { $$ = list_make1(makeString($1)); }
8422 | OPERATOR '(' any_operator ')'
8428 { $$ = list_make1(makeString($1)); }
8429 | OPERATOR '(' any_operator ')'
8435 { $$ = list_make1(makeString($1)); }
8436 | OPERATOR '(' any_operator ')'
8439 { $$ = list_make1(makeString("~~")); }
8441 { $$ = list_make1(makeString("!~~")); }
8443 { $$ = list_make1(makeString("~~*")); }
8445 { $$ = list_make1(makeString("!~~*")); }
8446 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
8447 * the regular expression is preprocessed by a function (similar_escape),
8448 * and the ~ operator for posix regular expressions is used.
8449 * x SIMILAR TO y -> x ~ similar_escape(y)
8450 * this transformation is made on the fly by the parser upwards.
8451 * however the SubLink structure which handles any/some/all stuff
8452 * is not ready for such a thing.
8458 $$ = list_make1($1);
8460 | expr_list ',' a_expr
8462 $$ = lappend($1, $3);
8467 extract_arg FROM a_expr
8469 A_Const *n = makeNode(A_Const);
8470 n->val.type = T_String;
8471 n->val.val.str = $1;
8472 $$ = list_make2((Node *) n, $3);
8474 | /*EMPTY*/ { $$ = NIL; }
8477 type_list: Typename { $$ = list_make1($1); }
8478 | type_list ',' Typename { $$ = lappend($1, $3); }
8481 array_expr_list: array_expr
8482 { $$ = list_make1($1); }
8483 | array_expr_list ',' array_expr
8484 { $$ = lappend($1, $3); }
8487 array_expr: '[' expr_list ']'
8489 ArrayExpr *n = makeNode(ArrayExpr);
8493 | '[' array_expr_list ']'
8495 ArrayExpr *n = makeNode(ArrayExpr);
8501 /* Allow delimited string SCONST in extract_arg as an SQL extension.
8502 * - thomas 2001-04-12
8507 | YEAR_P { $$ = "year"; }
8508 | MONTH_P { $$ = "month"; }
8509 | DAY_P { $$ = "day"; }
8510 | HOUR_P { $$ = "hour"; }
8511 | MINUTE_P { $$ = "minute"; }
8512 | SECOND_P { $$ = "second"; }
8513 | SCONST { $$ = $1; }
8516 /* OVERLAY() arguments
8517 * SQL99 defines the OVERLAY() function:
8518 * o overlay(text placing text from int for int)
8519 * o overlay(text placing text from int)
8522 a_expr overlay_placing substr_from substr_for
8524 $$ = list_make4($1, $2, $3, $4);
8526 | a_expr overlay_placing substr_from
8528 $$ = list_make3($1, $2, $3);
8537 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
8540 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
8541 | /*EMPTY*/ { $$ = NIL; }
8544 /* SUBSTRING() arguments
8545 * SQL9x defines a specific syntax for arguments to SUBSTRING():
8546 * o substring(text from int for int)
8547 * o substring(text from int) get entire string from starting point "int"
8548 * o substring(text for int) get first "int" characters of string
8549 * o substring(text from pattern) get entire string matching pattern
8550 * o substring(text from pattern for escape) same with specified escape char
8551 * We also want to support generic substring functions which accept
8552 * the usual generic list of arguments. So we will accept both styles
8553 * here, and convert the SQL9x style to the generic list for further
8554 * processing. - thomas 2000-11-28
8557 a_expr substr_from substr_for
8559 $$ = list_make3($1, $2, $3);
8561 | a_expr substr_for substr_from
8563 /* not legal per SQL99, but might as well allow it */
8564 $$ = list_make3($1, $3, $2);
8566 | a_expr substr_from
8568 $$ = list_make2($1, $2);
8573 * Since there are no cases where this syntax allows
8574 * a textual FOR value, we forcibly cast the argument
8575 * to int4. The possible matches in pg_proc are
8576 * substring(text,int4) and substring(text,text),
8577 * and we don't want the parser to choose the latter,
8578 * which it is likely to do if the second argument
8579 * is unknown or doesn't have an implicit cast to int4.
8581 A_Const *n = makeNode(A_Const);
8582 n->val.type = T_Integer;
8583 n->val.val.ival = 1;
8584 $$ = list_make3($1, (Node *) n,
8585 makeTypeCast($2, SystemTypeName("int4")));
8596 FROM a_expr { $$ = $2; }
8599 substr_for: FOR a_expr { $$ = $2; }
8602 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
8603 | FROM expr_list { $$ = $2; }
8604 | expr_list { $$ = $1; }
8607 in_expr: select_with_parens
8609 SubLink *n = makeNode(SubLink);
8611 /* other fields will be filled later */
8614 | '(' expr_list ')' { $$ = (Node *)$2; }
8618 * Define SQL92-style case clause.
8619 * - Full specification
8620 * CASE WHEN a = b THEN c ... ELSE d END
8621 * - Implicit argument
8622 * CASE a WHEN b THEN c ... ELSE d END
8624 case_expr: CASE case_arg when_clause_list case_default END_P
8626 CaseExpr *c = makeNode(CaseExpr);
8627 c->casetype = 0; /* not analyzed yet */
8628 c->arg = (Expr *) $2;
8630 c->defresult = (Expr *) $4;
8636 /* There must be at least one */
8637 when_clause { $$ = list_make1($1); }
8638 | when_clause_list when_clause { $$ = lappend($1, $2); }
8642 WHEN a_expr THEN a_expr
8644 CaseWhen *w = makeNode(CaseWhen);
8645 w->expr = (Expr *) $2;
8646 w->result = (Expr *) $4;
8652 ELSE a_expr { $$ = $2; }
8653 | /*EMPTY*/ { $$ = NULL; }
8656 case_arg: a_expr { $$ = $1; }
8657 | /*EMPTY*/ { $$ = NULL; }
8661 * columnref starts with relation_name not ColId, so that OLD and NEW
8662 * references can be accepted. Note that when there are more than two
8663 * dotted names, the first name is not actually a relation name...
8665 columnref: relation_name
8667 $$ = makeColumnRef($1, NIL, @1);
8669 | relation_name indirection
8671 $$ = makeColumnRef($1, $2, @1);
8678 $$ = (Node *) makeString($2);
8682 $$ = (Node *) makeString("*");
8686 A_Indices *ai = makeNode(A_Indices);
8691 | '[' a_expr ':' a_expr ']'
8693 A_Indices *ai = makeNode(A_Indices);
8701 indirection_el { $$ = list_make1($1); }
8702 | indirection indirection_el { $$ = lappend($1, $2); }
8706 /*EMPTY*/ { $$ = NIL; }
8707 | opt_indirection indirection_el { $$ = lappend($1, $2); }
8710 opt_asymmetric: ASYMMETRIC
8715 * The SQL spec defines "contextually typed value expressions" and
8716 * "contextually typed row value constructors", which for our purposes
8717 * are the same as "a_expr" and "row" except that DEFAULT can appear at
8722 a_expr { $$ = (Node *) $1; }
8723 | DEFAULT { $$ = (Node *) makeNode(SetToDefault); }
8727 ctext_expr { $$ = list_make1($1); }
8728 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
8732 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
8733 * making VALUES a fully reserved word, which will probably break more apps
8734 * than allowing the noise-word is worth.
8736 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
8740 /*****************************************************************************
8742 * target list for SELECT
8744 *****************************************************************************/
8747 target_el { $$ = list_make1($1); }
8748 | target_list ',' target_el { $$ = lappend($1, $3); }
8751 /* AS is not optional because shift/red conflict with unary ops */
8752 target_el: a_expr AS ColLabel
8754 $$ = makeNode(ResTarget);
8756 $$->indirection = NIL;
8757 $$->val = (Node *)$1;
8762 $$ = makeNode(ResTarget);
8764 $$->indirection = NIL;
8765 $$->val = (Node *)$1;
8770 ColumnRef *n = makeNode(ColumnRef);
8771 n->fields = list_make1(makeString("*"));
8774 $$ = makeNode(ResTarget);
8776 $$->indirection = NIL;
8777 $$->val = (Node *)n;
8783 /*****************************************************************************
8785 * Names and constants
8787 *****************************************************************************/
8790 SpecialRuleRelation { $$ = $1; }
8791 | ColId { $$ = $1; }
8794 qualified_name_list:
8795 qualified_name { $$ = list_make1($1); }
8796 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
8800 * The production for a qualified relation name has to exactly match the
8801 * production for a qualified func_name, because in a FROM clause we cannot
8802 * tell which we are parsing until we see what comes after it ('(' for a
8803 * func_name, something else for a relation). Therefore we allow 'indirection'
8804 * which may contain subscripts, and reject that case in the C code.
8809 $$ = makeNode(RangeVar);
8810 $$->catalogname = NULL;
8811 $$->schemaname = NULL;
8814 | relation_name indirection
8816 check_qualified_name($2);
8817 $$ = makeNode(RangeVar);
8818 switch (list_length($2))
8821 $$->catalogname = NULL;
8822 $$->schemaname = $1;
8823 $$->relname = strVal(linitial($2));
8826 $$->catalogname = $1;
8827 $$->schemaname = strVal(linitial($2));
8828 $$->relname = strVal(lsecond($2));
8832 (errcode(ERRCODE_SYNTAX_ERROR),
8833 errmsg("improper qualified name (too many dotted names): %s",
8834 NameListToString(lcons(makeString($1), $2)))));
8841 { $$ = list_make1(makeString($1)); }
8842 | name_list ',' name
8843 { $$ = lappend($1, makeString($3)); }
8847 name: ColId { $$ = $1; };
8855 attr_name: ColLabel { $$ = $1; };
8857 index_name: ColId { $$ = $1; };
8859 file_name: Sconst { $$ = $1; };
8862 * The production for a qualified func_name has to exactly match the
8863 * production for a qualified columnref, because we cannot tell which we
8864 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
8865 * anything else for a columnref). Therefore we allow 'indirection' which
8866 * may contain subscripts, and reject that case in the C code. (If we
8867 * ever implement SQL99-like methods, such syntax may actually become legal!)
8869 func_name: type_function_name
8870 { $$ = list_make1(makeString($1)); }
8871 | relation_name indirection
8872 { $$ = check_func_name(lcons(makeString($1), $2)); }
8881 A_Const *n = makeNode(A_Const);
8882 n->val.type = T_Integer;
8883 n->val.val.ival = $1;
8888 A_Const *n = makeNode(A_Const);
8889 n->val.type = T_Float;
8890 n->val.val.str = $1;
8895 A_Const *n = makeNode(A_Const);
8896 n->val.type = T_String;
8897 n->val.val.str = $1;
8902 A_Const *n = makeNode(A_Const);
8903 n->val.type = T_BitString;
8904 n->val.val.str = $1;
8909 /* This is a bit constant per SQL99:
8910 * Without Feature F511, "BIT data type",
8911 * a <general literal> shall not be a
8912 * <bit string literal> or a <hex string literal>.
8914 A_Const *n = makeNode(A_Const);
8915 n->val.type = T_BitString;
8916 n->val.val.str = $1;
8921 /* generic type 'literal' syntax */
8922 A_Const *n = makeNode(A_Const);
8923 n->typename = makeTypeNameFromNameList($1);
8924 n->typename->location = @1;
8925 n->val.type = T_String;
8926 n->val.val.str = $2;
8929 | func_name '(' expr_list ')' Sconst
8931 /* generic syntax with a type modifier */
8932 A_Const *n = makeNode(A_Const);
8933 n->typename = makeTypeNameFromNameList($1);
8934 n->typename->typmods = $3;
8935 n->typename->location = @1;
8936 n->val.type = T_String;
8937 n->val.val.str = $5;
8940 | ConstTypename Sconst
8942 A_Const *n = makeNode(A_Const);
8944 n->val.type = T_String;
8945 n->val.val.str = $2;
8948 | ConstInterval Sconst opt_interval
8950 A_Const *n = makeNode(A_Const);
8952 n->val.type = T_String;
8953 n->val.val.str = $2;
8954 /* precision is not specified, but fields may be... */
8955 if ($3 != INTERVAL_FULL_RANGE)
8956 n->typename->typmods = list_make1(makeIntConst($3));
8959 | ConstInterval '(' Iconst ')' Sconst opt_interval
8961 A_Const *n = makeNode(A_Const);
8963 n->val.type = T_String;
8964 n->val.val.str = $5;
8965 n->typename->typmods = list_make2(makeIntConst($6),
8971 $$ = (Node *)makeBoolAConst(TRUE);
8975 $$ = (Node *)makeBoolAConst(FALSE);
8979 A_Const *n = makeNode(A_Const);
8980 n->val.type = T_Null;
8985 Iconst: ICONST { $$ = $1; };
8986 Sconst: SCONST { $$ = $1; };
8987 RoleId: ColId { $$ = $1; };
8989 SignedIconst: ICONST { $$ = $1; }
8990 | '-' ICONST { $$ = - $2; }
8994 * Name classification hierarchy.
8996 * IDENT is the lexeme returned by the lexer for identifiers that match
8997 * no known keyword. In most cases, we can accept certain keywords as
8998 * names, not only IDENTs. We prefer to accept as many such keywords
8999 * as possible to minimize the impact of "reserved words" on programmers.
9000 * So, we divide names into several possible classes. The classification
9001 * is chosen in part to make keywords acceptable as names wherever possible.
9004 /* Column identifier --- names that can be column, table, etc names.
9006 ColId: IDENT { $$ = $1; }
9007 | unreserved_keyword { $$ = pstrdup($1); }
9008 | col_name_keyword { $$ = pstrdup($1); }
9011 /* Type/function identifier --- names that can be type or function names.
9013 type_function_name: IDENT { $$ = $1; }
9014 | unreserved_keyword { $$ = pstrdup($1); }
9015 | type_func_name_keyword { $$ = pstrdup($1); }
9018 /* Column label --- allowed labels in "AS" clauses.
9019 * This presently includes *all* Postgres keywords.
9021 ColLabel: IDENT { $$ = $1; }
9022 | unreserved_keyword { $$ = pstrdup($1); }
9023 | col_name_keyword { $$ = pstrdup($1); }
9024 | type_func_name_keyword { $$ = pstrdup($1); }
9025 | reserved_keyword { $$ = pstrdup($1); }
9030 * Keyword category lists. Generally, every keyword present in
9031 * the Postgres grammar should appear in exactly one of these lists.
9033 * Put a new keyword into the first list that it can go into without causing
9034 * shift or reduce conflicts. The earlier lists define "less reserved"
9035 * categories of keywords.
9037 * Make sure that each keyword's category in keywords.c matches where
9038 * it is listed here. (Someday we may be able to generate these lists and
9039 * keywords.c's table from a common master list.)
9042 /* "Unreserved" keywords --- available for use as any kind of name.
9280 /* Column identifier --- keywords that can be column, table, etc names.
9282 * Many of these keywords will in fact be recognized as type or function
9283 * names too; but they have special productions for the purpose, and so
9284 * can't be treated as "generic" type or function names.
9286 * The type names appearing here are not usable as function names
9287 * because they can be followed by '(' in typename productions, which
9288 * looks too much like a function call for an LR(1) parser.
9338 /* Type/function identifier --- keywords that can be type or function names.
9340 * Most of these are keywords that are used as operators in expressions;
9341 * in general such keywords can't be column names because they would be
9342 * ambiguous with variables, but they are unambiguous as function identifiers.
9344 * Do not include POSITION, SUBSTRING, etc here since they have explicit
9345 * productions in a_expr to support the goofy SQL9x argument syntax.
9346 * - thomas 2000-11-28
9348 type_func_name_keyword:
9371 /* Reserved keyword --- these keywords are usable only as a ColLabel.
9373 * Keywords appear here if they could not be distinguished from variable,
9374 * type, or function names in some contexts. Don't put things here unless
9455 SpecialRuleRelation:
9462 (errcode(ERRCODE_SYNTAX_ERROR),
9463 errmsg("OLD used in query that is not in a rule")));
9471 (errcode(ERRCODE_SYNTAX_ERROR),
9472 errmsg("NEW used in query that is not in a rule")));
9479 makeColumnRef(char *relname, List *indirection, int location)
9482 * Generate a ColumnRef node, with an A_Indirection node added if there
9483 * is any subscripting in the specified indirection list. However,
9484 * any field selection at the start of the indirection list must be
9485 * transposed into the "fields" part of the ColumnRef node.
9487 ColumnRef *c = makeNode(ColumnRef);
9491 c->location = location;
9492 foreach(l, indirection)
9494 if (IsA(lfirst(l), A_Indices))
9496 A_Indirection *i = makeNode(A_Indirection);
9500 /* easy case - all indirection goes to A_Indirection */
9501 c->fields = list_make1(makeString(relname));
9502 i->indirection = indirection;
9506 /* got to split the list in two */
9507 i->indirection = list_copy_tail(indirection, nfields);
9508 indirection = list_truncate(indirection, nfields);
9509 c->fields = lcons(makeString(relname), indirection);
9511 i->arg = (Node *) c;
9516 /* No subscripting, so all indirection gets added to field list */
9517 c->fields = lcons(makeString(relname), indirection);
9522 makeTypeCast(Node *arg, TypeName *typename)
9525 * Simply generate a TypeCast node.
9527 * Earlier we would determine whether an A_Const would
9528 * be acceptable, however Domains require coerce_type()
9529 * to process them -- applying constraints as required.
9531 TypeCast *n = makeNode(TypeCast);
9533 n->typename = typename;
9538 makeStringConst(char *str, TypeName *typename)
9540 A_Const *n = makeNode(A_Const);
9542 n->val.type = T_String;
9543 n->val.val.str = str;
9544 n->typename = typename;
9550 makeIntConst(int val)
9552 A_Const *n = makeNode(A_Const);
9553 n->val.type = T_Integer;
9554 n->val.val.ival = val;
9555 n->typename = SystemTypeName("int4");
9561 makeFloatConst(char *str)
9563 A_Const *n = makeNode(A_Const);
9565 n->val.type = T_Float;
9566 n->val.val.str = str;
9567 n->typename = SystemTypeName("float8");
9573 makeAConst(Value *v)
9580 n = makeFloatConst(v->val.str);
9584 n = makeIntConst(v->val.ival);
9589 n = makeStringConst(v->val.str, NULL);
9597 * Create an A_Const node and initialize to a boolean constant.
9600 makeBoolAConst(bool state)
9602 A_Const *n = makeNode(A_Const);
9603 n->val.type = T_String;
9604 n->val.val.str = (state? "t": "f");
9605 n->typename = SystemTypeName("bool");
9610 * Create and populate a FuncCall node to support the OVERLAPS operator.
9613 makeOverlaps(List *largs, List *rargs, int location)
9615 FuncCall *n = makeNode(FuncCall);
9617 n->funcname = SystemFuncName("overlaps");
9618 if (list_length(largs) == 1)
9619 largs = lappend(largs, largs);
9620 else if (list_length(largs) != 2)
9622 (errcode(ERRCODE_SYNTAX_ERROR),
9623 errmsg("wrong number of parameters on left side of OVERLAPS expression")));
9624 if (list_length(rargs) == 1)
9625 rargs = lappend(rargs, rargs);
9626 else if (list_length(rargs) != 2)
9628 (errcode(ERRCODE_SYNTAX_ERROR),
9629 errmsg("wrong number of parameters on right side of OVERLAPS expression")));
9630 n->args = list_concat(largs, rargs);
9631 n->agg_star = FALSE;
9632 n->agg_distinct = FALSE;
9633 n->location = location;
9637 /* check_qualified_name --- check the result of qualified_name production
9639 * It's easiest to let the grammar production for qualified_name allow
9640 * subscripts and '*', which we then must reject here.
9643 check_qualified_name(List *names)
9649 if (!IsA(lfirst(i), String))
9650 yyerror("syntax error");
9651 else if (strcmp(strVal(lfirst(i)), "*") == 0)
9652 yyerror("syntax error");
9656 /* check_func_name --- check the result of func_name production
9658 * It's easiest to let the grammar production for func_name allow subscripts
9659 * and '*', which we then must reject here.
9662 check_func_name(List *names)
9668 if (!IsA(lfirst(i), String))
9669 yyerror("syntax error");
9670 else if (strcmp(strVal(lfirst(i)), "*") == 0)
9671 yyerror("syntax error");
9676 /* extractArgTypes()
9677 * Given a list of FunctionParameter nodes, extract a list of just the
9678 * argument types (TypeNames) for input parameters only. This is what
9679 * is needed to look up an existing function, which is what is wanted by
9680 * the productions that use this call.
9683 extractArgTypes(List *parameters)
9688 foreach(i, parameters)
9690 FunctionParameter *p = (FunctionParameter *) lfirst(i);
9692 if (p->mode != FUNC_PARAM_OUT) /* keep if IN or INOUT */
9693 result = lappend(result, p->argType);
9698 /* findLeftmostSelect()
9699 * Find the leftmost component SelectStmt in a set-operation parsetree.
9702 findLeftmostSelect(SelectStmt *node)
9704 while (node && node->op != SETOP_NONE)
9709 /* insertSelectOptions()
9710 * Insert ORDER BY, etc into an already-constructed SelectStmt.
9712 * This routine is just to avoid duplicating code in SelectStmt productions.
9715 insertSelectOptions(SelectStmt *stmt,
9716 List *sortClause, List *lockingClause,
9717 Node *limitOffset, Node *limitCount)
9720 * Tests here are to reject constructs like
9721 * (SELECT foo ORDER BY bar) ORDER BY baz
9725 if (stmt->sortClause)
9727 (errcode(ERRCODE_SYNTAX_ERROR),
9728 errmsg("multiple ORDER BY clauses not allowed")));
9729 stmt->sortClause = sortClause;
9731 /* We can handle multiple locking clauses, though */
9732 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
9735 if (stmt->limitOffset)
9737 (errcode(ERRCODE_SYNTAX_ERROR),
9738 errmsg("multiple OFFSET clauses not allowed")));
9739 stmt->limitOffset = limitOffset;
9743 if (stmt->limitCount)
9745 (errcode(ERRCODE_SYNTAX_ERROR),
9746 errmsg("multiple LIMIT clauses not allowed")));
9747 stmt->limitCount = limitCount;
9752 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
9754 SelectStmt *n = makeNode(SelectStmt);
9758 n->larg = (SelectStmt *) larg;
9759 n->rarg = (SelectStmt *) rarg;
9764 * Build a properly-qualified reference to a built-in function.
9767 SystemFuncName(char *name)
9769 return list_make1(makeString(name));
9773 * Build a properly-qualified reference to a built-in type.
9775 * typmod is defaulted, but may be changed afterwards by caller.
9778 SystemTypeName(char *name)
9780 return makeTypeNameFromNameList(list_make1(makeString(name)));
9784 * Initialize to parse one query string
9789 QueryIsRule = FALSE;
9792 /* exprIsNullConstant()
9793 * Test whether an a_expr is a plain NULL constant or not.
9796 exprIsNullConstant(Node *arg)
9798 if (arg && IsA(arg, A_Const))
9800 A_Const *con = (A_Const *) arg;
9802 if (con->val.type == T_Null &&
9803 con->typename == NULL)
9810 * Handle negation of a numeric constant.
9812 * Formerly, we did this here because the optimizer couldn't cope with
9813 * indexquals that looked like "var = -4" --- it wants "var = const"
9814 * and a unary minus operator applied to a constant didn't qualify.
9815 * As of Postgres 7.0, that problem doesn't exist anymore because there
9816 * is a constant-subexpression simplifier in the optimizer. However,
9817 * there's still a good reason for doing this here, which is that we can
9818 * postpone committing to a particular internal representation for simple
9819 * negative constants. It's better to leave "-123.456" in string form
9820 * until we know what the desired type is.
9823 doNegate(Node *n, int location)
9825 if (IsA(n, A_Const))
9827 A_Const *con = (A_Const *)n;
9829 if (con->val.type == T_Integer)
9831 con->val.val.ival = -con->val.val.ival;
9834 if (con->val.type == T_Float)
9836 doNegateFloat(&con->val);
9841 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
9845 doNegateFloat(Value *v)
9847 char *oldval = v->val.str;
9852 v->val.str = oldval+1; /* just strip the '-' */
9855 char *newval = (char *) palloc(strlen(oldval) + 2);
9858 strcpy(newval+1, oldval);
9859 v->val.str = newval;
9864 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args)
9866 XmlExpr *x = makeNode(XmlExpr);
9871 * named_args is a list of ResTarget; it'll be split apart into separate
9872 * expression and name lists in transformXmlExpr().
9874 x->named_args = named_args;
9881 * Create a DefElem setting "oids" to the specified value.
9884 defWithOids(bool value)
9886 DefElem *f = makeNode(DefElem);
9888 f->defname = "oids";
9889 f->arg = (Node *) makeInteger(value);
9899 * Must undefine base_yylex before including scan.c, since we want it
9900 * to create the function base_yylex not filtered_base_yylex.