gram.y 120.7 KB
Newer Older
1 2
%{ /* -*-text-*- */

3
/*#define YYDEBUG 1*/
4
/*-------------------------------------------------------------------------
5
 *
6
 * gram.y--
7
 *	  POSTGRES SQL YACC rules/actions
8
 *
9 10 11 12
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
13
 *	  $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.28 1998/09/01 03:24:08 momjian Exp $
14 15
 *
 * HISTORY
16 17 18
 *	  AUTHOR			DATE			MAJOR EVENT
 *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
 *	  Andrew Yu			Oct, 1994		lispy code conversion
19 20
 *
 * NOTES
21 22
 *	  CAPITALS are used to represent terminal symbols.
 *	  non-capitals are used to represent non-terminals.
23 24
 *	  SQL92-specific syntax is separated from plain SQL/Postgres syntax
 *	  to help isolate the non-extensible portions of the parser.
25
 *
26 27
 *	  if you use list, make sure the datum is a node so that the printing
 *	  routines work
28 29
 *
 * WARNING
30 31
 *	  sometimes we assign constants to makeStrings. Make sure we don't free
 *	  those.
32 33 34 35 36
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include <ctype.h>
B
Bruce Momjian 已提交
37

38 39
#include "postgres.h"
#include "nodes/parsenodes.h"
40
#include "nodes/print.h"
B
Bruce Momjian 已提交
41
#include "parser/gramparse.h"
42
#include "parser/parse_type.h"
43
#include "utils/acl.h"
44
#include "utils/palloc.h"
45 46 47 48
#include "catalog/catname.h"
#include "utils/elog.h"
#include "access/xact.h"

M
 
Marc G. Fournier 已提交
49
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
50 51 52
#include "mb/pg_wchar.h"
#endif

53
static char saved_relname[NAMEDATALEN];  /* need this for complex attributes */
54
static bool QueryIsRule = FALSE;
55
static List *saved_In_Expr = NIL;
56 57
static Oid	*param_type_info;
static int	pfunc_num_args;
58 59
extern List *parsetree;

60

61
/*
62
 * If you need access to certain yacc-generated variables and find that
63 64 65 66 67
 * they're static by default, uncomment the next line.  (this is not a
 * problem, yet.)
 */
/*#define __YYSCLASS*/

68
static char *xlateSqlFunc(char *);
69
static char *xlateSqlType(char *);
70
static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
B
Bruce Momjian 已提交
71
static Node *makeRowExpr(char *opr, List *largs, List *rargs);
72
void mapTargetColumns(List *source, List *target);
73 74
static List *makeConstantList( A_Const *node);
static char *FlattenStringList(List *list);
75
static char *fmtId(char *rawid);
76
static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr);
77 78 79
static void param_type_init(Oid *typev, int nargs);

Oid	param_type(int t); /* used in parse_expr.c */
80 81 82 83 84 85 86 87

/* old versions of flex define this as a macro */
#if defined(yywrap)
#undef yywrap
#endif /* yywrap */
%}


88 89 90 91 92 93 94
%union
{
	double				dval;
	int					ival;
	char				chr;
	char				*str;
	bool				boolean;
95
	bool*				pboolean;	/* for pg_shadow privileges */
96 97 98 99 100 101 102 103 104 105
	List				*list;
	Node				*node;
	Value				*value;

	Attr				*attr;

	TypeName			*typnam;
	DefElem				*defelt;
	ParamString			*param;
	SortGroupBy			*sortgroupby;
M
 
Marc G. Fournier 已提交
106
	JoinUsing			*joinusing;
107 108 109 110 111 112 113 114 115 116
	IndexElem			*ielem;
	RangeVar			*range;
	RelExpr				*relexp;
	A_Indices			*aind;
	ResTarget			*target;
	ParamNo				*paramno;

	VersionStmt			*vstmt;
	DefineStmt			*dstmt;
	RuleStmt			*rstmt;
B
Bruce Momjian 已提交
117
	InsertStmt			*astmt;
118 119
}

120
%type <node>	stmt,
121
		AddAttrStmt, ClosePortalStmt,
122
		CopyStmt, CreateStmt, CreateAsStmt, CreateSeqStmt, DefineStmt, DestroyStmt,
123
		ExtendStmt, FetchStmt,	GrantStmt, CreateTrigStmt, DropTrigStmt,
124
		CreatePLangStmt, DropPLangStmt,
M
 
Marc G. Fournier 已提交
125
		IndexStmt, ListenStmt, UnlistenStmt, LockStmt, OptimizableStmt,
B
Bruce Momjian 已提交
126 127
		ProcedureStmt, 	RecipeStmt, RemoveAggrStmt, RemoveOperStmt,
		RemoveFuncStmt, RemoveStmt,
128
		RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
129
		CreatedbStmt, DestroydbStmt, VacuumStmt, CursorStmt, SubSelect, SubUnion,
B
Bruce Momjian 已提交
130
		UpdateStmt, InsertStmt, SelectStmt, NotifyStmt, DeleteStmt, ClusterStmt,
131 132
		ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
		CreateUserStmt, AlterUserStmt, DropUserStmt
133

M
 
Marc G. Fournier 已提交
134
%type <str>	opt_database1, opt_database2, location, encoding
135

136 137 138 139 140
%type <pboolean> user_createdb_clause, user_createuser_clause
%type <str>   user_passwd_clause
%type <str>   user_valid_clause
%type <list>  user_group_list, user_group_clause

141
%type <str>		join_expr, join_outer, join_spec
142
%type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
143

144
%type <str>		TriggerEvents, TriggerFuncArg
145

146 147
%type <str>		relation_name, copy_file_name, copy_delimiter, def_name,
		database_name, access_method_clause, access_method, attr_name,
148
		class, index_name, name, func_name, file_name, recipe_name, aggr_argtype
149

150
%type <str>		opt_id, opt_portal_name,
B
Bruce Momjian 已提交
151
		all_Op, MathOp, opt_name, opt_unique,
152
		result, OptUseOp, opt_class, SpecialRuleRelation
153

154 155
%type <str>		privileges, operation_commalist, grantee
%type <chr>		operation, TriggerOneEvent
156

157
%type <list>	stmtblock, stmtmulti,
158 159
		relation_name_list, OptTableElementList,
		OptInherit, definition,
160
		opt_with, func_args, func_args_list,
161 162 163 164
		oper_argtypes, OptStmtList, OptStmtBlock, OptStmtMulti,
		opt_column_list, columnList, opt_va_list, va_list,
		sort_clause, sortby_list, index_params, index_list, name_list,
		from_clause, from_list, opt_array_bounds, nest_array_bounds,
165
		expr_list, attrs, res_target_list, res_target_list2,
M
 
Marc G. Fournier 已提交
166
		def_list, opt_indirection, group_clause, TriggerFuncArgs
167

168 169 170 171 172
%type <node>	func_return
%type <boolean>	set_opt

%type <boolean>	TriggerForOpt, TriggerForType

173 174
%type <list>	union_clause, select_list
%type <list>	join_list
M
 
Marc G. Fournier 已提交
175
%type <joinusing>
176 177
				join_using
%type <boolean>	opt_union
178
%type <boolean>	opt_table
179
%type <boolean>	opt_trans
180

181
%type <node>	position_expr
182 183
%type <list>	extract_list, position_list
%type <list>	substr_list, substr_from, substr_for, trim_list
184
%type <list>	opt_interval
185

186 187
%type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
				index_opt_unique, opt_verbose, opt_analyze
188

B
Bruce Momjian 已提交
189 190
%type <ival>	copy_dirn, def_type, opt_direction, remove_type,
				opt_column, event
191

B
Bruce Momjian 已提交
192
%type <ival>	fetch_how_many
193

V
Vadim B. Mikheev 已提交
194 195 196
%type <list>	OptSeqList
%type <defelt>	OptSeqElem

197 198 199
%type <dstmt>	def_rest
%type <astmt>	insert_rest

200 201
%type <node>	OptTableElement, ConstraintElem
%type <node>	columnDef, alter_clause
202
%type <defelt>	def_elem
203
%type <node>	def_arg, columnElem, where_clause,
204
				a_expr, a_expr_or_null, b_expr, AexprConst,
205
				in_expr, in_expr_nodes, not_in_expr, not_in_expr_nodes,
206
				having_clause
207
%type <list>	row_descriptor, row_list, c_list, c_expr
208
%type <node>	row_expr
209 210
%type <str>		row_op
%type <ival>	sub_type
211 212
%type <list>	OptCreateAs, CreateAsList
%type <node>	CreateAsElement
213
%type <value>	NumericOnly, FloatOnly, IntegerOnly
214
%type <attr>	event_object, attr
215
%type <sortgroupby>		sortby
216 217 218 219
%type <ielem>	index_elem, func_index
%type <range>	from_val
%type <relexp>	relation_expr
%type <target>	res_target_el, res_target_el2
220
%type <paramno> ParamNo
221

222
%type <typnam>	Typename, opt_type, Array, Generic, Character, Datetime, Numeric
223
%type <str>		generic, numeric, character, datetime
224
%type <str>		extract_arg
225 226 227 228
%type <str>		opt_charset, opt_collate
%type <str>		opt_float, opt_numeric, opt_decimal
%type <boolean>	opt_varying, opt_timezone

229
%type <ival>	Iconst
230
%type <str>		Sconst
231
%type <str>		UserId, var_value, zone_value
232
%type <str>		ColId, ColLabel
233
%type <str>		TypeId
234

235
%type <node>	TableConstraint
236 237 238
%type <list>	constraint_list, constraint_expr
%type <list>	default_list, default_expr
%type <list>	ColQualList, ColQualifier
239
%type <node>	ColConstraint, ColConstraintElem
240 241
%type <list>	key_actions, key_action
%type <str>		key_match, key_reference
242 243 244

/*
 * If you make any token changes, remember to:
245 246
 *		- use "yacc -d" and update parse.h
 *		- update the keyword table in parser/keywords.c
247 248
 */

249 250 251 252 253 254 255 256 257 258
/* Reserved word tokens
 * SQL92 syntax has many type-specific constructs.
 * So, go ahead and make these types reserved words,
 *  and call-out the syntax explicitly.
 * This gets annoying when trying to also retain Postgres' nice
 *  type-extensible features, but we don't really have a choice.
 * - thomas 1997-10-11
 */

/* Keywords (in SQL92 reserved words) */
259
%token	ACTION, ADD, ALL, ALTER, AND, ANY AS, ASC,
260
		BEGIN_TRANS, BETWEEN, BOTH, BY,
261 262 263
		CASCADE, CAST, CHAR, CHARACTER, CHECK, CLOSE, COLLATE, COLUMN, COMMIT, 
		CONSTRAINT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME, 
		CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
264 265 266 267 268 269
		DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
		END_TRANS, EXECUTE, EXISTS, EXTRACT,
		FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
		GRANT, GROUP, HAVING, HOUR_P,
		IN, INNER_P, INSERT, INTERVAL, INTO, IS,
		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
M
 
Marc G. Fournier 已提交
270
		MATCH, MINUTE_P, MONTH_P, NAMES,
271
		NATIONAL, NATURAL, NCHAR, NO, NOT, NOTIFY, NULL_P, NUMERIC,
272
		ON, OPTION, OR, ORDER, OUTER_P,
273
		PARTIAL, POSITION, PRECISION, PRIMARY, PRIVILEGES, PROCEDURE, PUBLIC,
274 275
		REFERENCES, REVOKE, RIGHT, ROLLBACK,
		SECOND_P, SELECT, SET, SUBSTRING,
276 277 278
		TABLE, TIME, TIMESTAMP, TIMEZONE_HOUR, TIMEZONE_MINUTE,
		TO, TRAILING, TRANSACTION, TRIM,
		UNION, UNIQUE, UPDATE, USER, USING,
279
		VALUES, VARCHAR, VARYING, VIEW,
280 281 282 283 284 285 286 287 288
		WHERE, WITH, WORK, YEAR_P, ZONE

/* Keywords (in SQL3 reserved words) */
%token	FALSE_P, TRIGGER, TRUE_P

/* Keywords (in SQL92 non-reserved words) */
%token	TYPE_P

/* Keywords for Postgres support (not in SQL92 reserved words) */
289
%token	ABORT_TRANS, AFTER, AGGREGATE, ANALYZE,
290
		BACKWARD, BEFORE, BINARY, CACHE, CLUSTER, COPY, CYCLE,
291
		DATABASE, DELIMITERS, DO, EACH, EXPLAIN, EXTEND,
B
Bruce Momjian 已提交
292
		FORWARD, FUNCTION, HANDLER,
293 294
		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
		LANCOMPILER, LISTEN, LOAD, LOCK_P, LOCATION, MAXVALUE, MINVALUE, MOVE,
295
		NEW, NONE, NOTHING, NOTNULL, OIDS, OPERATOR, PROCEDURAL,
296
		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
297
		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
298
		VACUUM, VERBOSE, VERSION, ENCODING, UNLISTEN
299

300
/* Keywords (obsolete; retain through next version for parser - thomas 1997-12-04) */
301 302
%token	ARCHIVE

303 304 305 306 307 308
/*
 * Tokens for pg_passwd support.  The CREATEDB and CREATEUSER tokens should go away
 * when some sort of pg_privileges relation is introduced.
 *
 *                                    Todd A. Brandys
 */
309
%token	PASSWORD, CREATEDB, NOCREATEDB, CREATEUSER, NOCREATEUSER, VALID, UNTIL
310

311
/* Special keywords, not in the query language - see the "lex" file */
312
%token <str>	IDENT, SCONST, Op
313 314 315
%token <ival>	ICONST, PARAM
%token <dval>	FCONST

316
/* these are not real. they are here so that they get generated as #define's*/
317
%token			OP
318 319

/* precedence */
320 321 322 323
%left		OR
%left		AND
%right		NOT
%right		'='
324
%nonassoc	'<' '>'
325 326 327
%nonassoc	LIKE
%nonassoc	BETWEEN
%nonassoc	IN
328
%nonassoc	Op				/* multi-character ops and user-defined operators */
329 330 331 332 333 334 335 336 337 338 339 340 341 342
%nonassoc	NOTNULL
%nonassoc	ISNULL
%nonassoc	IS
%left		'+' '-'
%left		'*' '/'
%left		'|'				/* this is the relation union op, not logical or */
/* Unary Operators */
%right		':'
%left		';'				/* end of statement or natural log */
%right		UMINUS
%left		'.'
%left		'[' ']'
%nonassoc	TYPECAST
%left		UNION
343 344
%%

345
stmtblock:  stmtmulti
346
				{ parsetree = $1; }
347
		| stmt
348 349
				{ parsetree = lcons($1,NIL); }
		;
350

351
stmtmulti:  stmtmulti stmt ';'
352
				{ $$ = lappend($1, $2); }
353
		| stmtmulti stmt
354
				{ $$ = lappend($1, $2); }
355
		| stmt ';'
356 357
				{ $$ = lcons($1,NIL); }
		;
358 359

stmt :	  AddAttrStmt
360
		| AlterUserStmt
361 362 363
		| ClosePortalStmt
		| CopyStmt
		| CreateStmt
364
		| CreateAsStmt
365
		| CreateSeqStmt
366
		| CreatePLangStmt
367
		| CreateTrigStmt
368
		| CreateUserStmt
369 370 371
		| ClusterStmt
		| DefineStmt
		| DestroyStmt
372
		| DropPLangStmt
373
		| DropTrigStmt
374
		| DropUserStmt
375 376 377 378 379 380
		| ExtendStmt
		| ExplainStmt
		| FetchStmt
		| GrantStmt
		| IndexStmt
		| ListenStmt
M
 
Marc G. Fournier 已提交
381
		| UnlistenStmt
382
		| LockStmt
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		| ProcedureStmt
		| RecipeStmt
		| RemoveAggrStmt
		| RemoveOperStmt
		| RemoveFuncStmt
		| RemoveStmt
		| RenameStmt
		| RevokeStmt
		| OptimizableStmt
		| RuleStmt
		| TransactionStmt
		| ViewStmt
		| LoadStmt
		| CreatedbStmt
		| DestroydbStmt
		| VacuumStmt
		| VariableSetStmt
		| VariableShowStmt
		| VariableResetStmt
		;
403

404 405
/*****************************************************************************
 *
406
 * Create a new Postgres DBMS user
407 408 409 410
 *
 *
 *****************************************************************************/

411
CreateUserStmt:  CREATE USER UserId user_passwd_clause user_createdb_clause
412 413 414 415 416 417 418 419 420 421 422 423
			user_createuser_clause user_group_clause user_valid_clause
				{
					CreateUserStmt *n = makeNode(CreateUserStmt);
					n->user = $3;
					n->password = $4;
					n->createdb = $5;
					n->createuser = $6;
					n->groupElts = $7;
					n->validUntil = $8;
					$$ = (Node *)n;
				}
		;
424 425 426 427 428 429 430 431

/*****************************************************************************
 *
 * Alter a postresql DBMS user
 *
 *
 *****************************************************************************/

432
AlterUserStmt:  ALTER USER UserId user_passwd_clause user_createdb_clause
433 434 435 436 437 438 439 440 441 442 443 444
			user_createuser_clause user_group_clause user_valid_clause
				{
					AlterUserStmt *n = makeNode(AlterUserStmt);
					n->user = $3;
					n->password = $4;
					n->createdb = $5;
					n->createuser = $6;
					n->groupElts = $7;
					n->validUntil = $8;
					$$ = (Node *)n;
				}
		;
445 446 447 448 449 450 451 452

/*****************************************************************************
 *
 * Drop a postresql DBMS user
 *
 *
 *****************************************************************************/

453
DropUserStmt:  DROP USER UserId
454 455 456 457 458 459 460
				{
					DropUserStmt *n = makeNode(DropUserStmt);
					n->user = $3;
					$$ = (Node *)n;
				}
		;

461
user_passwd_clause:  WITH PASSWORD UserId		{ $$ = $3; }
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
			| /*EMPTY*/							{ $$ = NULL; }
		;

user_createdb_clause:  CREATEDB
				{
					bool*  b;
					$$ = (b = (bool*)palloc(sizeof(bool)));
					*b = true;
				}
			| NOCREATEDB
				{
					bool*  b;
					$$ = (b = (bool*)palloc(sizeof(bool)));
					*b = false;
				}
			| /*EMPTY*/							{ $$ = NULL; }
		;

user_createuser_clause:  CREATEUSER
				{
					bool*  b;
					$$ = (b = (bool*)palloc(sizeof(bool)));
					*b = true;
				}
			| NOCREATEUSER
				{
					bool*  b;
					$$ = (b = (bool*)palloc(sizeof(bool)));
					*b = false;
				}
			| /*EMPTY*/							{ $$ = NULL; }
		;

495
user_group_list:  user_group_list ',' UserId
496 497 498
				{
					$$ = lcons((void*)makeString($3), $1);
				}
499
			| UserId
500 501 502 503 504 505 506 507 508 509 510 511
				{
					$$ = lcons((void*)makeString($1), NIL);
				}
		;

user_group_clause:  IN GROUP user_group_list	{ $$ = $3; }
			| /*EMPTY*/							{ $$ = NULL; }
		;

user_valid_clause:  VALID UNTIL SCONST			{ $$ = $3; }
			| /*EMPTY*/							{ $$ = NULL; }
		;
512

513
/*****************************************************************************
514
 *
515
 * Set PG internal variable
516
 *	  SET name TO 'var_value'
517 518
 * Include SQL92 syntax (thomas 1997-10-22):
 *    SET TIME ZONE 'var_value'
519
 *
520 521
 *****************************************************************************/

522
VariableSetStmt:  SET ColId TO var_value
523 524 525 526 527 528
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
529
		| SET ColId '=' var_value
530 531 532 533 534 535
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
536
		| SET TIME ZONE zone_value
537 538 539 540 541 542
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "timezone";
					n->value = $4;
					$$ = (Node *) n;
				}
M
 
Marc G. Fournier 已提交
543 544 545 546 547 548 549 550 551 552 553
		| SET NAMES encoding
				{
#ifdef MB
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "client_encoding";
					n->value = $3;
					$$ = (Node *) n;
#else
					elog(ERROR, "SET NAMES is not supported");
#endif
				}
554 555
		;

556
var_value:  Sconst			{ $$ = $1; }
557
		| DEFAULT			{ $$ = NULL; }
558 559
		;

560
zone_value:  Sconst			{ $$ = $1; }
561
		| DEFAULT			{ $$ = NULL; }
562
		| LOCAL				{ $$ = NULL; }
563
		;
564

565
VariableShowStmt:  SHOW ColId
566 567 568 569 570
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
571 572 573 574 575 576
		| SHOW TIME ZONE
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
577 578
		;

579
VariableResetStmt:	RESET ColId
580 581 582 583 584
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
585 586 587 588 589 590
		| RESET TIME ZONE
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
591
		;
592

593

594 595
/*****************************************************************************
 *
596 597
 *		QUERY :
 *				addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
598 599 600
 *
 *****************************************************************************/

601
AddAttrStmt:  ALTER TABLE relation_name opt_inh_star alter_clause
602 603 604 605
				{
					AddAttrStmt *n = makeNode(AddAttrStmt);
					n->relname = $3;
					n->inh = $4;
606
					n->colDef = $5;
607 608 609
					$$ = (Node *)n;
				}
		;
610

611 612 613 614
alter_clause:  ADD opt_column columnDef
				{
					$$ = $3;
				}
615
			| ADD '(' OptTableElementList ')'
616
				{
617
					Node *lp = lfirst($3);
618 619

					if (length($3) != 1)
B
Bruce Momjian 已提交
620
						elog(ERROR,"ALTER TABLE/ADD() allows one column only");
621 622
					$$ = lp;
				}
623
			| DROP opt_column ColId
B
Bruce Momjian 已提交
624
				{	elog(ERROR,"ALTER TABLE/DROP COLUMN not yet implemented"); }
625
			| ALTER opt_column ColId SET DEFAULT default_expr
B
Bruce Momjian 已提交
626
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented"); }
627
			| ALTER opt_column ColId DROP DEFAULT
B
Bruce Momjian 已提交
628
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented"); }
629
			| ADD ConstraintElem
B
Bruce Momjian 已提交
630
				{	elog(ERROR,"ALTER TABLE/ADD CONSTRAINT not yet implemented"); }
631 632
		;

633 634

/*****************************************************************************
635
 *
636 637
 *		QUERY :
 *				close <optname>
638
 *
639 640
 *****************************************************************************/

641
ClosePortalStmt:  CLOSE opt_id
642 643 644 645 646 647
				{
					ClosePortalStmt *n = makeNode(ClosePortalStmt);
					n->portalname = $2;
					$$ = (Node *)n;
				}
		;
648 649 650 651


/*****************************************************************************
 *
652 653 654
 *		QUERY :
 *				COPY [BINARY] <relname> FROM/TO
 *				[USING DELIMITERS <delimiter>]
655 656 657
 *
 *****************************************************************************/

658
CopyStmt:  COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
659 660 661 662 663 664 665 666 667 668 669 670 671 672
				{
					CopyStmt *n = makeNode(CopyStmt);
					n->binary = $2;
					n->relname = $3;
					n->oids = $4;
					n->direction = $5;
					n->filename = $6;
					n->delimiter = $7;
					$$ = (Node *)n;
				}
		;

copy_dirn:	TO
				{ $$ = TO; }
673
		| FROM
674 675
				{ $$ = FROM; }
		;
676

677
/*
678 679
 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
 * used depends on the direction. (It really doesn't make sense to copy from
680
 * stdout. We silently correct the "typo".		 - AY 9/94
681
 */
682 683 684 685
copy_file_name:  Sconst							{ $$ = $1; }
		| STDIN									{ $$ = NULL; }
		| STDOUT								{ $$ = NULL; }
		;
686

687 688
opt_binary:  BINARY								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
689
		;
690

691
opt_with_copy:	WITH OIDS						{ $$ = TRUE; }
692
		| /*EMPTY*/								{ $$ = FALSE; }
693
		;
694

695 696 697
/*
 * the default copy delimiter is tab but the user can configure it
 */
698 699
copy_delimiter:  USING DELIMITERS Sconst		{ $$ = $3; }
		| /*EMPTY*/								{ $$ = "\t"; }
700
		;
701 702 703 704


/*****************************************************************************
 *
705 706
 *		QUERY :
 *				CREATE relname
707 708 709 710
 *
 *****************************************************************************/

CreateStmt:  CREATE TABLE relation_name '(' OptTableElementList ')'
711
				OptInherit OptArchiveType
712 713 714 715 716
				{
					CreateStmt *n = makeNode(CreateStmt);
					n->relname = $3;
					n->tableElts = $5;
					n->inhRelnames = $7;
717
					n->constraints = NIL;
718 719 720 721
					$$ = (Node *)n;
				}
		;

722
OptTableElementList:  OptTableElementList ',' OptTableElement
723 724 725 726 727 728 729 730 731 732 733 734 735
				{
					if ($3 != NULL)
						$$ = lappend($1, $3);
					else
						$$ = $1;
				}
			| OptTableElement
				{
					if ($1 != NULL)
						$$ = lcons($1, NIL);
					else
						$$ = NULL;
				}
736
			| /*EMPTY*/							{ $$ = NULL; }
737
		;
738

739 740
OptTableElement:  columnDef						{ $$ = $1; }
			| TableConstraint					{ $$ = $1; }
741
		;
742

743
columnDef:  ColId Typename ColQualifier
744 745 746 747 748 749 750
				{
					ColumnDef *n = makeNode(ColumnDef);
					n->colname = $1;
					n->typename = $2;
					n->defval = NULL;
					n->is_not_null = FALSE;
					n->constraints = $3;
751 752 753 754 755 756 757 758 759 760 761 762 763
					$$ = (Node *)n;
				}
			| ColId SERIAL
				{
					ColumnDef *n = makeNode(ColumnDef);
					n->colname = $1;
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("integer");
					n->defval = NULL;
					n->is_not_null = TRUE;
					n->is_sequence = TRUE;
					n->constraints = NULL;

764 765 766 767
					$$ = (Node *)n;
				}
		;

768
ColQualifier:  ColQualList						{ $$ = $1; }
769
			| /*EMPTY*/							{ $$ = NULL; }
770
		;
771

772 773 774 775 776 777 778 779 780 781 782 783 784 785
ColQualList:  ColQualList ColConstraint
				{
					if ($2 != NULL)
						$$ = lappend($1, $2);
					else
						$$ = $1;
				}
			| ColConstraint
				{
					if ($1 != NULL)
						$$ = lcons($1, NIL);
					else
						$$ = NULL;
				}
786 787
		;

788 789 790 791
ColConstraint:
		CONSTRAINT name ColConstraintElem
				{
						Constraint *n = (Constraint *)$3;
792
						if (n != NULL) n->name = fmtId($2);
793 794 795 796
						$$ = $3;
				}
		| ColConstraintElem
				{ $$ = $1; }
797
		;
798

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
ColConstraintElem:  CHECK '(' constraint_expr ')'
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| DEFAULT default_expr
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_DEFAULT;
					n->name = NULL;
					n->def = FlattenStringList($2);
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| NOT NULL_P
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_NOTNULL;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| UNIQUE
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_UNIQUE;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| PRIMARY KEY
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| REFERENCES ColId opt_column_list key_match key_actions
				{
B
Bruce Momjian 已提交
846
					elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
847 848
					$$ = NULL;
				}
849
		;
V
Vadim B. Mikheev 已提交
850

851 852 853 854 855 856 857 858 859 860 861
default_list:  default_list ',' default_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| default_expr
				{
					$$ = $1;
				}
		;

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
default_expr:  AexprConst
				{	$$ = makeConstantList((A_Const *) $1); }
			| NULL_P
				{	$$ = lcons( makeString("NULL"), NIL); }
			| '-' default_expr %prec UMINUS
				{	$$ = lcons( makeString( "-"), $2); }
			| default_expr '+' default_expr
				{	$$ = nconc( $1, lcons( makeString( "+"), $3)); }
			| default_expr '-' default_expr
				{	$$ = nconc( $1, lcons( makeString( "-"), $3)); }
			| default_expr '/' default_expr
				{	$$ = nconc( $1, lcons( makeString( "/"), $3)); }
			| default_expr '*' default_expr
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
			| default_expr '=' default_expr
B
Bruce Momjian 已提交
877
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
878
			| default_expr '<' default_expr
B
Bruce Momjian 已提交
879
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
880
			| default_expr '>' default_expr
B
Bruce Momjian 已提交
881
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
882 883 884 885 886 887 888 889 890 891 892
			| ':' default_expr
				{	$$ = lcons( makeString( ":"), $2); }
			| ';' default_expr
				{	$$ = lcons( makeString( ";"), $2); }
			| '|' default_expr
				{	$$ = lcons( makeString( "|"), $2); }
			| default_expr TYPECAST Typename
				{
					$3->name = fmtId($3->name);
					$$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
				}
893
			| CAST '(' default_expr AS Typename ')'
894
				{
895 896
					$5->name = fmtId($5->name);
					$$ = nconc( lcons( makeString( "CAST"), $3), makeList( makeString("AS"), $5, -1));
897 898 899
				}
			| '(' default_expr ')'
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
900
			| func_name '(' ')'
901 902 903 904
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
905
			| func_name '(' default_list ')'
906 907
				{
					$$ = makeList( makeString($1), makeString("("), -1);
908
					$$ = nconc( $$, $3);
909 910 911 912 913
					$$ = lappend( $$, makeString(")"));
				}
			| default_expr Op default_expr
				{
					if (!strcmp("<=", $2) || !strcmp(">=", $2))
B
Bruce Momjian 已提交
914
						elog(ERROR,"boolean expressions not supported in DEFAULT");
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
					$$ = nconc( $1, lcons( makeString( $2), $3));
				}
			| Op default_expr
				{	$$ = lcons( makeString( $1), $2); }
			| default_expr Op
				{	$$ = lappend( $1, makeString( $2)); }
			/* XXX - thomas 1997-10-07 v6.2 function-specific code to be changed */
			| CURRENT_DATE
				{	$$ = lcons( makeString( "date( 'current'::datetime + '0 sec')"), NIL); }
			| CURRENT_TIME
				{	$$ = lcons( makeString( "'now'::time"), NIL); }
			| CURRENT_TIME '(' Iconst ')'
				{
					if ($3 != 0)
						elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
					$$ = lcons( makeString( "'now'::time"), NIL);
				}
			| CURRENT_TIMESTAMP
				{	$$ = lcons( makeString( "now()"), NIL); }
			| CURRENT_TIMESTAMP '(' Iconst ')'
				{
					if ($3 != 0)
						elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
					$$ = lcons( makeString( "now()"), NIL);
				}
			| CURRENT_USER
				{	$$ = lcons( makeString( "CURRENT_USER"), NIL); }
942 943
			| USER
				{	$$ = lcons( makeString( "USER"), NIL); }
944 945
		;

946 947 948 949 950
/* ConstraintElem specifies constraint syntax which is not embedded into
 *  a column definition. ColConstraintElem specifies the embedded form.
 * - thomas 1997-12-03
 */
TableConstraint:  CONSTRAINT name ConstraintElem
951
				{
952
						Constraint *n = (Constraint *)$3;
953
						if (n != NULL) n->name = fmtId($2);
954 955
						$$ = $3;
				}
956 957
		| ConstraintElem
				{ $$ = $1; }
958 959
		;

960
ConstraintElem:  CHECK '(' constraint_expr ')'
961
				{
962 963 964 965 966
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					$$ = (Node *)n;
967
				}
968
		| UNIQUE '(' columnList ')'
969 970 971 972 973 974 975 976
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_UNIQUE;
					n->name = NULL;
					n->def = NULL;
					n->keys = $3;
					$$ = (Node *)n;
				}
977
		| PRIMARY KEY '(' columnList ')'
978 979 980 981 982 983 984 985
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = $4;
					$$ = (Node *)n;
				}
986
		| FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
987 988 989 990
				{
					elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
					$$ = NULL;
				}
991
		;
992

993 994 995 996 997 998 999 1000 1001 1002 1003
constraint_list:  constraint_list ',' constraint_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| constraint_expr
				{
					$$ = $1;
				}
		;

1004
constraint_expr:  AexprConst
1005
				{	$$ = makeConstantList((A_Const *) $1); }
1006
			| NULL_P
1007
				{	$$ = lcons( makeString("NULL"), NIL); }
1008
			| ColId
1009
				{
1010
					$$ = lcons( makeString(fmtId($1)), NIL);
1011
				}
1012
			| '-' constraint_expr %prec UMINUS
1013
				{	$$ = lcons( makeString( "-"), $2); }
1014
			| constraint_expr '+' constraint_expr
1015
				{	$$ = nconc( $1, lcons( makeString( "+"), $3)); }
1016
			| constraint_expr '-' constraint_expr
1017
				{	$$ = nconc( $1, lcons( makeString( "-"), $3)); }
1018
			| constraint_expr '/' constraint_expr
1019
				{	$$ = nconc( $1, lcons( makeString( "/"), $3)); }
1020
			| constraint_expr '*' constraint_expr
1021
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
1022
			| constraint_expr '=' constraint_expr
1023
				{	$$ = nconc( $1, lcons( makeString( "="), $3)); }
1024
			| constraint_expr '<' constraint_expr
1025
				{	$$ = nconc( $1, lcons( makeString( "<"), $3)); }
1026
			| constraint_expr '>' constraint_expr
1027
				{	$$ = nconc( $1, lcons( makeString( ">"), $3)); }
1028
			| ':' constraint_expr
1029
				{	$$ = lcons( makeString( ":"), $2); }
1030
			| ';' constraint_expr
1031
				{	$$ = lcons( makeString( ";"), $2); }
1032
			| '|' constraint_expr
1033
				{	$$ = lcons( makeString( "|"), $2); }
1034
			| constraint_expr TYPECAST Typename
1035
				{
1036 1037
					$3->name = fmtId($3->name);
					$$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
1038
				}
1039
			| CAST '(' constraint_expr AS Typename ')'
1040
				{
1041 1042
					$5->name = fmtId($5->name);
					$$ = nconc( lcons( makeString( "CAST"), $3), makeList( makeString("AS"), $5, -1));
1043
				}
1044
			| '(' constraint_expr ')'
1045
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
1046
			| func_name '(' ')'
1047 1048 1049 1050
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
1051
			| func_name '(' constraint_list ')'
1052 1053 1054 1055 1056
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = nconc( $$, $3);
					$$ = lappend( $$, makeString(")"));
				}
1057
			| constraint_expr Op constraint_expr
1058
				{	$$ = nconc( $1, lcons( makeString( $2), $3)); }
1059
			| constraint_expr LIKE constraint_expr
1060 1061 1062
				{	$$ = nconc( $1, lcons( makeString( "LIKE"), $3)); }
			| constraint_expr NOT LIKE constraint_expr
				{	$$ = nconc( $1, lcons( makeString( "NOT LIKE"), $4)); }
1063
			| constraint_expr AND constraint_expr
1064
				{	$$ = nconc( $1, lcons( makeString( "AND"), $3)); }
1065
			| constraint_expr OR constraint_expr
1066
				{	$$ = nconc( $1, lcons( makeString( "OR"), $3)); }
1067 1068
			| NOT constraint_expr
				{	$$ = lcons( makeString( "NOT"), $2); }
1069
			| Op constraint_expr
1070
				{	$$ = lcons( makeString( $1), $2); }
1071
			| constraint_expr Op
1072
				{	$$ = lappend( $1, makeString( $2)); }
1073 1074 1075 1076 1077 1078 1079 1080
			| constraint_expr ISNULL
				{	$$ = lappend( $1, makeString( "IS NULL")); }
			| constraint_expr IS NULL_P
				{	$$ = lappend( $1, makeString( "IS NULL")); }
			| constraint_expr NOTNULL
				{	$$ = lappend( $1, makeString( "IS NOT NULL")); }
			| constraint_expr IS NOT NULL_P
				{	$$ = lappend( $1, makeString( "IS NOT NULL")); }
1081
			| constraint_expr IS TRUE_P
1082
				{	$$ = lappend( $1, makeString( "IS TRUE")); }
1083
			| constraint_expr IS FALSE_P
1084
				{	$$ = lappend( $1, makeString( "IS FALSE")); }
1085
			| constraint_expr IS NOT TRUE_P
1086
				{	$$ = lappend( $1, makeString( "IS NOT TRUE")); }
1087
			| constraint_expr IS NOT FALSE_P
1088
				{	$$ = lappend( $1, makeString( "IS NOT FALSE")); }
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
			| constraint_expr IN '(' c_list ')'
				{
					$$ = lappend( $1, makeString("IN"));
					$$ = lappend( $$, makeString("("));
					$$ = nconc( $$, $4);
					$$ = lappend( $$, makeString(")"));
				}
			| constraint_expr NOT IN '(' c_list ')'
				{
					$$ = lappend( $1, makeString("NOT IN"));
					$$ = lappend( $$, makeString("("));
					$$ = nconc( $$, $5);
					$$ = lappend( $$, makeString(")"));
				}
			| constraint_expr BETWEEN c_expr AND c_expr
				{
					$$ = lappend( $1, makeString("BETWEEN"));
					$$ = nconc( $$, $3);
					$$ = lappend( $$, makeString("AND"));
					$$ = nconc( $$, $5);
				}
			| constraint_expr NOT BETWEEN c_expr AND c_expr
				{
					$$ = lappend( $1, makeString("NOT BETWEEN"));
					$$ = nconc( $$, $4);
					$$ = lappend( $$, makeString("AND"));
					$$ = nconc( $$, $6);
				}
		;

c_list:  c_list ',' c_expr
				{
					$$ = lappend($1, makeString(","));
					$$ = nconc($$, $3);
				}
			| c_expr
				{
					$$ = $1;
				}
		;

c_expr:  AexprConst
				{	$$ = makeConstantList((A_Const *) $1); }
1132 1133
		;

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
key_match:  MATCH FULL					{ $$ = NULL; }
		| MATCH PARTIAL					{ $$ = NULL; }
		| /*EMPTY*/						{ $$ = NULL; }
		;

key_actions:  key_action key_action		{ $$ = NIL; }
		| key_action					{ $$ = NIL; }
		| /*EMPTY*/						{ $$ = NIL; }
		;

key_action:  ON DELETE key_reference	{ $$ = NIL; }
		| ON UPDATE key_reference		{ $$ = NIL; }
		;

key_reference:  NO ACTION				{ $$ = NULL; }
		| CASCADE						{ $$ = NULL; }
		| SET DEFAULT					{ $$ = NULL; }
		| SET NULL_P					{ $$ = NULL; }
		;

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
OptInherit:  INHERITS '(' relation_name_list ')'		{ $$ = $3; }
		| /*EMPTY*/										{ $$ = NIL; }
		;

/*
 *	"ARCHIVE" keyword was removed in 6.3, but we keep it for now
 *  so people can upgrade with old pg_dump scripts. - momjian 1997-11-20(?)
 */
OptArchiveType:  ARCHIVE '=' NONE						{ }
		| /*EMPTY*/										{ }
		;

1166 1167
CreateAsStmt:  CREATE TABLE relation_name OptCreateAs AS SubSelect
				{
B
Bruce Momjian 已提交
1168
					SelectStmt *n = (SelectStmt *)$6;
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
					if ($4 != NIL)
						mapTargetColumns($4, n->targetList);
					n->into = $3;
					$$ = (Node *)n;
				}
		;

OptCreateAs:  '(' CreateAsList ')'				{ $$ = $2; }
			| /*EMPTY*/							{ $$ = NULL; }
		;

CreateAsList:  CreateAsList ',' CreateAsElement	{ $$ = lappend($1, $3); }
			| CreateAsElement					{ $$ = lcons($1, NIL); }
		;

CreateAsElement:  ColId
				{
					ColumnDef *n = makeNode(ColumnDef);
					n->colname = $1;
					n->typename = NULL;
					n->defval = NULL;
					n->is_not_null = FALSE;
					n->constraints = NULL;
					$$ = (Node *)n;
				}
		;

1196

V
Vadim B. Mikheev 已提交
1197 1198
/*****************************************************************************
 *
1199 1200
 *		QUERY :
 *				CREATE SEQUENCE seqname
V
Vadim B. Mikheev 已提交
1201 1202 1203
 *
 *****************************************************************************/

1204
CreateSeqStmt:  CREATE SEQUENCE relation_name OptSeqList
1205 1206 1207 1208 1209 1210 1211
				{
					CreateSeqStmt *n = makeNode(CreateSeqStmt);
					n->seqname = $3;
					n->options = $4;
					$$ = (Node *)n;
				}
		;
V
Vadim B. Mikheev 已提交
1212

1213
OptSeqList:  OptSeqList OptSeqElem
1214
				{ $$ = lappend($1, $2); }
1215
			|	{ $$ = NIL; }
1216 1217
		;

1218
OptSeqElem:  CACHE IntegerOnly
1219 1220
				{
					$$ = makeNode(DefElem);
1221
					$$->defname = "cache";
1222 1223
					$$->arg = (Node *)$2;
				}
1224
			| CYCLE
1225 1226
				{
					$$ = makeNode(DefElem);
1227
					$$->defname = "cycle";
1228 1229
					$$->arg = (Node *)NULL;
				}
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
			| INCREMENT IntegerOnly
				{
					$$ = makeNode(DefElem);
					$$->defname = "increment";
					$$->arg = (Node *)$2;
				}
			| MAXVALUE IntegerOnly
				{
					$$ = makeNode(DefElem);
					$$->defname = "maxvalue";
					$$->arg = (Node *)$2;
				}
			| MINVALUE IntegerOnly
				{
					$$ = makeNode(DefElem);
					$$->defname = "minvalue";
					$$->arg = (Node *)$2;
				}
			| START IntegerOnly
				{
					$$ = makeNode(DefElem);
					$$->defname = "start";
					$$->arg = (Node *)$2;
				}
		;

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
NumericOnly:  FloatOnly					{ $$ = $1; }
			| IntegerOnly				{ $$ = $1; }

FloatOnly:  FCONST
				{
					$$ = makeFloat($1);
				}
			| '-' FCONST
				{
					$$ = makeFloat($2);
					$$->val.dval = - $$->val.dval;
				}
		;

1270 1271 1272 1273 1274 1275 1276 1277 1278
IntegerOnly:  Iconst
				{
					$$ = makeInteger($1);
				}
			| '-' Iconst
				{
					$$ = makeInteger($2);
					$$->val.ival = - $$->val.ival;
				}
1279
		;
V
Vadim B. Mikheev 已提交
1280

1281 1282 1283 1284 1285 1286 1287 1288
/*****************************************************************************
 *
 *		QUERIES :
 *				CREATE PROCEDURAL LANGUAGE ...
 *				DROP PROCEDURAL LANGUAGE ...
 *
 *****************************************************************************/

1289
CreatePLangStmt:  CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst 
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
			HANDLER def_name LANCOMPILER Sconst
			{
				CreatePLangStmt *n = makeNode(CreatePLangStmt);
				n->plname = $5;
				n->plhandler = $7;
				n->plcompiler = $9;
				n->pltrusted = $2;
				$$ = (Node *)n;
			}
		;

PLangTrusted:		TRUSTED { $$ = TRUE; }
			|	{ $$ = FALSE; }

1304
DropPLangStmt:  DROP PROCEDURAL LANGUAGE Sconst
1305 1306 1307 1308 1309 1310
			{
				DropPLangStmt *n = makeNode(DropPLangStmt);
				n->plname = $4;
				$$ = (Node *)n;
			}
		;
V
Vadim B. Mikheev 已提交
1311

V
Vadim B. Mikheev 已提交
1312 1313
/*****************************************************************************
 *
1314 1315 1316
 *		QUERIES :
 *				CREATE TRIGGER ...
 *				DROP TRIGGER ...
V
Vadim B. Mikheev 已提交
1317 1318 1319
 *
 *****************************************************************************/

1320
CreateTrigStmt:  CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
				relation_name TriggerForSpec EXECUTE PROCEDURE
				name '(' TriggerFuncArgs ')'
				{
					CreateTrigStmt *n = makeNode(CreateTrigStmt);
					n->trigname = $3;
					n->relname = $7;
					n->funcname = $11;
					n->args = $13;
					n->before = $4;
					n->row = $8;
					memcpy (n->actions, $5, 4);
					$$ = (Node *)n;
				}
		;

1336 1337
TriggerActionTime:  BEFORE						{ $$ = TRUE; }
			| AFTER								{ $$ = FALSE; }
1338 1339 1340
		;

TriggerEvents:	TriggerOneEvent
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
				{
					char *e = palloc (4);
					e[0] = $1; e[1] = 0; $$ = e;
				}
			| TriggerOneEvent OR TriggerOneEvent
				{
					char *e = palloc (4);
					e[0] = $1; e[1] = $3; e[2] = 0; $$ = e;
				}
			| TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
				{
					char *e = palloc (4);
					e[0] = $1; e[1] = $3; e[2] = $5; e[3] = 0;
					$$ = e;
				}
1356 1357
		;

1358 1359 1360
TriggerOneEvent:  INSERT					{ $$ = 'i'; }
			| DELETE						{ $$ = 'd'; }
			| UPDATE						{ $$ = 'u'; }
1361 1362
		;

1363
TriggerForSpec:  FOR TriggerForOpt TriggerForType
1364
				{
1365
					$$ = $3;
1366 1367
				}
		;
V
Vadim B. Mikheev 已提交
1368

1369 1370 1371 1372 1373 1374 1375 1376
TriggerForOpt:  EACH						{ $$ = TRUE; }
			| /*EMPTY*/						{ $$ = FALSE; }
		;

TriggerForType:  ROW						{ $$ = TRUE; }
			| STATEMENT						{ $$ = FALSE; }
		;

1377
TriggerFuncArgs:  TriggerFuncArg
1378
				{ $$ = lcons($1, NIL); }
1379
			| TriggerFuncArgs ',' TriggerFuncArg
1380
				{ $$ = lappend($1, $3); }
1381
			| /*EMPTY*/
1382
				{ $$ = NIL; }
1383 1384
		;

1385
TriggerFuncArg:  ICONST
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
				{
					char *s = (char *) palloc (256);
					sprintf (s, "%d", $1);
					$$ = s;
				}
			| FCONST
				{
					char *s = (char *) palloc (256);
					sprintf (s, "%g", $1);
					$$ = s;
				}
			| Sconst						{  $$ = $1; }
			| IDENT							{  $$ = $1; }
1399
		;
V
Vadim B. Mikheev 已提交
1400

1401
DropTrigStmt:  DROP TRIGGER name ON relation_name
1402 1403 1404 1405 1406 1407 1408
				{
					DropTrigStmt *n = makeNode(DropTrigStmt);
					n->trigname = $3;
					n->relname = $5;
					$$ = (Node *) n;
				}
		;
V
Vadim B. Mikheev 已提交
1409

1410

1411 1412
/*****************************************************************************
 *
1413 1414
 *		QUERY :
 *				define (type,operator,aggregate)
1415 1416 1417 1418
 *
 *****************************************************************************/

DefineStmt:  CREATE def_type def_rest
1419 1420 1421 1422 1423
				{
					$3->defType = $2;
					$$ = (Node *)$3;
				}
		;
1424

1425
def_rest:  def_name definition
1426 1427 1428 1429 1430 1431 1432
				{
					$$ = makeNode(DefineStmt);
					$$->defname = $1;
					$$->definition = $2;
				}
		;

1433
def_type:  OPERATOR							{ $$ = OPERATOR; }
1434
		| TYPE_P							{ $$ = TYPE_P; }
1435
		| AGGREGATE							{ $$ = AGGREGATE; }
1436
		;
1437

1438
def_name:  PROCEDURE						{ $$ = "procedure"; }
1439 1440 1441 1442
		| JOIN								{ $$ = "join"; }
		| ColId								{ $$ = $1; }
		| MathOp							{ $$ = $1; }
		| Op								{ $$ = $1; }
1443
		;
1444

1445
definition:  '(' def_list ')'				{ $$ = $2; }
1446
		;
1447

1448
def_list:  def_elem							{ $$ = lcons($1, NIL); }
1449
		| def_list ',' def_elem				{ $$ = lappend($1, $3); }
1450
		;
1451 1452

def_elem:  def_name '=' def_arg
1453 1454 1455 1456 1457
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)$3;
				}
1458
		| def_name
1459 1460 1461 1462 1463
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)NULL;
				}
1464
		| DEFAULT '=' def_arg
1465 1466
				{
					$$ = makeNode(DefElem);
1467
					$$->defname = "default";
1468 1469 1470 1471
					$$->arg = (Node *)$3;
				}
		;

1472 1473
def_arg:  ColId							{  $$ = (Node *)makeString($1); }
		| all_Op						{  $$ = (Node *)makeString($1); }
1474
		| NumericOnly					{  $$ = (Node *)$1; }
1475 1476
		| Sconst						{  $$ = (Node *)makeString($1); }
		| SETOF ColId
1477 1478 1479 1480 1481
				{
					TypeName *n = makeNode(TypeName);
					n->name = $2;
					n->setof = TRUE;
					n->arrayBounds = NULL;
1482
					n->typmod = -1;
1483 1484
					$$ = (Node *)n;
				}
1485
		;
1486 1487 1488 1489


/*****************************************************************************
 *
1490 1491
 *		QUERY:
 *				destroy <relname1> [, <relname2> .. <relnameN> ]
1492 1493 1494
 *
 *****************************************************************************/

1495
DestroyStmt:  DROP TABLE relation_name_list
1496 1497 1498
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1499
					n->sequence = FALSE;
1500 1501
					$$ = (Node *)n;
				}
1502
		| DROP SEQUENCE relation_name_list
1503 1504 1505
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1506
					n->sequence = TRUE;
1507 1508 1509
					$$ = (Node *)n;
				}
		;
1510 1511 1512 1513


/*****************************************************************************
 *
1514
 *		QUERY:
V
Vadim B. Mikheev 已提交
1515
 *			fetch/move [forward | backward] [number | all ] [ in <portalname> ]
1516 1517 1518
 *
 *****************************************************************************/

1519 1520 1521 1522 1523 1524
FetchStmt:	FETCH opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
V
Vadim B. Mikheev 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533
					n->ismove = false;
					$$ = (Node *)n;
				}
		|	MOVE opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
1534
					n->ismove = TRUE;
1535 1536 1537 1538 1539 1540 1541 1542
					$$ = (Node *)n;
				}
		;

opt_direction:	FORWARD							{ $$ = FORWARD; }
		| BACKWARD								{ $$ = BACKWARD; }
		| /*EMPTY*/								{ $$ = FORWARD; /* default */ }
		;
1543

1544
fetch_how_many:  Iconst
1545
			   { $$ = $1;
B
Bruce Momjian 已提交
1546
				 if ($1 <= 0) elog(ERROR,"Please specify nonnegative count for fetch"); }
1547
		| ALL							{ $$ = 0; /* 0 means fetch all tuples*/ }
1548
		| /*EMPTY*/						{ $$ = 1; /*default*/ }
1549
		;
1550

1551
opt_portal_name:  IN name				{ $$ = $2; }
V
Vadim B. Mikheev 已提交
1552 1553 1554
		| /*EMPTY*/						{ $$ = NULL; }
		;

1555

1556 1557
/*****************************************************************************
 *
1558 1559
 *		QUERY:
 *				GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1560 1561 1562
 *
 *****************************************************************************/

1563
GrantStmt:  GRANT privileges ON relation_name_list TO grantee opt_with_grant
1564 1565 1566 1567
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'+');
				}
		;
1568 1569

privileges:  ALL PRIVILEGES
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| ALL
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| operation_commalist
				{
				 $$ = $1;
				}
		;

1583
operation_commalist:  operation
1584 1585 1586 1587 1588 1589 1590 1591 1592
				{
						$$ = aclmakepriv("",$1);
				}
		| operation_commalist ',' operation
				{
						$$ = aclmakepriv($1,$3);
				}
		;

1593
operation:  SELECT
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
				{
						$$ = ACL_MODE_RD_CHR;
				}
		| INSERT
				{
						$$ = ACL_MODE_AP_CHR;
				}
		| UPDATE
				{
						$$ = ACL_MODE_WR_CHR;
				}
		| DELETE
				{
						$$ = ACL_MODE_WR_CHR;
				}
1609
		| RULE
1610 1611 1612 1613 1614 1615 1616 1617 1618
				{
						$$ = ACL_MODE_RU_CHR;
				}
		;

grantee:  PUBLIC
				{
						$$ = aclmakeuser("A","");
				}
1619
		| GROUP ColId
1620 1621 1622
				{
						$$ = aclmakeuser("G",$2);
				}
1623
		| ColId
1624 1625 1626 1627
				{
						$$ = aclmakeuser("U",$1);
				}
		;
1628

1629
opt_with_grant:  WITH GRANT OPTION
1630 1631
				{
					yyerror("WITH GRANT OPTION is not supported.  Only relation owners can set privileges");
1632 1633
				 }
		| /*EMPTY*/
1634 1635
		;

1636

1637 1638
/*****************************************************************************
 *
1639 1640
 *		QUERY:
 *				REVOKE [privileges] ON [relation_name] FROM [user]
1641 1642 1643
 *
 *****************************************************************************/

1644
RevokeStmt:  REVOKE privileges ON relation_name_list FROM grantee
1645 1646 1647 1648
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'-');
				}
		;
1649

1650

1651 1652
/*****************************************************************************
 *
1653
 *		QUERY:
B
Bruce Momjian 已提交
1654
 *				create index <indexname> on <relname>
1655 1656
 *				  using <access> "(" (<col> with <op>)+ ")" [with
 *				  <target_list>]
1657
 *
1658
 *	[where <qual>] is not supported anymore
1659 1660
 *****************************************************************************/

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
IndexStmt:	CREATE index_opt_unique INDEX index_name ON relation_name
			access_method_clause '(' index_params ')' opt_with
				{
					/* should check that access_method is valid,
					   etc ... but doesn't */
					IndexStmt *n = makeNode(IndexStmt);
					n->unique = $2;
					n->idxname = $4;
					n->relname = $6;
					n->accessMethod = $7;
					n->indexParams = $9;
					n->withClause = $11;
					n->whereClause = NULL;
					$$ = (Node *)n;
				}
		;

1678 1679
index_opt_unique:  UNIQUE						{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
1680 1681
		;

1682 1683
access_method_clause:  USING access_method		{ $$ = $2; }
		| /*EMPTY*/								{ $$ = "btree"; }
1684
		;
M
Marc G. Fournier 已提交
1685

1686 1687 1688 1689 1690 1691 1692 1693
index_params:  index_list						{ $$ = $1; }
		| func_index							{ $$ = lcons($1,NIL); }
		;

index_list:  index_list ',' index_elem			{ $$ = lappend($1, $3); }
		| index_elem							{ $$ = lcons($1, NIL); }
		;

1694
func_index:  func_name '(' name_list ')' opt_type opt_class
1695 1696 1697 1698 1699
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = $3;
					$$->class = $6;
1700
					$$->typename = $5;
1701 1702 1703 1704 1705 1706 1707 1708 1709
				}
		  ;

index_elem:  attr_name opt_type opt_class
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = NIL;
					$$->class = $3;
1710
					$$->typename = $2;
1711 1712 1713
				}
		;

1714 1715 1716
opt_type:  ':' Typename							{ $$ = $2; }
		| FOR Typename							{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL; }
1717 1718 1719 1720 1721 1722
		;

/* opt_class "WITH class" conflicts with preceeding opt_type
 *  for Typename of "TIMESTAMP WITH TIME ZONE"
 * So, remove "WITH class" from the syntax. OK??
 * - thomas 1997-10-12
1723
 *		| WITH class							{ $$ = $2; }
1724 1725
 */
opt_class:  class								{ $$ = $1; }
1726
		| USING class							{ $$ = $2; }
1727 1728 1729 1730
		| /*EMPTY*/								{ $$ = NULL; }
		;


1731 1732
/*****************************************************************************
 *
1733 1734
 *		QUERY:
 *				extend index <indexname> [where <qual>]
1735 1736 1737 1738
 *
 *****************************************************************************/

ExtendStmt:  EXTEND INDEX index_name where_clause
1739 1740 1741 1742 1743 1744 1745
				{
					ExtendStmt *n = makeNode(ExtendStmt);
					n->idxname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
1746

1747

1748 1749
/*****************************************************************************
 *
1750 1751
 *		QUERY:
 *				execute recipe <recipeName>
1752 1753 1754
 *
 *****************************************************************************/

1755
RecipeStmt:  EXECUTE RECIPE recipe_name
1756 1757 1758
				{
					RecipeStmt *n;
					if (!IsTransactionBlock())
B
Bruce Momjian 已提交
1759
						elog(ERROR,"EXECUTE RECIPE may only be used in begin/end transaction blocks");
1760 1761 1762 1763 1764 1765

					n = makeNode(RecipeStmt);
					n->recipeName = $3;
					$$ = (Node *)n;
				}
		;
1766 1767 1768 1769


/*****************************************************************************
 *
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
 *		QUERY:
 *				define function <fname>
 *					   (language = <lang>, returntype = <typename>
 *						[, arch_pct = <percentage | pre-defined>]
 *						[, disk_pct = <percentage | pre-defined>]
 *						[, byte_pct = <percentage | pre-defined>]
 *						[, perbyte_cpu = <int | pre-defined>]
 *						[, percall_cpu = <int | pre-defined>]
 *						[, iscachable])
 *						[arg is (<type-1> { , <type-n>})]
 *						as <filename or code in language as appropriate>
1781 1782 1783
 *
 *****************************************************************************/

1784 1785
ProcedureStmt:	CREATE FUNCTION func_name func_args
			 RETURNS func_return opt_with AS Sconst LANGUAGE Sconst
1786 1787 1788 1789
				{
					ProcedureStmt *n = makeNode(ProcedureStmt);
					n->funcname = $3;
					n->defArgs = $4;
1790
					n->returnType = $6;
1791 1792 1793 1794 1795 1796 1797
					n->withClause = $7;
					n->as = $9;
					n->language = $11;
					$$ = (Node *)n;
				};

opt_with:  WITH definition						{ $$ = $2; }
1798
		| /*EMPTY*/								{ $$ = NIL; }
1799 1800
		;

1801
func_args:  '(' func_args_list ')'				{ $$ = $2; }
1802
		| '(' ')'								{ $$ = NIL; }
1803
		;
1804

1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
func_args_list:  TypeId
				{	$$ = lcons(makeString($1),NIL); }
		| func_args_list ',' TypeId
				{	$$ = lappend($1,makeString($3)); }
		;

func_return:  set_opt TypeId
				{
					TypeName *n = makeNode(TypeName);
					n->name = $2;
					n->setof = $1;
					n->arrayBounds = NULL;
					$$ = (Node *)n;
				}
		;

set_opt:  SETOF									{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;
1824 1825 1826

/*****************************************************************************
 *
1827
 *		QUERY:
1828
 *
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
 *		remove function <funcname>
 *				(REMOVE FUNCTION "funcname" (arg1, arg2, ...))
 *		remove aggregate <aggname>
 *				(REMOVE AGGREGATE "aggname" "aggtype")
 *		remove operator <opname>
 *				(REMOVE OPERATOR "opname" (leftoperand_typ rightoperand_typ))
 *		remove type <typename>
 *				(REMOVE TYPE "typename")
 *		remove rule <rulename>
 *				(REMOVE RULE "rulename")
1839 1840 1841 1842
 *
 *****************************************************************************/

RemoveStmt:  DROP remove_type name
1843 1844 1845 1846 1847 1848 1849 1850
				{
					RemoveStmt *n = makeNode(RemoveStmt);
					n->removeType = $2;
					n->name = $3;
					$$ = (Node *)n;
				}
		;

1851
remove_type:  TYPE_P							{  $$ = TYPE_P; }
1852 1853 1854
		| INDEX									{  $$ = INDEX; }
		| RULE									{  $$ = RULE; }
		| VIEW									{  $$ = VIEW; }
1855
		;
1856

1857

1858
RemoveAggrStmt:  DROP AGGREGATE name aggr_argtype
1859 1860 1861 1862 1863 1864 1865 1866 1867
				{
						RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
						n->aggname = $3;
						n->aggtype = $4;
						$$ = (Node *)n;
				}
		;

aggr_argtype:  name								{ $$ = $1; }
1868
		| '*'									{ $$ = NULL; }
1869
		;
1870

1871 1872

RemoveFuncStmt:  DROP FUNCTION func_name func_args
1873 1874 1875
				{
					RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
					n->funcname = $3;
1876
					n->args = $4;
1877 1878 1879 1880
					$$ = (Node *)n;
				}
		;

1881 1882

RemoveOperStmt:  DROP OPERATOR all_Op '(' oper_argtypes ')'
1883 1884 1885 1886 1887 1888 1889
				{
					RemoveOperStmt *n = makeNode(RemoveOperStmt);
					n->opname = $3;
					n->args = $5;
					$$ = (Node *)n;
				}
		;
1890

1891
all_Op:  Op | MathOp;
1892

1893
MathOp:	'+'				{ $$ = "+"; }
1894 1895 1896 1897 1898 1899
		| '-'			{ $$ = "-"; }
		| '*'			{ $$ = "*"; }
		| '/'			{ $$ = "/"; }
		| '<'			{ $$ = "<"; }
		| '>'			{ $$ = ">"; }
		| '='			{ $$ = "="; }
1900 1901 1902 1903
		;

oper_argtypes:	name
				{
B
Bruce Momjian 已提交
1904
				   elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
1905 1906 1907 1908 1909 1910 1911 1912
				}
		| name ',' name
				{ $$ = makeList(makeString($1), makeString($3), -1); }
		| NONE ',' name			/* left unary */
				{ $$ = makeList(NULL, makeString($3), -1); }
		| name ',' NONE			/* right unary */
				{ $$ = makeList(makeString($1), NULL, -1); }
		;
1913

1914

1915 1916
/*****************************************************************************
 *
1917 1918 1919
 *		QUERY:
 *				rename <attrname1> in <relname> [*] to <attrname2>
 *				rename <relname1> to <relname2>
1920
 *
1921 1922
 *****************************************************************************/

1923
RenameStmt:  ALTER TABLE relation_name opt_inh_star
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
				  RENAME opt_column opt_name TO name
				{
					RenameStmt *n = makeNode(RenameStmt);
					n->relname = $3;
					n->inh = $4;
					n->column = $7;
					n->newname = $9;
					$$ = (Node *)n;
				}
		;

opt_name:  name							{ $$ = $1; }
1936
		| /*EMPTY*/						{ $$ = NULL; }
1937 1938 1939 1940 1941
		;

opt_column:  COLUMN						{ $$ = COLUMN; }
		| /*EMPTY*/						{ $$ = 0; }
		;
1942 1943 1944


/*****************************************************************************
1945
 *
1946 1947
 *		QUERY:	Define Rewrite Rule , Define Tuple Rule
 *				Define Rule <old rules >
1948
 *
1949
 *		only rewrite rule is supported -- ay 9/94
1950
 *
1951 1952
 *****************************************************************************/

1953
RuleStmt:  CREATE RULE name AS
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
		   { QueryIsRule=TRUE; }
		   ON event TO event_object where_clause
		   DO opt_instead OptStmtList
				{
					RuleStmt *n = makeNode(RuleStmt);
					n->rulename = $3;
					n->event = $7;
					n->object = $9;
					n->whereClause = $10;
					n->instead = $12;
					n->actions = $13;
					$$ = (Node *)n;
				}
		;

OptStmtList:  NOTHING					{ $$ = NIL; }
		| OptimizableStmt				{ $$ = lcons($1, NIL); }
		| '[' OptStmtBlock ']'			{ $$ = $2; }
M
 
Marc G. Fournier 已提交
1972
		| '(' OptStmtBlock ')'			{ $$ = $2; }
1973
		;
1974

1975
OptStmtBlock:  OptStmtMulti
1976
				{  $$ = $1; }
1977
		| OptimizableStmt
1978 1979
				{ $$ = lcons($1, NIL); }
		;
1980

1981
OptStmtMulti:  OptStmtMulti OptimizableStmt ';'
1982
				{  $$ = lappend($1, $2); }
1983
		| OptStmtMulti OptimizableStmt
1984
				{  $$ = lappend($1, $2); }
1985
		| OptimizableStmt ';'
1986 1987
				{ $$ = lcons($1, NIL); }
		;
1988

1989
event_object:  relation_name '.' attr_name
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
				{
					$$ = makeNode(Attr);
					$$->relname = $1;
					$$->paramNo = NULL;
					$$->attrs = lcons(makeString($3), NIL);
					$$->indirection = NIL;
				}
		| relation_name
				{
					$$ = makeNode(Attr);
					$$->relname = $1;
					$$->paramNo = NULL;
					$$->attrs = NIL;
					$$->indirection = NIL;
				}
		;
2006 2007

/* change me to select, update, etc. some day */
2008 2009 2010 2011 2012
event:	SELECT							{ $$ = CMD_SELECT; }
		| UPDATE						{ $$ = CMD_UPDATE; }
		| DELETE						{ $$ = CMD_DELETE; }
		| INSERT						{ $$ = CMD_INSERT; }
		 ;
2013

2014
opt_instead:  INSTEAD					{ $$ = TRUE; }
2015
		| /*EMPTY*/						{ $$ = FALSE; }
2016
		;
2017 2018 2019 2020


/*****************************************************************************
 *
2021 2022 2023
 *		QUERY:
 *				NOTIFY <relation_name>	can appear both in rule bodies and
 *				as a query-level command
2024 2025 2026
 *
 *****************************************************************************/

2027
NotifyStmt:  NOTIFY relation_name
2028 2029 2030 2031 2032 2033
				{
					NotifyStmt *n = makeNode(NotifyStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
		;
2034

2035
ListenStmt:  LISTEN relation_name
2036 2037 2038 2039 2040
				{
					ListenStmt *n = makeNode(ListenStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
2041 2042
;

M
 
Marc G. Fournier 已提交
2043 2044 2045 2046 2047 2048 2049 2050
UnlistenStmt:  UNLISTEN relation_name
				{
					UnlistenStmt *n = makeNode(UnlistenStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
;

2051 2052 2053

/*****************************************************************************
 *
2054
 *		Transactions:
2055
 *
2056 2057 2058 2059 2060 2061
 *		abort transaction
 *				(ABORT)
 *		begin transaction
 *				(BEGIN)
 *		end transaction
 *				(END)
2062
 *
2063 2064
 *****************************************************************************/

2065
TransactionStmt: ABORT_TRANS opt_trans
2066 2067 2068 2069 2070
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
2071
		| BEGIN_TRANS opt_trans
2072 2073 2074 2075 2076
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = BEGIN_TRANS;
					$$ = (Node *)n;
				}
2077
		| COMMIT opt_trans
2078 2079 2080 2081 2082
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
2083
		| END_TRANS opt_trans
2084 2085 2086 2087 2088
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
2089
		| ROLLBACK opt_trans
2090 2091 2092 2093 2094
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
2095
		;
2096

2097 2098 2099
opt_trans: WORK									{ $$ = TRUE; }
		| TRANSACTION							{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = TRUE; }
2100
		;
2101 2102 2103 2104


/*****************************************************************************
 *
2105 2106
 *		QUERY:
 *				define view <viewname> '('target-list ')' [where <quals> ]
2107 2108 2109
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2110
ViewStmt:  CREATE VIEW name AS SelectStmt
2111 2112 2113 2114
				{
					ViewStmt *n = makeNode(ViewStmt);
					n->viewname = $3;
					n->query = (Query *)$5;
B
Bruce Momjian 已提交
2115 2116
					if (((SelectStmt *)n->query)->sortClause != NULL)
						elog(ERROR,"Order by and Distinct on views is not implemented.");
B
Bruce Momjian 已提交
2117
					if (((SelectStmt *)n->query)->unionClause != NULL)
B
Bruce Momjian 已提交
2118
						elog(ERROR,"Views on unions not implemented.");
2119 2120 2121
					$$ = (Node *)n;
				}
		;
2122 2123 2124 2125


/*****************************************************************************
 *
2126 2127
 *		QUERY:
 *				load "filename"
2128 2129 2130
 *
 *****************************************************************************/

2131
LoadStmt:  LOAD file_name
2132 2133 2134 2135 2136 2137
				{
					LoadStmt *n = makeNode(LoadStmt);
					n->filename = $2;
					$$ = (Node *)n;
				}
		;
2138 2139 2140 2141


/*****************************************************************************
 *
2142 2143
 *		QUERY:
 *				createdb dbname
2144 2145 2146
 *
 *****************************************************************************/

M
 
Marc G. Fournier 已提交
2147
CreatedbStmt:  CREATE DATABASE database_name WITH opt_database1 opt_database2
2148 2149
				{
					CreatedbStmt *n = makeNode(CreatedbStmt);
M
 
Marc G. Fournier 已提交
2150 2151 2152
					if ($5 == NULL && $6 == NULL) {
						elog(ERROR, "CREATE DATABASE WITH requires at least an option");
					}
2153
					n->dbname = $3;
M
 
Marc G. Fournier 已提交
2154
					n->dbpath = $5;
M
 
Marc G. Fournier 已提交
2155
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164
					if ($6 != NULL) {
						n->encoding = pg_char_to_encoding($6);
						if (n->encoding < 0) {
							elog(ERROR, "invalid encoding name %s", $6);
						}
					} else {
						n->encoding = GetTemplateEncoding();
					}
#else
B
Bruce Momjian 已提交
2165 2166
					if ($6 != NULL)
						elog(ERROR, "WITH ENCODING is not supported");
2167
					n->encoding = 0;
M
 
Marc G. Fournier 已提交
2168
#endif
2169 2170
					$$ = (Node *)n;
				}
M
 
Marc G. Fournier 已提交
2171 2172 2173 2174 2175
		| CREATE DATABASE database_name
				{
					CreatedbStmt *n = makeNode(CreatedbStmt);
					n->dbname = $3;
					n->dbpath = NULL;
M
 
Marc G. Fournier 已提交
2176
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
2177
					n->encoding = GetTemplateEncoding();
2178 2179
#else
					n->encoding = 0;
M
 
Marc G. Fournier 已提交
2180 2181 2182 2183 2184 2185 2186
#endif
					$$ = (Node *)n;
				}
		;

opt_database1:  LOCATION '=' location			{ $$ = $3; }
		| /*EMPTY*/								{ $$ = NULL; }
2187
		;
2188

M
 
Marc G. Fournier 已提交
2189
opt_database2:  ENCODING '=' encoding			{ $$ = $3; }
2190 2191 2192 2193 2194 2195 2196
		| /*EMPTY*/								{ $$ = NULL; }
		;

location:  Sconst								{ $$ = $1; }
		| DEFAULT								{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
		;
2197

M
 
Marc G. Fournier 已提交
2198 2199 2200 2201 2202
encoding:  Sconst								{ $$ = $1; }
		| DEFAULT								{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

2203 2204
/*****************************************************************************
 *
2205 2206
 *		QUERY:
 *				destroydb dbname
2207 2208 2209
 *
 *****************************************************************************/

2210 2211 2212 2213 2214 2215 2216
DestroydbStmt:	DROP DATABASE database_name
				{
					DestroydbStmt *n = makeNode(DestroydbStmt);
					n->dbname = $3;
					$$ = (Node *)n;
				}
		;
2217 2218 2219 2220


/*****************************************************************************
 *
2221 2222
 *		QUERY:
 *				cluster <index_name> on <relation_name>
2223 2224 2225
 *
 *****************************************************************************/

2226
ClusterStmt:  CLUSTER index_name ON relation_name
2227 2228 2229 2230 2231 2232 2233
				{
				   ClusterStmt *n = makeNode(ClusterStmt);
				   n->relname = $4;
				   n->indexname = $2;
				   $$ = (Node*)n;
				}
		;
2234

2235

2236 2237
/*****************************************************************************
 *
2238 2239
 *		QUERY:
 *				vacuum
2240 2241 2242
 *
 *****************************************************************************/

V
Vadim B. Mikheev 已提交
2243
VacuumStmt:  VACUUM opt_verbose opt_analyze
2244 2245 2246 2247 2248 2249 2250 2251
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
					n->analyze = $3;
					n->vacrel = NULL;
					n->va_spec = NIL;
					$$ = (Node *)n;
				}
B
Bruce Momjian 已提交
2252
		| VACUUM opt_verbose opt_analyze relation_name opt_va_list
2253 2254 2255
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
B
Bruce Momjian 已提交
2256 2257
					n->analyze = $3;
					n->vacrel = $4;
2258 2259
					n->va_spec = $5;
					if ( $5 != NIL && !$4 )
B
Bruce Momjian 已提交
2260
						elog(ERROR,"parser: syntax error at or near \"(\"");
2261 2262 2263 2264
					$$ = (Node *)n;
				}
		;

2265
opt_verbose:  VERBOSE							{ $$ = TRUE; }
2266
		| /*EMPTY*/								{ $$ = FALSE; }
2267 2268
		;

2269
opt_analyze:  ANALYZE							{ $$ = TRUE; }
2270
		| /*EMPTY*/								{ $$ = FALSE; }
2271
		;
V
Vadim B. Mikheev 已提交
2272

2273 2274
opt_va_list:  '(' va_list ')'					{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NIL; }
2275
		;
V
Vadim B. Mikheev 已提交
2276

2277
va_list:  name
2278 2279 2280 2281
				{ $$=lcons($1,NIL); }
		| va_list ',' name
				{ $$=lappend($1,$3); }
		;
2282

2283

2284 2285
/*****************************************************************************
 *
2286 2287
 *		QUERY:
 *				EXPLAIN query
2288 2289 2290
 *
 *****************************************************************************/

2291
ExplainStmt:  EXPLAIN opt_verbose OptimizableStmt
2292 2293 2294 2295 2296 2297 2298
				{
					ExplainStmt *n = makeNode(ExplainStmt);
					n->verbose = $2;
					n->query = (Query*)$3;
					$$ = (Node *)n;
				}
		;
2299

2300

2301
/*****************************************************************************
2302 2303 2304 2305 2306 2307 2308 2309
 *																			 *
 *		Optimizable Stmts:													 *
 *																			 *
 *		one of the five queries processed by the planner					 *
 *																			 *
 *		[ultimately] produces query-trees as specified						 *
 *		in the query-spec document in ~postgres/ref							 *
 *																			 *
2310 2311
 *****************************************************************************/

B
Bruce Momjian 已提交
2312
OptimizableStmt:  SelectStmt
2313
		| CursorStmt
B
Bruce Momjian 已提交
2314 2315
		| UpdateStmt
		| InsertStmt
2316 2317 2318
		| NotifyStmt
		| DeleteStmt					/* by default all are $$=$1 */
		;
2319 2320 2321 2322


/*****************************************************************************
 *
2323 2324
 *		QUERY:
 *				INSERT STATEMENTS
2325
 *
2326 2327
 *****************************************************************************/

B
Bruce Momjian 已提交
2328
InsertStmt:  INSERT INTO relation_name opt_column_list insert_rest
2329 2330 2331 2332 2333 2334
				{
					$5->relname = $3;
					$5->cols = $4;
					$$ = (Node *)$5;
				}
		;
2335

2336
insert_rest:  VALUES '(' res_target_list2 ')'
2337
				{
B
Bruce Momjian 已提交
2338
					$$ = makeNode(InsertStmt);
2339
					$$->unique = NULL;
2340 2341
					$$->targetList = $3;
					$$->fromClause = NIL;
2342
					$$->whereClause = NULL;
2343
					$$->groupClause = NIL;
2344
					$$->havingClause = NULL;
2345
					$$->unionClause = NIL;
2346
				}
2347 2348 2349 2350
		| SELECT opt_unique res_target_list2
			 from_clause where_clause
			 group_clause having_clause
			 union_clause
2351
				{
B
Bruce Momjian 已提交
2352
					$$ = makeNode(InsertStmt);
2353 2354 2355 2356 2357 2358 2359
					$$->unique = $2;
					$$->targetList = $3;
					$$->fromClause = $4;
					$$->whereClause = $5;
					$$->groupClause = $6;
					$$->havingClause = $7;
					$$->unionClause = $8;
2360 2361 2362
				}
		;

2363
opt_column_list:  '(' columnList ')'			{ $$ = $2; }
2364 2365
		| /*EMPTY*/								{ $$ = NIL; }
		;
2366

2367
columnList:
2368 2369 2370 2371 2372
		  columnList ',' columnElem
				{ $$ = lappend($1, $3); }
		| columnElem
				{ $$ = lcons($1, NIL); }
		;
2373

2374
columnElem:  ColId opt_indirection
2375 2376 2377 2378 2379 2380 2381
				{
					Ident *id = makeNode(Ident);
					id->name = $1;
					id->indirection = $2;
					$$ = (Node *)id;
				}
		;
2382

2383

2384 2385
/*****************************************************************************
 *
2386 2387
 *		QUERY:
 *				DELETE STATEMENTS
2388 2389
 *
 *****************************************************************************/
2390

2391
DeleteStmt:  DELETE FROM relation_name
2392 2393 2394 2395 2396 2397 2398 2399
			 where_clause
				{
					DeleteStmt *n = makeNode(DeleteStmt);
					n->relname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
2400

2401 2402 2403 2404 2405
/*
 *	Total hack to just lock a table inside a transaction.
 *	Is it worth making this a separate command, with
 *	its own node type and file.  I don't think so. bjm 1998/1/22
 */
2406
LockStmt:  LOCK_P opt_table relation_name
2407 2408 2409 2410 2411 2412 2413 2414
				{
					DeleteStmt *n = makeNode(DeleteStmt);
					A_Const *c = makeNode(A_Const);

					c->val.type = T_String;
					c->val.val.str = "f";
					c->typename = makeNode(TypeName);
					c->typename->name = xlateSqlType("bool");
2415
					c->typename->typmod = -1;
2416

2417
					n->relname = $3;
2418
					n->whereClause = (Node *)c;
2419 2420 2421 2422
					$$ = (Node *)n;
				}
		;

2423 2424 2425

/*****************************************************************************
 *
2426
 *		QUERY:
B
Bruce Momjian 已提交
2427
 *				UpdateStmt (UPDATE)
2428 2429 2430
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2431
UpdateStmt:  UPDATE relation_name
2432 2433 2434 2435
			  SET res_target_list
			  from_clause
			  where_clause
				{
B
Bruce Momjian 已提交
2436
					UpdateStmt *n = makeNode(UpdateStmt);
2437 2438 2439 2440 2441 2442 2443
					n->relname = $2;
					n->targetList = $4;
					n->fromClause = $5;
					n->whereClause = $6;
					$$ = (Node *)n;
				}
		;
2444

2445 2446 2447

/*****************************************************************************
 *
2448 2449
 *		QUERY:
 *				CURSOR STATEMENTS
2450 2451
 *
 *****************************************************************************/
2452
CursorStmt:  DECLARE name opt_binary CURSOR FOR
2453 2454 2455 2456
 			 SELECT opt_unique res_target_list2
			 from_clause where_clause
			 group_clause having_clause
			 union_clause sort_clause
2457
				{
2458
					SelectStmt *n = makeNode(SelectStmt);
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468

					/* from PORTAL name */
					/*
					 *	15 august 1991 -- since 3.0 postgres does locking
					 *	right, we discovered that portals were violating
					 *	locking protocol.  portal locks cannot span xacts.
					 *	as a short-term fix, we installed the check here.
					 *							-- mao
					 */
					if (!IsTransactionBlock())
B
Bruce Momjian 已提交
2469
						elog(ERROR,"Named portals may only be used in begin/end transaction blocks");
2470 2471 2472 2473 2474 2475 2476 2477

					n->portalname = $2;
					n->binary = $3;
					n->unique = $7;
					n->targetList = $8;
					n->fromClause = $9;
					n->whereClause = $10;
					n->groupClause = $11;
2478 2479 2480
					n->havingClause = $12;
					n->unionClause = $13;
					n->sortClause = $14;
2481 2482 2483
					$$ = (Node *)n;
				}
		;
2484 2485 2486 2487


/*****************************************************************************
 *
2488 2489
 *		QUERY:
 *				SELECT STATEMENTS
2490 2491 2492
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2493
SelectStmt:  SELECT opt_unique res_target_list2
2494 2495 2496
			 result from_clause where_clause
			 group_clause having_clause
			 union_clause sort_clause
2497
				{
B
Bruce Momjian 已提交
2498
					SelectStmt *n = makeNode(SelectStmt);
2499 2500 2501 2502 2503 2504 2505
					n->unique = $2;
					n->targetList = $3;
					n->into = $4;
					n->fromClause = $5;
					n->whereClause = $6;
					n->groupClause = $7;
					n->havingClause = $8;
2506
					n->unionClause = $9;
2507 2508 2509 2510 2511
					n->sortClause = $10;
					$$ = (Node *)n;
				}
		;

2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
SubSelect:  SELECT opt_unique res_target_list2
			 from_clause where_clause
			 group_clause having_clause
			 union_clause
				{
					SelectStmt *n = makeNode(SelectStmt);
					n->unique = $2;
					n->targetList = $3;
					n->fromClause = $4;
					n->whereClause = $5;
					n->groupClause = $6;
					n->havingClause = $7;
					n->unionClause = $8;
					$$ = (Node *)n;
				}
		;

2529 2530
union_clause:  UNION opt_union select_list
				{
B
Bruce Momjian 已提交
2531
					SelectStmt *n = (SelectStmt *)lfirst($3);
2532 2533 2534 2535 2536
					n->unionall = $2;
					$$ = $3;
				}
		| /*EMPTY*/
				{ $$ = NIL; }
2537
		;
2538

2539
select_list:  select_list UNION opt_union SubUnion
2540
				{
B
Bruce Momjian 已提交
2541
					SelectStmt *n = (SelectStmt *)$4;
2542 2543 2544
					n->unionall = $3;
					$$ = lappend($1, $4);
				}
2545
		| SubUnion
2546 2547 2548
				{ $$ = lcons($1, NIL); }
		;

2549
SubUnion:	SELECT opt_unique res_target_list2
2550
			 from_clause where_clause
2551
			 group_clause having_clause
2552
				{
B
Bruce Momjian 已提交
2553
					SelectStmt *n = makeNode(SelectStmt);
2554
					n->unique = $2;
2555
					n->unionall = FALSE;
2556
					n->targetList = $3;
2557 2558 2559 2560
					n->fromClause = $4;
					n->whereClause = $5;
					n->groupClause = $6;
					n->havingClause = $7;
2561 2562 2563
					$$ = (Node *)n;
				}
		;
2564

2565 2566 2567 2568 2569 2570
result:  INTO opt_table relation_name			{ $$= $3; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

opt_table:  TABLE								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
2571
		;
2572

2573 2574 2575 2576
opt_union:  ALL									{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;

2577 2578
opt_unique:  DISTINCT							{ $$ = "*"; }
		| DISTINCT ON ColId						{ $$ = $3; }
2579 2580
		| ALL									{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
2581
		;
2582

2583
sort_clause:  ORDER BY sortby_list				{ $$ = $3; }
2584
		| /*EMPTY*/								{ $$ = NIL; }
2585
		;
2586

2587 2588
sortby_list:  sortby							{ $$ = lcons($1, NIL); }
		| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
2589
		;
2590

M
 
Marc G. Fournier 已提交
2591
sortby: a_expr OptUseOp
2592 2593
				{
					$$ = makeNode(SortGroupBy);
M
 
Marc G. Fournier 已提交
2594
					$$->node = $1;
2595 2596 2597 2598 2599
					$$->useOp = $2;
				}
		;

OptUseOp:  USING Op								{ $$ = $2; }
2600 2601 2602 2603 2604
		| USING '<'								{ $$ = "<"; }
		| USING '>'								{ $$ = ">"; }
		| ASC									{ $$ = "<"; }
		| DESC									{ $$ = ">"; }
		| /*EMPTY*/								{ $$ = "<"; /*default*/ }
2605
		;
2606 2607

/*
2608 2609
 *	jimmy bell-style recursive queries aren't supported in the
 *	current system.
2610
 *
2611 2612
 *	...however, recursive addattr and rename supported.  make special
 *	cases for these.
2613
 */
2614 2615
opt_inh_star:  '*'								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
2616
		;
2617

2618
relation_name_list:  name_list;
2619

2620
name_list:  name
2621
				{	$$ = lcons(makeString($1),NIL); }
2622
		| name_list ',' name
2623
				{	$$ = lappend($1,makeString($3)); }
2624
		;
2625

M
 
Marc G. Fournier 已提交
2626
group_clause:  GROUP BY expr_list			{ $$ = $3; }
2627 2628
		| /*EMPTY*/								{ $$ = NIL; }
		;
2629

B
Bruce Momjian 已提交
2630 2631
having_clause:  HAVING a_expr
				{
2632
					$$ = $2;
B
Bruce Momjian 已提交
2633
				}
2634 2635
		| /*EMPTY*/								{ $$ = NULL; }
		;
2636

2637

2638
/*****************************************************************************
2639
 *
2640 2641 2642
 *	clauses common to all Optimizable Stmts:
 *		from_clause		-
 *		where_clause	-
2643
 *
2644 2645
 *****************************************************************************/

2646
from_clause:  FROM '(' relation_expr join_expr JOIN relation_expr join_spec ')'
2647 2648
				{
					$$ = NIL;
B
Bruce Momjian 已提交
2649
					elog(ERROR,"JOIN not yet implemented");
2650 2651 2652 2653 2654 2655 2656
				}
		| FROM from_list						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NIL; }
		;

from_list:	from_list ',' from_val
				{ $$ = lappend($1, $3); }
2657
		| from_val CROSS JOIN from_val
B
Bruce Momjian 已提交
2658
				{ elog(ERROR,"CROSS JOIN not yet implemented"); }
2659
		| from_val
2660 2661
				{ $$ = lcons($1, NIL); }
		;
2662

2663
from_val:  relation_expr AS ColLabel
2664 2665 2666 2667 2668
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $3;
				}
2669
		| relation_expr ColId
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $2;
				}
		| relation_expr
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = NULL;
				}
		;
2682

2683
join_expr:  NATURAL join_expr					{ $$ = NULL; }
2684
		| FULL join_outer
B
Bruce Momjian 已提交
2685
				{ elog(ERROR,"FULL OUTER JOIN not yet implemented"); }
2686
		| LEFT join_outer
B
Bruce Momjian 已提交
2687
				{ elog(ERROR,"LEFT OUTER JOIN not yet implemented"); }
2688
		| RIGHT join_outer
B
Bruce Momjian 已提交
2689
				{ elog(ERROR,"RIGHT OUTER JOIN not yet implemented"); }
2690
		| OUTER_P
B
Bruce Momjian 已提交
2691
				{ elog(ERROR,"OUTER JOIN not yet implemented"); }
2692
		| INNER_P
B
Bruce Momjian 已提交
2693
				{ elog(ERROR,"INNER JOIN not yet implemented"); }
2694
		| UNION
B
Bruce Momjian 已提交
2695
				{ elog(ERROR,"UNION JOIN not yet implemented"); }
2696
		| /*EMPTY*/
B
Bruce Momjian 已提交
2697
				{ elog(ERROR,"INNER JOIN not yet implemented"); }
2698 2699
		;

2700 2701
join_outer:  OUTER_P							{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2702 2703
		;

2704 2705 2706
join_spec:	ON '(' a_expr ')'					{ $$ = NULL; }
		| USING '(' join_list ')'				{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2707 2708
		;

2709
join_list:  join_using							{ $$ = lcons($1, NIL); }
2710 2711
		| join_list ',' join_using				{ $$ = lappend($1, $3); }
		;
2712

2713
join_using:  ColId
M
 
Marc G. Fournier 已提交
2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
				/*  Changed from SortGroupBy parse node to new JoinUsing node.
			  	 *  SortGroupBy no longer needs these structure members.
				 *
			  	 *  Once, acknowledged, this comment can be removed by the
				 *  developer(s) working on the JOIN clause.
				 *
			  	 *    - daveh@insightdist.com  1998-07-31
				 */ 
				{
					$$ = makeNode(JoinUsing);
2724 2725 2726 2727
					$$->resno = 0;
					$$->range = NULL;
					$$->name = $1;
				}
2728
		| ColId '.' ColId
2729
				{
M
 
Marc G. Fournier 已提交
2730
					$$ = makeNode(JoinUsing);
2731 2732 2733 2734 2735 2736
					$$->resno = 0;
					$$->range = $1;
					$$->name = $3;
				}
		| Iconst
				{
M
 
Marc G. Fournier 已提交
2737
					$$ = makeNode(JoinUsing);
2738 2739 2740 2741 2742 2743
					$$->resno = $1;
					$$->range = NULL;
					$$->name = NULL;
				}
		;

2744 2745
where_clause:  WHERE a_expr						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
		;

relation_expr:	relation_name
				{
					/* normal relations */
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = FALSE;
				}
		| relation_name '*'				  %prec '='
				{
2757
					/* inheritance query */
2758 2759 2760 2761
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = TRUE;
				}
2762 2763

opt_array_bounds:  '[' ']' nest_array_bounds
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
				{  $$ = lcons(makeInteger(-1), $3); }
		| '[' Iconst ']' nest_array_bounds
				{  $$ = lcons(makeInteger($2), $4); }
		| /* EMPTY */
				{  $$ = NIL; }
		;

nest_array_bounds:	'[' ']' nest_array_bounds
				{  $$ = lcons(makeInteger(-1), $3); }
		| '[' Iconst ']' nest_array_bounds
				{  $$ = lcons(makeInteger($2), $4); }
		| /*EMPTY*/
				{  $$ = NIL; }
		;
2778

2779

2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
/*****************************************************************************
 *
 *	Type syntax
 *		SQL92 introduces a large amount of type-specific syntax.
 *		Define individual clauses to handle these cases, and use
 *		 the generic case to handle regular type-extensible Postgres syntax.
 *		- thomas 1997-10-10
 *
 *****************************************************************************/

Typename:  Array opt_array_bounds
				{
					$$ = $1;
					$$->arrayBounds = $2;
2794 2795 2796 2797

					/* Is this the name of a complex type? If so, implement
					 * it as a set.
					 */
2798
					if (!strcmp(saved_relname, $$->name))
2799 2800 2801 2802 2803
						/* This attr is the same type as the relation
						 * being defined. The classic example: create
						 * emp(name=text,mgr=emp)
						 */
						$$->setof = TRUE;
2804
					else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
2805 2806 2807 2808 2809 2810 2811
						 /* (Eventually add in here that the set can only
						  * contain one element.)
						  */
						$$->setof = TRUE;
					else
						$$->setof = FALSE;
				}
2812 2813
		| Character
		| SETOF Array
2814
				{
2815
					$$ = $2;
2816 2817 2818 2819
					$$->setof = TRUE;
				}
		;

2820 2821 2822
Array:  Generic
		| Datetime
		| Numeric
2823
		;
2824

2825
Generic:  generic
2826
				{
2827 2828
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($1);
2829
					$$->typmod = -1;
2830 2831 2832
				}
		;

2833
generic:  IDENT									{ $$ = $1; }
2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
		| TYPE_P								{ $$ = xlateSqlType("type"); }
		;

/* SQL92 numeric data types
 * Check FLOAT() precision limits assuming IEEE floating types.
 * Provide rudimentary DECIMAL() and NUMERIC() implementations
 *  by checking parameters and making sure they match what is possible with INTEGER.
 * - thomas 1997-09-18
 */
Numeric:  FLOAT opt_float
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($2);
2847
					$$->typmod = -1;
2848
				}
2849 2850 2851 2852 2853
		| DOUBLE PRECISION
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("float");
				}
2854
		| DECIMAL opt_decimal
2855
				{
2856
					$$ = makeNode(TypeName);
2857
					$$->name = xlateSqlType("integer");
2858
					$$->typmod = -1;
2859 2860 2861 2862 2863
				}
		| NUMERIC opt_numeric
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("integer");
2864
					$$->typmod = -1;
2865 2866
				}
		;
2867

2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
numeric:  FLOAT
				{	$$ = xlateSqlType("float8"); }
		| DOUBLE PRECISION
				{	$$ = xlateSqlType("float8"); }
		| DECIMAL
				{	$$ = xlateSqlType("decimal"); }
		| NUMERIC
				{	$$ = xlateSqlType("numeric"); }
		;

2878 2879 2880
opt_float:  '(' Iconst ')'
				{
					if ($2 < 1)
B
Bruce Momjian 已提交
2881
						elog(ERROR,"precision for FLOAT must be at least 1");
2882 2883 2884 2885 2886
					else if ($2 < 7)
						$$ = xlateSqlType("float4");
					else if ($2 < 16)
						$$ = xlateSqlType("float8");
					else
B
Bruce Momjian 已提交
2887
						elog(ERROR,"precision for FLOAT must be less than 16");
2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
				}
		| /*EMPTY*/
				{
					$$ = xlateSqlType("float8");
				}
		;

opt_numeric:  '(' Iconst ',' Iconst ')'
				{
					if ($2 != 9)
2898
						elog(ERROR,"NUMERIC precision %d must be 9",$2);
2899
					if ($4 != 0)
2900
						elog(ERROR,"NUMERIC scale %d must be zero",$4);
2901 2902 2903 2904
				}
		| '(' Iconst ')'
				{
					if ($2 != 9)
2905
						elog(ERROR,"NUMERIC precision %d must be 9",$2);
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
				}
		| /*EMPTY*/
				{
					$$ = NULL;
				}
		;

opt_decimal:  '(' Iconst ',' Iconst ')'
				{
					if ($2 > 9)
2916
						elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2917
					if ($4 != 0)
2918
						elog(ERROR,"DECIMAL scale %d must be zero",$4);
2919 2920 2921 2922 2923
					$$ = NULL;
				}
		| '(' Iconst ')'
				{
					if ($2 > 9)
2924
						elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942
					$$ = NULL;
				}
		| /*EMPTY*/
				{
					$$ = NULL;
				}
		;

/* SQL92 character data types
 * The following implements CHAR() and VARCHAR().
 * We do it here instead of the 'Generic' production
 * because we don't want to allow arrays of VARCHAR().
 * I haven't thought about whether that will work or not.
 *								- ay 6/95
 */
Character:  character '(' Iconst ')'
				{
					$$ = makeNode(TypeName);
2943
					if (strcasecmp($1, "char") == 0)
2944
						$$->name = xlateSqlType("bpchar");
2945
					else if (strcasecmp($1, "varchar") == 0)
2946 2947
						$$->name = xlateSqlType("varchar");
					else
2948 2949
						yyerror("internal parsing error; unrecognized character type");

2950
					if ($3 < 1)
2951
						elog(ERROR,"length for '%s' type must be at least 1",$1);
2952 2953 2954 2955 2956
					else if ($3 > 4096)
						/* we can store a char() of length up to the size
						 * of a page (8KB) - page headers and friends but
						 * just to be safe here...	- ay 6/95
						 * XXX note this hardcoded limit - thomas 1997-07-13
2957
						 */
2958
						elog(ERROR,"length for type '%s' cannot exceed 4096",$1);
2959 2960 2961 2962 2963 2964

					/* we actually implement this sort of like a varlen, so
					 * the first 4 bytes is the length. (the difference
					 * between this and "text" is that we blank-pad and
					 * truncate where necessary
					 */
2965
					$$->typmod = VARHDRSZ + $3;
2966
				}
2967
		| character
2968 2969
				{
					$$ = makeNode(TypeName);
2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
					/* Let's try to make all single-character types into bpchar(1)
					 * - thomas 1998-05-07
					 */
					if (strcasecmp($1, "char") == 0)
					{
						$$->name = xlateSqlType("bpchar");
						$$->typmod = VARHDRSZ + 1;
					}
					else
					{
						$$->name = xlateSqlType($1);
						$$->typmod = -1;
					}
2983 2984
				}
		;
2985

2986 2987 2988 2989 2990 2991
character:  CHARACTER opt_varying opt_charset opt_collate
				{
					char *type, *c;
					if (($3 == NULL) || (strcasecmp($3, "sql_text") == 0)) {
						if ($2) type = xlateSqlType("varchar");
						else type = xlateSqlType("char");
2992
					} else {
2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
						if ($2) {
							c = palloc(strlen("var") + strlen($3) + 1);
							strcpy(c, "var");
							strcat(c, $3);
							type = xlateSqlType(c);
						} else {
							type = xlateSqlType($3);
						}
					};
					if ($4 != NULL)
3003
						elog(NOTICE,"COLLATE %s not yet implemented; clause ignored",$4);
3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
					$$ = type;
				}
		| CHAR opt_varying						{ $$ = xlateSqlType($2? "varchar": "char"); }
		| VARCHAR								{ $$ = xlateSqlType("varchar"); }
		| NATIONAL CHARACTER opt_varying		{ $$ = xlateSqlType($3? "varchar": "char"); }
		| NCHAR opt_varying						{ $$ = xlateSqlType($2? "varchar": "char"); }
		;

opt_varying:  VARYING							{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;

opt_charset:  CHARACTER SET ColId				{ $$ = $3; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

opt_collate:  COLLATE ColId						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

Datetime:  datetime
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($1);
3028
					$$->typmod = -1;
3029 3030 3031 3032 3033 3034
				}
		| TIMESTAMP opt_timezone
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("timestamp");
					$$->timezone = $2;
3035
					$$->typmod = -1;
3036 3037 3038 3039 3040
				}
		| TIME
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("time");
3041
					$$->typmod = -1;
3042 3043 3044 3045 3046
				}
		| INTERVAL opt_interval
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("interval");
3047
					$$->typmod = -1;
3048
				}
3049
		;
3050

3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
datetime:  YEAR_P								{ $$ = "year"; }
		| MONTH_P								{ $$ = "month"; }
		| DAY_P									{ $$ = "day"; }
		| HOUR_P								{ $$ = "hour"; }
		| MINUTE_P								{ $$ = "minute"; }
		| SECOND_P								{ $$ = "second"; }
		;

opt_timezone:  WITH TIME ZONE					{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;

opt_interval:  datetime							{ $$ = lcons($1, NIL); }
		| YEAR_P TO MONTH_P						{ $$ = NIL; }
		| DAY_P TO HOUR_P						{ $$ = NIL; }
		| DAY_P TO MINUTE_P						{ $$ = NIL; }
		| DAY_P TO SECOND_P						{ $$ = NIL; }
		| HOUR_P TO MINUTE_P					{ $$ = NIL; }
		| HOUR_P TO SECOND_P					{ $$ = NIL; }
3070
		| MINUTE_P TO SECOND_P					{ $$ = NIL; }
3071
		| /*EMPTY*/								{ $$ = NIL; }
3072 3073
		;

3074 3075 3076

/*****************************************************************************
 *
3077
 *	expression grammar, still needs some cleanup
3078 3079 3080
 *
 *****************************************************************************/

3081
a_expr_or_null:  a_expr
3082
				{ $$ = $1; }
3083
		| NULL_P
3084 3085 3086 3087 3088
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Null;
					$$ = (Node *)n;
				}
3089 3090 3091 3092 3093
		;

/* Expressions using row descriptors
 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
 *  with singleton expressions.
3094 3095 3096 3097 3098
 * Eliminated lots of code by defining row_op and sub_type clauses.
 * However, can not consolidate EXPR_LINK case with others subselects
 *  due to shift/reduce conflict with the non-subselect clause (the parser
 *  would have to look ahead more than one token to resolve the conflict).
 * - thomas 1998-05-09
3099 3100 3101
 */
row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
				{
3102 3103
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3104 3105 3106
					n->oper = lcons("=",NIL);
					n->useor = false;
					n->subLinkType = ANY_SUBLINK;
3107 3108
					n->subselect = $6;
					$$ = (Node *)n;
3109 3110 3111
				}
		| '(' row_descriptor ')' NOT IN '(' SubSelect ')'
				{
3112 3113
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3114 3115 3116 3117 3118 3119
					n->oper = lcons("<>",NIL);
					n->useor = true;
					n->subLinkType = ALL_SUBLINK;
					n->subselect = $7;
					$$ = (Node *)n;
				}
3120
		| '(' row_descriptor ')' row_op sub_type '(' SubSelect ')'
3121 3122 3123 3124 3125 3126 3127 3128
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->oper = lcons($4, NIL);
					if (strcmp($4,"<>") == 0)
						n->useor = true;
					else
						n->useor = false;
3129
					n->subLinkType = $5;
B
Bruce Momjian 已提交
3130 3131 3132
					n->subselect = $7;
					$$ = (Node *)n;
				}
3133
		| '(' row_descriptor ')' row_op '(' SubSelect ')'
B
Bruce Momjian 已提交
3134 3135 3136
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3137
					n->oper = lcons($4, NIL);
3138 3139 3140 3141
					if (strcmp($4,"<>") == 0)
						n->useor = true;
					else
						n->useor = false;
3142 3143
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $6;
B
Bruce Momjian 已提交
3144 3145
					$$ = (Node *)n;
				}
3146
		| '(' row_descriptor ')' row_op '(' row_descriptor ')'
3147
				{
B
Bruce Momjian 已提交
3148
					$$ = makeRowExpr($4, $2, $6);
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
				}
		;

row_descriptor:  row_list ',' a_expr
				{
					$$ = lappend($1, $3);
				}
		;

row_list:  row_list ',' a_expr
				{
					$$ = lappend($1, $3);
				}
		| a_expr
				{
					$$ = lcons($1, NIL);
				}
		;
3167

3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181
row_op:  Op									{ $$ = $1; }
		| '<'								{ $$ = "<"; }
		| '='								{ $$ = "="; }
		| '>'								{ $$ = ">"; }
		| '+'								{ $$ = "+"; }
		| '-'								{ $$ = "-"; }
		| '*'								{ $$ = "*"; }
		| '/'								{ $$ = "/"; }
		;

sub_type:  ANY								{ $$ = ANY_SUBLINK; }
		| ALL								{ $$ = ALL_SUBLINK; }
		;

3182 3183 3184 3185 3186 3187 3188 3189
/*
 * This is the heart of the expression syntax.
 * Note that the BETWEEN clause looks similar to a boolean expression
 *  and so we must define b_expr which is almost the same as a_expr
 *  but without the boolean expressions.
 * All operations are allowed in a BETWEEN clause if surrounded by parens.
 */

3190
a_expr:  attr opt_indirection
3191 3192 3193 3194
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
3195 3196
		| row_expr
				{	$$ = $1;  }
3197 3198
		| AexprConst
				{	$$ = $1;  }
3199 3200 3201 3202 3203 3204 3205 3206
		| ColId
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
		| '-' a_expr %prec UMINUS
				{	$$ = makeA_Expr(OP, "-", NULL, $2); }
		| a_expr '+' a_expr
				{	$$ = makeA_Expr(OP, "+", $1, $3); }
		| a_expr '-' a_expr
				{	$$ = makeA_Expr(OP, "-", $1, $3); }
		| a_expr '/' a_expr
				{	$$ = makeA_Expr(OP, "/", $1, $3); }
		| a_expr '*' a_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| a_expr '<' a_expr
				{	$$ = makeA_Expr(OP, "<", $1, $3); }
		| a_expr '>' a_expr
				{	$$ = makeA_Expr(OP, ">", $1, $3); }
		| a_expr '=' a_expr
				{	$$ = makeA_Expr(OP, "=", $1, $3); }
		| ':' a_expr
				{	$$ = makeA_Expr(OP, ":", NULL, $2); }
		| ';' a_expr
				{	$$ = makeA_Expr(OP, ";", NULL, $2); }
		| '|' a_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
3229
		| a_expr TYPECAST Typename
3230
				{
3231
					$$ = (Node *)$1;
3232
					/* AexprConst can be either A_Const or ParamNo */
3233
					if (nodeTag($1) == T_A_Const) {
3234
						((A_Const *)$1)->typename = $3;
3235
					} else if (nodeTag($1) == T_Param) {
3236
						((ParamNo *)$1)->typename = $3;
3237 3238 3239 3240 3241 3242 3243
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
3244
				}
3245
		| CAST '(' a_expr AS Typename ')'
3246
				{
3247
					$$ = (Node *)$3;
3248
					/* AexprConst can be either A_Const or ParamNo */
3249 3250 3251 3252
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($5) == T_Param) {
						((ParamNo *)$3)->typename = $5;
3253 3254 3255
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
3256 3257
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
3258 3259
						$$ = (Node *)n;
					}
3260 3261 3262 3263
				}
		| '(' a_expr_or_null ')'
				{	$$ = $2; }
		| a_expr Op a_expr
3264
				{	$$ = makeIndexable($2,$1,$3);	}
3265
		| a_expr LIKE a_expr
3266
				{	$$ = makeIndexable("~~", $1, $3); }
3267 3268 3269 3270 3271 3272
		| a_expr NOT LIKE a_expr
				{	$$ = makeA_Expr(OP, "!~~", $1, $4); }
		| Op a_expr
				{	$$ = makeA_Expr(OP, $1, NULL, $2); }
		| a_expr Op
				{	$$ = makeA_Expr(OP, $2, $1, NULL); }
3273
		| func_name '(' '*' ')'
3274
				{
3275
					/* cheap hack for aggregate (eg. count) */
3276
					FuncCall *n = makeNode(FuncCall);
3277
					A_Const *star = makeNode(A_Const);
3278

3279 3280
					star->val.type = T_String;
					star->val.val.str = "";
3281 3282 3283 3284
					n->funcname = $1;
					n->args = lcons(star, NIL);
					$$ = (Node *)n;
				}
3285
		| func_name '(' ')'
3286 3287 3288 3289
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
3290 3291
					$$ = (Node *)n;
				}
3292
		| func_name '(' expr_list ')'
3293 3294 3295 3296 3297 3298
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309
		| CURRENT_DATE
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("date");
					t->setof = FALSE;
3310 3311
					t->typmod = -1;
 
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
					$$ = (Node *)n;
				}
		| CURRENT_TIME
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("time");
					t->setof = FALSE;
3325
					t->typmod = -1;
3326 3327 3328

					$$ = (Node *)n;
				}
3329
		| CURRENT_TIME '(' Iconst ')'
3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343
				{
					FuncCall *n = makeNode(FuncCall);
					A_Const *s = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->funcname = xlateSqlType("time");
					n->args = lcons(s, NIL);

					s->val.type = T_String;
					s->val.val.str = "now";
					s->typename = t;

					t->name = xlateSqlType("time");
					t->setof = FALSE;
3344
					t->typmod = -1;
3345

3346
					if ($3 != 0)
3347
						elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361

					$$ = (Node *)n;
				}
		| CURRENT_TIMESTAMP
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("timestamp");
					t->setof = FALSE;
3362
					t->typmod = -1;
3363 3364 3365

					$$ = (Node *)n;
				}
3366
		| CURRENT_TIMESTAMP '(' Iconst ')'
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380
				{
					FuncCall *n = makeNode(FuncCall);
					A_Const *s = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->funcname = xlateSqlType("timestamp");
					n->args = lcons(s, NIL);

					s->val.type = T_String;
					s->val.val.str = "now";
					s->typename = t;

					t->name = xlateSqlType("timestamp");
					t->setof = FALSE;
3381
					t->typmod = -1;
3382

3383
					if ($3 != 0)
3384
						elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);
3385

3386 3387
					$$ = (Node *)n;
				}
3388
		| CURRENT_USER
3389 3390 3391 3392 3393 3394
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3395 3396 3397 3398 3399 3400 3401
		| USER
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3402 3403
		| EXISTS '(' SubSelect ')'
				{
3404 3405
					SubLink *n = makeNode(SubLink);
					n->lefthand = NIL;
3406
					n->useor = false;
3407
					n->oper = NIL;
3408
					n->subLinkType = EXISTS_SUBLINK;
3409 3410
					n->subselect = $3;
					$$ = (Node *)n;
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463
				}
		| EXTRACT '(' extract_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "date_part";
					n->args = $3;
					$$ = (Node *)n;
				}
		| POSITION '(' position_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "strpos";
					n->args = $3;
					$$ = (Node *)n;
				}
		| SUBSTRING '(' substr_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "substr";
					n->args = $3;
					$$ = (Node *)n;
				}
		/* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
		| TRIM '(' BOTH trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' LEADING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "ltrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' TRAILING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "rtrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $3;
					$$ = (Node *)n;
				}
		| a_expr ISNULL
				{	$$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3464
		| a_expr IS NULL_P
3465 3466 3467
				{	$$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
		| a_expr NOTNULL
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3468
		| a_expr IS NOT NULL_P
3469
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3470 3471 3472 3473 3474
		/* IS TRUE, IS FALSE, etc used to be function calls
		 *  but let's make them expressions to allow the optimizer
		 *  a chance to eliminate them if a_expr is a constant string.
		 * - thomas 1997-12-22
		 */
3475 3476
		| a_expr IS TRUE_P
				{
3477 3478 3479 3480 3481
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "t";
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3482
					n->typename->typmod = -1;
3483
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3484
				}
3485
		| a_expr IS NOT FALSE_P
3486
				{
3487 3488 3489 3490 3491
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "t";
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3492
					n->typename->typmod = -1;
3493
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3494
				}
3495
		| a_expr IS FALSE_P
3496
				{
3497 3498 3499 3500 3501
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "f";
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3502
					n->typename->typmod = -1;
3503
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3504
				}
3505
		| a_expr IS NOT TRUE_P
3506
				{
3507 3508 3509 3510 3511
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "f";
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3512
					n->typename->typmod = -1;
3513
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3514
				}
3515
		| a_expr BETWEEN b_expr AND b_expr
3516 3517 3518 3519 3520
				{
					$$ = makeA_Expr(AND, NULL,
						makeA_Expr(OP, ">=", $1, $3),
						makeA_Expr(OP, "<=", $1, $5));
				}
3521
		| a_expr NOT BETWEEN b_expr AND b_expr
3522 3523 3524 3525 3526
				{
					$$ = makeA_Expr(OR, NULL,
						makeA_Expr(OP, "<", $1, $4),
						makeA_Expr(OP, ">", $1, $6));
				}
3527
		| a_expr IN { saved_In_Expr = lcons($1,saved_In_Expr); } '(' in_expr ')'
3528
				{
3529
					saved_In_Expr = lnext(saved_In_Expr);
3530 3531
					if (nodeTag($5) == T_SubLink)
					{
3532 3533 3534 3535 3536 3537
							SubLink *n = (SubLink *)$5;
							n->lefthand = lcons($1, NIL);
							n->oper = lcons("=",NIL);
							n->useor = false;
							n->subLinkType = ANY_SUBLINK;
							$$ = (Node *)n;
3538 3539 3540
					}
					else	$$ = $5;
				}
3541
		| a_expr NOT IN { saved_In_Expr = lcons($1,saved_In_Expr); } '(' not_in_expr ')'
3542
				{
3543
					saved_In_Expr = lnext(saved_In_Expr);
3544 3545
					if (nodeTag($6) == T_SubLink)
					{
3546 3547 3548 3549 3550 3551
							SubLink *n = (SubLink *)$6;
							n->lefthand = lcons($1, NIL);
							n->oper = lcons("<>",NIL);
							n->useor = false;
							n->subLinkType = ALL_SUBLINK;
							$$ = (Node *)n;
3552 3553 3554
					}
					else	$$ = $6;
				}
3555
		| a_expr Op '(' SubSelect ')'
3556 3557
				{
					SubLink *n = makeNode(SubLink);
3558
					n->lefthand = lcons($1, NULL);
3559 3560
					n->oper = lcons($2,NIL);
					n->useor = false;
3561 3562
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
3563 3564
					$$ = (Node *)n;
				}
3565
		| a_expr '+' '(' SubSelect ')'
B
Bruce Momjian 已提交
3566 3567
				{
					SubLink *n = makeNode(SubLink);
3568
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3569 3570
					n->oper = lcons("+",NIL);
					n->useor = false;
3571 3572
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3573 3574
					$$ = (Node *)n;
				}
3575
		| a_expr '-' '(' SubSelect ')'
B
Bruce Momjian 已提交
3576 3577
				{
					SubLink *n = makeNode(SubLink);
3578
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3579 3580
					n->oper = lcons("-",NIL);
					n->useor = false;
3581 3582
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3583 3584
					$$ = (Node *)n;
				}
3585
		| a_expr '/' '(' SubSelect ')'
B
Bruce Momjian 已提交
3586 3587
				{
					SubLink *n = makeNode(SubLink);
3588
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3589 3590
					n->oper = lcons("/",NIL);
					n->useor = false;
3591 3592
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3593 3594
					$$ = (Node *)n;
				}
3595
		| a_expr '*' '(' SubSelect ')'
B
Bruce Momjian 已提交
3596 3597
				{
					SubLink *n = makeNode(SubLink);
3598
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3599 3600
					n->oper = lcons("*",NIL);
					n->useor = false;
3601 3602
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3603 3604
					$$ = (Node *)n;
				}
3605
		| a_expr '<' '(' SubSelect ')'
B
Bruce Momjian 已提交
3606 3607
				{
					SubLink *n = makeNode(SubLink);
3608
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3609 3610
					n->oper = lcons("<",NIL);
					n->useor = false;
3611 3612
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3613 3614
					$$ = (Node *)n;
				}
3615
		| a_expr '>' '(' SubSelect ')'
B
Bruce Momjian 已提交
3616 3617
				{
					SubLink *n = makeNode(SubLink);
3618
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3619 3620
					n->oper = lcons(">",NIL);
					n->useor = false;
3621 3622
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3623 3624
					$$ = (Node *)n;
				}
3625
		| a_expr '=' '(' SubSelect ')'
B
Bruce Momjian 已提交
3626 3627
				{
					SubLink *n = makeNode(SubLink);
3628
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
3629 3630
					n->oper = lcons("=",NIL);
					n->useor = false;
3631 3632
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
3633 3634
					$$ = (Node *)n;
				}
3635
		| a_expr Op ANY '(' SubSelect ')'
3636 3637
				{
					SubLink *n = makeNode(SubLink);
3638
					n->lefthand = lcons($1,NIL);
3639 3640
					n->oper = lcons($2,NIL);
					n->useor = false;
3641
					n->subLinkType = ANY_SUBLINK;
3642 3643 3644
					n->subselect = $5;
					$$ = (Node *)n;
				}
3645
		| a_expr '+' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3646 3647
				{
					SubLink *n = makeNode(SubLink);
3648
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3649 3650
					n->oper = lcons("+",NIL);
					n->useor = false;
3651
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3652 3653 3654
					n->subselect = $5;
					$$ = (Node *)n;
				}
3655
		| a_expr '-' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3656 3657
				{
					SubLink *n = makeNode(SubLink);
3658
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3659 3660
					n->oper = lcons("-",NIL);
					n->useor = false;
3661
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3662 3663 3664
					n->subselect = $5;
					$$ = (Node *)n;
				}
3665
		| a_expr '/' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3666 3667
				{
					SubLink *n = makeNode(SubLink);
3668
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3669 3670
					n->oper = lcons("/",NIL);
					n->useor = false;
3671
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3672 3673 3674
					n->subselect = $5;
					$$ = (Node *)n;
				}
3675
		| a_expr '*' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3676 3677
				{
					SubLink *n = makeNode(SubLink);
3678
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3679 3680
					n->oper = lcons("*",NIL);
					n->useor = false;
3681
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3682 3683 3684
					n->subselect = $5;
					$$ = (Node *)n;
				}
3685
		| a_expr '<' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3686 3687
				{
					SubLink *n = makeNode(SubLink);
3688
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3689 3690
					n->oper = lcons("<",NIL);
					n->useor = false;
3691
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3692 3693 3694
					n->subselect = $5;
					$$ = (Node *)n;
				}
3695
		| a_expr '>' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3696 3697
				{
					SubLink *n = makeNode(SubLink);
3698
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3699 3700
					n->oper = lcons(">",NIL);
					n->useor = false;
3701
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3702 3703 3704
					n->subselect = $5;
					$$ = (Node *)n;
				}
3705
		| a_expr '=' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
3706 3707
				{
					SubLink *n = makeNode(SubLink);
3708
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
3709 3710
					n->oper = lcons("=",NIL);
					n->useor = false;
3711
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
3712 3713 3714
					n->subselect = $5;
					$$ = (Node *)n;
				}
3715
		| a_expr Op ALL '(' SubSelect ')'
3716 3717 3718 3719 3720 3721
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons($2,NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3722
					n->subselect = $5;
3723 3724
					$$ = (Node *)n;
				}
3725
		| a_expr '+' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3726 3727 3728 3729 3730 3731
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("+",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3732
					n->subselect = $5;
B
Bruce Momjian 已提交
3733 3734
					$$ = (Node *)n;
				}
3735
		| a_expr '-' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3736 3737 3738 3739 3740 3741
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("-",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3742
					n->subselect = $5;
B
Bruce Momjian 已提交
3743 3744
					$$ = (Node *)n;
				}
3745
		| a_expr '/' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3746 3747 3748 3749 3750 3751
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("/",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3752
					n->subselect = $5;
B
Bruce Momjian 已提交
3753 3754
					$$ = (Node *)n;
				}
3755
		| a_expr '*' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3756 3757 3758 3759 3760 3761
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("*",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3762
					n->subselect = $5;
B
Bruce Momjian 已提交
3763 3764
					$$ = (Node *)n;
				}
3765
		| a_expr '<' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3766 3767 3768 3769 3770 3771
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("<",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3772
					n->subselect = $5;
B
Bruce Momjian 已提交
3773 3774
					$$ = (Node *)n;
				}
3775
		| a_expr '>' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3776 3777 3778 3779 3780 3781
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons(">",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3782
					n->subselect = $5;
B
Bruce Momjian 已提交
3783 3784
					$$ = (Node *)n;
				}
3785
		| a_expr '=' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
3786 3787 3788 3789 3790 3791
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("=",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
3792
					n->subselect = $5;
B
Bruce Momjian 已提交
3793 3794
					$$ = (Node *)n;
				}
3795 3796 3797 3798 3799 3800 3801
		| a_expr AND a_expr
				{	$$ = makeA_Expr(AND, NULL, $1, $3); }
		| a_expr OR a_expr
				{	$$ = makeA_Expr(OR, NULL, $1, $3); }
		| NOT a_expr
				{	$$ = makeA_Expr(NOT, NULL, NULL, $2); }
		;
3802

3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
/*
 * b_expr is a subset of the complete expression syntax
 *  defined by a_expr. b_expr is used in BETWEEN clauses
 *  to eliminate parser ambiguities stemming from the AND keyword.
 */

b_expr:  attr opt_indirection
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
		| AexprConst
				{	$$ = $1;  }
		| ColId
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
		| '-' b_expr %prec UMINUS
				{	$$ = makeA_Expr(OP, "-", NULL, $2); }
		| b_expr '+' b_expr
				{	$$ = makeA_Expr(OP, "+", $1, $3); }
		| b_expr '-' b_expr
				{	$$ = makeA_Expr(OP, "-", $1, $3); }
		| b_expr '/' b_expr
				{	$$ = makeA_Expr(OP, "/", $1, $3); }
		| b_expr '*' b_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| ':' b_expr
				{	$$ = makeA_Expr(OP, ":", NULL, $2); }
		| ';' b_expr
				{	$$ = makeA_Expr(OP, ";", NULL, $2); }
		| '|' b_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
		| b_expr TYPECAST Typename
				{
					$$ = (Node *)$1;
					/* AexprConst can be either A_Const or ParamNo */
					if (nodeTag($1) == T_A_Const) {
						((A_Const *)$1)->typename = $3;
					} else if (nodeTag($1) == T_Param) {
						((ParamNo *)$1)->typename = $3;
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
				}
3856
		| CAST '(' b_expr AS Typename ')'
3857
				{
3858
					$$ = (Node *)$3;
3859
					/* AexprConst can be either A_Const or ParamNo */
3860 3861 3862 3863
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($3) == T_Param) {
						((ParamNo *)$3)->typename = $5;
3864 3865 3866
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
3867 3868
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879
						$$ = (Node *)n;
					}
				}
		| '(' a_expr ')'
				{	$$ = $2; }
		| b_expr Op b_expr
				{	$$ = makeIndexable($2,$1,$3);	}
		| Op b_expr
				{	$$ = makeA_Expr(OP, $1, NULL, $2); }
		| b_expr Op
				{	$$ = makeA_Expr(OP, $2, $1, NULL); }
3880
		| func_name '(' ')'
3881 3882 3883 3884 3885 3886
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
					$$ = (Node *)n;
				}
3887
		| func_name '(' expr_list ')'
3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
		| CURRENT_DATE
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("date");
					t->setof = FALSE;
3905
					t->typmod = -1;
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919

					$$ = (Node *)n;
				}
		| CURRENT_TIME
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("time");
					t->setof = FALSE;
3920
					t->typmod = -1;
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938

					$$ = (Node *)n;
				}
		| CURRENT_TIME '(' Iconst ')'
				{
					FuncCall *n = makeNode(FuncCall);
					A_Const *s = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->funcname = xlateSqlType("time");
					n->args = lcons(s, NIL);

					s->val.type = T_String;
					s->val.val.str = "now";
					s->typename = t;

					t->name = xlateSqlType("time");
					t->setof = FALSE;
3939
					t->typmod = -1;
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956

					if ($3 != 0)
						elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);

					$$ = (Node *)n;
				}
		| CURRENT_TIMESTAMP
				{
					A_Const *n = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->val.type = T_String;
					n->val.val.str = "now";
					n->typename = t;

					t->name = xlateSqlType("timestamp");
					t->setof = FALSE;
3957
					t->typmod = -1;
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975

					$$ = (Node *)n;
				}
		| CURRENT_TIMESTAMP '(' Iconst ')'
				{
					FuncCall *n = makeNode(FuncCall);
					A_Const *s = makeNode(A_Const);
					TypeName *t = makeNode(TypeName);

					n->funcname = xlateSqlType("timestamp");
					n->args = lcons(s, NIL);

					s->val.type = T_String;
					s->val.val.str = "now";
					s->typename = t;

					t->name = xlateSqlType("timestamp");
					t->setof = FALSE;
3976
					t->typmod = -1;
3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989

					if ($3 != 0)
						elog(NOTICE,"CURRENT_TIMESTAMP(%d) precision not implemented; zero used instead",$3);

					$$ = (Node *)n;
				}
		| CURRENT_USER
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3990 3991 3992 3993 3994 3995 3996
		| USER
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041
		| POSITION '(' position_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "strpos";
					n->args = $3;
					$$ = (Node *)n;
				}
		| SUBSTRING '(' substr_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "substr";
					n->args = $3;
					$$ = (Node *)n;
				}
		/* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
		| TRIM '(' BOTH trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' LEADING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "ltrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' TRAILING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "rtrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $3;
					$$ = (Node *)n;
				}
		;

4042
opt_indirection:  '[' a_expr ']' opt_indirection
4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058
				{
					A_Indices *ai = makeNode(A_Indices);
					ai->lidx = NULL;
					ai->uidx = $2;
					$$ = lcons(ai, $4);
				}
		| '[' a_expr ':' a_expr ']' opt_indirection
				{
					A_Indices *ai = makeNode(A_Indices);
					ai->lidx = $2;
					ai->uidx = $4;
					$$ = lcons(ai, $6);
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
4059

4060
expr_list:  a_expr_or_null
4061
				{ $$ = lcons($1, NIL); }
4062
		| expr_list ',' a_expr_or_null
4063
				{ $$ = lappend($1, $3); }
4064
		| expr_list USING a_expr
4065 4066
				{ $$ = lappend($1, $3); }
		;
4067

4068
extract_list:  extract_arg FROM a_expr
4069 4070 4071 4072 4073 4074 4075 4076 4077
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = $1;
					$$ = lappend(lcons((Node *)n,NIL), $3);
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
4078

4079 4080 4081
extract_arg:  datetime						{ $$ = $1; }
		| TIMEZONE_HOUR						{ $$ = "tz_hour"; }
		| TIMEZONE_MINUTE					{ $$ = "tz_minute"; }
4082 4083
		;

4084
position_list:  position_expr IN position_expr
4085
				{	$$ = makeList($3, $1, -1); }
4086 4087 4088
		| /* EMPTY */
				{	$$ = NIL; }
		;
4089

4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
position_expr:  attr opt_indirection
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
		| AexprConst
				{	$$ = $1;  }
		| '-' position_expr %prec UMINUS
				{	$$ = makeA_Expr(OP, "-", NULL, $2); }
		| position_expr '+' position_expr
				{	$$ = makeA_Expr(OP, "+", $1, $3); }
		| position_expr '-' position_expr
				{	$$ = makeA_Expr(OP, "-", $1, $3); }
		| position_expr '/' position_expr
				{	$$ = makeA_Expr(OP, "/", $1, $3); }
		| position_expr '*' position_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| '|' position_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
4109
		| position_expr TYPECAST Typename
4110
				{
4111
					$$ = (Node *)$1;
4112
					/* AexprConst can be either A_Const or ParamNo */
4113
					if (nodeTag($1) == T_A_Const) {
4114
						((A_Const *)$1)->typename = $3;
4115
					} else if (nodeTag($1) == T_Param) {
4116
						((ParamNo *)$1)->typename = $3;
4117 4118 4119 4120 4121 4122 4123
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
4124
				}
4125
		| CAST '(' position_expr AS Typename ')'
4126
				{
4127
					$$ = (Node *)$3;
4128
					/* AexprConst can be either A_Const or ParamNo */
4129 4130 4131 4132
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($3) == T_Param) {
						((ParamNo *)$3)->typename = $5;
4133 4134 4135
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
4136 4137
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
4138 4139
						$$ = (Node *)n;
					}
4140 4141 4142 4143 4144 4145 4146 4147 4148
				}
		| '(' position_expr ')'
				{	$$ = $2; }
		| position_expr Op position_expr
				{	$$ = makeA_Expr(OP, $2, $1, $3); }
		| Op position_expr
				{	$$ = makeA_Expr(OP, $1, NULL, $2); }
		| position_expr Op
				{	$$ = makeA_Expr(OP, $2, $1, NULL); }
4149
		| ColId
4150 4151 4152 4153 4154 4155 4156
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
4157
		| func_name '(' ')'
4158 4159 4160 4161 4162 4163
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
					$$ = (Node *)n;
				}
4164
		| func_name '(' expr_list ')'
4165 4166 4167 4168 4169 4170
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215
		| POSITION '(' position_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "strpos";
					n->args = $3;
					$$ = (Node *)n;
				}
		| SUBSTRING '(' substr_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "substr";
					n->args = $3;
					$$ = (Node *)n;
				}
		/* various trim expressions are defined in SQL92 - thomas 1997-07-19 */
		| TRIM '(' BOTH trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' LEADING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "ltrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' TRAILING trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "rtrim";
					n->args = $4;
					$$ = (Node *)n;
				}
		| TRIM '(' trim_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "btrim";
					n->args = $3;
					$$ = (Node *)n;
				}
		;

4216
substr_list:  expr_list substr_from substr_for
4217
				{
4218
					$$ = nconc(nconc($1,$2),$3);
4219 4220 4221 4222
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
4223

4224
substr_from:  FROM expr_list
4225 4226
				{	$$ = $2; }
		| /* EMPTY */
4227 4228 4229 4230 4231 4232
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Integer;
					n->val.val.ival = 1;
					$$ = lcons((Node *)n,NIL);
				}
4233
		;
4234

4235
substr_for:  FOR expr_list
4236 4237 4238 4239
				{	$$ = $2; }
		| /* EMPTY */
				{	$$ = NIL; }
		;
4240

4241
trim_list:  a_expr FROM expr_list
4242
				{ $$ = lappend($3, $1); }
4243
		| FROM expr_list
4244
				{ $$ = $2; }
4245
		| expr_list
4246 4247
				{ $$ = $1; }
		;
4248

4249 4250
in_expr:  SubSelect
				{
4251 4252 4253
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
4254 4255 4256 4257 4258
				}
		| in_expr_nodes
				{	$$ = $1; }
		;

4259
in_expr_nodes:  AexprConst
4260
				{	$$ = makeA_Expr(OP, "=", lfirst(saved_In_Expr), $1); }
4261
		| in_expr_nodes ',' AexprConst
4262
				{	$$ = makeA_Expr(OR, NULL, $1,
4263
						makeA_Expr(OP, "=", lfirst(saved_In_Expr), $3));
4264 4265
				}
		;
4266

4267 4268
not_in_expr:  SubSelect
				{
4269 4270 4271
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
4272 4273 4274 4275 4276
				}
		| not_in_expr_nodes
				{	$$ = $1; }
		;

4277
not_in_expr_nodes:  AexprConst
4278
				{	$$ = makeA_Expr(OP, "<>", lfirst(saved_In_Expr), $1); }
4279
		| not_in_expr_nodes ',' AexprConst
4280
				{	$$ = makeA_Expr(AND, NULL, $1,
4281
						makeA_Expr(OP, "<>", lfirst(saved_In_Expr), $3));
4282 4283
				}
		;
4284

4285
attr:  relation_name '.' attrs
4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
				{
					$$ = makeNode(Attr);
					$$->relname = $1;
					$$->paramNo = NULL;
					$$->attrs = $3;
					$$->indirection = NULL;
				}
		| ParamNo '.' attrs
				{
					$$ = makeNode(Attr);
					$$->relname = NULL;
					$$->paramNo = $1;
					$$->attrs = $3;
					$$->indirection = NULL;
				}
		;

attrs:	  attr_name
				{ $$ = lcons(makeString($1), NIL); }
		| attrs '.' attr_name
				{ $$ = lappend($1, makeString($3)); }
		| attrs '.' '*'
				{ $$ = lappend($1, makeString("*")); }
		;

4311 4312 4313

/*****************************************************************************
 *
4314
 *	target lists
4315 4316 4317
 *
 *****************************************************************************/

4318
res_target_list:  res_target_list ',' res_target_el
4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
				{	$$ = lappend($1,$3);  }
		| res_target_el
				{	$$ = lcons($1, NIL);  }
		| '*'
				{
					ResTarget *rt = makeNode(ResTarget);
					Attr *att = makeNode(Attr);
					att->relname = "*";
					att->paramNo = NULL;
					att->attrs = NULL;
					att->indirection = NIL;
					rt->name = NULL;
					rt->indirection = NULL;
					rt->val = (Node *)att;
					$$ = lcons(rt, NIL);
				}
		;
4336

4337
res_target_el:  ColId opt_indirection '=' a_expr_or_null
4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363
				{
					$$ = makeNode(ResTarget);
					$$->name = $1;
					$$->indirection = $2;
					$$->val = (Node *)$4;
				}
		| attr opt_indirection
				{
					$$ = makeNode(ResTarget);
					$$->name = NULL;
					$$->indirection = $2;
					$$->val = (Node *)$1;
				}
		| relation_name '.' '*'
				{
					Attr *att = makeNode(Attr);
					att->relname = $1;
					att->paramNo = NULL;
					att->attrs = lcons(makeString("*"), NIL);
					att->indirection = NIL;
					$$ = makeNode(ResTarget);
					$$->name = NULL;
					$$->indirection = NULL;
					$$->val = (Node *)att;
				}
		;
4364 4365 4366

/*
** target list for select.
B
Bruce Momjian 已提交
4367
** should get rid of the other but is still needed by the defunct select into
4368 4369
** and update (uses a subset)
*/
4370
res_target_list2:  res_target_list2 ',' res_target_el2
4371 4372 4373 4374
				{	$$ = lappend($1, $3);  }
		| res_target_el2
				{	$$ = lcons($1, NIL);  }
		;
4375 4376

/* AS is not optional because shift/red conflict with unary ops */
4377
res_target_el2:  a_expr_or_null AS ColLabel
4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
				{
					$$ = makeNode(ResTarget);
					$$->name = $3;
					$$->indirection = NULL;
					$$->val = (Node *)$1;
				}
		| a_expr_or_null
				{
					$$ = makeNode(ResTarget);
					$$->name = NULL;
					$$->indirection = NULL;
					$$->val = (Node *)$1;
				}
		| relation_name '.' '*'
				{
					Attr *att = makeNode(Attr);
					att->relname = $1;
					att->paramNo = NULL;
					att->attrs = lcons(makeString("*"), NIL);
					att->indirection = NIL;
					$$ = makeNode(ResTarget);
					$$->name = NULL;
					$$->indirection = NULL;
					$$->val = (Node *)att;
				}
		| '*'
				{
					Attr *att = makeNode(Attr);
					att->relname = "*";
					att->paramNo = NULL;
					att->attrs = NULL;
					att->indirection = NIL;
					$$ = makeNode(ResTarget);
					$$->name = NULL;
					$$->indirection = NULL;
					$$->val = (Node *)att;
				}
		;

4417
opt_id:  ColId									{ $$ = $1; }
4418 4419 4420 4421 4422 4423
		| /* EMPTY */							{ $$ = NULL; }
		;

relation_name:	SpecialRuleRelation
				{
					$$ = $1;
4424
					StrNCpy(saved_relname, $1, NAMEDATALEN);
4425 4426 4427
				}
		| ColId
				{
4428
					/* disallow refs to variable system tables */
4429
					if (strcmp(LogRelationName, $1) == 0
4430
					   || strcmp(VariableRelationName, $1) == 0)
4431
						elog(ERROR,"%s cannot be accessed by users",$1);
4432 4433
					else
						$$ = $1;
4434
					StrNCpy(saved_relname, $1, NAMEDATALEN);
4435 4436 4437
				}
		;

4438
database_name:			ColId			{ $$ = $1; };
4439
access_method:			IDENT			{ $$ = $1; };
4440
attr_name:				ColId			{ $$ = $1; };
4441
class:					IDENT			{ $$ = $1; };
4442
index_name:				ColId			{ $$ = $1; };
4443

4444 4445 4446 4447 4448
/* Functions
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
 */
name:					ColId			{ $$ = $1; };
4449
func_name:				ColId			{ $$ = xlateSqlFunc($1); };
4450 4451

file_name:				Sconst			{ $$ = $1; };
4452
recipe_name:			IDENT			{ $$ = $1; };
4453

4454 4455 4456
/* Constants
 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
 */
4457
AexprConst:  Iconst
4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Integer;
					n->val.val.ival = $1;
					$$ = (Node *)n;
				}
		| FCONST
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Float;
					n->val.val.dval = $1;
					$$ = (Node *)n;
				}
		| Sconst
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = $1;
					$$ = (Node *)n;
				}
4478 4479 4480 4481 4482 4483 4484 4485
		| Typename Sconst
				{
					A_Const *n = makeNode(A_Const);
					n->typename = $1;
					n->val.type = T_String;
					n->val.val.str = $2;
					$$ = (Node *)n;
				}
4486 4487
		| ParamNo
				{	$$ = (Node *)$1;  }
4488 4489 4490 4491 4492
		| TRUE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "t";
4493 4494
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
4495
					n->typename->typmod = -1;
4496 4497 4498 4499 4500 4501 4502
					$$ = (Node *)n;
				}
		| FALSE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "f";
4503 4504
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
4505
					n->typename->typmod = -1;
4506 4507
					$$ = (Node *)n;
				}
4508
		;
4509 4510

ParamNo:  PARAM
4511 4512 4513 4514 4515
				{
					$$ = makeNode(ParamNo);
					$$->number = $1;
				}
		;
4516

4517 4518
Iconst:  ICONST							{ $$ = $1; };
Sconst:  SCONST							{ $$ = $1; };
4519
UserId:  IDENT							{ $$ = $1; };
4520

4521 4522 4523 4524 4525
/* Column and type identifier
 * Does not include explicit datetime types
 *  since these must be decoupled in Typename syntax.
 * Use ColId for most identifiers. - thomas 1997-10-21
 */
4526 4527 4528 4529 4530 4531 4532
TypeId:  ColId
			{	$$ = xlateSqlType($1); }
		| numeric
			{	$$ = xlateSqlType($1); }
		| character
			{	$$ = xlateSqlType($1); }
		;
4533 4534 4535
/* Column identifier
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
4536 4537 4538 4539
 * Add other keywords. Note that as the syntax expands,
 *  some of these keywords will have to be removed from this
 *  list due to shift/reduce conflicts in yacc. If so, move
 *  down to the ColLabel entity. - thomas 1997-11-06
4540
 */
4541
ColId:  IDENT							{ $$ = $1; }
4542
		| datetime						{ $$ = $1; }
4543
		| ACTION						{ $$ = "action"; }
4544 4545
		| CACHE							{ $$ = "cache"; }
		| CYCLE							{ $$ = "cycle"; }
4546 4547
		| DATABASE						{ $$ = "database"; }
		| DELIMITERS					{ $$ = "delimiters"; }
4548 4549
		| DOUBLE						{ $$ = "double"; }
		| EACH							{ $$ = "each"; }
M
 
Marc G. Fournier 已提交
4550
		| ENCODING						{ $$ = "encoding"; }
4551
		| FUNCTION						{ $$ = "function"; }
4552
		| INCREMENT						{ $$ = "increment"; }
4553 4554 4555 4556 4557
		| INDEX							{ $$ = "index"; }
		| KEY							{ $$ = "key"; }
		| LANGUAGE						{ $$ = "language"; }
		| LOCATION						{ $$ = "location"; }
		| MATCH							{ $$ = "match"; }
4558 4559
		| MAXVALUE						{ $$ = "maxvalue"; }
		| MINVALUE						{ $$ = "minvalue"; }
4560 4561
		| OPERATOR						{ $$ = "operator"; }
		| OPTION						{ $$ = "option"; }
4562
		| PASSWORD						{ $$ = "password"; }
4563 4564
		| PRIVILEGES					{ $$ = "privileges"; }
		| RECIPE						{ $$ = "recipe"; }
4565
		| ROW							{ $$ = "row"; }
4566
		| SERIAL						{ $$ = "serial"; }
4567
		| START							{ $$ = "start"; }
4568
		| STATEMENT						{ $$ = "statement"; }
4569
		| TIME							{ $$ = "time"; }
4570 4571
		| TIMEZONE_HOUR					{ $$ = "timezone_hour"; }
		| TIMEZONE_MINUTE				{ $$ = "timezone_minute"; }
4572
		| TRIGGER						{ $$ = "trigger"; }
4573
		| TYPE_P						{ $$ = "type"; }
4574
		| VALID							{ $$ = "valid"; }
4575 4576
		| VERSION						{ $$ = "version"; }
		| ZONE							{ $$ = "zone"; }
4577 4578 4579 4580 4581 4582 4583 4584
		;

/* Column label
 * Allowed labels in "AS" clauses.
 * Include TRUE/FALSE SQL3 reserved words for Postgres backward
 *  compatibility. Cannot allow this for column names since the
 *  syntax would not distinguish between the constant value and
 *  a column name. - thomas 1997-10-24
4585 4586 4587
 * Add other keywords to this list. Note that they appear here
 *  rather than in ColId if there was a shift/reduce conflict
 *  when used as a full identifier. - thomas 1997-11-06
4588 4589
 */
ColLabel:  ColId						{ $$ = $1; }
4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601
		| ARCHIVE						{ $$ = "archive"; }
		| CLUSTER						{ $$ = "cluster"; }
		| CONSTRAINT					{ $$ = "constraint"; }
		| CROSS							{ $$ = "cross"; }
		| FOREIGN						{ $$ = "foreign"; }
		| GROUP							{ $$ = "group"; }
		| LOAD							{ $$ = "load"; }
		| ORDER							{ $$ = "order"; }
		| POSITION						{ $$ = "position"; }
		| PRECISION						{ $$ = "precision"; }
		| TABLE							{ $$ = "table"; }
		| TRANSACTION					{ $$ = "transaction"; }
4602 4603
		| TRUE_P						{ $$ = "true"; }
		| FALSE_P						{ $$ = "false"; }
4604
		;
4605 4606

SpecialRuleRelation:  CURRENT
4607 4608 4609 4610
				{
					if (QueryIsRule)
						$$ = "*CURRENT*";
					else
B
Bruce Momjian 已提交
4611
						elog(ERROR,"CURRENT used in non-rule query");
4612 4613 4614 4615 4616 4617
				}
		| NEW
				{
					if (QueryIsRule)
						$$ = "*NEW*";
					else
B
Bruce Momjian 已提交
4618
						elog(ERROR,"NEW used in non-rule query");
4619 4620
				}
		;
4621 4622 4623

%%

4624 4625
static Node *
makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
4626
{
4627 4628 4629 4630 4631 4632
	A_Expr *a = makeNode(A_Expr);
	a->oper = oper;
	a->opname = opname;
	a->lexpr = lexpr;
	a->rexpr = rexpr;
	return (Node *)a;
4633 4634
}

B
Bruce Momjian 已提交
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
/* makeRowExpr()
 * Generate separate operator nodes for a single row descriptor expression.
 * Perhaps this should go deeper in the parser someday... - thomas 1997-12-22
 */
static Node *
makeRowExpr(char *opr, List *largs, List *rargs)
{
	Node *expr = NULL;
	Node *larg, *rarg;

	if (length(largs) != length(rargs))
		elog(ERROR,"Unequal number of entries in row expression");

	if (lnext(largs) != NIL)
		expr = makeRowExpr(opr,lnext(largs),lnext(rargs));

	larg = lfirst(largs);
	rarg = lfirst(rargs);

	if ((strcmp(opr, "=") == 0)
	 || (strcmp(opr, "<") == 0)
	 || (strcmp(opr, "<=") == 0)
	 || (strcmp(opr, ">") == 0)
	 || (strcmp(opr, ">=") == 0))
	{
		if (expr == NULL)
			expr = makeA_Expr(OP, opr, larg, rarg);
		else
			expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
	}
	else if (strcmp(opr, "<>") == 0)
	{
		if (expr == NULL)
			expr = makeA_Expr(OP, opr, larg, rarg);
		else
			expr = makeA_Expr(OR, NULL, expr, makeA_Expr(OP, opr, larg, rarg));
	}
	else
	{
		elog(ERROR,"Operator '%s' not implemented for row expressions",opr);
	}

#if FALSE
	while ((largs != NIL) && (rargs != NIL))
	{
		larg = lfirst(largs);
		rarg = lfirst(rargs);

		if (expr == NULL)
			expr = makeA_Expr(OP, opr, larg, rarg);
		else
			expr = makeA_Expr(AND, NULL, expr, makeA_Expr(OP, opr, larg, rarg));

		largs = lnext(largs);
		rargs = lnext(rargs);
	}
	pprint(expr);
#endif

	return expr;
}

4697 4698 4699 4700 4701 4702 4703
void
mapTargetColumns(List *src, List *dst)
{
	ColumnDef *s;
	ResTarget *d;

	if (length(src) != length(dst))
B
Bruce Momjian 已提交
4704
		elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718

	while ((src != NIL) && (dst != NIL))
	{
		s = (ColumnDef *)lfirst(src);
		d = (ResTarget *)lfirst(dst);

		d->name = s->colname;

		src = lnext(src);
		dst = lnext(dst);
	}

	return;
} /* mapTargetColumns() */
4719

4720 4721 4722 4723 4724
static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr)
{
	Node *result = NULL;

	/* we do this so indexes can be used */
4725 4726
	if (strcmp(opname,"~") == 0 ||
		strcmp(opname,"~*") == 0)
4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743
	{
		if (nodeTag(rexpr) == T_A_Const &&
		   ((A_Const *)rexpr)->val.type == T_String &&
		   ((A_Const *)rexpr)->val.val.str[0] == '^')
		{
			A_Const *n = (A_Const *)rexpr;
			char *match_least = palloc(strlen(n->val.val.str)+2);
			char *match_most = palloc(strlen(n->val.val.str)+2);
			int pos, match_pos=0;

			/* skip leading ^ */
			for (pos = 1; n->val.val.str[pos]; pos++)
			{
				if (n->val.val.str[pos] == '.' ||
					n->val.val.str[pos] == '?' ||
					n->val.val.str[pos] == '*' ||
					n->val.val.str[pos] == '[' ||
4744
					n->val.val.str[pos] == '$' ||
4745
					(strcmp(opname,"~*") == 0 && isalpha(n->val.val.str[pos])))
4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
		     		break;
		     	if (n->val.val.str[pos] == '\\')
					pos++;
				match_least[match_pos] = n->val.val.str[pos];
				match_most[match_pos++] = n->val.val.str[pos];
			}

			if (match_pos != 0)
			{
				A_Const *least = makeNode(A_Const);
				A_Const *most = makeNode(A_Const);
				
				/* make strings to be used in index use */
				match_least[match_pos] = '\0';
				match_most[match_pos] = '\377';
				match_most[match_pos+1] = '\0';
				least->val.type = T_String;
				least->val.val.str = match_least;
				most->val.type = T_String;
				most->val.val.str = match_most;
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~", lexpr, rexpr),
						makeA_Expr(AND, NULL,
							makeA_Expr(OP, ">=", lexpr, (Node *)least),
							makeA_Expr(OP, "<=", lexpr, (Node *)most)));
			}
		}
	}
	else if (strcmp(opname,"~~") == 0)
	{
		if (nodeTag(rexpr) == T_A_Const &&
		   ((A_Const *)rexpr)->val.type == T_String)
		{
			A_Const *n = (A_Const *)rexpr;
			char *match_least = palloc(strlen(n->val.val.str)+2);
			char *match_most = palloc(strlen(n->val.val.str)+2);
			int pos, match_pos=0;
	
			for (pos = 0; n->val.val.str[pos]; pos++)
			{
4786 4787 4788 4789 4790 4791 4792
				if (n->val.val.str[pos] == '%' &&
					 n->val.val.str[pos+1] != '%')
					break;
				if(n->val.val.str[pos] == '_')
			     		break;
			     	if (n->val.val.str[pos] == '\\' ||
				     	n->val.val.str[pos] == '%')
4793
					pos++;
4794 4795
				if (n->val.val.str[pos] == '\0')
					break;
4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824
				match_least[match_pos] = n->val.val.str[pos];
				match_most[match_pos++] = n->val.val.str[pos];
			}
	
			if (match_pos != 0)
			{
				A_Const *least = makeNode(A_Const);
				A_Const *most = makeNode(A_Const);
				
				/* make strings to be used in index use */
				match_least[match_pos] = '\0';
				match_most[match_pos] = '\377';
				match_most[match_pos+1] = '\0';
				least->val.type = T_String;
				least->val.val.str = match_least;
				most->val.type = T_String;
				most->val.val.str = match_most;
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~~", lexpr, rexpr),
						makeA_Expr(AND, NULL,
							makeA_Expr(OP, ">=", lexpr, (Node *)least),
							makeA_Expr(OP, "<=", lexpr, (Node *)most)));
			}
		}
	}
	
	if (result == NULL)
		result = makeA_Expr(OP, opname, lexpr, rexpr);
	return result;
4825
} /* makeIndexable() */
4826 4827


4828
/* xlateSqlFunc()
4829
 * Convert alternate type names to internal Postgres types.
4830 4831
 * Do not convert "float", since that is handled elsewhere
 *  for FLOAT(p) syntax.
4832
 */
4833
static char *
4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846
xlateSqlFunc(char *name)
{
	if (!strcasecmp(name,"character_length")
	 || !strcasecmp(name,"char_length"))
		return "length";
	else
		return name;
} /* xlateSqlFunc() */

/* xlateSqlType()
 * Convert alternate type names to internal Postgres types.
 */
static char *
4847 4848
xlateSqlType(char *name)
{
4849 4850
	if (!strcasecmp(name,"int")
	 || !strcasecmp(name,"integer"))
4851
		return "int4";
4852 4853
	else if (!strcasecmp(name, "smallint"))
		return "int2";
4854 4855
	else if (!strcasecmp(name, "real")
	 || !strcasecmp(name, "float"))
4856 4857 4858
		return "float8";
	else if (!strcasecmp(name, "interval"))
		return "timespan";
4859 4860
	else if (!strcasecmp(name, "boolean"))
		return "bool";
4861 4862
	else
		return name;
4863
} /* xlateSqlType() */
4864

4865

4866 4867
void parser_init(Oid *typev, int nargs)
{
4868
	QueryIsRule = FALSE;
4869 4870
	saved_relname[0]= '\0';
	saved_In_Expr = NULL;
4871

4872
	param_type_init(typev, nargs);
4873
}
4874

4875

4876 4877 4878
/* FlattenStringList()
 * Traverse list of string nodes and convert to a single string.
 * Used for reconstructing string form of complex expressions.
4879 4880
 *
 * Allocate at least one byte for terminator.
4881 4882 4883 4884
 */
static char *
FlattenStringList(List *list)
{
4885 4886
	List *l;
	Value *v;
4887 4888
	char *s;
	char *sp;
4889
	int nlist, len = 0;
4890 4891 4892 4893

	nlist = length(list);
	l = list;
	while(l != NIL) {
4894 4895
		v = (Value *)lfirst(l);
		sp = v->val.str;
4896
		l = lnext(l);
4897
		len += strlen(sp);
4898
	};
4899
	len += nlist;
4900

4901
	s = (char*) palloc(len+1);
4902 4903 4904 4905
	*s = '\0';

	l = list;
	while(l != NIL) {
4906 4907
		v = (Value *)lfirst(l);
		sp = v->val.str;
4908 4909
		l = lnext(l);
		strcat(s,sp);
4910
		if (l != NIL) strcat(s," ");
4911
	};
4912
	*(s+len) = '\0';
4913 4914

#ifdef PARSEDEBUG
4915
	elog(DEBUG, "flattened string is \"%s\"\n", s);
4916 4917
#endif

4918
	return s;
4919 4920
} /* FlattenStringList() */

4921

4922 4923 4924 4925 4926 4927
/* makeConstantList()
 * Convert constant value node into string node.
 */
static List *
makeConstantList( A_Const *n)
{
4928 4929
	List *result = NIL;
	char *typval = NULL;
4930
	char *defval = NULL;
4931

4932
	if (nodeTag(n) != T_A_Const) {
B
Bruce Momjian 已提交
4933
		elog(ERROR,"Cannot handle non-constant parameter");
4934 4935 4936 4937

	} else if (n->val.type == T_Float) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%g", n->val.val.dval);
4938
		result = lcons( makeString(defval), NIL);
4939 4940 4941 4942

	} else if (n->val.type == T_Integer) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%ld", n->val.val.ival);
4943
		result = lcons( makeString(defval), NIL);
4944 4945 4946 4947 4948 4949

	} else if (n->val.type == T_String) {
		defval = (char*) palloc(strlen( ((A_Const *) n)->val.val.str) + 3);
		strcpy( defval, "'");
		strcat( defval, ((A_Const *) n)->val.val.str);
		strcat( defval, "'");
4950 4951 4952 4953 4954 4955 4956 4957 4958 4959
		if (n->typename != NULL)
		{
			typval = (char*) palloc(strlen( n->typename->name) + 1);
			strcpy(typval, n->typename->name);
			result = lappend( lcons( makeString(typval), NIL), makeString(defval));
		}
		else
		{
			result = lcons( makeString(defval), NIL);
		}
4960 4961

	} else {
B
Bruce Momjian 已提交
4962
		elog(ERROR,"Internal error in makeConstantList(): cannot encode node");
4963 4964 4965
	};

#ifdef PARSEDEBUG
4966
	elog(DEBUG, "AexprConst argument is \"%s\"\n", defval);
4967 4968
#endif

4969
	return result;
4970
} /* makeConstantList() */
4971

4972

4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994
/* fmtId()
 * Check input string for non-lowercase/non-numeric characters.
 * Returns either input string or input surrounded by double quotes.
 */
static char *
fmtId(char *rawid)
{
	static char *cp;

	for (cp = rawid; *cp != '\0'; cp++)
		if (! (islower(*cp) || isdigit(*cp) || (*cp == '_'))) break;

	if (*cp != '\0') {
		cp = palloc(strlen(rawid)+1);
		strcpy(cp,"\"");
		strcat(cp,rawid);
		strcat(cp,"\"");
	} else {
		cp = rawid;
	};

#ifdef PARSEDEBUG
4995 4996
	elog(DEBUG, "fmtId- %sconvert %s to %s\n",
	 ((cp == rawid)? "do not ": ""), rawid, cp);
4997 4998
#endif

4999
	return cp;
5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020
}

/*
 * param_type_init()
 *
 * keep enough information around fill out the type of param nodes
 * used in postquel functions
 */
static void
param_type_init(Oid *typev, int nargs)
{
	pfunc_num_args = nargs;
	param_type_info = typev;
}

Oid param_type(int t)
{
	if ((t > pfunc_num_args) || (t == 0))
		return InvalidOid;
	return param_type_info[t - 1];
}