Add vacuum_freeze_table_age GUC option, to control when VACUUM should
[PostgreSQL.git] / src / backend / parser / gram.y
blob2bf8a42121b52f5a59cd8a30eafccfb27692fbc7
1 %{
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
6 * gram.y
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
13 * IDENTIFICATION
14 * $PostgreSQL$
16 * HISTORY
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
21 * NOTES
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled during parse analysis so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
38 * a good example.
40 * WARNINGS
41 * If you use a list, make sure the datum is a node so that the printing
42 * routines work.
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
45 * those.
47 *-------------------------------------------------------------------------
49 #include "postgres.h"
51 #include <ctype.h>
52 #include <limits.h>
54 #include "catalog/index.h"
55 #include "catalog/namespace.h"
56 #include "commands/defrem.h"
57 #include "nodes/makefuncs.h"
58 #include "nodes/nodeFuncs.h"
59 #include "parser/gramparse.h"
60 #include "storage/lmgr.h"
61 #include "utils/date.h"
62 #include "utils/datetime.h"
63 #include "utils/numeric.h"
64 #include "utils/xml.h"
67 /* Location tracking support --- simpler than bison's default */
68 #define YYLLOC_DEFAULT(Current, Rhs, N) \
69 do { \
70 if (N) \
71 (Current) = (Rhs)[1]; \
72 else \
73 (Current) = (Rhs)[0]; \
74 } while (0)
77 * The %name-prefix option below will make bison call base_yylex, but we
78 * really want it to call filtered_base_yylex (see parser.c).
80 #define base_yylex filtered_base_yylex
83 * Bison doesn't allocate anything that needs to live across parser calls,
84 * so we can easily have it use palloc instead of malloc. This prevents
85 * memory leaks if we error out during parsing. Note this only works with
86 * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
87 * if possible, so there's not really much problem anyhow, at least if
88 * you're building with gcc.
90 #define YYMALLOC palloc
91 #define YYFREE pfree
93 extern List *parsetree; /* final parse result is delivered here */
95 static bool QueryIsRule = FALSE;
98 * If you need access to certain yacc-generated variables and find that
99 * they're static by default, uncomment the next line. (this is not a
100 * problem, yet.)
102 /*#define __YYSCLASS*/
104 static Node *makeColumnRef(char *colname, List *indirection, int location);
105 static Node *makeTypeCast(Node *arg, TypeName *typename, int location);
106 static Node *makeStringConst(char *str, int location);
107 static Node *makeStringConstCast(char *str, int location, TypeName *typename);
108 static Node *makeIntConst(int val, int location);
109 static Node *makeFloatConst(char *str, int location);
110 static Node *makeBitStringConst(char *str, int location);
111 static Node *makeNullAConst(int location);
112 static Node *makeAConst(Value *v, int location);
113 static Node *makeBoolAConst(bool state, int location);
114 static FuncCall *makeOverlaps(List *largs, List *rargs, int location);
115 static void check_qualified_name(List *names);
116 static List *check_func_name(List *names);
117 static List *check_indirection(List *indirection);
118 static List *extractArgTypes(List *parameters);
119 static SelectStmt *findLeftmostSelect(SelectStmt *node);
120 static void insertSelectOptions(SelectStmt *stmt,
121 List *sortClause, List *lockingClause,
122 Node *limitOffset, Node *limitCount,
123 WithClause *withClause);
124 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
125 static Node *doNegate(Node *n, int location);
126 static void doNegateFloat(Value *v);
127 static Node *makeAArrayExpr(List *elements, int location);
128 static Node *makeXmlExpr(XmlExprOp op, char *name, List *named_args,
129 List *args, int location);
130 static List *mergeTableFuncParameters(List *func_args, List *columns);
131 static TypeName *TableFuncTypeName(List *columns);
135 %expect 0
136 %name-prefix="base_yy"
137 %locations
139 %union
141 int ival;
142 char chr;
143 char *str;
144 const char *keyword;
145 bool boolean;
146 JoinType jtype;
147 DropBehavior dbehavior;
148 OnCommitAction oncommit;
149 List *list;
150 Node *node;
151 Value *value;
152 ObjectType objtype;
154 TypeName *typnam;
155 FunctionParameter *fun_param;
156 FunctionParameterMode fun_param_mode;
157 FuncWithArgs *funwithargs;
158 DefElem *defelt;
159 OptionDefElem *optdef;
160 SortBy *sortby;
161 WindowDef *windef;
162 JoinExpr *jexpr;
163 IndexElem *ielem;
164 Alias *alias;
165 RangeVar *range;
166 IntoClause *into;
167 WithClause *with;
168 A_Indices *aind;
169 ResTarget *target;
170 PrivTarget *privtarget;
172 InsertStmt *istmt;
173 VariableSetStmt *vsetstmt;
176 %type <node> stmt schema_stmt
177 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterFdwStmt
178 AlterForeignServerStmt AlterGroupStmt
179 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
180 AlterUserStmt AlterUserMappingStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
181 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
182 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
183 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
184 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
185 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
186 CreateFdwStmt CreateForeignServerStmt CreateAssertStmt CreateTrigStmt
187 CreateUserStmt CreateUserMappingStmt CreateRoleStmt
188 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
189 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
190 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
191 DropUserStmt DropdbStmt DropTableSpaceStmt DropFdwStmt
192 DropForeignServerStmt DropUserMappingStmt 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
213 %type <list> alter_table_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 opt_with_data
227 %type <list> OptRoleList
228 %type <defelt> OptRoleElem
230 %type <str> opt_type
231 %type <str> foreign_server_version opt_foreign_server_version
232 %type <str> auth_ident
234 %type <str> OptSchemaName
235 %type <list> OptSchemaEltList
237 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
238 %type <str> opt_lancompiler
240 %type <str> TriggerEvents
241 %type <value> TriggerFuncArg
243 %type <str> relation_name copy_file_name
244 database_name access_method_clause access_method attr_name
245 index_name name file_name cluster_index_specification
247 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
248 opt_class opt_validator
250 %type <range> qualified_name OptConstrFromTable
252 %type <str> all_Op MathOp SpecialRuleRelation
254 %type <str> iso_level opt_encoding
255 %type <node> grantee
256 %type <list> grantee_list
257 %type <str> privilege
258 %type <list> privileges privilege_list
259 %type <privtarget> privilege_target
260 %type <funwithargs> function_with_argtypes
261 %type <list> function_with_argtypes_list
262 %type <chr> TriggerOneEvent
264 %type <list> stmtblock stmtmulti
265 OptTableElementList TableElementList OptInherit definition
266 OptWith opt_distinct opt_definition func_args func_args_list
267 func_args_with_defaults func_args_with_defaults_list
268 func_as createfunc_opt_list alterfunc_opt_list
269 aggr_args old_aggr_definition old_aggr_list
270 oper_argtypes RuleActionList RuleActionMulti
271 opt_column_list columnList opt_name_list
272 sort_clause opt_sort_clause sortby_list index_params
273 name_list from_clause from_list opt_array_bounds
274 qualified_name_list any_name any_name_list
275 any_operator expr_list attrs
276 target_list insert_column_list set_target_list
277 set_clause_list set_clause multiple_set_clause
278 ctext_expr_list ctext_row def_list indirection opt_indirection
279 group_clause TriggerFuncArgs select_limit
280 opt_select_limit opclass_item_list opclass_drop_list
281 opt_opfamily transaction_mode_list_or_empty
282 TableFuncElementList opt_type_modifiers
283 prep_type_clause
284 execute_param_clause using_clause returning_clause
285 enum_val_list table_func_column_list
286 create_generic_options alter_generic_options
287 relation_expr_list
289 %type <range> OptTempTableName
290 %type <into> into_clause create_as_target
292 %type <defelt> createfunc_opt_item common_func_opt_item
293 %type <fun_param> func_arg func_arg_with_default table_func_column
294 %type <fun_param_mode> arg_class
295 %type <typnam> func_return func_type
297 %type <boolean> TriggerForType OptTemp
298 %type <oncommit> OnCommitOption
300 %type <node> for_locking_item
301 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
302 %type <list> locked_rels_list
303 %type <boolean> opt_all
305 %type <node> join_outer join_qual
306 %type <jtype> join_type
308 %type <list> extract_list overlay_list position_list
309 %type <list> substr_list trim_list
310 %type <list> opt_interval interval_second
311 %type <node> overlay_placing substr_from substr_for
313 %type <boolean> opt_instead opt_analyze
314 %type <boolean> index_opt_unique opt_verbose opt_full
315 %type <boolean> opt_freeze opt_default opt_recheck
316 %type <defelt> opt_binary opt_oids copy_delimiter
318 %type <boolean> copy_from
320 %type <ival> opt_column event cursor_options opt_hold opt_set_data
321 %type <objtype> reindex_type drop_type comment_type
323 %type <node> fetch_direction select_limit_value select_offset_value
324 select_offset_value2 opt_select_fetch_first_value
325 %type <ival> row_or_rows first_or_next
327 %type <list> OptSeqOptList SeqOptList
328 %type <defelt> SeqOptElem
330 %type <istmt> insert_rest
332 %type <vsetstmt> set_rest SetResetClause
334 %type <node> TableElement ConstraintElem TableFuncElement
335 %type <node> columnDef
336 %type <defelt> def_elem old_aggr_elem
337 %type <node> def_arg columnElem where_clause where_or_current_clause
338 a_expr b_expr c_expr func_expr AexprConst indirection_el
339 columnref in_expr having_clause func_table array_expr
340 %type <list> row type_list array_expr_list
341 %type <node> case_expr case_arg when_clause case_default
342 %type <list> when_clause_list
343 %type <ival> sub_type
344 %type <list> OptCreateAs CreateAsList
345 %type <node> CreateAsElement ctext_expr
346 %type <value> NumericOnly
347 %type <alias> alias_clause
348 %type <sortby> sortby
349 %type <ielem> index_elem
350 %type <node> table_ref
351 %type <jexpr> joined_table
352 %type <range> relation_expr
353 %type <range> relation_expr_opt_alias
354 %type <target> target_el single_set_clause set_target insert_column_item
356 %type <str> generic_option_name
357 %type <node> generic_option_arg
358 %type <defelt> generic_option_elem
359 %type <optdef> alter_generic_option_elem
360 %type <list> generic_option_list alter_generic_option_list
362 %type <typnam> Typename SimpleTypename ConstTypename
363 GenericType Numeric opt_float
364 Character ConstCharacter
365 CharacterWithLength CharacterWithoutLength
366 ConstDatetime ConstInterval
367 Bit ConstBit BitWithLength BitWithoutLength
368 %type <str> character
369 %type <str> extract_arg
370 %type <str> opt_charset
371 %type <boolean> opt_varying opt_timezone
373 %type <ival> Iconst SignedIconst
374 %type <str> Sconst comment_text
375 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
376 %type <list> var_list
377 %type <str> ColId ColLabel var_name type_function_name param_name
378 %type <node> var_value zone_value
380 %type <keyword> unreserved_keyword type_func_name_keyword
381 %type <keyword> col_name_keyword reserved_keyword
383 %type <node> TableConstraint TableLikeClause
384 %type <list> TableLikeOptionList
385 %type <ival> TableLikeOption
386 %type <list> ColQualList
387 %type <node> ColConstraint ColConstraintElem ConstraintAttr
388 %type <ival> key_actions key_delete key_match key_update key_action
389 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
390 ConstraintTimeSpec
392 %type <list> constraints_set_list
393 %type <boolean> constraints_set_mode
394 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
395 %type <list> opt_check_option
397 %type <target> xml_attribute_el
398 %type <list> xml_attribute_list xml_attributes
399 %type <node> xml_root_version opt_xml_root_standalone
400 %type <ival> document_or_content
401 %type <boolean> xml_whitespace_option
403 %type <node> common_table_expr
404 %type <with> with_clause
405 %type <list> cte_list
407 %type <list> window_clause window_definition_list opt_partition_clause
408 %type <windef> window_definition over_clause window_specification
409 %type <str> opt_existing_window_name
410 %type <ival> opt_frame_clause frame_extent frame_bound
414 * If you make any token changes, update the keyword table in
415 * parser/keywords.c and add new keywords to the appropriate one of
416 * the reserved-or-not-so-reserved keyword lists, below; search
417 * this file for "Name classification hierarchy".
420 /* ordinary key words in alphabetical order */
421 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
422 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
423 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
425 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
426 BOOLEAN_P BOTH BY
428 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
429 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
430 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
431 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
432 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
433 CREATEROLE CREATEUSER CROSS CSV CTYPE CURRENT_P
434 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
435 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
437 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
438 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
439 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
441 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT
442 EXCLUDING EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
444 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOLLOWING FOR FORCE FOREIGN FORWARD
445 FREEZE FROM FULL FUNCTION
447 GLOBAL GRANT GRANTED GREATEST GROUP_P
449 HANDLER HAVING HEADER_P HOLD HOUR_P
451 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
452 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
453 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
454 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
456 JOIN
460 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
461 LIBRARY LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
462 LOCK_P LOGIN_P
464 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
466 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
467 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
468 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
470 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OPTIONS OR
471 ORDER OUT_P OUTER_P OVER OVERLAPS OVERLAY OWNED OWNER
473 PARSER PARTIAL PARTITION PASSWORD PLACING PLANS POSITION
474 PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
475 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
477 QUOTE
479 RANGE READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX
480 RELATIVE_P RELEASE RENAME REPEATABLE REPLACE REPLICA RESET RESTART
481 RESTRICT RETURNING RETURNS REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
483 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
484 SERIALIZABLE SERVER SESSION SESSION_USER SET SETOF SHARE
485 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
486 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
487 SYMMETRIC SYSID SYSTEM_P
489 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
490 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
491 TRUNCATE TRUSTED TYPE_P
493 UNBOUNDED UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
494 UPDATE USER USING
496 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
497 VERBOSE VERSION_P VIEW VOLATILE
499 WHEN WHERE WHITESPACE_P WINDOW WITH WITHOUT WORK WRAPPER WRITE
501 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
502 XMLPI XMLROOT XMLSERIALIZE
504 YEAR_P YES_P
506 ZONE
508 /* The grammar thinks these are keywords, but they are not in the keywords.c
509 * list and so can never be entered directly. The filter in parser.c
510 * creates these tokens when required.
512 %token NULLS_FIRST NULLS_LAST WITH_TIME
514 /* Special token types, not actually keywords - see the "lex" file */
515 %token <str> IDENT FCONST SCONST BCONST XCONST Op
516 %token <ival> ICONST PARAM
518 /* precedence: lowest to highest */
519 %nonassoc SET /* see relation_expr_opt_alias */
520 %left UNION EXCEPT
521 %left INTERSECT
522 %left OR
523 %left AND
524 %right NOT
525 %right '='
526 %nonassoc '<' '>'
527 %nonassoc LIKE ILIKE SIMILAR
528 %nonassoc ESCAPE
529 %nonassoc OVERLAPS
530 %nonassoc BETWEEN
531 %nonassoc IN_P
532 %left POSTFIXOP /* dummy for postfix Op rules */
534 * To support target_el without AS, we must give IDENT an explicit priority
535 * between POSTFIXOP and Op. We can safely assign the same priority to
536 * various unreserved keywords as needed to resolve ambiguities (this can't
537 * have any bad effects since obviously the keywords will still behave the
538 * same as if they weren't keywords). We need to do this for PARTITION,
539 * RANGE, ROWS to support opt_existing_window_name; and for RANGE, ROWS
540 * so that they can follow a_expr without creating
541 * postfix-operator problems.
543 %nonassoc IDENT PARTITION RANGE ROWS
544 %left Op OPERATOR /* multi-character ops and user-defined operators */
545 %nonassoc NOTNULL
546 %nonassoc ISNULL
547 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
548 %left '+' '-'
549 %left '*' '/' '%'
550 %left '^'
551 /* Unary Operators */
552 %left AT ZONE /* sets precedence for AT TIME ZONE */
553 %right UMINUS
554 %left '[' ']'
555 %left '(' ')'
556 %left TYPECAST
557 %left '.'
559 * These might seem to be low-precedence, but actually they are not part
560 * of the arithmetic hierarchy at all in their use as JOIN operators.
561 * We make them high-precedence to support their use as function names.
562 * They wouldn't be given a precedence at all, were it not that we need
563 * left-associativity among the JOIN rules themselves.
565 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
566 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
567 %right PRESERVE STRIP_P
571 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
572 * psql already handles such cases, but other interfaces don't.
573 * bjm 1999/10/05
575 stmtblock: stmtmulti { parsetree = $1; }
578 /* the thrashing around here is to discard "empty" statements... */
579 stmtmulti: stmtmulti ';' stmt
580 { if ($3 != NULL)
581 $$ = lappend($1, $3);
582 else
583 $$ = $1;
585 | stmt
586 { if ($1 != NULL)
587 $$ = list_make1($1);
588 else
589 $$ = NIL;
593 stmt :
594 AlterDatabaseStmt
595 | AlterDatabaseSetStmt
596 | AlterDomainStmt
597 | AlterFdwStmt
598 | AlterForeignServerStmt
599 | AlterFunctionStmt
600 | AlterGroupStmt
601 | AlterObjectSchemaStmt
602 | AlterOwnerStmt
603 | AlterSeqStmt
604 | AlterTableStmt
605 | AlterRoleSetStmt
606 | AlterRoleStmt
607 | AlterTSConfigurationStmt
608 | AlterTSDictionaryStmt
609 | AlterUserMappingStmt
610 | AlterUserSetStmt
611 | AlterUserStmt
612 | AnalyzeStmt
613 | CheckPointStmt
614 | ClosePortalStmt
615 | ClusterStmt
616 | CommentStmt
617 | ConstraintsSetStmt
618 | CopyStmt
619 | CreateAsStmt
620 | CreateAssertStmt
621 | CreateCastStmt
622 | CreateConversionStmt
623 | CreateDomainStmt
624 | CreateFdwStmt
625 | CreateForeignServerStmt
626 | CreateFunctionStmt
627 | CreateGroupStmt
628 | CreateOpClassStmt
629 | CreateOpFamilyStmt
630 | AlterOpFamilyStmt
631 | CreatePLangStmt
632 | CreateSchemaStmt
633 | CreateSeqStmt
634 | CreateStmt
635 | CreateTableSpaceStmt
636 | CreateTrigStmt
637 | CreateRoleStmt
638 | CreateUserStmt
639 | CreateUserMappingStmt
640 | CreatedbStmt
641 | DeallocateStmt
642 | DeclareCursorStmt
643 | DefineStmt
644 | DeleteStmt
645 | DiscardStmt
646 | DropAssertStmt
647 | DropCastStmt
648 | DropFdwStmt
649 | DropForeignServerStmt
650 | DropGroupStmt
651 | DropOpClassStmt
652 | DropOpFamilyStmt
653 | DropOwnedStmt
654 | DropPLangStmt
655 | DropRuleStmt
656 | DropStmt
657 | DropTableSpaceStmt
658 | DropTrigStmt
659 | DropRoleStmt
660 | DropUserStmt
661 | DropUserMappingStmt
662 | DropdbStmt
663 | ExecuteStmt
664 | ExplainStmt
665 | FetchStmt
666 | GrantStmt
667 | GrantRoleStmt
668 | IndexStmt
669 | InsertStmt
670 | ListenStmt
671 | LoadStmt
672 | LockStmt
673 | NotifyStmt
674 | PrepareStmt
675 | ReassignOwnedStmt
676 | ReindexStmt
677 | RemoveAggrStmt
678 | RemoveFuncStmt
679 | RemoveOperStmt
680 | RenameStmt
681 | RevokeStmt
682 | RevokeRoleStmt
683 | RuleStmt
684 | SelectStmt
685 | TransactionStmt
686 | TruncateStmt
687 | UnlistenStmt
688 | UpdateStmt
689 | VacuumStmt
690 | VariableResetStmt
691 | VariableSetStmt
692 | VariableShowStmt
693 | ViewStmt
694 | /*EMPTY*/
695 { $$ = NULL; }
698 /*****************************************************************************
700 * Create a new Postgres DBMS role
702 *****************************************************************************/
704 CreateRoleStmt:
705 CREATE ROLE RoleId opt_with OptRoleList
707 CreateRoleStmt *n = makeNode(CreateRoleStmt);
708 n->stmt_type = ROLESTMT_ROLE;
709 n->role = $3;
710 n->options = $5;
711 $$ = (Node *)n;
716 opt_with: WITH {}
717 | /*EMPTY*/ {}
721 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
722 * for backwards compatibility). Note: the only option required by SQL99
723 * is "WITH ADMIN name".
725 OptRoleList:
726 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
727 | /* EMPTY */ { $$ = NIL; }
730 OptRoleElem:
731 PASSWORD Sconst
733 $$ = makeDefElem("password",
734 (Node *)makeString($2));
736 | PASSWORD NULL_P
738 $$ = makeDefElem("password", NULL);
740 | ENCRYPTED PASSWORD Sconst
742 $$ = makeDefElem("encryptedPassword",
743 (Node *)makeString($3));
745 | UNENCRYPTED PASSWORD Sconst
747 $$ = makeDefElem("unencryptedPassword",
748 (Node *)makeString($3));
750 | SUPERUSER_P
752 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
754 | NOSUPERUSER
756 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
758 | INHERIT
760 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
762 | NOINHERIT
764 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
766 | CREATEDB
768 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
770 | NOCREATEDB
772 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
774 | CREATEROLE
776 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
778 | NOCREATEROLE
780 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
782 | CREATEUSER
784 /* For backwards compatibility, synonym for SUPERUSER */
785 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
787 | NOCREATEUSER
789 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
791 | LOGIN_P
793 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
795 | NOLOGIN_P
797 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
799 | CONNECTION LIMIT SignedIconst
801 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
803 | VALID UNTIL Sconst
805 $$ = makeDefElem("validUntil", (Node *)makeString($3));
807 /* Supported but not documented for roles, for use by ALTER GROUP. */
808 | USER name_list
810 $$ = makeDefElem("rolemembers", (Node *)$2);
812 /* The following are not supported by ALTER ROLE/USER/GROUP */
813 | SYSID Iconst
815 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
817 | ADMIN name_list
819 $$ = makeDefElem("adminmembers", (Node *)$2);
821 | ROLE name_list
823 $$ = makeDefElem("rolemembers", (Node *)$2);
825 | IN_P ROLE name_list
827 $$ = makeDefElem("addroleto", (Node *)$3);
829 | IN_P GROUP_P name_list
831 $$ = makeDefElem("addroleto", (Node *)$3);
836 /*****************************************************************************
838 * Create a new Postgres DBMS user (role with implied login ability)
840 *****************************************************************************/
842 CreateUserStmt:
843 CREATE USER RoleId opt_with OptRoleList
845 CreateRoleStmt *n = makeNode(CreateRoleStmt);
846 n->stmt_type = ROLESTMT_USER;
847 n->role = $3;
848 n->options = $5;
849 $$ = (Node *)n;
854 /*****************************************************************************
856 * Alter a postgresql DBMS role
858 *****************************************************************************/
860 AlterRoleStmt:
861 ALTER ROLE RoleId opt_with OptRoleList
863 AlterRoleStmt *n = makeNode(AlterRoleStmt);
864 n->role = $3;
865 n->action = +1; /* add, if there are members */
866 n->options = $5;
867 $$ = (Node *)n;
871 AlterRoleSetStmt:
872 ALTER ROLE RoleId SetResetClause
874 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
875 n->role = $3;
876 n->setstmt = $4;
877 $$ = (Node *)n;
882 /*****************************************************************************
884 * Alter a postgresql DBMS user
886 *****************************************************************************/
888 AlterUserStmt:
889 ALTER USER RoleId opt_with OptRoleList
891 AlterRoleStmt *n = makeNode(AlterRoleStmt);
892 n->role = $3;
893 n->action = +1; /* add, if there are members */
894 n->options = $5;
895 $$ = (Node *)n;
900 AlterUserSetStmt:
901 ALTER USER RoleId SetResetClause
903 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
904 n->role = $3;
905 n->setstmt = $4;
906 $$ = (Node *)n;
911 /*****************************************************************************
913 * Drop a postgresql DBMS role
915 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
916 * might own objects in multiple databases, there is presently no way to
917 * implement either cascading or restricting. Caveat DBA.
918 *****************************************************************************/
920 DropRoleStmt:
921 DROP ROLE name_list
923 DropRoleStmt *n = makeNode(DropRoleStmt);
924 n->missing_ok = FALSE;
925 n->roles = $3;
926 $$ = (Node *)n;
928 | DROP ROLE IF_P EXISTS name_list
930 DropRoleStmt *n = makeNode(DropRoleStmt);
931 n->missing_ok = TRUE;
932 n->roles = $5;
933 $$ = (Node *)n;
937 /*****************************************************************************
939 * Drop a postgresql DBMS user
941 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
942 * might own objects in multiple databases, there is presently no way to
943 * implement either cascading or restricting. Caveat DBA.
944 *****************************************************************************/
946 DropUserStmt:
947 DROP USER name_list
949 DropRoleStmt *n = makeNode(DropRoleStmt);
950 n->missing_ok = FALSE;
951 n->roles = $3;
952 $$ = (Node *)n;
954 | DROP USER IF_P EXISTS name_list
956 DropRoleStmt *n = makeNode(DropRoleStmt);
957 n->roles = $5;
958 n->missing_ok = TRUE;
959 $$ = (Node *)n;
964 /*****************************************************************************
966 * Create a postgresql group (role without login ability)
968 *****************************************************************************/
970 CreateGroupStmt:
971 CREATE GROUP_P RoleId opt_with OptRoleList
973 CreateRoleStmt *n = makeNode(CreateRoleStmt);
974 n->stmt_type = ROLESTMT_GROUP;
975 n->role = $3;
976 n->options = $5;
977 $$ = (Node *)n;
982 /*****************************************************************************
984 * Alter a postgresql group
986 *****************************************************************************/
988 AlterGroupStmt:
989 ALTER GROUP_P RoleId add_drop USER name_list
991 AlterRoleStmt *n = makeNode(AlterRoleStmt);
992 n->role = $3;
993 n->action = $4;
994 n->options = list_make1(makeDefElem("rolemembers",
995 (Node *)$6));
996 $$ = (Node *)n;
1000 add_drop: ADD_P { $$ = +1; }
1001 | DROP { $$ = -1; }
1005 /*****************************************************************************
1007 * Drop a postgresql group
1009 * XXX see above notes about cascading DROP USER; groups have same problem.
1010 *****************************************************************************/
1012 DropGroupStmt:
1013 DROP GROUP_P name_list
1015 DropRoleStmt *n = makeNode(DropRoleStmt);
1016 n->missing_ok = FALSE;
1017 n->roles = $3;
1018 $$ = (Node *)n;
1020 | DROP GROUP_P IF_P EXISTS name_list
1022 DropRoleStmt *n = makeNode(DropRoleStmt);
1023 n->missing_ok = TRUE;
1024 n->roles = $5;
1025 $$ = (Node *)n;
1030 /*****************************************************************************
1032 * Manipulate a schema
1034 *****************************************************************************/
1036 CreateSchemaStmt:
1037 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
1039 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1040 /* One can omit the schema name or the authorization id. */
1041 if ($3 != NULL)
1042 n->schemaname = $3;
1043 else
1044 n->schemaname = $5;
1045 n->authid = $5;
1046 n->schemaElts = $6;
1047 $$ = (Node *)n;
1049 | CREATE SCHEMA ColId OptSchemaEltList
1051 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1052 /* ...but not both */
1053 n->schemaname = $3;
1054 n->authid = NULL;
1055 n->schemaElts = $4;
1056 $$ = (Node *)n;
1060 OptSchemaName:
1061 ColId { $$ = $1; }
1062 | /* EMPTY */ { $$ = NULL; }
1065 OptSchemaEltList:
1066 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1067 | /* EMPTY */ { $$ = NIL; }
1071 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1072 * statement (in addition to by themselves).
1074 schema_stmt:
1075 CreateStmt
1076 | IndexStmt
1077 | CreateSeqStmt
1078 | CreateTrigStmt
1079 | GrantStmt
1080 | ViewStmt
1084 /*****************************************************************************
1086 * Set PG internal variable
1087 * SET name TO 'var_value'
1088 * Include SQL92 syntax (thomas 1997-10-22):
1089 * SET TIME ZONE 'var_value'
1091 *****************************************************************************/
1093 VariableSetStmt:
1094 SET set_rest
1096 VariableSetStmt *n = $2;
1097 n->is_local = false;
1098 $$ = (Node *) n;
1100 | SET LOCAL set_rest
1102 VariableSetStmt *n = $3;
1103 n->is_local = true;
1104 $$ = (Node *) n;
1106 | SET SESSION set_rest
1108 VariableSetStmt *n = $3;
1109 n->is_local = false;
1110 $$ = (Node *) n;
1114 set_rest: /* Generic SET syntaxes: */
1115 var_name TO var_list
1117 VariableSetStmt *n = makeNode(VariableSetStmt);
1118 n->kind = VAR_SET_VALUE;
1119 n->name = $1;
1120 n->args = $3;
1121 $$ = n;
1123 | var_name '=' var_list
1125 VariableSetStmt *n = makeNode(VariableSetStmt);
1126 n->kind = VAR_SET_VALUE;
1127 n->name = $1;
1128 n->args = $3;
1129 $$ = n;
1131 | var_name TO DEFAULT
1133 VariableSetStmt *n = makeNode(VariableSetStmt);
1134 n->kind = VAR_SET_DEFAULT;
1135 n->name = $1;
1136 $$ = n;
1138 | var_name '=' DEFAULT
1140 VariableSetStmt *n = makeNode(VariableSetStmt);
1141 n->kind = VAR_SET_DEFAULT;
1142 n->name = $1;
1143 $$ = n;
1145 | var_name FROM CURRENT_P
1147 VariableSetStmt *n = makeNode(VariableSetStmt);
1148 n->kind = VAR_SET_CURRENT;
1149 n->name = $1;
1150 $$ = n;
1152 /* Special syntaxes mandated by SQL standard: */
1153 | TIME ZONE zone_value
1155 VariableSetStmt *n = makeNode(VariableSetStmt);
1156 n->kind = VAR_SET_VALUE;
1157 n->name = "timezone";
1158 if ($3 != NULL)
1159 n->args = list_make1($3);
1160 else
1161 n->kind = VAR_SET_DEFAULT;
1162 $$ = n;
1164 | TRANSACTION transaction_mode_list
1166 VariableSetStmt *n = makeNode(VariableSetStmt);
1167 n->kind = VAR_SET_MULTI;
1168 n->name = "TRANSACTION";
1169 n->args = $2;
1170 $$ = n;
1172 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1174 VariableSetStmt *n = makeNode(VariableSetStmt);
1175 n->kind = VAR_SET_MULTI;
1176 n->name = "SESSION CHARACTERISTICS";
1177 n->args = $5;
1178 $$ = n;
1180 | CATALOG_P Sconst
1182 ereport(ERROR,
1183 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1184 errmsg("current database cannot be changed"),
1185 scanner_errposition(@2)));
1186 $$ = NULL; /*not reached*/
1188 | SCHEMA Sconst
1190 VariableSetStmt *n = makeNode(VariableSetStmt);
1191 n->kind = VAR_SET_VALUE;
1192 n->name = "search_path";
1193 n->args = list_make1(makeStringConst($2, @2));
1194 $$ = n;
1196 | NAMES opt_encoding
1198 VariableSetStmt *n = makeNode(VariableSetStmt);
1199 n->kind = VAR_SET_VALUE;
1200 n->name = "client_encoding";
1201 if ($2 != NULL)
1202 n->args = list_make1(makeStringConst($2, @2));
1203 else
1204 n->kind = VAR_SET_DEFAULT;
1205 $$ = n;
1207 | ROLE ColId_or_Sconst
1209 VariableSetStmt *n = makeNode(VariableSetStmt);
1210 n->kind = VAR_SET_VALUE;
1211 n->name = "role";
1212 n->args = list_make1(makeStringConst($2, @2));
1213 $$ = n;
1215 | SESSION AUTHORIZATION ColId_or_Sconst
1217 VariableSetStmt *n = makeNode(VariableSetStmt);
1218 n->kind = VAR_SET_VALUE;
1219 n->name = "session_authorization";
1220 n->args = list_make1(makeStringConst($3, @3));
1221 $$ = n;
1223 | SESSION AUTHORIZATION DEFAULT
1225 VariableSetStmt *n = makeNode(VariableSetStmt);
1226 n->kind = VAR_SET_DEFAULT;
1227 n->name = "session_authorization";
1228 $$ = n;
1230 | XML_P OPTION document_or_content
1232 VariableSetStmt *n = makeNode(VariableSetStmt);
1233 n->kind = VAR_SET_VALUE;
1234 n->name = "xmloption";
1235 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1236 $$ = n;
1240 var_name: ColId { $$ = $1; }
1241 | var_name '.' ColId
1243 $$ = palloc(strlen($1) + strlen($3) + 2);
1244 sprintf($$, "%s.%s", $1, $3);
1248 var_list: var_value { $$ = list_make1($1); }
1249 | var_list ',' var_value { $$ = lappend($1, $3); }
1252 var_value: opt_boolean
1253 { $$ = makeStringConst($1, @1); }
1254 | ColId_or_Sconst
1255 { $$ = makeStringConst($1, @1); }
1256 | NumericOnly
1257 { $$ = makeAConst($1, @1); }
1260 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1261 | READ COMMITTED { $$ = "read committed"; }
1262 | REPEATABLE READ { $$ = "repeatable read"; }
1263 | SERIALIZABLE { $$ = "serializable"; }
1266 opt_boolean:
1267 TRUE_P { $$ = "true"; }
1268 | FALSE_P { $$ = "false"; }
1269 | ON { $$ = "on"; }
1270 | OFF { $$ = "off"; }
1273 /* Timezone values can be:
1274 * - a string such as 'pst8pdt'
1275 * - an identifier such as "pst8pdt"
1276 * - an integer or floating point number
1277 * - a time interval per SQL99
1278 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1279 * so use IDENT (meaning we reject anything that is a key word).
1281 zone_value:
1282 Sconst
1284 $$ = makeStringConst($1, @1);
1286 | IDENT
1288 $$ = makeStringConst($1, @1);
1290 | ConstInterval Sconst opt_interval
1292 TypeName *t = $1;
1293 if ($3 != NIL)
1295 A_Const *n = (A_Const *) linitial($3);
1296 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1297 ereport(ERROR,
1298 (errcode(ERRCODE_SYNTAX_ERROR),
1299 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1300 scanner_errposition(@3)));
1302 t->typmods = $3;
1303 $$ = makeStringConstCast($2, @2, t);
1305 | ConstInterval '(' Iconst ')' Sconst opt_interval
1307 TypeName *t = $1;
1308 if ($6 != NIL)
1310 A_Const *n = (A_Const *) linitial($6);
1311 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1312 ereport(ERROR,
1313 (errcode(ERRCODE_SYNTAX_ERROR),
1314 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1315 scanner_errposition(@6)));
1316 if (list_length($6) != 1)
1317 ereport(ERROR,
1318 (errcode(ERRCODE_SYNTAX_ERROR),
1319 errmsg("interval precision specified twice"),
1320 scanner_errposition(@1)));
1321 t->typmods = lappend($6, makeIntConst($3, @3));
1323 else
1324 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1325 makeIntConst($3, @3));
1326 $$ = makeStringConstCast($5, @5, t);
1328 | NumericOnly { $$ = makeAConst($1, @1); }
1329 | DEFAULT { $$ = NULL; }
1330 | LOCAL { $$ = NULL; }
1333 opt_encoding:
1334 Sconst { $$ = $1; }
1335 | DEFAULT { $$ = NULL; }
1336 | /*EMPTY*/ { $$ = NULL; }
1339 ColId_or_Sconst:
1340 ColId { $$ = $1; }
1341 | Sconst { $$ = $1; }
1344 VariableResetStmt:
1345 RESET var_name
1347 VariableSetStmt *n = makeNode(VariableSetStmt);
1348 n->kind = VAR_RESET;
1349 n->name = $2;
1350 $$ = (Node *) n;
1352 | RESET TIME ZONE
1354 VariableSetStmt *n = makeNode(VariableSetStmt);
1355 n->kind = VAR_RESET;
1356 n->name = "timezone";
1357 $$ = (Node *) n;
1359 | RESET TRANSACTION ISOLATION LEVEL
1361 VariableSetStmt *n = makeNode(VariableSetStmt);
1362 n->kind = VAR_RESET;
1363 n->name = "transaction_isolation";
1364 $$ = (Node *) n;
1366 | RESET SESSION AUTHORIZATION
1368 VariableSetStmt *n = makeNode(VariableSetStmt);
1369 n->kind = VAR_RESET;
1370 n->name = "session_authorization";
1371 $$ = (Node *) n;
1373 | RESET ALL
1375 VariableSetStmt *n = makeNode(VariableSetStmt);
1376 n->kind = VAR_RESET_ALL;
1377 $$ = (Node *) n;
1381 /* SetResetClause allows SET or RESET without LOCAL */
1382 SetResetClause:
1383 SET set_rest { $$ = $2; }
1384 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1388 VariableShowStmt:
1389 SHOW var_name
1391 VariableShowStmt *n = makeNode(VariableShowStmt);
1392 n->name = $2;
1393 $$ = (Node *) n;
1395 | SHOW TIME ZONE
1397 VariableShowStmt *n = makeNode(VariableShowStmt);
1398 n->name = "timezone";
1399 $$ = (Node *) n;
1401 | SHOW TRANSACTION ISOLATION LEVEL
1403 VariableShowStmt *n = makeNode(VariableShowStmt);
1404 n->name = "transaction_isolation";
1405 $$ = (Node *) n;
1407 | SHOW SESSION AUTHORIZATION
1409 VariableShowStmt *n = makeNode(VariableShowStmt);
1410 n->name = "session_authorization";
1411 $$ = (Node *) n;
1413 | SHOW ALL
1415 VariableShowStmt *n = makeNode(VariableShowStmt);
1416 n->name = "all";
1417 $$ = (Node *) n;
1422 ConstraintsSetStmt:
1423 SET CONSTRAINTS constraints_set_list constraints_set_mode
1425 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1426 n->constraints = $3;
1427 n->deferred = $4;
1428 $$ = (Node *) n;
1432 constraints_set_list:
1433 ALL { $$ = NIL; }
1434 | qualified_name_list { $$ = $1; }
1437 constraints_set_mode:
1438 DEFERRED { $$ = TRUE; }
1439 | IMMEDIATE { $$ = FALSE; }
1444 * Checkpoint statement
1446 CheckPointStmt:
1447 CHECKPOINT
1449 CheckPointStmt *n = makeNode(CheckPointStmt);
1450 $$ = (Node *)n;
1455 /*****************************************************************************
1457 * DISCARD { ALL | TEMP | PLANS }
1459 *****************************************************************************/
1461 DiscardStmt:
1462 DISCARD ALL
1464 DiscardStmt *n = makeNode(DiscardStmt);
1465 n->target = DISCARD_ALL;
1466 $$ = (Node *) n;
1468 | DISCARD TEMP
1470 DiscardStmt *n = makeNode(DiscardStmt);
1471 n->target = DISCARD_TEMP;
1472 $$ = (Node *) n;
1474 | DISCARD TEMPORARY
1476 DiscardStmt *n = makeNode(DiscardStmt);
1477 n->target = DISCARD_TEMP;
1478 $$ = (Node *) n;
1480 | DISCARD PLANS
1482 DiscardStmt *n = makeNode(DiscardStmt);
1483 n->target = DISCARD_PLANS;
1484 $$ = (Node *) n;
1489 /*****************************************************************************
1491 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1493 * Note: we accept all subcommands for each of the four variants, and sort
1494 * out what's really legal at execution time.
1495 *****************************************************************************/
1497 AlterTableStmt:
1498 ALTER TABLE relation_expr alter_table_cmds
1500 AlterTableStmt *n = makeNode(AlterTableStmt);
1501 n->relation = $3;
1502 n->cmds = $4;
1503 n->relkind = OBJECT_TABLE;
1504 $$ = (Node *)n;
1506 | ALTER INDEX qualified_name alter_table_cmds
1508 AlterTableStmt *n = makeNode(AlterTableStmt);
1509 n->relation = $3;
1510 n->cmds = $4;
1511 n->relkind = OBJECT_INDEX;
1512 $$ = (Node *)n;
1514 | ALTER SEQUENCE qualified_name alter_table_cmds
1516 AlterTableStmt *n = makeNode(AlterTableStmt);
1517 n->relation = $3;
1518 n->cmds = $4;
1519 n->relkind = OBJECT_SEQUENCE;
1520 $$ = (Node *)n;
1522 | ALTER VIEW qualified_name alter_table_cmds
1524 AlterTableStmt *n = makeNode(AlterTableStmt);
1525 n->relation = $3;
1526 n->cmds = $4;
1527 n->relkind = OBJECT_VIEW;
1528 $$ = (Node *)n;
1532 alter_table_cmds:
1533 alter_table_cmd { $$ = list_make1($1); }
1534 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1537 alter_table_cmd:
1538 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1539 ADD_P opt_column columnDef
1541 AlterTableCmd *n = makeNode(AlterTableCmd);
1542 n->subtype = AT_AddColumn;
1543 n->def = $3;
1544 $$ = (Node *)n;
1546 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1547 | ALTER opt_column ColId alter_column_default
1549 AlterTableCmd *n = makeNode(AlterTableCmd);
1550 n->subtype = AT_ColumnDefault;
1551 n->name = $3;
1552 n->def = $4;
1553 $$ = (Node *)n;
1555 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1556 | ALTER opt_column ColId DROP NOT NULL_P
1558 AlterTableCmd *n = makeNode(AlterTableCmd);
1559 n->subtype = AT_DropNotNull;
1560 n->name = $3;
1561 $$ = (Node *)n;
1563 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1564 | ALTER opt_column ColId SET NOT NULL_P
1566 AlterTableCmd *n = makeNode(AlterTableCmd);
1567 n->subtype = AT_SetNotNull;
1568 n->name = $3;
1569 $$ = (Node *)n;
1571 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1572 | ALTER opt_column ColId SET STATISTICS SignedIconst
1574 AlterTableCmd *n = makeNode(AlterTableCmd);
1575 n->subtype = AT_SetStatistics;
1576 n->name = $3;
1577 n->def = (Node *) makeInteger($6);
1578 $$ = (Node *)n;
1580 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1581 | ALTER opt_column ColId SET STORAGE ColId
1583 AlterTableCmd *n = makeNode(AlterTableCmd);
1584 n->subtype = AT_SetStorage;
1585 n->name = $3;
1586 n->def = (Node *) makeString($6);
1587 $$ = (Node *)n;
1589 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1590 | DROP opt_column ColId opt_drop_behavior
1592 AlterTableCmd *n = makeNode(AlterTableCmd);
1593 n->subtype = AT_DropColumn;
1594 n->name = $3;
1595 n->behavior = $4;
1596 $$ = (Node *)n;
1599 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1600 * [ USING <expression> ]
1602 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1604 AlterTableCmd *n = makeNode(AlterTableCmd);
1605 n->subtype = AT_AlterColumnType;
1606 n->name = $3;
1607 n->def = (Node *) $6;
1608 n->transform = $7;
1609 $$ = (Node *)n;
1611 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1612 | ADD_P TableConstraint
1614 AlterTableCmd *n = makeNode(AlterTableCmd);
1615 n->subtype = AT_AddConstraint;
1616 n->def = $2;
1617 $$ = (Node *)n;
1619 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1620 | DROP CONSTRAINT name opt_drop_behavior
1622 AlterTableCmd *n = makeNode(AlterTableCmd);
1623 n->subtype = AT_DropConstraint;
1624 n->name = $3;
1625 n->behavior = $4;
1626 $$ = (Node *)n;
1628 /* ALTER TABLE <name> SET WITHOUT OIDS */
1629 | SET WITHOUT OIDS
1631 AlterTableCmd *n = makeNode(AlterTableCmd);
1632 n->subtype = AT_DropOids;
1633 $$ = (Node *)n;
1635 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1636 | CLUSTER ON name
1638 AlterTableCmd *n = makeNode(AlterTableCmd);
1639 n->subtype = AT_ClusterOn;
1640 n->name = $3;
1641 $$ = (Node *)n;
1643 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1644 | SET WITHOUT CLUSTER
1646 AlterTableCmd *n = makeNode(AlterTableCmd);
1647 n->subtype = AT_DropCluster;
1648 n->name = NULL;
1649 $$ = (Node *)n;
1651 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1652 | ENABLE_P TRIGGER name
1654 AlterTableCmd *n = makeNode(AlterTableCmd);
1655 n->subtype = AT_EnableTrig;
1656 n->name = $3;
1657 $$ = (Node *)n;
1659 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1660 | ENABLE_P ALWAYS TRIGGER name
1662 AlterTableCmd *n = makeNode(AlterTableCmd);
1663 n->subtype = AT_EnableAlwaysTrig;
1664 n->name = $4;
1665 $$ = (Node *)n;
1667 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1668 | ENABLE_P REPLICA TRIGGER name
1670 AlterTableCmd *n = makeNode(AlterTableCmd);
1671 n->subtype = AT_EnableReplicaTrig;
1672 n->name = $4;
1673 $$ = (Node *)n;
1675 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1676 | ENABLE_P TRIGGER ALL
1678 AlterTableCmd *n = makeNode(AlterTableCmd);
1679 n->subtype = AT_EnableTrigAll;
1680 $$ = (Node *)n;
1682 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1683 | ENABLE_P TRIGGER USER
1685 AlterTableCmd *n = makeNode(AlterTableCmd);
1686 n->subtype = AT_EnableTrigUser;
1687 $$ = (Node *)n;
1689 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1690 | DISABLE_P TRIGGER name
1692 AlterTableCmd *n = makeNode(AlterTableCmd);
1693 n->subtype = AT_DisableTrig;
1694 n->name = $3;
1695 $$ = (Node *)n;
1697 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1698 | DISABLE_P TRIGGER ALL
1700 AlterTableCmd *n = makeNode(AlterTableCmd);
1701 n->subtype = AT_DisableTrigAll;
1702 $$ = (Node *)n;
1704 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1705 | DISABLE_P TRIGGER USER
1707 AlterTableCmd *n = makeNode(AlterTableCmd);
1708 n->subtype = AT_DisableTrigUser;
1709 $$ = (Node *)n;
1711 /* ALTER TABLE <name> ENABLE RULE <rule> */
1712 | ENABLE_P RULE name
1714 AlterTableCmd *n = makeNode(AlterTableCmd);
1715 n->subtype = AT_EnableRule;
1716 n->name = $3;
1717 $$ = (Node *)n;
1719 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1720 | ENABLE_P ALWAYS RULE name
1722 AlterTableCmd *n = makeNode(AlterTableCmd);
1723 n->subtype = AT_EnableAlwaysRule;
1724 n->name = $4;
1725 $$ = (Node *)n;
1727 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1728 | ENABLE_P REPLICA RULE name
1730 AlterTableCmd *n = makeNode(AlterTableCmd);
1731 n->subtype = AT_EnableReplicaRule;
1732 n->name = $4;
1733 $$ = (Node *)n;
1735 /* ALTER TABLE <name> DISABLE RULE <rule> */
1736 | DISABLE_P RULE name
1738 AlterTableCmd *n = makeNode(AlterTableCmd);
1739 n->subtype = AT_DisableRule;
1740 n->name = $3;
1741 $$ = (Node *)n;
1743 /* ALTER TABLE <name> INHERIT <parent> */
1744 | INHERIT qualified_name
1746 AlterTableCmd *n = makeNode(AlterTableCmd);
1747 n->subtype = AT_AddInherit;
1748 n->def = (Node *) $2;
1749 $$ = (Node *)n;
1751 /* ALTER TABLE <name> NO INHERIT <parent> */
1752 | NO INHERIT qualified_name
1754 AlterTableCmd *n = makeNode(AlterTableCmd);
1755 n->subtype = AT_DropInherit;
1756 n->def = (Node *) $3;
1757 $$ = (Node *)n;
1759 /* ALTER TABLE <name> OWNER TO RoleId */
1760 | OWNER TO RoleId
1762 AlterTableCmd *n = makeNode(AlterTableCmd);
1763 n->subtype = AT_ChangeOwner;
1764 n->name = $3;
1765 $$ = (Node *)n;
1767 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1768 | SET TABLESPACE name
1770 AlterTableCmd *n = makeNode(AlterTableCmd);
1771 n->subtype = AT_SetTableSpace;
1772 n->name = $3;
1773 $$ = (Node *)n;
1775 /* ALTER TABLE <name> SET (...) */
1776 | SET definition
1778 AlterTableCmd *n = makeNode(AlterTableCmd);
1779 n->subtype = AT_SetRelOptions;
1780 n->def = (Node *)$2;
1781 $$ = (Node *)n;
1783 /* ALTER TABLE <name> RESET (...) */
1784 | RESET definition
1786 AlterTableCmd *n = makeNode(AlterTableCmd);
1787 n->subtype = AT_ResetRelOptions;
1788 n->def = (Node *)$2;
1789 $$ = (Node *)n;
1793 alter_column_default:
1794 SET DEFAULT a_expr { $$ = $3; }
1795 | DROP DEFAULT { $$ = NULL; }
1798 opt_drop_behavior:
1799 CASCADE { $$ = DROP_CASCADE; }
1800 | RESTRICT { $$ = DROP_RESTRICT; }
1801 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1804 alter_using:
1805 USING a_expr { $$ = $2; }
1806 | /* EMPTY */ { $$ = NULL; }
1811 /*****************************************************************************
1813 * QUERY :
1814 * close <portalname>
1816 *****************************************************************************/
1818 ClosePortalStmt:
1819 CLOSE name
1821 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1822 n->portalname = $2;
1823 $$ = (Node *)n;
1825 | CLOSE ALL
1827 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1828 n->portalname = NULL;
1829 $$ = (Node *)n;
1834 /*****************************************************************************
1836 * QUERY :
1837 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1839 * BINARY, OIDS, and DELIMITERS kept in old locations
1840 * for backward compatibility. 2002-06-18
1842 * COPY ( SELECT ... ) TO file [WITH options]
1843 * This form doesn't have the backwards-compatible option
1844 * syntax.
1846 *****************************************************************************/
1848 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1849 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1851 CopyStmt *n = makeNode(CopyStmt);
1852 n->relation = $3;
1853 n->query = NULL;
1854 n->attlist = $4;
1855 n->is_from = $6;
1856 n->filename = $7;
1858 n->options = NIL;
1859 /* Concatenate user-supplied flags */
1860 if ($2)
1861 n->options = lappend(n->options, $2);
1862 if ($5)
1863 n->options = lappend(n->options, $5);
1864 if ($8)
1865 n->options = lappend(n->options, $8);
1866 if ($10)
1867 n->options = list_concat(n->options, $10);
1868 $$ = (Node *)n;
1870 | COPY select_with_parens TO copy_file_name opt_with
1871 copy_opt_list
1873 CopyStmt *n = makeNode(CopyStmt);
1874 n->relation = NULL;
1875 n->query = $2;
1876 n->attlist = NIL;
1877 n->is_from = false;
1878 n->filename = $4;
1879 n->options = $6;
1880 $$ = (Node *)n;
1884 copy_from:
1885 FROM { $$ = TRUE; }
1886 | TO { $$ = FALSE; }
1890 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1891 * used depends on the direction. (It really doesn't make sense to copy from
1892 * stdout. We silently correct the "typo".) - AY 9/94
1894 copy_file_name:
1895 Sconst { $$ = $1; }
1896 | STDIN { $$ = NULL; }
1897 | STDOUT { $$ = NULL; }
1902 copy_opt_list:
1903 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1904 | /* EMPTY */ { $$ = NIL; }
1908 copy_opt_item:
1909 BINARY
1911 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1913 | OIDS
1915 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1917 | DELIMITER opt_as Sconst
1919 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1921 | NULL_P opt_as Sconst
1923 $$ = makeDefElem("null", (Node *)makeString($3));
1925 | CSV
1927 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1929 | HEADER_P
1931 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1933 | QUOTE opt_as Sconst
1935 $$ = makeDefElem("quote", (Node *)makeString($3));
1937 | ESCAPE opt_as Sconst
1939 $$ = makeDefElem("escape", (Node *)makeString($3));
1941 | FORCE QUOTE columnList
1943 $$ = makeDefElem("force_quote", (Node *)$3);
1945 | FORCE NOT NULL_P columnList
1947 $$ = makeDefElem("force_notnull", (Node *)$4);
1951 /* The following exist for backward compatibility */
1953 opt_binary:
1954 BINARY
1956 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1958 | /*EMPTY*/ { $$ = NULL; }
1961 opt_oids:
1962 WITH OIDS
1964 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1966 | /*EMPTY*/ { $$ = NULL; }
1969 copy_delimiter:
1970 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1971 opt_using DELIMITERS Sconst
1973 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1975 | /*EMPTY*/ { $$ = NULL; }
1978 opt_using:
1979 USING {}
1980 | /*EMPTY*/ {}
1984 /*****************************************************************************
1986 * QUERY :
1987 * CREATE TABLE relname
1989 *****************************************************************************/
1991 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1992 OptInherit OptWith OnCommitOption OptTableSpace
1994 CreateStmt *n = makeNode(CreateStmt);
1995 $4->istemp = $2;
1996 n->relation = $4;
1997 n->tableElts = $6;
1998 n->inhRelations = $8;
1999 n->constraints = NIL;
2000 n->options = $9;
2001 n->oncommit = $10;
2002 n->tablespacename = $11;
2003 $$ = (Node *)n;
2005 | CREATE OptTemp TABLE qualified_name OF qualified_name
2006 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
2008 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
2009 * by our inheritance capabilities. Let's try it...
2011 CreateStmt *n = makeNode(CreateStmt);
2012 $4->istemp = $2;
2013 n->relation = $4;
2014 n->tableElts = $8;
2015 n->inhRelations = list_make1($6);
2016 n->constraints = NIL;
2017 n->options = $10;
2018 n->oncommit = $11;
2019 n->tablespacename = $12;
2020 $$ = (Node *)n;
2025 * Redundancy here is needed to avoid shift/reduce conflicts,
2026 * since TEMP is not a reserved word. See also OptTempTableName.
2028 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
2029 * the LOCAL keyword is really meaningless.
2031 OptTemp: TEMPORARY { $$ = TRUE; }
2032 | TEMP { $$ = TRUE; }
2033 | LOCAL TEMPORARY { $$ = TRUE; }
2034 | LOCAL TEMP { $$ = TRUE; }
2035 | GLOBAL TEMPORARY { $$ = TRUE; }
2036 | GLOBAL TEMP { $$ = TRUE; }
2037 | /*EMPTY*/ { $$ = FALSE; }
2040 OptTableElementList:
2041 TableElementList { $$ = $1; }
2042 | /*EMPTY*/ { $$ = NIL; }
2045 TableElementList:
2046 TableElement
2048 $$ = list_make1($1);
2050 | TableElementList ',' TableElement
2052 $$ = lappend($1, $3);
2056 TableElement:
2057 columnDef { $$ = $1; }
2058 | TableLikeClause { $$ = $1; }
2059 | TableConstraint { $$ = $1; }
2062 columnDef: ColId Typename ColQualList
2064 ColumnDef *n = makeNode(ColumnDef);
2065 n->colname = $1;
2066 n->typename = $2;
2067 n->constraints = $3;
2068 n->is_local = true;
2069 $$ = (Node *)n;
2073 ColQualList:
2074 ColQualList ColConstraint { $$ = lappend($1, $2); }
2075 | /*EMPTY*/ { $$ = NIL; }
2078 ColConstraint:
2079 CONSTRAINT name ColConstraintElem
2081 switch (nodeTag($3))
2083 case T_Constraint:
2085 Constraint *n = (Constraint *)$3;
2086 n->name = $2;
2088 break;
2089 case T_FkConstraint:
2091 FkConstraint *n = (FkConstraint *)$3;
2092 n->constr_name = $2;
2094 break;
2095 default:
2096 break;
2098 $$ = $3;
2100 | ColConstraintElem { $$ = $1; }
2101 | ConstraintAttr { $$ = $1; }
2104 /* DEFAULT NULL is already the default for Postgres.
2105 * But define it here and carry it forward into the system
2106 * to make it explicit.
2107 * - thomas 1998-09-13
2109 * WITH NULL and NULL are not SQL92-standard syntax elements,
2110 * so leave them out. Use DEFAULT NULL to explicitly indicate
2111 * that a column may have that value. WITH NULL leads to
2112 * shift/reduce conflicts with WITH TIME ZONE anyway.
2113 * - thomas 1999-01-08
2115 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2116 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2117 * or be part of a_expr NOT LIKE or similar constructs).
2119 ColConstraintElem:
2120 NOT NULL_P
2122 Constraint *n = makeNode(Constraint);
2123 n->contype = CONSTR_NOTNULL;
2124 n->name = NULL;
2125 n->raw_expr = NULL;
2126 n->cooked_expr = NULL;
2127 n->keys = NULL;
2128 n->indexspace = NULL;
2129 $$ = (Node *)n;
2131 | NULL_P
2133 Constraint *n = makeNode(Constraint);
2134 n->contype = CONSTR_NULL;
2135 n->name = NULL;
2136 n->raw_expr = NULL;
2137 n->cooked_expr = NULL;
2138 n->keys = NULL;
2139 n->indexspace = NULL;
2140 $$ = (Node *)n;
2142 | UNIQUE opt_definition OptConsTableSpace
2144 Constraint *n = makeNode(Constraint);
2145 n->contype = CONSTR_UNIQUE;
2146 n->name = NULL;
2147 n->raw_expr = NULL;
2148 n->cooked_expr = NULL;
2149 n->keys = NULL;
2150 n->options = $2;
2151 n->indexspace = $3;
2152 $$ = (Node *)n;
2154 | PRIMARY KEY opt_definition OptConsTableSpace
2156 Constraint *n = makeNode(Constraint);
2157 n->contype = CONSTR_PRIMARY;
2158 n->name = NULL;
2159 n->raw_expr = NULL;
2160 n->cooked_expr = NULL;
2161 n->keys = NULL;
2162 n->options = $3;
2163 n->indexspace = $4;
2164 $$ = (Node *)n;
2166 | CHECK '(' a_expr ')'
2168 Constraint *n = makeNode(Constraint);
2169 n->contype = CONSTR_CHECK;
2170 n->name = NULL;
2171 n->raw_expr = $3;
2172 n->cooked_expr = NULL;
2173 n->keys = NULL;
2174 n->indexspace = NULL;
2175 $$ = (Node *)n;
2177 | DEFAULT b_expr
2179 Constraint *n = makeNode(Constraint);
2180 n->contype = CONSTR_DEFAULT;
2181 n->name = NULL;
2182 n->raw_expr = $2;
2183 n->cooked_expr = NULL;
2184 n->keys = NULL;
2185 n->indexspace = NULL;
2186 $$ = (Node *)n;
2188 | REFERENCES qualified_name opt_column_list key_match key_actions
2190 FkConstraint *n = makeNode(FkConstraint);
2191 n->constr_name = NULL;
2192 n->pktable = $2;
2193 n->fk_attrs = NIL;
2194 n->pk_attrs = $3;
2195 n->fk_matchtype = $4;
2196 n->fk_upd_action = (char) ($5 >> 8);
2197 n->fk_del_action = (char) ($5 & 0xFF);
2198 n->deferrable = FALSE;
2199 n->initdeferred = FALSE;
2200 $$ = (Node *)n;
2205 * ConstraintAttr represents constraint attributes, which we parse as if
2206 * they were independent constraint clauses, in order to avoid shift/reduce
2207 * conflicts (since NOT might start either an independent NOT NULL clause
2208 * or an attribute). parse_utilcmd.c is responsible for attaching the
2209 * attribute information to the preceding "real" constraint node, and for
2210 * complaining if attribute clauses appear in the wrong place or wrong
2211 * combinations.
2213 * See also ConstraintAttributeSpec, which can be used in places where
2214 * there is no parsing conflict.
2216 ConstraintAttr:
2217 DEFERRABLE
2219 Constraint *n = makeNode(Constraint);
2220 n->contype = CONSTR_ATTR_DEFERRABLE;
2221 $$ = (Node *)n;
2223 | NOT DEFERRABLE
2225 Constraint *n = makeNode(Constraint);
2226 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2227 $$ = (Node *)n;
2229 | INITIALLY DEFERRED
2231 Constraint *n = makeNode(Constraint);
2232 n->contype = CONSTR_ATTR_DEFERRED;
2233 $$ = (Node *)n;
2235 | INITIALLY IMMEDIATE
2237 Constraint *n = makeNode(Constraint);
2238 n->contype = CONSTR_ATTR_IMMEDIATE;
2239 $$ = (Node *)n;
2245 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2246 * This seems to be a poor man's inheritance capability, with the resulting
2247 * tables completely decoupled except for the original commonality in definitions.
2249 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2250 * which is a part of SQL:2003.
2252 TableLikeClause:
2253 LIKE qualified_name TableLikeOptionList
2255 InhRelation *n = makeNode(InhRelation);
2256 n->relation = $2;
2257 n->options = $3;
2258 $$ = (Node *)n;
2262 TableLikeOptionList:
2263 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2264 | /* EMPTY */ { $$ = NIL; }
2267 TableLikeOption:
2268 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2269 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2270 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2271 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2272 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2273 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2277 /* ConstraintElem specifies constraint syntax which is not embedded into
2278 * a column definition. ColConstraintElem specifies the embedded form.
2279 * - thomas 1997-12-03
2281 TableConstraint:
2282 CONSTRAINT name ConstraintElem
2284 switch (nodeTag($3))
2286 case T_Constraint:
2288 Constraint *n = (Constraint *)$3;
2289 n->name = $2;
2291 break;
2292 case T_FkConstraint:
2294 FkConstraint *n = (FkConstraint *)$3;
2295 n->constr_name = $2;
2297 break;
2298 default:
2299 break;
2301 $$ = $3;
2303 | ConstraintElem { $$ = $1; }
2306 ConstraintElem:
2307 CHECK '(' a_expr ')'
2309 Constraint *n = makeNode(Constraint);
2310 n->contype = CONSTR_CHECK;
2311 n->name = NULL;
2312 n->raw_expr = $3;
2313 n->cooked_expr = NULL;
2314 n->indexspace = NULL;
2315 $$ = (Node *)n;
2317 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2319 Constraint *n = makeNode(Constraint);
2320 n->contype = CONSTR_UNIQUE;
2321 n->name = NULL;
2322 n->raw_expr = NULL;
2323 n->cooked_expr = NULL;
2324 n->keys = $3;
2325 n->options = $5;
2326 n->indexspace = $6;
2327 $$ = (Node *)n;
2329 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2331 Constraint *n = makeNode(Constraint);
2332 n->contype = CONSTR_PRIMARY;
2333 n->name = NULL;
2334 n->raw_expr = NULL;
2335 n->cooked_expr = NULL;
2336 n->keys = $4;
2337 n->options = $6;
2338 n->indexspace = $7;
2339 $$ = (Node *)n;
2341 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2342 opt_column_list key_match key_actions ConstraintAttributeSpec
2344 FkConstraint *n = makeNode(FkConstraint);
2345 n->constr_name = NULL;
2346 n->pktable = $7;
2347 n->fk_attrs = $4;
2348 n->pk_attrs = $8;
2349 n->fk_matchtype = $9;
2350 n->fk_upd_action = (char) ($10 >> 8);
2351 n->fk_del_action = (char) ($10 & 0xFF);
2352 n->deferrable = ($11 & 1) != 0;
2353 n->initdeferred = ($11 & 2) != 0;
2354 $$ = (Node *)n;
2358 opt_column_list:
2359 '(' columnList ')' { $$ = $2; }
2360 | /*EMPTY*/ { $$ = NIL; }
2363 columnList:
2364 columnElem { $$ = list_make1($1); }
2365 | columnList ',' columnElem { $$ = lappend($1, $3); }
2368 columnElem: ColId
2370 $$ = (Node *) makeString($1);
2374 key_match: MATCH FULL
2376 $$ = FKCONSTR_MATCH_FULL;
2378 | MATCH PARTIAL
2380 ereport(ERROR,
2381 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2382 errmsg("MATCH PARTIAL not yet implemented"),
2383 scanner_errposition(@1)));
2384 $$ = FKCONSTR_MATCH_PARTIAL;
2386 | MATCH SIMPLE
2388 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2390 | /*EMPTY*/
2392 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2397 * We combine the update and delete actions into one value temporarily
2398 * for simplicity of parsing, and then break them down again in the
2399 * calling production. update is in the left 8 bits, delete in the right.
2400 * Note that NOACTION is the default.
2402 key_actions:
2403 key_update
2404 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2405 | key_delete
2406 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2407 | key_update key_delete
2408 { $$ = ($1 << 8) | ($2 & 0xFF); }
2409 | key_delete key_update
2410 { $$ = ($2 << 8) | ($1 & 0xFF); }
2411 | /*EMPTY*/
2412 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2415 key_update: ON UPDATE key_action { $$ = $3; }
2418 key_delete: ON DELETE_P key_action { $$ = $3; }
2421 key_action:
2422 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2423 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2424 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2425 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2426 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2429 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2430 | /*EMPTY*/ { $$ = NIL; }
2433 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2434 OptWith:
2435 WITH definition { $$ = $2; }
2436 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2437 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2438 | /*EMPTY*/ { $$ = NIL; }
2441 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2442 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2443 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2444 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2447 OptTableSpace: TABLESPACE name { $$ = $2; }
2448 | /*EMPTY*/ { $$ = NULL; }
2451 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2452 | /*EMPTY*/ { $$ = NULL; }
2457 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2458 * SELECT ... INTO.
2461 CreateAsStmt:
2462 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2465 * When the SelectStmt is a set-operation tree, we must
2466 * stuff the INTO information into the leftmost component
2467 * Select, because that's where analyze.c will expect
2468 * to find it. Similarly, the output column names must
2469 * be attached to that Select's target list.
2471 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2472 if (n->intoClause != NULL)
2473 ereport(ERROR,
2474 (errcode(ERRCODE_SYNTAX_ERROR),
2475 errmsg("CREATE TABLE AS cannot specify INTO"),
2476 scanner_errposition(exprLocation((Node *) n->intoClause))));
2477 $4->rel->istemp = $2;
2478 n->intoClause = $4;
2479 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2480 if (!$7)
2481 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2482 $$ = $6;
2486 create_as_target:
2487 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2489 $$ = makeNode(IntoClause);
2490 $$->rel = $1;
2491 $$->colNames = $2;
2492 $$->options = $3;
2493 $$->onCommit = $4;
2494 $$->tableSpaceName = $5;
2498 OptCreateAs:
2499 '(' CreateAsList ')' { $$ = $2; }
2500 | /*EMPTY*/ { $$ = NIL; }
2503 CreateAsList:
2504 CreateAsElement { $$ = list_make1($1); }
2505 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2508 CreateAsElement:
2509 ColId
2511 ColumnDef *n = makeNode(ColumnDef);
2512 n->colname = $1;
2513 n->typename = NULL;
2514 n->inhcount = 0;
2515 n->is_local = true;
2516 n->is_not_null = false;
2517 n->raw_default = NULL;
2518 n->cooked_default = NULL;
2519 n->constraints = NIL;
2520 $$ = (Node *)n;
2524 opt_with_data:
2525 WITH DATA_P { $$ = TRUE; }
2526 | WITH NO DATA_P { $$ = FALSE; }
2527 | /*EMPTY*/ { $$ = TRUE; }
2531 /*****************************************************************************
2533 * QUERY :
2534 * CREATE SEQUENCE seqname
2535 * ALTER SEQUENCE seqname
2537 *****************************************************************************/
2539 CreateSeqStmt:
2540 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2542 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2543 $4->istemp = $2;
2544 n->sequence = $4;
2545 n->options = $5;
2546 $$ = (Node *)n;
2550 AlterSeqStmt:
2551 ALTER SEQUENCE qualified_name SeqOptList
2553 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2554 n->sequence = $3;
2555 n->options = $4;
2556 $$ = (Node *)n;
2560 OptSeqOptList: SeqOptList { $$ = $1; }
2561 | /*EMPTY*/ { $$ = NIL; }
2564 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2565 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2568 SeqOptElem: CACHE NumericOnly
2570 $$ = makeDefElem("cache", (Node *)$2);
2572 | CYCLE
2574 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2576 | NO CYCLE
2578 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2580 | INCREMENT opt_by NumericOnly
2582 $$ = makeDefElem("increment", (Node *)$3);
2584 | MAXVALUE NumericOnly
2586 $$ = makeDefElem("maxvalue", (Node *)$2);
2588 | MINVALUE NumericOnly
2590 $$ = makeDefElem("minvalue", (Node *)$2);
2592 | NO MAXVALUE
2594 $$ = makeDefElem("maxvalue", NULL);
2596 | NO MINVALUE
2598 $$ = makeDefElem("minvalue", NULL);
2600 | OWNED BY any_name
2602 $$ = makeDefElem("owned_by", (Node *)$3);
2604 | START opt_with NumericOnly
2606 $$ = makeDefElem("start", (Node *)$3);
2608 | RESTART
2610 $$ = makeDefElem("restart", NULL);
2612 | RESTART opt_with NumericOnly
2614 $$ = makeDefElem("restart", (Node *)$3);
2618 opt_by: BY {}
2619 | /* empty */ {}
2622 NumericOnly:
2623 FCONST { $$ = makeFloat($1); }
2624 | '-' FCONST
2626 $$ = makeFloat($2);
2627 doNegateFloat($$);
2629 | SignedIconst { $$ = makeInteger($1); };
2632 /*****************************************************************************
2634 * QUERIES :
2635 * CREATE PROCEDURAL LANGUAGE ...
2636 * DROP PROCEDURAL LANGUAGE ...
2638 *****************************************************************************/
2640 CreatePLangStmt:
2641 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2643 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2644 n->plname = $5;
2645 /* parameters are all to be supplied by system */
2646 n->plhandler = NIL;
2647 n->plvalidator = NIL;
2648 n->pltrusted = false;
2649 $$ = (Node *)n;
2651 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2652 HANDLER handler_name opt_validator opt_lancompiler
2654 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2655 n->plname = $5;
2656 n->plhandler = $7;
2657 n->plvalidator = $8;
2658 n->pltrusted = $2;
2659 /* LANCOMPILER is now ignored entirely */
2660 $$ = (Node *)n;
2664 opt_trusted:
2665 TRUSTED { $$ = TRUE; }
2666 | /*EMPTY*/ { $$ = FALSE; }
2669 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2670 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2671 * Work around by using simple names, instead.
2673 handler_name:
2674 name { $$ = list_make1(makeString($1)); }
2675 | name attrs { $$ = lcons(makeString($1), $2); }
2678 opt_validator:
2679 VALIDATOR handler_name { $$ = $2; }
2680 | /*EMPTY*/ { $$ = NIL; }
2683 opt_lancompiler:
2684 LANCOMPILER Sconst { $$ = $2; }
2685 | /*EMPTY*/ { $$ = NULL; }
2688 DropPLangStmt:
2689 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2691 DropPLangStmt *n = makeNode(DropPLangStmt);
2692 n->plname = $4;
2693 n->behavior = $5;
2694 n->missing_ok = false;
2695 $$ = (Node *)n;
2697 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2699 DropPLangStmt *n = makeNode(DropPLangStmt);
2700 n->plname = $6;
2701 n->behavior = $7;
2702 n->missing_ok = true;
2703 $$ = (Node *)n;
2707 opt_procedural:
2708 PROCEDURAL {}
2709 | /*EMPTY*/ {}
2712 /*****************************************************************************
2714 * QUERY:
2715 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2717 *****************************************************************************/
2719 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2721 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2722 n->tablespacename = $3;
2723 n->owner = $4;
2724 n->location = $6;
2725 $$ = (Node *) n;
2729 OptTableSpaceOwner: OWNER name { $$ = $2; }
2730 | /*EMPTY */ { $$ = NULL; }
2733 /*****************************************************************************
2735 * QUERY :
2736 * DROP TABLESPACE <tablespace>
2738 * No need for drop behaviour as we cannot implement dependencies for
2739 * objects in other databases; we can only support RESTRICT.
2741 ****************************************************************************/
2743 DropTableSpaceStmt: DROP TABLESPACE name
2745 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2746 n->tablespacename = $3;
2747 n->missing_ok = false;
2748 $$ = (Node *) n;
2750 | DROP TABLESPACE IF_P EXISTS name
2752 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2753 n->tablespacename = $5;
2754 n->missing_ok = true;
2755 $$ = (Node *) n;
2759 /*****************************************************************************
2761 * QUERY:
2762 * CREATE FOREIGN DATA WRAPPER name LIBRARY 'library_name' LANGUAGE C
2764 *****************************************************************************/
2766 CreateFdwStmt: CREATE FOREIGN DATA_P WRAPPER name LIBRARY Sconst LANGUAGE ColId create_generic_options
2768 CreateFdwStmt *n = makeNode(CreateFdwStmt);
2769 n->fdwname = $5;
2770 n->library = $7;
2771 n->options = $10;
2772 $$ = (Node *) n;
2774 if (pg_strcasecmp($9, "C") != 0)
2775 ereport(ERROR,
2776 (errcode(ERRCODE_SYNTAX_ERROR),
2777 errmsg("language for foreign-data wrapper must be C"),
2778 scanner_errposition(@9)));
2782 /*****************************************************************************
2784 * QUERY :
2785 * DROP FOREIGN DATA WRAPPER name
2787 ****************************************************************************/
2789 DropFdwStmt: DROP FOREIGN DATA_P WRAPPER name opt_drop_behavior
2791 DropFdwStmt *n = makeNode(DropFdwStmt);
2792 n->fdwname = $5;
2793 n->missing_ok = false;
2794 n->behavior = $6;
2795 $$ = (Node *) n;
2797 | DROP FOREIGN DATA_P WRAPPER IF_P EXISTS name opt_drop_behavior
2799 DropFdwStmt *n = makeNode(DropFdwStmt);
2800 n->fdwname = $7;
2801 n->missing_ok = true;
2802 n->behavior = $8;
2803 $$ = (Node *) n;
2807 /*****************************************************************************
2809 * QUERY :
2810 * ALTER FOREIGN DATA WRAPPER name
2812 ****************************************************************************/
2814 AlterFdwStmt: ALTER FOREIGN DATA_P WRAPPER name LIBRARY Sconst alter_generic_options
2816 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2817 n->fdwname = $5;
2818 n->library = $7;
2819 n->options = $8;
2820 $$ = (Node *) n;
2822 | ALTER FOREIGN DATA_P WRAPPER name LIBRARY Sconst
2824 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2825 n->fdwname = $5;
2826 n->library = $7;
2827 $$ = (Node *) n;
2829 | ALTER FOREIGN DATA_P WRAPPER name alter_generic_options
2831 AlterFdwStmt *n = makeNode(AlterFdwStmt);
2832 n->fdwname = $5;
2833 n->options = $6;
2834 $$ = (Node *) n;
2838 /* Options definition for CREATE FDW, SERVER and USER MAPPING */
2839 create_generic_options:
2840 OPTIONS '(' generic_option_list ')' { $$ = $3; }
2841 | /*EMPTY*/ { $$ = NIL; }
2844 generic_option_list: generic_option_elem
2846 $$ = list_make1(makeOptionDefElem(ALTER_OPT_ADD, $1));
2848 | generic_option_list ',' generic_option_elem
2850 $$ = lappend($1, makeOptionDefElem(ALTER_OPT_ADD, $3));
2854 /* Options definition for ALTER FDW, SERVER and USER MAPPING */
2855 alter_generic_options:
2856 OPTIONS '(' alter_generic_option_list ')' { $$ = $3; }
2859 alter_generic_option_list:
2860 alter_generic_option_elem
2862 $$ = list_make1($1);
2864 | generic_option_elem
2866 $$ = list_make1(makeOptionDefElem(ALTER_OPT_ADD, $1));
2868 | alter_generic_option_list ',' alter_generic_option_elem
2870 $$ = lappend($1, $3);
2872 | alter_generic_option_list ',' generic_option_elem
2874 $$ = lappend($1, makeOptionDefElem(ALTER_OPT_ADD, $3));
2878 alter_generic_option_elem:
2879 ADD_P generic_option_elem
2881 $$ = makeOptionDefElem(ALTER_OPT_ADD, $2);
2883 | SET generic_option_elem
2885 $$ = makeOptionDefElem(ALTER_OPT_SET, $2);
2887 | DROP generic_option_name
2889 $$ = makeOptionDefElem(ALTER_OPT_DROP,
2890 makeDefElem($2, NULL));
2894 generic_option_elem:
2895 generic_option_name generic_option_arg { $$ = makeDefElem($1, $2); }
2898 generic_option_name:
2899 attr_name { $$ = $1; }
2902 generic_option_arg:
2903 Sconst { $$ = (Node *)makeString($1); }
2906 /*****************************************************************************
2908 * QUERY:
2909 * CREATE SERVER name [TYPE] [VERSION] [OPTIONS]
2911 *****************************************************************************/
2913 CreateForeignServerStmt: CREATE SERVER name opt_type opt_foreign_server_version
2914 FOREIGN DATA_P WRAPPER name create_generic_options
2916 CreateForeignServerStmt *n = makeNode(CreateForeignServerStmt);
2917 n->servername = $3;
2918 n->servertype = $4;
2919 n->version = $5;
2920 n->fdwname = $9;
2921 n->options = $10;
2922 $$ = (Node *) n;
2926 opt_type:
2927 TYPE_P Sconst { $$ = $2; }
2928 | /*EMPTY*/ { $$ = NULL; }
2932 foreign_server_version:
2933 VERSION_P Sconst { $$ = $2; }
2934 | VERSION_P NULL_P { $$ = NULL; }
2937 opt_foreign_server_version:
2938 foreign_server_version { $$ = $1; }
2939 | /*EMPTY*/ { $$ = NULL; }
2942 /*****************************************************************************
2944 * QUERY :
2945 * DROP SERVER name
2947 ****************************************************************************/
2949 DropForeignServerStmt: DROP SERVER name opt_drop_behavior
2951 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
2952 n->servername = $3;
2953 n->missing_ok = false;
2954 n->behavior = $4;
2955 $$ = (Node *) n;
2957 | DROP SERVER IF_P EXISTS name opt_drop_behavior
2959 DropForeignServerStmt *n = makeNode(DropForeignServerStmt);
2960 n->servername = $5;
2961 n->missing_ok = true;
2962 n->behavior = $6;
2963 $$ = (Node *) n;
2967 /*****************************************************************************
2969 * QUERY :
2970 * ALTER SERVER name [VERSION] [OPTIONS]
2972 ****************************************************************************/
2974 AlterForeignServerStmt: ALTER SERVER name foreign_server_version alter_generic_options
2976 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2977 n->servername = $3;
2978 n->version = $4;
2979 n->options = $5;
2980 n->has_version = true;
2981 $$ = (Node *) n;
2983 | ALTER SERVER name foreign_server_version
2985 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2986 n->servername = $3;
2987 n->version = $4;
2988 n->has_version = true;
2989 $$ = (Node *) n;
2991 | ALTER SERVER name alter_generic_options
2993 AlterForeignServerStmt *n = makeNode(AlterForeignServerStmt);
2994 n->servername = $3;
2995 n->options = $4;
2996 $$ = (Node *) n;
3000 /*****************************************************************************
3002 * QUERY:
3003 * CREATE USER MAPPING FOR auth_ident SERVER name [OPTIONS]
3005 *****************************************************************************/
3007 CreateUserMappingStmt: CREATE USER MAPPING FOR auth_ident SERVER name create_generic_options
3009 CreateUserMappingStmt *n = makeNode(CreateUserMappingStmt);
3010 n->username = $5;
3011 n->servername = $7;
3012 n->options = $8;
3013 $$ = (Node *) n;
3017 /* User mapping authorization identifier */
3018 auth_ident:
3019 CURRENT_USER { $$ = "current_user"; }
3020 | USER { $$ = "current_user"; }
3021 | RoleId { $$ = (strcmp($1, "public") == 0) ? NULL : $1; }
3024 /*****************************************************************************
3026 * QUERY :
3027 * DROP USER MAPPING FOR auth_ident SERVER name
3029 ****************************************************************************/
3031 DropUserMappingStmt: DROP USER MAPPING FOR auth_ident SERVER name
3033 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3034 n->username = $5;
3035 n->servername = $7;
3036 n->missing_ok = false;
3037 $$ = (Node *) n;
3039 | DROP USER MAPPING IF_P EXISTS FOR auth_ident SERVER name
3041 DropUserMappingStmt *n = makeNode(DropUserMappingStmt);
3042 n->username = $7;
3043 n->servername = $9;
3044 n->missing_ok = true;
3045 $$ = (Node *) n;
3049 /*****************************************************************************
3051 * QUERY :
3052 * ALTER USER MAPPING FOR auth_ident SERVER name OPTIONS
3054 ****************************************************************************/
3056 AlterUserMappingStmt: ALTER USER MAPPING FOR auth_ident SERVER name alter_generic_options
3058 AlterUserMappingStmt *n = makeNode(AlterUserMappingStmt);
3059 n->username = $5;
3060 n->servername = $7;
3061 n->options = $8;
3062 $$ = (Node *) n;
3066 /*****************************************************************************
3068 * QUERIES :
3069 * CREATE TRIGGER ...
3070 * DROP TRIGGER ...
3072 *****************************************************************************/
3074 CreateTrigStmt:
3075 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
3076 qualified_name TriggerForSpec EXECUTE PROCEDURE
3077 func_name '(' TriggerFuncArgs ')'
3079 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3080 n->trigname = $3;
3081 n->relation = $7;
3082 n->funcname = $11;
3083 n->args = $13;
3084 n->before = $4;
3085 n->row = $8;
3086 memcpy(n->actions, $5, 4);
3087 n->isconstraint = FALSE;
3088 n->deferrable = FALSE;
3089 n->initdeferred = FALSE;
3090 n->constrrel = NULL;
3091 $$ = (Node *)n;
3093 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
3094 qualified_name OptConstrFromTable
3095 ConstraintAttributeSpec
3096 FOR EACH ROW EXECUTE PROCEDURE
3097 func_name '(' TriggerFuncArgs ')'
3099 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3100 n->trigname = $4;
3101 n->relation = $8;
3102 n->funcname = $16;
3103 n->args = $18;
3104 n->before = FALSE;
3105 n->row = TRUE;
3106 memcpy(n->actions, $6, 4);
3107 n->isconstraint = TRUE;
3108 n->deferrable = ($10 & 1) != 0;
3109 n->initdeferred = ($10 & 2) != 0;
3111 n->constrrel = $9;
3112 $$ = (Node *)n;
3116 TriggerActionTime:
3117 BEFORE { $$ = TRUE; }
3118 | AFTER { $$ = FALSE; }
3121 TriggerEvents:
3122 TriggerOneEvent
3124 char *e = palloc(4);
3125 e[0] = $1; e[1] = '\0';
3126 $$ = e;
3128 | TriggerOneEvent OR TriggerOneEvent
3130 char *e = palloc(4);
3131 e[0] = $1; e[1] = $3; e[2] = '\0';
3132 $$ = e;
3134 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
3136 char *e = palloc(4);
3137 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
3138 $$ = e;
3142 TriggerOneEvent:
3143 INSERT { $$ = 'i'; }
3144 | DELETE_P { $$ = 'd'; }
3145 | UPDATE { $$ = 'u'; }
3146 | TRUNCATE { $$ = 't'; }
3149 TriggerForSpec:
3150 FOR TriggerForOpt TriggerForType
3152 $$ = $3;
3154 | /* EMPTY */
3157 * If ROW/STATEMENT not specified, default to
3158 * STATEMENT, per SQL
3160 $$ = FALSE;
3164 TriggerForOpt:
3165 EACH {}
3166 | /*EMPTY*/ {}
3169 TriggerForType:
3170 ROW { $$ = TRUE; }
3171 | STATEMENT { $$ = FALSE; }
3174 TriggerFuncArgs:
3175 TriggerFuncArg { $$ = list_make1($1); }
3176 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
3177 | /*EMPTY*/ { $$ = NIL; }
3180 TriggerFuncArg:
3181 Iconst
3183 char buf[64];
3184 snprintf(buf, sizeof(buf), "%d", $1);
3185 $$ = makeString(pstrdup(buf));
3187 | FCONST { $$ = makeString($1); }
3188 | Sconst { $$ = makeString($1); }
3189 | BCONST { $$ = makeString($1); }
3190 | XCONST { $$ = makeString($1); }
3191 | ColId { $$ = makeString($1); }
3194 OptConstrFromTable:
3195 FROM qualified_name { $$ = $2; }
3196 | /*EMPTY*/ { $$ = NULL; }
3199 ConstraintAttributeSpec:
3200 ConstraintDeferrabilitySpec
3201 { $$ = $1; }
3202 | ConstraintDeferrabilitySpec ConstraintTimeSpec
3204 if ($1 == 0 && $2 != 0)
3205 ereport(ERROR,
3206 (errcode(ERRCODE_SYNTAX_ERROR),
3207 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3208 scanner_errposition(@1)));
3209 $$ = $1 | $2;
3211 | ConstraintTimeSpec
3213 if ($1 != 0)
3214 $$ = 3;
3215 else
3216 $$ = 0;
3218 | ConstraintTimeSpec ConstraintDeferrabilitySpec
3220 if ($2 == 0 && $1 != 0)
3221 ereport(ERROR,
3222 (errcode(ERRCODE_SYNTAX_ERROR),
3223 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
3224 scanner_errposition(@1)));
3225 $$ = $1 | $2;
3227 | /*EMPTY*/
3228 { $$ = 0; }
3231 ConstraintDeferrabilitySpec:
3232 NOT DEFERRABLE { $$ = 0; }
3233 | DEFERRABLE { $$ = 1; }
3236 ConstraintTimeSpec:
3237 INITIALLY IMMEDIATE { $$ = 0; }
3238 | INITIALLY DEFERRED { $$ = 2; }
3242 DropTrigStmt:
3243 DROP TRIGGER name ON qualified_name opt_drop_behavior
3245 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3246 n->relation = $5;
3247 n->property = $3;
3248 n->behavior = $6;
3249 n->removeType = OBJECT_TRIGGER;
3250 n->missing_ok = false;
3251 $$ = (Node *) n;
3253 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
3255 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3256 n->relation = $7;
3257 n->property = $5;
3258 n->behavior = $8;
3259 n->removeType = OBJECT_TRIGGER;
3260 n->missing_ok = true;
3261 $$ = (Node *) n;
3266 /*****************************************************************************
3268 * QUERIES :
3269 * CREATE ASSERTION ...
3270 * DROP ASSERTION ...
3272 *****************************************************************************/
3274 CreateAssertStmt:
3275 CREATE ASSERTION name CHECK '(' a_expr ')'
3276 ConstraintAttributeSpec
3278 CreateTrigStmt *n = makeNode(CreateTrigStmt);
3279 n->trigname = $3;
3280 n->args = list_make1($6);
3281 n->isconstraint = TRUE;
3282 n->deferrable = ($8 & 1) != 0;
3283 n->initdeferred = ($8 & 2) != 0;
3285 ereport(ERROR,
3286 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3287 errmsg("CREATE ASSERTION is not yet implemented")));
3289 $$ = (Node *)n;
3293 DropAssertStmt:
3294 DROP ASSERTION name opt_drop_behavior
3296 DropPropertyStmt *n = makeNode(DropPropertyStmt);
3297 n->relation = NULL;
3298 n->property = $3;
3299 n->behavior = $4;
3300 n->removeType = OBJECT_TRIGGER; /* XXX */
3301 ereport(ERROR,
3302 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3303 errmsg("DROP ASSERTION is not yet implemented")));
3304 $$ = (Node *) n;
3309 /*****************************************************************************
3311 * QUERY :
3312 * define (aggregate,operator,type)
3314 *****************************************************************************/
3316 DefineStmt:
3317 CREATE AGGREGATE func_name aggr_args definition
3319 DefineStmt *n = makeNode(DefineStmt);
3320 n->kind = OBJECT_AGGREGATE;
3321 n->oldstyle = false;
3322 n->defnames = $3;
3323 n->args = $4;
3324 n->definition = $5;
3325 $$ = (Node *)n;
3327 | CREATE AGGREGATE func_name old_aggr_definition
3329 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
3330 DefineStmt *n = makeNode(DefineStmt);
3331 n->kind = OBJECT_AGGREGATE;
3332 n->oldstyle = true;
3333 n->defnames = $3;
3334 n->args = NIL;
3335 n->definition = $4;
3336 $$ = (Node *)n;
3338 | CREATE OPERATOR any_operator definition
3340 DefineStmt *n = makeNode(DefineStmt);
3341 n->kind = OBJECT_OPERATOR;
3342 n->oldstyle = false;
3343 n->defnames = $3;
3344 n->args = NIL;
3345 n->definition = $4;
3346 $$ = (Node *)n;
3348 | CREATE TYPE_P any_name definition
3350 DefineStmt *n = makeNode(DefineStmt);
3351 n->kind = OBJECT_TYPE;
3352 n->oldstyle = false;
3353 n->defnames = $3;
3354 n->args = NIL;
3355 n->definition = $4;
3356 $$ = (Node *)n;
3358 | CREATE TYPE_P any_name
3360 /* Shell type (identified by lack of definition) */
3361 DefineStmt *n = makeNode(DefineStmt);
3362 n->kind = OBJECT_TYPE;
3363 n->oldstyle = false;
3364 n->defnames = $3;
3365 n->args = NIL;
3366 n->definition = NIL;
3367 $$ = (Node *)n;
3369 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3371 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3372 RangeVar *r = makeNode(RangeVar);
3374 /* can't use qualified_name, sigh */
3375 switch (list_length($3))
3377 case 1:
3378 r->catalogname = NULL;
3379 r->schemaname = NULL;
3380 r->relname = strVal(linitial($3));
3381 break;
3382 case 2:
3383 r->catalogname = NULL;
3384 r->schemaname = strVal(linitial($3));
3385 r->relname = strVal(lsecond($3));
3386 break;
3387 case 3:
3388 r->catalogname = strVal(linitial($3));
3389 r->schemaname = strVal(lsecond($3));
3390 r->relname = strVal(lthird($3));
3391 break;
3392 default:
3393 ereport(ERROR,
3394 (errcode(ERRCODE_SYNTAX_ERROR),
3395 errmsg("improper qualified name (too many dotted names): %s",
3396 NameListToString($3)),
3397 scanner_errposition(@3)));
3398 break;
3400 r->location = @3;
3401 n->typevar = r;
3402 n->coldeflist = $6;
3403 $$ = (Node *)n;
3405 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3407 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3408 n->typename = $3;
3409 n->vals = $7;
3410 $$ = (Node *)n;
3412 | CREATE TEXT_P SEARCH PARSER any_name definition
3414 DefineStmt *n = makeNode(DefineStmt);
3415 n->kind = OBJECT_TSPARSER;
3416 n->args = NIL;
3417 n->defnames = $5;
3418 n->definition = $6;
3419 $$ = (Node *)n;
3421 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3423 DefineStmt *n = makeNode(DefineStmt);
3424 n->kind = OBJECT_TSDICTIONARY;
3425 n->args = NIL;
3426 n->defnames = $5;
3427 n->definition = $6;
3428 $$ = (Node *)n;
3430 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3432 DefineStmt *n = makeNode(DefineStmt);
3433 n->kind = OBJECT_TSTEMPLATE;
3434 n->args = NIL;
3435 n->defnames = $5;
3436 n->definition = $6;
3437 $$ = (Node *)n;
3439 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3441 DefineStmt *n = makeNode(DefineStmt);
3442 n->kind = OBJECT_TSCONFIGURATION;
3443 n->args = NIL;
3444 n->defnames = $5;
3445 n->definition = $6;
3446 $$ = (Node *)n;
3450 definition: '(' def_list ')' { $$ = $2; }
3453 def_list: def_elem { $$ = list_make1($1); }
3454 | def_list ',' def_elem { $$ = lappend($1, $3); }
3457 def_elem: ColLabel '=' def_arg
3459 $$ = makeDefElem($1, (Node *)$3);
3461 | ColLabel
3463 $$ = makeDefElem($1, NULL);
3467 /* Note: any simple identifier will be returned as a type name! */
3468 def_arg: func_type { $$ = (Node *)$1; }
3469 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3470 | qual_all_Op { $$ = (Node *)$1; }
3471 | NumericOnly { $$ = (Node *)$1; }
3472 | Sconst { $$ = (Node *)makeString($1); }
3475 aggr_args: '(' type_list ')' { $$ = $2; }
3476 | '(' '*' ')' { $$ = NIL; }
3479 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3482 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3483 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3487 * Must use IDENT here to avoid reduce/reduce conflicts; fortunately none of
3488 * the item names needed in old aggregate definitions are likely to become
3489 * SQL keywords.
3491 old_aggr_elem: IDENT '=' def_arg
3493 $$ = makeDefElem($1, (Node *)$3);
3497 enum_val_list: Sconst
3498 { $$ = list_make1(makeString($1)); }
3499 | enum_val_list ',' Sconst
3500 { $$ = lappend($1, makeString($3)); }
3504 /*****************************************************************************
3506 * QUERIES :
3507 * CREATE OPERATOR CLASS ...
3508 * CREATE OPERATOR FAMILY ...
3509 * ALTER OPERATOR FAMILY ...
3510 * DROP OPERATOR CLASS ...
3511 * DROP OPERATOR FAMILY ...
3513 *****************************************************************************/
3515 CreateOpClassStmt:
3516 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3517 USING access_method opt_opfamily AS opclass_item_list
3519 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3520 n->opclassname = $4;
3521 n->isDefault = $5;
3522 n->datatype = $8;
3523 n->amname = $10;
3524 n->opfamilyname = $11;
3525 n->items = $13;
3526 $$ = (Node *) n;
3530 opclass_item_list:
3531 opclass_item { $$ = list_make1($1); }
3532 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3535 opclass_item:
3536 OPERATOR Iconst any_operator opt_recheck
3538 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3539 n->itemtype = OPCLASS_ITEM_OPERATOR;
3540 n->name = $3;
3541 n->args = NIL;
3542 n->number = $2;
3543 $$ = (Node *) n;
3545 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3547 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3548 n->itemtype = OPCLASS_ITEM_OPERATOR;
3549 n->name = $3;
3550 n->args = $4;
3551 n->number = $2;
3552 $$ = (Node *) n;
3554 | FUNCTION Iconst func_name func_args
3556 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3557 n->itemtype = OPCLASS_ITEM_FUNCTION;
3558 n->name = $3;
3559 n->args = extractArgTypes($4);
3560 n->number = $2;
3561 $$ = (Node *) n;
3563 | FUNCTION Iconst '(' type_list ')' func_name func_args
3565 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3566 n->itemtype = OPCLASS_ITEM_FUNCTION;
3567 n->name = $6;
3568 n->args = extractArgTypes($7);
3569 n->number = $2;
3570 n->class_args = $4;
3571 $$ = (Node *) n;
3573 | STORAGE Typename
3575 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3576 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3577 n->storedtype = $2;
3578 $$ = (Node *) n;
3582 opt_default: DEFAULT { $$ = TRUE; }
3583 | /*EMPTY*/ { $$ = FALSE; }
3586 opt_opfamily: FAMILY any_name { $$ = $2; }
3587 | /*EMPTY*/ { $$ = NIL; }
3590 opt_recheck: RECHECK
3592 ereport(ERROR,
3593 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3594 errmsg("RECHECK is no longer supported"),
3595 errhint("Update your data type."),
3596 scanner_errposition(@1)));
3597 $$ = TRUE;
3599 | /*EMPTY*/ { $$ = FALSE; }
3603 CreateOpFamilyStmt:
3604 CREATE OPERATOR FAMILY any_name USING access_method
3606 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3607 n->opfamilyname = $4;
3608 n->amname = $6;
3609 $$ = (Node *) n;
3613 AlterOpFamilyStmt:
3614 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3616 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3617 n->opfamilyname = $4;
3618 n->amname = $6;
3619 n->isDrop = false;
3620 n->items = $8;
3621 $$ = (Node *) n;
3623 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3625 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3626 n->opfamilyname = $4;
3627 n->amname = $6;
3628 n->isDrop = true;
3629 n->items = $8;
3630 $$ = (Node *) n;
3634 opclass_drop_list:
3635 opclass_drop { $$ = list_make1($1); }
3636 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3639 opclass_drop:
3640 OPERATOR Iconst '(' type_list ')'
3642 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3643 n->itemtype = OPCLASS_ITEM_OPERATOR;
3644 n->number = $2;
3645 n->args = $4;
3646 $$ = (Node *) n;
3648 | FUNCTION Iconst '(' type_list ')'
3650 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3651 n->itemtype = OPCLASS_ITEM_FUNCTION;
3652 n->number = $2;
3653 n->args = $4;
3654 $$ = (Node *) n;
3659 DropOpClassStmt:
3660 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3662 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3663 n->opclassname = $4;
3664 n->amname = $6;
3665 n->behavior = $7;
3666 n->missing_ok = false;
3667 $$ = (Node *) n;
3669 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3671 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3672 n->opclassname = $6;
3673 n->amname = $8;
3674 n->behavior = $9;
3675 n->missing_ok = true;
3676 $$ = (Node *) n;
3680 DropOpFamilyStmt:
3681 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3683 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3684 n->opfamilyname = $4;
3685 n->amname = $6;
3686 n->behavior = $7;
3687 n->missing_ok = false;
3688 $$ = (Node *) n;
3690 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3692 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3693 n->opfamilyname = $6;
3694 n->amname = $8;
3695 n->behavior = $9;
3696 n->missing_ok = true;
3697 $$ = (Node *) n;
3702 /*****************************************************************************
3704 * QUERY:
3706 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3707 * REASSIGN OWNED BY username [, username ...] TO username
3709 *****************************************************************************/
3710 DropOwnedStmt:
3711 DROP OWNED BY name_list opt_drop_behavior
3713 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3714 n->roles = $4;
3715 n->behavior = $5;
3716 $$ = (Node *)n;
3720 ReassignOwnedStmt:
3721 REASSIGN OWNED BY name_list TO name
3723 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3724 n->roles = $4;
3725 n->newrole = $6;
3726 $$ = (Node *)n;
3730 /*****************************************************************************
3732 * QUERY:
3734 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3735 * [ RESTRICT | CASCADE ]
3737 *****************************************************************************/
3739 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3741 DropStmt *n = makeNode(DropStmt);
3742 n->removeType = $2;
3743 n->missing_ok = TRUE;
3744 n->objects = $5;
3745 n->behavior = $6;
3746 $$ = (Node *)n;
3748 | DROP drop_type any_name_list opt_drop_behavior
3750 DropStmt *n = makeNode(DropStmt);
3751 n->removeType = $2;
3752 n->missing_ok = FALSE;
3753 n->objects = $3;
3754 n->behavior = $4;
3755 $$ = (Node *)n;
3760 drop_type: TABLE { $$ = OBJECT_TABLE; }
3761 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3762 | VIEW { $$ = OBJECT_VIEW; }
3763 | INDEX { $$ = OBJECT_INDEX; }
3764 | TYPE_P { $$ = OBJECT_TYPE; }
3765 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3766 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3767 | SCHEMA { $$ = OBJECT_SCHEMA; }
3768 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3769 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3770 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3771 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3774 any_name_list:
3775 any_name { $$ = list_make1($1); }
3776 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3779 any_name: ColId { $$ = list_make1(makeString($1)); }
3780 | ColId attrs { $$ = lcons(makeString($1), $2); }
3783 attrs: '.' attr_name
3784 { $$ = list_make1(makeString($2)); }
3785 | attrs '.' attr_name
3786 { $$ = lappend($1, makeString($3)); }
3790 /*****************************************************************************
3792 * QUERY:
3793 * truncate table relname1, relname2, ...
3795 *****************************************************************************/
3797 TruncateStmt:
3798 TRUNCATE opt_table relation_expr_list opt_restart_seqs opt_drop_behavior
3800 TruncateStmt *n = makeNode(TruncateStmt);
3801 n->relations = $3;
3802 n->restart_seqs = $4;
3803 n->behavior = $5;
3804 $$ = (Node *)n;
3808 opt_restart_seqs:
3809 CONTINUE_P IDENTITY_P { $$ = false; }
3810 | RESTART IDENTITY_P { $$ = true; }
3811 | /* EMPTY */ { $$ = false; }
3814 /*****************************************************************************
3816 * The COMMENT ON statement can take different forms based upon the type of
3817 * the object associated with the comment. The form of the statement is:
3819 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3820 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3821 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3822 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3823 * TEXT SEARCH TEMPLATE |
3824 * TEXT SEARCH CONFIGURATION ] <objname> |
3825 * AGGREGATE <aggname> (arg1, ...) |
3826 * FUNCTION <funcname> (arg1, arg2, ...) |
3827 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3828 * TRIGGER <triggername> ON <relname> |
3829 * CONSTRAINT <constraintname> ON <relname> |
3830 * RULE <rulename> ON <relname> ]
3831 * IS 'text'
3833 *****************************************************************************/
3835 CommentStmt:
3836 COMMENT ON comment_type any_name IS comment_text
3838 CommentStmt *n = makeNode(CommentStmt);
3839 n->objtype = $3;
3840 n->objname = $4;
3841 n->objargs = NIL;
3842 n->comment = $6;
3843 $$ = (Node *) n;
3845 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3847 CommentStmt *n = makeNode(CommentStmt);
3848 n->objtype = OBJECT_AGGREGATE;
3849 n->objname = $4;
3850 n->objargs = $5;
3851 n->comment = $7;
3852 $$ = (Node *) n;
3854 | COMMENT ON FUNCTION func_name func_args IS comment_text
3856 CommentStmt *n = makeNode(CommentStmt);
3857 n->objtype = OBJECT_FUNCTION;
3858 n->objname = $4;
3859 n->objargs = extractArgTypes($5);
3860 n->comment = $7;
3861 $$ = (Node *) n;
3863 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3865 CommentStmt *n = makeNode(CommentStmt);
3866 n->objtype = OBJECT_OPERATOR;
3867 n->objname = $4;
3868 n->objargs = $5;
3869 n->comment = $7;
3870 $$ = (Node *) n;
3872 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3874 CommentStmt *n = makeNode(CommentStmt);
3875 n->objtype = OBJECT_CONSTRAINT;
3876 n->objname = lappend($6, makeString($4));
3877 n->objargs = NIL;
3878 n->comment = $8;
3879 $$ = (Node *) n;
3881 | COMMENT ON RULE name ON any_name IS comment_text
3883 CommentStmt *n = makeNode(CommentStmt);
3884 n->objtype = OBJECT_RULE;
3885 n->objname = lappend($6, makeString($4));
3886 n->objargs = NIL;
3887 n->comment = $8;
3888 $$ = (Node *) n;
3890 | COMMENT ON RULE name IS comment_text
3892 /* Obsolete syntax supported for awhile for compatibility */
3893 CommentStmt *n = makeNode(CommentStmt);
3894 n->objtype = OBJECT_RULE;
3895 n->objname = list_make1(makeString($4));
3896 n->objargs = NIL;
3897 n->comment = $6;
3898 $$ = (Node *) n;
3900 | COMMENT ON TRIGGER name ON any_name IS comment_text
3902 CommentStmt *n = makeNode(CommentStmt);
3903 n->objtype = OBJECT_TRIGGER;
3904 n->objname = lappend($6, makeString($4));
3905 n->objargs = NIL;
3906 n->comment = $8;
3907 $$ = (Node *) n;
3909 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3911 CommentStmt *n = makeNode(CommentStmt);
3912 n->objtype = OBJECT_OPCLASS;
3913 n->objname = $5;
3914 n->objargs = list_make1(makeString($7));
3915 n->comment = $9;
3916 $$ = (Node *) n;
3918 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3920 CommentStmt *n = makeNode(CommentStmt);
3921 n->objtype = OBJECT_OPFAMILY;
3922 n->objname = $5;
3923 n->objargs = list_make1(makeString($7));
3924 n->comment = $9;
3925 $$ = (Node *) n;
3927 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3929 CommentStmt *n = makeNode(CommentStmt);
3930 n->objtype = OBJECT_LARGEOBJECT;
3931 n->objname = list_make1($5);
3932 n->objargs = NIL;
3933 n->comment = $7;
3934 $$ = (Node *) n;
3936 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3938 CommentStmt *n = makeNode(CommentStmt);
3939 n->objtype = OBJECT_CAST;
3940 n->objname = list_make1($5);
3941 n->objargs = list_make1($7);
3942 n->comment = $10;
3943 $$ = (Node *) n;
3945 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3947 CommentStmt *n = makeNode(CommentStmt);
3948 n->objtype = OBJECT_LANGUAGE;
3949 n->objname = $5;
3950 n->objargs = NIL;
3951 n->comment = $7;
3952 $$ = (Node *) n;
3954 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3956 CommentStmt *n = makeNode(CommentStmt);
3957 n->objtype = OBJECT_TSPARSER;
3958 n->objname = $6;
3959 n->comment = $8;
3960 $$ = (Node *) n;
3962 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3964 CommentStmt *n = makeNode(CommentStmt);
3965 n->objtype = OBJECT_TSDICTIONARY;
3966 n->objname = $6;
3967 n->comment = $8;
3968 $$ = (Node *) n;
3970 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3972 CommentStmt *n = makeNode(CommentStmt);
3973 n->objtype = OBJECT_TSTEMPLATE;
3974 n->objname = $6;
3975 n->comment = $8;
3976 $$ = (Node *) n;
3978 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3980 CommentStmt *n = makeNode(CommentStmt);
3981 n->objtype = OBJECT_TSCONFIGURATION;
3982 n->objname = $6;
3983 n->comment = $8;
3984 $$ = (Node *) n;
3988 comment_type:
3989 COLUMN { $$ = OBJECT_COLUMN; }
3990 | DATABASE { $$ = OBJECT_DATABASE; }
3991 | SCHEMA { $$ = OBJECT_SCHEMA; }
3992 | INDEX { $$ = OBJECT_INDEX; }
3993 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3994 | TABLE { $$ = OBJECT_TABLE; }
3995 | DOMAIN_P { $$ = OBJECT_TYPE; }
3996 | TYPE_P { $$ = OBJECT_TYPE; }
3997 | VIEW { $$ = OBJECT_VIEW; }
3998 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3999 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
4000 | ROLE { $$ = OBJECT_ROLE; }
4003 comment_text:
4004 Sconst { $$ = $1; }
4005 | NULL_P { $$ = NULL; }
4008 /*****************************************************************************
4010 * QUERY:
4011 * fetch/move
4013 *****************************************************************************/
4015 FetchStmt: FETCH fetch_direction from_in name
4017 FetchStmt *n = (FetchStmt *) $2;
4018 n->portalname = $4;
4019 n->ismove = FALSE;
4020 $$ = (Node *)n;
4022 | FETCH name
4024 FetchStmt *n = makeNode(FetchStmt);
4025 n->direction = FETCH_FORWARD;
4026 n->howMany = 1;
4027 n->portalname = $2;
4028 n->ismove = FALSE;
4029 $$ = (Node *)n;
4031 | MOVE fetch_direction from_in name
4033 FetchStmt *n = (FetchStmt *) $2;
4034 n->portalname = $4;
4035 n->ismove = TRUE;
4036 $$ = (Node *)n;
4038 | MOVE name
4040 FetchStmt *n = makeNode(FetchStmt);
4041 n->direction = FETCH_FORWARD;
4042 n->howMany = 1;
4043 n->portalname = $2;
4044 n->ismove = TRUE;
4045 $$ = (Node *)n;
4049 fetch_direction:
4050 /*EMPTY*/
4052 FetchStmt *n = makeNode(FetchStmt);
4053 n->direction = FETCH_FORWARD;
4054 n->howMany = 1;
4055 $$ = (Node *)n;
4057 | NEXT
4059 FetchStmt *n = makeNode(FetchStmt);
4060 n->direction = FETCH_FORWARD;
4061 n->howMany = 1;
4062 $$ = (Node *)n;
4064 | PRIOR
4066 FetchStmt *n = makeNode(FetchStmt);
4067 n->direction = FETCH_BACKWARD;
4068 n->howMany = 1;
4069 $$ = (Node *)n;
4071 | FIRST_P
4073 FetchStmt *n = makeNode(FetchStmt);
4074 n->direction = FETCH_ABSOLUTE;
4075 n->howMany = 1;
4076 $$ = (Node *)n;
4078 | LAST_P
4080 FetchStmt *n = makeNode(FetchStmt);
4081 n->direction = FETCH_ABSOLUTE;
4082 n->howMany = -1;
4083 $$ = (Node *)n;
4085 | ABSOLUTE_P SignedIconst
4087 FetchStmt *n = makeNode(FetchStmt);
4088 n->direction = FETCH_ABSOLUTE;
4089 n->howMany = $2;
4090 $$ = (Node *)n;
4092 | RELATIVE_P SignedIconst
4094 FetchStmt *n = makeNode(FetchStmt);
4095 n->direction = FETCH_RELATIVE;
4096 n->howMany = $2;
4097 $$ = (Node *)n;
4099 | SignedIconst
4101 FetchStmt *n = makeNode(FetchStmt);
4102 n->direction = FETCH_FORWARD;
4103 n->howMany = $1;
4104 $$ = (Node *)n;
4106 | ALL
4108 FetchStmt *n = makeNode(FetchStmt);
4109 n->direction = FETCH_FORWARD;
4110 n->howMany = FETCH_ALL;
4111 $$ = (Node *)n;
4113 | FORWARD
4115 FetchStmt *n = makeNode(FetchStmt);
4116 n->direction = FETCH_FORWARD;
4117 n->howMany = 1;
4118 $$ = (Node *)n;
4120 | FORWARD SignedIconst
4122 FetchStmt *n = makeNode(FetchStmt);
4123 n->direction = FETCH_FORWARD;
4124 n->howMany = $2;
4125 $$ = (Node *)n;
4127 | FORWARD ALL
4129 FetchStmt *n = makeNode(FetchStmt);
4130 n->direction = FETCH_FORWARD;
4131 n->howMany = FETCH_ALL;
4132 $$ = (Node *)n;
4134 | BACKWARD
4136 FetchStmt *n = makeNode(FetchStmt);
4137 n->direction = FETCH_BACKWARD;
4138 n->howMany = 1;
4139 $$ = (Node *)n;
4141 | BACKWARD SignedIconst
4143 FetchStmt *n = makeNode(FetchStmt);
4144 n->direction = FETCH_BACKWARD;
4145 n->howMany = $2;
4146 $$ = (Node *)n;
4148 | BACKWARD ALL
4150 FetchStmt *n = makeNode(FetchStmt);
4151 n->direction = FETCH_BACKWARD;
4152 n->howMany = FETCH_ALL;
4153 $$ = (Node *)n;
4157 from_in: FROM {}
4158 | IN_P {}
4162 /*****************************************************************************
4164 * GRANT and REVOKE statements
4166 *****************************************************************************/
4168 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
4169 opt_grant_grant_option
4171 GrantStmt *n = makeNode(GrantStmt);
4172 n->is_grant = true;
4173 n->privileges = $2;
4174 n->objtype = ($4)->objtype;
4175 n->objects = ($4)->objs;
4176 n->grantees = $6;
4177 n->grant_option = $7;
4178 $$ = (Node*)n;
4182 RevokeStmt:
4183 REVOKE privileges ON privilege_target
4184 FROM grantee_list opt_drop_behavior
4186 GrantStmt *n = makeNode(GrantStmt);
4187 n->is_grant = false;
4188 n->grant_option = false;
4189 n->privileges = $2;
4190 n->objtype = ($4)->objtype;
4191 n->objects = ($4)->objs;
4192 n->grantees = $6;
4193 n->behavior = $7;
4194 $$ = (Node *)n;
4196 | REVOKE GRANT OPTION FOR privileges ON privilege_target
4197 FROM grantee_list opt_drop_behavior
4199 GrantStmt *n = makeNode(GrantStmt);
4200 n->is_grant = false;
4201 n->grant_option = true;
4202 n->privileges = $5;
4203 n->objtype = ($7)->objtype;
4204 n->objects = ($7)->objs;
4205 n->grantees = $9;
4206 n->behavior = $10;
4207 $$ = (Node *)n;
4213 * A privilege list is represented as a list of strings; the validity of
4214 * the privilege names gets checked at execution. This is a bit annoying
4215 * but we have little choice because of the syntactic conflict with lists
4216 * of role names in GRANT/REVOKE. What's more, we have to call out in
4217 * the "privilege" production any reserved keywords that need to be usable
4218 * as privilege names.
4221 /* either ALL [PRIVILEGES] or a list of individual privileges */
4222 privileges: privilege_list
4223 { $$ = $1; }
4224 | ALL
4225 { $$ = NIL; }
4226 | ALL PRIVILEGES
4227 { $$ = NIL; }
4230 privilege_list: privilege
4231 { $$ = list_make1(makeString($1)); }
4232 | privilege_list ',' privilege
4233 { $$ = lappend($1, makeString($3)); }
4236 privilege: SELECT { $$ = pstrdup($1); }
4237 | REFERENCES { $$ = pstrdup($1); }
4238 | CREATE { $$ = pstrdup($1); }
4239 | ColId { $$ = $1; }
4243 /* Don't bother trying to fold the first two rules into one using
4244 * opt_table. You're going to get conflicts.
4246 privilege_target:
4247 qualified_name_list
4249 PrivTarget *n = makeNode(PrivTarget);
4250 n->objtype = ACL_OBJECT_RELATION;
4251 n->objs = $1;
4252 $$ = n;
4254 | TABLE qualified_name_list
4256 PrivTarget *n = makeNode(PrivTarget);
4257 n->objtype = ACL_OBJECT_RELATION;
4258 n->objs = $2;
4259 $$ = n;
4261 | SEQUENCE qualified_name_list
4263 PrivTarget *n = makeNode(PrivTarget);
4264 n->objtype = ACL_OBJECT_SEQUENCE;
4265 n->objs = $2;
4266 $$ = n;
4268 | FOREIGN DATA_P WRAPPER name_list
4270 PrivTarget *n = makeNode(PrivTarget);
4271 n->objtype = ACL_OBJECT_FDW;
4272 n->objs = $4;
4273 $$ = n;
4275 | FOREIGN SERVER name_list
4277 PrivTarget *n = makeNode(PrivTarget);
4278 n->objtype = ACL_OBJECT_FOREIGN_SERVER;
4279 n->objs = $3;
4280 $$ = n;
4282 | FUNCTION function_with_argtypes_list
4284 PrivTarget *n = makeNode(PrivTarget);
4285 n->objtype = ACL_OBJECT_FUNCTION;
4286 n->objs = $2;
4287 $$ = n;
4289 | DATABASE name_list
4291 PrivTarget *n = makeNode(PrivTarget);
4292 n->objtype = ACL_OBJECT_DATABASE;
4293 n->objs = $2;
4294 $$ = n;
4296 | LANGUAGE name_list
4298 PrivTarget *n = makeNode(PrivTarget);
4299 n->objtype = ACL_OBJECT_LANGUAGE;
4300 n->objs = $2;
4301 $$ = n;
4303 | SCHEMA name_list
4305 PrivTarget *n = makeNode(PrivTarget);
4306 n->objtype = ACL_OBJECT_NAMESPACE;
4307 n->objs = $2;
4308 $$ = n;
4310 | TABLESPACE name_list
4312 PrivTarget *n = makeNode(PrivTarget);
4313 n->objtype = ACL_OBJECT_TABLESPACE;
4314 n->objs = $2;
4315 $$ = n;
4320 grantee_list:
4321 grantee { $$ = list_make1($1); }
4322 | grantee_list ',' grantee { $$ = lappend($1, $3); }
4325 grantee: RoleId
4327 PrivGrantee *n = makeNode(PrivGrantee);
4328 /* This hack lets us avoid reserving PUBLIC as a keyword*/
4329 if (strcmp($1, "public") == 0)
4330 n->rolname = NULL;
4331 else
4332 n->rolname = $1;
4333 $$ = (Node *)n;
4335 | GROUP_P RoleId
4337 PrivGrantee *n = makeNode(PrivGrantee);
4338 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
4339 if (strcmp($2, "public") == 0)
4340 n->rolname = NULL;
4341 else
4342 n->rolname = $2;
4343 $$ = (Node *)n;
4348 opt_grant_grant_option:
4349 WITH GRANT OPTION { $$ = TRUE; }
4350 | /*EMPTY*/ { $$ = FALSE; }
4353 function_with_argtypes_list:
4354 function_with_argtypes { $$ = list_make1($1); }
4355 | function_with_argtypes_list ',' function_with_argtypes
4356 { $$ = lappend($1, $3); }
4359 function_with_argtypes:
4360 func_name func_args
4362 FuncWithArgs *n = makeNode(FuncWithArgs);
4363 n->funcname = $1;
4364 n->funcargs = extractArgTypes($2);
4365 $$ = n;
4369 /*****************************************************************************
4371 * GRANT and REVOKE ROLE statements
4373 *****************************************************************************/
4375 GrantRoleStmt:
4376 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4378 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4379 n->is_grant = true;
4380 n->granted_roles = $2;
4381 n->grantee_roles = $4;
4382 n->admin_opt = $5;
4383 n->grantor = $6;
4384 $$ = (Node*)n;
4388 RevokeRoleStmt:
4389 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4391 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4392 n->is_grant = false;
4393 n->admin_opt = false;
4394 n->granted_roles = $2;
4395 n->grantee_roles = $4;
4396 n->behavior = $6;
4397 $$ = (Node*)n;
4399 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4401 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4402 n->is_grant = false;
4403 n->admin_opt = true;
4404 n->granted_roles = $5;
4405 n->grantee_roles = $7;
4406 n->behavior = $9;
4407 $$ = (Node*)n;
4411 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4412 | /*EMPTY*/ { $$ = FALSE; }
4415 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4416 | /*EMPTY*/ { $$ = NULL; }
4420 /*****************************************************************************
4422 * QUERY: CREATE INDEX
4424 * Note: we can't factor CONCURRENTLY into a separate production without
4425 * making it a reserved word.
4427 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4428 * willing to make TABLESPACE a fully reserved word.
4429 *****************************************************************************/
4431 IndexStmt: CREATE index_opt_unique INDEX index_name
4432 ON qualified_name access_method_clause '(' index_params ')'
4433 opt_definition OptTableSpace where_clause
4435 IndexStmt *n = makeNode(IndexStmt);
4436 n->unique = $2;
4437 n->concurrent = false;
4438 n->idxname = $4;
4439 n->relation = $6;
4440 n->accessMethod = $7;
4441 n->indexParams = $9;
4442 n->options = $11;
4443 n->tableSpace = $12;
4444 n->whereClause = $13;
4445 $$ = (Node *)n;
4447 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4448 ON qualified_name access_method_clause '(' index_params ')'
4449 opt_definition OptTableSpace where_clause
4451 IndexStmt *n = makeNode(IndexStmt);
4452 n->unique = $2;
4453 n->concurrent = true;
4454 n->idxname = $5;
4455 n->relation = $7;
4456 n->accessMethod = $8;
4457 n->indexParams = $10;
4458 n->options = $12;
4459 n->tableSpace = $13;
4460 n->whereClause = $14;
4461 $$ = (Node *)n;
4465 index_opt_unique:
4466 UNIQUE { $$ = TRUE; }
4467 | /*EMPTY*/ { $$ = FALSE; }
4470 access_method_clause:
4471 USING access_method { $$ = $2; }
4472 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4475 index_params: index_elem { $$ = list_make1($1); }
4476 | index_params ',' index_elem { $$ = lappend($1, $3); }
4480 * Index attributes can be either simple column references, or arbitrary
4481 * expressions in parens. For backwards-compatibility reasons, we allow
4482 * an expression that's just a function call to be written without parens.
4484 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4486 $$ = makeNode(IndexElem);
4487 $$->name = $1;
4488 $$->expr = NULL;
4489 $$->opclass = $2;
4490 $$->ordering = $3;
4491 $$->nulls_ordering = $4;
4493 | func_expr opt_class opt_asc_desc opt_nulls_order
4495 $$ = makeNode(IndexElem);
4496 $$->name = NULL;
4497 $$->expr = $1;
4498 $$->opclass = $2;
4499 $$->ordering = $3;
4500 $$->nulls_ordering = $4;
4502 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4504 $$ = makeNode(IndexElem);
4505 $$->name = NULL;
4506 $$->expr = $2;
4507 $$->opclass = $4;
4508 $$->ordering = $5;
4509 $$->nulls_ordering = $6;
4513 opt_class: any_name { $$ = $1; }
4514 | USING any_name { $$ = $2; }
4515 | /*EMPTY*/ { $$ = NIL; }
4518 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4519 | DESC { $$ = SORTBY_DESC; }
4520 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4523 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4524 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4525 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4529 /*****************************************************************************
4531 * QUERY:
4532 * create [or replace] function <fname>
4533 * [(<type-1> { , <type-n>})]
4534 * returns <type-r>
4535 * as <filename or code in language as appropriate>
4536 * language <lang> [with parameters]
4538 *****************************************************************************/
4540 CreateFunctionStmt:
4541 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4542 RETURNS func_return createfunc_opt_list opt_definition
4544 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4545 n->replace = $2;
4546 n->funcname = $4;
4547 n->parameters = $5;
4548 n->returnType = $7;
4549 n->options = $8;
4550 n->withClause = $9;
4551 $$ = (Node *)n;
4553 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4554 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4556 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4557 n->replace = $2;
4558 n->funcname = $4;
4559 n->parameters = mergeTableFuncParameters($5, $9);
4560 n->returnType = TableFuncTypeName($9);
4561 n->returnType->location = @7;
4562 n->options = $11;
4563 n->withClause = $12;
4564 $$ = (Node *)n;
4566 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4567 createfunc_opt_list opt_definition
4569 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4570 n->replace = $2;
4571 n->funcname = $4;
4572 n->parameters = $5;
4573 n->returnType = NULL;
4574 n->options = $6;
4575 n->withClause = $7;
4576 $$ = (Node *)n;
4580 opt_or_replace:
4581 OR REPLACE { $$ = TRUE; }
4582 | /*EMPTY*/ { $$ = FALSE; }
4585 func_args: '(' func_args_list ')' { $$ = $2; }
4586 | '(' ')' { $$ = NIL; }
4589 func_args_list:
4590 func_arg { $$ = list_make1($1); }
4591 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4595 * func_args_with_defaults is separate because we only want to accept
4596 * defaults in CREATE FUNCTION, not in ALTER etc.
4598 func_args_with_defaults:
4599 '(' func_args_with_defaults_list ')' { $$ = $2; }
4600 | '(' ')' { $$ = NIL; }
4603 func_args_with_defaults_list:
4604 func_arg_with_default { $$ = list_make1($1); }
4605 | func_args_with_defaults_list ',' func_arg_with_default
4606 { $$ = lappend($1, $3); }
4610 * The style with arg_class first is SQL99 standard, but Oracle puts
4611 * param_name first; accept both since it's likely people will try both
4612 * anyway. Don't bother trying to save productions by letting arg_class
4613 * have an empty alternative ... you'll get shift/reduce conflicts.
4615 * We can catch over-specified arguments here if we want to,
4616 * but for now better to silently swallow typmod, etc.
4617 * - thomas 2000-03-22
4619 func_arg:
4620 arg_class param_name func_type
4622 FunctionParameter *n = makeNode(FunctionParameter);
4623 n->name = $2;
4624 n->argType = $3;
4625 n->mode = $1;
4626 n->defexpr = NULL;
4627 $$ = n;
4629 | param_name arg_class func_type
4631 FunctionParameter *n = makeNode(FunctionParameter);
4632 n->name = $1;
4633 n->argType = $3;
4634 n->mode = $2;
4635 n->defexpr = NULL;
4636 $$ = n;
4638 | param_name func_type
4640 FunctionParameter *n = makeNode(FunctionParameter);
4641 n->name = $1;
4642 n->argType = $2;
4643 n->mode = FUNC_PARAM_IN;
4644 n->defexpr = NULL;
4645 $$ = n;
4647 | arg_class func_type
4649 FunctionParameter *n = makeNode(FunctionParameter);
4650 n->name = NULL;
4651 n->argType = $2;
4652 n->mode = $1;
4653 n->defexpr = NULL;
4654 $$ = n;
4656 | func_type
4658 FunctionParameter *n = makeNode(FunctionParameter);
4659 n->name = NULL;
4660 n->argType = $1;
4661 n->mode = FUNC_PARAM_IN;
4662 n->defexpr = NULL;
4663 $$ = n;
4667 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4668 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4669 | OUT_P { $$ = FUNC_PARAM_OUT; }
4670 | INOUT { $$ = FUNC_PARAM_INOUT; }
4671 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4672 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4676 * Ideally param_name should be ColId, but that causes too many conflicts.
4678 param_name: type_function_name
4681 func_return:
4682 func_type
4684 /* We can catch over-specified results here if we want to,
4685 * but for now better to silently swallow typmod, etc.
4686 * - thomas 2000-03-22
4688 $$ = $1;
4693 * We would like to make the %TYPE productions here be ColId attrs etc,
4694 * but that causes reduce/reduce conflicts. type_function_name
4695 * is next best choice.
4697 func_type: Typename { $$ = $1; }
4698 | type_function_name attrs '%' TYPE_P
4700 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4701 $$->pct_type = true;
4702 $$->location = @1;
4704 | SETOF type_function_name attrs '%' TYPE_P
4706 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4707 $$->pct_type = true;
4708 $$->setof = TRUE;
4709 $$->location = @2;
4713 func_arg_with_default:
4714 func_arg
4716 $$ = $1;
4718 | func_arg DEFAULT a_expr
4720 $$ = $1;
4721 $$->defexpr = $3;
4723 | func_arg '=' a_expr
4725 $$ = $1;
4726 $$->defexpr = $3;
4731 createfunc_opt_list:
4732 /* Must be at least one to prevent conflict */
4733 createfunc_opt_item { $$ = list_make1($1); }
4734 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4738 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4740 common_func_opt_item:
4741 CALLED ON NULL_P INPUT_P
4743 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4745 | RETURNS NULL_P ON NULL_P INPUT_P
4747 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4749 | STRICT_P
4751 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4753 | IMMUTABLE
4755 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4757 | STABLE
4759 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4761 | VOLATILE
4763 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4765 | EXTERNAL SECURITY DEFINER
4767 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4769 | EXTERNAL SECURITY INVOKER
4771 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4773 | SECURITY DEFINER
4775 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4777 | SECURITY INVOKER
4779 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4781 | COST NumericOnly
4783 $$ = makeDefElem("cost", (Node *)$2);
4785 | ROWS NumericOnly
4787 $$ = makeDefElem("rows", (Node *)$2);
4789 | SetResetClause
4791 /* we abuse the normal content of a DefElem here */
4792 $$ = makeDefElem("set", (Node *)$1);
4796 createfunc_opt_item:
4797 AS func_as
4799 $$ = makeDefElem("as", (Node *)$2);
4801 | LANGUAGE ColId_or_Sconst
4803 $$ = makeDefElem("language", (Node *)makeString($2));
4805 | WINDOW
4807 $$ = makeDefElem("window", (Node *)makeInteger(TRUE));
4809 | common_func_opt_item
4811 $$ = $1;
4815 func_as: Sconst { $$ = list_make1(makeString($1)); }
4816 | Sconst ',' Sconst
4818 $$ = list_make2(makeString($1), makeString($3));
4822 opt_definition:
4823 WITH definition { $$ = $2; }
4824 | /*EMPTY*/ { $$ = NIL; }
4827 table_func_column: param_name func_type
4829 FunctionParameter *n = makeNode(FunctionParameter);
4830 n->name = $1;
4831 n->argType = $2;
4832 n->mode = FUNC_PARAM_TABLE;
4833 n->defexpr = NULL;
4834 $$ = n;
4838 table_func_column_list:
4839 table_func_column
4841 $$ = list_make1($1);
4843 | table_func_column_list ',' table_func_column
4845 $$ = lappend($1, $3);
4849 /*****************************************************************************
4850 * ALTER FUNCTION
4852 * RENAME and OWNER subcommands are already provided by the generic
4853 * ALTER infrastructure, here we just specify alterations that can
4854 * only be applied to functions.
4856 *****************************************************************************/
4857 AlterFunctionStmt:
4858 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4860 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4861 n->func = $3;
4862 n->actions = $4;
4863 $$ = (Node *) n;
4867 alterfunc_opt_list:
4868 /* At least one option must be specified */
4869 common_func_opt_item { $$ = list_make1($1); }
4870 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4873 /* Ignored, merely for SQL compliance */
4874 opt_restrict:
4875 RESTRICT
4876 | /* EMPTY */
4880 /*****************************************************************************
4882 * QUERY:
4884 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4885 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4886 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4888 *****************************************************************************/
4890 RemoveFuncStmt:
4891 DROP FUNCTION func_name func_args opt_drop_behavior
4893 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4894 n->kind = OBJECT_FUNCTION;
4895 n->name = $3;
4896 n->args = extractArgTypes($4);
4897 n->behavior = $5;
4898 n->missing_ok = false;
4899 $$ = (Node *)n;
4901 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4903 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4904 n->kind = OBJECT_FUNCTION;
4905 n->name = $5;
4906 n->args = extractArgTypes($6);
4907 n->behavior = $7;
4908 n->missing_ok = true;
4909 $$ = (Node *)n;
4913 RemoveAggrStmt:
4914 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4916 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4917 n->kind = OBJECT_AGGREGATE;
4918 n->name = $3;
4919 n->args = $4;
4920 n->behavior = $5;
4921 n->missing_ok = false;
4922 $$ = (Node *)n;
4924 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4926 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4927 n->kind = OBJECT_AGGREGATE;
4928 n->name = $5;
4929 n->args = $6;
4930 n->behavior = $7;
4931 n->missing_ok = true;
4932 $$ = (Node *)n;
4936 RemoveOperStmt:
4937 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
4939 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4940 n->kind = OBJECT_OPERATOR;
4941 n->name = $3;
4942 n->args = $4;
4943 n->behavior = $5;
4944 n->missing_ok = false;
4945 $$ = (Node *)n;
4947 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
4949 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4950 n->kind = OBJECT_OPERATOR;
4951 n->name = $5;
4952 n->args = $6;
4953 n->behavior = $7;
4954 n->missing_ok = true;
4955 $$ = (Node *)n;
4959 oper_argtypes:
4960 '(' Typename ')'
4962 ereport(ERROR,
4963 (errcode(ERRCODE_SYNTAX_ERROR),
4964 errmsg("missing argument"),
4965 errhint("Use NONE to denote the missing argument of a unary operator."),
4966 scanner_errposition(@3)));
4968 | '(' Typename ',' Typename ')'
4969 { $$ = list_make2($2, $4); }
4970 | '(' NONE ',' Typename ')' /* left unary */
4971 { $$ = list_make2(NULL, $4); }
4972 | '(' Typename ',' NONE ')' /* right unary */
4973 { $$ = list_make2($2, NULL); }
4976 any_operator:
4977 all_Op
4978 { $$ = list_make1(makeString($1)); }
4979 | ColId '.' any_operator
4980 { $$ = lcons(makeString($1), $3); }
4984 /*****************************************************************************
4986 * CREATE CAST / DROP CAST
4988 *****************************************************************************/
4990 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4991 WITH FUNCTION function_with_argtypes cast_context
4993 CreateCastStmt *n = makeNode(CreateCastStmt);
4994 n->sourcetype = $4;
4995 n->targettype = $6;
4996 n->func = $10;
4997 n->context = (CoercionContext) $11;
4998 n->inout = false;
4999 $$ = (Node *)n;
5001 | CREATE CAST '(' Typename AS Typename ')'
5002 WITHOUT FUNCTION cast_context
5004 CreateCastStmt *n = makeNode(CreateCastStmt);
5005 n->sourcetype = $4;
5006 n->targettype = $6;
5007 n->func = NULL;
5008 n->context = (CoercionContext) $10;
5009 n->inout = false;
5010 $$ = (Node *)n;
5012 | CREATE CAST '(' Typename AS Typename ')'
5013 WITH INOUT cast_context
5015 CreateCastStmt *n = makeNode(CreateCastStmt);
5016 n->sourcetype = $4;
5017 n->targettype = $6;
5018 n->func = NULL;
5019 n->context = (CoercionContext) $10;
5020 n->inout = true;
5021 $$ = (Node *)n;
5025 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
5026 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
5027 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
5031 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
5033 DropCastStmt *n = makeNode(DropCastStmt);
5034 n->sourcetype = $5;
5035 n->targettype = $7;
5036 n->behavior = $9;
5037 n->missing_ok = $3;
5038 $$ = (Node *)n;
5042 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
5043 | /*EMPTY*/ { $$ = FALSE; }
5047 /*****************************************************************************
5049 * QUERY:
5051 * REINDEX type <name> [FORCE]
5053 * FORCE no longer does anything, but we accept it for backwards compatibility
5054 *****************************************************************************/
5056 ReindexStmt:
5057 REINDEX reindex_type qualified_name opt_force
5059 ReindexStmt *n = makeNode(ReindexStmt);
5060 n->kind = $2;
5061 n->relation = $3;
5062 n->name = NULL;
5063 $$ = (Node *)n;
5065 | REINDEX SYSTEM_P name opt_force
5067 ReindexStmt *n = makeNode(ReindexStmt);
5068 n->kind = OBJECT_DATABASE;
5069 n->name = $3;
5070 n->relation = NULL;
5071 n->do_system = true;
5072 n->do_user = false;
5073 $$ = (Node *)n;
5075 | REINDEX DATABASE name opt_force
5077 ReindexStmt *n = makeNode(ReindexStmt);
5078 n->kind = OBJECT_DATABASE;
5079 n->name = $3;
5080 n->relation = NULL;
5081 n->do_system = true;
5082 n->do_user = true;
5083 $$ = (Node *)n;
5087 reindex_type:
5088 INDEX { $$ = OBJECT_INDEX; }
5089 | TABLE { $$ = OBJECT_TABLE; }
5092 opt_force: FORCE { $$ = TRUE; }
5093 | /* EMPTY */ { $$ = FALSE; }
5097 /*****************************************************************************
5099 * ALTER THING name RENAME TO newname
5101 *****************************************************************************/
5103 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
5105 RenameStmt *n = makeNode(RenameStmt);
5106 n->renameType = OBJECT_AGGREGATE;
5107 n->object = $3;
5108 n->objarg = $4;
5109 n->newname = $7;
5110 $$ = (Node *)n;
5112 | ALTER CONVERSION_P any_name RENAME TO name
5114 RenameStmt *n = makeNode(RenameStmt);
5115 n->renameType = OBJECT_CONVERSION;
5116 n->object = $3;
5117 n->newname = $6;
5118 $$ = (Node *)n;
5120 | ALTER DATABASE database_name RENAME TO database_name
5122 RenameStmt *n = makeNode(RenameStmt);
5123 n->renameType = OBJECT_DATABASE;
5124 n->subname = $3;
5125 n->newname = $6;
5126 $$ = (Node *)n;
5128 | ALTER FUNCTION function_with_argtypes RENAME TO name
5130 RenameStmt *n = makeNode(RenameStmt);
5131 n->renameType = OBJECT_FUNCTION;
5132 n->object = $3->funcname;
5133 n->objarg = $3->funcargs;
5134 n->newname = $6;
5135 $$ = (Node *)n;
5137 | ALTER GROUP_P RoleId RENAME TO RoleId
5139 RenameStmt *n = makeNode(RenameStmt);
5140 n->renameType = OBJECT_ROLE;
5141 n->subname = $3;
5142 n->newname = $6;
5143 $$ = (Node *)n;
5145 | ALTER opt_procedural LANGUAGE name RENAME TO name
5147 RenameStmt *n = makeNode(RenameStmt);
5148 n->renameType = OBJECT_LANGUAGE;
5149 n->subname = $4;
5150 n->newname = $7;
5151 $$ = (Node *)n;
5153 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
5155 RenameStmt *n = makeNode(RenameStmt);
5156 n->renameType = OBJECT_OPCLASS;
5157 n->object = $4;
5158 n->subname = $6;
5159 n->newname = $9;
5160 $$ = (Node *)n;
5162 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
5164 RenameStmt *n = makeNode(RenameStmt);
5165 n->renameType = OBJECT_OPFAMILY;
5166 n->object = $4;
5167 n->subname = $6;
5168 n->newname = $9;
5169 $$ = (Node *)n;
5171 | ALTER SCHEMA name RENAME TO name
5173 RenameStmt *n = makeNode(RenameStmt);
5174 n->renameType = OBJECT_SCHEMA;
5175 n->subname = $3;
5176 n->newname = $6;
5177 $$ = (Node *)n;
5179 | ALTER TABLE relation_expr RENAME TO name
5181 RenameStmt *n = makeNode(RenameStmt);
5182 n->renameType = OBJECT_TABLE;
5183 n->relation = $3;
5184 n->subname = NULL;
5185 n->newname = $6;
5186 $$ = (Node *)n;
5188 | ALTER SEQUENCE qualified_name RENAME TO name
5190 RenameStmt *n = makeNode(RenameStmt);
5191 n->renameType = OBJECT_SEQUENCE;
5192 n->relation = $3;
5193 n->subname = NULL;
5194 n->newname = $6;
5195 $$ = (Node *)n;
5197 | ALTER VIEW qualified_name RENAME TO name
5199 RenameStmt *n = makeNode(RenameStmt);
5200 n->renameType = OBJECT_VIEW;
5201 n->relation = $3;
5202 n->subname = NULL;
5203 n->newname = $6;
5204 $$ = (Node *)n;
5206 | ALTER INDEX qualified_name RENAME TO name
5208 RenameStmt *n = makeNode(RenameStmt);
5209 n->renameType = OBJECT_INDEX;
5210 n->relation = $3;
5211 n->subname = NULL;
5212 n->newname = $6;
5213 $$ = (Node *)n;
5215 | ALTER TABLE relation_expr RENAME opt_column name TO name
5217 RenameStmt *n = makeNode(RenameStmt);
5218 n->renameType = OBJECT_COLUMN;
5219 n->relation = $3;
5220 n->subname = $6;
5221 n->newname = $8;
5222 $$ = (Node *)n;
5224 | ALTER TRIGGER name ON qualified_name RENAME TO name
5226 RenameStmt *n = makeNode(RenameStmt);
5227 n->renameType = OBJECT_TRIGGER;
5228 n->relation = $5;
5229 n->subname = $3;
5230 n->newname = $8;
5231 $$ = (Node *)n;
5233 | ALTER ROLE RoleId RENAME TO RoleId
5235 RenameStmt *n = makeNode(RenameStmt);
5236 n->renameType = OBJECT_ROLE;
5237 n->subname = $3;
5238 n->newname = $6;
5239 $$ = (Node *)n;
5241 | ALTER USER RoleId RENAME TO RoleId
5243 RenameStmt *n = makeNode(RenameStmt);
5244 n->renameType = OBJECT_ROLE;
5245 n->subname = $3;
5246 n->newname = $6;
5247 $$ = (Node *)n;
5249 | ALTER TABLESPACE name RENAME TO name
5251 RenameStmt *n = makeNode(RenameStmt);
5252 n->renameType = OBJECT_TABLESPACE;
5253 n->subname = $3;
5254 n->newname = $6;
5255 $$ = (Node *)n;
5257 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
5259 RenameStmt *n = makeNode(RenameStmt);
5260 n->renameType = OBJECT_TSPARSER;
5261 n->object = $5;
5262 n->newname = $8;
5263 $$ = (Node *)n;
5265 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
5267 RenameStmt *n = makeNode(RenameStmt);
5268 n->renameType = OBJECT_TSDICTIONARY;
5269 n->object = $5;
5270 n->newname = $8;
5271 $$ = (Node *)n;
5273 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
5275 RenameStmt *n = makeNode(RenameStmt);
5276 n->renameType = OBJECT_TSTEMPLATE;
5277 n->object = $5;
5278 n->newname = $8;
5279 $$ = (Node *)n;
5281 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
5283 RenameStmt *n = makeNode(RenameStmt);
5284 n->renameType = OBJECT_TSCONFIGURATION;
5285 n->object = $5;
5286 n->newname = $8;
5287 $$ = (Node *)n;
5289 | ALTER TYPE_P any_name RENAME TO name
5291 RenameStmt *n = makeNode(RenameStmt);
5292 n->renameType = OBJECT_TYPE;
5293 n->object = $3;
5294 n->newname = $6;
5295 $$ = (Node *)n;
5299 opt_column: COLUMN { $$ = COLUMN; }
5300 | /*EMPTY*/ { $$ = 0; }
5303 opt_set_data: SET DATA_P { $$ = 1; }
5304 | /*EMPTY*/ { $$ = 0; }
5307 /*****************************************************************************
5309 * ALTER THING name SET SCHEMA name
5311 *****************************************************************************/
5313 AlterObjectSchemaStmt:
5314 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
5316 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5317 n->objectType = OBJECT_AGGREGATE;
5318 n->object = $3;
5319 n->objarg = $4;
5320 n->newschema = $7;
5321 $$ = (Node *)n;
5323 | ALTER DOMAIN_P any_name SET SCHEMA name
5325 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5326 n->objectType = OBJECT_DOMAIN;
5327 n->object = $3;
5328 n->newschema = $6;
5329 $$ = (Node *)n;
5331 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
5333 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5334 n->objectType = OBJECT_FUNCTION;
5335 n->object = $3->funcname;
5336 n->objarg = $3->funcargs;
5337 n->newschema = $6;
5338 $$ = (Node *)n;
5340 | ALTER TABLE relation_expr SET SCHEMA name
5342 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5343 n->objectType = OBJECT_TABLE;
5344 n->relation = $3;
5345 n->newschema = $6;
5346 $$ = (Node *)n;
5348 | ALTER SEQUENCE qualified_name SET SCHEMA name
5350 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5351 n->objectType = OBJECT_SEQUENCE;
5352 n->relation = $3;
5353 n->newschema = $6;
5354 $$ = (Node *)n;
5356 | ALTER VIEW qualified_name SET SCHEMA name
5358 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5359 n->objectType = OBJECT_VIEW;
5360 n->relation = $3;
5361 n->newschema = $6;
5362 $$ = (Node *)n;
5364 | ALTER TYPE_P any_name SET SCHEMA name
5366 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
5367 n->objectType = OBJECT_TYPE;
5368 n->object = $3;
5369 n->newschema = $6;
5370 $$ = (Node *)n;
5374 /*****************************************************************************
5376 * ALTER THING name OWNER TO newname
5378 *****************************************************************************/
5380 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5382 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5383 n->objectType = OBJECT_AGGREGATE;
5384 n->object = $3;
5385 n->objarg = $4;
5386 n->newowner = $7;
5387 $$ = (Node *)n;
5389 | ALTER CONVERSION_P any_name OWNER TO RoleId
5391 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5392 n->objectType = OBJECT_CONVERSION;
5393 n->object = $3;
5394 n->newowner = $6;
5395 $$ = (Node *)n;
5397 | ALTER DATABASE database_name OWNER TO RoleId
5399 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5400 n->objectType = OBJECT_DATABASE;
5401 n->object = list_make1(makeString($3));
5402 n->newowner = $6;
5403 $$ = (Node *)n;
5405 | ALTER DOMAIN_P any_name OWNER TO RoleId
5407 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5408 n->objectType = OBJECT_DOMAIN;
5409 n->object = $3;
5410 n->newowner = $6;
5411 $$ = (Node *)n;
5413 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5415 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5416 n->objectType = OBJECT_FUNCTION;
5417 n->object = $3->funcname;
5418 n->objarg = $3->funcargs;
5419 n->newowner = $6;
5420 $$ = (Node *)n;
5422 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5424 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5425 n->objectType = OBJECT_LANGUAGE;
5426 n->object = list_make1(makeString($4));
5427 n->newowner = $7;
5428 $$ = (Node *)n;
5430 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5432 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5433 n->objectType = OBJECT_OPERATOR;
5434 n->object = $3;
5435 n->objarg = $4;
5436 n->newowner = $7;
5437 $$ = (Node *)n;
5439 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5441 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5442 n->objectType = OBJECT_OPCLASS;
5443 n->object = $4;
5444 n->addname = $6;
5445 n->newowner = $9;
5446 $$ = (Node *)n;
5448 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5450 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5451 n->objectType = OBJECT_OPFAMILY;
5452 n->object = $4;
5453 n->addname = $6;
5454 n->newowner = $9;
5455 $$ = (Node *)n;
5457 | ALTER SCHEMA name OWNER TO RoleId
5459 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5460 n->objectType = OBJECT_SCHEMA;
5461 n->object = list_make1(makeString($3));
5462 n->newowner = $6;
5463 $$ = (Node *)n;
5465 | ALTER TYPE_P any_name OWNER TO RoleId
5467 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5468 n->objectType = OBJECT_TYPE;
5469 n->object = $3;
5470 n->newowner = $6;
5471 $$ = (Node *)n;
5473 | ALTER TABLESPACE name OWNER TO RoleId
5475 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5476 n->objectType = OBJECT_TABLESPACE;
5477 n->object = list_make1(makeString($3));
5478 n->newowner = $6;
5479 $$ = (Node *)n;
5481 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5483 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5484 n->objectType = OBJECT_TSDICTIONARY;
5485 n->object = $5;
5486 n->newowner = $8;
5487 $$ = (Node *)n;
5489 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5491 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5492 n->objectType = OBJECT_TSCONFIGURATION;
5493 n->object = $5;
5494 n->newowner = $8;
5495 $$ = (Node *)n;
5497 | ALTER FOREIGN DATA_P WRAPPER name OWNER TO RoleId
5499 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5500 n->objectType = OBJECT_FDW;
5501 n->object = list_make1(makeString($5));
5502 n->newowner = $8;
5503 $$ = (Node *)n;
5505 | ALTER SERVER name OWNER TO RoleId
5507 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5508 n->objectType = OBJECT_FOREIGN_SERVER;
5509 n->object = list_make1(makeString($3));
5510 n->newowner = $6;
5511 $$ = (Node *)n;
5516 /*****************************************************************************
5518 * QUERY: Define Rewrite Rule
5520 *****************************************************************************/
5522 RuleStmt: CREATE opt_or_replace RULE name AS
5523 { QueryIsRule=TRUE; }
5524 ON event TO qualified_name where_clause
5525 DO opt_instead RuleActionList
5527 RuleStmt *n = makeNode(RuleStmt);
5528 n->replace = $2;
5529 n->relation = $10;
5530 n->rulename = $4;
5531 n->whereClause = $11;
5532 n->event = $8;
5533 n->instead = $13;
5534 n->actions = $14;
5535 $$ = (Node *)n;
5536 QueryIsRule=FALSE;
5540 RuleActionList:
5541 NOTHING { $$ = NIL; }
5542 | RuleActionStmt { $$ = list_make1($1); }
5543 | '(' RuleActionMulti ')' { $$ = $2; }
5546 /* the thrashing around here is to discard "empty" statements... */
5547 RuleActionMulti:
5548 RuleActionMulti ';' RuleActionStmtOrEmpty
5549 { if ($3 != NULL)
5550 $$ = lappend($1, $3);
5551 else
5552 $$ = $1;
5554 | RuleActionStmtOrEmpty
5555 { if ($1 != NULL)
5556 $$ = list_make1($1);
5557 else
5558 $$ = NIL;
5562 RuleActionStmt:
5563 SelectStmt
5564 | InsertStmt
5565 | UpdateStmt
5566 | DeleteStmt
5567 | NotifyStmt
5570 RuleActionStmtOrEmpty:
5571 RuleActionStmt { $$ = $1; }
5572 | /*EMPTY*/ { $$ = NULL; }
5575 event: SELECT { $$ = CMD_SELECT; }
5576 | UPDATE { $$ = CMD_UPDATE; }
5577 | DELETE_P { $$ = CMD_DELETE; }
5578 | INSERT { $$ = CMD_INSERT; }
5581 opt_instead:
5582 INSTEAD { $$ = TRUE; }
5583 | ALSO { $$ = FALSE; }
5584 | /*EMPTY*/ { $$ = FALSE; }
5588 DropRuleStmt:
5589 DROP RULE name ON qualified_name opt_drop_behavior
5591 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5592 n->relation = $5;
5593 n->property = $3;
5594 n->behavior = $6;
5595 n->removeType = OBJECT_RULE;
5596 n->missing_ok = false;
5597 $$ = (Node *) n;
5599 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5601 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5602 n->relation = $7;
5603 n->property = $5;
5604 n->behavior = $8;
5605 n->removeType = OBJECT_RULE;
5606 n->missing_ok = true;
5607 $$ = (Node *) n;
5612 /*****************************************************************************
5614 * QUERY:
5615 * NOTIFY <identifier> can appear both in rule bodies and
5616 * as a query-level command
5618 *****************************************************************************/
5620 NotifyStmt: NOTIFY ColId
5622 NotifyStmt *n = makeNode(NotifyStmt);
5623 n->conditionname = $2;
5624 $$ = (Node *)n;
5628 ListenStmt: LISTEN ColId
5630 ListenStmt *n = makeNode(ListenStmt);
5631 n->conditionname = $2;
5632 $$ = (Node *)n;
5636 UnlistenStmt:
5637 UNLISTEN ColId
5639 UnlistenStmt *n = makeNode(UnlistenStmt);
5640 n->conditionname = $2;
5641 $$ = (Node *)n;
5643 | UNLISTEN '*'
5645 UnlistenStmt *n = makeNode(UnlistenStmt);
5646 n->conditionname = NULL;
5647 $$ = (Node *)n;
5652 /*****************************************************************************
5654 * Transactions:
5656 * BEGIN / COMMIT / ROLLBACK
5657 * (also older versions END / ABORT)
5659 *****************************************************************************/
5661 TransactionStmt:
5662 ABORT_P opt_transaction
5664 TransactionStmt *n = makeNode(TransactionStmt);
5665 n->kind = TRANS_STMT_ROLLBACK;
5666 n->options = NIL;
5667 $$ = (Node *)n;
5669 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5671 TransactionStmt *n = makeNode(TransactionStmt);
5672 n->kind = TRANS_STMT_BEGIN;
5673 n->options = $3;
5674 $$ = (Node *)n;
5676 | START TRANSACTION transaction_mode_list_or_empty
5678 TransactionStmt *n = makeNode(TransactionStmt);
5679 n->kind = TRANS_STMT_START;
5680 n->options = $3;
5681 $$ = (Node *)n;
5683 | COMMIT opt_transaction
5685 TransactionStmt *n = makeNode(TransactionStmt);
5686 n->kind = TRANS_STMT_COMMIT;
5687 n->options = NIL;
5688 $$ = (Node *)n;
5690 | END_P opt_transaction
5692 TransactionStmt *n = makeNode(TransactionStmt);
5693 n->kind = TRANS_STMT_COMMIT;
5694 n->options = NIL;
5695 $$ = (Node *)n;
5697 | ROLLBACK opt_transaction
5699 TransactionStmt *n = makeNode(TransactionStmt);
5700 n->kind = TRANS_STMT_ROLLBACK;
5701 n->options = NIL;
5702 $$ = (Node *)n;
5704 | SAVEPOINT ColId
5706 TransactionStmt *n = makeNode(TransactionStmt);
5707 n->kind = TRANS_STMT_SAVEPOINT;
5708 n->options = list_make1(makeDefElem("savepoint_name",
5709 (Node *)makeString($2)));
5710 $$ = (Node *)n;
5712 | RELEASE SAVEPOINT ColId
5714 TransactionStmt *n = makeNode(TransactionStmt);
5715 n->kind = TRANS_STMT_RELEASE;
5716 n->options = list_make1(makeDefElem("savepoint_name",
5717 (Node *)makeString($3)));
5718 $$ = (Node *)n;
5720 | RELEASE ColId
5722 TransactionStmt *n = makeNode(TransactionStmt);
5723 n->kind = TRANS_STMT_RELEASE;
5724 n->options = list_make1(makeDefElem("savepoint_name",
5725 (Node *)makeString($2)));
5726 $$ = (Node *)n;
5728 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5730 TransactionStmt *n = makeNode(TransactionStmt);
5731 n->kind = TRANS_STMT_ROLLBACK_TO;
5732 n->options = list_make1(makeDefElem("savepoint_name",
5733 (Node *)makeString($5)));
5734 $$ = (Node *)n;
5736 | ROLLBACK opt_transaction TO ColId
5738 TransactionStmt *n = makeNode(TransactionStmt);
5739 n->kind = TRANS_STMT_ROLLBACK_TO;
5740 n->options = list_make1(makeDefElem("savepoint_name",
5741 (Node *)makeString($4)));
5742 $$ = (Node *)n;
5744 | PREPARE TRANSACTION Sconst
5746 TransactionStmt *n = makeNode(TransactionStmt);
5747 n->kind = TRANS_STMT_PREPARE;
5748 n->gid = $3;
5749 $$ = (Node *)n;
5751 | COMMIT PREPARED Sconst
5753 TransactionStmt *n = makeNode(TransactionStmt);
5754 n->kind = TRANS_STMT_COMMIT_PREPARED;
5755 n->gid = $3;
5756 $$ = (Node *)n;
5758 | ROLLBACK PREPARED Sconst
5760 TransactionStmt *n = makeNode(TransactionStmt);
5761 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5762 n->gid = $3;
5763 $$ = (Node *)n;
5767 opt_transaction: WORK {}
5768 | TRANSACTION {}
5769 | /*EMPTY*/ {}
5772 transaction_mode_item:
5773 ISOLATION LEVEL iso_level
5774 { $$ = makeDefElem("transaction_isolation",
5775 makeStringConst($3, @3)); }
5776 | READ ONLY
5777 { $$ = makeDefElem("transaction_read_only",
5778 makeIntConst(TRUE, @1)); }
5779 | READ WRITE
5780 { $$ = makeDefElem("transaction_read_only",
5781 makeIntConst(FALSE, @1)); }
5784 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5785 transaction_mode_list:
5786 transaction_mode_item
5787 { $$ = list_make1($1); }
5788 | transaction_mode_list ',' transaction_mode_item
5789 { $$ = lappend($1, $3); }
5790 | transaction_mode_list transaction_mode_item
5791 { $$ = lappend($1, $2); }
5794 transaction_mode_list_or_empty:
5795 transaction_mode_list
5796 | /* EMPTY */
5797 { $$ = NIL; }
5801 /*****************************************************************************
5803 * QUERY:
5804 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5805 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5807 *****************************************************************************/
5809 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5810 AS SelectStmt opt_check_option
5812 ViewStmt *n = makeNode(ViewStmt);
5813 n->view = $4;
5814 n->view->istemp = $2;
5815 n->aliases = $5;
5816 n->query = $7;
5817 n->replace = false;
5818 $$ = (Node *) n;
5820 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5821 AS SelectStmt opt_check_option
5823 ViewStmt *n = makeNode(ViewStmt);
5824 n->view = $6;
5825 n->view->istemp = $4;
5826 n->aliases = $7;
5827 n->query = $9;
5828 n->replace = true;
5829 $$ = (Node *) n;
5833 opt_check_option:
5834 WITH CHECK OPTION
5836 ereport(ERROR,
5837 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5838 errmsg("WITH CHECK OPTION is not implemented")));
5840 | WITH CASCADED CHECK OPTION
5842 ereport(ERROR,
5843 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5844 errmsg("WITH CHECK OPTION is not implemented")));
5846 | WITH LOCAL CHECK OPTION
5848 ereport(ERROR,
5849 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5850 errmsg("WITH CHECK OPTION is not implemented")));
5852 | /* EMPTY */ { $$ = NIL; }
5855 /*****************************************************************************
5857 * QUERY:
5858 * LOAD "filename"
5860 *****************************************************************************/
5862 LoadStmt: LOAD file_name
5864 LoadStmt *n = makeNode(LoadStmt);
5865 n->filename = $2;
5866 $$ = (Node *)n;
5871 /*****************************************************************************
5873 * CREATE DATABASE
5875 *****************************************************************************/
5877 CreatedbStmt:
5878 CREATE DATABASE database_name opt_with createdb_opt_list
5880 CreatedbStmt *n = makeNode(CreatedbStmt);
5881 n->dbname = $3;
5882 n->options = $5;
5883 $$ = (Node *)n;
5887 createdb_opt_list:
5888 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5889 | /* EMPTY */ { $$ = NIL; }
5892 createdb_opt_item:
5893 TABLESPACE opt_equal name
5895 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5897 | TABLESPACE opt_equal DEFAULT
5899 $$ = makeDefElem("tablespace", NULL);
5901 | LOCATION opt_equal Sconst
5903 $$ = makeDefElem("location", (Node *)makeString($3));
5905 | LOCATION opt_equal DEFAULT
5907 $$ = makeDefElem("location", NULL);
5909 | TEMPLATE opt_equal name
5911 $$ = makeDefElem("template", (Node *)makeString($3));
5913 | TEMPLATE opt_equal DEFAULT
5915 $$ = makeDefElem("template", NULL);
5917 | ENCODING opt_equal Sconst
5919 $$ = makeDefElem("encoding", (Node *)makeString($3));
5921 | ENCODING opt_equal Iconst
5923 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5925 | ENCODING opt_equal DEFAULT
5927 $$ = makeDefElem("encoding", NULL);
5929 | COLLATE opt_equal Sconst
5931 $$ = makeDefElem("collate", (Node *)makeString($3));
5933 | COLLATE opt_equal DEFAULT
5935 $$ = makeDefElem("collate", NULL);
5937 | CTYPE opt_equal Sconst
5939 $$ = makeDefElem("ctype", (Node *)makeString($3));
5941 | CTYPE opt_equal DEFAULT
5943 $$ = makeDefElem("ctype", NULL);
5945 | CONNECTION LIMIT opt_equal SignedIconst
5947 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5949 | OWNER opt_equal name
5951 $$ = makeDefElem("owner", (Node *)makeString($3));
5953 | OWNER opt_equal DEFAULT
5955 $$ = makeDefElem("owner", NULL);
5960 * Though the equals sign doesn't match other WITH options, pg_dump uses
5961 * equals for backward compatibility, and it doesn't seem worth removing it.
5963 opt_equal: '=' {}
5964 | /*EMPTY*/ {}
5968 /*****************************************************************************
5970 * ALTER DATABASE
5972 *****************************************************************************/
5974 AlterDatabaseStmt:
5975 ALTER DATABASE database_name opt_with alterdb_opt_list
5977 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5978 n->dbname = $3;
5979 n->options = $5;
5980 $$ = (Node *)n;
5982 | ALTER DATABASE database_name SET TABLESPACE name
5984 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5985 n->dbname = $3;
5986 n->options = list_make1(makeDefElem("tablespace",
5987 (Node *)makeString($6)));
5988 $$ = (Node *)n;
5992 AlterDatabaseSetStmt:
5993 ALTER DATABASE database_name SetResetClause
5995 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5996 n->dbname = $3;
5997 n->setstmt = $4;
5998 $$ = (Node *)n;
6003 alterdb_opt_list:
6004 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
6005 | /* EMPTY */ { $$ = NIL; }
6008 alterdb_opt_item:
6009 CONNECTION LIMIT opt_equal SignedIconst
6011 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
6016 /*****************************************************************************
6018 * DROP DATABASE [ IF EXISTS ]
6020 * This is implicitly CASCADE, no need for drop behavior
6021 *****************************************************************************/
6023 DropdbStmt: DROP DATABASE database_name
6025 DropdbStmt *n = makeNode(DropdbStmt);
6026 n->dbname = $3;
6027 n->missing_ok = FALSE;
6028 $$ = (Node *)n;
6030 | DROP DATABASE IF_P EXISTS database_name
6032 DropdbStmt *n = makeNode(DropdbStmt);
6033 n->dbname = $5;
6034 n->missing_ok = TRUE;
6035 $$ = (Node *)n;
6040 /*****************************************************************************
6042 * Manipulate a domain
6044 *****************************************************************************/
6046 CreateDomainStmt:
6047 CREATE DOMAIN_P any_name opt_as Typename ColQualList
6049 CreateDomainStmt *n = makeNode(CreateDomainStmt);
6050 n->domainname = $3;
6051 n->typename = $5;
6052 n->constraints = $6;
6053 $$ = (Node *)n;
6057 AlterDomainStmt:
6058 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
6059 ALTER DOMAIN_P any_name alter_column_default
6061 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6062 n->subtype = 'T';
6063 n->typename = $3;
6064 n->def = $4;
6065 $$ = (Node *)n;
6067 /* ALTER DOMAIN <domain> DROP NOT NULL */
6068 | ALTER DOMAIN_P any_name DROP NOT NULL_P
6070 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6071 n->subtype = 'N';
6072 n->typename = $3;
6073 $$ = (Node *)n;
6075 /* ALTER DOMAIN <domain> SET NOT NULL */
6076 | ALTER DOMAIN_P any_name SET NOT NULL_P
6078 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6079 n->subtype = 'O';
6080 n->typename = $3;
6081 $$ = (Node *)n;
6083 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
6084 | ALTER DOMAIN_P any_name ADD_P TableConstraint
6086 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6087 n->subtype = 'C';
6088 n->typename = $3;
6089 n->def = $5;
6090 $$ = (Node *)n;
6092 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
6093 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
6095 AlterDomainStmt *n = makeNode(AlterDomainStmt);
6096 n->subtype = 'X';
6097 n->typename = $3;
6098 n->name = $6;
6099 n->behavior = $7;
6100 $$ = (Node *)n;
6104 opt_as: AS {}
6105 | /* EMPTY */ {}
6109 /*****************************************************************************
6111 * Manipulate a text search dictionary or configuration
6113 *****************************************************************************/
6115 AlterTSDictionaryStmt:
6116 ALTER TEXT_P SEARCH DICTIONARY any_name definition
6118 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
6119 n->dictname = $5;
6120 n->options = $6;
6121 $$ = (Node *)n;
6125 AlterTSConfigurationStmt:
6126 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
6128 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6129 n->cfgname = $5;
6130 n->tokentype = $9;
6131 n->dicts = $11;
6132 n->override = false;
6133 n->replace = false;
6134 $$ = (Node*)n;
6136 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
6138 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6139 n->cfgname = $5;
6140 n->tokentype = $9;
6141 n->dicts = $11;
6142 n->override = true;
6143 n->replace = false;
6144 $$ = (Node*)n;
6146 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
6148 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6149 n->cfgname = $5;
6150 n->tokentype = NIL;
6151 n->dicts = list_make2($9,$11);
6152 n->override = false;
6153 n->replace = true;
6154 $$ = (Node*)n;
6156 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
6158 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6159 n->cfgname = $5;
6160 n->tokentype = $9;
6161 n->dicts = list_make2($11,$13);
6162 n->override = false;
6163 n->replace = true;
6164 $$ = (Node*)n;
6166 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
6168 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6169 n->cfgname = $5;
6170 n->tokentype = $9;
6171 n->missing_ok = false;
6172 $$ = (Node*)n;
6174 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
6176 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
6177 n->cfgname = $5;
6178 n->tokentype = $11;
6179 n->missing_ok = true;
6180 $$ = (Node*)n;
6185 /*****************************************************************************
6187 * Manipulate a conversion
6189 * CREATE [DEFAULT] CONVERSION <conversion_name>
6190 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
6192 *****************************************************************************/
6194 CreateConversionStmt:
6195 CREATE opt_default CONVERSION_P any_name FOR Sconst
6196 TO Sconst FROM any_name
6198 CreateConversionStmt *n = makeNode(CreateConversionStmt);
6199 n->conversion_name = $4;
6200 n->for_encoding_name = $6;
6201 n->to_encoding_name = $8;
6202 n->func_name = $10;
6203 n->def = $2;
6204 $$ = (Node *)n;
6208 /*****************************************************************************
6210 * QUERY:
6211 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
6212 * CLUSTER [VERBOSE]
6213 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
6215 *****************************************************************************/
6217 ClusterStmt:
6218 CLUSTER opt_verbose qualified_name cluster_index_specification
6220 ClusterStmt *n = makeNode(ClusterStmt);
6221 n->relation = $3;
6222 n->indexname = $4;
6223 n->verbose = $2;
6224 $$ = (Node*)n;
6226 | CLUSTER opt_verbose
6228 ClusterStmt *n = makeNode(ClusterStmt);
6229 n->relation = NULL;
6230 n->indexname = NULL;
6231 n->verbose = $2;
6232 $$ = (Node*)n;
6234 /* kept for pre-8.3 compatibility */
6235 | CLUSTER opt_verbose index_name ON qualified_name
6237 ClusterStmt *n = makeNode(ClusterStmt);
6238 n->relation = $5;
6239 n->indexname = $3;
6240 n->verbose = $2;
6241 $$ = (Node*)n;
6245 cluster_index_specification:
6246 USING index_name { $$ = $2; }
6247 | /*EMPTY*/ { $$ = NULL; }
6251 /*****************************************************************************
6253 * QUERY:
6254 * VACUUM
6255 * ANALYZE
6257 *****************************************************************************/
6259 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
6261 VacuumStmt *n = makeNode(VacuumStmt);
6262 n->vacuum = true;
6263 n->analyze = false;
6264 n->full = $2;
6265 n->freeze_min_age = $3 ? 0 : -1;
6266 n->freeze_table_age = $3 ? 0 : -1;
6267 n->verbose = $4;
6268 n->relation = NULL;
6269 n->va_cols = NIL;
6270 $$ = (Node *)n;
6272 | VACUUM opt_full opt_freeze opt_verbose qualified_name
6274 VacuumStmt *n = makeNode(VacuumStmt);
6275 n->vacuum = true;
6276 n->analyze = false;
6277 n->full = $2;
6278 n->freeze_min_age = $3 ? 0 : -1;
6279 n->freeze_table_age = $3 ? 0 : -1;
6280 n->verbose = $4;
6281 n->relation = $5;
6282 n->va_cols = NIL;
6283 $$ = (Node *)n;
6285 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
6287 VacuumStmt *n = (VacuumStmt *) $5;
6288 n->vacuum = true;
6289 n->full = $2;
6290 n->freeze_min_age = $3 ? 0 : -1;
6291 n->freeze_table_age = $3 ? 0 : -1;
6292 n->verbose |= $4;
6293 $$ = (Node *)n;
6297 AnalyzeStmt:
6298 analyze_keyword opt_verbose
6300 VacuumStmt *n = makeNode(VacuumStmt);
6301 n->vacuum = false;
6302 n->analyze = true;
6303 n->full = false;
6304 n->freeze_min_age = -1;
6305 n->freeze_table_age = -1;
6306 n->verbose = $2;
6307 n->relation = NULL;
6308 n->va_cols = NIL;
6309 $$ = (Node *)n;
6311 | analyze_keyword opt_verbose qualified_name opt_name_list
6313 VacuumStmt *n = makeNode(VacuumStmt);
6314 n->vacuum = false;
6315 n->analyze = true;
6316 n->full = false;
6317 n->freeze_min_age = -1;
6318 n->freeze_table_age = -1;
6319 n->verbose = $2;
6320 n->relation = $3;
6321 n->va_cols = $4;
6322 $$ = (Node *)n;
6326 analyze_keyword:
6327 ANALYZE {}
6328 | ANALYSE /* British */ {}
6331 opt_verbose:
6332 VERBOSE { $$ = TRUE; }
6333 | /*EMPTY*/ { $$ = FALSE; }
6336 opt_full: FULL { $$ = TRUE; }
6337 | /*EMPTY*/ { $$ = FALSE; }
6340 opt_freeze: FREEZE { $$ = TRUE; }
6341 | /*EMPTY*/ { $$ = FALSE; }
6344 opt_name_list:
6345 '(' name_list ')' { $$ = $2; }
6346 | /*EMPTY*/ { $$ = NIL; }
6350 /*****************************************************************************
6352 * QUERY:
6353 * EXPLAIN [ANALYZE] [VERBOSE] query
6355 *****************************************************************************/
6357 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
6359 ExplainStmt *n = makeNode(ExplainStmt);
6360 n->analyze = $2;
6361 n->verbose = $3;
6362 n->query = $4;
6363 $$ = (Node *)n;
6367 ExplainableStmt:
6368 SelectStmt
6369 | InsertStmt
6370 | UpdateStmt
6371 | DeleteStmt
6372 | DeclareCursorStmt
6373 | CreateAsStmt
6374 | ExecuteStmt /* by default all are $$=$1 */
6377 opt_analyze:
6378 analyze_keyword { $$ = TRUE; }
6379 | /* EMPTY */ { $$ = FALSE; }
6382 /*****************************************************************************
6384 * QUERY:
6385 * PREPARE <plan_name> [(args, ...)] AS <query>
6387 *****************************************************************************/
6389 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
6391 PrepareStmt *n = makeNode(PrepareStmt);
6392 n->name = $2;
6393 n->argtypes = $3;
6394 n->query = $5;
6395 $$ = (Node *) n;
6399 prep_type_clause: '(' type_list ')' { $$ = $2; }
6400 | /* EMPTY */ { $$ = NIL; }
6403 PreparableStmt:
6404 SelectStmt
6405 | InsertStmt
6406 | UpdateStmt
6407 | DeleteStmt /* by default all are $$=$1 */
6410 /*****************************************************************************
6412 * EXECUTE <plan_name> [(params, ...)]
6413 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
6415 *****************************************************************************/
6417 ExecuteStmt: EXECUTE name execute_param_clause
6419 ExecuteStmt *n = makeNode(ExecuteStmt);
6420 n->name = $2;
6421 n->params = $3;
6422 n->into = NULL;
6423 $$ = (Node *) n;
6425 | CREATE OptTemp TABLE create_as_target AS
6426 EXECUTE name execute_param_clause
6428 ExecuteStmt *n = makeNode(ExecuteStmt);
6429 n->name = $7;
6430 n->params = $8;
6431 $4->rel->istemp = $2;
6432 n->into = $4;
6433 if ($4->colNames)
6434 ereport(ERROR,
6435 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6436 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6437 /* ... because it's not implemented, but it could be */
6438 $$ = (Node *) n;
6442 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6443 | /* EMPTY */ { $$ = NIL; }
6446 /*****************************************************************************
6448 * QUERY:
6449 * DEALLOCATE [PREPARE] <plan_name>
6451 *****************************************************************************/
6453 DeallocateStmt: DEALLOCATE name
6455 DeallocateStmt *n = makeNode(DeallocateStmt);
6456 n->name = $2;
6457 $$ = (Node *) n;
6459 | DEALLOCATE PREPARE name
6461 DeallocateStmt *n = makeNode(DeallocateStmt);
6462 n->name = $3;
6463 $$ = (Node *) n;
6465 | DEALLOCATE ALL
6467 DeallocateStmt *n = makeNode(DeallocateStmt);
6468 n->name = NULL;
6469 $$ = (Node *) n;
6471 | DEALLOCATE PREPARE ALL
6473 DeallocateStmt *n = makeNode(DeallocateStmt);
6474 n->name = NULL;
6475 $$ = (Node *) n;
6479 /*****************************************************************************
6481 * QUERY:
6482 * INSERT STATEMENTS
6484 *****************************************************************************/
6486 InsertStmt:
6487 INSERT INTO qualified_name insert_rest returning_clause
6489 $4->relation = $3;
6490 $4->returningList = $5;
6491 $$ = (Node *) $4;
6495 insert_rest:
6496 SelectStmt
6498 $$ = makeNode(InsertStmt);
6499 $$->cols = NIL;
6500 $$->selectStmt = $1;
6502 | '(' insert_column_list ')' SelectStmt
6504 $$ = makeNode(InsertStmt);
6505 $$->cols = $2;
6506 $$->selectStmt = $4;
6508 | DEFAULT VALUES
6510 $$ = makeNode(InsertStmt);
6511 $$->cols = NIL;
6512 $$->selectStmt = NULL;
6516 insert_column_list:
6517 insert_column_item
6518 { $$ = list_make1($1); }
6519 | insert_column_list ',' insert_column_item
6520 { $$ = lappend($1, $3); }
6523 insert_column_item:
6524 ColId opt_indirection
6526 $$ = makeNode(ResTarget);
6527 $$->name = $1;
6528 $$->indirection = check_indirection($2);
6529 $$->val = NULL;
6530 $$->location = @1;
6534 returning_clause:
6535 RETURNING target_list { $$ = $2; }
6536 | /* EMPTY */ { $$ = NIL; }
6540 /*****************************************************************************
6542 * QUERY:
6543 * DELETE STATEMENTS
6545 *****************************************************************************/
6547 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6548 using_clause where_or_current_clause returning_clause
6550 DeleteStmt *n = makeNode(DeleteStmt);
6551 n->relation = $3;
6552 n->usingClause = $4;
6553 n->whereClause = $5;
6554 n->returningList = $6;
6555 $$ = (Node *)n;
6559 using_clause:
6560 USING from_list { $$ = $2; }
6561 | /*EMPTY*/ { $$ = NIL; }
6565 /*****************************************************************************
6567 * QUERY:
6568 * LOCK TABLE
6570 *****************************************************************************/
6572 LockStmt: LOCK_P opt_table relation_expr_list opt_lock opt_nowait
6574 LockStmt *n = makeNode(LockStmt);
6576 n->relations = $3;
6577 n->mode = $4;
6578 n->nowait = $5;
6579 $$ = (Node *)n;
6583 opt_lock: IN_P lock_type MODE { $$ = $2; }
6584 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6587 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6588 | ROW SHARE { $$ = RowShareLock; }
6589 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6590 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6591 | SHARE { $$ = ShareLock; }
6592 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6593 | EXCLUSIVE { $$ = ExclusiveLock; }
6594 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6597 opt_nowait: NOWAIT { $$ = TRUE; }
6598 | /*EMPTY*/ { $$ = FALSE; }
6602 /*****************************************************************************
6604 * QUERY:
6605 * UpdateStmt (UPDATE)
6607 *****************************************************************************/
6609 UpdateStmt: UPDATE relation_expr_opt_alias
6610 SET set_clause_list
6611 from_clause
6612 where_or_current_clause
6613 returning_clause
6615 UpdateStmt *n = makeNode(UpdateStmt);
6616 n->relation = $2;
6617 n->targetList = $4;
6618 n->fromClause = $5;
6619 n->whereClause = $6;
6620 n->returningList = $7;
6621 $$ = (Node *)n;
6625 set_clause_list:
6626 set_clause { $$ = $1; }
6627 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6630 set_clause:
6631 single_set_clause { $$ = list_make1($1); }
6632 | multiple_set_clause { $$ = $1; }
6635 single_set_clause:
6636 set_target '=' ctext_expr
6638 $$ = $1;
6639 $$->val = (Node *) $3;
6643 multiple_set_clause:
6644 '(' set_target_list ')' '=' ctext_row
6646 ListCell *col_cell;
6647 ListCell *val_cell;
6650 * Break the ctext_row apart, merge individual expressions
6651 * into the destination ResTargets. XXX this approach
6652 * cannot work for general row expressions as sources.
6654 if (list_length($2) != list_length($5))
6655 ereport(ERROR,
6656 (errcode(ERRCODE_SYNTAX_ERROR),
6657 errmsg("number of columns does not match number of values"),
6658 scanner_errposition(@1)));
6659 forboth(col_cell, $2, val_cell, $5)
6661 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6662 Node *res_val = (Node *) lfirst(val_cell);
6664 res_col->val = res_val;
6667 $$ = $2;
6671 set_target:
6672 ColId opt_indirection
6674 $$ = makeNode(ResTarget);
6675 $$->name = $1;
6676 $$->indirection = check_indirection($2);
6677 $$->val = NULL; /* upper production sets this */
6678 $$->location = @1;
6682 set_target_list:
6683 set_target { $$ = list_make1($1); }
6684 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6688 /*****************************************************************************
6690 * QUERY:
6691 * CURSOR STATEMENTS
6693 *****************************************************************************/
6694 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6696 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6697 n->portalname = $2;
6698 /* currently we always set FAST_PLAN option */
6699 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6700 n->query = $7;
6701 $$ = (Node *)n;
6705 cursor_options: /*EMPTY*/ { $$ = 0; }
6706 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6707 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6708 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6709 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6712 opt_hold: /* EMPTY */ { $$ = 0; }
6713 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6714 | WITHOUT HOLD { $$ = 0; }
6717 /*****************************************************************************
6719 * QUERY:
6720 * SELECT STATEMENTS
6722 *****************************************************************************/
6724 /* A complete SELECT statement looks like this.
6726 * The rule returns either a single SelectStmt node or a tree of them,
6727 * representing a set-operation tree.
6729 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6730 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6731 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6732 * To resolve the ambiguity, we are careful to define the grammar so that
6733 * the decision is staved off as long as possible: as long as we can keep
6734 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6735 * it's no longer possible to do that will we decide that parens belong to
6736 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6737 * parentheses are treated as part of the sub-select. The necessity of doing
6738 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6739 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6740 * SELECT viewpoint when we see the UNION.
6742 * This approach is implemented by defining a nonterminal select_with_parens,
6743 * which represents a SELECT with at least one outer layer of parentheses,
6744 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6745 * in the expression grammar. We will then have shift-reduce conflicts
6746 * which we can resolve in favor of always treating '(' <select> ')' as
6747 * a select_with_parens. To resolve the conflicts, the productions that
6748 * conflict with the select_with_parens productions are manually given
6749 * precedences lower than the precedence of ')', thereby ensuring that we
6750 * shift ')' (and then reduce to select_with_parens) rather than trying to
6751 * reduce the inner <select> nonterminal to something else. We use UMINUS
6752 * precedence for this, which is a fairly arbitrary choice.
6754 * To be able to define select_with_parens itself without ambiguity, we need
6755 * a nonterminal select_no_parens that represents a SELECT structure with no
6756 * outermost parentheses. This is a little bit tedious, but it works.
6758 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6759 * with or without outer parentheses.
6762 SelectStmt: select_no_parens %prec UMINUS
6763 | select_with_parens %prec UMINUS
6766 select_with_parens:
6767 '(' select_no_parens ')' { $$ = $2; }
6768 | '(' select_with_parens ')' { $$ = $2; }
6772 * This rule parses the equivalent of the standard's <query expression>.
6773 * The duplicative productions are annoying, but hard to get rid of without
6774 * creating shift/reduce conflicts.
6776 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6777 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6778 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6779 * 2002-08-28 bjm
6781 select_no_parens:
6782 simple_select { $$ = $1; }
6783 | select_clause sort_clause
6785 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6786 NULL, NULL, NULL);
6787 $$ = $1;
6789 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6791 insertSelectOptions((SelectStmt *) $1, $2, $3,
6792 list_nth($4, 0), list_nth($4, 1),
6793 NULL);
6794 $$ = $1;
6796 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6798 insertSelectOptions((SelectStmt *) $1, $2, $4,
6799 list_nth($3, 0), list_nth($3, 1),
6800 NULL);
6801 $$ = $1;
6803 | with_clause simple_select
6805 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6806 NULL, NULL,
6807 $1);
6808 $$ = $2;
6810 | with_clause select_clause sort_clause
6812 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6813 NULL, NULL,
6814 $1);
6815 $$ = $2;
6817 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6819 insertSelectOptions((SelectStmt *) $2, $3, $4,
6820 list_nth($5, 0), list_nth($5, 1),
6821 $1);
6822 $$ = $2;
6824 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6826 insertSelectOptions((SelectStmt *) $2, $3, $5,
6827 list_nth($4, 0), list_nth($4, 1),
6828 $1);
6829 $$ = $2;
6833 select_clause:
6834 simple_select { $$ = $1; }
6835 | select_with_parens { $$ = $1; }
6839 * This rule parses SELECT statements that can appear within set operations,
6840 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6841 * the ordering of the set operations. Without '(' and ')' we want the
6842 * operations to be ordered per the precedence specs at the head of this file.
6844 * As with select_no_parens, simple_select cannot have outer parentheses,
6845 * but can have parenthesized subclauses.
6847 * Note that sort clauses cannot be included at this level --- SQL92 requires
6848 * SELECT foo UNION SELECT bar ORDER BY baz
6849 * to be parsed as
6850 * (SELECT foo UNION SELECT bar) ORDER BY baz
6851 * not
6852 * SELECT foo UNION (SELECT bar ORDER BY baz)
6853 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6854 * described as part of the select_no_parens production, not simple_select.
6855 * This does not limit functionality, because you can reintroduce these
6856 * clauses inside parentheses.
6858 * NOTE: only the leftmost component SelectStmt should have INTO.
6859 * However, this is not checked by the grammar; parse analysis must check it.
6861 simple_select:
6862 SELECT opt_distinct target_list
6863 into_clause from_clause where_clause
6864 group_clause having_clause window_clause
6866 SelectStmt *n = makeNode(SelectStmt);
6867 n->distinctClause = $2;
6868 n->targetList = $3;
6869 n->intoClause = $4;
6870 n->fromClause = $5;
6871 n->whereClause = $6;
6872 n->groupClause = $7;
6873 n->havingClause = $8;
6874 n->windowClause = $9;
6875 $$ = (Node *)n;
6877 | values_clause { $$ = $1; }
6878 | TABLE relation_expr
6880 /* same as SELECT * FROM relation_expr */
6881 ColumnRef *cr = makeNode(ColumnRef);
6882 ResTarget *rt = makeNode(ResTarget);
6883 SelectStmt *n = makeNode(SelectStmt);
6885 cr->fields = list_make1(makeNode(A_Star));
6886 cr->location = -1;
6888 rt->name = NULL;
6889 rt->indirection = NIL;
6890 rt->val = (Node *)cr;
6891 rt->location = -1;
6893 n->targetList = list_make1(rt);
6894 n->fromClause = list_make1($2);
6895 $$ = (Node *)n;
6897 | select_clause UNION opt_all select_clause
6899 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6901 | select_clause INTERSECT opt_all select_clause
6903 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6905 | select_clause EXCEPT opt_all select_clause
6907 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6912 * SQL standard WITH clause looks like:
6914 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6915 * AS (query) [ SEARCH or CYCLE clause ]
6917 * We don't currently support the SEARCH or CYCLE clause.
6919 with_clause:
6920 WITH cte_list
6922 $$ = makeNode(WithClause);
6923 $$->ctes = $2;
6924 $$->recursive = false;
6925 $$->location = @1;
6927 | WITH RECURSIVE cte_list
6929 $$ = makeNode(WithClause);
6930 $$->ctes = $3;
6931 $$->recursive = true;
6932 $$->location = @1;
6936 cte_list:
6937 common_table_expr { $$ = list_make1($1); }
6938 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
6941 common_table_expr: name opt_name_list AS select_with_parens
6943 CommonTableExpr *n = makeNode(CommonTableExpr);
6944 n->ctename = $1;
6945 n->aliascolnames = $2;
6946 n->ctequery = $4;
6947 n->location = @1;
6948 $$ = (Node *) n;
6952 into_clause:
6953 INTO OptTempTableName
6955 $$ = makeNode(IntoClause);
6956 $$->rel = $2;
6957 $$->colNames = NIL;
6958 $$->options = NIL;
6959 $$->onCommit = ONCOMMIT_NOOP;
6960 $$->tableSpaceName = NULL;
6962 | /*EMPTY*/
6963 { $$ = NULL; }
6967 * Redundancy here is needed to avoid shift/reduce conflicts,
6968 * since TEMP is not a reserved word. See also OptTemp.
6970 OptTempTableName:
6971 TEMPORARY opt_table qualified_name
6973 $$ = $3;
6974 $$->istemp = true;
6976 | TEMP opt_table qualified_name
6978 $$ = $3;
6979 $$->istemp = true;
6981 | LOCAL TEMPORARY opt_table qualified_name
6983 $$ = $4;
6984 $$->istemp = true;
6986 | LOCAL TEMP opt_table qualified_name
6988 $$ = $4;
6989 $$->istemp = true;
6991 | GLOBAL TEMPORARY opt_table qualified_name
6993 $$ = $4;
6994 $$->istemp = true;
6996 | GLOBAL TEMP opt_table qualified_name
6998 $$ = $4;
6999 $$->istemp = true;
7001 | TABLE qualified_name
7003 $$ = $2;
7004 $$->istemp = false;
7006 | qualified_name
7008 $$ = $1;
7009 $$->istemp = false;
7013 opt_table: TABLE {}
7014 | /*EMPTY*/ {}
7017 opt_all: ALL { $$ = TRUE; }
7018 | DISTINCT { $$ = FALSE; }
7019 | /*EMPTY*/ { $$ = FALSE; }
7022 /* We use (NIL) as a placeholder to indicate that all target expressions
7023 * should be placed in the DISTINCT list during parsetree analysis.
7025 opt_distinct:
7026 DISTINCT { $$ = list_make1(NIL); }
7027 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
7028 | ALL { $$ = NIL; }
7029 | /*EMPTY*/ { $$ = NIL; }
7032 opt_sort_clause:
7033 sort_clause { $$ = $1;}
7034 | /*EMPTY*/ { $$ = NIL; }
7037 sort_clause:
7038 ORDER BY sortby_list { $$ = $3; }
7041 sortby_list:
7042 sortby { $$ = list_make1($1); }
7043 | sortby_list ',' sortby { $$ = lappend($1, $3); }
7046 sortby: a_expr USING qual_all_Op opt_nulls_order
7048 $$ = makeNode(SortBy);
7049 $$->node = $1;
7050 $$->sortby_dir = SORTBY_USING;
7051 $$->sortby_nulls = $4;
7052 $$->useOp = $3;
7053 $$->location = @3;
7055 | a_expr opt_asc_desc opt_nulls_order
7057 $$ = makeNode(SortBy);
7058 $$->node = $1;
7059 $$->sortby_dir = $2;
7060 $$->sortby_nulls = $3;
7061 $$->useOp = NIL;
7062 $$->location = -1; /* no operator */
7067 select_limit:
7068 LIMIT select_limit_value OFFSET select_offset_value
7069 { $$ = list_make2($4, $2); }
7070 | OFFSET select_offset_value LIMIT select_limit_value
7071 { $$ = list_make2($2, $4); }
7072 | LIMIT select_limit_value
7073 { $$ = list_make2(NULL, $2); }
7074 | OFFSET select_offset_value
7075 { $$ = list_make2($2, NULL); }
7076 | LIMIT select_limit_value ',' select_offset_value
7078 /* Disabled because it was too confusing, bjm 2002-02-18 */
7079 ereport(ERROR,
7080 (errcode(ERRCODE_SYNTAX_ERROR),
7081 errmsg("LIMIT #,# syntax is not supported"),
7082 errhint("Use separate LIMIT and OFFSET clauses."),
7083 scanner_errposition(@1)));
7085 /* SQL:2008 syntax variants */
7086 | OFFSET select_offset_value2 row_or_rows
7087 { $$ = list_make2($2, NULL); }
7088 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7089 { $$ = list_make2(NULL, $3); }
7090 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
7091 { $$ = list_make2($2, $6); }
7094 opt_select_limit:
7095 select_limit { $$ = $1; }
7096 | /* EMPTY */
7097 { $$ = list_make2(NULL,NULL); }
7100 select_limit_value:
7101 a_expr { $$ = $1; }
7102 | ALL
7104 /* LIMIT ALL is represented as a NULL constant */
7105 $$ = makeNullAConst(@1);
7110 * Allowing full expressions without parentheses causes various parsing
7111 * problems with the trailing ROW/ROWS key words. SQL only calls for
7112 * constants, so we allow the rest only with parentheses.
7114 opt_select_fetch_first_value:
7115 SignedIconst { $$ = makeIntConst($1, @1); }
7116 | '(' a_expr ')' { $$ = $2; }
7117 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
7120 select_offset_value:
7121 a_expr { $$ = $1; }
7125 * Again, the trailing ROW/ROWS in this case prevent the full expression
7126 * syntax. c_expr is the best we can do.
7128 select_offset_value2:
7129 c_expr { $$ = $1; }
7132 /* noise words */
7133 row_or_rows:
7134 ROW { $$ = 0; }
7135 | ROWS { $$ = 0; }
7138 /* noise words */
7139 first_or_next:
7140 FIRST_P { $$ = 0; }
7141 | NEXT { $$ = 0; }
7144 group_clause:
7145 GROUP_P BY expr_list { $$ = $3; }
7146 | /*EMPTY*/ { $$ = NIL; }
7149 having_clause:
7150 HAVING a_expr { $$ = $2; }
7151 | /*EMPTY*/ { $$ = NULL; }
7154 for_locking_clause:
7155 for_locking_items { $$ = $1; }
7156 | FOR READ ONLY { $$ = NIL; }
7159 opt_for_locking_clause:
7160 for_locking_clause { $$ = $1; }
7161 | /* EMPTY */ { $$ = NIL; }
7164 for_locking_items:
7165 for_locking_item { $$ = list_make1($1); }
7166 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
7169 for_locking_item:
7170 FOR UPDATE locked_rels_list opt_nowait
7172 LockingClause *n = makeNode(LockingClause);
7173 n->lockedRels = $3;
7174 n->forUpdate = TRUE;
7175 n->noWait = $4;
7176 $$ = (Node *) n;
7178 | FOR SHARE locked_rels_list opt_nowait
7180 LockingClause *n = makeNode(LockingClause);
7181 n->lockedRels = $3;
7182 n->forUpdate = FALSE;
7183 n->noWait = $4;
7184 $$ = (Node *) n;
7188 locked_rels_list:
7189 OF qualified_name_list { $$ = $2; }
7190 | /* EMPTY */ { $$ = NIL; }
7194 values_clause:
7195 VALUES ctext_row
7197 SelectStmt *n = makeNode(SelectStmt);
7198 n->valuesLists = list_make1($2);
7199 $$ = (Node *) n;
7201 | values_clause ',' ctext_row
7203 SelectStmt *n = (SelectStmt *) $1;
7204 n->valuesLists = lappend(n->valuesLists, $3);
7205 $$ = (Node *) n;
7210 /*****************************************************************************
7212 * clauses common to all Optimizable Stmts:
7213 * from_clause - allow list of both JOIN expressions and table names
7214 * where_clause - qualifications for joins or restrictions
7216 *****************************************************************************/
7218 from_clause:
7219 FROM from_list { $$ = $2; }
7220 | /*EMPTY*/ { $$ = NIL; }
7223 from_list:
7224 table_ref { $$ = list_make1($1); }
7225 | from_list ',' table_ref { $$ = lappend($1, $3); }
7229 * table_ref is where an alias clause can be attached. Note we cannot make
7230 * alias_clause have an empty production because that causes parse conflicts
7231 * between table_ref := '(' joined_table ')' alias_clause
7232 * and joined_table := '(' joined_table ')'. So, we must have the
7233 * redundant-looking productions here instead.
7235 table_ref: relation_expr
7237 $$ = (Node *) $1;
7239 | relation_expr alias_clause
7241 $1->alias = $2;
7242 $$ = (Node *) $1;
7244 | func_table
7246 RangeFunction *n = makeNode(RangeFunction);
7247 n->funccallnode = $1;
7248 n->coldeflist = NIL;
7249 $$ = (Node *) n;
7251 | func_table alias_clause
7253 RangeFunction *n = makeNode(RangeFunction);
7254 n->funccallnode = $1;
7255 n->alias = $2;
7256 n->coldeflist = NIL;
7257 $$ = (Node *) n;
7259 | func_table AS '(' TableFuncElementList ')'
7261 RangeFunction *n = makeNode(RangeFunction);
7262 n->funccallnode = $1;
7263 n->coldeflist = $4;
7264 $$ = (Node *) n;
7266 | func_table AS ColId '(' TableFuncElementList ')'
7268 RangeFunction *n = makeNode(RangeFunction);
7269 Alias *a = makeNode(Alias);
7270 n->funccallnode = $1;
7271 a->aliasname = $3;
7272 n->alias = a;
7273 n->coldeflist = $5;
7274 $$ = (Node *) n;
7276 | func_table ColId '(' TableFuncElementList ')'
7278 RangeFunction *n = makeNode(RangeFunction);
7279 Alias *a = makeNode(Alias);
7280 n->funccallnode = $1;
7281 a->aliasname = $2;
7282 n->alias = a;
7283 n->coldeflist = $4;
7284 $$ = (Node *) n;
7286 | select_with_parens
7289 * The SQL spec does not permit a subselect
7290 * (<derived_table>) without an alias clause,
7291 * so we don't either. This avoids the problem
7292 * of needing to invent a unique refname for it.
7293 * That could be surmounted if there's sufficient
7294 * popular demand, but for now let's just implement
7295 * the spec and see if anyone complains.
7296 * However, it does seem like a good idea to emit
7297 * an error message that's better than "syntax error".
7299 if (IsA($1, SelectStmt) &&
7300 ((SelectStmt *) $1)->valuesLists)
7301 ereport(ERROR,
7302 (errcode(ERRCODE_SYNTAX_ERROR),
7303 errmsg("VALUES in FROM must have an alias"),
7304 errhint("For example, FROM (VALUES ...) [AS] foo."),
7305 scanner_errposition(@1)));
7306 else
7307 ereport(ERROR,
7308 (errcode(ERRCODE_SYNTAX_ERROR),
7309 errmsg("subquery in FROM must have an alias"),
7310 errhint("For example, FROM (SELECT ...) [AS] foo."),
7311 scanner_errposition(@1)));
7312 $$ = NULL;
7314 | select_with_parens alias_clause
7316 RangeSubselect *n = makeNode(RangeSubselect);
7317 n->subquery = $1;
7318 n->alias = $2;
7319 $$ = (Node *) n;
7321 | joined_table
7323 $$ = (Node *) $1;
7325 | '(' joined_table ')' alias_clause
7327 $2->alias = $4;
7328 $$ = (Node *) $2;
7334 * It may seem silly to separate joined_table from table_ref, but there is
7335 * method in SQL92's madness: if you don't do it this way you get reduce-
7336 * reduce conflicts, because it's not clear to the parser generator whether
7337 * to expect alias_clause after ')' or not. For the same reason we must
7338 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
7339 * join_type to expand to empty; if we try it, the parser generator can't
7340 * figure out when to reduce an empty join_type right after table_ref.
7342 * Note that a CROSS JOIN is the same as an unqualified
7343 * INNER JOIN, and an INNER JOIN/ON has the same shape
7344 * but a qualification expression to limit membership.
7345 * A NATURAL JOIN implicitly matches column names between
7346 * tables and the shape is determined by which columns are
7347 * in common. We'll collect columns during the later transformations.
7350 joined_table:
7351 '(' joined_table ')'
7353 $$ = $2;
7355 | table_ref CROSS JOIN table_ref
7357 /* CROSS JOIN is same as unqualified inner join */
7358 JoinExpr *n = makeNode(JoinExpr);
7359 n->jointype = JOIN_INNER;
7360 n->isNatural = FALSE;
7361 n->larg = $1;
7362 n->rarg = $4;
7363 n->using = NIL;
7364 n->quals = NULL;
7365 $$ = n;
7367 | table_ref join_type JOIN table_ref join_qual
7369 JoinExpr *n = makeNode(JoinExpr);
7370 n->jointype = $2;
7371 n->isNatural = FALSE;
7372 n->larg = $1;
7373 n->rarg = $4;
7374 if ($5 != NULL && IsA($5, List))
7375 n->using = (List *) $5; /* USING clause */
7376 else
7377 n->quals = $5; /* ON clause */
7378 $$ = n;
7380 | table_ref JOIN table_ref join_qual
7382 /* letting join_type reduce to empty doesn't work */
7383 JoinExpr *n = makeNode(JoinExpr);
7384 n->jointype = JOIN_INNER;
7385 n->isNatural = FALSE;
7386 n->larg = $1;
7387 n->rarg = $3;
7388 if ($4 != NULL && IsA($4, List))
7389 n->using = (List *) $4; /* USING clause */
7390 else
7391 n->quals = $4; /* ON clause */
7392 $$ = n;
7394 | table_ref NATURAL join_type JOIN table_ref
7396 JoinExpr *n = makeNode(JoinExpr);
7397 n->jointype = $3;
7398 n->isNatural = TRUE;
7399 n->larg = $1;
7400 n->rarg = $5;
7401 n->using = NIL; /* figure out which columns later... */
7402 n->quals = NULL; /* fill later */
7403 $$ = n;
7405 | table_ref NATURAL JOIN table_ref
7407 /* letting join_type reduce to empty doesn't work */
7408 JoinExpr *n = makeNode(JoinExpr);
7409 n->jointype = JOIN_INNER;
7410 n->isNatural = TRUE;
7411 n->larg = $1;
7412 n->rarg = $4;
7413 n->using = NIL; /* figure out which columns later... */
7414 n->quals = NULL; /* fill later */
7415 $$ = n;
7419 alias_clause:
7420 AS ColId '(' name_list ')'
7422 $$ = makeNode(Alias);
7423 $$->aliasname = $2;
7424 $$->colnames = $4;
7426 | AS ColId
7428 $$ = makeNode(Alias);
7429 $$->aliasname = $2;
7431 | ColId '(' name_list ')'
7433 $$ = makeNode(Alias);
7434 $$->aliasname = $1;
7435 $$->colnames = $3;
7437 | ColId
7439 $$ = makeNode(Alias);
7440 $$->aliasname = $1;
7444 join_type: FULL join_outer { $$ = JOIN_FULL; }
7445 | LEFT join_outer { $$ = JOIN_LEFT; }
7446 | RIGHT join_outer { $$ = JOIN_RIGHT; }
7447 | INNER_P { $$ = JOIN_INNER; }
7450 /* OUTER is just noise... */
7451 join_outer: OUTER_P { $$ = NULL; }
7452 | /*EMPTY*/ { $$ = NULL; }
7455 /* JOIN qualification clauses
7456 * Possibilities are:
7457 * USING ( column list ) allows only unqualified column names,
7458 * which must match between tables.
7459 * ON expr allows more general qualifications.
7461 * We return USING as a List node, while an ON-expr will not be a List.
7464 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
7465 | ON a_expr { $$ = $2; }
7469 relation_expr:
7470 qualified_name
7472 /* default inheritance */
7473 $$ = $1;
7474 $$->inhOpt = INH_DEFAULT;
7475 $$->alias = NULL;
7477 | qualified_name '*'
7479 /* inheritance query */
7480 $$ = $1;
7481 $$->inhOpt = INH_YES;
7482 $$->alias = NULL;
7484 | ONLY qualified_name
7486 /* no inheritance */
7487 $$ = $2;
7488 $$->inhOpt = INH_NO;
7489 $$->alias = NULL;
7491 | ONLY '(' qualified_name ')'
7493 /* no inheritance, SQL99-style syntax */
7494 $$ = $3;
7495 $$->inhOpt = INH_NO;
7496 $$->alias = NULL;
7501 relation_expr_list:
7502 relation_expr { $$ = list_make1($1); }
7503 | relation_expr_list ',' relation_expr { $$ = lappend($1, $3); }
7508 * Given "UPDATE foo set set ...", we have to decide without looking any
7509 * further ahead whether the first "set" is an alias or the UPDATE's SET
7510 * keyword. Since "set" is allowed as a column name both interpretations
7511 * are feasible. We resolve the shift/reduce conflict by giving the first
7512 * relation_expr_opt_alias production a higher precedence than the SET token
7513 * has, causing the parser to prefer to reduce, in effect assuming that the
7514 * SET is not an alias.
7516 relation_expr_opt_alias: relation_expr %prec UMINUS
7518 $$ = $1;
7520 | relation_expr ColId
7522 Alias *alias = makeNode(Alias);
7523 alias->aliasname = $2;
7524 $1->alias = alias;
7525 $$ = $1;
7527 | relation_expr AS ColId
7529 Alias *alias = makeNode(Alias);
7530 alias->aliasname = $3;
7531 $1->alias = alias;
7532 $$ = $1;
7537 func_table: func_expr { $$ = $1; }
7541 where_clause:
7542 WHERE a_expr { $$ = $2; }
7543 | /*EMPTY*/ { $$ = NULL; }
7546 /* variant for UPDATE and DELETE */
7547 where_or_current_clause:
7548 WHERE a_expr { $$ = $2; }
7549 | WHERE CURRENT_P OF name
7551 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7552 /* cvarno is filled in by parse analysis */
7553 n->cursor_name = $4;
7554 n->cursor_param = 0;
7555 $$ = (Node *) n;
7557 | WHERE CURRENT_P OF PARAM
7559 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7560 /* cvarno is filled in by parse analysis */
7561 n->cursor_name = NULL;
7562 n->cursor_param = $4;
7563 $$ = (Node *) n;
7565 | /*EMPTY*/ { $$ = NULL; }
7569 TableFuncElementList:
7570 TableFuncElement
7572 $$ = list_make1($1);
7574 | TableFuncElementList ',' TableFuncElement
7576 $$ = lappend($1, $3);
7580 TableFuncElement: ColId Typename
7582 ColumnDef *n = makeNode(ColumnDef);
7583 n->colname = $1;
7584 n->typename = $2;
7585 n->constraints = NIL;
7586 n->is_local = true;
7587 $$ = (Node *)n;
7591 /*****************************************************************************
7593 * Type syntax
7594 * SQL92 introduces a large amount of type-specific syntax.
7595 * Define individual clauses to handle these cases, and use
7596 * the generic case to handle regular type-extensible Postgres syntax.
7597 * - thomas 1997-10-10
7599 *****************************************************************************/
7601 Typename: SimpleTypename opt_array_bounds
7603 $$ = $1;
7604 $$->arrayBounds = $2;
7606 | SETOF SimpleTypename opt_array_bounds
7608 $$ = $2;
7609 $$->arrayBounds = $3;
7610 $$->setof = TRUE;
7612 /* SQL standard syntax, currently only one-dimensional */
7613 | SimpleTypename ARRAY '[' Iconst ']'
7615 $$ = $1;
7616 $$->arrayBounds = list_make1(makeInteger($4));
7618 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7620 $$ = $2;
7621 $$->arrayBounds = list_make1(makeInteger($5));
7622 $$->setof = TRUE;
7624 | SimpleTypename ARRAY
7626 $$ = $1;
7627 $$->arrayBounds = list_make1(makeInteger(-1));
7629 | SETOF SimpleTypename ARRAY
7631 $$ = $2;
7632 $$->arrayBounds = list_make1(makeInteger(-1));
7633 $$->setof = TRUE;
7637 opt_array_bounds:
7638 opt_array_bounds '[' ']'
7639 { $$ = lappend($1, makeInteger(-1)); }
7640 | opt_array_bounds '[' Iconst ']'
7641 { $$ = lappend($1, makeInteger($3)); }
7642 | /*EMPTY*/
7643 { $$ = NIL; }
7646 SimpleTypename:
7647 GenericType { $$ = $1; }
7648 | Numeric { $$ = $1; }
7649 | Bit { $$ = $1; }
7650 | Character { $$ = $1; }
7651 | ConstDatetime { $$ = $1; }
7652 | ConstInterval opt_interval
7654 $$ = $1;
7655 $$->typmods = $2;
7657 | ConstInterval '(' Iconst ')' opt_interval
7659 $$ = $1;
7660 if ($5 != NIL)
7662 if (list_length($5) != 1)
7663 ereport(ERROR,
7664 (errcode(ERRCODE_SYNTAX_ERROR),
7665 errmsg("interval precision specified twice"),
7666 scanner_errposition(@1)));
7667 $$->typmods = lappend($5, makeIntConst($3, @3));
7669 else
7670 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7671 makeIntConst($3, @3));
7675 /* We have a separate ConstTypename to allow defaulting fixed-length
7676 * types such as CHAR() and BIT() to an unspecified length.
7677 * SQL9x requires that these default to a length of one, but this
7678 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7679 * where there is an obvious better choice to make.
7680 * Note that ConstInterval is not included here since it must
7681 * be pushed up higher in the rules to accomodate the postfix
7682 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7683 * the generic-type-name case in AExprConst to avoid premature
7684 * reduce/reduce conflicts against function names.
7686 ConstTypename:
7687 Numeric { $$ = $1; }
7688 | ConstBit { $$ = $1; }
7689 | ConstCharacter { $$ = $1; }
7690 | ConstDatetime { $$ = $1; }
7694 * GenericType covers all type names that don't have special syntax mandated
7695 * by the standard, including qualified names. We also allow type modifiers.
7696 * To avoid parsing conflicts against function invocations, the modifiers
7697 * have to be shown as expr_list here, but parse analysis will only accept
7698 * constants for them.
7700 GenericType:
7701 type_function_name opt_type_modifiers
7703 $$ = makeTypeName($1);
7704 $$->typmods = $2;
7705 $$->location = @1;
7707 | type_function_name attrs opt_type_modifiers
7709 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7710 $$->typmods = $3;
7711 $$->location = @1;
7715 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7716 | /* EMPTY */ { $$ = NIL; }
7720 * SQL92 numeric data types
7722 Numeric: INT_P
7724 $$ = SystemTypeName("int4");
7725 $$->location = @1;
7727 | INTEGER
7729 $$ = SystemTypeName("int4");
7730 $$->location = @1;
7732 | SMALLINT
7734 $$ = SystemTypeName("int2");
7735 $$->location = @1;
7737 | BIGINT
7739 $$ = SystemTypeName("int8");
7740 $$->location = @1;
7742 | REAL
7744 $$ = SystemTypeName("float4");
7745 $$->location = @1;
7747 | FLOAT_P opt_float
7749 $$ = $2;
7750 $$->location = @1;
7752 | DOUBLE_P PRECISION
7754 $$ = SystemTypeName("float8");
7755 $$->location = @1;
7757 | DECIMAL_P opt_type_modifiers
7759 $$ = SystemTypeName("numeric");
7760 $$->typmods = $2;
7761 $$->location = @1;
7763 | DEC opt_type_modifiers
7765 $$ = SystemTypeName("numeric");
7766 $$->typmods = $2;
7767 $$->location = @1;
7769 | NUMERIC opt_type_modifiers
7771 $$ = SystemTypeName("numeric");
7772 $$->typmods = $2;
7773 $$->location = @1;
7775 | BOOLEAN_P
7777 $$ = SystemTypeName("bool");
7778 $$->location = @1;
7782 opt_float: '(' Iconst ')'
7785 * Check FLOAT() precision limits assuming IEEE floating
7786 * types - thomas 1997-09-18
7788 if ($2 < 1)
7789 ereport(ERROR,
7790 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7791 errmsg("precision for type float must be at least 1 bit"),
7792 scanner_errposition(@2)));
7793 else if ($2 <= 24)
7794 $$ = SystemTypeName("float4");
7795 else if ($2 <= 53)
7796 $$ = SystemTypeName("float8");
7797 else
7798 ereport(ERROR,
7799 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7800 errmsg("precision for type float must be less than 54 bits"),
7801 scanner_errposition(@2)));
7803 | /*EMPTY*/
7805 $$ = SystemTypeName("float8");
7810 * SQL92 bit-field data types
7811 * The following implements BIT() and BIT VARYING().
7813 Bit: BitWithLength
7815 $$ = $1;
7817 | BitWithoutLength
7819 $$ = $1;
7823 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7824 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7825 ConstBit: BitWithLength
7827 $$ = $1;
7829 | BitWithoutLength
7831 $$ = $1;
7832 $$->typmods = NIL;
7836 BitWithLength:
7837 BIT opt_varying '(' expr_list ')'
7839 char *typname;
7841 typname = $2 ? "varbit" : "bit";
7842 $$ = SystemTypeName(typname);
7843 $$->typmods = $4;
7844 $$->location = @1;
7848 BitWithoutLength:
7849 BIT opt_varying
7851 /* bit defaults to bit(1), varbit to no limit */
7852 if ($2)
7854 $$ = SystemTypeName("varbit");
7856 else
7858 $$ = SystemTypeName("bit");
7859 $$->typmods = list_make1(makeIntConst(1, -1));
7861 $$->location = @1;
7867 * SQL92 character data types
7868 * The following implements CHAR() and VARCHAR().
7870 Character: CharacterWithLength
7872 $$ = $1;
7874 | CharacterWithoutLength
7876 $$ = $1;
7880 ConstCharacter: CharacterWithLength
7882 $$ = $1;
7884 | CharacterWithoutLength
7886 /* Length was not specified so allow to be unrestricted.
7887 * This handles problems with fixed-length (bpchar) strings
7888 * which in column definitions must default to a length
7889 * of one, but should not be constrained if the length
7890 * was not specified.
7892 $$ = $1;
7893 $$->typmods = NIL;
7897 CharacterWithLength: character '(' Iconst ')' opt_charset
7899 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7901 char *type;
7903 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7904 strcpy(type, $1);
7905 strcat(type, "_");
7906 strcat(type, $5);
7907 $1 = type;
7910 $$ = SystemTypeName($1);
7911 $$->typmods = list_make1(makeIntConst($3, @3));
7912 $$->location = @1;
7916 CharacterWithoutLength: character opt_charset
7918 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7920 char *type;
7922 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7923 strcpy(type, $1);
7924 strcat(type, "_");
7925 strcat(type, $2);
7926 $1 = type;
7929 $$ = SystemTypeName($1);
7931 /* char defaults to char(1), varchar to no limit */
7932 if (strcmp($1, "bpchar") == 0)
7933 $$->typmods = list_make1(makeIntConst(1, -1));
7935 $$->location = @1;
7939 character: CHARACTER opt_varying
7940 { $$ = $2 ? "varchar": "bpchar"; }
7941 | CHAR_P opt_varying
7942 { $$ = $2 ? "varchar": "bpchar"; }
7943 | VARCHAR
7944 { $$ = "varchar"; }
7945 | NATIONAL CHARACTER opt_varying
7946 { $$ = $3 ? "varchar": "bpchar"; }
7947 | NATIONAL CHAR_P opt_varying
7948 { $$ = $3 ? "varchar": "bpchar"; }
7949 | NCHAR opt_varying
7950 { $$ = $2 ? "varchar": "bpchar"; }
7953 opt_varying:
7954 VARYING { $$ = TRUE; }
7955 | /*EMPTY*/ { $$ = FALSE; }
7958 opt_charset:
7959 CHARACTER SET ColId { $$ = $3; }
7960 | /*EMPTY*/ { $$ = NULL; }
7964 * SQL92 date/time types
7966 ConstDatetime:
7967 TIMESTAMP '(' Iconst ')' opt_timezone
7969 if ($5)
7970 $$ = SystemTypeName("timestamptz");
7971 else
7972 $$ = SystemTypeName("timestamp");
7973 $$->typmods = list_make1(makeIntConst($3, @3));
7974 $$->location = @1;
7976 | TIMESTAMP opt_timezone
7978 if ($2)
7979 $$ = SystemTypeName("timestamptz");
7980 else
7981 $$ = SystemTypeName("timestamp");
7982 $$->location = @1;
7984 | TIME '(' Iconst ')' opt_timezone
7986 if ($5)
7987 $$ = SystemTypeName("timetz");
7988 else
7989 $$ = SystemTypeName("time");
7990 $$->typmods = list_make1(makeIntConst($3, @3));
7991 $$->location = @1;
7993 | TIME opt_timezone
7995 if ($2)
7996 $$ = SystemTypeName("timetz");
7997 else
7998 $$ = SystemTypeName("time");
7999 $$->location = @1;
8003 ConstInterval:
8004 INTERVAL
8006 $$ = SystemTypeName("interval");
8007 $$->location = @1;
8011 opt_timezone:
8012 WITH_TIME ZONE { $$ = TRUE; }
8013 | WITHOUT TIME ZONE { $$ = FALSE; }
8014 | /*EMPTY*/ { $$ = FALSE; }
8017 opt_interval:
8018 YEAR_P
8019 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
8020 | MONTH_P
8021 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
8022 | DAY_P
8023 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
8024 | HOUR_P
8025 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
8026 | MINUTE_P
8027 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
8028 | interval_second
8029 { $$ = $1; }
8030 | YEAR_P TO MONTH_P
8032 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
8033 INTERVAL_MASK(MONTH), @1));
8035 | DAY_P TO HOUR_P
8037 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8038 INTERVAL_MASK(HOUR), @1));
8040 | DAY_P TO MINUTE_P
8042 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
8043 INTERVAL_MASK(HOUR) |
8044 INTERVAL_MASK(MINUTE), @1));
8046 | DAY_P TO interval_second
8048 $$ = $3;
8049 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
8050 INTERVAL_MASK(HOUR) |
8051 INTERVAL_MASK(MINUTE) |
8052 INTERVAL_MASK(SECOND), @1);
8054 | HOUR_P TO MINUTE_P
8056 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
8057 INTERVAL_MASK(MINUTE), @1));
8059 | HOUR_P TO interval_second
8061 $$ = $3;
8062 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
8063 INTERVAL_MASK(MINUTE) |
8064 INTERVAL_MASK(SECOND), @1);
8066 | MINUTE_P TO interval_second
8068 $$ = $3;
8069 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
8070 INTERVAL_MASK(SECOND), @1);
8072 | /*EMPTY*/
8073 { $$ = NIL; }
8076 interval_second:
8077 SECOND_P
8079 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
8081 | SECOND_P '(' Iconst ')'
8083 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
8084 makeIntConst($3, @3));
8089 /*****************************************************************************
8091 * expression grammar
8093 *****************************************************************************/
8096 * General expressions
8097 * This is the heart of the expression syntax.
8099 * We have two expression types: a_expr is the unrestricted kind, and
8100 * b_expr is a subset that must be used in some places to avoid shift/reduce
8101 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
8102 * because that use of AND conflicts with AND as a boolean operator. So,
8103 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
8105 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
8106 * always be used by surrounding it with parens.
8108 * c_expr is all the productions that are common to a_expr and b_expr;
8109 * it's factored out just to eliminate redundant coding.
8111 a_expr: c_expr { $$ = $1; }
8112 | a_expr TYPECAST Typename
8113 { $$ = makeTypeCast($1, $3, @2); }
8114 | a_expr AT TIME ZONE a_expr
8116 FuncCall *n = makeNode(FuncCall);
8117 n->funcname = SystemFuncName("timezone");
8118 n->args = list_make2($5, $1);
8119 n->agg_star = FALSE;
8120 n->agg_distinct = FALSE;
8121 n->func_variadic = FALSE;
8122 n->over = NULL;
8123 n->location = @2;
8124 $$ = (Node *) n;
8127 * These operators must be called out explicitly in order to make use
8128 * of yacc/bison's automatic operator-precedence handling. All other
8129 * operator names are handled by the generic productions using "Op",
8130 * below; and all those operators will have the same precedence.
8132 * If you add more explicitly-known operators, be sure to add them
8133 * also to b_expr and to the MathOp list above.
8135 | '+' a_expr %prec UMINUS
8136 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8137 | '-' a_expr %prec UMINUS
8138 { $$ = doNegate($2, @1); }
8139 | a_expr '+' a_expr
8140 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8141 | a_expr '-' a_expr
8142 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8143 | a_expr '*' a_expr
8144 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8145 | a_expr '/' a_expr
8146 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8147 | a_expr '%' a_expr
8148 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8149 | a_expr '^' a_expr
8150 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8151 | a_expr '<' a_expr
8152 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8153 | a_expr '>' a_expr
8154 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8155 | a_expr '=' a_expr
8156 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8158 | a_expr qual_Op a_expr %prec Op
8159 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8160 | qual_Op a_expr %prec Op
8161 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8162 | a_expr qual_Op %prec POSTFIXOP
8163 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8165 | a_expr AND a_expr
8166 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
8167 | a_expr OR a_expr
8168 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
8169 | NOT a_expr
8170 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
8172 | a_expr LIKE a_expr
8173 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
8174 | a_expr LIKE a_expr ESCAPE a_expr
8176 FuncCall *n = makeNode(FuncCall);
8177 n->funcname = SystemFuncName("like_escape");
8178 n->args = list_make2($3, $5);
8179 n->agg_star = FALSE;
8180 n->agg_distinct = FALSE;
8181 n->func_variadic = FALSE;
8182 n->over = NULL;
8183 n->location = @4;
8184 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
8186 | a_expr NOT LIKE a_expr
8187 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
8188 | a_expr NOT LIKE a_expr ESCAPE a_expr
8190 FuncCall *n = makeNode(FuncCall);
8191 n->funcname = SystemFuncName("like_escape");
8192 n->args = list_make2($4, $6);
8193 n->agg_star = FALSE;
8194 n->agg_distinct = FALSE;
8195 n->func_variadic = FALSE;
8196 n->over = NULL;
8197 n->location = @5;
8198 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
8200 | a_expr ILIKE a_expr
8201 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
8202 | a_expr ILIKE a_expr ESCAPE a_expr
8204 FuncCall *n = makeNode(FuncCall);
8205 n->funcname = SystemFuncName("like_escape");
8206 n->args = list_make2($3, $5);
8207 n->agg_star = FALSE;
8208 n->agg_distinct = FALSE;
8209 n->func_variadic = FALSE;
8210 n->over = NULL;
8211 n->location = @4;
8212 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
8214 | a_expr NOT ILIKE a_expr
8215 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
8216 | a_expr NOT ILIKE a_expr ESCAPE a_expr
8218 FuncCall *n = makeNode(FuncCall);
8219 n->funcname = SystemFuncName("like_escape");
8220 n->args = list_make2($4, $6);
8221 n->agg_star = FALSE;
8222 n->agg_distinct = FALSE;
8223 n->func_variadic = FALSE;
8224 n->over = NULL;
8225 n->location = @5;
8226 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
8229 | a_expr SIMILAR TO a_expr %prec SIMILAR
8231 FuncCall *n = makeNode(FuncCall);
8232 n->funcname = SystemFuncName("similar_escape");
8233 n->args = list_make2($4, makeNullAConst(-1));
8234 n->agg_star = FALSE;
8235 n->agg_distinct = FALSE;
8236 n->func_variadic = FALSE;
8237 n->over = NULL;
8238 n->location = @2;
8239 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8241 | a_expr SIMILAR TO a_expr ESCAPE a_expr
8243 FuncCall *n = makeNode(FuncCall);
8244 n->funcname = SystemFuncName("similar_escape");
8245 n->args = list_make2($4, $6);
8246 n->agg_star = FALSE;
8247 n->agg_distinct = FALSE;
8248 n->func_variadic = FALSE;
8249 n->over = NULL;
8250 n->location = @5;
8251 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
8253 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
8255 FuncCall *n = makeNode(FuncCall);
8256 n->funcname = SystemFuncName("similar_escape");
8257 n->args = list_make2($5, makeNullAConst(-1));
8258 n->agg_star = FALSE;
8259 n->agg_distinct = FALSE;
8260 n->func_variadic = FALSE;
8261 n->over = NULL;
8262 n->location = @5;
8263 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8265 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
8267 FuncCall *n = makeNode(FuncCall);
8268 n->funcname = SystemFuncName("similar_escape");
8269 n->args = list_make2($5, $7);
8270 n->agg_star = FALSE;
8271 n->agg_distinct = FALSE;
8272 n->func_variadic = FALSE;
8273 n->over = NULL;
8274 n->location = @6;
8275 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
8278 /* NullTest clause
8279 * Define SQL92-style Null test clause.
8280 * Allow two forms described in the standard:
8281 * a IS NULL
8282 * a IS NOT NULL
8283 * Allow two SQL extensions
8284 * a ISNULL
8285 * a NOTNULL
8287 | a_expr IS NULL_P
8289 NullTest *n = makeNode(NullTest);
8290 n->arg = (Expr *) $1;
8291 n->nulltesttype = IS_NULL;
8292 $$ = (Node *)n;
8294 | a_expr ISNULL
8296 NullTest *n = makeNode(NullTest);
8297 n->arg = (Expr *) $1;
8298 n->nulltesttype = IS_NULL;
8299 $$ = (Node *)n;
8301 | a_expr IS NOT NULL_P
8303 NullTest *n = makeNode(NullTest);
8304 n->arg = (Expr *) $1;
8305 n->nulltesttype = IS_NOT_NULL;
8306 $$ = (Node *)n;
8308 | a_expr NOTNULL
8310 NullTest *n = makeNode(NullTest);
8311 n->arg = (Expr *) $1;
8312 n->nulltesttype = IS_NOT_NULL;
8313 $$ = (Node *)n;
8315 | row OVERLAPS row
8317 $$ = (Node *)makeOverlaps($1, $3, @2);
8319 | a_expr IS TRUE_P
8321 BooleanTest *b = makeNode(BooleanTest);
8322 b->arg = (Expr *) $1;
8323 b->booltesttype = IS_TRUE;
8324 $$ = (Node *)b;
8326 | a_expr IS NOT TRUE_P
8328 BooleanTest *b = makeNode(BooleanTest);
8329 b->arg = (Expr *) $1;
8330 b->booltesttype = IS_NOT_TRUE;
8331 $$ = (Node *)b;
8333 | a_expr IS FALSE_P
8335 BooleanTest *b = makeNode(BooleanTest);
8336 b->arg = (Expr *) $1;
8337 b->booltesttype = IS_FALSE;
8338 $$ = (Node *)b;
8340 | a_expr IS NOT FALSE_P
8342 BooleanTest *b = makeNode(BooleanTest);
8343 b->arg = (Expr *) $1;
8344 b->booltesttype = IS_NOT_FALSE;
8345 $$ = (Node *)b;
8347 | a_expr IS UNKNOWN
8349 BooleanTest *b = makeNode(BooleanTest);
8350 b->arg = (Expr *) $1;
8351 b->booltesttype = IS_UNKNOWN;
8352 $$ = (Node *)b;
8354 | a_expr IS NOT UNKNOWN
8356 BooleanTest *b = makeNode(BooleanTest);
8357 b->arg = (Expr *) $1;
8358 b->booltesttype = IS_NOT_UNKNOWN;
8359 $$ = (Node *)b;
8361 | a_expr IS DISTINCT FROM a_expr %prec IS
8363 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8365 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
8367 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8368 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
8369 "=", $1, $6, @2),
8370 @2);
8373 | a_expr IS OF '(' type_list ')' %prec IS
8375 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8377 | a_expr IS NOT OF '(' type_list ')' %prec IS
8379 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8382 * Ideally we would not use hard-wired operators below but instead use
8383 * opclasses. However, mixed data types and other issues make this
8384 * difficult: http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
8386 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8388 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8389 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8390 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8391 @2);
8393 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
8395 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8396 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8397 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8398 @2);
8400 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8402 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
8403 (Node *) makeA_Expr(AEXPR_AND, NIL,
8404 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
8405 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
8406 @2),
8407 (Node *) makeA_Expr(AEXPR_AND, NIL,
8408 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
8409 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
8410 @2),
8411 @2);
8413 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
8415 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
8416 (Node *) makeA_Expr(AEXPR_OR, NIL,
8417 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8418 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8419 @2),
8420 (Node *) makeA_Expr(AEXPR_OR, NIL,
8421 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
8422 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
8423 @2),
8424 @2);
8426 | a_expr IN_P in_expr
8428 /* in_expr returns a SubLink or a list of a_exprs */
8429 if (IsA($3, SubLink))
8431 /* generate foo = ANY (subquery) */
8432 SubLink *n = (SubLink *) $3;
8433 n->subLinkType = ANY_SUBLINK;
8434 n->testexpr = $1;
8435 n->operName = list_make1(makeString("="));
8436 n->location = @2;
8437 $$ = (Node *)n;
8439 else
8441 /* generate scalar IN expression */
8442 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
8445 | a_expr NOT IN_P in_expr
8447 /* in_expr returns a SubLink or a list of a_exprs */
8448 if (IsA($4, SubLink))
8450 /* generate NOT (foo = ANY (subquery)) */
8451 /* Make an = ANY node */
8452 SubLink *n = (SubLink *) $4;
8453 n->subLinkType = ANY_SUBLINK;
8454 n->testexpr = $1;
8455 n->operName = list_make1(makeString("="));
8456 n->location = @3;
8457 /* Stick a NOT on top */
8458 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
8460 else
8462 /* generate scalar NOT IN expression */
8463 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
8466 | a_expr subquery_Op sub_type select_with_parens %prec Op
8468 SubLink *n = makeNode(SubLink);
8469 n->subLinkType = $3;
8470 n->testexpr = $1;
8471 n->operName = $2;
8472 n->subselect = $4;
8473 n->location = @2;
8474 $$ = (Node *)n;
8476 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
8478 if ($3 == ANY_SUBLINK)
8479 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
8480 else
8481 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
8483 | UNIQUE select_with_parens
8485 /* Not sure how to get rid of the parentheses
8486 * but there are lots of shift/reduce errors without them.
8488 * Should be able to implement this by plopping the entire
8489 * select into a node, then transforming the target expressions
8490 * from whatever they are into count(*), and testing the
8491 * entire result equal to one.
8492 * But, will probably implement a separate node in the executor.
8494 ereport(ERROR,
8495 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8496 errmsg("UNIQUE predicate is not yet implemented"),
8497 scanner_errposition(@1)));
8499 | a_expr IS DOCUMENT_P %prec IS
8501 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8502 list_make1($1), @2);
8504 | a_expr IS NOT DOCUMENT_P %prec IS
8506 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8507 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8508 list_make1($1), @2),
8509 @2);
8514 * Restricted expressions
8516 * b_expr is a subset of the complete expression syntax defined by a_expr.
8518 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8519 * cause trouble in the places where b_expr is used. For simplicity, we
8520 * just eliminate all the boolean-keyword-operator productions from b_expr.
8522 b_expr: c_expr
8523 { $$ = $1; }
8524 | b_expr TYPECAST Typename
8525 { $$ = makeTypeCast($1, $3, @2); }
8526 | '+' b_expr %prec UMINUS
8527 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8528 | '-' b_expr %prec UMINUS
8529 { $$ = doNegate($2, @1); }
8530 | b_expr '+' b_expr
8531 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8532 | b_expr '-' b_expr
8533 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8534 | b_expr '*' b_expr
8535 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8536 | b_expr '/' b_expr
8537 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8538 | b_expr '%' b_expr
8539 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8540 | b_expr '^' b_expr
8541 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8542 | b_expr '<' b_expr
8543 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8544 | b_expr '>' b_expr
8545 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8546 | b_expr '=' b_expr
8547 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8548 | b_expr qual_Op b_expr %prec Op
8549 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8550 | qual_Op b_expr %prec Op
8551 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8552 | b_expr qual_Op %prec POSTFIXOP
8553 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8554 | b_expr IS DISTINCT FROM b_expr %prec IS
8556 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8558 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8560 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8561 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8563 | b_expr IS OF '(' type_list ')' %prec IS
8565 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8567 | b_expr IS NOT OF '(' type_list ')' %prec IS
8569 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8571 | b_expr IS DOCUMENT_P %prec IS
8573 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8574 list_make1($1), @2);
8576 | b_expr IS NOT DOCUMENT_P %prec IS
8578 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8579 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8580 list_make1($1), @2),
8581 @2);
8586 * Productions that can be used in both a_expr and b_expr.
8588 * Note: productions that refer recursively to a_expr or b_expr mostly
8589 * cannot appear here. However, it's OK to refer to a_exprs that occur
8590 * inside parentheses, such as function arguments; that cannot introduce
8591 * ambiguity to the b_expr syntax.
8593 c_expr: columnref { $$ = $1; }
8594 | AexprConst { $$ = $1; }
8595 | PARAM opt_indirection
8597 ParamRef *p = makeNode(ParamRef);
8598 p->number = $1;
8599 p->location = @1;
8600 if ($2)
8602 A_Indirection *n = makeNode(A_Indirection);
8603 n->arg = (Node *) p;
8604 n->indirection = check_indirection($2);
8605 $$ = (Node *) n;
8607 else
8608 $$ = (Node *) p;
8610 | '(' a_expr ')' opt_indirection
8612 if ($4)
8614 A_Indirection *n = makeNode(A_Indirection);
8615 n->arg = $2;
8616 n->indirection = check_indirection($4);
8617 $$ = (Node *)n;
8619 else
8620 $$ = $2;
8622 | case_expr
8623 { $$ = $1; }
8624 | func_expr
8625 { $$ = $1; }
8626 | select_with_parens %prec UMINUS
8628 SubLink *n = makeNode(SubLink);
8629 n->subLinkType = EXPR_SUBLINK;
8630 n->testexpr = NULL;
8631 n->operName = NIL;
8632 n->subselect = $1;
8633 n->location = @1;
8634 $$ = (Node *)n;
8636 | EXISTS select_with_parens
8638 SubLink *n = makeNode(SubLink);
8639 n->subLinkType = EXISTS_SUBLINK;
8640 n->testexpr = NULL;
8641 n->operName = NIL;
8642 n->subselect = $2;
8643 n->location = @1;
8644 $$ = (Node *)n;
8646 | ARRAY select_with_parens
8648 SubLink *n = makeNode(SubLink);
8649 n->subLinkType = ARRAY_SUBLINK;
8650 n->testexpr = NULL;
8651 n->operName = NIL;
8652 n->subselect = $2;
8653 n->location = @1;
8654 $$ = (Node *)n;
8656 | ARRAY array_expr
8658 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8659 Assert(IsA(n, A_ArrayExpr));
8660 /* point outermost A_ArrayExpr to the ARRAY keyword */
8661 n->location = @1;
8662 $$ = (Node *)n;
8664 | row
8666 RowExpr *r = makeNode(RowExpr);
8667 r->args = $1;
8668 r->row_typeid = InvalidOid; /* not analyzed yet */
8669 r->location = @1;
8670 $$ = (Node *)r;
8675 * func_expr is split out from c_expr just so that we have a classification
8676 * for "everything that is a function call or looks like one". This isn't
8677 * very important, but it saves us having to document which variants are
8678 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8679 * (Note that many of the special SQL functions wouldn't actually make any
8680 * sense as functional index entries, but we ignore that consideration here.)
8682 func_expr: func_name '(' ')' over_clause
8684 FuncCall *n = makeNode(FuncCall);
8685 n->funcname = $1;
8686 n->args = NIL;
8687 n->agg_star = FALSE;
8688 n->agg_distinct = FALSE;
8689 n->func_variadic = FALSE;
8690 n->over = $4;
8691 n->location = @1;
8692 $$ = (Node *)n;
8694 | func_name '(' expr_list ')' over_clause
8696 FuncCall *n = makeNode(FuncCall);
8697 n->funcname = $1;
8698 n->args = $3;
8699 n->agg_star = FALSE;
8700 n->agg_distinct = FALSE;
8701 n->func_variadic = FALSE;
8702 n->over = $5;
8703 n->location = @1;
8704 $$ = (Node *)n;
8706 | func_name '(' VARIADIC a_expr ')' over_clause
8708 FuncCall *n = makeNode(FuncCall);
8709 n->funcname = $1;
8710 n->args = list_make1($4);
8711 n->agg_star = FALSE;
8712 n->agg_distinct = FALSE;
8713 n->func_variadic = TRUE;
8714 n->over = $6;
8715 n->location = @1;
8716 $$ = (Node *)n;
8718 | func_name '(' expr_list ',' VARIADIC a_expr ')' over_clause
8720 FuncCall *n = makeNode(FuncCall);
8721 n->funcname = $1;
8722 n->args = lappend($3, $6);
8723 n->agg_star = FALSE;
8724 n->agg_distinct = FALSE;
8725 n->func_variadic = TRUE;
8726 n->over = $8;
8727 n->location = @1;
8728 $$ = (Node *)n;
8730 | func_name '(' ALL expr_list ')' over_clause
8732 FuncCall *n = makeNode(FuncCall);
8733 n->funcname = $1;
8734 n->args = $4;
8735 n->agg_star = FALSE;
8736 n->agg_distinct = FALSE;
8737 /* Ideally we'd mark the FuncCall node to indicate
8738 * "must be an aggregate", but there's no provision
8739 * for that in FuncCall at the moment.
8741 n->func_variadic = FALSE;
8742 n->over = $6;
8743 n->location = @1;
8744 $$ = (Node *)n;
8746 | func_name '(' DISTINCT expr_list ')' over_clause
8748 FuncCall *n = makeNode(FuncCall);
8749 n->funcname = $1;
8750 n->args = $4;
8751 n->agg_star = FALSE;
8752 n->agg_distinct = TRUE;
8753 n->func_variadic = FALSE;
8754 n->over = $6;
8755 n->location = @1;
8756 $$ = (Node *)n;
8758 | func_name '(' '*' ')' over_clause
8761 * We consider AGGREGATE(*) to invoke a parameterless
8762 * aggregate. This does the right thing for COUNT(*),
8763 * and there are no other aggregates in SQL92 that accept
8764 * '*' as parameter.
8766 * The FuncCall node is also marked agg_star = true,
8767 * so that later processing can detect what the argument
8768 * really was.
8770 FuncCall *n = makeNode(FuncCall);
8771 n->funcname = $1;
8772 n->args = NIL;
8773 n->agg_star = TRUE;
8774 n->agg_distinct = FALSE;
8775 n->func_variadic = FALSE;
8776 n->over = $5;
8777 n->location = @1;
8778 $$ = (Node *)n;
8780 | CURRENT_DATE
8783 * Translate as "'now'::text::date".
8785 * We cannot use "'now'::date" because coerce_type() will
8786 * immediately reduce that to a constant representing
8787 * today's date. We need to delay the conversion until
8788 * runtime, else the wrong things will happen when
8789 * CURRENT_DATE is used in a column default value or rule.
8791 * This could be simplified if we had a way to generate
8792 * an expression tree representing runtime application
8793 * of type-input conversion functions. (As of PG 7.3
8794 * that is actually possible, but not clear that we want
8795 * to rely on it.)
8797 Node *n;
8798 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8799 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8801 | CURRENT_TIME
8804 * Translate as "'now'::text::timetz".
8805 * See comments for CURRENT_DATE.
8807 Node *n;
8808 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8809 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8811 | CURRENT_TIME '(' Iconst ')'
8814 * Translate as "'now'::text::timetz(n)".
8815 * See comments for CURRENT_DATE.
8817 Node *n;
8818 TypeName *d;
8819 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8820 d = SystemTypeName("timetz");
8821 d->typmods = list_make1(makeIntConst($3, @3));
8822 $$ = makeTypeCast(n, d, -1);
8824 | CURRENT_TIMESTAMP
8827 * Translate as "now()", since we have a function that
8828 * does exactly what is needed.
8830 FuncCall *n = makeNode(FuncCall);
8831 n->funcname = SystemFuncName("now");
8832 n->args = NIL;
8833 n->agg_star = FALSE;
8834 n->agg_distinct = FALSE;
8835 n->func_variadic = FALSE;
8836 n->over = NULL;
8837 n->location = @1;
8838 $$ = (Node *)n;
8840 | CURRENT_TIMESTAMP '(' Iconst ')'
8843 * Translate as "'now'::text::timestamptz(n)".
8844 * See comments for CURRENT_DATE.
8846 Node *n;
8847 TypeName *d;
8848 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8849 d = SystemTypeName("timestamptz");
8850 d->typmods = list_make1(makeIntConst($3, @3));
8851 $$ = makeTypeCast(n, d, -1);
8853 | LOCALTIME
8856 * Translate as "'now'::text::time".
8857 * See comments for CURRENT_DATE.
8859 Node *n;
8860 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8861 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8863 | LOCALTIME '(' Iconst ')'
8866 * Translate as "'now'::text::time(n)".
8867 * See comments for CURRENT_DATE.
8869 Node *n;
8870 TypeName *d;
8871 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8872 d = SystemTypeName("time");
8873 d->typmods = list_make1(makeIntConst($3, @3));
8874 $$ = makeTypeCast((Node *)n, d, -1);
8876 | LOCALTIMESTAMP
8879 * Translate as "'now'::text::timestamp".
8880 * See comments for CURRENT_DATE.
8882 Node *n;
8883 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8884 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8886 | LOCALTIMESTAMP '(' Iconst ')'
8889 * Translate as "'now'::text::timestamp(n)".
8890 * See comments for CURRENT_DATE.
8892 Node *n;
8893 TypeName *d;
8894 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8895 d = SystemTypeName("timestamp");
8896 d->typmods = list_make1(makeIntConst($3, @3));
8897 $$ = makeTypeCast(n, d, -1);
8899 | CURRENT_ROLE
8901 FuncCall *n = makeNode(FuncCall);
8902 n->funcname = SystemFuncName("current_user");
8903 n->args = NIL;
8904 n->agg_star = FALSE;
8905 n->agg_distinct = FALSE;
8906 n->func_variadic = FALSE;
8907 n->over = NULL;
8908 n->location = @1;
8909 $$ = (Node *)n;
8911 | CURRENT_USER
8913 FuncCall *n = makeNode(FuncCall);
8914 n->funcname = SystemFuncName("current_user");
8915 n->args = NIL;
8916 n->agg_star = FALSE;
8917 n->agg_distinct = FALSE;
8918 n->func_variadic = FALSE;
8919 n->over = NULL;
8920 n->location = @1;
8921 $$ = (Node *)n;
8923 | SESSION_USER
8925 FuncCall *n = makeNode(FuncCall);
8926 n->funcname = SystemFuncName("session_user");
8927 n->args = NIL;
8928 n->agg_star = FALSE;
8929 n->agg_distinct = FALSE;
8930 n->func_variadic = FALSE;
8931 n->over = NULL;
8932 n->location = @1;
8933 $$ = (Node *)n;
8935 | USER
8937 FuncCall *n = makeNode(FuncCall);
8938 n->funcname = SystemFuncName("current_user");
8939 n->args = NIL;
8940 n->agg_star = FALSE;
8941 n->agg_distinct = FALSE;
8942 n->func_variadic = FALSE;
8943 n->over = NULL;
8944 n->location = @1;
8945 $$ = (Node *)n;
8947 | CURRENT_CATALOG
8949 FuncCall *n = makeNode(FuncCall);
8950 n->funcname = SystemFuncName("current_database");
8951 n->args = NIL;
8952 n->agg_star = FALSE;
8953 n->agg_distinct = FALSE;
8954 n->func_variadic = FALSE;
8955 n->over = NULL;
8956 n->location = @1;
8957 $$ = (Node *)n;
8959 | CURRENT_SCHEMA
8961 FuncCall *n = makeNode(FuncCall);
8962 n->funcname = SystemFuncName("current_schema");
8963 n->args = NIL;
8964 n->agg_star = FALSE;
8965 n->agg_distinct = FALSE;
8966 n->func_variadic = FALSE;
8967 n->over = NULL;
8968 n->location = @1;
8969 $$ = (Node *)n;
8971 | CAST '(' a_expr AS Typename ')'
8972 { $$ = makeTypeCast($3, $5, @1); }
8973 | EXTRACT '(' extract_list ')'
8975 FuncCall *n = makeNode(FuncCall);
8976 n->funcname = SystemFuncName("date_part");
8977 n->args = $3;
8978 n->agg_star = FALSE;
8979 n->agg_distinct = FALSE;
8980 n->func_variadic = FALSE;
8981 n->over = NULL;
8982 n->location = @1;
8983 $$ = (Node *)n;
8985 | OVERLAY '(' overlay_list ')'
8987 /* overlay(A PLACING B FROM C FOR D) is converted to
8988 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8989 * overlay(A PLACING B FROM C) is converted to
8990 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8992 FuncCall *n = makeNode(FuncCall);
8993 n->funcname = SystemFuncName("overlay");
8994 n->args = $3;
8995 n->agg_star = FALSE;
8996 n->agg_distinct = FALSE;
8997 n->func_variadic = FALSE;
8998 n->over = NULL;
8999 n->location = @1;
9000 $$ = (Node *)n;
9002 | POSITION '(' position_list ')'
9004 /* position(A in B) is converted to position(B, A) */
9005 FuncCall *n = makeNode(FuncCall);
9006 n->funcname = SystemFuncName("position");
9007 n->args = $3;
9008 n->agg_star = FALSE;
9009 n->agg_distinct = FALSE;
9010 n->func_variadic = FALSE;
9011 n->over = NULL;
9012 n->location = @1;
9013 $$ = (Node *)n;
9015 | SUBSTRING '(' substr_list ')'
9017 /* substring(A from B for C) is converted to
9018 * substring(A, B, C) - thomas 2000-11-28
9020 FuncCall *n = makeNode(FuncCall);
9021 n->funcname = SystemFuncName("substring");
9022 n->args = $3;
9023 n->agg_star = FALSE;
9024 n->agg_distinct = FALSE;
9025 n->func_variadic = FALSE;
9026 n->over = NULL;
9027 n->location = @1;
9028 $$ = (Node *)n;
9030 | TREAT '(' a_expr AS Typename ')'
9032 /* TREAT(expr AS target) converts expr of a particular type to target,
9033 * which is defined to be a subtype of the original expression.
9034 * In SQL99, this is intended for use with structured UDTs,
9035 * but let's make this a generally useful form allowing stronger
9036 * coercions than are handled by implicit casting.
9038 FuncCall *n = makeNode(FuncCall);
9039 /* Convert SystemTypeName() to SystemFuncName() even though
9040 * at the moment they result in the same thing.
9042 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
9043 n->args = list_make1($3);
9044 n->agg_star = FALSE;
9045 n->agg_distinct = FALSE;
9046 n->func_variadic = FALSE;
9047 n->over = NULL;
9048 n->location = @1;
9049 $$ = (Node *)n;
9051 | TRIM '(' BOTH trim_list ')'
9053 /* various trim expressions are defined in SQL92
9054 * - thomas 1997-07-19
9056 FuncCall *n = makeNode(FuncCall);
9057 n->funcname = SystemFuncName("btrim");
9058 n->args = $4;
9059 n->agg_star = FALSE;
9060 n->agg_distinct = FALSE;
9061 n->func_variadic = FALSE;
9062 n->over = NULL;
9063 n->location = @1;
9064 $$ = (Node *)n;
9066 | TRIM '(' LEADING trim_list ')'
9068 FuncCall *n = makeNode(FuncCall);
9069 n->funcname = SystemFuncName("ltrim");
9070 n->args = $4;
9071 n->agg_star = FALSE;
9072 n->agg_distinct = FALSE;
9073 n->func_variadic = FALSE;
9074 n->over = NULL;
9075 n->location = @1;
9076 $$ = (Node *)n;
9078 | TRIM '(' TRAILING trim_list ')'
9080 FuncCall *n = makeNode(FuncCall);
9081 n->funcname = SystemFuncName("rtrim");
9082 n->args = $4;
9083 n->agg_star = FALSE;
9084 n->agg_distinct = FALSE;
9085 n->func_variadic = FALSE;
9086 n->over = NULL;
9087 n->location = @1;
9088 $$ = (Node *)n;
9090 | TRIM '(' trim_list ')'
9092 FuncCall *n = makeNode(FuncCall);
9093 n->funcname = SystemFuncName("btrim");
9094 n->args = $3;
9095 n->agg_star = FALSE;
9096 n->agg_distinct = FALSE;
9097 n->func_variadic = FALSE;
9098 n->over = NULL;
9099 n->location = @1;
9100 $$ = (Node *)n;
9102 | NULLIF '(' a_expr ',' a_expr ')'
9104 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
9106 | COALESCE '(' expr_list ')'
9108 CoalesceExpr *c = makeNode(CoalesceExpr);
9109 c->args = $3;
9110 c->location = @1;
9111 $$ = (Node *)c;
9113 | GREATEST '(' expr_list ')'
9115 MinMaxExpr *v = makeNode(MinMaxExpr);
9116 v->args = $3;
9117 v->op = IS_GREATEST;
9118 v->location = @1;
9119 $$ = (Node *)v;
9121 | LEAST '(' expr_list ')'
9123 MinMaxExpr *v = makeNode(MinMaxExpr);
9124 v->args = $3;
9125 v->op = IS_LEAST;
9126 v->location = @1;
9127 $$ = (Node *)v;
9129 | XMLCONCAT '(' expr_list ')'
9131 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
9133 | XMLELEMENT '(' NAME_P ColLabel ')'
9135 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
9137 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
9139 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
9141 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
9143 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
9145 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
9147 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
9149 | XMLFOREST '(' xml_attribute_list ')'
9151 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
9153 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
9155 XmlExpr *x = (XmlExpr *)
9156 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
9157 list_make2($4, makeBoolAConst($5, -1)),
9158 @1);
9159 x->xmloption = $3;
9160 $$ = (Node *)x;
9162 | XMLPI '(' NAME_P ColLabel ')'
9164 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
9166 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
9168 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
9170 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
9172 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
9173 list_make3($3, $5, $6), @1);
9175 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
9177 XmlSerialize *n = makeNode(XmlSerialize);
9178 n->xmloption = $3;
9179 n->expr = $4;
9180 n->typename = $6;
9181 n->location = @1;
9182 $$ = (Node *)n;
9187 * SQL/XML support
9189 xml_root_version: VERSION_P a_expr
9190 { $$ = $2; }
9191 | VERSION_P NO VALUE_P
9192 { $$ = makeNullAConst(-1); }
9195 opt_xml_root_standalone: ',' STANDALONE_P YES_P
9196 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
9197 | ',' STANDALONE_P NO
9198 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
9199 | ',' STANDALONE_P NO VALUE_P
9200 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
9201 | /*EMPTY*/
9202 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
9205 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
9208 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
9209 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
9212 xml_attribute_el: a_expr AS ColLabel
9214 $$ = makeNode(ResTarget);
9215 $$->name = $3;
9216 $$->indirection = NIL;
9217 $$->val = (Node *) $1;
9218 $$->location = @1;
9220 | a_expr
9222 $$ = makeNode(ResTarget);
9223 $$->name = NULL;
9224 $$->indirection = NIL;
9225 $$->val = (Node *) $1;
9226 $$->location = @1;
9230 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
9231 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
9234 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
9235 | STRIP_P WHITESPACE_P { $$ = FALSE; }
9236 | /*EMPTY*/ { $$ = FALSE; }
9240 * Window Definitions
9242 window_clause:
9243 WINDOW window_definition_list { $$ = $2; }
9244 | /*EMPTY*/ { $$ = NIL; }
9247 window_definition_list:
9248 window_definition { $$ = list_make1($1); }
9249 | window_definition_list ',' window_definition
9250 { $$ = lappend($1, $3); }
9253 window_definition:
9254 ColId AS window_specification
9256 WindowDef *n = $3;
9257 n->name = $1;
9258 $$ = n;
9262 over_clause: OVER window_specification
9263 { $$ = $2; }
9264 | OVER ColId
9266 WindowDef *n = makeNode(WindowDef);
9267 n->name = $2;
9268 n->refname = NULL;
9269 n->partitionClause = NIL;
9270 n->orderClause = NIL;
9271 n->frameOptions = FRAMEOPTION_DEFAULTS;
9272 n->location = @2;
9273 $$ = n;
9275 | /*EMPTY*/
9276 { $$ = NULL; }
9279 window_specification: '(' opt_existing_window_name opt_partition_clause
9280 opt_sort_clause opt_frame_clause ')'
9282 WindowDef *n = makeNode(WindowDef);
9283 n->name = NULL;
9284 n->refname = $2;
9285 n->partitionClause = $3;
9286 n->orderClause = $4;
9287 n->frameOptions = $5;
9288 n->location = @1;
9289 $$ = n;
9294 * If we see PARTITION, RANGE, or ROWS as the first token after the '('
9295 * of a window_specification, we want the assumption to be that there is
9296 * no existing_window_name; but those keywords are unreserved and so could
9297 * be ColIds. We fix this by making them have the same precedence as IDENT
9298 * and giving the empty production here a slightly higher precedence, so
9299 * that the shift/reduce conflict is resolved in favor of reducing the rule.
9300 * These keywords are thus precluded from being an existing_window_name but
9301 * are not reserved for any other purpose.
9303 opt_existing_window_name: ColId { $$ = $1; }
9304 | /*EMPTY*/ %prec Op { $$ = NULL; }
9307 opt_partition_clause: PARTITION BY expr_list { $$ = $3; }
9308 | /*EMPTY*/ { $$ = NIL; }
9312 * This is only a subset of the full SQL:2008 frame_clause grammar.
9313 * We don't support <expression> PRECEDING, <expression> FOLLOWING,
9314 * nor <window frame exclusion> yet.
9316 opt_frame_clause:
9317 RANGE frame_extent
9319 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_RANGE | $2;
9321 | ROWS frame_extent
9323 $$ = FRAMEOPTION_NONDEFAULT | FRAMEOPTION_ROWS | $2;
9325 | /*EMPTY*/
9326 { $$ = FRAMEOPTION_DEFAULTS; }
9329 frame_extent: frame_bound
9331 /* reject invalid cases */
9332 if ($1 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9333 ereport(ERROR,
9334 (errcode(ERRCODE_WINDOWING_ERROR),
9335 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9336 scanner_errposition(@1)));
9337 if ($1 & FRAMEOPTION_START_CURRENT_ROW)
9338 ereport(ERROR,
9339 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9340 errmsg("frame start at CURRENT ROW is not implemented"),
9341 scanner_errposition(@1)));
9342 $$ = $1 | FRAMEOPTION_END_CURRENT_ROW;
9344 | BETWEEN frame_bound AND frame_bound
9346 /* reject invalid cases */
9347 if ($2 & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)
9348 ereport(ERROR,
9349 (errcode(ERRCODE_WINDOWING_ERROR),
9350 errmsg("frame start cannot be UNBOUNDED FOLLOWING"),
9351 scanner_errposition(@2)));
9352 if ($2 & FRAMEOPTION_START_CURRENT_ROW)
9353 ereport(ERROR,
9354 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
9355 errmsg("frame start at CURRENT ROW is not implemented"),
9356 scanner_errposition(@2)));
9357 if ($4 & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
9358 ereport(ERROR,
9359 (errcode(ERRCODE_WINDOWING_ERROR),
9360 errmsg("frame end cannot be UNBOUNDED PRECEDING"),
9361 scanner_errposition(@4)));
9362 /* shift converts START_ options to END_ options */
9363 $$ = FRAMEOPTION_BETWEEN | $2 | ($4 << 1);
9368 * This is used for both frame start and frame end, with output set up on
9369 * the assumption it's frame start; the frame_extent productions must reject
9370 * invalid cases.
9372 frame_bound:
9373 UNBOUNDED PRECEDING
9375 $$ = FRAMEOPTION_START_UNBOUNDED_PRECEDING;
9377 | UNBOUNDED FOLLOWING
9379 $$ = FRAMEOPTION_START_UNBOUNDED_FOLLOWING;
9381 | CURRENT_P ROW
9383 $$ = FRAMEOPTION_START_CURRENT_ROW;
9389 * Supporting nonterminals for expressions.
9392 /* Explicit row production.
9394 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
9395 * without conflicting with the parenthesized a_expr production. Without the
9396 * ROW keyword, there must be more than one a_expr inside the parens.
9398 row: ROW '(' expr_list ')' { $$ = $3; }
9399 | ROW '(' ')' { $$ = NIL; }
9400 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
9403 sub_type: ANY { $$ = ANY_SUBLINK; }
9404 | SOME { $$ = ANY_SUBLINK; }
9405 | ALL { $$ = ALL_SUBLINK; }
9408 all_Op: Op { $$ = $1; }
9409 | MathOp { $$ = $1; }
9412 MathOp: '+' { $$ = "+"; }
9413 | '-' { $$ = "-"; }
9414 | '*' { $$ = "*"; }
9415 | '/' { $$ = "/"; }
9416 | '%' { $$ = "%"; }
9417 | '^' { $$ = "^"; }
9418 | '<' { $$ = "<"; }
9419 | '>' { $$ = ">"; }
9420 | '=' { $$ = "="; }
9423 qual_Op: Op
9424 { $$ = list_make1(makeString($1)); }
9425 | OPERATOR '(' any_operator ')'
9426 { $$ = $3; }
9429 qual_all_Op:
9430 all_Op
9431 { $$ = list_make1(makeString($1)); }
9432 | OPERATOR '(' any_operator ')'
9433 { $$ = $3; }
9436 subquery_Op:
9437 all_Op
9438 { $$ = list_make1(makeString($1)); }
9439 | OPERATOR '(' any_operator ')'
9440 { $$ = $3; }
9441 | LIKE
9442 { $$ = list_make1(makeString("~~")); }
9443 | NOT LIKE
9444 { $$ = list_make1(makeString("!~~")); }
9445 | ILIKE
9446 { $$ = list_make1(makeString("~~*")); }
9447 | NOT ILIKE
9448 { $$ = list_make1(makeString("!~~*")); }
9449 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
9450 * the regular expression is preprocessed by a function (similar_escape),
9451 * and the ~ operator for posix regular expressions is used.
9452 * x SIMILAR TO y -> x ~ similar_escape(y)
9453 * this transformation is made on the fly by the parser upwards.
9454 * however the SubLink structure which handles any/some/all stuff
9455 * is not ready for such a thing.
9459 expr_list: a_expr
9461 $$ = list_make1($1);
9463 | expr_list ',' a_expr
9465 $$ = lappend($1, $3);
9469 type_list: Typename { $$ = list_make1($1); }
9470 | type_list ',' Typename { $$ = lappend($1, $3); }
9473 array_expr: '[' expr_list ']'
9475 $$ = makeAArrayExpr($2, @1);
9477 | '[' array_expr_list ']'
9479 $$ = makeAArrayExpr($2, @1);
9481 | '[' ']'
9483 $$ = makeAArrayExpr(NIL, @1);
9487 array_expr_list: array_expr { $$ = list_make1($1); }
9488 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
9492 extract_list:
9493 extract_arg FROM a_expr
9495 $$ = list_make2(makeStringConst($1, @1), $3);
9497 | /*EMPTY*/ { $$ = NIL; }
9500 /* Allow delimited string Sconst in extract_arg as an SQL extension.
9501 * - thomas 2001-04-12
9503 extract_arg:
9504 IDENT { $$ = $1; }
9505 | YEAR_P { $$ = "year"; }
9506 | MONTH_P { $$ = "month"; }
9507 | DAY_P { $$ = "day"; }
9508 | HOUR_P { $$ = "hour"; }
9509 | MINUTE_P { $$ = "minute"; }
9510 | SECOND_P { $$ = "second"; }
9511 | Sconst { $$ = $1; }
9514 /* OVERLAY() arguments
9515 * SQL99 defines the OVERLAY() function:
9516 * o overlay(text placing text from int for int)
9517 * o overlay(text placing text from int)
9519 overlay_list:
9520 a_expr overlay_placing substr_from substr_for
9522 $$ = list_make4($1, $2, $3, $4);
9524 | a_expr overlay_placing substr_from
9526 $$ = list_make3($1, $2, $3);
9530 overlay_placing:
9531 PLACING a_expr
9532 { $$ = $2; }
9535 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
9537 position_list:
9538 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
9539 | /*EMPTY*/ { $$ = NIL; }
9542 /* SUBSTRING() arguments
9543 * SQL9x defines a specific syntax for arguments to SUBSTRING():
9544 * o substring(text from int for int)
9545 * o substring(text from int) get entire string from starting point "int"
9546 * o substring(text for int) get first "int" characters of string
9547 * o substring(text from pattern) get entire string matching pattern
9548 * o substring(text from pattern for escape) same with specified escape char
9549 * We also want to support generic substring functions which accept
9550 * the usual generic list of arguments. So we will accept both styles
9551 * here, and convert the SQL9x style to the generic list for further
9552 * processing. - thomas 2000-11-28
9554 substr_list:
9555 a_expr substr_from substr_for
9557 $$ = list_make3($1, $2, $3);
9559 | a_expr substr_for substr_from
9561 /* not legal per SQL99, but might as well allow it */
9562 $$ = list_make3($1, $3, $2);
9564 | a_expr substr_from
9566 $$ = list_make2($1, $2);
9568 | a_expr substr_for
9571 * Since there are no cases where this syntax allows
9572 * a textual FOR value, we forcibly cast the argument
9573 * to int4. The possible matches in pg_proc are
9574 * substring(text,int4) and substring(text,text),
9575 * and we don't want the parser to choose the latter,
9576 * which it is likely to do if the second argument
9577 * is unknown or doesn't have an implicit cast to int4.
9579 $$ = list_make3($1, makeIntConst(1, -1),
9580 makeTypeCast($2,
9581 SystemTypeName("int4"), -1));
9583 | expr_list
9585 $$ = $1;
9587 | /*EMPTY*/
9588 { $$ = NIL; }
9591 substr_from:
9592 FROM a_expr { $$ = $2; }
9595 substr_for: FOR a_expr { $$ = $2; }
9598 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
9599 | FROM expr_list { $$ = $2; }
9600 | expr_list { $$ = $1; }
9603 in_expr: select_with_parens
9605 SubLink *n = makeNode(SubLink);
9606 n->subselect = $1;
9607 /* other fields will be filled later */
9608 $$ = (Node *)n;
9610 | '(' expr_list ')' { $$ = (Node *)$2; }
9614 * Define SQL92-style case clause.
9615 * - Full specification
9616 * CASE WHEN a = b THEN c ... ELSE d END
9617 * - Implicit argument
9618 * CASE a WHEN b THEN c ... ELSE d END
9620 case_expr: CASE case_arg when_clause_list case_default END_P
9622 CaseExpr *c = makeNode(CaseExpr);
9623 c->casetype = InvalidOid; /* not analyzed yet */
9624 c->arg = (Expr *) $2;
9625 c->args = $3;
9626 c->defresult = (Expr *) $4;
9627 c->location = @1;
9628 $$ = (Node *)c;
9632 when_clause_list:
9633 /* There must be at least one */
9634 when_clause { $$ = list_make1($1); }
9635 | when_clause_list when_clause { $$ = lappend($1, $2); }
9638 when_clause:
9639 WHEN a_expr THEN a_expr
9641 CaseWhen *w = makeNode(CaseWhen);
9642 w->expr = (Expr *) $2;
9643 w->result = (Expr *) $4;
9644 w->location = @1;
9645 $$ = (Node *)w;
9649 case_default:
9650 ELSE a_expr { $$ = $2; }
9651 | /*EMPTY*/ { $$ = NULL; }
9654 case_arg: a_expr { $$ = $1; }
9655 | /*EMPTY*/ { $$ = NULL; }
9659 * columnref starts with relation_name not ColId, so that OLD and NEW
9660 * references can be accepted. Note that when there are more than two
9661 * dotted names, the first name is not actually a relation name...
9663 columnref: relation_name
9665 $$ = makeColumnRef($1, NIL, @1);
9667 | relation_name indirection
9669 $$ = makeColumnRef($1, $2, @1);
9673 indirection_el:
9674 '.' attr_name
9676 $$ = (Node *) makeString($2);
9678 | '.' '*'
9680 $$ = (Node *) makeNode(A_Star);
9682 | '[' a_expr ']'
9684 A_Indices *ai = makeNode(A_Indices);
9685 ai->lidx = NULL;
9686 ai->uidx = $2;
9687 $$ = (Node *) ai;
9689 | '[' a_expr ':' a_expr ']'
9691 A_Indices *ai = makeNode(A_Indices);
9692 ai->lidx = $2;
9693 ai->uidx = $4;
9694 $$ = (Node *) ai;
9698 indirection:
9699 indirection_el { $$ = list_make1($1); }
9700 | indirection indirection_el { $$ = lappend($1, $2); }
9703 opt_indirection:
9704 /*EMPTY*/ { $$ = NIL; }
9705 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9708 opt_asymmetric: ASYMMETRIC
9709 | /*EMPTY*/
9713 * The SQL spec defines "contextually typed value expressions" and
9714 * "contextually typed row value constructors", which for our purposes
9715 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9716 * the top level.
9719 ctext_expr:
9720 a_expr { $$ = (Node *) $1; }
9721 | DEFAULT
9723 SetToDefault *n = makeNode(SetToDefault);
9724 n->location = @1;
9725 $$ = (Node *) n;
9729 ctext_expr_list:
9730 ctext_expr { $$ = list_make1($1); }
9731 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9735 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9736 * making VALUES a fully reserved word, which will probably break more apps
9737 * than allowing the noise-word is worth.
9739 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9743 /*****************************************************************************
9745 * target list for SELECT
9747 *****************************************************************************/
9749 target_list:
9750 target_el { $$ = list_make1($1); }
9751 | target_list ',' target_el { $$ = lappend($1, $3); }
9754 target_el: a_expr AS ColLabel
9756 $$ = makeNode(ResTarget);
9757 $$->name = $3;
9758 $$->indirection = NIL;
9759 $$->val = (Node *)$1;
9760 $$->location = @1;
9763 * We support omitting AS only for column labels that aren't
9764 * any known keyword. There is an ambiguity against postfix
9765 * operators: is "a ! b" an infix expression, or a postfix
9766 * expression and a column label? We prefer to resolve this
9767 * as an infix expression, which we accomplish by assigning
9768 * IDENT a precedence higher than POSTFIXOP.
9770 | a_expr IDENT
9772 $$ = makeNode(ResTarget);
9773 $$->name = $2;
9774 $$->indirection = NIL;
9775 $$->val = (Node *)$1;
9776 $$->location = @1;
9778 | a_expr
9780 $$ = makeNode(ResTarget);
9781 $$->name = NULL;
9782 $$->indirection = NIL;
9783 $$->val = (Node *)$1;
9784 $$->location = @1;
9786 | '*'
9788 ColumnRef *n = makeNode(ColumnRef);
9789 n->fields = list_make1(makeNode(A_Star));
9790 n->location = @1;
9792 $$ = makeNode(ResTarget);
9793 $$->name = NULL;
9794 $$->indirection = NIL;
9795 $$->val = (Node *)n;
9796 $$->location = @1;
9801 /*****************************************************************************
9803 * Names and constants
9805 *****************************************************************************/
9807 relation_name:
9808 SpecialRuleRelation { $$ = $1; }
9809 | ColId { $$ = $1; }
9812 qualified_name_list:
9813 qualified_name { $$ = list_make1($1); }
9814 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9818 * The production for a qualified relation name has to exactly match the
9819 * production for a qualified func_name, because in a FROM clause we cannot
9820 * tell which we are parsing until we see what comes after it ('(' for a
9821 * func_name, something else for a relation). Therefore we allow 'indirection'
9822 * which may contain subscripts, and reject that case in the C code.
9824 qualified_name:
9825 relation_name
9827 $$ = makeNode(RangeVar);
9828 $$->catalogname = NULL;
9829 $$->schemaname = NULL;
9830 $$->relname = $1;
9831 $$->location = @1;
9833 | relation_name indirection
9835 check_qualified_name($2);
9836 $$ = makeNode(RangeVar);
9837 switch (list_length($2))
9839 case 1:
9840 $$->catalogname = NULL;
9841 $$->schemaname = $1;
9842 $$->relname = strVal(linitial($2));
9843 break;
9844 case 2:
9845 $$->catalogname = $1;
9846 $$->schemaname = strVal(linitial($2));
9847 $$->relname = strVal(lsecond($2));
9848 break;
9849 default:
9850 ereport(ERROR,
9851 (errcode(ERRCODE_SYNTAX_ERROR),
9852 errmsg("improper qualified name (too many dotted names): %s",
9853 NameListToString(lcons(makeString($1), $2))),
9854 scanner_errposition(@1)));
9855 break;
9857 $$->location = @1;
9861 name_list: name
9862 { $$ = list_make1(makeString($1)); }
9863 | name_list ',' name
9864 { $$ = lappend($1, makeString($3)); }
9868 name: ColId { $$ = $1; };
9870 database_name:
9871 ColId { $$ = $1; };
9873 access_method:
9874 ColId { $$ = $1; };
9876 attr_name: ColLabel { $$ = $1; };
9878 index_name: ColId { $$ = $1; };
9880 file_name: Sconst { $$ = $1; };
9883 * The production for a qualified func_name has to exactly match the
9884 * production for a qualified columnref, because we cannot tell which we
9885 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9886 * anything else for a columnref). Therefore we allow 'indirection' which
9887 * may contain subscripts, and reject that case in the C code. (If we
9888 * ever implement SQL99-like methods, such syntax may actually become legal!)
9890 func_name: type_function_name
9891 { $$ = list_make1(makeString($1)); }
9892 | relation_name indirection
9893 { $$ = check_func_name(lcons(makeString($1), $2)); }
9898 * Constants
9900 AexprConst: Iconst
9902 $$ = makeIntConst($1, @1);
9904 | FCONST
9906 $$ = makeFloatConst($1, @1);
9908 | Sconst
9910 $$ = makeStringConst($1, @1);
9912 | BCONST
9914 $$ = makeBitStringConst($1, @1);
9916 | XCONST
9918 /* This is a bit constant per SQL99:
9919 * Without Feature F511, "BIT data type",
9920 * a <general literal> shall not be a
9921 * <bit string literal> or a <hex string literal>.
9923 $$ = makeBitStringConst($1, @1);
9925 | func_name Sconst
9927 /* generic type 'literal' syntax */
9928 TypeName *t = makeTypeNameFromNameList($1);
9929 t->location = @1;
9930 $$ = makeStringConstCast($2, @2, t);
9932 | func_name '(' expr_list ')' Sconst
9934 /* generic syntax with a type modifier */
9935 TypeName *t = makeTypeNameFromNameList($1);
9936 t->typmods = $3;
9937 t->location = @1;
9938 $$ = makeStringConstCast($5, @5, t);
9940 | ConstTypename Sconst
9942 $$ = makeStringConstCast($2, @2, $1);
9944 | ConstInterval Sconst opt_interval
9946 TypeName *t = $1;
9947 t->typmods = $3;
9948 $$ = makeStringConstCast($2, @2, t);
9950 | ConstInterval '(' Iconst ')' Sconst opt_interval
9952 TypeName *t = $1;
9953 if ($6 != NIL)
9955 if (list_length($6) != 1)
9956 ereport(ERROR,
9957 (errcode(ERRCODE_SYNTAX_ERROR),
9958 errmsg("interval precision specified twice"),
9959 scanner_errposition(@1)));
9960 t->typmods = lappend($6, makeIntConst($3, @3));
9962 else
9963 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9964 makeIntConst($3, @3));
9965 $$ = makeStringConstCast($5, @5, t);
9967 | TRUE_P
9969 $$ = makeBoolAConst(TRUE, @1);
9971 | FALSE_P
9973 $$ = makeBoolAConst(FALSE, @1);
9975 | NULL_P
9977 $$ = makeNullAConst(@1);
9981 Iconst: ICONST { $$ = $1; };
9982 Sconst: SCONST { $$ = $1; };
9983 RoleId: ColId { $$ = $1; };
9985 SignedIconst: Iconst { $$ = $1; }
9986 | '+' Iconst { $$ = + $2; }
9987 | '-' Iconst { $$ = - $2; }
9991 * Name classification hierarchy.
9993 * IDENT is the lexeme returned by the lexer for identifiers that match
9994 * no known keyword. In most cases, we can accept certain keywords as
9995 * names, not only IDENTs. We prefer to accept as many such keywords
9996 * as possible to minimize the impact of "reserved words" on programmers.
9997 * So, we divide names into several possible classes. The classification
9998 * is chosen in part to make keywords acceptable as names wherever possible.
10001 /* Column identifier --- names that can be column, table, etc names.
10003 ColId: IDENT { $$ = $1; }
10004 | unreserved_keyword { $$ = pstrdup($1); }
10005 | col_name_keyword { $$ = pstrdup($1); }
10008 /* Type/function identifier --- names that can be type or function names.
10010 type_function_name: IDENT { $$ = $1; }
10011 | unreserved_keyword { $$ = pstrdup($1); }
10012 | type_func_name_keyword { $$ = pstrdup($1); }
10015 /* Column label --- allowed labels in "AS" clauses.
10016 * This presently includes *all* Postgres keywords.
10018 ColLabel: IDENT { $$ = $1; }
10019 | unreserved_keyword { $$ = pstrdup($1); }
10020 | col_name_keyword { $$ = pstrdup($1); }
10021 | type_func_name_keyword { $$ = pstrdup($1); }
10022 | reserved_keyword { $$ = pstrdup($1); }
10027 * Keyword category lists. Generally, every keyword present in
10028 * the Postgres grammar should appear in exactly one of these lists.
10030 * Put a new keyword into the first list that it can go into without causing
10031 * shift or reduce conflicts. The earlier lists define "less reserved"
10032 * categories of keywords.
10034 * Make sure that each keyword's category in keywords.c matches where
10035 * it is listed here. (Someday we may be able to generate these lists and
10036 * keywords.c's table from a common master list.)
10039 /* "Unreserved" keywords --- available for use as any kind of name.
10041 unreserved_keyword:
10042 ABORT_P
10043 | ABSOLUTE_P
10044 | ACCESS
10045 | ACTION
10046 | ADD_P
10047 | ADMIN
10048 | AFTER
10049 | AGGREGATE
10050 | ALSO
10051 | ALTER
10052 | ALWAYS
10053 | ASSERTION
10054 | ASSIGNMENT
10055 | AT
10056 | BACKWARD
10057 | BEFORE
10058 | BEGIN_P
10059 | BY
10060 | CACHE
10061 | CALLED
10062 | CASCADE
10063 | CASCADED
10064 | CATALOG_P
10065 | CHAIN
10066 | CHARACTERISTICS
10067 | CHECKPOINT
10068 | CLASS
10069 | CLOSE
10070 | CLUSTER
10071 | COMMENT
10072 | COMMIT
10073 | COMMITTED
10074 | CONCURRENTLY
10075 | CONFIGURATION
10076 | CONNECTION
10077 | CONSTRAINTS
10078 | CONTENT_P
10079 | CONTINUE_P
10080 | CONVERSION_P
10081 | COPY
10082 | COST
10083 | CREATEDB
10084 | CREATEROLE
10085 | CREATEUSER
10086 | CSV
10087 | CTYPE
10088 | CURRENT_P
10089 | CURSOR
10090 | CYCLE
10091 | DATA_P
10092 | DATABASE
10093 | DAY_P
10094 | DEALLOCATE
10095 | DECLARE
10096 | DEFAULTS
10097 | DEFERRED
10098 | DEFINER
10099 | DELETE_P
10100 | DELIMITER
10101 | DELIMITERS
10102 | DICTIONARY
10103 | DISABLE_P
10104 | DISCARD
10105 | DOCUMENT_P
10106 | DOMAIN_P
10107 | DOUBLE_P
10108 | DROP
10109 | EACH
10110 | ENABLE_P
10111 | ENCODING
10112 | ENCRYPTED
10113 | ENUM_P
10114 | ESCAPE
10115 | EXCLUDING
10116 | EXCLUSIVE
10117 | EXECUTE
10118 | EXPLAIN
10119 | EXTERNAL
10120 | FAMILY
10121 | FIRST_P
10122 | FOLLOWING
10123 | FORCE
10124 | FORWARD
10125 | FUNCTION
10126 | GLOBAL
10127 | GRANTED
10128 | HANDLER
10129 | HEADER_P
10130 | HOLD
10131 | HOUR_P
10132 | IDENTITY_P
10133 | IF_P
10134 | IMMEDIATE
10135 | IMMUTABLE
10136 | IMPLICIT_P
10137 | INCLUDING
10138 | INCREMENT
10139 | INDEX
10140 | INDEXES
10141 | INHERIT
10142 | INHERITS
10143 | INPUT_P
10144 | INSENSITIVE
10145 | INSERT
10146 | INSTEAD
10147 | INVOKER
10148 | ISOLATION
10149 | KEY
10150 | LIBRARY
10151 | LANCOMPILER
10152 | LANGUAGE
10153 | LARGE_P
10154 | LAST_P
10155 | LEVEL
10156 | LISTEN
10157 | LOAD
10158 | LOCAL
10159 | LOCATION
10160 | LOCK_P
10161 | LOGIN_P
10162 | MAPPING
10163 | MATCH
10164 | MAXVALUE
10165 | MINUTE_P
10166 | MINVALUE
10167 | MODE
10168 | MONTH_P
10169 | MOVE
10170 | NAME_P
10171 | NAMES
10172 | NEXT
10173 | NO
10174 | NOCREATEDB
10175 | NOCREATEROLE
10176 | NOCREATEUSER
10177 | NOINHERIT
10178 | NOLOGIN_P
10179 | NOSUPERUSER
10180 | NOTHING
10181 | NOTIFY
10182 | NOWAIT
10183 | NULLS_P
10184 | OBJECT_P
10185 | OF
10186 | OIDS
10187 | OPERATOR
10188 | OPTION
10189 | OPTIONS
10190 | OWNED
10191 | OWNER
10192 | PARSER
10193 | PARTIAL
10194 | PARTITION
10195 | PASSWORD
10196 | PLANS
10197 | PRECEDING
10198 | PREPARE
10199 | PREPARED
10200 | PRESERVE
10201 | PRIOR
10202 | PRIVILEGES
10203 | PROCEDURAL
10204 | PROCEDURE
10205 | QUOTE
10206 | RANGE
10207 | READ
10208 | REASSIGN
10209 | RECHECK
10210 | RECURSIVE
10211 | REINDEX
10212 | RELATIVE_P
10213 | RELEASE
10214 | RENAME
10215 | REPEATABLE
10216 | REPLACE
10217 | REPLICA
10218 | RESET
10219 | RESTART
10220 | RESTRICT
10221 | RETURNS
10222 | REVOKE
10223 | ROLE
10224 | ROLLBACK
10225 | ROWS
10226 | RULE
10227 | SAVEPOINT
10228 | SERVER
10229 | SCHEMA
10230 | SCROLL
10231 | SEARCH
10232 | SECOND_P
10233 | SECURITY
10234 | SEQUENCE
10235 | SERIALIZABLE
10236 | SESSION
10237 | SET
10238 | SHARE
10239 | SHOW
10240 | SIMPLE
10241 | STABLE
10242 | STANDALONE_P
10243 | START
10244 | STATEMENT
10245 | STATISTICS
10246 | STDIN
10247 | STDOUT
10248 | STORAGE
10249 | STRICT_P
10250 | STRIP_P
10251 | SUPERUSER_P
10252 | SYSID
10253 | SYSTEM_P
10254 | TABLESPACE
10255 | TEMP
10256 | TEMPLATE
10257 | TEMPORARY
10258 | TEXT_P
10259 | TRANSACTION
10260 | TRIGGER
10261 | TRUNCATE
10262 | TRUSTED
10263 | TYPE_P
10264 | UNBOUNDED
10265 | UNCOMMITTED
10266 | UNENCRYPTED
10267 | UNKNOWN
10268 | UNLISTEN
10269 | UNTIL
10270 | UPDATE
10271 | VACUUM
10272 | VALID
10273 | VALIDATOR
10274 | VALUE_P
10275 | VARYING
10276 | VERSION_P
10277 | VIEW
10278 | VOLATILE
10279 | WHITESPACE_P
10280 | WITHOUT
10281 | WORK
10282 | WRAPPER
10283 | WRITE
10284 | XML_P
10285 | YEAR_P
10286 | YES_P
10287 | ZONE
10290 /* Column identifier --- keywords that can be column, table, etc names.
10292 * Many of these keywords will in fact be recognized as type or function
10293 * names too; but they have special productions for the purpose, and so
10294 * can't be treated as "generic" type or function names.
10296 * The type names appearing here are not usable as function names
10297 * because they can be followed by '(' in typename productions, which
10298 * looks too much like a function call for an LR(1) parser.
10300 col_name_keyword:
10301 BIGINT
10302 | BIT
10303 | BOOLEAN_P
10304 | CHAR_P
10305 | CHARACTER
10306 | COALESCE
10307 | DEC
10308 | DECIMAL_P
10309 | EXISTS
10310 | EXTRACT
10311 | FLOAT_P
10312 | GREATEST
10313 | INOUT
10314 | INT_P
10315 | INTEGER
10316 | INTERVAL
10317 | LEAST
10318 | NATIONAL
10319 | NCHAR
10320 | NONE
10321 | NULLIF
10322 | NUMERIC
10323 | OUT_P
10324 | OVERLAY
10325 | POSITION
10326 | PRECISION
10327 | REAL
10328 | ROW
10329 | SETOF
10330 | SMALLINT
10331 | SUBSTRING
10332 | TIME
10333 | TIMESTAMP
10334 | TREAT
10335 | TRIM
10336 | VALUES
10337 | VARCHAR
10338 | XMLATTRIBUTES
10339 | XMLCONCAT
10340 | XMLELEMENT
10341 | XMLFOREST
10342 | XMLPARSE
10343 | XMLPI
10344 | XMLROOT
10345 | XMLSERIALIZE
10348 /* Type/function identifier --- keywords that can be type or function names.
10350 * Most of these are keywords that are used as operators in expressions;
10351 * in general such keywords can't be column names because they would be
10352 * ambiguous with variables, but they are unambiguous as function identifiers.
10354 * Do not include POSITION, SUBSTRING, etc here since they have explicit
10355 * productions in a_expr to support the goofy SQL9x argument syntax.
10356 * - thomas 2000-11-28
10358 type_func_name_keyword:
10359 AUTHORIZATION
10360 | BETWEEN
10361 | BINARY
10362 | CROSS
10363 | CURRENT_SCHEMA
10364 | FREEZE
10365 | FULL
10366 | ILIKE
10367 | INNER_P
10368 | IS
10369 | ISNULL
10370 | JOIN
10371 | LEFT
10372 | LIKE
10373 | NATURAL
10374 | NOTNULL
10375 | OUTER_P
10376 | OVER
10377 | OVERLAPS
10378 | RIGHT
10379 | SIMILAR
10380 | VERBOSE
10383 /* Reserved keyword --- these keywords are usable only as a ColLabel.
10385 * Keywords appear here if they could not be distinguished from variable,
10386 * type, or function names in some contexts. Don't put things here unless
10387 * forced to.
10389 reserved_keyword:
10391 | ANALYSE
10392 | ANALYZE
10393 | AND
10394 | ANY
10395 | ARRAY
10396 | AS
10397 | ASC
10398 | ASYMMETRIC
10399 | BOTH
10400 | CASE
10401 | CAST
10402 | CHECK
10403 | COLLATE
10404 | COLUMN
10405 | CONSTRAINT
10406 | CREATE
10407 | CURRENT_CATALOG
10408 | CURRENT_DATE
10409 | CURRENT_ROLE
10410 | CURRENT_TIME
10411 | CURRENT_TIMESTAMP
10412 | CURRENT_USER
10413 | DEFAULT
10414 | DEFERRABLE
10415 | DESC
10416 | DISTINCT
10417 | DO
10418 | ELSE
10419 | END_P
10420 | EXCEPT
10421 | FALSE_P
10422 | FETCH
10423 | FOR
10424 | FOREIGN
10425 | FROM
10426 | GRANT
10427 | GROUP_P
10428 | HAVING
10429 | IN_P
10430 | INITIALLY
10431 | INTERSECT
10432 | INTO
10433 | LEADING
10434 | LIMIT
10435 | LOCALTIME
10436 | LOCALTIMESTAMP
10437 | NEW
10438 | NOT
10439 | NULL_P
10440 | OFF
10441 | OFFSET
10442 | OLD
10443 | ON
10444 | ONLY
10445 | OR
10446 | ORDER
10447 | PLACING
10448 | PRIMARY
10449 | REFERENCES
10450 | RETURNING
10451 | SELECT
10452 | SESSION_USER
10453 | SOME
10454 | SYMMETRIC
10455 | TABLE
10456 | THEN
10457 | TO
10458 | TRAILING
10459 | TRUE_P
10460 | UNION
10461 | UNIQUE
10462 | USER
10463 | USING
10464 | VARIADIC
10465 | WHEN
10466 | WHERE
10467 | WINDOW
10468 | WITH
10472 SpecialRuleRelation:
10475 if (QueryIsRule)
10476 $$ = "*OLD*";
10477 else
10478 ereport(ERROR,
10479 (errcode(ERRCODE_SYNTAX_ERROR),
10480 errmsg("OLD used in query that is not in a rule"),
10481 scanner_errposition(@1)));
10483 | NEW
10485 if (QueryIsRule)
10486 $$ = "*NEW*";
10487 else
10488 ereport(ERROR,
10489 (errcode(ERRCODE_SYNTAX_ERROR),
10490 errmsg("NEW used in query that is not in a rule"),
10491 scanner_errposition(@1)));
10497 static Node *
10498 makeColumnRef(char *colname, List *indirection, int location)
10501 * Generate a ColumnRef node, with an A_Indirection node added if there
10502 * is any subscripting in the specified indirection list. However,
10503 * any field selection at the start of the indirection list must be
10504 * transposed into the "fields" part of the ColumnRef node.
10506 ColumnRef *c = makeNode(ColumnRef);
10507 int nfields = 0;
10508 ListCell *l;
10510 c->location = location;
10511 foreach(l, indirection)
10513 if (IsA(lfirst(l), A_Indices))
10515 A_Indirection *i = makeNode(A_Indirection);
10517 if (nfields == 0)
10519 /* easy case - all indirection goes to A_Indirection */
10520 c->fields = list_make1(makeString(colname));
10521 i->indirection = check_indirection(indirection);
10523 else
10525 /* got to split the list in two */
10526 i->indirection = check_indirection(list_copy_tail(indirection,
10527 nfields));
10528 indirection = list_truncate(indirection, nfields);
10529 c->fields = lcons(makeString(colname), indirection);
10531 i->arg = (Node *) c;
10532 return (Node *) i;
10534 else if (IsA(lfirst(l), A_Star))
10536 /* We only allow '*' at the end of a ColumnRef */
10537 if (lnext(l) != NULL)
10538 yyerror("improper use of \"*\"");
10540 nfields++;
10542 /* No subscripting, so all indirection gets added to field list */
10543 c->fields = lcons(makeString(colname), indirection);
10544 return (Node *) c;
10547 static Node *
10548 makeTypeCast(Node *arg, TypeName *typename, int location)
10550 TypeCast *n = makeNode(TypeCast);
10551 n->arg = arg;
10552 n->typename = typename;
10553 n->location = location;
10554 return (Node *) n;
10557 static Node *
10558 makeStringConst(char *str, int location)
10560 A_Const *n = makeNode(A_Const);
10562 n->val.type = T_String;
10563 n->val.val.str = str;
10564 n->location = location;
10566 return (Node *)n;
10569 static Node *
10570 makeStringConstCast(char *str, int location, TypeName *typename)
10572 Node *s = makeStringConst(str, location);
10574 return makeTypeCast(s, typename, -1);
10577 static Node *
10578 makeIntConst(int val, int location)
10580 A_Const *n = makeNode(A_Const);
10582 n->val.type = T_Integer;
10583 n->val.val.ival = val;
10584 n->location = location;
10586 return (Node *)n;
10589 static Node *
10590 makeFloatConst(char *str, int location)
10592 A_Const *n = makeNode(A_Const);
10594 n->val.type = T_Float;
10595 n->val.val.str = str;
10596 n->location = location;
10598 return (Node *)n;
10601 static Node *
10602 makeBitStringConst(char *str, int location)
10604 A_Const *n = makeNode(A_Const);
10606 n->val.type = T_BitString;
10607 n->val.val.str = str;
10608 n->location = location;
10610 return (Node *)n;
10613 static Node *
10614 makeNullAConst(int location)
10616 A_Const *n = makeNode(A_Const);
10618 n->val.type = T_Null;
10619 n->location = location;
10621 return (Node *)n;
10624 static Node *
10625 makeAConst(Value *v, int location)
10627 Node *n;
10629 switch (v->type)
10631 case T_Float:
10632 n = makeFloatConst(v->val.str, location);
10633 break;
10635 case T_Integer:
10636 n = makeIntConst(v->val.ival, location);
10637 break;
10639 case T_String:
10640 default:
10641 n = makeStringConst(v->val.str, location);
10642 break;
10645 return n;
10648 /* makeBoolAConst()
10649 * Create an A_Const string node and put it inside a boolean cast.
10651 static Node *
10652 makeBoolAConst(bool state, int location)
10654 A_Const *n = makeNode(A_Const);
10656 n->val.type = T_String;
10657 n->val.val.str = (state ? "t" : "f");
10658 n->location = location;
10660 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
10663 /* makeOverlaps()
10664 * Create and populate a FuncCall node to support the OVERLAPS operator.
10666 static FuncCall *
10667 makeOverlaps(List *largs, List *rargs, int location)
10669 FuncCall *n = makeNode(FuncCall);
10671 n->funcname = SystemFuncName("overlaps");
10672 if (list_length(largs) == 1)
10673 largs = lappend(largs, largs);
10674 else if (list_length(largs) != 2)
10675 ereport(ERROR,
10676 (errcode(ERRCODE_SYNTAX_ERROR),
10677 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10678 scanner_errposition(location)));
10679 if (list_length(rargs) == 1)
10680 rargs = lappend(rargs, rargs);
10681 else if (list_length(rargs) != 2)
10682 ereport(ERROR,
10683 (errcode(ERRCODE_SYNTAX_ERROR),
10684 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10685 scanner_errposition(location)));
10686 n->args = list_concat(largs, rargs);
10687 n->agg_star = FALSE;
10688 n->agg_distinct = FALSE;
10689 n->func_variadic = FALSE;
10690 n->over = NULL;
10691 n->location = location;
10692 return n;
10695 /* check_qualified_name --- check the result of qualified_name production
10697 * It's easiest to let the grammar production for qualified_name allow
10698 * subscripts and '*', which we then must reject here.
10700 static void
10701 check_qualified_name(List *names)
10703 ListCell *i;
10705 foreach(i, names)
10707 if (!IsA(lfirst(i), String))
10708 yyerror("syntax error");
10712 /* check_func_name --- check the result of func_name production
10714 * It's easiest to let the grammar production for func_name allow subscripts
10715 * and '*', which we then must reject here.
10717 static List *
10718 check_func_name(List *names)
10720 ListCell *i;
10722 foreach(i, names)
10724 if (!IsA(lfirst(i), String))
10725 yyerror("syntax error");
10727 return names;
10730 /* check_indirection --- check the result of indirection production
10732 * We only allow '*' at the end of the list, but it's hard to enforce that
10733 * in the grammar, so do it here.
10735 static List *
10736 check_indirection(List *indirection)
10738 ListCell *l;
10740 foreach(l, indirection)
10742 if (IsA(lfirst(l), A_Star))
10744 if (lnext(l) != NULL)
10745 yyerror("improper use of \"*\"");
10748 return indirection;
10751 /* extractArgTypes()
10752 * Given a list of FunctionParameter nodes, extract a list of just the
10753 * argument types (TypeNames) for input parameters only. This is what
10754 * is needed to look up an existing function, which is what is wanted by
10755 * the productions that use this call.
10757 static List *
10758 extractArgTypes(List *parameters)
10760 List *result = NIL;
10761 ListCell *i;
10763 foreach(i, parameters)
10765 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10767 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10768 result = lappend(result, p->argType);
10770 return result;
10773 /* findLeftmostSelect()
10774 * Find the leftmost component SelectStmt in a set-operation parsetree.
10776 static SelectStmt *
10777 findLeftmostSelect(SelectStmt *node)
10779 while (node && node->op != SETOP_NONE)
10780 node = node->larg;
10781 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10782 return node;
10785 /* insertSelectOptions()
10786 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10788 * This routine is just to avoid duplicating code in SelectStmt productions.
10790 static void
10791 insertSelectOptions(SelectStmt *stmt,
10792 List *sortClause, List *lockingClause,
10793 Node *limitOffset, Node *limitCount,
10794 WithClause *withClause)
10796 Assert(IsA(stmt, SelectStmt));
10799 * Tests here are to reject constructs like
10800 * (SELECT foo ORDER BY bar) ORDER BY baz
10802 if (sortClause)
10804 if (stmt->sortClause)
10805 ereport(ERROR,
10806 (errcode(ERRCODE_SYNTAX_ERROR),
10807 errmsg("multiple ORDER BY clauses not allowed"),
10808 scanner_errposition(exprLocation((Node *) sortClause))));
10809 stmt->sortClause = sortClause;
10811 /* We can handle multiple locking clauses, though */
10812 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10813 if (limitOffset)
10815 if (stmt->limitOffset)
10816 ereport(ERROR,
10817 (errcode(ERRCODE_SYNTAX_ERROR),
10818 errmsg("multiple OFFSET clauses not allowed"),
10819 scanner_errposition(exprLocation(limitOffset))));
10820 stmt->limitOffset = limitOffset;
10822 if (limitCount)
10824 if (stmt->limitCount)
10825 ereport(ERROR,
10826 (errcode(ERRCODE_SYNTAX_ERROR),
10827 errmsg("multiple LIMIT clauses not allowed"),
10828 scanner_errposition(exprLocation(limitCount))));
10829 stmt->limitCount = limitCount;
10831 if (withClause)
10833 if (stmt->withClause)
10834 ereport(ERROR,
10835 (errcode(ERRCODE_SYNTAX_ERROR),
10836 errmsg("multiple WITH clauses not allowed"),
10837 scanner_errposition(exprLocation((Node *) withClause))));
10838 stmt->withClause = withClause;
10842 static Node *
10843 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10845 SelectStmt *n = makeNode(SelectStmt);
10847 n->op = op;
10848 n->all = all;
10849 n->larg = (SelectStmt *) larg;
10850 n->rarg = (SelectStmt *) rarg;
10851 return (Node *) n;
10854 /* SystemFuncName()
10855 * Build a properly-qualified reference to a built-in function.
10857 List *
10858 SystemFuncName(char *name)
10860 return list_make2(makeString("pg_catalog"), makeString(name));
10863 /* SystemTypeName()
10864 * Build a properly-qualified reference to a built-in type.
10866 * typmod is defaulted, but may be changed afterwards by caller.
10867 * Likewise for the location.
10869 TypeName *
10870 SystemTypeName(char *name)
10872 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10873 makeString(name)));
10876 /* doNegate()
10877 * Handle negation of a numeric constant.
10879 * Formerly, we did this here because the optimizer couldn't cope with
10880 * indexquals that looked like "var = -4" --- it wants "var = const"
10881 * and a unary minus operator applied to a constant didn't qualify.
10882 * As of Postgres 7.0, that problem doesn't exist anymore because there
10883 * is a constant-subexpression simplifier in the optimizer. However,
10884 * there's still a good reason for doing this here, which is that we can
10885 * postpone committing to a particular internal representation for simple
10886 * negative constants. It's better to leave "-123.456" in string form
10887 * until we know what the desired type is.
10889 static Node *
10890 doNegate(Node *n, int location)
10892 if (IsA(n, A_Const))
10894 A_Const *con = (A_Const *)n;
10896 /* report the constant's location as that of the '-' sign */
10897 con->location = location;
10899 if (con->val.type == T_Integer)
10901 con->val.val.ival = -con->val.val.ival;
10902 return n;
10904 if (con->val.type == T_Float)
10906 doNegateFloat(&con->val);
10907 return n;
10911 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10914 static void
10915 doNegateFloat(Value *v)
10917 char *oldval = v->val.str;
10919 Assert(IsA(v, Float));
10920 if (*oldval == '+')
10921 oldval++;
10922 if (*oldval == '-')
10923 v->val.str = oldval+1; /* just strip the '-' */
10924 else
10926 char *newval = (char *) palloc(strlen(oldval) + 2);
10928 *newval = '-';
10929 strcpy(newval+1, oldval);
10930 v->val.str = newval;
10934 static Node *
10935 makeAArrayExpr(List *elements, int location)
10937 A_ArrayExpr *n = makeNode(A_ArrayExpr);
10939 n->elements = elements;
10940 n->location = location;
10941 return (Node *) n;
10944 static Node *
10945 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
10946 int location)
10948 XmlExpr *x = makeNode(XmlExpr);
10950 x->op = op;
10951 x->name = name;
10953 * named_args is a list of ResTarget; it'll be split apart into separate
10954 * expression and name lists in transformXmlExpr().
10956 x->named_args = named_args;
10957 x->arg_names = NIL;
10958 x->args = args;
10959 /* xmloption, if relevant, must be filled in by caller */
10960 /* type and typmod will be filled in during parse analysis */
10961 x->location = location;
10962 return (Node *) x;
10965 /* parser_init()
10966 * Initialize to parse one query string
10968 void
10969 parser_init(void)
10971 QueryIsRule = FALSE;
10975 * Merge the input and output parameters of a table function.
10977 static List *
10978 mergeTableFuncParameters(List *func_args, List *columns)
10980 ListCell *lc;
10982 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
10983 foreach(lc, func_args)
10985 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
10987 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
10988 ereport(ERROR,
10989 (errcode(ERRCODE_SYNTAX_ERROR),
10990 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
10993 return list_concat(func_args, columns);
10997 * Determine return type of a TABLE function. A single result column
10998 * returns setof that column's type; otherwise return setof record.
11000 static TypeName *
11001 TableFuncTypeName(List *columns)
11003 TypeName *result;
11005 if (list_length(columns) == 1)
11007 FunctionParameter *p = (FunctionParameter *) linitial(columns);
11009 result = (TypeName *) copyObject(p->argType);
11011 else
11012 result = SystemTypeName("record");
11014 result->setof = true;
11016 return result;
11020 * Must undefine base_yylex before including scan.c, since we want it
11021 * to create the function base_yylex not filtered_base_yylex.
11023 #undef base_yylex
11025 #include "scan.c"