4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
14 * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.231 2001/06/19 22:39:11 tgl Exp $
17 * AUTHOR DATE MAJOR EVENT
18 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
19 * Andrew Yu Oct, 1994 lispy code conversion
22 * CAPITALS are used to represent terminal symbols.
23 * non-capitals are used to represent non-terminals.
24 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
25 * to help isolate the non-extensible portions of the parser.
27 * In general, nothing in this file should initiate database accesses
28 * nor depend on changeable state (such as SET variables). If you do
29 * database accesses, your code will fail when we have aborted the
30 * current transaction and are just parsing commands to find the next
31 * ROLLBACK or COMMIT. If you make use of SET variables, then you
32 * will do the wrong thing in multi-query strings like this:
33 * SET SQL_inheritance TO off; SELECT * FROM foo;
34 * because the entire string is parsed by gram.y before the SET gets
35 * executed. Anything that depends on the database or changeable state
36 * should be handled inside parse_analyze() so that it happens at the
37 * right time not the wrong time. The handling of SQL_inheritance is
41 * If you use a list, make sure the datum is a node so that the printing
44 * Sometimes we assign constants to makeStrings. Make sure we don't free
47 *-------------------------------------------------------------------------
53 #include "access/htup.h"
54 #include "catalog/catname.h"
55 #include "catalog/pg_type.h"
56 #include "nodes/params.h"
57 #include "nodes/parsenodes.h"
58 #include "parser/gramparse.h"
59 #include "storage/lmgr.h"
60 #include "utils/acl.h"
61 #include "utils/numeric.h"
64 #include "mb/pg_wchar.h"
66 #define GetStandardEncoding() 0 /* SQL_ASCII */
67 #define GetStandardEncodingName() "SQL_ASCII"
70 extern List *parsetree; /* final parse result is delivered here */
72 static bool QueryIsRule = FALSE;
73 static Oid *param_type_info;
74 static int pfunc_num_args;
78 * If you need access to certain yacc-generated variables and find that
79 * they're static by default, uncomment the next line. (this is not a
82 /*#define __YYSCLASS*/
84 static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
85 static Node *makeTypeCast(Node *arg, TypeName *typename);
86 static Node *makeRowExpr(char *opr, List *largs, List *rargs);
87 static void mapTargetColumns(List *source, List *target);
88 static SelectStmt *findLeftmostSelect(SelectStmt *node);
89 static void insertSelectOptions(SelectStmt *stmt,
90 List *sortClause, List *forUpdate,
91 Node *limitOffset, Node *limitCount);
92 static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
93 static bool exprIsNullConstant(Node *arg);
94 static Node *doNegate(Node *n);
95 static void doNegateFloat(Value *v);
116 SortGroupBy *sortgroupby;
131 AlterGroupStmt, AlterSchemaStmt, AlterTableStmt, AlterUserStmt,
133 ClosePortalStmt, ClusterStmt, CommentStmt, ConstraintsSetStmt,
134 CopyStmt, CreateAsStmt, CreateGroupStmt, CreatePLangStmt,
135 CreateSchemaStmt, CreateSeqStmt, CreateStmt, CreateTrigStmt,
136 CreateUserStmt, CreatedbStmt, CursorStmt, DefineStmt, DeleteStmt,
137 DropGroupStmt, DropPLangStmt, DropSchemaStmt, DropStmt, DropTrigStmt,
138 DropUserStmt, DropdbStmt, ExplainStmt, ExtendStmt, FetchStmt,
139 GrantStmt, IndexStmt, InsertStmt, ListenStmt, LoadStmt, LockStmt,
140 NotifyStmt, OptimizableStmt, ProcedureStmt, ReindexStmt,
141 RemoveAggrStmt, RemoveFuncStmt, RemoveOperStmt,
142 RenameStmt, RevokeStmt, RuleActionStmt, RuleActionStmtOrEmpty,
143 RuleStmt, SelectStmt, TransactionStmt, TruncateStmt,
144 UnlistenStmt, UpdateStmt, VacuumStmt, VariableResetStmt,
145 VariableSetStmt, VariableShowStmt, ViewStmt, CheckPointStmt
147 %type <node> select_no_parens, select_with_parens, select_clause,
150 %type <node> alter_column_default
151 %type <ival> drop_behavior
153 %type <list> createdb_opt_list, createdb_opt_item
155 %type <ival> opt_lock, lock_type
156 %type <boolean> opt_lmode, opt_force
158 %type <ival> user_createdb_clause, user_createuser_clause
159 %type <str> user_passwd_clause
160 %type <ival> sysid_clause
161 %type <str> user_valid_clause
162 %type <list> user_list, user_group_clause, users_in_new_group_clause
164 %type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted, opt_procedural
166 %type <str> OptConstrFromTable
168 %type <str> TriggerEvents
169 %type <value> TriggerFuncArg
171 %type <str> relation_name, copy_file_name, copy_delimiter, copy_null,
172 database_name, access_method_clause, access_method, attr_name,
173 class, index_name, name, func_name, file_name
176 all_Op, MathOp, opt_name,
177 OptUseOp, opt_class, SpecialRuleRelation
179 %type <str> opt_level, opt_encoding
180 %type <str> privileges, operation_commalist
182 %type <list> grantee_list
183 %type <chr> operation, TriggerOneEvent
185 %type <list> stmtblock, stmtmulti,
186 into_clause, OptTempTableName, relation_name_list,
187 OptTableElementList, OptInherit, definition, opt_distinct,
188 opt_with, func_args, func_args_list, func_as,
189 oper_argtypes, RuleActionList, RuleActionMulti,
190 opt_column_list, columnList, opt_name_list,
191 sort_clause, sortby_list, index_params, index_list, name_list,
192 from_clause, from_list, opt_array_bounds,
193 expr_list, attrs, target_list, update_target_list,
194 def_list, opt_indirection, group_clause, TriggerFuncArgs,
195 select_limit, opt_select_limit
197 %type <typnam> func_arg, func_return, func_type, aggr_argtype
199 %type <boolean> opt_arg, TriggerForOpt, TriggerForType, OptTemp
201 %type <list> for_update_clause, opt_for_update_clause, update_list
202 %type <boolean> opt_all
203 %type <boolean> opt_table
204 %type <boolean> opt_chain, opt_trans
206 %type <node> join_outer, join_qual
207 %type <jtype> join_type
209 %type <list> extract_list, position_list
210 %type <list> substr_list, trim_list
211 %type <list> opt_interval
212 %type <node> substr_from, substr_for
214 %type <boolean> opt_binary, opt_using, opt_instead, opt_cursor
215 %type <boolean> opt_with_copy, index_opt_unique, opt_verbose, analyze_keyword
217 %type <ival> copy_dirn, direction, reindex_type, drop_type,
218 opt_column, event, comment_type, comment_cl,
219 comment_ag, comment_fn, comment_op, comment_tg
221 %type <ival> fetch_how_many
223 %type <node> select_limit_value, select_offset_value
225 %type <list> OptSeqList
226 %type <defelt> OptSeqElem
228 %type <istmt> insert_rest
230 %type <node> OptTableElement, ConstraintElem
231 %type <node> columnDef
232 %type <defelt> def_elem
233 %type <node> def_arg, columnElem, where_clause,
234 a_expr, b_expr, c_expr, AexprConst,
235 in_expr, having_clause
236 %type <list> row_descriptor, row_list, in_expr_nodes
237 %type <node> row_expr
238 %type <node> case_expr, case_arg, when_clause, case_default
239 %type <list> when_clause_list
240 %type <ival> sub_type
241 %type <list> OptCreateAs, CreateAsList
242 %type <node> CreateAsElement
243 %type <value> NumericOnly, FloatOnly, IntegerOnly
244 %type <attr> event_object, attr, alias_clause
245 %type <sortgroupby> sortby
246 %type <ielem> index_elem, func_index
247 %type <node> table_ref
248 %type <jexpr> joined_table
249 %type <range> relation_expr
250 %type <target> target_el, update_target_el
251 %type <paramno> ParamNo
253 %type <typnam> Typename, SimpleTypename, ConstTypename
254 GenericType, Numeric, Geometric, Character, ConstDatetime, ConstInterval, Bit
255 %type <str> character, datetime, bit
256 %type <str> extract_arg
257 %type <str> opt_charset, opt_collate
258 %type <str> opt_float
259 %type <ival> opt_numeric, opt_decimal
260 %type <boolean> opt_varying, opt_timezone
263 %type <str> Sconst, comment_text
264 %type <str> UserId, opt_boolean, var_value, zone_value, ColId_or_Sconst
265 %type <str> ColId, ColLabel, TokenId
267 %type <node> TableConstraint
268 %type <list> ColQualList
269 %type <node> ColConstraint, ColConstraintElem, ConstraintAttr
270 %type <ival> key_actions, key_delete, key_update, key_reference
271 %type <str> key_match
272 %type <ival> ConstraintAttributeSpec, ConstraintDeferrabilitySpec,
275 %type <list> constraints_set_list
276 %type <list> constraints_set_namelist
277 %type <boolean> constraints_set_mode
280 * If you make any token changes, remember to:
281 * - use "yacc -d" and update parse.h
282 * - update the keyword table in parser/keywords.c
285 /* Reserved word tokens
286 * SQL92 syntax has many type-specific constructs.
287 * So, go ahead and make these types reserved words,
288 * and call-out the syntax explicitly.
289 * This gets annoying when trying to also retain Postgres' nice
290 * type-extensible features, but we don't really have a choice.
291 * - thomas 1997-10-11
292 * NOTE: Whenever possible, try to add new keywords to the ColId list,
293 * or failing that, at least to the ColLabel list.
296 /* Keywords (in SQL92 reserved words) */
297 %token ABSOLUTE, ACTION, ADD, ALL, ALTER, AND, ANY, AS, ASC, AT, AUTHORIZATION,
298 BEGIN_TRANS, BETWEEN, BOTH, BY,
299 CASCADE, CASE, CAST, CHAR, CHARACTER, CHECK, CLOSE,
300 COALESCE, COLLATE, COLUMN, COMMIT,
301 CONSTRAINT, CONSTRAINTS, CREATE, CROSS, CURRENT_DATE,
302 CURRENT_TIME, CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
303 DAY_P, DEC, DECIMAL, DECLARE, DEFAULT, DELETE, DESC,
304 DISTINCT, DOUBLE, DROP,
305 ELSE, END_TRANS, ESCAPE, EXCEPT, EXECUTE, EXISTS, EXTRACT,
306 FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
307 GLOBAL, GRANT, GROUP, HAVING, HOUR_P,
308 IN, INNER_P, INSENSITIVE, INSERT, INTERSECT, INTERVAL, INTO, IS,
309 ISOLATION, JOIN, KEY, LANGUAGE, LEADING, LEFT, LEVEL, LIKE, LOCAL,
310 MATCH, MINUTE_P, MONTH_P, NAMES,
311 NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULLIF, NULL_P, NUMERIC,
312 OF, OLD, ON, ONLY, OPTION, OR, ORDER, OUTER_P, OVERLAPS,
313 PARTIAL, POSITION, PRECISION, PRIMARY, PRIOR, PRIVILEGES, PROCEDURE, PUBLIC,
314 READ, REFERENCES, RELATIVE, REVOKE, RIGHT, ROLLBACK,
315 SCHEMA, SCROLL, SECOND_P, SELECT, SESSION, SESSION_USER, SET, SOME, SUBSTRING,
316 TABLE, TEMPORARY, THEN, TIME, TIMESTAMP, TIMEZONE_HOUR,
317 TIMEZONE_MINUTE, TO, TRAILING, TRANSACTION, TRIM, TRUE_P,
318 UNION, UNIQUE, UNKNOWN, UPDATE, USER, USING,
319 VALUES, VARCHAR, VARYING, VIEW,
320 WHEN, WHERE, WITH, WORK, YEAR_P, ZONE
322 /* Keywords (in SQL3 reserved words) */
323 %token CHAIN, CHARACTERISTICS,
324 DEFERRABLE, DEFERRED,
325 IMMEDIATE, INITIALLY, INOUT,
332 /* Keywords (in SQL92 non-reserved words) */
333 %token COMMITTED, SERIALIZABLE, TYPE_P
335 /* Keywords for Postgres support (not in SQL92 reserved words)
337 * The CREATEDB and CREATEUSER tokens should go away
338 * when some sort of pg_privileges relation is introduced.
339 * - Todd A. Brandys 1998-01-01?
341 %token ABORT_TRANS, ACCESS, AFTER, AGGREGATE, ANALYSE, ANALYZE,
342 BACKWARD, BEFORE, BINARY, BIT,
343 CACHE, CHECKPOINT, CLUSTER, COMMENT, COPY, CREATEDB, CREATEUSER, CYCLE,
344 DATABASE, DELIMITERS, DO,
345 EACH, ENCODING, EXCLUSIVE, EXPLAIN, EXTEND,
346 FORCE, FORWARD, FUNCTION, HANDLER,
347 ILIKE, INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
348 LANCOMPILER, LIMIT, LISTEN, LOAD, LOCATION, LOCK_P,
349 MAXVALUE, MINVALUE, MODE, MOVE,
350 NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
351 OFFSET, OIDS, OPERATOR, OWNER, PASSWORD, PROCEDURAL,
352 REINDEX, RENAME, RESET, RETURNS, ROW, RULE,
353 SEQUENCE, SERIAL, SETOF, SHARE, SHOW, START, STATEMENT,
354 STATISTICS, STDIN, STDOUT, SYSID,
355 TEMP, TEMPLATE, TOAST, TRUNCATE, TRUSTED,
356 UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
358 /* The grammar thinks these are keywords, but they are not in the keywords.c
359 * list and so can never be entered directly. The filter in parser.c
360 * creates these tokens when required.
364 /* Special keywords, not in the query language - see the "lex" file */
365 %token <str> IDENT, FCONST, SCONST, BITCONST, Op
366 %token <ival> ICONST, PARAM
368 /* these are not real. they are here so that they get generated as #define's*/
371 /* precedence: lowest to highest */
374 %left JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
385 %left POSTFIXOP /* dummy for postfix Op rules */
386 %left Op /* multi-character ops and user-defined operators */
389 %nonassoc IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
393 /* Unary Operators */
394 %left AT ZONE /* sets precedence for AT TIME ZONE */
403 * Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
404 * psql already handles such cases, but other interfaces don't.
411 /* the thrashing around here is to discard "empty" statements... */
412 stmtmulti: stmtmulti ';' stmt
413 { if ($3 != (Node *)NULL)
414 $$ = lappend($1, $3);
419 { if ($1 != (Node *)NULL)
426 stmt : AlterSchemaStmt
481 { $$ = (Node *)NULL; }
484 /*****************************************************************************
486 * Create a new Postgres DBMS user
489 *****************************************************************************/
491 CreateUserStmt: CREATE USER UserId
492 user_createdb_clause user_createuser_clause user_group_clause
495 CreateUserStmt *n = makeNode(CreateUserStmt);
499 n->createdb = $4 == +1 ? TRUE : FALSE;
500 n->createuser = $5 == +1 ? TRUE : FALSE;
505 | CREATE USER UserId WITH sysid_clause user_passwd_clause
506 user_createdb_clause user_createuser_clause user_group_clause
509 CreateUserStmt *n = makeNode(CreateUserStmt);
513 n->createdb = $7 == +1 ? TRUE : FALSE;
514 n->createuser = $8 == +1 ? TRUE : FALSE;
521 /*****************************************************************************
523 * Alter a postgresql DBMS user
526 *****************************************************************************/
528 AlterUserStmt: ALTER USER UserId user_createdb_clause
529 user_createuser_clause user_valid_clause
531 AlterUserStmt *n = makeNode(AlterUserStmt);
539 | ALTER USER UserId WITH PASSWORD Sconst
541 user_createuser_clause user_valid_clause
543 AlterUserStmt *n = makeNode(AlterUserStmt);
553 /*****************************************************************************
555 * Drop a postgresql DBMS user
558 *****************************************************************************/
560 DropUserStmt: DROP USER user_list
562 DropUserStmt *n = makeNode(DropUserStmt);
568 user_passwd_clause: PASSWORD Sconst { $$ = $2; }
569 | /*EMPTY*/ { $$ = NULL; }
572 sysid_clause: SYSID Iconst
575 elog(ERROR, "sysid must be positive");
578 | /*EMPTY*/ { $$ = -1; }
581 user_createdb_clause: CREATEDB { $$ = +1; }
582 | NOCREATEDB { $$ = -1; }
583 | /*EMPTY*/ { $$ = 0; }
586 user_createuser_clause: CREATEUSER { $$ = +1; }
587 | NOCREATEUSER { $$ = -1; }
588 | /*EMPTY*/ { $$ = 0; }
591 user_list: user_list ',' UserId
593 $$ = lappend($1, makeString($3));
597 $$ = makeList1(makeString($1));
601 user_group_clause: IN GROUP user_list { $$ = $3; }
602 | /*EMPTY*/ { $$ = NULL; }
605 user_valid_clause: VALID UNTIL SCONST { $$ = $3; }
606 | /*EMPTY*/ { $$ = NULL; }
610 /*****************************************************************************
612 * Create a postgresql group
615 *****************************************************************************/
617 CreateGroupStmt: CREATE GROUP UserId
619 CreateGroupStmt *n = makeNode(CreateGroupStmt);
625 | CREATE GROUP UserId WITH sysid_clause users_in_new_group_clause
627 CreateGroupStmt *n = makeNode(CreateGroupStmt);
635 users_in_new_group_clause: USER user_list { $$ = $2; }
636 | /* EMPTY */ { $$ = NULL; }
639 /*****************************************************************************
641 * Alter a postgresql group
644 *****************************************************************************/
646 AlterGroupStmt: ALTER GROUP UserId ADD USER user_list
648 AlterGroupStmt *n = makeNode(AlterGroupStmt);
655 | ALTER GROUP UserId DROP USER user_list
657 AlterGroupStmt *n = makeNode(AlterGroupStmt);
666 /*****************************************************************************
668 * Drop a postgresql group
671 *****************************************************************************/
673 DropGroupStmt: DROP GROUP UserId
675 DropGroupStmt *n = makeNode(DropGroupStmt);
682 /*****************************************************************************
684 * Manipulate a schema
687 *****************************************************************************/
689 CreateSchemaStmt: CREATE SCHEMA UserId
691 /* for now, just make this the same as CREATE DATABASE */
692 CreatedbStmt *n = makeNode(CreatedbStmt);
695 n->dbtemplate = NULL;
701 AlterSchemaStmt: ALTER SCHEMA UserId
703 elog(ERROR, "ALTER SCHEMA not yet supported");
707 DropSchemaStmt: DROP SCHEMA UserId
709 DropdbStmt *n = makeNode(DropdbStmt);
715 /*****************************************************************************
717 * Set PG internal variable
718 * SET name TO 'var_value'
719 * Include SQL92 syntax (thomas 1997-10-22):
720 * SET TIME ZONE 'var_value'
722 *****************************************************************************/
724 VariableSetStmt: SET ColId TO var_value
726 VariableSetStmt *n = makeNode(VariableSetStmt);
731 | SET ColId '=' var_value
733 VariableSetStmt *n = makeNode(VariableSetStmt);
738 | SET TIME ZONE zone_value
740 VariableSetStmt *n = makeNode(VariableSetStmt);
741 n->name = "timezone";
745 | SET TRANSACTION ISOLATION LEVEL opt_level
747 VariableSetStmt *n = makeNode(VariableSetStmt);
748 n->name = "XactIsoLevel";
752 | SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL opt_level
754 VariableSetStmt *n = makeNode(VariableSetStmt);
755 n->name = "DefaultXactIsoLevel";
759 | SET NAMES opt_encoding
761 VariableSetStmt *n = makeNode(VariableSetStmt);
762 n->name = "client_encoding";
766 | SET SESSION AUTHORIZATION ColId_or_Sconst
768 VariableSetStmt *n = makeNode(VariableSetStmt);
769 n->name = "session_authorization";
775 opt_level: READ COMMITTED { $$ = "committed"; }
776 | SERIALIZABLE { $$ = "serializable"; }
779 var_value: opt_boolean { $$ = $1; }
780 | SCONST { $$ = $1; }
784 sprintf(buf, "%d", $1);
790 sprintf(buf, "%d", -($2));
793 | FCONST { $$ = $1; }
796 char * s = palloc(strlen($2)+2);
807 /* List of words? Then concatenate together */
809 elog(ERROR, "SET must have at least one argument");
813 Value *p = (Value *) lfirst(n);
814 Assert(IsA(p, String));
815 /* keep track of room for string and trailing comma */
816 slen += (strlen(p->val.str) + 1);
818 result = palloc(slen + 1);
822 Value *p = (Value *) lfirst(n);
823 strcat(result, p->val.str);
826 /* remove the trailing comma from the last element */
827 *(result+strlen(result)-1) = '\0';
830 | DEFAULT { $$ = NULL; }
833 opt_boolean: TRUE_P { $$ = "true"; }
834 | FALSE_P { $$ = "false"; }
836 | OFF { $$ = "off"; }
839 zone_value: Sconst { $$ = $1; }
840 | DEFAULT { $$ = NULL; }
841 | LOCAL { $$ = NULL; }
844 opt_encoding: Sconst { $$ = $1; }
845 | DEFAULT { $$ = NULL; }
846 | /*EMPTY*/ { $$ = NULL; }
849 ColId_or_Sconst: ColId { $$ = $1; }
850 | SCONST { $$ = $1; }
853 VariableShowStmt: SHOW ColId
855 VariableShowStmt *n = makeNode(VariableShowStmt);
861 VariableShowStmt *n = makeNode(VariableShowStmt);
862 n->name = "timezone";
867 VariableShowStmt *n = makeNode(VariableShowStmt);
871 | SHOW TRANSACTION ISOLATION LEVEL
873 VariableShowStmt *n = makeNode(VariableShowStmt);
874 n->name = "XactIsoLevel";
879 VariableResetStmt: RESET ColId
881 VariableResetStmt *n = makeNode(VariableResetStmt);
887 VariableResetStmt *n = makeNode(VariableResetStmt);
888 n->name = "timezone";
891 | RESET TRANSACTION ISOLATION LEVEL
893 VariableResetStmt *n = makeNode(VariableResetStmt);
894 n->name = "XactIsoLevel";
899 VariableResetStmt *n = makeNode(VariableResetStmt);
906 ConstraintsSetStmt: SET CONSTRAINTS constraints_set_list constraints_set_mode
908 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
916 constraints_set_list: ALL
920 | constraints_set_namelist
927 constraints_set_namelist: IDENT
931 | constraints_set_namelist ',' IDENT
933 $$ = lappend($1, $3);
938 constraints_set_mode: DEFERRED
950 * Checkpoint statement
952 CheckPointStmt: CHECKPOINT
954 CheckPointStmt *n = makeNode(CheckPointStmt);
959 /*****************************************************************************
961 * ALTER TABLE variations
963 *****************************************************************************/
966 /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
967 ALTER TABLE relation_expr ADD opt_column columnDef
969 AlterTableStmt *n = makeNode(AlterTableStmt);
971 n->relname = $3->relname;
972 n->inhOpt = $3->inhOpt;
976 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
977 | ALTER TABLE relation_expr ALTER opt_column ColId alter_column_default
979 AlterTableStmt *n = makeNode(AlterTableStmt);
981 n->relname = $3->relname;
982 n->inhOpt = $3->inhOpt;
987 /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <Iconst> */
988 | ALTER TABLE relation_expr ALTER opt_column ColId SET STATISTICS Iconst
990 AlterTableStmt *n = makeNode(AlterTableStmt);
992 n->relname = $3->relname;
993 n->inhOpt = $3->inhOpt;
995 n->def = (Node *) makeInteger($9);
998 /* ALTER TABLE <relation> DROP [COLUMN] <colname> {RESTRICT|CASCADE} */
999 | ALTER TABLE relation_expr DROP opt_column ColId drop_behavior
1001 AlterTableStmt *n = makeNode(AlterTableStmt);
1003 n->relname = $3->relname;
1004 n->inhOpt = $3->inhOpt;
1009 /* ALTER TABLE <relation> ADD CONSTRAINT ... */
1010 | ALTER TABLE relation_expr ADD TableConstraint
1012 AlterTableStmt *n = makeNode(AlterTableStmt);
1014 n->relname = $3->relname;
1015 n->inhOpt = $3->inhOpt;
1019 /* ALTER TABLE <relation> DROP CONSTRAINT <name> {RESTRICT|CASCADE} */
1020 | ALTER TABLE relation_expr DROP CONSTRAINT name drop_behavior
1022 AlterTableStmt *n = makeNode(AlterTableStmt);
1024 n->relname = $3->relname;
1025 n->inhOpt = $3->inhOpt;
1030 /* ALTER TABLE <name> CREATE TOAST TABLE */
1031 | ALTER TABLE relation_name CREATE TOAST TABLE
1033 AlterTableStmt *n = makeNode(AlterTableStmt);
1039 /* ALTER TABLE <name> OWNER TO UserId */
1040 | ALTER TABLE relation_name OWNER TO UserId
1042 AlterTableStmt *n = makeNode(AlterTableStmt);
1051 alter_column_default:
1054 /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
1055 if (exprIsNullConstant($3))
1060 | DROP DEFAULT { $$ = NULL; }
1063 drop_behavior: CASCADE { $$ = CASCADE; }
1064 | RESTRICT { $$ = RESTRICT; }
1069 /*****************************************************************************
1074 *****************************************************************************/
1076 ClosePortalStmt: CLOSE opt_id
1078 ClosePortalStmt *n = makeNode(ClosePortalStmt);
1084 opt_id: ColId { $$ = $1; }
1085 | /*EMPTY*/ { $$ = NULL; }
1089 /*****************************************************************************
1092 * COPY [BINARY] <relname> FROM/TO
1093 * [USING DELIMITERS <delimiter>]
1095 *****************************************************************************/
1097 CopyStmt: COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter copy_null
1099 CopyStmt *n = makeNode(CopyStmt);
1118 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
1119 * used depends on the direction. (It really doesn't make sense to copy from
1120 * stdout. We silently correct the "typo". - AY 9/94
1122 copy_file_name: Sconst { $$ = $1; }
1123 | STDIN { $$ = NULL; }
1124 | STDOUT { $$ = NULL; }
1127 opt_binary: BINARY { $$ = TRUE; }
1128 | /*EMPTY*/ { $$ = FALSE; }
1131 opt_with_copy: WITH OIDS { $$ = TRUE; }
1132 | /*EMPTY*/ { $$ = FALSE; }
1136 * the default copy delimiter is tab but the user can configure it
1138 copy_delimiter: opt_using DELIMITERS Sconst { $$ = $3; }
1139 | /*EMPTY*/ { $$ = "\t"; }
1142 opt_using: USING { $$ = TRUE; }
1143 | /*EMPTY*/ { $$ = TRUE; }
1146 copy_null: WITH NULL_P AS Sconst { $$ = $4; }
1147 | /*EMPTY*/ { $$ = "\\N"; }
1149 /*****************************************************************************
1154 *****************************************************************************/
1156 CreateStmt: CREATE OptTemp TABLE relation_name '(' OptTableElementList ')' OptInherit
1158 CreateStmt *n = makeNode(CreateStmt);
1162 n->inhRelnames = $8;
1163 n->constraints = NIL;
1169 * Redundancy here is needed to avoid shift/reduce conflicts,
1170 * since TEMP is not a reserved word. See also OptTempTableName.
1172 OptTemp: TEMPORARY { $$ = TRUE; }
1173 | TEMP { $$ = TRUE; }
1174 | LOCAL TEMPORARY { $$ = TRUE; }
1175 | LOCAL TEMP { $$ = TRUE; }
1178 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
1183 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
1186 | /*EMPTY*/ { $$ = FALSE; }
1189 OptTableElementList: OptTableElementList ',' OptTableElement
1192 $$ = lappend($1, $3);
1203 | /*EMPTY*/ { $$ = NIL; }
1206 OptTableElement: columnDef { $$ = $1; }
1207 | TableConstraint { $$ = $1; }
1210 columnDef: ColId Typename ColQualList opt_collate
1212 ColumnDef *n = makeNode(ColumnDef);
1215 n->constraints = $3;
1218 elog(NOTICE,"CREATE TABLE/COLLATE %s not yet implemented"
1219 "; clause ignored", $4);
1223 | ColId SERIAL ColQualList opt_collate
1225 ColumnDef *n = makeNode(ColumnDef);
1227 n->typename = makeNode(TypeName);
1228 n->typename->name = xlateSqlType("integer");
1229 n->typename->typmod = -1;
1230 n->is_sequence = TRUE;
1231 n->constraints = $3;
1234 elog(NOTICE,"CREATE TABLE/COLLATE %s not yet implemented"
1235 "; clause ignored", $4);
1241 ColQualList: ColQualList ColConstraint { $$ = lappend($1, $2); }
1242 | /*EMPTY*/ { $$ = NIL; }
1246 CONSTRAINT name ColConstraintElem
1248 switch (nodeTag($3))
1252 Constraint *n = (Constraint *)$3;
1256 case T_FkConstraint:
1258 FkConstraint *n = (FkConstraint *)$3;
1259 n->constr_name = $2;
1273 /* DEFAULT NULL is already the default for Postgres.
1274 * But define it here and carry it forward into the system
1275 * to make it explicit.
1276 * - thomas 1998-09-13
1278 * WITH NULL and NULL are not SQL92-standard syntax elements,
1279 * so leave them out. Use DEFAULT NULL to explicitly indicate
1280 * that a column may have that value. WITH NULL leads to
1281 * shift/reduce conflicts with WITH TIME ZONE anyway.
1282 * - thomas 1999-01-08
1284 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
1285 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
1286 * or be part of a_expr NOT LIKE or similar constructs).
1291 Constraint *n = makeNode(Constraint);
1292 n->contype = CONSTR_NOTNULL;
1295 n->cooked_expr = NULL;
1301 Constraint *n = makeNode(Constraint);
1302 n->contype = CONSTR_NULL;
1305 n->cooked_expr = NULL;
1311 Constraint *n = makeNode(Constraint);
1312 n->contype = CONSTR_UNIQUE;
1315 n->cooked_expr = NULL;
1321 Constraint *n = makeNode(Constraint);
1322 n->contype = CONSTR_PRIMARY;
1325 n->cooked_expr = NULL;
1329 | CHECK '(' a_expr ')'
1331 Constraint *n = makeNode(Constraint);
1332 n->contype = CONSTR_CHECK;
1335 n->cooked_expr = NULL;
1341 Constraint *n = makeNode(Constraint);
1342 n->contype = CONSTR_DEFAULT;
1344 if (exprIsNullConstant($2))
1346 /* DEFAULT NULL should be reported as empty expr */
1353 n->cooked_expr = NULL;
1357 | REFERENCES ColId opt_column_list key_match key_actions
1359 FkConstraint *n = makeNode(FkConstraint);
1360 n->constr_name = NULL;
1361 n->pktable_name = $2;
1366 n->deferrable = FALSE;
1367 n->initdeferred = FALSE;
1373 * ConstraintAttr represents constraint attributes, which we parse as if
1374 * they were independent constraint clauses, in order to avoid shift/reduce
1375 * conflicts (since NOT might start either an independent NOT NULL clause
1376 * or an attribute). analyze.c is responsible for attaching the attribute
1377 * information to the preceding "real" constraint node, and for complaining
1378 * if attribute clauses appear in the wrong place or wrong combinations.
1380 * See also ConstraintAttributeSpec, which can be used in places where
1381 * there is no parsing conflict.
1383 ConstraintAttr: DEFERRABLE
1385 Constraint *n = makeNode(Constraint);
1386 n->contype = CONSTR_ATTR_DEFERRABLE;
1391 Constraint *n = makeNode(Constraint);
1392 n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
1395 | INITIALLY DEFERRED
1397 Constraint *n = makeNode(Constraint);
1398 n->contype = CONSTR_ATTR_DEFERRED;
1401 | INITIALLY IMMEDIATE
1403 Constraint *n = makeNode(Constraint);
1404 n->contype = CONSTR_ATTR_IMMEDIATE;
1410 /* ConstraintElem specifies constraint syntax which is not embedded into
1411 * a column definition. ColConstraintElem specifies the embedded form.
1412 * - thomas 1997-12-03
1414 TableConstraint: CONSTRAINT name ConstraintElem
1416 switch (nodeTag($3))
1420 Constraint *n = (Constraint *)$3;
1424 case T_FkConstraint:
1426 FkConstraint *n = (FkConstraint *)$3;
1427 n->constr_name = $2;
1439 ConstraintElem: CHECK '(' a_expr ')'
1441 Constraint *n = makeNode(Constraint);
1442 n->contype = CONSTR_CHECK;
1445 n->cooked_expr = NULL;
1448 | UNIQUE '(' columnList ')'
1450 Constraint *n = makeNode(Constraint);
1451 n->contype = CONSTR_UNIQUE;
1454 n->cooked_expr = NULL;
1458 | PRIMARY KEY '(' columnList ')'
1460 Constraint *n = makeNode(Constraint);
1461 n->contype = CONSTR_PRIMARY;
1464 n->cooked_expr = NULL;
1468 | FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list
1469 key_match key_actions ConstraintAttributeSpec
1471 FkConstraint *n = makeNode(FkConstraint);
1472 n->constr_name = NULL;
1473 n->pktable_name = $7;
1478 n->deferrable = ($11 & 1) != 0;
1479 n->initdeferred = ($11 & 2) != 0;
1484 key_match: MATCH FULL
1490 elog(ERROR, "FOREIGN KEY/MATCH PARTIAL not yet implemented");
1499 key_actions: key_delete { $$ = $1; }
1500 | key_update { $$ = $1; }
1501 | key_delete key_update { $$ = $1 | $2; }
1502 | key_update key_delete { $$ = $1 | $2; }
1503 | /*EMPTY*/ { $$ = 0; }
1506 key_delete: ON DELETE key_reference { $$ = $3 << FKCONSTR_ON_DELETE_SHIFT; }
1509 key_update: ON UPDATE key_reference { $$ = $3 << FKCONSTR_ON_UPDATE_SHIFT; }
1512 key_reference: NO ACTION { $$ = FKCONSTR_ON_KEY_NOACTION; }
1513 | RESTRICT { $$ = FKCONSTR_ON_KEY_RESTRICT; }
1514 | CASCADE { $$ = FKCONSTR_ON_KEY_CASCADE; }
1515 | SET NULL_P { $$ = FKCONSTR_ON_KEY_SETNULL; }
1516 | SET DEFAULT { $$ = FKCONSTR_ON_KEY_SETDEFAULT; }
1519 OptInherit: INHERITS '(' relation_name_list ')' { $$ = $3; }
1520 | /*EMPTY*/ { $$ = NIL; }
1524 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
1528 CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SelectStmt
1531 * When the SelectStmt is a set-operation tree, we must
1532 * stuff the INTO information into the leftmost component
1533 * Select, because that's where analyze.c will expect
1534 * to find it. Similarly, the output column names must
1535 * be attached to that Select's target list.
1537 SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
1538 if (n->into != NULL)
1539 elog(ERROR,"CREATE TABLE/AS SELECT may not specify INTO");
1543 mapTargetColumns($5, n->targetList);
1548 OptCreateAs: '(' CreateAsList ')' { $$ = $2; }
1549 | /*EMPTY*/ { $$ = NIL; }
1552 CreateAsList: CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1553 | CreateAsElement { $$ = makeList1($1); }
1556 CreateAsElement: ColId
1558 ColumnDef *n = makeNode(ColumnDef);
1561 n->raw_default = NULL;
1562 n->cooked_default = NULL;
1563 n->is_not_null = FALSE;
1564 n->constraints = NULL;
1570 /*****************************************************************************
1573 * CREATE SEQUENCE seqname
1575 *****************************************************************************/
1577 CreateSeqStmt: CREATE SEQUENCE relation_name OptSeqList
1579 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1586 OptSeqList: OptSeqList OptSeqElem
1587 { $$ = lappend($1, $2); }
1591 OptSeqElem: CACHE IntegerOnly
1593 $$ = makeNode(DefElem);
1594 $$->defname = "cache";
1595 $$->arg = (Node *)$2;
1599 $$ = makeNode(DefElem);
1600 $$->defname = "cycle";
1601 $$->arg = (Node *)NULL;
1603 | INCREMENT IntegerOnly
1605 $$ = makeNode(DefElem);
1606 $$->defname = "increment";
1607 $$->arg = (Node *)$2;
1609 | MAXVALUE IntegerOnly
1611 $$ = makeNode(DefElem);
1612 $$->defname = "maxvalue";
1613 $$->arg = (Node *)$2;
1615 | MINVALUE IntegerOnly
1617 $$ = makeNode(DefElem);
1618 $$->defname = "minvalue";
1619 $$->arg = (Node *)$2;
1623 $$ = makeNode(DefElem);
1624 $$->defname = "start";
1625 $$->arg = (Node *)$2;
1629 NumericOnly: FloatOnly { $$ = $1; }
1630 | IntegerOnly { $$ = $1; }
1645 $$ = makeInteger($1);
1649 $$ = makeInteger($2);
1650 $$->val.ival = - $$->val.ival;
1654 /*****************************************************************************
1657 * CREATE PROCEDURAL LANGUAGE ...
1658 * DROP PROCEDURAL LANGUAGE ...
1660 *****************************************************************************/
1662 CreatePLangStmt: CREATE PLangTrusted opt_procedural LANGUAGE Sconst
1663 HANDLER func_name LANCOMPILER Sconst
1665 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1674 PLangTrusted: TRUSTED { $$ = TRUE; }
1675 | /*EMPTY*/ { $$ = FALSE; }
1678 DropPLangStmt: DROP opt_procedural LANGUAGE Sconst
1680 DropPLangStmt *n = makeNode(DropPLangStmt);
1686 opt_procedural: PROCEDURAL { $$ = TRUE; }
1687 | /*EMPTY*/ { $$ = TRUE; }
1690 /*****************************************************************************
1693 * CREATE TRIGGER ...
1696 *****************************************************************************/
1698 CreateTrigStmt: CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1699 relation_name TriggerForSpec EXECUTE PROCEDURE
1700 name '(' TriggerFuncArgs ')'
1702 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1709 memcpy (n->actions, $5, 4);
1710 n->lang = NULL; /* unused */
1711 n->text = NULL; /* unused */
1712 n->attr = NULL; /* unused */
1713 n->when = NULL; /* unused */
1715 n->isconstraint = FALSE;
1716 n->deferrable = FALSE;
1717 n->initdeferred = FALSE;
1718 n->constrrelname = NULL;
1721 | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
1722 relation_name OptConstrFromTable
1723 ConstraintAttributeSpec
1724 FOR EACH ROW EXECUTE PROCEDURE name '(' TriggerFuncArgs ')'
1726 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1733 memcpy (n->actions, $6, 4);
1734 n->lang = NULL; /* unused */
1735 n->text = NULL; /* unused */
1736 n->attr = NULL; /* unused */
1737 n->when = NULL; /* unused */
1739 n->isconstraint = TRUE;
1740 n->deferrable = ($10 & 1) != 0;
1741 n->initdeferred = ($10 & 2) != 0;
1743 n->constrrelname = $9;
1748 TriggerActionTime: BEFORE { $$ = TRUE; }
1749 | AFTER { $$ = FALSE; }
1752 TriggerEvents: TriggerOneEvent
1754 char *e = palloc (4);
1755 e[0] = $1; e[1] = 0; $$ = e;
1757 | TriggerOneEvent OR TriggerOneEvent
1759 char *e = palloc (4);
1760 e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
1762 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
1764 char *e = palloc (4);
1765 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
1770 TriggerOneEvent: INSERT { $$ = 'i'; }
1771 | DELETE { $$ = 'd'; }
1772 | UPDATE { $$ = 'u'; }
1775 TriggerForSpec: FOR TriggerForOpt TriggerForType
1781 TriggerForOpt: EACH { $$ = TRUE; }
1782 | /*EMPTY*/ { $$ = FALSE; }
1785 TriggerForType: ROW { $$ = TRUE; }
1786 | STATEMENT { $$ = FALSE; }
1789 TriggerFuncArgs: TriggerFuncArg
1790 { $$ = makeList1($1); }
1791 | TriggerFuncArgs ',' TriggerFuncArg
1792 { $$ = lappend($1, $3); }
1797 TriggerFuncArg: ICONST
1800 sprintf (buf, "%d", $1);
1801 $$ = makeString(pstrdup(buf));
1805 $$ = makeString($1);
1809 $$ = makeString($1);
1813 $$ = makeString($1);
1817 $$ = makeString($1);
1821 OptConstrFromTable: /* Empty */
1825 | FROM relation_name
1831 ConstraintAttributeSpec: ConstraintDeferrabilitySpec
1833 | ConstraintDeferrabilitySpec ConstraintTimeSpec
1835 if ($1 == 0 && $2 != 0)
1836 elog(ERROR, "INITIALLY DEFERRED constraint must be DEFERRABLE");
1839 | ConstraintTimeSpec
1846 | ConstraintTimeSpec ConstraintDeferrabilitySpec
1848 if ($2 == 0 && $1 != 0)
1849 elog(ERROR, "INITIALLY DEFERRED constraint must be DEFERRABLE");
1856 ConstraintDeferrabilitySpec: NOT DEFERRABLE
1862 ConstraintTimeSpec: INITIALLY IMMEDIATE
1864 | INITIALLY DEFERRED
1869 DropTrigStmt: DROP TRIGGER name ON relation_name
1871 DropTrigStmt *n = makeNode(DropTrigStmt);
1879 /*****************************************************************************
1882 * define (aggregate,operator,type)
1884 *****************************************************************************/
1886 DefineStmt: CREATE AGGREGATE func_name definition
1888 DefineStmt *n = makeNode(DefineStmt);
1889 n->defType = AGGREGATE;
1894 | CREATE OPERATOR all_Op definition
1896 DefineStmt *n = makeNode(DefineStmt);
1897 n->defType = OPERATOR;
1902 | CREATE TYPE_P name definition
1904 DefineStmt *n = makeNode(DefineStmt);
1905 n->defType = TYPE_P;
1912 definition: '(' def_list ')' { $$ = $2; }
1915 def_list: def_elem { $$ = makeList1($1); }
1916 | def_list ',' def_elem { $$ = lappend($1, $3); }
1919 def_elem: ColLabel '=' def_arg
1921 $$ = makeNode(DefElem);
1923 $$->arg = (Node *)$3;
1927 $$ = makeNode(DefElem);
1929 $$->arg = (Node *)NULL;
1933 def_arg: func_return { $$ = (Node *)$1; }
1934 | TokenId { $$ = (Node *)makeString($1); }
1935 | all_Op { $$ = (Node *)makeString($1); }
1936 | NumericOnly { $$ = (Node *)$1; }
1937 | Sconst { $$ = (Node *)makeString($1); }
1941 /*****************************************************************************
1945 * DROP itemtype itemname [, itemname ...]
1947 *****************************************************************************/
1949 DropStmt: DROP drop_type name_list
1951 DropStmt *n = makeNode(DropStmt);
1958 drop_type: TABLE { $$ = DROP_TABLE; }
1959 | SEQUENCE { $$ = DROP_SEQUENCE; }
1960 | VIEW { $$ = DROP_VIEW; }
1961 | INDEX { $$ = DROP_INDEX; }
1962 | RULE { $$ = DROP_RULE; }
1963 | TYPE_P { $$ = DROP_TYPE_P; }
1966 /*****************************************************************************
1969 * truncate table relname
1971 *****************************************************************************/
1973 TruncateStmt: TRUNCATE opt_table relation_name
1975 TruncateStmt *n = makeNode(TruncateStmt);
1981 /*****************************************************************************
1983 * The COMMENT ON statement can take different forms based upon the type of
1984 * the object associated with the comment. The form of the statement is:
1986 * COMMENT ON [ [ DATABASE | INDEX | RULE | SEQUENCE | TABLE | TYPE | VIEW ]
1987 * <objname> | AGGREGATE <aggname> <aggtype> | FUNCTION
1988 * <funcname> (arg1, arg2, ...) | OPERATOR <op>
1989 * (leftoperand_typ rightoperand_typ) | TRIGGER <triggername> ON
1990 * <relname> ] IS 'text'
1992 *****************************************************************************/
1994 CommentStmt: COMMENT ON comment_type name IS comment_text
1996 CommentStmt *n = makeNode(CommentStmt);
1999 n->objproperty = NULL;
2004 | COMMENT ON comment_cl relation_name '.' attr_name IS comment_text
2006 CommentStmt *n = makeNode(CommentStmt);
2009 n->objproperty = $6;
2014 | COMMENT ON comment_ag name aggr_argtype IS comment_text
2016 CommentStmt *n = makeNode(CommentStmt);
2019 n->objproperty = NULL;
2020 n->objlist = makeList1($5);
2024 | COMMENT ON comment_fn func_name func_args IS comment_text
2026 CommentStmt *n = makeNode(CommentStmt);
2029 n->objproperty = NULL;
2034 | COMMENT ON comment_op all_Op '(' oper_argtypes ')' IS comment_text
2036 CommentStmt *n = makeNode(CommentStmt);
2039 n->objproperty = NULL;
2044 | COMMENT ON comment_tg name ON relation_name IS comment_text
2046 CommentStmt *n = makeNode(CommentStmt);
2049 n->objproperty = $6;
2056 comment_type: DATABASE { $$ = DATABASE; }
2057 | INDEX { $$ = INDEX; }
2058 | RULE { $$ = RULE; }
2059 | SEQUENCE { $$ = SEQUENCE; }
2060 | TABLE { $$ = TABLE; }
2061 | TYPE_P { $$ = TYPE_P; }
2062 | VIEW { $$ = VIEW; }
2065 comment_cl: COLUMN { $$ = COLUMN; }
2068 comment_ag: AGGREGATE { $$ = AGGREGATE; }
2071 comment_fn: FUNCTION { $$ = FUNCTION; }
2074 comment_op: OPERATOR { $$ = OPERATOR; }
2077 comment_tg: TRIGGER { $$ = TRIGGER; }
2080 comment_text: Sconst { $$ = $1; }
2081 | NULL_P { $$ = NULL; }
2084 /*****************************************************************************
2087 * fetch/move [forward | backward] [ # | all ] [ in <portalname> ]
2088 * fetch [ forward | backward | absolute | relative ]
2089 * [ # | all | next | prior ] [ [ in | from ] <portalname> ]
2091 *****************************************************************************/
2093 FetchStmt: FETCH direction fetch_how_many from_in name
2095 FetchStmt *n = makeNode(FetchStmt);
2099 elog(ERROR,"FETCH/RELATIVE at current position is not supported");
2105 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2113 | FETCH fetch_how_many from_in name
2115 FetchStmt *n = makeNode(FetchStmt);
2119 n->direction = BACKWARD;
2123 n->direction = FORWARD;
2130 | FETCH direction from_in name
2132 FetchStmt *n = makeNode(FetchStmt);
2143 | FETCH from_in name
2145 FetchStmt *n = makeNode(FetchStmt);
2146 n->direction = FORWARD;
2154 FetchStmt *n = makeNode(FetchStmt);
2155 n->direction = FORWARD;
2162 | MOVE direction fetch_how_many from_in name
2164 FetchStmt *n = makeNode(FetchStmt);
2168 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
2176 | MOVE fetch_how_many from_in name
2178 FetchStmt *n = makeNode(FetchStmt);
2182 n->direction = BACKWARD;
2186 n->direction = FORWARD;
2193 | MOVE direction from_in name
2195 FetchStmt *n = makeNode(FetchStmt);
2204 FetchStmt *n = makeNode(FetchStmt);
2205 n->direction = FORWARD;
2213 FetchStmt *n = makeNode(FetchStmt);
2214 n->direction = FORWARD;
2222 direction: FORWARD { $$ = FORWARD; }
2223 | BACKWARD { $$ = BACKWARD; }
2224 | RELATIVE { $$ = RELATIVE; }
2227 elog(NOTICE,"FETCH/ABSOLUTE not supported, using RELATIVE");
2232 fetch_how_many: Iconst { $$ = $1; }
2233 | '-' Iconst { $$ = - $2; }
2234 | ALL { $$ = 0; /* 0 means fetch all tuples*/ }
2236 | PRIOR { $$ = -1; }
2244 /*****************************************************************************
2246 * GRANT privileges ON [TABLE] relation_name_list TO [GROUP] grantee, ...
2248 *****************************************************************************/
2250 GrantStmt: GRANT privileges ON opt_table relation_name_list TO grantee_list opt_with_grant
2252 GrantStmt *n = makeNode(GrantStmt);
2261 privileges: ALL PRIVILEGES
2263 $$ = aclmakepriv(ACL_MODE_STR,0);
2267 $$ = aclmakepriv(ACL_MODE_STR,0);
2269 | operation_commalist
2275 operation_commalist: operation
2277 $$ = aclmakepriv("",$1);
2279 | operation_commalist ',' operation
2281 $$ = aclmakepriv($1,$3);
2287 $$ = ACL_MODE_SELECT_CHR;
2291 $$ = ACL_MODE_INSERT_CHR;
2295 $$ = ACL_MODE_UPDATE_CHR;
2299 $$ = ACL_MODE_DELETE_CHR;
2303 $$ = ACL_MODE_RULE_CHR;
2307 $$ = ACL_MODE_REFERENCES_CHR;
2311 $$ = ACL_MODE_TRIGGER_CHR;
2317 PrivGrantee *n = makeNode(PrivGrantee);
2319 n->groupname = NULL;
2324 PrivGrantee *n = makeNode(PrivGrantee);
2331 PrivGrantee *n = makeNode(PrivGrantee);
2333 n->groupname = NULL;
2338 grantee_list: grantee { $$ = makeList1($1); }
2339 | grantee_list ',' grantee { $$ = lappend($1, $3); }
2342 opt_with_grant: WITH GRANT OPTION
2344 elog(ERROR,"WITH GRANT OPTION is not supported. Only relation owners can set privileges");
2350 /*****************************************************************************
2352 * REVOKE privileges ON [TABLE] relation_name_list FROM user, ...
2354 *****************************************************************************/
2356 RevokeStmt: REVOKE privileges ON opt_table relation_name_list FROM grantee_list
2358 GrantStmt *n = makeNode(GrantStmt);
2359 n->is_grant = false;
2368 /*****************************************************************************
2371 * create index <indexname> on <relname>
2372 * using <access> "(" (<col> with <op>)+ ")" [with
2375 * [where <qual>] is not supported anymore
2376 *****************************************************************************/
2378 IndexStmt: CREATE index_opt_unique INDEX index_name ON relation_name
2379 access_method_clause '(' index_params ')' opt_with
2381 IndexStmt *n = makeNode(IndexStmt);
2385 n->accessMethod = $7;
2386 n->indexParams = $9;
2387 n->withClause = $11;
2388 n->whereClause = NULL;
2393 index_opt_unique: UNIQUE { $$ = TRUE; }
2394 | /*EMPTY*/ { $$ = FALSE; }
2397 access_method_clause: USING access_method { $$ = $2; }
2398 | /*EMPTY*/ { $$ = "btree"; }
2401 index_params: index_list { $$ = $1; }
2402 | func_index { $$ = makeList1($1); }
2405 index_list: index_list ',' index_elem { $$ = lappend($1, $3); }
2406 | index_elem { $$ = makeList1($1); }
2409 func_index: func_name '(' name_list ')' opt_class
2411 $$ = makeNode(IndexElem);
2418 index_elem: attr_name opt_class
2420 $$ = makeNode(IndexElem);
2430 * Release 7.0 removed network_ops, timespan_ops, and
2431 * datetime_ops, so we suppress it from being passed to
2432 * the parser so the default *_ops is used. This can be
2433 * removed in some later release. bjm 2000/02/07
2435 * Release 7.1 removes lztext_ops, so suppress that too
2436 * for a while. tgl 2000/07/30
2438 if (strcmp($1, "network_ops") != 0 &&
2439 strcmp($1, "timespan_ops") != 0 &&
2440 strcmp($1, "datetime_ops") != 0 &&
2441 strcmp($1, "lztext_ops") != 0)
2446 | USING class { $$ = $2; }
2447 | /*EMPTY*/ { $$ = NULL; }
2451 /*****************************************************************************
2454 * extend index <indexname> [where <qual>]
2456 *****************************************************************************/
2458 ExtendStmt: EXTEND INDEX index_name where_clause
2460 ExtendStmt *n = makeNode(ExtendStmt);
2462 n->whereClause = $4;
2467 /*****************************************************************************
2470 * execute recipe <recipeName>
2472 *****************************************************************************/
2475 RecipeStmt: EXECUTE RECIPE recipe_name
2477 RecipeStmt *n = makeNode(RecipeStmt);
2484 /*****************************************************************************
2487 * define function <fname>
2488 * [(<type-1> { , <type-n>})]
2490 * as <filename or code in language as appropriate>
2491 * language <lang> [with
2492 * [ arch_pct = <percentage | pre-defined>]
2493 * [, disk_pct = <percentage | pre-defined>]
2494 * [, byte_pct = <percentage | pre-defined>]
2495 * [, perbyte_cpu = <int | pre-defined>]
2496 * [, percall_cpu = <int | pre-defined>]
2499 *****************************************************************************/
2501 ProcedureStmt: CREATE FUNCTION func_name func_args
2502 RETURNS func_return AS func_as LANGUAGE Sconst opt_with
2504 ProcedureStmt *n = makeNode(ProcedureStmt);
2507 n->returnType = (Node *)$6;
2508 n->withClause = $11;
2514 opt_with: WITH definition { $$ = $2; }
2515 | /*EMPTY*/ { $$ = NIL; }
2518 func_args: '(' func_args_list ')' { $$ = $2; }
2519 | '(' ')' { $$ = NIL; }
2522 func_args_list: func_arg
2523 { $$ = makeList1($1); }
2524 | func_args_list ',' func_arg
2525 { $$ = lappend($1, $3); }
2528 func_arg: opt_arg func_type
2530 /* We can catch over-specified arguments here if we want to,
2531 * but for now better to silently swallow typmod, etc.
2532 * - thomas 2000-03-22
2548 elog(ERROR, "CREATE FUNCTION/OUT parameters are not supported");
2553 elog(ERROR, "CREATE FUNCTION/INOUT parameters are not supported");
2559 { $$ = makeList1(makeString($1)); }
2561 { $$ = makeList2(makeString($1), makeString($3)); }
2564 func_return: func_type
2566 /* We can catch over-specified arguments here if we want to,
2567 * but for now better to silently swallow typmod, etc.
2568 * - thomas 2000-03-22
2578 | IDENT '.' ColId '%' TYPE_P
2580 $$ = makeNode(TypeName);
2587 /*****************************************************************************
2591 * DROP FUNCTION funcname (arg1, arg2, ...)
2592 * DROP AGGREGATE aggname aggtype
2593 * DROP OPERATOR opname (leftoperand_typ rightoperand_typ)
2595 *****************************************************************************/
2597 RemoveFuncStmt: DROP FUNCTION func_name func_args
2599 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
2606 RemoveAggrStmt: DROP AGGREGATE func_name aggr_argtype
2608 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
2610 n->aggtype = (Node *) $4;
2615 aggr_argtype: Typename { $$ = $1; }
2616 | '*' { $$ = NULL; }
2619 RemoveOperStmt: DROP OPERATOR all_Op '(' oper_argtypes ')'
2621 RemoveOperStmt *n = makeNode(RemoveOperStmt);
2628 oper_argtypes: Typename
2630 elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
2632 | Typename ',' Typename
2633 { $$ = makeList2($1, $3); }
2634 | NONE ',' Typename /* left unary */
2635 { $$ = makeList2(NULL, $3); }
2636 | Typename ',' NONE /* right unary */
2637 { $$ = makeList2($1, NULL); }
2641 /*****************************************************************************
2645 * REINDEX type <typename> [FORCE] [ALL]
2647 *****************************************************************************/
2649 ReindexStmt: REINDEX reindex_type name opt_force
2651 ReindexStmt *n = makeNode(ReindexStmt);
2652 n->reindexType = $2;
2659 reindex_type: INDEX { $$ = INDEX; }
2660 | TABLE { $$ = TABLE; }
2661 | DATABASE { $$ = DATABASE; }
2663 opt_force: FORCE { $$ = TRUE; }
2664 | /* EMPTY */ { $$ = FALSE; }
2668 /*****************************************************************************
2671 * rename <attrname1> in <relname> [*] to <attrname2>
2672 * rename <relname1> to <relname2>
2674 *****************************************************************************/
2676 RenameStmt: ALTER TABLE relation_expr RENAME opt_column opt_name TO name
2678 RenameStmt *n = makeNode(RenameStmt);
2679 n->relname = $3->relname;
2680 n->inhOpt = $3->inhOpt;
2687 opt_name: name { $$ = $1; }
2688 | /*EMPTY*/ { $$ = NULL; }
2691 opt_column: COLUMN { $$ = COLUMN; }
2692 | /*EMPTY*/ { $$ = 0; }
2696 /*****************************************************************************
2698 * QUERY: Define Rewrite Rule , Define Tuple Rule
2699 * Define Rule <old rules >
2701 * only rewrite rule is supported -- ay 9/94
2703 *****************************************************************************/
2705 RuleStmt: CREATE RULE name AS
2706 { QueryIsRule=TRUE; }
2707 ON event TO event_object where_clause
2708 DO opt_instead RuleActionList
2710 RuleStmt *n = makeNode(RuleStmt);
2714 n->whereClause = $10;
2721 RuleActionList: NOTHING { $$ = NIL; }
2722 | RuleActionStmt { $$ = makeList1($1); }
2723 | '[' RuleActionMulti ']' { $$ = $2; }
2724 | '(' RuleActionMulti ')' { $$ = $2; }
2727 /* the thrashing around here is to discard "empty" statements... */
2728 RuleActionMulti: RuleActionMulti ';' RuleActionStmtOrEmpty
2729 { if ($3 != (Node *) NULL)
2730 $$ = lappend($1, $3);
2734 | RuleActionStmtOrEmpty
2735 { if ($1 != (Node *) NULL)
2742 RuleActionStmt: SelectStmt
2749 RuleActionStmtOrEmpty: RuleActionStmt
2751 { $$ = (Node *)NULL; }
2754 event_object: relation_name '.' attr_name
2756 $$ = makeNode(Attr);
2759 $$->attrs = makeList1(makeString($3));
2760 $$->indirection = NIL;
2764 $$ = makeNode(Attr);
2768 $$->indirection = NIL;
2772 /* change me to select, update, etc. some day */
2773 event: SELECT { $$ = CMD_SELECT; }
2774 | UPDATE { $$ = CMD_UPDATE; }
2775 | DELETE { $$ = CMD_DELETE; }
2776 | INSERT { $$ = CMD_INSERT; }
2779 opt_instead: INSTEAD { $$ = TRUE; }
2780 | /*EMPTY*/ { $$ = FALSE; }
2784 /*****************************************************************************
2787 * NOTIFY <relation_name> can appear both in rule bodies and
2788 * as a query-level command
2790 *****************************************************************************/
2792 NotifyStmt: NOTIFY relation_name
2794 NotifyStmt *n = makeNode(NotifyStmt);
2800 ListenStmt: LISTEN relation_name
2802 ListenStmt *n = makeNode(ListenStmt);
2808 UnlistenStmt: UNLISTEN relation_name
2810 UnlistenStmt *n = makeNode(UnlistenStmt);
2816 UnlistenStmt *n = makeNode(UnlistenStmt);
2823 /*****************************************************************************
2827 * BEGIN / COMMIT / ROLLBACK
2828 * (also older versions END / ABORT)
2830 *****************************************************************************/
2832 TransactionStmt: ABORT_TRANS opt_trans
2834 TransactionStmt *n = makeNode(TransactionStmt);
2835 n->command = ROLLBACK;
2838 | BEGIN_TRANS opt_trans
2840 TransactionStmt *n = makeNode(TransactionStmt);
2841 n->command = BEGIN_TRANS;
2846 TransactionStmt *n = makeNode(TransactionStmt);
2847 n->command = COMMIT;
2850 | COMMIT opt_trans opt_chain
2852 TransactionStmt *n = makeNode(TransactionStmt);
2853 n->command = COMMIT;
2856 | END_TRANS opt_trans
2858 TransactionStmt *n = makeNode(TransactionStmt);
2859 n->command = COMMIT;
2862 | ROLLBACK opt_trans
2864 TransactionStmt *n = makeNode(TransactionStmt);
2865 n->command = ROLLBACK;
2868 | ROLLBACK opt_trans opt_chain
2870 TransactionStmt *n = makeNode(TransactionStmt);
2871 n->command = ROLLBACK;
2876 opt_trans: WORK { $$ = TRUE; }
2877 | TRANSACTION { $$ = TRUE; }
2878 | /*EMPTY*/ { $$ = TRUE; }
2881 opt_chain: AND NO CHAIN
2885 /* SQL99 asks that conforming dbs reject AND CHAIN
2886 * if they don't support it. So we can't just ignore it.
2887 * - thomas 2000-08-06
2889 elog(ERROR, "COMMIT/CHAIN not yet supported");
2895 /*****************************************************************************
2898 * define view <viewname> '('target-list ')' [where <quals> ]
2900 *****************************************************************************/
2902 ViewStmt: CREATE VIEW name opt_column_list AS SelectStmt
2904 ViewStmt *n = makeNode(ViewStmt);
2907 n->query = (Query *) $6;
2913 /*****************************************************************************
2918 *****************************************************************************/
2920 LoadStmt: LOAD file_name
2922 LoadStmt *n = makeNode(LoadStmt);
2929 /*****************************************************************************
2934 *****************************************************************************/
2936 CreatedbStmt: CREATE DATABASE database_name WITH createdb_opt_list
2938 CreatedbStmt *n = makeNode(CreatedbStmt);
2942 /* set default options */
2944 n->dbtemplate = NULL;
2946 /* process additional options */
2949 List *optitem = (List *) lfirst(l);
2951 switch (lfirsti(optitem))
2954 n->dbpath = (char *) lsecond(optitem);
2957 n->dbtemplate = (char *) lsecond(optitem);
2960 n->encoding = lfirsti(lnext(optitem));
2966 | CREATE DATABASE database_name
2968 CreatedbStmt *n = makeNode(CreatedbStmt);
2971 n->dbtemplate = NULL;
2977 createdb_opt_list: createdb_opt_item
2978 { $$ = makeList1($1); }
2979 | createdb_opt_list createdb_opt_item
2980 { $$ = lappend($1, $2); }
2984 * createdb_opt_item returns 2-element lists, with the first element
2985 * being an integer code to indicate which item was specified.
2987 createdb_opt_item: LOCATION '=' Sconst
2989 $$ = lconsi(1, makeList1($3));
2991 | LOCATION '=' DEFAULT
2993 $$ = lconsi(1, makeList1(NULL));
2997 $$ = lconsi(2, makeList1($3));
2999 | TEMPLATE '=' DEFAULT
3001 $$ = lconsi(2, makeList1(NULL));
3003 | ENCODING '=' Sconst
3007 encoding = pg_char_to_encoding($3);
3009 elog(ERROR, "%s is not a valid encoding name", $3);
3011 if (strcasecmp($3, GetStandardEncodingName()) != 0)
3012 elog(ERROR, "Multi-byte support is not enabled");
3013 encoding = GetStandardEncoding();
3015 $$ = lconsi(3, makeListi1(encoding));
3017 | ENCODING '=' Iconst
3020 if (!pg_get_encent_by_encoding($3))
3021 elog(ERROR, "%d is not a valid encoding code", $3);
3023 if ($3 != GetStandardEncoding())
3024 elog(ERROR, "Multi-byte support is not enabled");
3026 $$ = lconsi(3, makeListi1($3));
3028 | ENCODING '=' DEFAULT
3030 $$ = lconsi(3, makeListi1(-1));
3035 /*****************************************************************************
3040 *****************************************************************************/
3042 DropdbStmt: DROP DATABASE database_name
3044 DropdbStmt *n = makeNode(DropdbStmt);
3051 /*****************************************************************************
3054 * cluster <index_name> on <relation_name>
3056 *****************************************************************************/
3058 ClusterStmt: CLUSTER index_name ON relation_name
3060 ClusterStmt *n = makeNode(ClusterStmt);
3067 /*****************************************************************************
3073 *****************************************************************************/
3075 VacuumStmt: VACUUM opt_verbose
3077 VacuumStmt *n = makeNode(VacuumStmt);
3085 | VACUUM opt_verbose relation_name
3087 VacuumStmt *n = makeNode(VacuumStmt);
3095 | VACUUM opt_verbose AnalyzeStmt
3097 VacuumStmt *n = (VacuumStmt *) $3;
3104 AnalyzeStmt: analyze_keyword opt_verbose
3106 VacuumStmt *n = makeNode(VacuumStmt);
3114 | analyze_keyword opt_verbose relation_name opt_name_list
3116 VacuumStmt *n = makeNode(VacuumStmt);
3126 analyze_keyword: ANALYZE { $$ = TRUE; }
3127 | ANALYSE /* British */ { $$ = TRUE; }
3130 opt_verbose: VERBOSE { $$ = TRUE; }
3131 | /*EMPTY*/ { $$ = FALSE; }
3134 opt_name_list: '(' name_list ')' { $$ = $2; }
3135 | /*EMPTY*/ { $$ = NIL; }
3139 /*****************************************************************************
3144 *****************************************************************************/
3146 ExplainStmt: EXPLAIN opt_verbose OptimizableStmt
3148 ExplainStmt *n = makeNode(ExplainStmt);
3150 n->query = (Query*)$3;
3156 /*****************************************************************************
3158 * Optimizable Stmts: *
3160 * one of the five queries processed by the planner *
3162 * [ultimately] produces query-trees as specified *
3163 * in the query-spec document in ~postgres/ref *
3165 *****************************************************************************/
3167 OptimizableStmt: SelectStmt
3171 | DeleteStmt /* by default all are $$=$1 */
3175 /*****************************************************************************
3180 *****************************************************************************/
3182 /* This rule used 'opt_column_list' between 'relation_name' and 'insert_rest'
3183 * originally. When the second rule of 'insert_rest' was changed to use the
3184 * new 'SelectStmt' rule (for INTERSECT and EXCEPT) it produced a shift/reduce
3185 * conflict. So I just changed the rules 'InsertStmt' and 'insert_rest' to
3186 * accept the same statements without any shift/reduce conflicts
3188 InsertStmt: INSERT INTO relation_name insert_rest
3195 insert_rest: VALUES '(' target_list ')'
3197 $$ = makeNode(InsertStmt);
3199 $$->targetList = $3;
3200 $$->selectStmt = NULL;
3204 $$ = makeNode(InsertStmt);
3206 $$->targetList = NIL;
3207 $$->selectStmt = NULL;
3211 $$ = makeNode(InsertStmt);
3213 $$->targetList = NIL;
3214 $$->selectStmt = $1;
3216 | '(' columnList ')' VALUES '(' target_list ')'
3218 $$ = makeNode(InsertStmt);
3220 $$->targetList = $6;
3221 $$->selectStmt = NULL;
3223 | '(' columnList ')' SelectStmt
3225 $$ = makeNode(InsertStmt);
3227 $$->targetList = NIL;
3228 $$->selectStmt = $4;
3232 opt_column_list: '(' columnList ')' { $$ = $2; }
3233 | /*EMPTY*/ { $$ = NIL; }
3236 columnList: columnList ',' columnElem
3237 { $$ = lappend($1, $3); }
3239 { $$ = makeList1($1); }
3242 columnElem: ColId opt_indirection
3244 Ident *id = makeNode(Ident);
3246 id->indirection = $2;
3252 /*****************************************************************************
3257 *****************************************************************************/
3259 DeleteStmt: DELETE FROM relation_expr where_clause
3261 DeleteStmt *n = makeNode(DeleteStmt);
3262 n->relname = $3->relname;
3263 n->inhOpt = $3->inhOpt;
3264 n->whereClause = $4;
3269 LockStmt: LOCK_P opt_table relation_name opt_lock
3271 LockStmt *n = makeNode(LockStmt);
3279 opt_lock: IN lock_type MODE { $$ = $2; }
3280 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
3283 lock_type: SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
3284 | ROW opt_lmode { $$ = ($2? RowShareLock: RowExclusiveLock); }
3285 | ACCESS opt_lmode { $$ = ($2? AccessShareLock: AccessExclusiveLock); }
3286 | opt_lmode { $$ = ($1? ShareLock: ExclusiveLock); }
3289 opt_lmode: SHARE { $$ = TRUE; }
3290 | EXCLUSIVE { $$ = FALSE; }
3294 /*****************************************************************************
3297 * UpdateStmt (UPDATE)
3299 *****************************************************************************/
3301 UpdateStmt: UPDATE relation_expr
3302 SET update_target_list
3306 UpdateStmt *n = makeNode(UpdateStmt);
3307 n->relname = $2->relname;
3308 n->inhOpt = $2->inhOpt;
3311 n->whereClause = $6;
3317 /*****************************************************************************
3322 *****************************************************************************/
3323 CursorStmt: DECLARE name opt_cursor CURSOR FOR SelectStmt
3325 SelectStmt *n = (SelectStmt *)$6;
3332 opt_cursor: BINARY { $$ = TRUE; }
3333 | INSENSITIVE { $$ = FALSE; }
3334 | SCROLL { $$ = FALSE; }
3335 | INSENSITIVE SCROLL { $$ = FALSE; }
3336 | /*EMPTY*/ { $$ = FALSE; }
3339 /*****************************************************************************
3344 *****************************************************************************/
3346 /* A complete SELECT statement looks like this.
3348 * The rule returns either a single SelectStmt node or a tree of them,
3349 * representing a set-operation tree.
3351 * There is an ambiguity when a sub-SELECT is within an a_expr and there
3352 * are excess parentheses: do the parentheses belong to the sub-SELECT or
3353 * to the surrounding a_expr? We don't really care, but yacc wants to know.
3354 * To resolve the ambiguity, we are careful to define the grammar so that
3355 * the decision is staved off as long as possible: as long as we can keep
3356 * absorbing parentheses into the sub-SELECT, we will do so, and only when
3357 * it's no longer possible to do that will we decide that parens belong to
3358 * the expression. For example, in "SELECT (((SELECT 2)) + 3)" the extra
3359 * parentheses are treated as part of the sub-select. The necessity of doing
3360 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)". Had we
3361 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
3362 * SELECT viewpoint when we see the UNION.
3364 * This approach is implemented by defining a nonterminal select_with_parens,
3365 * which represents a SELECT with at least one outer layer of parentheses,
3366 * and being careful to use select_with_parens, never '(' SelectStmt ')',
3367 * in the expression grammar. We will then have shift-reduce conflicts
3368 * which we can resolve in favor of always treating '(' <select> ')' as
3369 * a select_with_parens. To resolve the conflicts, the productions that
3370 * conflict with the select_with_parens productions are manually given
3371 * precedences lower than the precedence of ')', thereby ensuring that we
3372 * shift ')' (and then reduce to select_with_parens) rather than trying to
3373 * reduce the inner <select> nonterminal to something else. We use UMINUS
3374 * precedence for this, which is a fairly arbitrary choice.
3376 * To be able to define select_with_parens itself without ambiguity, we need
3377 * a nonterminal select_no_parens that represents a SELECT structure with no
3378 * outermost parentheses. This is a little bit tedious, but it works.
3380 * In non-expression contexts, we use SelectStmt which can represent a SELECT
3381 * with or without outer parentheses.
3384 SelectStmt: select_no_parens %prec UMINUS
3385 | select_with_parens %prec UMINUS
3388 select_with_parens: '(' select_no_parens ')'
3392 | '(' select_with_parens ')'
3398 select_no_parens: simple_select
3402 | select_clause sort_clause opt_for_update_clause opt_select_limit
3404 insertSelectOptions((SelectStmt *) $1, $2, $3,
3405 nth(0, $4), nth(1, $4));
3408 | select_clause for_update_clause opt_select_limit
3410 insertSelectOptions((SelectStmt *) $1, NIL, $2,
3411 nth(0, $3), nth(1, $3));
3414 | select_clause select_limit
3416 insertSelectOptions((SelectStmt *) $1, NIL, NIL,
3417 nth(0, $2), nth(1, $2));
3422 select_clause: simple_select
3423 | select_with_parens
3427 * This rule parses SELECT statements that can appear within set operations,
3428 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
3429 * the ordering of the set operations. Without '(' and ')' we want the
3430 * operations to be ordered per the precedence specs at the head of this file.
3432 * As with select_no_parens, simple_select cannot have outer parentheses,
3433 * but can have parenthesized subclauses.
3435 * Note that sort clauses cannot be included at this level --- SQL92 requires
3436 * SELECT foo UNION SELECT bar ORDER BY baz
3438 * (SELECT foo UNION SELECT bar) ORDER BY baz
3440 * SELECT foo UNION (SELECT bar ORDER BY baz)
3441 * Likewise FOR UPDATE and LIMIT. Therefore, those clauses are described
3442 * as part of the select_no_parens production, not simple_select.
3443 * This does not limit functionality, because you can reintroduce sort and
3444 * limit clauses inside parentheses.
3446 * NOTE: only the leftmost component SelectStmt should have INTO.
3447 * However, this is not checked by the grammar; parse analysis must check it.
3449 simple_select: SELECT opt_distinct target_list
3450 into_clause from_clause where_clause
3451 group_clause having_clause
3453 SelectStmt *n = makeNode(SelectStmt);
3454 n->distinctClause = $2;
3456 n->istemp = (bool) ((Value *) lfirst($4))->val.ival;
3457 n->into = (char *) lnext($4);
3459 n->whereClause = $6;
3460 n->groupClause = $7;
3461 n->havingClause = $8;
3464 | select_clause UNION opt_all select_clause
3466 $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
3468 | select_clause INTERSECT opt_all select_clause
3470 $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
3472 | select_clause EXCEPT opt_all select_clause
3474 $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
3478 /* easy way to return two values. Can someone improve this? bjm */
3479 into_clause: INTO OptTempTableName { $$ = $2; }
3480 | /*EMPTY*/ { $$ = makeList1(makeInteger(FALSE)); }
3484 * Redundancy here is needed to avoid shift/reduce conflicts,
3485 * since TEMP is not a reserved word. See also OptTemp.
3487 * The result is a cons cell (not a true list!) containing
3488 * a boolean and a table name.
3490 OptTempTableName: TEMPORARY opt_table relation_name
3491 { $$ = lcons(makeInteger(TRUE), (List *) $3); }
3492 | TEMP opt_table relation_name
3493 { $$ = lcons(makeInteger(TRUE), (List *) $3); }
3494 | LOCAL TEMPORARY opt_table relation_name
3495 { $$ = lcons(makeInteger(TRUE), (List *) $4); }
3496 | LOCAL TEMP opt_table relation_name
3497 { $$ = lcons(makeInteger(TRUE), (List *) $4); }
3498 | GLOBAL TEMPORARY opt_table relation_name
3500 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
3501 $$ = lcons(makeInteger(TRUE), (List *) $4);
3503 | GLOBAL TEMP opt_table relation_name
3505 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
3506 $$ = lcons(makeInteger(TRUE), (List *) $4);
3508 | TABLE relation_name
3509 { $$ = lcons(makeInteger(FALSE), (List *) $2); }
3511 { $$ = lcons(makeInteger(FALSE), (List *) $1); }
3514 opt_table: TABLE { $$ = TRUE; }
3515 | /*EMPTY*/ { $$ = FALSE; }
3518 opt_all: ALL { $$ = TRUE; }
3519 | /*EMPTY*/ { $$ = FALSE; }
3522 /* We use (NIL) as a placeholder to indicate that all target expressions
3523 * should be placed in the DISTINCT list during parsetree analysis.
3525 opt_distinct: DISTINCT { $$ = makeList1(NIL); }
3526 | DISTINCT ON '(' expr_list ')' { $$ = $4; }
3528 | /*EMPTY*/ { $$ = NIL; }
3531 sort_clause: ORDER BY sortby_list { $$ = $3; }
3534 sortby_list: sortby { $$ = makeList1($1); }
3535 | sortby_list ',' sortby { $$ = lappend($1, $3); }
3538 sortby: a_expr OptUseOp
3540 $$ = makeNode(SortGroupBy);
3546 OptUseOp: USING all_Op { $$ = $2; }
3548 | DESC { $$ = ">"; }
3549 | /*EMPTY*/ { $$ = "<"; /*default*/ }
3553 select_limit: LIMIT select_limit_value ',' select_offset_value
3554 { $$ = makeList2($4, $2); }
3555 | LIMIT select_limit_value OFFSET select_offset_value
3556 { $$ = makeList2($4, $2); }
3557 | LIMIT select_limit_value
3558 { $$ = makeList2(NULL, $2); }
3559 | OFFSET select_offset_value LIMIT select_limit_value
3560 { $$ = makeList2($2, $4); }
3561 | OFFSET select_offset_value
3562 { $$ = makeList2($2, NULL); }
3565 opt_select_limit: select_limit { $$ = $1; }
3566 | /* EMPTY */ { $$ = makeList2(NULL,NULL); }
3569 select_limit_value: Iconst
3571 Const *n = makeNode(Const);
3574 elog(ERROR, "LIMIT must not be negative");
3576 n->consttype = INT4OID;
3577 n->constlen = sizeof(int4);
3578 n->constvalue = Int32GetDatum($1);
3579 n->constisnull = FALSE;
3580 n->constbyval = TRUE;
3581 n->constisset = FALSE;
3582 n->constiscast = FALSE;
3587 /* LIMIT ALL is represented as a NULL constant */
3588 Const *n = makeNode(Const);
3590 n->consttype = INT4OID;
3591 n->constlen = sizeof(int4);
3592 n->constvalue = (Datum) 0;
3593 n->constisnull = TRUE;
3594 n->constbyval = TRUE;
3595 n->constisset = FALSE;
3596 n->constiscast = FALSE;
3601 Param *n = makeNode(Param);
3603 n->paramkind = PARAM_NUM;
3605 n->paramtype = INT4OID;
3610 select_offset_value: Iconst
3612 Const *n = makeNode(Const);
3615 elog(ERROR, "OFFSET must not be negative");
3617 n->consttype = INT4OID;
3618 n->constlen = sizeof(int4);
3619 n->constvalue = Int32GetDatum($1);
3620 n->constisnull = FALSE;
3621 n->constbyval = TRUE;
3622 n->constisset = FALSE;
3623 n->constiscast = FALSE;
3628 Param *n = makeNode(Param);
3630 n->paramkind = PARAM_NUM;
3632 n->paramtype = INT4OID;
3638 * jimmy bell-style recursive queries aren't supported in the
3641 * ...however, recursive addattr and rename supported. make special
3644 relation_name_list: name_list;
3647 { $$ = makeList1(makeString($1)); }
3648 | name_list ',' name
3649 { $$ = lappend($1, makeString($3)); }
3652 group_clause: GROUP BY expr_list { $$ = $3; }
3653 | /*EMPTY*/ { $$ = NIL; }
3656 having_clause: HAVING a_expr
3660 | /*EMPTY*/ { $$ = NULL; }
3663 for_update_clause: FOR UPDATE update_list { $$ = $3; }
3664 | FOR READ ONLY { $$ = NULL; }
3667 opt_for_update_clause: for_update_clause { $$ = $1; }
3668 | /* EMPTY */ { $$ = NULL; }
3671 update_list: OF name_list { $$ = $2; }
3672 | /* EMPTY */ { $$ = makeList1(NULL); }
3675 /*****************************************************************************
3677 * clauses common to all Optimizable Stmts:
3678 * from_clause - allow list of both JOIN expressions and table names
3679 * where_clause - qualifications for joins or restrictions
3681 *****************************************************************************/
3683 from_clause: FROM from_list { $$ = $2; }
3684 | /*EMPTY*/ { $$ = NIL; }
3687 from_list: from_list ',' table_ref { $$ = lappend($1, $3); }
3688 | table_ref { $$ = makeList1($1); }
3692 * table_ref is where an alias clause can be attached. Note we cannot make
3693 * alias_clause have an empty production because that causes parse conflicts
3694 * between table_ref := '(' joined_table ')' alias_clause
3695 * and joined_table := '(' joined_table ')'. So, we must have the
3696 * redundant-looking productions here instead.
3698 table_ref: relation_expr
3702 | relation_expr alias_clause
3707 | select_with_parens
3710 * The SQL spec does not permit a subselect
3711 * (<derived_table>) without an alias clause,
3712 * so we don't either. This avoids the problem
3713 * of needing to invent a unique refname for it.
3714 * That could be surmounted if there's sufficient
3715 * popular demand, but for now let's just implement
3716 * the spec and see if anyone complains.
3717 * However, it does seem like a good idea to emit
3718 * an error message that's better than "parse error".
3720 elog(ERROR, "sub-SELECT in FROM must have an alias"
3721 "\n\tFor example, FROM (SELECT ...) [AS] foo");
3724 | select_with_parens alias_clause
3726 RangeSubselect *n = makeNode(RangeSubselect);
3735 | '(' joined_table ')' alias_clause
3743 * It may seem silly to separate joined_table from table_ref, but there is
3744 * method in SQL92's madness: if you don't do it this way you get reduce-
3745 * reduce conflicts, because it's not clear to the parser generator whether
3746 * to expect alias_clause after ')' or not. For the same reason we must
3747 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
3748 * join_type to expand to empty; if we try it, the parser generator can't
3749 * figure out when to reduce an empty join_type right after table_ref.
3751 * Note that a CROSS JOIN is the same as an unqualified
3752 * INNER JOIN, and an INNER JOIN/ON has the same shape
3753 * but a qualification expression to limit membership.
3754 * A NATURAL JOIN implicitly matches column names between
3755 * tables and the shape is determined by which columns are
3756 * in common. We'll collect columns during the later transformations.
3759 joined_table: '(' joined_table ')'
3763 | table_ref CROSS JOIN table_ref
3765 /* CROSS JOIN is same as unqualified inner join */
3766 JoinExpr *n = makeNode(JoinExpr);
3767 n->jointype = JOIN_INNER;
3768 n->isNatural = FALSE;
3775 | table_ref UNIONJOIN table_ref
3777 /* UNION JOIN is made into 1 token to avoid shift/reduce
3778 * conflict against regular UNION keyword.
3780 JoinExpr *n = makeNode(JoinExpr);
3781 n->jointype = JOIN_UNION;
3782 n->isNatural = FALSE;
3789 | table_ref join_type JOIN table_ref join_qual
3791 JoinExpr *n = makeNode(JoinExpr);
3793 n->isNatural = FALSE;
3796 if ($5 != NULL && IsA($5, List))
3797 n->using = (List *) $5; /* USING clause */
3799 n->quals = $5; /* ON clause */
3802 | table_ref JOIN table_ref join_qual
3804 /* letting join_type reduce to empty doesn't work */
3805 JoinExpr *n = makeNode(JoinExpr);
3806 n->jointype = JOIN_INNER;
3807 n->isNatural = FALSE;
3810 if ($4 != NULL && IsA($4, List))
3811 n->using = (List *) $4; /* USING clause */
3813 n->quals = $4; /* ON clause */
3816 | table_ref NATURAL join_type JOIN table_ref
3818 JoinExpr *n = makeNode(JoinExpr);
3820 n->isNatural = TRUE;
3823 n->using = NIL; /* figure out which columns later... */
3824 n->quals = NULL; /* fill later */
3827 | table_ref NATURAL JOIN table_ref
3829 /* letting join_type reduce to empty doesn't work */
3830 JoinExpr *n = makeNode(JoinExpr);
3831 n->jointype = JOIN_INNER;
3832 n->isNatural = TRUE;
3835 n->using = NIL; /* figure out which columns later... */
3836 n->quals = NULL; /* fill later */
3841 alias_clause: AS ColId '(' name_list ')'
3843 $$ = makeNode(Attr);
3849 $$ = makeNode(Attr);
3852 | ColId '(' name_list ')'
3854 $$ = makeNode(Attr);
3860 $$ = makeNode(Attr);
3865 join_type: FULL join_outer { $$ = JOIN_FULL; }
3866 | LEFT join_outer { $$ = JOIN_LEFT; }
3867 | RIGHT join_outer { $$ = JOIN_RIGHT; }
3868 | INNER_P { $$ = JOIN_INNER; }
3871 /* OUTER is just noise... */
3872 join_outer: OUTER_P { $$ = NULL; }
3873 | /*EMPTY*/ { $$ = NULL; }
3876 /* JOIN qualification clauses
3877 * Possibilities are:
3878 * USING ( column list ) allows only unqualified column names,
3879 * which must match between tables.
3880 * ON expr allows more general qualifications.
3882 * We return USING as a List node, while an ON-expr will not be a List.
3885 join_qual: USING '(' name_list ')' { $$ = (Node *) $3; }
3886 | ON a_expr { $$ = $2; }
3890 relation_expr: relation_name
3892 /* default inheritance */
3893 $$ = makeNode(RangeVar);
3895 $$->inhOpt = INH_DEFAULT;
3900 /* inheritance query */
3901 $$ = makeNode(RangeVar);
3903 $$->inhOpt = INH_YES;
3906 | ONLY relation_name
3908 /* no inheritance */
3909 $$ = makeNode(RangeVar);
3911 $$->inhOpt = INH_NO;
3916 where_clause: WHERE a_expr { $$ = $2; }
3917 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
3921 /*****************************************************************************
3924 * SQL92 introduces a large amount of type-specific syntax.
3925 * Define individual clauses to handle these cases, and use
3926 * the generic case to handle regular type-extensible Postgres syntax.
3927 * - thomas 1997-10-10
3929 *****************************************************************************/
3931 Typename: SimpleTypename opt_array_bounds
3934 $$->arrayBounds = $2;
3936 | SETOF SimpleTypename
3943 opt_array_bounds: opt_array_bounds '[' ']'
3944 { $$ = lappend($1, makeInteger(-1)); }
3945 | opt_array_bounds '[' Iconst ']'
3946 { $$ = lappend($1, makeInteger($3)); }
3951 SimpleTypename: ConstTypename
3955 ConstTypename: GenericType
3965 $$ = makeNode(TypeName);
3966 $$->name = xlateSqlType($1);
3971 /* SQL92 numeric data types
3972 * Check FLOAT() precision limits assuming IEEE floating types.
3973 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
3974 * - thomas 1997-09-18
3976 Numeric: FLOAT opt_float
3978 $$ = makeNode(TypeName);
3979 $$->name = xlateSqlType($2);
3984 $$ = makeNode(TypeName);
3985 $$->name = xlateSqlType("float8");
3988 | DECIMAL opt_decimal
3990 $$ = makeNode(TypeName);
3991 $$->name = xlateSqlType("decimal");
3996 $$ = makeNode(TypeName);
3997 $$->name = xlateSqlType("decimal");
4000 | NUMERIC opt_numeric
4002 $$ = makeNode(TypeName);
4003 $$->name = xlateSqlType("numeric");
4010 $$ = makeNode(TypeName);
4011 $$->name = xlateSqlType("path");
4016 opt_float: '(' Iconst ')'
4019 elog(ERROR,"precision for FLOAT must be at least 1");
4021 $$ = xlateSqlType("float4");
4023 $$ = xlateSqlType("float8");
4025 elog(ERROR,"precision for FLOAT must be less than 16");
4029 $$ = xlateSqlType("float8");
4033 opt_numeric: '(' Iconst ',' Iconst ')'
4035 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4036 elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
4037 $2, NUMERIC_MAX_PRECISION);
4038 if ($4 < 0 || $4 > $2)
4039 elog(ERROR,"NUMERIC scale %d must be between 0 and precision %d",
4042 $$ = (($2 << 16) | $4) + VARHDRSZ;
4046 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4047 elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
4048 $2, NUMERIC_MAX_PRECISION);
4050 $$ = ($2 << 16) + VARHDRSZ;
4054 /* Insert "-1" meaning "default"; may be replaced later */
4059 opt_decimal: '(' Iconst ',' Iconst ')'
4061 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4062 elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
4063 $2, NUMERIC_MAX_PRECISION);
4064 if ($4 < 0 || $4 > $2)
4065 elog(ERROR,"DECIMAL scale %d must be between 0 and precision %d",
4068 $$ = (($2 << 16) | $4) + VARHDRSZ;
4072 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
4073 elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
4074 $2, NUMERIC_MAX_PRECISION);
4076 $$ = ($2 << 16) + VARHDRSZ;
4080 /* Insert "-1" meaning "default"; may be replaced later */
4087 * SQL92 bit-field data types
4088 * The following implements BIT() and BIT VARYING().
4090 Bit: bit '(' Iconst ')'
4092 $$ = makeNode(TypeName);
4095 elog(ERROR,"length for type '%s' must be at least 1",
4097 else if ($3 > (MaxAttrSize * BITS_PER_BYTE))
4098 elog(ERROR,"length for type '%s' cannot exceed %d",
4099 $1, (MaxAttrSize * BITS_PER_BYTE));
4104 $$ = makeNode(TypeName);
4106 /* default length, if needed, will be inserted later */
4111 bit: BIT opt_varying
4115 if ($2) type = xlateSqlType("varbit");
4116 else type = xlateSqlType("bit");
4122 * SQL92 character data types
4123 * The following implements CHAR() and VARCHAR().
4125 Character: character '(' Iconst ')'
4127 $$ = makeNode(TypeName);
4130 elog(ERROR,"length for type '%s' must be at least 1",
4132 else if ($3 > MaxAttrSize)
4133 elog(ERROR,"length for type '%s' cannot exceed %d",
4136 /* we actually implement these like a varlen, so
4137 * the first 4 bytes is the length. (the difference
4138 * between these and "text" is that we blank-pad and
4139 * truncate where necessary)
4141 $$->typmod = VARHDRSZ + $3;
4145 $$ = makeNode(TypeName);
4147 /* default length, if needed, will be inserted later */
4152 character: CHARACTER opt_varying opt_charset
4155 if (($3 == NULL) || (strcmp($3, "sql_text") == 0)) {
4156 if ($2) type = xlateSqlType("varchar");
4157 else type = xlateSqlType("bpchar");
4160 c = palloc(strlen("var") + strlen($3) + 1);
4163 type = xlateSqlType(c);
4165 type = xlateSqlType($3);
4170 | CHAR opt_varying { $$ = xlateSqlType($2 ? "varchar": "bpchar"); }
4171 | VARCHAR { $$ = xlateSqlType("varchar"); }
4172 | NATIONAL CHARACTER opt_varying { $$ = xlateSqlType($3 ? "varchar": "bpchar"); }
4173 | NATIONAL CHAR opt_varying { $$ = xlateSqlType($3 ? "varchar": "bpchar"); }
4174 | NCHAR opt_varying { $$ = xlateSqlType($2 ? "varchar": "bpchar"); }
4177 opt_varying: VARYING { $$ = TRUE; }
4178 | /*EMPTY*/ { $$ = FALSE; }
4181 opt_charset: CHARACTER SET ColId { $$ = $3; }
4182 | /*EMPTY*/ { $$ = NULL; }
4185 opt_collate: COLLATE ColId { $$ = $2; }
4186 | /*EMPTY*/ { $$ = NULL; }
4189 ConstDatetime: datetime
4191 $$ = makeNode(TypeName);
4192 $$->name = xlateSqlType($1);
4195 | TIMESTAMP opt_timezone
4197 $$ = makeNode(TypeName);
4198 $$->name = xlateSqlType("timestamp");
4204 $$ = makeNode(TypeName);
4206 $$->name = xlateSqlType("timetz");
4208 $$->name = xlateSqlType("time");
4213 ConstInterval: INTERVAL opt_interval
4215 $$ = makeNode(TypeName);
4216 $$->name = xlateSqlType("interval");
4221 datetime: YEAR_P { $$ = "year"; }
4222 | MONTH_P { $$ = "month"; }
4223 | DAY_P { $$ = "day"; }
4224 | HOUR_P { $$ = "hour"; }
4225 | MINUTE_P { $$ = "minute"; }
4226 | SECOND_P { $$ = "second"; }
4229 opt_timezone: WITH TIME ZONE { $$ = TRUE; }
4230 | WITHOUT TIME ZONE { $$ = FALSE; }
4231 | /*EMPTY*/ { $$ = FALSE; }
4234 opt_interval: datetime { $$ = makeList1($1); }
4235 | YEAR_P TO MONTH_P { $$ = NIL; }
4236 | DAY_P TO HOUR_P { $$ = NIL; }
4237 | DAY_P TO MINUTE_P { $$ = NIL; }
4238 | DAY_P TO SECOND_P { $$ = NIL; }
4239 | HOUR_P TO MINUTE_P { $$ = NIL; }
4240 | HOUR_P TO SECOND_P { $$ = NIL; }
4241 | MINUTE_P TO SECOND_P { $$ = NIL; }
4242 | /*EMPTY*/ { $$ = NIL; }
4246 /*****************************************************************************
4248 * expression grammar
4250 *****************************************************************************/
4252 /* Expressions using row descriptors
4253 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
4254 * with singleton expressions.
4256 row_expr: '(' row_descriptor ')' IN select_with_parens
4258 SubLink *n = makeNode(SubLink);
4260 n->oper = (List *) makeA_Expr(OP, "=", NULL, NULL);
4262 n->subLinkType = ANY_SUBLINK;
4266 | '(' row_descriptor ')' NOT IN select_with_parens
4268 SubLink *n = makeNode(SubLink);
4270 n->oper = (List *) makeA_Expr(OP, "<>", NULL, NULL);
4272 n->subLinkType = ALL_SUBLINK;
4276 | '(' row_descriptor ')' all_Op sub_type select_with_parens
4278 SubLink *n = makeNode(SubLink);
4280 n->oper = (List *) makeA_Expr(OP, $4, NULL, NULL);
4281 if (strcmp($4, "<>") == 0)
4285 n->subLinkType = $5;
4289 | '(' row_descriptor ')' all_Op select_with_parens
4291 SubLink *n = makeNode(SubLink);
4293 n->oper = (List *) makeA_Expr(OP, $4, NULL, NULL);
4294 if (strcmp($4, "<>") == 0)
4298 n->subLinkType = MULTIEXPR_SUBLINK;
4302 | '(' row_descriptor ')' all_Op '(' row_descriptor ')'
4304 $$ = makeRowExpr($4, $2, $6);
4306 | '(' row_descriptor ')' OVERLAPS '(' row_descriptor ')'
4308 FuncCall *n = makeNode(FuncCall);
4311 n->funcname = xlateSqlFunc("overlaps");
4312 if (length(largs) == 1)
4313 largs = lappend(largs, $2);
4314 else if (length(largs) != 2)
4315 elog(ERROR, "Wrong number of parameters"
4316 " on left side of OVERLAPS expression");
4317 if (length(rargs) == 1)
4318 rargs = lappend(rargs, $6);
4319 else if (length(rargs) != 2)
4320 elog(ERROR, "Wrong number of parameters"
4321 " on right side of OVERLAPS expression");
4322 n->args = nconc(largs, rargs);
4323 n->agg_star = FALSE;
4324 n->agg_distinct = FALSE;
4329 row_descriptor: row_list ',' a_expr
4331 $$ = lappend($1, $3);
4335 row_list: row_list ',' a_expr
4337 $$ = lappend($1, $3);
4345 sub_type: ANY { $$ = ANY_SUBLINK; }
4346 | SOME { $$ = ANY_SUBLINK; }
4347 | ALL { $$ = ALL_SUBLINK; }
4350 all_Op: Op | MathOp;
4352 MathOp: '+' { $$ = "+"; }
4364 * General expressions
4365 * This is the heart of the expression syntax.
4367 * We have two expression types: a_expr is the unrestricted kind, and
4368 * b_expr is a subset that must be used in some places to avoid shift/reduce
4369 * conflicts. For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
4370 * because that use of AND conflicts with AND as a boolean operator. So,
4371 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
4373 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
4374 * always be used by surrounding it with parens.
4376 * c_expr is all the productions that are common to a_expr and b_expr;
4377 * it's factored out just to eliminate redundant coding.
4381 | a_expr TYPECAST Typename
4382 { $$ = makeTypeCast($1, $3); }
4383 | a_expr AT TIME ZONE c_expr
4385 FuncCall *n = makeNode(FuncCall);
4386 n->funcname = "timezone";
4387 n->args = makeList2($5, $1);
4388 n->agg_star = FALSE;
4389 n->agg_distinct = FALSE;
4393 * These operators must be called out explicitly in order to make use
4394 * of yacc/bison's automatic operator-precedence handling. All other
4395 * operator names are handled by the generic productions using "Op",
4396 * below; and all those operators will have the same precedence.
4398 * If you add more explicitly-known operators, be sure to add them
4399 * also to b_expr and to the MathOp list above.
4401 | '+' a_expr %prec UMINUS
4402 { $$ = makeA_Expr(OP, "+", NULL, $2); }
4403 | '-' a_expr %prec UMINUS
4404 { $$ = doNegate($2); }
4406 { $$ = makeA_Expr(OP, "%", NULL, $2); }
4408 { $$ = makeA_Expr(OP, "^", NULL, $2); }
4410 { $$ = makeA_Expr(OP, "%", $1, NULL); }
4412 { $$ = makeA_Expr(OP, "^", $1, NULL); }
4414 { $$ = makeA_Expr(OP, "+", $1, $3); }
4416 { $$ = makeA_Expr(OP, "-", $1, $3); }
4418 { $$ = makeA_Expr(OP, "*", $1, $3); }
4420 { $$ = makeA_Expr(OP, "/", $1, $3); }
4422 { $$ = makeA_Expr(OP, "%", $1, $3); }
4424 { $$ = makeA_Expr(OP, "^", $1, $3); }
4426 { $$ = makeA_Expr(OP, "<", $1, $3); }
4428 { $$ = makeA_Expr(OP, ">", $1, $3); }
4432 * Special-case "foo = NULL" and "NULL = foo" for
4433 * compatibility with standards-broken products
4434 * (like Microsoft's). Turn these into IS NULL exprs.
4436 if (exprIsNullConstant($3))
4438 NullTest *n = makeNode(NullTest);
4440 n->nulltesttype = IS_NULL;
4443 else if (exprIsNullConstant($1))
4445 NullTest *n = makeNode(NullTest);
4447 n->nulltesttype = IS_NULL;
4451 $$ = makeA_Expr(OP, "=", $1, $3);
4455 { $$ = makeA_Expr(OP, $2, $1, $3); }
4457 { $$ = makeA_Expr(OP, $1, NULL, $2); }
4458 | a_expr Op %prec POSTFIXOP
4459 { $$ = makeA_Expr(OP, $2, $1, NULL); }
4462 { $$ = makeA_Expr(AND, NULL, $1, $3); }
4464 { $$ = makeA_Expr(OR, NULL, $1, $3); }
4466 { $$ = makeA_Expr(NOT, NULL, NULL, $2); }
4468 | a_expr LIKE a_expr
4469 { $$ = makeA_Expr(OP, "~~", $1, $3); }
4470 | a_expr LIKE a_expr ESCAPE a_expr
4472 FuncCall *n = makeNode(FuncCall);
4473 n->funcname = "like_escape";
4474 n->args = makeList2($3, $5);
4475 n->agg_star = FALSE;
4476 n->agg_distinct = FALSE;
4477 $$ = makeA_Expr(OP, "~~", $1, (Node *) n);
4479 | a_expr NOT LIKE a_expr
4480 { $$ = makeA_Expr(OP, "!~~", $1, $4); }
4481 | a_expr NOT LIKE a_expr ESCAPE a_expr
4483 FuncCall *n = makeNode(FuncCall);
4484 n->funcname = "like_escape";
4485 n->args = makeList2($4, $6);
4486 n->agg_star = FALSE;
4487 n->agg_distinct = FALSE;
4488 $$ = makeA_Expr(OP, "!~~", $1, (Node *) n);
4490 | a_expr ILIKE a_expr
4491 { $$ = makeA_Expr(OP, "~~*", $1, $3); }
4492 | a_expr ILIKE a_expr ESCAPE a_expr
4494 FuncCall *n = makeNode(FuncCall);
4495 n->funcname = "like_escape";
4496 n->args = makeList2($3, $5);
4497 n->agg_star = FALSE;
4498 n->agg_distinct = FALSE;
4499 $$ = makeA_Expr(OP, "~~*", $1, (Node *) n);
4501 | a_expr NOT ILIKE a_expr
4502 { $$ = makeA_Expr(OP, "!~~*", $1, $4); }
4503 | a_expr NOT ILIKE a_expr ESCAPE a_expr
4505 FuncCall *n = makeNode(FuncCall);
4506 n->funcname = "like_escape";
4507 n->args = makeList2($4, $6);
4508 n->agg_star = FALSE;
4509 n->agg_distinct = FALSE;
4510 $$ = makeA_Expr(OP, "!~~*", $1, (Node *) n);
4513 * Define SQL92-style Null test clause.
4514 * Allow two forms described in the standard:
4517 * Allow two SQL extensions
4520 * NOTE: this is not yet fully SQL-compatible, since SQL92
4521 * allows a row constructor as argument, not just a scalar.
4525 NullTest *n = makeNode(NullTest);
4527 n->nulltesttype = IS_NULL;
4532 NullTest *n = makeNode(NullTest);
4534 n->nulltesttype = IS_NULL;
4539 NullTest *n = makeNode(NullTest);
4541 n->nulltesttype = IS_NOT_NULL;
4544 | a_expr IS NOT NULL_P
4546 NullTest *n = makeNode(NullTest);
4548 n->nulltesttype = IS_NOT_NULL;
4551 /* IS TRUE, IS FALSE, etc used to be function calls
4552 * but let's make them expressions to allow the optimizer
4553 * a chance to eliminate them if a_expr is a constant string.
4554 * - thomas 1997-12-22
4556 * Created BooleanTest Node type, and changed handling
4562 BooleanTest *b = makeNode(BooleanTest);
4564 b->booltesttype = IS_TRUE;
4567 | a_expr IS NOT TRUE_P
4569 BooleanTest *b = makeNode(BooleanTest);
4571 b->booltesttype = IS_NOT_TRUE;
4576 BooleanTest *b = makeNode(BooleanTest);
4578 b->booltesttype = IS_FALSE;
4581 | a_expr IS NOT FALSE_P
4583 BooleanTest *b = makeNode(BooleanTest);
4585 b->booltesttype = IS_NOT_FALSE;
4590 BooleanTest *b = makeNode(BooleanTest);
4592 b->booltesttype = IS_UNKNOWN;
4595 | a_expr IS NOT UNKNOWN
4597 BooleanTest *b = makeNode(BooleanTest);
4599 b->booltesttype = IS_NOT_UNKNOWN;
4602 | a_expr BETWEEN b_expr AND b_expr %prec BETWEEN
4604 $$ = makeA_Expr(AND, NULL,
4605 makeA_Expr(OP, ">=", $1, $3),
4606 makeA_Expr(OP, "<=", $1, $5));
4608 | a_expr NOT BETWEEN b_expr AND b_expr %prec BETWEEN
4610 $$ = makeA_Expr(OR, NULL,
4611 makeA_Expr(OP, "<", $1, $4),
4612 makeA_Expr(OP, ">", $1, $6));
4616 /* in_expr returns a SubLink or a list of a_exprs */
4617 if (IsA($3, SubLink))
4619 SubLink *n = (SubLink *)$3;
4620 n->lefthand = makeList1($1);
4621 n->oper = (List *) makeA_Expr(OP, "=", NULL, NULL);
4623 n->subLinkType = ANY_SUBLINK;
4630 foreach(l, (List *) $3)
4632 Node *cmp = makeA_Expr(OP, "=", $1, lfirst(l));
4636 n = makeA_Expr(OR, NULL, n, cmp);
4641 | a_expr NOT IN in_expr
4643 /* in_expr returns a SubLink or a list of a_exprs */
4644 if (IsA($4, SubLink))
4646 SubLink *n = (SubLink *)$4;
4647 n->lefthand = makeList1($1);
4648 n->oper = (List *) makeA_Expr(OP, "<>", NULL, NULL);
4650 n->subLinkType = ALL_SUBLINK;
4657 foreach(l, (List *) $4)
4659 Node *cmp = makeA_Expr(OP, "<>", $1, lfirst(l));
4663 n = makeA_Expr(AND, NULL, n, cmp);
4668 | a_expr all_Op sub_type select_with_parens %prec Op
4670 SubLink *n = makeNode(SubLink);
4671 n->lefthand = makeList1($1);
4672 n->oper = (List *) makeA_Expr(OP, $2, NULL, NULL);
4673 n->useor = FALSE; /* doesn't matter since only one col */
4674 n->subLinkType = $3;
4683 * Restricted expressions
4685 * b_expr is a subset of the complete expression syntax defined by a_expr.
4687 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
4688 * cause trouble in the places where b_expr is used. For simplicity, we
4689 * just eliminate all the boolean-keyword-operator productions from b_expr.
4693 | b_expr TYPECAST Typename
4694 { $$ = makeTypeCast($1, $3); }
4695 | '+' b_expr %prec UMINUS
4696 { $$ = makeA_Expr(OP, "+", NULL, $2); }
4697 | '-' b_expr %prec UMINUS
4698 { $$ = doNegate($2); }
4700 { $$ = makeA_Expr(OP, "%", NULL, $2); }
4702 { $$ = makeA_Expr(OP, "^", NULL, $2); }
4704 { $$ = makeA_Expr(OP, "%", $1, NULL); }
4706 { $$ = makeA_Expr(OP, "^", $1, NULL); }
4708 { $$ = makeA_Expr(OP, "+", $1, $3); }
4710 { $$ = makeA_Expr(OP, "-", $1, $3); }
4712 { $$ = makeA_Expr(OP, "*", $1, $3); }
4714 { $$ = makeA_Expr(OP, "/", $1, $3); }
4716 { $$ = makeA_Expr(OP, "%", $1, $3); }
4718 { $$ = makeA_Expr(OP, "^", $1, $3); }
4720 { $$ = makeA_Expr(OP, "<", $1, $3); }
4722 { $$ = makeA_Expr(OP, ">", $1, $3); }
4724 { $$ = makeA_Expr(OP, "=", $1, $3); }
4727 { $$ = makeA_Expr(OP, $2, $1, $3); }
4729 { $$ = makeA_Expr(OP, $1, NULL, $2); }
4730 | b_expr Op %prec POSTFIXOP
4731 { $$ = makeA_Expr(OP, $2, $1, NULL); }
4735 * Productions that can be used in both a_expr and b_expr.
4737 * Note: productions that refer recursively to a_expr or b_expr mostly
4738 * cannot appear here. However, it's OK to refer to a_exprs that occur
4739 * inside parentheses, such as function arguments; that cannot introduce
4740 * ambiguity to the b_expr syntax.
4743 { $$ = (Node *) $1; }
4744 | ColId opt_indirection
4746 /* could be a column name or a relation_name */
4747 Ident *n = makeNode(Ident);
4749 n->indirection = $2;
4756 | CAST '(' a_expr AS Typename ')'
4757 { $$ = makeTypeCast($3, $5); }
4762 FuncCall *n = makeNode(FuncCall);
4765 n->agg_star = FALSE;
4766 n->agg_distinct = FALSE;
4769 | func_name '(' expr_list ')'
4771 FuncCall *n = makeNode(FuncCall);
4774 n->agg_star = FALSE;
4775 n->agg_distinct = FALSE;
4778 | func_name '(' ALL expr_list ')'
4780 FuncCall *n = makeNode(FuncCall);
4783 n->agg_star = FALSE;
4784 n->agg_distinct = FALSE;
4785 /* Ideally we'd mark the FuncCall node to indicate
4786 * "must be an aggregate", but there's no provision
4787 * for that in FuncCall at the moment.
4791 | func_name '(' DISTINCT expr_list ')'
4793 FuncCall *n = makeNode(FuncCall);
4796 n->agg_star = FALSE;
4797 n->agg_distinct = TRUE;
4800 | func_name '(' '*' ')'
4803 * For now, we transform AGGREGATE(*) into AGGREGATE(1).
4805 * This does the right thing for COUNT(*) (in fact,
4806 * any certainly-non-null expression would do for COUNT),
4807 * and there are no other aggregates in SQL92 that accept
4810 * The FuncCall node is also marked agg_star = true,
4811 * so that later processing can detect what the argument
4814 FuncCall *n = makeNode(FuncCall);
4815 A_Const *star = makeNode(A_Const);
4817 star->val.type = T_Integer;
4818 star->val.val.ival = 1;
4820 n->args = makeList1(star);
4822 n->agg_distinct = FALSE;
4828 * Translate as "date('now'::text)".
4830 * We cannot use "'now'::date" because coerce_type() will
4831 * immediately reduce that to a constant representing
4832 * today's date. We need to delay the conversion until
4833 * runtime, else the wrong things will happen when
4834 * CURRENT_DATE is used in a column default value or rule.
4836 * This could be simplified if we had a way to generate
4837 * an expression tree representing runtime application
4838 * of type-input conversion functions...
4840 A_Const *s = makeNode(A_Const);
4841 TypeName *t = makeNode(TypeName);
4842 FuncCall *n = makeNode(FuncCall);
4844 s->val.type = T_String;
4845 s->val.val.str = "now";
4848 t->name = xlateSqlType("text");
4852 n->funcname = xlateSqlType("date");
4853 n->args = makeList1(s);
4854 n->agg_star = FALSE;
4855 n->agg_distinct = FALSE;
4862 * Translate as "time('now'::text)".
4863 * See comments for CURRENT_DATE.
4865 A_Const *s = makeNode(A_Const);
4866 TypeName *t = makeNode(TypeName);
4867 FuncCall *n = makeNode(FuncCall);
4869 s->val.type = T_String;
4870 s->val.val.str = "now";
4873 t->name = xlateSqlType("text");
4877 n->funcname = xlateSqlType("time");
4878 n->args = makeList1(s);
4879 n->agg_star = FALSE;
4880 n->agg_distinct = FALSE;
4884 | CURRENT_TIME '(' Iconst ')'
4887 * Translate as "time('now'::text)".
4888 * See comments for CURRENT_DATE.
4890 A_Const *s = makeNode(A_Const);
4891 TypeName *t = makeNode(TypeName);
4892 FuncCall *n = makeNode(FuncCall);
4894 s->val.type = T_String;
4895 s->val.val.str = "now";
4898 t->name = xlateSqlType("text");
4902 n->funcname = xlateSqlType("time");
4903 n->args = makeList1(s);
4904 n->agg_star = FALSE;
4905 n->agg_distinct = FALSE;
4908 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented"
4909 "; zero used instead",$3);
4916 * Translate as "timestamp('now'::text)".
4917 * See comments for CURRENT_DATE.
4919 A_Const *s = makeNode(A_Const);
4920 TypeName *t = makeNode(TypeName);
4921 FuncCall *n = makeNode(FuncCall);
4923 s->val.type = T_String;
4924 s->val.val.str = "now";
4927 t->name = xlateSqlType("text");
4931 n->funcname = xlateSqlType("timestamp");
4932 n->args = makeList1(s);
4933 n->agg_star = FALSE;
4934 n->agg_distinct = FALSE;
4938 | CURRENT_TIMESTAMP '(' Iconst ')'
4941 * Translate as "timestamp('now'::text)".
4942 * See comments for CURRENT_DATE.
4944 A_Const *s = makeNode(A_Const);
4945 TypeName *t = makeNode(TypeName);
4946 FuncCall *n = makeNode(FuncCall);
4948 s->val.type = T_String;
4949 s->val.val.str = "now";
4952 t->name = xlateSqlType("text");
4956 n->funcname = xlateSqlType("timestamp");
4957 n->args = makeList1(s);
4958 n->agg_star = FALSE;
4959 n->agg_distinct = FALSE;
4962 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented"
4963 "; zero used instead",$3);
4969 FuncCall *n = makeNode(FuncCall);
4970 n->funcname = "current_user";
4972 n->agg_star = FALSE;
4973 n->agg_distinct = FALSE;
4978 FuncCall *n = makeNode(FuncCall);
4979 n->funcname = "session_user";
4981 n->agg_star = FALSE;
4982 n->agg_distinct = FALSE;
4987 FuncCall *n = makeNode(FuncCall);
4988 n->funcname = "current_user";
4990 n->agg_star = FALSE;
4991 n->agg_distinct = FALSE;
4994 | EXTRACT '(' extract_list ')'
4996 FuncCall *n = makeNode(FuncCall);
4997 n->funcname = "date_part";
4999 n->agg_star = FALSE;
5000 n->agg_distinct = FALSE;
5003 | POSITION '(' position_list ')'
5005 /* position(A in B) is converted to position(B, A) */
5006 FuncCall *n = makeNode(FuncCall);
5007 n->funcname = "position";
5009 n->agg_star = FALSE;
5010 n->agg_distinct = FALSE;
5013 | SUBSTRING '(' substr_list ')'
5015 /* substring(A from B for C) is converted to
5016 * substring(A, B, C) - thomas 2000-11-28
5018 FuncCall *n = makeNode(FuncCall);
5019 n->funcname = "substring";
5021 n->agg_star = FALSE;
5022 n->agg_distinct = FALSE;
5025 | TRIM '(' BOTH trim_list ')'
5027 /* various trim expressions are defined in SQL92
5028 * - thomas 1997-07-19
5030 FuncCall *n = makeNode(FuncCall);
5031 n->funcname = "btrim";
5033 n->agg_star = FALSE;
5034 n->agg_distinct = FALSE;
5037 | TRIM '(' LEADING trim_list ')'
5039 FuncCall *n = makeNode(FuncCall);
5040 n->funcname = "ltrim";
5042 n->agg_star = FALSE;
5043 n->agg_distinct = FALSE;
5046 | TRIM '(' TRAILING trim_list ')'
5048 FuncCall *n = makeNode(FuncCall);
5049 n->funcname = "rtrim";
5051 n->agg_star = FALSE;
5052 n->agg_distinct = FALSE;
5055 | TRIM '(' trim_list ')'
5057 FuncCall *n = makeNode(FuncCall);
5058 n->funcname = "btrim";
5060 n->agg_star = FALSE;
5061 n->agg_distinct = FALSE;
5064 | select_with_parens %prec UMINUS
5066 SubLink *n = makeNode(SubLink);
5070 n->subLinkType = EXPR_SUBLINK;
5074 | EXISTS select_with_parens
5076 SubLink *n = makeNode(SubLink);
5080 n->subLinkType = EXISTS_SUBLINK;
5087 * Supporting nonterminals for expressions.
5090 opt_indirection: opt_indirection '[' a_expr ']'
5092 A_Indices *ai = makeNode(A_Indices);
5095 $$ = lappend($1, ai);
5097 | opt_indirection '[' a_expr ':' a_expr ']'
5099 A_Indices *ai = makeNode(A_Indices);
5102 $$ = lappend($1, ai);
5109 { $$ = makeList1($1); }
5110 | expr_list ',' a_expr
5111 { $$ = lappend($1, $3); }
5112 | expr_list USING a_expr
5113 { $$ = lappend($1, $3); }
5116 extract_list: extract_arg FROM a_expr
5118 A_Const *n = makeNode(A_Const);
5119 n->val.type = T_String;
5120 n->val.val.str = $1;
5121 $$ = makeList2((Node *) n, $3);
5127 /* Allow delimited string SCONST in extract_arg as an SQL extension.
5128 * - thomas 2001-04-12
5131 extract_arg: datetime { $$ = $1; }
5132 | SCONST { $$ = $1; }
5133 | IDENT { $$ = $1; }
5134 | TIMEZONE_HOUR { $$ = "tz_hour"; }
5135 | TIMEZONE_MINUTE { $$ = "tz_minute"; }
5138 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
5140 position_list: b_expr IN b_expr
5141 { $$ = makeList2($3, $1); }
5146 /* SUBSTRING() arguments
5147 * SQL9x defines a specific syntax for arguments to SUBSTRING():
5148 * o substring(text from int for int)
5149 * o substring(text from int) get entire string from starting point "int"
5150 * o substring(text for int) get first "int" characters of string
5151 * We also want to implement generic substring functions which accept
5152 * the usual generic list of arguments. So we will accept both styles
5153 * here, and convert the SQL9x style to the generic list for further
5154 * processing. - thomas 2000-11-28
5156 substr_list: a_expr substr_from substr_for
5158 $$ = makeList3($1, $2, $3);
5160 | a_expr substr_for substr_from
5162 $$ = makeList3($1, $3, $2);
5164 | a_expr substr_from
5166 $$ = makeList2($1, $2);
5170 A_Const *n = makeNode(A_Const);
5171 n->val.type = T_Integer;
5172 n->val.val.ival = 1;
5173 $$ = makeList3($1, (Node *)n, $2);
5183 substr_from: FROM a_expr
5187 substr_for: FOR a_expr
5191 trim_list: a_expr FROM expr_list
5192 { $$ = lappend($3, $1); }
5199 in_expr: select_with_parens
5201 SubLink *n = makeNode(SubLink);
5205 | '(' in_expr_nodes ')'
5206 { $$ = (Node *)$2; }
5209 in_expr_nodes: a_expr
5210 { $$ = makeList1($1); }
5211 | in_expr_nodes ',' a_expr
5212 { $$ = lappend($1, $3); }
5216 * Define SQL92-style case clause.
5217 * Allow all four forms described in the standard:
5218 * - Full specification
5219 * CASE WHEN a = b THEN c ... ELSE d END
5220 * - Implicit argument
5221 * CASE a WHEN b THEN c ... ELSE d END
5222 * - Conditional NULL
5224 * same as CASE WHEN x = y THEN NULL ELSE x END
5225 * - Conditional substitution from list, use first non-null argument
5227 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
5228 * - thomas 1998-11-09
5230 case_expr: CASE case_arg when_clause_list case_default END_TRANS
5232 CaseExpr *c = makeNode(CaseExpr);
5238 | NULLIF '(' a_expr ',' a_expr ')'
5240 CaseExpr *c = makeNode(CaseExpr);
5241 CaseWhen *w = makeNode(CaseWhen);
5243 A_Const *n = makeNode(A_Const);
5244 n->val.type = T_Null;
5245 w->result = (Node *)n;
5247 w->expr = makeA_Expr(OP, "=", $3, $5);
5248 c->args = makeList1(w);
5252 | COALESCE '(' expr_list ')'
5254 CaseExpr *c = makeNode(CaseExpr);
5258 CaseWhen *w = makeNode(CaseWhen);
5259 NullTest *n = makeNode(NullTest);
5261 n->nulltesttype = IS_NOT_NULL;
5262 w->expr = (Node *) n;
5263 w->result = lfirst(l);
5264 c->args = lappend(c->args, w);
5270 when_clause_list: when_clause_list when_clause
5271 { $$ = lappend($1, $2); }
5273 { $$ = makeList1($1); }
5276 when_clause: WHEN a_expr THEN a_expr
5278 CaseWhen *w = makeNode(CaseWhen);
5285 case_default: ELSE a_expr { $$ = $2; }
5286 | /*EMPTY*/ { $$ = NULL; }
5295 attr: relation_name '.' attrs opt_indirection
5297 $$ = makeNode(Attr);
5301 $$->indirection = $4;
5303 | ParamNo '.' attrs opt_indirection
5305 $$ = makeNode(Attr);
5309 $$->indirection = $4;
5314 { $$ = makeList1(makeString($1)); }
5315 | attrs '.' attr_name
5316 { $$ = lappend($1, makeString($3)); }
5318 { $$ = lappend($1, makeString("*")); }
5322 /*****************************************************************************
5326 *****************************************************************************/
5328 /* Target lists as found in SELECT ... and INSERT VALUES ( ... ) */
5330 target_list: target_list ',' target_el
5331 { $$ = lappend($1, $3); }
5333 { $$ = makeList1($1); }
5336 /* AS is not optional because shift/red conflict with unary ops */
5337 target_el: a_expr AS ColLabel
5339 $$ = makeNode(ResTarget);
5341 $$->indirection = NULL;
5342 $$->val = (Node *)$1;
5346 $$ = makeNode(ResTarget);
5348 $$->indirection = NULL;
5349 $$->val = (Node *)$1;
5351 | relation_name '.' '*'
5353 Attr *att = makeNode(Attr);
5355 att->paramNo = NULL;
5356 att->attrs = makeList1(makeString("*"));
5357 att->indirection = NIL;
5358 $$ = makeNode(ResTarget);
5360 $$->indirection = NULL;
5361 $$->val = (Node *)att;
5365 Attr *att = makeNode(Attr);
5367 att->paramNo = NULL;
5369 att->indirection = NIL;
5370 $$ = makeNode(ResTarget);
5372 $$->indirection = NULL;
5373 $$->val = (Node *)att;
5377 /* Target list as found in UPDATE table SET ... */
5379 update_target_list: update_target_list ',' update_target_el
5380 { $$ = lappend($1,$3); }
5382 { $$ = makeList1($1); }
5385 update_target_el: ColId opt_indirection '=' a_expr
5387 $$ = makeNode(ResTarget);
5389 $$->indirection = $2;
5390 $$->val = (Node *)$4;
5394 /*****************************************************************************
5396 * Names and constants
5398 *****************************************************************************/
5400 relation_name: SpecialRuleRelation
5406 /* disallow refs to variable system tables */
5407 if (strcmp(LogRelationName, $1) == 0)
5408 elog(ERROR,"%s cannot be accessed by users",$1);
5414 name: ColId { $$ = $1; };
5415 database_name: ColId { $$ = $1; };
5416 access_method: ColId { $$ = $1; };
5417 attr_name: ColId { $$ = $1; };
5418 class: ColId { $$ = $1; };
5419 index_name: ColId { $$ = $1; };
5422 * Include date/time keywords as SQL92 extension.
5423 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
5424 * Any tokens which show up as operators will screw up the parsing if
5425 * allowed as identifiers, but are acceptable as ColLabels:
5426 * BETWEEN, IN, IS, ISNULL, NOTNULL, OVERLAPS
5427 * Thanks to Tom Lane for pointing this out. - thomas 2000-03-29
5428 * We need OVERLAPS allowed as a function name to enable the implementation
5429 * of argument type variations on the underlying implementation. These
5430 * variations are done as SQL-language entries in the pg_proc catalog.
5431 * Do not include SUBSTRING here since it has explicit productions
5432 * in a_expr to support the goofy SQL9x argument syntax.
5433 * - thomas 2000-11-28
5435 func_name: ColId { $$ = xlateSqlFunc($1); }
5436 | BETWEEN { $$ = xlateSqlFunc("between"); }
5437 | ILIKE { $$ = xlateSqlFunc("ilike"); }
5438 | IN { $$ = xlateSqlFunc("in"); }
5439 | IS { $$ = xlateSqlFunc("is"); }
5440 | ISNULL { $$ = xlateSqlFunc("isnull"); }
5441 | LIKE { $$ = xlateSqlFunc("like"); }
5442 | NOTNULL { $$ = xlateSqlFunc("notnull"); }
5443 | OVERLAPS { $$ = xlateSqlFunc("overlaps"); }
5446 file_name: Sconst { $$ = $1; };
5449 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
5453 A_Const *n = makeNode(A_Const);
5454 n->val.type = T_Integer;
5455 n->val.val.ival = $1;
5460 A_Const *n = makeNode(A_Const);
5461 n->val.type = T_Float;
5462 n->val.val.str = $1;
5467 A_Const *n = makeNode(A_Const);
5468 n->val.type = T_String;
5469 n->val.val.str = $1;
5474 A_Const *n = makeNode(A_Const);
5475 n->val.type = T_BitString;
5476 n->val.val.str = $1;
5479 /* This rule formerly used Typename,
5480 * but that causes reduce conflicts with subscripted column names.
5481 * Now, separate into ConstTypename and ConstInterval,
5482 * to allow implementing the SQL92 syntax for INTERVAL literals.
5483 * - thomas 2000-06-24
5485 | ConstTypename Sconst
5487 A_Const *n = makeNode(A_Const);
5489 n->val.type = T_String;
5490 n->val.val.str = $2;
5493 | ConstInterval Sconst opt_interval
5495 A_Const *n = makeNode(A_Const);
5497 n->val.type = T_String;
5498 n->val.val.str = $2;
5502 { $$ = (Node *)$1; }
5505 A_Const *n = makeNode(A_Const);
5506 n->val.type = T_String;
5507 n->val.val.str = "t";
5508 n->typename = makeNode(TypeName);
5509 n->typename->name = xlateSqlType("bool");
5510 n->typename->typmod = -1;
5515 A_Const *n = makeNode(A_Const);
5516 n->val.type = T_String;
5517 n->val.val.str = "f";
5518 n->typename = makeNode(TypeName);
5519 n->typename->name = xlateSqlType("bool");
5520 n->typename->typmod = -1;
5525 A_Const *n = makeNode(A_Const);
5526 n->val.type = T_Null;
5531 ParamNo: PARAM opt_indirection
5533 $$ = makeNode(ParamNo);
5535 $$->indirection = $2;
5539 Iconst: ICONST { $$ = $1; };
5540 Sconst: SCONST { $$ = $1; };
5541 UserId: ColId { $$ = $1; };
5543 /* Column identifier
5544 * Include date/time keywords as SQL92 extension.
5545 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
5546 * Add other keywords. Note that as the syntax expands,
5547 * some of these keywords will have to be removed from this
5548 * list due to shift/reduce conflicts in yacc. If so, move
5549 * down to the ColLabel entity. - thomas 1997-11-06
5551 ColId: IDENT { $$ = $1; }
5552 | datetime { $$ = $1; }
5553 | TokenId { $$ = $1; }
5554 | INTERVAL { $$ = "interval"; }
5555 | NATIONAL { $$ = "national"; }
5556 | NONE { $$ = "none"; }
5557 | PATH_P { $$ = "path"; }
5558 | SERIAL { $$ = "serial"; }
5559 | TIME { $$ = "time"; }
5560 | TIMESTAMP { $$ = "timestamp"; }
5563 /* Parser tokens to be used as identifiers.
5564 * Tokens involving data types should appear in ColId only,
5565 * since they will conflict with real TypeName productions.
5567 TokenId: ABSOLUTE { $$ = "absolute"; }
5568 | ACCESS { $$ = "access"; }
5569 | ACTION { $$ = "action"; }
5570 | ADD { $$ = "add"; }
5571 | AFTER { $$ = "after"; }
5572 | AGGREGATE { $$ = "aggregate"; }
5573 | ALTER { $$ = "alter"; }
5575 | AUTHORIZATION { $$ = "authorization"; }
5576 | BACKWARD { $$ = "backward"; }
5577 | BEFORE { $$ = "before"; }
5578 | BEGIN_TRANS { $$ = "begin"; }
5580 | CACHE { $$ = "cache"; }
5581 | CASCADE { $$ = "cascade"; }
5582 | CHAIN { $$ = "chain"; }
5583 | CHARACTERISTICS { $$ = "characteristics"; }
5584 | CHECKPOINT { $$ = "checkpoint"; }
5585 | CLOSE { $$ = "close"; }
5586 | COMMENT { $$ = "comment"; }
5587 | COMMIT { $$ = "commit"; }
5588 | COMMITTED { $$ = "committed"; }
5589 | CONSTRAINTS { $$ = "constraints"; }
5590 | CREATE { $$ = "create"; }
5591 | CREATEDB { $$ = "createdb"; }
5592 | CREATEUSER { $$ = "createuser"; }
5593 | CURSOR { $$ = "cursor"; }
5594 | CYCLE { $$ = "cycle"; }
5595 | DATABASE { $$ = "database"; }
5596 | DECLARE { $$ = "declare"; }
5597 | DEFERRED { $$ = "deferred"; }
5598 | DELETE { $$ = "delete"; }
5599 | DELIMITERS { $$ = "delimiters"; }
5600 | DOUBLE { $$ = "double"; }
5601 | DROP { $$ = "drop"; }
5602 | EACH { $$ = "each"; }
5603 | ENCODING { $$ = "encoding"; }
5604 | ESCAPE { $$ = "escape"; }
5605 | EXCLUSIVE { $$ = "exclusive"; }
5606 | EXECUTE { $$ = "execute"; }
5607 | FETCH { $$ = "fetch"; }
5608 | FORCE { $$ = "force"; }
5609 | FORWARD { $$ = "forward"; }
5610 | FUNCTION { $$ = "function"; }
5611 | GRANT { $$ = "grant"; }
5612 | HANDLER { $$ = "handler"; }
5613 | IMMEDIATE { $$ = "immediate"; }
5614 | INCREMENT { $$ = "increment"; }
5615 | INDEX { $$ = "index"; }
5616 | INHERITS { $$ = "inherits"; }
5617 | INSENSITIVE { $$ = "insensitive"; }
5618 | INSERT { $$ = "insert"; }
5619 | INSTEAD { $$ = "instead"; }
5620 | ISOLATION { $$ = "isolation"; }
5621 | KEY { $$ = "key"; }
5622 | LANGUAGE { $$ = "language"; }
5623 | LANCOMPILER { $$ = "lancompiler"; }
5624 | LEVEL { $$ = "level"; }
5625 | LOCATION { $$ = "location"; }
5626 | MATCH { $$ = "match"; }
5627 | MAXVALUE { $$ = "maxvalue"; }
5628 | MINVALUE { $$ = "minvalue"; }
5629 | MODE { $$ = "mode"; }
5630 | NAMES { $$ = "names"; }
5631 | NEXT { $$ = "next"; }
5633 | NOCREATEDB { $$ = "nocreatedb"; }
5634 | NOCREATEUSER { $$ = "nocreateuser"; }
5635 | NOTHING { $$ = "nothing"; }
5636 | NOTIFY { $$ = "notify"; }
5638 | OIDS { $$ = "oids"; }
5639 | OPERATOR { $$ = "operator"; }
5640 | OPTION { $$ = "option"; }
5641 | OWNER { $$ = "owner"; }
5642 | PARTIAL { $$ = "partial"; }
5643 | PASSWORD { $$ = "password"; }
5644 | PENDANT { $$ = "pendant"; }
5645 | PRIOR { $$ = "prior"; }
5646 | PRIVILEGES { $$ = "privileges"; }
5647 | PROCEDURAL { $$ = "procedural"; }
5648 | PROCEDURE { $$ = "procedure"; }
5649 | READ { $$ = "read"; }
5650 | REINDEX { $$ = "reindex"; }
5651 | RELATIVE { $$ = "relative"; }
5652 | RENAME { $$ = "rename"; }
5653 | RESTRICT { $$ = "restrict"; }
5654 | RETURNS { $$ = "returns"; }
5655 | REVOKE { $$ = "revoke"; }
5656 | ROLLBACK { $$ = "rollback"; }
5657 | ROW { $$ = "row"; }
5658 | RULE { $$ = "rule"; }
5659 | SCHEMA { $$ = "schema"; }
5660 | SCROLL { $$ = "scroll"; }
5661 | SESSION { $$ = "session"; }
5662 | SEQUENCE { $$ = "sequence"; }
5663 | SERIALIZABLE { $$ = "serializable"; }
5664 | SET { $$ = "set"; }
5665 | SHARE { $$ = "share"; }
5666 | START { $$ = "start"; }
5667 | STATEMENT { $$ = "statement"; }
5668 | STATISTICS { $$ = "statistics"; }
5669 | STDIN { $$ = "stdin"; }
5670 | STDOUT { $$ = "stdout"; }
5671 | SYSID { $$ = "sysid"; }
5672 | TEMP { $$ = "temp"; }
5673 | TEMPLATE { $$ = "template"; }
5674 | TEMPORARY { $$ = "temporary"; }
5675 | TIMEZONE_HOUR { $$ = "timezone_hour"; }
5676 | TIMEZONE_MINUTE { $$ = "timezone_minute"; }
5677 | TOAST { $$ = "toast"; }
5678 | TRIGGER { $$ = "trigger"; }
5679 | TRUNCATE { $$ = "truncate"; }
5680 | TRUSTED { $$ = "trusted"; }
5681 | TYPE_P { $$ = "type"; }
5682 | UNLISTEN { $$ = "unlisten"; }
5683 | UNTIL { $$ = "until"; }
5684 | UPDATE { $$ = "update"; }
5685 | VALID { $$ = "valid"; }
5686 | VALUES { $$ = "values"; }
5687 | VARYING { $$ = "varying"; }
5688 | VERSION { $$ = "version"; }
5689 | VIEW { $$ = "view"; }
5690 | WITH { $$ = "with"; }
5691 | WITHOUT { $$ = "without"; }
5692 | WORK { $$ = "work"; }
5693 | ZONE { $$ = "zone"; }
5697 * Allowed labels in "AS" clauses.
5698 * Include TRUE/FALSE SQL3 reserved words for Postgres backward
5699 * compatibility. Cannot allow this for column names since the
5700 * syntax would not distinguish between the constant value and
5701 * a column name. - thomas 1997-10-24
5702 * Add other keywords to this list. Note that they appear here
5703 * rather than in ColId if there was a shift/reduce conflict
5704 * when used as a full identifier. - thomas 1997-11-06
5706 ColLabel: ColId { $$ = $1; }
5707 | ABORT_TRANS { $$ = "abort"; }
5708 | ALL { $$ = "all"; }
5709 | ANALYSE { $$ = "analyse"; } /* British */
5710 | ANALYZE { $$ = "analyze"; }
5711 | AND { $$ = "and"; }
5712 | ANY { $$ = "any"; }
5713 | ASC { $$ = "asc"; }
5714 | BETWEEN { $$ = "between"; }
5715 | BINARY { $$ = "binary"; }
5716 | BIT { $$ = "bit"; }
5717 | BOTH { $$ = "both"; }
5718 | CASE { $$ = "case"; }
5719 | CAST { $$ = "cast"; }
5720 | CHAR { $$ = "char"; }
5721 | CHARACTER { $$ = "character"; }
5722 | CHECK { $$ = "check"; }
5723 | CLUSTER { $$ = "cluster"; }
5724 | COALESCE { $$ = "coalesce"; }
5725 | COLLATE { $$ = "collate"; }
5726 | COLUMN { $$ = "column"; }
5727 | CONSTRAINT { $$ = "constraint"; }
5728 | COPY { $$ = "copy"; }
5729 | CROSS { $$ = "cross"; }
5730 | CURRENT_DATE { $$ = "current_date"; }
5731 | CURRENT_TIME { $$ = "current_time"; }
5732 | CURRENT_TIMESTAMP { $$ = "current_timestamp"; }
5733 | CURRENT_USER { $$ = "current_user"; }
5734 | DEC { $$ = "dec"; }
5735 | DECIMAL { $$ = "decimal"; }
5736 | DEFAULT { $$ = "default"; }
5737 | DEFERRABLE { $$ = "deferrable"; }
5738 | DESC { $$ = "desc"; }
5739 | DISTINCT { $$ = "distinct"; }
5741 | ELSE { $$ = "else"; }
5742 | END_TRANS { $$ = "end"; }
5743 | EXCEPT { $$ = "except"; }
5744 | EXISTS { $$ = "exists"; }
5745 | EXPLAIN { $$ = "explain"; }
5746 | EXTEND { $$ = "extend"; }
5747 | EXTRACT { $$ = "extract"; }
5748 | FALSE_P { $$ = "false"; }
5749 | FLOAT { $$ = "float"; }
5750 | FOR { $$ = "for"; }
5751 | FOREIGN { $$ = "foreign"; }
5752 | FROM { $$ = "from"; }
5753 | FULL { $$ = "full"; }
5754 | GLOBAL { $$ = "global"; }
5755 | GROUP { $$ = "group"; }
5756 | HAVING { $$ = "having"; }
5757 | ILIKE { $$ = "ilike"; }
5758 | INITIALLY { $$ = "initially"; }
5760 | INNER_P { $$ = "inner"; }
5761 | INTERSECT { $$ = "intersect"; }
5762 | INTO { $$ = "into"; }
5763 | INOUT { $$ = "inout"; }
5765 | ISNULL { $$ = "isnull"; }
5766 | JOIN { $$ = "join"; }
5767 | LEADING { $$ = "leading"; }
5768 | LEFT { $$ = "left"; }
5769 | LIKE { $$ = "like"; }
5770 | LIMIT { $$ = "limit"; }
5771 | LISTEN { $$ = "listen"; }
5772 | LOAD { $$ = "load"; }
5773 | LOCAL { $$ = "local"; }
5774 | LOCK_P { $$ = "lock"; }
5775 | MOVE { $$ = "move"; }
5776 | NATURAL { $$ = "natural"; }
5777 | NCHAR { $$ = "nchar"; }
5778 | NEW { $$ = "new"; }
5779 | NOT { $$ = "not"; }
5780 | NOTNULL { $$ = "notnull"; }
5781 | NULLIF { $$ = "nullif"; }
5782 | NULL_P { $$ = "null"; }
5783 | NUMERIC { $$ = "numeric"; }
5784 | OFF { $$ = "off"; }
5785 | OFFSET { $$ = "offset"; }
5786 | OLD { $$ = "old"; }
5788 | ONLY { $$ = "only"; }
5790 | ORDER { $$ = "order"; }
5791 | OUT { $$ = "out"; }
5792 | OUTER_P { $$ = "outer"; }
5793 | OVERLAPS { $$ = "overlaps"; }
5794 | POSITION { $$ = "position"; }
5795 | PRECISION { $$ = "precision"; }
5796 | PRIMARY { $$ = "primary"; }
5797 | PUBLIC { $$ = "public"; }
5798 | REFERENCES { $$ = "references"; }
5799 | RESET { $$ = "reset"; }
5800 | RIGHT { $$ = "right"; }
5801 | SELECT { $$ = "select"; }
5802 | SESSION_USER { $$ = "session_user"; }
5803 | SETOF { $$ = "setof"; }
5804 | SHOW { $$ = "show"; }
5805 | SOME { $$ = "some"; }
5806 | SUBSTRING { $$ = "substring"; }
5807 | TABLE { $$ = "table"; }
5808 | THEN { $$ = "then"; }
5810 | TRAILING { $$ = "trailing"; }
5811 | TRANSACTION { $$ = "transaction"; }
5812 | TRIM { $$ = "trim"; }
5813 | TRUE_P { $$ = "true"; }
5814 | UNION { $$ = "union"; }
5815 | UNIQUE { $$ = "unique"; }
5816 | UNKNOWN { $$ = "unknown"; }
5817 | USER { $$ = "user"; }
5818 | USING { $$ = "using"; }
5819 | VACUUM { $$ = "vacuum"; }
5820 | VARCHAR { $$ = "varchar"; }
5821 | VERBOSE { $$ = "verbose"; }
5822 | WHEN { $$ = "when"; }
5823 | WHERE { $$ = "where"; }
5826 SpecialRuleRelation: OLD
5831 elog(ERROR,"OLD used in non-rule query");
5838 elog(ERROR,"NEW used in non-rule query");
5845 makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
5847 A_Expr *a = makeNode(A_Expr);
5856 makeTypeCast(Node *arg, TypeName *typename)
5859 * If arg is an A_Const or ParamNo, just stick the typename into the
5860 * field reserved for it --- unless there's something there already!
5861 * (We don't want to collapse x::type1::type2 into just x::type2.)
5862 * Otherwise, generate a TypeCast node.
5864 if (IsA(arg, A_Const) &&
5865 ((A_Const *) arg)->typename == NULL)
5867 ((A_Const *) arg)->typename = typename;
5870 else if (IsA(arg, ParamNo) &&
5871 ((ParamNo *) arg)->typename == NULL)
5873 ((ParamNo *) arg)->typename = typename;
5878 TypeCast *n = makeNode(TypeCast);
5880 n->typename = typename;
5886 * Generate separate operator nodes for a single row descriptor expression.
5887 * Perhaps this should go deeper in the parser someday...
5888 * - thomas 1997-12-22
5891 makeRowExpr(char *opr, List *largs, List *rargs)
5896 if (length(largs) != length(rargs))
5897 elog(ERROR,"Unequal number of entries in row expression");
5899 if (lnext(largs) != NIL)
5900 expr = makeRowExpr(opr,lnext(largs),lnext(rargs));
5902 larg = lfirst(largs);
5903 rarg = lfirst(rargs);
5905 if ((strcmp(opr, "=") == 0)
5906 || (strcmp(opr, "<") == 0)
5907 || (strcmp(opr, "<=") == 0)
5908 || (strcmp(opr, ">") == 0)
5909 || (strcmp(opr, ">=") == 0))
5912 expr = makeA_Expr(OP, opr, larg, rarg);
5914 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
5916 else if (strcmp(opr, "<>") == 0)
5919 expr = makeA_Expr(OP, opr, larg, rarg);
5921 expr = makeA_Expr(OR, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
5925 elog(ERROR,"Operator '%s' not implemented for row expressions",opr);
5932 mapTargetColumns(List *src, List *dst)
5937 if (length(src) != length(dst))
5938 elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
5940 while ((src != NIL) && (dst != NIL))
5942 s = (ColumnDef *)lfirst(src);
5943 d = (ResTarget *)lfirst(dst);
5945 d->name = s->colname;
5950 } /* mapTargetColumns() */
5953 /* findLeftmostSelect()
5954 * Find the leftmost component SelectStmt in a set-operation parsetree.
5957 findLeftmostSelect(SelectStmt *node)
5959 while (node && node->op != SETOP_NONE)
5961 Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
5965 /* insertSelectOptions()
5966 * Insert ORDER BY, etc into an already-constructed SelectStmt.
5968 * This routine is just to avoid duplicating code in SelectStmt productions.
5971 insertSelectOptions(SelectStmt *stmt,
5972 List *sortClause, List *forUpdate,
5973 Node *limitOffset, Node *limitCount)
5976 * Tests here are to reject constructs like
5977 * (SELECT foo ORDER BY bar) ORDER BY baz
5981 if (stmt->sortClause)
5982 elog(ERROR, "Multiple ORDER BY clauses not allowed");
5983 stmt->sortClause = sortClause;
5987 if (stmt->forUpdate)
5988 elog(ERROR, "Multiple FOR UPDATE clauses not allowed");
5989 stmt->forUpdate = forUpdate;
5993 if (stmt->limitOffset)
5994 elog(ERROR, "Multiple OFFSET clauses not allowed");
5995 stmt->limitOffset = limitOffset;
5999 if (stmt->limitCount)
6000 elog(ERROR, "Multiple LIMIT clauses not allowed");
6001 stmt->limitCount = limitCount;
6006 makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
6008 SelectStmt *n = makeNode(SelectStmt);
6012 n->larg = (SelectStmt *) larg;
6013 n->rarg = (SelectStmt *) rarg;
6019 * Convert alternate function names to internal Postgres functions.
6021 * Do not convert "float", since that is handled elsewhere
6022 * for FLOAT(p) syntax.
6024 * Converting "datetime" to "timestamp" and "timespan" to "interval"
6025 * is a temporary expedient for pre-7.0 to 7.0 compatibility;
6026 * these should go away for v7.1.
6029 xlateSqlFunc(char *name)
6031 if (strcmp(name,"character_length") == 0)
6032 return "char_length";
6033 else if (strcmp(name,"datetime") == 0)
6035 else if (strcmp(name,"timespan") == 0)
6039 } /* xlateSqlFunc() */
6042 * Convert alternate type names to internal Postgres types.
6044 * NB: do NOT put "char" -> "bpchar" here, because that renders it impossible
6045 * to refer to our single-byte char type, even with quotes. (Without quotes,
6046 * CHAR is a keyword, and the code above produces "bpchar" for it.)
6048 * Convert "datetime" and "timespan" to allow a transition to SQL92 type names.
6049 * Remove this translation for v7.1 - thomas 2000-03-25
6051 * Convert "lztext" to "text" to allow forward compatibility for anyone using
6052 * the undocumented "lztext" type in 7.0. This can go away in 7.2 or later
6056 xlateSqlType(char *name)
6058 if ((strcmp(name,"int") == 0)
6059 || (strcmp(name,"integer") == 0))
6061 else if (strcmp(name, "smallint") == 0)
6063 else if (strcmp(name, "bigint") == 0)
6065 else if (strcmp(name, "real") == 0)
6067 else if (strcmp(name, "float") == 0)
6069 else if (strcmp(name, "decimal") == 0)
6071 else if (strcmp(name, "datetime") == 0)
6073 else if (strcmp(name, "timespan") == 0)
6075 else if (strcmp(name, "lztext") == 0)
6077 else if (strcmp(name, "boolean") == 0)
6081 } /* xlateSqlType() */
6084 void parser_init(Oid *typev, int nargs)
6086 QueryIsRule = FALSE;
6088 * Keep enough information around to fill out the type of param nodes
6089 * used in postquel functions
6091 param_type_info = typev;
6092 pfunc_num_args = nargs;
6095 Oid param_type(int t)
6097 if ((t > pfunc_num_args) || (t <= 0))
6099 return param_type_info[t - 1];
6103 * Test whether an a_expr is a plain NULL constant or not.
6106 exprIsNullConstant(Node *arg)
6108 if (arg && IsA(arg, A_Const))
6110 A_Const *con = (A_Const *) arg;
6112 if (con->val.type == T_Null &&
6113 con->typename == NULL)
6120 * doNegate --- handle negation of a numeric constant.
6122 * Formerly, we did this here because the optimizer couldn't cope with
6123 * indexquals that looked like "var = -4" --- it wants "var = const"
6124 * and a unary minus operator applied to a constant didn't qualify.
6125 * As of Postgres 7.0, that problem doesn't exist anymore because there
6126 * is a constant-subexpression simplifier in the optimizer. However,
6127 * there's still a good reason for doing this here, which is that we can
6128 * postpone committing to a particular internal representation for simple
6129 * negative constants. It's better to leave "-123.456" in string form
6130 * until we know what the desired type is.
6135 if (IsA(n, A_Const))
6137 A_Const *con = (A_Const *)n;
6139 if (con->val.type == T_Integer)
6141 con->val.val.ival = -con->val.val.ival;
6144 if (con->val.type == T_Float)
6146 doNegateFloat(&con->val);
6151 return makeA_Expr(OP, "-", NULL, n);
6155 doNegateFloat(Value *v)
6157 char *oldval = v->val.str;
6159 Assert(IsA(v, Float));
6163 v->val.str = oldval+1; /* just strip the '-' */
6166 char *newval = (char *) palloc(strlen(oldval) + 2);
6169 strcpy(newval+1, oldval);
6170 v->val.str = newval;