4 /*-------------------------------------------------------------------------
7 * POSTGRES SQL YACC rules/actions
9 * Copyright (c) 1994, Regents of the University of California
13 * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.106 1999/10/03 23:55:30 tgl Exp $
16 * AUTHOR DATE MAJOR EVENT
17 * Andrew Yu Sept, 1994 POSTQUEL to SQL conversion
18 * Andrew Yu Oct, 1994 lispy code conversion
21 * CAPITALS are used to represent terminal symbols.
22 * non-capitals are used to represent non-terminals.
23 * SQL92-specific syntax is separated from plain SQL/Postgres syntax
24 * to help isolate the non-extensible portions of the parser.
26 * if you use list, make sure the datum is a node so that the printing
30 * sometimes we assign constants to makeStrings. Make sure we don't free
33 *-------------------------------------------------------------------------
38 #include "access/htup.h"
39 #include "access/xact.h"
40 #include "catalog/catname.h"
41 #include "catalog/pg_type.h"
42 #include "nodes/parsenodes.h"
43 #include "nodes/print.h"
44 #include "parser/analyze.h"
45 #include "parser/gramparse.h"
46 #include "parser/parse_type.h"
47 #include "storage/bufpage.h"
48 #include "storage/lmgr.h"
49 #include "utils/acl.h"
50 #include "utils/numeric.h"
53 #include "mb/pg_wchar.h"
56 static char saved_relname[NAMEDATALEN]; /* need this for complex attributes */
57 static bool QueryIsRule = FALSE;
58 static Oid *param_type_info;
59 static int pfunc_num_args;
60 extern List *parsetree;
64 * If you need access to certain yacc-generated variables and find that
65 * they're static by default, uncomment the next line. (this is not a
68 /*#define __YYSCLASS*/
70 static char *xlateSqlFunc(char *);
71 static char *xlateSqlType(char *);
72 static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
73 static Node *makeRowExpr(char *opr, List *largs, List *rargs);
74 static void mapTargetColumns(List *source, List *target);
75 static char *fmtId(char *rawid);
76 static void param_type_init(Oid *typev, int nargs);
77 static Node *doNegate(Node *n);
79 Oid param_type(int t); /* used in parse_expr.c */
81 /* old versions of flex define this as a macro */
95 bool* pboolean; /* for pg_shadow privileges */
104 SortGroupBy *sortgroupby;
120 AddAttrStmt, ClosePortalStmt,
121 CopyStmt, CreateStmt, CreateAsStmt, CreateSeqStmt, DefineStmt, DestroyStmt,
123 ExtendStmt, FetchStmt, GrantStmt, CreateTrigStmt, DropTrigStmt,
124 CreatePLangStmt, DropPLangStmt,
125 IndexStmt, ListenStmt, UnlistenStmt, LockStmt, OptimizableStmt,
126 ProcedureStmt, RemoveAggrStmt, RemoveOperStmt,
127 RemoveFuncStmt, RemoveStmt,
128 RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
129 CreatedbStmt, DestroydbStmt, VacuumStmt, CursorStmt, SubSelect,
130 UpdateStmt, InsertStmt, select_clause, SelectStmt, NotifyStmt, DeleteStmt,
131 ClusterStmt, ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
132 CreateUserStmt, AlterUserStmt, DropUserStmt, RuleActionStmt,
135 %type <str> opt_database1, opt_database2, location, encoding
137 %type <ival> opt_lock, lock_type
138 %type <boolean> opt_lmode
140 %type <pboolean> user_createdb_clause, user_createuser_clause
141 %type <str> user_passwd_clause
142 %type <str> user_valid_clause
143 %type <list> user_group_list, user_group_clause
145 %type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
147 %type <ival> OptConstrTrigDeferrable, OptConstrTrigInitdeferred
148 %type <str> OptConstrFromTable
150 %type <str> TriggerEvents, TriggerFuncArg
152 %type <str> relation_name, copy_file_name, copy_delimiter, def_name,
153 database_name, access_method_clause, access_method, attr_name,
154 class, index_name, name, func_name, file_name, aggr_argtype
156 %type <str> opt_id, opt_portal_name,
157 all_Op, MathOp, opt_name, opt_unique,
158 OptUseOp, opt_class, SpecialRuleRelation
160 %type <str> opt_level
161 %type <str> privileges, operation_commalist, grantee
162 %type <chr> operation, TriggerOneEvent
164 %type <list> stmtblock, stmtmulti,
165 result, relation_name_list, OptTableElementList,
166 OptInherit, definition,
167 opt_with, func_args, func_args_list, func_as,
168 oper_argtypes, RuleActionList, RuleActionBlock, RuleActionMulti,
169 opt_column_list, columnList, opt_va_list, va_list,
170 sort_clause, sortby_list, index_params, index_list, name_list,
171 from_clause, from_expr, table_list, opt_array_bounds,
172 expr_list, attrs, target_list, update_target_list,
173 def_list, opt_indirection, group_clause, TriggerFuncArgs,
176 %type <node> func_return
177 %type <boolean> set_opt
179 %type <boolean> TriggerForOpt, TriggerForType, OptTemp, OptTempType, OptTempScope
181 %type <list> for_update_clause, update_list
182 %type <boolean> opt_union
183 %type <boolean> opt_table
184 %type <boolean> opt_trans
186 %type <list> join_clause_with_union, join_clause, join_list, join_qual, using_list
187 %type <node> join_expr, using_expr
188 %type <str> join_outer
189 %type <ival> join_type
191 %type <list> extract_list, position_list
192 %type <list> substr_list, substr_from, substr_for, trim_list
193 %type <list> opt_interval
195 %type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
196 index_opt_unique, opt_verbose, opt_analyze
197 %type <boolean> opt_cursor
199 %type <ival> copy_dirn, def_type, opt_direction, remove_type,
202 %type <ival> fetch_how_many
204 %type <node> select_limit_value, select_offset_value
206 %type <list> OptSeqList
207 %type <defelt> OptSeqElem
209 %type <dstmt> def_rest
210 %type <astmt> insert_rest
212 %type <node> OptTableElement, ConstraintElem
213 %type <node> columnDef, alter_clause
214 %type <defelt> def_elem
215 %type <node> def_arg, columnElem, where_clause,
216 a_expr, a_expr_or_null, b_expr, AexprConst,
217 in_expr, having_clause
218 %type <list> row_descriptor, row_list, in_expr_nodes
219 %type <node> row_expr
221 %type <node> case_expr, case_arg, when_clause, case_default
222 %type <list> when_clause_list
223 %type <ival> sub_type
224 %type <list> OptCreateAs, CreateAsList
225 %type <node> CreateAsElement
226 %type <value> NumericOnly, FloatOnly, IntegerOnly
227 %type <attr> event_object, attr
228 %type <sortgroupby> sortby
229 %type <ielem> index_elem, func_index
230 %type <range> table_expr
231 %type <relexp> relation_expr
232 %type <target> target_el, update_target_el
233 %type <paramno> ParamNo
235 %type <typnam> Typename, opt_type, SimpleTypename,
236 Generic, Numeric, Character, Datetime
237 %type <str> generic, numeric, character, datetime
238 %type <str> extract_arg
239 %type <str> opt_charset, opt_collate
240 %type <str> opt_float
241 %type <ival> opt_numeric, opt_decimal
242 %type <boolean> opt_varying, opt_timezone
246 %type <str> UserId, var_value, zone_value
247 %type <str> ColId, ColLabel
250 %type <node> TableConstraint
251 %type <list> ColPrimaryKey, ColQualList, ColQualifier
252 %type <node> ColConstraint, ColConstraintElem
253 %type <list> key_actions, key_action
254 %type <str> key_match, key_reference
256 %type <list> constraints_set_list
257 %type <list> constraints_set_namelist
258 %type <boolean> constraints_set_mode
261 * If you make any token changes, remember to:
262 * - use "yacc -d" and update parse.h
263 * - update the keyword table in parser/keywords.c
266 /* Reserved word tokens
267 * SQL92 syntax has many type-specific constructs.
268 * So, go ahead and make these types reserved words,
269 * and call-out the syntax explicitly.
270 * This gets annoying when trying to also retain Postgres' nice
271 * type-extensible features, but we don't really have a choice.
272 * - thomas 1997-10-11
275 /* Keywords (in SQL92 reserved words) */
276 %token ABSOLUTE, ACTION, ADD, ALL, ALTER, AND, ANY, AS, ASC,
277 BEGIN_TRANS, BETWEEN, BOTH, BY,
278 CASCADE, CASE, CAST, CHAR, CHARACTER, CHECK, CLOSE,
279 COALESCE, COLLATE, COLUMN, COMMIT,
280 CONSTRAINT, CONSTRAINTS, CREATE, CROSS, CURRENT, CURRENT_DATE,
281 CURRENT_TIME, CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
282 DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
283 ELSE, END_TRANS, EXCEPT, EXECUTE, EXISTS, EXTRACT,
284 FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
285 GLOBAL, GRANT, GROUP, HAVING, HOUR_P,
286 IN, INNER_P, INSENSITIVE, INSERT, INTERSECT, INTERVAL, INTO, IS,
287 ISOLATION, JOIN, KEY, LANGUAGE, LEADING, LEFT, LEVEL, LIKE, LOCAL,
288 MATCH, MINUTE_P, MONTH_P, NAMES,
289 NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULLIF, NULL_P, NUMERIC,
290 OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
291 PARTIAL, POSITION, PRECISION, PRIMARY, PRIOR, PRIVILEGES, PROCEDURE, PUBLIC,
292 READ, REFERENCES, RELATIVE, REVOKE, RIGHT, ROLLBACK,
293 SCROLL, SECOND_P, SELECT, SET, SUBSTRING,
294 TABLE, TEMP, TEMPORARY, THEN, TIME, TIMESTAMP, TIMEZONE_HOUR,
295 TIMEZONE_MINUTE, TO, TRAILING, TRANSACTION, TRIM, TRUE_P,
296 UNION, UNIQUE, UPDATE, USER, USING,
297 VALUES, VARCHAR, VARYING, VIEW,
298 WHEN, WHERE, WITH, WORK, YEAR_P, ZONE
300 /* Keywords (in SQL3 reserved words) */
301 %token DEFERRABLE, DEFERRED,
302 IMMEDIATE, INITIALLY,
307 /* Keywords (in SQL92 non-reserved words) */
308 %token COMMITTED, SERIALIZABLE, TYPE_P
310 /* Keywords for Postgres support (not in SQL92 reserved words)
312 * The CREATEDB and CREATEUSER tokens should go away
313 * when some sort of pg_privileges relation is introduced.
314 * - Todd A. Brandys 1998-01-01?
316 %token ABORT_TRANS, ACCESS, AFTER, AGGREGATE, ANALYZE,
317 BACKWARD, BEFORE, BINARY,
318 CACHE, CLUSTER, COPY, CREATEDB, CREATEUSER, CYCLE,
319 DATABASE, DELIMITERS, DO,
320 EACH, ENCODING, EXCLUSIVE, EXPLAIN, EXTEND,
321 FORWARD, FUNCTION, HANDLER,
322 INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
323 LANCOMPILER, LIMIT, LISTEN, LOAD, LOCATION, LOCK_P,
324 MAXVALUE, MINVALUE, MODE, MOVE,
325 NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
326 OFFSET, OIDS, OPERATOR, PASSWORD, PROCEDURAL,
327 RENAME, RESET, RETURNS, ROW, RULE,
328 SEQUENCE, SERIAL, SETOF, SHARE, SHOW, START, STATEMENT, STDIN, STDOUT,
330 UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
332 /* Special keywords, not in the query language - see the "lex" file */
333 %token <str> IDENT, SCONST, Op
334 %token <ival> ICONST, PARAM
337 /* these are not real. they are here so that they get generated as #define's*/
349 %left Op /* multi-character ops and user-defined operators */
357 %left '|' /* this is the relation union op, not logical or */
358 /* Unary Operators */
360 %left ';' /* end of statement or natural log */
365 %left UNION INTERSECT EXCEPT
368 stmtblock: stmtmulti opt_semi
372 stmtmulti: stmtmulti ';' stmt
373 { $$ = lappend($1, $3); }
375 { $$ = lcons($1,NIL); }
428 /*****************************************************************************
430 * Create a new Postgres DBMS user
433 *****************************************************************************/
435 CreateUserStmt: CREATE USER UserId user_passwd_clause user_createdb_clause
436 user_createuser_clause user_group_clause user_valid_clause
438 CreateUserStmt *n = makeNode(CreateUserStmt);
449 /*****************************************************************************
451 * Alter a postresql DBMS user
454 *****************************************************************************/
456 AlterUserStmt: ALTER USER UserId user_passwd_clause user_createdb_clause
457 user_createuser_clause user_group_clause user_valid_clause
459 AlterUserStmt *n = makeNode(AlterUserStmt);
470 /*****************************************************************************
472 * Drop a postresql DBMS user
475 *****************************************************************************/
477 DropUserStmt: DROP USER UserId
479 DropUserStmt *n = makeNode(DropUserStmt);
485 user_passwd_clause: WITH PASSWORD UserId { $$ = $3; }
486 | /*EMPTY*/ { $$ = NULL; }
489 user_createdb_clause: CREATEDB
492 $$ = (b = (bool*)palloc(sizeof(bool)));
498 $$ = (b = (bool*)palloc(sizeof(bool)));
501 | /*EMPTY*/ { $$ = NULL; }
504 user_createuser_clause: CREATEUSER
507 $$ = (b = (bool*)palloc(sizeof(bool)));
513 $$ = (b = (bool*)palloc(sizeof(bool)));
516 | /*EMPTY*/ { $$ = NULL; }
519 user_group_list: user_group_list ',' UserId
521 $$ = lcons((void*)makeString($3), $1);
525 $$ = lcons((void*)makeString($1), NIL);
529 user_group_clause: IN GROUP user_group_list { $$ = $3; }
530 | /*EMPTY*/ { $$ = NULL; }
533 user_valid_clause: VALID UNTIL SCONST { $$ = $3; }
534 | /*EMPTY*/ { $$ = NULL; }
537 /*****************************************************************************
539 * Set PG internal variable
540 * SET name TO 'var_value'
541 * Include SQL92 syntax (thomas 1997-10-22):
542 * SET TIME ZONE 'var_value'
544 *****************************************************************************/
546 VariableSetStmt: SET ColId TO var_value
548 VariableSetStmt *n = makeNode(VariableSetStmt);
553 | SET ColId '=' var_value
555 VariableSetStmt *n = makeNode(VariableSetStmt);
560 | SET TIME ZONE zone_value
562 VariableSetStmt *n = makeNode(VariableSetStmt);
563 n->name = "timezone";
567 | SET TRANSACTION ISOLATION LEVEL opt_level
569 VariableSetStmt *n = makeNode(VariableSetStmt);
570 n->name = "XactIsoLevel";
577 VariableSetStmt *n = makeNode(VariableSetStmt);
578 n->name = "client_encoding";
582 elog(ERROR, "SET NAMES is not supported");
587 opt_level: READ COMMITTED { $$ = "committed"; }
588 | SERIALIZABLE { $$ = "serializable"; }
591 var_value: Sconst { $$ = $1; }
592 | DEFAULT { $$ = NULL; }
595 zone_value: Sconst { $$ = $1; }
596 | DEFAULT { $$ = NULL; }
597 | LOCAL { $$ = NULL; }
600 VariableShowStmt: SHOW ColId
602 VariableShowStmt *n = makeNode(VariableShowStmt);
608 VariableShowStmt *n = makeNode(VariableShowStmt);
609 n->name = "timezone";
612 | SHOW TRANSACTION ISOLATION LEVEL
614 VariableShowStmt *n = makeNode(VariableShowStmt);
615 n->name = "XactIsoLevel";
620 VariableResetStmt: RESET ColId
622 VariableResetStmt *n = makeNode(VariableResetStmt);
628 VariableResetStmt *n = makeNode(VariableResetStmt);
629 n->name = "timezone";
632 | RESET TRANSACTION ISOLATION LEVEL
634 VariableResetStmt *n = makeNode(VariableResetStmt);
635 n->name = "XactIsoLevel";
641 ConstraintsSetStmt: SET CONSTRAINTS constraints_set_list constraints_set_mode
643 ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
651 constraints_set_list: ALL
655 | constraints_set_namelist
662 constraints_set_namelist: IDENT
664 $$ = lappend(NIL, $1);
666 | constraints_set_namelist ',' IDENT
668 $$ = lappend($1, $3);
673 constraints_set_mode: DEFERRED
684 /*****************************************************************************
687 * addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
689 *****************************************************************************/
691 AddAttrStmt: ALTER TABLE relation_name opt_inh_star alter_clause
693 AddAttrStmt *n = makeNode(AddAttrStmt);
701 alter_clause: ADD opt_column columnDef
705 | ADD '(' OptTableElementList ')'
707 Node *lp = lfirst($3);
710 elog(ERROR,"ALTER TABLE/ADD() allows one column only");
713 | DROP opt_column ColId
714 { elog(ERROR,"ALTER TABLE/DROP COLUMN not yet implemented"); }
715 | ALTER opt_column ColId SET DEFAULT a_expr
716 { elog(ERROR,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented"); }
717 | ALTER opt_column ColId DROP DEFAULT
718 { elog(ERROR,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented"); }
720 { elog(ERROR,"ALTER TABLE/ADD CONSTRAINT not yet implemented"); }
724 /*****************************************************************************
729 *****************************************************************************/
731 ClosePortalStmt: CLOSE opt_id
733 ClosePortalStmt *n = makeNode(ClosePortalStmt);
739 opt_id: ColId { $$ = $1; }
740 | /*EMPTY*/ { $$ = NULL; }
744 /*****************************************************************************
747 * COPY [BINARY] <relname> FROM/TO
748 * [USING DELIMITERS <delimiter>]
750 *****************************************************************************/
752 CopyStmt: COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
754 CopyStmt *n = makeNode(CopyStmt);
772 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
773 * used depends on the direction. (It really doesn't make sense to copy from
774 * stdout. We silently correct the "typo". - AY 9/94
776 copy_file_name: Sconst { $$ = $1; }
777 | STDIN { $$ = NULL; }
778 | STDOUT { $$ = NULL; }
781 opt_binary: BINARY { $$ = TRUE; }
782 | /*EMPTY*/ { $$ = FALSE; }
785 opt_with_copy: WITH OIDS { $$ = TRUE; }
786 | /*EMPTY*/ { $$ = FALSE; }
790 * the default copy delimiter is tab but the user can configure it
792 copy_delimiter: USING DELIMITERS Sconst { $$ = $3; }
793 | /*EMPTY*/ { $$ = "\t"; }
797 /*****************************************************************************
802 *****************************************************************************/
804 CreateStmt: CREATE OptTemp TABLE relation_name '(' OptTableElementList ')'
807 CreateStmt *n = makeNode(CreateStmt);
812 n->constraints = NIL;
817 OptTemp: OptTempType { $$ = $1; }
818 | OptTempScope OptTempType { $$ = $2; }
821 OptTempType: TEMP { $$ = TRUE; }
822 | TEMPORARY { $$ = TRUE; }
823 | /*EMPTY*/ { $$ = FALSE; }
828 elog(ERROR, "GLOBAL TEMPORARY TABLE is not currently supported");
837 OptTableElementList: OptTableElementList ',' OptTableElement
840 $$ = lappend($1, $3);
851 | /*EMPTY*/ { $$ = NULL; }
854 OptTableElement: columnDef { $$ = $1; }
855 | TableConstraint { $$ = $1; }
858 columnDef: ColId Typename ColQualifier
860 ColumnDef *n = makeNode(ColumnDef);
863 n->raw_default = NULL;
864 n->cooked_default = NULL;
865 n->is_not_null = FALSE;
869 | ColId SERIAL ColPrimaryKey
871 ColumnDef *n = makeNode(ColumnDef);
873 n->typename = makeNode(TypeName);
874 n->typename->name = xlateSqlType("integer");
875 n->raw_default = NULL;
876 n->cooked_default = NULL;
877 n->is_not_null = TRUE;
878 n->is_sequence = TRUE;
885 ColQualifier: ColQualList { $$ = $1; }
886 | /*EMPTY*/ { $$ = NULL; }
889 ColQualList: ColQualList ColConstraint
892 $$ = lappend($1, $2);
905 ColPrimaryKey: PRIMARY KEY
907 Constraint *n = makeNode(Constraint);
908 n->contype = CONSTR_PRIMARY;
911 n->cooked_expr = NULL;
913 $$ = lcons((Node *)n, NIL);
915 | /*EMPTY*/ { $$ = NULL; }
919 CONSTRAINT name ColConstraintElem
921 Constraint *n = (Constraint *)$3;
922 if (n != NULL) n->name = fmtId($2);
929 /* DEFAULT NULL is already the default for Postgres.
930 * But define it here and carry it forward into the system
931 * to make it explicit.
932 * - thomas 1998-09-13
933 * WITH NULL and NULL are not SQL92-standard syntax elements,
934 * so leave them out. Use DEFAULT NULL to explicitly indicate
935 * that a column may have that value. WITH NULL leads to
936 * shift/reduce conflicts with WITH TIME ZONE anyway.
937 * - thomas 1999-01-08
938 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
939 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
940 * or be part of a_expr NOT LIKE or similar constructs).
942 ColConstraintElem: CHECK '(' a_expr ')'
944 Constraint *n = makeNode(Constraint);
945 n->contype = CONSTR_CHECK;
948 n->cooked_expr = NULL;
954 Constraint *n = makeNode(Constraint);
955 n->contype = CONSTR_DEFAULT;
958 n->cooked_expr = NULL;
964 Constraint *n = makeNode(Constraint);
965 n->contype = CONSTR_DEFAULT;
968 n->cooked_expr = NULL;
974 Constraint *n = makeNode(Constraint);
975 n->contype = CONSTR_NOTNULL;
978 n->cooked_expr = NULL;
984 Constraint *n = makeNode(Constraint);
985 n->contype = CONSTR_UNIQUE;
988 n->cooked_expr = NULL;
994 Constraint *n = makeNode(Constraint);
995 n->contype = CONSTR_PRIMARY;
998 n->cooked_expr = NULL;
1002 | REFERENCES ColId opt_column_list key_match key_actions
1004 elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
1009 /* ConstraintElem specifies constraint syntax which is not embedded into
1010 * a column definition. ColConstraintElem specifies the embedded form.
1011 * - thomas 1997-12-03
1013 TableConstraint: CONSTRAINT name ConstraintElem
1015 Constraint *n = (Constraint *)$3;
1016 if (n != NULL) n->name = fmtId($2);
1023 ConstraintElem: CHECK '(' a_expr ')'
1025 Constraint *n = makeNode(Constraint);
1026 n->contype = CONSTR_CHECK;
1029 n->cooked_expr = NULL;
1032 | UNIQUE '(' columnList ')'
1034 Constraint *n = makeNode(Constraint);
1035 n->contype = CONSTR_UNIQUE;
1038 n->cooked_expr = NULL;
1042 | PRIMARY KEY '(' columnList ')'
1044 Constraint *n = makeNode(Constraint);
1045 n->contype = CONSTR_PRIMARY;
1048 n->cooked_expr = NULL;
1052 | FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
1054 elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
1059 key_match: MATCH FULL { $$ = NULL; }
1060 | MATCH PARTIAL { $$ = NULL; }
1061 | /*EMPTY*/ { $$ = NULL; }
1064 key_actions: key_action key_action { $$ = NIL; }
1065 | key_action { $$ = NIL; }
1066 | /*EMPTY*/ { $$ = NIL; }
1069 key_action: ON DELETE key_reference { $$ = NIL; }
1070 | ON UPDATE key_reference { $$ = NIL; }
1073 key_reference: NO ACTION { $$ = NULL; }
1074 | CASCADE { $$ = NULL; }
1075 | SET DEFAULT { $$ = NULL; }
1076 | SET NULL_P { $$ = NULL; }
1079 OptInherit: INHERITS '(' relation_name_list ')' { $$ = $3; }
1080 | /*EMPTY*/ { $$ = NIL; }
1083 CreateAsStmt: CREATE OptTemp TABLE relation_name OptCreateAs AS SubSelect
1085 SelectStmt *n = (SelectStmt *)$7;
1087 mapTargetColumns($5, n->targetList);
1094 OptCreateAs: '(' CreateAsList ')' { $$ = $2; }
1095 | /*EMPTY*/ { $$ = NULL; }
1098 CreateAsList: CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1099 | CreateAsElement { $$ = lcons($1, NIL); }
1102 CreateAsElement: ColId
1104 ColumnDef *n = makeNode(ColumnDef);
1107 n->raw_default = NULL;
1108 n->cooked_default = NULL;
1109 n->is_not_null = FALSE;
1110 n->constraints = NULL;
1116 /*****************************************************************************
1119 * CREATE SEQUENCE seqname
1121 *****************************************************************************/
1123 CreateSeqStmt: CREATE SEQUENCE relation_name OptSeqList
1125 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1132 OptSeqList: OptSeqList OptSeqElem
1133 { $$ = lappend($1, $2); }
1137 OptSeqElem: CACHE IntegerOnly
1139 $$ = makeNode(DefElem);
1140 $$->defname = "cache";
1141 $$->arg = (Node *)$2;
1145 $$ = makeNode(DefElem);
1146 $$->defname = "cycle";
1147 $$->arg = (Node *)NULL;
1149 | INCREMENT IntegerOnly
1151 $$ = makeNode(DefElem);
1152 $$->defname = "increment";
1153 $$->arg = (Node *)$2;
1155 | MAXVALUE IntegerOnly
1157 $$ = makeNode(DefElem);
1158 $$->defname = "maxvalue";
1159 $$->arg = (Node *)$2;
1161 | MINVALUE IntegerOnly
1163 $$ = makeNode(DefElem);
1164 $$->defname = "minvalue";
1165 $$->arg = (Node *)$2;
1169 $$ = makeNode(DefElem);
1170 $$->defname = "start";
1171 $$->arg = (Node *)$2;
1175 NumericOnly: FloatOnly { $$ = $1; }
1176 | IntegerOnly { $$ = $1; }
1185 $$->val.dval = - $$->val.dval;
1191 $$ = makeInteger($1);
1195 $$ = makeInteger($2);
1196 $$->val.ival = - $$->val.ival;
1200 /*****************************************************************************
1203 * CREATE PROCEDURAL LANGUAGE ...
1204 * DROP PROCEDURAL LANGUAGE ...
1206 *****************************************************************************/
1208 CreatePLangStmt: CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst
1209 HANDLER def_name LANCOMPILER Sconst
1211 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1220 PLangTrusted: TRUSTED { $$ = TRUE; }
1223 DropPLangStmt: DROP PROCEDURAL LANGUAGE Sconst
1225 DropPLangStmt *n = makeNode(DropPLangStmt);
1231 /*****************************************************************************
1234 * CREATE TRIGGER ...
1237 *****************************************************************************/
1239 CreateTrigStmt: CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1240 relation_name TriggerForSpec EXECUTE PROCEDURE
1241 name '(' TriggerFuncArgs ')'
1243 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1250 memcpy (n->actions, $5, 4);
1251 n->lang = NULL; /* unused */
1252 n->text = NULL; /* unused */
1253 n->attr = NULL; /* unused */
1254 n->when = NULL; /* unused */
1256 n->isconstraint = false;
1257 n->deferrable = false;
1258 n->initdeferred = false;
1259 n->constrrelname = NULL;
1262 | CREATE CONSTRAINT TRIGGER name AFTER TriggerOneEvent ON
1263 relation_name OptConstrFromTable
1264 OptConstrTrigDeferrable OptConstrTrigInitdeferred
1265 FOR EACH ROW EXECUTE PROCEDURE name '(' TriggerFuncArgs ')'
1267 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1275 n->actions[1] = '\0';
1276 n->lang = NULL; /* unused */
1277 n->text = NULL; /* unused */
1278 n->attr = NULL; /* unused */
1279 n->when = NULL; /* unused */
1282 * Check that the DEFERRABLE and INITIALLY combination
1285 n->isconstraint = true;
1289 elog(ERROR, "INITIALLY DEFERRED constraint "
1290 "cannot be NOT DEFERRABLE");
1291 n->deferrable = true;
1292 n->initdeferred = true;
1294 n->deferrable = ($10 == 1);
1295 n->initdeferred = false;
1298 n->constrrelname = $9;
1303 TriggerActionTime: BEFORE { $$ = TRUE; }
1304 | AFTER { $$ = FALSE; }
1307 TriggerEvents: TriggerOneEvent
1309 char *e = palloc (4);
1310 e[0] = $1; e[1] = 0; $$ = e;
1312 | TriggerOneEvent OR TriggerOneEvent
1314 char *e = palloc (4);
1315 e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
1317 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
1319 char *e = palloc (4);
1320 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
1325 TriggerOneEvent: INSERT { $$ = 'i'; }
1326 | DELETE { $$ = 'd'; }
1327 | UPDATE { $$ = 'u'; }
1330 TriggerForSpec: FOR TriggerForOpt TriggerForType
1336 TriggerForOpt: EACH { $$ = TRUE; }
1337 | /*EMPTY*/ { $$ = FALSE; }
1340 TriggerForType: ROW { $$ = TRUE; }
1341 | STATEMENT { $$ = FALSE; }
1344 TriggerFuncArgs: TriggerFuncArg
1345 { $$ = lcons($1, NIL); }
1346 | TriggerFuncArgs ',' TriggerFuncArg
1347 { $$ = lappend($1, $3); }
1352 TriggerFuncArg: ICONST
1354 char *s = (char *) palloc (256);
1355 sprintf (s, "%d", $1);
1360 char *s = (char *) palloc (256);
1361 sprintf (s, "%g", $1);
1364 | Sconst { $$ = $1; }
1365 | IDENT { $$ = $1; }
1368 OptConstrFromTable: /* Empty */
1372 | FROM relation_name
1378 OptConstrTrigDeferrable: /* Empty */
1392 OptConstrTrigInitdeferred: /* Empty */
1396 | INITIALLY DEFERRED
1400 | INITIALLY IMMEDIATE
1406 DropTrigStmt: DROP TRIGGER name ON relation_name
1408 DropTrigStmt *n = makeNode(DropTrigStmt);
1416 /*****************************************************************************
1419 * define (type,operator,aggregate)
1421 *****************************************************************************/
1423 DefineStmt: CREATE def_type def_rest
1430 def_rest: def_name definition
1432 $$ = makeNode(DefineStmt);
1434 $$->definition = $2;
1438 def_type: OPERATOR { $$ = OPERATOR; }
1439 | TYPE_P { $$ = TYPE_P; }
1440 | AGGREGATE { $$ = AGGREGATE; }
1443 def_name: PROCEDURE { $$ = "procedure"; }
1444 | JOIN { $$ = "join"; }
1445 | ColId { $$ = $1; }
1446 | MathOp { $$ = $1; }
1450 definition: '(' def_list ')' { $$ = $2; }
1453 def_list: def_elem { $$ = lcons($1, NIL); }
1454 | def_list ',' def_elem { $$ = lappend($1, $3); }
1457 def_elem: def_name '=' def_arg
1459 $$ = makeNode(DefElem);
1461 $$->arg = (Node *)$3;
1465 $$ = makeNode(DefElem);
1467 $$->arg = (Node *)NULL;
1469 | DEFAULT '=' def_arg
1471 $$ = makeNode(DefElem);
1472 $$->defname = "default";
1473 $$->arg = (Node *)$3;
1477 def_arg: ColId { $$ = (Node *)makeString($1); }
1478 | all_Op { $$ = (Node *)makeString($1); }
1479 | NumericOnly { $$ = (Node *)$1; }
1480 | Sconst { $$ = (Node *)makeString($1); }
1483 TypeName *n = makeNode(TypeName);
1486 n->arrayBounds = NULL;
1493 /*****************************************************************************
1496 * destroy <relname1> [, <relname2> .. <relnameN> ]
1498 *****************************************************************************/
1500 DestroyStmt: DROP TABLE relation_name_list
1502 DestroyStmt *n = makeNode(DestroyStmt);
1504 n->sequence = FALSE;
1507 | DROP SEQUENCE relation_name_list
1509 DestroyStmt *n = makeNode(DestroyStmt);
1516 /*****************************************************************************
1519 * truncate table relname
1521 *****************************************************************************/
1523 TruncateStmt: TRUNCATE TABLE relation_name
1525 TruncateStmt *n = makeNode(TruncateStmt);
1531 /*****************************************************************************
1534 * fetch/move [forward | backward] [ # | all ] [ in <portalname> ]
1535 * fetch [ forward | backward | absolute | relative ]
1536 * [ # | all | next | prior ] [ [ in | from ] <portalname> ]
1538 *****************************************************************************/
1540 FetchStmt: FETCH opt_direction fetch_how_many opt_portal_name
1542 FetchStmt *n = makeNode(FetchStmt);
1546 elog(ERROR,"FETCH/RELATIVE at current position is not supported");
1552 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
1560 | MOVE opt_direction fetch_how_many opt_portal_name
1562 FetchStmt *n = makeNode(FetchStmt);
1566 $2 = (($2 == FORWARD)? BACKWARD: FORWARD);
1576 opt_direction: FORWARD { $$ = FORWARD; }
1577 | BACKWARD { $$ = BACKWARD; }
1578 | RELATIVE { $$ = RELATIVE; }
1581 elog(NOTICE,"FETCH/ABSOLUTE not supported, using RELATIVE");
1584 | /*EMPTY*/ { $$ = FORWARD; /* default */ }
1587 fetch_how_many: Iconst { $$ = $1; }
1588 | '-' Iconst { $$ = - $2; }
1589 | ALL { $$ = 0; /* 0 means fetch all tuples*/ }
1591 | PRIOR { $$ = -1; }
1592 | /*EMPTY*/ { $$ = 1; /*default*/ }
1595 opt_portal_name: IN name { $$ = $2; }
1596 | FROM name { $$ = $2; }
1597 | /*EMPTY*/ { $$ = NULL; }
1601 /*****************************************************************************
1604 * GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1606 *****************************************************************************/
1608 GrantStmt: GRANT privileges ON relation_name_list TO grantee opt_with_grant
1610 $$ = (Node*)makeAclStmt($2,$4,$6,'+');
1614 privileges: ALL PRIVILEGES
1616 $$ = aclmakepriv("rwaR",0);
1620 $$ = aclmakepriv("rwaR",0);
1622 | operation_commalist
1628 operation_commalist: operation
1630 $$ = aclmakepriv("",$1);
1632 | operation_commalist ',' operation
1634 $$ = aclmakepriv($1,$3);
1640 $$ = ACL_MODE_RD_CHR;
1644 $$ = ACL_MODE_AP_CHR;
1648 $$ = ACL_MODE_WR_CHR;
1652 $$ = ACL_MODE_WR_CHR;
1656 $$ = ACL_MODE_RU_CHR;
1662 $$ = aclmakeuser("A","");
1666 $$ = aclmakeuser("G",$2);
1670 $$ = aclmakeuser("U",$1);
1674 opt_with_grant: WITH GRANT OPTION
1676 yyerror("WITH GRANT OPTION is not supported. Only relation owners can set privileges");
1682 /*****************************************************************************
1685 * REVOKE [privileges] ON [relation_name] FROM [user]
1687 *****************************************************************************/
1689 RevokeStmt: REVOKE privileges ON relation_name_list FROM grantee
1691 $$ = (Node*)makeAclStmt($2,$4,$6,'-');
1696 /*****************************************************************************
1699 * create index <indexname> on <relname>
1700 * using <access> "(" (<col> with <op>)+ ")" [with
1703 * [where <qual>] is not supported anymore
1704 *****************************************************************************/
1706 IndexStmt: CREATE index_opt_unique INDEX index_name ON relation_name
1707 access_method_clause '(' index_params ')' opt_with
1709 /* should check that access_method is valid,
1710 etc ... but doesn't */
1711 IndexStmt *n = makeNode(IndexStmt);
1715 n->accessMethod = $7;
1716 n->indexParams = $9;
1717 n->withClause = $11;
1718 n->whereClause = NULL;
1723 index_opt_unique: UNIQUE { $$ = TRUE; }
1724 | /*EMPTY*/ { $$ = FALSE; }
1727 access_method_clause: USING access_method { $$ = $2; }
1728 | /*EMPTY*/ { $$ = "btree"; }
1731 index_params: index_list { $$ = $1; }
1732 | func_index { $$ = lcons($1,NIL); }
1735 index_list: index_list ',' index_elem { $$ = lappend($1, $3); }
1736 | index_elem { $$ = lcons($1, NIL); }
1739 func_index: func_name '(' name_list ')' opt_type opt_class
1741 $$ = makeNode(IndexElem);
1749 index_elem: attr_name opt_type opt_class
1751 $$ = makeNode(IndexElem);
1759 opt_type: ':' Typename { $$ = $2; }
1760 | FOR Typename { $$ = $2; }
1761 | /*EMPTY*/ { $$ = NULL; }
1764 /* opt_class "WITH class" conflicts with preceeding opt_type
1765 * for Typename of "TIMESTAMP WITH TIME ZONE"
1766 * So, remove "WITH class" from the syntax. OK??
1767 * - thomas 1997-10-12
1768 * | WITH class { $$ = $2; }
1770 opt_class: class { $$ = $1; }
1771 | USING class { $$ = $2; }
1772 | /*EMPTY*/ { $$ = NULL; }
1776 /*****************************************************************************
1779 * extend index <indexname> [where <qual>]
1781 *****************************************************************************/
1783 ExtendStmt: EXTEND INDEX index_name where_clause
1785 ExtendStmt *n = makeNode(ExtendStmt);
1787 n->whereClause = $4;
1792 /*****************************************************************************
1795 * execute recipe <recipeName>
1797 *****************************************************************************/
1800 RecipeStmt: EXECUTE RECIPE recipe_name
1803 if (!IsTransactionBlock())
1804 elog(ERROR,"EXECUTE RECIPE may only be used in begin/end transaction blocks");
1806 n = makeNode(RecipeStmt);
1813 /*****************************************************************************
1816 * define function <fname>
1817 * (language = <lang>, returntype = <typename>
1818 * [, arch_pct = <percentage | pre-defined>]
1819 * [, disk_pct = <percentage | pre-defined>]
1820 * [, byte_pct = <percentage | pre-defined>]
1821 * [, perbyte_cpu = <int | pre-defined>]
1822 * [, percall_cpu = <int | pre-defined>]
1824 * [arg is (<type-1> { , <type-n>})]
1825 * as <filename or code in language as appropriate>
1827 *****************************************************************************/
1829 ProcedureStmt: CREATE FUNCTION func_name func_args
1830 RETURNS func_return opt_with AS func_as LANGUAGE Sconst
1832 ProcedureStmt *n = makeNode(ProcedureStmt);
1842 opt_with: WITH definition { $$ = $2; }
1843 | /*EMPTY*/ { $$ = NIL; }
1846 func_args: '(' func_args_list ')' { $$ = $2; }
1847 | '(' ')' { $$ = NIL; }
1850 func_args_list: TypeId
1851 { $$ = lcons(makeString($1),NIL); }
1852 | func_args_list ',' TypeId
1853 { $$ = lappend($1,makeString($3)); }
1857 { $$ = lcons(makeString($1),NIL); }
1859 { $$ = lappend(lcons(makeString($1),NIL), makeString($3)); }
1862 func_return: set_opt TypeId
1864 TypeName *n = makeNode(TypeName);
1867 n->arrayBounds = NULL;
1872 set_opt: SETOF { $$ = TRUE; }
1873 | /*EMPTY*/ { $$ = FALSE; }
1876 /*****************************************************************************
1880 * remove function <funcname>
1881 * (REMOVE FUNCTION "funcname" (arg1, arg2, ...))
1882 * remove aggregate <aggname>
1883 * (REMOVE AGGREGATE "aggname" "aggtype")
1884 * remove operator <opname>
1885 * (REMOVE OPERATOR "opname" (leftoperand_typ rightoperand_typ))
1886 * remove type <typename>
1887 * (REMOVE TYPE "typename")
1888 * remove rule <rulename>
1889 * (REMOVE RULE "rulename")
1891 *****************************************************************************/
1893 RemoveStmt: DROP remove_type name
1895 RemoveStmt *n = makeNode(RemoveStmt);
1902 remove_type: TYPE_P { $$ = TYPE_P; }
1903 | INDEX { $$ = INDEX; }
1904 | RULE { $$ = RULE; }
1905 | VIEW { $$ = VIEW; }
1909 RemoveAggrStmt: DROP AGGREGATE name aggr_argtype
1911 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
1918 aggr_argtype: name { $$ = $1; }
1919 | '*' { $$ = NULL; }
1923 RemoveFuncStmt: DROP FUNCTION func_name func_args
1925 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
1933 RemoveOperStmt: DROP OPERATOR all_Op '(' oper_argtypes ')'
1935 RemoveOperStmt *n = makeNode(RemoveOperStmt);
1942 all_Op: Op | MathOp;
1944 MathOp: '+' { $$ = "+"; }
1958 elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
1961 { $$ = makeList(makeString($1), makeString($3), -1); }
1962 | NONE ',' name /* left unary */
1963 { $$ = makeList(NULL, makeString($3), -1); }
1964 | name ',' NONE /* right unary */
1965 { $$ = makeList(makeString($1), NULL, -1); }
1969 /*****************************************************************************
1972 * rename <attrname1> in <relname> [*] to <attrname2>
1973 * rename <relname1> to <relname2>
1975 *****************************************************************************/
1977 RenameStmt: ALTER TABLE relation_name opt_inh_star
1978 RENAME opt_column opt_name TO name
1980 RenameStmt *n = makeNode(RenameStmt);
1989 opt_name: name { $$ = $1; }
1990 | /*EMPTY*/ { $$ = NULL; }
1993 opt_column: COLUMN { $$ = COLUMN; }
1994 | /*EMPTY*/ { $$ = 0; }
1998 /*****************************************************************************
2000 * QUERY: Define Rewrite Rule , Define Tuple Rule
2001 * Define Rule <old rules >
2003 * only rewrite rule is supported -- ay 9/94
2005 *****************************************************************************/
2007 RuleStmt: CREATE RULE name AS
2008 { QueryIsRule=TRUE; }
2009 ON event TO event_object where_clause
2010 DO opt_instead RuleActionList
2012 RuleStmt *n = makeNode(RuleStmt);
2016 n->whereClause = $10;
2023 RuleActionList: NOTHING { $$ = NIL; }
2024 | SelectStmt { $$ = lcons($1, NIL); }
2025 | RuleActionStmt { $$ = lcons($1, NIL); }
2026 | '[' RuleActionBlock ']' { $$ = $2; }
2027 | '(' RuleActionBlock ')' { $$ = $2; }
2030 RuleActionBlock: RuleActionMulti { $$ = $1; }
2031 | RuleActionStmt { $$ = lcons($1, NIL); }
2034 RuleActionMulti: RuleActionMulti RuleActionStmt
2035 { $$ = lappend($1, $2); }
2036 | RuleActionMulti RuleActionStmt ';'
2037 { $$ = lappend($1, $2); }
2038 | RuleActionStmt ';'
2039 { $$ = lcons($1, NIL); }
2042 RuleActionStmt: InsertStmt
2048 event_object: relation_name '.' attr_name
2050 $$ = makeNode(Attr);
2053 $$->attrs = lcons(makeString($3), NIL);
2054 $$->indirection = NIL;
2058 $$ = makeNode(Attr);
2062 $$->indirection = NIL;
2066 /* change me to select, update, etc. some day */
2067 event: SELECT { $$ = CMD_SELECT; }
2068 | UPDATE { $$ = CMD_UPDATE; }
2069 | DELETE { $$ = CMD_DELETE; }
2070 | INSERT { $$ = CMD_INSERT; }
2073 opt_instead: INSTEAD { $$ = TRUE; }
2074 | /*EMPTY*/ { $$ = FALSE; }
2078 /*****************************************************************************
2081 * NOTIFY <relation_name> can appear both in rule bodies and
2082 * as a query-level command
2084 *****************************************************************************/
2086 NotifyStmt: NOTIFY relation_name
2088 NotifyStmt *n = makeNode(NotifyStmt);
2094 ListenStmt: LISTEN relation_name
2096 ListenStmt *n = makeNode(ListenStmt);
2102 UnlistenStmt: UNLISTEN relation_name
2104 UnlistenStmt *n = makeNode(UnlistenStmt);
2110 UnlistenStmt *n = makeNode(UnlistenStmt);
2117 /*****************************************************************************
2128 *****************************************************************************/
2130 TransactionStmt: ABORT_TRANS opt_trans
2132 TransactionStmt *n = makeNode(TransactionStmt);
2133 n->command = ABORT_TRANS;
2136 | BEGIN_TRANS opt_trans
2138 TransactionStmt *n = makeNode(TransactionStmt);
2139 n->command = BEGIN_TRANS;
2144 TransactionStmt *n = makeNode(TransactionStmt);
2145 n->command = END_TRANS;
2148 | END_TRANS opt_trans
2150 TransactionStmt *n = makeNode(TransactionStmt);
2151 n->command = END_TRANS;
2154 | ROLLBACK opt_trans
2156 TransactionStmt *n = makeNode(TransactionStmt);
2157 n->command = ABORT_TRANS;
2162 opt_trans: WORK { $$ = TRUE; }
2163 | TRANSACTION { $$ = TRUE; }
2164 | /*EMPTY*/ { $$ = TRUE; }
2168 /*****************************************************************************
2171 * define view <viewname> '('target-list ')' [where <quals> ]
2173 *****************************************************************************/
2175 ViewStmt: CREATE VIEW name AS SelectStmt
2177 ViewStmt *n = makeNode(ViewStmt);
2179 n->query = (Query *)$5;
2180 if (((SelectStmt *)n->query)->sortClause != NULL)
2181 elog(ERROR,"Order by and Distinct on views is not implemented.");
2182 if (((SelectStmt *)n->query)->unionClause != NULL)
2183 elog(ERROR,"Views on unions not implemented.");
2184 if (((SelectStmt *)n->query)->forUpdate != NULL)
2185 elog(ERROR, "SELECT FOR UPDATE is not allowed in CREATE VIEW");
2191 /*****************************************************************************
2196 *****************************************************************************/
2198 LoadStmt: LOAD file_name
2200 LoadStmt *n = makeNode(LoadStmt);
2207 /*****************************************************************************
2212 *****************************************************************************/
2214 CreatedbStmt: CREATE DATABASE database_name WITH opt_database1 opt_database2
2216 CreatedbStmt *n = makeNode(CreatedbStmt);
2217 if ($5 == NULL && $6 == NULL) {
2218 elog(ERROR, "CREATE DATABASE WITH requires at least an option");
2224 n->encoding = pg_char_to_encoding($6);
2225 if (n->encoding < 0) {
2226 elog(ERROR, "invalid encoding name %s", $6);
2229 n->encoding = GetTemplateEncoding();
2233 elog(ERROR, "WITH ENCODING is not supported");
2238 | CREATE DATABASE database_name
2240 CreatedbStmt *n = makeNode(CreatedbStmt);
2244 n->encoding = GetTemplateEncoding();
2252 opt_database1: LOCATION '=' location { $$ = $3; }
2253 | /*EMPTY*/ { $$ = NULL; }
2256 opt_database2: ENCODING '=' encoding { $$ = $3; }
2257 | /*EMPTY*/ { $$ = NULL; }
2260 location: Sconst { $$ = $1; }
2261 | DEFAULT { $$ = NULL; }
2262 | /*EMPTY*/ { $$ = NULL; }
2265 encoding: Sconst { $$ = $1; }
2266 | DEFAULT { $$ = NULL; }
2267 | /*EMPTY*/ { $$ = NULL; }
2270 /*****************************************************************************
2275 *****************************************************************************/
2277 DestroydbStmt: DROP DATABASE database_name
2279 DestroydbStmt *n = makeNode(DestroydbStmt);
2286 /*****************************************************************************
2289 * cluster <index_name> on <relation_name>
2291 *****************************************************************************/
2293 ClusterStmt: CLUSTER index_name ON relation_name
2295 ClusterStmt *n = makeNode(ClusterStmt);
2302 /*****************************************************************************
2307 *****************************************************************************/
2309 VacuumStmt: VACUUM opt_verbose opt_analyze
2311 VacuumStmt *n = makeNode(VacuumStmt);
2318 | VACUUM opt_verbose opt_analyze relation_name opt_va_list
2320 VacuumStmt *n = makeNode(VacuumStmt);
2325 if ( $5 != NIL && !$4 )
2326 elog(ERROR,"parser: syntax error at or near \"(\"");
2331 opt_verbose: VERBOSE { $$ = TRUE; }
2332 | /*EMPTY*/ { $$ = FALSE; }
2335 opt_analyze: ANALYZE { $$ = TRUE; }
2336 | /*EMPTY*/ { $$ = FALSE; }
2339 opt_va_list: '(' va_list ')' { $$ = $2; }
2340 | /*EMPTY*/ { $$ = NIL; }
2344 { $$=lcons($1,NIL); }
2346 { $$=lappend($1,$3); }
2350 /*****************************************************************************
2355 *****************************************************************************/
2357 ExplainStmt: EXPLAIN opt_verbose OptimizableStmt
2359 ExplainStmt *n = makeNode(ExplainStmt);
2361 n->query = (Query*)$3;
2367 /*****************************************************************************
2369 * Optimizable Stmts: *
2371 * one of the five queries processed by the planner *
2373 * [ultimately] produces query-trees as specified *
2374 * in the query-spec document in ~postgres/ref *
2376 *****************************************************************************/
2378 OptimizableStmt: SelectStmt
2383 | DeleteStmt /* by default all are $$=$1 */
2387 /*****************************************************************************
2392 *****************************************************************************/
2394 /* This rule used 'opt_column_list' between 'relation_name' and 'insert_rest'
2395 * originally. When the second rule of 'insert_rest' was changed to use the
2396 * new 'SelectStmt' rule (for INTERSECT and EXCEPT) it produced a shift/reduce
2397 * conflict. So I just changed the rules 'InsertStmt' and 'insert_rest' to
2398 * accept the same statements without any shift/reduce conflicts
2400 InsertStmt: INSERT INTO relation_name insert_rest
2407 insert_rest: VALUES '(' target_list ')'
2409 $$ = makeNode(InsertStmt);
2412 $$->targetList = $3;
2413 $$->fromClause = NIL;
2414 $$->whereClause = NULL;
2415 $$->groupClause = NIL;
2416 $$->havingClause = NULL;
2417 $$->unionClause = NIL;
2421 $$ = makeNode(InsertStmt);
2423 $$->targetList = NIL;
2424 $$->fromClause = NIL;
2425 $$->whereClause = NULL;
2426 $$->groupClause = NIL;
2427 $$->havingClause = NULL;
2428 $$->unionClause = NIL;
2429 $$->intersectClause = NIL;
2431 /* We want the full power of SelectStatements including INTERSECT and EXCEPT
2432 * for insertion. However, we can't support sort or limit clauses.
2436 SelectStmt *n = (SelectStmt *) $1;
2438 elog(ERROR, "INSERT ... SELECT can't have ORDER BY");
2439 $$ = makeNode(InsertStmt);
2441 $$->unique = n->unique;
2442 $$->targetList = n->targetList;
2443 $$->fromClause = n->fromClause;
2444 $$->whereClause = n->whereClause;
2445 $$->groupClause = n->groupClause;
2446 $$->havingClause = n->havingClause;
2447 $$->unionClause = n->unionClause;
2448 $$->intersectClause = n->intersectClause;
2449 $$->unionall = n->unionall;
2450 $$->forUpdate = n->forUpdate;
2452 | '(' columnList ')' VALUES '(' target_list ')'
2454 $$ = makeNode(InsertStmt);
2457 $$->targetList = $6;
2458 $$->fromClause = NIL;
2459 $$->whereClause = NULL;
2460 $$->groupClause = NIL;
2461 $$->havingClause = NULL;
2462 $$->unionClause = NIL;
2463 $$->intersectClause = NIL;
2465 | '(' columnList ')' SelectStmt
2467 SelectStmt *n = (SelectStmt *) $4;
2469 elog(ERROR, "INSERT ... SELECT can't have ORDER BY");
2470 $$ = makeNode(InsertStmt);
2472 $$->unique = n->unique;
2473 $$->targetList = n->targetList;
2474 $$->fromClause = n->fromClause;
2475 $$->whereClause = n->whereClause;
2476 $$->groupClause = n->groupClause;
2477 $$->havingClause = n->havingClause;
2478 $$->unionClause = n->unionClause;
2479 $$->intersectClause = n->intersectClause;
2480 $$->unionall = n->unionall;
2481 $$->forUpdate = n->forUpdate;
2485 opt_column_list: '(' columnList ')' { $$ = $2; }
2486 | /*EMPTY*/ { $$ = NIL; }
2490 columnList ',' columnElem
2491 { $$ = lappend($1, $3); }
2493 { $$ = lcons($1, NIL); }
2496 columnElem: ColId opt_indirection
2498 Ident *id = makeNode(Ident);
2500 id->indirection = $2;
2506 /*****************************************************************************
2511 *****************************************************************************/
2513 DeleteStmt: DELETE FROM relation_name
2516 DeleteStmt *n = makeNode(DeleteStmt);
2518 n->whereClause = $4;
2523 LockStmt: LOCK_P opt_table relation_name opt_lock
2525 LockStmt *n = makeNode(LockStmt);
2533 opt_lock: IN lock_type MODE { $$ = $2; }
2534 | /*EMPTY*/ { $$ = AccessExclusiveLock; }
2537 lock_type: SHARE ROW EXCLUSIVE { $$ = ShareRowExclusiveLock; }
2538 | ROW opt_lmode { $$ = ($2? RowShareLock: RowExclusiveLock); }
2539 | ACCESS opt_lmode { $$ = ($2? AccessShareLock: AccessExclusiveLock); }
2540 | opt_lmode { $$ = ($1? ShareLock: ExclusiveLock); }
2543 opt_lmode: SHARE { $$ = TRUE; }
2544 | EXCLUSIVE { $$ = FALSE; }
2548 /*****************************************************************************
2551 * UpdateStmt (UPDATE)
2553 *****************************************************************************/
2555 UpdateStmt: UPDATE relation_name
2556 SET update_target_list
2560 UpdateStmt *n = makeNode(UpdateStmt);
2564 n->whereClause = $6;
2570 /*****************************************************************************
2575 *****************************************************************************/
2576 CursorStmt: DECLARE name opt_cursor CURSOR FOR SelectStmt
2580 n= (SelectStmt *)$6;
2581 /* from PORTAL name */
2583 * 15 august 1991 -- since 3.0 postgres does locking
2584 * right, we discovered that portals were violating
2585 * locking protocol. portal locks cannot span xacts.
2586 * as a short-term fix, we installed the check here.
2589 if (!IsTransactionBlock())
2590 elog(ERROR,"Named portals may only be used in begin/end transaction blocks");
2594 if (n->forUpdate != NULL)
2595 elog(ERROR,"DECLARE/UPDATE not supported;"
2596 " Cursors must be READ ONLY.");
2601 opt_cursor: BINARY { $$ = TRUE; }
2602 | INSENSITIVE { $$ = FALSE; }
2603 | SCROLL { $$ = FALSE; }
2604 | INSENSITIVE SCROLL { $$ = FALSE; }
2605 | /*EMPTY*/ { $$ = FALSE; }
2608 /*****************************************************************************
2613 *****************************************************************************/
2615 /* A complete SELECT statement looks like this. Note sort, for_update,
2616 * and limit clauses can only appear once, not in each subselect.
2618 * The rule returns a SelectStmt Node having the set operations attached to
2619 * unionClause and intersectClause (NIL if no set operations were present)
2622 SelectStmt: select_clause sort_clause for_update_clause opt_select_limit
2624 if IsA($1, SelectStmt)
2626 /* There were no set operations, so just attach the
2629 SelectStmt *n = (SelectStmt *) $1;
2632 n->limitOffset = nth(0, $4);
2633 n->limitCount = nth(1, $4);
2638 /* There were set operations. The root of the operator
2639 * tree is delivered by $1, but we must hand back a
2640 * SelectStmt node not an A_Expr Node.
2641 * So we find the leftmost 'SelectStmt' in the operator
2642 * tree $1 (which is the first Select Statement in the
2643 * query), which will be the returned node.
2644 * Then we attach the whole operator tree to that node's
2645 * 'intersectClause', and a list of all 'SelectStmt' Nodes
2646 * in the tree to its 'unionClause'. (NOTE that this means
2647 * the top node has indirect recursive pointers to itself!
2648 * This would cause trouble if we tried copyObject!!)
2649 * The intersectClause and unionClause subtrees will be
2650 * left untouched by the main parser, and will only be
2651 * processed when control gets to the function
2652 * Except_Intersect_Rewrite() (in rewriteHandler.c).
2654 Node *op = (Node *) $1;
2655 List *select_list = NIL;
2656 SelectStmt *first_select;
2657 bool intersect_present = false,
2658 unionall_present = false;
2660 /* Take the operator tree as an argument and create a
2661 * list of all SelectStmt Nodes found in the tree.
2663 * If one of the SelectStmt Nodes has the 'unionall' flag
2664 * set to true the 'unionall_present' flag is also set to
2667 create_select_list(op, &select_list, &unionall_present);
2669 /* Replace all the A_Expr Nodes in the operator tree by
2672 * If an INTERSECT or an EXCEPT is present, the
2673 * 'intersect_present' flag is set to true
2675 op = A_Expr_to_Expr(op, &intersect_present);
2677 /* If both flags are set to true we have a UNION ALL
2678 * statement mixed up with INTERSECT or EXCEPT
2679 * which can not be handled at the moment.
2681 if (intersect_present && unionall_present)
2682 elog(ERROR, "UNION ALL not allowed in mixed set operations");
2684 /* Get the leftmost SeletStmt Node (which automatically
2685 * represents the first Select Statement of the query!)
2687 first_select = (SelectStmt *) lfirst(select_list);
2689 /* Attach the list of all SeletStmt Nodes to unionClause */
2690 first_select->unionClause = select_list;
2692 /* Attach the whole operator tree to intersectClause */
2693 first_select->intersectClause = (List *) op;
2695 /* finally attach the sort clause &etc */
2696 first_select->sortClause = $2;
2697 first_select->forUpdate = $3;
2698 first_select->limitOffset = nth(0, $4);
2699 first_select->limitCount = nth(1, $4);
2700 $$ = (Node *) first_select;
2702 if (((SelectStmt *)$$)->forUpdate != NULL && QueryIsRule)
2703 elog(ERROR, "SELECT FOR UPDATE is not allowed in RULES");
2707 /* This rule parses Select statements that can appear within set operations,
2708 * including UNION, INTERSECT and EXCEPT. '(' and ')' can be used to specify
2709 * the ordering of the set operations. Without '(' and ')' we want the
2710 * operations to be left associative.
2712 * Note that sort clauses cannot be included at this level --- a sort clause
2713 * can only appear at the end of the complete Select, and it will be handled
2714 * by the topmost SelectStmt rule. Likewise FOR UPDATE and LIMIT.
2716 * The rule builds up an operator tree using A_Expr Nodes. AND Nodes represent
2717 * INTERSECTs, OR Nodes represent UNIONs, and AND NOT nodes represent EXCEPTs.
2718 * The SelectStatements to be connected are the left and right arguments to
2720 * If no set operations appear in the query, the tree consists only of one
2723 select_clause: '(' select_clause ')'
2731 | select_clause EXCEPT select_clause
2733 $$ = (Node *)makeA_Expr(AND,NULL,$1,
2734 makeA_Expr(NOT,NULL,NULL,$3));
2736 | select_clause UNION opt_union select_clause
2738 if (IsA($4, SelectStmt))
2740 SelectStmt *n = (SelectStmt *)$4;
2742 /* NOTE: if UNION ALL appears with a parenthesized set
2743 * operation to its right, the ALL is silently discarded.
2744 * Should we generate an error instead? I think it may
2745 * be OK since ALL with UNION to its right is ignored
2749 $$ = (Node *)makeA_Expr(OR,NULL,$1,$4);
2751 | select_clause INTERSECT select_clause
2753 $$ = (Node *)makeA_Expr(AND,NULL,$1,$3);
2757 SubSelect: SELECT opt_unique target_list
2758 result from_clause where_clause
2759 group_clause having_clause
2761 SelectStmt *n = makeNode(SelectStmt);
2763 n->unionall = FALSE;
2765 /* This is new: Subselects support the INTO clause
2766 * which allows queries that are not part of the
2767 * SQL92 standard and should not be formulated!
2768 * We need it for INTERSECT and EXCEPT and I did not
2769 * want to create a new rule 'SubSelect1' including the
2770 * feature. If it makes troubles we will have to add
2771 * a new rule and change this to prevent INTOs in
2774 n->istemp = (bool) ((Value *) lfirst($4))->val.ival;
2775 n->into = (char *) lnext($4);
2778 n->whereClause = $6;
2779 n->groupClause = $7;
2780 n->havingClause = $8;
2785 /* easy way to return two values. Can someone improve this? bjm */
2786 result: INTO OptTemp opt_table relation_name { $$ = lcons(makeInteger($2), (List *)$4); }
2787 | /*EMPTY*/ { $$ = lcons(makeInteger(false), NIL); }
2790 opt_table: TABLE { $$ = TRUE; }
2791 | /*EMPTY*/ { $$ = FALSE; }
2794 opt_union: ALL { $$ = TRUE; }
2795 | /*EMPTY*/ { $$ = FALSE; }
2798 opt_unique: DISTINCT { $$ = "*"; }
2799 | DISTINCT ON ColId { $$ = $3; }
2800 | ALL { $$ = NULL; }
2801 | /*EMPTY*/ { $$ = NULL; }
2804 sort_clause: ORDER BY sortby_list { $$ = $3; }
2805 | /*EMPTY*/ { $$ = NIL; }
2808 sortby_list: sortby { $$ = lcons($1, NIL); }
2809 | sortby_list ',' sortby { $$ = lappend($1, $3); }
2812 sortby: a_expr OptUseOp
2814 $$ = makeNode(SortGroupBy);
2820 OptUseOp: USING Op { $$ = $2; }
2821 | USING '<' { $$ = "<"; }
2822 | USING '>' { $$ = ">"; }
2824 | DESC { $$ = ">"; }
2825 | /*EMPTY*/ { $$ = "<"; /*default*/ }
2829 opt_select_limit: LIMIT select_limit_value ',' select_offset_value
2830 { $$ = lappend(lappend(NIL, $4), $2); }
2831 | LIMIT select_limit_value OFFSET select_offset_value
2832 { $$ = lappend(lappend(NIL, $4), $2); }
2833 | LIMIT select_limit_value
2834 { $$ = lappend(lappend(NIL, NULL), $2); }
2835 | OFFSET select_offset_value LIMIT select_limit_value
2836 { $$ = lappend(lappend(NIL, $2), $4); }
2837 | OFFSET select_offset_value
2838 { $$ = lappend(lappend(NIL, $2), NULL); }
2840 { $$ = lappend(lappend(NIL, NULL), NULL); }
2843 select_limit_value: Iconst
2845 Const *n = makeNode(Const);
2848 elog(ERROR, "selection limit must be ALL or a positive integer value > 0");
2850 n->consttype = INT4OID;
2851 n->constlen = sizeof(int4);
2852 n->constvalue = (Datum)$1;
2853 n->constisnull = FALSE;
2854 n->constbyval = TRUE;
2855 n->constisset = FALSE;
2856 n->constiscast = FALSE;
2861 Const *n = makeNode(Const);
2863 n->consttype = INT4OID;
2864 n->constlen = sizeof(int4);
2865 n->constvalue = (Datum)0;
2866 n->constisnull = FALSE;
2867 n->constbyval = TRUE;
2868 n->constisset = FALSE;
2869 n->constiscast = FALSE;
2874 Param *n = makeNode(Param);
2876 n->paramkind = PARAM_NUM;
2878 n->paramtype = INT4OID;
2883 select_offset_value: Iconst
2885 Const *n = makeNode(Const);
2887 n->consttype = INT4OID;
2888 n->constlen = sizeof(int4);
2889 n->constvalue = (Datum)$1;
2890 n->constisnull = FALSE;
2891 n->constbyval = TRUE;
2892 n->constisset = FALSE;
2893 n->constiscast = FALSE;
2898 Param *n = makeNode(Param);
2900 n->paramkind = PARAM_NUM;
2902 n->paramtype = INT4OID;
2907 * jimmy bell-style recursive queries aren't supported in the
2910 * ...however, recursive addattr and rename supported. make special
2913 opt_inh_star: '*' { $$ = TRUE; }
2914 | /*EMPTY*/ { $$ = FALSE; }
2917 relation_name_list: name_list;
2920 { $$ = lcons(makeString($1),NIL); }
2921 | name_list ',' name
2922 { $$ = lappend($1,makeString($3)); }
2925 group_clause: GROUP BY expr_list { $$ = $3; }
2926 | /*EMPTY*/ { $$ = NIL; }
2929 having_clause: HAVING a_expr
2933 | /*EMPTY*/ { $$ = NULL; }
2936 for_update_clause: FOR UPDATE update_list { $$ = $3; }
2937 | FOR READ ONLY { $$ = NULL; }
2938 | /* EMPTY */ { $$ = NULL; }
2941 update_list: OF va_list { $$ = $2; }
2942 | /* EMPTY */ { $$ = lcons(NULL, NULL); }
2945 /*****************************************************************************
2947 * clauses common to all Optimizable Stmts:
2951 *****************************************************************************/
2953 from_clause: FROM from_expr { $$ = $2; }
2954 | /*EMPTY*/ { $$ = NIL; }
2957 from_expr: '(' join_clause_with_union ')'
2965 table_list: table_list ',' table_expr
2966 { $$ = lappend($1, $3); }
2968 { $$ = lcons($1, NIL); }
2971 table_expr: relation_expr AS ColLabel
2973 $$ = makeNode(RangeVar);
2977 | relation_expr ColId
2979 $$ = makeNode(RangeVar);
2985 $$ = makeNode(RangeVar);
2991 /* A UNION JOIN is the same as a FULL OUTER JOIN which *omits*
2992 * all result rows which would have matched on an INNER JOIN.
2993 * Let's reject this for now. - thomas 1999-01-08
2995 join_clause_with_union: join_clause
2997 | table_expr UNION JOIN table_expr
2998 { elog(ERROR,"UNION JOIN not yet implemented"); }
3001 join_clause: table_expr join_list
3003 Node *n = lfirst($2);
3005 /* JoinExpr came back? then it is a join of some sort...
3007 if (IsA(n, JoinExpr))
3009 JoinExpr *j = (JoinExpr *)n;
3013 /* otherwise, it was a cross join,
3014 * which we just represent as an inner join...
3021 join_list: join_list join_expr
3023 $$ = lappend($1, $2);
3027 $$ = lcons($1, NIL);
3031 /* This is everything but the left side of a join.
3032 * Note that a CROSS JOIN is the same as an unqualified
3033 * inner join, so just pass back the right-side table.
3034 * A NATURAL JOIN implicitly matches column names between
3035 * tables, so we'll collect those during the later transformation.
3037 join_expr: join_type JOIN table_expr join_qual
3039 JoinExpr *n = makeNode(JoinExpr);
3041 n->rarg = (Node *)$3;
3045 | NATURAL join_type JOIN table_expr
3047 JoinExpr *n = makeNode(JoinExpr);
3049 n->rarg = (Node *)$4;
3050 n->quals = NULL; /* figure out which columns later... */
3053 | CROSS JOIN table_expr
3054 { $$ = (Node *)$3; }
3057 /* OUTER is just noise... */
3058 join_type: FULL join_outer
3061 elog(NOTICE,"FULL OUTER JOIN not yet implemented");
3066 elog(NOTICE,"LEFT OUTER JOIN not yet implemented");
3071 elog(NOTICE,"RIGHT OUTER JOIN not yet implemented");
3076 elog(NOTICE,"OUTER JOIN not yet implemented");
3088 join_outer: OUTER_P { $$ = NULL; }
3089 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
3092 /* JOIN qualification clauses
3093 * Possibilities are:
3094 * USING ( column list ) allows only unqualified column names,
3095 * which must match between tables.
3096 * ON expr allows more general qualifications.
3097 * - thomas 1999-01-07
3100 join_qual: USING '(' using_list ')' { $$ = $3; }
3101 | ON a_expr { $$ = lcons($2, NIL); }
3104 using_list: using_list ',' using_expr { $$ = lappend($1, $3); }
3105 | using_expr { $$ = lcons($1, NIL); }
3110 /* could be a column name or a relation_name */
3111 Ident *n = makeNode(Ident);
3113 n->indirection = NULL;
3118 where_clause: WHERE a_expr { $$ = $2; }
3119 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
3122 relation_expr: relation_name
3124 /* normal relations */
3125 $$ = makeNode(RelExpr);
3129 | relation_name '*' %prec '='
3131 /* inheritance query */
3132 $$ = makeNode(RelExpr);
3137 opt_array_bounds: '[' ']' opt_array_bounds
3138 { $$ = lcons(makeInteger(-1), $3); }
3139 | '[' Iconst ']' opt_array_bounds
3140 { $$ = lcons(makeInteger($2), $4); }
3146 /*****************************************************************************
3149 * SQL92 introduces a large amount of type-specific syntax.
3150 * Define individual clauses to handle these cases, and use
3151 * the generic case to handle regular type-extensible Postgres syntax.
3152 * - thomas 1997-10-10
3154 *****************************************************************************/
3156 Typename: SimpleTypename opt_array_bounds
3159 $$->arrayBounds = $2;
3161 /* Is this the name of a complex type? If so, implement
3164 if (!strcmp(saved_relname, $$->name))
3165 /* This attr is the same type as the relation
3166 * being defined. The classic example: create
3167 * emp(name=text,mgr=emp)
3170 else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
3171 /* (Eventually add in here that the set can only
3172 * contain one element.)
3178 | SETOF SimpleTypename
3185 SimpleTypename: Generic
3193 $$ = makeNode(TypeName);
3194 $$->name = xlateSqlType($1);
3199 generic: IDENT { $$ = $1; }
3200 | TYPE_P { $$ = "type"; }
3203 /* SQL92 numeric data types
3204 * Check FLOAT() precision limits assuming IEEE floating types.
3205 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
3206 * - thomas 1997-09-18
3208 Numeric: FLOAT opt_float
3210 $$ = makeNode(TypeName);
3211 $$->name = xlateSqlType($2);
3216 $$ = makeNode(TypeName);
3217 $$->name = xlateSqlType("float");
3219 | DECIMAL opt_decimal
3221 $$ = makeNode(TypeName);
3222 $$->name = xlateSqlType("numeric");
3225 | NUMERIC opt_numeric
3227 $$ = makeNode(TypeName);
3228 $$->name = xlateSqlType("numeric");
3234 { $$ = xlateSqlType("float8"); }
3236 { $$ = xlateSqlType("float8"); }
3238 { $$ = xlateSqlType("numeric"); }
3240 { $$ = xlateSqlType("numeric"); }
3243 opt_float: '(' Iconst ')'
3246 elog(ERROR,"precision for FLOAT must be at least 1");
3248 $$ = xlateSqlType("float4");
3250 $$ = xlateSqlType("float8");
3252 elog(ERROR,"precision for FLOAT must be less than 16");
3256 $$ = xlateSqlType("float8");
3260 opt_numeric: '(' Iconst ',' Iconst ')'
3262 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
3263 elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
3264 $2, NUMERIC_MAX_PRECISION);
3265 if ($4 < 0 || $4 > $2)
3266 elog(ERROR,"NUMERIC scale %d must be between 0 and precision %d",
3269 $$ = (($2 << 16) | $4) + VARHDRSZ;
3273 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
3274 elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
3275 $2, NUMERIC_MAX_PRECISION);
3277 $$ = ($2 << 16) + VARHDRSZ;
3281 $$ = ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE) + VARHDRSZ;
3285 opt_decimal: '(' Iconst ',' Iconst ')'
3287 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
3288 elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
3289 $2, NUMERIC_MAX_PRECISION);
3290 if ($4 < 0 || $4 > $2)
3291 elog(ERROR,"DECIMAL scale %d must be between 0 and precision %d",
3294 $$ = (($2 << 16) | $4) + VARHDRSZ;
3298 if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
3299 elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
3300 $2, NUMERIC_MAX_PRECISION);
3302 $$ = ($2 << 16) + VARHDRSZ;
3306 $$ = ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE) + VARHDRSZ;
3311 /* SQL92 character data types
3312 * The following implements CHAR() and VARCHAR().
3314 Character: character '(' Iconst ')'
3316 $$ = makeNode(TypeName);
3317 if (strcasecmp($1, "char") == 0)
3318 $$->name = xlateSqlType("bpchar");
3319 else if (strcasecmp($1, "varchar") == 0)
3320 $$->name = xlateSqlType("varchar");
3322 yyerror("internal parsing error; unrecognized character type");
3325 elog(ERROR,"length for '%s' type must be at least 1",$1);
3326 else if ($3 > MaxAttrSize)
3327 elog(ERROR,"length for type '%s' cannot exceed %d",$1,
3330 /* we actually implement this sort of like a varlen, so
3331 * the first 4 bytes is the length. (the difference
3332 * between this and "text" is that we blank-pad and
3333 * truncate where necessary
3335 $$->typmod = VARHDRSZ + $3;
3339 $$ = makeNode(TypeName);
3340 /* Let's try to make all single-character types into bpchar(1)
3341 * - thomas 1998-05-07
3343 if (strcasecmp($1, "char") == 0)
3345 $$->name = xlateSqlType("bpchar");
3346 $$->typmod = VARHDRSZ + 1;
3350 $$->name = xlateSqlType($1);
3356 character: CHARACTER opt_varying opt_charset opt_collate
3359 if (($3 == NULL) || (strcasecmp($3, "sql_text") == 0)) {
3360 if ($2) type = xlateSqlType("varchar");
3361 else type = xlateSqlType("char");
3364 c = palloc(strlen("var") + strlen($3) + 1);
3367 type = xlateSqlType(c);
3369 type = xlateSqlType($3);
3373 elog(NOTICE,"COLLATE %s not yet implemented; clause ignored",$4);
3376 | CHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
3377 | VARCHAR { $$ = xlateSqlType("varchar"); }
3378 | NATIONAL CHARACTER opt_varying { $$ = xlateSqlType($3? "varchar": "char"); }
3379 | NCHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
3382 opt_varying: VARYING { $$ = TRUE; }
3383 | /*EMPTY*/ { $$ = FALSE; }
3386 opt_charset: CHARACTER SET ColId { $$ = $3; }
3387 | /*EMPTY*/ { $$ = NULL; }
3390 opt_collate: COLLATE ColId { $$ = $2; }
3391 | /*EMPTY*/ { $$ = NULL; }
3396 $$ = makeNode(TypeName);
3397 $$->name = xlateSqlType($1);
3400 | TIMESTAMP opt_timezone
3402 $$ = makeNode(TypeName);
3403 $$->name = xlateSqlType("timestamp");
3409 $$ = makeNode(TypeName);
3410 $$->name = xlateSqlType("time");
3413 | INTERVAL opt_interval
3415 $$ = makeNode(TypeName);
3416 $$->name = xlateSqlType("interval");
3421 datetime: YEAR_P { $$ = "year"; }
3422 | MONTH_P { $$ = "month"; }
3423 | DAY_P { $$ = "day"; }
3424 | HOUR_P { $$ = "hour"; }
3425 | MINUTE_P { $$ = "minute"; }
3426 | SECOND_P { $$ = "second"; }
3429 opt_timezone: WITH TIME ZONE { $$ = TRUE; }
3430 | /*EMPTY*/ { $$ = FALSE; }
3433 opt_interval: datetime { $$ = lcons($1, NIL); }
3434 | YEAR_P TO MONTH_P { $$ = NIL; }
3435 | DAY_P TO HOUR_P { $$ = NIL; }
3436 | DAY_P TO MINUTE_P { $$ = NIL; }
3437 | DAY_P TO SECOND_P { $$ = NIL; }
3438 | HOUR_P TO MINUTE_P { $$ = NIL; }
3439 | HOUR_P TO SECOND_P { $$ = NIL; }
3440 | MINUTE_P TO SECOND_P { $$ = NIL; }
3441 | /*EMPTY*/ { $$ = NIL; }
3445 /*****************************************************************************
3447 * expression grammar, still needs some cleanup
3449 *****************************************************************************/
3451 a_expr_or_null: a_expr
3455 A_Const *n = makeNode(A_Const);
3456 n->val.type = T_Null;
3461 /* Expressions using row descriptors
3462 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
3463 * with singleton expressions.
3464 * Eliminated lots of code by defining row_op and sub_type clauses.
3465 * However, can not consolidate EXPR_LINK case with others subselects
3466 * due to shift/reduce conflict with the non-subselect clause (the parser
3467 * would have to look ahead more than one token to resolve the conflict).
3468 * - thomas 1998-05-09
3470 row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
3472 SubLink *n = makeNode(SubLink);
3474 n->oper = lcons("=",NIL);
3476 n->subLinkType = ANY_SUBLINK;
3480 | '(' row_descriptor ')' NOT IN '(' SubSelect ')'
3482 SubLink *n = makeNode(SubLink);
3484 n->oper = lcons("<>",NIL);
3486 n->subLinkType = ALL_SUBLINK;
3490 | '(' row_descriptor ')' row_op sub_type '(' SubSelect ')'
3492 SubLink *n = makeNode(SubLink);
3494 n->oper = lcons($4, NIL);
3495 if (strcmp($4,"<>") == 0)
3499 n->subLinkType = $5;
3503 | '(' row_descriptor ')' row_op '(' SubSelect ')'
3505 SubLink *n = makeNode(SubLink);
3507 n->oper = lcons($4, NIL);
3508 if (strcmp($4,"<>") == 0)
3512 n->subLinkType = EXPR_SUBLINK;
3516 | '(' row_descriptor ')' row_op '(' row_descriptor ')'
3518 $$ = makeRowExpr($4, $2, $6);
3522 row_descriptor: row_list ',' a_expr
3524 $$ = lappend($1, $3);
3528 row_list: row_list ',' a_expr
3530 $$ = lappend($1, $3);
3534 $$ = lcons($1, NIL);
3538 row_op: Op { $$ = $1; }
3551 sub_type: ANY { $$ = ANY_SUBLINK; }
3552 | ALL { $$ = ALL_SUBLINK; }
3555 /* General expressions
3556 * This is the heart of the expression syntax.
3557 * Note that the BETWEEN clause looks similar to a boolean expression
3558 * and so we must define b_expr which is almost the same as a_expr
3559 * but without the boolean expressions.
3560 * All operations/expressions are allowed in a BETWEEN clause
3561 * if surrounded by parens.
3564 { $$ = (Node *) $1; }
3569 | ColId opt_indirection
3571 /* could be a column name or a relation_name */
3572 Ident *n = makeNode(Ident);
3574 n->indirection = $2;
3577 | '-' a_expr %prec UMINUS
3578 { $$ = doNegate($2); }
3580 { $$ = makeA_Expr(OP, "%", NULL, $2); }
3582 { $$ = makeA_Expr(OP, "^", NULL, $2); }
3584 { $$ = makeA_Expr(OP, "|", NULL, $2); }
3586 { $$ = makeA_Expr(OP, ":", NULL, $2); }
3588 { $$ = makeA_Expr(OP, ";", NULL, $2); }
3590 { $$ = makeA_Expr(OP, "%", $1, NULL); }
3592 { $$ = makeA_Expr(OP, "^", $1, NULL); }
3594 { $$ = makeA_Expr(OP, "|", $1, NULL); }
3596 { $$ = makeA_Expr(OP, "+", $1, $3); }
3598 { $$ = makeA_Expr(OP, "-", $1, $3); }
3600 { $$ = makeA_Expr(OP, "*", $1, $3); }
3602 { $$ = makeA_Expr(OP, "/", $1, $3); }
3604 { $$ = makeA_Expr(OP, "%", $1, $3); }
3606 { $$ = makeA_Expr(OP, "^", $1, $3); }
3608 { $$ = makeA_Expr(OP, "|", $1, $3); }
3610 { $$ = makeA_Expr(OP, "<", $1, $3); }
3612 { $$ = makeA_Expr(OP, ">", $1, $3); }
3615 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3616 /* We allow this for standards-broken SQL products, like MS stuff */
3618 { $$ = makeA_Expr(ISNULL, NULL, $3, NULL); }
3621 { $$ = makeA_Expr(OP, "=", $1, $3); }
3622 | a_expr TYPECAST Typename
3625 /* AexprConst can be either A_Const or ParamNo */
3626 if (nodeTag($1) == T_A_Const) {
3627 ((A_Const *)$1)->typename = $3;
3628 } else if (nodeTag($1) == T_ParamNo) {
3629 ((ParamNo *)$1)->typename = $3;
3630 /* otherwise, try to transform to a function call */
3632 FuncCall *n = makeNode(FuncCall);
3633 n->funcname = $3->name;
3634 n->args = lcons($1,NIL);
3638 | CAST '(' a_expr AS Typename ')'
3641 /* AexprConst can be either A_Const or ParamNo */
3642 if (nodeTag($3) == T_A_Const) {
3643 ((A_Const *)$3)->typename = $5;
3644 } else if (nodeTag($3) == T_ParamNo) {
3645 ((ParamNo *)$3)->typename = $5;
3646 /* otherwise, try to transform to a function call */
3648 FuncCall *n = makeNode(FuncCall);
3649 n->funcname = $5->name;
3650 n->args = lcons($3,NIL);
3654 | '(' a_expr_or_null ')'
3657 { $$ = makeA_Expr(OP, $2, $1, $3); }
3658 | a_expr LIKE a_expr
3659 { $$ = makeA_Expr(OP, "~~", $1, $3); }
3660 | a_expr NOT LIKE a_expr
3661 { $$ = makeA_Expr(OP, "!~~", $1, $4); }
3663 { $$ = makeA_Expr(OP, $1, NULL, $2); }
3665 { $$ = makeA_Expr(OP, $2, $1, NULL); }
3666 | func_name '(' '*' ')'
3668 /* cheap hack for aggregate (eg. count) */
3669 FuncCall *n = makeNode(FuncCall);
3670 A_Const *star = makeNode(A_Const);
3672 star->val.type = T_String;
3673 star->val.val.str = "";
3675 n->args = lcons(star, NIL);
3680 FuncCall *n = makeNode(FuncCall);
3685 | func_name '(' expr_list ')'
3687 FuncCall *n = makeNode(FuncCall);
3694 A_Const *n = makeNode(A_Const);
3695 TypeName *t = makeNode(TypeName);
3697 n->val.type = T_String;
3698 n->val.val.str = "now";
3701 t->name = xlateSqlType("date");
3709 A_Const *n = makeNode(A_Const);
3710 TypeName *t = makeNode(TypeName);
3712 n->val.type = T_String;
3713 n->val.val.str = "now";
3716 t->name = xlateSqlType("time");
3722 | CURRENT_TIME '(' Iconst ')'
3724 FuncCall *n = makeNode(FuncCall);
3725 A_Const *s = makeNode(A_Const);
3726 TypeName *t = makeNode(TypeName);
3728 n->funcname = xlateSqlType("time");
3729 n->args = lcons(s, NIL);
3731 s->val.type = T_String;
3732 s->val.val.str = "now";
3735 t->name = xlateSqlType("time");
3740 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3746 A_Const *n = makeNode(A_Const);
3747 TypeName *t = makeNode(TypeName);
3749 n->val.type = T_String;
3750 n->val.val.str = "now";
3753 t->name = xlateSqlType("timestamp");
3759 | CURRENT_TIMESTAMP '(' Iconst ')'
3761 FuncCall *n = makeNode(FuncCall);
3762 A_Const *s = makeNode(A_Const);
3763 TypeName *t = makeNode(TypeName);
3765 n->funcname = xlateSqlType("timestamp");
3766 n->args = lcons(s, NIL);
3768 s->val.type = T_String;
3769 s->val.val.str = "now";
3772 t->name = xlateSqlType("timestamp");
3777 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
3783 FuncCall *n = makeNode(FuncCall);
3784 n->funcname = "getpgusername";
3790 FuncCall *n = makeNode(FuncCall);
3791 n->funcname = "getpgusername";
3795 | EXISTS '(' SubSelect ')'
3797 SubLink *n = makeNode(SubLink);
3801 n->subLinkType = EXISTS_SUBLINK;
3805 | EXTRACT '(' extract_list ')'
3807 FuncCall *n = makeNode(FuncCall);
3808 n->funcname = "date_part";
3812 | POSITION '(' position_list ')'
3814 FuncCall *n = makeNode(FuncCall);
3815 n->funcname = "strpos";
3819 | SUBSTRING '(' substr_list ')'
3821 FuncCall *n = makeNode(FuncCall);
3822 n->funcname = "substr";
3826 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
3827 | TRIM '(' BOTH trim_list ')'
3829 FuncCall *n = makeNode(FuncCall);
3830 n->funcname = "btrim";
3834 | TRIM '(' LEADING trim_list ')'
3836 FuncCall *n = makeNode(FuncCall);
3837 n->funcname = "ltrim";
3841 | TRIM '(' TRAILING trim_list ')'
3843 FuncCall *n = makeNode(FuncCall);
3844 n->funcname = "rtrim";
3848 | TRIM '(' trim_list ')'
3850 FuncCall *n = makeNode(FuncCall);
3851 n->funcname = "btrim";
3856 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3858 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3860 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3861 | a_expr IS NOT NULL_P
3862 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3863 /* IS TRUE, IS FALSE, etc used to be function calls
3864 * but let's make them expressions to allow the optimizer
3865 * a chance to eliminate them if a_expr is a constant string.
3866 * - thomas 1997-12-22
3870 A_Const *n = makeNode(A_Const);
3871 n->val.type = T_String;
3872 n->val.val.str = "t";
3873 n->typename = makeNode(TypeName);
3874 n->typename->name = xlateSqlType("bool");
3875 n->typename->typmod = -1;
3876 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3878 | a_expr IS NOT FALSE_P
3880 A_Const *n = makeNode(A_Const);
3881 n->val.type = T_String;
3882 n->val.val.str = "t";
3883 n->typename = makeNode(TypeName);
3884 n->typename->name = xlateSqlType("bool");
3885 n->typename->typmod = -1;
3886 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3890 A_Const *n = makeNode(A_Const);
3891 n->val.type = T_String;
3892 n->val.val.str = "f";
3893 n->typename = makeNode(TypeName);
3894 n->typename->name = xlateSqlType("bool");
3895 n->typename->typmod = -1;
3896 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3898 | a_expr IS NOT TRUE_P
3900 A_Const *n = makeNode(A_Const);
3901 n->val.type = T_String;
3902 n->val.val.str = "f";
3903 n->typename = makeNode(TypeName);
3904 n->typename->name = xlateSqlType("bool");
3905 n->typename->typmod = -1;
3906 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3908 | a_expr BETWEEN b_expr AND b_expr
3910 $$ = makeA_Expr(AND, NULL,
3911 makeA_Expr(OP, ">=", $1, $3),
3912 makeA_Expr(OP, "<=", $1, $5));
3914 | a_expr NOT BETWEEN b_expr AND b_expr
3916 $$ = makeA_Expr(OR, NULL,
3917 makeA_Expr(OP, "<", $1, $4),
3918 makeA_Expr(OP, ">", $1, $6));
3920 | a_expr IN '(' in_expr ')'
3922 /* in_expr returns a SubLink or a list of a_exprs */
3923 if (IsA($4, SubLink))
3925 SubLink *n = (SubLink *)$4;
3926 n->lefthand = lcons($1, NIL);
3927 n->oper = lcons("=",NIL);
3929 n->subLinkType = ANY_SUBLINK;
3936 foreach(l, (List *) $4)
3938 Node *cmp = makeA_Expr(OP, "=", $1, lfirst(l));
3942 n = makeA_Expr(OR, NULL, n, cmp);
3947 | a_expr NOT IN '(' in_expr ')'
3949 /* in_expr returns a SubLink or a list of a_exprs */
3950 if (IsA($5, SubLink))
3952 SubLink *n = (SubLink *)$5;
3953 n->lefthand = lcons($1, NIL);
3954 n->oper = lcons("<>",NIL);
3956 n->subLinkType = ALL_SUBLINK;
3963 foreach(l, (List *) $5)
3965 Node *cmp = makeA_Expr(OP, "<>", $1, lfirst(l));
3969 n = makeA_Expr(AND, NULL, n, cmp);
3974 | a_expr Op '(' SubSelect ')'
3976 SubLink *n = makeNode(SubLink);
3977 n->lefthand = lcons($1, NULL);
3978 n->oper = lcons($2,NIL);
3980 n->subLinkType = EXPR_SUBLINK;
3984 | a_expr '+' '(' SubSelect ')'
3986 SubLink *n = makeNode(SubLink);
3987 n->lefthand = lcons($1, NULL);
3988 n->oper = lcons("+",NIL);
3990 n->subLinkType = EXPR_SUBLINK;
3994 | a_expr '-' '(' SubSelect ')'
3996 SubLink *n = makeNode(SubLink);
3997 n->lefthand = lcons($1, NULL);
3998 n->oper = lcons("-",NIL);
4000 n->subLinkType = EXPR_SUBLINK;
4004 | a_expr '*' '(' SubSelect ')'
4006 SubLink *n = makeNode(SubLink);
4007 n->lefthand = lcons($1, NULL);
4008 n->oper = lcons("*",NIL);
4010 n->subLinkType = EXPR_SUBLINK;
4014 | a_expr '/' '(' SubSelect ')'
4016 SubLink *n = makeNode(SubLink);
4017 n->lefthand = lcons($1, NULL);
4018 n->oper = lcons("/",NIL);
4020 n->subLinkType = EXPR_SUBLINK;
4024 | a_expr '%' '(' SubSelect ')'
4026 SubLink *n = makeNode(SubLink);
4027 n->lefthand = lcons($1, NULL);
4028 n->oper = lcons("%",NIL);
4030 n->subLinkType = EXPR_SUBLINK;
4034 | a_expr '^' '(' SubSelect ')'
4036 SubLink *n = makeNode(SubLink);
4037 n->lefthand = lcons($1, NULL);
4038 n->oper = lcons("^",NIL);
4040 n->subLinkType = EXPR_SUBLINK;
4044 | a_expr '|' '(' SubSelect ')'
4046 SubLink *n = makeNode(SubLink);
4047 n->lefthand = lcons($1, NULL);
4048 n->oper = lcons("|",NIL);
4050 n->subLinkType = EXPR_SUBLINK;
4054 | a_expr '<' '(' SubSelect ')'
4056 SubLink *n = makeNode(SubLink);
4057 n->lefthand = lcons($1, NULL);
4058 n->oper = lcons("<",NIL);
4060 n->subLinkType = EXPR_SUBLINK;
4064 | a_expr '>' '(' SubSelect ')'
4066 SubLink *n = makeNode(SubLink);
4067 n->lefthand = lcons($1, NULL);
4068 n->oper = lcons(">",NIL);
4070 n->subLinkType = EXPR_SUBLINK;
4074 | a_expr '=' '(' SubSelect ')'
4076 SubLink *n = makeNode(SubLink);
4077 n->lefthand = lcons($1, NULL);
4078 n->oper = lcons("=",NIL);
4080 n->subLinkType = EXPR_SUBLINK;
4084 | a_expr Op ANY '(' SubSelect ')'
4086 SubLink *n = makeNode(SubLink);
4087 n->lefthand = lcons($1,NIL);
4088 n->oper = lcons($2,NIL);
4090 n->subLinkType = ANY_SUBLINK;
4094 | a_expr '+' ANY '(' SubSelect ')'
4096 SubLink *n = makeNode(SubLink);
4097 n->lefthand = lcons($1,NIL);
4098 n->oper = lcons("+",NIL);
4100 n->subLinkType = ANY_SUBLINK;
4104 | a_expr '-' ANY '(' SubSelect ')'
4106 SubLink *n = makeNode(SubLink);
4107 n->lefthand = lcons($1,NIL);
4108 n->oper = lcons("-",NIL);
4110 n->subLinkType = ANY_SUBLINK;
4114 | a_expr '*' ANY '(' SubSelect ')'
4116 SubLink *n = makeNode(SubLink);
4117 n->lefthand = lcons($1,NIL);
4118 n->oper = lcons("*",NIL);
4120 n->subLinkType = ANY_SUBLINK;
4124 | a_expr '/' ANY '(' SubSelect ')'
4126 SubLink *n = makeNode(SubLink);
4127 n->lefthand = lcons($1,NIL);
4128 n->oper = lcons("/",NIL);
4130 n->subLinkType = ANY_SUBLINK;
4134 | a_expr '%' ANY '(' SubSelect ')'
4136 SubLink *n = makeNode(SubLink);
4137 n->lefthand = lcons($1,NIL);
4138 n->oper = lcons("%",NIL);
4140 n->subLinkType = ANY_SUBLINK;
4144 | a_expr '^' ANY '(' SubSelect ')'
4146 SubLink *n = makeNode(SubLink);
4147 n->lefthand = lcons($1,NIL);
4148 n->oper = lcons("^",NIL);
4150 n->subLinkType = ANY_SUBLINK;
4154 | a_expr '|' ANY '(' SubSelect ')'
4156 SubLink *n = makeNode(SubLink);
4157 n->lefthand = lcons($1,NIL);
4158 n->oper = lcons("|",NIL);
4160 n->subLinkType = ANY_SUBLINK;
4164 | a_expr '<' ANY '(' SubSelect ')'
4166 SubLink *n = makeNode(SubLink);
4167 n->lefthand = lcons($1,NIL);
4168 n->oper = lcons("<",NIL);
4170 n->subLinkType = ANY_SUBLINK;
4174 | a_expr '>' ANY '(' SubSelect ')'
4176 SubLink *n = makeNode(SubLink);
4177 n->lefthand = lcons($1,NIL);
4178 n->oper = lcons(">",NIL);
4180 n->subLinkType = ANY_SUBLINK;
4184 | a_expr '=' ANY '(' SubSelect ')'
4186 SubLink *n = makeNode(SubLink);
4187 n->lefthand = lcons($1,NIL);
4188 n->oper = lcons("=",NIL);
4190 n->subLinkType = ANY_SUBLINK;
4194 | a_expr Op ALL '(' SubSelect ')'
4196 SubLink *n = makeNode(SubLink);
4197 n->lefthand = lcons($1, NULL);
4198 n->oper = lcons($2,NIL);
4200 n->subLinkType = ALL_SUBLINK;
4204 | a_expr '+' ALL '(' SubSelect ')'
4206 SubLink *n = makeNode(SubLink);
4207 n->lefthand = lcons($1, NULL);
4208 n->oper = lcons("+",NIL);
4210 n->subLinkType = ALL_SUBLINK;
4214 | a_expr '-' ALL '(' SubSelect ')'
4216 SubLink *n = makeNode(SubLink);
4217 n->lefthand = lcons($1, NULL);
4218 n->oper = lcons("-",NIL);
4220 n->subLinkType = ALL_SUBLINK;
4224 | a_expr '*' ALL '(' SubSelect ')'
4226 SubLink *n = makeNode(SubLink);
4227 n->lefthand = lcons($1, NULL);
4228 n->oper = lcons("*",NIL);
4230 n->subLinkType = ALL_SUBLINK;
4234 | a_expr '/' ALL '(' SubSelect ')'
4236 SubLink *n = makeNode(SubLink);
4237 n->lefthand = lcons($1, NULL);
4238 n->oper = lcons("/",NIL);
4240 n->subLinkType = ALL_SUBLINK;
4244 | a_expr '%' ALL '(' SubSelect ')'
4246 SubLink *n = makeNode(SubLink);
4247 n->lefthand = lcons($1, NULL);
4248 n->oper = lcons("%",NIL);
4250 n->subLinkType = ALL_SUBLINK;
4254 | a_expr '^' ALL '(' SubSelect ')'
4256 SubLink *n = makeNode(SubLink);
4257 n->lefthand = lcons($1, NULL);
4258 n->oper = lcons("^",NIL);
4260 n->subLinkType = ALL_SUBLINK;
4264 | a_expr '|' ALL '(' SubSelect ')'
4266 SubLink *n = makeNode(SubLink);
4267 n->lefthand = lcons($1, NULL);
4268 n->oper = lcons("|",NIL);
4270 n->subLinkType = ALL_SUBLINK;
4274 | a_expr '<' ALL '(' SubSelect ')'
4276 SubLink *n = makeNode(SubLink);
4277 n->lefthand = lcons($1, NULL);
4278 n->oper = lcons("<",NIL);
4280 n->subLinkType = ALL_SUBLINK;
4284 | a_expr '>' ALL '(' SubSelect ')'
4286 SubLink *n = makeNode(SubLink);
4287 n->lefthand = lcons($1, NULL);
4288 n->oper = lcons(">",NIL);
4290 n->subLinkType = ALL_SUBLINK;
4294 | a_expr '=' ALL '(' SubSelect ')'
4296 SubLink *n = makeNode(SubLink);
4297 n->lefthand = lcons($1, NULL);
4298 n->oper = lcons("=",NIL);
4300 n->subLinkType = ALL_SUBLINK;
4305 { $$ = makeA_Expr(AND, NULL, $1, $3); }
4307 { $$ = makeA_Expr(OR, NULL, $1, $3); }
4309 { $$ = makeA_Expr(NOT, NULL, NULL, $2); }
4314 /* Restricted expressions
4315 * b_expr is a subset of the complete expression syntax
4316 * defined by a_expr. b_expr is used in BETWEEN clauses
4317 * to eliminate parser ambiguities stemming from the AND keyword,
4318 * and also in POSITION clauses where the IN keyword gives trouble.
4321 { $$ = (Node *) $1; }
4324 | ColId opt_indirection
4326 /* could be a column name or a relation_name */
4327 Ident *n = makeNode(Ident);
4329 n->indirection = $2;
4332 | '-' b_expr %prec UMINUS
4333 { $$ = doNegate($2); }
4335 { $$ = makeA_Expr(OP, "%", NULL, $2); }
4337 { $$ = makeA_Expr(OP, "^", NULL, $2); }
4339 { $$ = makeA_Expr(OP, "|", NULL, $2); }
4341 { $$ = makeA_Expr(OP, ":", NULL, $2); }
4343 { $$ = makeA_Expr(OP, ";", NULL, $2); }
4345 { $$ = makeA_Expr(OP, "%", $1, NULL); }
4347 { $$ = makeA_Expr(OP, "^", $1, NULL); }
4349 { $$ = makeA_Expr(OP, "|", $1, NULL); }
4351 { $$ = makeA_Expr(OP, "+", $1, $3); }
4353 { $$ = makeA_Expr(OP, "-", $1, $3); }
4355 { $$ = makeA_Expr(OP, "*", $1, $3); }
4357 { $$ = makeA_Expr(OP, "/", $1, $3); }
4359 { $$ = makeA_Expr(OP, "%", $1, $3); }
4361 { $$ = makeA_Expr(OP, "^", $1, $3); }
4363 { $$ = makeA_Expr(OP, "|", $1, $3); }
4364 | b_expr TYPECAST Typename
4367 /* AexprConst can be either A_Const or ParamNo */
4368 if (nodeTag($1) == T_A_Const) {
4369 ((A_Const *)$1)->typename = $3;
4370 } else if (nodeTag($1) == T_ParamNo) {
4371 ((ParamNo *)$1)->typename = $3;
4372 /* otherwise, try to transform to a function call */
4374 FuncCall *n = makeNode(FuncCall);
4375 n->funcname = $3->name;
4376 n->args = lcons($1,NIL);
4380 | CAST '(' b_expr AS Typename ')'
4383 /* AexprConst can be either A_Const or ParamNo */
4384 if (nodeTag($3) == T_A_Const) {
4385 ((A_Const *)$3)->typename = $5;
4386 } else if (nodeTag($3) == T_ParamNo) {
4387 ((ParamNo *)$3)->typename = $5;
4388 /* otherwise, try to transform to a function call */
4390 FuncCall *n = makeNode(FuncCall);
4391 n->funcname = $5->name;
4392 n->args = lcons($3,NIL);
4399 { $$ = makeA_Expr(OP, $2,$1,$3); }
4401 { $$ = makeA_Expr(OP, $1, NULL, $2); }
4403 { $$ = makeA_Expr(OP, $2, $1, NULL); }
4406 FuncCall *n = makeNode(FuncCall);
4411 | func_name '(' expr_list ')'
4413 FuncCall *n = makeNode(FuncCall);
4420 A_Const *n = makeNode(A_Const);
4421 TypeName *t = makeNode(TypeName);
4423 n->val.type = T_String;
4424 n->val.val.str = "now";
4427 t->name = xlateSqlType("date");
4435 A_Const *n = makeNode(A_Const);
4436 TypeName *t = makeNode(TypeName);
4438 n->val.type = T_String;
4439 n->val.val.str = "now";
4442 t->name = xlateSqlType("time");
4448 | CURRENT_TIME '(' Iconst ')'
4450 FuncCall *n = makeNode(FuncCall);
4451 A_Const *s = makeNode(A_Const);
4452 TypeName *t = makeNode(TypeName);
4454 n->funcname = xlateSqlType("time");
4455 n->args = lcons(s, NIL);
4457 s->val.type = T_String;
4458 s->val.val.str = "now";
4461 t->name = xlateSqlType("time");
4466 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
4472 A_Const *n = makeNode(A_Const);
4473 TypeName *t = makeNode(TypeName);
4475 n->val.type = T_String;
4476 n->val.val.str = "now";
4479 t->name = xlateSqlType("timestamp");
4485 | CURRENT_TIMESTAMP '(' Iconst ')'
4487 FuncCall *n = makeNode(FuncCall);
4488 A_Const *s = makeNode(A_Const);
4489 TypeName *t = makeNode(TypeName);
4491 n->funcname = xlateSqlType("timestamp");
4492 n->args = lcons(s, NIL);
4494 s->val.type = T_String;
4495 s->val.val.str = "now";
4498 t->name = xlateSqlType("timestamp");
4503 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
4509 FuncCall *n = makeNode(FuncCall);
4510 n->funcname = "getpgusername";
4516 FuncCall *n = makeNode(FuncCall);
4517 n->funcname = "getpgusername";
4521 | POSITION '(' position_list ')'
4523 FuncCall *n = makeNode(FuncCall);
4524 n->funcname = "strpos";
4528 | SUBSTRING '(' substr_list ')'
4530 FuncCall *n = makeNode(FuncCall);
4531 n->funcname = "substr";
4535 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
4536 | TRIM '(' BOTH trim_list ')'
4538 FuncCall *n = makeNode(FuncCall);
4539 n->funcname = "btrim";
4543 | TRIM '(' LEADING trim_list ')'
4545 FuncCall *n = makeNode(FuncCall);
4546 n->funcname = "ltrim";
4550 | TRIM '(' TRAILING trim_list ')'
4552 FuncCall *n = makeNode(FuncCall);
4553 n->funcname = "rtrim";
4557 | TRIM '(' trim_list ')'
4559 FuncCall *n = makeNode(FuncCall);
4560 n->funcname = "btrim";
4566 opt_indirection: '[' a_expr ']' opt_indirection
4568 A_Indices *ai = makeNode(A_Indices);
4573 | '[' a_expr ':' a_expr ']' opt_indirection
4575 A_Indices *ai = makeNode(A_Indices);
4584 expr_list: a_expr_or_null
4585 { $$ = lcons($1, NIL); }
4586 | expr_list ',' a_expr_or_null
4587 { $$ = lappend($1, $3); }
4588 | expr_list USING a_expr
4589 { $$ = lappend($1, $3); }
4592 extract_list: extract_arg FROM a_expr
4594 A_Const *n = makeNode(A_Const);
4595 n->val.type = T_String;
4596 n->val.val.str = $1;
4597 $$ = lappend(lcons((Node *)n,NIL), $3);
4603 extract_arg: datetime { $$ = $1; }
4604 | TIMEZONE_HOUR { $$ = "tz_hour"; }
4605 | TIMEZONE_MINUTE { $$ = "tz_minute"; }
4608 /* position_list uses b_expr not a_expr to avoid conflict with general IN */
4610 position_list: b_expr IN b_expr
4611 { $$ = makeList($3, $1, -1); }
4616 substr_list: expr_list substr_from substr_for
4618 $$ = nconc(nconc($1,$2),$3);
4624 substr_from: FROM expr_list
4628 A_Const *n = makeNode(A_Const);
4629 n->val.type = T_Integer;
4630 n->val.val.ival = 1;
4631 $$ = lcons((Node *)n,NIL);
4635 substr_for: FOR expr_list
4641 trim_list: a_expr FROM expr_list
4642 { $$ = lappend($3, $1); }
4651 SubLink *n = makeNode(SubLink);
4656 { $$ = (Node *)$1; }
4659 in_expr_nodes: a_expr
4660 { $$ = lcons($1, NIL); }
4661 | in_expr_nodes ',' a_expr
4662 { $$ = lappend($1, $3); }
4666 * Define SQL92-style case clause.
4667 * Allow all four forms described in the standard:
4668 * - Full specification
4669 * CASE WHEN a = b THEN c ... ELSE d END
4670 * - Implicit argument
4671 * CASE a WHEN b THEN c ... ELSE d END
4672 * - Conditional NULL
4674 * same as CASE WHEN x = y THEN NULL ELSE x END
4675 * - Conditional substitution from list, use first non-null argument
4677 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
4678 * - thomas 1998-11-09
4680 case_expr: CASE case_arg when_clause_list case_default END_TRANS
4682 CaseExpr *c = makeNode(CaseExpr);
4688 | NULLIF '(' a_expr ',' a_expr ')'
4690 CaseExpr *c = makeNode(CaseExpr);
4691 CaseWhen *w = makeNode(CaseWhen);
4693 A_Const *n = makeNode(A_Const);
4694 n->val.type = T_Null;
4695 w->result = (Node *)n;
4697 w->expr = makeA_Expr(OP, "=", $3, $5);
4698 c->args = lcons(w, NIL);
4702 | COALESCE '(' expr_list ')'
4704 CaseExpr *c = makeNode(CaseExpr);
4709 w = makeNode(CaseWhen);
4710 w->expr = makeA_Expr(NOTNULL, NULL, lfirst(l), NULL);
4711 w->result = lfirst(l);
4712 c->args = lappend(c->args, w);
4718 when_clause_list: when_clause_list when_clause
4719 { $$ = lappend($1, $2); }
4721 { $$ = lcons($1, NIL); }
4724 when_clause: WHEN a_expr THEN a_expr_or_null
4726 CaseWhen *w = makeNode(CaseWhen);
4733 case_default: ELSE a_expr_or_null { $$ = $2; }
4734 | /*EMPTY*/ { $$ = NULL; }
4743 attr: relation_name '.' attrs opt_indirection
4745 $$ = makeNode(Attr);
4749 $$->indirection = $4;
4751 | ParamNo '.' attrs opt_indirection
4753 $$ = makeNode(Attr);
4757 $$->indirection = $4;
4762 { $$ = lcons(makeString($1), NIL); }
4763 | attrs '.' attr_name
4764 { $$ = lappend($1, makeString($3)); }
4766 { $$ = lappend($1, makeString("*")); }
4770 /*****************************************************************************
4774 *****************************************************************************/
4776 /* Target lists as found in SELECT ... and INSERT VALUES ( ... ) */
4778 target_list: target_list ',' target_el
4779 { $$ = lappend($1, $3); }
4781 { $$ = lcons($1, NIL); }
4784 /* AS is not optional because shift/red conflict with unary ops */
4785 target_el: a_expr_or_null AS ColLabel
4787 $$ = makeNode(ResTarget);
4789 $$->indirection = NULL;
4790 $$->val = (Node *)$1;
4794 $$ = makeNode(ResTarget);
4796 $$->indirection = NULL;
4797 $$->val = (Node *)$1;
4799 | relation_name '.' '*'
4801 Attr *att = makeNode(Attr);
4803 att->paramNo = NULL;
4804 att->attrs = lcons(makeString("*"), NIL);
4805 att->indirection = NIL;
4806 $$ = makeNode(ResTarget);
4808 $$->indirection = NULL;
4809 $$->val = (Node *)att;
4813 Attr *att = makeNode(Attr);
4815 att->paramNo = NULL;
4817 att->indirection = NIL;
4818 $$ = makeNode(ResTarget);
4820 $$->indirection = NULL;
4821 $$->val = (Node *)att;
4825 /* Target list as found in UPDATE table SET ... */
4827 update_target_list: update_target_list ',' update_target_el
4828 { $$ = lappend($1,$3); }
4830 { $$ = lcons($1, NIL); }
4833 update_target_el: ColId opt_indirection '=' a_expr_or_null
4835 $$ = makeNode(ResTarget);
4837 $$->indirection = $2;
4838 $$->val = (Node *)$4;
4842 /*****************************************************************************
4844 * Names and constants
4846 *****************************************************************************/
4848 relation_name: SpecialRuleRelation
4851 StrNCpy(saved_relname, $1, NAMEDATALEN);
4855 /* disallow refs to variable system tables */
4856 if (strcmp(LogRelationName, $1) == 0
4857 || strcmp(VariableRelationName, $1) == 0)
4858 elog(ERROR,"%s cannot be accessed by users",$1);
4861 StrNCpy(saved_relname, $1, NAMEDATALEN);
4865 database_name: ColId { $$ = $1; };
4866 access_method: IDENT { $$ = $1; };
4867 attr_name: ColId { $$ = $1; };
4868 class: IDENT { $$ = $1; };
4869 index_name: ColId { $$ = $1; };
4872 * Include date/time keywords as SQL92 extension.
4873 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
4875 name: ColId { $$ = $1; };
4876 func_name: ColId { $$ = xlateSqlFunc($1); };
4878 file_name: Sconst { $$ = $1; };
4879 /* NOT USED recipe_name: IDENT { $$ = $1; };*/
4882 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
4886 A_Const *n = makeNode(A_Const);
4887 n->val.type = T_Integer;
4888 n->val.val.ival = $1;
4893 A_Const *n = makeNode(A_Const);
4894 n->val.type = T_Float;
4895 n->val.val.dval = $1;
4900 A_Const *n = makeNode(A_Const);
4901 n->val.type = T_String;
4902 n->val.val.str = $1;
4905 /* this rule formerly used Typename, but that causes reduce conflicts
4906 * with subscripted column names ...
4908 | SimpleTypename Sconst
4910 A_Const *n = makeNode(A_Const);
4912 n->val.type = T_String;
4913 n->val.val.str = $2;
4917 { $$ = (Node *)$1; }
4920 A_Const *n = makeNode(A_Const);
4921 n->val.type = T_String;
4922 n->val.val.str = "t";
4923 n->typename = makeNode(TypeName);
4924 n->typename->name = xlateSqlType("bool");
4925 n->typename->typmod = -1;
4930 A_Const *n = makeNode(A_Const);
4931 n->val.type = T_String;
4932 n->val.val.str = "f";
4933 n->typename = makeNode(TypeName);
4934 n->typename->name = xlateSqlType("bool");
4935 n->typename->typmod = -1;
4940 ParamNo: PARAM opt_indirection
4942 $$ = makeNode(ParamNo);
4944 $$->indirection = $2;
4948 Iconst: ICONST { $$ = $1; };
4949 Sconst: SCONST { $$ = $1; };
4950 UserId: IDENT { $$ = $1; };
4952 /* Column and type identifier
4953 * Does not include explicit datetime types
4954 * since these must be decoupled in Typename syntax.
4955 * Use ColId for most identifiers. - thomas 1997-10-21
4958 { $$ = xlateSqlType($1); }
4960 { $$ = xlateSqlType($1); }
4962 { $$ = xlateSqlType($1); }
4964 /* Column identifier
4965 * Include date/time keywords as SQL92 extension.
4966 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
4967 * Add other keywords. Note that as the syntax expands,
4968 * some of these keywords will have to be removed from this
4969 * list due to shift/reduce conflicts in yacc. If so, move
4970 * down to the ColLabel entity. - thomas 1997-11-06
4972 ColId: IDENT { $$ = $1; }
4973 | datetime { $$ = $1; }
4974 | ABSOLUTE { $$ = "absolute"; }
4975 | ACCESS { $$ = "access"; }
4976 | ACTION { $$ = "action"; }
4977 | AFTER { $$ = "after"; }
4978 | AGGREGATE { $$ = "aggregate"; }
4979 | BACKWARD { $$ = "backward"; }
4980 | BEFORE { $$ = "before"; }
4981 | CACHE { $$ = "cache"; }
4982 | COMMITTED { $$ = "committed"; }
4983 | CONSTRAINTS { $$ = "constraints"; }
4984 | CREATEDB { $$ = "createdb"; }
4985 | CREATEUSER { $$ = "createuser"; }
4986 | CYCLE { $$ = "cycle"; }
4987 | DATABASE { $$ = "database"; }
4988 | DEFERRABLE { $$ = "deferrable"; }
4989 | DEFERRED { $$ = "deferred"; }
4990 | DELIMITERS { $$ = "delimiters"; }
4991 | DOUBLE { $$ = "double"; }
4992 | EACH { $$ = "each"; }
4993 | ENCODING { $$ = "encoding"; }
4994 | EXCLUSIVE { $$ = "exclusive"; }
4995 | FORWARD { $$ = "forward"; }
4996 | FUNCTION { $$ = "function"; }
4997 | HANDLER { $$ = "handler"; }
4998 | IMMEDIATE { $$ = "immediate"; }
4999 | INCREMENT { $$ = "increment"; }
5000 | INDEX { $$ = "index"; }
5001 | INHERITS { $$ = "inherits"; }
5002 | INITIALLY { $$ = "initially"; }
5003 | INSENSITIVE { $$ = "insensitive"; }
5004 | INSTEAD { $$ = "instead"; }
5005 | ISNULL { $$ = "isnull"; }
5006 | ISOLATION { $$ = "isolation"; }
5007 | KEY { $$ = "key"; }
5008 | LANGUAGE { $$ = "language"; }
5009 | LANCOMPILER { $$ = "lancompiler"; }
5010 | LEVEL { $$ = "level"; }
5011 | LOCATION { $$ = "location"; }
5012 | MATCH { $$ = "match"; }
5013 | MAXVALUE { $$ = "maxvalue"; }
5014 | MINVALUE { $$ = "minvalue"; }
5015 | MODE { $$ = "mode"; }
5016 | NEXT { $$ = "next"; }
5017 | NOCREATEDB { $$ = "nocreatedb"; }
5018 | NOCREATEUSER { $$ = "nocreateuser"; }
5019 | NOTHING { $$ = "nothing"; }
5020 | NOTNULL { $$ = "notnull"; }
5022 | OIDS { $$ = "oids"; }
5023 | ONLY { $$ = "only"; }
5024 | OPERATOR { $$ = "operator"; }
5025 | OPTION { $$ = "option"; }
5026 | PASSWORD { $$ = "password"; }
5027 | PENDANT { $$ = "pendant"; }
5028 | PRIOR { $$ = "prior"; }
5029 | PRIVILEGES { $$ = "privileges"; }
5030 | PROCEDURAL { $$ = "procedural"; }
5031 | READ { $$ = "read"; }
5032 | RELATIVE { $$ = "relative"; }
5033 | RENAME { $$ = "rename"; }
5034 | RESTRICT { $$ = "restrict"; }
5035 | RETURNS { $$ = "returns"; }
5036 | ROW { $$ = "row"; }
5037 | RULE { $$ = "rule"; }
5038 | SCROLL { $$ = "scroll"; }
5039 | SEQUENCE { $$ = "sequence"; }
5040 | SERIAL { $$ = "serial"; }
5041 | SERIALIZABLE { $$ = "serializable"; }
5042 | SHARE { $$ = "share"; }
5043 | START { $$ = "start"; }
5044 | STATEMENT { $$ = "statement"; }
5045 | STDIN { $$ = "stdin"; }
5046 | STDOUT { $$ = "stdout"; }
5047 | TIME { $$ = "time"; }
5048 | TIMESTAMP { $$ = "timestamp"; }
5049 | TIMEZONE_HOUR { $$ = "timezone_hour"; }
5050 | TIMEZONE_MINUTE { $$ = "timezone_minute"; }
5051 | TRIGGER { $$ = "trigger"; }
5052 | TRUSTED { $$ = "trusted"; }
5053 | TYPE_P { $$ = "type"; }
5054 | VALID { $$ = "valid"; }
5055 | VERSION { $$ = "version"; }
5056 | ZONE { $$ = "zone"; }
5060 * Allowed labels in "AS" clauses.
5061 * Include TRUE/FALSE SQL3 reserved words for Postgres backward
5062 * compatibility. Cannot allow this for column names since the
5063 * syntax would not distinguish between the constant value and
5064 * a column name. - thomas 1997-10-24
5065 * Add other keywords to this list. Note that they appear here
5066 * rather than in ColId if there was a shift/reduce conflict
5067 * when used as a full identifier. - thomas 1997-11-06
5069 ColLabel: ColId { $$ = $1; }
5070 | ABORT_TRANS { $$ = "abort"; }
5071 | ANALYZE { $$ = "analyze"; }
5072 | BINARY { $$ = "binary"; }
5073 | CASE { $$ = "case"; }
5074 | CLUSTER { $$ = "cluster"; }
5075 | COALESCE { $$ = "coalesce"; }
5076 | CONSTRAINT { $$ = "constraint"; }
5077 | COPY { $$ = "copy"; }
5078 | CURRENT { $$ = "current"; }
5080 | ELSE { $$ = "else"; }
5081 | END_TRANS { $$ = "end"; }
5082 | EXPLAIN { $$ = "explain"; }
5083 | EXTEND { $$ = "extend"; }
5084 | FALSE_P { $$ = "false"; }
5085 | FOREIGN { $$ = "foreign"; }
5086 | GLOBAL { $$ = "global"; }
5087 | GROUP { $$ = "group"; }
5088 | LISTEN { $$ = "listen"; }
5089 | LOAD { $$ = "load"; }
5090 | LOCAL { $$ = "local"; }
5091 | LOCK_P { $$ = "lock"; }
5092 | MOVE { $$ = "move"; }
5093 | NEW { $$ = "new"; }
5094 | NONE { $$ = "none"; }
5095 | NULLIF { $$ = "nullif"; }
5096 | ORDER { $$ = "order"; }
5097 | POSITION { $$ = "position"; }
5098 | PRECISION { $$ = "precision"; }
5099 | RESET { $$ = "reset"; }
5100 | SETOF { $$ = "setof"; }
5101 | SHOW { $$ = "show"; }
5102 | TABLE { $$ = "table"; }
5103 | THEN { $$ = "then"; }
5104 | TRANSACTION { $$ = "transaction"; }
5105 | TRUE_P { $$ = "true"; }
5106 | VACUUM { $$ = "vacuum"; }
5107 | VERBOSE { $$ = "verbose"; }
5108 | WHEN { $$ = "when"; }
5111 SpecialRuleRelation: CURRENT
5116 elog(ERROR,"CURRENT used in non-rule query");
5123 elog(ERROR,"NEW used in non-rule query");
5130 makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
5132 A_Expr *a = makeNode(A_Expr);
5141 * Generate separate operator nodes for a single row descriptor expression.
5142 * Perhaps this should go deeper in the parser someday...
5143 * - thomas 1997-12-22
5146 makeRowExpr(char *opr, List *largs, List *rargs)
5151 if (length(largs) != length(rargs))
5152 elog(ERROR,"Unequal number of entries in row expression");
5154 if (lnext(largs) != NIL)
5155 expr = makeRowExpr(opr,lnext(largs),lnext(rargs));
5157 larg = lfirst(largs);
5158 rarg = lfirst(rargs);
5160 if ((strcmp(opr, "=") == 0)
5161 || (strcmp(opr, "<") == 0)
5162 || (strcmp(opr, "<=") == 0)
5163 || (strcmp(opr, ">") == 0)
5164 || (strcmp(opr, ">=") == 0))
5167 expr = makeA_Expr(OP, opr, larg, rarg);
5169 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
5171 else if (strcmp(opr, "<>") == 0)
5174 expr = makeA_Expr(OP, opr, larg, rarg);
5176 expr = makeA_Expr(OR, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
5180 elog(ERROR,"Operator '%s' not implemented for row expressions",opr);
5187 mapTargetColumns(List *src, List *dst)
5192 if (length(src) != length(dst))
5193 elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
5195 while ((src != NIL) && (dst != NIL))
5197 s = (ColumnDef *)lfirst(src);
5198 d = (ResTarget *)lfirst(dst);
5200 d->name = s->colname;
5207 } /* mapTargetColumns() */
5211 * Convert alternate type names to internal Postgres types.
5212 * Do not convert "float", since that is handled elsewhere
5213 * for FLOAT(p) syntax.
5216 xlateSqlFunc(char *name)
5218 if (!strcasecmp(name,"character_length")
5219 || !strcasecmp(name,"char_length"))
5223 } /* xlateSqlFunc() */
5226 * Convert alternate type names to internal Postgres types.
5229 xlateSqlType(char *name)
5231 if (!strcasecmp(name,"int")
5232 || !strcasecmp(name,"integer"))
5234 else if (!strcasecmp(name, "smallint"))
5236 else if (!strcasecmp(name, "real")
5237 || !strcasecmp(name, "float"))
5239 else if (!strcasecmp(name, "interval"))
5241 else if (!strcasecmp(name, "boolean"))
5245 } /* xlateSqlType() */
5248 void parser_init(Oid *typev, int nargs)
5250 QueryIsRule = FALSE;
5251 saved_relname[0]= '\0';
5253 param_type_init(typev, nargs);
5258 * Check input string for non-lowercase/non-numeric characters.
5259 * Returns either input string or input surrounded by double quotes.
5266 for (cp = rawid; *cp != '\0'; cp++)
5267 if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break;
5270 cp = palloc(strlen(rawid)+3);
5284 * keep enough information around fill out the type of param nodes
5285 * used in postquel functions
5288 param_type_init(Oid *typev, int nargs)
5290 pfunc_num_args = nargs;
5291 param_type_info = typev;
5294 Oid param_type(int t)
5296 if ((t > pfunc_num_args) || (t == 0))
5298 return param_type_info[t - 1];
5302 * The optimizer doesn't like '-' 4 for index use. It only checks for
5303 * Var '=' Const. It wants an integer of -4, so we try to merge the
5304 * minus into the constant.
5306 static Node *doNegate(Node *n)
5308 if (IsA(n, A_Const))
5310 A_Const *con = (A_Const *)n;
5312 if (con->val.type == T_Integer)
5314 con->val.val.ival = -con->val.val.ival;
5317 if (con->val.type == T_Float)
5319 con->val.val.dval = -con->val.val.dval;
5324 return makeA_Expr(OP, "-", NULL, n);