gram.y 140.5 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.62 1999/03/17 21:02:57 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
#include "catalog/catname.h"
#include "utils/elog.h"
#include "access/xact.h"
48
#include "storage/lmgr.h"
49
#include "utils/numeric.h"
B
Hi!  
Bruce Momjian 已提交
50
#include "parser/analyze.h"
51
#include "catalog/pg_type.h"
52

M
 
Marc G. Fournier 已提交
53
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
54 55 56
#include "mb/pg_wchar.h"
#endif

57
static char saved_relname[NAMEDATALEN];  /* need this for complex attributes */
58
static bool QueryIsRule = FALSE;
59
static List *saved_In_Expr = NIL;
60 61
static Oid	*param_type_info;
static int	pfunc_num_args;
62 63
extern List *parsetree;

64

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

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

Oid	param_type(int t); /* used in parse_expr.c */
84 85 86 87 88 89 90 91

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


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

	Attr				*attr;

	TypeName			*typnam;
	DefElem				*defelt;
	ParamString			*param;
	SortGroupBy			*sortgroupby;
110
	JoinExpr			*joinexpr;
111 112 113 114 115 116 117 118 119 120
	IndexElem			*ielem;
	RangeVar			*range;
	RelExpr				*relexp;
	A_Indices			*aind;
	ResTarget			*target;
	ParamNo				*paramno;

	VersionStmt			*vstmt;
	DefineStmt			*dstmt;
	RuleStmt			*rstmt;
B
Bruce Momjian 已提交
121
	InsertStmt			*astmt;
122 123
}

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

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

140 141
%type <str>	opt_lmode

142 143 144 145 146
%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

147
%type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
148

149
%type <str>		TriggerEvents, TriggerFuncArg
150

151 152
%type <str>		relation_name, copy_file_name, copy_delimiter, def_name,
		database_name, access_method_clause, access_method, attr_name,
B
Bruce Momjian 已提交
153
		class, index_name, name, func_name, file_name, aggr_argtype
154

155
%type <str>		opt_id, opt_portal_name,
B
Bruce Momjian 已提交
156
		all_Op, MathOp, opt_name, opt_unique,
157
		OptUseOp, opt_class, SpecialRuleRelation
158

159 160
%type <str>		privileges, operation_commalist, grantee
%type <chr>		operation, TriggerOneEvent
161

162
%type <list>	stmtblock, stmtmulti,
163
		result, relation_name_list, OptTableElementList,
164
		OptInherit, definition,
165
		opt_with, func_args, func_args_list,
166
		oper_argtypes, RuleActionList, RuleActionBlock, RuleActionMulti,
167 168
		opt_column_list, columnList, opt_va_list, va_list,
		sort_clause, sortby_list, index_params, index_list, name_list,
169
		from_clause, from_expr, table_list, opt_array_bounds, nest_array_bounds,
170
		expr_list, attrs, res_target_list, res_target_list2,
171 172
		def_list, opt_indirection, group_clause, TriggerFuncArgs,
		opt_select_limit
173

174 175 176
%type <node>	func_return
%type <boolean>	set_opt

177
%type <boolean>	TriggerForOpt, TriggerForType, OptTemp
178

179
%type <list>	for_update_clause, update_list
180
%type <boolean>	opt_union
181
%type <boolean>	opt_table
182
%type <boolean>	opt_trans
183

184 185 186 187 188
%type <list>	join_clause_with_union, join_clause, join_list, join_qual, using_list
%type <node>	join_expr, using_expr
%type <str>		join_outer
%type <ival>	join_type

189
%type <node>	position_expr
190 191
%type <list>	extract_list, position_list
%type <list>	substr_list, substr_from, substr_for, trim_list
192
%type <list>	opt_interval
193

194 195
%type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
				index_opt_unique, opt_verbose, opt_analyze
196
%type <boolean> cursor_clause, opt_cursor, opt_readonly, opt_of
197

B
Bruce Momjian 已提交
198 199
%type <ival>	copy_dirn, def_type, opt_direction, remove_type,
				opt_column, event
200

B
Bruce Momjian 已提交
201
%type <ival>	fetch_how_many
202

203 204
%type <node>	select_limit_value, select_offset_value

V
Vadim B. Mikheev 已提交
205 206 207
%type <list>	OptSeqList
%type <defelt>	OptSeqElem

208 209 210
%type <dstmt>	def_rest
%type <astmt>	insert_rest

211 212
%type <node>	OptTableElement, ConstraintElem
%type <node>	columnDef, alter_clause
213
%type <defelt>	def_elem
214
%type <node>	def_arg, columnElem, where_clause,
215
				a_expr, a_expr_or_null, b_expr, AexprConst,
216
				in_expr, in_expr_nodes, not_in_expr, not_in_expr_nodes,
217
				having_clause
218
%type <list>	row_descriptor, row_list, c_list, c_expr
219
%type <node>	row_expr
220
%type <str>		row_op
T
Thomas G. Lockhart 已提交
221 222
%type <node>	case_expr, case_arg, when_clause, case_default
%type <list>	when_clause_list
223
%type <ival>	sub_type
224 225
%type <list>	OptCreateAs, CreateAsList
%type <node>	CreateAsElement
226
%type <value>	NumericOnly, FloatOnly, IntegerOnly
227
%type <attr>	event_object, attr
228
%type <sortgroupby>		sortby
229
%type <ielem>	index_elem, func_index
230
%type <range>	table_expr
231 232
%type <relexp>	relation_expr
%type <target>	res_target_el, res_target_el2
233
%type <paramno> ParamNo
234

235
%type <typnam>	Typename, opt_type, Array, Generic, Character, Datetime, Numeric
236
%type <str>		generic, numeric, character, datetime
237
%type <str>		extract_arg
238
%type <str>		opt_charset, opt_collate
239 240
%type <str>		opt_float
%type <ival>	opt_numeric, opt_decimal
241 242
%type <boolean>	opt_varying, opt_timezone

243
%type <ival>	Iconst
244
%type <str>		Sconst
245
%type <str>		UserId, var_value, zone_value
246
%type <str>		ColId, ColLabel
247
%type <str>		TypeId
248

249
%type <node>	TableConstraint
250 251
%type <list>	constraint_list, constraint_expr
%type <list>	default_list, default_expr
252
%type <list>	ColPrimaryKey, ColQualList, ColQualifier
253
%type <node>	ColConstraint, ColConstraintElem
254 255
%type <list>	key_actions, key_action
%type <str>		key_match, key_reference
256 257 258

/*
 * If you make any token changes, remember to:
259 260
 *		- use "yacc -d" and update parse.h
 *		- update the keyword table in parser/keywords.c
261 262
 */

263 264 265 266 267 268 269 270 271 272
/* 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) */
273
%token	ABSOLUTE, ACTION, ADD, ALL, ALTER, AND, ANY, AS, ASC,
274
		BEGIN_TRANS, BETWEEN, BOTH, BY,
T
Thomas G. Lockhart 已提交
275 276
		CASCADE, CASE, CAST, CHAR, CHARACTER, CHECK, CLOSE,
		COALESCE, COLLATE, COLUMN, COMMIT, 
277 278
		CONSTRAINT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME, 
		CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
279
		DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
B
Hi!  
Bruce Momjian 已提交
280
		ELSE, END_TRANS, EXCEPT, EXECUTE, EXISTS, EXTRACT,
281
		FALSE_P, FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
282
		GRANT, GROUP, HAVING, HOUR_P,
B
Hi!  
Bruce Momjian 已提交
283 284
		IN, INNER_P, INSENSITIVE, INSERT, INTERSECT, INTERVAL, INTO, IS,
		ISOLATION, JOIN, KEY, LANGUAGE, LEADING, LEFT, LEVEL, LIKE, LOCAL,
M
 
Marc G. Fournier 已提交
285
		MATCH, MINUTE_P, MONTH_P, NAMES,
T
Thomas G. Lockhart 已提交
286
		NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULLIF, NULL_P, NUMERIC,
287 288 289 290
		OF, ON, ONLY, OPTION, OR, ORDER, OUTER_P,
		PARTIAL, POSITION, PRECISION, PRIMARY, PRIOR, PRIVILEGES, PROCEDURE, PUBLIC,
		READ, REFERENCES, RELATIVE, REVOKE, RIGHT, ROLLBACK,
		SCROLL, SECOND_P, SELECT, SET, SUBSTRING,
291
		TABLE, TEMP, THEN, TIME, TIMESTAMP, TIMEZONE_HOUR, TIMEZONE_MINUTE,
292
		TO, TRAILING, TRANSACTION, TRIM, TRUE_P,
293
		UNION, UNIQUE, UPDATE, USER, USING,
294
		VALUES, VARCHAR, VARYING, VIEW,
T
Thomas G. Lockhart 已提交
295
		WHEN, WHERE, WITH, WORK, YEAR_P, ZONE
296 297

/* Keywords (in SQL3 reserved words) */
298
%token	TRIGGER
299 300 301 302

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

303 304 305 306 307 308 309 310 311
/* Keywords for Postgres support (not in SQL92 reserved words)
 *
 * The CREATEDB and CREATEUSER tokens should go away
 * when some sort of pg_privileges relation is introduced.
 * - Todd A. Brandys 1998-01-01?
 */
%token	ABORT_TRANS, AFTER, AGGREGATE, ANALYZE, BACKWARD, BEFORE, BINARY, 
		CACHE, CLUSTER, COPY, CREATEDB, CREATEUSER, CYCLE,
		DATABASE, DELIMITERS, DO, EACH, ENCODING, EXPLAIN, EXTEND,
B
Bruce Momjian 已提交
312
		FORWARD, FUNCTION, HANDLER,
313
		INCREMENT, INDEX, INHERITS, INSTEAD, ISNULL,
314 315
		LANCOMPILER, LIMIT, LISTEN, LOAD, LOCATION, LOCK_P,
		MAXVALUE, MINVALUE, MOVE,
316
		NEW, NOCREATEDB, NOCREATEUSER, NONE, NOTHING, NOTIFY, NOTNULL,
317
		OFFSET, OIDS, OPERATOR, PASSWORD, PROCEDURAL,
318
		RECIPE, RENAME, RESET, RETURNS, ROW, RULE,
319
		SEQUENCE, SERIAL, SETOF, SHOW, START, STATEMENT, STDIN, STDOUT, TRUSTED, 
320
		UNLISTEN, UNTIL, VACUUM, VALID, VERBOSE, VERSION
321

322
/* Special keywords, not in the query language - see the "lex" file */
323
%token <str>	IDENT, SCONST, Op
324 325 326
%token <ival>	ICONST, PARAM
%token <dval>	FCONST

327
/* these are not real. they are here so that they get generated as #define's*/
328
%token			OP
329 330

/* precedence */
331 332 333 334
%left		OR
%left		AND
%right		NOT
%right		'='
335
%nonassoc	'<' '>'
336 337 338
%nonassoc	LIKE
%nonassoc	BETWEEN
%nonassoc	IN
339
%left		Op				/* multi-character ops and user-defined operators */
340 341 342 343
%nonassoc	NOTNULL
%nonassoc	ISNULL
%nonassoc	IS
%left		'+' '-'
344
%left		'*' '/' '%'
345 346 347 348 349 350 351 352
%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
B
Hi!  
Bruce Momjian 已提交
353
%left		UNION INTERSECT EXCEPT
354 355
%%

356
stmtblock:  stmtmulti
357
				{ parsetree = $1; }
358
		| stmt
359 360
				{ parsetree = lcons($1,NIL); }
		;
361

362
stmtmulti:  stmtmulti stmt ';'
363
				{ $$ = lappend($1, $2); }
B
Hi!  
Bruce Momjian 已提交
364 365 366 367 368 369 370
/***S*I***/
/* We comment the next rule because it seems to be redundant
 * and produces 16 shift/reduce conflicts with the new SelectStmt rule
 * needed for EXCEPT and INTERSECTS. So far I did not notice any
 * violations by removing the rule! */
/* 		| stmtmulti stmt
				{ $$ = lappend($1, $2); } */
371
		| stmt ';'
372 373
				{ $$ = lcons($1,NIL); }
		;
374 375

stmt :	  AddAttrStmt
376
		| AlterUserStmt
377 378 379
		| ClosePortalStmt
		| CopyStmt
		| CreateStmt
380
		| CreateAsStmt
381
		| CreateSeqStmt
382
		| CreatePLangStmt
383
		| CreateTrigStmt
384
		| CreateUserStmt
385 386 387
		| ClusterStmt
		| DefineStmt
		| DestroyStmt
388
		| DropPLangStmt
389
		| DropTrigStmt
390
		| DropUserStmt
391 392 393 394 395 396
		| ExtendStmt
		| ExplainStmt
		| FetchStmt
		| GrantStmt
		| IndexStmt
		| ListenStmt
M
 
Marc G. Fournier 已提交
397
		| UnlistenStmt
398
		| LockStmt
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
		| ProcedureStmt
		| RemoveAggrStmt
		| RemoveOperStmt
		| RemoveFuncStmt
		| RemoveStmt
		| RenameStmt
		| RevokeStmt
		| OptimizableStmt
		| RuleStmt
		| TransactionStmt
		| ViewStmt
		| LoadStmt
		| CreatedbStmt
		| DestroydbStmt
		| VacuumStmt
		| VariableSetStmt
		| VariableShowStmt
		| VariableResetStmt
		;
418

419 420
/*****************************************************************************
 *
421
 * Create a new Postgres DBMS user
422 423 424 425
 *
 *
 *****************************************************************************/

426
CreateUserStmt:  CREATE USER UserId user_passwd_clause user_createdb_clause
427 428 429 430 431 432 433 434 435 436 437 438
			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;
				}
		;
439 440 441 442 443 444 445 446

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

447
AlterUserStmt:  ALTER USER UserId user_passwd_clause user_createdb_clause
448 449 450 451 452 453 454 455 456 457 458 459
			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;
				}
		;
460 461 462 463 464 465 466 467

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

468
DropUserStmt:  DROP USER UserId
469 470 471 472 473 474 475
				{
					DropUserStmt *n = makeNode(DropUserStmt);
					n->user = $3;
					$$ = (Node *)n;
				}
		;

476
user_passwd_clause:  WITH PASSWORD UserId		{ $$ = $3; }
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
			| /*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; }
		;

510
user_group_list:  user_group_list ',' UserId
511 512 513
				{
					$$ = lcons((void*)makeString($3), $1);
				}
514
			| UserId
515 516 517 518 519 520 521 522 523 524 525 526
				{
					$$ = 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; }
		;
527

528
/*****************************************************************************
529
 *
530
 * Set PG internal variable
531
 *	  SET name TO 'var_value'
532 533
 * Include SQL92 syntax (thomas 1997-10-22):
 *    SET TIME ZONE 'var_value'
534
 *
535 536
 *****************************************************************************/

537
VariableSetStmt:  SET ColId TO var_value
538 539 540 541 542 543
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
544
		| SET ColId '=' var_value
545 546 547 548 549 550
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
551
		| SET TIME ZONE zone_value
552 553 554 555 556 557
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "timezone";
					n->value = $4;
					$$ = (Node *) n;
				}
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
		| SET TRANSACTION ISOLATION LEVEL READ ColId
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "XactIsoLevel";
					n->value = $6;
					if (strcasecmp(n->value, "COMMITTED"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", n->value);
					$$ = (Node *) n;
				}
		| SET TRANSACTION ISOLATION LEVEL ColId
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "XactIsoLevel";
					n->value = $5;
					if (strcasecmp(n->value, "SERIALIZABLE"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", n->value);
					$$ = (Node *) n;
				}
M
 
Marc G. Fournier 已提交
576 577 578 579 580 581 582 583 584 585 586
		| 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
				}
587 588
		;

589
var_value:  Sconst			{ $$ = $1; }
590
		| DEFAULT			{ $$ = NULL; }
591 592
		;

593
zone_value:  Sconst			{ $$ = $1; }
594
		| DEFAULT			{ $$ = NULL; }
595
		| LOCAL				{ $$ = NULL; }
596
		;
597

598
VariableShowStmt:  SHOW ColId
599 600 601 602 603
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
604 605 606 607 608 609
		| SHOW TIME ZONE
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
610 611 612 613 614 615
		| SHOW TRANSACTION ISOLATION LEVEL
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = "XactIsoLevel";
					$$ = (Node *) n;
				}
616 617
		;

618
VariableResetStmt:	RESET ColId
619 620 621 622 623
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
624 625 626 627 628 629
		| RESET TIME ZONE
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
630 631 632 633 634 635
		| RESET TRANSACTION ISOLATION LEVEL
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = "XactIsoLevel";
					$$ = (Node *) n;
				}
636
		;
637

638

639 640
/*****************************************************************************
 *
641 642
 *		QUERY :
 *				addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
643 644 645
 *
 *****************************************************************************/

646
AddAttrStmt:  ALTER TABLE relation_name opt_inh_star alter_clause
647 648 649 650
				{
					AddAttrStmt *n = makeNode(AddAttrStmt);
					n->relname = $3;
					n->inh = $4;
651
					n->colDef = $5;
652 653 654
					$$ = (Node *)n;
				}
		;
655

656 657 658 659
alter_clause:  ADD opt_column columnDef
				{
					$$ = $3;
				}
660
			| ADD '(' OptTableElementList ')'
661
				{
662
					Node *lp = lfirst($3);
663 664

					if (length($3) != 1)
B
Bruce Momjian 已提交
665
						elog(ERROR,"ALTER TABLE/ADD() allows one column only");
666 667
					$$ = lp;
				}
668
			| DROP opt_column ColId
B
Bruce Momjian 已提交
669
				{	elog(ERROR,"ALTER TABLE/DROP COLUMN not yet implemented"); }
670
			| ALTER opt_column ColId SET DEFAULT default_expr
B
Bruce Momjian 已提交
671
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented"); }
672
			| ALTER opt_column ColId DROP DEFAULT
B
Bruce Momjian 已提交
673
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented"); }
674
			| ADD ConstraintElem
B
Bruce Momjian 已提交
675
				{	elog(ERROR,"ALTER TABLE/ADD CONSTRAINT not yet implemented"); }
676 677
		;

678 679

/*****************************************************************************
680
 *
681 682
 *		QUERY :
 *				close <optname>
683
 *
684 685
 *****************************************************************************/

686
ClosePortalStmt:  CLOSE opt_id
687 688 689 690 691 692
				{
					ClosePortalStmt *n = makeNode(ClosePortalStmt);
					n->portalname = $2;
					$$ = (Node *)n;
				}
		;
693 694 695 696


/*****************************************************************************
 *
697 698 699
 *		QUERY :
 *				COPY [BINARY] <relname> FROM/TO
 *				[USING DELIMITERS <delimiter>]
700 701 702
 *
 *****************************************************************************/

703
CopyStmt:  COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
704 705 706 707 708 709 710 711 712 713 714 715 716 717
				{
					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; }
718
		| FROM
719 720
				{ $$ = FROM; }
		;
721

722
/*
723 724
 * 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
725
 * stdout. We silently correct the "typo".		 - AY 9/94
726
 */
727 728 729 730
copy_file_name:  Sconst							{ $$ = $1; }
		| STDIN									{ $$ = NULL; }
		| STDOUT								{ $$ = NULL; }
		;
731

732 733
opt_binary:  BINARY								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
734
		;
735

736
opt_with_copy:	WITH OIDS						{ $$ = TRUE; }
737
		| /*EMPTY*/								{ $$ = FALSE; }
738
		;
739

740 741 742
/*
 * the default copy delimiter is tab but the user can configure it
 */
743 744
copy_delimiter:  USING DELIMITERS Sconst		{ $$ = $3; }
		| /*EMPTY*/								{ $$ = "\t"; }
745
		;
746 747 748 749


/*****************************************************************************
 *
750 751
 *		QUERY :
 *				CREATE relname
752 753 754
 *
 *****************************************************************************/

755
CreateStmt:  CREATE OptTemp TABLE relation_name '(' OptTableElementList ')'
756
				OptInherit
757 758
				{
					CreateStmt *n = makeNode(CreateStmt);
759 760 761 762
					n->istemp = $2;
					n->relname = $4;
					n->tableElts = $6;
					n->inhRelnames = $8;
763
					n->constraints = NIL;
764 765 766 767
					$$ = (Node *)n;
				}
		;

768 769 770 771
OptTemp:  		TEMP						{ $$ = TRUE; }
			| /*EMPTY*/						{ $$ = FALSE; }
		;

772
OptTableElementList:  OptTableElementList ',' OptTableElement
773 774 775 776 777 778 779 780 781 782 783 784 785
				{
					if ($3 != NULL)
						$$ = lappend($1, $3);
					else
						$$ = $1;
				}
			| OptTableElement
				{
					if ($1 != NULL)
						$$ = lcons($1, NIL);
					else
						$$ = NULL;
				}
786
			| /*EMPTY*/							{ $$ = NULL; }
787
		;
788

789 790
OptTableElement:  columnDef						{ $$ = $1; }
			| TableConstraint					{ $$ = $1; }
791
		;
792

793
columnDef:  ColId Typename ColQualifier
794 795 796 797 798 799 800
				{
					ColumnDef *n = makeNode(ColumnDef);
					n->colname = $1;
					n->typename = $2;
					n->defval = NULL;
					n->is_not_null = FALSE;
					n->constraints = $3;
801 802
					$$ = (Node *)n;
				}
803
			| ColId SERIAL ColPrimaryKey
804 805 806 807 808 809 810 811
				{
					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;
812
					n->constraints = $3;
813

814 815 816 817
					$$ = (Node *)n;
				}
		;

818
ColQualifier:  ColQualList						{ $$ = $1; }
819
			| /*EMPTY*/							{ $$ = NULL; }
820
		;
821

822 823 824 825 826 827 828 829 830 831 832 833 834 835
ColQualList:  ColQualList ColConstraint
				{
					if ($2 != NULL)
						$$ = lappend($1, $2);
					else
						$$ = $1;
				}
			| ColConstraint
				{
					if ($1 != NULL)
						$$ = lcons($1, NIL);
					else
						$$ = NULL;
				}
836 837
		;

838 839 840 841 842 843 844 845 846 847 848 849
ColPrimaryKey:  PRIMARY KEY
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = lcons((Node *)n, NIL);
				}
			| /*EMPTY*/							{ $$ = NULL; }
		;

850 851 852 853
ColConstraint:
		CONSTRAINT name ColConstraintElem
				{
						Constraint *n = (Constraint *)$3;
854
						if (n != NULL) n->name = fmtId($2);
855 856 857 858
						$$ = $3;
				}
		| ColConstraintElem
				{ $$ = $1; }
859
		;
860

861
/* DEFAULT NULL is already the default for Postgres.
862 863 864
 * Bue define it here and carry it forward into the system
 * to make it explicit.
 * - thomas 1998-09-13
865 866 867 868 869
 * WITH NULL and NULL are not SQL92-standard syntax elements,
 * so leave them out. Use DEFAULT NULL to explicitly indicate
 * that a column may have that value. WITH NULL leads to
 * shift/reduce conflicts with WITH TIME ZONE anyway.
 * - thomas 1999-01-08
870
 */
871 872 873 874 875 876 877 878 879
ColConstraintElem:  CHECK '(' constraint_expr ')'
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					n->keys = NULL;
					$$ = (Node *)n;
				}
880 881 882 883 884 885 886 887 888
			| DEFAULT NULL_P
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_DEFAULT;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
			| 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 已提交
927
					elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
928 929
					$$ = NULL;
				}
930
		;
V
Vadim B. Mikheev 已提交
931

932 933 934 935 936 937 938 939 940 941 942
default_list:  default_list ',' default_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| default_expr
				{
					$$ = $1;
				}
		;

943 944 945
/* The Postgres default column value is NULL.
 * Rather than carrying DEFAULT NULL forward as a clause,
 * let's just have it be a no-op.
946 947
			| NULL_P
				{	$$ = lcons( makeString("NULL"), NIL); }
948 949 950 951
 * - thomas 1998-09-13
 */
default_expr:  AexprConst
				{	$$ = makeConstantList((A_Const *) $1); }
952 953 954 955 956 957 958 959
			| '-' 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)); }
960 961
			| default_expr '%' default_expr
				{	$$ = nconc( $1, lcons( makeString( "%"), $3)); }
962 963 964
			| default_expr '*' default_expr
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
			| default_expr '=' default_expr
B
Bruce Momjian 已提交
965
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
966
			| default_expr '<' default_expr
B
Bruce Momjian 已提交
967
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
968
			| default_expr '>' default_expr
B
Bruce Momjian 已提交
969
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
970 971 972 973 974 975 976 977 978 979 980
			| ':' 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));
				}
981
			| CAST '(' default_expr AS Typename ')'
982
				{
983 984
					$5->name = fmtId($5->name);
					$$ = nconc( lcons( makeString( "CAST"), $3), makeList( makeString("AS"), $5, -1));
985 986 987
				}
			| '(' default_expr ')'
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
988
			| func_name '(' ')'
989 990 991 992
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
993
			| func_name '(' default_list ')'
994 995
				{
					$$ = makeList( makeString($1), makeString("("), -1);
996
					$$ = nconc( $$, $3);
997 998 999 1000 1001
					$$ = lappend( $$, makeString(")"));
				}
			| default_expr Op default_expr
				{
					if (!strcmp("<=", $2) || !strcmp(">=", $2))
B
Bruce Momjian 已提交
1002
						elog(ERROR,"boolean expressions not supported in DEFAULT");
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
					$$ = 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); }
1030 1031
			| USER
				{	$$ = lcons( makeString( "USER"), NIL); }
1032 1033
		;

1034 1035 1036 1037 1038
/* 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
1039
				{
1040
						Constraint *n = (Constraint *)$3;
1041
						if (n != NULL) n->name = fmtId($2);
1042 1043
						$$ = $3;
				}
1044 1045
		| ConstraintElem
				{ $$ = $1; }
1046 1047
		;

1048
ConstraintElem:  CHECK '(' constraint_expr ')'
1049
				{
1050 1051 1052 1053 1054
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					$$ = (Node *)n;
1055
				}
1056
		| UNIQUE '(' columnList ')'
1057 1058 1059 1060 1061 1062 1063 1064
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_UNIQUE;
					n->name = NULL;
					n->def = NULL;
					n->keys = $3;
					$$ = (Node *)n;
				}
1065
		| PRIMARY KEY '(' columnList ')'
1066 1067 1068 1069 1070 1071 1072 1073
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = $4;
					$$ = (Node *)n;
				}
1074
		| FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
1075 1076 1077 1078
				{
					elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
					$$ = NULL;
				}
1079
		;
1080

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
constraint_list:  constraint_list ',' constraint_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| constraint_expr
				{
					$$ = $1;
				}
		;

1092
constraint_expr:  AexprConst
1093
				{	$$ = makeConstantList((A_Const *) $1); }
1094
			| NULL_P
1095
				{	$$ = lcons( makeString("NULL"), NIL); }
1096
			| ColId
1097
				{
1098
					$$ = lcons( makeString(fmtId($1)), NIL);
1099
				}
1100
			| '-' constraint_expr %prec UMINUS
1101
				{	$$ = lcons( makeString( "-"), $2); }
1102
			| constraint_expr '+' constraint_expr
1103
				{	$$ = nconc( $1, lcons( makeString( "+"), $3)); }
1104
			| constraint_expr '-' constraint_expr
1105
				{	$$ = nconc( $1, lcons( makeString( "-"), $3)); }
1106
			| constraint_expr '/' constraint_expr
1107
				{	$$ = nconc( $1, lcons( makeString( "/"), $3)); }
1108 1109
			| constraint_expr '%' constraint_expr
				{	$$ = nconc( $1, lcons( makeString( "%"), $3)); }
1110
			| constraint_expr '*' constraint_expr
1111
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
1112
			| constraint_expr '=' constraint_expr
1113
				{	$$ = nconc( $1, lcons( makeString( "="), $3)); }
1114
			| constraint_expr '<' constraint_expr
1115
				{	$$ = nconc( $1, lcons( makeString( "<"), $3)); }
1116
			| constraint_expr '>' constraint_expr
1117
				{	$$ = nconc( $1, lcons( makeString( ">"), $3)); }
1118
			| ':' constraint_expr
1119
				{	$$ = lcons( makeString( ":"), $2); }
1120
			| ';' constraint_expr
1121
				{	$$ = lcons( makeString( ";"), $2); }
1122
			| '|' constraint_expr
1123
				{	$$ = lcons( makeString( "|"), $2); }
1124
			| constraint_expr TYPECAST Typename
1125
				{
1126 1127
					$3->name = fmtId($3->name);
					$$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
1128
				}
1129
			| CAST '(' constraint_expr AS Typename ')'
1130
				{
1131 1132
					$5->name = fmtId($5->name);
					$$ = nconc( lcons( makeString( "CAST"), $3), makeList( makeString("AS"), $5, -1));
1133
				}
1134
			| '(' constraint_expr ')'
1135
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
1136
			| func_name '(' ')'
1137 1138 1139 1140
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
1141
			| func_name '(' constraint_list ')'
1142 1143 1144 1145 1146
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = nconc( $$, $3);
					$$ = lappend( $$, makeString(")"));
				}
1147
			| constraint_expr Op constraint_expr
1148
				{	$$ = nconc( $1, lcons( makeString( $2), $3)); }
1149
			| constraint_expr LIKE constraint_expr
1150 1151 1152
				{	$$ = nconc( $1, lcons( makeString( "LIKE"), $3)); }
			| constraint_expr NOT LIKE constraint_expr
				{	$$ = nconc( $1, lcons( makeString( "NOT LIKE"), $4)); }
1153
			| constraint_expr AND constraint_expr
1154
				{	$$ = nconc( $1, lcons( makeString( "AND"), $3)); }
1155
			| constraint_expr OR constraint_expr
1156
				{	$$ = nconc( $1, lcons( makeString( "OR"), $3)); }
1157 1158
			| NOT constraint_expr
				{	$$ = lcons( makeString( "NOT"), $2); }
1159
			| Op constraint_expr
1160
				{	$$ = lcons( makeString( $1), $2); }
1161
			| constraint_expr Op
1162
				{	$$ = lappend( $1, makeString( $2)); }
1163 1164 1165 1166 1167 1168 1169 1170
			| 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")); }
1171
			| constraint_expr IS TRUE_P
1172
				{	$$ = lappend( $1, makeString( "IS TRUE")); }
1173
			| constraint_expr IS FALSE_P
1174
				{	$$ = lappend( $1, makeString( "IS FALSE")); }
1175
			| constraint_expr IS NOT TRUE_P
1176
				{	$$ = lappend( $1, makeString( "IS NOT TRUE")); }
1177
			| constraint_expr IS NOT FALSE_P
1178
				{	$$ = lappend( $1, makeString( "IS NOT FALSE")); }
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
			| 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); }
1222 1223
		;

1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
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; }
		;

1244 1245 1246 1247
OptInherit:  INHERITS '(' relation_name_list ')'		{ $$ = $3; }
		| /*EMPTY*/										{ $$ = NIL; }
		;

1248
CreateAsStmt:  CREATE OptTemp TABLE relation_name OptCreateAs AS SubSelect
1249
				{
1250 1251 1252 1253 1254
					SelectStmt *n = (SelectStmt *)$7;
					if ($5 != NIL)
						mapTargetColumns($5, n->targetList);
					n->istemp = $2;
					n->into = $4;
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
					$$ = (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;
				}
		;

1279

V
Vadim B. Mikheev 已提交
1280 1281
/*****************************************************************************
 *
1282 1283
 *		QUERY :
 *				CREATE SEQUENCE seqname
V
Vadim B. Mikheev 已提交
1284 1285 1286
 *
 *****************************************************************************/

1287
CreateSeqStmt:  CREATE SEQUENCE relation_name OptSeqList
1288 1289 1290 1291 1292 1293 1294
				{
					CreateSeqStmt *n = makeNode(CreateSeqStmt);
					n->seqname = $3;
					n->options = $4;
					$$ = (Node *)n;
				}
		;
V
Vadim B. Mikheev 已提交
1295

1296
OptSeqList:  OptSeqList OptSeqElem
1297
				{ $$ = lappend($1, $2); }
1298
			|	{ $$ = NIL; }
1299 1300
		;

1301
OptSeqElem:  CACHE IntegerOnly
1302 1303
				{
					$$ = makeNode(DefElem);
1304
					$$->defname = "cache";
1305 1306
					$$->arg = (Node *)$2;
				}
1307
			| CYCLE
1308 1309
				{
					$$ = makeNode(DefElem);
1310
					$$->defname = "cycle";
1311 1312
					$$->arg = (Node *)NULL;
				}
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
			| 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;
				}
		;

1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
NumericOnly:  FloatOnly					{ $$ = $1; }
			| IntegerOnly				{ $$ = $1; }

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

1353 1354 1355 1356 1357 1358 1359 1360 1361
IntegerOnly:  Iconst
				{
					$$ = makeInteger($1);
				}
			| '-' Iconst
				{
					$$ = makeInteger($2);
					$$->val.ival = - $$->val.ival;
				}
1362
		;
V
Vadim B. Mikheev 已提交
1363

1364 1365 1366 1367 1368 1369 1370 1371
/*****************************************************************************
 *
 *		QUERIES :
 *				CREATE PROCEDURAL LANGUAGE ...
 *				DROP PROCEDURAL LANGUAGE ...
 *
 *****************************************************************************/

1372
CreatePLangStmt:  CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst 
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
			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; }

1387
DropPLangStmt:  DROP PROCEDURAL LANGUAGE Sconst
1388 1389 1390 1391 1392 1393
			{
				DropPLangStmt *n = makeNode(DropPLangStmt);
				n->plname = $4;
				$$ = (Node *)n;
			}
		;
V
Vadim B. Mikheev 已提交
1394

V
Vadim B. Mikheev 已提交
1395 1396
/*****************************************************************************
 *
1397 1398 1399
 *		QUERIES :
 *				CREATE TRIGGER ...
 *				DROP TRIGGER ...
V
Vadim B. Mikheev 已提交
1400 1401 1402
 *
 *****************************************************************************/

1403
CreateTrigStmt:  CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
				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;
				}
		;

1419 1420
TriggerActionTime:  BEFORE						{ $$ = TRUE; }
			| AFTER								{ $$ = FALSE; }
1421 1422 1423
		;

TriggerEvents:	TriggerOneEvent
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
				{
					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;
				}
1439 1440
		;

1441 1442 1443
TriggerOneEvent:  INSERT					{ $$ = 'i'; }
			| DELETE						{ $$ = 'd'; }
			| UPDATE						{ $$ = 'u'; }
1444 1445
		;

1446
TriggerForSpec:  FOR TriggerForOpt TriggerForType
1447
				{
1448
					$$ = $3;
1449 1450
				}
		;
V
Vadim B. Mikheev 已提交
1451

1452 1453 1454 1455 1456 1457 1458 1459
TriggerForOpt:  EACH						{ $$ = TRUE; }
			| /*EMPTY*/						{ $$ = FALSE; }
		;

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

1460
TriggerFuncArgs:  TriggerFuncArg
1461
				{ $$ = lcons($1, NIL); }
1462
			| TriggerFuncArgs ',' TriggerFuncArg
1463
				{ $$ = lappend($1, $3); }
1464
			| /*EMPTY*/
1465
				{ $$ = NIL; }
1466 1467
		;

1468
TriggerFuncArg:  ICONST
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
				{
					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; }
1482
		;
V
Vadim B. Mikheev 已提交
1483

1484
DropTrigStmt:  DROP TRIGGER name ON relation_name
1485 1486 1487 1488 1489 1490 1491
				{
					DropTrigStmt *n = makeNode(DropTrigStmt);
					n->trigname = $3;
					n->relname = $5;
					$$ = (Node *) n;
				}
		;
V
Vadim B. Mikheev 已提交
1492

1493

1494 1495
/*****************************************************************************
 *
1496 1497
 *		QUERY :
 *				define (type,operator,aggregate)
1498 1499 1500 1501
 *
 *****************************************************************************/

DefineStmt:  CREATE def_type def_rest
1502 1503 1504 1505 1506
				{
					$3->defType = $2;
					$$ = (Node *)$3;
				}
		;
1507

1508
def_rest:  def_name definition
1509 1510 1511 1512 1513 1514 1515
				{
					$$ = makeNode(DefineStmt);
					$$->defname = $1;
					$$->definition = $2;
				}
		;

1516
def_type:  OPERATOR							{ $$ = OPERATOR; }
1517
		| TYPE_P							{ $$ = TYPE_P; }
1518
		| AGGREGATE							{ $$ = AGGREGATE; }
1519
		;
1520

1521
def_name:  PROCEDURE						{ $$ = "procedure"; }
1522 1523 1524 1525
		| JOIN								{ $$ = "join"; }
		| ColId								{ $$ = $1; }
		| MathOp							{ $$ = $1; }
		| Op								{ $$ = $1; }
1526
		;
1527

1528
definition:  '(' def_list ')'				{ $$ = $2; }
1529
		;
1530

1531
def_list:  def_elem							{ $$ = lcons($1, NIL); }
1532
		| def_list ',' def_elem				{ $$ = lappend($1, $3); }
1533
		;
1534 1535

def_elem:  def_name '=' def_arg
1536 1537 1538 1539 1540
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)$3;
				}
1541
		| def_name
1542 1543 1544 1545 1546
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)NULL;
				}
1547
		| DEFAULT '=' def_arg
1548 1549
				{
					$$ = makeNode(DefElem);
1550
					$$->defname = "default";
1551 1552 1553 1554
					$$->arg = (Node *)$3;
				}
		;

1555 1556
def_arg:  ColId							{  $$ = (Node *)makeString($1); }
		| all_Op						{  $$ = (Node *)makeString($1); }
1557
		| NumericOnly					{  $$ = (Node *)$1; }
1558 1559
		| Sconst						{  $$ = (Node *)makeString($1); }
		| SETOF ColId
1560 1561 1562 1563 1564
				{
					TypeName *n = makeNode(TypeName);
					n->name = $2;
					n->setof = TRUE;
					n->arrayBounds = NULL;
1565
					n->typmod = -1;
1566 1567
					$$ = (Node *)n;
				}
1568
		;
1569 1570 1571 1572


/*****************************************************************************
 *
1573 1574
 *		QUERY:
 *				destroy <relname1> [, <relname2> .. <relnameN> ]
1575 1576 1577
 *
 *****************************************************************************/

1578
DestroyStmt:  DROP TABLE relation_name_list
1579 1580 1581
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1582
					n->sequence = FALSE;
1583 1584
					$$ = (Node *)n;
				}
1585
		| DROP SEQUENCE relation_name_list
1586 1587 1588
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1589
					n->sequence = TRUE;
1590 1591 1592
					$$ = (Node *)n;
				}
		;
1593 1594 1595 1596


/*****************************************************************************
 *
1597
 *		QUERY:
1598 1599 1600
 *			fetch/move [forward | backward] [ # | all ] [ in <portalname> ]
 *			fetch [ forward | backward | absolute | relative ]
 *			      [ # | all | next | prior ] [ [ in | from ] <portalname> ]
1601 1602 1603
 *
 *****************************************************************************/

1604 1605 1606
FetchStmt:	FETCH opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
					if ($2 == RELATIVE)
					{
						if ($3 == 0)
							elog(ERROR,"FETCH/RELATIVE at current position is not supported");
						$2 = FORWARD;
					}
					if ($3 < 0)
					{
						$3 = -$3;
						$2 = (($2 == FORWARD)? BACKWARD: FORWARD);
					}
1618 1619 1620
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
V
Vadim B. Mikheev 已提交
1621 1622 1623 1624 1625 1626
					n->ismove = false;
					$$ = (Node *)n;
				}
		|	MOVE opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
1627 1628 1629 1630 1631
					if ($3 < 0)
					{
						$3 = -$3;
						$2 = (($2 == FORWARD)? BACKWARD: FORWARD);
					}
V
Vadim B. Mikheev 已提交
1632 1633 1634
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
1635
					n->ismove = TRUE;
1636 1637 1638 1639
					$$ = (Node *)n;
				}
		;

1640 1641 1642 1643 1644 1645 1646 1647 1648
opt_direction:	FORWARD					{ $$ = FORWARD; }
		| BACKWARD						{ $$ = BACKWARD; }
		| RELATIVE						{ $$ = RELATIVE; }
		| ABSOLUTE
			{
				elog(NOTICE,"FETCH/ABSOLUTE not supported, using RELATIVE");
				$$ = RELATIVE;
			}
		| /*EMPTY*/						{ $$ = FORWARD; /* default */ }
1649
		;
1650

1651 1652
fetch_how_many:  Iconst					{ $$ = $1; }
		| '-' Iconst					{ $$ = - $2; }
1653
		| ALL							{ $$ = 0; /* 0 means fetch all tuples*/ }
1654 1655
		| NEXT							{ $$ = 1; }
		| PRIOR							{ $$ = -1; }
1656
		| /*EMPTY*/						{ $$ = 1; /*default*/ }
1657
		;
1658

1659
opt_portal_name:  IN name				{ $$ = $2; }
1660
		| FROM name						{ $$ = $2; }
V
Vadim B. Mikheev 已提交
1661 1662 1663
		| /*EMPTY*/						{ $$ = NULL; }
		;

1664

1665 1666
/*****************************************************************************
 *
1667 1668
 *		QUERY:
 *				GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1669 1670 1671
 *
 *****************************************************************************/

1672
GrantStmt:  GRANT privileges ON relation_name_list TO grantee opt_with_grant
1673 1674 1675 1676
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'+');
				}
		;
1677 1678

privileges:  ALL PRIVILEGES
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| ALL
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| operation_commalist
				{
				 $$ = $1;
				}
		;

1692
operation_commalist:  operation
1693 1694 1695 1696 1697 1698 1699 1700 1701
				{
						$$ = aclmakepriv("",$1);
				}
		| operation_commalist ',' operation
				{
						$$ = aclmakepriv($1,$3);
				}
		;

1702
operation:  SELECT
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
				{
						$$ = ACL_MODE_RD_CHR;
				}
		| INSERT
				{
						$$ = ACL_MODE_AP_CHR;
				}
		| UPDATE
				{
						$$ = ACL_MODE_WR_CHR;
				}
		| DELETE
				{
						$$ = ACL_MODE_WR_CHR;
				}
1718
		| RULE
1719 1720 1721 1722 1723 1724 1725 1726 1727
				{
						$$ = ACL_MODE_RU_CHR;
				}
		;

grantee:  PUBLIC
				{
						$$ = aclmakeuser("A","");
				}
1728
		| GROUP ColId
1729 1730 1731
				{
						$$ = aclmakeuser("G",$2);
				}
1732
		| ColId
1733 1734 1735 1736
				{
						$$ = aclmakeuser("U",$1);
				}
		;
1737

1738
opt_with_grant:  WITH GRANT OPTION
1739 1740
				{
					yyerror("WITH GRANT OPTION is not supported.  Only relation owners can set privileges");
1741 1742
				 }
		| /*EMPTY*/
1743 1744
		;

1745

1746 1747
/*****************************************************************************
 *
1748 1749
 *		QUERY:
 *				REVOKE [privileges] ON [relation_name] FROM [user]
1750 1751 1752
 *
 *****************************************************************************/

1753
RevokeStmt:  REVOKE privileges ON relation_name_list FROM grantee
1754 1755 1756 1757
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'-');
				}
		;
1758

1759

1760 1761
/*****************************************************************************
 *
1762
 *		QUERY:
B
Bruce Momjian 已提交
1763
 *				create index <indexname> on <relname>
1764 1765
 *				  using <access> "(" (<col> with <op>)+ ")" [with
 *				  <target_list>]
1766
 *
1767
 *	[where <qual>] is not supported anymore
1768 1769
 *****************************************************************************/

1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
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;
				}
		;

1787 1788
index_opt_unique:  UNIQUE						{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
1789 1790
		;

1791 1792
access_method_clause:  USING access_method		{ $$ = $2; }
		| /*EMPTY*/								{ $$ = "btree"; }
1793
		;
M
Marc G. Fournier 已提交
1794

1795 1796 1797 1798 1799 1800 1801 1802
index_params:  index_list						{ $$ = $1; }
		| func_index							{ $$ = lcons($1,NIL); }
		;

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

1803
func_index:  func_name '(' name_list ')' opt_type opt_class
1804 1805 1806 1807 1808
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = $3;
					$$->class = $6;
1809
					$$->typename = $5;
1810 1811 1812 1813 1814 1815 1816 1817 1818
				}
		  ;

index_elem:  attr_name opt_type opt_class
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = NIL;
					$$->class = $3;
1819
					$$->typename = $2;
1820 1821 1822
				}
		;

1823 1824 1825
opt_type:  ':' Typename							{ $$ = $2; }
		| FOR Typename							{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL; }
1826 1827 1828 1829 1830 1831
		;

/* 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
1832
 *		| WITH class							{ $$ = $2; }
1833 1834
 */
opt_class:  class								{ $$ = $1; }
1835
		| USING class							{ $$ = $2; }
1836 1837 1838 1839
		| /*EMPTY*/								{ $$ = NULL; }
		;


1840 1841
/*****************************************************************************
 *
1842 1843
 *		QUERY:
 *				extend index <indexname> [where <qual>]
1844 1845 1846 1847
 *
 *****************************************************************************/

ExtendStmt:  EXTEND INDEX index_name where_clause
1848 1849 1850 1851 1852 1853 1854
				{
					ExtendStmt *n = makeNode(ExtendStmt);
					n->idxname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
1855 1856 1857

/*****************************************************************************
 *
1858 1859
 *		QUERY:
 *				execute recipe <recipeName>
1860 1861 1862
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
1863
/* NOT USED
1864
RecipeStmt:  EXECUTE RECIPE recipe_name
1865 1866 1867
				{
					RecipeStmt *n;
					if (!IsTransactionBlock())
B
Bruce Momjian 已提交
1868
						elog(ERROR,"EXECUTE RECIPE may only be used in begin/end transaction blocks");
1869 1870 1871 1872 1873 1874

					n = makeNode(RecipeStmt);
					n->recipeName = $3;
					$$ = (Node *)n;
				}
		;
B
Bruce Momjian 已提交
1875
*/
1876 1877 1878

/*****************************************************************************
 *
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
 *		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>
1890 1891 1892
 *
 *****************************************************************************/

1893 1894
ProcedureStmt:	CREATE FUNCTION func_name func_args
			 RETURNS func_return opt_with AS Sconst LANGUAGE Sconst
1895 1896 1897 1898
				{
					ProcedureStmt *n = makeNode(ProcedureStmt);
					n->funcname = $3;
					n->defArgs = $4;
1899
					n->returnType = $6;
1900 1901 1902 1903 1904 1905 1906
					n->withClause = $7;
					n->as = $9;
					n->language = $11;
					$$ = (Node *)n;
				};

opt_with:  WITH definition						{ $$ = $2; }
1907
		| /*EMPTY*/								{ $$ = NIL; }
1908 1909
		;

1910
func_args:  '(' func_args_list ')'				{ $$ = $2; }
1911
		| '(' ')'								{ $$ = NIL; }
1912
		;
1913

1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
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; }
		;
1933 1934 1935

/*****************************************************************************
 *
1936
 *		QUERY:
1937
 *
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
 *		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")
1948 1949 1950 1951
 *
 *****************************************************************************/

RemoveStmt:  DROP remove_type name
1952 1953 1954 1955 1956 1957 1958 1959
				{
					RemoveStmt *n = makeNode(RemoveStmt);
					n->removeType = $2;
					n->name = $3;
					$$ = (Node *)n;
				}
		;

1960
remove_type:  TYPE_P							{  $$ = TYPE_P; }
1961 1962 1963
		| INDEX									{  $$ = INDEX; }
		| RULE									{  $$ = RULE; }
		| VIEW									{  $$ = VIEW; }
1964
		;
1965

1966

1967
RemoveAggrStmt:  DROP AGGREGATE name aggr_argtype
1968 1969 1970 1971 1972 1973 1974 1975 1976
				{
						RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
						n->aggname = $3;
						n->aggtype = $4;
						$$ = (Node *)n;
				}
		;

aggr_argtype:  name								{ $$ = $1; }
1977
		| '*'									{ $$ = NULL; }
1978
		;
1979

1980 1981

RemoveFuncStmt:  DROP FUNCTION func_name func_args
1982 1983 1984
				{
					RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
					n->funcname = $3;
1985
					n->args = $4;
1986 1987 1988 1989
					$$ = (Node *)n;
				}
		;

1990 1991

RemoveOperStmt:  DROP OPERATOR all_Op '(' oper_argtypes ')'
1992 1993 1994 1995 1996 1997 1998
				{
					RemoveOperStmt *n = makeNode(RemoveOperStmt);
					n->opname = $3;
					n->args = $5;
					$$ = (Node *)n;
				}
		;
1999

2000
all_Op:  Op | MathOp;
2001

2002
MathOp:	'+'				{ $$ = "+"; }
2003 2004 2005
		| '-'			{ $$ = "-"; }
		| '*'			{ $$ = "*"; }
		| '/'			{ $$ = "/"; }
2006
		| '%'			{ $$ = "%"; }
2007 2008 2009
		| '<'			{ $$ = "<"; }
		| '>'			{ $$ = ">"; }
		| '='			{ $$ = "="; }
2010 2011 2012 2013
		;

oper_argtypes:	name
				{
B
Bruce Momjian 已提交
2014
				   elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
2015 2016 2017 2018 2019 2020 2021 2022
				}
		| 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); }
		;
2023

2024

2025 2026
/*****************************************************************************
 *
2027 2028 2029
 *		QUERY:
 *				rename <attrname1> in <relname> [*] to <attrname2>
 *				rename <relname1> to <relname2>
2030
 *
2031 2032
 *****************************************************************************/

2033
RenameStmt:  ALTER TABLE relation_name opt_inh_star
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
				  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; }
2046
		| /*EMPTY*/						{ $$ = NULL; }
2047 2048 2049 2050 2051
		;

opt_column:  COLUMN						{ $$ = COLUMN; }
		| /*EMPTY*/						{ $$ = 0; }
		;
2052 2053 2054


/*****************************************************************************
2055
 *
2056 2057
 *		QUERY:	Define Rewrite Rule , Define Tuple Rule
 *				Define Rule <old rules >
2058
 *
2059
 *		only rewrite rule is supported -- ay 9/94
2060
 *
2061 2062
 *****************************************************************************/

2063
RuleStmt:  CREATE RULE name AS
2064 2065
		   { QueryIsRule=TRUE; }
		   ON event TO event_object where_clause
2066
		   DO opt_instead RuleActionList
2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078
				{
					RuleStmt *n = makeNode(RuleStmt);
					n->rulename = $3;
					n->event = $7;
					n->object = $9;
					n->whereClause = $10;
					n->instead = $12;
					n->actions = $13;
					$$ = (Node *)n;
				}
		;

2079 2080 2081 2082 2083
RuleActionList:  NOTHING				{ $$ = NIL; }
		| SelectStmt					{ $$ = lcons($1, NIL); }
		| RuleActionStmt				{ $$ = lcons($1, NIL); }
		| '[' RuleActionBlock ']'		{ $$ = $2; }
		| '(' RuleActionBlock ')'		{ $$ = $2; } 
2084
		;
2085

2086 2087
RuleActionBlock:  RuleActionMulti		{  $$ = $1; }
		| RuleActionStmt				{ $$ = lcons($1, NIL); }
2088
		;
2089

2090
RuleActionMulti:  RuleActionMulti RuleActionStmt
2091
				{  $$ = lappend($1, $2); }
2092 2093 2094
		| RuleActionMulti RuleActionStmt ';'
				{  $$ = lappend($1, $2); }
		| RuleActionStmt ';'
2095 2096
				{ $$ = lcons($1, NIL); }
		;
2097

2098 2099 2100 2101 2102 2103
RuleActionStmt:	InsertStmt
		| UpdateStmt
		| DeleteStmt
		| NotifyStmt
		;

2104
event_object:  relation_name '.' attr_name
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
				{
					$$ = 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;
				}
		;
2121 2122

/* change me to select, update, etc. some day */
2123 2124 2125 2126 2127
event:	SELECT							{ $$ = CMD_SELECT; }
		| UPDATE						{ $$ = CMD_UPDATE; }
		| DELETE						{ $$ = CMD_DELETE; }
		| INSERT						{ $$ = CMD_INSERT; }
		 ;
2128

2129
opt_instead:  INSTEAD					{ $$ = TRUE; }
2130
		| /*EMPTY*/						{ $$ = FALSE; }
2131
		;
2132 2133 2134 2135


/*****************************************************************************
 *
2136 2137 2138
 *		QUERY:
 *				NOTIFY <relation_name>	can appear both in rule bodies and
 *				as a query-level command
2139 2140 2141
 *
 *****************************************************************************/

2142
NotifyStmt:  NOTIFY relation_name
2143 2144 2145 2146 2147 2148
				{
					NotifyStmt *n = makeNode(NotifyStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
		;
2149

2150
ListenStmt:  LISTEN relation_name
2151 2152 2153 2154 2155
				{
					ListenStmt *n = makeNode(ListenStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
2156 2157
;

M
 
Marc G. Fournier 已提交
2158 2159 2160 2161 2162 2163
UnlistenStmt:  UNLISTEN relation_name
				{
					UnlistenStmt *n = makeNode(UnlistenStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
2164 2165 2166 2167 2168 2169
		| UNLISTEN '*'
				{
					UnlistenStmt *n = makeNode(UnlistenStmt);
					n->relname = "*";
					$$ = (Node *)n;
				}
M
 
Marc G. Fournier 已提交
2170 2171
;

2172 2173 2174

/*****************************************************************************
 *
2175
 *		Transactions:
2176
 *
2177 2178 2179 2180 2181 2182
 *		abort transaction
 *				(ABORT)
 *		begin transaction
 *				(BEGIN)
 *		end transaction
 *				(END)
2183
 *
2184 2185
 *****************************************************************************/

2186
TransactionStmt: ABORT_TRANS opt_trans
2187 2188 2189 2190 2191
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
2192
		| BEGIN_TRANS opt_trans
2193 2194 2195 2196 2197
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = BEGIN_TRANS;
					$$ = (Node *)n;
				}
2198
		| COMMIT opt_trans
2199 2200 2201 2202 2203
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
2204
		| END_TRANS opt_trans
2205 2206 2207 2208 2209
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
2210
		| ROLLBACK opt_trans
2211 2212 2213 2214 2215
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
2216
		;
2217

2218 2219 2220
opt_trans: WORK									{ $$ = TRUE; }
		| TRANSACTION							{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = TRUE; }
2221
		;
2222 2223 2224 2225


/*****************************************************************************
 *
2226 2227
 *		QUERY:
 *				define view <viewname> '('target-list ')' [where <quals> ]
2228 2229 2230
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2231
ViewStmt:  CREATE VIEW name AS SelectStmt
2232 2233 2234 2235
				{
					ViewStmt *n = makeNode(ViewStmt);
					n->viewname = $3;
					n->query = (Query *)$5;
B
Bruce Momjian 已提交
2236 2237
					if (((SelectStmt *)n->query)->sortClause != NULL)
						elog(ERROR,"Order by and Distinct on views is not implemented.");
B
Bruce Momjian 已提交
2238
					if (((SelectStmt *)n->query)->unionClause != NULL)
B
Bruce Momjian 已提交
2239
						elog(ERROR,"Views on unions not implemented.");
V
Vadim B. Mikheev 已提交
2240 2241
					if (((SelectStmt *)n->query)->forUpdate != NULL)
						elog(ERROR, "SELECT FOR UPDATE is not allowed in CREATE VIEW");
2242 2243 2244
					$$ = (Node *)n;
				}
		;
2245 2246 2247 2248


/*****************************************************************************
 *
2249 2250
 *		QUERY:
 *				load "filename"
2251 2252 2253
 *
 *****************************************************************************/

2254
LoadStmt:  LOAD file_name
2255 2256 2257 2258 2259 2260
				{
					LoadStmt *n = makeNode(LoadStmt);
					n->filename = $2;
					$$ = (Node *)n;
				}
		;
2261 2262 2263 2264


/*****************************************************************************
 *
2265 2266
 *		QUERY:
 *				createdb dbname
2267 2268 2269
 *
 *****************************************************************************/

M
 
Marc G. Fournier 已提交
2270
CreatedbStmt:  CREATE DATABASE database_name WITH opt_database1 opt_database2
2271 2272
				{
					CreatedbStmt *n = makeNode(CreatedbStmt);
M
 
Marc G. Fournier 已提交
2273 2274 2275
					if ($5 == NULL && $6 == NULL) {
						elog(ERROR, "CREATE DATABASE WITH requires at least an option");
					}
2276
					n->dbname = $3;
M
 
Marc G. Fournier 已提交
2277
					n->dbpath = $5;
M
 
Marc G. Fournier 已提交
2278
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287
					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 已提交
2288 2289
					if ($6 != NULL)
						elog(ERROR, "WITH ENCODING is not supported");
2290
					n->encoding = 0;
M
 
Marc G. Fournier 已提交
2291
#endif
2292 2293
					$$ = (Node *)n;
				}
M
 
Marc G. Fournier 已提交
2294 2295 2296 2297 2298
		| CREATE DATABASE database_name
				{
					CreatedbStmt *n = makeNode(CreatedbStmt);
					n->dbname = $3;
					n->dbpath = NULL;
M
 
Marc G. Fournier 已提交
2299
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
2300
					n->encoding = GetTemplateEncoding();
2301 2302
#else
					n->encoding = 0;
M
 
Marc G. Fournier 已提交
2303 2304 2305 2306 2307 2308 2309
#endif
					$$ = (Node *)n;
				}
		;

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

M
 
Marc G. Fournier 已提交
2312
opt_database2:  ENCODING '=' encoding			{ $$ = $3; }
2313 2314 2315 2316 2317 2318 2319
		| /*EMPTY*/								{ $$ = NULL; }
		;

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

M
 
Marc G. Fournier 已提交
2321 2322 2323 2324 2325
encoding:  Sconst								{ $$ = $1; }
		| DEFAULT								{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

2326 2327
/*****************************************************************************
 *
2328 2329
 *		QUERY:
 *				destroydb dbname
2330 2331 2332
 *
 *****************************************************************************/

2333 2334 2335 2336 2337 2338 2339
DestroydbStmt:	DROP DATABASE database_name
				{
					DestroydbStmt *n = makeNode(DestroydbStmt);
					n->dbname = $3;
					$$ = (Node *)n;
				}
		;
2340 2341 2342 2343


/*****************************************************************************
 *
2344 2345
 *		QUERY:
 *				cluster <index_name> on <relation_name>
2346 2347 2348
 *
 *****************************************************************************/

2349
ClusterStmt:  CLUSTER index_name ON relation_name
2350 2351 2352 2353 2354 2355 2356
				{
				   ClusterStmt *n = makeNode(ClusterStmt);
				   n->relname = $4;
				   n->indexname = $2;
				   $$ = (Node*)n;
				}
		;
2357

2358

2359 2360
/*****************************************************************************
 *
2361 2362
 *		QUERY:
 *				vacuum
2363 2364 2365
 *
 *****************************************************************************/

V
Vadim B. Mikheev 已提交
2366
VacuumStmt:  VACUUM opt_verbose opt_analyze
2367 2368 2369 2370 2371 2372 2373 2374
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
					n->analyze = $3;
					n->vacrel = NULL;
					n->va_spec = NIL;
					$$ = (Node *)n;
				}
B
Bruce Momjian 已提交
2375
		| VACUUM opt_verbose opt_analyze relation_name opt_va_list
2376 2377 2378
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
B
Bruce Momjian 已提交
2379 2380
					n->analyze = $3;
					n->vacrel = $4;
2381 2382
					n->va_spec = $5;
					if ( $5 != NIL && !$4 )
B
Bruce Momjian 已提交
2383
						elog(ERROR,"parser: syntax error at or near \"(\"");
2384 2385 2386 2387
					$$ = (Node *)n;
				}
		;

2388
opt_verbose:  VERBOSE							{ $$ = TRUE; }
2389
		| /*EMPTY*/								{ $$ = FALSE; }
2390 2391
		;

2392
opt_analyze:  ANALYZE							{ $$ = TRUE; }
2393
		| /*EMPTY*/								{ $$ = FALSE; }
2394
		;
V
Vadim B. Mikheev 已提交
2395

2396 2397
opt_va_list:  '(' va_list ')'					{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NIL; }
2398
		;
V
Vadim B. Mikheev 已提交
2399

2400
va_list:  name
2401 2402 2403 2404
				{ $$=lcons($1,NIL); }
		| va_list ',' name
				{ $$=lappend($1,$3); }
		;
2405

2406

2407 2408
/*****************************************************************************
 *
2409 2410
 *		QUERY:
 *				EXPLAIN query
2411 2412 2413
 *
 *****************************************************************************/

2414
ExplainStmt:  EXPLAIN opt_verbose OptimizableStmt
2415 2416 2417 2418 2419 2420 2421
				{
					ExplainStmt *n = makeNode(ExplainStmt);
					n->verbose = $2;
					n->query = (Query*)$3;
					$$ = (Node *)n;
				}
		;
2422

2423

2424
/*****************************************************************************
2425 2426 2427 2428 2429 2430 2431 2432
 *																			 *
 *		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							 *
 *																			 *
2433 2434
 *****************************************************************************/

B
Bruce Momjian 已提交
2435
OptimizableStmt:  SelectStmt
2436
		| CursorStmt
B
Bruce Momjian 已提交
2437 2438
		| UpdateStmt
		| InsertStmt
2439 2440 2441
		| NotifyStmt
		| DeleteStmt					/* by default all are $$=$1 */
		;
2442 2443 2444 2445


/*****************************************************************************
 *
2446 2447
 *		QUERY:
 *				INSERT STATEMENTS
2448
 *
2449 2450
 *****************************************************************************/

B
Hi!  
Bruce Momjian 已提交
2451 2452 2453 2454 2455 2456 2457
/***S*I***/
/* This rule used 'opt_column_list' between 'relation_name' and 'insert_rest'
 * originally. When the second rule of 'insert_rest' was changed to use
 * the new 'SelectStmt' rule (for INTERSECT and EXCEPT) it produced a shift/reduce
 * conflict. So I just changed the rules 'InsertStmt' and 'insert_rest' to accept
 * the same statements without any shift/reduce conflicts */
InsertStmt:  INSERT INTO relation_name  insert_rest
2458
				{
B
Hi!  
Bruce Momjian 已提交
2459 2460
 					$4->relname = $3;
					$$ = (Node *)$4;
2461 2462
				}
		;
2463

2464
insert_rest:  VALUES '(' res_target_list2 ')'
2465
				{
B
Bruce Momjian 已提交
2466
					$$ = makeNode(InsertStmt);
B
Hi!  
Bruce Momjian 已提交
2467
					$$->cols = NULL;
2468
					$$->unique = NULL;
2469 2470
					$$->targetList = $3;
					$$->fromClause = NIL;
2471
					$$->whereClause = NULL;
2472
					$$->groupClause = NIL;
2473
					$$->havingClause = NULL;
2474
					$$->unionClause = NIL;
2475
				}
2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
		| DEFAULT VALUES
				{
					$$ = makeNode(InsertStmt);
					$$->unique = NULL;
					$$->targetList = NIL;
					$$->fromClause = NIL;
					$$->whereClause = NULL;
					$$->groupClause = NIL;
					$$->havingClause = NULL;
					$$->unionClause = NIL;
B
Hi!  
Bruce Momjian 已提交
2486 2487
					/***S*I***/
				 	$$->intersectClause = NIL;
2488
				}
B
Hi!  
Bruce Momjian 已提交
2489 2490 2491 2492
		/***S*I***/
		/* We want the full power of SelectStatements including INTERSECT and EXCEPT
                 * for insertion */
		| SelectStmt
2493
				{
B
Hi!  
Bruce Momjian 已提交
2494 2495 2496
					SelectStmt *n;

					n = (SelectStmt *)$1;
B
Bruce Momjian 已提交
2497
					$$ = makeNode(InsertStmt);
B
Hi!  
Bruce Momjian 已提交
2498 2499 2500 2501 2502 2503 2504 2505 2506
					$$->cols = NULL;
					$$->unique = n->unique;
					$$->targetList = n->targetList;
					$$->fromClause = n->fromClause;
					$$->whereClause = n->whereClause;
					$$->groupClause = n->groupClause;
					$$->havingClause = n->havingClause;
					$$->unionClause = n->unionClause;
					$$->intersectClause = n->intersectClause;
2507
					$$->forUpdate = n->forUpdate;
B
Hi!  
Bruce Momjian 已提交
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
				}
		| '(' columnList ')' VALUES '(' res_target_list2 ')'
				{
					$$ = makeNode(InsertStmt);
					$$->cols = $2;
					$$->unique = NULL;
					$$->targetList = $6;
					$$->fromClause = NIL;
					$$->whereClause = NULL;
					$$->groupClause = NIL;
					$$->havingClause = NULL;
					$$->unionClause = NIL; 
					/***S*I***/
				 	$$->intersectClause = NIL;
				}
		| '(' columnList ')' SelectStmt
				{
					SelectStmt *n;

					n = (SelectStmt *)$4;
					$$ = makeNode(InsertStmt);
					$$->cols = $2;
					$$->unique = n->unique;
					$$->targetList = n->targetList;
					$$->fromClause = n->fromClause;
					$$->whereClause = n->whereClause;
					$$->groupClause = n->groupClause;
					$$->havingClause = n->havingClause;
					$$->unionClause = n->unionClause;
					$$->intersectClause = n->intersectClause;
2538 2539 2540
				}
		;

2541
opt_column_list:  '(' columnList ')'			{ $$ = $2; }
2542 2543
		| /*EMPTY*/								{ $$ = NIL; }
		;
2544

2545
columnList:
2546 2547 2548 2549 2550
		  columnList ',' columnElem
				{ $$ = lappend($1, $3); }
		| columnElem
				{ $$ = lcons($1, NIL); }
		;
2551

2552
columnElem:  ColId opt_indirection
2553 2554 2555 2556 2557 2558 2559
				{
					Ident *id = makeNode(Ident);
					id->name = $1;
					id->indirection = $2;
					$$ = (Node *)id;
				}
		;
2560

2561

2562 2563
/*****************************************************************************
 *
2564 2565
 *		QUERY:
 *				DELETE STATEMENTS
2566 2567
 *
 *****************************************************************************/
2568

2569
DeleteStmt:  DELETE FROM relation_name
2570 2571 2572 2573 2574 2575 2576 2577
			 where_clause
				{
					DeleteStmt *n = makeNode(DeleteStmt);
					n->relname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
2578

2579
LockStmt:	LOCK_P opt_table relation_name
2580
				{
2581 2582 2583 2584 2585 2586 2587 2588 2589
					LockStmt *n = makeNode(LockStmt);

					n->relname = $3;
					n->mode = AccessExclusiveLock;
					$$ = (Node *)n;
				}
		|	LOCK_P opt_table relation_name IN opt_lmode ROW IDENT IDENT
				{
					LockStmt *n = makeNode(LockStmt);
2590

2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
					n->relname = $3;
					if (strcasecmp($8, "MODE"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", $8);
					if ($5 != NULL)
					{
						if (strcasecmp($5, "SHARE"))
							elog(ERROR,"parser: syntax error at or near \"%s\"", $5);
						if (strcasecmp($7, "EXCLUSIVE"))
							elog(ERROR,"parser: syntax error at or near \"%s\"", $7);
						n->mode = ShareRowExclusiveLock;
					}
					else
					{
						if (strcasecmp($7, "SHARE") == 0)
							n->mode = RowShareLock;
						else if (strcasecmp($7, "EXCLUSIVE") == 0)
							n->mode = RowExclusiveLock;
						else
							elog(ERROR,"parser: syntax error at or near \"%s\"", $7);
					}
					$$ = (Node *)n;
				}
		|	LOCK_P opt_table relation_name IN IDENT IDENT IDENT
				{
					LockStmt *n = makeNode(LockStmt);

					n->relname = $3;
					if (strcasecmp($7, "MODE"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", $7);
					if (strcasecmp($5, "ACCESS"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", $5);
					if (strcasecmp($6, "SHARE") == 0)
						n->mode = AccessShareLock;
					else if (strcasecmp($6, "EXCLUSIVE") == 0)
						n->mode = AccessExclusiveLock;
					else
						elog(ERROR,"parser: syntax error at or near \"%s\"", $6);
					$$ = (Node *)n;
				}
		|	LOCK_P opt_table relation_name IN IDENT IDENT
				{
					LockStmt *n = makeNode(LockStmt);
2633

2634
					n->relname = $3;
2635 2636 2637 2638 2639 2640 2641 2642
					if (strcasecmp($6, "MODE"))
						elog(ERROR,"parser: syntax error at or near \"%s\"", $6);
					if (strcasecmp($5, "SHARE") == 0)
						n->mode = ShareLock;
					else if (strcasecmp($5, "EXCLUSIVE") == 0)
						n->mode = ExclusiveLock;
					else
						elog(ERROR,"parser: syntax error at or near \"%s\"", $5);
2643 2644 2645 2646
					$$ = (Node *)n;
				}
		;

2647 2648 2649
opt_lmode:	IDENT		{ $$ = $1; }
		| /*EMPTY*/		{ $$ = NULL; }
		;
2650 2651 2652

/*****************************************************************************
 *
2653
 *		QUERY:
B
Bruce Momjian 已提交
2654
 *				UpdateStmt (UPDATE)
2655 2656 2657
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2658
UpdateStmt:  UPDATE relation_name
2659 2660 2661 2662
			  SET res_target_list
			  from_clause
			  where_clause
				{
B
Bruce Momjian 已提交
2663
					UpdateStmt *n = makeNode(UpdateStmt);
2664 2665 2666 2667 2668 2669 2670
					n->relname = $2;
					n->targetList = $4;
					n->fromClause = $5;
					n->whereClause = $6;
					$$ = (Node *)n;
				}
		;
2671

2672 2673 2674

/*****************************************************************************
 *
2675 2676
 *		QUERY:
 *				CURSOR STATEMENTS
2677 2678
 *
 *****************************************************************************/
B
Hi!  
Bruce Momjian 已提交
2679 2680 2681 2682 2683 2684 2685 2686 2687
/***S*I***/
CursorStmt:  DECLARE name opt_cursor CURSOR FOR SelectStmt cursor_clause
  				{
 					SelectStmt *n;
  
 					n= (SelectStmt *)$6;
  					/* from PORTAL name */
  					/*
  					 *	15 august 1991 -- since 3.0 postgres does locking
2688 2689 2690 2691 2692 2693
					 *	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 已提交
2694
						elog(ERROR,"Named portals may only be used in begin/end transaction blocks");
2695 2696 2697 2698 2699 2700

					n->portalname = $2;
					n->binary = $3;
					$$ = (Node *)n;
				}
		;
2701

2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
opt_cursor:  BINARY						{ $$ = TRUE; }
		| INSENSITIVE					{ $$ = FALSE; }
		| SCROLL						{ $$ = FALSE; }
		| INSENSITIVE SCROLL			{ $$ = FALSE; }
		| /*EMPTY*/						{ $$ = FALSE; }
		;

cursor_clause:  FOR opt_readonly		{ $$ = $2; }
		| /*EMPTY*/						{ $$ = FALSE; }
		;

opt_readonly:  READ ONLY				{ $$ = TRUE; }
		| UPDATE opt_of
			{
				elog(ERROR,"DECLARE/UPDATE not supported;"
					 " Cursors must be READ ONLY.");
				$$ = FALSE;
			}
		;

opt_of:  OF columnList
			{
				$$ = FALSE;
			}
2726 2727 2728

/*****************************************************************************
 *
2729 2730
 *		QUERY:
 *				SELECT STATEMENTS
2731 2732
 *
 *****************************************************************************/
B
Hi!  
Bruce Momjian 已提交
2733 2734 2735 2736 2737
/***S*I***/
/* The new 'SelectStmt' rule adapted for the optional use of INTERSECT EXCEPT and UNION
 * accepts the use of '(' and ')' to select an order of set operations.
 * 
 * The rule returns a SelectStmt Node having the set operations attached to 
2738 2739 2740 2741
 * unionClause and intersectClause (NIL if no set operations were present)
 */

SelectStmt:	  select_clause sort_clause for_update_clause opt_select_limit
B
Hi!  
Bruce Momjian 已提交
2742 2743 2744 2745 2746 2747 2748
			{
				/* There were no set operations, so just attach the sortClause */
				if IsA($1, SelectStmt)
				{
				  SelectStmt *n = (SelectStmt *)$1;
  				  n->sortClause = $2;
				  n->forUpdate = $3;
2749 2750
				  n->limitOffset = nth(0, $4);
				  n->limitCount = nth(1, $4);
B
Hi!  
Bruce Momjian 已提交
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809
				  $$ = (Node *)n;
                }
				/* There were set operations: The root of the operator tree
				 * is delivered by $1 but we cannot hand back an A_Expr Node.
				 * So we search for the leftmost 'SelectStmt' in the operator
				 * tree $1 (which is the first Select Statement in the query 
				 * typed in by the user or where ever it came from). 
				 * 
				 * Then we attach the whole operator tree to 'intersectClause', 
				 * and a list of all 'SelectStmt' Nodes to 'unionClause' and 
				 * hand back the leftmost 'SelectStmt' Node. (We do it this way
				 * because the following functions (e.g. parse_analyze etc.)
				 * excpect a SelectStmt node and not an operator tree! The whole
				 * tree attached to 'intersectClause' won't be touched by 
				 * parse_analyze() etc. until the function 
				 * Except_Intersect_Rewrite() (in rewriteHandler.c) which performs
				 * the necessary steps to be able create a plan!) */
				else
				{
				  List *select_list = NIL;
				  SelectStmt *first_select;
				  Node *op = (Node *) $1;
				  bool intersect_present = FALSE, unionall_present = FALSE;

				  /* Take the operator tree as an argument and 
				   * create a list of all SelectStmt Nodes found in the tree.
				   *
				   * If one of the SelectStmt Nodes has the 'unionall' flag
				   * set to true the 'unionall_present' flag is also set to
				   * true */
				  create_select_list((Node *)op, &select_list, &unionall_present);

				  /* Replace all the A_Expr Nodes in the operator tree by
				   * Expr Nodes.
				   *
				   * If an INTERSECT or an EXCEPT is present, the 
				   * 'intersect_present' flag is set to true */
				  op = A_Expr_to_Expr(op, &intersect_present);

				  /* If both flags are set to true we have a UNION ALL
				   * statement mixed up with INTERSECT or EXCEPT 
				   * which can not be handled at the moment */
				  if (intersect_present && unionall_present)
				  {
				  	elog(ERROR,"UNION ALL not allowed in mixed set operations!");
				  }

				  /* Get the leftmost SeletStmt Node (which automatically
				   * represents the first Select Statement of the query!) */
				  first_select = (SelectStmt *)lfirst(select_list);

				  /* Attach the list of all SeletStmt Nodes to unionClause */
				  first_select->unionClause = select_list;

				  /* Attach the whole operator tree to intersectClause */
				  first_select->intersectClause = (List *) op;

				  /* finally attach the sort clause */
				  first_select->sortClause = $2;
B
Bruce Momjian 已提交
2810
				  first_select->forUpdate = $3;
B
Hi!  
Bruce Momjian 已提交
2811 2812
				  $$ = (Node *)first_select;
				}		
2813 2814
				if (((SelectStmt *)$$)->forUpdate != NULL && QueryIsRule)
					elog(ERROR, "SELECT FOR UPDATE is not allowed in RULES");
B
Hi!  
Bruce Momjian 已提交
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831
			}
		;

/***S*I***/ 
/* This rule parses Select statements including UNION INTERSECT and EXCEPT.
 * '(' and ')' can be used to specify the order of the operations 
 * (UNION EXCEPT INTERSECT). Without the use of '(' and ')' we want the
 * operations to be left associative.
 *
 *  The sort_clause is not handled here!
 * 
 * The rule builds up an operator tree using A_Expr Nodes. AND Nodes represent
 * INTERSECTs OR Nodes represent UNIONs and AND NOT nodes represent EXCEPTs. 
 * The SelectStatements to be connected are the left and right arguments to
 * the A_Expr Nodes.
 * If no set operations show up in the query the tree consists only of one
 * SelectStmt Node */
2832
select_clause: '(' select_clause ')'
B
Hi!  
Bruce Momjian 已提交
2833 2834 2835 2836 2837 2838 2839
			{
				$$ = $2; 
			}
		| SubSelect
			{
				$$ = $1; 
			}
2840
		| select_clause EXCEPT select_clause
B
Hi!  
Bruce Momjian 已提交
2841 2842 2843 2844
			{
				$$ = (Node *)makeA_Expr(AND,NULL,$1,
							makeA_Expr(NOT,NULL,NULL,$3));
			}
2845
		| select_clause UNION opt_union select_clause
B
Hi!  
Bruce Momjian 已提交
2846 2847 2848 2849 2850 2851 2852 2853
			{	
				if (IsA($4, SelectStmt))
				  {
				     SelectStmt *n = (SelectStmt *)$4;
				     n->unionall = $3;
				  }
				$$ = (Node *)makeA_Expr(OR,NULL,$1,$4);
			}
2854
		| select_clause INTERSECT select_clause
B
Hi!  
Bruce Momjian 已提交
2855 2856 2857 2858
			{
				$$ = (Node *)makeA_Expr(AND,NULL,$1,$3);
			}
		; 
2859

B
Hi!  
Bruce Momjian 已提交
2860 2861
/***S*I***/
SubSelect:	SELECT opt_unique res_target_list2
2862 2863
			 result from_clause where_clause
			 group_clause having_clause
2864
				{
B
Bruce Momjian 已提交
2865
					SelectStmt *n = makeNode(SelectStmt);
2866
					n->unique = $2;
B
Hi!  
Bruce Momjian 已提交
2867
					n->unionall = FALSE;
2868
					n->targetList = $3;
B
Hi!  
Bruce Momjian 已提交
2869 2870 2871 2872 2873 2874 2875 2876
					/***S*I***/
					/* This is new: Subselects support the INTO clause
					 * which allows queries that are not part of the
					 * SQL92 standard and should not be formulated!
					 * We need it for INTERSECT and EXCEPT and I did not
					 * want to create a new rule 'SubSelect1' including the
					 * feature. If it makes troubles we will have to add 
					 * a new rule and change this to prevent INTOs in 
2877
					 * Subselects again */
2878 2879
					n->istemp = (bool) ((Value *) lfirst($4))->val.ival;
					n->into = (char *) lnext($4);
B
Hi!  
Bruce Momjian 已提交
2880

2881 2882 2883 2884 2885 2886 2887
					n->fromClause = $5;
					n->whereClause = $6;
					n->groupClause = $7;
					n->havingClause = $8;
					$$ = (Node *)n;
				}
		;
2888

2889 2890 2891
		/* easy way to return two values. Can someone improve this?  bjm */
result:  INTO OptTemp opt_table relation_name	{ $$ = lcons(makeInteger($2), (List *)$4); }
		| /*EMPTY*/								{ $$ = lcons(makeInteger(false), NIL); }
2892 2893 2894 2895
		;

opt_table:  TABLE								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
2896
		;
2897

2898 2899 2900 2901
opt_union:  ALL									{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;

2902 2903
opt_unique:  DISTINCT							{ $$ = "*"; }
		| DISTINCT ON ColId						{ $$ = $3; }
2904 2905
		| ALL									{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
2906
		;
2907

2908
sort_clause:  ORDER BY sortby_list				{ $$ = $3; }
2909
		| /*EMPTY*/								{ $$ = NIL; }
2910
		;
2911

2912 2913
sortby_list:  sortby							{ $$ = lcons($1, NIL); }
		| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
2914
		;
2915

M
 
Marc G. Fournier 已提交
2916
sortby: a_expr OptUseOp
2917 2918
				{
					$$ = makeNode(SortGroupBy);
M
 
Marc G. Fournier 已提交
2919
					$$->node = $1;
2920 2921 2922 2923 2924
					$$->useOp = $2;
				}
		;

OptUseOp:  USING Op								{ $$ = $2; }
2925 2926 2927 2928 2929
		| USING '<'								{ $$ = "<"; }
		| USING '>'								{ $$ = ">"; }
		| ASC									{ $$ = "<"; }
		| DESC									{ $$ = ">"; }
		| /*EMPTY*/								{ $$ = "<"; /*default*/ }
2930
		;
2931

2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009

opt_select_limit:	LIMIT select_limit_value ',' select_offset_value
			{ $$ = lappend(lappend(NIL, $4), $2); }
		| LIMIT select_limit_value OFFSET select_offset_value
			{ $$ = lappend(lappend(NIL, $4), $2); }
		| LIMIT select_limit_value
			{ $$ = lappend(lappend(NIL, NULL), $2); }
		| OFFSET select_offset_value LIMIT select_limit_value
			{ $$ = lappend(lappend(NIL, $2), $4); }
		| OFFSET select_offset_value
			{ $$ = lappend(lappend(NIL, $2), NULL); }
		| /* EMPTY */
			{ $$ = lappend(lappend(NIL, NULL), NULL); }
		;

select_limit_value:		Iconst
			{
				Const	*n = makeNode(Const);

				if ($1 < 1)
					elog(ERROR, "selection limit must be ALL or a positive integer value > 0");

				n->consttype	= INT4OID;
				n->constlen		= sizeof(int4);
				n->constvalue	= (Datum)$1;
				n->constisnull	= FALSE;
				n->constbyval	= TRUE;
				n->constisset	= FALSE;
				n->constiscast	= FALSE;
				$$ = (Node *)n;
			}
		| ALL
			{
				Const	*n = makeNode(Const);

				n->consttype	= INT4OID;
				n->constlen		= sizeof(int4);
				n->constvalue	= (Datum)0;
				n->constisnull	= FALSE;
				n->constbyval	= TRUE;
				n->constisset	= FALSE;
				n->constiscast	= FALSE;
				$$ = (Node *)n;
			}
		| PARAM
			{
				Param	*n = makeNode(Param);

				n->paramkind	= PARAM_NUM;
				n->paramid		= $1;
				n->paramtype	= INT4OID;
				$$ = (Node *)n;
			}
		;

select_offset_value:	Iconst
			{
				Const	*n = makeNode(Const);

				n->consttype	= INT4OID;
				n->constlen		= sizeof(int4);
				n->constvalue	= (Datum)$1;
				n->constisnull	= FALSE;
				n->constbyval	= TRUE;
				n->constisset	= FALSE;
				n->constiscast	= FALSE;
				$$ = (Node *)n;
			}
		| PARAM
			{
				Param	*n = makeNode(Param);

				n->paramkind	= PARAM_NUM;
				n->paramid		= $1;
				n->paramtype	= INT4OID;
				$$ = (Node *)n;
			}
		;
3010
/*
3011 3012
 *	jimmy bell-style recursive queries aren't supported in the
 *	current system.
3013
 *
3014 3015
 *	...however, recursive addattr and rename supported.  make special
 *	cases for these.
3016
 */
3017 3018
opt_inh_star:  '*'								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
3019
		;
3020

3021
relation_name_list:  name_list;
3022

3023
name_list:  name
3024
				{	$$ = lcons(makeString($1),NIL); }
3025
		| name_list ',' name
3026
				{	$$ = lappend($1,makeString($3)); }
3027
		;
3028

3029
group_clause:  GROUP BY expr_list				{ $$ = $3; }
3030 3031
		| /*EMPTY*/								{ $$ = NIL; }
		;
3032

B
Bruce Momjian 已提交
3033 3034
having_clause:  HAVING a_expr
				{
3035
					$$ = $2;
B
Bruce Momjian 已提交
3036
				}
3037 3038
		| /*EMPTY*/								{ $$ = NULL; }
		;
3039

3040 3041 3042 3043 3044 3045
for_update_clause:  FOR UPDATE update_list		{ $$ = $3; }
		| /* EMPTY */							{ $$ = NULL; }
		;

update_list:  OF va_list						{ $$ = $2; }
		| /* EMPTY */							{ $$ = lcons(NULL, NULL); }
V
Vadim B. Mikheev 已提交
3046
		;
3047

3048
/*****************************************************************************
3049
 *
3050 3051 3052
 *	clauses common to all Optimizable Stmts:
 *		from_clause		-
 *		where_clause	-
3053
 *
3054 3055
 *****************************************************************************/

3056
from_clause:  FROM from_expr					{ $$ = $2; }
3057 3058 3059
		| /*EMPTY*/								{ $$ = NIL; }
		;

3060 3061 3062 3063 3064 3065 3066 3067 3068
from_expr:  '(' join_clause_with_union ')'
				{ $$ = $2; }
		| join_clause
				{ $$ = $1; }
		| table_list
				{ $$ = $1; }
		;

table_list:  table_list ',' table_expr
3069
				{ $$ = lappend($1, $3); }
3070
		| table_expr
3071 3072
				{ $$ = lcons($1, NIL); }
		;
3073

3074
table_expr:  relation_expr AS ColLabel
3075 3076 3077 3078 3079
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $3;
				}
3080
		| relation_expr ColId
3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $2;
				}
		| relation_expr
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = NULL;
				}
		;
3093

3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165
/* A UNION JOIN is the same as a FULL OUTER JOIN which *omits*
 * all result rows which would have matched on an INNER JOIN.
 * Let's reject this for now. - thomas 1999-01-08
 */
join_clause_with_union:  join_clause
				{	$$ = $1; }
		| table_expr UNION JOIN table_expr
				{	elog(ERROR,"UNION JOIN not yet implemented"); }
		;

join_clause:  table_expr join_list
				{
					Node *n = lfirst($2);

					/* JoinExpr came back? then it is a join of some sort...
					 */
					if (IsA(n, JoinExpr))
					{
						JoinExpr *j = (JoinExpr *)n;
						j->larg = $1;
						$$ = $2;
					}
					/* otherwise, it was a cross join,
					 * which we just represent as an inner join...
					 */
					else
						$$ = lcons($1, $2);
				}
		;

join_list:  join_list join_expr
				{
					$$ = lappend($1, $2);
				}
		| join_expr
				{
					$$ = lcons($1, NIL);
				}
		;

/* This is everything but the left side of a join.
 * Note that a CROSS JOIN is the same as an unqualified
 * inner join, so just pass back the right-side table.
 * A NATURAL JOIN implicitly matches column names between
 * tables, so we'll collect those during the later transformation.
 */
join_expr:  join_type JOIN table_expr join_qual
				{
					JoinExpr *n = makeNode(JoinExpr);
					n->jointype = $1;
					n->rarg = (Node *)$3;
					n->quals = $4;
					$$ = (Node *)n;
				}
		| NATURAL join_type JOIN table_expr
				{
					JoinExpr *n = makeNode(JoinExpr);
					n->jointype = $2;
					n->rarg = (Node *)$4;
					n->quals = NULL; /* figure out which columns later... */
					$$ = (Node *)n;
				}
		| CROSS JOIN table_expr
				{ $$ = (Node *)$3; }
		;

/* OUTER is just noise... */
join_type:  FULL join_outer
				{
					$$ = FULL;
					elog(NOTICE,"FULL OUTER JOIN not yet implemented");
				}
3166
		| LEFT join_outer
3167 3168 3169 3170
				{
					$$ = LEFT;
					elog(NOTICE,"LEFT OUTER JOIN not yet implemented");
				}
3171
		| RIGHT join_outer
3172 3173 3174 3175
				{
					$$ = RIGHT;
					elog(NOTICE,"RIGHT OUTER JOIN not yet implemented");
				}
3176
		| OUTER_P
3177 3178 3179 3180
				{
					$$ = LEFT;
					elog(NOTICE,"OUTER JOIN not yet implemented");
				}
3181
		| INNER_P
3182 3183 3184
				{
					$$ = INNER_P;
				}
3185
		| /*EMPTY*/
3186 3187 3188
				{
					$$ = INNER_P;
				}
3189 3190
		;

3191 3192
join_outer:  OUTER_P							{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
3193 3194
		;

3195 3196 3197 3198 3199 3200 3201 3202 3203 3204
/* JOIN qualification clauses
 * Possibilities are:
 *  USING ( column list ) allows only unqualified column names,
 *                        which must match between tables.
 *  ON expr allows more general qualifications.
 * - thomas 1999-01-07
 */

join_qual:  USING '(' using_list ')'			{ $$ = $3; }
		| ON a_expr								{ $$ = lcons($2, NIL); }
3205 3206
		;

3207 3208
using_list:  using_list ',' using_expr			{ $$ = lappend($1, $3); }
		| using_expr							{ $$ = lcons($1, NIL); }
3209
		;
3210

3211
using_expr:  ColId
3212
				{
3213 3214 3215 3216 3217
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
3218 3219 3220
				}
		;

3221 3222
where_clause:  WHERE a_expr						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
		;

relation_expr:	relation_name
				{
					/* normal relations */
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = FALSE;
				}
		| relation_name '*'				  %prec '='
				{
3234
					/* inheritance query */
3235 3236 3237 3238
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = TRUE;
				}
3239 3240

opt_array_bounds:  '[' ']' nest_array_bounds
3241 3242 3243
				{  $$ = lcons(makeInteger(-1), $3); }
		| '[' Iconst ']' nest_array_bounds
				{  $$ = lcons(makeInteger($2), $4); }
T
Thomas G. Lockhart 已提交
3244
		| /*EMPTY*/
3245 3246 3247 3248 3249 3250 3251 3252 3253 3254
				{  $$ = NIL; }
		;

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

3256

3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270
/*****************************************************************************
 *
 *	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;
3271 3272 3273 3274

					/* Is this the name of a complex type? If so, implement
					 * it as a set.
					 */
3275
					if (!strcmp(saved_relname, $$->name))
3276 3277 3278 3279 3280
						/* This attr is the same type as the relation
						 * being defined. The classic example: create
						 * emp(name=text,mgr=emp)
						 */
						$$->setof = TRUE;
3281
					else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
3282 3283 3284 3285 3286 3287 3288
						 /* (Eventually add in here that the set can only
						  * contain one element.)
						  */
						$$->setof = TRUE;
					else
						$$->setof = FALSE;
				}
3289 3290
		| Character
		| SETOF Array
3291
				{
3292
					$$ = $2;
3293 3294 3295 3296
					$$->setof = TRUE;
				}
		;

3297 3298 3299
Array:  Generic
		| Datetime
		| Numeric
3300
		;
3301

3302
Generic:  generic
3303
				{
3304 3305
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($1);
3306
					$$->typmod = -1;
3307 3308 3309
				}
		;

3310
generic:  IDENT									{ $$ = $1; }
3311 3312 3313 3314 3315
		| TYPE_P								{ $$ = xlateSqlType("type"); }
		;

/* SQL92 numeric data types
 * Check FLOAT() precision limits assuming IEEE floating types.
3316
 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
3317 3318 3319 3320 3321 3322
 * - thomas 1997-09-18
 */
Numeric:  FLOAT opt_float
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($2);
3323
					$$->typmod = -1;
3324
				}
3325 3326 3327 3328 3329
		| DOUBLE PRECISION
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("float");
				}
3330
		| DECIMAL opt_decimal
3331
				{
3332
					$$ = makeNode(TypeName);
3333
					$$->name = xlateSqlType("numeric");
3334
					$$->typmod = -1;
3335 3336 3337 3338
				}
		| NUMERIC opt_numeric
				{
					$$ = makeNode(TypeName);
3339 3340
					$$->name = xlateSqlType("numeric");
					$$->typmod = $2;
3341 3342
				}
		;
3343

3344 3345 3346 3347 3348
numeric:  FLOAT
				{	$$ = xlateSqlType("float8"); }
		| DOUBLE PRECISION
				{	$$ = xlateSqlType("float8"); }
		| DECIMAL
3349
				{	$$ = xlateSqlType("numeric"); }
3350 3351 3352 3353
		| NUMERIC
				{	$$ = xlateSqlType("numeric"); }
		;

3354 3355 3356
opt_float:  '(' Iconst ')'
				{
					if ($2 < 1)
B
Bruce Momjian 已提交
3357
						elog(ERROR,"precision for FLOAT must be at least 1");
3358 3359 3360 3361 3362
					else if ($2 < 7)
						$$ = xlateSqlType("float4");
					else if ($2 < 16)
						$$ = xlateSqlType("float8");
					else
B
Bruce Momjian 已提交
3363
						elog(ERROR,"precision for FLOAT must be less than 16");
3364 3365 3366 3367 3368 3369 3370 3371 3372
				}
		| /*EMPTY*/
				{
					$$ = xlateSqlType("float8");
				}
		;

opt_numeric:  '(' Iconst ',' Iconst ')'
				{
3373 3374 3375 3376 3377 3378 3379 3380
					if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
						elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
									$2, NUMERIC_MAX_PRECISION);
					if ($4 < 0 || $4 > $2)
						elog(ERROR,"NUMERIC scale %d must be between 0 and precision %d",
									$4,$2);

					$$ = (($2 << 16) | $4) + VARHDRSZ;
3381 3382 3383
				}
		| '(' Iconst ')'
				{
3384 3385 3386 3387 3388
					if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
						elog(ERROR,"NUMERIC precision %d must be beween 1 and %d",
									$2, NUMERIC_MAX_PRECISION);

					$$ = ($2 << 16) + VARHDRSZ;
3389 3390 3391
				}
		| /*EMPTY*/
				{
3392
					$$ = ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE) + VARHDRSZ;
3393 3394 3395 3396 3397
				}
		;

opt_decimal:  '(' Iconst ',' Iconst ')'
				{
3398 3399 3400 3401 3402 3403 3404 3405
					if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
						elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
									$2, NUMERIC_MAX_PRECISION);
					if ($4 < 0 || $4 > $2)
						elog(ERROR,"DECIMAL scale %d must be between 0 and precision %d",
									$4,$2);

					$$ = (($2 << 16) | $4) + VARHDRSZ;
3406 3407 3408
				}
		| '(' Iconst ')'
				{
3409 3410 3411 3412 3413
					if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
						elog(ERROR,"DECIMAL precision %d must be beween 1 and %d",
									$2, NUMERIC_MAX_PRECISION);

					$$ = ($2 << 16) + VARHDRSZ;
3414 3415 3416
				}
		| /*EMPTY*/
				{
3417
					$$ = ((NUMERIC_DEFAULT_PRECISION << 16) | NUMERIC_DEFAULT_SCALE) + VARHDRSZ;
3418 3419 3420
				}
		;

3421

3422 3423 3424 3425 3426 3427 3428 3429 3430 3431
/* 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);
3432
					if (strcasecmp($1, "char") == 0)
3433
						$$->name = xlateSqlType("bpchar");
3434
					else if (strcasecmp($1, "varchar") == 0)
3435 3436
						$$->name = xlateSqlType("varchar");
					else
3437 3438
						yyerror("internal parsing error; unrecognized character type");

3439
					if ($3 < 1)
3440
						elog(ERROR,"length for '%s' type must be at least 1",$1);
3441 3442 3443 3444 3445
					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
3446
						 */
3447
						elog(ERROR,"length for type '%s' cannot exceed 4096",$1);
3448 3449 3450 3451 3452 3453

					/* 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
					 */
3454
					$$->typmod = VARHDRSZ + $3;
3455
				}
3456
		| character
3457 3458
				{
					$$ = makeNode(TypeName);
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
					/* 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;
					}
3472 3473
				}
		;
3474

3475 3476 3477 3478 3479 3480
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");
3481
					} else {
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491
						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)
3492
						elog(NOTICE,"COLLATE %s not yet implemented; clause ignored",$4);
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
					$$ = 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);
3517
					$$->typmod = -1;
3518 3519 3520 3521 3522 3523
				}
		| TIMESTAMP opt_timezone
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("timestamp");
					$$->timezone = $2;
3524
					$$->typmod = -1;
3525 3526 3527 3528 3529
				}
		| TIME
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("time");
3530
					$$->typmod = -1;
3531 3532 3533 3534 3535
				}
		| INTERVAL opt_interval
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("interval");
3536
					$$->typmod = -1;
3537
				}
3538
		;
3539

3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
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; }
3559
		| MINUTE_P TO SECOND_P					{ $$ = NIL; }
3560
		| /*EMPTY*/								{ $$ = NIL; }
3561 3562
		;

3563 3564 3565

/*****************************************************************************
 *
3566
 *	expression grammar, still needs some cleanup
3567 3568 3569
 *
 *****************************************************************************/

3570
a_expr_or_null:  a_expr
3571
				{ $$ = $1; }
3572
		| NULL_P
3573 3574 3575 3576 3577
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Null;
					$$ = (Node *)n;
				}
3578 3579 3580 3581 3582
		;

/* Expressions using row descriptors
 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
 *  with singleton expressions.
3583 3584 3585 3586 3587
 * 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
3588 3589 3590
 */
row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
				{
3591 3592
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3593 3594 3595
					n->oper = lcons("=",NIL);
					n->useor = false;
					n->subLinkType = ANY_SUBLINK;
3596 3597
					n->subselect = $6;
					$$ = (Node *)n;
3598 3599 3600
				}
		| '(' row_descriptor ')' NOT IN '(' SubSelect ')'
				{
3601 3602
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3603 3604 3605 3606 3607 3608
					n->oper = lcons("<>",NIL);
					n->useor = true;
					n->subLinkType = ALL_SUBLINK;
					n->subselect = $7;
					$$ = (Node *)n;
				}
3609
		| '(' row_descriptor ')' row_op sub_type '(' SubSelect ')'
3610 3611 3612 3613 3614 3615 3616 3617
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->oper = lcons($4, NIL);
					if (strcmp($4,"<>") == 0)
						n->useor = true;
					else
						n->useor = false;
3618
					n->subLinkType = $5;
B
Bruce Momjian 已提交
3619 3620 3621
					n->subselect = $7;
					$$ = (Node *)n;
				}
3622
		| '(' row_descriptor ')' row_op '(' SubSelect ')'
B
Bruce Momjian 已提交
3623 3624 3625
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
3626
					n->oper = lcons($4, NIL);
3627 3628 3629 3630
					if (strcmp($4,"<>") == 0)
						n->useor = true;
					else
						n->useor = false;
3631 3632
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $6;
B
Bruce Momjian 已提交
3633 3634
					$$ = (Node *)n;
				}
3635
		| '(' row_descriptor ')' row_op '(' row_descriptor ')'
3636
				{
B
Bruce Momjian 已提交
3637
					$$ = makeRowExpr($4, $2, $6);
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655
				}
		;

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

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

3657 3658 3659 3660 3661 3662 3663 3664
row_op:  Op									{ $$ = $1; }
		| '<'								{ $$ = "<"; }
		| '='								{ $$ = "="; }
		| '>'								{ $$ = ">"; }
		| '+'								{ $$ = "+"; }
		| '-'								{ $$ = "-"; }
		| '*'								{ $$ = "*"; }
		| '/'								{ $$ = "/"; }
3665
		| '%'								{ $$ = "%"; }
3666 3667 3668 3669 3670 3671
		;

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

T
Thomas G. Lockhart 已提交
3672
/* General expressions
3673 3674 3675 3676
 * 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.
T
Thomas G. Lockhart 已提交
3677 3678
 * All operations/expressions are allowed in a BETWEEN clause
 *  if surrounded by parens.
3679
 */
3680
a_expr:  attr opt_indirection
3681 3682 3683 3684
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
3685 3686
		| row_expr
				{	$$ = $1;  }
3687 3688
		| AexprConst
				{	$$ = $1;  }
3689 3690 3691 3692 3693 3694 3695 3696
		| ColId
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
3697 3698 3699 3700 3701 3702 3703 3704
		| '-' 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); }
3705 3706
		| a_expr '%' a_expr
				{	$$ = makeA_Expr(OP, "%", $1, $3); }
3707 3708 3709 3710 3711 3712
		| 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); }
B
cleanup  
Bruce Momjian 已提交
3713
		/* We allow this for standards-broken SQL products, like MS stuff */
3714 3715
  		| a_expr '=' NULL_P
  				{	$$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
3716 3717 3718 3719 3720 3721 3722 3723
		| 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); }
3724
		| a_expr TYPECAST Typename
3725
				{
3726
					$$ = (Node *)$1;
3727
					/* AexprConst can be either A_Const or ParamNo */
3728
					if (nodeTag($1) == T_A_Const) {
3729
						((A_Const *)$1)->typename = $3;
3730
					} else if (nodeTag($1) == T_Param) {
3731
						((ParamNo *)$1)->typename = $3;
3732 3733 3734 3735 3736 3737 3738
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
3739
				}
3740
		| CAST '(' a_expr AS Typename ')'
3741
				{
3742
					$$ = (Node *)$3;
3743
					/* AexprConst can be either A_Const or ParamNo */
3744 3745 3746 3747
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($5) == T_Param) {
						((ParamNo *)$3)->typename = $5;
3748 3749 3750
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
3751 3752
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
3753 3754
						$$ = (Node *)n;
					}
3755 3756 3757 3758
				}
		| '(' a_expr_or_null ')'
				{	$$ = $2; }
		| a_expr Op a_expr
3759
				{	$$ = makeIndexable($2,$1,$3);	}
3760
		| a_expr LIKE a_expr
3761
				{	$$ = makeIndexable("~~", $1, $3); }
3762 3763 3764 3765 3766 3767
		| 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); }
3768
		| func_name '(' '*' ')'
3769
				{
3770
					/* cheap hack for aggregate (eg. count) */
3771
					FuncCall *n = makeNode(FuncCall);
3772
					A_Const *star = makeNode(A_Const);
3773

3774 3775
					star->val.type = T_String;
					star->val.val.str = "";
3776 3777 3778 3779
					n->funcname = $1;
					n->args = lcons(star, NIL);
					$$ = (Node *)n;
				}
3780
		| func_name '(' ')'
3781 3782 3783 3784
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
3785 3786
					$$ = (Node *)n;
				}
3787
		| func_name '(' expr_list ')'
3788 3789 3790 3791 3792 3793
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804
		| 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;
3805 3806
					t->typmod = -1;
 
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819
					$$ = (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;
3820
					t->typmod = -1;
3821 3822 3823

					$$ = (Node *)n;
				}
3824
		| CURRENT_TIME '(' Iconst ')'
3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838
				{
					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;
3839
					t->typmod = -1;
3840

3841
					if ($3 != 0)
3842
						elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856

					$$ = (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;
3857
					t->typmod = -1;
3858 3859 3860

					$$ = (Node *)n;
				}
3861
		| CURRENT_TIMESTAMP '(' Iconst ')'
3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875
				{
					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;
3876
					t->typmod = -1;
3877

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

3881 3882
					$$ = (Node *)n;
				}
3883
		| CURRENT_USER
3884 3885 3886 3887 3888 3889
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3890 3891 3892 3893 3894 3895 3896
		| USER
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3897 3898
		| EXISTS '(' SubSelect ')'
				{
3899 3900
					SubLink *n = makeNode(SubLink);
					n->lefthand = NIL;
3901
					n->useor = false;
3902
					n->oper = NIL;
3903
					n->subLinkType = EXISTS_SUBLINK;
3904 3905
					n->subselect = $3;
					$$ = (Node *)n;
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958
				}
		| 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); }
3959
		| a_expr IS NULL_P
3960 3961 3962
				{	$$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
		| a_expr NOTNULL
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3963
		| a_expr IS NOT NULL_P
3964
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3965 3966 3967 3968 3969
		/* 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
		 */
3970 3971
		| a_expr IS TRUE_P
				{
3972 3973 3974 3975 3976
					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");
3977
					n->typename->typmod = -1;
3978
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3979
				}
3980
		| a_expr IS NOT FALSE_P
3981
				{
3982 3983 3984 3985 3986
					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");
3987
					n->typename->typmod = -1;
3988
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3989
				}
3990
		| a_expr IS FALSE_P
3991
				{
3992 3993 3994 3995 3996
					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");
3997
					n->typename->typmod = -1;
3998
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3999
				}
4000
		| a_expr IS NOT TRUE_P
4001
				{
4002 4003 4004 4005 4006
					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");
4007
					n->typename->typmod = -1;
4008
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
4009
				}
4010
		| a_expr BETWEEN b_expr AND b_expr
4011 4012 4013 4014 4015
				{
					$$ = makeA_Expr(AND, NULL,
						makeA_Expr(OP, ">=", $1, $3),
						makeA_Expr(OP, "<=", $1, $5));
				}
4016
		| a_expr NOT BETWEEN b_expr AND b_expr
4017 4018 4019 4020 4021
				{
					$$ = makeA_Expr(OR, NULL,
						makeA_Expr(OP, "<", $1, $4),
						makeA_Expr(OP, ">", $1, $6));
				}
4022
		| a_expr IN { saved_In_Expr = lcons($1,saved_In_Expr); } '(' in_expr ')'
4023
				{
4024
					saved_In_Expr = lnext(saved_In_Expr);
4025 4026
					if (nodeTag($5) == T_SubLink)
					{
4027 4028 4029 4030 4031 4032
							SubLink *n = (SubLink *)$5;
							n->lefthand = lcons($1, NIL);
							n->oper = lcons("=",NIL);
							n->useor = false;
							n->subLinkType = ANY_SUBLINK;
							$$ = (Node *)n;
4033 4034 4035
					}
					else	$$ = $5;
				}
4036
		| a_expr NOT IN { saved_In_Expr = lcons($1,saved_In_Expr); } '(' not_in_expr ')'
4037
				{
4038
					saved_In_Expr = lnext(saved_In_Expr);
4039 4040
					if (nodeTag($6) == T_SubLink)
					{
4041 4042 4043 4044 4045 4046
							SubLink *n = (SubLink *)$6;
							n->lefthand = lcons($1, NIL);
							n->oper = lcons("<>",NIL);
							n->useor = false;
							n->subLinkType = ALL_SUBLINK;
							$$ = (Node *)n;
4047 4048 4049
					}
					else	$$ = $6;
				}
4050
		| a_expr Op '(' SubSelect ')'
4051 4052
				{
					SubLink *n = makeNode(SubLink);
4053
					n->lefthand = lcons($1, NULL);
4054 4055
					n->oper = lcons($2,NIL);
					n->useor = false;
4056 4057
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
4058 4059
					$$ = (Node *)n;
				}
4060
		| a_expr '+' '(' SubSelect ')'
B
Bruce Momjian 已提交
4061 4062
				{
					SubLink *n = makeNode(SubLink);
4063
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4064 4065
					n->oper = lcons("+",NIL);
					n->useor = false;
4066 4067
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4068 4069
					$$ = (Node *)n;
				}
4070
		| a_expr '-' '(' SubSelect ')'
B
Bruce Momjian 已提交
4071 4072
				{
					SubLink *n = makeNode(SubLink);
4073
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4074 4075
					n->oper = lcons("-",NIL);
					n->useor = false;
4076 4077
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4078 4079
					$$ = (Node *)n;
				}
4080
		| a_expr '/' '(' SubSelect ')'
B
Bruce Momjian 已提交
4081 4082
				{
					SubLink *n = makeNode(SubLink);
4083
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4084 4085
					n->oper = lcons("/",NIL);
					n->useor = false;
4086 4087
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4088 4089
					$$ = (Node *)n;
				}
4090 4091 4092 4093 4094 4095 4096 4097 4098 4099
		| a_expr '%' '(' SubSelect ')'
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("%",NIL);
					n->useor = false;
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
					$$ = (Node *)n;
				}
4100
		| a_expr '*' '(' SubSelect ')'
B
Bruce Momjian 已提交
4101 4102
				{
					SubLink *n = makeNode(SubLink);
4103
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4104 4105
					n->oper = lcons("*",NIL);
					n->useor = false;
4106 4107
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4108 4109
					$$ = (Node *)n;
				}
4110
		| a_expr '<' '(' SubSelect ')'
B
Bruce Momjian 已提交
4111 4112
				{
					SubLink *n = makeNode(SubLink);
4113
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4114 4115
					n->oper = lcons("<",NIL);
					n->useor = false;
4116 4117
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4118 4119
					$$ = (Node *)n;
				}
4120
		| a_expr '>' '(' SubSelect ')'
B
Bruce Momjian 已提交
4121 4122
				{
					SubLink *n = makeNode(SubLink);
4123
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4124 4125
					n->oper = lcons(">",NIL);
					n->useor = false;
4126 4127
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4128 4129
					$$ = (Node *)n;
				}
4130
		| a_expr '=' '(' SubSelect ')'
B
Bruce Momjian 已提交
4131 4132
				{
					SubLink *n = makeNode(SubLink);
4133
					n->lefthand = lcons($1, NULL);
B
Bruce Momjian 已提交
4134 4135
					n->oper = lcons("=",NIL);
					n->useor = false;
4136 4137
					n->subLinkType = EXPR_SUBLINK;
					n->subselect = $4;
B
Bruce Momjian 已提交
4138 4139
					$$ = (Node *)n;
				}
4140
		| a_expr Op ANY '(' SubSelect ')'
4141 4142
				{
					SubLink *n = makeNode(SubLink);
4143
					n->lefthand = lcons($1,NIL);
4144 4145
					n->oper = lcons($2,NIL);
					n->useor = false;
4146
					n->subLinkType = ANY_SUBLINK;
4147 4148 4149
					n->subselect = $5;
					$$ = (Node *)n;
				}
4150
		| a_expr '+' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4151 4152
				{
					SubLink *n = makeNode(SubLink);
4153
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4154 4155
					n->oper = lcons("+",NIL);
					n->useor = false;
4156
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4157 4158 4159
					n->subselect = $5;
					$$ = (Node *)n;
				}
4160
		| a_expr '-' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4161 4162
				{
					SubLink *n = makeNode(SubLink);
4163
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4164 4165
					n->oper = lcons("-",NIL);
					n->useor = false;
4166
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4167 4168 4169
					n->subselect = $5;
					$$ = (Node *)n;
				}
4170
		| a_expr '/' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4171 4172
				{
					SubLink *n = makeNode(SubLink);
4173
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4174 4175
					n->oper = lcons("/",NIL);
					n->useor = false;
4176
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4177 4178 4179
					n->subselect = $5;
					$$ = (Node *)n;
				}
4180 4181 4182 4183 4184 4185 4186 4187 4188 4189
		| a_expr '%' ANY '(' SubSelect ')'
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1,NIL);
					n->oper = lcons("%",NIL);
					n->useor = false;
					n->subLinkType = ANY_SUBLINK;
					n->subselect = $5;
					$$ = (Node *)n;
				}
4190
		| a_expr '*' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4191 4192
				{
					SubLink *n = makeNode(SubLink);
4193
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4194 4195
					n->oper = lcons("*",NIL);
					n->useor = false;
4196
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4197 4198 4199
					n->subselect = $5;
					$$ = (Node *)n;
				}
4200
		| a_expr '<' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4201 4202
				{
					SubLink *n = makeNode(SubLink);
4203
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4204 4205
					n->oper = lcons("<",NIL);
					n->useor = false;
4206
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4207 4208 4209
					n->subselect = $5;
					$$ = (Node *)n;
				}
4210
		| a_expr '>' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4211 4212
				{
					SubLink *n = makeNode(SubLink);
4213
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4214 4215
					n->oper = lcons(">",NIL);
					n->useor = false;
4216
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4217 4218 4219
					n->subselect = $5;
					$$ = (Node *)n;
				}
4220
		| a_expr '=' ANY '(' SubSelect ')'
B
Bruce Momjian 已提交
4221 4222
				{
					SubLink *n = makeNode(SubLink);
4223
					n->lefthand = lcons($1,NIL);
B
Bruce Momjian 已提交
4224 4225
					n->oper = lcons("=",NIL);
					n->useor = false;
4226
					n->subLinkType = ANY_SUBLINK;
B
Bruce Momjian 已提交
4227 4228 4229
					n->subselect = $5;
					$$ = (Node *)n;
				}
4230
		| a_expr Op ALL '(' SubSelect ')'
4231 4232 4233 4234 4235 4236
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons($2,NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4237
					n->subselect = $5;
4238 4239
					$$ = (Node *)n;
				}
4240
		| a_expr '+' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4241 4242 4243 4244 4245 4246
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("+",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4247
					n->subselect = $5;
B
Bruce Momjian 已提交
4248 4249
					$$ = (Node *)n;
				}
4250
		| a_expr '-' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4251 4252 4253 4254 4255 4256
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("-",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4257
					n->subselect = $5;
B
Bruce Momjian 已提交
4258 4259
					$$ = (Node *)n;
				}
4260
		| a_expr '/' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4261 4262 4263 4264 4265 4266
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("/",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4267
					n->subselect = $5;
B
Bruce Momjian 已提交
4268 4269
					$$ = (Node *)n;
				}
4270 4271 4272 4273 4274 4275 4276 4277 4278 4279
		| a_expr '%' ALL '(' SubSelect ')'
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("%",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
					n->subselect = $5;
					$$ = (Node *)n;
				}
4280
		| a_expr '*' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4281 4282 4283 4284 4285 4286
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("*",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4287
					n->subselect = $5;
B
Bruce Momjian 已提交
4288 4289
					$$ = (Node *)n;
				}
4290
		| a_expr '<' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4291 4292 4293 4294 4295 4296
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("<",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4297
					n->subselect = $5;
B
Bruce Momjian 已提交
4298 4299
					$$ = (Node *)n;
				}
4300
		| a_expr '>' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4301 4302 4303 4304 4305 4306
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons(">",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4307
					n->subselect = $5;
B
Bruce Momjian 已提交
4308 4309
					$$ = (Node *)n;
				}
4310
		| a_expr '=' ALL '(' SubSelect ')'
B
Bruce Momjian 已提交
4311 4312 4313 4314 4315 4316
				{
					SubLink *n = makeNode(SubLink);
					n->lefthand = lcons($1, NULL);
					n->oper = lcons("=",NIL);
					n->useor = false;
					n->subLinkType = ALL_SUBLINK;
4317
					n->subselect = $5;
B
Bruce Momjian 已提交
4318 4319
					$$ = (Node *)n;
				}
4320 4321 4322 4323 4324 4325
		| 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); }
T
Thomas G. Lockhart 已提交
4326 4327
		| case_expr
				{	$$ = $1; }
4328
		;
4329

T
Thomas G. Lockhart 已提交
4330
/* Restricted expressions
4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357
 * 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); }
4358 4359
		| b_expr '%' b_expr
				{	$$ = makeA_Expr(OP, "%", $1, $3); }
4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383
		| 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;
					}
				}
4384
		| CAST '(' b_expr AS Typename ')'
4385
				{
4386
					$$ = (Node *)$3;
4387
					/* AexprConst can be either A_Const or ParamNo */
4388 4389 4390 4391
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($3) == T_Param) {
						((ParamNo *)$3)->typename = $5;
4392 4393 4394
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
4395 4396
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407
						$$ = (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); }
4408
		| func_name '(' ')'
4409 4410 4411 4412 4413 4414
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
					$$ = (Node *)n;
				}
4415
		| func_name '(' expr_list ')'
4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
				{
					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;
4433
					t->typmod = -1;
4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447

					$$ = (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;
4448
					t->typmod = -1;
4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466

					$$ = (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;
4467
					t->typmod = -1;
4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484

					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;
4485
					t->typmod = -1;
4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503

					$$ = (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;
4504
					t->typmod = -1;
4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517

					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;
				}
4518 4519 4520 4521 4522 4523 4524
		| USER
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569
		| 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;
				}
		;

4570
opt_indirection:  '[' a_expr ']' opt_indirection
4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583
				{
					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);
				}
T
Thomas G. Lockhart 已提交
4584
		| /*EMPTY*/
4585 4586
				{	$$ = NIL; }
		;
4587

4588
expr_list:  a_expr_or_null
4589
				{ $$ = lcons($1, NIL); }
4590
		| expr_list ',' a_expr_or_null
4591
				{ $$ = lappend($1, $3); }
4592
		| expr_list USING a_expr
4593 4594
				{ $$ = lappend($1, $3); }
		;
4595

4596
extract_list:  extract_arg FROM a_expr
4597 4598 4599 4600 4601 4602
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = $1;
					$$ = lappend(lcons((Node *)n,NIL), $3);
				}
T
Thomas G. Lockhart 已提交
4603
		| /*EMPTY*/
4604 4605
				{	$$ = NIL; }
		;
4606

4607 4608 4609
extract_arg:  datetime						{ $$ = $1; }
		| TIMEZONE_HOUR						{ $$ = "tz_hour"; }
		| TIMEZONE_MINUTE					{ $$ = "tz_minute"; }
4610 4611
		;

4612
position_list:  position_expr IN position_expr
4613
				{	$$ = makeList($3, $1, -1); }
T
Thomas G. Lockhart 已提交
4614
		| /*EMPTY*/
4615 4616
				{	$$ = NIL; }
		;
4617

4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632
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); }
4633 4634
		| position_expr '%' position_expr
				{	$$ = makeA_Expr(OP, "%", $1, $3); }
4635 4636 4637 4638
		| position_expr '*' position_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| '|' position_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
4639
		| position_expr TYPECAST Typename
4640
				{
4641
					$$ = (Node *)$1;
4642
					/* AexprConst can be either A_Const or ParamNo */
4643
					if (nodeTag($1) == T_A_Const) {
4644
						((A_Const *)$1)->typename = $3;
4645
					} else if (nodeTag($1) == T_Param) {
4646
						((ParamNo *)$1)->typename = $3;
4647 4648 4649 4650 4651 4652 4653
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
4654
				}
4655
		| CAST '(' position_expr AS Typename ')'
4656
				{
4657
					$$ = (Node *)$3;
4658
					/* AexprConst can be either A_Const or ParamNo */
4659 4660 4661 4662
					if (nodeTag($3) == T_A_Const) {
						((A_Const *)$3)->typename = $5;
					} else if (nodeTag($3) == T_Param) {
						((ParamNo *)$3)->typename = $5;
4663 4664 4665
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
4666 4667
						n->funcname = $5->name;
						n->args = lcons($3,NIL);
4668 4669
						$$ = (Node *)n;
					}
4670 4671 4672 4673 4674 4675 4676 4677 4678
				}
		| '(' 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); }
4679
		| ColId
4680 4681 4682 4683 4684 4685 4686
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
4687
		| func_name '(' ')'
4688 4689 4690 4691 4692 4693
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
					$$ = (Node *)n;
				}
4694
		| func_name '(' expr_list ')'
4695 4696 4697 4698 4699 4700
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745
		| 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;
				}
		;

4746
substr_list:  expr_list substr_from substr_for
4747
				{
4748
					$$ = nconc(nconc($1,$2),$3);
4749
				}
T
Thomas G. Lockhart 已提交
4750
		| /*EMPTY*/
4751 4752
				{	$$ = NIL; }
		;
4753

4754
substr_from:  FROM expr_list
4755
				{	$$ = $2; }
T
Thomas G. Lockhart 已提交
4756
		| /*EMPTY*/
4757 4758 4759 4760 4761 4762
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Integer;
					n->val.val.ival = 1;
					$$ = lcons((Node *)n,NIL);
				}
4763
		;
4764

4765
substr_for:  FOR expr_list
4766
				{	$$ = $2; }
T
Thomas G. Lockhart 已提交
4767
		| /*EMPTY*/
4768 4769
				{	$$ = NIL; }
		;
4770

4771
trim_list:  a_expr FROM expr_list
4772
				{ $$ = lappend($3, $1); }
4773
		| FROM expr_list
4774
				{ $$ = $2; }
4775
		| expr_list
4776 4777
				{ $$ = $1; }
		;
4778

4779 4780
in_expr:  SubSelect
				{
4781 4782 4783
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
4784 4785 4786 4787 4788
				}
		| in_expr_nodes
				{	$$ = $1; }
		;

4789
in_expr_nodes:  AexprConst
4790
				{	$$ = makeA_Expr(OP, "=", lfirst(saved_In_Expr), $1); }
4791
		| in_expr_nodes ',' AexprConst
4792
				{	$$ = makeA_Expr(OR, NULL, $1,
4793
						makeA_Expr(OP, "=", lfirst(saved_In_Expr), $3));
4794 4795
				}
		;
4796

4797 4798
not_in_expr:  SubSelect
				{
4799 4800 4801
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
4802 4803 4804 4805 4806
				}
		| not_in_expr_nodes
				{	$$ = $1; }
		;

4807
not_in_expr_nodes:  AexprConst
4808
				{	$$ = makeA_Expr(OP, "<>", lfirst(saved_In_Expr), $1); }
4809
		| not_in_expr_nodes ',' AexprConst
4810
				{	$$ = makeA_Expr(AND, NULL, $1,
4811
						makeA_Expr(OP, "<>", lfirst(saved_In_Expr), $3));
4812 4813
				}
		;
4814

T
Thomas G. Lockhart 已提交
4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841
/* Case clause
 * Define SQL92-style case clause.
 * Allow all four forms described in the standard:
 * - Full specification
 *  CASE WHEN a = b THEN c ... ELSE d END
 * - Implicit argument
 *  CASE a WHEN b THEN c ... ELSE d END
 * - Conditional NULL
 *  NULLIF(x,y)
 *  same as CASE WHEN x = y THEN NULL ELSE x END
 * - Conditional substitution from list, use first non-null argument
 *  COALESCE(a,b,...)
 * same as CASE WHEN a IS NOT NULL THEN a WHEN b IS NOT NULL THEN b ... END
 * - thomas 1998-11-09
 */
case_expr:  CASE case_arg when_clause_list case_default END_TRANS
				{
					CaseExpr *c = makeNode(CaseExpr);
					c->arg = $2;
					c->args = $3;
					c->defresult = $4;
					$$ = (Node *)c;
				}
		| NULLIF '(' a_expr ',' a_expr ')'
				{
					CaseExpr *c = makeNode(CaseExpr);
					CaseWhen *w = makeNode(CaseWhen);
4842 4843 4844 4845 4846 4847
/*
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Null;
					w->result = (Node *)n;
*/
					w->expr = makeA_Expr(OP, "=", $3, $5);
T
Thomas G. Lockhart 已提交
4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903
					c->args = lcons(w, NIL);
					c->defresult = $3;
					$$ = (Node *)c;
				}
		| COALESCE '(' expr_list ')'
				{
					CaseExpr *c = makeNode(CaseExpr);
					CaseWhen *w;
					List *l;
					foreach (l,$3)
					{
						w = makeNode(CaseWhen);
						w->expr = makeA_Expr(NOTNULL, NULL, lfirst(l), NULL);
						w->result = lfirst(l);
						c->args = lappend(c->args, w);
					}
					$$ = (Node *)c;
				}
		;

when_clause_list:  when_clause_list when_clause
				{ $$ = lappend($1, $2); }
		| when_clause
				{ $$ = lcons($1, NIL); }
		;

when_clause:  WHEN a_expr THEN a_expr_or_null
				{
					CaseWhen *w = makeNode(CaseWhen);
					w->expr = $2;
					w->result = $4;
					$$ = (Node *)w;
				}
		;

case_default:  ELSE a_expr_or_null				{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

case_arg:  attr opt_indirection
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
		| ColId
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
		| /*EMPTY*/
				{	$$ = NULL; }
		;

4904
attr:  relation_name '.' attrs
4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
				{
					$$ = 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("*")); }
		;

4930 4931 4932

/*****************************************************************************
 *
4933
 *	target lists
4934 4935 4936
 *
 *****************************************************************************/

4937
res_target_list:  res_target_list ',' res_target_el
4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954
				{	$$ = 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);
				}
		;
4955

4956
res_target_el:  ColId opt_indirection '=' a_expr_or_null
4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982
				{
					$$ = 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;
				}
		;
4983 4984 4985

/*
** target list for select.
B
Bruce Momjian 已提交
4986
** should get rid of the other but is still needed by the defunct select into
4987 4988
** and update (uses a subset)
*/
4989
res_target_list2:  res_target_list2 ',' res_target_el2
4990 4991 4992 4993
				{	$$ = lappend($1, $3);  }
		| res_target_el2
				{	$$ = lcons($1, NIL);  }
		;
4994 4995

/* AS is not optional because shift/red conflict with unary ops */
4996
res_target_el2:  a_expr_or_null AS ColLabel
4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035
				{
					$$ = 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;
				}
		;

5036
opt_id:  ColId									{ $$ = $1; }
T
Thomas G. Lockhart 已提交
5037
		| /*EMPTY*/								{ $$ = NULL; }
5038 5039 5040 5041 5042
		;

relation_name:	SpecialRuleRelation
				{
					$$ = $1;
5043
					StrNCpy(saved_relname, $1, NAMEDATALEN);
5044 5045 5046
				}
		| ColId
				{
5047
					/* disallow refs to variable system tables */
5048
					if (strcmp(LogRelationName, $1) == 0
5049
					   || strcmp(VariableRelationName, $1) == 0)
5050
						elog(ERROR,"%s cannot be accessed by users",$1);
5051 5052
					else
						$$ = $1;
5053
					StrNCpy(saved_relname, $1, NAMEDATALEN);
5054 5055 5056
				}
		;

5057
database_name:			ColId			{ $$ = $1; };
5058
access_method:			IDENT			{ $$ = $1; };
5059
attr_name:				ColId			{ $$ = $1; };
5060
class:					IDENT			{ $$ = $1; };
5061
index_name:				ColId			{ $$ = $1; };
5062

5063 5064 5065 5066 5067
/* Functions
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
 */
name:					ColId			{ $$ = $1; };
5068
func_name:				ColId			{ $$ = xlateSqlFunc($1); };
5069 5070

file_name:				Sconst			{ $$ = $1; };
B
Bruce Momjian 已提交
5071
/* NOT USED recipe_name:			IDENT			{ $$ = $1; };*/
5072

5073 5074 5075
/* Constants
 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
 */
5076
AexprConst:  Iconst
5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096
				{
					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;
				}
5097 5098 5099 5100 5101 5102 5103 5104
		| Typename Sconst
				{
					A_Const *n = makeNode(A_Const);
					n->typename = $1;
					n->val.type = T_String;
					n->val.val.str = $2;
					$$ = (Node *)n;
				}
5105 5106
		| ParamNo
				{	$$ = (Node *)$1;  }
5107 5108 5109 5110 5111
		| TRUE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "t";
5112 5113
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
5114
					n->typename->typmod = -1;
5115 5116 5117 5118 5119 5120 5121
					$$ = (Node *)n;
				}
		| FALSE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "f";
5122 5123
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
5124
					n->typename->typmod = -1;
5125 5126
					$$ = (Node *)n;
				}
5127
		;
5128

5129
ParamNo:  PARAM opt_indirection
5130 5131 5132
				{
					$$ = makeNode(ParamNo);
					$$->number = $1;
5133
					$$->indirection = $2;
5134 5135
				}
		;
5136

5137 5138
Iconst:  ICONST							{ $$ = $1; };
Sconst:  SCONST							{ $$ = $1; };
5139
UserId:  IDENT							{ $$ = $1; };
5140

5141 5142 5143 5144 5145
/* 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
 */
5146 5147 5148 5149 5150 5151 5152
TypeId:  ColId
			{	$$ = xlateSqlType($1); }
		| numeric
			{	$$ = xlateSqlType($1); }
		| character
			{	$$ = xlateSqlType($1); }
		;
5153 5154 5155
/* Column identifier
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
5156 5157 5158 5159
 * 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
5160
 */
5161
ColId:  IDENT							{ $$ = $1; }
5162
		| datetime						{ $$ = $1; }
5163
		| ABSOLUTE						{ $$ = "absolute"; }
5164
		| ACTION						{ $$ = "action"; }
5165 5166 5167 5168
		| AFTER							{ $$ = "after"; }
		| AGGREGATE						{ $$ = "aggregate"; }
		| BACKWARD						{ $$ = "backward"; }
		| BEFORE						{ $$ = "before"; }
5169
		| CACHE							{ $$ = "cache"; }
5170 5171
		| CREATEDB						{ $$ = "createdb"; }
		| CREATEUSER					{ $$ = "createuser"; }
5172
		| CYCLE							{ $$ = "cycle"; }
5173 5174
		| DATABASE						{ $$ = "database"; }
		| DELIMITERS					{ $$ = "delimiters"; }
5175 5176
		| DOUBLE						{ $$ = "double"; }
		| EACH							{ $$ = "each"; }
M
 
Marc G. Fournier 已提交
5177
		| ENCODING						{ $$ = "encoding"; }
5178
		| FORWARD						{ $$ = "forward"; }
5179
		| FUNCTION						{ $$ = "function"; }
5180
		| HANDLER						{ $$ = "handler"; }
5181
		| INCREMENT						{ $$ = "increment"; }
5182
		| INDEX							{ $$ = "index"; }
5183
		| INHERITS						{ $$ = "inherits"; }
5184
		| INSENSITIVE					{ $$ = "insensitive"; }
5185 5186
		| INSTEAD						{ $$ = "instead"; }
		| ISNULL						{ $$ = "isnull"; }
5187 5188
		| KEY							{ $$ = "key"; }
		| LANGUAGE						{ $$ = "language"; }
5189
		| LANCOMPILER					{ $$ = "lancompiler"; }
5190 5191
		| LOCATION						{ $$ = "location"; }
		| MATCH							{ $$ = "match"; }
5192 5193
		| MAXVALUE						{ $$ = "maxvalue"; }
		| MINVALUE						{ $$ = "minvalue"; }
5194
		| NEXT							{ $$ = "next"; }
5195 5196 5197 5198
		| NOCREATEDB					{ $$ = "nocreatedb"; }
		| NOCREATEUSER					{ $$ = "nocreateuser"; }
		| NOTHING						{ $$ = "nothing"; }
		| NOTNULL						{ $$ = "notnull"; }
5199
		| OF							{ $$ = "of"; }
5200
		| OIDS							{ $$ = "oids"; }
5201
		| ONLY							{ $$ = "only"; }
5202 5203
		| OPERATOR						{ $$ = "operator"; }
		| OPTION						{ $$ = "option"; }
5204
		| PASSWORD						{ $$ = "password"; }
5205
		| PRIOR							{ $$ = "prior"; }
5206
		| PRIVILEGES					{ $$ = "privileges"; }
5207
		| PROCEDURAL					{ $$ = "procedural"; }
5208 5209
		| READ							{ $$ = "read"; }
		| RELATIVE						{ $$ = "relative"; }
5210 5211
		| RENAME						{ $$ = "rename"; }
		| RETURNS						{ $$ = "returns"; }
5212
		| ROW							{ $$ = "row"; }
5213
		| RULE							{ $$ = "rule"; }
5214
		| SCROLL						{ $$ = "scroll"; }
5215
		| SEQUENCE						{ $$ = "sequence"; }
5216
		| SERIAL						{ $$ = "serial"; }
5217
		| START							{ $$ = "start"; }
5218
		| STATEMENT						{ $$ = "statement"; }
5219 5220
		| STDIN							{ $$ = "stdin"; }
		| STDOUT						{ $$ = "stdout"; }
5221
		| TIME							{ $$ = "time"; }
5222
		| TIMESTAMP						{ $$ = "timestamp"; }
5223 5224
		| TIMEZONE_HOUR					{ $$ = "timezone_hour"; }
		| TIMEZONE_MINUTE				{ $$ = "timezone_minute"; }
5225
		| TRIGGER						{ $$ = "trigger"; }
5226
		| TRUSTED						{ $$ = "trusted"; }
5227
		| TYPE_P						{ $$ = "type"; }
5228
		| VALID							{ $$ = "valid"; }
5229 5230
		| VERSION						{ $$ = "version"; }
		| ZONE							{ $$ = "zone"; }
5231 5232 5233 5234 5235 5236 5237 5238
		;

/* 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
5239 5240 5241
 * 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
5242 5243
 */
ColLabel:  ColId						{ $$ = $1; }
5244 5245 5246
		| ABORT_TRANS					{ $$ = "abort"; }
		| ANALYZE						{ $$ = "analyze"; }
		| BINARY						{ $$ = "binary"; }
T
Thomas G. Lockhart 已提交
5247
		| CASE							{ $$ = "case"; }
5248
		| CLUSTER						{ $$ = "cluster"; }
T
Thomas G. Lockhart 已提交
5249
		| COALESCE						{ $$ = "coalesce"; }
5250
		| CONSTRAINT					{ $$ = "constraint"; }
5251 5252 5253
		| COPY							{ $$ = "copy"; }
		| CURRENT						{ $$ = "current"; }
		| DO							{ $$ = "do"; }
T
Thomas G. Lockhart 已提交
5254 5255
		| ELSE							{ $$ = "else"; }
		| END_TRANS						{ $$ = "end"; }
5256 5257 5258
		| EXPLAIN						{ $$ = "explain"; }
		| EXTEND						{ $$ = "extend"; }
		| FALSE_P						{ $$ = "false"; }
5259 5260
		| FOREIGN						{ $$ = "foreign"; }
		| GROUP							{ $$ = "group"; }
5261
		| LISTEN						{ $$ = "listen"; }
5262
		| LOAD							{ $$ = "load"; }
5263 5264 5265 5266
		| LOCK_P						{ $$ = "lock"; }
		| MOVE							{ $$ = "move"; }
		| NEW							{ $$ = "new"; }
		| NONE							{ $$ = "none"; }
T
Thomas G. Lockhart 已提交
5267
		| NULLIF						{ $$ = "nullif"; }
5268 5269 5270
		| ORDER							{ $$ = "order"; }
		| POSITION						{ $$ = "position"; }
		| PRECISION						{ $$ = "precision"; }
5271 5272 5273
		| RESET							{ $$ = "reset"; }
		| SETOF							{ $$ = "setof"; }
		| SHOW							{ $$ = "show"; }
5274
		| TABLE							{ $$ = "table"; }
T
Thomas G. Lockhart 已提交
5275
		| THEN							{ $$ = "then"; }
5276
		| TRANSACTION					{ $$ = "transaction"; }
5277
		| TRUE_P						{ $$ = "true"; }
5278 5279
		| VACUUM						{ $$ = "vacuum"; }
		| VERBOSE						{ $$ = "verbose"; }
T
Thomas G. Lockhart 已提交
5280
		| WHEN							{ $$ = "when"; }
5281
		;
5282 5283

SpecialRuleRelation:  CURRENT
5284 5285 5286 5287
				{
					if (QueryIsRule)
						$$ = "*CURRENT*";
					else
B
Bruce Momjian 已提交
5288
						elog(ERROR,"CURRENT used in non-rule query");
5289 5290 5291 5292 5293 5294
				}
		| NEW
				{
					if (QueryIsRule)
						$$ = "*NEW*";
					else
B
Bruce Momjian 已提交
5295
						elog(ERROR,"NEW used in non-rule query");
5296 5297
				}
		;
5298 5299 5300

%%

5301 5302
static Node *
makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
5303
{
5304 5305 5306 5307 5308 5309
	A_Expr *a = makeNode(A_Expr);
	a->oper = oper;
	a->opname = opname;
	a->lexpr = lexpr;
	a->rexpr = rexpr;
	return (Node *)a;
5310 5311
}

B
Bruce Momjian 已提交
5312 5313
/* makeRowExpr()
 * Generate separate operator nodes for a single row descriptor expression.
5314 5315
 * Perhaps this should go deeper in the parser someday...
 * - thomas 1997-12-22
B
Bruce Momjian 已提交
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357
 */
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);
	}

	return expr;
}

5358
static void
5359 5360 5361 5362 5363 5364
mapTargetColumns(List *src, List *dst)
{
	ColumnDef *s;
	ResTarget *d;

	if (length(src) != length(dst))
B
Bruce Momjian 已提交
5365
		elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379

	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() */
5380

5381 5382 5383 5384 5385
static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr)
{
	Node *result = NULL;

	/* we do this so indexes can be used */
5386 5387
	if (strcmp(opname,"~") == 0 ||
		strcmp(opname,"~*") == 0)
5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404
	{
		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] == '[' ||
5405
					n->val.val.str[pos] == '$' ||
5406
					(strcmp(opname,"~*") == 0 && isalpha(n->val.val.str[pos])))
5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426
		     		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;
B
Bruce Momjian 已提交
5427 5428 5429 5430 5431
#ifdef USE_LOCALE
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~", lexpr, rexpr),
						makeA_Expr(OP, ">=", lexpr, (Node *)least));
#else
5432 5433 5434 5435 5436
				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)));
B
Bruce Momjian 已提交
5437
#endif
5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452
			}
		}
	}
	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++)
			{
5453 5454 5455 5456 5457 5458 5459
				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] == '%')
5460
					pos++;
5461 5462
				if (n->val.val.str[pos] == '\0')
					break;
5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479
				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;
B
Bruce Momjian 已提交
5480 5481 5482 5483 5484
#ifdef USE_LOCALE
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~~", lexpr, rexpr),
						makeA_Expr(OP, ">=", lexpr, (Node *)least));
#else
5485 5486 5487 5488 5489
				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)));
B
Bruce Momjian 已提交
5490
#endif
5491 5492 5493 5494 5495 5496 5497
			}
		}
	}
	
	if (result == NULL)
		result = makeA_Expr(OP, opname, lexpr, rexpr);
	return result;
5498
} /* makeIndexable() */
5499 5500


5501
/* xlateSqlFunc()
5502
 * Convert alternate type names to internal Postgres types.
5503 5504
 * Do not convert "float", since that is handled elsewhere
 *  for FLOAT(p) syntax.
5505
 */
5506
static char *
5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519
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 *
5520 5521
xlateSqlType(char *name)
{
5522 5523
	if (!strcasecmp(name,"int")
	 || !strcasecmp(name,"integer"))
5524
		return "int4";
5525 5526
	else if (!strcasecmp(name, "smallint"))
		return "int2";
5527 5528
	else if (!strcasecmp(name, "real")
	 || !strcasecmp(name, "float"))
5529 5530 5531
		return "float8";
	else if (!strcasecmp(name, "interval"))
		return "timespan";
5532 5533
	else if (!strcasecmp(name, "boolean"))
		return "bool";
5534 5535
	else
		return name;
5536
} /* xlateSqlType() */
5537

5538

5539 5540
void parser_init(Oid *typev, int nargs)
{
5541
	QueryIsRule = FALSE;
5542 5543
	saved_relname[0]= '\0';
	saved_In_Expr = NULL;
5544

5545
	param_type_init(typev, nargs);
5546
}
5547

5548

5549 5550 5551
/* FlattenStringList()
 * Traverse list of string nodes and convert to a single string.
 * Used for reconstructing string form of complex expressions.
5552 5553
 *
 * Allocate at least one byte for terminator.
5554 5555 5556 5557
 */
static char *
FlattenStringList(List *list)
{
5558 5559
	List *l;
	Value *v;
5560 5561
	char *s;
	char *sp;
5562
	int nlist, len = 0;
5563 5564 5565 5566

	nlist = length(list);
	l = list;
	while(l != NIL) {
5567 5568
		v = (Value *)lfirst(l);
		sp = v->val.str;
5569
		l = lnext(l);
5570
		len += strlen(sp);
5571
	};
5572
	len += nlist;
5573

5574
	s = (char*) palloc(len+1);
5575 5576 5577 5578
	*s = '\0';

	l = list;
	while(l != NIL) {
5579 5580
		v = (Value *)lfirst(l);
		sp = v->val.str;
5581 5582
		l = lnext(l);
		strcat(s,sp);
5583
		if (l != NIL) strcat(s," ");
5584
	};
5585
	*(s+len) = '\0';
5586

5587
	return s;
5588 5589
} /* FlattenStringList() */

5590

5591 5592 5593 5594 5595 5596
/* makeConstantList()
 * Convert constant value node into string node.
 */
static List *
makeConstantList( A_Const *n)
{
5597 5598
	List *result = NIL;
	char *typval = NULL;
5599
	char *defval = NULL;
5600

5601
	if (nodeTag(n) != T_A_Const) {
B
Bruce Momjian 已提交
5602
		elog(ERROR,"Cannot handle non-constant parameter");
5603 5604 5605 5606

	} else if (n->val.type == T_Float) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%g", n->val.val.dval);
5607
		result = lcons( makeString(defval), NIL);
5608 5609 5610 5611

	} else if (n->val.type == T_Integer) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%ld", n->val.val.ival);
5612
		result = lcons( makeString(defval), NIL);
5613 5614 5615 5616 5617 5618

	} 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, "'");
5619 5620 5621 5622 5623 5624 5625 5626 5627 5628
		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);
		}
5629 5630

	} else {
B
Bruce Momjian 已提交
5631
		elog(ERROR,"Internal error in makeConstantList(): cannot encode node");
5632 5633
	};

5634
	return result;
5635
} /* makeConstantList() */
5636

5637

5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650
/* 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') {
5651
		cp = palloc(strlen(rawid)+3);
5652 5653 5654 5655 5656 5657 5658
		strcpy(cp,"\"");
		strcat(cp,rawid);
		strcat(cp,"\"");
	} else {
		cp = rawid;
	};

5659
	return cp;
5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680
}

/*
 * 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];
}