Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / parser / gram.y
blobde760b5d574f1e23b369cf775ba8402508caa4f5
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 %name-prefix="base_yy"
136 %locations
138 %union
140 int ival;
141 char chr;
142 char *str;
143 const char *keyword;
144 bool boolean;
145 JoinType jtype;
146 DropBehavior dbehavior;
147 OnCommitAction oncommit;
148 List *list;
149 Node *node;
150 Value *value;
151 ObjectType objtype;
153 TypeName *typnam;
154 FunctionParameter *fun_param;
155 FunctionParameterMode fun_param_mode;
156 FuncWithArgs *funwithargs;
157 DefElem *defelt;
158 SortBy *sortby;
159 JoinExpr *jexpr;
160 IndexElem *ielem;
161 Alias *alias;
162 RangeVar *range;
163 IntoClause *into;
164 WithClause *with;
165 A_Indices *aind;
166 ResTarget *target;
167 PrivTarget *privtarget;
169 InsertStmt *istmt;
170 VariableSetStmt *vsetstmt;
173 %type <node> stmt schema_stmt
174 AlterDatabaseStmt AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt
175 AlterObjectSchemaStmt AlterOwnerStmt AlterSeqStmt AlterTableStmt
176 AlterUserStmt AlterUserSetStmt AlterRoleStmt AlterRoleSetStmt
177 AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
178 ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
179 CreateDomainStmt CreateGroupStmt CreateOpClassStmt
180 CreateOpFamilyStmt AlterOpFamilyStmt CreatePLangStmt
181 CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
182 CreateAssertStmt CreateTrigStmt CreateUserStmt CreateRoleStmt
183 CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt DiscardStmt
184 DropGroupStmt DropOpClassStmt DropOpFamilyStmt DropPLangStmt DropStmt
185 DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt DropRoleStmt
186 DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
187 GrantStmt GrantRoleStmt IndexStmt InsertStmt ListenStmt LoadStmt
188 LockStmt NotifyStmt ExplainableStmt PreparableStmt
189 CreateFunctionStmt AlterFunctionStmt ReindexStmt RemoveAggrStmt
190 RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt RevokeRoleStmt
191 RuleActionStmt RuleActionStmtOrEmpty RuleStmt
192 SelectStmt TransactionStmt TruncateStmt
193 UnlistenStmt UpdateStmt VacuumStmt
194 VariableResetStmt VariableSetStmt VariableShowStmt
195 ViewStmt CheckPointStmt CreateConversionStmt
196 DeallocateStmt PrepareStmt ExecuteStmt
197 DropOwnedStmt ReassignOwnedStmt
198 AlterTSConfigurationStmt AlterTSDictionaryStmt
200 %type <node> select_no_parens select_with_parens select_clause
201 simple_select values_clause
203 %type <node> alter_column_default opclass_item opclass_drop alter_using
204 %type <ival> add_drop opt_asc_desc opt_nulls_order
206 %type <node> alter_table_cmd
207 %type <list> alter_table_cmds
209 %type <dbehavior> opt_drop_behavior
211 %type <list> createdb_opt_list alterdb_opt_list copy_opt_list
212 transaction_mode_list
213 %type <defelt> createdb_opt_item alterdb_opt_item copy_opt_item
214 transaction_mode_item
216 %type <ival> opt_lock lock_type cast_context
217 %type <boolean> opt_force opt_or_replace
218 opt_grant_grant_option opt_grant_admin_option
219 opt_nowait opt_if_exists opt_with_data
221 %type <list> OptRoleList
222 %type <defelt> OptRoleElem
224 %type <str> OptSchemaName
225 %type <list> OptSchemaEltList
227 %type <boolean> TriggerActionTime TriggerForSpec opt_trusted opt_restart_seqs
228 %type <str> opt_lancompiler
230 %type <str> TriggerEvents
231 %type <value> TriggerFuncArg
233 %type <str> relation_name copy_file_name
234 database_name access_method_clause access_method attr_name
235 index_name name file_name cluster_index_specification
237 %type <list> func_name handler_name qual_Op qual_all_Op subquery_Op
238 opt_class opt_validator
240 %type <range> qualified_name OptConstrFromTable
242 %type <str> all_Op MathOp SpecialRuleRelation
244 %type <str> iso_level opt_encoding
245 %type <node> grantee
246 %type <list> grantee_list
247 %type <str> privilege
248 %type <list> privileges privilege_list
249 %type <privtarget> privilege_target
250 %type <funwithargs> function_with_argtypes
251 %type <list> function_with_argtypes_list
252 %type <chr> TriggerOneEvent
254 %type <list> stmtblock stmtmulti
255 OptTableElementList TableElementList OptInherit definition
256 OptWith opt_distinct opt_definition func_args func_args_list
257 func_as createfunc_opt_list alterfunc_opt_list
258 aggr_args old_aggr_definition old_aggr_list
259 oper_argtypes RuleActionList RuleActionMulti
260 opt_column_list columnList opt_name_list
261 sort_clause opt_sort_clause sortby_list index_params
262 name_list from_clause from_list opt_array_bounds
263 qualified_name_list any_name any_name_list
264 any_operator expr_list attrs
265 target_list insert_column_list set_target_list
266 set_clause_list set_clause multiple_set_clause
267 ctext_expr_list ctext_row def_list indirection opt_indirection
268 group_clause TriggerFuncArgs select_limit
269 opt_select_limit opclass_item_list opclass_drop_list
270 opt_opfamily transaction_mode_list_or_empty
271 TableFuncElementList opt_type_modifiers
272 prep_type_clause
273 execute_param_clause using_clause returning_clause
274 enum_val_list table_func_column_list
276 %type <range> OptTempTableName
277 %type <into> into_clause create_as_target
279 %type <defelt> createfunc_opt_item common_func_opt_item
280 %type <fun_param> func_arg table_func_column
281 %type <fun_param_mode> arg_class
282 %type <typnam> func_return func_type
284 %type <boolean> TriggerForType OptTemp
285 %type <oncommit> OnCommitOption
287 %type <node> for_locking_item
288 %type <list> for_locking_clause opt_for_locking_clause for_locking_items
289 %type <list> locked_rels_list
290 %type <boolean> opt_all
292 %type <node> join_outer join_qual
293 %type <jtype> join_type
295 %type <list> extract_list overlay_list position_list
296 %type <list> substr_list trim_list
297 %type <list> opt_interval interval_second
298 %type <node> overlay_placing substr_from substr_for
300 %type <boolean> opt_instead opt_analyze
301 %type <boolean> index_opt_unique opt_verbose opt_full
302 %type <boolean> opt_freeze opt_default opt_recheck
303 %type <defelt> opt_binary opt_oids copy_delimiter
305 %type <boolean> copy_from
307 %type <ival> opt_column event cursor_options opt_hold opt_set_data
308 %type <objtype> reindex_type drop_type comment_type
310 %type <node> fetch_direction select_limit_value select_offset_value
311 select_offset_value2 opt_select_fetch_first_value
312 %type <ival> row_or_rows first_or_next
314 %type <list> OptSeqOptList SeqOptList
315 %type <defelt> SeqOptElem
317 %type <istmt> insert_rest
319 %type <vsetstmt> set_rest SetResetClause
321 %type <node> TableElement ConstraintElem TableFuncElement
322 %type <node> columnDef
323 %type <defelt> def_elem old_aggr_elem
324 %type <node> def_arg columnElem where_clause where_or_current_clause
325 a_expr b_expr c_expr func_expr AexprConst indirection_el
326 columnref in_expr having_clause func_table array_expr
327 %type <list> row type_list array_expr_list
328 %type <node> case_expr case_arg when_clause case_default
329 %type <list> when_clause_list
330 %type <ival> sub_type
331 %type <list> OptCreateAs CreateAsList
332 %type <node> CreateAsElement ctext_expr
333 %type <value> NumericOnly FloatOnly IntegerOnly
334 %type <alias> alias_clause
335 %type <sortby> sortby
336 %type <ielem> index_elem
337 %type <node> table_ref
338 %type <jexpr> joined_table
339 %type <range> relation_expr
340 %type <range> relation_expr_opt_alias
341 %type <target> target_el single_set_clause set_target insert_column_item
343 %type <typnam> Typename SimpleTypename ConstTypename
344 GenericType Numeric opt_float
345 Character ConstCharacter
346 CharacterWithLength CharacterWithoutLength
347 ConstDatetime ConstInterval
348 Bit ConstBit BitWithLength BitWithoutLength
349 %type <str> character
350 %type <str> extract_arg
351 %type <str> opt_charset
352 %type <boolean> opt_varying opt_timezone
354 %type <ival> Iconst SignedIconst
355 %type <str> Sconst comment_text
356 %type <str> RoleId opt_granted_by opt_boolean ColId_or_Sconst
357 %type <list> var_list
358 %type <str> ColId ColLabel var_name type_function_name param_name
359 %type <node> var_value zone_value
361 %type <keyword> unreserved_keyword type_func_name_keyword
362 %type <keyword> col_name_keyword reserved_keyword
364 %type <node> TableConstraint TableLikeClause
365 %type <list> TableLikeOptionList
366 %type <ival> TableLikeOption
367 %type <list> ColQualList
368 %type <node> ColConstraint ColConstraintElem ConstraintAttr
369 %type <ival> key_actions key_delete key_match key_update key_action
370 %type <ival> ConstraintAttributeSpec ConstraintDeferrabilitySpec
371 ConstraintTimeSpec
373 %type <list> constraints_set_list
374 %type <boolean> constraints_set_mode
375 %type <str> OptTableSpace OptConsTableSpace OptTableSpaceOwner
376 %type <list> opt_check_option
378 %type <target> xml_attribute_el
379 %type <list> xml_attribute_list xml_attributes
380 %type <node> xml_root_version opt_xml_root_standalone
381 %type <ival> document_or_content
382 %type <boolean> xml_whitespace_option
384 %type <node> common_table_expr
385 %type <with> with_clause
386 %type <list> cte_list
390 * If you make any token changes, update the keyword table in
391 * parser/keywords.c and add new keywords to the appropriate one of
392 * the reserved-or-not-so-reserved keyword lists, below; search
393 * this file for "Name classification hierarchy".
396 /* ordinary key words in alphabetical order */
397 %token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD_P ADMIN AFTER
398 AGGREGATE ALL ALSO ALTER ALWAYS ANALYSE ANALYZE AND ANY ARRAY AS ASC
399 ASSERTION ASSIGNMENT ASYMMETRIC AT AUTHORIZATION
401 BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
402 BOOLEAN_P BOTH BY
404 CACHE CALLED CASCADE CASCADED CASE CAST CATALOG_P CHAIN CHAR_P
405 CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
406 CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
407 COMMITTED CONCURRENTLY CONFIGURATION CONNECTION CONSTRAINT CONSTRAINTS
408 CONTENT_P CONTINUE_P CONVERSION_P COPY COST CREATE CREATEDB
409 CREATEROLE CREATEUSER CROSS CSV CTYPE CURRENT_P
410 CURRENT_CATALOG CURRENT_DATE CURRENT_ROLE CURRENT_SCHEMA
411 CURRENT_TIME CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
413 DATA_P DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
414 DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS DESC
415 DICTIONARY DISABLE_P DISCARD DISTINCT DO DOCUMENT_P DOMAIN_P DOUBLE_P DROP
417 EACH ELSE ENABLE_P ENCODING ENCRYPTED END_P ENUM_P ESCAPE EXCEPT EXCLUDING
418 EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
420 FALSE_P FAMILY FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
421 FREEZE FROM FULL FUNCTION
423 GLOBAL GRANT GRANTED GREATEST GROUP_P
425 HANDLER HAVING HEADER_P HOLD HOUR_P
427 IDENTITY_P IF_P ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P
428 INCLUDING INCREMENT INDEX INDEXES INHERIT INHERITS INITIALLY
429 INNER_P INOUT INPUT_P INSENSITIVE INSERT INSTEAD INT_P INTEGER
430 INTERSECT INTERVAL INTO INVOKER IS ISNULL ISOLATION
432 JOIN
436 LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEAST LEFT LEVEL
437 LIKE LIMIT LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
438 LOCK_P LOGIN_P
440 MAPPING MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
442 NAME_P NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
443 NOCREATEROLE NOCREATEUSER NOINHERIT NOLOGIN_P NONE NOSUPERUSER
444 NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P NULLIF NULLS_P NUMERIC
446 OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
447 ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNED OWNER
449 PARSER PARTIAL PASSWORD PLACING PLANS POSITION
450 PRECISION PRESERVE PREPARE PREPARED PRIMARY
451 PRIOR PRIVILEGES PROCEDURAL PROCEDURE
453 QUOTE
455 READ REAL REASSIGN RECHECK RECURSIVE REFERENCES REINDEX RELATIVE_P RELEASE
456 RENAME REPEATABLE REPLACE REPLICA RESET RESTART RESTRICT RETURNING RETURNS
457 REVOKE RIGHT ROLE ROLLBACK ROW ROWS RULE
459 SAVEPOINT SCHEMA SCROLL SEARCH SECOND_P SECURITY SELECT SEQUENCE
460 SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
461 SHOW SIMILAR SIMPLE SMALLINT SOME STABLE STANDALONE_P START STATEMENT
462 STATISTICS STDIN STDOUT STORAGE STRICT_P STRIP_P SUBSTRING SUPERUSER_P
463 SYMMETRIC SYSID SYSTEM_P
465 TABLE TABLESPACE TEMP TEMPLATE TEMPORARY TEXT_P THEN TIME TIMESTAMP
466 TO TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
467 TRUNCATE TRUSTED TYPE_P
469 UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
470 UPDATE USER USING
472 VACUUM VALID VALIDATOR VALUE_P VALUES VARCHAR VARIADIC VARYING
473 VERBOSE VERSION_P VIEW VOLATILE
475 WHEN WHERE WHITESPACE_P WITH WITHOUT WORK WRITE
477 XML_P XMLATTRIBUTES XMLCONCAT XMLELEMENT XMLFOREST XMLPARSE
478 XMLPI XMLROOT XMLSERIALIZE
480 YEAR_P YES_P
482 ZONE
484 /* The grammar thinks these are keywords, but they are not in the keywords.c
485 * list and so can never be entered directly. The filter in parser.c
486 * creates these tokens when required.
488 %token NULLS_FIRST NULLS_LAST WITH_TIME
490 /* Special token types, not actually keywords - see the "lex" file */
491 %token <str> IDENT FCONST SCONST BCONST XCONST Op
492 %token <ival> ICONST PARAM
494 /* precedence: lowest to highest */
495 %nonassoc SET /* see relation_expr_opt_alias */
496 %left UNION EXCEPT
497 %left INTERSECT
498 %left OR
499 %left AND
500 %right NOT
501 %right '='
502 %nonassoc '<' '>'
503 %nonassoc LIKE ILIKE SIMILAR
504 %nonassoc ESCAPE
505 %nonassoc OVERLAPS
506 %nonassoc BETWEEN
507 %nonassoc IN_P
508 %left POSTFIXOP /* dummy for postfix Op rules */
509 %nonassoc IDENT /* to support target_el without AS */
510 %left Op OPERATOR /* multi-character ops and user-defined operators */
511 %nonassoc NOTNULL
512 %nonassoc ISNULL
513 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
514 %left '+' '-'
515 %left '*' '/' '%'
516 %left '^'
517 /* Unary Operators */
518 %left AT ZONE /* sets precedence for AT TIME ZONE */
519 %right UMINUS
520 %left '[' ']'
521 %left '(' ')'
522 %left TYPECAST
523 %left '.'
525 * These might seem to be low-precedence, but actually they are not part
526 * of the arithmetic hierarchy at all in their use as JOIN operators.
527 * We make them high-precedence to support their use as function names.
528 * They wouldn't be given a precedence at all, were it not that we need
529 * left-associativity among the JOIN rules themselves.
531 %left JOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
532 /* kluge to keep xml_whitespace_option from causing shift/reduce conflicts */
533 %right PRESERVE STRIP_P
537 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
538 * psql already handles such cases, but other interfaces don't.
539 * bjm 1999/10/05
541 stmtblock: stmtmulti { parsetree = $1; }
544 /* the thrashing around here is to discard "empty" statements... */
545 stmtmulti: stmtmulti ';' stmt
546 { if ($3 != NULL)
547 $$ = lappend($1, $3);
548 else
549 $$ = $1;
551 | stmt
552 { if ($1 != NULL)
553 $$ = list_make1($1);
554 else
555 $$ = NIL;
559 stmt :
560 AlterDatabaseStmt
561 | AlterDatabaseSetStmt
562 | AlterDomainStmt
563 | AlterFunctionStmt
564 | AlterGroupStmt
565 | AlterObjectSchemaStmt
566 | AlterOwnerStmt
567 | AlterSeqStmt
568 | AlterTableStmt
569 | AlterRoleSetStmt
570 | AlterRoleStmt
571 | AlterTSConfigurationStmt
572 | AlterTSDictionaryStmt
573 | AlterUserSetStmt
574 | AlterUserStmt
575 | AnalyzeStmt
576 | CheckPointStmt
577 | ClosePortalStmt
578 | ClusterStmt
579 | CommentStmt
580 | ConstraintsSetStmt
581 | CopyStmt
582 | CreateAsStmt
583 | CreateAssertStmt
584 | CreateCastStmt
585 | CreateConversionStmt
586 | CreateDomainStmt
587 | CreateFunctionStmt
588 | CreateGroupStmt
589 | CreateOpClassStmt
590 | CreateOpFamilyStmt
591 | AlterOpFamilyStmt
592 | CreatePLangStmt
593 | CreateSchemaStmt
594 | CreateSeqStmt
595 | CreateStmt
596 | CreateTableSpaceStmt
597 | CreateTrigStmt
598 | CreateRoleStmt
599 | CreateUserStmt
600 | CreatedbStmt
601 | DeallocateStmt
602 | DeclareCursorStmt
603 | DefineStmt
604 | DeleteStmt
605 | DiscardStmt
606 | DropAssertStmt
607 | DropCastStmt
608 | DropGroupStmt
609 | DropOpClassStmt
610 | DropOpFamilyStmt
611 | DropOwnedStmt
612 | DropPLangStmt
613 | DropRuleStmt
614 | DropStmt
615 | DropTableSpaceStmt
616 | DropTrigStmt
617 | DropRoleStmt
618 | DropUserStmt
619 | DropdbStmt
620 | ExecuteStmt
621 | ExplainStmt
622 | FetchStmt
623 | GrantStmt
624 | GrantRoleStmt
625 | IndexStmt
626 | InsertStmt
627 | ListenStmt
628 | LoadStmt
629 | LockStmt
630 | NotifyStmt
631 | PrepareStmt
632 | ReassignOwnedStmt
633 | ReindexStmt
634 | RemoveAggrStmt
635 | RemoveFuncStmt
636 | RemoveOperStmt
637 | RenameStmt
638 | RevokeStmt
639 | RevokeRoleStmt
640 | RuleStmt
641 | SelectStmt
642 | TransactionStmt
643 | TruncateStmt
644 | UnlistenStmt
645 | UpdateStmt
646 | VacuumStmt
647 | VariableResetStmt
648 | VariableSetStmt
649 | VariableShowStmt
650 | ViewStmt
651 | /*EMPTY*/
652 { $$ = NULL; }
655 /*****************************************************************************
657 * Create a new Postgres DBMS role
659 *****************************************************************************/
661 CreateRoleStmt:
662 CREATE ROLE RoleId opt_with OptRoleList
664 CreateRoleStmt *n = makeNode(CreateRoleStmt);
665 n->stmt_type = ROLESTMT_ROLE;
666 n->role = $3;
667 n->options = $5;
668 $$ = (Node *)n;
673 opt_with: WITH {}
674 | /*EMPTY*/ {}
678 * Options for CREATE ROLE and ALTER ROLE (also used by CREATE/ALTER USER
679 * for backwards compatibility). Note: the only option required by SQL99
680 * is "WITH ADMIN name".
682 OptRoleList:
683 OptRoleList OptRoleElem { $$ = lappend($1, $2); }
684 | /* EMPTY */ { $$ = NIL; }
687 OptRoleElem:
688 PASSWORD Sconst
690 $$ = makeDefElem("password",
691 (Node *)makeString($2));
693 | PASSWORD NULL_P
695 $$ = makeDefElem("password", NULL);
697 | ENCRYPTED PASSWORD Sconst
699 $$ = makeDefElem("encryptedPassword",
700 (Node *)makeString($3));
702 | UNENCRYPTED PASSWORD Sconst
704 $$ = makeDefElem("unencryptedPassword",
705 (Node *)makeString($3));
707 | SUPERUSER_P
709 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
711 | NOSUPERUSER
713 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
715 | INHERIT
717 $$ = makeDefElem("inherit", (Node *)makeInteger(TRUE));
719 | NOINHERIT
721 $$ = makeDefElem("inherit", (Node *)makeInteger(FALSE));
723 | CREATEDB
725 $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
727 | NOCREATEDB
729 $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
731 | CREATEROLE
733 $$ = makeDefElem("createrole", (Node *)makeInteger(TRUE));
735 | NOCREATEROLE
737 $$ = makeDefElem("createrole", (Node *)makeInteger(FALSE));
739 | CREATEUSER
741 /* For backwards compatibility, synonym for SUPERUSER */
742 $$ = makeDefElem("superuser", (Node *)makeInteger(TRUE));
744 | NOCREATEUSER
746 $$ = makeDefElem("superuser", (Node *)makeInteger(FALSE));
748 | LOGIN_P
750 $$ = makeDefElem("canlogin", (Node *)makeInteger(TRUE));
752 | NOLOGIN_P
754 $$ = makeDefElem("canlogin", (Node *)makeInteger(FALSE));
756 | CONNECTION LIMIT SignedIconst
758 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($3));
760 | VALID UNTIL Sconst
762 $$ = makeDefElem("validUntil", (Node *)makeString($3));
764 /* Supported but not documented for roles, for use by ALTER GROUP. */
765 | USER name_list
767 $$ = makeDefElem("rolemembers", (Node *)$2);
769 /* The following are not supported by ALTER ROLE/USER/GROUP */
770 | SYSID Iconst
772 $$ = makeDefElem("sysid", (Node *)makeInteger($2));
774 | ADMIN name_list
776 $$ = makeDefElem("adminmembers", (Node *)$2);
778 | ROLE name_list
780 $$ = makeDefElem("rolemembers", (Node *)$2);
782 | IN_P ROLE name_list
784 $$ = makeDefElem("addroleto", (Node *)$3);
786 | IN_P GROUP_P name_list
788 $$ = makeDefElem("addroleto", (Node *)$3);
793 /*****************************************************************************
795 * Create a new Postgres DBMS user (role with implied login ability)
797 *****************************************************************************/
799 CreateUserStmt:
800 CREATE USER RoleId opt_with OptRoleList
802 CreateRoleStmt *n = makeNode(CreateRoleStmt);
803 n->stmt_type = ROLESTMT_USER;
804 n->role = $3;
805 n->options = $5;
806 $$ = (Node *)n;
811 /*****************************************************************************
813 * Alter a postgresql DBMS role
815 *****************************************************************************/
817 AlterRoleStmt:
818 ALTER ROLE RoleId opt_with OptRoleList
820 AlterRoleStmt *n = makeNode(AlterRoleStmt);
821 n->role = $3;
822 n->action = +1; /* add, if there are members */
823 n->options = $5;
824 $$ = (Node *)n;
828 AlterRoleSetStmt:
829 ALTER ROLE RoleId SetResetClause
831 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
832 n->role = $3;
833 n->setstmt = $4;
834 $$ = (Node *)n;
839 /*****************************************************************************
841 * Alter a postgresql DBMS user
843 *****************************************************************************/
845 AlterUserStmt:
846 ALTER USER RoleId opt_with OptRoleList
848 AlterRoleStmt *n = makeNode(AlterRoleStmt);
849 n->role = $3;
850 n->action = +1; /* add, if there are members */
851 n->options = $5;
852 $$ = (Node *)n;
857 AlterUserSetStmt:
858 ALTER USER RoleId SetResetClause
860 AlterRoleSetStmt *n = makeNode(AlterRoleSetStmt);
861 n->role = $3;
862 n->setstmt = $4;
863 $$ = (Node *)n;
868 /*****************************************************************************
870 * Drop a postgresql DBMS role
872 * XXX Ideally this would have CASCADE/RESTRICT options, but since a role
873 * might own objects in multiple databases, there is presently no way to
874 * implement either cascading or restricting. Caveat DBA.
875 *****************************************************************************/
877 DropRoleStmt:
878 DROP ROLE name_list
880 DropRoleStmt *n = makeNode(DropRoleStmt);
881 n->missing_ok = FALSE;
882 n->roles = $3;
883 $$ = (Node *)n;
885 | DROP ROLE IF_P EXISTS name_list
887 DropRoleStmt *n = makeNode(DropRoleStmt);
888 n->missing_ok = TRUE;
889 n->roles = $5;
890 $$ = (Node *)n;
894 /*****************************************************************************
896 * Drop a postgresql DBMS user
898 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
899 * might own objects in multiple databases, there is presently no way to
900 * implement either cascading or restricting. Caveat DBA.
901 *****************************************************************************/
903 DropUserStmt:
904 DROP USER name_list
906 DropRoleStmt *n = makeNode(DropRoleStmt);
907 n->missing_ok = FALSE;
908 n->roles = $3;
909 $$ = (Node *)n;
911 | DROP USER IF_P EXISTS name_list
913 DropRoleStmt *n = makeNode(DropRoleStmt);
914 n->roles = $5;
915 n->missing_ok = TRUE;
916 $$ = (Node *)n;
921 /*****************************************************************************
923 * Create a postgresql group (role without login ability)
925 *****************************************************************************/
927 CreateGroupStmt:
928 CREATE GROUP_P RoleId opt_with OptRoleList
930 CreateRoleStmt *n = makeNode(CreateRoleStmt);
931 n->stmt_type = ROLESTMT_GROUP;
932 n->role = $3;
933 n->options = $5;
934 $$ = (Node *)n;
939 /*****************************************************************************
941 * Alter a postgresql group
943 *****************************************************************************/
945 AlterGroupStmt:
946 ALTER GROUP_P RoleId add_drop USER name_list
948 AlterRoleStmt *n = makeNode(AlterRoleStmt);
949 n->role = $3;
950 n->action = $4;
951 n->options = list_make1(makeDefElem("rolemembers",
952 (Node *)$6));
953 $$ = (Node *)n;
957 add_drop: ADD_P { $$ = +1; }
958 | DROP { $$ = -1; }
962 /*****************************************************************************
964 * Drop a postgresql group
966 * XXX see above notes about cascading DROP USER; groups have same problem.
967 *****************************************************************************/
969 DropGroupStmt:
970 DROP GROUP_P name_list
972 DropRoleStmt *n = makeNode(DropRoleStmt);
973 n->missing_ok = FALSE;
974 n->roles = $3;
975 $$ = (Node *)n;
977 | DROP GROUP_P IF_P EXISTS name_list
979 DropRoleStmt *n = makeNode(DropRoleStmt);
980 n->missing_ok = TRUE;
981 n->roles = $5;
982 $$ = (Node *)n;
987 /*****************************************************************************
989 * Manipulate a schema
991 *****************************************************************************/
993 CreateSchemaStmt:
994 CREATE SCHEMA OptSchemaName AUTHORIZATION RoleId OptSchemaEltList
996 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
997 /* One can omit the schema name or the authorization id. */
998 if ($3 != NULL)
999 n->schemaname = $3;
1000 else
1001 n->schemaname = $5;
1002 n->authid = $5;
1003 n->schemaElts = $6;
1004 $$ = (Node *)n;
1006 | CREATE SCHEMA ColId OptSchemaEltList
1008 CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
1009 /* ...but not both */
1010 n->schemaname = $3;
1011 n->authid = NULL;
1012 n->schemaElts = $4;
1013 $$ = (Node *)n;
1017 OptSchemaName:
1018 ColId { $$ = $1; }
1019 | /* EMPTY */ { $$ = NULL; }
1022 OptSchemaEltList:
1023 OptSchemaEltList schema_stmt { $$ = lappend($1, $2); }
1024 | /* EMPTY */ { $$ = NIL; }
1028 * schema_stmt are the ones that can show up inside a CREATE SCHEMA
1029 * statement (in addition to by themselves).
1031 schema_stmt:
1032 CreateStmt
1033 | IndexStmt
1034 | CreateSeqStmt
1035 | CreateTrigStmt
1036 | GrantStmt
1037 | ViewStmt
1041 /*****************************************************************************
1043 * Set PG internal variable
1044 * SET name TO 'var_value'
1045 * Include SQL92 syntax (thomas 1997-10-22):
1046 * SET TIME ZONE 'var_value'
1048 *****************************************************************************/
1050 VariableSetStmt:
1051 SET set_rest
1053 VariableSetStmt *n = $2;
1054 n->is_local = false;
1055 $$ = (Node *) n;
1057 | SET LOCAL set_rest
1059 VariableSetStmt *n = $3;
1060 n->is_local = true;
1061 $$ = (Node *) n;
1063 | SET SESSION set_rest
1065 VariableSetStmt *n = $3;
1066 n->is_local = false;
1067 $$ = (Node *) n;
1071 set_rest: /* Generic SET syntaxes: */
1072 var_name TO var_list
1074 VariableSetStmt *n = makeNode(VariableSetStmt);
1075 n->kind = VAR_SET_VALUE;
1076 n->name = $1;
1077 n->args = $3;
1078 $$ = n;
1080 | var_name '=' var_list
1082 VariableSetStmt *n = makeNode(VariableSetStmt);
1083 n->kind = VAR_SET_VALUE;
1084 n->name = $1;
1085 n->args = $3;
1086 $$ = n;
1088 | var_name TO DEFAULT
1090 VariableSetStmt *n = makeNode(VariableSetStmt);
1091 n->kind = VAR_SET_DEFAULT;
1092 n->name = $1;
1093 $$ = n;
1095 | var_name '=' DEFAULT
1097 VariableSetStmt *n = makeNode(VariableSetStmt);
1098 n->kind = VAR_SET_DEFAULT;
1099 n->name = $1;
1100 $$ = n;
1102 | var_name FROM CURRENT_P
1104 VariableSetStmt *n = makeNode(VariableSetStmt);
1105 n->kind = VAR_SET_CURRENT;
1106 n->name = $1;
1107 $$ = n;
1109 /* Special syntaxes mandated by SQL standard: */
1110 | TIME ZONE zone_value
1112 VariableSetStmt *n = makeNode(VariableSetStmt);
1113 n->kind = VAR_SET_VALUE;
1114 n->name = "timezone";
1115 if ($3 != NULL)
1116 n->args = list_make1($3);
1117 else
1118 n->kind = VAR_SET_DEFAULT;
1119 $$ = n;
1121 | TRANSACTION transaction_mode_list
1123 VariableSetStmt *n = makeNode(VariableSetStmt);
1124 n->kind = VAR_SET_MULTI;
1125 n->name = "TRANSACTION";
1126 n->args = $2;
1127 $$ = n;
1129 | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
1131 VariableSetStmt *n = makeNode(VariableSetStmt);
1132 n->kind = VAR_SET_MULTI;
1133 n->name = "SESSION CHARACTERISTICS";
1134 n->args = $5;
1135 $$ = n;
1137 | CATALOG_P Sconst
1139 ereport(ERROR,
1140 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1141 errmsg("current database cannot be changed"),
1142 scanner_errposition(@2)));
1143 $$ = NULL; /*not reached*/
1145 | SCHEMA Sconst
1147 VariableSetStmt *n = makeNode(VariableSetStmt);
1148 n->kind = VAR_SET_VALUE;
1149 n->name = "search_path";
1150 n->args = list_make1(makeStringConst($2, @2));
1151 $$ = n;
1153 | NAMES opt_encoding
1155 VariableSetStmt *n = makeNode(VariableSetStmt);
1156 n->kind = VAR_SET_VALUE;
1157 n->name = "client_encoding";
1158 if ($2 != NULL)
1159 n->args = list_make1(makeStringConst($2, @2));
1160 else
1161 n->kind = VAR_SET_DEFAULT;
1162 $$ = n;
1164 | ROLE ColId_or_Sconst
1166 VariableSetStmt *n = makeNode(VariableSetStmt);
1167 n->kind = VAR_SET_VALUE;
1168 n->name = "role";
1169 n->args = list_make1(makeStringConst($2, @2));
1170 $$ = n;
1172 | SESSION AUTHORIZATION ColId_or_Sconst
1174 VariableSetStmt *n = makeNode(VariableSetStmt);
1175 n->kind = VAR_SET_VALUE;
1176 n->name = "session_authorization";
1177 n->args = list_make1(makeStringConst($3, @3));
1178 $$ = n;
1180 | SESSION AUTHORIZATION DEFAULT
1182 VariableSetStmt *n = makeNode(VariableSetStmt);
1183 n->kind = VAR_SET_DEFAULT;
1184 n->name = "session_authorization";
1185 $$ = n;
1187 | XML_P OPTION document_or_content
1189 VariableSetStmt *n = makeNode(VariableSetStmt);
1190 n->kind = VAR_SET_VALUE;
1191 n->name = "xmloption";
1192 n->args = list_make1(makeStringConst($3 == XMLOPTION_DOCUMENT ? "DOCUMENT" : "CONTENT", @3));
1193 $$ = n;
1197 var_name: ColId { $$ = $1; }
1198 | var_name '.' ColId
1200 $$ = palloc(strlen($1) + strlen($3) + 2);
1201 sprintf($$, "%s.%s", $1, $3);
1205 var_list: var_value { $$ = list_make1($1); }
1206 | var_list ',' var_value { $$ = lappend($1, $3); }
1209 var_value: opt_boolean
1210 { $$ = makeStringConst($1, @1); }
1211 | ColId_or_Sconst
1212 { $$ = makeStringConst($1, @1); }
1213 | NumericOnly
1214 { $$ = makeAConst($1, @1); }
1217 iso_level: READ UNCOMMITTED { $$ = "read uncommitted"; }
1218 | READ COMMITTED { $$ = "read committed"; }
1219 | REPEATABLE READ { $$ = "repeatable read"; }
1220 | SERIALIZABLE { $$ = "serializable"; }
1223 opt_boolean:
1224 TRUE_P { $$ = "true"; }
1225 | FALSE_P { $$ = "false"; }
1226 | ON { $$ = "on"; }
1227 | OFF { $$ = "off"; }
1230 /* Timezone values can be:
1231 * - a string such as 'pst8pdt'
1232 * - an identifier such as "pst8pdt"
1233 * - an integer or floating point number
1234 * - a time interval per SQL99
1235 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
1236 * so use IDENT and reject anything which is a reserved word.
1238 zone_value:
1239 Sconst
1241 $$ = makeStringConst($1, @1);
1243 | IDENT
1245 $$ = makeStringConst($1, @1);
1247 | ConstInterval Sconst opt_interval
1249 TypeName *t = $1;
1250 if ($3 != NIL)
1252 A_Const *n = (A_Const *) linitial($3);
1253 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1254 ereport(ERROR,
1255 (errcode(ERRCODE_SYNTAX_ERROR),
1256 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1257 scanner_errposition(@3)));
1259 t->typmods = $3;
1260 $$ = makeStringConstCast($2, @2, t);
1262 | ConstInterval '(' Iconst ')' Sconst opt_interval
1264 TypeName *t = $1;
1265 if ($6 != NIL)
1267 A_Const *n = (A_Const *) linitial($6);
1268 if ((n->val.val.ival & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
1269 ereport(ERROR,
1270 (errcode(ERRCODE_SYNTAX_ERROR),
1271 errmsg("time zone interval must be HOUR or HOUR TO MINUTE"),
1272 scanner_errposition(@6)));
1273 if (list_length($6) != 1)
1274 ereport(ERROR,
1275 (errcode(ERRCODE_SYNTAX_ERROR),
1276 errmsg("interval precision specified twice"),
1277 scanner_errposition(@1)));
1278 t->typmods = lappend($6, makeIntConst($3, @3));
1280 else
1281 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
1282 makeIntConst($3, @3));
1283 $$ = makeStringConstCast($5, @5, t);
1285 | NumericOnly { $$ = makeAConst($1, @1); }
1286 | DEFAULT { $$ = NULL; }
1287 | LOCAL { $$ = NULL; }
1290 opt_encoding:
1291 Sconst { $$ = $1; }
1292 | DEFAULT { $$ = NULL; }
1293 | /*EMPTY*/ { $$ = NULL; }
1296 ColId_or_Sconst:
1297 ColId { $$ = $1; }
1298 | SCONST { $$ = $1; }
1301 VariableResetStmt:
1302 RESET var_name
1304 VariableSetStmt *n = makeNode(VariableSetStmt);
1305 n->kind = VAR_RESET;
1306 n->name = $2;
1307 $$ = (Node *) n;
1309 | RESET TIME ZONE
1311 VariableSetStmt *n = makeNode(VariableSetStmt);
1312 n->kind = VAR_RESET;
1313 n->name = "timezone";
1314 $$ = (Node *) n;
1316 | RESET TRANSACTION ISOLATION LEVEL
1318 VariableSetStmt *n = makeNode(VariableSetStmt);
1319 n->kind = VAR_RESET;
1320 n->name = "transaction_isolation";
1321 $$ = (Node *) n;
1323 | RESET SESSION AUTHORIZATION
1325 VariableSetStmt *n = makeNode(VariableSetStmt);
1326 n->kind = VAR_RESET;
1327 n->name = "session_authorization";
1328 $$ = (Node *) n;
1330 | RESET ALL
1332 VariableSetStmt *n = makeNode(VariableSetStmt);
1333 n->kind = VAR_RESET_ALL;
1334 $$ = (Node *) n;
1338 /* SetResetClause allows SET or RESET without LOCAL */
1339 SetResetClause:
1340 SET set_rest { $$ = $2; }
1341 | VariableResetStmt { $$ = (VariableSetStmt *) $1; }
1345 VariableShowStmt:
1346 SHOW var_name
1348 VariableShowStmt *n = makeNode(VariableShowStmt);
1349 n->name = $2;
1350 $$ = (Node *) n;
1352 | SHOW TIME ZONE
1354 VariableShowStmt *n = makeNode(VariableShowStmt);
1355 n->name = "timezone";
1356 $$ = (Node *) n;
1358 | SHOW TRANSACTION ISOLATION LEVEL
1360 VariableShowStmt *n = makeNode(VariableShowStmt);
1361 n->name = "transaction_isolation";
1362 $$ = (Node *) n;
1364 | SHOW SESSION AUTHORIZATION
1366 VariableShowStmt *n = makeNode(VariableShowStmt);
1367 n->name = "session_authorization";
1368 $$ = (Node *) n;
1370 | SHOW ALL
1372 VariableShowStmt *n = makeNode(VariableShowStmt);
1373 n->name = "all";
1374 $$ = (Node *) n;
1379 ConstraintsSetStmt:
1380 SET CONSTRAINTS constraints_set_list constraints_set_mode
1382 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
1383 n->constraints = $3;
1384 n->deferred = $4;
1385 $$ = (Node *) n;
1389 constraints_set_list:
1390 ALL { $$ = NIL; }
1391 | qualified_name_list { $$ = $1; }
1394 constraints_set_mode:
1395 DEFERRED { $$ = TRUE; }
1396 | IMMEDIATE { $$ = FALSE; }
1401 * Checkpoint statement
1403 CheckPointStmt:
1404 CHECKPOINT
1406 CheckPointStmt *n = makeNode(CheckPointStmt);
1407 $$ = (Node *)n;
1412 /*****************************************************************************
1414 * DISCARD { ALL | TEMP | PLANS }
1416 *****************************************************************************/
1418 DiscardStmt:
1419 DISCARD ALL
1421 DiscardStmt *n = makeNode(DiscardStmt);
1422 n->target = DISCARD_ALL;
1423 $$ = (Node *) n;
1425 | DISCARD TEMP
1427 DiscardStmt *n = makeNode(DiscardStmt);
1428 n->target = DISCARD_TEMP;
1429 $$ = (Node *) n;
1431 | DISCARD TEMPORARY
1433 DiscardStmt *n = makeNode(DiscardStmt);
1434 n->target = DISCARD_TEMP;
1435 $$ = (Node *) n;
1437 | DISCARD PLANS
1439 DiscardStmt *n = makeNode(DiscardStmt);
1440 n->target = DISCARD_PLANS;
1441 $$ = (Node *) n;
1446 /*****************************************************************************
1448 * ALTER [ TABLE | INDEX | SEQUENCE | VIEW ] variations
1450 * Note: we accept all subcommands for each of the four variants, and sort
1451 * out what's really legal at execution time.
1452 *****************************************************************************/
1454 AlterTableStmt:
1455 ALTER TABLE relation_expr alter_table_cmds
1457 AlterTableStmt *n = makeNode(AlterTableStmt);
1458 n->relation = $3;
1459 n->cmds = $4;
1460 n->relkind = OBJECT_TABLE;
1461 $$ = (Node *)n;
1463 | ALTER INDEX relation_expr alter_table_cmds
1465 AlterTableStmt *n = makeNode(AlterTableStmt);
1466 n->relation = $3;
1467 n->cmds = $4;
1468 n->relkind = OBJECT_INDEX;
1469 $$ = (Node *)n;
1471 | ALTER SEQUENCE relation_expr alter_table_cmds
1473 AlterTableStmt *n = makeNode(AlterTableStmt);
1474 n->relation = $3;
1475 n->cmds = $4;
1476 n->relkind = OBJECT_SEQUENCE;
1477 $$ = (Node *)n;
1479 | ALTER VIEW relation_expr alter_table_cmds
1481 AlterTableStmt *n = makeNode(AlterTableStmt);
1482 n->relation = $3;
1483 n->cmds = $4;
1484 n->relkind = OBJECT_VIEW;
1485 $$ = (Node *)n;
1489 alter_table_cmds:
1490 alter_table_cmd { $$ = list_make1($1); }
1491 | alter_table_cmds ',' alter_table_cmd { $$ = lappend($1, $3); }
1494 alter_table_cmd:
1495 /* ALTER TABLE <name> ADD [COLUMN] <coldef> */
1496 ADD_P opt_column columnDef
1498 AlterTableCmd *n = makeNode(AlterTableCmd);
1499 n->subtype = AT_AddColumn;
1500 n->def = $3;
1501 $$ = (Node *)n;
1503 /* ALTER TABLE <name> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
1504 | ALTER opt_column ColId alter_column_default
1506 AlterTableCmd *n = makeNode(AlterTableCmd);
1507 n->subtype = AT_ColumnDefault;
1508 n->name = $3;
1509 n->def = $4;
1510 $$ = (Node *)n;
1512 /* ALTER TABLE <name> ALTER [COLUMN] <colname> DROP NOT NULL */
1513 | ALTER opt_column ColId DROP NOT NULL_P
1515 AlterTableCmd *n = makeNode(AlterTableCmd);
1516 n->subtype = AT_DropNotNull;
1517 n->name = $3;
1518 $$ = (Node *)n;
1520 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET NOT NULL */
1521 | ALTER opt_column ColId SET NOT NULL_P
1523 AlterTableCmd *n = makeNode(AlterTableCmd);
1524 n->subtype = AT_SetNotNull;
1525 n->name = $3;
1526 $$ = (Node *)n;
1528 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
1529 | ALTER opt_column ColId SET STATISTICS IntegerOnly
1531 AlterTableCmd *n = makeNode(AlterTableCmd);
1532 n->subtype = AT_SetStatistics;
1533 n->name = $3;
1534 n->def = (Node *) $6;
1535 $$ = (Node *)n;
1537 /* ALTER TABLE <name> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
1538 | ALTER opt_column ColId SET STORAGE ColId
1540 AlterTableCmd *n = makeNode(AlterTableCmd);
1541 n->subtype = AT_SetStorage;
1542 n->name = $3;
1543 n->def = (Node *) makeString($6);
1544 $$ = (Node *)n;
1546 /* ALTER TABLE <name> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
1547 | DROP opt_column ColId opt_drop_behavior
1549 AlterTableCmd *n = makeNode(AlterTableCmd);
1550 n->subtype = AT_DropColumn;
1551 n->name = $3;
1552 n->behavior = $4;
1553 $$ = (Node *)n;
1556 * ALTER TABLE <name> ALTER [COLUMN] <colname> [SET DATA] TYPE <typename>
1557 * [ USING <expression> ]
1559 | ALTER opt_column ColId opt_set_data TYPE_P Typename alter_using
1561 AlterTableCmd *n = makeNode(AlterTableCmd);
1562 n->subtype = AT_AlterColumnType;
1563 n->name = $3;
1564 n->def = (Node *) $6;
1565 n->transform = $7;
1566 $$ = (Node *)n;
1568 /* ALTER TABLE <name> ADD CONSTRAINT ... */
1569 | ADD_P TableConstraint
1571 AlterTableCmd *n = makeNode(AlterTableCmd);
1572 n->subtype = AT_AddConstraint;
1573 n->def = $2;
1574 $$ = (Node *)n;
1576 /* ALTER TABLE <name> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
1577 | DROP CONSTRAINT name opt_drop_behavior
1579 AlterTableCmd *n = makeNode(AlterTableCmd);
1580 n->subtype = AT_DropConstraint;
1581 n->name = $3;
1582 n->behavior = $4;
1583 $$ = (Node *)n;
1585 /* ALTER TABLE <name> SET WITHOUT OIDS */
1586 | SET WITHOUT OIDS
1588 AlterTableCmd *n = makeNode(AlterTableCmd);
1589 n->subtype = AT_DropOids;
1590 $$ = (Node *)n;
1592 /* ALTER TABLE <name> CLUSTER ON <indexname> */
1593 | CLUSTER ON name
1595 AlterTableCmd *n = makeNode(AlterTableCmd);
1596 n->subtype = AT_ClusterOn;
1597 n->name = $3;
1598 $$ = (Node *)n;
1600 /* ALTER TABLE <name> SET WITHOUT CLUSTER */
1601 | SET WITHOUT CLUSTER
1603 AlterTableCmd *n = makeNode(AlterTableCmd);
1604 n->subtype = AT_DropCluster;
1605 n->name = NULL;
1606 $$ = (Node *)n;
1608 /* ALTER TABLE <name> ENABLE TRIGGER <trig> */
1609 | ENABLE_P TRIGGER name
1611 AlterTableCmd *n = makeNode(AlterTableCmd);
1612 n->subtype = AT_EnableTrig;
1613 n->name = $3;
1614 $$ = (Node *)n;
1616 /* ALTER TABLE <name> ENABLE ALWAYS TRIGGER <trig> */
1617 | ENABLE_P ALWAYS TRIGGER name
1619 AlterTableCmd *n = makeNode(AlterTableCmd);
1620 n->subtype = AT_EnableAlwaysTrig;
1621 n->name = $4;
1622 $$ = (Node *)n;
1624 /* ALTER TABLE <name> ENABLE REPLICA TRIGGER <trig> */
1625 | ENABLE_P REPLICA TRIGGER name
1627 AlterTableCmd *n = makeNode(AlterTableCmd);
1628 n->subtype = AT_EnableReplicaTrig;
1629 n->name = $4;
1630 $$ = (Node *)n;
1632 /* ALTER TABLE <name> ENABLE TRIGGER ALL */
1633 | ENABLE_P TRIGGER ALL
1635 AlterTableCmd *n = makeNode(AlterTableCmd);
1636 n->subtype = AT_EnableTrigAll;
1637 $$ = (Node *)n;
1639 /* ALTER TABLE <name> ENABLE TRIGGER USER */
1640 | ENABLE_P TRIGGER USER
1642 AlterTableCmd *n = makeNode(AlterTableCmd);
1643 n->subtype = AT_EnableTrigUser;
1644 $$ = (Node *)n;
1646 /* ALTER TABLE <name> DISABLE TRIGGER <trig> */
1647 | DISABLE_P TRIGGER name
1649 AlterTableCmd *n = makeNode(AlterTableCmd);
1650 n->subtype = AT_DisableTrig;
1651 n->name = $3;
1652 $$ = (Node *)n;
1654 /* ALTER TABLE <name> DISABLE TRIGGER ALL */
1655 | DISABLE_P TRIGGER ALL
1657 AlterTableCmd *n = makeNode(AlterTableCmd);
1658 n->subtype = AT_DisableTrigAll;
1659 $$ = (Node *)n;
1661 /* ALTER TABLE <name> DISABLE TRIGGER USER */
1662 | DISABLE_P TRIGGER USER
1664 AlterTableCmd *n = makeNode(AlterTableCmd);
1665 n->subtype = AT_DisableTrigUser;
1666 $$ = (Node *)n;
1668 /* ALTER TABLE <name> ENABLE RULE <rule> */
1669 | ENABLE_P RULE name
1671 AlterTableCmd *n = makeNode(AlterTableCmd);
1672 n->subtype = AT_EnableRule;
1673 n->name = $3;
1674 $$ = (Node *)n;
1676 /* ALTER TABLE <name> ENABLE ALWAYS RULE <rule> */
1677 | ENABLE_P ALWAYS RULE name
1679 AlterTableCmd *n = makeNode(AlterTableCmd);
1680 n->subtype = AT_EnableAlwaysRule;
1681 n->name = $4;
1682 $$ = (Node *)n;
1684 /* ALTER TABLE <name> ENABLE REPLICA RULE <rule> */
1685 | ENABLE_P REPLICA RULE name
1687 AlterTableCmd *n = makeNode(AlterTableCmd);
1688 n->subtype = AT_EnableReplicaRule;
1689 n->name = $4;
1690 $$ = (Node *)n;
1692 /* ALTER TABLE <name> DISABLE RULE <rule> */
1693 | DISABLE_P RULE name
1695 AlterTableCmd *n = makeNode(AlterTableCmd);
1696 n->subtype = AT_DisableRule;
1697 n->name = $3;
1698 $$ = (Node *)n;
1700 /* ALTER TABLE <name> INHERIT <parent> */
1701 | INHERIT qualified_name
1703 AlterTableCmd *n = makeNode(AlterTableCmd);
1704 n->subtype = AT_AddInherit;
1705 n->def = (Node *) $2;
1706 $$ = (Node *)n;
1708 /* ALTER TABLE <name> NO INHERIT <parent> */
1709 | NO INHERIT qualified_name
1711 AlterTableCmd *n = makeNode(AlterTableCmd);
1712 n->subtype = AT_DropInherit;
1713 n->def = (Node *) $3;
1714 $$ = (Node *)n;
1716 /* ALTER TABLE <name> OWNER TO RoleId */
1717 | OWNER TO RoleId
1719 AlterTableCmd *n = makeNode(AlterTableCmd);
1720 n->subtype = AT_ChangeOwner;
1721 n->name = $3;
1722 $$ = (Node *)n;
1724 /* ALTER TABLE <name> SET TABLESPACE <tablespacename> */
1725 | SET TABLESPACE name
1727 AlterTableCmd *n = makeNode(AlterTableCmd);
1728 n->subtype = AT_SetTableSpace;
1729 n->name = $3;
1730 $$ = (Node *)n;
1732 /* ALTER TABLE <name> SET (...) */
1733 | SET definition
1735 AlterTableCmd *n = makeNode(AlterTableCmd);
1736 n->subtype = AT_SetRelOptions;
1737 n->def = (Node *)$2;
1738 $$ = (Node *)n;
1740 /* ALTER TABLE <name> RESET (...) */
1741 | RESET definition
1743 AlterTableCmd *n = makeNode(AlterTableCmd);
1744 n->subtype = AT_ResetRelOptions;
1745 n->def = (Node *)$2;
1746 $$ = (Node *)n;
1750 alter_column_default:
1751 SET DEFAULT a_expr { $$ = $3; }
1752 | DROP DEFAULT { $$ = NULL; }
1755 opt_drop_behavior:
1756 CASCADE { $$ = DROP_CASCADE; }
1757 | RESTRICT { $$ = DROP_RESTRICT; }
1758 | /* EMPTY */ { $$ = DROP_RESTRICT; /* default */ }
1761 alter_using:
1762 USING a_expr { $$ = $2; }
1763 | /* EMPTY */ { $$ = NULL; }
1768 /*****************************************************************************
1770 * QUERY :
1771 * close <portalname>
1773 *****************************************************************************/
1775 ClosePortalStmt:
1776 CLOSE name
1778 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1779 n->portalname = $2;
1780 $$ = (Node *)n;
1782 | CLOSE ALL
1784 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1785 n->portalname = NULL;
1786 $$ = (Node *)n;
1791 /*****************************************************************************
1793 * QUERY :
1794 * COPY relname ['(' columnList ')'] FROM/TO file [WITH options]
1796 * BINARY, OIDS, and DELIMITERS kept in old locations
1797 * for backward compatibility. 2002-06-18
1799 * COPY ( SELECT ... ) TO file [WITH options]
1800 * This form doesn't have the backwards-compatible option
1801 * syntax.
1803 *****************************************************************************/
1805 CopyStmt: COPY opt_binary qualified_name opt_column_list opt_oids
1806 copy_from copy_file_name copy_delimiter opt_with copy_opt_list
1808 CopyStmt *n = makeNode(CopyStmt);
1809 n->relation = $3;
1810 n->query = NULL;
1811 n->attlist = $4;
1812 n->is_from = $6;
1813 n->filename = $7;
1815 n->options = NIL;
1816 /* Concatenate user-supplied flags */
1817 if ($2)
1818 n->options = lappend(n->options, $2);
1819 if ($5)
1820 n->options = lappend(n->options, $5);
1821 if ($8)
1822 n->options = lappend(n->options, $8);
1823 if ($10)
1824 n->options = list_concat(n->options, $10);
1825 $$ = (Node *)n;
1827 | COPY select_with_parens TO copy_file_name opt_with
1828 copy_opt_list
1830 CopyStmt *n = makeNode(CopyStmt);
1831 n->relation = NULL;
1832 n->query = $2;
1833 n->attlist = NIL;
1834 n->is_from = false;
1835 n->filename = $4;
1836 n->options = $6;
1837 $$ = (Node *)n;
1841 copy_from:
1842 FROM { $$ = TRUE; }
1843 | TO { $$ = FALSE; }
1847 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1848 * used depends on the direction. (It really doesn't make sense to copy from
1849 * stdout. We silently correct the "typo".) - AY 9/94
1851 copy_file_name:
1852 Sconst { $$ = $1; }
1853 | STDIN { $$ = NULL; }
1854 | STDOUT { $$ = NULL; }
1859 copy_opt_list:
1860 copy_opt_list copy_opt_item { $$ = lappend($1, $2); }
1861 | /* EMPTY */ { $$ = NIL; }
1865 copy_opt_item:
1866 BINARY
1868 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1870 | OIDS
1872 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1874 | DELIMITER opt_as Sconst
1876 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1878 | NULL_P opt_as Sconst
1880 $$ = makeDefElem("null", (Node *)makeString($3));
1882 | CSV
1884 $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
1886 | HEADER_P
1888 $$ = makeDefElem("header", (Node *)makeInteger(TRUE));
1890 | QUOTE opt_as Sconst
1892 $$ = makeDefElem("quote", (Node *)makeString($3));
1894 | ESCAPE opt_as Sconst
1896 $$ = makeDefElem("escape", (Node *)makeString($3));
1898 | FORCE QUOTE columnList
1900 $$ = makeDefElem("force_quote", (Node *)$3);
1902 | FORCE NOT NULL_P columnList
1904 $$ = makeDefElem("force_notnull", (Node *)$4);
1908 /* The following exist for backward compatibility */
1910 opt_binary:
1911 BINARY
1913 $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
1915 | /*EMPTY*/ { $$ = NULL; }
1918 opt_oids:
1919 WITH OIDS
1921 $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
1923 | /*EMPTY*/ { $$ = NULL; }
1926 copy_delimiter:
1927 /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
1928 opt_using DELIMITERS Sconst
1930 $$ = makeDefElem("delimiter", (Node *)makeString($3));
1932 | /*EMPTY*/ { $$ = NULL; }
1935 opt_using:
1936 USING {}
1937 | /*EMPTY*/ {}
1941 /*****************************************************************************
1943 * QUERY :
1944 * CREATE TABLE relname
1946 *****************************************************************************/
1948 CreateStmt: CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
1949 OptInherit OptWith OnCommitOption OptTableSpace
1951 CreateStmt *n = makeNode(CreateStmt);
1952 $4->istemp = $2;
1953 n->relation = $4;
1954 n->tableElts = $6;
1955 n->inhRelations = $8;
1956 n->constraints = NIL;
1957 n->options = $9;
1958 n->oncommit = $10;
1959 n->tablespacename = $11;
1960 $$ = (Node *)n;
1962 | CREATE OptTemp TABLE qualified_name OF qualified_name
1963 '(' OptTableElementList ')' OptWith OnCommitOption OptTableSpace
1965 /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
1966 * by our inheritance capabilities. Let's try it...
1968 CreateStmt *n = makeNode(CreateStmt);
1969 $4->istemp = $2;
1970 n->relation = $4;
1971 n->tableElts = $8;
1972 n->inhRelations = list_make1($6);
1973 n->constraints = NIL;
1974 n->options = $10;
1975 n->oncommit = $11;
1976 n->tablespacename = $12;
1977 $$ = (Node *)n;
1982 * Redundancy here is needed to avoid shift/reduce conflicts,
1983 * since TEMP is not a reserved word. See also OptTempTableName.
1985 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
1986 * the LOCAL keyword is really meaningless.
1988 OptTemp: TEMPORARY { $$ = TRUE; }
1989 | TEMP { $$ = TRUE; }
1990 | LOCAL TEMPORARY { $$ = TRUE; }
1991 | LOCAL TEMP { $$ = TRUE; }
1992 | GLOBAL TEMPORARY { $$ = TRUE; }
1993 | GLOBAL TEMP { $$ = TRUE; }
1994 | /*EMPTY*/ { $$ = FALSE; }
1997 OptTableElementList:
1998 TableElementList { $$ = $1; }
1999 | /*EMPTY*/ { $$ = NIL; }
2002 TableElementList:
2003 TableElement
2005 $$ = list_make1($1);
2007 | TableElementList ',' TableElement
2009 $$ = lappend($1, $3);
2013 TableElement:
2014 columnDef { $$ = $1; }
2015 | TableLikeClause { $$ = $1; }
2016 | TableConstraint { $$ = $1; }
2019 columnDef: ColId Typename ColQualList
2021 ColumnDef *n = makeNode(ColumnDef);
2022 n->colname = $1;
2023 n->typename = $2;
2024 n->constraints = $3;
2025 n->is_local = true;
2026 $$ = (Node *)n;
2030 ColQualList:
2031 ColQualList ColConstraint { $$ = lappend($1, $2); }
2032 | /*EMPTY*/ { $$ = NIL; }
2035 ColConstraint:
2036 CONSTRAINT name ColConstraintElem
2038 switch (nodeTag($3))
2040 case T_Constraint:
2042 Constraint *n = (Constraint *)$3;
2043 n->name = $2;
2045 break;
2046 case T_FkConstraint:
2048 FkConstraint *n = (FkConstraint *)$3;
2049 n->constr_name = $2;
2051 break;
2052 default:
2053 break;
2055 $$ = $3;
2057 | ColConstraintElem { $$ = $1; }
2058 | ConstraintAttr { $$ = $1; }
2061 /* DEFAULT NULL is already the default for Postgres.
2062 * But define it here and carry it forward into the system
2063 * to make it explicit.
2064 * - thomas 1998-09-13
2066 * WITH NULL and NULL are not SQL92-standard syntax elements,
2067 * so leave them out. Use DEFAULT NULL to explicitly indicate
2068 * that a column may have that value. WITH NULL leads to
2069 * shift/reduce conflicts with WITH TIME ZONE anyway.
2070 * - thomas 1999-01-08
2072 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
2073 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
2074 * or be part of a_expr NOT LIKE or similar constructs).
2076 ColConstraintElem:
2077 NOT NULL_P
2079 Constraint *n = makeNode(Constraint);
2080 n->contype = CONSTR_NOTNULL;
2081 n->name = NULL;
2082 n->raw_expr = NULL;
2083 n->cooked_expr = NULL;
2084 n->keys = NULL;
2085 n->indexspace = NULL;
2086 $$ = (Node *)n;
2088 | NULL_P
2090 Constraint *n = makeNode(Constraint);
2091 n->contype = CONSTR_NULL;
2092 n->name = NULL;
2093 n->raw_expr = NULL;
2094 n->cooked_expr = NULL;
2095 n->keys = NULL;
2096 n->indexspace = NULL;
2097 $$ = (Node *)n;
2099 | UNIQUE opt_definition OptConsTableSpace
2101 Constraint *n = makeNode(Constraint);
2102 n->contype = CONSTR_UNIQUE;
2103 n->name = NULL;
2104 n->raw_expr = NULL;
2105 n->cooked_expr = NULL;
2106 n->keys = NULL;
2107 n->options = $2;
2108 n->indexspace = $3;
2109 $$ = (Node *)n;
2111 | PRIMARY KEY opt_definition OptConsTableSpace
2113 Constraint *n = makeNode(Constraint);
2114 n->contype = CONSTR_PRIMARY;
2115 n->name = NULL;
2116 n->raw_expr = NULL;
2117 n->cooked_expr = NULL;
2118 n->keys = NULL;
2119 n->options = $3;
2120 n->indexspace = $4;
2121 $$ = (Node *)n;
2123 | CHECK '(' a_expr ')'
2125 Constraint *n = makeNode(Constraint);
2126 n->contype = CONSTR_CHECK;
2127 n->name = NULL;
2128 n->raw_expr = $3;
2129 n->cooked_expr = NULL;
2130 n->keys = NULL;
2131 n->indexspace = NULL;
2132 $$ = (Node *)n;
2134 | DEFAULT b_expr
2136 Constraint *n = makeNode(Constraint);
2137 n->contype = CONSTR_DEFAULT;
2138 n->name = NULL;
2139 n->raw_expr = $2;
2140 n->cooked_expr = NULL;
2141 n->keys = NULL;
2142 n->indexspace = NULL;
2143 $$ = (Node *)n;
2145 | REFERENCES qualified_name opt_column_list key_match key_actions
2147 FkConstraint *n = makeNode(FkConstraint);
2148 n->constr_name = NULL;
2149 n->pktable = $2;
2150 n->fk_attrs = NIL;
2151 n->pk_attrs = $3;
2152 n->fk_matchtype = $4;
2153 n->fk_upd_action = (char) ($5 >> 8);
2154 n->fk_del_action = (char) ($5 & 0xFF);
2155 n->deferrable = FALSE;
2156 n->initdeferred = FALSE;
2157 $$ = (Node *)n;
2162 * ConstraintAttr represents constraint attributes, which we parse as if
2163 * they were independent constraint clauses, in order to avoid shift/reduce
2164 * conflicts (since NOT might start either an independent NOT NULL clause
2165 * or an attribute). parse_utilcmd.c is responsible for attaching the
2166 * attribute information to the preceding "real" constraint node, and for
2167 * complaining if attribute clauses appear in the wrong place or wrong
2168 * combinations.
2170 * See also ConstraintAttributeSpec, which can be used in places where
2171 * there is no parsing conflict.
2173 ConstraintAttr:
2174 DEFERRABLE
2176 Constraint *n = makeNode(Constraint);
2177 n->contype = CONSTR_ATTR_DEFERRABLE;
2178 $$ = (Node *)n;
2180 | NOT DEFERRABLE
2182 Constraint *n = makeNode(Constraint);
2183 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
2184 $$ = (Node *)n;
2186 | INITIALLY DEFERRED
2188 Constraint *n = makeNode(Constraint);
2189 n->contype = CONSTR_ATTR_DEFERRED;
2190 $$ = (Node *)n;
2192 | INITIALLY IMMEDIATE
2194 Constraint *n = makeNode(Constraint);
2195 n->contype = CONSTR_ATTR_IMMEDIATE;
2196 $$ = (Node *)n;
2202 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
2203 * This seems to be a poor man's inheritance capability, with the resulting
2204 * tables completely decoupled except for the original commonality in definitions.
2206 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
2207 * which is a part of SQL:2003.
2209 TableLikeClause:
2210 LIKE qualified_name TableLikeOptionList
2212 InhRelation *n = makeNode(InhRelation);
2213 n->relation = $2;
2214 n->options = $3;
2215 $$ = (Node *)n;
2219 TableLikeOptionList:
2220 TableLikeOptionList TableLikeOption { $$ = lappend_int($1, $2); }
2221 | /* EMPTY */ { $$ = NIL; }
2224 TableLikeOption:
2225 INCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_INCLUDING_DEFAULTS; }
2226 | EXCLUDING DEFAULTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_DEFAULTS; }
2227 | INCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_INCLUDING_CONSTRAINTS; }
2228 | EXCLUDING CONSTRAINTS { $$ = CREATE_TABLE_LIKE_EXCLUDING_CONSTRAINTS; }
2229 | INCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_INCLUDING_INDEXES; }
2230 | EXCLUDING INDEXES { $$ = CREATE_TABLE_LIKE_EXCLUDING_INDEXES; }
2234 /* ConstraintElem specifies constraint syntax which is not embedded into
2235 * a column definition. ColConstraintElem specifies the embedded form.
2236 * - thomas 1997-12-03
2238 TableConstraint:
2239 CONSTRAINT name ConstraintElem
2241 switch (nodeTag($3))
2243 case T_Constraint:
2245 Constraint *n = (Constraint *)$3;
2246 n->name = $2;
2248 break;
2249 case T_FkConstraint:
2251 FkConstraint *n = (FkConstraint *)$3;
2252 n->constr_name = $2;
2254 break;
2255 default:
2256 break;
2258 $$ = $3;
2260 | ConstraintElem { $$ = $1; }
2263 ConstraintElem:
2264 CHECK '(' a_expr ')'
2266 Constraint *n = makeNode(Constraint);
2267 n->contype = CONSTR_CHECK;
2268 n->name = NULL;
2269 n->raw_expr = $3;
2270 n->cooked_expr = NULL;
2271 n->indexspace = NULL;
2272 $$ = (Node *)n;
2274 | UNIQUE '(' columnList ')' opt_definition OptConsTableSpace
2276 Constraint *n = makeNode(Constraint);
2277 n->contype = CONSTR_UNIQUE;
2278 n->name = NULL;
2279 n->raw_expr = NULL;
2280 n->cooked_expr = NULL;
2281 n->keys = $3;
2282 n->options = $5;
2283 n->indexspace = $6;
2284 $$ = (Node *)n;
2286 | PRIMARY KEY '(' columnList ')' opt_definition OptConsTableSpace
2288 Constraint *n = makeNode(Constraint);
2289 n->contype = CONSTR_PRIMARY;
2290 n->name = NULL;
2291 n->raw_expr = NULL;
2292 n->cooked_expr = NULL;
2293 n->keys = $4;
2294 n->options = $6;
2295 n->indexspace = $7;
2296 $$ = (Node *)n;
2298 | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
2299 opt_column_list key_match key_actions ConstraintAttributeSpec
2301 FkConstraint *n = makeNode(FkConstraint);
2302 n->constr_name = NULL;
2303 n->pktable = $7;
2304 n->fk_attrs = $4;
2305 n->pk_attrs = $8;
2306 n->fk_matchtype = $9;
2307 n->fk_upd_action = (char) ($10 >> 8);
2308 n->fk_del_action = (char) ($10 & 0xFF);
2309 n->deferrable = ($11 & 1) != 0;
2310 n->initdeferred = ($11 & 2) != 0;
2311 $$ = (Node *)n;
2315 opt_column_list:
2316 '(' columnList ')' { $$ = $2; }
2317 | /*EMPTY*/ { $$ = NIL; }
2320 columnList:
2321 columnElem { $$ = list_make1($1); }
2322 | columnList ',' columnElem { $$ = lappend($1, $3); }
2325 columnElem: ColId
2327 $$ = (Node *) makeString($1);
2331 key_match: MATCH FULL
2333 $$ = FKCONSTR_MATCH_FULL;
2335 | MATCH PARTIAL
2337 ereport(ERROR,
2338 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2339 errmsg("MATCH PARTIAL not yet implemented"),
2340 scanner_errposition(@1)));
2341 $$ = FKCONSTR_MATCH_PARTIAL;
2343 | MATCH SIMPLE
2345 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2347 | /*EMPTY*/
2349 $$ = FKCONSTR_MATCH_UNSPECIFIED;
2354 * We combine the update and delete actions into one value temporarily
2355 * for simplicity of parsing, and then break them down again in the
2356 * calling production. update is in the left 8 bits, delete in the right.
2357 * Note that NOACTION is the default.
2359 key_actions:
2360 key_update
2361 { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2362 | key_delete
2363 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
2364 | key_update key_delete
2365 { $$ = ($1 << 8) | ($2 & 0xFF); }
2366 | key_delete key_update
2367 { $$ = ($2 << 8) | ($1 & 0xFF); }
2368 | /*EMPTY*/
2369 { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
2372 key_update: ON UPDATE key_action { $$ = $3; }
2375 key_delete: ON DELETE_P key_action { $$ = $3; }
2378 key_action:
2379 NO ACTION { $$ = FKCONSTR_ACTION_NOACTION; }
2380 | RESTRICT { $$ = FKCONSTR_ACTION_RESTRICT; }
2381 | CASCADE { $$ = FKCONSTR_ACTION_CASCADE; }
2382 | SET NULL_P { $$ = FKCONSTR_ACTION_SETNULL; }
2383 | SET DEFAULT { $$ = FKCONSTR_ACTION_SETDEFAULT; }
2386 OptInherit: INHERITS '(' qualified_name_list ')' { $$ = $3; }
2387 | /*EMPTY*/ { $$ = NIL; }
2390 /* WITH (options) is preferred, WITH OIDS and WITHOUT OIDS are legacy forms */
2391 OptWith:
2392 WITH definition { $$ = $2; }
2393 | WITH OIDS { $$ = list_make1(defWithOids(true)); }
2394 | WITHOUT OIDS { $$ = list_make1(defWithOids(false)); }
2395 | /*EMPTY*/ { $$ = NIL; }
2398 OnCommitOption: ON COMMIT DROP { $$ = ONCOMMIT_DROP; }
2399 | ON COMMIT DELETE_P ROWS { $$ = ONCOMMIT_DELETE_ROWS; }
2400 | ON COMMIT PRESERVE ROWS { $$ = ONCOMMIT_PRESERVE_ROWS; }
2401 | /*EMPTY*/ { $$ = ONCOMMIT_NOOP; }
2404 OptTableSpace: TABLESPACE name { $$ = $2; }
2405 | /*EMPTY*/ { $$ = NULL; }
2408 OptConsTableSpace: USING INDEX TABLESPACE name { $$ = $4; }
2409 | /*EMPTY*/ { $$ = NULL; }
2414 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
2415 * SELECT ... INTO.
2418 CreateAsStmt:
2419 CREATE OptTemp TABLE create_as_target AS SelectStmt opt_with_data
2422 * When the SelectStmt is a set-operation tree, we must
2423 * stuff the INTO information into the leftmost component
2424 * Select, because that's where analyze.c will expect
2425 * to find it. Similarly, the output column names must
2426 * be attached to that Select's target list.
2428 SelectStmt *n = findLeftmostSelect((SelectStmt *) $6);
2429 if (n->intoClause != NULL)
2430 ereport(ERROR,
2431 (errcode(ERRCODE_SYNTAX_ERROR),
2432 errmsg("CREATE TABLE AS cannot specify INTO"),
2433 scanner_errposition(exprLocation((Node *) n->intoClause))));
2434 $4->rel->istemp = $2;
2435 n->intoClause = $4;
2436 /* Implement WITH NO DATA by forcing top-level LIMIT 0 */
2437 if (!$7)
2438 ((SelectStmt *) $6)->limitCount = makeIntConst(0, -1);
2439 $$ = $6;
2443 create_as_target:
2444 qualified_name OptCreateAs OptWith OnCommitOption OptTableSpace
2446 $$ = makeNode(IntoClause);
2447 $$->rel = $1;
2448 $$->colNames = $2;
2449 $$->options = $3;
2450 $$->onCommit = $4;
2451 $$->tableSpaceName = $5;
2455 OptCreateAs:
2456 '(' CreateAsList ')' { $$ = $2; }
2457 | /*EMPTY*/ { $$ = NIL; }
2460 CreateAsList:
2461 CreateAsElement { $$ = list_make1($1); }
2462 | CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
2465 CreateAsElement:
2466 ColId
2468 ColumnDef *n = makeNode(ColumnDef);
2469 n->colname = $1;
2470 n->typename = NULL;
2471 n->inhcount = 0;
2472 n->is_local = true;
2473 n->is_not_null = false;
2474 n->raw_default = NULL;
2475 n->cooked_default = NULL;
2476 n->constraints = NIL;
2477 $$ = (Node *)n;
2481 opt_with_data:
2482 WITH DATA_P { $$ = TRUE; }
2483 | WITH NO DATA_P { $$ = FALSE; }
2484 | /*EMPTY*/ { $$ = TRUE; }
2488 /*****************************************************************************
2490 * QUERY :
2491 * CREATE SEQUENCE seqname
2492 * ALTER SEQUENCE seqname
2494 *****************************************************************************/
2496 CreateSeqStmt:
2497 CREATE OptTemp SEQUENCE qualified_name OptSeqOptList
2499 CreateSeqStmt *n = makeNode(CreateSeqStmt);
2500 $4->istemp = $2;
2501 n->sequence = $4;
2502 n->options = $5;
2503 $$ = (Node *)n;
2507 AlterSeqStmt:
2508 ALTER SEQUENCE relation_expr SeqOptList
2510 AlterSeqStmt *n = makeNode(AlterSeqStmt);
2511 n->sequence = $3;
2512 n->options = $4;
2513 $$ = (Node *)n;
2517 OptSeqOptList: SeqOptList { $$ = $1; }
2518 | /*EMPTY*/ { $$ = NIL; }
2521 SeqOptList: SeqOptElem { $$ = list_make1($1); }
2522 | SeqOptList SeqOptElem { $$ = lappend($1, $2); }
2525 SeqOptElem: CACHE NumericOnly
2527 $$ = makeDefElem("cache", (Node *)$2);
2529 | CYCLE
2531 $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
2533 | NO CYCLE
2535 $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
2537 | INCREMENT opt_by NumericOnly
2539 $$ = makeDefElem("increment", (Node *)$3);
2541 | MAXVALUE NumericOnly
2543 $$ = makeDefElem("maxvalue", (Node *)$2);
2545 | MINVALUE NumericOnly
2547 $$ = makeDefElem("minvalue", (Node *)$2);
2549 | NO MAXVALUE
2551 $$ = makeDefElem("maxvalue", NULL);
2553 | NO MINVALUE
2555 $$ = makeDefElem("minvalue", NULL);
2557 | OWNED BY any_name
2559 $$ = makeDefElem("owned_by", (Node *)$3);
2561 | START opt_with NumericOnly
2563 $$ = makeDefElem("start", (Node *)$3);
2565 | RESTART
2567 $$ = makeDefElem("restart", NULL);
2569 | RESTART opt_with NumericOnly
2571 $$ = makeDefElem("restart", (Node *)$3);
2575 opt_by: BY {}
2576 | /* empty */ {}
2579 NumericOnly:
2580 FloatOnly { $$ = $1; }
2581 | IntegerOnly { $$ = $1; }
2584 FloatOnly: FCONST { $$ = makeFloat($1); }
2585 | '-' FCONST
2587 $$ = makeFloat($2);
2588 doNegateFloat($$);
2592 IntegerOnly: SignedIconst { $$ = makeInteger($1); };
2595 /*****************************************************************************
2597 * QUERIES :
2598 * CREATE PROCEDURAL LANGUAGE ...
2599 * DROP PROCEDURAL LANGUAGE ...
2601 *****************************************************************************/
2603 CreatePLangStmt:
2604 CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2606 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2607 n->plname = $5;
2608 /* parameters are all to be supplied by system */
2609 n->plhandler = NIL;
2610 n->plvalidator = NIL;
2611 n->pltrusted = false;
2612 $$ = (Node *)n;
2614 | CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
2615 HANDLER handler_name opt_validator opt_lancompiler
2617 CreatePLangStmt *n = makeNode(CreatePLangStmt);
2618 n->plname = $5;
2619 n->plhandler = $7;
2620 n->plvalidator = $8;
2621 n->pltrusted = $2;
2622 /* LANCOMPILER is now ignored entirely */
2623 $$ = (Node *)n;
2627 opt_trusted:
2628 TRUSTED { $$ = TRUE; }
2629 | /*EMPTY*/ { $$ = FALSE; }
2632 /* This ought to be just func_name, but that causes reduce/reduce conflicts
2633 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
2634 * Work around by using simple names, instead.
2636 handler_name:
2637 name { $$ = list_make1(makeString($1)); }
2638 | name attrs { $$ = lcons(makeString($1), $2); }
2641 opt_validator:
2642 VALIDATOR handler_name { $$ = $2; }
2643 | /*EMPTY*/ { $$ = NIL; }
2646 opt_lancompiler:
2647 LANCOMPILER Sconst { $$ = $2; }
2648 | /*EMPTY*/ { $$ = NULL; }
2651 DropPLangStmt:
2652 DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
2654 DropPLangStmt *n = makeNode(DropPLangStmt);
2655 n->plname = $4;
2656 n->behavior = $5;
2657 n->missing_ok = false;
2658 $$ = (Node *)n;
2660 | DROP opt_procedural LANGUAGE IF_P EXISTS ColId_or_Sconst opt_drop_behavior
2662 DropPLangStmt *n = makeNode(DropPLangStmt);
2663 n->plname = $6;
2664 n->behavior = $7;
2665 n->missing_ok = true;
2666 $$ = (Node *)n;
2670 opt_procedural:
2671 PROCEDURAL {}
2672 | /*EMPTY*/ {}
2675 /*****************************************************************************
2677 * QUERY:
2678 * CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
2680 *****************************************************************************/
2682 CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
2684 CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
2685 n->tablespacename = $3;
2686 n->owner = $4;
2687 n->location = $6;
2688 $$ = (Node *) n;
2692 OptTableSpaceOwner: OWNER name { $$ = $2; }
2693 | /*EMPTY */ { $$ = NULL; }
2696 /*****************************************************************************
2698 * QUERY :
2699 * DROP TABLESPACE <tablespace>
2701 * No need for drop behaviour as we cannot implement dependencies for
2702 * objects in other databases; we can only support RESTRICT.
2704 ****************************************************************************/
2706 DropTableSpaceStmt: DROP TABLESPACE name
2708 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2709 n->tablespacename = $3;
2710 n->missing_ok = false;
2711 $$ = (Node *) n;
2713 | DROP TABLESPACE IF_P EXISTS name
2715 DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
2716 n->tablespacename = $5;
2717 n->missing_ok = true;
2718 $$ = (Node *) n;
2722 /*****************************************************************************
2724 * QUERIES :
2725 * CREATE TRIGGER ...
2726 * DROP TRIGGER ...
2728 *****************************************************************************/
2730 CreateTrigStmt:
2731 CREATE TRIGGER name TriggerActionTime TriggerEvents ON
2732 qualified_name TriggerForSpec EXECUTE PROCEDURE
2733 func_name '(' TriggerFuncArgs ')'
2735 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2736 n->trigname = $3;
2737 n->relation = $7;
2738 n->funcname = $11;
2739 n->args = $13;
2740 n->before = $4;
2741 n->row = $8;
2742 memcpy(n->actions, $5, 4);
2743 n->isconstraint = FALSE;
2744 n->deferrable = FALSE;
2745 n->initdeferred = FALSE;
2746 n->constrrel = NULL;
2747 $$ = (Node *)n;
2749 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
2750 qualified_name OptConstrFromTable
2751 ConstraintAttributeSpec
2752 FOR EACH ROW EXECUTE PROCEDURE
2753 func_name '(' TriggerFuncArgs ')'
2755 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2756 n->trigname = $4;
2757 n->relation = $8;
2758 n->funcname = $16;
2759 n->args = $18;
2760 n->before = FALSE;
2761 n->row = TRUE;
2762 memcpy(n->actions, $6, 4);
2763 n->isconstraint = TRUE;
2764 n->deferrable = ($10 & 1) != 0;
2765 n->initdeferred = ($10 & 2) != 0;
2767 n->constrrel = $9;
2768 $$ = (Node *)n;
2772 TriggerActionTime:
2773 BEFORE { $$ = TRUE; }
2774 | AFTER { $$ = FALSE; }
2777 TriggerEvents:
2778 TriggerOneEvent
2780 char *e = palloc(4);
2781 e[0] = $1; e[1] = '\0';
2782 $$ = e;
2784 | TriggerOneEvent OR TriggerOneEvent
2786 char *e = palloc(4);
2787 e[0] = $1; e[1] = $3; e[2] = '\0';
2788 $$ = e;
2790 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
2792 char *e = palloc(4);
2793 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
2794 $$ = e;
2798 TriggerOneEvent:
2799 INSERT { $$ = 'i'; }
2800 | DELETE_P { $$ = 'd'; }
2801 | UPDATE { $$ = 'u'; }
2802 | TRUNCATE { $$ = 't'; }
2805 TriggerForSpec:
2806 FOR TriggerForOpt TriggerForType
2808 $$ = $3;
2810 | /* EMPTY */
2813 * If ROW/STATEMENT not specified, default to
2814 * STATEMENT, per SQL
2816 $$ = FALSE;
2820 TriggerForOpt:
2821 EACH {}
2822 | /*EMPTY*/ {}
2825 TriggerForType:
2826 ROW { $$ = TRUE; }
2827 | STATEMENT { $$ = FALSE; }
2830 TriggerFuncArgs:
2831 TriggerFuncArg { $$ = list_make1($1); }
2832 | TriggerFuncArgs ',' TriggerFuncArg { $$ = lappend($1, $3); }
2833 | /*EMPTY*/ { $$ = NIL; }
2836 TriggerFuncArg:
2837 ICONST
2839 char buf[64];
2840 snprintf(buf, sizeof(buf), "%d", $1);
2841 $$ = makeString(pstrdup(buf));
2843 | FCONST { $$ = makeString($1); }
2844 | Sconst { $$ = makeString($1); }
2845 | BCONST { $$ = makeString($1); }
2846 | XCONST { $$ = makeString($1); }
2847 | ColId { $$ = makeString($1); }
2850 OptConstrFromTable:
2851 FROM qualified_name { $$ = $2; }
2852 | /*EMPTY*/ { $$ = NULL; }
2855 ConstraintAttributeSpec:
2856 ConstraintDeferrabilitySpec
2857 { $$ = $1; }
2858 | ConstraintDeferrabilitySpec ConstraintTimeSpec
2860 if ($1 == 0 && $2 != 0)
2861 ereport(ERROR,
2862 (errcode(ERRCODE_SYNTAX_ERROR),
2863 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2864 scanner_errposition(@1)));
2865 $$ = $1 | $2;
2867 | ConstraintTimeSpec
2869 if ($1 != 0)
2870 $$ = 3;
2871 else
2872 $$ = 0;
2874 | ConstraintTimeSpec ConstraintDeferrabilitySpec
2876 if ($2 == 0 && $1 != 0)
2877 ereport(ERROR,
2878 (errcode(ERRCODE_SYNTAX_ERROR),
2879 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE"),
2880 scanner_errposition(@1)));
2881 $$ = $1 | $2;
2883 | /*EMPTY*/
2884 { $$ = 0; }
2887 ConstraintDeferrabilitySpec:
2888 NOT DEFERRABLE { $$ = 0; }
2889 | DEFERRABLE { $$ = 1; }
2892 ConstraintTimeSpec:
2893 INITIALLY IMMEDIATE { $$ = 0; }
2894 | INITIALLY DEFERRED { $$ = 2; }
2898 DropTrigStmt:
2899 DROP TRIGGER name ON qualified_name opt_drop_behavior
2901 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2902 n->relation = $5;
2903 n->property = $3;
2904 n->behavior = $6;
2905 n->removeType = OBJECT_TRIGGER;
2906 n->missing_ok = false;
2907 $$ = (Node *) n;
2909 | DROP TRIGGER IF_P EXISTS name ON qualified_name opt_drop_behavior
2911 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2912 n->relation = $7;
2913 n->property = $5;
2914 n->behavior = $8;
2915 n->removeType = OBJECT_TRIGGER;
2916 n->missing_ok = true;
2917 $$ = (Node *) n;
2922 /*****************************************************************************
2924 * QUERIES :
2925 * CREATE ASSERTION ...
2926 * DROP ASSERTION ...
2928 *****************************************************************************/
2930 CreateAssertStmt:
2931 CREATE ASSERTION name CHECK '(' a_expr ')'
2932 ConstraintAttributeSpec
2934 CreateTrigStmt *n = makeNode(CreateTrigStmt);
2935 n->trigname = $3;
2936 n->args = list_make1($6);
2937 n->isconstraint = TRUE;
2938 n->deferrable = ($8 & 1) != 0;
2939 n->initdeferred = ($8 & 2) != 0;
2941 ereport(ERROR,
2942 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2943 errmsg("CREATE ASSERTION is not yet implemented")));
2945 $$ = (Node *)n;
2949 DropAssertStmt:
2950 DROP ASSERTION name opt_drop_behavior
2952 DropPropertyStmt *n = makeNode(DropPropertyStmt);
2953 n->relation = NULL;
2954 n->property = $3;
2955 n->behavior = $4;
2956 n->removeType = OBJECT_TRIGGER; /* XXX */
2957 ereport(ERROR,
2958 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2959 errmsg("DROP ASSERTION is not yet implemented")));
2960 $$ = (Node *) n;
2965 /*****************************************************************************
2967 * QUERY :
2968 * define (aggregate,operator,type)
2970 *****************************************************************************/
2972 DefineStmt:
2973 CREATE AGGREGATE func_name aggr_args definition
2975 DefineStmt *n = makeNode(DefineStmt);
2976 n->kind = OBJECT_AGGREGATE;
2977 n->oldstyle = false;
2978 n->defnames = $3;
2979 n->args = $4;
2980 n->definition = $5;
2981 $$ = (Node *)n;
2983 | CREATE AGGREGATE func_name old_aggr_definition
2985 /* old-style (pre-8.2) syntax for CREATE AGGREGATE */
2986 DefineStmt *n = makeNode(DefineStmt);
2987 n->kind = OBJECT_AGGREGATE;
2988 n->oldstyle = true;
2989 n->defnames = $3;
2990 n->args = NIL;
2991 n->definition = $4;
2992 $$ = (Node *)n;
2994 | CREATE OPERATOR any_operator definition
2996 DefineStmt *n = makeNode(DefineStmt);
2997 n->kind = OBJECT_OPERATOR;
2998 n->oldstyle = false;
2999 n->defnames = $3;
3000 n->args = NIL;
3001 n->definition = $4;
3002 $$ = (Node *)n;
3004 | CREATE TYPE_P any_name definition
3006 DefineStmt *n = makeNode(DefineStmt);
3007 n->kind = OBJECT_TYPE;
3008 n->oldstyle = false;
3009 n->defnames = $3;
3010 n->args = NIL;
3011 n->definition = $4;
3012 $$ = (Node *)n;
3014 | CREATE TYPE_P any_name
3016 /* Shell type (identified by lack of definition) */
3017 DefineStmt *n = makeNode(DefineStmt);
3018 n->kind = OBJECT_TYPE;
3019 n->oldstyle = false;
3020 n->defnames = $3;
3021 n->args = NIL;
3022 n->definition = NIL;
3023 $$ = (Node *)n;
3025 | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
3027 CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
3028 RangeVar *r = makeNode(RangeVar);
3030 /* can't use qualified_name, sigh */
3031 switch (list_length($3))
3033 case 1:
3034 r->catalogname = NULL;
3035 r->schemaname = NULL;
3036 r->relname = strVal(linitial($3));
3037 break;
3038 case 2:
3039 r->catalogname = NULL;
3040 r->schemaname = strVal(linitial($3));
3041 r->relname = strVal(lsecond($3));
3042 break;
3043 case 3:
3044 r->catalogname = strVal(linitial($3));
3045 r->schemaname = strVal(lsecond($3));
3046 r->relname = strVal(lthird($3));
3047 break;
3048 default:
3049 ereport(ERROR,
3050 (errcode(ERRCODE_SYNTAX_ERROR),
3051 errmsg("improper qualified name (too many dotted names): %s",
3052 NameListToString($3)),
3053 scanner_errposition(@3)));
3054 break;
3056 r->location = @3;
3057 n->typevar = r;
3058 n->coldeflist = $6;
3059 $$ = (Node *)n;
3061 | CREATE TYPE_P any_name AS ENUM_P '(' enum_val_list ')'
3063 CreateEnumStmt *n = makeNode(CreateEnumStmt);
3064 n->typename = $3;
3065 n->vals = $7;
3066 $$ = (Node *)n;
3068 | CREATE TEXT_P SEARCH PARSER any_name definition
3070 DefineStmt *n = makeNode(DefineStmt);
3071 n->kind = OBJECT_TSPARSER;
3072 n->args = NIL;
3073 n->defnames = $5;
3074 n->definition = $6;
3075 $$ = (Node *)n;
3077 | CREATE TEXT_P SEARCH DICTIONARY any_name definition
3079 DefineStmt *n = makeNode(DefineStmt);
3080 n->kind = OBJECT_TSDICTIONARY;
3081 n->args = NIL;
3082 n->defnames = $5;
3083 n->definition = $6;
3084 $$ = (Node *)n;
3086 | CREATE TEXT_P SEARCH TEMPLATE any_name definition
3088 DefineStmt *n = makeNode(DefineStmt);
3089 n->kind = OBJECT_TSTEMPLATE;
3090 n->args = NIL;
3091 n->defnames = $5;
3092 n->definition = $6;
3093 $$ = (Node *)n;
3095 | CREATE TEXT_P SEARCH CONFIGURATION any_name definition
3097 DefineStmt *n = makeNode(DefineStmt);
3098 n->kind = OBJECT_TSCONFIGURATION;
3099 n->args = NIL;
3100 n->defnames = $5;
3101 n->definition = $6;
3102 $$ = (Node *)n;
3106 definition: '(' def_list ')' { $$ = $2; }
3109 def_list: def_elem { $$ = list_make1($1); }
3110 | def_list ',' def_elem { $$ = lappend($1, $3); }
3113 def_elem: ColLabel '=' def_arg
3115 $$ = makeDefElem($1, (Node *)$3);
3117 | ColLabel
3119 $$ = makeDefElem($1, NULL);
3123 /* Note: any simple identifier will be returned as a type name! */
3124 def_arg: func_type { $$ = (Node *)$1; }
3125 | reserved_keyword { $$ = (Node *)makeString(pstrdup($1)); }
3126 | qual_all_Op { $$ = (Node *)$1; }
3127 | NumericOnly { $$ = (Node *)$1; }
3128 | Sconst { $$ = (Node *)makeString($1); }
3131 aggr_args: '(' type_list ')' { $$ = $2; }
3132 | '(' '*' ')' { $$ = NIL; }
3135 old_aggr_definition: '(' old_aggr_list ')' { $$ = $2; }
3138 old_aggr_list: old_aggr_elem { $$ = list_make1($1); }
3139 | old_aggr_list ',' old_aggr_elem { $$ = lappend($1, $3); }
3142 old_aggr_elem: IDENT '=' def_arg
3144 $$ = makeDefElem($1, (Node *)$3);
3148 enum_val_list: Sconst
3149 { $$ = list_make1(makeString($1)); }
3150 | enum_val_list ',' Sconst
3151 { $$ = lappend($1, makeString($3)); }
3155 /*****************************************************************************
3157 * QUERIES :
3158 * CREATE OPERATOR CLASS ...
3159 * CREATE OPERATOR FAMILY ...
3160 * ALTER OPERATOR FAMILY ...
3161 * DROP OPERATOR CLASS ...
3162 * DROP OPERATOR FAMILY ...
3164 *****************************************************************************/
3166 CreateOpClassStmt:
3167 CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
3168 USING access_method opt_opfamily AS opclass_item_list
3170 CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
3171 n->opclassname = $4;
3172 n->isDefault = $5;
3173 n->datatype = $8;
3174 n->amname = $10;
3175 n->opfamilyname = $11;
3176 n->items = $13;
3177 $$ = (Node *) n;
3181 opclass_item_list:
3182 opclass_item { $$ = list_make1($1); }
3183 | opclass_item_list ',' opclass_item { $$ = lappend($1, $3); }
3186 opclass_item:
3187 OPERATOR Iconst any_operator opt_recheck
3189 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3190 n->itemtype = OPCLASS_ITEM_OPERATOR;
3191 n->name = $3;
3192 n->args = NIL;
3193 n->number = $2;
3194 $$ = (Node *) n;
3196 | OPERATOR Iconst any_operator oper_argtypes opt_recheck
3198 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3199 n->itemtype = OPCLASS_ITEM_OPERATOR;
3200 n->name = $3;
3201 n->args = $4;
3202 n->number = $2;
3203 $$ = (Node *) n;
3205 | FUNCTION Iconst func_name func_args
3207 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3208 n->itemtype = OPCLASS_ITEM_FUNCTION;
3209 n->name = $3;
3210 n->args = extractArgTypes($4);
3211 n->number = $2;
3212 $$ = (Node *) n;
3214 | FUNCTION Iconst '(' type_list ')' func_name func_args
3216 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3217 n->itemtype = OPCLASS_ITEM_FUNCTION;
3218 n->name = $6;
3219 n->args = extractArgTypes($7);
3220 n->number = $2;
3221 n->class_args = $4;
3222 $$ = (Node *) n;
3224 | STORAGE Typename
3226 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3227 n->itemtype = OPCLASS_ITEM_STORAGETYPE;
3228 n->storedtype = $2;
3229 $$ = (Node *) n;
3233 opt_default: DEFAULT { $$ = TRUE; }
3234 | /*EMPTY*/ { $$ = FALSE; }
3237 opt_opfamily: FAMILY any_name { $$ = $2; }
3238 | /*EMPTY*/ { $$ = NIL; }
3241 opt_recheck: RECHECK
3243 ereport(ERROR,
3244 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3245 errmsg("RECHECK is no longer supported"),
3246 errhint("Update your data type."),
3247 scanner_errposition(@1)));
3248 $$ = TRUE;
3250 | /*EMPTY*/ { $$ = FALSE; }
3254 CreateOpFamilyStmt:
3255 CREATE OPERATOR FAMILY any_name USING access_method
3257 CreateOpFamilyStmt *n = makeNode(CreateOpFamilyStmt);
3258 n->opfamilyname = $4;
3259 n->amname = $6;
3260 $$ = (Node *) n;
3264 AlterOpFamilyStmt:
3265 ALTER OPERATOR FAMILY any_name USING access_method ADD_P opclass_item_list
3267 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3268 n->opfamilyname = $4;
3269 n->amname = $6;
3270 n->isDrop = false;
3271 n->items = $8;
3272 $$ = (Node *) n;
3274 | ALTER OPERATOR FAMILY any_name USING access_method DROP opclass_drop_list
3276 AlterOpFamilyStmt *n = makeNode(AlterOpFamilyStmt);
3277 n->opfamilyname = $4;
3278 n->amname = $6;
3279 n->isDrop = true;
3280 n->items = $8;
3281 $$ = (Node *) n;
3285 opclass_drop_list:
3286 opclass_drop { $$ = list_make1($1); }
3287 | opclass_drop_list ',' opclass_drop { $$ = lappend($1, $3); }
3290 opclass_drop:
3291 OPERATOR Iconst '(' type_list ')'
3293 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3294 n->itemtype = OPCLASS_ITEM_OPERATOR;
3295 n->number = $2;
3296 n->args = $4;
3297 $$ = (Node *) n;
3299 | FUNCTION Iconst '(' type_list ')'
3301 CreateOpClassItem *n = makeNode(CreateOpClassItem);
3302 n->itemtype = OPCLASS_ITEM_FUNCTION;
3303 n->number = $2;
3304 n->args = $4;
3305 $$ = (Node *) n;
3310 DropOpClassStmt:
3311 DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
3313 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3314 n->opclassname = $4;
3315 n->amname = $6;
3316 n->behavior = $7;
3317 n->missing_ok = false;
3318 $$ = (Node *) n;
3320 | DROP OPERATOR CLASS IF_P EXISTS any_name USING access_method opt_drop_behavior
3322 RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
3323 n->opclassname = $6;
3324 n->amname = $8;
3325 n->behavior = $9;
3326 n->missing_ok = true;
3327 $$ = (Node *) n;
3331 DropOpFamilyStmt:
3332 DROP OPERATOR FAMILY any_name USING access_method opt_drop_behavior
3334 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3335 n->opfamilyname = $4;
3336 n->amname = $6;
3337 n->behavior = $7;
3338 n->missing_ok = false;
3339 $$ = (Node *) n;
3341 | DROP OPERATOR FAMILY IF_P EXISTS any_name USING access_method opt_drop_behavior
3343 RemoveOpFamilyStmt *n = makeNode(RemoveOpFamilyStmt);
3344 n->opfamilyname = $6;
3345 n->amname = $8;
3346 n->behavior = $9;
3347 n->missing_ok = true;
3348 $$ = (Node *) n;
3353 /*****************************************************************************
3355 * QUERY:
3357 * DROP OWNED BY username [, username ...] [ RESTRICT | CASCADE ]
3358 * REASSIGN OWNED BY username [, username ...] TO username
3360 *****************************************************************************/
3361 DropOwnedStmt:
3362 DROP OWNED BY name_list opt_drop_behavior
3364 DropOwnedStmt *n = makeNode(DropOwnedStmt);
3365 n->roles = $4;
3366 n->behavior = $5;
3367 $$ = (Node *)n;
3371 ReassignOwnedStmt:
3372 REASSIGN OWNED BY name_list TO name
3374 ReassignOwnedStmt *n = makeNode(ReassignOwnedStmt);
3375 n->roles = $4;
3376 n->newrole = $6;
3377 $$ = (Node *)n;
3381 /*****************************************************************************
3383 * QUERY:
3385 * DROP itemtype [ IF EXISTS ] itemname [, itemname ...]
3386 * [ RESTRICT | CASCADE ]
3388 *****************************************************************************/
3390 DropStmt: DROP drop_type IF_P EXISTS any_name_list opt_drop_behavior
3392 DropStmt *n = makeNode(DropStmt);
3393 n->removeType = $2;
3394 n->missing_ok = TRUE;
3395 n->objects = $5;
3396 n->behavior = $6;
3397 $$ = (Node *)n;
3399 | DROP drop_type any_name_list opt_drop_behavior
3401 DropStmt *n = makeNode(DropStmt);
3402 n->removeType = $2;
3403 n->missing_ok = FALSE;
3404 n->objects = $3;
3405 n->behavior = $4;
3406 $$ = (Node *)n;
3411 drop_type: TABLE { $$ = OBJECT_TABLE; }
3412 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3413 | VIEW { $$ = OBJECT_VIEW; }
3414 | INDEX { $$ = OBJECT_INDEX; }
3415 | TYPE_P { $$ = OBJECT_TYPE; }
3416 | DOMAIN_P { $$ = OBJECT_DOMAIN; }
3417 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3418 | SCHEMA { $$ = OBJECT_SCHEMA; }
3419 | TEXT_P SEARCH PARSER { $$ = OBJECT_TSPARSER; }
3420 | TEXT_P SEARCH DICTIONARY { $$ = OBJECT_TSDICTIONARY; }
3421 | TEXT_P SEARCH TEMPLATE { $$ = OBJECT_TSTEMPLATE; }
3422 | TEXT_P SEARCH CONFIGURATION { $$ = OBJECT_TSCONFIGURATION; }
3425 any_name_list:
3426 any_name { $$ = list_make1($1); }
3427 | any_name_list ',' any_name { $$ = lappend($1, $3); }
3430 any_name: ColId { $$ = list_make1(makeString($1)); }
3431 | ColId attrs { $$ = lcons(makeString($1), $2); }
3434 attrs: '.' attr_name
3435 { $$ = list_make1(makeString($2)); }
3436 | attrs '.' attr_name
3437 { $$ = lappend($1, makeString($3)); }
3441 /*****************************************************************************
3443 * QUERY:
3444 * truncate table relname1, relname2, ...
3446 *****************************************************************************/
3448 TruncateStmt:
3449 TRUNCATE opt_table qualified_name_list opt_restart_seqs opt_drop_behavior
3451 TruncateStmt *n = makeNode(TruncateStmt);
3452 n->relations = $3;
3453 n->restart_seqs = $4;
3454 n->behavior = $5;
3455 $$ = (Node *)n;
3459 opt_restart_seqs:
3460 CONTINUE_P IDENTITY_P { $$ = false; }
3461 | RESTART IDENTITY_P { $$ = true; }
3462 | /* EMPTY */ { $$ = false; }
3465 /*****************************************************************************
3467 * The COMMENT ON statement can take different forms based upon the type of
3468 * the object associated with the comment. The form of the statement is:
3470 * COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
3471 * CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
3472 * CAST | COLUMN | SCHEMA | TABLESPACE | ROLE |
3473 * TEXT SEARCH PARSER | TEXT SEARCH DICTIONARY |
3474 * TEXT SEARCH TEMPLATE |
3475 * TEXT SEARCH CONFIGURATION ] <objname> |
3476 * AGGREGATE <aggname> (arg1, ...) |
3477 * FUNCTION <funcname> (arg1, arg2, ...) |
3478 * OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
3479 * TRIGGER <triggername> ON <relname> |
3480 * CONSTRAINT <constraintname> ON <relname> |
3481 * RULE <rulename> ON <relname> ]
3482 * IS 'text'
3484 *****************************************************************************/
3486 CommentStmt:
3487 COMMENT ON comment_type any_name IS comment_text
3489 CommentStmt *n = makeNode(CommentStmt);
3490 n->objtype = $3;
3491 n->objname = $4;
3492 n->objargs = NIL;
3493 n->comment = $6;
3494 $$ = (Node *) n;
3496 | COMMENT ON AGGREGATE func_name aggr_args IS comment_text
3498 CommentStmt *n = makeNode(CommentStmt);
3499 n->objtype = OBJECT_AGGREGATE;
3500 n->objname = $4;
3501 n->objargs = $5;
3502 n->comment = $7;
3503 $$ = (Node *) n;
3505 | COMMENT ON FUNCTION func_name func_args IS comment_text
3507 CommentStmt *n = makeNode(CommentStmt);
3508 n->objtype = OBJECT_FUNCTION;
3509 n->objname = $4;
3510 n->objargs = extractArgTypes($5);
3511 n->comment = $7;
3512 $$ = (Node *) n;
3514 | COMMENT ON OPERATOR any_operator oper_argtypes IS comment_text
3516 CommentStmt *n = makeNode(CommentStmt);
3517 n->objtype = OBJECT_OPERATOR;
3518 n->objname = $4;
3519 n->objargs = $5;
3520 n->comment = $7;
3521 $$ = (Node *) n;
3523 | COMMENT ON CONSTRAINT name ON any_name IS comment_text
3525 CommentStmt *n = makeNode(CommentStmt);
3526 n->objtype = OBJECT_CONSTRAINT;
3527 n->objname = lappend($6, makeString($4));
3528 n->objargs = NIL;
3529 n->comment = $8;
3530 $$ = (Node *) n;
3532 | COMMENT ON RULE name ON any_name IS comment_text
3534 CommentStmt *n = makeNode(CommentStmt);
3535 n->objtype = OBJECT_RULE;
3536 n->objname = lappend($6, makeString($4));
3537 n->objargs = NIL;
3538 n->comment = $8;
3539 $$ = (Node *) n;
3541 | COMMENT ON RULE name IS comment_text
3543 /* Obsolete syntax supported for awhile for compatibility */
3544 CommentStmt *n = makeNode(CommentStmt);
3545 n->objtype = OBJECT_RULE;
3546 n->objname = list_make1(makeString($4));
3547 n->objargs = NIL;
3548 n->comment = $6;
3549 $$ = (Node *) n;
3551 | COMMENT ON TRIGGER name ON any_name IS comment_text
3553 CommentStmt *n = makeNode(CommentStmt);
3554 n->objtype = OBJECT_TRIGGER;
3555 n->objname = lappend($6, makeString($4));
3556 n->objargs = NIL;
3557 n->comment = $8;
3558 $$ = (Node *) n;
3560 | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
3562 CommentStmt *n = makeNode(CommentStmt);
3563 n->objtype = OBJECT_OPCLASS;
3564 n->objname = $5;
3565 n->objargs = list_make1(makeString($7));
3566 n->comment = $9;
3567 $$ = (Node *) n;
3569 | COMMENT ON OPERATOR FAMILY any_name USING access_method IS comment_text
3571 CommentStmt *n = makeNode(CommentStmt);
3572 n->objtype = OBJECT_OPFAMILY;
3573 n->objname = $5;
3574 n->objargs = list_make1(makeString($7));
3575 n->comment = $9;
3576 $$ = (Node *) n;
3578 | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
3580 CommentStmt *n = makeNode(CommentStmt);
3581 n->objtype = OBJECT_LARGEOBJECT;
3582 n->objname = list_make1($5);
3583 n->objargs = NIL;
3584 n->comment = $7;
3585 $$ = (Node *) n;
3587 | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
3589 CommentStmt *n = makeNode(CommentStmt);
3590 n->objtype = OBJECT_CAST;
3591 n->objname = list_make1($5);
3592 n->objargs = list_make1($7);
3593 n->comment = $10;
3594 $$ = (Node *) n;
3596 | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
3598 CommentStmt *n = makeNode(CommentStmt);
3599 n->objtype = OBJECT_LANGUAGE;
3600 n->objname = $5;
3601 n->objargs = NIL;
3602 n->comment = $7;
3603 $$ = (Node *) n;
3605 | COMMENT ON TEXT_P SEARCH PARSER any_name IS comment_text
3607 CommentStmt *n = makeNode(CommentStmt);
3608 n->objtype = OBJECT_TSPARSER;
3609 n->objname = $6;
3610 n->comment = $8;
3611 $$ = (Node *) n;
3613 | COMMENT ON TEXT_P SEARCH DICTIONARY any_name IS comment_text
3615 CommentStmt *n = makeNode(CommentStmt);
3616 n->objtype = OBJECT_TSDICTIONARY;
3617 n->objname = $6;
3618 n->comment = $8;
3619 $$ = (Node *) n;
3621 | COMMENT ON TEXT_P SEARCH TEMPLATE any_name IS comment_text
3623 CommentStmt *n = makeNode(CommentStmt);
3624 n->objtype = OBJECT_TSTEMPLATE;
3625 n->objname = $6;
3626 n->comment = $8;
3627 $$ = (Node *) n;
3629 | COMMENT ON TEXT_P SEARCH CONFIGURATION any_name IS comment_text
3631 CommentStmt *n = makeNode(CommentStmt);
3632 n->objtype = OBJECT_TSCONFIGURATION;
3633 n->objname = $6;
3634 n->comment = $8;
3635 $$ = (Node *) n;
3639 comment_type:
3640 COLUMN { $$ = OBJECT_COLUMN; }
3641 | DATABASE { $$ = OBJECT_DATABASE; }
3642 | SCHEMA { $$ = OBJECT_SCHEMA; }
3643 | INDEX { $$ = OBJECT_INDEX; }
3644 | SEQUENCE { $$ = OBJECT_SEQUENCE; }
3645 | TABLE { $$ = OBJECT_TABLE; }
3646 | DOMAIN_P { $$ = OBJECT_TYPE; }
3647 | TYPE_P { $$ = OBJECT_TYPE; }
3648 | VIEW { $$ = OBJECT_VIEW; }
3649 | CONVERSION_P { $$ = OBJECT_CONVERSION; }
3650 | TABLESPACE { $$ = OBJECT_TABLESPACE; }
3651 | ROLE { $$ = OBJECT_ROLE; }
3654 comment_text:
3655 Sconst { $$ = $1; }
3656 | NULL_P { $$ = NULL; }
3659 /*****************************************************************************
3661 * QUERY:
3662 * fetch/move
3664 *****************************************************************************/
3666 FetchStmt: FETCH fetch_direction from_in name
3668 FetchStmt *n = (FetchStmt *) $2;
3669 n->portalname = $4;
3670 n->ismove = FALSE;
3671 $$ = (Node *)n;
3673 | FETCH name
3675 FetchStmt *n = makeNode(FetchStmt);
3676 n->direction = FETCH_FORWARD;
3677 n->howMany = 1;
3678 n->portalname = $2;
3679 n->ismove = FALSE;
3680 $$ = (Node *)n;
3682 | MOVE fetch_direction from_in name
3684 FetchStmt *n = (FetchStmt *) $2;
3685 n->portalname = $4;
3686 n->ismove = TRUE;
3687 $$ = (Node *)n;
3689 | MOVE name
3691 FetchStmt *n = makeNode(FetchStmt);
3692 n->direction = FETCH_FORWARD;
3693 n->howMany = 1;
3694 n->portalname = $2;
3695 n->ismove = TRUE;
3696 $$ = (Node *)n;
3700 fetch_direction:
3701 /*EMPTY*/
3703 FetchStmt *n = makeNode(FetchStmt);
3704 n->direction = FETCH_FORWARD;
3705 n->howMany = 1;
3706 $$ = (Node *)n;
3708 | NEXT
3710 FetchStmt *n = makeNode(FetchStmt);
3711 n->direction = FETCH_FORWARD;
3712 n->howMany = 1;
3713 $$ = (Node *)n;
3715 | PRIOR
3717 FetchStmt *n = makeNode(FetchStmt);
3718 n->direction = FETCH_BACKWARD;
3719 n->howMany = 1;
3720 $$ = (Node *)n;
3722 | FIRST_P
3724 FetchStmt *n = makeNode(FetchStmt);
3725 n->direction = FETCH_ABSOLUTE;
3726 n->howMany = 1;
3727 $$ = (Node *)n;
3729 | LAST_P
3731 FetchStmt *n = makeNode(FetchStmt);
3732 n->direction = FETCH_ABSOLUTE;
3733 n->howMany = -1;
3734 $$ = (Node *)n;
3736 | ABSOLUTE_P SignedIconst
3738 FetchStmt *n = makeNode(FetchStmt);
3739 n->direction = FETCH_ABSOLUTE;
3740 n->howMany = $2;
3741 $$ = (Node *)n;
3743 | RELATIVE_P SignedIconst
3745 FetchStmt *n = makeNode(FetchStmt);
3746 n->direction = FETCH_RELATIVE;
3747 n->howMany = $2;
3748 $$ = (Node *)n;
3750 | SignedIconst
3752 FetchStmt *n = makeNode(FetchStmt);
3753 n->direction = FETCH_FORWARD;
3754 n->howMany = $1;
3755 $$ = (Node *)n;
3757 | ALL
3759 FetchStmt *n = makeNode(FetchStmt);
3760 n->direction = FETCH_FORWARD;
3761 n->howMany = FETCH_ALL;
3762 $$ = (Node *)n;
3764 | FORWARD
3766 FetchStmt *n = makeNode(FetchStmt);
3767 n->direction = FETCH_FORWARD;
3768 n->howMany = 1;
3769 $$ = (Node *)n;
3771 | FORWARD SignedIconst
3773 FetchStmt *n = makeNode(FetchStmt);
3774 n->direction = FETCH_FORWARD;
3775 n->howMany = $2;
3776 $$ = (Node *)n;
3778 | FORWARD ALL
3780 FetchStmt *n = makeNode(FetchStmt);
3781 n->direction = FETCH_FORWARD;
3782 n->howMany = FETCH_ALL;
3783 $$ = (Node *)n;
3785 | BACKWARD
3787 FetchStmt *n = makeNode(FetchStmt);
3788 n->direction = FETCH_BACKWARD;
3789 n->howMany = 1;
3790 $$ = (Node *)n;
3792 | BACKWARD SignedIconst
3794 FetchStmt *n = makeNode(FetchStmt);
3795 n->direction = FETCH_BACKWARD;
3796 n->howMany = $2;
3797 $$ = (Node *)n;
3799 | BACKWARD ALL
3801 FetchStmt *n = makeNode(FetchStmt);
3802 n->direction = FETCH_BACKWARD;
3803 n->howMany = FETCH_ALL;
3804 $$ = (Node *)n;
3808 from_in: FROM {}
3809 | IN_P {}
3813 /*****************************************************************************
3815 * GRANT and REVOKE statements
3817 *****************************************************************************/
3819 GrantStmt: GRANT privileges ON privilege_target TO grantee_list
3820 opt_grant_grant_option
3822 GrantStmt *n = makeNode(GrantStmt);
3823 n->is_grant = true;
3824 n->privileges = $2;
3825 n->objtype = ($4)->objtype;
3826 n->objects = ($4)->objs;
3827 n->grantees = $6;
3828 n->grant_option = $7;
3829 $$ = (Node*)n;
3833 RevokeStmt:
3834 REVOKE privileges ON privilege_target
3835 FROM grantee_list opt_drop_behavior
3837 GrantStmt *n = makeNode(GrantStmt);
3838 n->is_grant = false;
3839 n->grant_option = false;
3840 n->privileges = $2;
3841 n->objtype = ($4)->objtype;
3842 n->objects = ($4)->objs;
3843 n->grantees = $6;
3844 n->behavior = $7;
3845 $$ = (Node *)n;
3847 | REVOKE GRANT OPTION FOR privileges ON privilege_target
3848 FROM grantee_list opt_drop_behavior
3850 GrantStmt *n = makeNode(GrantStmt);
3851 n->is_grant = false;
3852 n->grant_option = true;
3853 n->privileges = $5;
3854 n->objtype = ($7)->objtype;
3855 n->objects = ($7)->objs;
3856 n->grantees = $9;
3857 n->behavior = $10;
3858 $$ = (Node *)n;
3864 * A privilege list is represented as a list of strings; the validity of
3865 * the privilege names gets checked at execution. This is a bit annoying
3866 * but we have little choice because of the syntactic conflict with lists
3867 * of role names in GRANT/REVOKE. What's more, we have to call out in
3868 * the "privilege" production any reserved keywords that need to be usable
3869 * as privilege names.
3872 /* either ALL [PRIVILEGES] or a list of individual privileges */
3873 privileges: privilege_list
3874 { $$ = $1; }
3875 | ALL
3876 { $$ = NIL; }
3877 | ALL PRIVILEGES
3878 { $$ = NIL; }
3881 privilege_list: privilege
3882 { $$ = list_make1(makeString($1)); }
3883 | privilege_list ',' privilege
3884 { $$ = lappend($1, makeString($3)); }
3887 privilege: SELECT { $$ = pstrdup($1); }
3888 | REFERENCES { $$ = pstrdup($1); }
3889 | CREATE { $$ = pstrdup($1); }
3890 | ColId { $$ = $1; }
3894 /* Don't bother trying to fold the first two rules into one using
3895 * opt_table. You're going to get conflicts.
3897 privilege_target:
3898 qualified_name_list
3900 PrivTarget *n = makeNode(PrivTarget);
3901 n->objtype = ACL_OBJECT_RELATION;
3902 n->objs = $1;
3903 $$ = n;
3905 | TABLE qualified_name_list
3907 PrivTarget *n = makeNode(PrivTarget);
3908 n->objtype = ACL_OBJECT_RELATION;
3909 n->objs = $2;
3910 $$ = n;
3912 | SEQUENCE qualified_name_list
3914 PrivTarget *n = makeNode(PrivTarget);
3915 n->objtype = ACL_OBJECT_SEQUENCE;
3916 n->objs = $2;
3917 $$ = n;
3919 | FUNCTION function_with_argtypes_list
3921 PrivTarget *n = makeNode(PrivTarget);
3922 n->objtype = ACL_OBJECT_FUNCTION;
3923 n->objs = $2;
3924 $$ = n;
3926 | DATABASE name_list
3928 PrivTarget *n = makeNode(PrivTarget);
3929 n->objtype = ACL_OBJECT_DATABASE;
3930 n->objs = $2;
3931 $$ = n;
3933 | LANGUAGE name_list
3935 PrivTarget *n = makeNode(PrivTarget);
3936 n->objtype = ACL_OBJECT_LANGUAGE;
3937 n->objs = $2;
3938 $$ = n;
3940 | SCHEMA name_list
3942 PrivTarget *n = makeNode(PrivTarget);
3943 n->objtype = ACL_OBJECT_NAMESPACE;
3944 n->objs = $2;
3945 $$ = n;
3947 | TABLESPACE name_list
3949 PrivTarget *n = makeNode(PrivTarget);
3950 n->objtype = ACL_OBJECT_TABLESPACE;
3951 n->objs = $2;
3952 $$ = n;
3957 grantee_list:
3958 grantee { $$ = list_make1($1); }
3959 | grantee_list ',' grantee { $$ = lappend($1, $3); }
3962 grantee: RoleId
3964 PrivGrantee *n = makeNode(PrivGrantee);
3965 /* This hack lets us avoid reserving PUBLIC as a keyword*/
3966 if (strcmp($1, "public") == 0)
3967 n->rolname = NULL;
3968 else
3969 n->rolname = $1;
3970 $$ = (Node *)n;
3972 | GROUP_P RoleId
3974 PrivGrantee *n = makeNode(PrivGrantee);
3975 /* Treat GROUP PUBLIC as a synonym for PUBLIC */
3976 if (strcmp($2, "public") == 0)
3977 n->rolname = NULL;
3978 else
3979 n->rolname = $2;
3980 $$ = (Node *)n;
3985 opt_grant_grant_option:
3986 WITH GRANT OPTION { $$ = TRUE; }
3987 | /*EMPTY*/ { $$ = FALSE; }
3990 function_with_argtypes_list:
3991 function_with_argtypes { $$ = list_make1($1); }
3992 | function_with_argtypes_list ',' function_with_argtypes
3993 { $$ = lappend($1, $3); }
3996 function_with_argtypes:
3997 func_name func_args
3999 FuncWithArgs *n = makeNode(FuncWithArgs);
4000 n->funcname = $1;
4001 n->funcargs = extractArgTypes($2);
4002 $$ = n;
4006 /*****************************************************************************
4008 * GRANT and REVOKE ROLE statements
4010 *****************************************************************************/
4012 GrantRoleStmt:
4013 GRANT privilege_list TO name_list opt_grant_admin_option opt_granted_by
4015 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4016 n->is_grant = true;
4017 n->granted_roles = $2;
4018 n->grantee_roles = $4;
4019 n->admin_opt = $5;
4020 n->grantor = $6;
4021 $$ = (Node*)n;
4025 RevokeRoleStmt:
4026 REVOKE privilege_list FROM name_list opt_granted_by opt_drop_behavior
4028 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4029 n->is_grant = false;
4030 n->admin_opt = false;
4031 n->granted_roles = $2;
4032 n->grantee_roles = $4;
4033 n->behavior = $6;
4034 $$ = (Node*)n;
4036 | REVOKE ADMIN OPTION FOR privilege_list FROM name_list opt_granted_by opt_drop_behavior
4038 GrantRoleStmt *n = makeNode(GrantRoleStmt);
4039 n->is_grant = false;
4040 n->admin_opt = true;
4041 n->granted_roles = $5;
4042 n->grantee_roles = $7;
4043 n->behavior = $9;
4044 $$ = (Node*)n;
4048 opt_grant_admin_option: WITH ADMIN OPTION { $$ = TRUE; }
4049 | /*EMPTY*/ { $$ = FALSE; }
4052 opt_granted_by: GRANTED BY RoleId { $$ = $3; }
4053 | /*EMPTY*/ { $$ = NULL; }
4057 /*****************************************************************************
4059 * QUERY: CREATE INDEX
4061 * Note: we can't factor CONCURRENTLY into a separate production without
4062 * making it a reserved word.
4064 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
4065 * willing to make TABLESPACE a fully reserved word.
4066 *****************************************************************************/
4068 IndexStmt: CREATE index_opt_unique INDEX index_name
4069 ON qualified_name access_method_clause '(' index_params ')'
4070 opt_definition OptTableSpace where_clause
4072 IndexStmt *n = makeNode(IndexStmt);
4073 n->unique = $2;
4074 n->concurrent = false;
4075 n->idxname = $4;
4076 n->relation = $6;
4077 n->accessMethod = $7;
4078 n->indexParams = $9;
4079 n->options = $11;
4080 n->tableSpace = $12;
4081 n->whereClause = $13;
4082 $$ = (Node *)n;
4084 | CREATE index_opt_unique INDEX CONCURRENTLY index_name
4085 ON qualified_name access_method_clause '(' index_params ')'
4086 opt_definition OptTableSpace where_clause
4088 IndexStmt *n = makeNode(IndexStmt);
4089 n->unique = $2;
4090 n->concurrent = true;
4091 n->idxname = $5;
4092 n->relation = $7;
4093 n->accessMethod = $8;
4094 n->indexParams = $10;
4095 n->options = $12;
4096 n->tableSpace = $13;
4097 n->whereClause = $14;
4098 $$ = (Node *)n;
4102 index_opt_unique:
4103 UNIQUE { $$ = TRUE; }
4104 | /*EMPTY*/ { $$ = FALSE; }
4107 access_method_clause:
4108 USING access_method { $$ = $2; }
4109 | /*EMPTY*/ { $$ = DEFAULT_INDEX_TYPE; }
4112 index_params: index_elem { $$ = list_make1($1); }
4113 | index_params ',' index_elem { $$ = lappend($1, $3); }
4117 * Index attributes can be either simple column references, or arbitrary
4118 * expressions in parens. For backwards-compatibility reasons, we allow
4119 * an expression that's just a function call to be written without parens.
4121 index_elem: ColId opt_class opt_asc_desc opt_nulls_order
4123 $$ = makeNode(IndexElem);
4124 $$->name = $1;
4125 $$->expr = NULL;
4126 $$->opclass = $2;
4127 $$->ordering = $3;
4128 $$->nulls_ordering = $4;
4130 | func_expr opt_class opt_asc_desc opt_nulls_order
4132 $$ = makeNode(IndexElem);
4133 $$->name = NULL;
4134 $$->expr = $1;
4135 $$->opclass = $2;
4136 $$->ordering = $3;
4137 $$->nulls_ordering = $4;
4139 | '(' a_expr ')' opt_class opt_asc_desc opt_nulls_order
4141 $$ = makeNode(IndexElem);
4142 $$->name = NULL;
4143 $$->expr = $2;
4144 $$->opclass = $4;
4145 $$->ordering = $5;
4146 $$->nulls_ordering = $6;
4150 opt_class: any_name { $$ = $1; }
4151 | USING any_name { $$ = $2; }
4152 | /*EMPTY*/ { $$ = NIL; }
4155 opt_asc_desc: ASC { $$ = SORTBY_ASC; }
4156 | DESC { $$ = SORTBY_DESC; }
4157 | /*EMPTY*/ { $$ = SORTBY_DEFAULT; }
4160 opt_nulls_order: NULLS_FIRST { $$ = SORTBY_NULLS_FIRST; }
4161 | NULLS_LAST { $$ = SORTBY_NULLS_LAST; }
4162 | /*EMPTY*/ { $$ = SORTBY_NULLS_DEFAULT; }
4166 /*****************************************************************************
4168 * QUERY:
4169 * create [or replace] function <fname>
4170 * [(<type-1> { , <type-n>})]
4171 * returns <type-r>
4172 * as <filename or code in language as appropriate>
4173 * language <lang> [with parameters]
4175 *****************************************************************************/
4177 CreateFunctionStmt:
4178 CREATE opt_or_replace FUNCTION func_name func_args
4179 RETURNS func_return createfunc_opt_list opt_definition
4181 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4182 n->replace = $2;
4183 n->funcname = $4;
4184 n->parameters = $5;
4185 n->returnType = $7;
4186 n->options = $8;
4187 n->withClause = $9;
4188 $$ = (Node *)n;
4190 | CREATE opt_or_replace FUNCTION func_name func_args
4191 RETURNS TABLE '(' table_func_column_list ')' createfunc_opt_list opt_definition
4193 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4194 n->replace = $2;
4195 n->funcname = $4;
4196 n->parameters = mergeTableFuncParameters($5, $9);
4197 n->returnType = TableFuncTypeName($9);
4198 n->returnType->location = @7;
4199 n->options = $11;
4200 n->withClause = $12;
4201 $$ = (Node *)n;
4203 | CREATE opt_or_replace FUNCTION func_name func_args
4204 createfunc_opt_list opt_definition
4206 CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
4207 n->replace = $2;
4208 n->funcname = $4;
4209 n->parameters = $5;
4210 n->returnType = NULL;
4211 n->options = $6;
4212 n->withClause = $7;
4213 $$ = (Node *)n;
4217 opt_or_replace:
4218 OR REPLACE { $$ = TRUE; }
4219 | /*EMPTY*/ { $$ = FALSE; }
4222 func_args: '(' func_args_list ')' { $$ = $2; }
4223 | '(' ')' { $$ = NIL; }
4226 func_args_list:
4227 func_arg { $$ = list_make1($1); }
4228 | func_args_list ',' func_arg { $$ = lappend($1, $3); }
4232 * The style with arg_class first is SQL99 standard, but Oracle puts
4233 * param_name first; accept both since it's likely people will try both
4234 * anyway. Don't bother trying to save productions by letting arg_class
4235 * have an empty alternative ... you'll get shift/reduce conflicts.
4237 * We can catch over-specified arguments here if we want to,
4238 * but for now better to silently swallow typmod, etc.
4239 * - thomas 2000-03-22
4241 func_arg:
4242 arg_class param_name func_type
4244 FunctionParameter *n = makeNode(FunctionParameter);
4245 n->name = $2;
4246 n->argType = $3;
4247 n->mode = $1;
4248 $$ = n;
4250 | param_name arg_class func_type
4252 FunctionParameter *n = makeNode(FunctionParameter);
4253 n->name = $1;
4254 n->argType = $3;
4255 n->mode = $2;
4256 $$ = n;
4258 | param_name func_type
4260 FunctionParameter *n = makeNode(FunctionParameter);
4261 n->name = $1;
4262 n->argType = $2;
4263 n->mode = FUNC_PARAM_IN;
4264 $$ = n;
4266 | arg_class func_type
4268 FunctionParameter *n = makeNode(FunctionParameter);
4269 n->name = NULL;
4270 n->argType = $2;
4271 n->mode = $1;
4272 $$ = n;
4274 | func_type
4276 FunctionParameter *n = makeNode(FunctionParameter);
4277 n->name = NULL;
4278 n->argType = $1;
4279 n->mode = FUNC_PARAM_IN;
4280 $$ = n;
4284 /* INOUT is SQL99 standard, IN OUT is for Oracle compatibility */
4285 arg_class: IN_P { $$ = FUNC_PARAM_IN; }
4286 | OUT_P { $$ = FUNC_PARAM_OUT; }
4287 | INOUT { $$ = FUNC_PARAM_INOUT; }
4288 | IN_P OUT_P { $$ = FUNC_PARAM_INOUT; }
4289 | VARIADIC { $$ = FUNC_PARAM_VARIADIC; }
4293 * Ideally param_name should be ColId, but that causes too many conflicts.
4295 param_name: type_function_name
4298 func_return:
4299 func_type
4301 /* We can catch over-specified results here if we want to,
4302 * but for now better to silently swallow typmod, etc.
4303 * - thomas 2000-03-22
4305 $$ = $1;
4310 * We would like to make the %TYPE productions here be ColId attrs etc,
4311 * but that causes reduce/reduce conflicts. type_function_name
4312 * is next best choice.
4314 func_type: Typename { $$ = $1; }
4315 | type_function_name attrs '%' TYPE_P
4317 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
4318 $$->pct_type = true;
4319 $$->location = @1;
4321 | SETOF type_function_name attrs '%' TYPE_P
4323 $$ = makeTypeNameFromNameList(lcons(makeString($2), $3));
4324 $$->pct_type = true;
4325 $$->setof = TRUE;
4326 $$->location = @2;
4331 createfunc_opt_list:
4332 /* Must be at least one to prevent conflict */
4333 createfunc_opt_item { $$ = list_make1($1); }
4334 | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
4338 * Options common to both CREATE FUNCTION and ALTER FUNCTION
4340 common_func_opt_item:
4341 CALLED ON NULL_P INPUT_P
4343 $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
4345 | RETURNS NULL_P ON NULL_P INPUT_P
4347 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4349 | STRICT_P
4351 $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
4353 | IMMUTABLE
4355 $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
4357 | STABLE
4359 $$ = makeDefElem("volatility", (Node *)makeString("stable"));
4361 | VOLATILE
4363 $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
4365 | EXTERNAL SECURITY DEFINER
4367 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4369 | EXTERNAL SECURITY INVOKER
4371 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4373 | SECURITY DEFINER
4375 $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
4377 | SECURITY INVOKER
4379 $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
4381 | COST NumericOnly
4383 $$ = makeDefElem("cost", (Node *)$2);
4385 | ROWS NumericOnly
4387 $$ = makeDefElem("rows", (Node *)$2);
4389 | SetResetClause
4391 /* we abuse the normal content of a DefElem here */
4392 $$ = makeDefElem("set", (Node *)$1);
4396 createfunc_opt_item:
4397 AS func_as
4399 $$ = makeDefElem("as", (Node *)$2);
4401 | LANGUAGE ColId_or_Sconst
4403 $$ = makeDefElem("language", (Node *)makeString($2));
4405 | common_func_opt_item
4407 $$ = $1;
4411 func_as: Sconst { $$ = list_make1(makeString($1)); }
4412 | Sconst ',' Sconst
4414 $$ = list_make2(makeString($1), makeString($3));
4418 opt_definition:
4419 WITH definition { $$ = $2; }
4420 | /*EMPTY*/ { $$ = NIL; }
4423 table_func_column: param_name func_type
4425 FunctionParameter *n = makeNode(FunctionParameter);
4426 n->name = $1;
4427 n->argType = $2;
4428 n->mode = FUNC_PARAM_TABLE;
4429 $$ = n;
4433 table_func_column_list:
4434 table_func_column
4436 $$ = list_make1($1);
4438 | table_func_column_list ',' table_func_column
4440 $$ = lappend($1, $3);
4444 /*****************************************************************************
4445 * ALTER FUNCTION
4447 * RENAME and OWNER subcommands are already provided by the generic
4448 * ALTER infrastructure, here we just specify alterations that can
4449 * only be applied to functions.
4451 *****************************************************************************/
4452 AlterFunctionStmt:
4453 ALTER FUNCTION function_with_argtypes alterfunc_opt_list opt_restrict
4455 AlterFunctionStmt *n = makeNode(AlterFunctionStmt);
4456 n->func = $3;
4457 n->actions = $4;
4458 $$ = (Node *) n;
4462 alterfunc_opt_list:
4463 /* At least one option must be specified */
4464 common_func_opt_item { $$ = list_make1($1); }
4465 | alterfunc_opt_list common_func_opt_item { $$ = lappend($1, $2); }
4468 /* Ignored, merely for SQL compliance */
4469 opt_restrict:
4470 RESTRICT
4471 | /* EMPTY */
4475 /*****************************************************************************
4477 * QUERY:
4479 * DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
4480 * DROP AGGREGATE aggname (arg1, ...) [ RESTRICT | CASCADE ]
4481 * DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
4483 *****************************************************************************/
4485 RemoveFuncStmt:
4486 DROP FUNCTION func_name func_args opt_drop_behavior
4488 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4489 n->kind = OBJECT_FUNCTION;
4490 n->name = $3;
4491 n->args = extractArgTypes($4);
4492 n->behavior = $5;
4493 n->missing_ok = false;
4494 $$ = (Node *)n;
4496 | DROP FUNCTION IF_P EXISTS func_name func_args opt_drop_behavior
4498 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4499 n->kind = OBJECT_FUNCTION;
4500 n->name = $5;
4501 n->args = extractArgTypes($6);
4502 n->behavior = $7;
4503 n->missing_ok = true;
4504 $$ = (Node *)n;
4508 RemoveAggrStmt:
4509 DROP AGGREGATE func_name aggr_args opt_drop_behavior
4511 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4512 n->kind = OBJECT_AGGREGATE;
4513 n->name = $3;
4514 n->args = $4;
4515 n->behavior = $5;
4516 n->missing_ok = false;
4517 $$ = (Node *)n;
4519 | DROP AGGREGATE IF_P EXISTS func_name aggr_args opt_drop_behavior
4521 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4522 n->kind = OBJECT_AGGREGATE;
4523 n->name = $5;
4524 n->args = $6;
4525 n->behavior = $7;
4526 n->missing_ok = true;
4527 $$ = (Node *)n;
4531 RemoveOperStmt:
4532 DROP OPERATOR any_operator oper_argtypes opt_drop_behavior
4534 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4535 n->kind = OBJECT_OPERATOR;
4536 n->name = $3;
4537 n->args = $4;
4538 n->behavior = $5;
4539 n->missing_ok = false;
4540 $$ = (Node *)n;
4542 | DROP OPERATOR IF_P EXISTS any_operator oper_argtypes opt_drop_behavior
4544 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
4545 n->kind = OBJECT_OPERATOR;
4546 n->name = $5;
4547 n->args = $6;
4548 n->behavior = $7;
4549 n->missing_ok = true;
4550 $$ = (Node *)n;
4554 oper_argtypes:
4555 '(' Typename ')'
4557 ereport(ERROR,
4558 (errcode(ERRCODE_SYNTAX_ERROR),
4559 errmsg("missing argument"),
4560 errhint("Use NONE to denote the missing argument of a unary operator."),
4561 scanner_errposition(@3)));
4563 | '(' Typename ',' Typename ')'
4564 { $$ = list_make2($2, $4); }
4565 | '(' NONE ',' Typename ')' /* left unary */
4566 { $$ = list_make2(NULL, $4); }
4567 | '(' Typename ',' NONE ')' /* right unary */
4568 { $$ = list_make2($2, NULL); }
4571 any_operator:
4572 all_Op
4573 { $$ = list_make1(makeString($1)); }
4574 | ColId '.' any_operator
4575 { $$ = lcons(makeString($1), $3); }
4579 /*****************************************************************************
4581 * CREATE CAST / DROP CAST
4583 *****************************************************************************/
4585 CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
4586 WITH FUNCTION function_with_argtypes cast_context
4588 CreateCastStmt *n = makeNode(CreateCastStmt);
4589 n->sourcetype = $4;
4590 n->targettype = $6;
4591 n->func = $10;
4592 n->context = (CoercionContext) $11;
4593 n->inout = false;
4594 $$ = (Node *)n;
4596 | CREATE CAST '(' Typename AS Typename ')'
4597 WITHOUT FUNCTION cast_context
4599 CreateCastStmt *n = makeNode(CreateCastStmt);
4600 n->sourcetype = $4;
4601 n->targettype = $6;
4602 n->func = NULL;
4603 n->context = (CoercionContext) $10;
4604 n->inout = false;
4605 $$ = (Node *)n;
4607 | CREATE CAST '(' Typename AS Typename ')'
4608 WITH INOUT cast_context
4610 CreateCastStmt *n = makeNode(CreateCastStmt);
4611 n->sourcetype = $4;
4612 n->targettype = $6;
4613 n->func = NULL;
4614 n->context = (CoercionContext) $10;
4615 n->inout = true;
4616 $$ = (Node *)n;
4620 cast_context: AS IMPLICIT_P { $$ = COERCION_IMPLICIT; }
4621 | AS ASSIGNMENT { $$ = COERCION_ASSIGNMENT; }
4622 | /*EMPTY*/ { $$ = COERCION_EXPLICIT; }
4626 DropCastStmt: DROP CAST opt_if_exists '(' Typename AS Typename ')' opt_drop_behavior
4628 DropCastStmt *n = makeNode(DropCastStmt);
4629 n->sourcetype = $5;
4630 n->targettype = $7;
4631 n->behavior = $9;
4632 n->missing_ok = $3;
4633 $$ = (Node *)n;
4637 opt_if_exists: IF_P EXISTS { $$ = TRUE; }
4638 | /*EMPTY*/ { $$ = FALSE; }
4642 /*****************************************************************************
4644 * QUERY:
4646 * REINDEX type <name> [FORCE]
4648 * FORCE no longer does anything, but we accept it for backwards compatibility
4649 *****************************************************************************/
4651 ReindexStmt:
4652 REINDEX reindex_type qualified_name opt_force
4654 ReindexStmt *n = makeNode(ReindexStmt);
4655 n->kind = $2;
4656 n->relation = $3;
4657 n->name = NULL;
4658 $$ = (Node *)n;
4660 | REINDEX SYSTEM_P name opt_force
4662 ReindexStmt *n = makeNode(ReindexStmt);
4663 n->kind = OBJECT_DATABASE;
4664 n->name = $3;
4665 n->relation = NULL;
4666 n->do_system = true;
4667 n->do_user = false;
4668 $$ = (Node *)n;
4670 | REINDEX DATABASE name opt_force
4672 ReindexStmt *n = makeNode(ReindexStmt);
4673 n->kind = OBJECT_DATABASE;
4674 n->name = $3;
4675 n->relation = NULL;
4676 n->do_system = true;
4677 n->do_user = true;
4678 $$ = (Node *)n;
4682 reindex_type:
4683 INDEX { $$ = OBJECT_INDEX; }
4684 | TABLE { $$ = OBJECT_TABLE; }
4687 opt_force: FORCE { $$ = TRUE; }
4688 | /* EMPTY */ { $$ = FALSE; }
4692 /*****************************************************************************
4694 * ALTER THING name RENAME TO newname
4696 *****************************************************************************/
4698 RenameStmt: ALTER AGGREGATE func_name aggr_args RENAME TO name
4700 RenameStmt *n = makeNode(RenameStmt);
4701 n->renameType = OBJECT_AGGREGATE;
4702 n->object = $3;
4703 n->objarg = $4;
4704 n->newname = $7;
4705 $$ = (Node *)n;
4707 | ALTER CONVERSION_P any_name RENAME TO name
4709 RenameStmt *n = makeNode(RenameStmt);
4710 n->renameType = OBJECT_CONVERSION;
4711 n->object = $3;
4712 n->newname = $6;
4713 $$ = (Node *)n;
4715 | ALTER DATABASE database_name RENAME TO database_name
4717 RenameStmt *n = makeNode(RenameStmt);
4718 n->renameType = OBJECT_DATABASE;
4719 n->subname = $3;
4720 n->newname = $6;
4721 $$ = (Node *)n;
4723 | ALTER FUNCTION function_with_argtypes RENAME TO name
4725 RenameStmt *n = makeNode(RenameStmt);
4726 n->renameType = OBJECT_FUNCTION;
4727 n->object = $3->funcname;
4728 n->objarg = $3->funcargs;
4729 n->newname = $6;
4730 $$ = (Node *)n;
4732 | ALTER GROUP_P RoleId RENAME TO RoleId
4734 RenameStmt *n = makeNode(RenameStmt);
4735 n->renameType = OBJECT_ROLE;
4736 n->subname = $3;
4737 n->newname = $6;
4738 $$ = (Node *)n;
4740 | ALTER opt_procedural LANGUAGE name RENAME TO name
4742 RenameStmt *n = makeNode(RenameStmt);
4743 n->renameType = OBJECT_LANGUAGE;
4744 n->subname = $4;
4745 n->newname = $7;
4746 $$ = (Node *)n;
4748 | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
4750 RenameStmt *n = makeNode(RenameStmt);
4751 n->renameType = OBJECT_OPCLASS;
4752 n->object = $4;
4753 n->subname = $6;
4754 n->newname = $9;
4755 $$ = (Node *)n;
4757 | ALTER OPERATOR FAMILY any_name USING access_method RENAME TO name
4759 RenameStmt *n = makeNode(RenameStmt);
4760 n->renameType = OBJECT_OPFAMILY;
4761 n->object = $4;
4762 n->subname = $6;
4763 n->newname = $9;
4764 $$ = (Node *)n;
4766 | ALTER SCHEMA name RENAME TO name
4768 RenameStmt *n = makeNode(RenameStmt);
4769 n->renameType = OBJECT_SCHEMA;
4770 n->subname = $3;
4771 n->newname = $6;
4772 $$ = (Node *)n;
4774 | ALTER TABLE relation_expr RENAME TO name
4776 RenameStmt *n = makeNode(RenameStmt);
4777 n->renameType = OBJECT_TABLE;
4778 n->relation = $3;
4779 n->subname = NULL;
4780 n->newname = $6;
4781 $$ = (Node *)n;
4783 | ALTER SEQUENCE relation_expr RENAME TO name
4785 RenameStmt *n = makeNode(RenameStmt);
4786 n->renameType = OBJECT_SEQUENCE;
4787 n->relation = $3;
4788 n->subname = NULL;
4789 n->newname = $6;
4790 $$ = (Node *)n;
4792 | ALTER VIEW relation_expr RENAME TO name
4794 RenameStmt *n = makeNode(RenameStmt);
4795 n->renameType = OBJECT_VIEW;
4796 n->relation = $3;
4797 n->subname = NULL;
4798 n->newname = $6;
4799 $$ = (Node *)n;
4801 | ALTER INDEX relation_expr RENAME TO name
4803 RenameStmt *n = makeNode(RenameStmt);
4804 n->renameType = OBJECT_INDEX;
4805 n->relation = $3;
4806 n->subname = NULL;
4807 n->newname = $6;
4808 $$ = (Node *)n;
4810 | ALTER TABLE relation_expr RENAME opt_column name TO name
4812 RenameStmt *n = makeNode(RenameStmt);
4813 n->renameType = OBJECT_COLUMN;
4814 n->relation = $3;
4815 n->subname = $6;
4816 n->newname = $8;
4817 $$ = (Node *)n;
4819 | ALTER TRIGGER name ON relation_expr RENAME TO name
4821 RenameStmt *n = makeNode(RenameStmt);
4822 n->renameType = OBJECT_TRIGGER;
4823 n->relation = $5;
4824 n->subname = $3;
4825 n->newname = $8;
4826 $$ = (Node *)n;
4828 | ALTER ROLE RoleId RENAME TO RoleId
4830 RenameStmt *n = makeNode(RenameStmt);
4831 n->renameType = OBJECT_ROLE;
4832 n->subname = $3;
4833 n->newname = $6;
4834 $$ = (Node *)n;
4836 | ALTER USER RoleId RENAME TO RoleId
4838 RenameStmt *n = makeNode(RenameStmt);
4839 n->renameType = OBJECT_ROLE;
4840 n->subname = $3;
4841 n->newname = $6;
4842 $$ = (Node *)n;
4844 | ALTER TABLESPACE name RENAME TO name
4846 RenameStmt *n = makeNode(RenameStmt);
4847 n->renameType = OBJECT_TABLESPACE;
4848 n->subname = $3;
4849 n->newname = $6;
4850 $$ = (Node *)n;
4852 | ALTER TEXT_P SEARCH PARSER any_name RENAME TO name
4854 RenameStmt *n = makeNode(RenameStmt);
4855 n->renameType = OBJECT_TSPARSER;
4856 n->object = $5;
4857 n->newname = $8;
4858 $$ = (Node *)n;
4860 | ALTER TEXT_P SEARCH DICTIONARY any_name RENAME TO name
4862 RenameStmt *n = makeNode(RenameStmt);
4863 n->renameType = OBJECT_TSDICTIONARY;
4864 n->object = $5;
4865 n->newname = $8;
4866 $$ = (Node *)n;
4868 | ALTER TEXT_P SEARCH TEMPLATE any_name RENAME TO name
4870 RenameStmt *n = makeNode(RenameStmt);
4871 n->renameType = OBJECT_TSTEMPLATE;
4872 n->object = $5;
4873 n->newname = $8;
4874 $$ = (Node *)n;
4876 | ALTER TEXT_P SEARCH CONFIGURATION any_name RENAME TO name
4878 RenameStmt *n = makeNode(RenameStmt);
4879 n->renameType = OBJECT_TSCONFIGURATION;
4880 n->object = $5;
4881 n->newname = $8;
4882 $$ = (Node *)n;
4884 | ALTER TYPE_P any_name RENAME TO name
4886 RenameStmt *n = makeNode(RenameStmt);
4887 n->renameType = OBJECT_TYPE;
4888 n->object = $3;
4889 n->newname = $6;
4890 $$ = (Node *)n;
4894 opt_column: COLUMN { $$ = COLUMN; }
4895 | /*EMPTY*/ { $$ = 0; }
4898 opt_set_data: SET DATA_P { $$ = 1; }
4899 | /*EMPTY*/ { $$ = 0; }
4902 /*****************************************************************************
4904 * ALTER THING name SET SCHEMA name
4906 *****************************************************************************/
4908 AlterObjectSchemaStmt:
4909 ALTER AGGREGATE func_name aggr_args SET SCHEMA name
4911 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4912 n->objectType = OBJECT_AGGREGATE;
4913 n->object = $3;
4914 n->objarg = $4;
4915 n->newschema = $7;
4916 $$ = (Node *)n;
4918 | ALTER DOMAIN_P any_name SET SCHEMA name
4920 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4921 n->objectType = OBJECT_DOMAIN;
4922 n->object = $3;
4923 n->newschema = $6;
4924 $$ = (Node *)n;
4926 | ALTER FUNCTION function_with_argtypes SET SCHEMA name
4928 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4929 n->objectType = OBJECT_FUNCTION;
4930 n->object = $3->funcname;
4931 n->objarg = $3->funcargs;
4932 n->newschema = $6;
4933 $$ = (Node *)n;
4935 | ALTER TABLE relation_expr SET SCHEMA name
4937 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4938 n->objectType = OBJECT_TABLE;
4939 n->relation = $3;
4940 n->newschema = $6;
4941 $$ = (Node *)n;
4943 | ALTER SEQUENCE relation_expr SET SCHEMA name
4945 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4946 n->objectType = OBJECT_SEQUENCE;
4947 n->relation = $3;
4948 n->newschema = $6;
4949 $$ = (Node *)n;
4951 | ALTER VIEW relation_expr SET SCHEMA name
4953 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4954 n->objectType = OBJECT_VIEW;
4955 n->relation = $3;
4956 n->newschema = $6;
4957 $$ = (Node *)n;
4959 | ALTER TYPE_P any_name SET SCHEMA name
4961 AlterObjectSchemaStmt *n = makeNode(AlterObjectSchemaStmt);
4962 n->objectType = OBJECT_TYPE;
4963 n->object = $3;
4964 n->newschema = $6;
4965 $$ = (Node *)n;
4969 /*****************************************************************************
4971 * ALTER THING name OWNER TO newname
4973 *****************************************************************************/
4975 AlterOwnerStmt: ALTER AGGREGATE func_name aggr_args OWNER TO RoleId
4977 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4978 n->objectType = OBJECT_AGGREGATE;
4979 n->object = $3;
4980 n->objarg = $4;
4981 n->newowner = $7;
4982 $$ = (Node *)n;
4984 | ALTER CONVERSION_P any_name OWNER TO RoleId
4986 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4987 n->objectType = OBJECT_CONVERSION;
4988 n->object = $3;
4989 n->newowner = $6;
4990 $$ = (Node *)n;
4992 | ALTER DATABASE database_name OWNER TO RoleId
4994 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
4995 n->objectType = OBJECT_DATABASE;
4996 n->object = list_make1(makeString($3));
4997 n->newowner = $6;
4998 $$ = (Node *)n;
5000 | ALTER DOMAIN_P any_name OWNER TO RoleId
5002 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5003 n->objectType = OBJECT_DOMAIN;
5004 n->object = $3;
5005 n->newowner = $6;
5006 $$ = (Node *)n;
5008 | ALTER FUNCTION function_with_argtypes OWNER TO RoleId
5010 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5011 n->objectType = OBJECT_FUNCTION;
5012 n->object = $3->funcname;
5013 n->objarg = $3->funcargs;
5014 n->newowner = $6;
5015 $$ = (Node *)n;
5017 | ALTER opt_procedural LANGUAGE name OWNER TO RoleId
5019 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5020 n->objectType = OBJECT_LANGUAGE;
5021 n->object = list_make1(makeString($4));
5022 n->newowner = $7;
5023 $$ = (Node *)n;
5025 | ALTER OPERATOR any_operator oper_argtypes OWNER TO RoleId
5027 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5028 n->objectType = OBJECT_OPERATOR;
5029 n->object = $3;
5030 n->objarg = $4;
5031 n->newowner = $7;
5032 $$ = (Node *)n;
5034 | ALTER OPERATOR CLASS any_name USING access_method OWNER TO RoleId
5036 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5037 n->objectType = OBJECT_OPCLASS;
5038 n->object = $4;
5039 n->addname = $6;
5040 n->newowner = $9;
5041 $$ = (Node *)n;
5043 | ALTER OPERATOR FAMILY any_name USING access_method OWNER TO RoleId
5045 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5046 n->objectType = OBJECT_OPFAMILY;
5047 n->object = $4;
5048 n->addname = $6;
5049 n->newowner = $9;
5050 $$ = (Node *)n;
5052 | ALTER SCHEMA name OWNER TO RoleId
5054 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5055 n->objectType = OBJECT_SCHEMA;
5056 n->object = list_make1(makeString($3));
5057 n->newowner = $6;
5058 $$ = (Node *)n;
5060 | ALTER TYPE_P any_name OWNER TO RoleId
5062 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5063 n->objectType = OBJECT_TYPE;
5064 n->object = $3;
5065 n->newowner = $6;
5066 $$ = (Node *)n;
5068 | ALTER TABLESPACE name OWNER TO RoleId
5070 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5071 n->objectType = OBJECT_TABLESPACE;
5072 n->object = list_make1(makeString($3));
5073 n->newowner = $6;
5074 $$ = (Node *)n;
5076 | ALTER TEXT_P SEARCH DICTIONARY any_name OWNER TO RoleId
5078 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5079 n->objectType = OBJECT_TSDICTIONARY;
5080 n->object = $5;
5081 n->newowner = $8;
5082 $$ = (Node *)n;
5084 | ALTER TEXT_P SEARCH CONFIGURATION any_name OWNER TO RoleId
5086 AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
5087 n->objectType = OBJECT_TSCONFIGURATION;
5088 n->object = $5;
5089 n->newowner = $8;
5090 $$ = (Node *)n;
5095 /*****************************************************************************
5097 * QUERY: Define Rewrite Rule
5099 *****************************************************************************/
5101 RuleStmt: CREATE opt_or_replace RULE name AS
5102 { QueryIsRule=TRUE; }
5103 ON event TO qualified_name where_clause
5104 DO opt_instead RuleActionList
5106 RuleStmt *n = makeNode(RuleStmt);
5107 n->replace = $2;
5108 n->relation = $10;
5109 n->rulename = $4;
5110 n->whereClause = $11;
5111 n->event = $8;
5112 n->instead = $13;
5113 n->actions = $14;
5114 $$ = (Node *)n;
5115 QueryIsRule=FALSE;
5119 RuleActionList:
5120 NOTHING { $$ = NIL; }
5121 | RuleActionStmt { $$ = list_make1($1); }
5122 | '(' RuleActionMulti ')' { $$ = $2; }
5125 /* the thrashing around here is to discard "empty" statements... */
5126 RuleActionMulti:
5127 RuleActionMulti ';' RuleActionStmtOrEmpty
5128 { if ($3 != NULL)
5129 $$ = lappend($1, $3);
5130 else
5131 $$ = $1;
5133 | RuleActionStmtOrEmpty
5134 { if ($1 != NULL)
5135 $$ = list_make1($1);
5136 else
5137 $$ = NIL;
5141 RuleActionStmt:
5142 SelectStmt
5143 | InsertStmt
5144 | UpdateStmt
5145 | DeleteStmt
5146 | NotifyStmt
5149 RuleActionStmtOrEmpty:
5150 RuleActionStmt { $$ = $1; }
5151 | /*EMPTY*/ { $$ = NULL; }
5154 event: SELECT { $$ = CMD_SELECT; }
5155 | UPDATE { $$ = CMD_UPDATE; }
5156 | DELETE_P { $$ = CMD_DELETE; }
5157 | INSERT { $$ = CMD_INSERT; }
5160 opt_instead:
5161 INSTEAD { $$ = TRUE; }
5162 | ALSO { $$ = FALSE; }
5163 | /*EMPTY*/ { $$ = FALSE; }
5167 DropRuleStmt:
5168 DROP RULE name ON qualified_name opt_drop_behavior
5170 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5171 n->relation = $5;
5172 n->property = $3;
5173 n->behavior = $6;
5174 n->removeType = OBJECT_RULE;
5175 n->missing_ok = false;
5176 $$ = (Node *) n;
5178 | DROP RULE IF_P EXISTS name ON qualified_name opt_drop_behavior
5180 DropPropertyStmt *n = makeNode(DropPropertyStmt);
5181 n->relation = $7;
5182 n->property = $5;
5183 n->behavior = $8;
5184 n->removeType = OBJECT_RULE;
5185 n->missing_ok = true;
5186 $$ = (Node *) n;
5191 /*****************************************************************************
5193 * QUERY:
5194 * NOTIFY <identifier> can appear both in rule bodies and
5195 * as a query-level command
5197 *****************************************************************************/
5199 NotifyStmt: NOTIFY ColId
5201 NotifyStmt *n = makeNode(NotifyStmt);
5202 n->conditionname = $2;
5203 $$ = (Node *)n;
5207 ListenStmt: LISTEN ColId
5209 ListenStmt *n = makeNode(ListenStmt);
5210 n->conditionname = $2;
5211 $$ = (Node *)n;
5215 UnlistenStmt:
5216 UNLISTEN ColId
5218 UnlistenStmt *n = makeNode(UnlistenStmt);
5219 n->conditionname = $2;
5220 $$ = (Node *)n;
5222 | UNLISTEN '*'
5224 UnlistenStmt *n = makeNode(UnlistenStmt);
5225 n->conditionname = NULL;
5226 $$ = (Node *)n;
5231 /*****************************************************************************
5233 * Transactions:
5235 * BEGIN / COMMIT / ROLLBACK
5236 * (also older versions END / ABORT)
5238 *****************************************************************************/
5240 TransactionStmt:
5241 ABORT_P opt_transaction
5243 TransactionStmt *n = makeNode(TransactionStmt);
5244 n->kind = TRANS_STMT_ROLLBACK;
5245 n->options = NIL;
5246 $$ = (Node *)n;
5248 | BEGIN_P opt_transaction transaction_mode_list_or_empty
5250 TransactionStmt *n = makeNode(TransactionStmt);
5251 n->kind = TRANS_STMT_BEGIN;
5252 n->options = $3;
5253 $$ = (Node *)n;
5255 | START TRANSACTION transaction_mode_list_or_empty
5257 TransactionStmt *n = makeNode(TransactionStmt);
5258 n->kind = TRANS_STMT_START;
5259 n->options = $3;
5260 $$ = (Node *)n;
5262 | COMMIT opt_transaction
5264 TransactionStmt *n = makeNode(TransactionStmt);
5265 n->kind = TRANS_STMT_COMMIT;
5266 n->options = NIL;
5267 $$ = (Node *)n;
5269 | END_P opt_transaction
5271 TransactionStmt *n = makeNode(TransactionStmt);
5272 n->kind = TRANS_STMT_COMMIT;
5273 n->options = NIL;
5274 $$ = (Node *)n;
5276 | ROLLBACK opt_transaction
5278 TransactionStmt *n = makeNode(TransactionStmt);
5279 n->kind = TRANS_STMT_ROLLBACK;
5280 n->options = NIL;
5281 $$ = (Node *)n;
5283 | SAVEPOINT ColId
5285 TransactionStmt *n = makeNode(TransactionStmt);
5286 n->kind = TRANS_STMT_SAVEPOINT;
5287 n->options = list_make1(makeDefElem("savepoint_name",
5288 (Node *)makeString($2)));
5289 $$ = (Node *)n;
5291 | RELEASE SAVEPOINT ColId
5293 TransactionStmt *n = makeNode(TransactionStmt);
5294 n->kind = TRANS_STMT_RELEASE;
5295 n->options = list_make1(makeDefElem("savepoint_name",
5296 (Node *)makeString($3)));
5297 $$ = (Node *)n;
5299 | RELEASE ColId
5301 TransactionStmt *n = makeNode(TransactionStmt);
5302 n->kind = TRANS_STMT_RELEASE;
5303 n->options = list_make1(makeDefElem("savepoint_name",
5304 (Node *)makeString($2)));
5305 $$ = (Node *)n;
5307 | ROLLBACK opt_transaction TO SAVEPOINT ColId
5309 TransactionStmt *n = makeNode(TransactionStmt);
5310 n->kind = TRANS_STMT_ROLLBACK_TO;
5311 n->options = list_make1(makeDefElem("savepoint_name",
5312 (Node *)makeString($5)));
5313 $$ = (Node *)n;
5315 | ROLLBACK opt_transaction TO ColId
5317 TransactionStmt *n = makeNode(TransactionStmt);
5318 n->kind = TRANS_STMT_ROLLBACK_TO;
5319 n->options = list_make1(makeDefElem("savepoint_name",
5320 (Node *)makeString($4)));
5321 $$ = (Node *)n;
5323 | PREPARE TRANSACTION Sconst
5325 TransactionStmt *n = makeNode(TransactionStmt);
5326 n->kind = TRANS_STMT_PREPARE;
5327 n->gid = $3;
5328 $$ = (Node *)n;
5330 | COMMIT PREPARED Sconst
5332 TransactionStmt *n = makeNode(TransactionStmt);
5333 n->kind = TRANS_STMT_COMMIT_PREPARED;
5334 n->gid = $3;
5335 $$ = (Node *)n;
5337 | ROLLBACK PREPARED Sconst
5339 TransactionStmt *n = makeNode(TransactionStmt);
5340 n->kind = TRANS_STMT_ROLLBACK_PREPARED;
5341 n->gid = $3;
5342 $$ = (Node *)n;
5346 opt_transaction: WORK {}
5347 | TRANSACTION {}
5348 | /*EMPTY*/ {}
5351 transaction_mode_item:
5352 ISOLATION LEVEL iso_level
5353 { $$ = makeDefElem("transaction_isolation",
5354 makeStringConst($3, @3)); }
5355 | READ ONLY
5356 { $$ = makeDefElem("transaction_read_only",
5357 makeIntConst(TRUE, @1)); }
5358 | READ WRITE
5359 { $$ = makeDefElem("transaction_read_only",
5360 makeIntConst(FALSE, @1)); }
5363 /* Syntax with commas is SQL-spec, without commas is Postgres historical */
5364 transaction_mode_list:
5365 transaction_mode_item
5366 { $$ = list_make1($1); }
5367 | transaction_mode_list ',' transaction_mode_item
5368 { $$ = lappend($1, $3); }
5369 | transaction_mode_list transaction_mode_item
5370 { $$ = lappend($1, $2); }
5373 transaction_mode_list_or_empty:
5374 transaction_mode_list
5375 | /* EMPTY */
5376 { $$ = NIL; }
5380 /*****************************************************************************
5382 * QUERY:
5383 * CREATE [ OR REPLACE ] [ TEMP ] VIEW <viewname> '('target-list ')'
5384 * AS <query> [ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
5386 *****************************************************************************/
5388 ViewStmt: CREATE OptTemp VIEW qualified_name opt_column_list
5389 AS SelectStmt opt_check_option
5391 ViewStmt *n = makeNode(ViewStmt);
5392 n->view = $4;
5393 n->view->istemp = $2;
5394 n->aliases = $5;
5395 n->query = $7;
5396 n->replace = false;
5397 $$ = (Node *) n;
5399 | CREATE OR REPLACE OptTemp VIEW qualified_name opt_column_list
5400 AS SelectStmt opt_check_option
5402 ViewStmt *n = makeNode(ViewStmt);
5403 n->view = $6;
5404 n->view->istemp = $4;
5405 n->aliases = $7;
5406 n->query = $9;
5407 n->replace = true;
5408 $$ = (Node *) n;
5412 opt_check_option:
5413 WITH CHECK OPTION
5415 ereport(ERROR,
5416 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5417 errmsg("WITH CHECK OPTION is not implemented")));
5419 | WITH CASCADED CHECK OPTION
5421 ereport(ERROR,
5422 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5423 errmsg("WITH CHECK OPTION is not implemented")));
5425 | WITH LOCAL CHECK OPTION
5427 ereport(ERROR,
5428 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5429 errmsg("WITH CHECK OPTION is not implemented")));
5431 | /* EMPTY */ { $$ = NIL; }
5434 /*****************************************************************************
5436 * QUERY:
5437 * LOAD "filename"
5439 *****************************************************************************/
5441 LoadStmt: LOAD file_name
5443 LoadStmt *n = makeNode(LoadStmt);
5444 n->filename = $2;
5445 $$ = (Node *)n;
5450 /*****************************************************************************
5452 * CREATE DATABASE
5454 *****************************************************************************/
5456 CreatedbStmt:
5457 CREATE DATABASE database_name opt_with createdb_opt_list
5459 CreatedbStmt *n = makeNode(CreatedbStmt);
5460 n->dbname = $3;
5461 n->options = $5;
5462 $$ = (Node *)n;
5466 createdb_opt_list:
5467 createdb_opt_list createdb_opt_item { $$ = lappend($1, $2); }
5468 | /* EMPTY */ { $$ = NIL; }
5471 createdb_opt_item:
5472 TABLESPACE opt_equal name
5474 $$ = makeDefElem("tablespace", (Node *)makeString($3));
5476 | TABLESPACE opt_equal DEFAULT
5478 $$ = makeDefElem("tablespace", NULL);
5480 | LOCATION opt_equal Sconst
5482 $$ = makeDefElem("location", (Node *)makeString($3));
5484 | LOCATION opt_equal DEFAULT
5486 $$ = makeDefElem("location", NULL);
5488 | TEMPLATE opt_equal name
5490 $$ = makeDefElem("template", (Node *)makeString($3));
5492 | TEMPLATE opt_equal DEFAULT
5494 $$ = makeDefElem("template", NULL);
5496 | ENCODING opt_equal Sconst
5498 $$ = makeDefElem("encoding", (Node *)makeString($3));
5500 | ENCODING opt_equal Iconst
5502 $$ = makeDefElem("encoding", (Node *)makeInteger($3));
5504 | ENCODING opt_equal DEFAULT
5506 $$ = makeDefElem("encoding", NULL);
5508 | COLLATE opt_equal Sconst
5510 $$ = makeDefElem("collate", (Node *)makeString($3));
5512 | COLLATE opt_equal DEFAULT
5514 $$ = makeDefElem("collate", NULL);
5516 | CTYPE opt_equal Sconst
5518 $$ = makeDefElem("ctype", (Node *)makeString($3));
5520 | CTYPE opt_equal DEFAULT
5522 $$ = makeDefElem("ctype", NULL);
5524 | CONNECTION LIMIT opt_equal SignedIconst
5526 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5528 | OWNER opt_equal name
5530 $$ = makeDefElem("owner", (Node *)makeString($3));
5532 | OWNER opt_equal DEFAULT
5534 $$ = makeDefElem("owner", NULL);
5539 * Though the equals sign doesn't match other WITH options, pg_dump uses
5540 * equals for backward compatibility, and it doesn't seem worth removing it.
5542 opt_equal: '=' {}
5543 | /*EMPTY*/ {}
5547 /*****************************************************************************
5549 * ALTER DATABASE
5551 *****************************************************************************/
5553 AlterDatabaseStmt:
5554 ALTER DATABASE database_name opt_with alterdb_opt_list
5556 AlterDatabaseStmt *n = makeNode(AlterDatabaseStmt);
5557 n->dbname = $3;
5558 n->options = $5;
5559 $$ = (Node *)n;
5563 AlterDatabaseSetStmt:
5564 ALTER DATABASE database_name SetResetClause
5566 AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
5567 n->dbname = $3;
5568 n->setstmt = $4;
5569 $$ = (Node *)n;
5574 alterdb_opt_list:
5575 alterdb_opt_list alterdb_opt_item { $$ = lappend($1, $2); }
5576 | /* EMPTY */ { $$ = NIL; }
5579 alterdb_opt_item:
5580 CONNECTION LIMIT opt_equal SignedIconst
5582 $$ = makeDefElem("connectionlimit", (Node *)makeInteger($4));
5587 /*****************************************************************************
5589 * DROP DATABASE [ IF EXISTS ]
5591 * This is implicitly CASCADE, no need for drop behavior
5592 *****************************************************************************/
5594 DropdbStmt: DROP DATABASE database_name
5596 DropdbStmt *n = makeNode(DropdbStmt);
5597 n->dbname = $3;
5598 n->missing_ok = FALSE;
5599 $$ = (Node *)n;
5601 | DROP DATABASE IF_P EXISTS database_name
5603 DropdbStmt *n = makeNode(DropdbStmt);
5604 n->dbname = $5;
5605 n->missing_ok = TRUE;
5606 $$ = (Node *)n;
5611 /*****************************************************************************
5613 * Manipulate a domain
5615 *****************************************************************************/
5617 CreateDomainStmt:
5618 CREATE DOMAIN_P any_name opt_as Typename ColQualList
5620 CreateDomainStmt *n = makeNode(CreateDomainStmt);
5621 n->domainname = $3;
5622 n->typename = $5;
5623 n->constraints = $6;
5624 $$ = (Node *)n;
5628 AlterDomainStmt:
5629 /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
5630 ALTER DOMAIN_P any_name alter_column_default
5632 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5633 n->subtype = 'T';
5634 n->typename = $3;
5635 n->def = $4;
5636 $$ = (Node *)n;
5638 /* ALTER DOMAIN <domain> DROP NOT NULL */
5639 | ALTER DOMAIN_P any_name DROP NOT NULL_P
5641 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5642 n->subtype = 'N';
5643 n->typename = $3;
5644 $$ = (Node *)n;
5646 /* ALTER DOMAIN <domain> SET NOT NULL */
5647 | ALTER DOMAIN_P any_name SET NOT NULL_P
5649 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5650 n->subtype = 'O';
5651 n->typename = $3;
5652 $$ = (Node *)n;
5654 /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
5655 | ALTER DOMAIN_P any_name ADD_P TableConstraint
5657 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5658 n->subtype = 'C';
5659 n->typename = $3;
5660 n->def = $5;
5661 $$ = (Node *)n;
5663 /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
5664 | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
5666 AlterDomainStmt *n = makeNode(AlterDomainStmt);
5667 n->subtype = 'X';
5668 n->typename = $3;
5669 n->name = $6;
5670 n->behavior = $7;
5671 $$ = (Node *)n;
5675 opt_as: AS {}
5676 | /* EMPTY */ {}
5680 /*****************************************************************************
5682 * Manipulate a text search dictionary or configuration
5684 *****************************************************************************/
5686 AlterTSDictionaryStmt:
5687 ALTER TEXT_P SEARCH DICTIONARY any_name definition
5689 AlterTSDictionaryStmt *n = makeNode(AlterTSDictionaryStmt);
5690 n->dictname = $5;
5691 n->options = $6;
5692 $$ = (Node *)n;
5696 AlterTSConfigurationStmt:
5697 ALTER TEXT_P SEARCH CONFIGURATION any_name ADD_P MAPPING FOR name_list WITH any_name_list
5699 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5700 n->cfgname = $5;
5701 n->tokentype = $9;
5702 n->dicts = $11;
5703 n->override = false;
5704 n->replace = false;
5705 $$ = (Node*)n;
5707 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list WITH any_name_list
5709 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5710 n->cfgname = $5;
5711 n->tokentype = $9;
5712 n->dicts = $11;
5713 n->override = true;
5714 n->replace = false;
5715 $$ = (Node*)n;
5717 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING REPLACE any_name WITH any_name
5719 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5720 n->cfgname = $5;
5721 n->tokentype = NIL;
5722 n->dicts = list_make2($9,$11);
5723 n->override = false;
5724 n->replace = true;
5725 $$ = (Node*)n;
5727 | ALTER TEXT_P SEARCH CONFIGURATION any_name ALTER MAPPING FOR name_list REPLACE any_name WITH any_name
5729 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5730 n->cfgname = $5;
5731 n->tokentype = $9;
5732 n->dicts = list_make2($11,$13);
5733 n->override = false;
5734 n->replace = true;
5735 $$ = (Node*)n;
5737 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING FOR name_list
5739 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5740 n->cfgname = $5;
5741 n->tokentype = $9;
5742 n->missing_ok = false;
5743 $$ = (Node*)n;
5745 | ALTER TEXT_P SEARCH CONFIGURATION any_name DROP MAPPING IF_P EXISTS FOR name_list
5747 AlterTSConfigurationStmt *n = makeNode(AlterTSConfigurationStmt);
5748 n->cfgname = $5;
5749 n->tokentype = $11;
5750 n->missing_ok = true;
5751 $$ = (Node*)n;
5756 /*****************************************************************************
5758 * Manipulate a conversion
5760 * CREATE [DEFAULT] CONVERSION <conversion_name>
5761 * FOR <encoding_name> TO <encoding_name> FROM <func_name>
5763 *****************************************************************************/
5765 CreateConversionStmt:
5766 CREATE opt_default CONVERSION_P any_name FOR Sconst
5767 TO Sconst FROM any_name
5769 CreateConversionStmt *n = makeNode(CreateConversionStmt);
5770 n->conversion_name = $4;
5771 n->for_encoding_name = $6;
5772 n->to_encoding_name = $8;
5773 n->func_name = $10;
5774 n->def = $2;
5775 $$ = (Node *)n;
5779 /*****************************************************************************
5781 * QUERY:
5782 * CLUSTER <qualified_name> [ USING <index_name> ]
5783 * CLUSTER
5784 * CLUSTER <index_name> ON <qualified_name> (for pre-8.3)
5786 *****************************************************************************/
5788 ClusterStmt:
5789 CLUSTER qualified_name cluster_index_specification
5791 ClusterStmt *n = makeNode(ClusterStmt);
5792 n->relation = $2;
5793 n->indexname = $3;
5794 $$ = (Node*)n;
5796 | CLUSTER
5798 ClusterStmt *n = makeNode(ClusterStmt);
5799 n->relation = NULL;
5800 n->indexname = NULL;
5801 $$ = (Node*)n;
5803 /* kept for pre-8.3 compatibility */
5804 | CLUSTER index_name ON qualified_name
5806 ClusterStmt *n = makeNode(ClusterStmt);
5807 n->relation = $4;
5808 n->indexname = $2;
5809 $$ = (Node*)n;
5813 cluster_index_specification:
5814 USING index_name { $$ = $2; }
5815 | /*EMPTY*/ { $$ = NULL; }
5819 /*****************************************************************************
5821 * QUERY:
5822 * VACUUM
5823 * ANALYZE
5825 *****************************************************************************/
5827 VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
5829 VacuumStmt *n = makeNode(VacuumStmt);
5830 n->vacuum = true;
5831 n->analyze = false;
5832 n->full = $2;
5833 n->freeze_min_age = $3 ? 0 : -1;
5834 n->verbose = $4;
5835 n->relation = NULL;
5836 n->va_cols = NIL;
5837 $$ = (Node *)n;
5839 | VACUUM opt_full opt_freeze opt_verbose qualified_name
5841 VacuumStmt *n = makeNode(VacuumStmt);
5842 n->vacuum = true;
5843 n->analyze = false;
5844 n->full = $2;
5845 n->freeze_min_age = $3 ? 0 : -1;
5846 n->verbose = $4;
5847 n->relation = $5;
5848 n->va_cols = NIL;
5849 $$ = (Node *)n;
5851 | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
5853 VacuumStmt *n = (VacuumStmt *) $5;
5854 n->vacuum = true;
5855 n->full = $2;
5856 n->freeze_min_age = $3 ? 0 : -1;
5857 n->verbose |= $4;
5858 $$ = (Node *)n;
5862 AnalyzeStmt:
5863 analyze_keyword opt_verbose
5865 VacuumStmt *n = makeNode(VacuumStmt);
5866 n->vacuum = false;
5867 n->analyze = true;
5868 n->full = false;
5869 n->freeze_min_age = -1;
5870 n->verbose = $2;
5871 n->relation = NULL;
5872 n->va_cols = NIL;
5873 $$ = (Node *)n;
5875 | analyze_keyword opt_verbose qualified_name opt_name_list
5877 VacuumStmt *n = makeNode(VacuumStmt);
5878 n->vacuum = false;
5879 n->analyze = true;
5880 n->full = false;
5881 n->freeze_min_age = -1;
5882 n->verbose = $2;
5883 n->relation = $3;
5884 n->va_cols = $4;
5885 $$ = (Node *)n;
5889 analyze_keyword:
5890 ANALYZE {}
5891 | ANALYSE /* British */ {}
5894 opt_verbose:
5895 VERBOSE { $$ = TRUE; }
5896 | /*EMPTY*/ { $$ = FALSE; }
5899 opt_full: FULL { $$ = TRUE; }
5900 | /*EMPTY*/ { $$ = FALSE; }
5903 opt_freeze: FREEZE { $$ = TRUE; }
5904 | /*EMPTY*/ { $$ = FALSE; }
5907 opt_name_list:
5908 '(' name_list ')' { $$ = $2; }
5909 | /*EMPTY*/ { $$ = NIL; }
5913 /*****************************************************************************
5915 * QUERY:
5916 * EXPLAIN [ANALYZE] [VERBOSE] query
5918 *****************************************************************************/
5920 ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
5922 ExplainStmt *n = makeNode(ExplainStmt);
5923 n->analyze = $2;
5924 n->verbose = $3;
5925 n->query = $4;
5926 $$ = (Node *)n;
5930 ExplainableStmt:
5931 SelectStmt
5932 | InsertStmt
5933 | UpdateStmt
5934 | DeleteStmt
5935 | DeclareCursorStmt
5936 | CreateAsStmt
5937 | ExecuteStmt /* by default all are $$=$1 */
5940 opt_analyze:
5941 analyze_keyword { $$ = TRUE; }
5942 | /* EMPTY */ { $$ = FALSE; }
5945 /*****************************************************************************
5947 * QUERY:
5948 * PREPARE <plan_name> [(args, ...)] AS <query>
5950 *****************************************************************************/
5952 PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
5954 PrepareStmt *n = makeNode(PrepareStmt);
5955 n->name = $2;
5956 n->argtypes = $3;
5957 n->query = $5;
5958 $$ = (Node *) n;
5962 prep_type_clause: '(' type_list ')' { $$ = $2; }
5963 | /* EMPTY */ { $$ = NIL; }
5966 PreparableStmt:
5967 SelectStmt
5968 | InsertStmt
5969 | UpdateStmt
5970 | DeleteStmt /* by default all are $$=$1 */
5973 /*****************************************************************************
5975 * EXECUTE <plan_name> [(params, ...)]
5976 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
5978 *****************************************************************************/
5980 ExecuteStmt: EXECUTE name execute_param_clause
5982 ExecuteStmt *n = makeNode(ExecuteStmt);
5983 n->name = $2;
5984 n->params = $3;
5985 n->into = NULL;
5986 $$ = (Node *) n;
5988 | CREATE OptTemp TABLE create_as_target AS
5989 EXECUTE name execute_param_clause
5991 ExecuteStmt *n = makeNode(ExecuteStmt);
5992 n->name = $7;
5993 n->params = $8;
5994 $4->rel->istemp = $2;
5995 n->into = $4;
5996 if ($4->colNames)
5997 ereport(ERROR,
5998 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5999 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
6000 /* ... because it's not implemented, but it could be */
6001 $$ = (Node *) n;
6005 execute_param_clause: '(' expr_list ')' { $$ = $2; }
6006 | /* EMPTY */ { $$ = NIL; }
6009 /*****************************************************************************
6011 * QUERY:
6012 * DEALLOCATE [PREPARE] <plan_name>
6014 *****************************************************************************/
6016 DeallocateStmt: DEALLOCATE name
6018 DeallocateStmt *n = makeNode(DeallocateStmt);
6019 n->name = $2;
6020 $$ = (Node *) n;
6022 | DEALLOCATE PREPARE name
6024 DeallocateStmt *n = makeNode(DeallocateStmt);
6025 n->name = $3;
6026 $$ = (Node *) n;
6028 | DEALLOCATE ALL
6030 DeallocateStmt *n = makeNode(DeallocateStmt);
6031 n->name = NULL;
6032 $$ = (Node *) n;
6034 | DEALLOCATE PREPARE ALL
6036 DeallocateStmt *n = makeNode(DeallocateStmt);
6037 n->name = NULL;
6038 $$ = (Node *) n;
6042 /*****************************************************************************
6044 * QUERY:
6045 * INSERT STATEMENTS
6047 *****************************************************************************/
6049 InsertStmt:
6050 INSERT INTO qualified_name insert_rest returning_clause
6052 $4->relation = $3;
6053 $4->returningList = $5;
6054 $$ = (Node *) $4;
6058 insert_rest:
6059 SelectStmt
6061 $$ = makeNode(InsertStmt);
6062 $$->cols = NIL;
6063 $$->selectStmt = $1;
6065 | '(' insert_column_list ')' SelectStmt
6067 $$ = makeNode(InsertStmt);
6068 $$->cols = $2;
6069 $$->selectStmt = $4;
6071 | DEFAULT VALUES
6073 $$ = makeNode(InsertStmt);
6074 $$->cols = NIL;
6075 $$->selectStmt = NULL;
6079 insert_column_list:
6080 insert_column_item
6081 { $$ = list_make1($1); }
6082 | insert_column_list ',' insert_column_item
6083 { $$ = lappend($1, $3); }
6086 insert_column_item:
6087 ColId opt_indirection
6089 $$ = makeNode(ResTarget);
6090 $$->name = $1;
6091 $$->indirection = check_indirection($2);
6092 $$->val = NULL;
6093 $$->location = @1;
6097 returning_clause:
6098 RETURNING target_list { $$ = $2; }
6099 | /* EMPTY */ { $$ = NIL; }
6103 /*****************************************************************************
6105 * QUERY:
6106 * DELETE STATEMENTS
6108 *****************************************************************************/
6110 DeleteStmt: DELETE_P FROM relation_expr_opt_alias
6111 using_clause where_or_current_clause returning_clause
6113 DeleteStmt *n = makeNode(DeleteStmt);
6114 n->relation = $3;
6115 n->usingClause = $4;
6116 n->whereClause = $5;
6117 n->returningList = $6;
6118 $$ = (Node *)n;
6122 using_clause:
6123 USING from_list { $$ = $2; }
6124 | /*EMPTY*/ { $$ = NIL; }
6127 LockStmt: LOCK_P opt_table qualified_name_list opt_lock opt_nowait
6129 LockStmt *n = makeNode(LockStmt);
6131 n->relations = $3;
6132 n->mode = $4;
6133 n->nowait = $5;
6134 $$ = (Node *)n;
6138 opt_lock: IN_P lock_type MODE { $$ = $2; }
6139 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
6142 lock_type: ACCESS SHARE { $$ = AccessShareLock; }
6143 | ROW SHARE { $$ = RowShareLock; }
6144 | ROW EXCLUSIVE { $$ = RowExclusiveLock; }
6145 | SHARE UPDATE EXCLUSIVE { $$ = ShareUpdateExclusiveLock; }
6146 | SHARE { $$ = ShareLock; }
6147 | SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
6148 | EXCLUSIVE { $$ = ExclusiveLock; }
6149 | ACCESS EXCLUSIVE { $$ = AccessExclusiveLock; }
6152 opt_nowait: NOWAIT { $$ = TRUE; }
6153 | /*EMPTY*/ { $$ = FALSE; }
6157 /*****************************************************************************
6159 * QUERY:
6160 * UpdateStmt (UPDATE)
6162 *****************************************************************************/
6164 UpdateStmt: UPDATE relation_expr_opt_alias
6165 SET set_clause_list
6166 from_clause
6167 where_or_current_clause
6168 returning_clause
6170 UpdateStmt *n = makeNode(UpdateStmt);
6171 n->relation = $2;
6172 n->targetList = $4;
6173 n->fromClause = $5;
6174 n->whereClause = $6;
6175 n->returningList = $7;
6176 $$ = (Node *)n;
6180 set_clause_list:
6181 set_clause { $$ = $1; }
6182 | set_clause_list ',' set_clause { $$ = list_concat($1,$3); }
6185 set_clause:
6186 single_set_clause { $$ = list_make1($1); }
6187 | multiple_set_clause { $$ = $1; }
6190 single_set_clause:
6191 set_target '=' ctext_expr
6193 $$ = $1;
6194 $$->val = (Node *) $3;
6198 multiple_set_clause:
6199 '(' set_target_list ')' '=' ctext_row
6201 ListCell *col_cell;
6202 ListCell *val_cell;
6205 * Break the ctext_row apart, merge individual expressions
6206 * into the destination ResTargets. XXX this approach
6207 * cannot work for general row expressions as sources.
6209 if (list_length($2) != list_length($5))
6210 ereport(ERROR,
6211 (errcode(ERRCODE_SYNTAX_ERROR),
6212 errmsg("number of columns does not match number of values"),
6213 scanner_errposition(@1)));
6214 forboth(col_cell, $2, val_cell, $5)
6216 ResTarget *res_col = (ResTarget *) lfirst(col_cell);
6217 Node *res_val = (Node *) lfirst(val_cell);
6219 res_col->val = res_val;
6222 $$ = $2;
6226 set_target:
6227 ColId opt_indirection
6229 $$ = makeNode(ResTarget);
6230 $$->name = $1;
6231 $$->indirection = check_indirection($2);
6232 $$->val = NULL; /* upper production sets this */
6233 $$->location = @1;
6237 set_target_list:
6238 set_target { $$ = list_make1($1); }
6239 | set_target_list ',' set_target { $$ = lappend($1,$3); }
6243 /*****************************************************************************
6245 * QUERY:
6246 * CURSOR STATEMENTS
6248 *****************************************************************************/
6249 DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
6251 DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
6252 n->portalname = $2;
6253 /* currently we always set FAST_PLAN option */
6254 n->options = $3 | $5 | CURSOR_OPT_FAST_PLAN;
6255 n->query = $7;
6256 $$ = (Node *)n;
6260 cursor_options: /*EMPTY*/ { $$ = 0; }
6261 | cursor_options NO SCROLL { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
6262 | cursor_options SCROLL { $$ = $1 | CURSOR_OPT_SCROLL; }
6263 | cursor_options BINARY { $$ = $1 | CURSOR_OPT_BINARY; }
6264 | cursor_options INSENSITIVE { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
6267 opt_hold: /* EMPTY */ { $$ = 0; }
6268 | WITH HOLD { $$ = CURSOR_OPT_HOLD; }
6269 | WITHOUT HOLD { $$ = 0; }
6272 /*****************************************************************************
6274 * QUERY:
6275 * SELECT STATEMENTS
6277 *****************************************************************************/
6279 /* A complete SELECT statement looks like this.
6281 * The rule returns either a single SelectStmt node or a tree of them,
6282 * representing a set-operation tree.
6284 * There is an ambiguity when a sub-SELECT is within an a_expr and there
6285 * are excess parentheses: do the parentheses belong to the sub-SELECT or
6286 * to the surrounding a_expr? We don't really care, but yacc wants to know.
6287 * To resolve the ambiguity, we are careful to define the grammar so that
6288 * the decision is staved off as long as possible: as long as we can keep
6289 * absorbing parentheses into the sub-SELECT, we will do so, and only when
6290 * it's no longer possible to do that will we decide that parens belong to
6291 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
6292 * parentheses are treated as part of the sub-select. The necessity of doing
6293 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
6294 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
6295 * SELECT viewpoint when we see the UNION.
6297 * This approach is implemented by defining a nonterminal select_with_parens,
6298 * which represents a SELECT with at least one outer layer of parentheses,
6299 * and being careful to use select_with_parens, never '(' SelectStmt ')',
6300 * in the expression grammar. We will then have shift-reduce conflicts
6301 * which we can resolve in favor of always treating '(' <select> ')' as
6302 * a select_with_parens. To resolve the conflicts, the productions that
6303 * conflict with the select_with_parens productions are manually given
6304 * precedences lower than the precedence of ')', thereby ensuring that we
6305 * shift ')' (and then reduce to select_with_parens) rather than trying to
6306 * reduce the inner <select> nonterminal to something else. We use UMINUS
6307 * precedence for this, which is a fairly arbitrary choice.
6309 * To be able to define select_with_parens itself without ambiguity, we need
6310 * a nonterminal select_no_parens that represents a SELECT structure with no
6311 * outermost parentheses. This is a little bit tedious, but it works.
6313 * In non-expression contexts, we use SelectStmt which can represent a SELECT
6314 * with or without outer parentheses.
6317 SelectStmt: select_no_parens %prec UMINUS
6318 | select_with_parens %prec UMINUS
6321 select_with_parens:
6322 '(' select_no_parens ')' { $$ = $2; }
6323 | '(' select_with_parens ')' { $$ = $2; }
6327 * This rule parses the equivalent of the standard's <query expression>.
6328 * The duplicative productions are annoying, but hard to get rid of without
6329 * creating shift/reduce conflicts.
6331 * FOR UPDATE/SHARE may be before or after LIMIT/OFFSET.
6332 * In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
6333 * We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE/SHARE
6334 * 2002-08-28 bjm
6336 select_no_parens:
6337 simple_select { $$ = $1; }
6338 | select_clause sort_clause
6340 insertSelectOptions((SelectStmt *) $1, $2, NIL,
6341 NULL, NULL, NULL);
6342 $$ = $1;
6344 | select_clause opt_sort_clause for_locking_clause opt_select_limit
6346 insertSelectOptions((SelectStmt *) $1, $2, $3,
6347 list_nth($4, 0), list_nth($4, 1),
6348 NULL);
6349 $$ = $1;
6351 | select_clause opt_sort_clause select_limit opt_for_locking_clause
6353 insertSelectOptions((SelectStmt *) $1, $2, $4,
6354 list_nth($3, 0), list_nth($3, 1),
6355 NULL);
6356 $$ = $1;
6358 | with_clause simple_select
6360 insertSelectOptions((SelectStmt *) $2, NULL, NIL,
6361 NULL, NULL,
6362 $1);
6363 $$ = $2;
6365 | with_clause select_clause sort_clause
6367 insertSelectOptions((SelectStmt *) $2, $3, NIL,
6368 NULL, NULL,
6369 $1);
6370 $$ = $2;
6372 | with_clause select_clause opt_sort_clause for_locking_clause opt_select_limit
6374 insertSelectOptions((SelectStmt *) $2, $3, $4,
6375 list_nth($5, 0), list_nth($5, 1),
6376 $1);
6377 $$ = $2;
6379 | with_clause select_clause opt_sort_clause select_limit opt_for_locking_clause
6381 insertSelectOptions((SelectStmt *) $2, $3, $5,
6382 list_nth($4, 0), list_nth($4, 1),
6383 $1);
6384 $$ = $2;
6388 select_clause:
6389 simple_select { $$ = $1; }
6390 | select_with_parens { $$ = $1; }
6394 * This rule parses SELECT statements that can appear within set operations,
6395 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
6396 * the ordering of the set operations. Without '(' and ')' we want the
6397 * operations to be ordered per the precedence specs at the head of this file.
6399 * As with select_no_parens, simple_select cannot have outer parentheses,
6400 * but can have parenthesized subclauses.
6402 * Note that sort clauses cannot be included at this level --- SQL92 requires
6403 * SELECT foo UNION SELECT bar ORDER BY baz
6404 * to be parsed as
6405 * (SELECT foo UNION SELECT bar) ORDER BY baz
6406 * not
6407 * SELECT foo UNION (SELECT bar ORDER BY baz)
6408 * Likewise for WITH, FOR UPDATE and LIMIT. Therefore, those clauses are
6409 * described as part of the select_no_parens production, not simple_select.
6410 * This does not limit functionality, because you can reintroduce these
6411 * clauses inside parentheses.
6413 * NOTE: only the leftmost component SelectStmt should have INTO.
6414 * However, this is not checked by the grammar; parse analysis must check it.
6416 simple_select:
6417 SELECT opt_distinct target_list
6418 into_clause from_clause where_clause
6419 group_clause having_clause
6421 SelectStmt *n = makeNode(SelectStmt);
6422 n->distinctClause = $2;
6423 n->targetList = $3;
6424 n->intoClause = $4;
6425 n->fromClause = $5;
6426 n->whereClause = $6;
6427 n->groupClause = $7;
6428 n->havingClause = $8;
6429 $$ = (Node *)n;
6431 | values_clause { $$ = $1; }
6432 | select_clause UNION opt_all select_clause
6434 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
6436 | select_clause INTERSECT opt_all select_clause
6438 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
6440 | select_clause EXCEPT opt_all select_clause
6442 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
6447 * SQL standard WITH clause looks like:
6449 * WITH [ RECURSIVE ] <query name> [ (<column>,...) ]
6450 * AS (query) [ SEARCH or CYCLE clause ]
6452 * We don't currently support the SEARCH or CYCLE clause.
6454 with_clause:
6455 WITH cte_list
6457 $$ = makeNode(WithClause);
6458 $$->ctes = $2;
6459 $$->recursive = false;
6460 $$->location = @1;
6462 | WITH RECURSIVE cte_list
6464 $$ = makeNode(WithClause);
6465 $$->ctes = $3;
6466 $$->recursive = true;
6467 $$->location = @1;
6471 cte_list:
6472 common_table_expr { $$ = list_make1($1); }
6473 | cte_list ',' common_table_expr { $$ = lappend($1, $3); }
6476 common_table_expr: name opt_name_list AS select_with_parens
6478 CommonTableExpr *n = makeNode(CommonTableExpr);
6479 n->ctename = $1;
6480 n->aliascolnames = $2;
6481 n->ctequery = $4;
6482 n->location = @1;
6483 $$ = (Node *) n;
6487 into_clause:
6488 INTO OptTempTableName
6490 $$ = makeNode(IntoClause);
6491 $$->rel = $2;
6492 $$->colNames = NIL;
6493 $$->options = NIL;
6494 $$->onCommit = ONCOMMIT_NOOP;
6495 $$->tableSpaceName = NULL;
6497 | /*EMPTY*/
6498 { $$ = NULL; }
6502 * Redundancy here is needed to avoid shift/reduce conflicts,
6503 * since TEMP is not a reserved word. See also OptTemp.
6505 OptTempTableName:
6506 TEMPORARY opt_table qualified_name
6508 $$ = $3;
6509 $$->istemp = true;
6511 | TEMP opt_table qualified_name
6513 $$ = $3;
6514 $$->istemp = true;
6516 | LOCAL TEMPORARY opt_table qualified_name
6518 $$ = $4;
6519 $$->istemp = true;
6521 | LOCAL TEMP opt_table qualified_name
6523 $$ = $4;
6524 $$->istemp = true;
6526 | GLOBAL TEMPORARY opt_table qualified_name
6528 $$ = $4;
6529 $$->istemp = true;
6531 | GLOBAL TEMP opt_table qualified_name
6533 $$ = $4;
6534 $$->istemp = true;
6536 | TABLE qualified_name
6538 $$ = $2;
6539 $$->istemp = false;
6541 | qualified_name
6543 $$ = $1;
6544 $$->istemp = false;
6548 opt_table: TABLE {}
6549 | /*EMPTY*/ {}
6552 opt_all: ALL { $$ = TRUE; }
6553 | DISTINCT { $$ = FALSE; }
6554 | /*EMPTY*/ { $$ = FALSE; }
6557 /* We use (NIL) as a placeholder to indicate that all target expressions
6558 * should be placed in the DISTINCT list during parsetree analysis.
6560 opt_distinct:
6561 DISTINCT { $$ = list_make1(NIL); }
6562 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
6563 | ALL { $$ = NIL; }
6564 | /*EMPTY*/ { $$ = NIL; }
6567 opt_sort_clause:
6568 sort_clause { $$ = $1;}
6569 | /*EMPTY*/ { $$ = NIL; }
6572 sort_clause:
6573 ORDER BY sortby_list { $$ = $3; }
6576 sortby_list:
6577 sortby { $$ = list_make1($1); }
6578 | sortby_list ',' sortby { $$ = lappend($1, $3); }
6581 sortby: a_expr USING qual_all_Op opt_nulls_order
6583 $$ = makeNode(SortBy);
6584 $$->node = $1;
6585 $$->sortby_dir = SORTBY_USING;
6586 $$->sortby_nulls = $4;
6587 $$->useOp = $3;
6588 $$->location = @3;
6590 | a_expr opt_asc_desc opt_nulls_order
6592 $$ = makeNode(SortBy);
6593 $$->node = $1;
6594 $$->sortby_dir = $2;
6595 $$->sortby_nulls = $3;
6596 $$->useOp = NIL;
6597 $$->location = -1; /* no operator */
6602 select_limit:
6603 LIMIT select_limit_value OFFSET select_offset_value
6604 { $$ = list_make2($4, $2); }
6605 | OFFSET select_offset_value LIMIT select_limit_value
6606 { $$ = list_make2($2, $4); }
6607 | LIMIT select_limit_value
6608 { $$ = list_make2(NULL, $2); }
6609 | OFFSET select_offset_value
6610 { $$ = list_make2($2, NULL); }
6611 | LIMIT select_limit_value ',' select_offset_value
6613 /* Disabled because it was too confusing, bjm 2002-02-18 */
6614 ereport(ERROR,
6615 (errcode(ERRCODE_SYNTAX_ERROR),
6616 errmsg("LIMIT #,# syntax is not supported"),
6617 errhint("Use separate LIMIT and OFFSET clauses."),
6618 scanner_errposition(@1)));
6620 /* SQL:2008 syntax variants */
6621 | OFFSET select_offset_value2 row_or_rows
6622 { $$ = list_make2($2, NULL); }
6623 | FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
6624 { $$ = list_make2(NULL, $3); }
6625 | OFFSET select_offset_value2 row_or_rows FETCH first_or_next opt_select_fetch_first_value row_or_rows ONLY
6626 { $$ = list_make2($2, $6); }
6629 opt_select_limit:
6630 select_limit { $$ = $1; }
6631 | /* EMPTY */
6632 { $$ = list_make2(NULL,NULL); }
6635 select_limit_value:
6636 a_expr { $$ = $1; }
6637 | ALL
6639 /* LIMIT ALL is represented as a NULL constant */
6640 $$ = makeNullAConst(@1);
6645 * Allowing full expressions without parentheses causes various parsing
6646 * problems with the trailing ROW/ROWS key words. SQL only calls for
6647 * constants, so we allow the rest only with parentheses.
6649 opt_select_fetch_first_value:
6650 SignedIconst { $$ = makeIntConst($1, @1); }
6651 | '(' a_expr ')' { $$ = $2; }
6652 | /*EMPTY*/ { $$ = makeIntConst(1, -1); }
6655 select_offset_value:
6656 a_expr { $$ = $1; }
6660 * Again, the trailing ROW/ROWS in this case prevent the full expression
6661 * syntax. c_expr is the best we can do.
6663 select_offset_value2:
6664 c_expr { $$ = $1; }
6667 /* noise words */
6668 row_or_rows:
6669 ROW { $$ = 0; }
6670 | ROWS { $$ = 0; }
6672 /* noise words */
6673 first_or_next:
6674 FIRST_P { $$ = 0; }
6675 | NEXT { $$ = 0; }
6678 group_clause:
6679 GROUP_P BY expr_list { $$ = $3; }
6680 | /*EMPTY*/ { $$ = NIL; }
6683 having_clause:
6684 HAVING a_expr { $$ = $2; }
6685 | /*EMPTY*/ { $$ = NULL; }
6688 for_locking_clause:
6689 for_locking_items { $$ = $1; }
6690 | FOR READ ONLY { $$ = NIL; }
6693 opt_for_locking_clause:
6694 for_locking_clause { $$ = $1; }
6695 | /* EMPTY */ { $$ = NIL; }
6698 for_locking_items:
6699 for_locking_item { $$ = list_make1($1); }
6700 | for_locking_items for_locking_item { $$ = lappend($1, $2); }
6703 for_locking_item:
6704 FOR UPDATE locked_rels_list opt_nowait
6706 LockingClause *n = makeNode(LockingClause);
6707 n->lockedRels = $3;
6708 n->forUpdate = TRUE;
6709 n->noWait = $4;
6710 $$ = (Node *) n;
6712 | FOR SHARE locked_rels_list opt_nowait
6714 LockingClause *n = makeNode(LockingClause);
6715 n->lockedRels = $3;
6716 n->forUpdate = FALSE;
6717 n->noWait = $4;
6718 $$ = (Node *) n;
6722 locked_rels_list:
6723 OF qualified_name_list { $$ = $2; }
6724 | /* EMPTY */ { $$ = NIL; }
6728 values_clause:
6729 VALUES ctext_row
6731 SelectStmt *n = makeNode(SelectStmt);
6732 n->valuesLists = list_make1($2);
6733 $$ = (Node *) n;
6735 | values_clause ',' ctext_row
6737 SelectStmt *n = (SelectStmt *) $1;
6738 n->valuesLists = lappend(n->valuesLists, $3);
6739 $$ = (Node *) n;
6744 /*****************************************************************************
6746 * clauses common to all Optimizable Stmts:
6747 * from_clause - allow list of both JOIN expressions and table names
6748 * where_clause - qualifications for joins or restrictions
6750 *****************************************************************************/
6752 from_clause:
6753 FROM from_list { $$ = $2; }
6754 | /*EMPTY*/ { $$ = NIL; }
6757 from_list:
6758 table_ref { $$ = list_make1($1); }
6759 | from_list ',' table_ref { $$ = lappend($1, $3); }
6763 * table_ref is where an alias clause can be attached. Note we cannot make
6764 * alias_clause have an empty production because that causes parse conflicts
6765 * between table_ref := '(' joined_table ')' alias_clause
6766 * and joined_table := '(' joined_table ')'. So, we must have the
6767 * redundant-looking productions here instead.
6769 table_ref: relation_expr
6771 $$ = (Node *) $1;
6773 | relation_expr alias_clause
6775 $1->alias = $2;
6776 $$ = (Node *) $1;
6778 | func_table
6780 RangeFunction *n = makeNode(RangeFunction);
6781 n->funccallnode = $1;
6782 n->coldeflist = NIL;
6783 $$ = (Node *) n;
6785 | func_table alias_clause
6787 RangeFunction *n = makeNode(RangeFunction);
6788 n->funccallnode = $1;
6789 n->alias = $2;
6790 n->coldeflist = NIL;
6791 $$ = (Node *) n;
6793 | func_table AS '(' TableFuncElementList ')'
6795 RangeFunction *n = makeNode(RangeFunction);
6796 n->funccallnode = $1;
6797 n->coldeflist = $4;
6798 $$ = (Node *) n;
6800 | func_table AS ColId '(' TableFuncElementList ')'
6802 RangeFunction *n = makeNode(RangeFunction);
6803 Alias *a = makeNode(Alias);
6804 n->funccallnode = $1;
6805 a->aliasname = $3;
6806 n->alias = a;
6807 n->coldeflist = $5;
6808 $$ = (Node *) n;
6810 | func_table ColId '(' TableFuncElementList ')'
6812 RangeFunction *n = makeNode(RangeFunction);
6813 Alias *a = makeNode(Alias);
6814 n->funccallnode = $1;
6815 a->aliasname = $2;
6816 n->alias = a;
6817 n->coldeflist = $4;
6818 $$ = (Node *) n;
6820 | select_with_parens
6823 * The SQL spec does not permit a subselect
6824 * (<derived_table>) without an alias clause,
6825 * so we don't either. This avoids the problem
6826 * of needing to invent a unique refname for it.
6827 * That could be surmounted if there's sufficient
6828 * popular demand, but for now let's just implement
6829 * the spec and see if anyone complains.
6830 * However, it does seem like a good idea to emit
6831 * an error message that's better than "syntax error".
6833 if (IsA($1, SelectStmt) &&
6834 ((SelectStmt *) $1)->valuesLists)
6835 ereport(ERROR,
6836 (errcode(ERRCODE_SYNTAX_ERROR),
6837 errmsg("VALUES in FROM must have an alias"),
6838 errhint("For example, FROM (VALUES ...) [AS] foo."),
6839 scanner_errposition(@1)));
6840 else
6841 ereport(ERROR,
6842 (errcode(ERRCODE_SYNTAX_ERROR),
6843 errmsg("subquery in FROM must have an alias"),
6844 errhint("For example, FROM (SELECT ...) [AS] foo."),
6845 scanner_errposition(@1)));
6846 $$ = NULL;
6848 | select_with_parens alias_clause
6850 RangeSubselect *n = makeNode(RangeSubselect);
6851 n->subquery = $1;
6852 n->alias = $2;
6853 $$ = (Node *) n;
6855 | joined_table
6857 $$ = (Node *) $1;
6859 | '(' joined_table ')' alias_clause
6861 $2->alias = $4;
6862 $$ = (Node *) $2;
6868 * It may seem silly to separate joined_table from table_ref, but there is
6869 * method in SQL92's madness: if you don't do it this way you get reduce-
6870 * reduce conflicts, because it's not clear to the parser generator whether
6871 * to expect alias_clause after ')' or not. For the same reason we must
6872 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
6873 * join_type to expand to empty; if we try it, the parser generator can't
6874 * figure out when to reduce an empty join_type right after table_ref.
6876 * Note that a CROSS JOIN is the same as an unqualified
6877 * INNER JOIN, and an INNER JOIN/ON has the same shape
6878 * but a qualification expression to limit membership.
6879 * A NATURAL JOIN implicitly matches column names between
6880 * tables and the shape is determined by which columns are
6881 * in common. We'll collect columns during the later transformations.
6884 joined_table:
6885 '(' joined_table ')'
6887 $$ = $2;
6889 | table_ref CROSS JOIN table_ref
6891 /* CROSS JOIN is same as unqualified inner join */
6892 JoinExpr *n = makeNode(JoinExpr);
6893 n->jointype = JOIN_INNER;
6894 n->isNatural = FALSE;
6895 n->larg = $1;
6896 n->rarg = $4;
6897 n->using = NIL;
6898 n->quals = NULL;
6899 $$ = n;
6901 | table_ref join_type JOIN table_ref join_qual
6903 JoinExpr *n = makeNode(JoinExpr);
6904 n->jointype = $2;
6905 n->isNatural = FALSE;
6906 n->larg = $1;
6907 n->rarg = $4;
6908 if ($5 != NULL && IsA($5, List))
6909 n->using = (List *) $5; /* USING clause */
6910 else
6911 n->quals = $5; /* ON clause */
6912 $$ = n;
6914 | table_ref JOIN table_ref join_qual
6916 /* letting join_type reduce to empty doesn't work */
6917 JoinExpr *n = makeNode(JoinExpr);
6918 n->jointype = JOIN_INNER;
6919 n->isNatural = FALSE;
6920 n->larg = $1;
6921 n->rarg = $3;
6922 if ($4 != NULL && IsA($4, List))
6923 n->using = (List *) $4; /* USING clause */
6924 else
6925 n->quals = $4; /* ON clause */
6926 $$ = n;
6928 | table_ref NATURAL join_type JOIN table_ref
6930 JoinExpr *n = makeNode(JoinExpr);
6931 n->jointype = $3;
6932 n->isNatural = TRUE;
6933 n->larg = $1;
6934 n->rarg = $5;
6935 n->using = NIL; /* figure out which columns later... */
6936 n->quals = NULL; /* fill later */
6937 $$ = n;
6939 | table_ref NATURAL JOIN table_ref
6941 /* letting join_type reduce to empty doesn't work */
6942 JoinExpr *n = makeNode(JoinExpr);
6943 n->jointype = JOIN_INNER;
6944 n->isNatural = TRUE;
6945 n->larg = $1;
6946 n->rarg = $4;
6947 n->using = NIL; /* figure out which columns later... */
6948 n->quals = NULL; /* fill later */
6949 $$ = n;
6953 alias_clause:
6954 AS ColId '(' name_list ')'
6956 $$ = makeNode(Alias);
6957 $$->aliasname = $2;
6958 $$->colnames = $4;
6960 | AS ColId
6962 $$ = makeNode(Alias);
6963 $$->aliasname = $2;
6965 | ColId '(' name_list ')'
6967 $$ = makeNode(Alias);
6968 $$->aliasname = $1;
6969 $$->colnames = $3;
6971 | ColId
6973 $$ = makeNode(Alias);
6974 $$->aliasname = $1;
6978 join_type: FULL join_outer { $$ = JOIN_FULL; }
6979 | LEFT join_outer { $$ = JOIN_LEFT; }
6980 | RIGHT join_outer { $$ = JOIN_RIGHT; }
6981 | INNER_P { $$ = JOIN_INNER; }
6984 /* OUTER is just noise... */
6985 join_outer: OUTER_P { $$ = NULL; }
6986 | /*EMPTY*/ { $$ = NULL; }
6989 /* JOIN qualification clauses
6990 * Possibilities are:
6991 * USING ( column list ) allows only unqualified column names,
6992 * which must match between tables.
6993 * ON expr allows more general qualifications.
6995 * We return USING as a List node, while an ON-expr will not be a List.
6998 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
6999 | ON a_expr { $$ = $2; }
7003 relation_expr:
7004 qualified_name
7006 /* default inheritance */
7007 $$ = $1;
7008 $$->inhOpt = INH_DEFAULT;
7009 $$->alias = NULL;
7011 | qualified_name '*'
7013 /* inheritance query */
7014 $$ = $1;
7015 $$->inhOpt = INH_YES;
7016 $$->alias = NULL;
7018 | ONLY qualified_name
7020 /* no inheritance */
7021 $$ = $2;
7022 $$->inhOpt = INH_NO;
7023 $$->alias = NULL;
7025 | ONLY '(' qualified_name ')'
7027 /* no inheritance, SQL99-style syntax */
7028 $$ = $3;
7029 $$->inhOpt = INH_NO;
7030 $$->alias = NULL;
7036 * Given "UPDATE foo set set ...", we have to decide without looking any
7037 * further ahead whether the first "set" is an alias or the UPDATE's SET
7038 * keyword. Since "set" is allowed as a column name both interpretations
7039 * are feasible. We resolve the shift/reduce conflict by giving the first
7040 * relation_expr_opt_alias production a higher precedence than the SET token
7041 * has, causing the parser to prefer to reduce, in effect assuming that the
7042 * SET is not an alias.
7044 relation_expr_opt_alias: relation_expr %prec UMINUS
7046 $$ = $1;
7048 | relation_expr ColId
7050 Alias *alias = makeNode(Alias);
7051 alias->aliasname = $2;
7052 $1->alias = alias;
7053 $$ = $1;
7055 | relation_expr AS ColId
7057 Alias *alias = makeNode(Alias);
7058 alias->aliasname = $3;
7059 $1->alias = alias;
7060 $$ = $1;
7065 func_table: func_expr { $$ = $1; }
7069 where_clause:
7070 WHERE a_expr { $$ = $2; }
7071 | /*EMPTY*/ { $$ = NULL; }
7074 /* variant for UPDATE and DELETE */
7075 where_or_current_clause:
7076 WHERE a_expr { $$ = $2; }
7077 | WHERE CURRENT_P OF name
7079 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7080 /* cvarno is filled in by parse analysis */
7081 n->cursor_name = $4;
7082 n->cursor_param = 0;
7083 $$ = (Node *) n;
7085 | WHERE CURRENT_P OF PARAM
7087 CurrentOfExpr *n = makeNode(CurrentOfExpr);
7088 /* cvarno is filled in by parse analysis */
7089 n->cursor_name = NULL;
7090 n->cursor_param = $4;
7091 $$ = (Node *) n;
7093 | /*EMPTY*/ { $$ = NULL; }
7097 TableFuncElementList:
7098 TableFuncElement
7100 $$ = list_make1($1);
7102 | TableFuncElementList ',' TableFuncElement
7104 $$ = lappend($1, $3);
7108 TableFuncElement: ColId Typename
7110 ColumnDef *n = makeNode(ColumnDef);
7111 n->colname = $1;
7112 n->typename = $2;
7113 n->constraints = NIL;
7114 n->is_local = true;
7115 $$ = (Node *)n;
7119 /*****************************************************************************
7121 * Type syntax
7122 * SQL92 introduces a large amount of type-specific syntax.
7123 * Define individual clauses to handle these cases, and use
7124 * the generic case to handle regular type-extensible Postgres syntax.
7125 * - thomas 1997-10-10
7127 *****************************************************************************/
7129 Typename: SimpleTypename opt_array_bounds
7131 $$ = $1;
7132 $$->arrayBounds = $2;
7134 | SETOF SimpleTypename opt_array_bounds
7136 $$ = $2;
7137 $$->arrayBounds = $3;
7138 $$->setof = TRUE;
7140 /* SQL standard syntax, currently only one-dimensional */
7141 | SimpleTypename ARRAY '[' Iconst ']'
7143 $$ = $1;
7144 $$->arrayBounds = list_make1(makeInteger($4));
7146 | SETOF SimpleTypename ARRAY '[' Iconst ']'
7148 $$ = $2;
7149 $$->arrayBounds = list_make1(makeInteger($5));
7150 $$->setof = TRUE;
7152 | SimpleTypename ARRAY
7154 $$ = $1;
7155 $$->arrayBounds = list_make1(makeInteger(-1));
7157 | SETOF SimpleTypename ARRAY
7159 $$ = $2;
7160 $$->arrayBounds = list_make1(makeInteger(-1));
7161 $$->setof = TRUE;
7165 opt_array_bounds:
7166 opt_array_bounds '[' ']'
7167 { $$ = lappend($1, makeInteger(-1)); }
7168 | opt_array_bounds '[' Iconst ']'
7169 { $$ = lappend($1, makeInteger($3)); }
7170 | /*EMPTY*/
7171 { $$ = NIL; }
7174 SimpleTypename:
7175 GenericType { $$ = $1; }
7176 | Numeric { $$ = $1; }
7177 | Bit { $$ = $1; }
7178 | Character { $$ = $1; }
7179 | ConstDatetime { $$ = $1; }
7180 | ConstInterval opt_interval
7182 $$ = $1;
7183 $$->typmods = $2;
7185 | ConstInterval '(' Iconst ')' opt_interval
7187 $$ = $1;
7188 if ($5 != NIL)
7190 if (list_length($5) != 1)
7191 ereport(ERROR,
7192 (errcode(ERRCODE_SYNTAX_ERROR),
7193 errmsg("interval precision specified twice"),
7194 scanner_errposition(@1)));
7195 $$->typmods = lappend($5, makeIntConst($3, @3));
7197 else
7198 $$->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
7199 makeIntConst($3, @3));
7203 /* We have a separate ConstTypename to allow defaulting fixed-length
7204 * types such as CHAR() and BIT() to an unspecified length.
7205 * SQL9x requires that these default to a length of one, but this
7206 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
7207 * where there is an obvious better choice to make.
7208 * Note that ConstInterval is not included here since it must
7209 * be pushed up higher in the rules to accomodate the postfix
7210 * options (e.g. INTERVAL '1' YEAR). Likewise, we have to handle
7211 * the generic-type-name case in AExprConst to avoid premature
7212 * reduce/reduce conflicts against function names.
7214 ConstTypename:
7215 Numeric { $$ = $1; }
7216 | ConstBit { $$ = $1; }
7217 | ConstCharacter { $$ = $1; }
7218 | ConstDatetime { $$ = $1; }
7222 * GenericType covers all type names that don't have special syntax mandated
7223 * by the standard, including qualified names. We also allow type modifiers.
7224 * To avoid parsing conflicts against function invocations, the modifiers
7225 * have to be shown as expr_list here, but parse analysis will only accept
7226 * constants for them.
7228 GenericType:
7229 type_function_name opt_type_modifiers
7231 $$ = makeTypeName($1);
7232 $$->typmods = $2;
7233 $$->location = @1;
7235 | type_function_name attrs opt_type_modifiers
7237 $$ = makeTypeNameFromNameList(lcons(makeString($1), $2));
7238 $$->typmods = $3;
7239 $$->location = @1;
7243 opt_type_modifiers: '(' expr_list ')' { $$ = $2; }
7244 | /* EMPTY */ { $$ = NIL; }
7248 * SQL92 numeric data types
7250 Numeric: INT_P
7252 $$ = SystemTypeName("int4");
7253 $$->location = @1;
7255 | INTEGER
7257 $$ = SystemTypeName("int4");
7258 $$->location = @1;
7260 | SMALLINT
7262 $$ = SystemTypeName("int2");
7263 $$->location = @1;
7265 | BIGINT
7267 $$ = SystemTypeName("int8");
7268 $$->location = @1;
7270 | REAL
7272 $$ = SystemTypeName("float4");
7273 $$->location = @1;
7275 | FLOAT_P opt_float
7277 $$ = $2;
7278 $$->location = @1;
7280 | DOUBLE_P PRECISION
7282 $$ = SystemTypeName("float8");
7283 $$->location = @1;
7285 | DECIMAL_P opt_type_modifiers
7287 $$ = SystemTypeName("numeric");
7288 $$->typmods = $2;
7289 $$->location = @1;
7291 | DEC opt_type_modifiers
7293 $$ = SystemTypeName("numeric");
7294 $$->typmods = $2;
7295 $$->location = @1;
7297 | NUMERIC opt_type_modifiers
7299 $$ = SystemTypeName("numeric");
7300 $$->typmods = $2;
7301 $$->location = @1;
7303 | BOOLEAN_P
7305 $$ = SystemTypeName("bool");
7306 $$->location = @1;
7310 opt_float: '(' Iconst ')'
7313 * Check FLOAT() precision limits assuming IEEE floating
7314 * types - thomas 1997-09-18
7316 if ($2 < 1)
7317 ereport(ERROR,
7318 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7319 errmsg("precision for type float must be at least 1 bit"),
7320 scanner_errposition(@2)));
7321 else if ($2 <= 24)
7322 $$ = SystemTypeName("float4");
7323 else if ($2 <= 53)
7324 $$ = SystemTypeName("float8");
7325 else
7326 ereport(ERROR,
7327 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
7328 errmsg("precision for type float must be less than 54 bits"),
7329 scanner_errposition(@2)));
7331 | /*EMPTY*/
7333 $$ = SystemTypeName("float8");
7338 * SQL92 bit-field data types
7339 * The following implements BIT() and BIT VARYING().
7341 Bit: BitWithLength
7343 $$ = $1;
7345 | BitWithoutLength
7347 $$ = $1;
7351 /* ConstBit is like Bit except "BIT" defaults to unspecified length */
7352 /* See notes for ConstCharacter, which addresses same issue for "CHAR" */
7353 ConstBit: BitWithLength
7355 $$ = $1;
7357 | BitWithoutLength
7359 $$ = $1;
7360 $$->typmods = NIL;
7364 BitWithLength:
7365 BIT opt_varying '(' expr_list ')'
7367 char *typname;
7369 typname = $2 ? "varbit" : "bit";
7370 $$ = SystemTypeName(typname);
7371 $$->typmods = $4;
7372 $$->location = @1;
7376 BitWithoutLength:
7377 BIT opt_varying
7379 /* bit defaults to bit(1), varbit to no limit */
7380 if ($2)
7382 $$ = SystemTypeName("varbit");
7384 else
7386 $$ = SystemTypeName("bit");
7387 $$->typmods = list_make1(makeIntConst(1, -1));
7389 $$->location = @1;
7395 * SQL92 character data types
7396 * The following implements CHAR() and VARCHAR().
7398 Character: CharacterWithLength
7400 $$ = $1;
7402 | CharacterWithoutLength
7404 $$ = $1;
7408 ConstCharacter: CharacterWithLength
7410 $$ = $1;
7412 | CharacterWithoutLength
7414 /* Length was not specified so allow to be unrestricted.
7415 * This handles problems with fixed-length (bpchar) strings
7416 * which in column definitions must default to a length
7417 * of one, but should not be constrained if the length
7418 * was not specified.
7420 $$ = $1;
7421 $$->typmods = NIL;
7425 CharacterWithLength: character '(' Iconst ')' opt_charset
7427 if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
7429 char *type;
7431 type = palloc(strlen($1) + 1 + strlen($5) + 1);
7432 strcpy(type, $1);
7433 strcat(type, "_");
7434 strcat(type, $5);
7435 $1 = type;
7438 $$ = SystemTypeName($1);
7439 $$->typmods = list_make1(makeIntConst($3, @3));
7440 $$->location = @1;
7444 CharacterWithoutLength: character opt_charset
7446 if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
7448 char *type;
7450 type = palloc(strlen($1) + 1 + strlen($2) + 1);
7451 strcpy(type, $1);
7452 strcat(type, "_");
7453 strcat(type, $2);
7454 $1 = type;
7457 $$ = SystemTypeName($1);
7459 /* char defaults to char(1), varchar to no limit */
7460 if (strcmp($1, "bpchar") == 0)
7461 $$->typmods = list_make1(makeIntConst(1, -1));
7463 $$->location = @1;
7467 character: CHARACTER opt_varying
7468 { $$ = $2 ? "varchar": "bpchar"; }
7469 | CHAR_P opt_varying
7470 { $$ = $2 ? "varchar": "bpchar"; }
7471 | VARCHAR
7472 { $$ = "varchar"; }
7473 | NATIONAL CHARACTER opt_varying
7474 { $$ = $3 ? "varchar": "bpchar"; }
7475 | NATIONAL CHAR_P opt_varying
7476 { $$ = $3 ? "varchar": "bpchar"; }
7477 | NCHAR opt_varying
7478 { $$ = $2 ? "varchar": "bpchar"; }
7481 opt_varying:
7482 VARYING { $$ = TRUE; }
7483 | /*EMPTY*/ { $$ = FALSE; }
7486 opt_charset:
7487 CHARACTER SET ColId { $$ = $3; }
7488 | /*EMPTY*/ { $$ = NULL; }
7492 * SQL92 date/time types
7494 ConstDatetime:
7495 TIMESTAMP '(' Iconst ')' opt_timezone
7497 if ($5)
7498 $$ = SystemTypeName("timestamptz");
7499 else
7500 $$ = SystemTypeName("timestamp");
7501 $$->typmods = list_make1(makeIntConst($3, @3));
7502 $$->location = @1;
7504 | TIMESTAMP opt_timezone
7506 if ($2)
7507 $$ = SystemTypeName("timestamptz");
7508 else
7509 $$ = SystemTypeName("timestamp");
7510 $$->location = @1;
7512 | TIME '(' Iconst ')' opt_timezone
7514 if ($5)
7515 $$ = SystemTypeName("timetz");
7516 else
7517 $$ = SystemTypeName("time");
7518 $$->typmods = list_make1(makeIntConst($3, @3));
7519 $$->location = @1;
7521 | TIME opt_timezone
7523 if ($2)
7524 $$ = SystemTypeName("timetz");
7525 else
7526 $$ = SystemTypeName("time");
7527 $$->location = @1;
7531 ConstInterval:
7532 INTERVAL
7534 $$ = SystemTypeName("interval");
7535 $$->location = @1;
7539 opt_timezone:
7540 WITH_TIME ZONE { $$ = TRUE; }
7541 | WITHOUT TIME ZONE { $$ = FALSE; }
7542 | /*EMPTY*/ { $$ = FALSE; }
7545 opt_interval:
7546 YEAR_P
7547 { $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR), @1)); }
7548 | MONTH_P
7549 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MONTH), @1)); }
7550 | DAY_P
7551 { $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY), @1)); }
7552 | HOUR_P
7553 { $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR), @1)); }
7554 | MINUTE_P
7555 { $$ = list_make1(makeIntConst(INTERVAL_MASK(MINUTE), @1)); }
7556 | interval_second
7557 { $$ = $1; }
7558 | YEAR_P TO MONTH_P
7560 $$ = list_make1(makeIntConst(INTERVAL_MASK(YEAR) |
7561 INTERVAL_MASK(MONTH), @1));
7563 | DAY_P TO HOUR_P
7565 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7566 INTERVAL_MASK(HOUR), @1));
7568 | DAY_P TO MINUTE_P
7570 $$ = list_make1(makeIntConst(INTERVAL_MASK(DAY) |
7571 INTERVAL_MASK(HOUR) |
7572 INTERVAL_MASK(MINUTE), @1));
7574 | DAY_P TO interval_second
7576 $$ = $3;
7577 linitial($$) = makeIntConst(INTERVAL_MASK(DAY) |
7578 INTERVAL_MASK(HOUR) |
7579 INTERVAL_MASK(MINUTE) |
7580 INTERVAL_MASK(SECOND), @1);
7582 | HOUR_P TO MINUTE_P
7584 $$ = list_make1(makeIntConst(INTERVAL_MASK(HOUR) |
7585 INTERVAL_MASK(MINUTE), @1));
7587 | HOUR_P TO interval_second
7589 $$ = $3;
7590 linitial($$) = makeIntConst(INTERVAL_MASK(HOUR) |
7591 INTERVAL_MASK(MINUTE) |
7592 INTERVAL_MASK(SECOND), @1);
7594 | MINUTE_P TO interval_second
7596 $$ = $3;
7597 linitial($$) = makeIntConst(INTERVAL_MASK(MINUTE) |
7598 INTERVAL_MASK(SECOND), @1);
7600 | /*EMPTY*/
7601 { $$ = NIL; }
7604 interval_second:
7605 SECOND_P
7607 $$ = list_make1(makeIntConst(INTERVAL_MASK(SECOND), @1));
7609 | SECOND_P '(' Iconst ')'
7611 $$ = list_make2(makeIntConst(INTERVAL_MASK(SECOND), @1),
7612 makeIntConst($3, @3));
7617 /*****************************************************************************
7619 * expression grammar
7621 *****************************************************************************/
7624 * General expressions
7625 * This is the heart of the expression syntax.
7627 * We have two expression types: a_expr is the unrestricted kind, and
7628 * b_expr is a subset that must be used in some places to avoid shift/reduce
7629 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
7630 * because that use of AND conflicts with AND as a boolean operator. So,
7631 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
7633 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
7634 * always be used by surrounding it with parens.
7636 * c_expr is all the productions that are common to a_expr and b_expr;
7637 * it's factored out just to eliminate redundant coding.
7639 a_expr: c_expr { $$ = $1; }
7640 | a_expr TYPECAST Typename
7641 { $$ = makeTypeCast($1, $3, @2); }
7642 | a_expr AT TIME ZONE a_expr
7644 FuncCall *n = makeNode(FuncCall);
7645 n->funcname = SystemFuncName("timezone");
7646 n->args = list_make2($5, $1);
7647 n->agg_star = FALSE;
7648 n->agg_distinct = FALSE;
7649 n->func_variadic = FALSE;
7650 n->location = @2;
7651 $$ = (Node *) n;
7654 * These operators must be called out explicitly in order to make use
7655 * of yacc/bison's automatic operator-precedence handling. All other
7656 * operator names are handled by the generic productions using "Op",
7657 * below; and all those operators will have the same precedence.
7659 * If you add more explicitly-known operators, be sure to add them
7660 * also to b_expr and to the MathOp list above.
7662 | '+' a_expr %prec UMINUS
7663 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
7664 | '-' a_expr %prec UMINUS
7665 { $$ = doNegate($2, @1); }
7666 | a_expr '+' a_expr
7667 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
7668 | a_expr '-' a_expr
7669 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
7670 | a_expr '*' a_expr
7671 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
7672 | a_expr '/' a_expr
7673 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
7674 | a_expr '%' a_expr
7675 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
7676 | a_expr '^' a_expr
7677 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
7678 | a_expr '<' a_expr
7679 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
7680 | a_expr '>' a_expr
7681 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
7682 | a_expr '=' a_expr
7683 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
7685 | a_expr qual_Op a_expr %prec Op
7686 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
7687 | qual_Op a_expr %prec Op
7688 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
7689 | a_expr qual_Op %prec POSTFIXOP
7690 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
7692 | a_expr AND a_expr
7693 { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3, @2); }
7694 | a_expr OR a_expr
7695 { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3, @2); }
7696 | NOT a_expr
7697 { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2, @1); }
7699 | a_expr LIKE a_expr
7700 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3, @2); }
7701 | a_expr LIKE a_expr ESCAPE a_expr
7703 FuncCall *n = makeNode(FuncCall);
7704 n->funcname = SystemFuncName("like_escape");
7705 n->args = list_make2($3, $5);
7706 n->agg_star = FALSE;
7707 n->agg_distinct = FALSE;
7708 n->func_variadic = FALSE;
7709 n->location = @4;
7710 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n, @2);
7712 | a_expr NOT LIKE a_expr
7713 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4, @2); }
7714 | a_expr NOT LIKE a_expr ESCAPE a_expr
7716 FuncCall *n = makeNode(FuncCall);
7717 n->funcname = SystemFuncName("like_escape");
7718 n->args = list_make2($4, $6);
7719 n->agg_star = FALSE;
7720 n->agg_distinct = FALSE;
7721 n->func_variadic = FALSE;
7722 n->location = @5;
7723 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n, @2);
7725 | a_expr ILIKE a_expr
7726 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3, @2); }
7727 | a_expr ILIKE a_expr ESCAPE a_expr
7729 FuncCall *n = makeNode(FuncCall);
7730 n->funcname = SystemFuncName("like_escape");
7731 n->args = list_make2($3, $5);
7732 n->agg_star = FALSE;
7733 n->agg_distinct = FALSE;
7734 n->func_variadic = FALSE;
7735 n->location = @4;
7736 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n, @2);
7738 | a_expr NOT ILIKE a_expr
7739 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4, @2); }
7740 | a_expr NOT ILIKE a_expr ESCAPE a_expr
7742 FuncCall *n = makeNode(FuncCall);
7743 n->funcname = SystemFuncName("like_escape");
7744 n->args = list_make2($4, $6);
7745 n->agg_star = FALSE;
7746 n->agg_distinct = FALSE;
7747 n->func_variadic = FALSE;
7748 n->location = @5;
7749 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n, @2);
7752 | a_expr SIMILAR TO a_expr %prec SIMILAR
7754 FuncCall *n = makeNode(FuncCall);
7755 n->funcname = SystemFuncName("similar_escape");
7756 n->args = list_make2($4, makeNullAConst(-1));
7757 n->agg_star = FALSE;
7758 n->agg_distinct = FALSE;
7759 n->func_variadic = FALSE;
7760 n->location = @2;
7761 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7763 | a_expr SIMILAR TO a_expr ESCAPE a_expr
7765 FuncCall *n = makeNode(FuncCall);
7766 n->funcname = SystemFuncName("similar_escape");
7767 n->args = list_make2($4, $6);
7768 n->agg_star = FALSE;
7769 n->agg_distinct = FALSE;
7770 n->func_variadic = FALSE;
7771 n->location = @5;
7772 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n, @2);
7774 | a_expr NOT SIMILAR TO a_expr %prec SIMILAR
7776 FuncCall *n = makeNode(FuncCall);
7777 n->funcname = SystemFuncName("similar_escape");
7778 n->args = list_make2($5, makeNullAConst(-1));
7779 n->agg_star = FALSE;
7780 n->agg_distinct = FALSE;
7781 n->func_variadic = FALSE;
7782 n->location = @5;
7783 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7785 | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
7787 FuncCall *n = makeNode(FuncCall);
7788 n->funcname = SystemFuncName("similar_escape");
7789 n->args = list_make2($5, $7);
7790 n->agg_star = FALSE;
7791 n->agg_distinct = FALSE;
7792 n->func_variadic = FALSE;
7793 n->location = @6;
7794 $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n, @2);
7797 /* NullTest clause
7798 * Define SQL92-style Null test clause.
7799 * Allow two forms described in the standard:
7800 * a IS NULL
7801 * a IS NOT NULL
7802 * Allow two SQL extensions
7803 * a ISNULL
7804 * a NOTNULL
7806 | a_expr IS NULL_P
7808 NullTest *n = makeNode(NullTest);
7809 n->arg = (Expr *) $1;
7810 n->nulltesttype = IS_NULL;
7811 $$ = (Node *)n;
7813 | a_expr ISNULL
7815 NullTest *n = makeNode(NullTest);
7816 n->arg = (Expr *) $1;
7817 n->nulltesttype = IS_NULL;
7818 $$ = (Node *)n;
7820 | a_expr IS NOT NULL_P
7822 NullTest *n = makeNode(NullTest);
7823 n->arg = (Expr *) $1;
7824 n->nulltesttype = IS_NOT_NULL;
7825 $$ = (Node *)n;
7827 | a_expr NOTNULL
7829 NullTest *n = makeNode(NullTest);
7830 n->arg = (Expr *) $1;
7831 n->nulltesttype = IS_NOT_NULL;
7832 $$ = (Node *)n;
7834 | row OVERLAPS row
7836 $$ = (Node *)makeOverlaps($1, $3, @2);
7838 | a_expr IS TRUE_P
7840 BooleanTest *b = makeNode(BooleanTest);
7841 b->arg = (Expr *) $1;
7842 b->booltesttype = IS_TRUE;
7843 $$ = (Node *)b;
7845 | a_expr IS NOT TRUE_P
7847 BooleanTest *b = makeNode(BooleanTest);
7848 b->arg = (Expr *) $1;
7849 b->booltesttype = IS_NOT_TRUE;
7850 $$ = (Node *)b;
7852 | a_expr IS FALSE_P
7854 BooleanTest *b = makeNode(BooleanTest);
7855 b->arg = (Expr *) $1;
7856 b->booltesttype = IS_FALSE;
7857 $$ = (Node *)b;
7859 | a_expr IS NOT FALSE_P
7861 BooleanTest *b = makeNode(BooleanTest);
7862 b->arg = (Expr *) $1;
7863 b->booltesttype = IS_NOT_FALSE;
7864 $$ = (Node *)b;
7866 | a_expr IS UNKNOWN
7868 BooleanTest *b = makeNode(BooleanTest);
7869 b->arg = (Expr *) $1;
7870 b->booltesttype = IS_UNKNOWN;
7871 $$ = (Node *)b;
7873 | a_expr IS NOT UNKNOWN
7875 BooleanTest *b = makeNode(BooleanTest);
7876 b->arg = (Expr *) $1;
7877 b->booltesttype = IS_NOT_UNKNOWN;
7878 $$ = (Node *)b;
7880 | a_expr IS DISTINCT FROM a_expr %prec IS
7882 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
7884 | a_expr IS NOT DISTINCT FROM a_expr %prec IS
7886 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
7887 (Node *) makeSimpleA_Expr(AEXPR_DISTINCT,
7888 "=", $1, $6, @2),
7889 @2);
7892 | a_expr IS OF '(' type_list ')' %prec IS
7894 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
7896 | a_expr IS NOT OF '(' type_list ')' %prec IS
7898 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
7900 | a_expr BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7902 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7903 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7904 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7905 @2);
7907 | a_expr NOT BETWEEN opt_asymmetric b_expr AND b_expr %prec BETWEEN
7909 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7910 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7911 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7912 @2);
7914 | a_expr BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7916 $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
7917 (Node *) makeA_Expr(AEXPR_AND, NIL,
7918 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $4, @2),
7919 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $6, @2),
7920 @2),
7921 (Node *) makeA_Expr(AEXPR_AND, NIL,
7922 (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $6, @2),
7923 (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $4, @2),
7924 @2),
7925 @2);
7927 | a_expr NOT BETWEEN SYMMETRIC b_expr AND b_expr %prec BETWEEN
7929 $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
7930 (Node *) makeA_Expr(AEXPR_OR, NIL,
7931 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $5, @2),
7932 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $7, @2),
7933 @2),
7934 (Node *) makeA_Expr(AEXPR_OR, NIL,
7935 (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $7, @2),
7936 (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $5, @2),
7937 @2),
7938 @2);
7940 | a_expr IN_P in_expr
7942 /* in_expr returns a SubLink or a list of a_exprs */
7943 if (IsA($3, SubLink))
7945 /* generate foo = ANY (subquery) */
7946 SubLink *n = (SubLink *) $3;
7947 n->subLinkType = ANY_SUBLINK;
7948 n->testexpr = $1;
7949 n->operName = list_make1(makeString("="));
7950 n->location = @2;
7951 $$ = (Node *)n;
7953 else
7955 /* generate scalar IN expression */
7956 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "=", $1, $3, @2);
7959 | a_expr NOT IN_P in_expr
7961 /* in_expr returns a SubLink or a list of a_exprs */
7962 if (IsA($4, SubLink))
7964 /* generate NOT (foo = ANY (subquery)) */
7965 /* Make an = ANY node */
7966 SubLink *n = (SubLink *) $4;
7967 n->subLinkType = ANY_SUBLINK;
7968 n->testexpr = $1;
7969 n->operName = list_make1(makeString("="));
7970 n->location = @3;
7971 /* Stick a NOT on top */
7972 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n, @2);
7974 else
7976 /* generate scalar NOT IN expression */
7977 $$ = (Node *) makeSimpleA_Expr(AEXPR_IN, "<>", $1, $4, @2);
7980 | a_expr subquery_Op sub_type select_with_parens %prec Op
7982 SubLink *n = makeNode(SubLink);
7983 n->subLinkType = $3;
7984 n->testexpr = $1;
7985 n->operName = $2;
7986 n->subselect = $4;
7987 n->location = @2;
7988 $$ = (Node *)n;
7990 | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
7992 if ($3 == ANY_SUBLINK)
7993 $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5, @2);
7994 else
7995 $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5, @2);
7997 | UNIQUE select_with_parens
7999 /* Not sure how to get rid of the parentheses
8000 * but there are lots of shift/reduce errors without them.
8002 * Should be able to implement this by plopping the entire
8003 * select into a node, then transforming the target expressions
8004 * from whatever they are into count(*), and testing the
8005 * entire result equal to one.
8006 * But, will probably implement a separate node in the executor.
8008 ereport(ERROR,
8009 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8010 errmsg("UNIQUE predicate is not yet implemented"),
8011 scanner_errposition(@1)));
8013 | a_expr IS DOCUMENT_P %prec IS
8015 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8016 list_make1($1), @2);
8018 | a_expr IS NOT DOCUMENT_P %prec IS
8020 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8021 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8022 list_make1($1), @2),
8023 @2);
8028 * Restricted expressions
8030 * b_expr is a subset of the complete expression syntax defined by a_expr.
8032 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
8033 * cause trouble in the places where b_expr is used. For simplicity, we
8034 * just eliminate all the boolean-keyword-operator productions from b_expr.
8036 b_expr: c_expr
8037 { $$ = $1; }
8038 | b_expr TYPECAST Typename
8039 { $$ = makeTypeCast($1, $3, @2); }
8040 | '+' b_expr %prec UMINUS
8041 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2, @1); }
8042 | '-' b_expr %prec UMINUS
8043 { $$ = doNegate($2, @1); }
8044 | b_expr '+' b_expr
8045 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3, @2); }
8046 | b_expr '-' b_expr
8047 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3, @2); }
8048 | b_expr '*' b_expr
8049 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3, @2); }
8050 | b_expr '/' b_expr
8051 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3, @2); }
8052 | b_expr '%' b_expr
8053 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3, @2); }
8054 | b_expr '^' b_expr
8055 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3, @2); }
8056 | b_expr '<' b_expr
8057 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3, @2); }
8058 | b_expr '>' b_expr
8059 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3, @2); }
8060 | b_expr '=' b_expr
8061 { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3, @2); }
8062 | b_expr qual_Op b_expr %prec Op
8063 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3, @2); }
8064 | qual_Op b_expr %prec Op
8065 { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2, @1); }
8066 | b_expr qual_Op %prec POSTFIXOP
8067 { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL, @2); }
8068 | b_expr IS DISTINCT FROM b_expr %prec IS
8070 $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5, @2);
8072 | b_expr IS NOT DISTINCT FROM b_expr %prec IS
8074 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL,
8075 NULL, (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $6, @2), @2);
8077 | b_expr IS OF '(' type_list ')' %prec IS
8079 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5, @2);
8081 | b_expr IS NOT OF '(' type_list ')' %prec IS
8083 $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "<>", $1, (Node *) $6, @2);
8085 | b_expr IS DOCUMENT_P %prec IS
8087 $$ = makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8088 list_make1($1), @2);
8090 | b_expr IS NOT DOCUMENT_P %prec IS
8092 $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL,
8093 makeXmlExpr(IS_DOCUMENT, NULL, NIL,
8094 list_make1($1), @2),
8095 @2);
8100 * Productions that can be used in both a_expr and b_expr.
8102 * Note: productions that refer recursively to a_expr or b_expr mostly
8103 * cannot appear here. However, it's OK to refer to a_exprs that occur
8104 * inside parentheses, such as function arguments; that cannot introduce
8105 * ambiguity to the b_expr syntax.
8107 c_expr: columnref { $$ = $1; }
8108 | AexprConst { $$ = $1; }
8109 | PARAM opt_indirection
8111 ParamRef *p = makeNode(ParamRef);
8112 p->number = $1;
8113 p->location = @1;
8114 if ($2)
8116 A_Indirection *n = makeNode(A_Indirection);
8117 n->arg = (Node *) p;
8118 n->indirection = check_indirection($2);
8119 $$ = (Node *) n;
8121 else
8122 $$ = (Node *) p;
8124 | '(' a_expr ')' opt_indirection
8126 if ($4)
8128 A_Indirection *n = makeNode(A_Indirection);
8129 n->arg = $2;
8130 n->indirection = check_indirection($4);
8131 $$ = (Node *)n;
8133 else
8134 $$ = $2;
8136 | case_expr
8137 { $$ = $1; }
8138 | func_expr
8139 { $$ = $1; }
8140 | select_with_parens %prec UMINUS
8142 SubLink *n = makeNode(SubLink);
8143 n->subLinkType = EXPR_SUBLINK;
8144 n->testexpr = NULL;
8145 n->operName = NIL;
8146 n->subselect = $1;
8147 n->location = @1;
8148 $$ = (Node *)n;
8150 | EXISTS select_with_parens
8152 SubLink *n = makeNode(SubLink);
8153 n->subLinkType = EXISTS_SUBLINK;
8154 n->testexpr = NULL;
8155 n->operName = NIL;
8156 n->subselect = $2;
8157 n->location = @1;
8158 $$ = (Node *)n;
8160 | ARRAY select_with_parens
8162 SubLink *n = makeNode(SubLink);
8163 n->subLinkType = ARRAY_SUBLINK;
8164 n->testexpr = NULL;
8165 n->operName = NIL;
8166 n->subselect = $2;
8167 n->location = @1;
8168 $$ = (Node *)n;
8170 | ARRAY array_expr
8172 A_ArrayExpr *n = (A_ArrayExpr *) $2;
8173 Assert(IsA(n, A_ArrayExpr));
8174 /* point outermost A_ArrayExpr to the ARRAY keyword */
8175 n->location = @1;
8176 $$ = (Node *)n;
8178 | row
8180 RowExpr *r = makeNode(RowExpr);
8181 r->args = $1;
8182 r->row_typeid = InvalidOid; /* not analyzed yet */
8183 r->location = @1;
8184 $$ = (Node *)r;
8189 * func_expr is split out from c_expr just so that we have a classification
8190 * for "everything that is a function call or looks like one". This isn't
8191 * very important, but it saves us having to document which variants are
8192 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
8193 * (Note that many of the special SQL functions wouldn't actually make any
8194 * sense as functional index entries, but we ignore that consideration here.)
8196 func_expr: func_name '(' ')'
8198 FuncCall *n = makeNode(FuncCall);
8199 n->funcname = $1;
8200 n->args = NIL;
8201 n->agg_star = FALSE;
8202 n->agg_distinct = FALSE;
8203 n->func_variadic = FALSE;
8204 n->location = @1;
8205 $$ = (Node *)n;
8207 | func_name '(' expr_list ')'
8209 FuncCall *n = makeNode(FuncCall);
8210 n->funcname = $1;
8211 n->args = $3;
8212 n->agg_star = FALSE;
8213 n->agg_distinct = FALSE;
8214 n->func_variadic = FALSE;
8215 n->location = @1;
8216 $$ = (Node *)n;
8218 | func_name '(' VARIADIC a_expr ')'
8220 FuncCall *n = makeNode(FuncCall);
8221 n->funcname = $1;
8222 n->args = list_make1($4);
8223 n->agg_star = FALSE;
8224 n->agg_distinct = FALSE;
8225 n->func_variadic = TRUE;
8226 n->location = @1;
8227 $$ = (Node *)n;
8229 | func_name '(' expr_list ',' VARIADIC a_expr ')'
8231 FuncCall *n = makeNode(FuncCall);
8232 n->funcname = $1;
8233 n->args = lappend($3, $6);
8234 n->agg_star = FALSE;
8235 n->agg_distinct = FALSE;
8236 n->func_variadic = TRUE;
8237 n->location = @1;
8238 $$ = (Node *)n;
8240 | func_name '(' ALL expr_list ')'
8242 FuncCall *n = makeNode(FuncCall);
8243 n->funcname = $1;
8244 n->args = $4;
8245 n->agg_star = FALSE;
8246 n->agg_distinct = FALSE;
8247 /* Ideally we'd mark the FuncCall node to indicate
8248 * "must be an aggregate", but there's no provision
8249 * for that in FuncCall at the moment.
8251 n->func_variadic = FALSE;
8252 n->location = @1;
8253 $$ = (Node *)n;
8255 | func_name '(' DISTINCT expr_list ')'
8257 FuncCall *n = makeNode(FuncCall);
8258 n->funcname = $1;
8259 n->args = $4;
8260 n->agg_star = FALSE;
8261 n->agg_distinct = TRUE;
8262 n->func_variadic = FALSE;
8263 n->location = @1;
8264 $$ = (Node *)n;
8266 | func_name '(' '*' ')'
8269 * We consider AGGREGATE(*) to invoke a parameterless
8270 * aggregate. This does the right thing for COUNT(*),
8271 * and there are no other aggregates in SQL92 that accept
8272 * '*' as parameter.
8274 * The FuncCall node is also marked agg_star = true,
8275 * so that later processing can detect what the argument
8276 * really was.
8278 FuncCall *n = makeNode(FuncCall);
8279 n->funcname = $1;
8280 n->args = NIL;
8281 n->agg_star = TRUE;
8282 n->agg_distinct = FALSE;
8283 n->func_variadic = FALSE;
8284 n->location = @1;
8285 $$ = (Node *)n;
8287 | CURRENT_DATE
8290 * Translate as "'now'::text::date".
8292 * We cannot use "'now'::date" because coerce_type() will
8293 * immediately reduce that to a constant representing
8294 * today's date. We need to delay the conversion until
8295 * runtime, else the wrong things will happen when
8296 * CURRENT_DATE is used in a column default value or rule.
8298 * This could be simplified if we had a way to generate
8299 * an expression tree representing runtime application
8300 * of type-input conversion functions. (As of PG 7.3
8301 * that is actually possible, but not clear that we want
8302 * to rely on it.)
8304 Node *n;
8305 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8306 $$ = makeTypeCast(n, SystemTypeName("date"), -1);
8308 | CURRENT_TIME
8311 * Translate as "'now'::text::timetz".
8312 * See comments for CURRENT_DATE.
8314 Node *n;
8315 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8316 $$ = makeTypeCast(n, SystemTypeName("timetz"), -1);
8318 | CURRENT_TIME '(' Iconst ')'
8321 * Translate as "'now'::text::timetz(n)".
8322 * See comments for CURRENT_DATE.
8324 Node *n;
8325 TypeName *d;
8326 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8327 d = SystemTypeName("timetz");
8328 d->typmods = list_make1(makeIntConst($3, @3));
8329 $$ = makeTypeCast(n, d, -1);
8331 | CURRENT_TIMESTAMP
8334 * Translate as "now()", since we have a function that
8335 * does exactly what is needed.
8337 FuncCall *n = makeNode(FuncCall);
8338 n->funcname = SystemFuncName("now");
8339 n->args = NIL;
8340 n->agg_star = FALSE;
8341 n->agg_distinct = FALSE;
8342 n->func_variadic = FALSE;
8343 n->location = @1;
8344 $$ = (Node *)n;
8346 | CURRENT_TIMESTAMP '(' Iconst ')'
8349 * Translate as "'now'::text::timestamptz(n)".
8350 * See comments for CURRENT_DATE.
8352 Node *n;
8353 TypeName *d;
8354 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8355 d = SystemTypeName("timestamptz");
8356 d->typmods = list_make1(makeIntConst($3, @3));
8357 $$ = makeTypeCast(n, d, -1);
8359 | LOCALTIME
8362 * Translate as "'now'::text::time".
8363 * See comments for CURRENT_DATE.
8365 Node *n;
8366 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8367 $$ = makeTypeCast((Node *)n, SystemTypeName("time"), -1);
8369 | LOCALTIME '(' Iconst ')'
8372 * Translate as "'now'::text::time(n)".
8373 * See comments for CURRENT_DATE.
8375 Node *n;
8376 TypeName *d;
8377 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8378 d = SystemTypeName("time");
8379 d->typmods = list_make1(makeIntConst($3, @3));
8380 $$ = makeTypeCast((Node *)n, d, -1);
8382 | LOCALTIMESTAMP
8385 * Translate as "'now'::text::timestamp".
8386 * See comments for CURRENT_DATE.
8388 Node *n;
8389 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8390 $$ = makeTypeCast(n, SystemTypeName("timestamp"), -1);
8392 | LOCALTIMESTAMP '(' Iconst ')'
8395 * Translate as "'now'::text::timestamp(n)".
8396 * See comments for CURRENT_DATE.
8398 Node *n;
8399 TypeName *d;
8400 n = makeStringConstCast("now", @1, SystemTypeName("text"));
8401 d = SystemTypeName("timestamp");
8402 d->typmods = list_make1(makeIntConst($3, @3));
8403 $$ = makeTypeCast(n, d, -1);
8405 | CURRENT_ROLE
8407 FuncCall *n = makeNode(FuncCall);
8408 n->funcname = SystemFuncName("current_user");
8409 n->args = NIL;
8410 n->agg_star = FALSE;
8411 n->agg_distinct = FALSE;
8412 n->func_variadic = FALSE;
8413 n->location = @1;
8414 $$ = (Node *)n;
8416 | CURRENT_USER
8418 FuncCall *n = makeNode(FuncCall);
8419 n->funcname = SystemFuncName("current_user");
8420 n->args = NIL;
8421 n->agg_star = FALSE;
8422 n->agg_distinct = FALSE;
8423 n->func_variadic = FALSE;
8424 n->location = @1;
8425 $$ = (Node *)n;
8427 | SESSION_USER
8429 FuncCall *n = makeNode(FuncCall);
8430 n->funcname = SystemFuncName("session_user");
8431 n->args = NIL;
8432 n->agg_star = FALSE;
8433 n->agg_distinct = FALSE;
8434 n->func_variadic = FALSE;
8435 n->location = @1;
8436 $$ = (Node *)n;
8438 | USER
8440 FuncCall *n = makeNode(FuncCall);
8441 n->funcname = SystemFuncName("current_user");
8442 n->args = NIL;
8443 n->agg_star = FALSE;
8444 n->agg_distinct = FALSE;
8445 n->func_variadic = FALSE;
8446 n->location = @1;
8447 $$ = (Node *)n;
8449 | CURRENT_CATALOG
8451 FuncCall *n = makeNode(FuncCall);
8452 n->funcname = SystemFuncName("current_database");
8453 n->args = NIL;
8454 n->agg_star = FALSE;
8455 n->agg_distinct = FALSE;
8456 n->func_variadic = FALSE;
8457 n->location = @1;
8458 $$ = (Node *)n;
8460 | CURRENT_SCHEMA
8462 FuncCall *n = makeNode(FuncCall);
8463 n->funcname = SystemFuncName("current_schema");
8464 n->args = NIL;
8465 n->agg_star = FALSE;
8466 n->agg_distinct = FALSE;
8467 n->func_variadic = FALSE;
8468 n->location = @1;
8469 $$ = (Node *)n;
8471 | CAST '(' a_expr AS Typename ')'
8472 { $$ = makeTypeCast($3, $5, @1); }
8473 | EXTRACT '(' extract_list ')'
8475 FuncCall *n = makeNode(FuncCall);
8476 n->funcname = SystemFuncName("date_part");
8477 n->args = $3;
8478 n->agg_star = FALSE;
8479 n->agg_distinct = FALSE;
8480 n->func_variadic = FALSE;
8481 n->location = @1;
8482 $$ = (Node *)n;
8484 | OVERLAY '(' overlay_list ')'
8486 /* overlay(A PLACING B FROM C FOR D) is converted to
8487 * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
8488 * overlay(A PLACING B FROM C) is converted to
8489 * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
8491 FuncCall *n = makeNode(FuncCall);
8492 n->funcname = SystemFuncName("overlay");
8493 n->args = $3;
8494 n->agg_star = FALSE;
8495 n->agg_distinct = FALSE;
8496 n->func_variadic = FALSE;
8497 n->location = @1;
8498 $$ = (Node *)n;
8500 | POSITION '(' position_list ')'
8502 /* position(A in B) is converted to position(B, A) */
8503 FuncCall *n = makeNode(FuncCall);
8504 n->funcname = SystemFuncName("position");
8505 n->args = $3;
8506 n->agg_star = FALSE;
8507 n->agg_distinct = FALSE;
8508 n->func_variadic = FALSE;
8509 n->location = @1;
8510 $$ = (Node *)n;
8512 | SUBSTRING '(' substr_list ')'
8514 /* substring(A from B for C) is converted to
8515 * substring(A, B, C) - thomas 2000-11-28
8517 FuncCall *n = makeNode(FuncCall);
8518 n->funcname = SystemFuncName("substring");
8519 n->args = $3;
8520 n->agg_star = FALSE;
8521 n->agg_distinct = FALSE;
8522 n->func_variadic = FALSE;
8523 n->location = @1;
8524 $$ = (Node *)n;
8526 | TREAT '(' a_expr AS Typename ')'
8528 /* TREAT(expr AS target) converts expr of a particular type to target,
8529 * which is defined to be a subtype of the original expression.
8530 * In SQL99, this is intended for use with structured UDTs,
8531 * but let's make this a generally useful form allowing stronger
8532 * coercions than are handled by implicit casting.
8534 FuncCall *n = makeNode(FuncCall);
8535 /* Convert SystemTypeName() to SystemFuncName() even though
8536 * at the moment they result in the same thing.
8538 n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
8539 n->args = list_make1($3);
8540 n->agg_star = FALSE;
8541 n->agg_distinct = FALSE;
8542 n->func_variadic = FALSE;
8543 n->location = @1;
8544 $$ = (Node *)n;
8546 | TRIM '(' BOTH trim_list ')'
8548 /* various trim expressions are defined in SQL92
8549 * - thomas 1997-07-19
8551 FuncCall *n = makeNode(FuncCall);
8552 n->funcname = SystemFuncName("btrim");
8553 n->args = $4;
8554 n->agg_star = FALSE;
8555 n->agg_distinct = FALSE;
8556 n->func_variadic = FALSE;
8557 n->location = @1;
8558 $$ = (Node *)n;
8560 | TRIM '(' LEADING trim_list ')'
8562 FuncCall *n = makeNode(FuncCall);
8563 n->funcname = SystemFuncName("ltrim");
8564 n->args = $4;
8565 n->agg_star = FALSE;
8566 n->agg_distinct = FALSE;
8567 n->func_variadic = FALSE;
8568 n->location = @1;
8569 $$ = (Node *)n;
8571 | TRIM '(' TRAILING trim_list ')'
8573 FuncCall *n = makeNode(FuncCall);
8574 n->funcname = SystemFuncName("rtrim");
8575 n->args = $4;
8576 n->agg_star = FALSE;
8577 n->agg_distinct = FALSE;
8578 n->func_variadic = FALSE;
8579 n->location = @1;
8580 $$ = (Node *)n;
8582 | TRIM '(' trim_list ')'
8584 FuncCall *n = makeNode(FuncCall);
8585 n->funcname = SystemFuncName("btrim");
8586 n->args = $3;
8587 n->agg_star = FALSE;
8588 n->agg_distinct = FALSE;
8589 n->func_variadic = FALSE;
8590 n->location = @1;
8591 $$ = (Node *)n;
8593 | NULLIF '(' a_expr ',' a_expr ')'
8595 $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5, @1);
8597 | COALESCE '(' expr_list ')'
8599 CoalesceExpr *c = makeNode(CoalesceExpr);
8600 c->args = $3;
8601 c->location = @1;
8602 $$ = (Node *)c;
8604 | GREATEST '(' expr_list ')'
8606 MinMaxExpr *v = makeNode(MinMaxExpr);
8607 v->args = $3;
8608 v->op = IS_GREATEST;
8609 v->location = @1;
8610 $$ = (Node *)v;
8612 | LEAST '(' expr_list ')'
8614 MinMaxExpr *v = makeNode(MinMaxExpr);
8615 v->args = $3;
8616 v->op = IS_LEAST;
8617 v->location = @1;
8618 $$ = (Node *)v;
8620 | XMLCONCAT '(' expr_list ')'
8622 $$ = makeXmlExpr(IS_XMLCONCAT, NULL, NIL, $3, @1);
8624 | XMLELEMENT '(' NAME_P ColLabel ')'
8626 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, NIL, @1);
8628 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ')'
8630 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, NIL, @1);
8632 | XMLELEMENT '(' NAME_P ColLabel ',' expr_list ')'
8634 $$ = makeXmlExpr(IS_XMLELEMENT, $4, NIL, $6, @1);
8636 | XMLELEMENT '(' NAME_P ColLabel ',' xml_attributes ',' expr_list ')'
8638 $$ = makeXmlExpr(IS_XMLELEMENT, $4, $6, $8, @1);
8640 | XMLFOREST '(' xml_attribute_list ')'
8642 $$ = makeXmlExpr(IS_XMLFOREST, NULL, $3, NIL, @1);
8644 | XMLPARSE '(' document_or_content a_expr xml_whitespace_option ')'
8646 XmlExpr *x = (XmlExpr *)
8647 makeXmlExpr(IS_XMLPARSE, NULL, NIL,
8648 list_make2($4, makeBoolAConst($5, -1)),
8649 @1);
8650 x->xmloption = $3;
8651 $$ = (Node *)x;
8653 | XMLPI '(' NAME_P ColLabel ')'
8655 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, NIL, @1);
8657 | XMLPI '(' NAME_P ColLabel ',' a_expr ')'
8659 $$ = makeXmlExpr(IS_XMLPI, $4, NULL, list_make1($6), @1);
8661 | XMLROOT '(' a_expr ',' xml_root_version opt_xml_root_standalone ')'
8663 $$ = makeXmlExpr(IS_XMLROOT, NULL, NIL,
8664 list_make3($3, $5, $6), @1);
8666 | XMLSERIALIZE '(' document_or_content a_expr AS SimpleTypename ')'
8668 XmlSerialize *n = makeNode(XmlSerialize);
8669 n->xmloption = $3;
8670 n->expr = $4;
8671 n->typename = $6;
8672 n->location = @1;
8673 $$ = (Node *)n;
8678 * SQL/XML support
8680 xml_root_version: VERSION_P a_expr
8681 { $$ = $2; }
8682 | VERSION_P NO VALUE_P
8683 { $$ = makeNullAConst(-1); }
8686 opt_xml_root_standalone: ',' STANDALONE_P YES_P
8687 { $$ = makeIntConst(XML_STANDALONE_YES, -1); }
8688 | ',' STANDALONE_P NO
8689 { $$ = makeIntConst(XML_STANDALONE_NO, -1); }
8690 | ',' STANDALONE_P NO VALUE_P
8691 { $$ = makeIntConst(XML_STANDALONE_NO_VALUE, -1); }
8692 | /*EMPTY*/
8693 { $$ = makeIntConst(XML_STANDALONE_OMITTED, -1); }
8696 xml_attributes: XMLATTRIBUTES '(' xml_attribute_list ')' { $$ = $3; }
8699 xml_attribute_list: xml_attribute_el { $$ = list_make1($1); }
8700 | xml_attribute_list ',' xml_attribute_el { $$ = lappend($1, $3); }
8703 xml_attribute_el: a_expr AS ColLabel
8705 $$ = makeNode(ResTarget);
8706 $$->name = $3;
8707 $$->indirection = NIL;
8708 $$->val = (Node *) $1;
8709 $$->location = @1;
8711 | a_expr
8713 $$ = makeNode(ResTarget);
8714 $$->name = NULL;
8715 $$->indirection = NIL;
8716 $$->val = (Node *) $1;
8717 $$->location = @1;
8721 document_or_content: DOCUMENT_P { $$ = XMLOPTION_DOCUMENT; }
8722 | CONTENT_P { $$ = XMLOPTION_CONTENT; }
8725 xml_whitespace_option: PRESERVE WHITESPACE_P { $$ = TRUE; }
8726 | STRIP_P WHITESPACE_P { $$ = FALSE; }
8727 | /*EMPTY*/ { $$ = FALSE; }
8731 * Supporting nonterminals for expressions.
8734 /* Explicit row production.
8736 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
8737 * without conflicting with the parenthesized a_expr production. Without the
8738 * ROW keyword, there must be more than one a_expr inside the parens.
8740 row: ROW '(' expr_list ')' { $$ = $3; }
8741 | ROW '(' ')' { $$ = NIL; }
8742 | '(' expr_list ',' a_expr ')' { $$ = lappend($2, $4); }
8745 sub_type: ANY { $$ = ANY_SUBLINK; }
8746 | SOME { $$ = ANY_SUBLINK; }
8747 | ALL { $$ = ALL_SUBLINK; }
8750 all_Op: Op { $$ = $1; }
8751 | MathOp { $$ = $1; }
8754 MathOp: '+' { $$ = "+"; }
8755 | '-' { $$ = "-"; }
8756 | '*' { $$ = "*"; }
8757 | '/' { $$ = "/"; }
8758 | '%' { $$ = "%"; }
8759 | '^' { $$ = "^"; }
8760 | '<' { $$ = "<"; }
8761 | '>' { $$ = ">"; }
8762 | '=' { $$ = "="; }
8765 qual_Op: Op
8766 { $$ = list_make1(makeString($1)); }
8767 | OPERATOR '(' any_operator ')'
8768 { $$ = $3; }
8771 qual_all_Op:
8772 all_Op
8773 { $$ = list_make1(makeString($1)); }
8774 | OPERATOR '(' any_operator ')'
8775 { $$ = $3; }
8778 subquery_Op:
8779 all_Op
8780 { $$ = list_make1(makeString($1)); }
8781 | OPERATOR '(' any_operator ')'
8782 { $$ = $3; }
8783 | LIKE
8784 { $$ = list_make1(makeString("~~")); }
8785 | NOT LIKE
8786 { $$ = list_make1(makeString("!~~")); }
8787 | ILIKE
8788 { $$ = list_make1(makeString("~~*")); }
8789 | NOT ILIKE
8790 { $$ = list_make1(makeString("!~~*")); }
8791 /* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
8792 * the regular expression is preprocessed by a function (similar_escape),
8793 * and the ~ operator for posix regular expressions is used.
8794 * x SIMILAR TO y -> x ~ similar_escape(y)
8795 * this transformation is made on the fly by the parser upwards.
8796 * however the SubLink structure which handles any/some/all stuff
8797 * is not ready for such a thing.
8801 expr_list: a_expr
8803 $$ = list_make1($1);
8805 | expr_list ',' a_expr
8807 $$ = lappend($1, $3);
8811 type_list: Typename { $$ = list_make1($1); }
8812 | type_list ',' Typename { $$ = lappend($1, $3); }
8815 array_expr: '[' expr_list ']'
8817 $$ = makeAArrayExpr($2, @1);
8819 | '[' array_expr_list ']'
8821 $$ = makeAArrayExpr($2, @1);
8823 | '[' ']'
8825 $$ = makeAArrayExpr(NIL, @1);
8829 array_expr_list: array_expr { $$ = list_make1($1); }
8830 | array_expr_list ',' array_expr { $$ = lappend($1, $3); }
8834 extract_list:
8835 extract_arg FROM a_expr
8837 $$ = list_make2(makeStringConst($1, @1), $3);
8839 | /*EMPTY*/ { $$ = NIL; }
8842 /* Allow delimited string SCONST in extract_arg as an SQL extension.
8843 * - thomas 2001-04-12
8845 extract_arg:
8846 IDENT { $$ = $1; }
8847 | YEAR_P { $$ = "year"; }
8848 | MONTH_P { $$ = "month"; }
8849 | DAY_P { $$ = "day"; }
8850 | HOUR_P { $$ = "hour"; }
8851 | MINUTE_P { $$ = "minute"; }
8852 | SECOND_P { $$ = "second"; }
8853 | SCONST { $$ = $1; }
8856 /* OVERLAY() arguments
8857 * SQL99 defines the OVERLAY() function:
8858 * o overlay(text placing text from int for int)
8859 * o overlay(text placing text from int)
8861 overlay_list:
8862 a_expr overlay_placing substr_from substr_for
8864 $$ = list_make4($1, $2, $3, $4);
8866 | a_expr overlay_placing substr_from
8868 $$ = list_make3($1, $2, $3);
8872 overlay_placing:
8873 PLACING a_expr
8874 { $$ = $2; }
8877 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
8879 position_list:
8880 b_expr IN_P b_expr { $$ = list_make2($3, $1); }
8881 | /*EMPTY*/ { $$ = NIL; }
8884 /* SUBSTRING() arguments
8885 * SQL9x defines a specific syntax for arguments to SUBSTRING():
8886 * o substring(text from int for int)
8887 * o substring(text from int) get entire string from starting point "int"
8888 * o substring(text for int) get first "int" characters of string
8889 * o substring(text from pattern) get entire string matching pattern
8890 * o substring(text from pattern for escape) same with specified escape char
8891 * We also want to support generic substring functions which accept
8892 * the usual generic list of arguments. So we will accept both styles
8893 * here, and convert the SQL9x style to the generic list for further
8894 * processing. - thomas 2000-11-28
8896 substr_list:
8897 a_expr substr_from substr_for
8899 $$ = list_make3($1, $2, $3);
8901 | a_expr substr_for substr_from
8903 /* not legal per SQL99, but might as well allow it */
8904 $$ = list_make3($1, $3, $2);
8906 | a_expr substr_from
8908 $$ = list_make2($1, $2);
8910 | a_expr substr_for
8913 * Since there are no cases where this syntax allows
8914 * a textual FOR value, we forcibly cast the argument
8915 * to int4. The possible matches in pg_proc are
8916 * substring(text,int4) and substring(text,text),
8917 * and we don't want the parser to choose the latter,
8918 * which it is likely to do if the second argument
8919 * is unknown or doesn't have an implicit cast to int4.
8921 $$ = list_make3($1, makeIntConst(1, -1),
8922 makeTypeCast($2,
8923 SystemTypeName("int4"), -1));
8925 | expr_list
8927 $$ = $1;
8929 | /*EMPTY*/
8930 { $$ = NIL; }
8933 substr_from:
8934 FROM a_expr { $$ = $2; }
8937 substr_for: FOR a_expr { $$ = $2; }
8940 trim_list: a_expr FROM expr_list { $$ = lappend($3, $1); }
8941 | FROM expr_list { $$ = $2; }
8942 | expr_list { $$ = $1; }
8945 in_expr: select_with_parens
8947 SubLink *n = makeNode(SubLink);
8948 n->subselect = $1;
8949 /* other fields will be filled later */
8950 $$ = (Node *)n;
8952 | '(' expr_list ')' { $$ = (Node *)$2; }
8956 * Define SQL92-style case clause.
8957 * - Full specification
8958 * CASE WHEN a = b THEN c ... ELSE d END
8959 * - Implicit argument
8960 * CASE a WHEN b THEN c ... ELSE d END
8962 case_expr: CASE case_arg when_clause_list case_default END_P
8964 CaseExpr *c = makeNode(CaseExpr);
8965 c->casetype = InvalidOid; /* not analyzed yet */
8966 c->arg = (Expr *) $2;
8967 c->args = $3;
8968 c->defresult = (Expr *) $4;
8969 c->location = @1;
8970 $$ = (Node *)c;
8974 when_clause_list:
8975 /* There must be at least one */
8976 when_clause { $$ = list_make1($1); }
8977 | when_clause_list when_clause { $$ = lappend($1, $2); }
8980 when_clause:
8981 WHEN a_expr THEN a_expr
8983 CaseWhen *w = makeNode(CaseWhen);
8984 w->expr = (Expr *) $2;
8985 w->result = (Expr *) $4;
8986 w->location = @1;
8987 $$ = (Node *)w;
8991 case_default:
8992 ELSE a_expr { $$ = $2; }
8993 | /*EMPTY*/ { $$ = NULL; }
8996 case_arg: a_expr { $$ = $1; }
8997 | /*EMPTY*/ { $$ = NULL; }
9001 * columnref starts with relation_name not ColId, so that OLD and NEW
9002 * references can be accepted. Note that when there are more than two
9003 * dotted names, the first name is not actually a relation name...
9005 columnref: relation_name
9007 $$ = makeColumnRef($1, NIL, @1);
9009 | relation_name indirection
9011 $$ = makeColumnRef($1, $2, @1);
9015 indirection_el:
9016 '.' attr_name
9018 $$ = (Node *) makeString($2);
9020 | '.' '*'
9022 $$ = (Node *) makeNode(A_Star);
9024 | '[' a_expr ']'
9026 A_Indices *ai = makeNode(A_Indices);
9027 ai->lidx = NULL;
9028 ai->uidx = $2;
9029 $$ = (Node *) ai;
9031 | '[' a_expr ':' a_expr ']'
9033 A_Indices *ai = makeNode(A_Indices);
9034 ai->lidx = $2;
9035 ai->uidx = $4;
9036 $$ = (Node *) ai;
9040 indirection:
9041 indirection_el { $$ = list_make1($1); }
9042 | indirection indirection_el { $$ = lappend($1, $2); }
9045 opt_indirection:
9046 /*EMPTY*/ { $$ = NIL; }
9047 | opt_indirection indirection_el { $$ = lappend($1, $2); }
9050 opt_asymmetric: ASYMMETRIC
9051 | /*EMPTY*/
9055 * The SQL spec defines "contextually typed value expressions" and
9056 * "contextually typed row value constructors", which for our purposes
9057 * are the same as "a_expr" and "row" except that DEFAULT can appear at
9058 * the top level.
9061 ctext_expr:
9062 a_expr { $$ = (Node *) $1; }
9063 | DEFAULT
9065 SetToDefault *n = makeNode(SetToDefault);
9066 n->location = @1;
9067 $$ = (Node *) n;
9071 ctext_expr_list:
9072 ctext_expr { $$ = list_make1($1); }
9073 | ctext_expr_list ',' ctext_expr { $$ = lappend($1, $3); }
9077 * We should allow ROW '(' ctext_expr_list ')' too, but that seems to require
9078 * making VALUES a fully reserved word, which will probably break more apps
9079 * than allowing the noise-word is worth.
9081 ctext_row: '(' ctext_expr_list ')' { $$ = $2; }
9085 /*****************************************************************************
9087 * target list for SELECT
9089 *****************************************************************************/
9091 target_list:
9092 target_el { $$ = list_make1($1); }
9093 | target_list ',' target_el { $$ = lappend($1, $3); }
9096 target_el: a_expr AS ColLabel
9098 $$ = makeNode(ResTarget);
9099 $$->name = $3;
9100 $$->indirection = NIL;
9101 $$->val = (Node *)$1;
9102 $$->location = @1;
9105 * We support omitting AS only for column labels that aren't
9106 * any known keyword. There is an ambiguity against postfix
9107 * operators: is "a ! b" an infix expression, or a postfix
9108 * expression and a column label? We prefer to resolve this
9109 * as an infix expression, which we accomplish by assigning
9110 * IDENT a precedence higher than POSTFIXOP.
9112 | a_expr IDENT
9114 $$ = makeNode(ResTarget);
9115 $$->name = $2;
9116 $$->indirection = NIL;
9117 $$->val = (Node *)$1;
9118 $$->location = @1;
9120 | a_expr
9122 $$ = makeNode(ResTarget);
9123 $$->name = NULL;
9124 $$->indirection = NIL;
9125 $$->val = (Node *)$1;
9126 $$->location = @1;
9128 | '*'
9130 ColumnRef *n = makeNode(ColumnRef);
9131 n->fields = list_make1(makeNode(A_Star));
9132 n->location = @1;
9134 $$ = makeNode(ResTarget);
9135 $$->name = NULL;
9136 $$->indirection = NIL;
9137 $$->val = (Node *)n;
9138 $$->location = @1;
9143 /*****************************************************************************
9145 * Names and constants
9147 *****************************************************************************/
9149 relation_name:
9150 SpecialRuleRelation { $$ = $1; }
9151 | ColId { $$ = $1; }
9154 qualified_name_list:
9155 qualified_name { $$ = list_make1($1); }
9156 | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
9160 * The production for a qualified relation name has to exactly match the
9161 * production for a qualified func_name, because in a FROM clause we cannot
9162 * tell which we are parsing until we see what comes after it ('(' for a
9163 * func_name, something else for a relation). Therefore we allow 'indirection'
9164 * which may contain subscripts, and reject that case in the C code.
9166 qualified_name:
9167 relation_name
9169 $$ = makeNode(RangeVar);
9170 $$->catalogname = NULL;
9171 $$->schemaname = NULL;
9172 $$->relname = $1;
9173 $$->location = @1;
9175 | relation_name indirection
9177 check_qualified_name($2);
9178 $$ = makeNode(RangeVar);
9179 switch (list_length($2))
9181 case 1:
9182 $$->catalogname = NULL;
9183 $$->schemaname = $1;
9184 $$->relname = strVal(linitial($2));
9185 break;
9186 case 2:
9187 $$->catalogname = $1;
9188 $$->schemaname = strVal(linitial($2));
9189 $$->relname = strVal(lsecond($2));
9190 break;
9191 default:
9192 ereport(ERROR,
9193 (errcode(ERRCODE_SYNTAX_ERROR),
9194 errmsg("improper qualified name (too many dotted names): %s",
9195 NameListToString(lcons(makeString($1), $2))),
9196 scanner_errposition(@1)));
9197 break;
9199 $$->location = @1;
9203 name_list: name
9204 { $$ = list_make1(makeString($1)); }
9205 | name_list ',' name
9206 { $$ = lappend($1, makeString($3)); }
9210 name: ColId { $$ = $1; };
9212 database_name:
9213 ColId { $$ = $1; };
9215 access_method:
9216 ColId { $$ = $1; };
9218 attr_name: ColLabel { $$ = $1; };
9220 index_name: ColId { $$ = $1; };
9222 file_name: Sconst { $$ = $1; };
9225 * The production for a qualified func_name has to exactly match the
9226 * production for a qualified columnref, because we cannot tell which we
9227 * are parsing until we see what comes after it ('(' or Sconst for a func_name,
9228 * anything else for a columnref). Therefore we allow 'indirection' which
9229 * may contain subscripts, and reject that case in the C code. (If we
9230 * ever implement SQL99-like methods, such syntax may actually become legal!)
9232 func_name: type_function_name
9233 { $$ = list_make1(makeString($1)); }
9234 | relation_name indirection
9235 { $$ = check_func_name(lcons(makeString($1), $2)); }
9240 * Constants
9242 AexprConst: Iconst
9244 $$ = makeIntConst($1, @1);
9246 | FCONST
9248 $$ = makeFloatConst($1, @1);
9250 | Sconst
9252 $$ = makeStringConst($1, @1);
9254 | BCONST
9256 $$ = makeBitStringConst($1, @1);
9258 | XCONST
9260 /* This is a bit constant per SQL99:
9261 * Without Feature F511, "BIT data type",
9262 * a <general literal> shall not be a
9263 * <bit string literal> or a <hex string literal>.
9265 $$ = makeBitStringConst($1, @1);
9267 | func_name Sconst
9269 /* generic type 'literal' syntax */
9270 TypeName *t = makeTypeNameFromNameList($1);
9271 t->location = @1;
9272 $$ = makeStringConstCast($2, @2, t);
9274 | func_name '(' expr_list ')' Sconst
9276 /* generic syntax with a type modifier */
9277 TypeName *t = makeTypeNameFromNameList($1);
9278 t->typmods = $3;
9279 t->location = @1;
9280 $$ = makeStringConstCast($5, @5, t);
9282 | ConstTypename Sconst
9284 $$ = makeStringConstCast($2, @2, $1);
9286 | ConstInterval Sconst opt_interval
9288 TypeName *t = $1;
9289 t->typmods = $3;
9290 $$ = makeStringConstCast($2, @2, t);
9292 | ConstInterval '(' Iconst ')' Sconst opt_interval
9294 TypeName *t = $1;
9295 if ($6 != NIL)
9297 if (list_length($6) != 1)
9298 ereport(ERROR,
9299 (errcode(ERRCODE_SYNTAX_ERROR),
9300 errmsg("interval precision specified twice"),
9301 scanner_errposition(@1)));
9302 t->typmods = lappend($6, makeIntConst($3, @3));
9304 else
9305 t->typmods = list_make2(makeIntConst(INTERVAL_FULL_RANGE, -1),
9306 makeIntConst($3, @3));
9307 $$ = makeStringConstCast($5, @5, t);
9309 | TRUE_P
9311 $$ = makeBoolAConst(TRUE, @1);
9313 | FALSE_P
9315 $$ = makeBoolAConst(FALSE, @1);
9317 | NULL_P
9319 $$ = makeNullAConst(@1);
9323 Iconst: ICONST { $$ = $1; };
9324 Sconst: SCONST { $$ = $1; };
9325 RoleId: ColId { $$ = $1; };
9327 SignedIconst: ICONST { $$ = $1; }
9328 | '+' ICONST { $$ = + $2; }
9329 | '-' ICONST { $$ = - $2; }
9333 * Name classification hierarchy.
9335 * IDENT is the lexeme returned by the lexer for identifiers that match
9336 * no known keyword. In most cases, we can accept certain keywords as
9337 * names, not only IDENTs. We prefer to accept as many such keywords
9338 * as possible to minimize the impact of "reserved words" on programmers.
9339 * So, we divide names into several possible classes. The classification
9340 * is chosen in part to make keywords acceptable as names wherever possible.
9343 /* Column identifier --- names that can be column, table, etc names.
9345 ColId: IDENT { $$ = $1; }
9346 | unreserved_keyword { $$ = pstrdup($1); }
9347 | col_name_keyword { $$ = pstrdup($1); }
9350 /* Type/function identifier --- names that can be type or function names.
9352 type_function_name: IDENT { $$ = $1; }
9353 | unreserved_keyword { $$ = pstrdup($1); }
9354 | type_func_name_keyword { $$ = pstrdup($1); }
9357 /* Column label --- allowed labels in "AS" clauses.
9358 * This presently includes *all* Postgres keywords.
9360 ColLabel: IDENT { $$ = $1; }
9361 | unreserved_keyword { $$ = pstrdup($1); }
9362 | col_name_keyword { $$ = pstrdup($1); }
9363 | type_func_name_keyword { $$ = pstrdup($1); }
9364 | reserved_keyword { $$ = pstrdup($1); }
9369 * Keyword category lists. Generally, every keyword present in
9370 * the Postgres grammar should appear in exactly one of these lists.
9372 * Put a new keyword into the first list that it can go into without causing
9373 * shift or reduce conflicts. The earlier lists define "less reserved"
9374 * categories of keywords.
9376 * Make sure that each keyword's category in keywords.c matches where
9377 * it is listed here. (Someday we may be able to generate these lists and
9378 * keywords.c's table from a common master list.)
9381 /* "Unreserved" keywords --- available for use as any kind of name.
9383 unreserved_keyword:
9384 ABORT_P
9385 | ABSOLUTE_P
9386 | ACCESS
9387 | ACTION
9388 | ADD_P
9389 | ADMIN
9390 | AFTER
9391 | AGGREGATE
9392 | ALSO
9393 | ALTER
9394 | ALWAYS
9395 | ASSERTION
9396 | ASSIGNMENT
9397 | AT
9398 | BACKWARD
9399 | BEFORE
9400 | BEGIN_P
9401 | BY
9402 | CACHE
9403 | CALLED
9404 | CASCADE
9405 | CASCADED
9406 | CATALOG_P
9407 | CHAIN
9408 | CHARACTERISTICS
9409 | CHECKPOINT
9410 | CLASS
9411 | CLOSE
9412 | CLUSTER
9413 | COMMENT
9414 | COMMIT
9415 | COMMITTED
9416 | CONCURRENTLY
9417 | CONFIGURATION
9418 | CONNECTION
9419 | CONSTRAINTS
9420 | CONTENT_P
9421 | CONTINUE_P
9422 | CONVERSION_P
9423 | COPY
9424 | COST
9425 | CREATEDB
9426 | CREATEROLE
9427 | CREATEUSER
9428 | CSV
9429 | CTYPE
9430 | CURRENT_P
9431 | CURSOR
9432 | CYCLE
9433 | DATA_P
9434 | DATABASE
9435 | DAY_P
9436 | DEALLOCATE
9437 | DECLARE
9438 | DEFAULTS
9439 | DEFERRED
9440 | DEFINER
9441 | DELETE_P
9442 | DELIMITER
9443 | DELIMITERS
9444 | DICTIONARY
9445 | DISABLE_P
9446 | DISCARD
9447 | DOCUMENT_P
9448 | DOMAIN_P
9449 | DOUBLE_P
9450 | DROP
9451 | EACH
9452 | ENABLE_P
9453 | ENCODING
9454 | ENCRYPTED
9455 | ENUM_P
9456 | ESCAPE
9457 | EXCLUDING
9458 | EXCLUSIVE
9459 | EXECUTE
9460 | EXPLAIN
9461 | EXTERNAL
9462 | FAMILY
9463 | FIRST_P
9464 | FORCE
9465 | FORWARD
9466 | FUNCTION
9467 | GLOBAL
9468 | GRANTED
9469 | HANDLER
9470 | HEADER_P
9471 | HOLD
9472 | HOUR_P
9473 | IDENTITY_P
9474 | IF_P
9475 | IMMEDIATE
9476 | IMMUTABLE
9477 | IMPLICIT_P
9478 | INCLUDING
9479 | INCREMENT
9480 | INDEX
9481 | INDEXES
9482 | INHERIT
9483 | INHERITS
9484 | INPUT_P
9485 | INSENSITIVE
9486 | INSERT
9487 | INSTEAD
9488 | INVOKER
9489 | ISOLATION
9490 | KEY
9491 | LANCOMPILER
9492 | LANGUAGE
9493 | LARGE_P
9494 | LAST_P
9495 | LEVEL
9496 | LISTEN
9497 | LOAD
9498 | LOCAL
9499 | LOCATION
9500 | LOCK_P
9501 | LOGIN_P
9502 | MAPPING
9503 | MATCH
9504 | MAXVALUE
9505 | MINUTE_P
9506 | MINVALUE
9507 | MODE
9508 | MONTH_P
9509 | MOVE
9510 | NAME_P
9511 | NAMES
9512 | NEXT
9513 | NO
9514 | NOCREATEDB
9515 | NOCREATEROLE
9516 | NOCREATEUSER
9517 | NOINHERIT
9518 | NOLOGIN_P
9519 | NOSUPERUSER
9520 | NOTHING
9521 | NOTIFY
9522 | NOWAIT
9523 | NULLS_P
9524 | OBJECT_P
9525 | OF
9526 | OIDS
9527 | OPERATOR
9528 | OPTION
9529 | OWNED
9530 | OWNER
9531 | PARSER
9532 | PARTIAL
9533 | PASSWORD
9534 | PLANS
9535 | PREPARE
9536 | PREPARED
9537 | PRESERVE
9538 | PRIOR
9539 | PRIVILEGES
9540 | PROCEDURAL
9541 | PROCEDURE
9542 | QUOTE
9543 | READ
9544 | REASSIGN
9545 | RECHECK
9546 | RECURSIVE
9547 | REINDEX
9548 | RELATIVE_P
9549 | RELEASE
9550 | RENAME
9551 | REPEATABLE
9552 | REPLACE
9553 | REPLICA
9554 | RESET
9555 | RESTART
9556 | RESTRICT
9557 | RETURNS
9558 | REVOKE
9559 | ROLE
9560 | ROLLBACK
9561 | ROWS
9562 | RULE
9563 | SAVEPOINT
9564 | SCHEMA
9565 | SCROLL
9566 | SEARCH
9567 | SECOND_P
9568 | SECURITY
9569 | SEQUENCE
9570 | SERIALIZABLE
9571 | SESSION
9572 | SET
9573 | SHARE
9574 | SHOW
9575 | SIMPLE
9576 | STABLE
9577 | STANDALONE_P
9578 | START
9579 | STATEMENT
9580 | STATISTICS
9581 | STDIN
9582 | STDOUT
9583 | STORAGE
9584 | STRICT_P
9585 | STRIP_P
9586 | SUPERUSER_P
9587 | SYSID
9588 | SYSTEM_P
9589 | TABLESPACE
9590 | TEMP
9591 | TEMPLATE
9592 | TEMPORARY
9593 | TEXT_P
9594 | TRANSACTION
9595 | TRIGGER
9596 | TRUNCATE
9597 | TRUSTED
9598 | TYPE_P
9599 | UNCOMMITTED
9600 | UNENCRYPTED
9601 | UNKNOWN
9602 | UNLISTEN
9603 | UNTIL
9604 | UPDATE
9605 | VACUUM
9606 | VALID
9607 | VALIDATOR
9608 | VALUE_P
9609 | VARYING
9610 | VERSION_P
9611 | VIEW
9612 | VOLATILE
9613 | WHITESPACE_P
9614 | WITHOUT
9615 | WORK
9616 | WRITE
9617 | XML_P
9618 | YEAR_P
9619 | YES_P
9620 | ZONE
9623 /* Column identifier --- keywords that can be column, table, etc names.
9625 * Many of these keywords will in fact be recognized as type or function
9626 * names too; but they have special productions for the purpose, and so
9627 * can't be treated as "generic" type or function names.
9629 * The type names appearing here are not usable as function names
9630 * because they can be followed by '(' in typename productions, which
9631 * looks too much like a function call for an LR(1) parser.
9633 col_name_keyword:
9634 BIGINT
9635 | BIT
9636 | BOOLEAN_P
9637 | CHAR_P
9638 | CHARACTER
9639 | COALESCE
9640 | DEC
9641 | DECIMAL_P
9642 | EXISTS
9643 | EXTRACT
9644 | FLOAT_P
9645 | GREATEST
9646 | INOUT
9647 | INT_P
9648 | INTEGER
9649 | INTERVAL
9650 | LEAST
9651 | NATIONAL
9652 | NCHAR
9653 | NONE
9654 | NULLIF
9655 | NUMERIC
9656 | OUT_P
9657 | OVERLAY
9658 | POSITION
9659 | PRECISION
9660 | REAL
9661 | ROW
9662 | SETOF
9663 | SMALLINT
9664 | SUBSTRING
9665 | TIME
9666 | TIMESTAMP
9667 | TREAT
9668 | TRIM
9669 | VALUES
9670 | VARCHAR
9671 | XMLATTRIBUTES
9672 | XMLCONCAT
9673 | XMLELEMENT
9674 | XMLFOREST
9675 | XMLPARSE
9676 | XMLPI
9677 | XMLROOT
9678 | XMLSERIALIZE
9681 /* Type/function identifier --- keywords that can be type or function names.
9683 * Most of these are keywords that are used as operators in expressions;
9684 * in general such keywords can't be column names because they would be
9685 * ambiguous with variables, but they are unambiguous as function identifiers.
9687 * Do not include POSITION, SUBSTRING, etc here since they have explicit
9688 * productions in a_expr to support the goofy SQL9x argument syntax.
9689 * - thomas 2000-11-28
9691 type_func_name_keyword:
9692 AUTHORIZATION
9693 | BETWEEN
9694 | BINARY
9695 | CROSS
9696 | CURRENT_SCHEMA
9697 | FREEZE
9698 | FULL
9699 | ILIKE
9700 | INNER_P
9701 | IS
9702 | ISNULL
9703 | JOIN
9704 | LEFT
9705 | LIKE
9706 | NATURAL
9707 | NOTNULL
9708 | OUTER_P
9709 | OVERLAPS
9710 | RIGHT
9711 | SIMILAR
9712 | VERBOSE
9715 /* Reserved keyword --- these keywords are usable only as a ColLabel.
9717 * Keywords appear here if they could not be distinguished from variable,
9718 * type, or function names in some contexts. Don't put things here unless
9719 * forced to.
9721 reserved_keyword:
9723 | ANALYSE
9724 | ANALYZE
9725 | AND
9726 | ANY
9727 | ARRAY
9728 | AS
9729 | ASC
9730 | ASYMMETRIC
9731 | BOTH
9732 | CASE
9733 | CAST
9734 | CHECK
9735 | COLLATE
9736 | COLUMN
9737 | CONSTRAINT
9738 | CREATE
9739 | CURRENT_CATALOG
9740 | CURRENT_DATE
9741 | CURRENT_ROLE
9742 | CURRENT_TIME
9743 | CURRENT_TIMESTAMP
9744 | CURRENT_USER
9745 | DEFAULT
9746 | DEFERRABLE
9747 | DESC
9748 | DISTINCT
9749 | DO
9750 | ELSE
9751 | END_P
9752 | EXCEPT
9753 | FALSE_P
9754 | FETCH
9755 | FOR
9756 | FOREIGN
9757 | FROM
9758 | GRANT
9759 | GROUP_P
9760 | HAVING
9761 | IN_P
9762 | INITIALLY
9763 | INTERSECT
9764 | INTO
9765 | LEADING
9766 | LIMIT
9767 | LOCALTIME
9768 | LOCALTIMESTAMP
9769 | NEW
9770 | NOT
9771 | NULL_P
9772 | OFF
9773 | OFFSET
9774 | OLD
9775 | ON
9776 | ONLY
9777 | OR
9778 | ORDER
9779 | PLACING
9780 | PRIMARY
9781 | REFERENCES
9782 | RETURNING
9783 | SELECT
9784 | SESSION_USER
9785 | SOME
9786 | SYMMETRIC
9787 | TABLE
9788 | THEN
9789 | TO
9790 | TRAILING
9791 | TRUE_P
9792 | UNION
9793 | UNIQUE
9794 | USER
9795 | USING
9796 | VARIADIC
9797 | WHEN
9798 | WHERE
9799 | WITH
9803 SpecialRuleRelation:
9806 if (QueryIsRule)
9807 $$ = "*OLD*";
9808 else
9809 ereport(ERROR,
9810 (errcode(ERRCODE_SYNTAX_ERROR),
9811 errmsg("OLD used in query that is not in a rule"),
9812 scanner_errposition(@1)));
9814 | NEW
9816 if (QueryIsRule)
9817 $$ = "*NEW*";
9818 else
9819 ereport(ERROR,
9820 (errcode(ERRCODE_SYNTAX_ERROR),
9821 errmsg("NEW used in query that is not in a rule"),
9822 scanner_errposition(@1)));
9828 static Node *
9829 makeColumnRef(char *colname, List *indirection, int location)
9832 * Generate a ColumnRef node, with an A_Indirection node added if there
9833 * is any subscripting in the specified indirection list. However,
9834 * any field selection at the start of the indirection list must be
9835 * transposed into the "fields" part of the ColumnRef node.
9837 ColumnRef *c = makeNode(ColumnRef);
9838 int nfields = 0;
9839 ListCell *l;
9841 c->location = location;
9842 foreach(l, indirection)
9844 if (IsA(lfirst(l), A_Indices))
9846 A_Indirection *i = makeNode(A_Indirection);
9848 if (nfields == 0)
9850 /* easy case - all indirection goes to A_Indirection */
9851 c->fields = list_make1(makeString(colname));
9852 i->indirection = check_indirection(indirection);
9854 else
9856 /* got to split the list in two */
9857 i->indirection = check_indirection(list_copy_tail(indirection,
9858 nfields));
9859 indirection = list_truncate(indirection, nfields);
9860 c->fields = lcons(makeString(colname), indirection);
9862 i->arg = (Node *) c;
9863 return (Node *) i;
9865 else if (IsA(lfirst(l), A_Star))
9867 /* We only allow '*' at the end of a ColumnRef */
9868 if (lnext(l) != NULL)
9869 yyerror("improper use of \"*\"");
9871 nfields++;
9873 /* No subscripting, so all indirection gets added to field list */
9874 c->fields = lcons(makeString(colname), indirection);
9875 return (Node *) c;
9878 static Node *
9879 makeTypeCast(Node *arg, TypeName *typename, int location)
9881 TypeCast *n = makeNode(TypeCast);
9882 n->arg = arg;
9883 n->typename = typename;
9884 n->location = location;
9885 return (Node *) n;
9888 static Node *
9889 makeStringConst(char *str, int location)
9891 A_Const *n = makeNode(A_Const);
9893 n->val.type = T_String;
9894 n->val.val.str = str;
9895 n->location = location;
9897 return (Node *)n;
9900 static Node *
9901 makeStringConstCast(char *str, int location, TypeName *typename)
9903 Node *s = makeStringConst(str, location);
9905 return makeTypeCast(s, typename, -1);
9908 static Node *
9909 makeIntConst(int val, int location)
9911 A_Const *n = makeNode(A_Const);
9913 n->val.type = T_Integer;
9914 n->val.val.ival = val;
9915 n->location = location;
9917 return (Node *)n;
9920 static Node *
9921 makeFloatConst(char *str, int location)
9923 A_Const *n = makeNode(A_Const);
9925 n->val.type = T_Float;
9926 n->val.val.str = str;
9927 n->location = location;
9929 return (Node *)n;
9932 static Node *
9933 makeBitStringConst(char *str, int location)
9935 A_Const *n = makeNode(A_Const);
9937 n->val.type = T_BitString;
9938 n->val.val.str = str;
9939 n->location = location;
9941 return (Node *)n;
9944 static Node *
9945 makeNullAConst(int location)
9947 A_Const *n = makeNode(A_Const);
9949 n->val.type = T_Null;
9950 n->location = location;
9952 return (Node *)n;
9955 static Node *
9956 makeAConst(Value *v, int location)
9958 Node *n;
9960 switch (v->type)
9962 case T_Float:
9963 n = makeFloatConst(v->val.str, location);
9964 break;
9966 case T_Integer:
9967 n = makeIntConst(v->val.ival, location);
9968 break;
9970 case T_String:
9971 default:
9972 n = makeStringConst(v->val.str, location);
9973 break;
9976 return n;
9979 /* makeBoolAConst()
9980 * Create an A_Const string node and put it inside a boolean cast.
9982 static Node *
9983 makeBoolAConst(bool state, int location)
9985 A_Const *n = makeNode(A_Const);
9987 n->val.type = T_String;
9988 n->val.val.str = (state ? "t" : "f");
9989 n->location = location;
9991 return makeTypeCast((Node *)n, SystemTypeName("bool"), -1);
9994 /* makeOverlaps()
9995 * Create and populate a FuncCall node to support the OVERLAPS operator.
9997 static FuncCall *
9998 makeOverlaps(List *largs, List *rargs, int location)
10000 FuncCall *n = makeNode(FuncCall);
10002 n->funcname = SystemFuncName("overlaps");
10003 if (list_length(largs) == 1)
10004 largs = lappend(largs, largs);
10005 else if (list_length(largs) != 2)
10006 ereport(ERROR,
10007 (errcode(ERRCODE_SYNTAX_ERROR),
10008 errmsg("wrong number of parameters on left side of OVERLAPS expression"),
10009 scanner_errposition(location)));
10010 if (list_length(rargs) == 1)
10011 rargs = lappend(rargs, rargs);
10012 else if (list_length(rargs) != 2)
10013 ereport(ERROR,
10014 (errcode(ERRCODE_SYNTAX_ERROR),
10015 errmsg("wrong number of parameters on right side of OVERLAPS expression"),
10016 scanner_errposition(location)));
10017 n->args = list_concat(largs, rargs);
10018 n->agg_star = FALSE;
10019 n->agg_distinct = FALSE;
10020 n->func_variadic = FALSE;
10021 n->location = location;
10022 return n;
10025 /* check_qualified_name --- check the result of qualified_name production
10027 * It's easiest to let the grammar production for qualified_name allow
10028 * subscripts and '*', which we then must reject here.
10030 static void
10031 check_qualified_name(List *names)
10033 ListCell *i;
10035 foreach(i, names)
10037 if (!IsA(lfirst(i), String))
10038 yyerror("syntax error");
10042 /* check_func_name --- check the result of func_name production
10044 * It's easiest to let the grammar production for func_name allow subscripts
10045 * and '*', which we then must reject here.
10047 static List *
10048 check_func_name(List *names)
10050 ListCell *i;
10052 foreach(i, names)
10054 if (!IsA(lfirst(i), String))
10055 yyerror("syntax error");
10057 return names;
10060 /* check_indirection --- check the result of indirection production
10062 * We only allow '*' at the end of the list, but it's hard to enforce that
10063 * in the grammar, so do it here.
10065 static List *
10066 check_indirection(List *indirection)
10068 ListCell *l;
10070 foreach(l, indirection)
10072 if (IsA(lfirst(l), A_Star))
10074 if (lnext(l) != NULL)
10075 yyerror("improper use of \"*\"");
10078 return indirection;
10081 /* extractArgTypes()
10082 * Given a list of FunctionParameter nodes, extract a list of just the
10083 * argument types (TypeNames) for input parameters only. This is what
10084 * is needed to look up an existing function, which is what is wanted by
10085 * the productions that use this call.
10087 static List *
10088 extractArgTypes(List *parameters)
10090 List *result = NIL;
10091 ListCell *i;
10093 foreach(i, parameters)
10095 FunctionParameter *p = (FunctionParameter *) lfirst(i);
10097 if (p->mode != FUNC_PARAM_OUT && p->mode != FUNC_PARAM_TABLE)
10098 result = lappend(result, p->argType);
10100 return result;
10103 /* findLeftmostSelect()
10104 * Find the leftmost component SelectStmt in a set-operation parsetree.
10106 static SelectStmt *
10107 findLeftmostSelect(SelectStmt *node)
10109 while (node && node->op != SETOP_NONE)
10110 node = node->larg;
10111 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
10112 return node;
10115 /* insertSelectOptions()
10116 * Insert ORDER BY, etc into an already-constructed SelectStmt.
10118 * This routine is just to avoid duplicating code in SelectStmt productions.
10120 static void
10121 insertSelectOptions(SelectStmt *stmt,
10122 List *sortClause, List *lockingClause,
10123 Node *limitOffset, Node *limitCount,
10124 WithClause *withClause)
10126 Assert(IsA(stmt, SelectStmt));
10129 * Tests here are to reject constructs like
10130 * (SELECT foo ORDER BY bar) ORDER BY baz
10132 if (sortClause)
10134 if (stmt->sortClause)
10135 ereport(ERROR,
10136 (errcode(ERRCODE_SYNTAX_ERROR),
10137 errmsg("multiple ORDER BY clauses not allowed"),
10138 scanner_errposition(exprLocation((Node *) sortClause))));
10139 stmt->sortClause = sortClause;
10141 /* We can handle multiple locking clauses, though */
10142 stmt->lockingClause = list_concat(stmt->lockingClause, lockingClause);
10143 if (limitOffset)
10145 if (stmt->limitOffset)
10146 ereport(ERROR,
10147 (errcode(ERRCODE_SYNTAX_ERROR),
10148 errmsg("multiple OFFSET clauses not allowed"),
10149 scanner_errposition(exprLocation(limitOffset))));
10150 stmt->limitOffset = limitOffset;
10152 if (limitCount)
10154 if (stmt->limitCount)
10155 ereport(ERROR,
10156 (errcode(ERRCODE_SYNTAX_ERROR),
10157 errmsg("multiple LIMIT clauses not allowed"),
10158 scanner_errposition(exprLocation(limitCount))));
10159 stmt->limitCount = limitCount;
10161 if (withClause)
10163 if (stmt->withClause)
10164 ereport(ERROR,
10165 (errcode(ERRCODE_SYNTAX_ERROR),
10166 errmsg("multiple WITH clauses not allowed"),
10167 scanner_errposition(exprLocation((Node *) withClause))));
10168 stmt->withClause = withClause;
10172 static Node *
10173 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
10175 SelectStmt *n = makeNode(SelectStmt);
10177 n->op = op;
10178 n->all = all;
10179 n->larg = (SelectStmt *) larg;
10180 n->rarg = (SelectStmt *) rarg;
10181 return (Node *) n;
10184 /* SystemFuncName()
10185 * Build a properly-qualified reference to a built-in function.
10187 List *
10188 SystemFuncName(char *name)
10190 return list_make2(makeString("pg_catalog"), makeString(name));
10193 /* SystemTypeName()
10194 * Build a properly-qualified reference to a built-in type.
10196 * typmod is defaulted, but may be changed afterwards by caller.
10197 * Likewise for the location.
10199 TypeName *
10200 SystemTypeName(char *name)
10202 return makeTypeNameFromNameList(list_make2(makeString("pg_catalog"),
10203 makeString(name)));
10206 /* doNegate()
10207 * Handle negation of a numeric constant.
10209 * Formerly, we did this here because the optimizer couldn't cope with
10210 * indexquals that looked like "var = -4" --- it wants "var = const"
10211 * and a unary minus operator applied to a constant didn't qualify.
10212 * As of Postgres 7.0, that problem doesn't exist anymore because there
10213 * is a constant-subexpression simplifier in the optimizer. However,
10214 * there's still a good reason for doing this here, which is that we can
10215 * postpone committing to a particular internal representation for simple
10216 * negative constants. It's better to leave "-123.456" in string form
10217 * until we know what the desired type is.
10219 static Node *
10220 doNegate(Node *n, int location)
10222 if (IsA(n, A_Const))
10224 A_Const *con = (A_Const *)n;
10226 /* report the constant's location as that of the '-' sign */
10227 con->location = location;
10229 if (con->val.type == T_Integer)
10231 con->val.val.ival = -con->val.val.ival;
10232 return n;
10234 if (con->val.type == T_Float)
10236 doNegateFloat(&con->val);
10237 return n;
10241 return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n, location);
10244 static void
10245 doNegateFloat(Value *v)
10247 char *oldval = v->val.str;
10249 Assert(IsA(v, Float));
10250 if (*oldval == '+')
10251 oldval++;
10252 if (*oldval == '-')
10253 v->val.str = oldval+1; /* just strip the '-' */
10254 else
10256 char *newval = (char *) palloc(strlen(oldval) + 2);
10258 *newval = '-';
10259 strcpy(newval+1, oldval);
10260 v->val.str = newval;
10264 static Node *
10265 makeAArrayExpr(List *elements, int location)
10267 A_ArrayExpr *n = makeNode(A_ArrayExpr);
10269 n->elements = elements;
10270 n->location = location;
10271 return (Node *) n;
10274 static Node *
10275 makeXmlExpr(XmlExprOp op, char *name, List *named_args, List *args,
10276 int location)
10278 XmlExpr *x = makeNode(XmlExpr);
10280 x->op = op;
10281 x->name = name;
10283 * named_args is a list of ResTarget; it'll be split apart into separate
10284 * expression and name lists in transformXmlExpr().
10286 x->named_args = named_args;
10287 x->arg_names = NIL;
10288 x->args = args;
10289 /* xmloption, if relevant, must be filled in by caller */
10290 /* type and typmod will be filled in during parse analysis */
10291 x->location = location;
10292 return (Node *) x;
10295 /* parser_init()
10296 * Initialize to parse one query string
10298 void
10299 parser_init(void)
10301 QueryIsRule = FALSE;
10305 * Merge the input and output parameters of a table function.
10307 static List *
10308 mergeTableFuncParameters(List *func_args, List *columns)
10310 ListCell *lc;
10312 /* Explicit OUT and INOUT parameters shouldn't be used in this syntax */
10313 foreach(lc, func_args)
10315 FunctionParameter *p = (FunctionParameter *) lfirst(lc);
10317 if (p->mode != FUNC_PARAM_IN && p->mode != FUNC_PARAM_VARIADIC)
10318 ereport(ERROR,
10319 (errcode(ERRCODE_SYNTAX_ERROR),
10320 errmsg("OUT and INOUT arguments aren't allowed in TABLE functions")));
10323 return list_concat(func_args, columns);
10327 * Determine return type of a TABLE function. A single result column
10328 * returns setof that column's type; otherwise return setof record.
10330 static TypeName *
10331 TableFuncTypeName(List *columns)
10333 TypeName *result;
10335 if (list_length(columns) == 1)
10337 FunctionParameter *p = (FunctionParameter *) linitial(columns);
10339 result = (TypeName *) copyObject(p->argType);
10341 else
10342 result = SystemTypeName("record");
10344 result->setof = true;
10346 return result;
10350 * Must undefine base_yylex before including scan.c, since we want it
10351 * to create the function base_yylex not filtered_base_yylex.
10353 #undef base_yylex
10355 #include "scan.c"