Code review for function default parameters patch. Fix numerous problems as
[PostgreSQL.git] / src / backend / parser / gram.y
blob607b14800c0cf3cdbee2e44b244adf418b89393c
1 %{
3 /*#define YYDEBUG 1*/
4 /*-------------------------------------------------------------------------
6 * gram.y
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
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 SortBy *sortby;
160 JoinExpr *jexpr;
161 IndexElem *ielem;
162 Alias *alias;
163 RangeVar *range;
164 IntoClause *into;
165 WithClause *with;
166 A_Indices *aind;
167 ResTarget *target;
168 PrivTarget *privtarget;
170 InsertStmt *istmt;
171 VariableSetStmt *vsetstmt;
174 %type <node> stmt schema_stmt
175 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
176 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
177 AlterUserStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
178 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
179 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
180 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
181 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
182 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
183 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
184 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
185 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
186 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
187 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
188 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
189 LockStmt NotifyStmt ExplainableStmt PreparableStmt
190 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
191 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
192 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
193 SelectStmt TransactionStmt TruncateStmt
194 UnlistenStmt UpdateStmt VacuumStmt
195 VariableResetStmt VariableSetStmt VariableShowStmt
196 ViewStmt CheckPointStmt CreateConversionStmt
197 DeallocateStmt PrepareStmt ExecuteStmt
198 DropOwnedStmt ReassignOwnedStmt
199 AlterTSConfigurationStmt AlterTSDictionaryStmt
201 %type <node> select_no_parens select_with_parens select_clause
202 simple_select values_clause
204 %type <node> alter_column_default opclass_item opclass_drop alter_using
205 %type <ival> add_drop opt_asc_desc opt_nulls_order
207 %type <node> alter_table_cmd
208 %type <list> alter_table_cmds
210 %type <dbehavior> opt_drop_behavior
212 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
213 transaction_mode_list
214 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
215 transaction_mode_item
217 %type <ival> opt_lock lock_type cast_context
218 %type <boolean> opt_force opt_or_replace
219 opt_grant_grant_option opt_grant_admin_option
220 opt_nowait opt_if_exists opt_with_data
222 %type <list> OptRoleList
223 %type <defelt> OptRoleElem
225 %type <str> OptSchemaName
226 %type <list> OptSchemaEltList
228 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
229 %type <str> opt_lancompiler
231 %type <str> TriggerEvents
232 %type <value> TriggerFuncArg
234 %type <str> relation_name copy_file_name
235 database_name access_method_clause access_method attr_name
236 index_name name file_name cluster_index_specification
238 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
239 opt_class opt_validator
241 %type <range> qualified_name OptConstrFromTable
243 %type <str> all_Op MathOp SpecialRuleRelation
245 %type <str> iso_level opt_encoding
246 %type <node> grantee
247 %type <list> grantee_list
248 %type <str> privilege
249 %type <list> privileges privilege_list
250 %type <privtarget> privilege_target
251 %type <funwithargs> function_with_argtypes
252 %type <list> function_with_argtypes_list
253 %type <chr> TriggerOneEvent
255 %type <list> stmtblock stmtmulti
256 OptTableElementList TableElementList OptInherit definition
257 OptWith opt_distinct opt_definition func_args func_args_list
258 func_args_with_defaults func_args_with_defaults_list
259 func_as createfunc_opt_list alterfunc_opt_list
260 aggr_args old_aggr_definition old_aggr_list
261 oper_argtypes RuleActionList RuleActionMulti
262 opt_column_list columnList opt_name_list
263 sort_clause opt_sort_clause sortby_list index_params
264 name_list from_clause from_list opt_array_bounds
265 qualified_name_list any_name any_name_list
266 any_operator expr_list attrs
267 target_list insert_column_list set_target_list
268 set_clause_list set_clause multiple_set_clause
269 ctext_expr_list ctext_row def_list indirection opt_indirection
270 group_clause TriggerFuncArgs select_limit
271 opt_select_limit opclass_item_list opclass_drop_list
272 opt_opfamily transaction_mode_list_or_empty
273 TableFuncElementList opt_type_modifiers
274 prep_type_clause
275 execute_param_clause using_clause returning_clause
276 enum_val_list table_func_column_list
278 %type <range> OptTempTableName
279 %type <into> into_clause create_as_target
281 %type <defelt> createfunc_opt_item common_func_opt_item
282 %type <fun_param> func_arg func_arg_with_default table_func_column
283 %type <fun_param_mode> arg_class
284 %type <typnam> func_return func_type
286 %type <boolean> TriggerForType OptTemp
287 %type <oncommit> OnCommitOption
289 %type <node> for_locking_item
290 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
291 %type <list> locked_rels_list
292 %type <boolean> opt_all
294 %type <node> join_outer join_qual
295 %type <jtype> join_type
297 %type <list> extract_list overlay_list position_list
298 %type <list> substr_list trim_list
299 %type <list> opt_interval interval_second
300 %type <node> overlay_placing substr_from substr_for
302 %type <boolean> opt_instead opt_analyze
303 %type <boolean> index_opt_unique opt_verbose opt_full
304 %type <boolean> opt_freeze opt_default opt_recheck
305 %type <defelt> opt_binary opt_oids copy_delimiter
307 %type <boolean> copy_from
309 %type <ival> opt_column event cursor_options opt_hold opt_set_data
310 %type <objtype> reindex_type drop_type comment_type
312 %type <node> fetch_direction select_limit_value select_offset_value
313 select_offset_value2 opt_select_fetch_first_value
314 %type <ival> row_or_rows first_or_next
316 %type <list> OptSeqOptList SeqOptList
317 %type <defelt> SeqOptElem
319 %type <istmt> insert_rest
321 %type <vsetstmt> set_rest SetResetClause
323 %type <node> TableElement ConstraintElem TableFuncElement
324 %type <node> columnDef
325 %type <defelt> def_elem old_aggr_elem
326 %type <node> def_arg columnElem where_clause where_or_current_clause
327 a_expr b_expr c_expr func_expr AexprConst indirection_el
328 columnref in_expr having_clause func_table array_expr
329 %type <list> row type_list array_expr_list
330 %type <node> case_expr case_arg when_clause case_default
331 %type <list> when_clause_list
332 %type <ival> sub_type
333 %type <list> OptCreateAs CreateAsList
334 %type <node> CreateAsElement ctext_expr
335 %type <value> NumericOnly
336 %type <alias> alias_clause
337 %type <sortby> sortby
338 %type <ielem> index_elem
339 %type <node> table_ref
340 %type <jexpr> joined_table
341 %type <range> relation_expr
342 %type <range> relation_expr_opt_alias
343 %type <target> target_el single_set_clause set_target insert_column_item
345 %type <typnam> Typename SimpleTypename ConstTypename
346 GenericType Numeric opt_float
347 Character ConstCharacter
348 CharacterWithLength CharacterWithoutLength
349 ConstDatetime ConstInterval
350 Bit ConstBit BitWithLength BitWithoutLength
351 %type <str> character
352 %type <str> extract_arg
353 %type <str> opt_charset
354 %type <boolean> opt_varying opt_timezone
356 %type <ival> Iconst SignedIconst
357 %type <str> Sconst comment_text
358 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
359 %type <list> var_list
360 %type <str> ColId ColLabel var_name type_function_name param_name
361 %type <node> var_value zone_value
363 %type <keyword> unreserved_keyword type_func_name_keyword
364 %type <keyword> col_name_keyword reserved_keyword
366 %type <node> TableConstraint TableLikeClause
367 %type <list> TableLikeOptionList
368 %type <ival> TableLikeOption
369 %type <list> ColQualList
370 %type <node> ColConstraint ColConstraintElem ConstraintAttr
371 %type <ival> key_actions key_delete key_match key_update key_action
372 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
373 ConstraintTimeSpec
375 %type <list> constraints_set_list
376 %type <boolean> constraints_set_mode
377 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
378 %type <list> opt_check_option
380 %type <target> xml_attribute_el
381 %type <list> xml_attribute_list xml_attributes
382 %type <node> xml_root_version opt_xml_root_standalone
383 %type <ival> document_or_content
384 %type <boolean> xml_whitespace_option
386 %type <node> common_table_expr
387 %type <with> with_clause
388 %type <list> cte_list
392 * If you make any token changes, update the keyword table in
393 * parser/keywords.c and add new keywords to the appropriate one of
394 * the reserved-or-not-so-reserved keyword lists, below; search
395 * this file for "Name classification hierarchy".
398 /* ordinary key words in alphabetical order */
399 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
400 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
401 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
403 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
404 BOOLEAN_P BOTH BY
406 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
407 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
408 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
409 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
410 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
411 CREATEROLE CREATEUSER CROSS CSV CTYPE CURRENT_P
412 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
413 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
415 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
416 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
417 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
419 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
420 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
422 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
423 FREEZE FROM FULL FUNCTION
425 GLOBAL GRANT GRANTED GREATEST GROUP_P
427 HANDLER HAVING HEADER_P HOLD HOUR_P
429 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
430 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
431 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
432 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
434 JOIN
438 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
439 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
440 LOCK_P LOGIN_P
442 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
444 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
445 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
446 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
448 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
449 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
451 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
452 PRECISION PRESERVE PREPARE PREPARED PRIMARY
453 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
455 QUOTE
457 READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX RELATIVE_P RELEASE
458 RENAME REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS
459 REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
461 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
462 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
463 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
464 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
465 SYMMETRIC SYSID SYSTEM_P
467 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
468 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
469 TRUNCATE TRUSTED TYPE_P
471 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
472 UPDATE USER USING
474 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
475 VERBOSE VERSION_P VIEW VOLATILE
477 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRITE
479 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
480 XMLPI XMLROOT XMLSERIALIZE
482 YEAR_P YES_P
484 ZONE
486 /* The grammar thinks these are keywords, but they are not in the keywords.c
487 * list and so can never be entered directly. The filter in parser.c
488 * creates these tokens when required.
490 %token NULLS_FIRST NULLS_LAST WITH_TIME
492 /* Special token types, not actually keywords - see the "lex" file */
493 %token <str> IDENT FCONST SCONST BCONST XCONST Op
494 %token <ival> ICONST PARAM
496 /* precedence: lowest to highest */
497 %nonassoc SET /* see relation_expr_opt_alias */
498 %left UNION EXCEPT
499 %left INTERSECT
500 %left OR
501 %left AND
502 %right NOT
503 %right '='
504 %nonassoc '<' '>'
505 %nonassoc LIKE ILIKE SIMILAR
506 %nonassoc ESCAPE
507 %nonassoc OVERLAPS
508 %nonassoc BETWEEN
509 %nonassoc IN_P
510 %left POSTFIXOP /* dummy for postfix Op rules */
511 %nonassoc IDENT /* to support target_el without AS */
512 %left Op OPERATOR /* multi-character ops and user-defined operators */
513 %nonassoc NOTNULL
514 %nonassoc ISNULL
515 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
516 %left '+' '-'
517 %left '*' '/' '%'
518 %left '^'
519 /* Unary Operators */
520 %left AT ZONE /* sets precedence for AT TIME ZONE */
521 %right UMINUS
522 %left '[' ']'
523 %left '(' ')'
524 %left TYPECAST
525 %left '.'
527 * These might seem to be low-precedence, but actually they are not part
528 * of the arithmetic hierarchy at all in their use as JOIN operators.
529 * We make them high-precedence to support their use as function names.
530 * They wouldn't be given a precedence at all, were it not that we need
531 * left-associativity among the JOIN rules themselves.
533 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
534 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
535 %right PRESERVE STRIP_P
539 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
540 * psql already handles such cases, but other interfaces don't.
541 * bjm 1999/10/05
543 stmtblock: stmtmulti { parsetree = $1; }
546 /* the thrashing around here is to discard "empty" statements... */
547 stmtmulti: stmtmulti ';' stmt
548 { if ($3 != NULL)
549 $$ = lappend($1, $3);
550 else
551 $$ = $1;
553 | stmt
554 { if ($1 != NULL)
555 $$ = list_make1($1);
556 else
557 $$ = NIL;
561 stmt :
562 AlterDatabaseStmt
563 | AlterDatabaseSetStmt
564 | AlterDomainStmt
565 | AlterFunctionStmt
566 | AlterGroupStmt
567 | AlterObjectSchemaStmt
568 | AlterOwnerStmt
569 | AlterSeqStmt
570 | AlterTableStmt
571 | AlterRoleSetStmt
572 | AlterRoleStmt
573 | AlterTSConfigurationStmt
574 | AlterTSDictionaryStmt
575 | AlterUserSetStmt
576 | AlterUserStmt
577 | AnalyzeStmt
578 | CheckPointStmt
579 | ClosePortalStmt
580 | ClusterStmt
581 | CommentStmt
582 | ConstraintsSetStmt
583 | CopyStmt
584 | CreateAsStmt
585 | CreateAssertStmt
586 | CreateCastStmt
587 | CreateConversionStmt
588 | CreateDomainStmt
589 | CreateFunctionStmt
590 | CreateGroupStmt
591 | CreateOpClassStmt
592 | CreateOpFamilyStmt
593 | AlterOpFamilyStmt
594 | CreatePLangStmt
595 | CreateSchemaStmt
596 | CreateSeqStmt
597 | CreateStmt
598 | CreateTableSpaceStmt
599 | CreateTrigStmt
600 | CreateRoleStmt
601 | CreateUserStmt
602 | CreatedbStmt
603 | DeallocateStmt
604 | DeclareCursorStmt
605 | DefineStmt
606 | DeleteStmt
607 | DiscardStmt
608 | DropAssertStmt
609 | DropCastStmt
610 | DropGroupStmt
611 | DropOpClassStmt
612 | DropOpFamilyStmt
613 | DropOwnedStmt
614 | DropPLangStmt
615 | DropRuleStmt
616 | DropStmt
617 | DropTableSpaceStmt
618 | DropTrigStmt
619 | DropRoleStmt
620 | DropUserStmt
621 | DropdbStmt
622 | ExecuteStmt
623 | ExplainStmt
624 | FetchStmt
625 | GrantStmt
626 | GrantRoleStmt
627 | IndexStmt
628 | InsertStmt
629 | ListenStmt
630 | LoadStmt
631 | LockStmt
632 | NotifyStmt
633 | PrepareStmt
634 | ReassignOwnedStmt
635 | ReindexStmt
636 | RemoveAggrStmt
637 | RemoveFuncStmt
638 | RemoveOperStmt
639 | RenameStmt
640 | RevokeStmt
641 | RevokeRoleStmt
642 | RuleStmt
643 | SelectStmt
644 | TransactionStmt
645 | TruncateStmt
646 | UnlistenStmt
647 | UpdateStmt
648 | VacuumStmt
649 | VariableResetStmt
650 | VariableSetStmt
651 | VariableShowStmt
652 | ViewStmt
653 | /*EMPTY*/
654 { $$ = NULL; }
657 /*****************************************************************************
659 * Create a new Postgres DBMS role
661 *****************************************************************************/
663 CreateRoleStmt:
664 CREATE ROLE RoleId opt_with OptRoleList
666 CreateRoleStmt *n = makeNode(CreateRoleStmt);
667 n->stmt_type = ROLESTMT_ROLE;
668 n->role = $3;
669 n->options = $5;
670 $$ = (Node *)n;
675 opt_with: WITH {}
676 | /*EMPTY*/ {}
680 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
681 * for backwards compatibility). Note: the only option required by SQL99
682 * is "WITH ADMIN name".
684 OptRoleList:
685 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
686 | /* EMPTY */ { $$ = NIL; }
689 OptRoleElem:
690 PASSWORD Sconst
692 $$ = makeDefElem("password",
693 (Node *)makeString($2));
695 | PASSWORD NULL_P
697 $$ = makeDefElem("password", NULL);
699 | ENCRYPTED PASSWORD Sconst
701 $$ = makeDefElem("encryptedPassword",
702 (Node *)makeString($3));
704 | UNENCRYPTED PASSWORD Sconst
706 $$ = makeDefElem("unencryptedPassword",
707 (Node *)makeString($3));
709 | SUPERUSER_P
711 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
713 | NOSUPERUSER
715 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
717 | INHERIT
719 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
721 | NOINHERIT
723 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
725 | CREATEDB
727 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
729 | NOCREATEDB
731 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
733 | CREATEROLE
735 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
737 | NOCREATEROLE
739 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
741 | CREATEUSER
743 /* For backwards compatibility, synonym for SUPERUSER */
744 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
746 | NOCREATEUSER
748 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
750 | LOGIN_P
752 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
754 | NOLOGIN_P
756 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
758 | CONNECTION LIMIT SignedIconst
760 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
762 | VALID UNTIL Sconst
764 $$ = makeDefElem("validUntil", (Node *)makeString($3));
766 /* Supported but not documented for roles, for use by ALTER GROUP. */
767 | USER name_list
769 $$ = makeDefElem("rolemembers", (Node *)$2);
771 /* The following are not supported by ALTER ROLE/USER/GROUP */
772 | SYSID Iconst
774 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
776 | ADMIN name_list
778 $$ = makeDefElem("adminmembers", (Node *)$2);
780 | ROLE name_list
782 $$ = makeDefElem("rolemembers", (Node *)$2);
784 | IN_P ROLE name_list
786 $$ = makeDefElem("addroleto", (Node *)$3);
788 | IN_P GROUP_P name_list
790 $$ = makeDefElem("addroleto", (Node *)$3);
795 /*****************************************************************************
797 * Create a new Postgres DBMS user (role with implied login ability)
799 *****************************************************************************/
801 CreateUserStmt:
802 CREATE USER RoleId opt_with OptRoleList
804 CreateRoleStmt *n = makeNode(CreateRoleStmt);
805 n->stmt_type = ROLESTMT_USER;
806 n->role = $3;
807 n->options = $5;
808 $$ = (Node *)n;
813 /*****************************************************************************
815 * Alter a postgresql DBMS role
817 *****************************************************************************/
819 AlterRoleStmt:
820 ALTER ROLE RoleId opt_with OptRoleList
822 AlterRoleStmt *n = makeNode(AlterRoleStmt);
823 n->role = $3;
824 n->action = +1; /* add, if there are members */
825 n->options = $5;
826 $$ = (Node *)n;
830 AlterRoleSetStmt:
831 ALTER ROLE RoleId SetResetClause
833 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
834 n->role = $3;
835 n->setstmt = $4;
836 $$ = (Node *)n;
841 /*****************************************************************************
843 * Alter a postgresql DBMS user
845 *****************************************************************************/
847 AlterUserStmt:
848 ALTER USER RoleId opt_with OptRoleList
850 AlterRoleStmt *n = makeNode(AlterRoleStmt);
851 n->role = $3;
852 n->action = +1; /* add, if there are members */
853 n->options = $5;
854 $$ = (Node *)n;
859 AlterUserSetStmt:
860 ALTER USER RoleId SetResetClause
862 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
863 n->role = $3;
864 n->setstmt = $4;
865 $$ = (Node *)n;
870 /*****************************************************************************
872 * Drop a postgresql DBMS role
874 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
875 * might own objects in multiple databases, there is presently no way to
876 * implement either cascading or restricting. Caveat DBA.
877 *****************************************************************************/
879 DropRoleStmt:
880 DROP ROLE name_list
882 DropRoleStmt *n = makeNode(DropRoleStmt);
883 n->missing_ok = FALSE;
884 n->roles = $3;
885 $$ = (Node *)n;
887 | DROP ROLE IF_P EXISTS name_list
889 DropRoleStmt *n = makeNode(DropRoleStmt);
890 n->missing_ok = TRUE;
891 n->roles = $5;
892 $$ = (Node *)n;
896 /*****************************************************************************
898 * Drop a postgresql DBMS user
900 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
901 * might own objects in multiple databases, there is presently no way to
902 * implement either cascading or restricting. Caveat DBA.
903 *****************************************************************************/
905 DropUserStmt:
906 DROP USER name_list
908 DropRoleStmt *n = makeNode(DropRoleStmt);
909 n->missing_ok = FALSE;
910 n->roles = $3;
911 $$ = (Node *)n;
913 | DROP USER IF_P EXISTS name_list
915 DropRoleStmt *n = makeNode(DropRoleStmt);
916 n->roles = $5;
917 n->missing_ok = TRUE;
918 $$ = (Node *)n;
923 /*****************************************************************************
925 * Create a postgresql group (role without login ability)
927 *****************************************************************************/
929 CreateGroupStmt:
930 CREATE GROUP_P RoleId opt_with OptRoleList
932 CreateRoleStmt *n = makeNode(CreateRoleStmt);
933 n->stmt_type = ROLESTMT_GROUP;
934 n->role = $3;
935 n->options = $5;
936 $$ = (Node *)n;
941 /*****************************************************************************
943 * Alter a postgresql group
945 *****************************************************************************/
947 AlterGroupStmt:
948 ALTER GROUP_P RoleId add_drop USER name_list
950 AlterRoleStmt *n = makeNode(AlterRoleStmt);
951 n->role = $3;
952 n->action = $4;
953 n->options = list_make1(makeDefElem("rolemembers",
954 (Node *)$6));
955 $$ = (Node *)n;
959 add_drop: ADD_P { $$ = +1; }
960 | DROP { $$ = -1; }
964 /*****************************************************************************
966 * Drop a postgresql group
968 * XXX see above notes about cascading DROP USER; groups have same problem.
969 *****************************************************************************/
971 DropGroupStmt:
972 DROP GROUP_P name_list
974 DropRoleStmt *n = makeNode(DropRoleStmt);
975 n->missing_ok = FALSE;
976 n->roles = $3;
977 $$ = (Node *)n;
979 | DROP GROUP_P IF_P EXISTS name_list
981 DropRoleStmt *n = makeNode(DropRoleStmt);
982 n->missing_ok = TRUE;
983 n->roles = $5;
984 $$ = (Node *)n;
989 /*****************************************************************************
991 * Manipulate a schema
993 *****************************************************************************/
995 CreateSchemaStmt:
996 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
998 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
999 /* One can omit the schema name or the authorization id. */
1000 if ($3 != NULL)
1001 n->schemaname = $3;
1002 else
1003 n->schemaname = $5;
1004 n->authid = $5;
1005 n->schemaElts = $6;
1006 $$ = (Node *)n;
1008 | CREATE SCHEMA ColId OptSchemaEltList
1010 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1011 /* ...but not both */
1012 n->schemaname = $3;
1013 n->authid = NULL;
1014 n->schemaElts = $4;
1015 $$ = (Node *)n;
1019 OptSchemaName:
1020 ColId { $$ = $1; }
1021 | /* EMPTY */ { $$ = NULL; }
1024 OptSchemaEltList:
1025 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1026 | /* EMPTY */ { $$ = NIL; }
1030 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1031 * statement (in addition to by themselves).
1033 schema_stmt:
1034 CreateStmt
1035 | IndexStmt
1036 | CreateSeqStmt
1037 | CreateTrigStmt
1038 | GrantStmt
1039 | ViewStmt
1043 /*****************************************************************************
1045 * Set PG internal variable
1046 * SET name TO 'var_value'
1047 * Include SQL92 syntax (thomas 1997-10-22):
1048 * SET TIME ZONE 'var_value'
1050 *****************************************************************************/
1052 VariableSetStmt:
1053 SET set_rest
1055 VariableSetStmt *n = $2;
1056 n->is_local = false;
1057 $$ = (Node *) n;
1059 | SET LOCAL set_rest
1061 VariableSetStmt *n = $3;
1062 n->is_local = true;
1063 $$ = (Node *) n;
1065 | SET SESSION set_rest
1067 VariableSetStmt *n = $3;
1068 n->is_local = false;
1069 $$ = (Node *) n;
1073 set_rest: /* Generic SET syntaxes: */
1074 var_name TO var_list
1076 VariableSetStmt *n = makeNode(VariableSetStmt);
1077 n->kind = VAR_SET_VALUE;
1078 n->name = $1;
1079 n->args = $3;
1080 $$ = n;
1082 | var_name '=' var_list
1084 VariableSetStmt *n = makeNode(VariableSetStmt);
1085 n->kind = VAR_SET_VALUE;
1086 n->name = $1;
1087 n->args = $3;
1088 $$ = n;
1090 | var_name TO DEFAULT
1092 VariableSetStmt *n = makeNode(VariableSetStmt);
1093 n->kind = VAR_SET_DEFAULT;
1094 n->name = $1;
1095 $$ = n;
1097 | var_name '=' DEFAULT
1099 VariableSetStmt *n = makeNode(VariableSetStmt);
1100 n->kind = VAR_SET_DEFAULT;
1101 n->name = $1;
1102 $$ = n;
1104 | var_name FROM CURRENT_P
1106 VariableSetStmt *n = makeNode(VariableSetStmt);
1107 n->kind = VAR_SET_CURRENT;
1108 n->name = $1;
1109 $$ = n;
1111 /* Special syntaxes mandated by SQL standard: */
1112 | TIME ZONE zone_value
1114 VariableSetStmt *n = makeNode(VariableSetStmt);
1115 n->kind = VAR_SET_VALUE;
1116 n->name = "timezone";
1117 if ($3 != NULL)
1118 n->args = list_make1($3);
1119 else
1120 n->kind = VAR_SET_DEFAULT;
1121 $$ = n;
1123 | TRANSACTION transaction_mode_list
1125 VariableSetStmt *n = makeNode(VariableSetStmt);
1126 n->kind = VAR_SET_MULTI;
1127 n->name = "TRANSACTION";
1128 n->args = $2;
1129 $$ = n;
1131 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1133 VariableSetStmt *n = makeNode(VariableSetStmt);
1134 n->kind = VAR_SET_MULTI;
1135 n->name = "SESSION CHARACTERISTICS";
1136 n->args = $5;
1137 $$ = n;
1139 | CATALOG_P Sconst
1141 ereport(ERROR,
1142 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1143 errmsg("current database cannot be changed"),
1144 scanner_errposition(@2)));
1145 $$ = NULL; /*not reached*/
1147 | SCHEMA Sconst
1149 VariableSetStmt *n = makeNode(VariableSetStmt);
1150 n->kind = VAR_SET_VALUE;
1151 n->name = "search_path";
1152 n->args = list_make1(makeStringConst($2, @2));
1153 $$ = n;
1155 | NAMES opt_encoding
1157 VariableSetStmt *n = makeNode(VariableSetStmt);
1158 n->kind = VAR_SET_VALUE;
1159 n->name = "client_encoding";
1160 if ($2 != NULL)
1161 n->args = list_make1(makeStringConst($2, @2));
1162 else
1163 n->kind = VAR_SET_DEFAULT;
1164 $$ = n;
1166 | ROLE ColId_or_Sconst
1168 VariableSetStmt *n = makeNode(VariableSetStmt);
1169 n->kind = VAR_SET_VALUE;
1170 n->name = "role";
1171 n->args = list_make1(makeStringConst($2, @2));
1172 $$ = n;
1174 | SESSION AUTHORIZATION ColId_or_Sconst
1176 VariableSetStmt *n = makeNode(VariableSetStmt);
1177 n->kind = VAR_SET_VALUE;
1178 n->name = "session_authorization";
1179 n->args = list_make1(makeStringConst($3, @3));
1180 $$ = n;
1182 | SESSION AUTHORIZATION DEFAULT
1184 VariableSetStmt *n = makeNode(VariableSetStmt);
1185 n->kind = VAR_SET_DEFAULT;
1186 n->name = "session_authorization";
1187 $$ = n;
1189 | XML_P OPTION document_or_content
1191 VariableSetStmt *n = makeNode(VariableSetStmt);
1192 n->kind = VAR_SET_VALUE;
1193 n->name = "xmloption";
1194 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1195 $$ = n;
1199 var_name: ColId { $$ = $1; }
1200 | var_name '.' ColId
1202 $$ = palloc(strlen($1) + strlen($3) + 2);
1203 sprintf($$, "%s.%s", $1, $3);
1207 var_list: var_value { $$ = list_make1($1); }
1208 | var_list ',' var_value { $$ = lappend($1, $3); }
1211 var_value: opt_boolean
1212 { $$ = makeStringConst($1, @1); }
1213 | ColId_or_Sconst
1214 { $$ = makeStringConst($1, @1); }
1215 | NumericOnly
1216 { $$ = makeAConst($1, @1); }
1219 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1220 | READ COMMITTED { $$ = "read committed"; }
1221 | REPEATABLE READ { $$ = "repeatable read"; }
1222 | SERIALIZABLE { $$ = "serializable"; }
1225 opt_boolean:
1226 TRUE_P { $$ = "true"; }
1227 | FALSE_P { $$ = "false"; }
1228 | ON { $$ = "on"; }
1229 | OFF { $$ = "off"; }
1232 /* Timezone values can be:
1233 * - a string such as 'pst8pdt'
1234 * - an identifier such as "pst8pdt"
1235 * - an integer or floating point number
1236 * - a time interval per SQL99
1237 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1238 * so use IDENT and reject anything which is a reserved word.
1240 zone_value:
1241 Sconst
1243 $$ = makeStringConst($1, @1);
1245 | IDENT
1247 $$ = makeStringConst($1, @1);
1249 | ConstInterval Sconst opt_interval
1251 TypeName *t = $1;
1252 if ($3 != NIL)
1254 A_Const *n = (A_Const *) linitial($3);
1255 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1256 ereport(ERROR,
1257 (errcode(ERRCODE_SYNTAX_ERROR),
1258 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1259 scanner_errposition(@3)));
1261 t->typmods = $3;
1262 $$ = makeStringConstCast($2, @2, t);
1264 | ConstInterval '(' Iconst ')' Sconst opt_interval
1266 TypeName *t = $1;
1267 if ($6 != NIL)
1269 A_Const *n = (A_Const *) linitial($6);
1270 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1271 ereport(ERROR,
1272 (errcode(ERRCODE_SYNTAX_ERROR),
1273 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1274 scanner_errposition(@6)));
1275 if (list_length($6) != 1)
1276 ereport(ERROR,
1277 (errcode(ERRCODE_SYNTAX_ERROR),
1278 errmsg("interval precision specified twice"),
1279 scanner_errposition(@1)));
1280 t->typmods = lappend($6, makeIntConst($3, @3));
1282 else
1283 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1284 makeIntConst($3, @3));
1285 $$ = makeStringConstCast($5, @5, t);
1287 | NumericOnly { $$ = makeAConst($1, @1); }
1288 | DEFAULT { $$ = NULL; }
1289 | LOCAL { $$ = NULL; }
1292 opt_encoding:
1293 Sconst { $$ = $1; }
1294 | DEFAULT { $$ = NULL; }
1295 | /*EMPTY*/ { $$ = NULL; }
1298 ColId_or_Sconst:
1299 ColId { $$ = $1; }
1300 | Sconst { $$ = $1; }
1303 VariableResetStmt:
1304 RESET var_name
1306 VariableSetStmt *n = makeNode(VariableSetStmt);
1307 n->kind = VAR_RESET;
1308 n->name = $2;
1309 $$ = (Node *) n;
1311 | RESET TIME ZONE
1313 VariableSetStmt *n = makeNode(VariableSetStmt);
1314 n->kind = VAR_RESET;
1315 n->name = "timezone";
1316 $$ = (Node *) n;
1318 | RESET TRANSACTION ISOLATION LEVEL
1320 VariableSetStmt *n = makeNode(VariableSetStmt);
1321 n->kind = VAR_RESET;
1322 n->name = "transaction_isolation";
1323 $$ = (Node *) n;
1325 | RESET SESSION AUTHORIZATION
1327 VariableSetStmt *n = makeNode(VariableSetStmt);
1328 n->kind = VAR_RESET;
1329 n->name = "session_authorization";
1330 $$ = (Node *) n;
1332 | RESET ALL
1334 VariableSetStmt *n = makeNode(VariableSetStmt);
1335 n->kind = VAR_RESET_ALL;
1336 $$ = (Node *) n;
1340 /* SetResetClause allows SET or RESET without LOCAL */
1341 SetResetClause:
1342 SET set_rest { $$ = $2; }
1343 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1347 VariableShowStmt:
1348 SHOW var_name
1350 VariableShowStmt *n = makeNode(VariableShowStmt);
1351 n->name = $2;
1352 $$ = (Node *) n;
1354 | SHOW TIME ZONE
1356 VariableShowStmt *n = makeNode(VariableShowStmt);
1357 n->name = "timezone";
1358 $$ = (Node *) n;
1360 | SHOW TRANSACTION ISOLATION LEVEL
1362 VariableShowStmt *n = makeNode(VariableShowStmt);
1363 n->name = "transaction_isolation";
1364 $$ = (Node *) n;
1366 | SHOW SESSION AUTHORIZATION
1368 VariableShowStmt *n = makeNode(VariableShowStmt);
1369 n->name = "session_authorization";
1370 $$ = (Node *) n;
1372 | SHOW ALL
1374 VariableShowStmt *n = makeNode(VariableShowStmt);
1375 n->name = "all";
1376 $$ = (Node *) n;
1381 ConstraintsSetStmt:
1382 SET CONSTRAINTS constraints_set_list constraints_set_mode
1384 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1385 n->constraints = $3;
1386 n->deferred = $4;
1387 $$ = (Node *) n;
1391 constraints_set_list:
1392 ALL { $$ = NIL; }
1393 | qualified_name_list { $$ = $1; }
1396 constraints_set_mode:
1397 DEFERRED { $$ = TRUE; }
1398 | IMMEDIATE { $$ = FALSE; }
1403 * Checkpoint statement
1405 CheckPointStmt:
1406 CHECKPOINT
1408 CheckPointStmt *n = makeNode(CheckPointStmt);
1409 $$ = (Node *)n;
1414 /*****************************************************************************
1416 * DISCARD { ALL | TEMP | PLANS }
1418 *****************************************************************************/
1420 DiscardStmt:
1421 DISCARD ALL
1423 DiscardStmt *n = makeNode(DiscardStmt);
1424 n->target = DISCARD_ALL;
1425 $$ = (Node *) n;
1427 | DISCARD TEMP
1429 DiscardStmt *n = makeNode(DiscardStmt);
1430 n->target = DISCARD_TEMP;
1431 $$ = (Node *) n;
1433 | DISCARD TEMPORARY
1435 DiscardStmt *n = makeNode(DiscardStmt);
1436 n->target = DISCARD_TEMP;
1437 $$ = (Node *) n;
1439 | DISCARD PLANS
1441 DiscardStmt *n = makeNode(DiscardStmt);
1442 n->target = DISCARD_PLANS;
1443 $$ = (Node *) n;
1448 /*****************************************************************************
1450 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1452 * Note: we accept all subcommands for each of the four variants, and sort
1453 * out what's really legal at execution time.
1454 *****************************************************************************/
1456 AlterTableStmt:
1457 ALTER TABLE relation_expr alter_table_cmds
1459 AlterTableStmt *n = makeNode(AlterTableStmt);
1460 n->relation = $3;
1461 n->cmds = $4;
1462 n->relkind = OBJECT_TABLE;
1463 $$ = (Node *)n;
1465 | ALTER INDEX relation_expr alter_table_cmds
1467 AlterTableStmt *n = makeNode(AlterTableStmt);
1468 n->relation = $3;
1469 n->cmds = $4;
1470 n->relkind = OBJECT_INDEX;
1471 $$ = (Node *)n;
1473 | ALTER SEQUENCE relation_expr alter_table_cmds
1475 AlterTableStmt *n = makeNode(AlterTableStmt);
1476 n->relation = $3;
1477 n->cmds = $4;
1478 n->relkind = OBJECT_SEQUENCE;
1479 $$ = (Node *)n;
1481 | ALTER VIEW relation_expr alter_table_cmds
1483 AlterTableStmt *n = makeNode(AlterTableStmt);
1484 n->relation = $3;
1485 n->cmds = $4;
1486 n->relkind = OBJECT_VIEW;
1487 $$ = (Node *)n;
1491 alter_table_cmds:
1492 alter_table_cmd { $$ = list_make1($1); }
1493 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1496 alter_table_cmd:
1497 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1498 ADD_P opt_column columnDef
1500 AlterTableCmd *n = makeNode(AlterTableCmd);
1501 n->subtype = AT_AddColumn;
1502 n->def = $3;
1503 $$ = (Node *)n;
1505 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1506 | ALTER opt_column ColId alter_column_default
1508 AlterTableCmd *n = makeNode(AlterTableCmd);
1509 n->subtype = AT_ColumnDefault;
1510 n->name = $3;
1511 n->def = $4;
1512 $$ = (Node *)n;
1514 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1515 | ALTER opt_column ColId DROP NOT NULL_P
1517 AlterTableCmd *n = makeNode(AlterTableCmd);
1518 n->subtype = AT_DropNotNull;
1519 n->name = $3;
1520 $$ = (Node *)n;
1522 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1523 | ALTER opt_column ColId SET NOT NULL_P
1525 AlterTableCmd *n = makeNode(AlterTableCmd);
1526 n->subtype = AT_SetNotNull;
1527 n->name = $3;
1528 $$ = (Node *)n;
1530 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <SignedIconst> */
1531 | ALTER opt_column ColId SET STATISTICS SignedIconst
1533 AlterTableCmd *n = makeNode(AlterTableCmd);
1534 n->subtype = AT_SetStatistics;
1535 n->name = $3;
1536 n->def = (Node *) makeInteger($6);
1537 $$ = (Node *)n;
1539 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1540 | ALTER opt_column ColId SET STORAGE ColId
1542 AlterTableCmd *n = makeNode(AlterTableCmd);
1543 n->subtype = AT_SetStorage;
1544 n->name = $3;
1545 n->def = (Node *) makeString($6);
1546 $$ = (Node *)n;
1548 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1549 | DROP opt_column ColId opt_drop_behavior
1551 AlterTableCmd *n = makeNode(AlterTableCmd);
1552 n->subtype = AT_DropColumn;
1553 n->name = $3;
1554 n->behavior = $4;
1555 $$ = (Node *)n;
1558 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1559 * [ USING <expression> ]
1561 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1563 AlterTableCmd *n = makeNode(AlterTableCmd);
1564 n->subtype = AT_AlterColumnType;
1565 n->name = $3;
1566 n->def = (Node *) $6;
1567 n->transform = $7;
1568 $$ = (Node *)n;
1570 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1571 | ADD_P TableConstraint
1573 AlterTableCmd *n = makeNode(AlterTableCmd);
1574 n->subtype = AT_AddConstraint;
1575 n->def = $2;
1576 $$ = (Node *)n;
1578 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1579 | DROP CONSTRAINT name opt_drop_behavior
1581 AlterTableCmd *n = makeNode(AlterTableCmd);
1582 n->subtype = AT_DropConstraint;
1583 n->name = $3;
1584 n->behavior = $4;
1585 $$ = (Node *)n;
1587 /* ALTER TABLE <name> SET WITHOUT OIDS */
1588 | SET WITHOUT OIDS
1590 AlterTableCmd *n = makeNode(AlterTableCmd);
1591 n->subtype = AT_DropOids;
1592 $$ = (Node *)n;
1594 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1595 | CLUSTER ON name
1597 AlterTableCmd *n = makeNode(AlterTableCmd);
1598 n->subtype = AT_ClusterOn;
1599 n->name = $3;
1600 $$ = (Node *)n;
1602 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1603 | SET WITHOUT CLUSTER
1605 AlterTableCmd *n = makeNode(AlterTableCmd);
1606 n->subtype = AT_DropCluster;
1607 n->name = NULL;
1608 $$ = (Node *)n;
1610 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1611 | ENABLE_P TRIGGER name
1613 AlterTableCmd *n = makeNode(AlterTableCmd);
1614 n->subtype = AT_EnableTrig;
1615 n->name = $3;
1616 $$ = (Node *)n;
1618 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1619 | ENABLE_P ALWAYS TRIGGER name
1621 AlterTableCmd *n = makeNode(AlterTableCmd);
1622 n->subtype = AT_EnableAlwaysTrig;
1623 n->name = $4;
1624 $$ = (Node *)n;
1626 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1627 | ENABLE_P REPLICA TRIGGER name
1629 AlterTableCmd *n = makeNode(AlterTableCmd);
1630 n->subtype = AT_EnableReplicaTrig;
1631 n->name = $4;
1632 $$ = (Node *)n;
1634 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1635 | ENABLE_P TRIGGER ALL
1637 AlterTableCmd *n = makeNode(AlterTableCmd);
1638 n->subtype = AT_EnableTrigAll;
1639 $$ = (Node *)n;
1641 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1642 | ENABLE_P TRIGGER USER
1644 AlterTableCmd *n = makeNode(AlterTableCmd);
1645 n->subtype = AT_EnableTrigUser;
1646 $$ = (Node *)n;
1648 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1649 | DISABLE_P TRIGGER name
1651 AlterTableCmd *n = makeNode(AlterTableCmd);
1652 n->subtype = AT_DisableTrig;
1653 n->name = $3;
1654 $$ = (Node *)n;
1656 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1657 | DISABLE_P TRIGGER ALL
1659 AlterTableCmd *n = makeNode(AlterTableCmd);
1660 n->subtype = AT_DisableTrigAll;
1661 $$ = (Node *)n;
1663 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1664 | DISABLE_P TRIGGER USER
1666 AlterTableCmd *n = makeNode(AlterTableCmd);
1667 n->subtype = AT_DisableTrigUser;
1668 $$ = (Node *)n;
1670 /* ALTER TABLE <name> ENABLE RULE <rule> */
1671 | ENABLE_P RULE name
1673 AlterTableCmd *n = makeNode(AlterTableCmd);
1674 n->subtype = AT_EnableRule;
1675 n->name = $3;
1676 $$ = (Node *)n;
1678 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1679 | ENABLE_P ALWAYS RULE name
1681 AlterTableCmd *n = makeNode(AlterTableCmd);
1682 n->subtype = AT_EnableAlwaysRule;
1683 n->name = $4;
1684 $$ = (Node *)n;
1686 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1687 | ENABLE_P REPLICA RULE name
1689 AlterTableCmd *n = makeNode(AlterTableCmd);
1690 n->subtype = AT_EnableReplicaRule;
1691 n->name = $4;
1692 $$ = (Node *)n;
1694 /* ALTER TABLE <name> DISABLE RULE <rule> */
1695 | DISABLE_P RULE name
1697 AlterTableCmd *n = makeNode(AlterTableCmd);
1698 n->subtype = AT_DisableRule;
1699 n->name = $3;
1700 $$ = (Node *)n;
1702 /* ALTER TABLE <name> INHERIT <parent> */
1703 | INHERIT qualified_name
1705 AlterTableCmd *n = makeNode(AlterTableCmd);
1706 n->subtype = AT_AddInherit;
1707 n->def = (Node *) $2;
1708 $$ = (Node *)n;
1710 /* ALTER TABLE <name> NO INHERIT <parent> */
1711 | NO INHERIT qualified_name
1713 AlterTableCmd *n = makeNode(AlterTableCmd);
1714 n->subtype = AT_DropInherit;
1715 n->def = (Node *) $3;
1716 $$ = (Node *)n;
1718 /* ALTER TABLE <name> OWNER TO RoleId */
1719 | OWNER TO RoleId
1721 AlterTableCmd *n = makeNode(AlterTableCmd);
1722 n->subtype = AT_ChangeOwner;
1723 n->name = $3;
1724 $$ = (Node *)n;
1726 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1727 | SET TABLESPACE name
1729 AlterTableCmd *n = makeNode(AlterTableCmd);
1730 n->subtype = AT_SetTableSpace;
1731 n->name = $3;
1732 $$ = (Node *)n;
1734 /* ALTER TABLE <name> SET (...) */
1735 | SET definition
1737 AlterTableCmd *n = makeNode(AlterTableCmd);
1738 n->subtype = AT_SetRelOptions;
1739 n->def = (Node *)$2;
1740 $$ = (Node *)n;
1742 /* ALTER TABLE <name> RESET (...) */
1743 | RESET definition
1745 AlterTableCmd *n = makeNode(AlterTableCmd);
1746 n->subtype = AT_ResetRelOptions;
1747 n->def = (Node *)$2;
1748 $$ = (Node *)n;
1752 alter_column_default:
1753 SET DEFAULT a_expr { $$ = $3; }
1754 | DROP DEFAULT { $$ = NULL; }
1757 opt_drop_behavior:
1758 CASCADE { $$ = DROP_CASCADE; }
1759 | RESTRICT { $$ = DROP_RESTRICT; }
1760 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1763 alter_using:
1764 USING a_expr { $$ = $2; }
1765 | /* EMPTY */ { $$ = NULL; }
1770 /*****************************************************************************
1772 * QUERY :
1773 * close <portalname>
1775 *****************************************************************************/
1777 ClosePortalStmt:
1778 CLOSE name
1780 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1781 n->portalname = $2;
1782 $$ = (Node *)n;
1784 | CLOSE ALL
1786 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1787 n->portalname = NULL;
1788 $$ = (Node *)n;
1793 /*****************************************************************************
1795 * QUERY :
1796 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1798 * BINARY, OIDS, and DELIMITERS kept in old locations
1799 * for backward compatibility. 2002-06-18
1801 * COPY ( SELECT ... ) TO file [WITH options]
1802 * This form doesn't have the backwards-compatible option
1803 * syntax.
1805 *****************************************************************************/
1807 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1808 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1810 CopyStmt *n = makeNode(CopyStmt);
1811 n->relation = $3;
1812 n->query = NULL;
1813 n->attlist = $4;
1814 n->is_from = $6;
1815 n->filename = $7;
1817 n->options = NIL;
1818 /* Concatenate user-supplied flags */
1819 if ($2)
1820 n->options = lappend(n->options, $2);
1821 if ($5)
1822 n->options = lappend(n->options, $5);
1823 if ($8)
1824 n->options = lappend(n->options, $8);
1825 if ($10)
1826 n->options = list_concat(n->options, $10);
1827 $$ = (Node *)n;
1829 | COPY select_with_parens TO copy_file_name opt_with
1830 copy_opt_list
1832 CopyStmt *n = makeNode(CopyStmt);
1833 n->relation = NULL;
1834 n->query = $2;
1835 n->attlist = NIL;
1836 n->is_from = false;
1837 n->filename = $4;
1838 n->options = $6;
1839 $$ = (Node *)n;
1843 copy_from:
1844 FROM { $$ = TRUE; }
1845 | TO { $$ = FALSE; }
1849 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1850 * used depends on the direction. (It really doesn't make sense to copy from
1851 * stdout. We silently correct the "typo".) - AY 9/94
1853 copy_file_name:
1854 Sconst { $$ = $1; }
1855 | STDIN { $$ = NULL; }
1856 | STDOUT { $$ = NULL; }
1861 copy_opt_list:
1862 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1863 | /* EMPTY */ { $$ = NIL; }
1867 copy_opt_item:
1868 BINARY
1870 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1872 | OIDS
1874 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1876 | DELIMITER opt_as Sconst
1878 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1880 | NULL_P opt_as Sconst
1882 $$ = makeDefElem("null", (Node *)makeString($3));
1884 | CSV
1886 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1888 | HEADER_P
1890 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1892 | QUOTE opt_as Sconst
1894 $$ = makeDefElem("quote", (Node *)makeString($3));
1896 | ESCAPE opt_as Sconst
1898 $$ = makeDefElem("escape", (Node *)makeString($3));
1900 | FORCE QUOTE columnList
1902 $$ = makeDefElem("force_quote", (Node *)$3);
1904 | FORCE NOT NULL_P columnList
1906 $$ = makeDefElem("force_notnull", (Node *)$4);
1910 /* The following exist for backward compatibility */
1912 opt_binary:
1913 BINARY
1915 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1917 | /*EMPTY*/ { $$ = NULL; }
1920 opt_oids:
1921 WITH OIDS
1923 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1925 | /*EMPTY*/ { $$ = NULL; }
1928 copy_delimiter:
1929 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1930 opt_using DELIMITERS Sconst
1932 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1934 | /*EMPTY*/ { $$ = NULL; }
1937 opt_using:
1938 USING {}
1939 | /*EMPTY*/ {}
1943 /*****************************************************************************
1945 * QUERY :
1946 * CREATE TABLE relname
1948 *****************************************************************************/
1950 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1951 OptInherit OptWith OnCommitOption OptTableSpace
1953 CreateStmt *n = makeNode(CreateStmt);
1954 $4->istemp = $2;
1955 n->relation = $4;
1956 n->tableElts = $6;
1957 n->inhRelations = $8;
1958 n->constraints = NIL;
1959 n->options = $9;
1960 n->oncommit = $10;
1961 n->tablespacename = $11;
1962 $$ = (Node *)n;
1964 | CREATE OptTemp TABLE qualified_name OF qualified_name
1965 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1967 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1968 * by our inheritance capabilities. Let's try it...
1970 CreateStmt *n = makeNode(CreateStmt);
1971 $4->istemp = $2;
1972 n->relation = $4;
1973 n->tableElts = $8;
1974 n->inhRelations = list_make1($6);
1975 n->constraints = NIL;
1976 n->options = $10;
1977 n->oncommit = $11;
1978 n->tablespacename = $12;
1979 $$ = (Node *)n;
1984 * Redundancy here is needed to avoid shift/reduce conflicts,
1985 * since TEMP is not a reserved word. See also OptTempTableName.
1987 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1988 * the LOCAL keyword is really meaningless.
1990 OptTemp: TEMPORARY { $$ = TRUE; }
1991 | TEMP { $$ = TRUE; }
1992 | LOCAL TEMPORARY { $$ = TRUE; }
1993 | LOCAL TEMP { $$ = TRUE; }
1994 | GLOBAL TEMPORARY { $$ = TRUE; }
1995 | GLOBAL TEMP { $$ = TRUE; }
1996 | /*EMPTY*/ { $$ = FALSE; }
1999 OptTableElementList:
2000 TableElementList { $$ = $1; }
2001 | /*EMPTY*/ { $$ = NIL; }
2004 TableElementList:
2005 TableElement
2007 $$ = list_make1($1);
2009 | TableElementList ',' TableElement
2011 $$ = lappend($1, $3);
2015 TableElement:
2016 columnDef { $$ = $1; }
2017 | TableLikeClause { $$ = $1; }
2018 | TableConstraint { $$ = $1; }
2021 columnDef: ColId Typename ColQualList
2023 ColumnDef *n = makeNode(ColumnDef);
2024 n->colname = $1;
2025 n->typename = $2;
2026 n->constraints = $3;
2027 n->is_local = true;
2028 $$ = (Node *)n;
2032 ColQualList:
2033 ColQualList ColConstraint { $$ = lappend($1, $2); }
2034 | /*EMPTY*/ { $$ = NIL; }
2037 ColConstraint:
2038 CONSTRAINT name ColConstraintElem
2040 switch (nodeTag($3))
2042 case T_Constraint:
2044 Constraint *n = (Constraint *)$3;
2045 n->name = $2;
2047 break;
2048 case T_FkConstraint:
2050 FkConstraint *n = (FkConstraint *)$3;
2051 n->constr_name = $2;
2053 break;
2054 default:
2055 break;
2057 $$ = $3;
2059 | ColConstraintElem { $$ = $1; }
2060 | ConstraintAttr { $$ = $1; }
2063 /* DEFAULT NULL is already the default for Postgres.
2064 * But define it here and carry it forward into the system
2065 * to make it explicit.
2066 * - thomas 1998-09-13
2068 * WITH NULL and NULL are not SQL92-standard syntax elements,
2069 * so leave them out. Use DEFAULT NULL to explicitly indicate
2070 * that a column may have that value. WITH NULL leads to
2071 * shift/reduce conflicts with WITH TIME ZONE anyway.
2072 * - thomas 1999-01-08
2074 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2075 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2076 * or be part of a_expr NOT LIKE or similar constructs).
2078 ColConstraintElem:
2079 NOT NULL_P
2081 Constraint *n = makeNode(Constraint);
2082 n->contype = CONSTR_NOTNULL;
2083 n->name = NULL;
2084 n->raw_expr = NULL;
2085 n->cooked_expr = NULL;
2086 n->keys = NULL;
2087 n->indexspace = NULL;
2088 $$ = (Node *)n;
2090 | NULL_P
2092 Constraint *n = makeNode(Constraint);
2093 n->contype = CONSTR_NULL;
2094 n->name = NULL;
2095 n->raw_expr = NULL;
2096 n->cooked_expr = NULL;
2097 n->keys = NULL;
2098 n->indexspace = NULL;
2099 $$ = (Node *)n;
2101 | UNIQUE opt_definition OptConsTableSpace
2103 Constraint *n = makeNode(Constraint);
2104 n->contype = CONSTR_UNIQUE;
2105 n->name = NULL;
2106 n->raw_expr = NULL;
2107 n->cooked_expr = NULL;
2108 n->keys = NULL;
2109 n->options = $2;
2110 n->indexspace = $3;
2111 $$ = (Node *)n;
2113 | PRIMARY KEY opt_definition OptConsTableSpace
2115 Constraint *n = makeNode(Constraint);
2116 n->contype = CONSTR_PRIMARY;
2117 n->name = NULL;
2118 n->raw_expr = NULL;
2119 n->cooked_expr = NULL;
2120 n->keys = NULL;
2121 n->options = $3;
2122 n->indexspace = $4;
2123 $$ = (Node *)n;
2125 | CHECK '(' a_expr ')'
2127 Constraint *n = makeNode(Constraint);
2128 n->contype = CONSTR_CHECK;
2129 n->name = NULL;
2130 n->raw_expr = $3;
2131 n->cooked_expr = NULL;
2132 n->keys = NULL;
2133 n->indexspace = NULL;
2134 $$ = (Node *)n;
2136 | DEFAULT b_expr
2138 Constraint *n = makeNode(Constraint);
2139 n->contype = CONSTR_DEFAULT;
2140 n->name = NULL;
2141 n->raw_expr = $2;
2142 n->cooked_expr = NULL;
2143 n->keys = NULL;
2144 n->indexspace = NULL;
2145 $$ = (Node *)n;
2147 | REFERENCES qualified_name opt_column_list key_match key_actions
2149 FkConstraint *n = makeNode(FkConstraint);
2150 n->constr_name = NULL;
2151 n->pktable = $2;
2152 n->fk_attrs = NIL;
2153 n->pk_attrs = $3;
2154 n->fk_matchtype = $4;
2155 n->fk_upd_action = (char) ($5 >> 8);
2156 n->fk_del_action = (char) ($5 & 0xFF);
2157 n->deferrable = FALSE;
2158 n->initdeferred = FALSE;
2159 $$ = (Node *)n;
2164 * ConstraintAttr represents constraint attributes, which we parse as if
2165 * they were independent constraint clauses, in order to avoid shift/reduce
2166 * conflicts (since NOT might start either an independent NOT NULL clause
2167 * or an attribute). parse_utilcmd.c is responsible for attaching the
2168 * attribute information to the preceding "real" constraint node, and for
2169 * complaining if attribute clauses appear in the wrong place or wrong
2170 * combinations.
2172 * See also ConstraintAttributeSpec, which can be used in places where
2173 * there is no parsing conflict.
2175 ConstraintAttr:
2176 DEFERRABLE
2178 Constraint *n = makeNode(Constraint);
2179 n->contype = CONSTR_ATTR_DEFERRABLE;
2180 $$ = (Node *)n;
2182 | NOT DEFERRABLE
2184 Constraint *n = makeNode(Constraint);
2185 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2186 $$ = (Node *)n;
2188 | INITIALLY DEFERRED
2190 Constraint *n = makeNode(Constraint);
2191 n->contype = CONSTR_ATTR_DEFERRED;
2192 $$ = (Node *)n;
2194 | INITIALLY IMMEDIATE
2196 Constraint *n = makeNode(Constraint);
2197 n->contype = CONSTR_ATTR_IMMEDIATE;
2198 $$ = (Node *)n;
2204 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2205 * This seems to be a poor man's inheritance capability, with the resulting
2206 * tables completely decoupled except for the original commonality in definitions.
2208 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2209 * which is a part of SQL:2003.
2211 TableLikeClause:
2212 LIKE qualified_name TableLikeOptionList
2214 InhRelation *n = makeNode(InhRelation);
2215 n->relation = $2;
2216 n->options = $3;
2217 $$ = (Node *)n;
2221 TableLikeOptionList:
2222 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2223 | /* EMPTY */ { $$ = NIL; }
2226 TableLikeOption:
2227 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2228 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2229 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2230 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2231 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2232 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2236 /* ConstraintElem specifies constraint syntax which is not embedded into
2237 * a column definition. ColConstraintElem specifies the embedded form.
2238 * - thomas 1997-12-03
2240 TableConstraint:
2241 CONSTRAINT name ConstraintElem
2243 switch (nodeTag($3))
2245 case T_Constraint:
2247 Constraint *n = (Constraint *)$3;
2248 n->name = $2;
2250 break;
2251 case T_FkConstraint:
2253 FkConstraint *n = (FkConstraint *)$3;
2254 n->constr_name = $2;
2256 break;
2257 default:
2258 break;
2260 $$ = $3;
2262 | ConstraintElem { $$ = $1; }
2265 ConstraintElem:
2266 CHECK '(' a_expr ')'
2268 Constraint *n = makeNode(Constraint);
2269 n->contype = CONSTR_CHECK;
2270 n->name = NULL;
2271 n->raw_expr = $3;
2272 n->cooked_expr = NULL;
2273 n->indexspace = NULL;
2274 $$ = (Node *)n;
2276 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2278 Constraint *n = makeNode(Constraint);
2279 n->contype = CONSTR_UNIQUE;
2280 n->name = NULL;
2281 n->raw_expr = NULL;
2282 n->cooked_expr = NULL;
2283 n->keys = $3;
2284 n->options = $5;
2285 n->indexspace = $6;
2286 $$ = (Node *)n;
2288 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2290 Constraint *n = makeNode(Constraint);
2291 n->contype = CONSTR_PRIMARY;
2292 n->name = NULL;
2293 n->raw_expr = NULL;
2294 n->cooked_expr = NULL;
2295 n->keys = $4;
2296 n->options = $6;
2297 n->indexspace = $7;
2298 $$ = (Node *)n;
2300 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2301 opt_column_list key_match key_actions ConstraintAttributeSpec
2303 FkConstraint *n = makeNode(FkConstraint);
2304 n->constr_name = NULL;
2305 n->pktable = $7;
2306 n->fk_attrs = $4;
2307 n->pk_attrs = $8;
2308 n->fk_matchtype = $9;
2309 n->fk_upd_action = (char) ($10 >> 8);
2310 n->fk_del_action = (char) ($10 & 0xFF);
2311 n->deferrable = ($11 & 1) != 0;
2312 n->initdeferred = ($11 & 2) != 0;
2313 $$ = (Node *)n;
2317 opt_column_list:
2318 '(' columnList ')' { $$ = $2; }
2319 | /*EMPTY*/ { $$ = NIL; }
2322 columnList:
2323 columnElem { $$ = list_make1($1); }
2324 | columnList ',' columnElem { $$ = lappend($1, $3); }
2327 columnElem: ColId
2329 $$ = (Node *) makeString($1);
2333 key_match: MATCH FULL
2335 $$ = FKCONSTR_MATCH_FULL;
2337 | MATCH PARTIAL
2339 ereport(ERROR,
2340 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2341 errmsg("MATCH PARTIAL not yet implemented"),
2342 scanner_errposition(@1)));
2343 $$ = FKCONSTR_MATCH_PARTIAL;
2345 | MATCH SIMPLE
2347 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2349 | /*EMPTY*/
2351 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2356 * We combine the update and delete actions into one value temporarily
2357 * for simplicity of parsing, and then break them down again in the
2358 * calling production. update is in the left 8 bits, delete in the right.
2359 * Note that NOACTION is the default.
2361 key_actions:
2362 key_update
2363 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2364 | key_delete
2365 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2366 | key_update key_delete
2367 { $$ = ($1 << 8) | ($2 & 0xFF); }
2368 | key_delete key_update
2369 { $$ = ($2 << 8) | ($1 & 0xFF); }
2370 | /*EMPTY*/
2371 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2374 key_update: ON UPDATE key_action { $$ = $3; }
2377 key_delete: ON DELETE_P key_action { $$ = $3; }
2380 key_action:
2381 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2382 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2383 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2384 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2385 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2388 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2389 | /*EMPTY*/ { $$ = NIL; }
2392 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2393 OptWith:
2394 WITH definition { $$ = $2; }
2395 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2396 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2397 | /*EMPTY*/ { $$ = NIL; }
2400 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2401 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2402 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2403 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2406 OptTableSpace: TABLESPACE name { $$ = $2; }
2407 | /*EMPTY*/ { $$ = NULL; }
2410 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2411 | /*EMPTY*/ { $$ = NULL; }
2416 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2417 * SELECT ... INTO.
2420 CreateAsStmt:
2421 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2424 * When the SelectStmt is a set-operation tree, we must
2425 * stuff the INTO information into the leftmost component
2426 * Select, because that's where analyze.c will expect
2427 * to find it. Similarly, the output column names must
2428 * be attached to that Select's target list.
2430 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2431 if (n->intoClause != NULL)
2432 ereport(ERROR,
2433 (errcode(ERRCODE_SYNTAX_ERROR),
2434 errmsg("CREATE TABLE AS cannot specify INTO"),
2435 scanner_errposition(exprLocation((Node *) n->intoClause))));
2436 $4->rel->istemp = $2;
2437 n->intoClause = $4;
2438 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2439 if (!$7)
2440 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2441 $$ = $6;
2445 create_as_target:
2446 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2448 $$ = makeNode(IntoClause);
2449 $$->rel = $1;
2450 $$->colNames = $2;
2451 $$->options = $3;
2452 $$->onCommit = $4;
2453 $$->tableSpaceName = $5;
2457 OptCreateAs:
2458 '(' CreateAsList ')' { $$ = $2; }
2459 | /*EMPTY*/ { $$ = NIL; }
2462 CreateAsList:
2463 CreateAsElement { $$ = list_make1($1); }
2464 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2467 CreateAsElement:
2468 ColId
2470 ColumnDef *n = makeNode(ColumnDef);
2471 n->colname = $1;
2472 n->typename = NULL;
2473 n->inhcount = 0;
2474 n->is_local = true;
2475 n->is_not_null = false;
2476 n->raw_default = NULL;
2477 n->cooked_default = NULL;
2478 n->constraints = NIL;
2479 $$ = (Node *)n;
2483 opt_with_data:
2484 WITH DATA_P { $$ = TRUE; }
2485 | WITH NO DATA_P { $$ = FALSE; }
2486 | /*EMPTY*/ { $$ = TRUE; }
2490 /*****************************************************************************
2492 * QUERY :
2493 * CREATE SEQUENCE seqname
2494 * ALTER SEQUENCE seqname
2496 *****************************************************************************/
2498 CreateSeqStmt:
2499 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2501 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2502 $4->istemp = $2;
2503 n->sequence = $4;
2504 n->options = $5;
2505 $$ = (Node *)n;
2509 AlterSeqStmt:
2510 ALTER SEQUENCE relation_expr SeqOptList
2512 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2513 n->sequence = $3;
2514 n->options = $4;
2515 $$ = (Node *)n;
2519 OptSeqOptList: SeqOptList { $$ = $1; }
2520 | /*EMPTY*/ { $$ = NIL; }
2523 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2524 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2527 SeqOptElem: CACHE NumericOnly
2529 $$ = makeDefElem("cache", (Node *)$2);
2531 | CYCLE
2533 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2535 | NO CYCLE
2537 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2539 | INCREMENT opt_by NumericOnly
2541 $$ = makeDefElem("increment", (Node *)$3);
2543 | MAXVALUE NumericOnly
2545 $$ = makeDefElem("maxvalue", (Node *)$2);
2547 | MINVALUE NumericOnly
2549 $$ = makeDefElem("minvalue", (Node *)$2);
2551 | NO MAXVALUE
2553 $$ = makeDefElem("maxvalue", NULL);
2555 | NO MINVALUE
2557 $$ = makeDefElem("minvalue", NULL);
2559 | OWNED BY any_name
2561 $$ = makeDefElem("owned_by", (Node *)$3);
2563 | START opt_with NumericOnly
2565 $$ = makeDefElem("start", (Node *)$3);
2567 | RESTART
2569 $$ = makeDefElem("restart", NULL);
2571 | RESTART opt_with NumericOnly
2573 $$ = makeDefElem("restart", (Node *)$3);
2577 opt_by: BY {}
2578 | /* empty */ {}
2581 NumericOnly:
2582 FCONST { $$ = makeFloat($1); }
2583 | '-' FCONST
2585 $$ = makeFloat($2);
2586 doNegateFloat($$);
2588 | SignedIconst { $$ = makeInteger($1); };
2591 /*****************************************************************************
2593 * QUERIES :
2594 * CREATE PROCEDURAL LANGUAGE ...
2595 * DROP PROCEDURAL LANGUAGE ...
2597 *****************************************************************************/
2599 CreatePLangStmt:
2600 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2602 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2603 n->plname = $5;
2604 /* parameters are all to be supplied by system */
2605 n->plhandler = NIL;
2606 n->plvalidator = NIL;
2607 n->pltrusted = false;
2608 $$ = (Node *)n;
2610 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2611 HANDLER handler_name opt_validator opt_lancompiler
2613 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2614 n->plname = $5;
2615 n->plhandler = $7;
2616 n->plvalidator = $8;
2617 n->pltrusted = $2;
2618 /* LANCOMPILER is now ignored entirely */
2619 $$ = (Node *)n;
2623 opt_trusted:
2624 TRUSTED { $$ = TRUE; }
2625 | /*EMPTY*/ { $$ = FALSE; }
2628 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2629 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2630 * Work around by using simple names, instead.
2632 handler_name:
2633 name { $$ = list_make1(makeString($1)); }
2634 | name attrs { $$ = lcons(makeString($1), $2); }
2637 opt_validator:
2638 VALIDATOR handler_name { $$ = $2; }
2639 | /*EMPTY*/ { $$ = NIL; }
2642 opt_lancompiler:
2643 LANCOMPILER Sconst { $$ = $2; }
2644 | /*EMPTY*/ { $$ = NULL; }
2647 DropPLangStmt:
2648 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2650 DropPLangStmt *n = makeNode(DropPLangStmt);
2651 n->plname = $4;
2652 n->behavior = $5;
2653 n->missing_ok = false;
2654 $$ = (Node *)n;
2656 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2658 DropPLangStmt *n = makeNode(DropPLangStmt);
2659 n->plname = $6;
2660 n->behavior = $7;
2661 n->missing_ok = true;
2662 $$ = (Node *)n;
2666 opt_procedural:
2667 PROCEDURAL {}
2668 | /*EMPTY*/ {}
2671 /*****************************************************************************
2673 * QUERY:
2674 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2676 *****************************************************************************/
2678 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2680 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2681 n->tablespacename = $3;
2682 n->owner = $4;
2683 n->location = $6;
2684 $$ = (Node *) n;
2688 OptTableSpaceOwner: OWNER name { $$ = $2; }
2689 | /*EMPTY */ { $$ = NULL; }
2692 /*****************************************************************************
2694 * QUERY :
2695 * DROP TABLESPACE <tablespace>
2697 * No need for drop behaviour as we cannot implement dependencies for
2698 * objects in other databases; we can only support RESTRICT.
2700 ****************************************************************************/
2702 DropTableSpaceStmt: DROP TABLESPACE name
2704 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2705 n->tablespacename = $3;
2706 n->missing_ok = false;
2707 $$ = (Node *) n;
2709 | DROP TABLESPACE IF_P EXISTS name
2711 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2712 n->tablespacename = $5;
2713 n->missing_ok = true;
2714 $$ = (Node *) n;
2718 /*****************************************************************************
2720 * QUERIES :
2721 * CREATE TRIGGER ...
2722 * DROP TRIGGER ...
2724 *****************************************************************************/
2726 CreateTrigStmt:
2727 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2728 qualified_name TriggerForSpec EXECUTE PROCEDURE
2729 func_name '(' TriggerFuncArgs ')'
2731 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2732 n->trigname = $3;
2733 n->relation = $7;
2734 n->funcname = $11;
2735 n->args = $13;
2736 n->before = $4;
2737 n->row = $8;
2738 memcpy(n->actions, $5, 4);
2739 n->isconstraint = FALSE;
2740 n->deferrable = FALSE;
2741 n->initdeferred = FALSE;
2742 n->constrrel = NULL;
2743 $$ = (Node *)n;
2745 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2746 qualified_name OptConstrFromTable
2747 ConstraintAttributeSpec
2748 FOR EACH ROW EXECUTE PROCEDURE
2749 func_name '(' TriggerFuncArgs ')'
2751 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2752 n->trigname = $4;
2753 n->relation = $8;
2754 n->funcname = $16;
2755 n->args = $18;
2756 n->before = FALSE;
2757 n->row = TRUE;
2758 memcpy(n->actions, $6, 4);
2759 n->isconstraint = TRUE;
2760 n->deferrable = ($10 & 1) != 0;
2761 n->initdeferred = ($10 & 2) != 0;
2763 n->constrrel = $9;
2764 $$ = (Node *)n;
2768 TriggerActionTime:
2769 BEFORE { $$ = TRUE; }
2770 | AFTER { $$ = FALSE; }
2773 TriggerEvents:
2774 TriggerOneEvent
2776 char *e = palloc(4);
2777 e[0] = $1; e[1] = '\0';
2778 $$ = e;
2780 | TriggerOneEvent OR TriggerOneEvent
2782 char *e = palloc(4);
2783 e[0] = $1; e[1] = $3; e[2] = '\0';
2784 $$ = e;
2786 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2788 char *e = palloc(4);
2789 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2790 $$ = e;
2794 TriggerOneEvent:
2795 INSERT { $$ = 'i'; }
2796 | DELETE_P { $$ = 'd'; }
2797 | UPDATE { $$ = 'u'; }
2798 | TRUNCATE { $$ = 't'; }
2801 TriggerForSpec:
2802 FOR TriggerForOpt TriggerForType
2804 $$ = $3;
2806 | /* EMPTY */
2809 * If ROW/STATEMENT not specified, default to
2810 * STATEMENT, per SQL
2812 $$ = FALSE;
2816 TriggerForOpt:
2817 EACH {}
2818 | /*EMPTY*/ {}
2821 TriggerForType:
2822 ROW { $$ = TRUE; }
2823 | STATEMENT { $$ = FALSE; }
2826 TriggerFuncArgs:
2827 TriggerFuncArg { $$ = list_make1($1); }
2828 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2829 | /*EMPTY*/ { $$ = NIL; }
2832 TriggerFuncArg:
2833 Iconst
2835 char buf[64];
2836 snprintf(buf, sizeof(buf), "%d", $1);
2837 $$ = makeString(pstrdup(buf));
2839 | FCONST { $$ = makeString($1); }
2840 | Sconst { $$ = makeString($1); }
2841 | BCONST { $$ = makeString($1); }
2842 | XCONST { $$ = makeString($1); }
2843 | ColId { $$ = makeString($1); }
2846 OptConstrFromTable:
2847 FROM qualified_name { $$ = $2; }
2848 | /*EMPTY*/ { $$ = NULL; }
2851 ConstraintAttributeSpec:
2852 ConstraintDeferrabilitySpec
2853 { $$ = $1; }
2854 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2856 if ($1 == 0 && $2 != 0)
2857 ereport(ERROR,
2858 (errcode(ERRCODE_SYNTAX_ERROR),
2859 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2860 scanner_errposition(@1)));
2861 $$ = $1 | $2;
2863 | ConstraintTimeSpec
2865 if ($1 != 0)
2866 $$ = 3;
2867 else
2868 $$ = 0;
2870 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2872 if ($2 == 0 && $1 != 0)
2873 ereport(ERROR,
2874 (errcode(ERRCODE_SYNTAX_ERROR),
2875 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2876 scanner_errposition(@1)));
2877 $$ = $1 | $2;
2879 | /*EMPTY*/
2880 { $$ = 0; }
2883 ConstraintDeferrabilitySpec:
2884 NOT DEFERRABLE { $$ = 0; }
2885 | DEFERRABLE { $$ = 1; }
2888 ConstraintTimeSpec:
2889 INITIALLY IMMEDIATE { $$ = 0; }
2890 | INITIALLY DEFERRED { $$ = 2; }
2894 DropTrigStmt:
2895 DROP TRIGGER name ON qualified_name opt_drop_behavior
2897 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2898 n->relation = $5;
2899 n->property = $3;
2900 n->behavior = $6;
2901 n->removeType = OBJECT_TRIGGER;
2902 n->missing_ok = false;
2903 $$ = (Node *) n;
2905 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
2907 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2908 n->relation = $7;
2909 n->property = $5;
2910 n->behavior = $8;
2911 n->removeType = OBJECT_TRIGGER;
2912 n->missing_ok = true;
2913 $$ = (Node *) n;
2918 /*****************************************************************************
2920 * QUERIES :
2921 * CREATE ASSERTION ...
2922 * DROP ASSERTION ...
2924 *****************************************************************************/
2926 CreateAssertStmt:
2927 CREATE ASSERTION name CHECK '(' a_expr ')'
2928 ConstraintAttributeSpec
2930 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2931 n->trigname = $3;
2932 n->args = list_make1($6);
2933 n->isconstraint = TRUE;
2934 n->deferrable = ($8 & 1) != 0;
2935 n->initdeferred = ($8 & 2) != 0;
2937 ereport(ERROR,
2938 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2939 errmsg("CREATE ASSERTION is not yet implemented")));
2941 $$ = (Node *)n;
2945 DropAssertStmt:
2946 DROP ASSERTION name opt_drop_behavior
2948 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2949 n->relation = NULL;
2950 n->property = $3;
2951 n->behavior = $4;
2952 n->removeType = OBJECT_TRIGGER; /* XXX */
2953 ereport(ERROR,
2954 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2955 errmsg("DROP ASSERTION is not yet implemented")));
2956 $$ = (Node *) n;
2961 /*****************************************************************************
2963 * QUERY :
2964 * define (aggregate,operator,type)
2966 *****************************************************************************/
2968 DefineStmt:
2969 CREATE AGGREGATE func_name aggr_args definition
2971 DefineStmt *n = makeNode(DefineStmt);
2972 n->kind = OBJECT_AGGREGATE;
2973 n->oldstyle = false;
2974 n->defnames = $3;
2975 n->args = $4;
2976 n->definition = $5;
2977 $$ = (Node *)n;
2979 | CREATE AGGREGATE func_name old_aggr_definition
2981 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
2982 DefineStmt *n = makeNode(DefineStmt);
2983 n->kind = OBJECT_AGGREGATE;
2984 n->oldstyle = true;
2985 n->defnames = $3;
2986 n->args = NIL;
2987 n->definition = $4;
2988 $$ = (Node *)n;
2990 | CREATE OPERATOR any_operator definition
2992 DefineStmt *n = makeNode(DefineStmt);
2993 n->kind = OBJECT_OPERATOR;
2994 n->oldstyle = false;
2995 n->defnames = $3;
2996 n->args = NIL;
2997 n->definition = $4;
2998 $$ = (Node *)n;
3000 | CREATE TYPE_P any_name definition
3002 DefineStmt *n = makeNode(DefineStmt);
3003 n->kind = OBJECT_TYPE;
3004 n->oldstyle = false;
3005 n->defnames = $3;
3006 n->args = NIL;
3007 n->definition = $4;
3008 $$ = (Node *)n;
3010 | CREATE TYPE_P any_name
3012 /* Shell type (identified by lack of definition) */
3013 DefineStmt *n = makeNode(DefineStmt);
3014 n->kind = OBJECT_TYPE;
3015 n->oldstyle = false;
3016 n->defnames = $3;
3017 n->args = NIL;
3018 n->definition = NIL;
3019 $$ = (Node *)n;
3021 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3023 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3024 RangeVar *r = makeNode(RangeVar);
3026 /* can't use qualified_name, sigh */
3027 switch (list_length($3))
3029 case 1:
3030 r->catalogname = NULL;
3031 r->schemaname = NULL;
3032 r->relname = strVal(linitial($3));
3033 break;
3034 case 2:
3035 r->catalogname = NULL;
3036 r->schemaname = strVal(linitial($3));
3037 r->relname = strVal(lsecond($3));
3038 break;
3039 case 3:
3040 r->catalogname = strVal(linitial($3));
3041 r->schemaname = strVal(lsecond($3));
3042 r->relname = strVal(lthird($3));
3043 break;
3044 default:
3045 ereport(ERROR,
3046 (errcode(ERRCODE_SYNTAX_ERROR),
3047 errmsg("improper qualified name (too many dotted names): %s",
3048 NameListToString($3)),
3049 scanner_errposition(@3)));
3050 break;
3052 r->location = @3;
3053 n->typevar = r;
3054 n->coldeflist = $6;
3055 $$ = (Node *)n;
3057 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3059 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3060 n->typename = $3;
3061 n->vals = $7;
3062 $$ = (Node *)n;
3064 | CREATE TEXT_P SEARCH PARSER any_name definition
3066 DefineStmt *n = makeNode(DefineStmt);
3067 n->kind = OBJECT_TSPARSER;
3068 n->args = NIL;
3069 n->defnames = $5;
3070 n->definition = $6;
3071 $$ = (Node *)n;
3073 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3075 DefineStmt *n = makeNode(DefineStmt);
3076 n->kind = OBJECT_TSDICTIONARY;
3077 n->args = NIL;
3078 n->defnames = $5;
3079 n->definition = $6;
3080 $$ = (Node *)n;
3082 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3084 DefineStmt *n = makeNode(DefineStmt);
3085 n->kind = OBJECT_TSTEMPLATE;
3086 n->args = NIL;
3087 n->defnames = $5;
3088 n->definition = $6;
3089 $$ = (Node *)n;
3091 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3093 DefineStmt *n = makeNode(DefineStmt);
3094 n->kind = OBJECT_TSCONFIGURATION;
3095 n->args = NIL;
3096 n->defnames = $5;
3097 n->definition = $6;
3098 $$ = (Node *)n;
3102 definition: '(' def_list ')' { $$ = $2; }
3105 def_list: def_elem { $$ = list_make1($1); }
3106 | def_list ',' def_elem { $$ = lappend($1, $3); }
3109 def_elem: ColLabel '=' def_arg
3111 $$ = makeDefElem($1, (Node *)$3);
3113 | ColLabel
3115 $$ = makeDefElem($1, NULL);
3119 /* Note: any simple identifier will be returned as a type name! */
3120 def_arg: func_type { $$ = (Node *)$1; }
3121 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3122 | qual_all_Op { $$ = (Node *)$1; }
3123 | NumericOnly { $$ = (Node *)$1; }
3124 | Sconst { $$ = (Node *)makeString($1); }
3127 aggr_args: '(' type_list ')' { $$ = $2; }
3128 | '(' '*' ')' { $$ = NIL; }
3131 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3134 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3135 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3138 old_aggr_elem: IDENT '=' def_arg
3140 $$ = makeDefElem($1, (Node *)$3);
3144 enum_val_list: Sconst
3145 { $$ = list_make1(makeString($1)); }
3146 | enum_val_list ',' Sconst
3147 { $$ = lappend($1, makeString($3)); }
3151 /*****************************************************************************
3153 * QUERIES :
3154 * CREATE OPERATOR CLASS ...
3155 * CREATE OPERATOR FAMILY ...
3156 * ALTER OPERATOR FAMILY ...
3157 * DROP OPERATOR CLASS ...
3158 * DROP OPERATOR FAMILY ...
3160 *****************************************************************************/
3162 CreateOpClassStmt:
3163 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3164 USING access_method opt_opfamily AS opclass_item_list
3166 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3167 n->opclassname = $4;
3168 n->isDefault = $5;
3169 n->datatype = $8;
3170 n->amname = $10;
3171 n->opfamilyname = $11;
3172 n->items = $13;
3173 $$ = (Node *) n;
3177 opclass_item_list:
3178 opclass_item { $$ = list_make1($1); }
3179 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3182 opclass_item:
3183 OPERATOR Iconst any_operator opt_recheck
3185 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3186 n->itemtype = OPCLASS_ITEM_OPERATOR;
3187 n->name = $3;
3188 n->args = NIL;
3189 n->number = $2;
3190 $$ = (Node *) n;
3192 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3194 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3195 n->itemtype = OPCLASS_ITEM_OPERATOR;
3196 n->name = $3;
3197 n->args = $4;
3198 n->number = $2;
3199 $$ = (Node *) n;
3201 | FUNCTION Iconst func_name func_args
3203 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3204 n->itemtype = OPCLASS_ITEM_FUNCTION;
3205 n->name = $3;
3206 n->args = extractArgTypes($4);
3207 n->number = $2;
3208 $$ = (Node *) n;
3210 | FUNCTION Iconst '(' type_list ')' func_name func_args
3212 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3213 n->itemtype = OPCLASS_ITEM_FUNCTION;
3214 n->name = $6;
3215 n->args = extractArgTypes($7);
3216 n->number = $2;
3217 n->class_args = $4;
3218 $$ = (Node *) n;
3220 | STORAGE Typename
3222 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3223 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3224 n->storedtype = $2;
3225 $$ = (Node *) n;
3229 opt_default: DEFAULT { $$ = TRUE; }
3230 | /*EMPTY*/ { $$ = FALSE; }
3233 opt_opfamily: FAMILY any_name { $$ = $2; }
3234 | /*EMPTY*/ { $$ = NIL; }
3237 opt_recheck: RECHECK
3239 ereport(ERROR,
3240 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3241 errmsg("RECHECK is no longer supported"),
3242 errhint("Update your data type."),
3243 scanner_errposition(@1)));
3244 $$ = TRUE;
3246 | /*EMPTY*/ { $$ = FALSE; }
3250 CreateOpFamilyStmt:
3251 CREATE OPERATOR FAMILY any_name USING access_method
3253 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3254 n->opfamilyname = $4;
3255 n->amname = $6;
3256 $$ = (Node *) n;
3260 AlterOpFamilyStmt:
3261 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3263 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3264 n->opfamilyname = $4;
3265 n->amname = $6;
3266 n->isDrop = false;
3267 n->items = $8;
3268 $$ = (Node *) n;
3270 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3272 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3273 n->opfamilyname = $4;
3274 n->amname = $6;
3275 n->isDrop = true;
3276 n->items = $8;
3277 $$ = (Node *) n;
3281 opclass_drop_list:
3282 opclass_drop { $$ = list_make1($1); }
3283 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3286 opclass_drop:
3287 OPERATOR Iconst '(' type_list ')'
3289 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3290 n->itemtype = OPCLASS_ITEM_OPERATOR;
3291 n->number = $2;
3292 n->args = $4;
3293 $$ = (Node *) n;
3295 | FUNCTION Iconst '(' type_list ')'
3297 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3298 n->itemtype = OPCLASS_ITEM_FUNCTION;
3299 n->number = $2;
3300 n->args = $4;
3301 $$ = (Node *) n;
3306 DropOpClassStmt:
3307 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3309 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3310 n->opclassname = $4;
3311 n->amname = $6;
3312 n->behavior = $7;
3313 n->missing_ok = false;
3314 $$ = (Node *) n;
3316 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3318 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3319 n->opclassname = $6;
3320 n->amname = $8;
3321 n->behavior = $9;
3322 n->missing_ok = true;
3323 $$ = (Node *) n;
3327 DropOpFamilyStmt:
3328 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3330 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3331 n->opfamilyname = $4;
3332 n->amname = $6;
3333 n->behavior = $7;
3334 n->missing_ok = false;
3335 $$ = (Node *) n;
3337 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3339 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3340 n->opfamilyname = $6;
3341 n->amname = $8;
3342 n->behavior = $9;
3343 n->missing_ok = true;
3344 $$ = (Node *) n;
3349 /*****************************************************************************
3351 * QUERY:
3353 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3354 * REASSIGN OWNED BY username [, username ...] TO username
3356 *****************************************************************************/
3357 DropOwnedStmt:
3358 DROP OWNED BY name_list opt_drop_behavior
3360 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3361 n->roles = $4;
3362 n->behavior = $5;
3363 $$ = (Node *)n;
3367 ReassignOwnedStmt:
3368 REASSIGN OWNED BY name_list TO name
3370 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3371 n->roles = $4;
3372 n->newrole = $6;
3373 $$ = (Node *)n;
3377 /*****************************************************************************
3379 * QUERY:
3381 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3382 * [ RESTRICT | CASCADE ]
3384 *****************************************************************************/
3386 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3388 DropStmt *n = makeNode(DropStmt);
3389 n->removeType = $2;
3390 n->missing_ok = TRUE;
3391 n->objects = $5;
3392 n->behavior = $6;
3393 $$ = (Node *)n;
3395 | DROP drop_type any_name_list opt_drop_behavior
3397 DropStmt *n = makeNode(DropStmt);
3398 n->removeType = $2;
3399 n->missing_ok = FALSE;
3400 n->objects = $3;
3401 n->behavior = $4;
3402 $$ = (Node *)n;
3407 drop_type: TABLE { $$ = OBJECT_TABLE; }
3408 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3409 | VIEW { $$ = OBJECT_VIEW; }
3410 | INDEX { $$ = OBJECT_INDEX; }
3411 | TYPE_P { $$ = OBJECT_TYPE; }
3412 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3413 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3414 | SCHEMA { $$ = OBJECT_SCHEMA; }
3415 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3416 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3417 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3418 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3421 any_name_list:
3422 any_name { $$ = list_make1($1); }
3423 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3426 any_name: ColId { $$ = list_make1(makeString($1)); }
3427 | ColId attrs { $$ = lcons(makeString($1), $2); }
3430 attrs: '.' attr_name
3431 { $$ = list_make1(makeString($2)); }
3432 | attrs '.' attr_name
3433 { $$ = lappend($1, makeString($3)); }
3437 /*****************************************************************************
3439 * QUERY:
3440 * truncate table relname1, relname2, ...
3442 *****************************************************************************/
3444 TruncateStmt:
3445 TRUNCATE opt_table qualified_name_list opt_restart_seqs opt_drop_behavior
3447 TruncateStmt *n = makeNode(TruncateStmt);
3448 n->relations = $3;
3449 n->restart_seqs = $4;
3450 n->behavior = $5;
3451 $$ = (Node *)n;
3455 opt_restart_seqs:
3456 CONTINUE_P IDENTITY_P { $$ = false; }
3457 | RESTART IDENTITY_P { $$ = true; }
3458 | /* EMPTY */ { $$ = false; }
3461 /*****************************************************************************
3463 * The COMMENT ON statement can take different forms based upon the type of
3464 * the object associated with the comment. The form of the statement is:
3466 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3467 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3468 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3469 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3470 * TEXT SEARCH TEMPLATE |
3471 * TEXT SEARCH CONFIGURATION ] <objname> |
3472 * AGGREGATE <aggname> (arg1, ...) |
3473 * FUNCTION <funcname> (arg1, arg2, ...) |
3474 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3475 * TRIGGER <triggername> ON <relname> |
3476 * CONSTRAINT <constraintname> ON <relname> |
3477 * RULE <rulename> ON <relname> ]
3478 * IS 'text'
3480 *****************************************************************************/
3482 CommentStmt:
3483 COMMENT ON comment_type any_name IS comment_text
3485 CommentStmt *n = makeNode(CommentStmt);
3486 n->objtype = $3;
3487 n->objname = $4;
3488 n->objargs = NIL;
3489 n->comment = $6;
3490 $$ = (Node *) n;
3492 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3494 CommentStmt *n = makeNode(CommentStmt);
3495 n->objtype = OBJECT_AGGREGATE;
3496 n->objname = $4;
3497 n->objargs = $5;
3498 n->comment = $7;
3499 $$ = (Node *) n;
3501 | COMMENT ON FUNCTION func_name func_args IS comment_text
3503 CommentStmt *n = makeNode(CommentStmt);
3504 n->objtype = OBJECT_FUNCTION;
3505 n->objname = $4;
3506 n->objargs = extractArgTypes($5);
3507 n->comment = $7;
3508 $$ = (Node *) n;
3510 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3512 CommentStmt *n = makeNode(CommentStmt);
3513 n->objtype = OBJECT_OPERATOR;
3514 n->objname = $4;
3515 n->objargs = $5;
3516 n->comment = $7;
3517 $$ = (Node *) n;
3519 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3521 CommentStmt *n = makeNode(CommentStmt);
3522 n->objtype = OBJECT_CONSTRAINT;
3523 n->objname = lappend($6, makeString($4));
3524 n->objargs = NIL;
3525 n->comment = $8;
3526 $$ = (Node *) n;
3528 | COMMENT ON RULE name ON any_name IS comment_text
3530 CommentStmt *n = makeNode(CommentStmt);
3531 n->objtype = OBJECT_RULE;
3532 n->objname = lappend($6, makeString($4));
3533 n->objargs = NIL;
3534 n->comment = $8;
3535 $$ = (Node *) n;
3537 | COMMENT ON RULE name IS comment_text
3539 /* Obsolete syntax supported for awhile for compatibility */
3540 CommentStmt *n = makeNode(CommentStmt);
3541 n->objtype = OBJECT_RULE;
3542 n->objname = list_make1(makeString($4));
3543 n->objargs = NIL;
3544 n->comment = $6;
3545 $$ = (Node *) n;
3547 | COMMENT ON TRIGGER name ON any_name IS comment_text
3549 CommentStmt *n = makeNode(CommentStmt);
3550 n->objtype = OBJECT_TRIGGER;
3551 n->objname = lappend($6, makeString($4));
3552 n->objargs = NIL;
3553 n->comment = $8;
3554 $$ = (Node *) n;
3556 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3558 CommentStmt *n = makeNode(CommentStmt);
3559 n->objtype = OBJECT_OPCLASS;
3560 n->objname = $5;
3561 n->objargs = list_make1(makeString($7));
3562 n->comment = $9;
3563 $$ = (Node *) n;
3565 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3567 CommentStmt *n = makeNode(CommentStmt);
3568 n->objtype = OBJECT_OPFAMILY;
3569 n->objname = $5;
3570 n->objargs = list_make1(makeString($7));
3571 n->comment = $9;
3572 $$ = (Node *) n;
3574 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3576 CommentStmt *n = makeNode(CommentStmt);
3577 n->objtype = OBJECT_LARGEOBJECT;
3578 n->objname = list_make1($5);
3579 n->objargs = NIL;
3580 n->comment = $7;
3581 $$ = (Node *) n;
3583 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3585 CommentStmt *n = makeNode(CommentStmt);
3586 n->objtype = OBJECT_CAST;
3587 n->objname = list_make1($5);
3588 n->objargs = list_make1($7);
3589 n->comment = $10;
3590 $$ = (Node *) n;
3592 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3594 CommentStmt *n = makeNode(CommentStmt);
3595 n->objtype = OBJECT_LANGUAGE;
3596 n->objname = $5;
3597 n->objargs = NIL;
3598 n->comment = $7;
3599 $$ = (Node *) n;
3601 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3603 CommentStmt *n = makeNode(CommentStmt);
3604 n->objtype = OBJECT_TSPARSER;
3605 n->objname = $6;
3606 n->comment = $8;
3607 $$ = (Node *) n;
3609 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3611 CommentStmt *n = makeNode(CommentStmt);
3612 n->objtype = OBJECT_TSDICTIONARY;
3613 n->objname = $6;
3614 n->comment = $8;
3615 $$ = (Node *) n;
3617 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3619 CommentStmt *n = makeNode(CommentStmt);
3620 n->objtype = OBJECT_TSTEMPLATE;
3621 n->objname = $6;
3622 n->comment = $8;
3623 $$ = (Node *) n;
3625 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3627 CommentStmt *n = makeNode(CommentStmt);
3628 n->objtype = OBJECT_TSCONFIGURATION;
3629 n->objname = $6;
3630 n->comment = $8;
3631 $$ = (Node *) n;
3635 comment_type:
3636 COLUMN { $$ = OBJECT_COLUMN; }
3637 | DATABASE { $$ = OBJECT_DATABASE; }
3638 | SCHEMA { $$ = OBJECT_SCHEMA; }
3639 | INDEX { $$ = OBJECT_INDEX; }
3640 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3641 | TABLE { $$ = OBJECT_TABLE; }
3642 | DOMAIN_P { $$ = OBJECT_TYPE; }
3643 | TYPE_P { $$ = OBJECT_TYPE; }
3644 | VIEW { $$ = OBJECT_VIEW; }
3645 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3646 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3647 | ROLE { $$ = OBJECT_ROLE; }
3650 comment_text:
3651 Sconst { $$ = $1; }
3652 | NULL_P { $$ = NULL; }
3655 /*****************************************************************************
3657 * QUERY:
3658 * fetch/move
3660 *****************************************************************************/
3662 FetchStmt: FETCH fetch_direction from_in name
3664 FetchStmt *n = (FetchStmt *) $2;
3665 n->portalname = $4;
3666 n->ismove = FALSE;
3667 $$ = (Node *)n;
3669 | FETCH name
3671 FetchStmt *n = makeNode(FetchStmt);
3672 n->direction = FETCH_FORWARD;
3673 n->howMany = 1;
3674 n->portalname = $2;
3675 n->ismove = FALSE;
3676 $$ = (Node *)n;
3678 | MOVE fetch_direction from_in name
3680 FetchStmt *n = (FetchStmt *) $2;
3681 n->portalname = $4;
3682 n->ismove = TRUE;
3683 $$ = (Node *)n;
3685 | MOVE name
3687 FetchStmt *n = makeNode(FetchStmt);
3688 n->direction = FETCH_FORWARD;
3689 n->howMany = 1;
3690 n->portalname = $2;
3691 n->ismove = TRUE;
3692 $$ = (Node *)n;
3696 fetch_direction:
3697 /*EMPTY*/
3699 FetchStmt *n = makeNode(FetchStmt);
3700 n->direction = FETCH_FORWARD;
3701 n->howMany = 1;
3702 $$ = (Node *)n;
3704 | NEXT
3706 FetchStmt *n = makeNode(FetchStmt);
3707 n->direction = FETCH_FORWARD;
3708 n->howMany = 1;
3709 $$ = (Node *)n;
3711 | PRIOR
3713 FetchStmt *n = makeNode(FetchStmt);
3714 n->direction = FETCH_BACKWARD;
3715 n->howMany = 1;
3716 $$ = (Node *)n;
3718 | FIRST_P
3720 FetchStmt *n = makeNode(FetchStmt);
3721 n->direction = FETCH_ABSOLUTE;
3722 n->howMany = 1;
3723 $$ = (Node *)n;
3725 | LAST_P
3727 FetchStmt *n = makeNode(FetchStmt);
3728 n->direction = FETCH_ABSOLUTE;
3729 n->howMany = -1;
3730 $$ = (Node *)n;
3732 | ABSOLUTE_P SignedIconst
3734 FetchStmt *n = makeNode(FetchStmt);
3735 n->direction = FETCH_ABSOLUTE;
3736 n->howMany = $2;
3737 $$ = (Node *)n;
3739 | RELATIVE_P SignedIconst
3741 FetchStmt *n = makeNode(FetchStmt);
3742 n->direction = FETCH_RELATIVE;
3743 n->howMany = $2;
3744 $$ = (Node *)n;
3746 | SignedIconst
3748 FetchStmt *n = makeNode(FetchStmt);
3749 n->direction = FETCH_FORWARD;
3750 n->howMany = $1;
3751 $$ = (Node *)n;
3753 | ALL
3755 FetchStmt *n = makeNode(FetchStmt);
3756 n->direction = FETCH_FORWARD;
3757 n->howMany = FETCH_ALL;
3758 $$ = (Node *)n;
3760 | FORWARD
3762 FetchStmt *n = makeNode(FetchStmt);
3763 n->direction = FETCH_FORWARD;
3764 n->howMany = 1;
3765 $$ = (Node *)n;
3767 | FORWARD SignedIconst
3769 FetchStmt *n = makeNode(FetchStmt);
3770 n->direction = FETCH_FORWARD;
3771 n->howMany = $2;
3772 $$ = (Node *)n;
3774 | FORWARD ALL
3776 FetchStmt *n = makeNode(FetchStmt);
3777 n->direction = FETCH_FORWARD;
3778 n->howMany = FETCH_ALL;
3779 $$ = (Node *)n;
3781 | BACKWARD
3783 FetchStmt *n = makeNode(FetchStmt);
3784 n->direction = FETCH_BACKWARD;
3785 n->howMany = 1;
3786 $$ = (Node *)n;
3788 | BACKWARD SignedIconst
3790 FetchStmt *n = makeNode(FetchStmt);
3791 n->direction = FETCH_BACKWARD;
3792 n->howMany = $2;
3793 $$ = (Node *)n;
3795 | BACKWARD ALL
3797 FetchStmt *n = makeNode(FetchStmt);
3798 n->direction = FETCH_BACKWARD;
3799 n->howMany = FETCH_ALL;
3800 $$ = (Node *)n;
3804 from_in: FROM {}
3805 | IN_P {}
3809 /*****************************************************************************
3811 * GRANT and REVOKE statements
3813 *****************************************************************************/
3815 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3816 opt_grant_grant_option
3818 GrantStmt *n = makeNode(GrantStmt);
3819 n->is_grant = true;
3820 n->privileges = $2;
3821 n->objtype = ($4)->objtype;
3822 n->objects = ($4)->objs;
3823 n->grantees = $6;
3824 n->grant_option = $7;
3825 $$ = (Node*)n;
3829 RevokeStmt:
3830 REVOKE privileges ON privilege_target
3831 FROM grantee_list opt_drop_behavior
3833 GrantStmt *n = makeNode(GrantStmt);
3834 n->is_grant = false;
3835 n->grant_option = false;
3836 n->privileges = $2;
3837 n->objtype = ($4)->objtype;
3838 n->objects = ($4)->objs;
3839 n->grantees = $6;
3840 n->behavior = $7;
3841 $$ = (Node *)n;
3843 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3844 FROM grantee_list opt_drop_behavior
3846 GrantStmt *n = makeNode(GrantStmt);
3847 n->is_grant = false;
3848 n->grant_option = true;
3849 n->privileges = $5;
3850 n->objtype = ($7)->objtype;
3851 n->objects = ($7)->objs;
3852 n->grantees = $9;
3853 n->behavior = $10;
3854 $$ = (Node *)n;
3860 * A privilege list is represented as a list of strings; the validity of
3861 * the privilege names gets checked at execution. This is a bit annoying
3862 * but we have little choice because of the syntactic conflict with lists
3863 * of role names in GRANT/REVOKE. What's more, we have to call out in
3864 * the "privilege" production any reserved keywords that need to be usable
3865 * as privilege names.
3868 /* either ALL [PRIVILEGES] or a list of individual privileges */
3869 privileges: privilege_list
3870 { $$ = $1; }
3871 | ALL
3872 { $$ = NIL; }
3873 | ALL PRIVILEGES
3874 { $$ = NIL; }
3877 privilege_list: privilege
3878 { $$ = list_make1(makeString($1)); }
3879 | privilege_list ',' privilege
3880 { $$ = lappend($1, makeString($3)); }
3883 privilege: SELECT { $$ = pstrdup($1); }
3884 | REFERENCES { $$ = pstrdup($1); }
3885 | CREATE { $$ = pstrdup($1); }
3886 | ColId { $$ = $1; }
3890 /* Don't bother trying to fold the first two rules into one using
3891 * opt_table. You're going to get conflicts.
3893 privilege_target:
3894 qualified_name_list
3896 PrivTarget *n = makeNode(PrivTarget);
3897 n->objtype = ACL_OBJECT_RELATION;
3898 n->objs = $1;
3899 $$ = n;
3901 | TABLE qualified_name_list
3903 PrivTarget *n = makeNode(PrivTarget);
3904 n->objtype = ACL_OBJECT_RELATION;
3905 n->objs = $2;
3906 $$ = n;
3908 | SEQUENCE qualified_name_list
3910 PrivTarget *n = makeNode(PrivTarget);
3911 n->objtype = ACL_OBJECT_SEQUENCE;
3912 n->objs = $2;
3913 $$ = n;
3915 | FUNCTION function_with_argtypes_list
3917 PrivTarget *n = makeNode(PrivTarget);
3918 n->objtype = ACL_OBJECT_FUNCTION;
3919 n->objs = $2;
3920 $$ = n;
3922 | DATABASE name_list
3924 PrivTarget *n = makeNode(PrivTarget);
3925 n->objtype = ACL_OBJECT_DATABASE;
3926 n->objs = $2;
3927 $$ = n;
3929 | LANGUAGE name_list
3931 PrivTarget *n = makeNode(PrivTarget);
3932 n->objtype = ACL_OBJECT_LANGUAGE;
3933 n->objs = $2;
3934 $$ = n;
3936 | SCHEMA name_list
3938 PrivTarget *n = makeNode(PrivTarget);
3939 n->objtype = ACL_OBJECT_NAMESPACE;
3940 n->objs = $2;
3941 $$ = n;
3943 | TABLESPACE name_list
3945 PrivTarget *n = makeNode(PrivTarget);
3946 n->objtype = ACL_OBJECT_TABLESPACE;
3947 n->objs = $2;
3948 $$ = n;
3953 grantee_list:
3954 grantee { $$ = list_make1($1); }
3955 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3958 grantee: RoleId
3960 PrivGrantee *n = makeNode(PrivGrantee);
3961 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3962 if (strcmp($1, "public") == 0)
3963 n->rolname = NULL;
3964 else
3965 n->rolname = $1;
3966 $$ = (Node *)n;
3968 | GROUP_P RoleId
3970 PrivGrantee *n = makeNode(PrivGrantee);
3971 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3972 if (strcmp($2, "public") == 0)
3973 n->rolname = NULL;
3974 else
3975 n->rolname = $2;
3976 $$ = (Node *)n;
3981 opt_grant_grant_option:
3982 WITH GRANT OPTION { $$ = TRUE; }
3983 | /*EMPTY*/ { $$ = FALSE; }
3986 function_with_argtypes_list:
3987 function_with_argtypes { $$ = list_make1($1); }
3988 | function_with_argtypes_list ',' function_with_argtypes
3989 { $$ = lappend($1, $3); }
3992 function_with_argtypes:
3993 func_name func_args
3995 FuncWithArgs *n = makeNode(FuncWithArgs);
3996 n->funcname = $1;
3997 n->funcargs = extractArgTypes($2);
3998 $$ = n;
4002 /*****************************************************************************
4004 * GRANT and REVOKE ROLE statements
4006 *****************************************************************************/
4008 GrantRoleStmt:
4009 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4011 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4012 n->is_grant = true;
4013 n->granted_roles = $2;
4014 n->grantee_roles = $4;
4015 n->admin_opt = $5;
4016 n->grantor = $6;
4017 $$ = (Node*)n;
4021 RevokeRoleStmt:
4022 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4024 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4025 n->is_grant = false;
4026 n->admin_opt = false;
4027 n->granted_roles = $2;
4028 n->grantee_roles = $4;
4029 n->behavior = $6;
4030 $$ = (Node*)n;
4032 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4034 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4035 n->is_grant = false;
4036 n->admin_opt = true;
4037 n->granted_roles = $5;
4038 n->grantee_roles = $7;
4039 n->behavior = $9;
4040 $$ = (Node*)n;
4044 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4045 | /*EMPTY*/ { $$ = FALSE; }
4048 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4049 | /*EMPTY*/ { $$ = NULL; }
4053 /*****************************************************************************
4055 * QUERY: CREATE INDEX
4057 * Note: we can't factor CONCURRENTLY into a separate production without
4058 * making it a reserved word.
4060 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4061 * willing to make TABLESPACE a fully reserved word.
4062 *****************************************************************************/
4064 IndexStmt: CREATE index_opt_unique INDEX index_name
4065 ON qualified_name access_method_clause '(' index_params ')'
4066 opt_definition OptTableSpace where_clause
4068 IndexStmt *n = makeNode(IndexStmt);
4069 n->unique = $2;
4070 n->concurrent = false;
4071 n->idxname = $4;
4072 n->relation = $6;
4073 n->accessMethod = $7;
4074 n->indexParams = $9;
4075 n->options = $11;
4076 n->tableSpace = $12;
4077 n->whereClause = $13;
4078 $$ = (Node *)n;
4080 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4081 ON qualified_name access_method_clause '(' index_params ')'
4082 opt_definition OptTableSpace where_clause
4084 IndexStmt *n = makeNode(IndexStmt);
4085 n->unique = $2;
4086 n->concurrent = true;
4087 n->idxname = $5;
4088 n->relation = $7;
4089 n->accessMethod = $8;
4090 n->indexParams = $10;
4091 n->options = $12;
4092 n->tableSpace = $13;
4093 n->whereClause = $14;
4094 $$ = (Node *)n;
4098 index_opt_unique:
4099 UNIQUE { $$ = TRUE; }
4100 | /*EMPTY*/ { $$ = FALSE; }
4103 access_method_clause:
4104 USING access_method { $$ = $2; }
4105 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4108 index_params: index_elem { $$ = list_make1($1); }
4109 | index_params ',' index_elem { $$ = lappend($1, $3); }
4113 * Index attributes can be either simple column references, or arbitrary
4114 * expressions in parens. For backwards-compatibility reasons, we allow
4115 * an expression that's just a function call to be written without parens.
4117 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4119 $$ = makeNode(IndexElem);
4120 $$->name = $1;
4121 $$->expr = NULL;
4122 $$->opclass = $2;
4123 $$->ordering = $3;
4124 $$->nulls_ordering = $4;
4126 | func_expr opt_class opt_asc_desc opt_nulls_order
4128 $$ = makeNode(IndexElem);
4129 $$->name = NULL;
4130 $$->expr = $1;
4131 $$->opclass = $2;
4132 $$->ordering = $3;
4133 $$->nulls_ordering = $4;
4135 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4137 $$ = makeNode(IndexElem);
4138 $$->name = NULL;
4139 $$->expr = $2;
4140 $$->opclass = $4;
4141 $$->ordering = $5;
4142 $$->nulls_ordering = $6;
4146 opt_class: any_name { $$ = $1; }
4147 | USING any_name { $$ = $2; }
4148 | /*EMPTY*/ { $$ = NIL; }
4151 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4152 | DESC { $$ = SORTBY_DESC; }
4153 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4156 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4157 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4158 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4162 /*****************************************************************************
4164 * QUERY:
4165 * create [or replace] function <fname>
4166 * [(<type-1> { , <type-n>})]
4167 * returns <type-r>
4168 * as <filename or code in language as appropriate>
4169 * language <lang> [with parameters]
4171 *****************************************************************************/
4173 CreateFunctionStmt:
4174 CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4175 RETURNS func_return createfunc_opt_list opt_definition
4177 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4178 n->replace = $2;
4179 n->funcname = $4;
4180 n->parameters = $5;
4181 n->returnType = $7;
4182 n->options = $8;
4183 n->withClause = $9;
4184 $$ = (Node *)n;
4186 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4187 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4189 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4190 n->replace = $2;
4191 n->funcname = $4;
4192 n->parameters = mergeTableFuncParameters($5, $9);
4193 n->returnType = TableFuncTypeName($9);
4194 n->returnType->location = @7;
4195 n->options = $11;
4196 n->withClause = $12;
4197 $$ = (Node *)n;
4199 | CREATE opt_or_replace FUNCTION func_name func_args_with_defaults
4200 createfunc_opt_list opt_definition
4202 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4203 n->replace = $2;
4204 n->funcname = $4;
4205 n->parameters = $5;
4206 n->returnType = NULL;
4207 n->options = $6;
4208 n->withClause = $7;
4209 $$ = (Node *)n;
4213 opt_or_replace:
4214 OR REPLACE { $$ = TRUE; }
4215 | /*EMPTY*/ { $$ = FALSE; }
4218 func_args: '(' func_args_list ')' { $$ = $2; }
4219 | '(' ')' { $$ = NIL; }
4222 func_args_list:
4223 func_arg { $$ = list_make1($1); }
4224 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4228 * func_args_with_defaults is separate because we only want to accept
4229 * defaults in CREATE FUNCTION, not in ALTER etc.
4231 func_args_with_defaults:
4232 '(' func_args_with_defaults_list ')' { $$ = $2; }
4233 | '(' ')' { $$ = NIL; }
4236 func_args_with_defaults_list:
4237 func_arg_with_default { $$ = list_make1($1); }
4238 | func_args_with_defaults_list ',' func_arg_with_default
4239 { $$ = lappend($1, $3); }
4243 * The style with arg_class first is SQL99 standard, but Oracle puts
4244 * param_name first; accept both since it's likely people will try both
4245 * anyway. Don't bother trying to save productions by letting arg_class
4246 * have an empty alternative ... you'll get shift/reduce conflicts.
4248 * We can catch over-specified arguments here if we want to,
4249 * but for now better to silently swallow typmod, etc.
4250 * - thomas 2000-03-22
4252 func_arg:
4253 arg_class param_name func_type
4255 FunctionParameter *n = makeNode(FunctionParameter);
4256 n->name = $2;
4257 n->argType = $3;
4258 n->mode = $1;
4259 n->defexpr = NULL;
4260 $$ = n;
4262 | param_name arg_class func_type
4264 FunctionParameter *n = makeNode(FunctionParameter);
4265 n->name = $1;
4266 n->argType = $3;
4267 n->mode = $2;
4268 n->defexpr = NULL;
4269 $$ = n;
4271 | param_name func_type
4273 FunctionParameter *n = makeNode(FunctionParameter);
4274 n->name = $1;
4275 n->argType = $2;
4276 n->mode = FUNC_PARAM_IN;
4277 n->defexpr = NULL;
4278 $$ = n;
4280 | arg_class func_type
4282 FunctionParameter *n = makeNode(FunctionParameter);
4283 n->name = NULL;
4284 n->argType = $2;
4285 n->mode = $1;
4286 n->defexpr = NULL;
4287 $$ = n;
4289 | func_type
4291 FunctionParameter *n = makeNode(FunctionParameter);
4292 n->name = NULL;
4293 n->argType = $1;
4294 n->mode = FUNC_PARAM_IN;
4295 n->defexpr = NULL;
4296 $$ = n;
4300 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4301 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4302 | OUT_P { $$ = FUNC_PARAM_OUT; }
4303 | INOUT { $$ = FUNC_PARAM_INOUT; }
4304 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4305 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4309 * Ideally param_name should be ColId, but that causes too many conflicts.
4311 param_name: type_function_name
4314 func_return:
4315 func_type
4317 /* We can catch over-specified results here if we want to,
4318 * but for now better to silently swallow typmod, etc.
4319 * - thomas 2000-03-22
4321 $$ = $1;
4326 * We would like to make the %TYPE productions here be ColId attrs etc,
4327 * but that causes reduce/reduce conflicts. type_function_name
4328 * is next best choice.
4330 func_type: Typename { $$ = $1; }
4331 | type_function_name attrs '%' TYPE_P
4333 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4334 $$->pct_type = true;
4335 $$->location = @1;
4337 | SETOF type_function_name attrs '%' TYPE_P
4339 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4340 $$->pct_type = true;
4341 $$->setof = TRUE;
4342 $$->location = @2;
4346 func_arg_with_default:
4347 func_arg
4349 $$ = $1;
4351 | func_arg DEFAULT a_expr
4353 $$ = $1;
4354 $$->defexpr = $3;
4356 | func_arg '=' a_expr
4358 $$ = $1;
4359 $$->defexpr = $3;
4364 createfunc_opt_list:
4365 /* Must be at least one to prevent conflict */
4366 createfunc_opt_item { $$ = list_make1($1); }
4367 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4371 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4373 common_func_opt_item:
4374 CALLED ON NULL_P INPUT_P
4376 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4378 | RETURNS NULL_P ON NULL_P INPUT_P
4380 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4382 | STRICT_P
4384 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4386 | IMMUTABLE
4388 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4390 | STABLE
4392 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4394 | VOLATILE
4396 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4398 | EXTERNAL SECURITY DEFINER
4400 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4402 | EXTERNAL SECURITY INVOKER
4404 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4406 | SECURITY DEFINER
4408 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4410 | SECURITY INVOKER
4412 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4414 | COST NumericOnly
4416 $$ = makeDefElem("cost", (Node *)$2);
4418 | ROWS NumericOnly
4420 $$ = makeDefElem("rows", (Node *)$2);
4422 | SetResetClause
4424 /* we abuse the normal content of a DefElem here */
4425 $$ = makeDefElem("set", (Node *)$1);
4429 createfunc_opt_item:
4430 AS func_as
4432 $$ = makeDefElem("as", (Node *)$2);
4434 | LANGUAGE ColId_or_Sconst
4436 $$ = makeDefElem("language", (Node *)makeString($2));
4438 | common_func_opt_item
4440 $$ = $1;
4444 func_as: Sconst { $$ = list_make1(makeString($1)); }
4445 | Sconst ',' Sconst
4447 $$ = list_make2(makeString($1), makeString($3));
4451 opt_definition:
4452 WITH definition { $$ = $2; }
4453 | /*EMPTY*/ { $$ = NIL; }
4456 table_func_column: param_name func_type
4458 FunctionParameter *n = makeNode(FunctionParameter);
4459 n->name = $1;
4460 n->argType = $2;
4461 n->mode = FUNC_PARAM_TABLE;
4462 n->defexpr = NULL;
4463 $$ = n;
4467 table_func_column_list:
4468 table_func_column
4470 $$ = list_make1($1);
4472 | table_func_column_list ',' table_func_column
4474 $$ = lappend($1, $3);
4478 /*****************************************************************************
4479 * ALTER FUNCTION
4481 * RENAME and OWNER subcommands are already provided by the generic
4482 * ALTER infrastructure, here we just specify alterations that can
4483 * only be applied to functions.
4485 *****************************************************************************/
4486 AlterFunctionStmt:
4487 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4489 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4490 n->func = $3;
4491 n->actions = $4;
4492 $$ = (Node *) n;
4496 alterfunc_opt_list:
4497 /* At least one option must be specified */
4498 common_func_opt_item { $$ = list_make1($1); }
4499 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4502 /* Ignored, merely for SQL compliance */
4503 opt_restrict:
4504 RESTRICT
4505 | /* EMPTY */
4509 /*****************************************************************************
4511 * QUERY:
4513 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4514 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4515 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4517 *****************************************************************************/
4519 RemoveFuncStmt:
4520 DROP FUNCTION func_name func_args opt_drop_behavior
4522 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4523 n->kind = OBJECT_FUNCTION;
4524 n->name = $3;
4525 n->args = extractArgTypes($4);
4526 n->behavior = $5;
4527 n->missing_ok = false;
4528 $$ = (Node *)n;
4530 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4532 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4533 n->kind = OBJECT_FUNCTION;
4534 n->name = $5;
4535 n->args = extractArgTypes($6);
4536 n->behavior = $7;
4537 n->missing_ok = true;
4538 $$ = (Node *)n;
4542 RemoveAggrStmt:
4543 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4545 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4546 n->kind = OBJECT_AGGREGATE;
4547 n->name = $3;
4548 n->args = $4;
4549 n->behavior = $5;
4550 n->missing_ok = false;
4551 $$ = (Node *)n;
4553 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4555 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4556 n->kind = OBJECT_AGGREGATE;
4557 n->name = $5;
4558 n->args = $6;
4559 n->behavior = $7;
4560 n->missing_ok = true;
4561 $$ = (Node *)n;
4565 RemoveOperStmt:
4566 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
4568 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4569 n->kind = OBJECT_OPERATOR;
4570 n->name = $3;
4571 n->args = $4;
4572 n->behavior = $5;
4573 n->missing_ok = false;
4574 $$ = (Node *)n;
4576 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
4578 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4579 n->kind = OBJECT_OPERATOR;
4580 n->name = $5;
4581 n->args = $6;
4582 n->behavior = $7;
4583 n->missing_ok = true;
4584 $$ = (Node *)n;
4588 oper_argtypes:
4589 '(' Typename ')'
4591 ereport(ERROR,
4592 (errcode(ERRCODE_SYNTAX_ERROR),
4593 errmsg("missing argument"),
4594 errhint("Use NONE to denote the missing argument of a unary operator."),
4595 scanner_errposition(@3)));
4597 | '(' Typename ',' Typename ')'
4598 { $$ = list_make2($2, $4); }
4599 | '(' NONE ',' Typename ')' /* left unary */
4600 { $$ = list_make2(NULL, $4); }
4601 | '(' Typename ',' NONE ')' /* right unary */
4602 { $$ = list_make2($2, NULL); }
4605 any_operator:
4606 all_Op
4607 { $$ = list_make1(makeString($1)); }
4608 | ColId '.' any_operator
4609 { $$ = lcons(makeString($1), $3); }
4613 /*****************************************************************************
4615 * CREATE CAST / DROP CAST
4617 *****************************************************************************/
4619 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4620 WITH FUNCTION function_with_argtypes cast_context
4622 CreateCastStmt *n = makeNode(CreateCastStmt);
4623 n->sourcetype = $4;
4624 n->targettype = $6;
4625 n->func = $10;
4626 n->context = (CoercionContext) $11;
4627 n->inout = false;
4628 $$ = (Node *)n;
4630 | CREATE CAST '(' Typename AS Typename ')'
4631 WITHOUT FUNCTION cast_context
4633 CreateCastStmt *n = makeNode(CreateCastStmt);
4634 n->sourcetype = $4;
4635 n->targettype = $6;
4636 n->func = NULL;
4637 n->context = (CoercionContext) $10;
4638 n->inout = false;
4639 $$ = (Node *)n;
4641 | CREATE CAST '(' Typename AS Typename ')'
4642 WITH INOUT cast_context
4644 CreateCastStmt *n = makeNode(CreateCastStmt);
4645 n->sourcetype = $4;
4646 n->targettype = $6;
4647 n->func = NULL;
4648 n->context = (CoercionContext) $10;
4649 n->inout = true;
4650 $$ = (Node *)n;
4654 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
4655 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
4656 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
4660 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
4662 DropCastStmt *n = makeNode(DropCastStmt);
4663 n->sourcetype = $5;
4664 n->targettype = $7;
4665 n->behavior = $9;
4666 n->missing_ok = $3;
4667 $$ = (Node *)n;
4671 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
4672 | /*EMPTY*/ { $$ = FALSE; }
4676 /*****************************************************************************
4678 * QUERY:
4680 * REINDEX type <name> [FORCE]
4682 * FORCE no longer does anything, but we accept it for backwards compatibility
4683 *****************************************************************************/
4685 ReindexStmt:
4686 REINDEX reindex_type qualified_name opt_force
4688 ReindexStmt *n = makeNode(ReindexStmt);
4689 n->kind = $2;
4690 n->relation = $3;
4691 n->name = NULL;
4692 $$ = (Node *)n;
4694 | REINDEX SYSTEM_P name opt_force
4696 ReindexStmt *n = makeNode(ReindexStmt);
4697 n->kind = OBJECT_DATABASE;
4698 n->name = $3;
4699 n->relation = NULL;
4700 n->do_system = true;
4701 n->do_user = false;
4702 $$ = (Node *)n;
4704 | REINDEX DATABASE name opt_force
4706 ReindexStmt *n = makeNode(ReindexStmt);
4707 n->kind = OBJECT_DATABASE;
4708 n->name = $3;
4709 n->relation = NULL;
4710 n->do_system = true;
4711 n->do_user = true;
4712 $$ = (Node *)n;
4716 reindex_type:
4717 INDEX { $$ = OBJECT_INDEX; }
4718 | TABLE { $$ = OBJECT_TABLE; }
4721 opt_force: FORCE { $$ = TRUE; }
4722 | /* EMPTY */ { $$ = FALSE; }
4726 /*****************************************************************************
4728 * ALTER THING name RENAME TO newname
4730 *****************************************************************************/
4732 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
4734 RenameStmt *n = makeNode(RenameStmt);
4735 n->renameType = OBJECT_AGGREGATE;
4736 n->object = $3;
4737 n->objarg = $4;
4738 n->newname = $7;
4739 $$ = (Node *)n;
4741 | ALTER CONVERSION_P any_name RENAME TO name
4743 RenameStmt *n = makeNode(RenameStmt);
4744 n->renameType = OBJECT_CONVERSION;
4745 n->object = $3;
4746 n->newname = $6;
4747 $$ = (Node *)n;
4749 | ALTER DATABASE database_name RENAME TO database_name
4751 RenameStmt *n = makeNode(RenameStmt);
4752 n->renameType = OBJECT_DATABASE;
4753 n->subname = $3;
4754 n->newname = $6;
4755 $$ = (Node *)n;
4757 | ALTER FUNCTION function_with_argtypes RENAME TO name
4759 RenameStmt *n = makeNode(RenameStmt);
4760 n->renameType = OBJECT_FUNCTION;
4761 n->object = $3->funcname;
4762 n->objarg = $3->funcargs;
4763 n->newname = $6;
4764 $$ = (Node *)n;
4766 | ALTER GROUP_P RoleId RENAME TO RoleId
4768 RenameStmt *n = makeNode(RenameStmt);
4769 n->renameType = OBJECT_ROLE;
4770 n->subname = $3;
4771 n->newname = $6;
4772 $$ = (Node *)n;
4774 | ALTER opt_procedural LANGUAGE name RENAME TO name
4776 RenameStmt *n = makeNode(RenameStmt);
4777 n->renameType = OBJECT_LANGUAGE;
4778 n->subname = $4;
4779 n->newname = $7;
4780 $$ = (Node *)n;
4782 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
4784 RenameStmt *n = makeNode(RenameStmt);
4785 n->renameType = OBJECT_OPCLASS;
4786 n->object = $4;
4787 n->subname = $6;
4788 n->newname = $9;
4789 $$ = (Node *)n;
4791 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
4793 RenameStmt *n = makeNode(RenameStmt);
4794 n->renameType = OBJECT_OPFAMILY;
4795 n->object = $4;
4796 n->subname = $6;
4797 n->newname = $9;
4798 $$ = (Node *)n;
4800 | ALTER SCHEMA name RENAME TO name
4802 RenameStmt *n = makeNode(RenameStmt);
4803 n->renameType = OBJECT_SCHEMA;
4804 n->subname = $3;
4805 n->newname = $6;
4806 $$ = (Node *)n;
4808 | ALTER TABLE relation_expr RENAME TO name
4810 RenameStmt *n = makeNode(RenameStmt);
4811 n->renameType = OBJECT_TABLE;
4812 n->relation = $3;
4813 n->subname = NULL;
4814 n->newname = $6;
4815 $$ = (Node *)n;
4817 | ALTER SEQUENCE relation_expr RENAME TO name
4819 RenameStmt *n = makeNode(RenameStmt);
4820 n->renameType = OBJECT_SEQUENCE;
4821 n->relation = $3;
4822 n->subname = NULL;
4823 n->newname = $6;
4824 $$ = (Node *)n;
4826 | ALTER VIEW relation_expr RENAME TO name
4828 RenameStmt *n = makeNode(RenameStmt);
4829 n->renameType = OBJECT_VIEW;
4830 n->relation = $3;
4831 n->subname = NULL;
4832 n->newname = $6;
4833 $$ = (Node *)n;
4835 | ALTER INDEX relation_expr RENAME TO name
4837 RenameStmt *n = makeNode(RenameStmt);
4838 n->renameType = OBJECT_INDEX;
4839 n->relation = $3;
4840 n->subname = NULL;
4841 n->newname = $6;
4842 $$ = (Node *)n;
4844 | ALTER TABLE relation_expr RENAME opt_column name TO name
4846 RenameStmt *n = makeNode(RenameStmt);
4847 n->renameType = OBJECT_COLUMN;
4848 n->relation = $3;
4849 n->subname = $6;
4850 n->newname = $8;
4851 $$ = (Node *)n;
4853 | ALTER TRIGGER name ON relation_expr RENAME TO name
4855 RenameStmt *n = makeNode(RenameStmt);
4856 n->renameType = OBJECT_TRIGGER;
4857 n->relation = $5;
4858 n->subname = $3;
4859 n->newname = $8;
4860 $$ = (Node *)n;
4862 | ALTER ROLE RoleId RENAME TO RoleId
4864 RenameStmt *n = makeNode(RenameStmt);
4865 n->renameType = OBJECT_ROLE;
4866 n->subname = $3;
4867 n->newname = $6;
4868 $$ = (Node *)n;
4870 | ALTER USER RoleId RENAME TO RoleId
4872 RenameStmt *n = makeNode(RenameStmt);
4873 n->renameType = OBJECT_ROLE;
4874 n->subname = $3;
4875 n->newname = $6;
4876 $$ = (Node *)n;
4878 | ALTER TABLESPACE name RENAME TO name
4880 RenameStmt *n = makeNode(RenameStmt);
4881 n->renameType = OBJECT_TABLESPACE;
4882 n->subname = $3;
4883 n->newname = $6;
4884 $$ = (Node *)n;
4886 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
4888 RenameStmt *n = makeNode(RenameStmt);
4889 n->renameType = OBJECT_TSPARSER;
4890 n->object = $5;
4891 n->newname = $8;
4892 $$ = (Node *)n;
4894 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
4896 RenameStmt *n = makeNode(RenameStmt);
4897 n->renameType = OBJECT_TSDICTIONARY;
4898 n->object = $5;
4899 n->newname = $8;
4900 $$ = (Node *)n;
4902 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
4904 RenameStmt *n = makeNode(RenameStmt);
4905 n->renameType = OBJECT_TSTEMPLATE;
4906 n->object = $5;
4907 n->newname = $8;
4908 $$ = (Node *)n;
4910 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
4912 RenameStmt *n = makeNode(RenameStmt);
4913 n->renameType = OBJECT_TSCONFIGURATION;
4914 n->object = $5;
4915 n->newname = $8;
4916 $$ = (Node *)n;
4918 | ALTER TYPE_P any_name RENAME TO name
4920 RenameStmt *n = makeNode(RenameStmt);
4921 n->renameType = OBJECT_TYPE;
4922 n->object = $3;
4923 n->newname = $6;
4924 $$ = (Node *)n;
4928 opt_column: COLUMN { $$ = COLUMN; }
4929 | /*EMPTY*/ { $$ = 0; }
4932 opt_set_data: SET DATA_P { $$ = 1; }
4933 | /*EMPTY*/ { $$ = 0; }
4936 /*****************************************************************************
4938 * ALTER THING name SET SCHEMA name
4940 *****************************************************************************/
4942 AlterObjectSchemaStmt:
4943 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
4945 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4946 n->objectType = OBJECT_AGGREGATE;
4947 n->object = $3;
4948 n->objarg = $4;
4949 n->newschema = $7;
4950 $$ = (Node *)n;
4952 | ALTER DOMAIN_P any_name SET SCHEMA name
4954 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4955 n->objectType = OBJECT_DOMAIN;
4956 n->object = $3;
4957 n->newschema = $6;
4958 $$ = (Node *)n;
4960 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
4962 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4963 n->objectType = OBJECT_FUNCTION;
4964 n->object = $3->funcname;
4965 n->objarg = $3->funcargs;
4966 n->newschema = $6;
4967 $$ = (Node *)n;
4969 | ALTER TABLE relation_expr SET SCHEMA name
4971 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4972 n->objectType = OBJECT_TABLE;
4973 n->relation = $3;
4974 n->newschema = $6;
4975 $$ = (Node *)n;
4977 | ALTER SEQUENCE relation_expr SET SCHEMA name
4979 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4980 n->objectType = OBJECT_SEQUENCE;
4981 n->relation = $3;
4982 n->newschema = $6;
4983 $$ = (Node *)n;
4985 | ALTER VIEW relation_expr SET SCHEMA name
4987 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4988 n->objectType = OBJECT_VIEW;
4989 n->relation = $3;
4990 n->newschema = $6;
4991 $$ = (Node *)n;
4993 | ALTER TYPE_P any_name SET SCHEMA name
4995 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4996 n->objectType = OBJECT_TYPE;
4997 n->object = $3;
4998 n->newschema = $6;
4999 $$ = (Node *)n;
5003 /*****************************************************************************
5005 * ALTER THING name OWNER TO newname
5007 *****************************************************************************/
5009 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
5011 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5012 n->objectType = OBJECT_AGGREGATE;
5013 n->object = $3;
5014 n->objarg = $4;
5015 n->newowner = $7;
5016 $$ = (Node *)n;
5018 | ALTER CONVERSION_P any_name OWNER TO RoleId
5020 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5021 n->objectType = OBJECT_CONVERSION;
5022 n->object = $3;
5023 n->newowner = $6;
5024 $$ = (Node *)n;
5026 | ALTER DATABASE database_name OWNER TO RoleId
5028 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5029 n->objectType = OBJECT_DATABASE;
5030 n->object = list_make1(makeString($3));
5031 n->newowner = $6;
5032 $$ = (Node *)n;
5034 | ALTER DOMAIN_P any_name OWNER TO RoleId
5036 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5037 n->objectType = OBJECT_DOMAIN;
5038 n->object = $3;
5039 n->newowner = $6;
5040 $$ = (Node *)n;
5042 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5044 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5045 n->objectType = OBJECT_FUNCTION;
5046 n->object = $3->funcname;
5047 n->objarg = $3->funcargs;
5048 n->newowner = $6;
5049 $$ = (Node *)n;
5051 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5053 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5054 n->objectType = OBJECT_LANGUAGE;
5055 n->object = list_make1(makeString($4));
5056 n->newowner = $7;
5057 $$ = (Node *)n;
5059 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5061 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5062 n->objectType = OBJECT_OPERATOR;
5063 n->object = $3;
5064 n->objarg = $4;
5065 n->newowner = $7;
5066 $$ = (Node *)n;
5068 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5070 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5071 n->objectType = OBJECT_OPCLASS;
5072 n->object = $4;
5073 n->addname = $6;
5074 n->newowner = $9;
5075 $$ = (Node *)n;
5077 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5079 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5080 n->objectType = OBJECT_OPFAMILY;
5081 n->object = $4;
5082 n->addname = $6;
5083 n->newowner = $9;
5084 $$ = (Node *)n;
5086 | ALTER SCHEMA name OWNER TO RoleId
5088 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5089 n->objectType = OBJECT_SCHEMA;
5090 n->object = list_make1(makeString($3));
5091 n->newowner = $6;
5092 $$ = (Node *)n;
5094 | ALTER TYPE_P any_name OWNER TO RoleId
5096 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5097 n->objectType = OBJECT_TYPE;
5098 n->object = $3;
5099 n->newowner = $6;
5100 $$ = (Node *)n;
5102 | ALTER TABLESPACE name OWNER TO RoleId
5104 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5105 n->objectType = OBJECT_TABLESPACE;
5106 n->object = list_make1(makeString($3));
5107 n->newowner = $6;
5108 $$ = (Node *)n;
5110 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5112 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5113 n->objectType = OBJECT_TSDICTIONARY;
5114 n->object = $5;
5115 n->newowner = $8;
5116 $$ = (Node *)n;
5118 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5120 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5121 n->objectType = OBJECT_TSCONFIGURATION;
5122 n->object = $5;
5123 n->newowner = $8;
5124 $$ = (Node *)n;
5129 /*****************************************************************************
5131 * QUERY: Define Rewrite Rule
5133 *****************************************************************************/
5135 RuleStmt: CREATE opt_or_replace RULE name AS
5136 { QueryIsRule=TRUE; }
5137 ON event TO qualified_name where_clause
5138 DO opt_instead RuleActionList
5140 RuleStmt *n = makeNode(RuleStmt);
5141 n->replace = $2;
5142 n->relation = $10;
5143 n->rulename = $4;
5144 n->whereClause = $11;
5145 n->event = $8;
5146 n->instead = $13;
5147 n->actions = $14;
5148 $$ = (Node *)n;
5149 QueryIsRule=FALSE;
5153 RuleActionList:
5154 NOTHING { $$ = NIL; }
5155 | RuleActionStmt { $$ = list_make1($1); }
5156 | '(' RuleActionMulti ')' { $$ = $2; }
5159 /* the thrashing around here is to discard "empty" statements... */
5160 RuleActionMulti:
5161 RuleActionMulti ';' RuleActionStmtOrEmpty
5162 { if ($3 != NULL)
5163 $$ = lappend($1, $3);
5164 else
5165 $$ = $1;
5167 | RuleActionStmtOrEmpty
5168 { if ($1 != NULL)
5169 $$ = list_make1($1);
5170 else
5171 $$ = NIL;
5175 RuleActionStmt:
5176 SelectStmt
5177 | InsertStmt
5178 | UpdateStmt
5179 | DeleteStmt
5180 | NotifyStmt
5183 RuleActionStmtOrEmpty:
5184 RuleActionStmt { $$ = $1; }
5185 | /*EMPTY*/ { $$ = NULL; }
5188 event: SELECT { $$ = CMD_SELECT; }
5189 | UPDATE { $$ = CMD_UPDATE; }
5190 | DELETE_P { $$ = CMD_DELETE; }
5191 | INSERT { $$ = CMD_INSERT; }
5194 opt_instead:
5195 INSTEAD { $$ = TRUE; }
5196 | ALSO { $$ = FALSE; }
5197 | /*EMPTY*/ { $$ = FALSE; }
5201 DropRuleStmt:
5202 DROP RULE name ON qualified_name opt_drop_behavior
5204 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5205 n->relation = $5;
5206 n->property = $3;
5207 n->behavior = $6;
5208 n->removeType = OBJECT_RULE;
5209 n->missing_ok = false;
5210 $$ = (Node *) n;
5212 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5214 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5215 n->relation = $7;
5216 n->property = $5;
5217 n->behavior = $8;
5218 n->removeType = OBJECT_RULE;
5219 n->missing_ok = true;
5220 $$ = (Node *) n;
5225 /*****************************************************************************
5227 * QUERY:
5228 * NOTIFY <identifier> can appear both in rule bodies and
5229 * as a query-level command
5231 *****************************************************************************/
5233 NotifyStmt: NOTIFY ColId
5235 NotifyStmt *n = makeNode(NotifyStmt);
5236 n->conditionname = $2;
5237 $$ = (Node *)n;
5241 ListenStmt: LISTEN ColId
5243 ListenStmt *n = makeNode(ListenStmt);
5244 n->conditionname = $2;
5245 $$ = (Node *)n;
5249 UnlistenStmt:
5250 UNLISTEN ColId
5252 UnlistenStmt *n = makeNode(UnlistenStmt);
5253 n->conditionname = $2;
5254 $$ = (Node *)n;
5256 | UNLISTEN '*'
5258 UnlistenStmt *n = makeNode(UnlistenStmt);
5259 n->conditionname = NULL;
5260 $$ = (Node *)n;
5265 /*****************************************************************************
5267 * Transactions:
5269 * BEGIN / COMMIT / ROLLBACK
5270 * (also older versions END / ABORT)
5272 *****************************************************************************/
5274 TransactionStmt:
5275 ABORT_P opt_transaction
5277 TransactionStmt *n = makeNode(TransactionStmt);
5278 n->kind = TRANS_STMT_ROLLBACK;
5279 n->options = NIL;
5280 $$ = (Node *)n;
5282 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5284 TransactionStmt *n = makeNode(TransactionStmt);
5285 n->kind = TRANS_STMT_BEGIN;
5286 n->options = $3;
5287 $$ = (Node *)n;
5289 | START TRANSACTION transaction_mode_list_or_empty
5291 TransactionStmt *n = makeNode(TransactionStmt);
5292 n->kind = TRANS_STMT_START;
5293 n->options = $3;
5294 $$ = (Node *)n;
5296 | COMMIT opt_transaction
5298 TransactionStmt *n = makeNode(TransactionStmt);
5299 n->kind = TRANS_STMT_COMMIT;
5300 n->options = NIL;
5301 $$ = (Node *)n;
5303 | END_P opt_transaction
5305 TransactionStmt *n = makeNode(TransactionStmt);
5306 n->kind = TRANS_STMT_COMMIT;
5307 n->options = NIL;
5308 $$ = (Node *)n;
5310 | ROLLBACK opt_transaction
5312 TransactionStmt *n = makeNode(TransactionStmt);
5313 n->kind = TRANS_STMT_ROLLBACK;
5314 n->options = NIL;
5315 $$ = (Node *)n;
5317 | SAVEPOINT ColId
5319 TransactionStmt *n = makeNode(TransactionStmt);
5320 n->kind = TRANS_STMT_SAVEPOINT;
5321 n->options = list_make1(makeDefElem("savepoint_name",
5322 (Node *)makeString($2)));
5323 $$ = (Node *)n;
5325 | RELEASE SAVEPOINT ColId
5327 TransactionStmt *n = makeNode(TransactionStmt);
5328 n->kind = TRANS_STMT_RELEASE;
5329 n->options = list_make1(makeDefElem("savepoint_name",
5330 (Node *)makeString($3)));
5331 $$ = (Node *)n;
5333 | RELEASE ColId
5335 TransactionStmt *n = makeNode(TransactionStmt);
5336 n->kind = TRANS_STMT_RELEASE;
5337 n->options = list_make1(makeDefElem("savepoint_name",
5338 (Node *)makeString($2)));
5339 $$ = (Node *)n;
5341 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5343 TransactionStmt *n = makeNode(TransactionStmt);
5344 n->kind = TRANS_STMT_ROLLBACK_TO;
5345 n->options = list_make1(makeDefElem("savepoint_name",
5346 (Node *)makeString($5)));
5347 $$ = (Node *)n;
5349 | ROLLBACK opt_transaction TO ColId
5351 TransactionStmt *n = makeNode(TransactionStmt);
5352 n->kind = TRANS_STMT_ROLLBACK_TO;
5353 n->options = list_make1(makeDefElem("savepoint_name",
5354 (Node *)makeString($4)));
5355 $$ = (Node *)n;
5357 | PREPARE TRANSACTION Sconst
5359 TransactionStmt *n = makeNode(TransactionStmt);
5360 n->kind = TRANS_STMT_PREPARE;
5361 n->gid = $3;
5362 $$ = (Node *)n;
5364 | COMMIT PREPARED Sconst
5366 TransactionStmt *n = makeNode(TransactionStmt);
5367 n->kind = TRANS_STMT_COMMIT_PREPARED;
5368 n->gid = $3;
5369 $$ = (Node *)n;
5371 | ROLLBACK PREPARED Sconst
5373 TransactionStmt *n = makeNode(TransactionStmt);
5374 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5375 n->gid = $3;
5376 $$ = (Node *)n;
5380 opt_transaction: WORK {}
5381 | TRANSACTION {}
5382 | /*EMPTY*/ {}
5385 transaction_mode_item:
5386 ISOLATION LEVEL iso_level
5387 { $$ = makeDefElem("transaction_isolation",
5388 makeStringConst($3, @3)); }
5389 | READ ONLY
5390 { $$ = makeDefElem("transaction_read_only",
5391 makeIntConst(TRUE, @1)); }
5392 | READ WRITE
5393 { $$ = makeDefElem("transaction_read_only",
5394 makeIntConst(FALSE, @1)); }
5397 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5398 transaction_mode_list:
5399 transaction_mode_item
5400 { $$ = list_make1($1); }
5401 | transaction_mode_list ',' transaction_mode_item
5402 { $$ = lappend($1, $3); }
5403 | transaction_mode_list transaction_mode_item
5404 { $$ = lappend($1, $2); }
5407 transaction_mode_list_or_empty:
5408 transaction_mode_list
5409 | /* EMPTY */
5410 { $$ = NIL; }
5414 /*****************************************************************************
5416 * QUERY:
5417 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5418 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5420 *****************************************************************************/
5422 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5423 AS SelectStmt opt_check_option
5425 ViewStmt *n = makeNode(ViewStmt);
5426 n->view = $4;
5427 n->view->istemp = $2;
5428 n->aliases = $5;
5429 n->query = $7;
5430 n->replace = false;
5431 $$ = (Node *) n;
5433 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5434 AS SelectStmt opt_check_option
5436 ViewStmt *n = makeNode(ViewStmt);
5437 n->view = $6;
5438 n->view->istemp = $4;
5439 n->aliases = $7;
5440 n->query = $9;
5441 n->replace = true;
5442 $$ = (Node *) n;
5446 opt_check_option:
5447 WITH CHECK OPTION
5449 ereport(ERROR,
5450 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5451 errmsg("WITH CHECK OPTION is not implemented")));
5453 | WITH CASCADED CHECK OPTION
5455 ereport(ERROR,
5456 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5457 errmsg("WITH CHECK OPTION is not implemented")));
5459 | WITH LOCAL CHECK OPTION
5461 ereport(ERROR,
5462 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5463 errmsg("WITH CHECK OPTION is not implemented")));
5465 | /* EMPTY */ { $$ = NIL; }
5468 /*****************************************************************************
5470 * QUERY:
5471 * LOAD "filename"
5473 *****************************************************************************/
5475 LoadStmt: LOAD file_name
5477 LoadStmt *n = makeNode(LoadStmt);
5478 n->filename = $2;
5479 $$ = (Node *)n;
5484 /*****************************************************************************
5486 * CREATE DATABASE
5488 *****************************************************************************/
5490 CreatedbStmt:
5491 CREATE DATABASE database_name opt_with createdb_opt_list
5493 CreatedbStmt *n = makeNode(CreatedbStmt);
5494 n->dbname = $3;
5495 n->options = $5;
5496 $$ = (Node *)n;
5500 createdb_opt_list:
5501 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5502 | /* EMPTY */ { $$ = NIL; }
5505 createdb_opt_item:
5506 TABLESPACE opt_equal name
5508 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5510 | TABLESPACE opt_equal DEFAULT
5512 $$ = makeDefElem("tablespace", NULL);
5514 | LOCATION opt_equal Sconst
5516 $$ = makeDefElem("location", (Node *)makeString($3));
5518 | LOCATION opt_equal DEFAULT
5520 $$ = makeDefElem("location", NULL);
5522 | TEMPLATE opt_equal name
5524 $$ = makeDefElem("template", (Node *)makeString($3));
5526 | TEMPLATE opt_equal DEFAULT
5528 $$ = makeDefElem("template", NULL);
5530 | ENCODING opt_equal Sconst
5532 $$ = makeDefElem("encoding", (Node *)makeString($3));
5534 | ENCODING opt_equal Iconst
5536 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5538 | ENCODING opt_equal DEFAULT
5540 $$ = makeDefElem("encoding", NULL);
5542 | COLLATE opt_equal Sconst
5544 $$ = makeDefElem("collate", (Node *)makeString($3));
5546 | COLLATE opt_equal DEFAULT
5548 $$ = makeDefElem("collate", NULL);
5550 | CTYPE opt_equal Sconst
5552 $$ = makeDefElem("ctype", (Node *)makeString($3));
5554 | CTYPE opt_equal DEFAULT
5556 $$ = makeDefElem("ctype", NULL);
5558 | CONNECTION LIMIT opt_equal SignedIconst
5560 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5562 | OWNER opt_equal name
5564 $$ = makeDefElem("owner", (Node *)makeString($3));
5566 | OWNER opt_equal DEFAULT
5568 $$ = makeDefElem("owner", NULL);
5573 * Though the equals sign doesn't match other WITH options, pg_dump uses
5574 * equals for backward compatibility, and it doesn't seem worth removing it.
5576 opt_equal: '=' {}
5577 | /*EMPTY*/ {}
5581 /*****************************************************************************
5583 * ALTER DATABASE
5585 *****************************************************************************/
5587 AlterDatabaseStmt:
5588 ALTER DATABASE database_name opt_with alterdb_opt_list
5590 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5591 n->dbname = $3;
5592 n->options = $5;
5593 $$ = (Node *)n;
5595 | ALTER DATABASE database_name SET TABLESPACE name
5597 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5598 n->dbname = $3;
5599 n->options = list_make1(makeDefElem("tablespace",
5600 (Node *)makeString($6)));
5601 $$ = (Node *)n;
5605 AlterDatabaseSetStmt:
5606 ALTER DATABASE database_name SetResetClause
5608 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5609 n->dbname = $3;
5610 n->setstmt = $4;
5611 $$ = (Node *)n;
5616 alterdb_opt_list:
5617 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5618 | /* EMPTY */ { $$ = NIL; }
5621 alterdb_opt_item:
5622 CONNECTION LIMIT opt_equal SignedIconst
5624 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5629 /*****************************************************************************
5631 * DROP DATABASE [ IF EXISTS ]
5633 * This is implicitly CASCADE, no need for drop behavior
5634 *****************************************************************************/
5636 DropdbStmt: DROP DATABASE database_name
5638 DropdbStmt *n = makeNode(DropdbStmt);
5639 n->dbname = $3;
5640 n->missing_ok = FALSE;
5641 $$ = (Node *)n;
5643 | DROP DATABASE IF_P EXISTS database_name
5645 DropdbStmt *n = makeNode(DropdbStmt);
5646 n->dbname = $5;
5647 n->missing_ok = TRUE;
5648 $$ = (Node *)n;
5653 /*****************************************************************************
5655 * Manipulate a domain
5657 *****************************************************************************/
5659 CreateDomainStmt:
5660 CREATE DOMAIN_P any_name opt_as Typename ColQualList
5662 CreateDomainStmt *n = makeNode(CreateDomainStmt);
5663 n->domainname = $3;
5664 n->typename = $5;
5665 n->constraints = $6;
5666 $$ = (Node *)n;
5670 AlterDomainStmt:
5671 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
5672 ALTER DOMAIN_P any_name alter_column_default
5674 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5675 n->subtype = 'T';
5676 n->typename = $3;
5677 n->def = $4;
5678 $$ = (Node *)n;
5680 /* ALTER DOMAIN <domain> DROP NOT NULL */
5681 | ALTER DOMAIN_P any_name DROP NOT NULL_P
5683 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5684 n->subtype = 'N';
5685 n->typename = $3;
5686 $$ = (Node *)n;
5688 /* ALTER DOMAIN <domain> SET NOT NULL */
5689 | ALTER DOMAIN_P any_name SET NOT NULL_P
5691 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5692 n->subtype = 'O';
5693 n->typename = $3;
5694 $$ = (Node *)n;
5696 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
5697 | ALTER DOMAIN_P any_name ADD_P TableConstraint
5699 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5700 n->subtype = 'C';
5701 n->typename = $3;
5702 n->def = $5;
5703 $$ = (Node *)n;
5705 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
5706 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
5708 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5709 n->subtype = 'X';
5710 n->typename = $3;
5711 n->name = $6;
5712 n->behavior = $7;
5713 $$ = (Node *)n;
5717 opt_as: AS {}
5718 | /* EMPTY */ {}
5722 /*****************************************************************************
5724 * Manipulate a text search dictionary or configuration
5726 *****************************************************************************/
5728 AlterTSDictionaryStmt:
5729 ALTER TEXT_P SEARCH DICTIONARY any_name definition
5731 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
5732 n->dictname = $5;
5733 n->options = $6;
5734 $$ = (Node *)n;
5738 AlterTSConfigurationStmt:
5739 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
5741 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5742 n->cfgname = $5;
5743 n->tokentype = $9;
5744 n->dicts = $11;
5745 n->override = false;
5746 n->replace = false;
5747 $$ = (Node*)n;
5749 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
5751 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5752 n->cfgname = $5;
5753 n->tokentype = $9;
5754 n->dicts = $11;
5755 n->override = true;
5756 n->replace = false;
5757 $$ = (Node*)n;
5759 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
5761 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5762 n->cfgname = $5;
5763 n->tokentype = NIL;
5764 n->dicts = list_make2($9,$11);
5765 n->override = false;
5766 n->replace = true;
5767 $$ = (Node*)n;
5769 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
5771 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5772 n->cfgname = $5;
5773 n->tokentype = $9;
5774 n->dicts = list_make2($11,$13);
5775 n->override = false;
5776 n->replace = true;
5777 $$ = (Node*)n;
5779 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
5781 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5782 n->cfgname = $5;
5783 n->tokentype = $9;
5784 n->missing_ok = false;
5785 $$ = (Node*)n;
5787 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
5789 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5790 n->cfgname = $5;
5791 n->tokentype = $11;
5792 n->missing_ok = true;
5793 $$ = (Node*)n;
5798 /*****************************************************************************
5800 * Manipulate a conversion
5802 * CREATE [DEFAULT] CONVERSION <conversion_name>
5803 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
5805 *****************************************************************************/
5807 CreateConversionStmt:
5808 CREATE opt_default CONVERSION_P any_name FOR Sconst
5809 TO Sconst FROM any_name
5811 CreateConversionStmt *n = makeNode(CreateConversionStmt);
5812 n->conversion_name = $4;
5813 n->for_encoding_name = $6;
5814 n->to_encoding_name = $8;
5815 n->func_name = $10;
5816 n->def = $2;
5817 $$ = (Node *)n;
5821 /*****************************************************************************
5823 * QUERY:
5824 * CLUSTER [VERBOSE] <qualified_name> [ USING <index_name> ]
5825 * CLUSTER [VERBOSE]
5826 * CLUSTER [VERBOSE] <index_name> ON <qualified_name> (for pre-8.3)
5828 *****************************************************************************/
5830 ClusterStmt:
5831 CLUSTER opt_verbose qualified_name cluster_index_specification
5833 ClusterStmt *n = makeNode(ClusterStmt);
5834 n->relation = $3;
5835 n->indexname = $4;
5836 n->verbose = $2;
5837 $$ = (Node*)n;
5839 | CLUSTER opt_verbose
5841 ClusterStmt *n = makeNode(ClusterStmt);
5842 n->relation = NULL;
5843 n->indexname = NULL;
5844 n->verbose = $2;
5845 $$ = (Node*)n;
5847 /* kept for pre-8.3 compatibility */
5848 | CLUSTER opt_verbose index_name ON qualified_name
5850 ClusterStmt *n = makeNode(ClusterStmt);
5851 n->relation = $5;
5852 n->indexname = $3;
5853 n->verbose = $2;
5854 $$ = (Node*)n;
5858 cluster_index_specification:
5859 USING index_name { $$ = $2; }
5860 | /*EMPTY*/ { $$ = NULL; }
5864 /*****************************************************************************
5866 * QUERY:
5867 * VACUUM
5868 * ANALYZE
5870 *****************************************************************************/
5872 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
5874 VacuumStmt *n = makeNode(VacuumStmt);
5875 n->vacuum = true;
5876 n->analyze = false;
5877 n->full = $2;
5878 n->freeze_min_age = $3 ? 0 : -1;
5879 n->scan_all = $2 || $3;
5880 n->verbose = $4;
5881 n->relation = NULL;
5882 n->va_cols = NIL;
5883 $$ = (Node *)n;
5885 | VACUUM opt_full opt_freeze opt_verbose qualified_name
5887 VacuumStmt *n = makeNode(VacuumStmt);
5888 n->vacuum = true;
5889 n->analyze = false;
5890 n->full = $2;
5891 n->freeze_min_age = $3 ? 0 : -1;
5892 n->scan_all = $2 || $3;
5893 n->verbose = $4;
5894 n->relation = $5;
5895 n->va_cols = NIL;
5896 $$ = (Node *)n;
5898 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
5900 VacuumStmt *n = (VacuumStmt *) $5;
5901 n->vacuum = true;
5902 n->full = $2;
5903 n->freeze_min_age = $3 ? 0 : -1;
5904 n->scan_all = $2 || $3;
5905 n->verbose |= $4;
5906 $$ = (Node *)n;
5910 AnalyzeStmt:
5911 analyze_keyword opt_verbose
5913 VacuumStmt *n = makeNode(VacuumStmt);
5914 n->vacuum = false;
5915 n->analyze = true;
5916 n->full = false;
5917 n->freeze_min_age = -1;
5918 n->verbose = $2;
5919 n->relation = NULL;
5920 n->va_cols = NIL;
5921 $$ = (Node *)n;
5923 | analyze_keyword opt_verbose qualified_name opt_name_list
5925 VacuumStmt *n = makeNode(VacuumStmt);
5926 n->vacuum = false;
5927 n->analyze = true;
5928 n->full = false;
5929 n->freeze_min_age = -1;
5930 n->verbose = $2;
5931 n->relation = $3;
5932 n->va_cols = $4;
5933 $$ = (Node *)n;
5937 analyze_keyword:
5938 ANALYZE {}
5939 | ANALYSE /* British */ {}
5942 opt_verbose:
5943 VERBOSE { $$ = TRUE; }
5944 | /*EMPTY*/ { $$ = FALSE; }
5947 opt_full: FULL { $$ = TRUE; }
5948 | /*EMPTY*/ { $$ = FALSE; }
5951 opt_freeze: FREEZE { $$ = TRUE; }
5952 | /*EMPTY*/ { $$ = FALSE; }
5955 opt_name_list:
5956 '(' name_list ')' { $$ = $2; }
5957 | /*EMPTY*/ { $$ = NIL; }
5961 /*****************************************************************************
5963 * QUERY:
5964 * EXPLAIN [ANALYZE] [VERBOSE] query
5966 *****************************************************************************/
5968 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
5970 ExplainStmt *n = makeNode(ExplainStmt);
5971 n->analyze = $2;
5972 n->verbose = $3;
5973 n->query = $4;
5974 $$ = (Node *)n;
5978 ExplainableStmt:
5979 SelectStmt
5980 | InsertStmt
5981 | UpdateStmt
5982 | DeleteStmt
5983 | DeclareCursorStmt
5984 | CreateAsStmt
5985 | ExecuteStmt /* by default all are $$=$1 */
5988 opt_analyze:
5989 analyze_keyword { $$ = TRUE; }
5990 | /* EMPTY */ { $$ = FALSE; }
5993 /*****************************************************************************
5995 * QUERY:
5996 * PREPARE <plan_name> [(args, ...)] AS <query>
5998 *****************************************************************************/
6000 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
6002 PrepareStmt *n = makeNode(PrepareStmt);
6003 n->name = $2;
6004 n->argtypes = $3;
6005 n->query = $5;
6006 $$ = (Node *) n;
6010 prep_type_clause: '(' type_list ')' { $$ = $2; }
6011 | /* EMPTY */ { $$ = NIL; }
6014 PreparableStmt:
6015 SelectStmt
6016 | InsertStmt
6017 | UpdateStmt
6018 | DeleteStmt /* by default all are $$=$1 */
6021 /*****************************************************************************
6023 * EXECUTE <plan_name> [(params, ...)]
6024 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
6026 *****************************************************************************/
6028 ExecuteStmt: EXECUTE name execute_param_clause
6030 ExecuteStmt *n = makeNode(ExecuteStmt);
6031 n->name = $2;
6032 n->params = $3;
6033 n->into = NULL;
6034 $$ = (Node *) n;
6036 | CREATE OptTemp TABLE create_as_target AS
6037 EXECUTE name execute_param_clause
6039 ExecuteStmt *n = makeNode(ExecuteStmt);
6040 n->name = $7;
6041 n->params = $8;
6042 $4->rel->istemp = $2;
6043 n->into = $4;
6044 if ($4->colNames)
6045 ereport(ERROR,
6046 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
6047 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6048 /* ... because it's not implemented, but it could be */
6049 $$ = (Node *) n;
6053 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6054 | /* EMPTY */ { $$ = NIL; }
6057 /*****************************************************************************
6059 * QUERY:
6060 * DEALLOCATE [PREPARE] <plan_name>
6062 *****************************************************************************/
6064 DeallocateStmt: DEALLOCATE name
6066 DeallocateStmt *n = makeNode(DeallocateStmt);
6067 n->name = $2;
6068 $$ = (Node *) n;
6070 | DEALLOCATE PREPARE name
6072 DeallocateStmt *n = makeNode(DeallocateStmt);
6073 n->name = $3;
6074 $$ = (Node *) n;
6076 | DEALLOCATE ALL
6078 DeallocateStmt *n = makeNode(DeallocateStmt);
6079 n->name = NULL;
6080 $$ = (Node *) n;
6082 | DEALLOCATE PREPARE ALL
6084 DeallocateStmt *n = makeNode(DeallocateStmt);
6085 n->name = NULL;
6086 $$ = (Node *) n;
6090 /*****************************************************************************
6092 * QUERY:
6093 * INSERT STATEMENTS
6095 *****************************************************************************/
6097 InsertStmt:
6098 INSERT INTO qualified_name insert_rest returning_clause
6100 $4->relation = $3;
6101 $4->returningList = $5;
6102 $$ = (Node *) $4;
6106 insert_rest:
6107 SelectStmt
6109 $$ = makeNode(InsertStmt);
6110 $$->cols = NIL;
6111 $$->selectStmt = $1;
6113 | '(' insert_column_list ')' SelectStmt
6115 $$ = makeNode(InsertStmt);
6116 $$->cols = $2;
6117 $$->selectStmt = $4;
6119 | DEFAULT VALUES
6121 $$ = makeNode(InsertStmt);
6122 $$->cols = NIL;
6123 $$->selectStmt = NULL;
6127 insert_column_list:
6128 insert_column_item
6129 { $$ = list_make1($1); }
6130 | insert_column_list ',' insert_column_item
6131 { $$ = lappend($1, $3); }
6134 insert_column_item:
6135 ColId opt_indirection
6137 $$ = makeNode(ResTarget);
6138 $$->name = $1;
6139 $$->indirection = check_indirection($2);
6140 $$->val = NULL;
6141 $$->location = @1;
6145 returning_clause:
6146 RETURNING target_list { $$ = $2; }
6147 | /* EMPTY */ { $$ = NIL; }
6151 /*****************************************************************************
6153 * QUERY:
6154 * DELETE STATEMENTS
6156 *****************************************************************************/
6158 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6159 using_clause where_or_current_clause returning_clause
6161 DeleteStmt *n = makeNode(DeleteStmt);
6162 n->relation = $3;
6163 n->usingClause = $4;
6164 n->whereClause = $5;
6165 n->returningList = $6;
6166 $$ = (Node *)n;
6170 using_clause:
6171 USING from_list { $$ = $2; }
6172 | /*EMPTY*/ { $$ = NIL; }
6175 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
6177 LockStmt *n = makeNode(LockStmt);
6179 n->relations = $3;
6180 n->mode = $4;
6181 n->nowait = $5;
6182 $$ = (Node *)n;
6186 opt_lock: IN_P lock_type MODE { $$ = $2; }
6187 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6190 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6191 | ROW SHARE { $$ = RowShareLock; }
6192 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6193 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6194 | SHARE { $$ = ShareLock; }
6195 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6196 | EXCLUSIVE { $$ = ExclusiveLock; }
6197 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6200 opt_nowait: NOWAIT { $$ = TRUE; }
6201 | /*EMPTY*/ { $$ = FALSE; }
6205 /*****************************************************************************
6207 * QUERY:
6208 * UpdateStmt (UPDATE)
6210 *****************************************************************************/
6212 UpdateStmt: UPDATE relation_expr_opt_alias
6213 SET set_clause_list
6214 from_clause
6215 where_or_current_clause
6216 returning_clause
6218 UpdateStmt *n = makeNode(UpdateStmt);
6219 n->relation = $2;
6220 n->targetList = $4;
6221 n->fromClause = $5;
6222 n->whereClause = $6;
6223 n->returningList = $7;
6224 $$ = (Node *)n;
6228 set_clause_list:
6229 set_clause { $$ = $1; }
6230 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6233 set_clause:
6234 single_set_clause { $$ = list_make1($1); }
6235 | multiple_set_clause { $$ = $1; }
6238 single_set_clause:
6239 set_target '=' ctext_expr
6241 $$ = $1;
6242 $$->val = (Node *) $3;
6246 multiple_set_clause:
6247 '(' set_target_list ')' '=' ctext_row
6249 ListCell *col_cell;
6250 ListCell *val_cell;
6253 * Break the ctext_row apart, merge individual expressions
6254 * into the destination ResTargets. XXX this approach
6255 * cannot work for general row expressions as sources.
6257 if (list_length($2) != list_length($5))
6258 ereport(ERROR,
6259 (errcode(ERRCODE_SYNTAX_ERROR),
6260 errmsg("number of columns does not match number of values"),
6261 scanner_errposition(@1)));
6262 forboth(col_cell, $2, val_cell, $5)
6264 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6265 Node *res_val = (Node *) lfirst(val_cell);
6267 res_col->val = res_val;
6270 $$ = $2;
6274 set_target:
6275 ColId opt_indirection
6277 $$ = makeNode(ResTarget);
6278 $$->name = $1;
6279 $$->indirection = check_indirection($2);
6280 $$->val = NULL; /* upper production sets this */
6281 $$->location = @1;
6285 set_target_list:
6286 set_target { $$ = list_make1($1); }
6287 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6291 /*****************************************************************************
6293 * QUERY:
6294 * CURSOR STATEMENTS
6296 *****************************************************************************/
6297 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6299 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6300 n->portalname = $2;
6301 /* currently we always set FAST_PLAN option */
6302 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6303 n->query = $7;
6304 $$ = (Node *)n;
6308 cursor_options: /*EMPTY*/ { $$ = 0; }
6309 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6310 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6311 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6312 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6315 opt_hold: /* EMPTY */ { $$ = 0; }
6316 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6317 | WITHOUT HOLD { $$ = 0; }
6320 /*****************************************************************************
6322 * QUERY:
6323 * SELECT STATEMENTS
6325 *****************************************************************************/
6327 /* A complete SELECT statement looks like this.
6329 * The rule returns either a single SelectStmt node or a tree of them,
6330 * representing a set-operation tree.
6332 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6333 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6334 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6335 * To resolve the ambiguity, we are careful to define the grammar so that
6336 * the decision is staved off as long as possible: as long as we can keep
6337 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6338 * it's no longer possible to do that will we decide that parens belong to
6339 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6340 * parentheses are treated as part of the sub-select. The necessity of doing
6341 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6342 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6343 * SELECT viewpoint when we see the UNION.
6345 * This approach is implemented by defining a nonterminal select_with_parens,
6346 * which represents a SELECT with at least one outer layer of parentheses,
6347 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6348 * in the expression grammar. We will then have shift-reduce conflicts
6349 * which we can resolve in favor of always treating '(' <select> ')' as
6350 * a select_with_parens. To resolve the conflicts, the productions that
6351 * conflict with the select_with_parens productions are manually given
6352 * precedences lower than the precedence of ')', thereby ensuring that we
6353 * shift ')' (and then reduce to select_with_parens) rather than trying to
6354 * reduce the inner <select> nonterminal to something else. We use UMINUS
6355 * precedence for this, which is a fairly arbitrary choice.
6357 * To be able to define select_with_parens itself without ambiguity, we need
6358 * a nonterminal select_no_parens that represents a SELECT structure with no
6359 * outermost parentheses. This is a little bit tedious, but it works.
6361 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6362 * with or without outer parentheses.
6365 SelectStmt: select_no_parens %prec UMINUS
6366 | select_with_parens %prec UMINUS
6369 select_with_parens:
6370 '(' select_no_parens ')' { $$ = $2; }
6371 | '(' select_with_parens ')' { $$ = $2; }
6375 * This rule parses the equivalent of the standard's <query expression>.
6376 * The duplicative productions are annoying, but hard to get rid of without
6377 * creating shift/reduce conflicts.
6379 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6380 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6381 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6382 * 2002-08-28 bjm
6384 select_no_parens:
6385 simple_select { $$ = $1; }
6386 | select_clause sort_clause
6388 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6389 NULL, NULL, NULL);
6390 $$ = $1;
6392 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6394 insertSelectOptions((SelectStmt *) $1, $2, $3,
6395 list_nth($4, 0), list_nth($4, 1),
6396 NULL);
6397 $$ = $1;
6399 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6401 insertSelectOptions((SelectStmt *) $1, $2, $4,
6402 list_nth($3, 0), list_nth($3, 1),
6403 NULL);
6404 $$ = $1;
6406 | with_clause simple_select
6408 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6409 NULL, NULL,
6410 $1);
6411 $$ = $2;
6413 | with_clause select_clause sort_clause
6415 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6416 NULL, NULL,
6417 $1);
6418 $$ = $2;
6420 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6422 insertSelectOptions((SelectStmt *) $2, $3, $4,
6423 list_nth($5, 0), list_nth($5, 1),
6424 $1);
6425 $$ = $2;
6427 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6429 insertSelectOptions((SelectStmt *) $2, $3, $5,
6430 list_nth($4, 0), list_nth($4, 1),
6431 $1);
6432 $$ = $2;
6436 select_clause:
6437 simple_select { $$ = $1; }
6438 | select_with_parens { $$ = $1; }
6442 * This rule parses SELECT statements that can appear within set operations,
6443 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6444 * the ordering of the set operations. Without '(' and ')' we want the
6445 * operations to be ordered per the precedence specs at the head of this file.
6447 * As with select_no_parens, simple_select cannot have outer parentheses,
6448 * but can have parenthesized subclauses.
6450 * Note that sort clauses cannot be included at this level --- SQL92 requires
6451 * SELECT foo UNION SELECT bar ORDER BY baz
6452 * to be parsed as
6453 * (SELECT foo UNION SELECT bar) ORDER BY baz
6454 * not
6455 * SELECT foo UNION (SELECT bar ORDER BY baz)
6456 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6457 * described as part of the select_no_parens production, not simple_select.
6458 * This does not limit functionality, because you can reintroduce these
6459 * clauses inside parentheses.
6461 * NOTE: only the leftmost component SelectStmt should have INTO.
6462 * However, this is not checked by the grammar; parse analysis must check it.
6464 simple_select:
6465 SELECT opt_distinct target_list
6466 into_clause from_clause where_clause
6467 group_clause having_clause
6469 SelectStmt *n = makeNode(SelectStmt);
6470 n->distinctClause = $2;
6471 n->targetList = $3;
6472 n->intoClause = $4;
6473 n->fromClause = $5;
6474 n->whereClause = $6;
6475 n->groupClause = $7;
6476 n->havingClause = $8;
6477 $$ = (Node *)n;
6479 | values_clause { $$ = $1; }
6480 | TABLE relation_expr
6482 /* same as SELECT * FROM relation_expr */
6483 ColumnRef *cr = makeNode(ColumnRef);
6484 ResTarget *rt = makeNode(ResTarget);
6485 SelectStmt *n = makeNode(SelectStmt);
6487 cr->fields = list_make1(makeNode(A_Star));
6488 cr->location = -1;
6490 rt->name = NULL;
6491 rt->indirection = NIL;
6492 rt->val = (Node *)cr;
6493 rt->location = -1;
6495 n->targetList = list_make1(rt);
6496 n->fromClause = list_make1($2);
6497 $$ = (Node *)n;
6499 | select_clause UNION opt_all select_clause
6501 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6503 | select_clause INTERSECT opt_all select_clause
6505 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6507 | select_clause EXCEPT opt_all select_clause
6509 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6514 * SQL standard WITH clause looks like:
6516 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6517 * AS (query) [ SEARCH or CYCLE clause ]
6519 * We don't currently support the SEARCH or CYCLE clause.
6521 with_clause:
6522 WITH cte_list
6524 $$ = makeNode(WithClause);
6525 $$->ctes = $2;
6526 $$->recursive = false;
6527 $$->location = @1;
6529 | WITH RECURSIVE cte_list
6531 $$ = makeNode(WithClause);
6532 $$->ctes = $3;
6533 $$->recursive = true;
6534 $$->location = @1;
6538 cte_list:
6539 common_table_expr { $$ = list_make1($1); }
6540 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
6543 common_table_expr: name opt_name_list AS select_with_parens
6545 CommonTableExpr *n = makeNode(CommonTableExpr);
6546 n->ctename = $1;
6547 n->aliascolnames = $2;
6548 n->ctequery = $4;
6549 n->location = @1;
6550 $$ = (Node *) n;
6554 into_clause:
6555 INTO OptTempTableName
6557 $$ = makeNode(IntoClause);
6558 $$->rel = $2;
6559 $$->colNames = NIL;
6560 $$->options = NIL;
6561 $$->onCommit = ONCOMMIT_NOOP;
6562 $$->tableSpaceName = NULL;
6564 | /*EMPTY*/
6565 { $$ = NULL; }
6569 * Redundancy here is needed to avoid shift/reduce conflicts,
6570 * since TEMP is not a reserved word. See also OptTemp.
6572 OptTempTableName:
6573 TEMPORARY opt_table qualified_name
6575 $$ = $3;
6576 $$->istemp = true;
6578 | TEMP opt_table qualified_name
6580 $$ = $3;
6581 $$->istemp = true;
6583 | LOCAL TEMPORARY opt_table qualified_name
6585 $$ = $4;
6586 $$->istemp = true;
6588 | LOCAL TEMP opt_table qualified_name
6590 $$ = $4;
6591 $$->istemp = true;
6593 | GLOBAL TEMPORARY opt_table qualified_name
6595 $$ = $4;
6596 $$->istemp = true;
6598 | GLOBAL TEMP opt_table qualified_name
6600 $$ = $4;
6601 $$->istemp = true;
6603 | TABLE qualified_name
6605 $$ = $2;
6606 $$->istemp = false;
6608 | qualified_name
6610 $$ = $1;
6611 $$->istemp = false;
6615 opt_table: TABLE {}
6616 | /*EMPTY*/ {}
6619 opt_all: ALL { $$ = TRUE; }
6620 | DISTINCT { $$ = FALSE; }
6621 | /*EMPTY*/ { $$ = FALSE; }
6624 /* We use (NIL) as a placeholder to indicate that all target expressions
6625 * should be placed in the DISTINCT list during parsetree analysis.
6627 opt_distinct:
6628 DISTINCT { $$ = list_make1(NIL); }
6629 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6630 | ALL { $$ = NIL; }
6631 | /*EMPTY*/ { $$ = NIL; }
6634 opt_sort_clause:
6635 sort_clause { $$ = $1;}
6636 | /*EMPTY*/ { $$ = NIL; }
6639 sort_clause:
6640 ORDER BY sortby_list { $$ = $3; }
6643 sortby_list:
6644 sortby { $$ = list_make1($1); }
6645 | sortby_list ',' sortby { $$ = lappend($1, $3); }
6648 sortby: a_expr USING qual_all_Op opt_nulls_order
6650 $$ = makeNode(SortBy);
6651 $$->node = $1;
6652 $$->sortby_dir = SORTBY_USING;
6653 $$->sortby_nulls = $4;
6654 $$->useOp = $3;
6655 $$->location = @3;
6657 | a_expr opt_asc_desc opt_nulls_order
6659 $$ = makeNode(SortBy);
6660 $$->node = $1;
6661 $$->sortby_dir = $2;
6662 $$->sortby_nulls = $3;
6663 $$->useOp = NIL;
6664 $$->location = -1; /* no operator */
6669 select_limit:
6670 LIMIT select_limit_value OFFSET select_offset_value
6671 { $$ = list_make2($4, $2); }
6672 | OFFSET select_offset_value LIMIT select_limit_value
6673 { $$ = list_make2($2, $4); }
6674 | LIMIT select_limit_value
6675 { $$ = list_make2(NULL, $2); }
6676 | OFFSET select_offset_value
6677 { $$ = list_make2($2, NULL); }
6678 | LIMIT select_limit_value ',' select_offset_value
6680 /* Disabled because it was too confusing, bjm 2002-02-18 */
6681 ereport(ERROR,
6682 (errcode(ERRCODE_SYNTAX_ERROR),
6683 errmsg("LIMIT #,# syntax is not supported"),
6684 errhint("Use separate LIMIT and OFFSET clauses."),
6685 scanner_errposition(@1)));
6687 /* SQL:2008 syntax variants */
6688 | OFFSET select_offset_value2 row_or_rows
6689 { $$ = list_make2($2, NULL); }
6690 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
6691 { $$ = list_make2(NULL, $3); }
6692 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
6693 { $$ = list_make2($2, $6); }
6696 opt_select_limit:
6697 select_limit { $$ = $1; }
6698 | /* EMPTY */
6699 { $$ = list_make2(NULL,NULL); }
6702 select_limit_value:
6703 a_expr { $$ = $1; }
6704 | ALL
6706 /* LIMIT ALL is represented as a NULL constant */
6707 $$ = makeNullAConst(@1);
6712 * Allowing full expressions without parentheses causes various parsing
6713 * problems with the trailing ROW/ROWS key words. SQL only calls for
6714 * constants, so we allow the rest only with parentheses.
6716 opt_select_fetch_first_value:
6717 SignedIconst { $$ = makeIntConst($1, @1); }
6718 | '(' a_expr ')' { $$ = $2; }
6719 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
6722 select_offset_value:
6723 a_expr { $$ = $1; }
6727 * Again, the trailing ROW/ROWS in this case prevent the full expression
6728 * syntax. c_expr is the best we can do.
6730 select_offset_value2:
6731 c_expr { $$ = $1; }
6734 /* noise words */
6735 row_or_rows:
6736 ROW { $$ = 0; }
6737 | ROWS { $$ = 0; }
6740 /* noise words */
6741 first_or_next:
6742 FIRST_P { $$ = 0; }
6743 | NEXT { $$ = 0; }
6746 group_clause:
6747 GROUP_P BY expr_list { $$ = $3; }
6748 | /*EMPTY*/ { $$ = NIL; }
6751 having_clause:
6752 HAVING a_expr { $$ = $2; }
6753 | /*EMPTY*/ { $$ = NULL; }
6756 for_locking_clause:
6757 for_locking_items { $$ = $1; }
6758 | FOR READ ONLY { $$ = NIL; }
6761 opt_for_locking_clause:
6762 for_locking_clause { $$ = $1; }
6763 | /* EMPTY */ { $$ = NIL; }
6766 for_locking_items:
6767 for_locking_item { $$ = list_make1($1); }
6768 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
6771 for_locking_item:
6772 FOR UPDATE locked_rels_list opt_nowait
6774 LockingClause *n = makeNode(LockingClause);
6775 n->lockedRels = $3;
6776 n->forUpdate = TRUE;
6777 n->noWait = $4;
6778 $$ = (Node *) n;
6780 | FOR SHARE locked_rels_list opt_nowait
6782 LockingClause *n = makeNode(LockingClause);
6783 n->lockedRels = $3;
6784 n->forUpdate = FALSE;
6785 n->noWait = $4;
6786 $$ = (Node *) n;
6790 locked_rels_list:
6791 OF qualified_name_list { $$ = $2; }
6792 | /* EMPTY */ { $$ = NIL; }
6796 values_clause:
6797 VALUES ctext_row
6799 SelectStmt *n = makeNode(SelectStmt);
6800 n->valuesLists = list_make1($2);
6801 $$ = (Node *) n;
6803 | values_clause ',' ctext_row
6805 SelectStmt *n = (SelectStmt *) $1;
6806 n->valuesLists = lappend(n->valuesLists, $3);
6807 $$ = (Node *) n;
6812 /*****************************************************************************
6814 * clauses common to all Optimizable Stmts:
6815 * from_clause - allow list of both JOIN expressions and table names
6816 * where_clause - qualifications for joins or restrictions
6818 *****************************************************************************/
6820 from_clause:
6821 FROM from_list { $$ = $2; }
6822 | /*EMPTY*/ { $$ = NIL; }
6825 from_list:
6826 table_ref { $$ = list_make1($1); }
6827 | from_list ',' table_ref { $$ = lappend($1, $3); }
6831 * table_ref is where an alias clause can be attached. Note we cannot make
6832 * alias_clause have an empty production because that causes parse conflicts
6833 * between table_ref := '(' joined_table ')' alias_clause
6834 * and joined_table := '(' joined_table ')'. So, we must have the
6835 * redundant-looking productions here instead.
6837 table_ref: relation_expr
6839 $$ = (Node *) $1;
6841 | relation_expr alias_clause
6843 $1->alias = $2;
6844 $$ = (Node *) $1;
6846 | func_table
6848 RangeFunction *n = makeNode(RangeFunction);
6849 n->funccallnode = $1;
6850 n->coldeflist = NIL;
6851 $$ = (Node *) n;
6853 | func_table alias_clause
6855 RangeFunction *n = makeNode(RangeFunction);
6856 n->funccallnode = $1;
6857 n->alias = $2;
6858 n->coldeflist = NIL;
6859 $$ = (Node *) n;
6861 | func_table AS '(' TableFuncElementList ')'
6863 RangeFunction *n = makeNode(RangeFunction);
6864 n->funccallnode = $1;
6865 n->coldeflist = $4;
6866 $$ = (Node *) n;
6868 | func_table AS ColId '(' TableFuncElementList ')'
6870 RangeFunction *n = makeNode(RangeFunction);
6871 Alias *a = makeNode(Alias);
6872 n->funccallnode = $1;
6873 a->aliasname = $3;
6874 n->alias = a;
6875 n->coldeflist = $5;
6876 $$ = (Node *) n;
6878 | func_table ColId '(' TableFuncElementList ')'
6880 RangeFunction *n = makeNode(RangeFunction);
6881 Alias *a = makeNode(Alias);
6882 n->funccallnode = $1;
6883 a->aliasname = $2;
6884 n->alias = a;
6885 n->coldeflist = $4;
6886 $$ = (Node *) n;
6888 | select_with_parens
6891 * The SQL spec does not permit a subselect
6892 * (<derived_table>) without an alias clause,
6893 * so we don't either. This avoids the problem
6894 * of needing to invent a unique refname for it.
6895 * That could be surmounted if there's sufficient
6896 * popular demand, but for now let's just implement
6897 * the spec and see if anyone complains.
6898 * However, it does seem like a good idea to emit
6899 * an error message that's better than "syntax error".
6901 if (IsA($1, SelectStmt) &&
6902 ((SelectStmt *) $1)->valuesLists)
6903 ereport(ERROR,
6904 (errcode(ERRCODE_SYNTAX_ERROR),
6905 errmsg("VALUES in FROM must have an alias"),
6906 errhint("For example, FROM (VALUES ...) [AS] foo."),
6907 scanner_errposition(@1)));
6908 else
6909 ereport(ERROR,
6910 (errcode(ERRCODE_SYNTAX_ERROR),
6911 errmsg("subquery in FROM must have an alias"),
6912 errhint("For example, FROM (SELECT ...) [AS] foo."),
6913 scanner_errposition(@1)));
6914 $$ = NULL;
6916 | select_with_parens alias_clause
6918 RangeSubselect *n = makeNode(RangeSubselect);
6919 n->subquery = $1;
6920 n->alias = $2;
6921 $$ = (Node *) n;
6923 | joined_table
6925 $$ = (Node *) $1;
6927 | '(' joined_table ')' alias_clause
6929 $2->alias = $4;
6930 $$ = (Node *) $2;
6936 * It may seem silly to separate joined_table from table_ref, but there is
6937 * method in SQL92's madness: if you don't do it this way you get reduce-
6938 * reduce conflicts, because it's not clear to the parser generator whether
6939 * to expect alias_clause after ')' or not. For the same reason we must
6940 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
6941 * join_type to expand to empty; if we try it, the parser generator can't
6942 * figure out when to reduce an empty join_type right after table_ref.
6944 * Note that a CROSS JOIN is the same as an unqualified
6945 * INNER JOIN, and an INNER JOIN/ON has the same shape
6946 * but a qualification expression to limit membership.
6947 * A NATURAL JOIN implicitly matches column names between
6948 * tables and the shape is determined by which columns are
6949 * in common. We'll collect columns during the later transformations.
6952 joined_table:
6953 '(' joined_table ')'
6955 $$ = $2;
6957 | table_ref CROSS JOIN table_ref
6959 /* CROSS JOIN is same as unqualified inner join */
6960 JoinExpr *n = makeNode(JoinExpr);
6961 n->jointype = JOIN_INNER;
6962 n->isNatural = FALSE;
6963 n->larg = $1;
6964 n->rarg = $4;
6965 n->using = NIL;
6966 n->quals = NULL;
6967 $$ = n;
6969 | table_ref join_type JOIN table_ref join_qual
6971 JoinExpr *n = makeNode(JoinExpr);
6972 n->jointype = $2;
6973 n->isNatural = FALSE;
6974 n->larg = $1;
6975 n->rarg = $4;
6976 if ($5 != NULL && IsA($5, List))
6977 n->using = (List *) $5; /* USING clause */
6978 else
6979 n->quals = $5; /* ON clause */
6980 $$ = n;
6982 | table_ref JOIN table_ref join_qual
6984 /* letting join_type reduce to empty doesn't work */
6985 JoinExpr *n = makeNode(JoinExpr);
6986 n->jointype = JOIN_INNER;
6987 n->isNatural = FALSE;
6988 n->larg = $1;
6989 n->rarg = $3;
6990 if ($4 != NULL && IsA($4, List))
6991 n->using = (List *) $4; /* USING clause */
6992 else
6993 n->quals = $4; /* ON clause */
6994 $$ = n;
6996 | table_ref NATURAL join_type JOIN table_ref
6998 JoinExpr *n = makeNode(JoinExpr);
6999 n->jointype = $3;
7000 n->isNatural = TRUE;
7001 n->larg = $1;
7002 n->rarg = $5;
7003 n->using = NIL; /* figure out which columns later... */
7004 n->quals = NULL; /* fill later */
7005 $$ = n;
7007 | table_ref NATURAL JOIN table_ref
7009 /* letting join_type reduce to empty doesn't work */
7010 JoinExpr *n = makeNode(JoinExpr);
7011 n->jointype = JOIN_INNER;
7012 n->isNatural = TRUE;
7013 n->larg = $1;
7014 n->rarg = $4;
7015 n->using = NIL; /* figure out which columns later... */
7016 n->quals = NULL; /* fill later */
7017 $$ = n;
7021 alias_clause:
7022 AS ColId '(' name_list ')'
7024 $$ = makeNode(Alias);
7025 $$->aliasname = $2;
7026 $$->colnames = $4;
7028 | AS ColId
7030 $$ = makeNode(Alias);
7031 $$->aliasname = $2;
7033 | ColId '(' name_list ')'
7035 $$ = makeNode(Alias);
7036 $$->aliasname = $1;
7037 $$->colnames = $3;
7039 | ColId
7041 $$ = makeNode(Alias);
7042 $$->aliasname = $1;
7046 join_type: FULL join_outer { $$ = JOIN_FULL; }
7047 | LEFT join_outer { $$ = JOIN_LEFT; }
7048 | RIGHT join_outer { $$ = JOIN_RIGHT; }
7049 | INNER_P { $$ = JOIN_INNER; }
7052 /* OUTER is just noise... */
7053 join_outer: OUTER_P { $$ = NULL; }
7054 | /*EMPTY*/ { $$ = NULL; }
7057 /* JOIN qualification clauses
7058 * Possibilities are:
7059 * USING ( column list ) allows only unqualified column names,
7060 * which must match between tables.
7061 * ON expr allows more general qualifications.
7063 * We return USING as a List node, while an ON-expr will not be a List.
7066 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
7067 | ON a_expr { $$ = $2; }
7071 relation_expr:
7072 qualified_name
7074 /* default inheritance */
7075 $$ = $1;
7076 $$->inhOpt = INH_DEFAULT;
7077 $$->alias = NULL;
7079 | qualified_name '*'
7081 /* inheritance query */
7082 $$ = $1;
7083 $$->inhOpt = INH_YES;
7084 $$->alias = NULL;
7086 | ONLY qualified_name
7088 /* no inheritance */
7089 $$ = $2;
7090 $$->inhOpt = INH_NO;
7091 $$->alias = NULL;
7093 | ONLY '(' qualified_name ')'
7095 /* no inheritance, SQL99-style syntax */
7096 $$ = $3;
7097 $$->inhOpt = INH_NO;
7098 $$->alias = NULL;
7104 * Given "UPDATE foo set set ...", we have to decide without looking any
7105 * further ahead whether the first "set" is an alias or the UPDATE's SET
7106 * keyword. Since "set" is allowed as a column name both interpretations
7107 * are feasible. We resolve the shift/reduce conflict by giving the first
7108 * relation_expr_opt_alias production a higher precedence than the SET token
7109 * has, causing the parser to prefer to reduce, in effect assuming that the
7110 * SET is not an alias.
7112 relation_expr_opt_alias: relation_expr %prec UMINUS
7114 $$ = $1;
7116 | relation_expr ColId
7118 Alias *alias = makeNode(Alias);
7119 alias->aliasname = $2;
7120 $1->alias = alias;
7121 $$ = $1;
7123 | relation_expr AS ColId
7125 Alias *alias = makeNode(Alias);
7126 alias->aliasname = $3;
7127 $1->alias = alias;
7128 $$ = $1;
7133 func_table: func_expr { $$ = $1; }
7137 where_clause:
7138 WHERE a_expr { $$ = $2; }
7139 | /*EMPTY*/ { $$ = NULL; }
7142 /* variant for UPDATE and DELETE */
7143 where_or_current_clause:
7144 WHERE a_expr { $$ = $2; }
7145 | WHERE CURRENT_P OF name
7147 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7148 /* cvarno is filled in by parse analysis */
7149 n->cursor_name = $4;
7150 n->cursor_param = 0;
7151 $$ = (Node *) n;
7153 | WHERE CURRENT_P OF PARAM
7155 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7156 /* cvarno is filled in by parse analysis */
7157 n->cursor_name = NULL;
7158 n->cursor_param = $4;
7159 $$ = (Node *) n;
7161 | /*EMPTY*/ { $$ = NULL; }
7165 TableFuncElementList:
7166 TableFuncElement
7168 $$ = list_make1($1);
7170 | TableFuncElementList ',' TableFuncElement
7172 $$ = lappend($1, $3);
7176 TableFuncElement: ColId Typename
7178 ColumnDef *n = makeNode(ColumnDef);
7179 n->colname = $1;
7180 n->typename = $2;
7181 n->constraints = NIL;
7182 n->is_local = true;
7183 $$ = (Node *)n;
7187 /*****************************************************************************
7189 * Type syntax
7190 * SQL92 introduces a large amount of type-specific syntax.
7191 * Define individual clauses to handle these cases, and use
7192 * the generic case to handle regular type-extensible Postgres syntax.
7193 * - thomas 1997-10-10
7195 *****************************************************************************/
7197 Typename: SimpleTypename opt_array_bounds
7199 $$ = $1;
7200 $$->arrayBounds = $2;
7202 | SETOF SimpleTypename opt_array_bounds
7204 $$ = $2;
7205 $$->arrayBounds = $3;
7206 $$->setof = TRUE;
7208 /* SQL standard syntax, currently only one-dimensional */
7209 | SimpleTypename ARRAY '[' Iconst ']'
7211 $$ = $1;
7212 $$->arrayBounds = list_make1(makeInteger($4));
7214 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7216 $$ = $2;
7217 $$->arrayBounds = list_make1(makeInteger($5));
7218 $$->setof = TRUE;
7220 | SimpleTypename ARRAY
7222 $$ = $1;
7223 $$->arrayBounds = list_make1(makeInteger(-1));
7225 | SETOF SimpleTypename ARRAY
7227 $$ = $2;
7228 $$->arrayBounds = list_make1(makeInteger(-1));
7229 $$->setof = TRUE;
7233 opt_array_bounds:
7234 opt_array_bounds '[' ']'
7235 { $$ = lappend($1, makeInteger(-1)); }
7236 | opt_array_bounds '[' Iconst ']'
7237 { $$ = lappend($1, makeInteger($3)); }
7238 | /*EMPTY*/
7239 { $$ = NIL; }
7242 SimpleTypename:
7243 GenericType { $$ = $1; }
7244 | Numeric { $$ = $1; }
7245 | Bit { $$ = $1; }
7246 | Character { $$ = $1; }
7247 | ConstDatetime { $$ = $1; }
7248 | ConstInterval opt_interval
7250 $$ = $1;
7251 $$->typmods = $2;
7253 | ConstInterval '(' Iconst ')' opt_interval
7255 $$ = $1;
7256 if ($5 != NIL)
7258 if (list_length($5) != 1)
7259 ereport(ERROR,
7260 (errcode(ERRCODE_SYNTAX_ERROR),
7261 errmsg("interval precision specified twice"),
7262 scanner_errposition(@1)));
7263 $$->typmods = lappend($5, makeIntConst($3, @3));
7265 else
7266 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7267 makeIntConst($3, @3));
7271 /* We have a separate ConstTypename to allow defaulting fixed-length
7272 * types such as CHAR() and BIT() to an unspecified length.
7273 * SQL9x requires that these default to a length of one, but this
7274 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7275 * where there is an obvious better choice to make.
7276 * Note that ConstInterval is not included here since it must
7277 * be pushed up higher in the rules to accomodate the postfix
7278 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7279 * the generic-type-name case in AExprConst to avoid premature
7280 * reduce/reduce conflicts against function names.
7282 ConstTypename:
7283 Numeric { $$ = $1; }
7284 | ConstBit { $$ = $1; }
7285 | ConstCharacter { $$ = $1; }
7286 | ConstDatetime { $$ = $1; }
7290 * GenericType covers all type names that don't have special syntax mandated
7291 * by the standard, including qualified names. We also allow type modifiers.
7292 * To avoid parsing conflicts against function invocations, the modifiers
7293 * have to be shown as expr_list here, but parse analysis will only accept
7294 * constants for them.
7296 GenericType:
7297 type_function_name opt_type_modifiers
7299 $$ = makeTypeName($1);
7300 $$->typmods = $2;
7301 $$->location = @1;
7303 | type_function_name attrs opt_type_modifiers
7305 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7306 $$->typmods = $3;
7307 $$->location = @1;
7311 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7312 | /* EMPTY */ { $$ = NIL; }
7316 * SQL92 numeric data types
7318 Numeric: INT_P
7320 $$ = SystemTypeName("int4");
7321 $$->location = @1;
7323 | INTEGER
7325 $$ = SystemTypeName("int4");
7326 $$->location = @1;
7328 | SMALLINT
7330 $$ = SystemTypeName("int2");
7331 $$->location = @1;
7333 | BIGINT
7335 $$ = SystemTypeName("int8");
7336 $$->location = @1;
7338 | REAL
7340 $$ = SystemTypeName("float4");
7341 $$->location = @1;
7343 | FLOAT_P opt_float
7345 $$ = $2;
7346 $$->location = @1;
7348 | DOUBLE_P PRECISION
7350 $$ = SystemTypeName("float8");
7351 $$->location = @1;
7353 | DECIMAL_P opt_type_modifiers
7355 $$ = SystemTypeName("numeric");
7356 $$->typmods = $2;
7357 $$->location = @1;
7359 | DEC opt_type_modifiers
7361 $$ = SystemTypeName("numeric");
7362 $$->typmods = $2;
7363 $$->location = @1;
7365 | NUMERIC opt_type_modifiers
7367 $$ = SystemTypeName("numeric");
7368 $$->typmods = $2;
7369 $$->location = @1;
7371 | BOOLEAN_P
7373 $$ = SystemTypeName("bool");
7374 $$->location = @1;
7378 opt_float: '(' Iconst ')'
7381 * Check FLOAT() precision limits assuming IEEE floating
7382 * types - thomas 1997-09-18
7384 if ($2 < 1)
7385 ereport(ERROR,
7386 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7387 errmsg("precision for type float must be at least 1 bit"),
7388 scanner_errposition(@2)));
7389 else if ($2 <= 24)
7390 $$ = SystemTypeName("float4");
7391 else if ($2 <= 53)
7392 $$ = SystemTypeName("float8");
7393 else
7394 ereport(ERROR,
7395 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7396 errmsg("precision for type float must be less than 54 bits"),
7397 scanner_errposition(@2)));
7399 | /*EMPTY*/
7401 $$ = SystemTypeName("float8");
7406 * SQL92 bit-field data types
7407 * The following implements BIT() and BIT VARYING().
7409 Bit: BitWithLength
7411 $$ = $1;
7413 | BitWithoutLength
7415 $$ = $1;
7419 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7420 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7421 ConstBit: BitWithLength
7423 $$ = $1;
7425 | BitWithoutLength
7427 $$ = $1;
7428 $$->typmods = NIL;
7432 BitWithLength:
7433 BIT opt_varying '(' expr_list ')'
7435 char *typname;
7437 typname = $2 ? "varbit" : "bit";
7438 $$ = SystemTypeName(typname);
7439 $$->typmods = $4;
7440 $$->location = @1;
7444 BitWithoutLength:
7445 BIT opt_varying
7447 /* bit defaults to bit(1), varbit to no limit */
7448 if ($2)
7450 $$ = SystemTypeName("varbit");
7452 else
7454 $$ = SystemTypeName("bit");
7455 $$->typmods = list_make1(makeIntConst(1, -1));
7457 $$->location = @1;
7463 * SQL92 character data types
7464 * The following implements CHAR() and VARCHAR().
7466 Character: CharacterWithLength
7468 $$ = $1;
7470 | CharacterWithoutLength
7472 $$ = $1;
7476 ConstCharacter: CharacterWithLength
7478 $$ = $1;
7480 | CharacterWithoutLength
7482 /* Length was not specified so allow to be unrestricted.
7483 * This handles problems with fixed-length (bpchar) strings
7484 * which in column definitions must default to a length
7485 * of one, but should not be constrained if the length
7486 * was not specified.
7488 $$ = $1;
7489 $$->typmods = NIL;
7493 CharacterWithLength: character '(' Iconst ')' opt_charset
7495 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7497 char *type;
7499 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7500 strcpy(type, $1);
7501 strcat(type, "_");
7502 strcat(type, $5);
7503 $1 = type;
7506 $$ = SystemTypeName($1);
7507 $$->typmods = list_make1(makeIntConst($3, @3));
7508 $$->location = @1;
7512 CharacterWithoutLength: character opt_charset
7514 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7516 char *type;
7518 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7519 strcpy(type, $1);
7520 strcat(type, "_");
7521 strcat(type, $2);
7522 $1 = type;
7525 $$ = SystemTypeName($1);
7527 /* char defaults to char(1), varchar to no limit */
7528 if (strcmp($1, "bpchar") == 0)
7529 $$->typmods = list_make1(makeIntConst(1, -1));
7531 $$->location = @1;
7535 character: CHARACTER opt_varying
7536 { $$ = $2 ? "varchar": "bpchar"; }
7537 | CHAR_P opt_varying
7538 { $$ = $2 ? "varchar": "bpchar"; }
7539 | VARCHAR
7540 { $$ = "varchar"; }
7541 | NATIONAL CHARACTER opt_varying
7542 { $$ = $3 ? "varchar": "bpchar"; }
7543 | NATIONAL CHAR_P opt_varying
7544 { $$ = $3 ? "varchar": "bpchar"; }
7545 | NCHAR opt_varying
7546 { $$ = $2 ? "varchar": "bpchar"; }
7549 opt_varying:
7550 VARYING { $$ = TRUE; }
7551 | /*EMPTY*/ { $$ = FALSE; }
7554 opt_charset:
7555 CHARACTER SET ColId { $$ = $3; }
7556 | /*EMPTY*/ { $$ = NULL; }
7560 * SQL92 date/time types
7562 ConstDatetime:
7563 TIMESTAMP '(' Iconst ')' opt_timezone
7565 if ($5)
7566 $$ = SystemTypeName("timestamptz");
7567 else
7568 $$ = SystemTypeName("timestamp");
7569 $$->typmods = list_make1(makeIntConst($3, @3));
7570 $$->location = @1;
7572 | TIMESTAMP opt_timezone
7574 if ($2)
7575 $$ = SystemTypeName("timestamptz");
7576 else
7577 $$ = SystemTypeName("timestamp");
7578 $$->location = @1;
7580 | TIME '(' Iconst ')' opt_timezone
7582 if ($5)
7583 $$ = SystemTypeName("timetz");
7584 else
7585 $$ = SystemTypeName("time");
7586 $$->typmods = list_make1(makeIntConst($3, @3));
7587 $$->location = @1;
7589 | TIME opt_timezone
7591 if ($2)
7592 $$ = SystemTypeName("timetz");
7593 else
7594 $$ = SystemTypeName("time");
7595 $$->location = @1;
7599 ConstInterval:
7600 INTERVAL
7602 $$ = SystemTypeName("interval");
7603 $$->location = @1;
7607 opt_timezone:
7608 WITH_TIME ZONE { $$ = TRUE; }
7609 | WITHOUT TIME ZONE { $$ = FALSE; }
7610 | /*EMPTY*/ { $$ = FALSE; }
7613 opt_interval:
7614 YEAR_P
7615 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
7616 | MONTH_P
7617 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
7618 | DAY_P
7619 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
7620 | HOUR_P
7621 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
7622 | MINUTE_P
7623 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
7624 | interval_second
7625 { $$ = $1; }
7626 | YEAR_P TO MONTH_P
7628 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
7629 INTERVAL_MASK(MONTH), @1));
7631 | DAY_P TO HOUR_P
7633 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7634 INTERVAL_MASK(HOUR), @1));
7636 | DAY_P TO MINUTE_P
7638 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7639 INTERVAL_MASK(HOUR) |
7640 INTERVAL_MASK(MINUTE), @1));
7642 | DAY_P TO interval_second
7644 $$ = $3;
7645 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
7646 INTERVAL_MASK(HOUR) |
7647 INTERVAL_MASK(MINUTE) |
7648 INTERVAL_MASK(SECOND), @1);
7650 | HOUR_P TO MINUTE_P
7652 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
7653 INTERVAL_MASK(MINUTE), @1));
7655 | HOUR_P TO interval_second
7657 $$ = $3;
7658 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
7659 INTERVAL_MASK(MINUTE) |
7660 INTERVAL_MASK(SECOND), @1);
7662 | MINUTE_P TO interval_second
7664 $$ = $3;
7665 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
7666 INTERVAL_MASK(SECOND), @1);
7668 | /*EMPTY*/
7669 { $$ = NIL; }
7672 interval_second:
7673 SECOND_P
7675 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
7677 | SECOND_P '(' Iconst ')'
7679 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
7680 makeIntConst($3, @3));
7685 /*****************************************************************************
7687 * expression grammar
7689 *****************************************************************************/
7692 * General expressions
7693 * This is the heart of the expression syntax.
7695 * We have two expression types: a_expr is the unrestricted kind, and
7696 * b_expr is a subset that must be used in some places to avoid shift/reduce
7697 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
7698 * because that use of AND conflicts with AND as a boolean operator. So,
7699 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
7701 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
7702 * always be used by surrounding it with parens.
7704 * c_expr is all the productions that are common to a_expr and b_expr;
7705 * it's factored out just to eliminate redundant coding.
7707 a_expr: c_expr { $$ = $1; }
7708 | a_expr TYPECAST Typename
7709 { $$ = makeTypeCast($1, $3, @2); }
7710 | a_expr AT TIME ZONE a_expr
7712 FuncCall *n = makeNode(FuncCall);
7713 n->funcname = SystemFuncName("timezone");
7714 n->args = list_make2($5, $1);
7715 n->agg_star = FALSE;
7716 n->agg_distinct = FALSE;
7717 n->func_variadic = FALSE;
7718 n->location = @2;
7719 $$ = (Node *) n;
7722 * These operators must be called out explicitly in order to make use
7723 * of yacc/bison's automatic operator-precedence handling. All other
7724 * operator names are handled by the generic productions using "Op",
7725 * below; and all those operators will have the same precedence.
7727 * If you add more explicitly-known operators, be sure to add them
7728 * also to b_expr and to the MathOp list above.
7730 | '+' a_expr %prec UMINUS
7731 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7732 | '-' a_expr %prec UMINUS
7733 { $$ = doNegate($2, @1); }
7734 | a_expr '+' a_expr
7735 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7736 | a_expr '-' a_expr
7737 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7738 | a_expr '*' a_expr
7739 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7740 | a_expr '/' a_expr
7741 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7742 | a_expr '%' a_expr
7743 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7744 | a_expr '^' a_expr
7745 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7746 | a_expr '<' a_expr
7747 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7748 | a_expr '>' a_expr
7749 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7750 | a_expr '=' a_expr
7751 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7753 | a_expr qual_Op a_expr %prec Op
7754 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7755 | qual_Op a_expr %prec Op
7756 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7757 | a_expr qual_Op %prec POSTFIXOP
7758 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7760 | a_expr AND a_expr
7761 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
7762 | a_expr OR a_expr
7763 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
7764 | NOT a_expr
7765 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
7767 | a_expr LIKE a_expr
7768 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
7769 | a_expr LIKE a_expr ESCAPE a_expr
7771 FuncCall *n = makeNode(FuncCall);
7772 n->funcname = SystemFuncName("like_escape");
7773 n->args = list_make2($3, $5);
7774 n->agg_star = FALSE;
7775 n->agg_distinct = FALSE;
7776 n->func_variadic = FALSE;
7777 n->location = @4;
7778 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
7780 | a_expr NOT LIKE a_expr
7781 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
7782 | a_expr NOT LIKE a_expr ESCAPE a_expr
7784 FuncCall *n = makeNode(FuncCall);
7785 n->funcname = SystemFuncName("like_escape");
7786 n->args = list_make2($4, $6);
7787 n->agg_star = FALSE;
7788 n->agg_distinct = FALSE;
7789 n->func_variadic = FALSE;
7790 n->location = @5;
7791 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
7793 | a_expr ILIKE a_expr
7794 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
7795 | a_expr ILIKE a_expr ESCAPE a_expr
7797 FuncCall *n = makeNode(FuncCall);
7798 n->funcname = SystemFuncName("like_escape");
7799 n->args = list_make2($3, $5);
7800 n->agg_star = FALSE;
7801 n->agg_distinct = FALSE;
7802 n->func_variadic = FALSE;
7803 n->location = @4;
7804 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
7806 | a_expr NOT ILIKE a_expr
7807 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
7808 | a_expr NOT ILIKE a_expr ESCAPE a_expr
7810 FuncCall *n = makeNode(FuncCall);
7811 n->funcname = SystemFuncName("like_escape");
7812 n->args = list_make2($4, $6);
7813 n->agg_star = FALSE;
7814 n->agg_distinct = FALSE;
7815 n->func_variadic = FALSE;
7816 n->location = @5;
7817 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
7820 | a_expr SIMILAR TO a_expr %prec SIMILAR
7822 FuncCall *n = makeNode(FuncCall);
7823 n->funcname = SystemFuncName("similar_escape");
7824 n->args = list_make2($4, makeNullAConst(-1));
7825 n->agg_star = FALSE;
7826 n->agg_distinct = FALSE;
7827 n->func_variadic = FALSE;
7828 n->location = @2;
7829 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7831 | a_expr SIMILAR TO a_expr ESCAPE a_expr
7833 FuncCall *n = makeNode(FuncCall);
7834 n->funcname = SystemFuncName("similar_escape");
7835 n->args = list_make2($4, $6);
7836 n->agg_star = FALSE;
7837 n->agg_distinct = FALSE;
7838 n->func_variadic = FALSE;
7839 n->location = @5;
7840 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7842 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
7844 FuncCall *n = makeNode(FuncCall);
7845 n->funcname = SystemFuncName("similar_escape");
7846 n->args = list_make2($5, makeNullAConst(-1));
7847 n->agg_star = FALSE;
7848 n->agg_distinct = FALSE;
7849 n->func_variadic = FALSE;
7850 n->location = @5;
7851 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7853 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
7855 FuncCall *n = makeNode(FuncCall);
7856 n->funcname = SystemFuncName("similar_escape");
7857 n->args = list_make2($5, $7);
7858 n->agg_star = FALSE;
7859 n->agg_distinct = FALSE;
7860 n->func_variadic = FALSE;
7861 n->location = @6;
7862 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7865 /* NullTest clause
7866 * Define SQL92-style Null test clause.
7867 * Allow two forms described in the standard:
7868 * a IS NULL
7869 * a IS NOT NULL
7870 * Allow two SQL extensions
7871 * a ISNULL
7872 * a NOTNULL
7874 | a_expr IS NULL_P
7876 NullTest *n = makeNode(NullTest);
7877 n->arg = (Expr *) $1;
7878 n->nulltesttype = IS_NULL;
7879 $$ = (Node *)n;
7881 | a_expr ISNULL
7883 NullTest *n = makeNode(NullTest);
7884 n->arg = (Expr *) $1;
7885 n->nulltesttype = IS_NULL;
7886 $$ = (Node *)n;
7888 | a_expr IS NOT NULL_P
7890 NullTest *n = makeNode(NullTest);
7891 n->arg = (Expr *) $1;
7892 n->nulltesttype = IS_NOT_NULL;
7893 $$ = (Node *)n;
7895 | a_expr NOTNULL
7897 NullTest *n = makeNode(NullTest);
7898 n->arg = (Expr *) $1;
7899 n->nulltesttype = IS_NOT_NULL;
7900 $$ = (Node *)n;
7902 | row OVERLAPS row
7904 $$ = (Node *)makeOverlaps($1, $3, @2);
7906 | a_expr IS TRUE_P
7908 BooleanTest *b = makeNode(BooleanTest);
7909 b->arg = (Expr *) $1;
7910 b->booltesttype = IS_TRUE;
7911 $$ = (Node *)b;
7913 | a_expr IS NOT TRUE_P
7915 BooleanTest *b = makeNode(BooleanTest);
7916 b->arg = (Expr *) $1;
7917 b->booltesttype = IS_NOT_TRUE;
7918 $$ = (Node *)b;
7920 | a_expr IS FALSE_P
7922 BooleanTest *b = makeNode(BooleanTest);
7923 b->arg = (Expr *) $1;
7924 b->booltesttype = IS_FALSE;
7925 $$ = (Node *)b;
7927 | a_expr IS NOT FALSE_P
7929 BooleanTest *b = makeNode(BooleanTest);
7930 b->arg = (Expr *) $1;
7931 b->booltesttype = IS_NOT_FALSE;
7932 $$ = (Node *)b;
7934 | a_expr IS UNKNOWN
7936 BooleanTest *b = makeNode(BooleanTest);
7937 b->arg = (Expr *) $1;
7938 b->booltesttype = IS_UNKNOWN;
7939 $$ = (Node *)b;
7941 | a_expr IS NOT UNKNOWN
7943 BooleanTest *b = makeNode(BooleanTest);
7944 b->arg = (Expr *) $1;
7945 b->booltesttype = IS_NOT_UNKNOWN;
7946 $$ = (Node *)b;
7948 | a_expr IS DISTINCT FROM a_expr %prec IS
7950 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7952 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
7954 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7955 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
7956 "=", $1, $6, @2),
7957 @2);
7960 | a_expr IS OF '(' type_list ')' %prec IS
7962 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7964 | a_expr IS NOT OF '(' type_list ')' %prec IS
7966 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7968 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7970 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7971 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7972 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7973 @2);
7975 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7977 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7978 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7979 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7980 @2);
7982 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7984 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7985 (Node *) makeA_Expr(AEXPR_AND, NIL,
7986 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7987 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7988 @2),
7989 (Node *) makeA_Expr(AEXPR_AND, NIL,
7990 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
7991 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
7992 @2),
7993 @2);
7995 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7997 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7998 (Node *) makeA_Expr(AEXPR_OR, NIL,
7999 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
8000 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
8001 @2),
8002 (Node *) makeA_Expr(AEXPR_OR, NIL,
8003 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
8004 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
8005 @2),
8006 @2);
8008 | a_expr IN_P in_expr
8010 /* in_expr returns a SubLink or a list of a_exprs */
8011 if (IsA($3, SubLink))
8013 /* generate foo = ANY (subquery) */
8014 SubLink *n = (SubLink *) $3;
8015 n->subLinkType = ANY_SUBLINK;
8016 n->testexpr = $1;
8017 n->operName = list_make1(makeString("="));
8018 n->location = @2;
8019 $$ = (Node *)n;
8021 else
8023 /* generate scalar IN expression */
8024 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
8027 | a_expr NOT IN_P in_expr
8029 /* in_expr returns a SubLink or a list of a_exprs */
8030 if (IsA($4, SubLink))
8032 /* generate NOT (foo = ANY (subquery)) */
8033 /* Make an = ANY node */
8034 SubLink *n = (SubLink *) $4;
8035 n->subLinkType = ANY_SUBLINK;
8036 n->testexpr = $1;
8037 n->operName = list_make1(makeString("="));
8038 n->location = @3;
8039 /* Stick a NOT on top */
8040 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
8042 else
8044 /* generate scalar NOT IN expression */
8045 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
8048 | a_expr subquery_Op sub_type select_with_parens %prec Op
8050 SubLink *n = makeNode(SubLink);
8051 n->subLinkType = $3;
8052 n->testexpr = $1;
8053 n->operName = $2;
8054 n->subselect = $4;
8055 n->location = @2;
8056 $$ = (Node *)n;
8058 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
8060 if ($3 == ANY_SUBLINK)
8061 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
8062 else
8063 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
8065 | UNIQUE select_with_parens
8067 /* Not sure how to get rid of the parentheses
8068 * but there are lots of shift/reduce errors without them.
8070 * Should be able to implement this by plopping the entire
8071 * select into a node, then transforming the target expressions
8072 * from whatever they are into count(*), and testing the
8073 * entire result equal to one.
8074 * But, will probably implement a separate node in the executor.
8076 ereport(ERROR,
8077 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8078 errmsg("UNIQUE predicate is not yet implemented"),
8079 scanner_errposition(@1)));
8081 | a_expr IS DOCUMENT_P %prec IS
8083 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8084 list_make1($1), @2);
8086 | a_expr IS NOT DOCUMENT_P %prec IS
8088 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8089 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8090 list_make1($1), @2),
8091 @2);
8096 * Restricted expressions
8098 * b_expr is a subset of the complete expression syntax defined by a_expr.
8100 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8101 * cause trouble in the places where b_expr is used. For simplicity, we
8102 * just eliminate all the boolean-keyword-operator productions from b_expr.
8104 b_expr: c_expr
8105 { $$ = $1; }
8106 | b_expr TYPECAST Typename
8107 { $$ = makeTypeCast($1, $3, @2); }
8108 | '+' b_expr %prec UMINUS
8109 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8110 | '-' b_expr %prec UMINUS
8111 { $$ = doNegate($2, @1); }
8112 | b_expr '+' b_expr
8113 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8114 | b_expr '-' b_expr
8115 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8116 | b_expr '*' b_expr
8117 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8118 | b_expr '/' b_expr
8119 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8120 | b_expr '%' b_expr
8121 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8122 | b_expr '^' b_expr
8123 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8124 | b_expr '<' b_expr
8125 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8126 | b_expr '>' b_expr
8127 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8128 | b_expr '=' b_expr
8129 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8130 | b_expr qual_Op b_expr %prec Op
8131 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8132 | qual_Op b_expr %prec Op
8133 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8134 | b_expr qual_Op %prec POSTFIXOP
8135 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8136 | b_expr IS DISTINCT FROM b_expr %prec IS
8138 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8140 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8142 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8143 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8145 | b_expr IS OF '(' type_list ')' %prec IS
8147 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8149 | b_expr IS NOT OF '(' type_list ')' %prec IS
8151 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8153 | b_expr IS DOCUMENT_P %prec IS
8155 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8156 list_make1($1), @2);
8158 | b_expr IS NOT DOCUMENT_P %prec IS
8160 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8161 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8162 list_make1($1), @2),
8163 @2);
8168 * Productions that can be used in both a_expr and b_expr.
8170 * Note: productions that refer recursively to a_expr or b_expr mostly
8171 * cannot appear here. However, it's OK to refer to a_exprs that occur
8172 * inside parentheses, such as function arguments; that cannot introduce
8173 * ambiguity to the b_expr syntax.
8175 c_expr: columnref { $$ = $1; }
8176 | AexprConst { $$ = $1; }
8177 | PARAM opt_indirection
8179 ParamRef *p = makeNode(ParamRef);
8180 p->number = $1;
8181 p->location = @1;
8182 if ($2)
8184 A_Indirection *n = makeNode(A_Indirection);
8185 n->arg = (Node *) p;
8186 n->indirection = check_indirection($2);
8187 $$ = (Node *) n;
8189 else
8190 $$ = (Node *) p;
8192 | '(' a_expr ')' opt_indirection
8194 if ($4)
8196 A_Indirection *n = makeNode(A_Indirection);
8197 n->arg = $2;
8198 n->indirection = check_indirection($4);
8199 $$ = (Node *)n;
8201 else
8202 $$ = $2;
8204 | case_expr
8205 { $$ = $1; }
8206 | func_expr
8207 { $$ = $1; }
8208 | select_with_parens %prec UMINUS
8210 SubLink *n = makeNode(SubLink);
8211 n->subLinkType = EXPR_SUBLINK;
8212 n->testexpr = NULL;
8213 n->operName = NIL;
8214 n->subselect = $1;
8215 n->location = @1;
8216 $$ = (Node *)n;
8218 | EXISTS select_with_parens
8220 SubLink *n = makeNode(SubLink);
8221 n->subLinkType = EXISTS_SUBLINK;
8222 n->testexpr = NULL;
8223 n->operName = NIL;
8224 n->subselect = $2;
8225 n->location = @1;
8226 $$ = (Node *)n;
8228 | ARRAY select_with_parens
8230 SubLink *n = makeNode(SubLink);
8231 n->subLinkType = ARRAY_SUBLINK;
8232 n->testexpr = NULL;
8233 n->operName = NIL;
8234 n->subselect = $2;
8235 n->location = @1;
8236 $$ = (Node *)n;
8238 | ARRAY array_expr
8240 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8241 Assert(IsA(n, A_ArrayExpr));
8242 /* point outermost A_ArrayExpr to the ARRAY keyword */
8243 n->location = @1;
8244 $$ = (Node *)n;
8246 | row
8248 RowExpr *r = makeNode(RowExpr);
8249 r->args = $1;
8250 r->row_typeid = InvalidOid; /* not analyzed yet */
8251 r->location = @1;
8252 $$ = (Node *)r;
8257 * func_expr is split out from c_expr just so that we have a classification
8258 * for "everything that is a function call or looks like one". This isn't
8259 * very important, but it saves us having to document which variants are
8260 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8261 * (Note that many of the special SQL functions wouldn't actually make any
8262 * sense as functional index entries, but we ignore that consideration here.)
8264 func_expr: func_name '(' ')'
8266 FuncCall *n = makeNode(FuncCall);
8267 n->funcname = $1;
8268 n->args = NIL;
8269 n->agg_star = FALSE;
8270 n->agg_distinct = FALSE;
8271 n->func_variadic = FALSE;
8272 n->location = @1;
8273 $$ = (Node *)n;
8275 | func_name '(' expr_list ')'
8277 FuncCall *n = makeNode(FuncCall);
8278 n->funcname = $1;
8279 n->args = $3;
8280 n->agg_star = FALSE;
8281 n->agg_distinct = FALSE;
8282 n->func_variadic = FALSE;
8283 n->location = @1;
8284 $$ = (Node *)n;
8286 | func_name '(' VARIADIC a_expr ')'
8288 FuncCall *n = makeNode(FuncCall);
8289 n->funcname = $1;
8290 n->args = list_make1($4);
8291 n->agg_star = FALSE;
8292 n->agg_distinct = FALSE;
8293 n->func_variadic = TRUE;
8294 n->location = @1;
8295 $$ = (Node *)n;
8297 | func_name '(' expr_list ',' VARIADIC a_expr ')'
8299 FuncCall *n = makeNode(FuncCall);
8300 n->funcname = $1;
8301 n->args = lappend($3, $6);
8302 n->agg_star = FALSE;
8303 n->agg_distinct = FALSE;
8304 n->func_variadic = TRUE;
8305 n->location = @1;
8306 $$ = (Node *)n;
8308 | func_name '(' ALL expr_list ')'
8310 FuncCall *n = makeNode(FuncCall);
8311 n->funcname = $1;
8312 n->args = $4;
8313 n->agg_star = FALSE;
8314 n->agg_distinct = FALSE;
8315 /* Ideally we'd mark the FuncCall node to indicate
8316 * "must be an aggregate", but there's no provision
8317 * for that in FuncCall at the moment.
8319 n->func_variadic = FALSE;
8320 n->location = @1;
8321 $$ = (Node *)n;
8323 | func_name '(' DISTINCT expr_list ')'
8325 FuncCall *n = makeNode(FuncCall);
8326 n->funcname = $1;
8327 n->args = $4;
8328 n->agg_star = FALSE;
8329 n->agg_distinct = TRUE;
8330 n->func_variadic = FALSE;
8331 n->location = @1;
8332 $$ = (Node *)n;
8334 | func_name '(' '*' ')'
8337 * We consider AGGREGATE(*) to invoke a parameterless
8338 * aggregate. This does the right thing for COUNT(*),
8339 * and there are no other aggregates in SQL92 that accept
8340 * '*' as parameter.
8342 * The FuncCall node is also marked agg_star = true,
8343 * so that later processing can detect what the argument
8344 * really was.
8346 FuncCall *n = makeNode(FuncCall);
8347 n->funcname = $1;
8348 n->args = NIL;
8349 n->agg_star = TRUE;
8350 n->agg_distinct = FALSE;
8351 n->func_variadic = FALSE;
8352 n->location = @1;
8353 $$ = (Node *)n;
8355 | CURRENT_DATE
8358 * Translate as "'now'::text::date".
8360 * We cannot use "'now'::date" because coerce_type() will
8361 * immediately reduce that to a constant representing
8362 * today's date. We need to delay the conversion until
8363 * runtime, else the wrong things will happen when
8364 * CURRENT_DATE is used in a column default value or rule.
8366 * This could be simplified if we had a way to generate
8367 * an expression tree representing runtime application
8368 * of type-input conversion functions. (As of PG 7.3
8369 * that is actually possible, but not clear that we want
8370 * to rely on it.)
8372 Node *n;
8373 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8374 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8376 | CURRENT_TIME
8379 * Translate as "'now'::text::timetz".
8380 * See comments for CURRENT_DATE.
8382 Node *n;
8383 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8384 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8386 | CURRENT_TIME '(' Iconst ')'
8389 * Translate as "'now'::text::timetz(n)".
8390 * See comments for CURRENT_DATE.
8392 Node *n;
8393 TypeName *d;
8394 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8395 d = SystemTypeName("timetz");
8396 d->typmods = list_make1(makeIntConst($3, @3));
8397 $$ = makeTypeCast(n, d, -1);
8399 | CURRENT_TIMESTAMP
8402 * Translate as "now()", since we have a function that
8403 * does exactly what is needed.
8405 FuncCall *n = makeNode(FuncCall);
8406 n->funcname = SystemFuncName("now");
8407 n->args = NIL;
8408 n->agg_star = FALSE;
8409 n->agg_distinct = FALSE;
8410 n->func_variadic = FALSE;
8411 n->location = @1;
8412 $$ = (Node *)n;
8414 | CURRENT_TIMESTAMP '(' Iconst ')'
8417 * Translate as "'now'::text::timestamptz(n)".
8418 * See comments for CURRENT_DATE.
8420 Node *n;
8421 TypeName *d;
8422 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8423 d = SystemTypeName("timestamptz");
8424 d->typmods = list_make1(makeIntConst($3, @3));
8425 $$ = makeTypeCast(n, d, -1);
8427 | LOCALTIME
8430 * Translate as "'now'::text::time".
8431 * See comments for CURRENT_DATE.
8433 Node *n;
8434 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8435 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8437 | LOCALTIME '(' Iconst ')'
8440 * Translate as "'now'::text::time(n)".
8441 * See comments for CURRENT_DATE.
8443 Node *n;
8444 TypeName *d;
8445 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8446 d = SystemTypeName("time");
8447 d->typmods = list_make1(makeIntConst($3, @3));
8448 $$ = makeTypeCast((Node *)n, d, -1);
8450 | LOCALTIMESTAMP
8453 * Translate as "'now'::text::timestamp".
8454 * See comments for CURRENT_DATE.
8456 Node *n;
8457 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8458 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8460 | LOCALTIMESTAMP '(' Iconst ')'
8463 * Translate as "'now'::text::timestamp(n)".
8464 * See comments for CURRENT_DATE.
8466 Node *n;
8467 TypeName *d;
8468 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8469 d = SystemTypeName("timestamp");
8470 d->typmods = list_make1(makeIntConst($3, @3));
8471 $$ = makeTypeCast(n, d, -1);
8473 | CURRENT_ROLE
8475 FuncCall *n = makeNode(FuncCall);
8476 n->funcname = SystemFuncName("current_user");
8477 n->args = NIL;
8478 n->agg_star = FALSE;
8479 n->agg_distinct = FALSE;
8480 n->func_variadic = FALSE;
8481 n->location = @1;
8482 $$ = (Node *)n;
8484 | CURRENT_USER
8486 FuncCall *n = makeNode(FuncCall);
8487 n->funcname = SystemFuncName("current_user");
8488 n->args = NIL;
8489 n->agg_star = FALSE;
8490 n->agg_distinct = FALSE;
8491 n->func_variadic = FALSE;
8492 n->location = @1;
8493 $$ = (Node *)n;
8495 | SESSION_USER
8497 FuncCall *n = makeNode(FuncCall);
8498 n->funcname = SystemFuncName("session_user");
8499 n->args = NIL;
8500 n->agg_star = FALSE;
8501 n->agg_distinct = FALSE;
8502 n->func_variadic = FALSE;
8503 n->location = @1;
8504 $$ = (Node *)n;
8506 | USER
8508 FuncCall *n = makeNode(FuncCall);
8509 n->funcname = SystemFuncName("current_user");
8510 n->args = NIL;
8511 n->agg_star = FALSE;
8512 n->agg_distinct = FALSE;
8513 n->func_variadic = FALSE;
8514 n->location = @1;
8515 $$ = (Node *)n;
8517 | CURRENT_CATALOG
8519 FuncCall *n = makeNode(FuncCall);
8520 n->funcname = SystemFuncName("current_database");
8521 n->args = NIL;
8522 n->agg_star = FALSE;
8523 n->agg_distinct = FALSE;
8524 n->func_variadic = FALSE;
8525 n->location = @1;
8526 $$ = (Node *)n;
8528 | CURRENT_SCHEMA
8530 FuncCall *n = makeNode(FuncCall);
8531 n->funcname = SystemFuncName("current_schema");
8532 n->args = NIL;
8533 n->agg_star = FALSE;
8534 n->agg_distinct = FALSE;
8535 n->func_variadic = FALSE;
8536 n->location = @1;
8537 $$ = (Node *)n;
8539 | CAST '(' a_expr AS Typename ')'
8540 { $$ = makeTypeCast($3, $5, @1); }
8541 | EXTRACT '(' extract_list ')'
8543 FuncCall *n = makeNode(FuncCall);
8544 n->funcname = SystemFuncName("date_part");
8545 n->args = $3;
8546 n->agg_star = FALSE;
8547 n->agg_distinct = FALSE;
8548 n->func_variadic = FALSE;
8549 n->location = @1;
8550 $$ = (Node *)n;
8552 | OVERLAY '(' overlay_list ')'
8554 /* overlay(A PLACING B FROM C FOR D) is converted to
8555 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8556 * overlay(A PLACING B FROM C) is converted to
8557 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8559 FuncCall *n = makeNode(FuncCall);
8560 n->funcname = SystemFuncName("overlay");
8561 n->args = $3;
8562 n->agg_star = FALSE;
8563 n->agg_distinct = FALSE;
8564 n->func_variadic = FALSE;
8565 n->location = @1;
8566 $$ = (Node *)n;
8568 | POSITION '(' position_list ')'
8570 /* position(A in B) is converted to position(B, A) */
8571 FuncCall *n = makeNode(FuncCall);
8572 n->funcname = SystemFuncName("position");
8573 n->args = $3;
8574 n->agg_star = FALSE;
8575 n->agg_distinct = FALSE;
8576 n->func_variadic = FALSE;
8577 n->location = @1;
8578 $$ = (Node *)n;
8580 | SUBSTRING '(' substr_list ')'
8582 /* substring(A from B for C) is converted to
8583 * substring(A, B, C) - thomas 2000-11-28
8585 FuncCall *n = makeNode(FuncCall);
8586 n->funcname = SystemFuncName("substring");
8587 n->args = $3;
8588 n->agg_star = FALSE;
8589 n->agg_distinct = FALSE;
8590 n->func_variadic = FALSE;
8591 n->location = @1;
8592 $$ = (Node *)n;
8594 | TREAT '(' a_expr AS Typename ')'
8596 /* TREAT(expr AS target) converts expr of a particular type to target,
8597 * which is defined to be a subtype of the original expression.
8598 * In SQL99, this is intended for use with structured UDTs,
8599 * but let's make this a generally useful form allowing stronger
8600 * coercions than are handled by implicit casting.
8602 FuncCall *n = makeNode(FuncCall);
8603 /* Convert SystemTypeName() to SystemFuncName() even though
8604 * at the moment they result in the same thing.
8606 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8607 n->args = list_make1($3);
8608 n->agg_star = FALSE;
8609 n->agg_distinct = FALSE;
8610 n->func_variadic = FALSE;
8611 n->location = @1;
8612 $$ = (Node *)n;
8614 | TRIM '(' BOTH trim_list ')'
8616 /* various trim expressions are defined in SQL92
8617 * - thomas 1997-07-19
8619 FuncCall *n = makeNode(FuncCall);
8620 n->funcname = SystemFuncName("btrim");
8621 n->args = $4;
8622 n->agg_star = FALSE;
8623 n->agg_distinct = FALSE;
8624 n->func_variadic = FALSE;
8625 n->location = @1;
8626 $$ = (Node *)n;
8628 | TRIM '(' LEADING trim_list ')'
8630 FuncCall *n = makeNode(FuncCall);
8631 n->funcname = SystemFuncName("ltrim");
8632 n->args = $4;
8633 n->agg_star = FALSE;
8634 n->agg_distinct = FALSE;
8635 n->func_variadic = FALSE;
8636 n->location = @1;
8637 $$ = (Node *)n;
8639 | TRIM '(' TRAILING trim_list ')'
8641 FuncCall *n = makeNode(FuncCall);
8642 n->funcname = SystemFuncName("rtrim");
8643 n->args = $4;
8644 n->agg_star = FALSE;
8645 n->agg_distinct = FALSE;
8646 n->func_variadic = FALSE;
8647 n->location = @1;
8648 $$ = (Node *)n;
8650 | TRIM '(' trim_list ')'
8652 FuncCall *n = makeNode(FuncCall);
8653 n->funcname = SystemFuncName("btrim");
8654 n->args = $3;
8655 n->agg_star = FALSE;
8656 n->agg_distinct = FALSE;
8657 n->func_variadic = FALSE;
8658 n->location = @1;
8659 $$ = (Node *)n;
8661 | NULLIF '(' a_expr ',' a_expr ')'
8663 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
8665 | COALESCE '(' expr_list ')'
8667 CoalesceExpr *c = makeNode(CoalesceExpr);
8668 c->args = $3;
8669 c->location = @1;
8670 $$ = (Node *)c;
8672 | GREATEST '(' expr_list ')'
8674 MinMaxExpr *v = makeNode(MinMaxExpr);
8675 v->args = $3;
8676 v->op = IS_GREATEST;
8677 v->location = @1;
8678 $$ = (Node *)v;
8680 | LEAST '(' expr_list ')'
8682 MinMaxExpr *v = makeNode(MinMaxExpr);
8683 v->args = $3;
8684 v->op = IS_LEAST;
8685 v->location = @1;
8686 $$ = (Node *)v;
8688 | XMLCONCAT '(' expr_list ')'
8690 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
8692 | XMLELEMENT '(' NAME_P ColLabel ')'
8694 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
8696 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
8698 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
8700 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
8702 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
8704 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
8706 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
8708 | XMLFOREST '(' xml_attribute_list ')'
8710 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
8712 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
8714 XmlExpr *x = (XmlExpr *)
8715 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
8716 list_make2($4, makeBoolAConst($5, -1)),
8717 @1);
8718 x->xmloption = $3;
8719 $$ = (Node *)x;
8721 | XMLPI '(' NAME_P ColLabel ')'
8723 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
8725 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
8727 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
8729 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
8731 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
8732 list_make3($3, $5, $6), @1);
8734 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
8736 XmlSerialize *n = makeNode(XmlSerialize);
8737 n->xmloption = $3;
8738 n->expr = $4;
8739 n->typename = $6;
8740 n->location = @1;
8741 $$ = (Node *)n;
8746 * SQL/XML support
8748 xml_root_version: VERSION_P a_expr
8749 { $$ = $2; }
8750 | VERSION_P NO VALUE_P
8751 { $$ = makeNullAConst(-1); }
8754 opt_xml_root_standalone: ',' STANDALONE_P YES_P
8755 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
8756 | ',' STANDALONE_P NO
8757 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
8758 | ',' STANDALONE_P NO VALUE_P
8759 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
8760 | /*EMPTY*/
8761 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
8764 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
8767 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
8768 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
8771 xml_attribute_el: a_expr AS ColLabel
8773 $$ = makeNode(ResTarget);
8774 $$->name = $3;
8775 $$->indirection = NIL;
8776 $$->val = (Node *) $1;
8777 $$->location = @1;
8779 | a_expr
8781 $$ = makeNode(ResTarget);
8782 $$->name = NULL;
8783 $$->indirection = NIL;
8784 $$->val = (Node *) $1;
8785 $$->location = @1;
8789 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
8790 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
8793 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
8794 | STRIP_P WHITESPACE_P { $$ = FALSE; }
8795 | /*EMPTY*/ { $$ = FALSE; }
8799 * Supporting nonterminals for expressions.
8802 /* Explicit row production.
8804 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
8805 * without conflicting with the parenthesized a_expr production. Without the
8806 * ROW keyword, there must be more than one a_expr inside the parens.
8808 row: ROW '(' expr_list ')' { $$ = $3; }
8809 | ROW '(' ')' { $$ = NIL; }
8810 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
8813 sub_type: ANY { $$ = ANY_SUBLINK; }
8814 | SOME { $$ = ANY_SUBLINK; }
8815 | ALL { $$ = ALL_SUBLINK; }
8818 all_Op: Op { $$ = $1; }
8819 | MathOp { $$ = $1; }
8822 MathOp: '+' { $$ = "+"; }
8823 | '-' { $$ = "-"; }
8824 | '*' { $$ = "*"; }
8825 | '/' { $$ = "/"; }
8826 | '%' { $$ = "%"; }
8827 | '^' { $$ = "^"; }
8828 | '<' { $$ = "<"; }
8829 | '>' { $$ = ">"; }
8830 | '=' { $$ = "="; }
8833 qual_Op: Op
8834 { $$ = list_make1(makeString($1)); }
8835 | OPERATOR '(' any_operator ')'
8836 { $$ = $3; }
8839 qual_all_Op:
8840 all_Op
8841 { $$ = list_make1(makeString($1)); }
8842 | OPERATOR '(' any_operator ')'
8843 { $$ = $3; }
8846 subquery_Op:
8847 all_Op
8848 { $$ = list_make1(makeString($1)); }
8849 | OPERATOR '(' any_operator ')'
8850 { $$ = $3; }
8851 | LIKE
8852 { $$ = list_make1(makeString("~~")); }
8853 | NOT LIKE
8854 { $$ = list_make1(makeString("!~~")); }
8855 | ILIKE
8856 { $$ = list_make1(makeString("~~*")); }
8857 | NOT ILIKE
8858 { $$ = list_make1(makeString("!~~*")); }
8859 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
8860 * the regular expression is preprocessed by a function (similar_escape),
8861 * and the ~ operator for posix regular expressions is used.
8862 * x SIMILAR TO y -> x ~ similar_escape(y)
8863 * this transformation is made on the fly by the parser upwards.
8864 * however the SubLink structure which handles any/some/all stuff
8865 * is not ready for such a thing.
8869 expr_list: a_expr
8871 $$ = list_make1($1);
8873 | expr_list ',' a_expr
8875 $$ = lappend($1, $3);
8879 type_list: Typename { $$ = list_make1($1); }
8880 | type_list ',' Typename { $$ = lappend($1, $3); }
8883 array_expr: '[' expr_list ']'
8885 $$ = makeAArrayExpr($2, @1);
8887 | '[' array_expr_list ']'
8889 $$ = makeAArrayExpr($2, @1);
8891 | '[' ']'
8893 $$ = makeAArrayExpr(NIL, @1);
8897 array_expr_list: array_expr { $$ = list_make1($1); }
8898 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
8902 extract_list:
8903 extract_arg FROM a_expr
8905 $$ = list_make2(makeStringConst($1, @1), $3);
8907 | /*EMPTY*/ { $$ = NIL; }
8910 /* Allow delimited string Sconst in extract_arg as an SQL extension.
8911 * - thomas 2001-04-12
8913 extract_arg:
8914 IDENT { $$ = $1; }
8915 | YEAR_P { $$ = "year"; }
8916 | MONTH_P { $$ = "month"; }
8917 | DAY_P { $$ = "day"; }
8918 | HOUR_P { $$ = "hour"; }
8919 | MINUTE_P { $$ = "minute"; }
8920 | SECOND_P { $$ = "second"; }
8921 | Sconst { $$ = $1; }
8924 /* OVERLAY() arguments
8925 * SQL99 defines the OVERLAY() function:
8926 * o overlay(text placing text from int for int)
8927 * o overlay(text placing text from int)
8929 overlay_list:
8930 a_expr overlay_placing substr_from substr_for
8932 $$ = list_make4($1, $2, $3, $4);
8934 | a_expr overlay_placing substr_from
8936 $$ = list_make3($1, $2, $3);
8940 overlay_placing:
8941 PLACING a_expr
8942 { $$ = $2; }
8945 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
8947 position_list:
8948 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
8949 | /*EMPTY*/ { $$ = NIL; }
8952 /* SUBSTRING() arguments
8953 * SQL9x defines a specific syntax for arguments to SUBSTRING():
8954 * o substring(text from int for int)
8955 * o substring(text from int) get entire string from starting point "int"
8956 * o substring(text for int) get first "int" characters of string
8957 * o substring(text from pattern) get entire string matching pattern
8958 * o substring(text from pattern for escape) same with specified escape char
8959 * We also want to support generic substring functions which accept
8960 * the usual generic list of arguments. So we will accept both styles
8961 * here, and convert the SQL9x style to the generic list for further
8962 * processing. - thomas 2000-11-28
8964 substr_list:
8965 a_expr substr_from substr_for
8967 $$ = list_make3($1, $2, $3);
8969 | a_expr substr_for substr_from
8971 /* not legal per SQL99, but might as well allow it */
8972 $$ = list_make3($1, $3, $2);
8974 | a_expr substr_from
8976 $$ = list_make2($1, $2);
8978 | a_expr substr_for
8981 * Since there are no cases where this syntax allows
8982 * a textual FOR value, we forcibly cast the argument
8983 * to int4. The possible matches in pg_proc are
8984 * substring(text,int4) and substring(text,text),
8985 * and we don't want the parser to choose the latter,
8986 * which it is likely to do if the second argument
8987 * is unknown or doesn't have an implicit cast to int4.
8989 $$ = list_make3($1, makeIntConst(1, -1),
8990 makeTypeCast($2,
8991 SystemTypeName("int4"), -1));
8993 | expr_list
8995 $$ = $1;
8997 | /*EMPTY*/
8998 { $$ = NIL; }
9001 substr_from:
9002 FROM a_expr { $$ = $2; }
9005 substr_for: FOR a_expr { $$ = $2; }
9008 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
9009 | FROM expr_list { $$ = $2; }
9010 | expr_list { $$ = $1; }
9013 in_expr: select_with_parens
9015 SubLink *n = makeNode(SubLink);
9016 n->subselect = $1;
9017 /* other fields will be filled later */
9018 $$ = (Node *)n;
9020 | '(' expr_list ')' { $$ = (Node *)$2; }
9024 * Define SQL92-style case clause.
9025 * - Full specification
9026 * CASE WHEN a = b THEN c ... ELSE d END
9027 * - Implicit argument
9028 * CASE a WHEN b THEN c ... ELSE d END
9030 case_expr: CASE case_arg when_clause_list case_default END_P
9032 CaseExpr *c = makeNode(CaseExpr);
9033 c->casetype = InvalidOid; /* not analyzed yet */
9034 c->arg = (Expr *) $2;
9035 c->args = $3;
9036 c->defresult = (Expr *) $4;
9037 c->location = @1;
9038 $$ = (Node *)c;
9042 when_clause_list:
9043 /* There must be at least one */
9044 when_clause { $$ = list_make1($1); }
9045 | when_clause_list when_clause { $$ = lappend($1, $2); }
9048 when_clause:
9049 WHEN a_expr THEN a_expr
9051 CaseWhen *w = makeNode(CaseWhen);
9052 w->expr = (Expr *) $2;
9053 w->result = (Expr *) $4;
9054 w->location = @1;
9055 $$ = (Node *)w;
9059 case_default:
9060 ELSE a_expr { $$ = $2; }
9061 | /*EMPTY*/ { $$ = NULL; }
9064 case_arg: a_expr { $$ = $1; }
9065 | /*EMPTY*/ { $$ = NULL; }
9069 * columnref starts with relation_name not ColId, so that OLD and NEW
9070 * references can be accepted. Note that when there are more than two
9071 * dotted names, the first name is not actually a relation name...
9073 columnref: relation_name
9075 $$ = makeColumnRef($1, NIL, @1);
9077 | relation_name indirection
9079 $$ = makeColumnRef($1, $2, @1);
9083 indirection_el:
9084 '.' attr_name
9086 $$ = (Node *) makeString($2);
9088 | '.' '*'
9090 $$ = (Node *) makeNode(A_Star);
9092 | '[' a_expr ']'
9094 A_Indices *ai = makeNode(A_Indices);
9095 ai->lidx = NULL;
9096 ai->uidx = $2;
9097 $$ = (Node *) ai;
9099 | '[' a_expr ':' a_expr ']'
9101 A_Indices *ai = makeNode(A_Indices);
9102 ai->lidx = $2;
9103 ai->uidx = $4;
9104 $$ = (Node *) ai;
9108 indirection:
9109 indirection_el { $$ = list_make1($1); }
9110 | indirection indirection_el { $$ = lappend($1, $2); }
9113 opt_indirection:
9114 /*EMPTY*/ { $$ = NIL; }
9115 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9118 opt_asymmetric: ASYMMETRIC
9119 | /*EMPTY*/
9123 * The SQL spec defines "contextually typed value expressions" and
9124 * "contextually typed row value constructors", which for our purposes
9125 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9126 * the top level.
9129 ctext_expr:
9130 a_expr { $$ = (Node *) $1; }
9131 | DEFAULT
9133 SetToDefault *n = makeNode(SetToDefault);
9134 n->location = @1;
9135 $$ = (Node *) n;
9139 ctext_expr_list:
9140 ctext_expr { $$ = list_make1($1); }
9141 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9145 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9146 * making VALUES a fully reserved word, which will probably break more apps
9147 * than allowing the noise-word is worth.
9149 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9153 /*****************************************************************************
9155 * target list for SELECT
9157 *****************************************************************************/
9159 target_list:
9160 target_el { $$ = list_make1($1); }
9161 | target_list ',' target_el { $$ = lappend($1, $3); }
9164 target_el: a_expr AS ColLabel
9166 $$ = makeNode(ResTarget);
9167 $$->name = $3;
9168 $$->indirection = NIL;
9169 $$->val = (Node *)$1;
9170 $$->location = @1;
9173 * We support omitting AS only for column labels that aren't
9174 * any known keyword. There is an ambiguity against postfix
9175 * operators: is "a ! b" an infix expression, or a postfix
9176 * expression and a column label? We prefer to resolve this
9177 * as an infix expression, which we accomplish by assigning
9178 * IDENT a precedence higher than POSTFIXOP.
9180 | a_expr IDENT
9182 $$ = makeNode(ResTarget);
9183 $$->name = $2;
9184 $$->indirection = NIL;
9185 $$->val = (Node *)$1;
9186 $$->location = @1;
9188 | a_expr
9190 $$ = makeNode(ResTarget);
9191 $$->name = NULL;
9192 $$->indirection = NIL;
9193 $$->val = (Node *)$1;
9194 $$->location = @1;
9196 | '*'
9198 ColumnRef *n = makeNode(ColumnRef);
9199 n->fields = list_make1(makeNode(A_Star));
9200 n->location = @1;
9202 $$ = makeNode(ResTarget);
9203 $$->name = NULL;
9204 $$->indirection = NIL;
9205 $$->val = (Node *)n;
9206 $$->location = @1;
9211 /*****************************************************************************
9213 * Names and constants
9215 *****************************************************************************/
9217 relation_name:
9218 SpecialRuleRelation { $$ = $1; }
9219 | ColId { $$ = $1; }
9222 qualified_name_list:
9223 qualified_name { $$ = list_make1($1); }
9224 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9228 * The production for a qualified relation name has to exactly match the
9229 * production for a qualified func_name, because in a FROM clause we cannot
9230 * tell which we are parsing until we see what comes after it ('(' for a
9231 * func_name, something else for a relation). Therefore we allow 'indirection'
9232 * which may contain subscripts, and reject that case in the C code.
9234 qualified_name:
9235 relation_name
9237 $$ = makeNode(RangeVar);
9238 $$->catalogname = NULL;
9239 $$->schemaname = NULL;
9240 $$->relname = $1;
9241 $$->location = @1;
9243 | relation_name indirection
9245 check_qualified_name($2);
9246 $$ = makeNode(RangeVar);
9247 switch (list_length($2))
9249 case 1:
9250 $$->catalogname = NULL;
9251 $$->schemaname = $1;
9252 $$->relname = strVal(linitial($2));
9253 break;
9254 case 2:
9255 $$->catalogname = $1;
9256 $$->schemaname = strVal(linitial($2));
9257 $$->relname = strVal(lsecond($2));
9258 break;
9259 default:
9260 ereport(ERROR,
9261 (errcode(ERRCODE_SYNTAX_ERROR),
9262 errmsg("improper qualified name (too many dotted names): %s",
9263 NameListToString(lcons(makeString($1), $2))),
9264 scanner_errposition(@1)));
9265 break;
9267 $$->location = @1;
9271 name_list: name
9272 { $$ = list_make1(makeString($1)); }
9273 | name_list ',' name
9274 { $$ = lappend($1, makeString($3)); }
9278 name: ColId { $$ = $1; };
9280 database_name:
9281 ColId { $$ = $1; };
9283 access_method:
9284 ColId { $$ = $1; };
9286 attr_name: ColLabel { $$ = $1; };
9288 index_name: ColId { $$ = $1; };
9290 file_name: Sconst { $$ = $1; };
9293 * The production for a qualified func_name has to exactly match the
9294 * production for a qualified columnref, because we cannot tell which we
9295 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9296 * anything else for a columnref). Therefore we allow 'indirection' which
9297 * may contain subscripts, and reject that case in the C code. (If we
9298 * ever implement SQL99-like methods, such syntax may actually become legal!)
9300 func_name: type_function_name
9301 { $$ = list_make1(makeString($1)); }
9302 | relation_name indirection
9303 { $$ = check_func_name(lcons(makeString($1), $2)); }
9308 * Constants
9310 AexprConst: Iconst
9312 $$ = makeIntConst($1, @1);
9314 | FCONST
9316 $$ = makeFloatConst($1, @1);
9318 | Sconst
9320 $$ = makeStringConst($1, @1);
9322 | BCONST
9324 $$ = makeBitStringConst($1, @1);
9326 | XCONST
9328 /* This is a bit constant per SQL99:
9329 * Without Feature F511, "BIT data type",
9330 * a <general literal> shall not be a
9331 * <bit string literal> or a <hex string literal>.
9333 $$ = makeBitStringConst($1, @1);
9335 | func_name Sconst
9337 /* generic type 'literal' syntax */
9338 TypeName *t = makeTypeNameFromNameList($1);
9339 t->location = @1;
9340 $$ = makeStringConstCast($2, @2, t);
9342 | func_name '(' expr_list ')' Sconst
9344 /* generic syntax with a type modifier */
9345 TypeName *t = makeTypeNameFromNameList($1);
9346 t->typmods = $3;
9347 t->location = @1;
9348 $$ = makeStringConstCast($5, @5, t);
9350 | ConstTypename Sconst
9352 $$ = makeStringConstCast($2, @2, $1);
9354 | ConstInterval Sconst opt_interval
9356 TypeName *t = $1;
9357 t->typmods = $3;
9358 $$ = makeStringConstCast($2, @2, t);
9360 | ConstInterval '(' Iconst ')' Sconst opt_interval
9362 TypeName *t = $1;
9363 if ($6 != NIL)
9365 if (list_length($6) != 1)
9366 ereport(ERROR,
9367 (errcode(ERRCODE_SYNTAX_ERROR),
9368 errmsg("interval precision specified twice"),
9369 scanner_errposition(@1)));
9370 t->typmods = lappend($6, makeIntConst($3, @3));
9372 else
9373 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9374 makeIntConst($3, @3));
9375 $$ = makeStringConstCast($5, @5, t);
9377 | TRUE_P
9379 $$ = makeBoolAConst(TRUE, @1);
9381 | FALSE_P
9383 $$ = makeBoolAConst(FALSE, @1);
9385 | NULL_P
9387 $$ = makeNullAConst(@1);
9391 Iconst: ICONST { $$ = $1; };
9392 Sconst: SCONST { $$ = $1; };
9393 RoleId: ColId { $$ = $1; };
9395 SignedIconst: Iconst { $$ = $1; }
9396 | '+' Iconst { $$ = + $2; }
9397 | '-' Iconst { $$ = - $2; }
9401 * Name classification hierarchy.
9403 * IDENT is the lexeme returned by the lexer for identifiers that match
9404 * no known keyword. In most cases, we can accept certain keywords as
9405 * names, not only IDENTs. We prefer to accept as many such keywords
9406 * as possible to minimize the impact of "reserved words" on programmers.
9407 * So, we divide names into several possible classes. The classification
9408 * is chosen in part to make keywords acceptable as names wherever possible.
9411 /* Column identifier --- names that can be column, table, etc names.
9413 ColId: IDENT { $$ = $1; }
9414 | unreserved_keyword { $$ = pstrdup($1); }
9415 | col_name_keyword { $$ = pstrdup($1); }
9418 /* Type/function identifier --- names that can be type or function names.
9420 type_function_name: IDENT { $$ = $1; }
9421 | unreserved_keyword { $$ = pstrdup($1); }
9422 | type_func_name_keyword { $$ = pstrdup($1); }
9425 /* Column label --- allowed labels in "AS" clauses.
9426 * This presently includes *all* Postgres keywords.
9428 ColLabel: IDENT { $$ = $1; }
9429 | unreserved_keyword { $$ = pstrdup($1); }
9430 | col_name_keyword { $$ = pstrdup($1); }
9431 | type_func_name_keyword { $$ = pstrdup($1); }
9432 | reserved_keyword { $$ = pstrdup($1); }
9437 * Keyword category lists. Generally, every keyword present in
9438 * the Postgres grammar should appear in exactly one of these lists.
9440 * Put a new keyword into the first list that it can go into without causing
9441 * shift or reduce conflicts. The earlier lists define "less reserved"
9442 * categories of keywords.
9444 * Make sure that each keyword's category in keywords.c matches where
9445 * it is listed here. (Someday we may be able to generate these lists and
9446 * keywords.c's table from a common master list.)
9449 /* "Unreserved" keywords --- available for use as any kind of name.
9451 unreserved_keyword:
9452 ABORT_P
9453 | ABSOLUTE_P
9454 | ACCESS
9455 | ACTION
9456 | ADD_P
9457 | ADMIN
9458 | AFTER
9459 | AGGREGATE
9460 | ALSO
9461 | ALTER
9462 | ALWAYS
9463 | ASSERTION
9464 | ASSIGNMENT
9465 | AT
9466 | BACKWARD
9467 | BEFORE
9468 | BEGIN_P
9469 | BY
9470 | CACHE
9471 | CALLED
9472 | CASCADE
9473 | CASCADED
9474 | CATALOG_P
9475 | CHAIN
9476 | CHARACTERISTICS
9477 | CHECKPOINT
9478 | CLASS
9479 | CLOSE
9480 | CLUSTER
9481 | COMMENT
9482 | COMMIT
9483 | COMMITTED
9484 | CONCURRENTLY
9485 | CONFIGURATION
9486 | CONNECTION
9487 | CONSTRAINTS
9488 | CONTENT_P
9489 | CONTINUE_P
9490 | CONVERSION_P
9491 | COPY
9492 | COST
9493 | CREATEDB
9494 | CREATEROLE
9495 | CREATEUSER
9496 | CSV
9497 | CTYPE
9498 | CURRENT_P
9499 | CURSOR
9500 | CYCLE
9501 | DATA_P
9502 | DATABASE
9503 | DAY_P
9504 | DEALLOCATE
9505 | DECLARE
9506 | DEFAULTS
9507 | DEFERRED
9508 | DEFINER
9509 | DELETE_P
9510 | DELIMITER
9511 | DELIMITERS
9512 | DICTIONARY
9513 | DISABLE_P
9514 | DISCARD
9515 | DOCUMENT_P
9516 | DOMAIN_P
9517 | DOUBLE_P
9518 | DROP
9519 | EACH
9520 | ENABLE_P
9521 | ENCODING
9522 | ENCRYPTED
9523 | ENUM_P
9524 | ESCAPE
9525 | EXCLUDING
9526 | EXCLUSIVE
9527 | EXECUTE
9528 | EXPLAIN
9529 | EXTERNAL
9530 | FAMILY
9531 | FIRST_P
9532 | FORCE
9533 | FORWARD
9534 | FUNCTION
9535 | GLOBAL
9536 | GRANTED
9537 | HANDLER
9538 | HEADER_P
9539 | HOLD
9540 | HOUR_P
9541 | IDENTITY_P
9542 | IF_P
9543 | IMMEDIATE
9544 | IMMUTABLE
9545 | IMPLICIT_P
9546 | INCLUDING
9547 | INCREMENT
9548 | INDEX
9549 | INDEXES
9550 | INHERIT
9551 | INHERITS
9552 | INPUT_P
9553 | INSENSITIVE
9554 | INSERT
9555 | INSTEAD
9556 | INVOKER
9557 | ISOLATION
9558 | KEY
9559 | LANCOMPILER
9560 | LANGUAGE
9561 | LARGE_P
9562 | LAST_P
9563 | LEVEL
9564 | LISTEN
9565 | LOAD
9566 | LOCAL
9567 | LOCATION
9568 | LOCK_P
9569 | LOGIN_P
9570 | MAPPING
9571 | MATCH
9572 | MAXVALUE
9573 | MINUTE_P
9574 | MINVALUE
9575 | MODE
9576 | MONTH_P
9577 | MOVE
9578 | NAME_P
9579 | NAMES
9580 | NEXT
9581 | NO
9582 | NOCREATEDB
9583 | NOCREATEROLE
9584 | NOCREATEUSER
9585 | NOINHERIT
9586 | NOLOGIN_P
9587 | NOSUPERUSER
9588 | NOTHING
9589 | NOTIFY
9590 | NOWAIT
9591 | NULLS_P
9592 | OBJECT_P
9593 | OF
9594 | OIDS
9595 | OPERATOR
9596 | OPTION
9597 | OWNED
9598 | OWNER
9599 | PARSER
9600 | PARTIAL
9601 | PASSWORD
9602 | PLANS
9603 | PREPARE
9604 | PREPARED
9605 | PRESERVE
9606 | PRIOR
9607 | PRIVILEGES
9608 | PROCEDURAL
9609 | PROCEDURE
9610 | QUOTE
9611 | READ
9612 | REASSIGN
9613 | RECHECK
9614 | RECURSIVE
9615 | REINDEX
9616 | RELATIVE_P
9617 | RELEASE
9618 | RENAME
9619 | REPEATABLE
9620 | REPLACE
9621 | REPLICA
9622 | RESET
9623 | RESTART
9624 | RESTRICT
9625 | RETURNS
9626 | REVOKE
9627 | ROLE
9628 | ROLLBACK
9629 | ROWS
9630 | RULE
9631 | SAVEPOINT
9632 | SCHEMA
9633 | SCROLL
9634 | SEARCH
9635 | SECOND_P
9636 | SECURITY
9637 | SEQUENCE
9638 | SERIALIZABLE
9639 | SESSION
9640 | SET
9641 | SHARE
9642 | SHOW
9643 | SIMPLE
9644 | STABLE
9645 | STANDALONE_P
9646 | START
9647 | STATEMENT
9648 | STATISTICS
9649 | STDIN
9650 | STDOUT
9651 | STORAGE
9652 | STRICT_P
9653 | STRIP_P
9654 | SUPERUSER_P
9655 | SYSID
9656 | SYSTEM_P
9657 | TABLESPACE
9658 | TEMP
9659 | TEMPLATE
9660 | TEMPORARY
9661 | TEXT_P
9662 | TRANSACTION
9663 | TRIGGER
9664 | TRUNCATE
9665 | TRUSTED
9666 | TYPE_P
9667 | UNCOMMITTED
9668 | UNENCRYPTED
9669 | UNKNOWN
9670 | UNLISTEN
9671 | UNTIL
9672 | UPDATE
9673 | VACUUM
9674 | VALID
9675 | VALIDATOR
9676 | VALUE_P
9677 | VARYING
9678 | VERSION_P
9679 | VIEW
9680 | VOLATILE
9681 | WHITESPACE_P
9682 | WITHOUT
9683 | WORK
9684 | WRITE
9685 | XML_P
9686 | YEAR_P
9687 | YES_P
9688 | ZONE
9691 /* Column identifier --- keywords that can be column, table, etc names.
9693 * Many of these keywords will in fact be recognized as type or function
9694 * names too; but they have special productions for the purpose, and so
9695 * can't be treated as "generic" type or function names.
9697 * The type names appearing here are not usable as function names
9698 * because they can be followed by '(' in typename productions, which
9699 * looks too much like a function call for an LR(1) parser.
9701 col_name_keyword:
9702 BIGINT
9703 | BIT
9704 | BOOLEAN_P
9705 | CHAR_P
9706 | CHARACTER
9707 | COALESCE
9708 | DEC
9709 | DECIMAL_P
9710 | EXISTS
9711 | EXTRACT
9712 | FLOAT_P
9713 | GREATEST
9714 | INOUT
9715 | INT_P
9716 | INTEGER
9717 | INTERVAL
9718 | LEAST
9719 | NATIONAL
9720 | NCHAR
9721 | NONE
9722 | NULLIF
9723 | NUMERIC
9724 | OUT_P
9725 | OVERLAY
9726 | POSITION
9727 | PRECISION
9728 | REAL
9729 | ROW
9730 | SETOF
9731 | SMALLINT
9732 | SUBSTRING
9733 | TIME
9734 | TIMESTAMP
9735 | TREAT
9736 | TRIM
9737 | VALUES
9738 | VARCHAR
9739 | XMLATTRIBUTES
9740 | XMLCONCAT
9741 | XMLELEMENT
9742 | XMLFOREST
9743 | XMLPARSE
9744 | XMLPI
9745 | XMLROOT
9746 | XMLSERIALIZE
9749 /* Type/function identifier --- keywords that can be type or function names.
9751 * Most of these are keywords that are used as operators in expressions;
9752 * in general such keywords can't be column names because they would be
9753 * ambiguous with variables, but they are unambiguous as function identifiers.
9755 * Do not include POSITION, SUBSTRING, etc here since they have explicit
9756 * productions in a_expr to support the goofy SQL9x argument syntax.
9757 * - thomas 2000-11-28
9759 type_func_name_keyword:
9760 AUTHORIZATION
9761 | BETWEEN
9762 | BINARY
9763 | CROSS
9764 | CURRENT_SCHEMA
9765 | FREEZE
9766 | FULL
9767 | ILIKE
9768 | INNER_P
9769 | IS
9770 | ISNULL
9771 | JOIN
9772 | LEFT
9773 | LIKE
9774 | NATURAL
9775 | NOTNULL
9776 | OUTER_P
9777 | OVERLAPS
9778 | RIGHT
9779 | SIMILAR
9780 | VERBOSE
9783 /* Reserved keyword --- these keywords are usable only as a ColLabel.
9785 * Keywords appear here if they could not be distinguished from variable,
9786 * type, or function names in some contexts. Don't put things here unless
9787 * forced to.
9789 reserved_keyword:
9791 | ANALYSE
9792 | ANALYZE
9793 | AND
9794 | ANY
9795 | ARRAY
9796 | AS
9797 | ASC
9798 | ASYMMETRIC
9799 | BOTH
9800 | CASE
9801 | CAST
9802 | CHECK
9803 | COLLATE
9804 | COLUMN
9805 | CONSTRAINT
9806 | CREATE
9807 | CURRENT_CATALOG
9808 | CURRENT_DATE
9809 | CURRENT_ROLE
9810 | CURRENT_TIME
9811 | CURRENT_TIMESTAMP
9812 | CURRENT_USER
9813 | DEFAULT
9814 | DEFERRABLE
9815 | DESC
9816 | DISTINCT
9817 | DO
9818 | ELSE
9819 | END_P
9820 | EXCEPT
9821 | FALSE_P
9822 | FETCH
9823 | FOR
9824 | FOREIGN
9825 | FROM
9826 | GRANT
9827 | GROUP_P
9828 | HAVING
9829 | IN_P
9830 | INITIALLY
9831 | INTERSECT
9832 | INTO
9833 | LEADING
9834 | LIMIT
9835 | LOCALTIME
9836 | LOCALTIMESTAMP
9837 | NEW
9838 | NOT
9839 | NULL_P
9840 | OFF
9841 | OFFSET
9842 | OLD
9843 | ON
9844 | ONLY
9845 | OR
9846 | ORDER
9847 | PLACING
9848 | PRIMARY
9849 | REFERENCES
9850 | RETURNING
9851 | SELECT
9852 | SESSION_USER
9853 | SOME
9854 | SYMMETRIC
9855 | TABLE
9856 | THEN
9857 | TO
9858 | TRAILING
9859 | TRUE_P
9860 | UNION
9861 | UNIQUE
9862 | USER
9863 | USING
9864 | VARIADIC
9865 | WHEN
9866 | WHERE
9867 | WITH
9871 SpecialRuleRelation:
9874 if (QueryIsRule)
9875 $$ = "*OLD*";
9876 else
9877 ereport(ERROR,
9878 (errcode(ERRCODE_SYNTAX_ERROR),
9879 errmsg("OLD used in query that is not in a rule"),
9880 scanner_errposition(@1)));
9882 | NEW
9884 if (QueryIsRule)
9885 $$ = "*NEW*";
9886 else
9887 ereport(ERROR,
9888 (errcode(ERRCODE_SYNTAX_ERROR),
9889 errmsg("NEW used in query that is not in a rule"),
9890 scanner_errposition(@1)));
9896 static Node *
9897 makeColumnRef(char *colname, List *indirection, int location)
9900 * Generate a ColumnRef node, with an A_Indirection node added if there
9901 * is any subscripting in the specified indirection list. However,
9902 * any field selection at the start of the indirection list must be
9903 * transposed into the "fields" part of the ColumnRef node.
9905 ColumnRef *c = makeNode(ColumnRef);
9906 int nfields = 0;
9907 ListCell *l;
9909 c->location = location;
9910 foreach(l, indirection)
9912 if (IsA(lfirst(l), A_Indices))
9914 A_Indirection *i = makeNode(A_Indirection);
9916 if (nfields == 0)
9918 /* easy case - all indirection goes to A_Indirection */
9919 c->fields = list_make1(makeString(colname));
9920 i->indirection = check_indirection(indirection);
9922 else
9924 /* got to split the list in two */
9925 i->indirection = check_indirection(list_copy_tail(indirection,
9926 nfields));
9927 indirection = list_truncate(indirection, nfields);
9928 c->fields = lcons(makeString(colname), indirection);
9930 i->arg = (Node *) c;
9931 return (Node *) i;
9933 else if (IsA(lfirst(l), A_Star))
9935 /* We only allow '*' at the end of a ColumnRef */
9936 if (lnext(l) != NULL)
9937 yyerror("improper use of \"*\"");
9939 nfields++;
9941 /* No subscripting, so all indirection gets added to field list */
9942 c->fields = lcons(makeString(colname), indirection);
9943 return (Node *) c;
9946 static Node *
9947 makeTypeCast(Node *arg, TypeName *typename, int location)
9949 TypeCast *n = makeNode(TypeCast);
9950 n->arg = arg;
9951 n->typename = typename;
9952 n->location = location;
9953 return (Node *) n;
9956 static Node *
9957 makeStringConst(char *str, int location)
9959 A_Const *n = makeNode(A_Const);
9961 n->val.type = T_String;
9962 n->val.val.str = str;
9963 n->location = location;
9965 return (Node *)n;
9968 static Node *
9969 makeStringConstCast(char *str, int location, TypeName *typename)
9971 Node *s = makeStringConst(str, location);
9973 return makeTypeCast(s, typename, -1);
9976 static Node *
9977 makeIntConst(int val, int location)
9979 A_Const *n = makeNode(A_Const);
9981 n->val.type = T_Integer;
9982 n->val.val.ival = val;
9983 n->location = location;
9985 return (Node *)n;
9988 static Node *
9989 makeFloatConst(char *str, int location)
9991 A_Const *n = makeNode(A_Const);
9993 n->val.type = T_Float;
9994 n->val.val.str = str;
9995 n->location = location;
9997 return (Node *)n;
10000 static Node *
10001 makeBitStringConst(char *str, int location)
10003 A_Const *n = makeNode(A_Const);
10005 n->val.type = T_BitString;
10006 n->val.val.str = str;
10007 n->location = location;
10009 return (Node *)n;
10012 static Node *
10013 makeNullAConst(int location)
10015 A_Const *n = makeNode(A_Const);
10017 n->val.type = T_Null;
10018 n->location = location;
10020 return (Node *)n;
10023 static Node *
10024 makeAConst(Value *v, int location)
10026 Node *n;
10028 switch (v->type)
10030 case T_Float:
10031 n = makeFloatConst(v->val.str, location);
10032 break;
10034 case T_Integer:
10035 n = makeIntConst(v->val.ival, location);
10036 break;
10038 case T_String:
10039 default:
10040 n = makeStringConst(v->val.str, location);
10041 break;
10044 return n;
10047 /* makeBoolAConst()
10048 * Create an A_Const string node and put it inside a boolean cast.
10050 static Node *
10051 makeBoolAConst(bool state, int location)
10053 A_Const *n = makeNode(A_Const);
10055 n->val.type = T_String;
10056 n->val.val.str = (state ? "t" : "f");
10057 n->location = location;
10059 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
10062 /* makeOverlaps()
10063 * Create and populate a FuncCall node to support the OVERLAPS operator.
10065 static FuncCall *
10066 makeOverlaps(List *largs, List *rargs, int location)
10068 FuncCall *n = makeNode(FuncCall);
10070 n->funcname = SystemFuncName("overlaps");
10071 if (list_length(largs) == 1)
10072 largs = lappend(largs, largs);
10073 else if (list_length(largs) != 2)
10074 ereport(ERROR,
10075 (errcode(ERRCODE_SYNTAX_ERROR),
10076 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10077 scanner_errposition(location)));
10078 if (list_length(rargs) == 1)
10079 rargs = lappend(rargs, rargs);
10080 else if (list_length(rargs) != 2)
10081 ereport(ERROR,
10082 (errcode(ERRCODE_SYNTAX_ERROR),
10083 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10084 scanner_errposition(location)));
10085 n->args = list_concat(largs, rargs);
10086 n->agg_star = FALSE;
10087 n->agg_distinct = FALSE;
10088 n->func_variadic = FALSE;
10089 n->location = location;
10090 return n;
10093 /* check_qualified_name --- check the result of qualified_name production
10095 * It's easiest to let the grammar production for qualified_name allow
10096 * subscripts and '*', which we then must reject here.
10098 static void
10099 check_qualified_name(List *names)
10101 ListCell *i;
10103 foreach(i, names)
10105 if (!IsA(lfirst(i), String))
10106 yyerror("syntax error");
10110 /* check_func_name --- check the result of func_name production
10112 * It's easiest to let the grammar production for func_name allow subscripts
10113 * and '*', which we then must reject here.
10115 static List *
10116 check_func_name(List *names)
10118 ListCell *i;
10120 foreach(i, names)
10122 if (!IsA(lfirst(i), String))
10123 yyerror("syntax error");
10125 return names;
10128 /* check_indirection --- check the result of indirection production
10130 * We only allow '*' at the end of the list, but it's hard to enforce that
10131 * in the grammar, so do it here.
10133 static List *
10134 check_indirection(List *indirection)
10136 ListCell *l;
10138 foreach(l, indirection)
10140 if (IsA(lfirst(l), A_Star))
10142 if (lnext(l) != NULL)
10143 yyerror("improper use of \"*\"");
10146 return indirection;
10149 /* extractArgTypes()
10150 * Given a list of FunctionParameter nodes, extract a list of just the
10151 * argument types (TypeNames) for input parameters only. This is what
10152 * is needed to look up an existing function, which is what is wanted by
10153 * the productions that use this call.
10155 static List *
10156 extractArgTypes(List *parameters)
10158 List *result = NIL;
10159 ListCell *i;
10161 foreach(i, parameters)
10163 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10165 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10166 result = lappend(result, p->argType);
10168 return result;
10171 /* findLeftmostSelect()
10172 * Find the leftmost component SelectStmt in a set-operation parsetree.
10174 static SelectStmt *
10175 findLeftmostSelect(SelectStmt *node)
10177 while (node && node->op != SETOP_NONE)
10178 node = node->larg;
10179 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10180 return node;
10183 /* insertSelectOptions()
10184 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10186 * This routine is just to avoid duplicating code in SelectStmt productions.
10188 static void
10189 insertSelectOptions(SelectStmt *stmt,
10190 List *sortClause, List *lockingClause,
10191 Node *limitOffset, Node *limitCount,
10192 WithClause *withClause)
10194 Assert(IsA(stmt, SelectStmt));
10197 * Tests here are to reject constructs like
10198 * (SELECT foo ORDER BY bar) ORDER BY baz
10200 if (sortClause)
10202 if (stmt->sortClause)
10203 ereport(ERROR,
10204 (errcode(ERRCODE_SYNTAX_ERROR),
10205 errmsg("multiple ORDER BY clauses not allowed"),
10206 scanner_errposition(exprLocation((Node *) sortClause))));
10207 stmt->sortClause = sortClause;
10209 /* We can handle multiple locking clauses, though */
10210 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10211 if (limitOffset)
10213 if (stmt->limitOffset)
10214 ereport(ERROR,
10215 (errcode(ERRCODE_SYNTAX_ERROR),
10216 errmsg("multiple OFFSET clauses not allowed"),
10217 scanner_errposition(exprLocation(limitOffset))));
10218 stmt->limitOffset = limitOffset;
10220 if (limitCount)
10222 if (stmt->limitCount)
10223 ereport(ERROR,
10224 (errcode(ERRCODE_SYNTAX_ERROR),
10225 errmsg("multiple LIMIT clauses not allowed"),
10226 scanner_errposition(exprLocation(limitCount))));
10227 stmt->limitCount = limitCount;
10229 if (withClause)
10231 if (stmt->withClause)
10232 ereport(ERROR,
10233 (errcode(ERRCODE_SYNTAX_ERROR),
10234 errmsg("multiple WITH clauses not allowed"),
10235 scanner_errposition(exprLocation((Node *) withClause))));
10236 stmt->withClause = withClause;
10240 static Node *
10241 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10243 SelectStmt *n = makeNode(SelectStmt);
10245 n->op = op;
10246 n->all = all;
10247 n->larg = (SelectStmt *) larg;
10248 n->rarg = (SelectStmt *) rarg;
10249 return (Node *) n;
10252 /* SystemFuncName()
10253 * Build a properly-qualified reference to a built-in function.
10255 List *
10256 SystemFuncName(char *name)
10258 return list_make2(makeString("pg_catalog"), makeString(name));
10261 /* SystemTypeName()
10262 * Build a properly-qualified reference to a built-in type.
10264 * typmod is defaulted, but may be changed afterwards by caller.
10265 * Likewise for the location.
10267 TypeName *
10268 SystemTypeName(char *name)
10270 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10271 makeString(name)));
10274 /* doNegate()
10275 * Handle negation of a numeric constant.
10277 * Formerly, we did this here because the optimizer couldn't cope with
10278 * indexquals that looked like "var = -4" --- it wants "var = const"
10279 * and a unary minus operator applied to a constant didn't qualify.
10280 * As of Postgres 7.0, that problem doesn't exist anymore because there
10281 * is a constant-subexpression simplifier in the optimizer. However,
10282 * there's still a good reason for doing this here, which is that we can
10283 * postpone committing to a particular internal representation for simple
10284 * negative constants. It's better to leave "-123.456" in string form
10285 * until we know what the desired type is.
10287 static Node *
10288 doNegate(Node *n, int location)
10290 if (IsA(n, A_Const))
10292 A_Const *con = (A_Const *)n;
10294 /* report the constant's location as that of the '-' sign */
10295 con->location = location;
10297 if (con->val.type == T_Integer)
10299 con->val.val.ival = -con->val.val.ival;
10300 return n;
10302 if (con->val.type == T_Float)
10304 doNegateFloat(&con->val);
10305 return n;
10309 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10312 static void
10313 doNegateFloat(Value *v)
10315 char *oldval = v->val.str;
10317 Assert(IsA(v, Float));
10318 if (*oldval == '+')
10319 oldval++;
10320 if (*oldval == '-')
10321 v->val.str = oldval+1; /* just strip the '-' */
10322 else
10324 char *newval = (char *) palloc(strlen(oldval) + 2);
10326 *newval = '-';
10327 strcpy(newval+1, oldval);
10328 v->val.str = newval;
10332 static Node *
10333 makeAArrayExpr(List *elements, int location)
10335 A_ArrayExpr *n = makeNode(A_ArrayExpr);
10337 n->elements = elements;
10338 n->location = location;
10339 return (Node *) n;
10342 static Node *
10343 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
10344 int location)
10346 XmlExpr *x = makeNode(XmlExpr);
10348 x->op = op;
10349 x->name = name;
10351 * named_args is a list of ResTarget; it'll be split apart into separate
10352 * expression and name lists in transformXmlExpr().
10354 x->named_args = named_args;
10355 x->arg_names = NIL;
10356 x->args = args;
10357 /* xmloption, if relevant, must be filled in by caller */
10358 /* type and typmod will be filled in during parse analysis */
10359 x->location = location;
10360 return (Node *) x;
10363 /* parser_init()
10364 * Initialize to parse one query string
10366 void
10367 parser_init(void)
10369 QueryIsRule = FALSE;
10373 * Merge the input and output parameters of a table function.
10375 static List *
10376 mergeTableFuncParameters(List *func_args, List *columns)
10378 ListCell *lc;
10380 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
10381 foreach(lc, func_args)
10383 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
10385 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
10386 ereport(ERROR,
10387 (errcode(ERRCODE_SYNTAX_ERROR),
10388 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
10391 return list_concat(func_args, columns);
10395 * Determine return type of a TABLE function. A single result column
10396 * returns setof that column's type; otherwise return setof record.
10398 static TypeName *
10399 TableFuncTypeName(List *columns)
10401 TypeName *result;
10403 if (list_length(columns) == 1)
10405 FunctionParameter *p = (FunctionParameter *) linitial(columns);
10407 result = (TypeName *) copyObject(p->argType);
10409 else
10410 result = SystemTypeName("record");
10412 result->setof = true;
10414 return result;
10418 * Must undefine base_yylex before including scan.c, since we want it
10419 * to create the function base_yylex not filtered_base_yylex.
10421 #undef base_yylex
10423 #include "scan.c"