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 1.83 1998/01/04 04:31:08 momjian 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 *-------------------------------------------------------------------------
39 #include "nodes/parsenodes.h"
40 #include "nodes/print.h"
41 #include "parser/gramparse.h"
42 #include "parser/parse_type.h"
43 #include "utils/acl.h"
44 #include "utils/palloc.h"
45 #include "catalog/catname.h"
46 #include "utils/elog.h"
47 #include "access/xact.h"
49 static char saved_relname[NAMEDATALEN]; /* need this for complex attributes */
50 static bool QueryIsRule = FALSE;
51 static Node *saved_In_Expr;
52 static Oid *param_type_info;
53 static int pfunc_num_args;
54 extern List *parsetree;
58 * If you need access to certain yacc-generated variables and find that
59 * they're static by default, uncomment the next line. (this is not a
62 /*#define __YYSCLASS*/
64 static char *xlateSqlType(char *);
65 static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
66 static Node *makeRowExpr(char *opr, List *largs, List *rargs);
67 void mapTargetColumns(List *source, List *target);
68 static List *makeConstantList( A_Const *node);
69 static char *FlattenStringList(List *list);
70 static char *fmtId(char *rawid);
71 static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr);
72 static void param_type_init(Oid *typev, int nargs);
74 Oid param_type(int t); /* used in parse_expr.c */
76 /* old versions of flex define this as a macro */
90 bool* pboolean; /* for pg_user privileges */
100 SortGroupBy *sortgroupby;
115 AddAttrStmt, ClosePortalStmt,
116 CopyStmt, CreateStmt, CreateAsStmt, CreateSeqStmt, DefineStmt, DestroyStmt,
117 ExtendStmt, FetchStmt, GrantStmt, CreateTrigStmt, DropTrigStmt,
118 CreatePLangStmt, DropPLangStmt,
119 IndexStmt, ListenStmt, OptimizableStmt,
120 ProcedureStmt, RecipeStmt, RemoveAggrStmt, RemoveOperStmt,
121 RemoveFuncStmt, RemoveStmt,
122 RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
123 CreatedbStmt, DestroydbStmt, VacuumStmt, CursorStmt, SubSelect,
124 ReplaceStmt, AppendStmt, RetrieveStmt, NotifyStmt, DeleteStmt, ClusterStmt,
125 ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
126 CreateUserStmt, AlterUserStmt, DropUserStmt
130 %type <str> opt_database, location
132 %type <pboolean> user_createdb_clause, user_createuser_clause
133 %type <str> user_passwd_clause
134 %type <str> user_valid_clause
135 %type <list> user_group_list, user_group_clause
137 %type <str> join_expr, join_outer, join_spec
138 %type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
140 %type <str> TriggerEvents, TriggerFuncArg
142 %type <str> relation_name, copy_file_name, copy_delimiter, def_name,
143 database_name, access_method_clause, access_method, attr_name,
144 class, index_name, name, file_name, recipe_name, aggr_argtype
146 %type <str> opt_id, opt_portal_name,
147 all_Op, MathOp, opt_name, opt_unique,
148 result, OptUseOp, opt_class, SpecialRuleRelation
150 %type <str> privileges, operation_commalist, grantee
151 %type <chr> operation, TriggerOneEvent
153 %type <list> stmtblock, stmtmulti,
154 relation_name_list, OptTableElementList,
155 OptInherit, definition,
156 opt_with, def_args, def_name_list, func_argtypes,
157 oper_argtypes, OptStmtList, OptStmtBlock, OptStmtMulti,
158 opt_column_list, columnList, opt_va_list, va_list,
159 sort_clause, sortby_list, index_params, index_list, name_list,
160 from_clause, from_list, opt_array_bounds, nest_array_bounds,
161 expr_list, attrs, res_target_list, res_target_list2,
162 def_list, opt_indirection, group_clause, groupby_list, TriggerFuncArgs
164 %type <list> union_clause, select_list
165 %type <list> join_list
168 %type <boolean> opt_union
170 %type <node> position_expr
171 %type <list> extract_list, position_list
172 %type <list> substr_list, substr_from, substr_for, trim_list
173 %type <list> opt_interval
175 %type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
176 index_opt_unique, opt_verbose, opt_analyze
178 %type <ival> copy_dirn, def_type, opt_direction, remove_type,
181 %type <ival> fetch_how_many
183 %type <list> OptSeqList
184 %type <defelt> OptSeqElem
186 %type <dstmt> def_rest
187 %type <astmt> insert_rest
189 %type <node> OptTableElement, ConstraintElem
190 %type <node> columnDef, alter_clause
191 %type <defelt> def_elem
192 %type <node> def_arg, columnElem, where_clause,
193 a_expr, a_expr_or_null, AexprConst,
194 in_expr, in_expr_nodes, not_in_expr, not_in_expr_nodes,
196 %type <list> row_descriptor, row_list
197 %type <node> row_expr
198 %type <list> OptCreateAs, CreateAsList
199 %type <node> CreateAsElement
200 %type <value> NumConst
201 %type <attr> event_object, attr
202 %type <sortgroupby> groupby
203 %type <sortgroupby> sortby
204 %type <ielem> index_elem, func_index
205 %type <range> from_val
206 %type <relexp> relation_expr
207 %type <target> res_target_el, res_target_el2
208 %type <paramno> ParamNo
210 %type <typnam> Typename, opt_type, Array, Generic, Character, Datetime, Numeric
211 %type <str> generic, character, datetime
212 %type <str> opt_charset, opt_collate
213 %type <str> opt_float, opt_numeric, opt_decimal
214 %type <boolean> opt_varying, opt_timezone
218 %type <str> Id, var_value, zone_value
219 %type <str> ColId, ColLabel
221 %type <node> TableConstraint
222 %type <list> constraint_list, constraint_expr
223 %type <list> default_list, default_expr
224 %type <list> ColQualList, ColQualifier
225 %type <node> ColConstraint, ColConstraintElem
226 %type <list> key_actions, key_action
227 %type <str> key_match, key_reference
230 * If you make any token changes, remember to:
231 * - use "yacc -d" and update parse.h
232 * - update the keyword table in parser/keywords.c
235 /* Reserved word tokens
236 * SQL92 syntax has many type-specific constructs.
237 * So, go ahead and make these types reserved words,
238 * and call-out the syntax explicitly.
239 * This gets annoying when trying to also retain Postgres' nice
240 * type-extensible features, but we don't really have a choice.
241 * - thomas 1997-10-11
244 /* Keywords (in SQL92 reserved words) */
245 %token ACTION, ADD, ALL, ALTER, AND, AS, ASC,
246 BEGIN_TRANS, BETWEEN, BOTH, BY,
247 CASCADE, CAST, CHAR, CHARACTER, CHECK, CLOSE, COLLATE, COLUMN, COMMIT,
248 CONSTRAINT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME,
249 CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
250 DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
251 END_TRANS, EXECUTE, EXISTS, EXTRACT,
252 FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
253 GRANT, GROUP, HAVING, HOUR_P,
254 IN, INNER_P, INSERT, INTERVAL, INTO, IS,
255 JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
256 MATCH, MINUTE_P, MONTH_P,
257 NATIONAL, NATURAL, NCHAR, NO, NOT, NOTIFY, NULL_P, NUMERIC,
258 ON, OPTION, OR, ORDER, OUTER_P,
259 PARTIAL, POSITION, PRECISION, PRIMARY, PRIVILEGES, PROCEDURE, PUBLIC,
260 REFERENCES, REVOKE, RIGHT, ROLLBACK,
261 SECOND_P, SELECT, SET, SUBSTRING,
262 TABLE, TIME, TIMESTAMP, TO, TRAILING, TRANSACTION, TRIM,
263 UNION, UNIQUE, UPDATE, USING,
264 VALUES, VARCHAR, VARYING, VERBOSE, VERSION, VIEW,
265 WHERE, WITH, WORK, YEAR_P, ZONE
267 /* Keywords (in SQL3 reserved words) */
268 %token FALSE_P, TRIGGER, TRUE_P
270 /* Keywords (in SQL92 non-reserved words) */
273 /* Keywords for Postgres support (not in SQL92 reserved words) */
274 %token ABORT_TRANS, ACL, AFTER, AGGREGATE, ANALYZE,
275 APPEND, BACKWARD, BEFORE, BINARY, CHANGE, CLUSTER, COPY,
276 DATABASE, DELIMITERS, DO, EXPLAIN, EXTEND,
277 FORWARD, FUNCTION, HANDLER,
278 INDEX, INHERITS, INSTEAD, ISNULL,
279 LANCOMPILER, LISTEN, LOAD, LOCATION, MERGE, MOVE,
280 NEW, NONE, NOTHING, NOTNULL, OIDS, OPERATOR, PROCEDURAL,
281 RECIPE, RENAME, REPLACE, RESET, RETRIEVE, RETURNS, RULE,
282 SEQUENCE, SETOF, SHOW, STDIN, STDOUT, TRUSTED,
283 VACUUM, VERBOSE, VERSION
285 /* Keywords (obsolete; retain temporarily for parser - thomas 1997-12-04) */
289 * Tokens for pg_passwd support. The CREATEDB and CREATEUSER tokens should go away
290 * when some sort of pg_privileges relation is introduced.
294 %token USER, PASSWORD, CREATEDB, NOCREATEDB, CREATEUSER, NOCREATEUSER, VALID, UNTIL
296 /* Special keywords, not in the query language - see the "lex" file */
297 %token <str> IDENT, SCONST, Op
298 %token <ival> ICONST, PARAM
301 /* these are not real. they are here so that they get generated as #define's*/
313 %nonassoc Op /* multi-character ops and user-defined operators */
319 %left '|' /* this is the relation union op, not logical or */
320 /* Unary Operators */
322 %left ';' /* end of statement or natural log */
334 { parsetree = lcons($1,NIL); }
337 stmtmulti: stmtmulti stmt ';'
338 { $$ = lappend($1, $2); }
340 { $$ = lappend($1, $2); }
342 { $$ = lcons($1,NIL); }
388 /*****************************************************************************
390 * Create a new Postgres DBMS user
393 *****************************************************************************/
395 CreateUserStmt: CREATE USER Id user_passwd_clause user_createdb_clause
396 user_createuser_clause user_group_clause user_valid_clause
398 CreateUserStmt *n = makeNode(CreateUserStmt);
409 /*****************************************************************************
411 * Alter a postresql DBMS user
414 *****************************************************************************/
416 AlterUserStmt: ALTER USER Id user_passwd_clause user_createdb_clause
417 user_createuser_clause user_group_clause user_valid_clause
419 AlterUserStmt *n = makeNode(AlterUserStmt);
430 /*****************************************************************************
432 * Drop a postresql DBMS user
435 *****************************************************************************/
437 DropUserStmt: DROP USER Id
439 DropUserStmt *n = makeNode(DropUserStmt);
445 user_passwd_clause: WITH PASSWORD Id { $$ = $3; }
446 | /*EMPTY*/ { $$ = NULL; }
449 user_createdb_clause: CREATEDB
452 $$ = (b = (bool*)palloc(sizeof(bool)));
458 $$ = (b = (bool*)palloc(sizeof(bool)));
461 | /*EMPTY*/ { $$ = NULL; }
464 user_createuser_clause: CREATEUSER
467 $$ = (b = (bool*)palloc(sizeof(bool)));
473 $$ = (b = (bool*)palloc(sizeof(bool)));
476 | /*EMPTY*/ { $$ = NULL; }
479 user_group_list: user_group_list ',' Id
481 $$ = lcons((void*)makeString($3), $1);
485 $$ = lcons((void*)makeString($1), NIL);
489 user_group_clause: IN GROUP user_group_list { $$ = $3; }
490 | /*EMPTY*/ { $$ = NULL; }
493 user_valid_clause: VALID UNTIL SCONST { $$ = $3; }
494 | /*EMPTY*/ { $$ = NULL; }
497 /*****************************************************************************
499 * Set PG internal variable
500 * SET name TO 'var_value'
501 * Include SQL92 syntax (thomas 1997-10-22):
502 * SET TIME ZONE 'var_value'
504 *****************************************************************************/
506 VariableSetStmt: SET ColId TO var_value
508 VariableSetStmt *n = makeNode(VariableSetStmt);
513 | SET ColId '=' var_value
515 VariableSetStmt *n = makeNode(VariableSetStmt);
520 | SET TIME ZONE zone_value
522 VariableSetStmt *n = makeNode(VariableSetStmt);
523 n->name = "timezone";
529 var_value: Sconst { $$ = $1; }
530 | DEFAULT { $$ = NULL; }
533 zone_value: Sconst { $$ = $1; }
534 | DEFAULT { $$ = NULL; }
535 | LOCAL { $$ = "default"; }
538 VariableShowStmt: SHOW ColId
540 VariableShowStmt *n = makeNode(VariableShowStmt);
546 VariableShowStmt *n = makeNode(VariableShowStmt);
547 n->name = "timezone";
552 VariableResetStmt: RESET ColId
554 VariableResetStmt *n = makeNode(VariableResetStmt);
560 VariableResetStmt *n = makeNode(VariableResetStmt);
561 n->name = "timezone";
567 /*****************************************************************************
570 * addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
572 *****************************************************************************/
574 AddAttrStmt: ALTER TABLE relation_name opt_inh_star alter_clause
576 AddAttrStmt *n = makeNode(AddAttrStmt);
584 alter_clause: ADD opt_column columnDef
588 | ADD '(' OptTableElementList ')'
590 Node *lp = lfirst($3);
593 elog(WARN,"ALTER TABLE/ADD() allows one column only",NULL);
596 | DROP opt_column ColId
597 { elog(WARN,"ALTER TABLE/DROP COLUMN not yet implemented",NULL); }
598 | ALTER opt_column ColId SET DEFAULT default_expr
599 { elog(WARN,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented",NULL); }
600 | ALTER opt_column ColId DROP DEFAULT
601 { elog(WARN,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented",NULL); }
603 { elog(WARN,"ALTER TABLE/ADD CONSTRAINT not yet implemented",NULL); }
607 /*****************************************************************************
612 *****************************************************************************/
614 ClosePortalStmt: CLOSE opt_id
616 ClosePortalStmt *n = makeNode(ClosePortalStmt);
623 /*****************************************************************************
626 * COPY [BINARY] <relname> FROM/TO
627 * [USING DELIMITERS <delimiter>]
629 *****************************************************************************/
631 CopyStmt: COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
633 CopyStmt *n = makeNode(CopyStmt);
651 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
652 * used depends on the direction. (It really doesn't make sense to copy from
653 * stdout. We silently correct the "typo". - AY 9/94
655 copy_file_name: Sconst { $$ = $1; }
656 | STDIN { $$ = NULL; }
657 | STDOUT { $$ = NULL; }
660 opt_binary: BINARY { $$ = TRUE; }
661 | /*EMPTY*/ { $$ = FALSE; }
664 opt_with_copy: WITH OIDS { $$ = TRUE; }
665 | /*EMPTY*/ { $$ = FALSE; }
669 * the default copy delimiter is tab but the user can configure it
671 copy_delimiter: USING DELIMITERS Sconst { $$ = $3; }
672 | /*EMPTY*/ { $$ = "\t"; }
676 /*****************************************************************************
681 *****************************************************************************/
683 CreateStmt: CREATE TABLE relation_name '(' OptTableElementList ')'
684 OptInherit OptArchiveType
686 CreateStmt *n = makeNode(CreateStmt);
690 n->constraints = NIL;
695 OptTableElementList: OptTableElementList ',' OptTableElement
696 { $$ = lappend($1, $3); }
697 | OptTableElement { $$ = lcons($1, NIL); }
698 | /*EMPTY*/ { $$ = NULL; }
701 OptTableElement: columnDef { $$ = $1; }
702 | TableConstraint { $$ = $1; }
705 columnDef: ColId Typename ColQualifier
707 ColumnDef *n = makeNode(ColumnDef);
711 n->is_not_null = FALSE;
717 ColQualifier: ColQualList { $$ = $1; }
718 | /*EMPTY*/ { $$ = NULL; }
721 ColQualList: ColQualList ColConstraint { $$ = lappend($1,$2); }
722 | ColConstraint { $$ = lcons($1, NIL); }
726 CONSTRAINT name ColConstraintElem
728 Constraint *n = (Constraint *)$3;
736 ColConstraintElem: CHECK '(' constraint_expr ')'
738 Constraint *n = makeNode(Constraint);
739 n->contype = CONSTR_CHECK;
741 n->def = FlattenStringList($3);
745 | DEFAULT default_expr
747 Constraint *n = makeNode(Constraint);
748 n->contype = CONSTR_DEFAULT;
750 n->def = FlattenStringList($2);
756 Constraint *n = makeNode(Constraint);
757 n->contype = CONSTR_NOTNULL;
765 Constraint *n = makeNode(Constraint);
766 n->contype = CONSTR_UNIQUE;
774 Constraint *n = makeNode(Constraint);
775 n->contype = CONSTR_PRIMARY;
781 | REFERENCES ColId opt_column_list key_match key_actions
783 elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented",NULL);
788 default_list: default_list ',' default_expr
790 $$ = lappend($1,makeString(","));
799 default_expr: AexprConst
800 { $$ = makeConstantList((A_Const *) $1); }
802 { $$ = lcons( makeString("NULL"), NIL); }
803 | '-' default_expr %prec UMINUS
804 { $$ = lcons( makeString( "-"), $2); }
805 | default_expr '+' default_expr
806 { $$ = nconc( $1, lcons( makeString( "+"), $3)); }
807 | default_expr '-' default_expr
808 { $$ = nconc( $1, lcons( makeString( "-"), $3)); }
809 | default_expr '/' default_expr
810 { $$ = nconc( $1, lcons( makeString( "/"), $3)); }
811 | default_expr '*' default_expr
812 { $$ = nconc( $1, lcons( makeString( "*"), $3)); }
813 | default_expr '=' default_expr
814 { elog(WARN,"boolean expressions not supported in DEFAULT",NULL); }
815 | default_expr '<' default_expr
816 { elog(WARN,"boolean expressions not supported in DEFAULT",NULL); }
817 | default_expr '>' default_expr
818 { elog(WARN,"boolean expressions not supported in DEFAULT",NULL); }
820 { $$ = lcons( makeString( ":"), $2); }
822 { $$ = lcons( makeString( ";"), $2); }
824 { $$ = lcons( makeString( "|"), $2); }
825 | default_expr TYPECAST Typename
827 $3->name = fmtId($3->name);
828 $$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
830 | CAST default_expr AS Typename
832 $4->name = fmtId($4->name);
833 $$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
835 | '(' default_expr ')'
836 { $$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
839 $$ = makeList( makeString($1), makeString("("), -1);
840 $$ = lappend( $$, makeString(")"));
842 | name '(' default_list ')'
844 $$ = makeList( makeString($1), makeString("("), -1);
846 $$ = lappend( $$, makeString(")"));
848 | default_expr Op default_expr
850 if (!strcmp("<=", $2) || !strcmp(">=", $2))
851 elog(WARN,"boolean expressions not supported in DEFAULT",NULL);
852 $$ = nconc( $1, lcons( makeString( $2), $3));
855 { $$ = lcons( makeString( $1), $2); }
857 { $$ = lappend( $1, makeString( $2)); }
858 /* XXX - thomas 1997-10-07 v6.2 function-specific code to be changed */
860 { $$ = lcons( makeString( "date( 'current'::datetime + '0 sec')"), NIL); }
862 { $$ = lcons( makeString( "'now'::time"), NIL); }
863 | CURRENT_TIME '(' Iconst ')'
866 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
867 $$ = lcons( makeString( "'now'::time"), NIL);
870 { $$ = lcons( makeString( "now()"), NIL); }
871 | CURRENT_TIMESTAMP '(' Iconst ')'
874 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
875 $$ = lcons( makeString( "now()"), NIL);
878 { $$ = lcons( makeString( "CURRENT_USER"), NIL); }
881 /* ConstraintElem specifies constraint syntax which is not embedded into
882 * a column definition. ColConstraintElem specifies the embedded form.
883 * - thomas 1997-12-03
885 TableConstraint: CONSTRAINT name ConstraintElem
887 Constraint *n = (Constraint *)$3;
895 ConstraintElem: CHECK '(' constraint_expr ')'
897 Constraint *n = makeNode(Constraint);
898 n->contype = CONSTR_CHECK;
900 n->def = FlattenStringList($3);
903 | UNIQUE '(' columnList ')'
905 Constraint *n = makeNode(Constraint);
906 n->contype = CONSTR_UNIQUE;
912 | PRIMARY KEY '(' columnList ')'
914 Constraint *n = makeNode(Constraint);
915 n->contype = CONSTR_PRIMARY;
921 | FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
922 { elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented",NULL); }
925 constraint_list: constraint_list ',' constraint_expr
927 $$ = lappend($1,makeString(","));
936 constraint_expr: AexprConst
937 { $$ = makeConstantList((A_Const *) $1); }
939 { $$ = lcons( makeString("NULL"), NIL); }
942 $$ = lcons( makeString(fmtId($1)), NIL);
944 | '-' constraint_expr %prec UMINUS
945 { $$ = lcons( makeString( "-"), $2); }
946 | constraint_expr '+' constraint_expr
947 { $$ = nconc( $1, lcons( makeString( "+"), $3)); }
948 | constraint_expr '-' constraint_expr
949 { $$ = nconc( $1, lcons( makeString( "-"), $3)); }
950 | constraint_expr '/' constraint_expr
951 { $$ = nconc( $1, lcons( makeString( "/"), $3)); }
952 | constraint_expr '*' constraint_expr
953 { $$ = nconc( $1, lcons( makeString( "*"), $3)); }
954 | constraint_expr '=' constraint_expr
955 { $$ = nconc( $1, lcons( makeString( "="), $3)); }
956 | constraint_expr '<' constraint_expr
957 { $$ = nconc( $1, lcons( makeString( "<"), $3)); }
958 | constraint_expr '>' constraint_expr
959 { $$ = nconc( $1, lcons( makeString( ">"), $3)); }
960 | ':' constraint_expr
961 { $$ = lcons( makeString( ":"), $2); }
962 | ';' constraint_expr
963 { $$ = lcons( makeString( ";"), $2); }
964 | '|' constraint_expr
965 { $$ = lcons( makeString( "|"), $2); }
966 | constraint_expr TYPECAST Typename
968 $3->name = fmtId($3->name);
969 $$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
971 | CAST constraint_expr AS Typename
973 $4->name = fmtId($4->name);
974 $$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
976 | '(' constraint_expr ')'
977 { $$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
980 $$ = makeList( makeString($1), makeString("("), -1);
981 $$ = lappend( $$, makeString(")"));
983 | name '(' constraint_list ')'
985 $$ = makeList( makeString($1), makeString("("), -1);
987 $$ = lappend( $$, makeString(")"));
989 | constraint_expr Op constraint_expr
990 { $$ = nconc( $1, lcons( makeString( $2), $3)); }
991 | constraint_expr AND constraint_expr
992 { $$ = nconc( $1, lcons( makeString( "AND"), $3)); }
993 | constraint_expr OR constraint_expr
994 { $$ = nconc( $1, lcons( makeString( "OR"), $3)); }
995 | NOT constraint_expr
996 { $$ = lcons( makeString( "NOT"), $2); }
998 { $$ = lcons( makeString( $1), $2); }
1000 { $$ = lappend( $1, makeString( $2)); }
1001 | constraint_expr ISNULL
1002 { $$ = lappend( $1, makeString( "IS NULL")); }
1003 | constraint_expr IS NULL_P
1004 { $$ = lappend( $1, makeString( "IS NULL")); }
1005 | constraint_expr NOTNULL
1006 { $$ = lappend( $1, makeString( "IS NOT NULL")); }
1007 | constraint_expr IS NOT NULL_P
1008 { $$ = lappend( $1, makeString( "IS NOT NULL")); }
1009 | constraint_expr IS TRUE_P
1010 { $$ = lappend( $1, makeString( "IS TRUE")); }
1011 | constraint_expr IS FALSE_P
1012 { $$ = lappend( $1, makeString( "IS FALSE")); }
1013 | constraint_expr IS NOT TRUE_P
1014 { $$ = lappend( $1, makeString( "IS NOT TRUE")); }
1015 | constraint_expr IS NOT FALSE_P
1016 { $$ = lappend( $1, makeString( "IS NOT FALSE")); }
1019 key_match: MATCH FULL { $$ = NULL; }
1020 | MATCH PARTIAL { $$ = NULL; }
1021 | /*EMPTY*/ { $$ = NULL; }
1024 key_actions: key_action key_action { $$ = NIL; }
1025 | key_action { $$ = NIL; }
1026 | /*EMPTY*/ { $$ = NIL; }
1029 key_action: ON DELETE key_reference { $$ = NIL; }
1030 | ON UPDATE key_reference { $$ = NIL; }
1033 key_reference: NO ACTION { $$ = NULL; }
1034 | CASCADE { $$ = NULL; }
1035 | SET DEFAULT { $$ = NULL; }
1036 | SET NULL_P { $$ = NULL; }
1039 OptInherit: INHERITS '(' relation_name_list ')' { $$ = $3; }
1040 | /*EMPTY*/ { $$ = NIL; }
1044 * "ARCHIVE" keyword was removed in 6.3, but we keep it for now
1045 * so people can upgrade with old pg_dump scripts. - momjian 1997-11-20(?)
1047 OptArchiveType: ARCHIVE '=' NONE { }
1051 CreateAsStmt: CREATE TABLE relation_name OptCreateAs AS SubSelect
1053 RetrieveStmt *n = (RetrieveStmt *)$6;
1055 mapTargetColumns($4, n->targetList);
1061 OptCreateAs: '(' CreateAsList ')' { $$ = $2; }
1062 | /*EMPTY*/ { $$ = NULL; }
1065 CreateAsList: CreateAsList ',' CreateAsElement { $$ = lappend($1, $3); }
1066 | CreateAsElement { $$ = lcons($1, NIL); }
1069 CreateAsElement: ColId
1071 ColumnDef *n = makeNode(ColumnDef);
1075 n->is_not_null = FALSE;
1076 n->constraints = NULL;
1082 /*****************************************************************************
1085 * CREATE SEQUENCE seqname
1087 *****************************************************************************/
1089 CreateSeqStmt: CREATE SEQUENCE relation_name OptSeqList
1091 CreateSeqStmt *n = makeNode(CreateSeqStmt);
1099 OptSeqList OptSeqElem
1100 { $$ = lappend($1, $2); }
1104 OptSeqElem: IDENT NumConst
1106 $$ = makeNode(DefElem);
1108 $$->arg = (Node *)$2;
1112 $$ = makeNode(DefElem);
1114 $$->arg = (Node *)NULL;
1118 /*****************************************************************************
1121 * CREATE PROCEDURAL LANGUAGE ...
1122 * DROP PROCEDURAL LANGUAGE ...
1124 *****************************************************************************/
1126 CreatePLangStmt: CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst
1127 HANDLER def_name LANCOMPILER Sconst
1129 CreatePLangStmt *n = makeNode(CreatePLangStmt);
1138 PLangTrusted: TRUSTED { $$ = TRUE; }
1141 DropPLangStmt: DROP PROCEDURAL LANGUAGE Sconst
1143 DropPLangStmt *n = makeNode(DropPLangStmt);
1149 /*****************************************************************************
1152 * CREATE TRIGGER ...
1155 *****************************************************************************/
1157 CreateTrigStmt: CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1158 relation_name TriggerForSpec EXECUTE PROCEDURE
1159 name '(' TriggerFuncArgs ')'
1161 CreateTrigStmt *n = makeNode(CreateTrigStmt);
1168 memcpy (n->actions, $5, 4);
1173 TriggerActionTime: BEFORE { $$ = TRUE; }
1174 | AFTER { $$ = FALSE; }
1177 TriggerEvents: TriggerOneEvent
1179 char *e = palloc (4);
1180 e[0] = $1; e[1] = 0; $$ = e;
1182 | TriggerOneEvent OR TriggerOneEvent
1184 char *e = palloc (4);
1185 e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
1187 | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
1189 char *e = palloc (4);
1190 e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
1195 TriggerOneEvent: INSERT { $$ = 'i'; }
1196 | DELETE { $$ = 'd'; }
1197 | UPDATE { $$ = 'u'; }
1200 TriggerForSpec: FOR name name
1202 if ( strcmp ($2, "each") != 0 )
1203 elog(WARN,"parser: syntax error near %s",$2);
1204 if ( strcmp ($3, "row") == 0 )
1206 else if ( strcmp ($3, "statement") == 0 )
1209 elog(WARN,"parser: syntax error near %s",$3);
1213 TriggerFuncArgs: TriggerFuncArg
1214 { $$ = lcons($1, NIL); }
1215 | TriggerFuncArgs ',' TriggerFuncArg
1216 { $$ = lappend($1, $3); }
1221 TriggerFuncArg: ICONST
1223 char *s = (char *) palloc (256);
1224 sprintf (s, "%d", $1);
1229 char *s = (char *) palloc (256);
1230 sprintf (s, "%g", $1);
1233 | Sconst { $$ = $1; }
1234 | IDENT { $$ = $1; }
1237 DropTrigStmt: DROP TRIGGER name ON relation_name
1239 DropTrigStmt *n = makeNode(DropTrigStmt);
1247 /*****************************************************************************
1250 * define (type,operator,aggregate)
1252 *****************************************************************************/
1254 DefineStmt: CREATE def_type def_rest
1261 def_rest: def_name definition
1263 $$ = makeNode(DefineStmt);
1265 $$->definition = $2;
1269 def_type: OPERATOR { $$ = OPERATOR; }
1270 | TYPE_P { $$ = TYPE_P; }
1271 | AGGREGATE { $$ = AGGREGATE; }
1274 def_name: PROCEDURE { $$ = "procedure"; }
1275 | JOIN { $$ = "join"; }
1276 | ColId { $$ = $1; }
1277 | MathOp { $$ = $1; }
1281 definition: '(' def_list ')' { $$ = $2; }
1284 def_list: def_elem { $$ = lcons($1, NIL); }
1285 | def_list ',' def_elem { $$ = lappend($1, $3); }
1288 def_elem: def_name '=' def_arg
1290 $$ = makeNode(DefElem);
1292 $$->arg = (Node *)$3;
1296 $$ = makeNode(DefElem);
1298 $$->arg = (Node *)NULL;
1300 | DEFAULT '=' def_arg
1302 $$ = makeNode(DefElem);
1303 $$->defname = "default";
1304 $$->arg = (Node *)$3;
1308 def_arg: ColId { $$ = (Node *)makeString($1); }
1309 | all_Op { $$ = (Node *)makeString($1); }
1310 | NumConst { $$ = (Node *)$1; /* already a Value */ }
1311 | Sconst { $$ = (Node *)makeString($1); }
1314 TypeName *n = makeNode(TypeName);
1317 n->arrayBounds = NULL;
1320 | DOUBLE { $$ = (Node *)makeString("double"); }
1324 /*****************************************************************************
1327 * destroy <relname1> [, <relname2> .. <relnameN> ]
1329 *****************************************************************************/
1331 DestroyStmt: DROP TABLE relation_name_list
1333 DestroyStmt *n = makeNode(DestroyStmt);
1335 n->sequence = FALSE;
1338 | DROP SEQUENCE relation_name_list
1340 DestroyStmt *n = makeNode(DestroyStmt);
1348 /*****************************************************************************
1351 * fetch/move [forward | backward] [number | all ] [ in <portalname> ]
1353 *****************************************************************************/
1355 FetchStmt: FETCH opt_direction fetch_how_many opt_portal_name
1357 FetchStmt *n = makeNode(FetchStmt);
1364 | MOVE opt_direction fetch_how_many opt_portal_name
1366 FetchStmt *n = makeNode(FetchStmt);
1375 opt_direction: FORWARD { $$ = FORWARD; }
1376 | BACKWARD { $$ = BACKWARD; }
1377 | /*EMPTY*/ { $$ = FORWARD; /* default */ }
1380 fetch_how_many: Iconst
1382 if ($1 <= 0) elog(WARN,"Please specify nonnegative count for fetch",NULL); }
1383 | ALL { $$ = 0; /* 0 means fetch all tuples*/ }
1384 | /*EMPTY*/ { $$ = 1; /*default*/ }
1387 opt_portal_name: IN name { $$ = $2; }
1388 | /*EMPTY*/ { $$ = NULL; }
1392 /*****************************************************************************
1395 * GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1397 *****************************************************************************/
1399 GrantStmt: GRANT privileges ON relation_name_list TO grantee opt_with_grant
1401 $$ = (Node*)makeAclStmt($2,$4,$6,'+');
1407 privileges: ALL PRIVILEGES
1409 $$ = aclmakepriv("rwaR",0);
1413 $$ = aclmakepriv("rwaR",0);
1415 | operation_commalist
1421 operation_commalist: operation
1423 $$ = aclmakepriv("",$1);
1425 | operation_commalist ',' operation
1427 $$ = aclmakepriv($1,$3);
1434 $$ = ACL_MODE_RD_CHR;
1438 $$ = ACL_MODE_AP_CHR;
1442 $$ = ACL_MODE_WR_CHR;
1446 $$ = ACL_MODE_WR_CHR;
1450 $$ = ACL_MODE_RU_CHR;
1456 $$ = aclmakeuser("A","");
1460 $$ = aclmakeuser("G",$2);
1464 $$ = aclmakeuser("U",$1);
1468 opt_with_grant: WITH GRANT OPTION
1470 yyerror("WITH GRANT OPTION is not supported. Only relation owners can set privileges");
1476 /*****************************************************************************
1479 * REVOKE [privileges] ON [relation_name] FROM [user]
1481 *****************************************************************************/
1483 RevokeStmt: REVOKE privileges ON relation_name_list FROM grantee
1485 $$ = (Node*)makeAclStmt($2,$4,$6,'-');
1492 /*****************************************************************************
1495 * create index <indexname> on <relname>
1496 * using <access> "(" (<col> with <op>)+ ")" [with
1499 * [where <qual>] is not supported anymore
1500 *****************************************************************************/
1502 IndexStmt: CREATE index_opt_unique INDEX index_name ON relation_name
1503 access_method_clause '(' index_params ')' opt_with
1505 /* should check that access_method is valid,
1506 etc ... but doesn't */
1507 IndexStmt *n = makeNode(IndexStmt);
1511 n->accessMethod = $7;
1512 n->indexParams = $9;
1513 n->withClause = $11;
1514 n->whereClause = NULL;
1519 index_opt_unique: UNIQUE { $$ = TRUE; }
1520 | /*EMPTY*/ { $$ = FALSE; }
1523 access_method_clause: USING access_method { $$ = $2; }
1524 | /*EMPTY*/ { $$ = "btree"; }
1527 index_params: index_list { $$ = $1; }
1528 | func_index { $$ = lcons($1,NIL); }
1531 index_list: index_list ',' index_elem { $$ = lappend($1, $3); }
1532 | index_elem { $$ = lcons($1, NIL); }
1535 func_index: name '(' name_list ')' opt_type opt_class
1537 $$ = makeNode(IndexElem);
1545 index_elem: attr_name opt_type opt_class
1547 $$ = makeNode(IndexElem);
1555 opt_type: ':' Typename { $$ = $2; }
1556 | FOR Typename { $$ = $2; }
1557 | /*EMPTY*/ { $$ = NULL; }
1560 /* opt_class "WITH class" conflicts with preceeding opt_type
1561 * for Typename of "TIMESTAMP WITH TIME ZONE"
1562 * So, remove "WITH class" from the syntax. OK??
1563 * - thomas 1997-10-12
1564 * | WITH class { $$ = $2; }
1566 opt_class: class { $$ = $1; }
1567 | USING class { $$ = $2; }
1568 | /*EMPTY*/ { $$ = NULL; }
1572 /*****************************************************************************
1575 * extend index <indexname> [where <qual>]
1577 *****************************************************************************/
1579 ExtendStmt: EXTEND INDEX index_name where_clause
1581 ExtendStmt *n = makeNode(ExtendStmt);
1583 n->whereClause = $4;
1589 /*****************************************************************************
1592 * execute recipe <recipeName>
1594 *****************************************************************************/
1596 RecipeStmt: EXECUTE RECIPE recipe_name
1599 if (!IsTransactionBlock())
1600 elog(WARN,"EXECUTE RECIPE may only be used in begin/end transaction blocks",NULL);
1602 n = makeNode(RecipeStmt);
1609 /*****************************************************************************
1612 * define function <fname>
1613 * (language = <lang>, returntype = <typename>
1614 * [, arch_pct = <percentage | pre-defined>]
1615 * [, disk_pct = <percentage | pre-defined>]
1616 * [, byte_pct = <percentage | pre-defined>]
1617 * [, perbyte_cpu = <int | pre-defined>]
1618 * [, percall_cpu = <int | pre-defined>]
1620 * [arg is (<type-1> { , <type-n>})]
1621 * as <filename or code in language as appropriate>
1623 *****************************************************************************/
1625 ProcedureStmt: CREATE FUNCTION def_name def_args
1626 RETURNS def_arg opt_with AS Sconst LANGUAGE Sconst
1628 ProcedureStmt *n = makeNode(ProcedureStmt);
1631 n->returnType = (Node *)$6;
1638 opt_with: WITH definition { $$ = $2; }
1639 | /*EMPTY*/ { $$ = NIL; }
1642 def_args: '(' def_name_list ')' { $$ = $2; }
1643 | '(' ')' { $$ = NIL; }
1646 def_name_list: name_list;
1648 /*****************************************************************************
1652 * remove function <funcname>
1653 * (REMOVE FUNCTION "funcname" (arg1, arg2, ...))
1654 * remove aggregate <aggname>
1655 * (REMOVE AGGREGATE "aggname" "aggtype")
1656 * remove operator <opname>
1657 * (REMOVE OPERATOR "opname" (leftoperand_typ rightoperand_typ))
1658 * remove type <typename>
1659 * (REMOVE TYPE "typename")
1660 * remove rule <rulename>
1661 * (REMOVE RULE "rulename")
1663 *****************************************************************************/
1665 RemoveStmt: DROP remove_type name
1667 RemoveStmt *n = makeNode(RemoveStmt);
1674 remove_type: TYPE_P { $$ = TYPE_P; }
1675 | INDEX { $$ = INDEX; }
1676 | RULE { $$ = RULE; }
1677 | VIEW { $$ = VIEW; }
1680 RemoveAggrStmt: DROP AGGREGATE name aggr_argtype
1682 RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
1689 aggr_argtype: name { $$ = $1; }
1690 | '*' { $$ = NULL; }
1693 RemoveFuncStmt: DROP FUNCTION name '(' func_argtypes ')'
1695 RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
1702 func_argtypes: name_list { $$ = $1; }
1703 | /*EMPTY*/ { $$ = NIL; }
1706 RemoveOperStmt: DROP OPERATOR all_Op '(' oper_argtypes ')'
1708 RemoveOperStmt *n = makeNode(RemoveOperStmt);
1715 all_Op: Op | MathOp;
1717 MathOp: '+' { $$ = "+"; }
1728 elog(WARN,"parser: argument type missing (use NONE for unary operators)",NULL);
1731 { $$ = makeList(makeString($1), makeString($3), -1); }
1732 | NONE ',' name /* left unary */
1733 { $$ = makeList(NULL, makeString($3), -1); }
1734 | name ',' NONE /* right unary */
1735 { $$ = makeList(makeString($1), NULL, -1); }
1739 /*****************************************************************************
1742 * rename <attrname1> in <relname> [*] to <attrname2>
1743 * rename <relname1> to <relname2>
1745 *****************************************************************************/
1747 RenameStmt: ALTER TABLE relation_name opt_inh_star
1748 RENAME opt_column opt_name TO name
1750 RenameStmt *n = makeNode(RenameStmt);
1759 opt_name: name { $$ = $1; }
1760 | /*EMPTY*/ { $$ = NULL; }
1763 opt_column: COLUMN { $$ = COLUMN; }
1764 | /*EMPTY*/ { $$ = 0; }
1768 /*****************************************************************************
1770 * QUERY: Define Rewrite Rule , Define Tuple Rule
1771 * Define Rule <old rules >
1773 * only rewrite rule is supported -- ay 9/94
1775 *****************************************************************************/
1777 RuleStmt: CREATE RULE name AS
1778 { QueryIsRule=TRUE; }
1779 ON event TO event_object where_clause
1780 DO opt_instead OptStmtList
1782 RuleStmt *n = makeNode(RuleStmt);
1786 n->whereClause = $10;
1793 OptStmtList: NOTHING { $$ = NIL; }
1794 | OptimizableStmt { $$ = lcons($1, NIL); }
1795 | '[' OptStmtBlock ']' { $$ = $2; }
1798 OptStmtBlock: OptStmtMulti
1801 { $$ = lcons($1, NIL); }
1804 OptStmtMulti: OptStmtMulti OptimizableStmt ';'
1805 { $$ = lappend($1, $2); }
1806 | OptStmtMulti OptimizableStmt
1807 { $$ = lappend($1, $2); }
1808 | OptimizableStmt ';'
1809 { $$ = lcons($1, NIL); }
1812 event_object: relation_name '.' attr_name
1814 $$ = makeNode(Attr);
1817 $$->attrs = lcons(makeString($3), NIL);
1818 $$->indirection = NIL;
1822 $$ = makeNode(Attr);
1826 $$->indirection = NIL;
1830 /* change me to select, update, etc. some day */
1831 event: SELECT { $$ = CMD_SELECT; }
1832 | UPDATE { $$ = CMD_UPDATE; }
1833 | DELETE { $$ = CMD_DELETE; }
1834 | INSERT { $$ = CMD_INSERT; }
1837 opt_instead: INSTEAD { $$ = TRUE; }
1838 | /*EMPTY*/ { $$ = FALSE; }
1842 /*****************************************************************************
1845 * NOTIFY <relation_name> can appear both in rule bodies and
1846 * as a query-level command
1848 *****************************************************************************/
1850 NotifyStmt: NOTIFY relation_name
1852 NotifyStmt *n = makeNode(NotifyStmt);
1858 ListenStmt: LISTEN relation_name
1860 ListenStmt *n = makeNode(ListenStmt);
1867 /*****************************************************************************
1878 *****************************************************************************/
1880 TransactionStmt: ABORT_TRANS TRANSACTION
1882 TransactionStmt *n = makeNode(TransactionStmt);
1883 n->command = ABORT_TRANS;
1886 | BEGIN_TRANS TRANSACTION
1888 TransactionStmt *n = makeNode(TransactionStmt);
1889 n->command = BEGIN_TRANS;
1894 TransactionStmt *n = makeNode(TransactionStmt);
1895 n->command = BEGIN_TRANS;
1900 TransactionStmt *n = makeNode(TransactionStmt);
1901 n->command = END_TRANS;
1904 | END_TRANS TRANSACTION
1906 TransactionStmt *n = makeNode(TransactionStmt);
1907 n->command = END_TRANS;
1912 TransactionStmt *n = makeNode(TransactionStmt);
1913 n->command = ABORT_TRANS;
1919 TransactionStmt *n = makeNode(TransactionStmt);
1920 n->command = ABORT_TRANS;
1925 TransactionStmt *n = makeNode(TransactionStmt);
1926 n->command = BEGIN_TRANS;
1931 TransactionStmt *n = makeNode(TransactionStmt);
1932 n->command = END_TRANS;
1938 TransactionStmt *n = makeNode(TransactionStmt);
1939 n->command = END_TRANS;
1944 TransactionStmt *n = makeNode(TransactionStmt);
1945 n->command = ABORT_TRANS;
1951 /*****************************************************************************
1954 * define view <viewname> '('target-list ')' [where <quals> ]
1956 *****************************************************************************/
1958 ViewStmt: CREATE VIEW name AS RetrieveStmt
1960 ViewStmt *n = makeNode(ViewStmt);
1962 n->query = (Query *)$5;
1968 /*****************************************************************************
1973 *****************************************************************************/
1975 LoadStmt: LOAD file_name
1977 LoadStmt *n = makeNode(LoadStmt);
1984 /*****************************************************************************
1989 *****************************************************************************/
1991 CreatedbStmt: CREATE DATABASE database_name opt_database
1993 CreatedbStmt *n = makeNode(CreatedbStmt);
2000 opt_database: WITH LOCATION '=' location { $$ = $4; }
2001 | /*EMPTY*/ { $$ = NULL; }
2004 location: Sconst { $$ = $1; }
2005 | DEFAULT { $$ = NULL; }
2006 | /*EMPTY*/ { $$ = NULL; }
2009 /*****************************************************************************
2014 *****************************************************************************/
2016 DestroydbStmt: DROP DATABASE database_name
2018 DestroydbStmt *n = makeNode(DestroydbStmt);
2025 /*****************************************************************************
2028 * cluster <index_name> on <relation_name>
2030 *****************************************************************************/
2032 ClusterStmt: CLUSTER index_name ON relation_name
2034 ClusterStmt *n = makeNode(ClusterStmt);
2042 /*****************************************************************************
2047 *****************************************************************************/
2049 VacuumStmt: VACUUM opt_verbose opt_analyze
2051 VacuumStmt *n = makeNode(VacuumStmt);
2058 | VACUUM opt_verbose opt_analyze relation_name opt_va_list
2060 VacuumStmt *n = makeNode(VacuumStmt);
2065 if ( $5 != NIL && !$4 )
2066 elog(WARN,"parser: syntax error at or near \"(\"",NULL);
2071 opt_verbose: VERBOSE { $$ = TRUE; }
2072 | /*EMPTY*/ { $$ = FALSE; }
2075 opt_analyze: ANALYZE { $$ = TRUE; }
2076 | /*EMPTY*/ { $$ = FALSE; }
2079 opt_va_list: '(' va_list ')'
2086 { $$=lcons($1,NIL); }
2088 { $$=lappend($1,$3); }
2092 /*****************************************************************************
2097 *****************************************************************************/
2099 ExplainStmt: EXPLAIN opt_verbose OptimizableStmt
2101 ExplainStmt *n = makeNode(ExplainStmt);
2103 n->query = (Query*)$3;
2109 /*****************************************************************************
2111 * Optimizable Stmts: *
2113 * one of the five queries processed by the planner *
2115 * [ultimately] produces query-trees as specified *
2116 * in the query-spec document in ~postgres/ref *
2118 *****************************************************************************/
2120 OptimizableStmt: RetrieveStmt
2125 | DeleteStmt /* by default all are $$=$1 */
2129 /*****************************************************************************
2134 *****************************************************************************/
2136 AppendStmt: INSERT INTO relation_name opt_column_list insert_rest
2144 insert_rest: VALUES '(' res_target_list2 ')'
2146 $$ = makeNode(AppendStmt);
2147 $$->targetList = $3;
2148 $$->fromClause = NIL;
2149 $$->whereClause = NULL;
2151 | SELECT res_target_list2 from_clause where_clause
2153 $$ = makeNode(AppendStmt);
2154 $$->targetList = $2;
2155 $$->fromClause = $3;
2156 $$->whereClause = $4;
2160 opt_column_list: '(' columnList ')' { $$ = $2; }
2161 | /*EMPTY*/ { $$ = NIL; }
2165 columnList ',' columnElem
2166 { $$ = lappend($1, $3); }
2168 { $$ = lcons($1, NIL); }
2171 columnElem: ColId opt_indirection
2173 Ident *id = makeNode(Ident);
2175 id->indirection = $2;
2181 /*****************************************************************************
2186 *****************************************************************************/
2188 DeleteStmt: DELETE FROM relation_name
2191 DeleteStmt *n = makeNode(DeleteStmt);
2193 n->whereClause = $4;
2199 /*****************************************************************************
2202 * ReplaceStmt (UPDATE)
2204 *****************************************************************************/
2206 ReplaceStmt: UPDATE relation_name
2211 ReplaceStmt *n = makeNode(ReplaceStmt);
2215 n->whereClause = $6;
2221 /*****************************************************************************
2226 *****************************************************************************/
2228 CursorStmt: DECLARE name opt_binary CURSOR FOR
2229 SELECT opt_unique res_target_list2
2230 from_clause where_clause group_clause sort_clause
2232 CursorStmt *n = makeNode(CursorStmt);
2234 /* from PORTAL name */
2236 * 15 august 1991 -- since 3.0 postgres does locking
2237 * right, we discovered that portals were violating
2238 * locking protocol. portal locks cannot span xacts.
2239 * as a short-term fix, we installed the check here.
2242 if (!IsTransactionBlock())
2243 elog(WARN,"Named portals may only be used in begin/end transaction blocks",NULL);
2250 n->whereClause = $10;
2251 n->groupClause = $11;
2252 n->sortClause = $12;
2258 /*****************************************************************************
2263 *****************************************************************************/
2265 RetrieveStmt: SELECT opt_unique res_target_list2
2266 result from_clause where_clause
2267 group_clause having_clause
2268 union_clause sort_clause
2270 RetrieveStmt *n = makeNode(RetrieveStmt);
2275 n->whereClause = $6;
2276 n->groupClause = $7;
2277 n->havingClause = $8;
2278 n->unionClause = $9;
2279 n->sortClause = $10;
2284 union_clause: UNION opt_union select_list
2286 RetrieveStmt *n = (RetrieveStmt *)lfirst($3);
2294 select_list: select_list UNION opt_union SubSelect
2296 RetrieveStmt *n = (RetrieveStmt *)$4;
2298 $$ = lappend($1, $4);
2301 { $$ = lcons($1, NIL); }
2304 SubSelect: SELECT opt_unique res_target_list2
2305 from_clause where_clause
2306 group_clause having_clause
2308 RetrieveStmt *n = makeNode(RetrieveStmt);
2310 n->unionall = FALSE;
2313 n->whereClause = $5;
2314 n->groupClause = $6;
2315 n->havingClause = $7;
2320 result: INTO TABLE relation_name
2326 opt_union: ALL { $$ = TRUE; }
2327 | /*EMPTY*/ { $$ = FALSE; }
2330 opt_unique: DISTINCT { $$ = "*"; }
2331 | DISTINCT ON ColId { $$ = $3; }
2332 | ALL { $$ = NULL; }
2333 | /*EMPTY*/ { $$ = NULL; }
2336 sort_clause: ORDER BY sortby_list { $$ = $3; }
2337 | /*EMPTY*/ { $$ = NIL; }
2340 sortby_list: sortby { $$ = lcons($1, NIL); }
2341 | sortby_list ',' sortby { $$ = lappend($1, $3); }
2344 sortby: ColId OptUseOp
2346 $$ = makeNode(SortGroupBy);
2352 | ColId '.' ColId OptUseOp
2354 $$ = makeNode(SortGroupBy);
2362 $$ = makeNode(SortGroupBy);
2370 OptUseOp: USING Op { $$ = $2; }
2371 | USING '<' { $$ = "<"; }
2372 | USING '>' { $$ = ">"; }
2374 | DESC { $$ = ">"; }
2375 | /*EMPTY*/ { $$ = "<"; /*default*/ }
2379 * jimmy bell-style recursive queries aren't supported in the
2382 * ...however, recursive addattr and rename supported. make special
2385 opt_inh_star: '*' { $$ = TRUE; }
2386 | /*EMPTY*/ { $$ = FALSE; }
2389 relation_name_list: name_list;
2392 { $$ = lcons(makeString($1),NIL); }
2393 | name_list ',' name
2394 { $$ = lappend($1,makeString($3)); }
2397 group_clause: GROUP BY groupby_list { $$ = $3; }
2398 | /*EMPTY*/ { $$ = NIL; }
2401 groupby_list: groupby { $$ = lcons($1, NIL); }
2402 | groupby_list ',' groupby { $$ = lappend($1, $3); }
2407 $$ = makeNode(SortGroupBy);
2415 $$ = makeNode(SortGroupBy);
2423 $$ = makeNode(SortGroupBy);
2431 having_clause: HAVING a_expr { $$ = $2; }
2432 | /*EMPTY*/ { $$ = NULL; }
2436 /*****************************************************************************
2438 * clauses common to all Optimizable Stmts:
2442 *****************************************************************************/
2444 from_clause: FROM '(' relation_expr join_expr JOIN relation_expr join_spec ')'
2447 elog(WARN,"JOIN not yet implemented",NULL);
2449 | FROM from_list { $$ = $2; }
2450 | /*EMPTY*/ { $$ = NIL; }
2453 from_list: from_list ',' from_val
2454 { $$ = lappend($1, $3); }
2455 | from_val CROSS JOIN from_val
2456 { elog(WARN,"CROSS JOIN not yet implemented",NULL); }
2458 { $$ = lcons($1, NIL); }
2461 from_val: relation_expr AS ColLabel
2463 $$ = makeNode(RangeVar);
2467 | relation_expr ColId
2469 $$ = makeNode(RangeVar);
2475 $$ = makeNode(RangeVar);
2481 join_expr: NATURAL join_expr { $$ = NULL; }
2483 { elog(WARN,"FULL OUTER JOIN not yet implemented",NULL); }
2485 { elog(WARN,"LEFT OUTER JOIN not yet implemented",NULL); }
2487 { elog(WARN,"RIGHT OUTER JOIN not yet implemented",NULL); }
2489 { elog(WARN,"OUTER JOIN not yet implemented",NULL); }
2491 { elog(WARN,"INNER JOIN not yet implemented",NULL); }
2493 { elog(WARN,"UNION JOIN not yet implemented",NULL); }
2495 { elog(WARN,"INNER JOIN not yet implemented",NULL); }
2498 join_outer: OUTER_P { $$ = NULL; }
2499 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2502 join_spec: ON '(' a_expr ')' { $$ = NULL; }
2503 | USING '(' join_list ')' { $$ = NULL; }
2504 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2507 join_list: join_using { $$ = lcons($1, NIL); }
2508 | join_list ',' join_using { $$ = lappend($1, $3); }
2513 $$ = makeNode(SortGroupBy);
2521 $$ = makeNode(SortGroupBy);
2529 $$ = makeNode(SortGroupBy);
2537 where_clause: WHERE a_expr { $$ = $2; }
2538 | /*EMPTY*/ { $$ = NULL; /* no qualifiers */ }
2541 relation_expr: relation_name
2543 /* normal relations */
2544 $$ = makeNode(RelExpr);
2548 | relation_name '*' %prec '='
2550 /* inheritance query */
2551 $$ = makeNode(RelExpr);
2556 opt_array_bounds: '[' ']' nest_array_bounds
2557 { $$ = lcons(makeInteger(-1), $3); }
2558 | '[' Iconst ']' nest_array_bounds
2559 { $$ = lcons(makeInteger($2), $4); }
2564 nest_array_bounds: '[' ']' nest_array_bounds
2565 { $$ = lcons(makeInteger(-1), $3); }
2566 | '[' Iconst ']' nest_array_bounds
2567 { $$ = lcons(makeInteger($2), $4); }
2573 /*****************************************************************************
2576 * SQL92 introduces a large amount of type-specific syntax.
2577 * Define individual clauses to handle these cases, and use
2578 * the generic case to handle regular type-extensible Postgres syntax.
2579 * - thomas 1997-10-10
2581 *****************************************************************************/
2583 Typename: Array opt_array_bounds
2586 $$->arrayBounds = $2;
2588 /* Is this the name of a complex type? If so, implement
2591 if (!strcmp(saved_relname, $$->name))
2592 /* This attr is the same type as the relation
2593 * being defined. The classic example: create
2594 * emp(name=text,mgr=emp)
2597 else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
2598 /* (Eventually add in here that the set can only
2599 * contain one element.)
2620 $$ = makeNode(TypeName);
2621 $$->name = xlateSqlType($1);
2625 generic: Id { $$ = $1; }
2626 | TYPE_P { $$ = xlateSqlType("type"); }
2627 | DOUBLE PRECISION { $$ = xlateSqlType("float8"); }
2630 /* SQL92 numeric data types
2631 * Check FLOAT() precision limits assuming IEEE floating types.
2632 * Provide rudimentary DECIMAL() and NUMERIC() implementations
2633 * by checking parameters and making sure they match what is possible with INTEGER.
2634 * - thomas 1997-09-18
2636 Numeric: FLOAT opt_float
2638 $$ = makeNode(TypeName);
2639 $$->name = xlateSqlType($2);
2641 | DECIMAL opt_decimal
2643 $$ = makeNode(TypeName);
2644 $$->name = xlateSqlType("integer");
2646 | NUMERIC opt_numeric
2648 $$ = makeNode(TypeName);
2649 $$->name = xlateSqlType("integer");
2653 opt_float: '(' Iconst ')'
2656 elog(WARN,"precision for FLOAT must be at least 1",NULL);
2658 $$ = xlateSqlType("float4");
2660 $$ = xlateSqlType("float8");
2662 elog(WARN,"precision for FLOAT must be less than 16",NULL);
2666 $$ = xlateSqlType("float8");
2670 opt_numeric: '(' Iconst ',' Iconst ')'
2673 elog(WARN,"NUMERIC precision %d must be 9",$2);
2675 elog(WARN,"NUMERIC scale %d must be zero",$4);
2680 elog(WARN,"NUMERIC precision %d must be 9",$2);
2688 opt_decimal: '(' Iconst ',' Iconst ')'
2691 elog(WARN,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2693 elog(WARN,"DECIMAL scale %d must be zero",$4);
2699 elog(WARN,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2708 /* SQL92 character data types
2709 * The following implements CHAR() and VARCHAR().
2710 * We do it here instead of the 'Generic' production
2711 * because we don't want to allow arrays of VARCHAR().
2712 * I haven't thought about whether that will work or not.
2715 Character: character '(' Iconst ')'
2717 $$ = makeNode(TypeName);
2718 if (!strcasecmp($1, "char"))
2719 $$->name = xlateSqlType("bpchar");
2720 else if (!strcasecmp($1, "varchar"))
2721 $$->name = xlateSqlType("varchar");
2723 yyerror("parse error");
2725 elog(WARN,"length for '%s' type must be at least 1",$1);
2727 /* we can store a char() of length up to the size
2728 * of a page (8KB) - page headers and friends but
2729 * just to be safe here... - ay 6/95
2730 * XXX note this hardcoded limit - thomas 1997-07-13
2732 elog(WARN,"length for type '%s' cannot exceed 4096",$1);
2734 /* we actually implement this sort of like a varlen, so
2735 * the first 4 bytes is the length. (the difference
2736 * between this and "text" is that we blank-pad and
2737 * truncate where necessary
2739 $$->typlen = VARHDRSZ + $3;
2743 $$ = makeNode(TypeName);
2744 $$->name = xlateSqlType($1);
2748 character: CHARACTER opt_varying opt_charset opt_collate
2751 if (($3 == NULL) || (strcasecmp($3, "sql_text") == 0)) {
2752 if ($2) type = xlateSqlType("varchar");
2753 else type = xlateSqlType("char");
2756 c = palloc(strlen("var") + strlen($3) + 1);
2759 type = xlateSqlType(c);
2761 type = xlateSqlType($3);
2765 elog(WARN,"COLLATE %s not yet implemented",$4);
2768 | CHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
2769 | VARCHAR { $$ = xlateSqlType("varchar"); }
2770 | NATIONAL CHARACTER opt_varying { $$ = xlateSqlType($3? "varchar": "char"); }
2771 | NCHAR opt_varying { $$ = xlateSqlType($2? "varchar": "char"); }
2774 opt_varying: VARYING { $$ = TRUE; }
2775 | /*EMPTY*/ { $$ = FALSE; }
2778 opt_charset: CHARACTER SET ColId { $$ = $3; }
2779 | /*EMPTY*/ { $$ = NULL; }
2782 opt_collate: COLLATE ColId { $$ = $2; }
2783 | /*EMPTY*/ { $$ = NULL; }
2788 $$ = makeNode(TypeName);
2789 $$->name = xlateSqlType($1);
2791 | TIMESTAMP opt_timezone
2793 $$ = makeNode(TypeName);
2794 $$->name = xlateSqlType("timestamp");
2799 $$ = makeNode(TypeName);
2800 $$->name = xlateSqlType("time");
2802 | INTERVAL opt_interval
2804 $$ = makeNode(TypeName);
2805 $$->name = xlateSqlType("interval");
2809 datetime: YEAR_P { $$ = "year"; }
2810 | MONTH_P { $$ = "month"; }
2811 | DAY_P { $$ = "day"; }
2812 | HOUR_P { $$ = "hour"; }
2813 | MINUTE_P { $$ = "minute"; }
2814 | SECOND_P { $$ = "second"; }
2817 opt_timezone: WITH TIME ZONE { $$ = TRUE; }
2818 | /*EMPTY*/ { $$ = FALSE; }
2821 opt_interval: datetime { $$ = lcons($1, NIL); }
2822 | YEAR_P TO MONTH_P { $$ = NIL; }
2823 | DAY_P TO HOUR_P { $$ = NIL; }
2824 | DAY_P TO MINUTE_P { $$ = NIL; }
2825 | DAY_P TO SECOND_P { $$ = NIL; }
2826 | HOUR_P TO MINUTE_P { $$ = NIL; }
2827 | HOUR_P TO SECOND_P { $$ = NIL; }
2828 | /*EMPTY*/ { $$ = NIL; }
2832 /*****************************************************************************
2834 * expression grammar, still needs some cleanup
2836 *****************************************************************************/
2838 a_expr_or_null: a_expr
2842 A_Const *n = makeNode(A_Const);
2843 n->val.type = T_Null;
2848 /* Expressions using row descriptors
2849 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
2850 * with singleton expressions.
2852 row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
2856 | '(' row_descriptor ')' NOT IN '(' SubSelect ')'
2860 | '(' row_descriptor ')' '=' '(' row_descriptor ')'
2862 $$ = makeRowExpr("=", $2, $6);
2864 | '(' row_descriptor ')' '<' '(' row_descriptor ')'
2866 $$ = makeRowExpr("<", $2, $6);
2868 | '(' row_descriptor ')' '>' '(' row_descriptor ')'
2870 $$ = makeRowExpr("<", $2, $6);
2872 | '(' row_descriptor ')' Op '(' row_descriptor ')'
2874 $$ = makeRowExpr($4, $2, $6);
2878 row_descriptor: row_list ',' a_expr
2880 $$ = lappend($1, $3);
2884 row_list: row_list ',' a_expr
2886 $$ = lappend($1, $3);
2890 $$ = lcons($1, NIL);
2894 a_expr: attr opt_indirection
2896 $1->indirection = $2;
2903 | '-' a_expr %prec UMINUS
2904 { $$ = makeA_Expr(OP, "-", NULL, $2); }
2906 { $$ = makeA_Expr(OP, "+", $1, $3); }
2908 { $$ = makeA_Expr(OP, "-", $1, $3); }
2910 { $$ = makeA_Expr(OP, "/", $1, $3); }
2912 { $$ = makeA_Expr(OP, "*", $1, $3); }
2914 { $$ = makeA_Expr(OP, "<", $1, $3); }
2916 { $$ = makeA_Expr(OP, ">", $1, $3); }
2918 { $$ = makeA_Expr(OP, "=", $1, $3); }
2920 { $$ = makeA_Expr(OP, ":", NULL, $2); }
2922 { $$ = makeA_Expr(OP, ";", NULL, $2); }
2924 { $$ = makeA_Expr(OP, "|", NULL, $2); }
2925 | a_expr TYPECAST Typename
2928 /* AexprConst can be either A_Const or ParamNo */
2929 if (nodeTag($1) == T_A_Const) {
2930 ((A_Const *)$1)->typename = $3;
2931 } else if (nodeTag($1) == T_Param) {
2932 ((ParamNo *)$1)->typename = $3;
2933 /* otherwise, try to transform to a function call */
2935 FuncCall *n = makeNode(FuncCall);
2936 n->funcname = $3->name;
2937 n->args = lcons($1,NIL);
2941 | CAST a_expr AS Typename
2944 /* AexprConst can be either A_Const or ParamNo */
2945 if (nodeTag($2) == T_A_Const) {
2946 ((A_Const *)$2)->typename = $4;
2947 } else if (nodeTag($2) == T_Param) {
2948 ((ParamNo *)$2)->typename = $4;
2949 /* otherwise, try to transform to a function call */
2951 FuncCall *n = makeNode(FuncCall);
2952 n->funcname = $4->name;
2953 n->args = lcons($2,NIL);
2957 | '(' a_expr_or_null ')'
2960 { $$ = makeIndexable($2,$1,$3); }
2961 | a_expr LIKE a_expr
2962 { $$ = makeIndexable("~~", $1, $3); }
2963 | a_expr NOT LIKE a_expr
2964 { $$ = makeA_Expr(OP, "!~~", $1, $4); }
2966 { $$ = makeA_Expr(OP, $1, NULL, $2); }
2968 { $$ = makeA_Expr(OP, $2, $1, NULL); }
2971 /* could be a column name or a relation_name */
2972 Ident *n = makeNode(Ident);
2974 n->indirection = NULL;
2979 /* cheap hack for aggregate (eg. count) */
2980 FuncCall *n = makeNode(FuncCall);
2981 A_Const *star = makeNode(A_Const);
2983 star->val.type = T_String;
2984 star->val.val.str = "";
2986 n->args = lcons(star, NIL);
2991 FuncCall *n = makeNode(FuncCall);
2996 | name '(' expr_list ')'
2998 FuncCall *n = makeNode(FuncCall);
3005 A_Const *n = makeNode(A_Const);
3006 TypeName *t = makeNode(TypeName);
3008 n->val.type = T_String;
3009 n->val.val.str = "now";
3012 t->name = xlateSqlType("date");
3019 A_Const *n = makeNode(A_Const);
3020 TypeName *t = makeNode(TypeName);
3022 n->val.type = T_String;
3023 n->val.val.str = "now";
3026 t->name = xlateSqlType("time");
3031 | CURRENT_TIME '(' Iconst ')'
3033 FuncCall *n = makeNode(FuncCall);
3034 A_Const *s = makeNode(A_Const);
3035 TypeName *t = makeNode(TypeName);
3037 n->funcname = xlateSqlType("time");
3038 n->args = lcons(s, NIL);
3040 s->val.type = T_String;
3041 s->val.val.str = "now";
3044 t->name = xlateSqlType("time");
3048 elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3054 A_Const *n = makeNode(A_Const);
3055 TypeName *t = makeNode(TypeName);
3057 n->val.type = T_String;
3058 n->val.val.str = "now";
3061 t->name = xlateSqlType("timestamp");
3066 | CURRENT_TIMESTAMP '(' Iconst ')'
3068 FuncCall *n = makeNode(FuncCall);
3069 A_Const *s = makeNode(A_Const);
3070 TypeName *t = makeNode(TypeName);
3072 n->funcname = xlateSqlType("timestamp");
3073 n->args = lcons(s, NIL);
3075 s->val.type = T_String;
3076 s->val.val.str = "now";
3079 t->name = xlateSqlType("timestamp");
3083 elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
3089 FuncCall *n = makeNode(FuncCall);
3090 n->funcname = "getpgusername";
3094 /* We probably need to define an "exists" node,
3095 * since the optimizer could choose to find only one match.
3096 * Perhaps the first implementation could just check for
3097 * count(*) > 0? - thomas 1997-07-19
3099 | EXISTS '(' SubSelect ')'
3101 elog(WARN,"EXISTS not yet implemented",NULL);
3104 | EXTRACT '(' extract_list ')'
3106 FuncCall *n = makeNode(FuncCall);
3107 n->funcname = "date_part";
3111 | POSITION '(' position_list ')'
3113 FuncCall *n = makeNode(FuncCall);
3114 n->funcname = "strpos";
3118 | SUBSTRING '(' substr_list ')'
3120 FuncCall *n = makeNode(FuncCall);
3121 n->funcname = "substr";
3125 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
3126 | TRIM '(' BOTH trim_list ')'
3128 FuncCall *n = makeNode(FuncCall);
3129 n->funcname = "btrim";
3133 | TRIM '(' LEADING trim_list ')'
3135 FuncCall *n = makeNode(FuncCall);
3136 n->funcname = "ltrim";
3140 | TRIM '(' TRAILING trim_list ')'
3142 FuncCall *n = makeNode(FuncCall);
3143 n->funcname = "rtrim";
3147 | TRIM '(' trim_list ')'
3149 FuncCall *n = makeNode(FuncCall);
3150 n->funcname = "btrim";
3155 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3157 { $$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3159 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3160 | a_expr IS NOT NULL_P
3161 { $$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3162 /* IS TRUE, IS FALSE, etc used to be function calls
3163 * but let's make them expressions to allow the optimizer
3164 * a chance to eliminate them if a_expr is a constant string.
3165 * - thomas 1997-12-22
3169 A_Const *n = makeNode(A_Const);
3170 n->val.type = T_String;
3171 n->val.val.str = "t";
3172 n->typename = makeNode(TypeName);
3173 n->typename->name = xlateSqlType("bool");
3174 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3176 | a_expr IS NOT FALSE_P
3178 A_Const *n = makeNode(A_Const);
3179 n->val.type = T_String;
3180 n->val.val.str = "t";
3181 n->typename = makeNode(TypeName);
3182 n->typename->name = xlateSqlType("bool");
3183 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3187 A_Const *n = makeNode(A_Const);
3188 n->val.type = T_String;
3189 n->val.val.str = "f";
3190 n->typename = makeNode(TypeName);
3191 n->typename->name = xlateSqlType("bool");
3192 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3194 | a_expr IS NOT TRUE_P
3196 A_Const *n = makeNode(A_Const);
3197 n->val.type = T_String;
3198 n->val.val.str = "f";
3199 n->typename = makeNode(TypeName);
3200 n->typename->name = xlateSqlType("bool");
3201 $$ = makeA_Expr(OP, "=", $1,(Node *)n);
3203 | a_expr BETWEEN AexprConst AND AexprConst
3205 $$ = makeA_Expr(AND, NULL,
3206 makeA_Expr(OP, ">=", $1, $3),
3207 makeA_Expr(OP, "<=", $1, $5));
3209 | a_expr NOT BETWEEN AexprConst AND AexprConst
3211 $$ = makeA_Expr(OR, NULL,
3212 makeA_Expr(OP, "<", $1, $4),
3213 makeA_Expr(OP, ">", $1, $6));
3215 | a_expr IN { saved_In_Expr = $1; } '(' in_expr ')'
3217 | a_expr NOT IN { saved_In_Expr = $1; } '(' not_in_expr ')'
3220 { $$ = makeA_Expr(AND, NULL, $1, $3); }
3222 { $$ = makeA_Expr(OR, NULL, $1, $3); }
3224 { $$ = makeA_Expr(NOT, NULL, NULL, $2); }
3227 opt_indirection: '[' a_expr ']' opt_indirection
3229 A_Indices *ai = makeNode(A_Indices);
3234 | '[' a_expr ':' a_expr ']' opt_indirection
3236 A_Indices *ai = makeNode(A_Indices);
3245 expr_list: a_expr_or_null
3246 { $$ = lcons($1, NIL); }
3247 | expr_list ',' a_expr_or_null
3248 { $$ = lappend($1, $3); }
3249 | expr_list USING a_expr
3250 { $$ = lappend($1, $3); }
3253 extract_list: datetime FROM a_expr
3255 A_Const *n = makeNode(A_Const);
3256 n->val.type = T_String;
3257 n->val.val.str = $1;
3258 $$ = lappend(lcons((Node *)n,NIL), $3);
3264 position_list: position_expr IN position_expr
3265 { $$ = makeList($3, $1, -1); }
3270 position_expr: attr opt_indirection
3272 $1->indirection = $2;
3277 | '-' position_expr %prec UMINUS
3278 { $$ = makeA_Expr(OP, "-", NULL, $2); }
3279 | position_expr '+' position_expr
3280 { $$ = makeA_Expr(OP, "+", $1, $3); }
3281 | position_expr '-' position_expr
3282 { $$ = makeA_Expr(OP, "-", $1, $3); }
3283 | position_expr '/' position_expr
3284 { $$ = makeA_Expr(OP, "/", $1, $3); }
3285 | position_expr '*' position_expr
3286 { $$ = makeA_Expr(OP, "*", $1, $3); }
3288 { $$ = makeA_Expr(OP, "|", NULL, $2); }
3289 | position_expr TYPECAST Typename
3292 /* AexprConst can be either A_Const or ParamNo */
3293 if (nodeTag($1) == T_A_Const) {
3294 ((A_Const *)$1)->typename = $3;
3295 } else if (nodeTag($1) == T_Param) {
3296 ((ParamNo *)$1)->typename = $3;
3297 /* otherwise, try to transform to a function call */
3299 FuncCall *n = makeNode(FuncCall);
3300 n->funcname = $3->name;
3301 n->args = lcons($1,NIL);
3305 | CAST position_expr AS Typename
3308 /* AexprConst can be either A_Const or ParamNo */
3309 if (nodeTag($2) == T_A_Const) {
3310 ((A_Const *)$2)->typename = $4;
3311 } else if (nodeTag($2) == T_Param) {
3312 ((ParamNo *)$2)->typename = $4;
3313 /* otherwise, try to transform to a function call */
3315 FuncCall *n = makeNode(FuncCall);
3316 n->funcname = $4->name;
3317 n->args = lcons($2,NIL);
3321 | '(' position_expr ')'
3323 | position_expr Op position_expr
3324 { $$ = makeA_Expr(OP, $2, $1, $3); }
3326 { $$ = makeA_Expr(OP, $1, NULL, $2); }
3328 { $$ = makeA_Expr(OP, $2, $1, NULL); }
3331 /* could be a column name or a relation_name */
3332 Ident *n = makeNode(Ident);
3334 n->indirection = NULL;
3339 FuncCall *n = makeNode(FuncCall);
3344 | name '(' expr_list ')'
3346 FuncCall *n = makeNode(FuncCall);
3351 | POSITION '(' position_list ')'
3353 FuncCall *n = makeNode(FuncCall);
3354 n->funcname = "strpos";
3358 | SUBSTRING '(' substr_list ')'
3360 FuncCall *n = makeNode(FuncCall);
3361 n->funcname = "substr";
3365 /* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
3366 | TRIM '(' BOTH trim_list ')'
3368 FuncCall *n = makeNode(FuncCall);
3369 n->funcname = "btrim";
3373 | TRIM '(' LEADING trim_list ')'
3375 FuncCall *n = makeNode(FuncCall);
3376 n->funcname = "ltrim";
3380 | TRIM '(' TRAILING trim_list ')'
3382 FuncCall *n = makeNode(FuncCall);
3383 n->funcname = "rtrim";
3387 | TRIM '(' trim_list ')'
3389 FuncCall *n = makeNode(FuncCall);
3390 n->funcname = "btrim";
3396 substr_list: expr_list substr_from substr_for
3398 $$ = nconc(nconc($1,$2),$3);
3404 substr_from: FROM expr_list
3408 A_Const *n = makeNode(A_Const);
3409 n->val.type = T_Integer;
3410 n->val.val.ival = 1;
3411 $$ = lcons((Node *)n,NIL);
3415 substr_for: FOR expr_list
3421 trim_list: a_expr FROM expr_list
3422 { $$ = lappend($3, $1); }
3431 elog(WARN,"IN (SUBSELECT) not yet implemented",NULL);
3438 in_expr_nodes: AexprConst
3439 { $$ = makeA_Expr(OP, "=", saved_In_Expr, $1); }
3440 | in_expr_nodes ',' AexprConst
3441 { $$ = makeA_Expr(OR, NULL, $1,
3442 makeA_Expr(OP, "=", saved_In_Expr, $3));
3446 not_in_expr: SubSelect
3448 elog(WARN,"NOT IN (SUBSELECT) not yet implemented",NULL);
3455 not_in_expr_nodes: AexprConst
3456 { $$ = makeA_Expr(OP, "<>", saved_In_Expr, $1); }
3457 | not_in_expr_nodes ',' AexprConst
3458 { $$ = makeA_Expr(AND, NULL, $1,
3459 makeA_Expr(OP, "<>", saved_In_Expr, $3));
3463 attr: relation_name '.' attrs
3465 $$ = makeNode(Attr);
3469 $$->indirection = NULL;
3473 $$ = makeNode(Attr);
3477 $$->indirection = NULL;
3482 { $$ = lcons(makeString($1), NIL); }
3483 | attrs '.' attr_name
3484 { $$ = lappend($1, makeString($3)); }
3486 { $$ = lappend($1, makeString("*")); }
3490 /*****************************************************************************
3494 *****************************************************************************/
3496 res_target_list: res_target_list ',' res_target_el
3497 { $$ = lappend($1,$3); }
3499 { $$ = lcons($1, NIL); }
3502 ResTarget *rt = makeNode(ResTarget);
3503 Attr *att = makeNode(Attr);
3505 att->paramNo = NULL;
3507 att->indirection = NIL;
3509 rt->indirection = NULL;
3510 rt->val = (Node *)att;
3511 $$ = lcons(rt, NIL);
3515 res_target_el: ColId opt_indirection '=' a_expr_or_null
3517 $$ = makeNode(ResTarget);
3519 $$->indirection = $2;
3520 $$->val = (Node *)$4;
3522 | attr opt_indirection
3524 $$ = makeNode(ResTarget);
3526 $$->indirection = $2;
3527 $$->val = (Node *)$1;
3529 | relation_name '.' '*'
3531 Attr *att = makeNode(Attr);
3533 att->paramNo = NULL;
3534 att->attrs = lcons(makeString("*"), NIL);
3535 att->indirection = NIL;
3536 $$ = makeNode(ResTarget);
3538 $$->indirection = NULL;
3539 $$->val = (Node *)att;
3544 ** target list for select.
3545 ** should get rid of the other but is still needed by the defunct retrieve into
3546 ** and update (uses a subset)
3548 res_target_list2: res_target_list2 ',' res_target_el2
3549 { $$ = lappend($1, $3); }
3551 { $$ = lcons($1, NIL); }
3554 /* AS is not optional because shift/red conflict with unary ops */
3555 res_target_el2: a_expr_or_null AS ColLabel
3557 $$ = makeNode(ResTarget);
3559 $$->indirection = NULL;
3560 $$->val = (Node *)$1;
3564 $$ = makeNode(ResTarget);
3566 $$->indirection = NULL;
3567 $$->val = (Node *)$1;
3569 | relation_name '.' '*'
3571 Attr *att = makeNode(Attr);
3573 att->paramNo = NULL;
3574 att->attrs = lcons(makeString("*"), NIL);
3575 att->indirection = NIL;
3576 $$ = makeNode(ResTarget);
3578 $$->indirection = NULL;
3579 $$->val = (Node *)att;
3583 Attr *att = makeNode(Attr);
3585 att->paramNo = NULL;
3587 att->indirection = NIL;
3588 $$ = makeNode(ResTarget);
3590 $$->indirection = NULL;
3591 $$->val = (Node *)att;
3595 opt_id: ColId { $$ = $1; }
3596 | /* EMPTY */ { $$ = NULL; }
3599 relation_name: SpecialRuleRelation
3602 StrNCpy(saved_relname, $1, NAMEDATALEN);
3606 /* disallow refs to variable system tables */
3607 if (strcmp(LogRelationName, $1) == 0
3608 || strcmp(VariableRelationName, $1) == 0)
3609 elog(WARN,"%s cannot be accessed by users",$1);
3612 StrNCpy(saved_relname, $1, NAMEDATALEN);
3616 database_name: ColId { $$ = $1; };
3617 access_method: Id { $$ = $1; };
3618 attr_name: ColId { $$ = $1; };
3619 class: Id { $$ = $1; };
3620 index_name: ColId { $$ = $1; };
3623 * Include date/time keywords as SQL92 extension.
3624 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
3626 name: ColId { $$ = $1; };
3628 file_name: Sconst { $$ = $1; };
3629 recipe_name: Id { $$ = $1; };
3632 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
3636 A_Const *n = makeNode(A_Const);
3637 n->val.type = T_Integer;
3638 n->val.val.ival = $1;
3643 A_Const *n = makeNode(A_Const);
3644 n->val.type = T_Float;
3645 n->val.val.dval = $1;
3650 A_Const *n = makeNode(A_Const);
3651 n->val.type = T_String;
3652 n->val.val.str = $1;
3657 A_Const *n = makeNode(A_Const);
3659 n->val.type = T_String;
3660 n->val.val.str = $2;
3664 { $$ = (Node *)$1; }
3667 A_Const *n = makeNode(A_Const);
3668 n->val.type = T_String;
3669 n->val.val.str = "t";
3670 n->typename = makeNode(TypeName);
3671 n->typename->name = xlateSqlType("bool");
3676 A_Const *n = makeNode(A_Const);
3677 n->val.type = T_String;
3678 n->val.val.str = "f";
3679 n->typename = makeNode(TypeName);
3680 n->typename->name = xlateSqlType("bool");
3687 $$ = makeNode(ParamNo);
3692 NumConst: Iconst { $$ = makeInteger($1); }
3693 | FCONST { $$ = makeFloat($1); }
3696 Iconst: ICONST { $$ = $1; };
3697 Sconst: SCONST { $$ = $1; };
3699 /* Column and type identifier
3700 * Does not include explicit datetime types
3701 * since these must be decoupled in Typename syntax.
3702 * Use ColId for most identifiers. - thomas 1997-10-21
3704 Id: IDENT { $$ = $1; };
3706 /* Column identifier
3707 * Include date/time keywords as SQL92 extension.
3708 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
3709 * Add other keywords. Note that as the syntax expands,
3710 * some of these keywords will have to be removed from this
3711 * list due to shift/reduce conflicts in yacc. If so, move
3712 * down to the ColLabel entity. - thomas 1997-11-06
3714 ColId: Id { $$ = $1; }
3715 | datetime { $$ = $1; }
3716 | ACTION { $$ = "action"; }
3717 | DATABASE { $$ = "database"; }
3718 | DELIMITERS { $$ = "delimiters"; }
3719 | FUNCTION { $$ = "function"; }
3720 | INDEX { $$ = "index"; }
3721 | KEY { $$ = "key"; }
3722 | LANGUAGE { $$ = "language"; }
3723 | LOCATION { $$ = "location"; }
3724 | MATCH { $$ = "match"; }
3725 | OPERATOR { $$ = "operator"; }
3726 | OPTION { $$ = "option"; }
3727 | PRIVILEGES { $$ = "privileges"; }
3728 | RECIPE { $$ = "recipe"; }
3729 | TIME { $$ = "time"; }
3730 | TRIGGER { $$ = "trigger"; }
3731 | TYPE_P { $$ = "type"; }
3732 | VERSION { $$ = "version"; }
3733 | ZONE { $$ = "zone"; }
3737 * Allowed labels in "AS" clauses.
3738 * Include TRUE/FALSE SQL3 reserved words for Postgres backward
3739 * compatibility. Cannot allow this for column names since the
3740 * syntax would not distinguish between the constant value and
3741 * a column name. - thomas 1997-10-24
3742 * Add other keywords to this list. Note that they appear here
3743 * rather than in ColId if there was a shift/reduce conflict
3744 * when used as a full identifier. - thomas 1997-11-06
3746 ColLabel: ColId { $$ = $1; }
3747 | ARCHIVE { $$ = "archive"; }
3748 | CLUSTER { $$ = "cluster"; }
3749 | CONSTRAINT { $$ = "constraint"; }
3750 | CROSS { $$ = "cross"; }
3751 | FOREIGN { $$ = "foreign"; }
3752 | GROUP { $$ = "group"; }
3753 | LOAD { $$ = "load"; }
3754 | ORDER { $$ = "order"; }
3755 | POSITION { $$ = "position"; }
3756 | PRECISION { $$ = "precision"; }
3757 | TABLE { $$ = "table"; }
3758 | TRANSACTION { $$ = "transaction"; }
3759 | TRUE_P { $$ = "true"; }
3760 | FALSE_P { $$ = "false"; }
3763 SpecialRuleRelation: CURRENT
3768 elog(WARN,"CURRENT used in non-rule query",NULL);
3775 elog(WARN,"NEW used in non-rule query",NULL);
3782 makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
3784 A_Expr *a = makeNode(A_Expr);
3793 * Generate separate operator nodes for a single row descriptor expression.
3794 * Perhaps this should go deeper in the parser someday... - thomas 1997-12-22
3797 makeRowExpr(char *opr, List *largs, List *rargs)
3802 if (length(largs) != length(rargs))
3803 elog(WARN,"Unequal number of entries in row expression",NULL);
3805 if (lnext(largs) != NIL)
3806 expr = makeRowExpr(opr,lnext(largs),lnext(rargs));
3808 larg = lfirst(largs);
3809 rarg = lfirst(rargs);
3811 if ((strcmp(opr, "=") == 0)
3812 || (strcmp(opr, "<") == 0)
3813 || (strcmp(opr, "<=") == 0)
3814 || (strcmp(opr, ">") == 0)
3815 || (strcmp(opr, ">=") == 0))
3818 expr = makeA_Expr(OP, opr, larg, rarg);
3820 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3822 else if (strcmp(opr, "<>") == 0)
3825 expr = makeA_Expr(OP, opr, larg, rarg);
3827 expr = makeA_Expr(OR, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3831 elog(WARN,"Operator '%s' not implemented for row expressions",opr);
3835 while ((largs != NIL) && (rargs != NIL))
3837 larg = lfirst(largs);
3838 rarg = lfirst(rargs);
3841 expr = makeA_Expr(OP, opr, larg, rarg);
3843 expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
3845 largs = lnext(largs);
3846 rargs = lnext(rargs);
3852 } /* makeRowExpr() */
3855 mapTargetColumns(List *src, List *dst)
3860 if (length(src) != length(dst))
3861 elog(WARN,"CREATE TABLE/AS SELECT has mismatched column count",NULL);
3863 while ((src != NIL) && (dst != NIL))
3865 s = (ColumnDef *)lfirst(src);
3866 d = (ResTarget *)lfirst(dst);
3868 d->name = s->colname;
3875 } /* mapTargetColumns() */
3877 static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr)
3879 Node *result = NULL;
3881 /* we do this so indexes can be used */
3882 if (strcmp(opname,"~") == 0 ||
3883 strcmp(opname,"~*") == 0)
3885 if (nodeTag(rexpr) == T_A_Const &&
3886 ((A_Const *)rexpr)->val.type == T_String &&
3887 ((A_Const *)rexpr)->val.val.str[0] == '^')
3889 A_Const *n = (A_Const *)rexpr;
3890 char *match_least = palloc(strlen(n->val.val.str)+2);
3891 char *match_most = palloc(strlen(n->val.val.str)+2);
3892 int pos, match_pos=0;
3894 /* skip leading ^ */
3895 for (pos = 1; n->val.val.str[pos]; pos++)
3897 if (n->val.val.str[pos] == '.' ||
3898 n->val.val.str[pos] == '?' ||
3899 n->val.val.str[pos] == '*' ||
3900 n->val.val.str[pos] == '[' ||
3901 n->val.val.str[pos] == '$' ||
3902 (strcmp(opname,"~*") == 0 && isalpha(n->val.val.str[pos])))
3904 if (n->val.val.str[pos] == '\\')
3906 match_least[match_pos] = n->val.val.str[pos];
3907 match_most[match_pos++] = n->val.val.str[pos];
3912 A_Const *least = makeNode(A_Const);
3913 A_Const *most = makeNode(A_Const);
3915 /* make strings to be used in index use */
3916 match_least[match_pos] = '\0';
3917 match_most[match_pos] = '\377';
3918 match_most[match_pos+1] = '\0';
3919 least->val.type = T_String;
3920 least->val.val.str = match_least;
3921 most->val.type = T_String;
3922 most->val.val.str = match_most;
3923 result = makeA_Expr(AND, NULL,
3924 makeA_Expr(OP, "~", lexpr, rexpr),
3925 makeA_Expr(AND, NULL,
3926 makeA_Expr(OP, ">=", lexpr, (Node *)least),
3927 makeA_Expr(OP, "<=", lexpr, (Node *)most)));
3931 else if (strcmp(opname,"~~") == 0)
3933 if (nodeTag(rexpr) == T_A_Const &&
3934 ((A_Const *)rexpr)->val.type == T_String)
3936 A_Const *n = (A_Const *)rexpr;
3937 char *match_least = palloc(strlen(n->val.val.str)+2);
3938 char *match_most = palloc(strlen(n->val.val.str)+2);
3939 int pos, match_pos=0;
3941 for (pos = 0; n->val.val.str[pos]; pos++)
3943 if ((n->val.val.str[pos] == '%' &&
3944 n->val.val.str[pos+1] != '%') ||
3945 (n->val.val.str[pos] == '_' &&
3946 n->val.val.str[pos+1] != '_'))
3948 if (n->val.val.str[pos] == '%' ||
3949 n->val.val.str[pos] == '_' ||
3950 n->val.val.str[pos] == '\\')
3952 match_least[match_pos] = n->val.val.str[pos];
3953 match_most[match_pos++] = n->val.val.str[pos];
3958 A_Const *least = makeNode(A_Const);
3959 A_Const *most = makeNode(A_Const);
3961 /* make strings to be used in index use */
3962 match_least[match_pos] = '\0';
3963 match_most[match_pos] = '\377';
3964 match_most[match_pos+1] = '\0';
3965 least->val.type = T_String;
3966 least->val.val.str = match_least;
3967 most->val.type = T_String;
3968 most->val.val.str = match_most;
3969 result = makeA_Expr(AND, NULL,
3970 makeA_Expr(OP, "~~", lexpr, rexpr),
3971 makeA_Expr(AND, NULL,
3972 makeA_Expr(OP, ">=", lexpr, (Node *)least),
3973 makeA_Expr(OP, "<=", lexpr, (Node *)most)));
3979 result = makeA_Expr(OP, opname, lexpr, rexpr);
3981 } /* makeIndexable() */
3985 * Convert alternate type names to internal Postgres types.
3986 * Do not convert "float", since that is handled elsewhere
3987 * for FLOAT(p) syntax.
3990 xlateSqlType(char *name)
3992 if (!strcasecmp(name,"int")
3993 || !strcasecmp(name,"integer"))
3995 else if (!strcasecmp(name, "smallint"))
3997 else if (!strcasecmp(name, "real"))
3999 else if (!strcasecmp(name, "interval"))
4001 else if (!strcasecmp(name, "boolean"))
4005 } /* xlateSqlName() */
4008 void parser_init(Oid *typev, int nargs)
4010 QueryIsRule = FALSE;
4011 saved_relname[0]= '\0';
4012 saved_In_Expr = NULL;
4014 param_type_init(typev, nargs);
4018 /* FlattenStringList()
4019 * Traverse list of string nodes and convert to a single string.
4020 * Used for reconstructing string form of complex expressions.
4022 * Allocate at least one byte for terminator.
4025 FlattenStringList(List *list)
4033 nlist = length(list);
4036 v = (Value *)lfirst(l);
4043 s = (char*) palloc(len+1);
4048 v = (Value *)lfirst(l);
4052 if (l != NIL) strcat(s," ");
4057 printf( "flattened string is \"%s\"\n", s);
4061 } /* FlattenStringList() */
4064 /* makeConstantList()
4065 * Convert constant value node into string node.
4068 makeConstantList( A_Const *n)
4070 char *defval = NULL;
4071 if (nodeTag(n) != T_A_Const) {
4072 elog(WARN,"Cannot handle non-constant parameter",NULL);
4074 } else if (n->val.type == T_Float) {
4075 defval = (char*) palloc(20+1);
4076 sprintf( defval, "%g", n->val.val.dval);
4078 } else if (n->val.type == T_Integer) {
4079 defval = (char*) palloc(20+1);
4080 sprintf( defval, "%ld", n->val.val.ival);
4082 } else if (n->val.type == T_String) {
4083 defval = (char*) palloc(strlen( ((A_Const *) n)->val.val.str) + 3);
4084 strcpy( defval, "'");
4085 strcat( defval, ((A_Const *) n)->val.val.str);
4086 strcat( defval, "'");
4089 elog(WARN,"Internal error in makeConstantList(): cannot encode node",NULL);
4093 printf( "AexprConst argument is \"%s\"\n", defval);
4096 return( lcons( makeString(defval), NIL));
4097 } /* makeConstantList() */
4101 * Check input string for non-lowercase/non-numeric characters.
4102 * Returns either input string or input surrounded by double quotes.
4109 for (cp = rawid; *cp != '\0'; cp++)
4110 if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break;
4113 cp = palloc(strlen(rawid)+1);
4122 printf("fmtId- %sconvert %s to %s\n", ((cp == rawid)? "do not ": ""), rawid, cp);
4131 * keep enough information around fill out the type of param nodes
4132 * used in postquel functions
4135 param_type_init(Oid *typev, int nargs)
4137 pfunc_num_args = nargs;
4138 param_type_info = typev;
4141 Oid param_type(int t)
4143 if ((t > pfunc_num_args) || (t == 0))
4145 return param_type_info[t - 1];