gram.y 99.3 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 1.92 1998/01/17 04:53:16 momjian Exp $
14 15
 *
 * HISTORY
16 17 18
 *	  AUTHOR			DATE			MAJOR EVENT
 *	  Andrew Yu			Sept, 1994		POSTQUEL to SQL conversion
 *	  Andrew Yu			Oct, 1994		lispy code conversion
19 20
 *
 * NOTES
21 22
 *	  CAPITALS are used to represent terminal symbols.
 *	  non-capitals are used to represent non-terminals.
23 24
 *	  SQL92-specific syntax is separated from plain SQL/Postgres syntax
 *	  to help isolate the non-extensible portions of the parser.
25
 *
26 27
 *	  if you use list, make sure the datum is a node so that the printing
 *	  routines work
28 29
 *
 * WARNING
30 31
 *	  sometimes we assign constants to makeStrings. Make sure we don't free
 *	  those.
32 33 34 35 36
 *
 *-------------------------------------------------------------------------
 */
#include <string.h>
#include <ctype.h>
B
Bruce Momjian 已提交
37

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

49
static char saved_relname[NAMEDATALEN];  /* need this for complex attributes */
50
static bool QueryIsRule = FALSE;
51
static Node *saved_In_Expr;
52 53
static Oid	*param_type_info;
static int	pfunc_num_args;
54 55
extern List *parsetree;

56

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

static char *xlateSqlType(char *);
65
static Node *makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr);
66
void mapTargetColumns(List *source, List *target);
67 68
static List *makeConstantList( A_Const *node);
static char *FlattenStringList(List *list);
69
static char *fmtId(char *rawid);
70
static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr);
71 72 73
static void param_type_init(Oid *typev, int nargs);

Oid	param_type(int t); /* used in parse_expr.c */
74 75 76 77 78 79 80 81

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


82 83 84 85 86 87 88
%union
{
	double				dval;
	int					ival;
	char				chr;
	char				*str;
	bool				boolean;
89
	bool*				pboolean;	/* for pg_user privileges */
90 91 92 93 94 95 96 97 98 99 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;
	IndexElem			*ielem;
	RangeVar			*range;
	RelExpr				*relexp;
	A_Indices			*aind;
	ResTarget			*target;
	ParamNo				*paramno;

	VersionStmt			*vstmt;
	DefineStmt			*dstmt;
	RuleStmt			*rstmt;
B
Bruce Momjian 已提交
110
	InsertStmt			*astmt;
111 112
}

113
%type <node>	stmt,
114
		AddAttrStmt, ClosePortalStmt,
115
		CopyStmt, CreateStmt, CreateAsStmt, CreateSeqStmt, DefineStmt, DestroyStmt,
116
		ExtendStmt, FetchStmt,	GrantStmt, CreateTrigStmt, DropTrigStmt,
117
		CreatePLangStmt, DropPLangStmt,
V
Vadim B. Mikheev 已提交
118
		IndexStmt, ListenStmt, OptimizableStmt,
B
Bruce Momjian 已提交
119 120
		ProcedureStmt, 	RecipeStmt, RemoveAggrStmt, RemoveOperStmt,
		RemoveFuncStmt, RemoveStmt,
121
		RenameStmt, RevokeStmt, RuleStmt, TransactionStmt, ViewStmt, LoadStmt,
B
Bruce Momjian 已提交
122
		CreatedbStmt, DestroydbStmt, VacuumStmt, CursorStmt, SubSelect,
B
Bruce Momjian 已提交
123
		UpdateStmt, InsertStmt, SelectStmt, NotifyStmt, DeleteStmt, ClusterStmt,
124 125
		ExplainStmt, VariableSetStmt, VariableShowStmt, VariableResetStmt,
		CreateUserStmt, AlterUserStmt, DropUserStmt
126

B
Bruce Momjian 已提交
127 128
%type <rtstmt> 

129 130
%type <str>		opt_database, location

131 132 133 134 135
%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

136
%type <str>		join_expr, join_outer, join_spec
137
%type <boolean> TriggerActionTime, TriggerForSpec, PLangTrusted
138

139
%type <str>		TriggerEvents, TriggerFuncArg
140

141 142
%type <str>		relation_name, copy_file_name, copy_delimiter, def_name,
		database_name, access_method_clause, access_method, attr_name,
143
		class, index_name, name, file_name, recipe_name, aggr_argtype
144

145
%type <str>		opt_id, opt_portal_name,
B
Bruce Momjian 已提交
146
		all_Op, MathOp, opt_name, opt_unique,
147
		result, OptUseOp, opt_class, SpecialRuleRelation
148

149 150
%type <str>		privileges, operation_commalist, grantee
%type <chr>		operation, TriggerOneEvent
151

152
%type <list>	stmtblock, stmtmulti,
153 154
		relation_name_list, OptTableElementList,
		OptInherit, definition,
155 156 157 158 159
		opt_with, def_args, def_name_list, func_argtypes,
		oper_argtypes, OptStmtList, OptStmtBlock, OptStmtMulti,
		opt_column_list, columnList, opt_va_list, va_list,
		sort_clause, sortby_list, index_params, index_list, name_list,
		from_clause, from_list, opt_array_bounds, nest_array_bounds,
160
		expr_list, attrs, res_target_list, res_target_list2,
161
		def_list, opt_indirection, group_clause, groupby_list, TriggerFuncArgs
162

163 164
%type <list>	union_clause, select_list
%type <list>	join_list
165 166 167
%type <sortgroupby>
				join_using
%type <boolean>	opt_union
168

169
%type <node>	position_expr
170 171
%type <list>	extract_list, position_list
%type <list>	substr_list, substr_from, substr_for, trim_list
172
%type <list>	opt_interval
173

174 175
%type <boolean> opt_inh_star, opt_binary, opt_instead, opt_with_copy,
				index_opt_unique, opt_verbose, opt_analyze
176

B
Bruce Momjian 已提交
177 178
%type <ival>	copy_dirn, def_type, opt_direction, remove_type,
				opt_column, event
179

B
Bruce Momjian 已提交
180
%type <ival>	fetch_how_many
181

V
Vadim B. Mikheev 已提交
182 183 184
%type <list>	OptSeqList
%type <defelt>	OptSeqElem

185 186 187
%type <dstmt>	def_rest
%type <astmt>	insert_rest

188 189
%type <node>	OptTableElement, ConstraintElem
%type <node>	columnDef, alter_clause
190
%type <defelt>	def_elem
191
%type <node>	def_arg, columnElem, where_clause,
192
				a_expr, a_expr_or_null, AexprConst,
193
				in_expr, in_expr_nodes, not_in_expr, not_in_expr_nodes,
194
				having_clause
195 196 197 198
%type <list>	row_descriptor, row_list
%type <node>	row_expr
%type <list>	OptCreateAs, CreateAsList
%type <node>	CreateAsElement
199 200
%type <value>	NumConst
%type <attr>	event_object, attr
201 202
%type <sortgroupby>		groupby
%type <sortgroupby>		sortby
203 204 205 206
%type <ielem>	index_elem, func_index
%type <range>	from_val
%type <relexp>	relation_expr
%type <target>	res_target_el, res_target_el2
207
%type <paramno> ParamNo
208

209 210 211 212 213 214
%type <typnam>	Typename, opt_type, Array, Generic, Character, Datetime, Numeric
%type <str>		generic, character, datetime
%type <str>		opt_charset, opt_collate
%type <str>		opt_float, opt_numeric, opt_decimal
%type <boolean>	opt_varying, opt_timezone

215
%type <ival>	Iconst
216
%type <str>		Sconst
B
Bruce Momjian 已提交
217
%type <str>		Id, var_value, zone_value
218 219
%type <str>		ColId, ColLabel

220
%type <node>	TableConstraint
221 222 223
%type <list>	constraint_list, constraint_expr
%type <list>	default_list, default_expr
%type <list>	ColQualList, ColQualifier
224
%type <node>	ColConstraint, ColConstraintElem
225 226
%type <list>	key_actions, key_action
%type <str>		key_match, key_reference
227 228 229

/*
 * If you make any token changes, remember to:
230 231
 *		- use "yacc -d" and update parse.h
 *		- update the keyword table in parser/keywords.c
232 233
 */

234 235 236 237 238 239 240 241 242 243
/* 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) */
244
%token	ACTION, ADD, ALL, ALTER, AND, AS, ASC,
245
		BEGIN_TRANS, BETWEEN, BOTH, BY,
246 247 248
		CASCADE, CAST, CHAR, CHARACTER, CHECK, CLOSE, COLLATE, COLUMN, COMMIT, 
		CONSTRAINT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME, 
		CURRENT_TIMESTAMP, CURRENT_USER, CURSOR,
249 250 251 252 253 254 255
		DAY_P, DECIMAL, DECLARE, DEFAULT, DELETE, DESC, DISTINCT, DOUBLE, DROP,
		END_TRANS, EXECUTE, EXISTS, EXTRACT,
		FETCH, FLOAT, FOR, FOREIGN, FROM, FULL,
		GRANT, GROUP, HAVING, HOUR_P,
		IN, INNER_P, INSERT, INTERVAL, INTO, IS,
		JOIN, KEY, LANGUAGE, LEADING, LEFT, LIKE, LOCAL,
		MATCH, MINUTE_P, MONTH_P,
256
		NATIONAL, NATURAL, NCHAR, NO, NOT, NOTIFY, NULL_P, NUMERIC,
257
		ON, OPTION, OR, ORDER, OUTER_P,
258
		PARTIAL, POSITION, PRECISION, PRIMARY, PRIVILEGES, PROCEDURE, PUBLIC,
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		REFERENCES, REVOKE, RIGHT, ROLLBACK,
		SECOND_P, SELECT, SET, SUBSTRING,
		TABLE, TIME, TIMESTAMP, TO, TRAILING, TRANSACTION, TRIM,
		UNION, UNIQUE, UPDATE, USING,
		VALUES, VARCHAR, VARYING, VERBOSE, VERSION, VIEW,
		WHERE, WITH, WORK, YEAR_P, ZONE

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

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

/* Keywords for Postgres support (not in SQL92 reserved words) */
%token	ABORT_TRANS, ACL, AFTER, AGGREGATE, ANALYZE,
B
Bruce Momjian 已提交
274
		APPEND, BACKWARD, BEFORE, BINARY, CHANGE, CLUSTER, COPY,
275
		DATABASE, DELIMITERS, DO, EXPLAIN, EXTEND,
B
Bruce Momjian 已提交
276
		FORWARD, FUNCTION, HANDLER,
277
		INDEX, INHERITS, INSTEAD, ISNULL,
B
Bruce Momjian 已提交
278
		LANCOMPILER, LISTEN, LOAD, LOCATION, MERGE, MOVE,
279
		NEW, NONE, NOTHING, NOTNULL, OIDS, OPERATOR, PROCEDURAL,
280
		RECIPE, RENAME, REPLACE, RESET, RETRIEVE, RETURNS, RULE,
B
Bruce Momjian 已提交
281
		SEQUENCE, SETOF, SHOW, STDIN, STDOUT, TRUSTED, 
282
		VACUUM, VERBOSE, VERSION
283

284 285 286
/* Keywords (obsolete; retain temporarily for parser - thomas 1997-12-04) */
%token	ARCHIVE

287 288 289 290 291 292
/*
 * Tokens for pg_passwd support.  The CREATEDB and CREATEUSER tokens should go away
 * when some sort of pg_privileges relation is introduced.
 *
 *                                    Todd A. Brandys
 */
293
%token	USER, PASSWORD, CREATEDB, NOCREATEDB, CREATEUSER, NOCREATEUSER, VALID, UNTIL
294

295
/* Special keywords, not in the query language - see the "lex" file */
296
%token <str>	IDENT, SCONST, Op
297 298 299
%token <ival>	ICONST, PARAM
%token <dval>	FCONST

300
/* these are not real. they are here so that they get generated as #define's*/
301
%token			OP
302 303

/* precedence */
304 305 306 307
%left		OR
%left		AND
%right		NOT
%right		'='
308
%nonassoc	'<' '>'
309 310 311
%nonassoc	LIKE
%nonassoc	BETWEEN
%nonassoc	IN
312
%nonassoc	Op				/* multi-character ops and user-defined operators */
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
%nonassoc	NOTNULL
%nonassoc	ISNULL
%nonassoc	IS
%left		'+' '-'
%left		'*' '/'
%left		'|'				/* this is the relation union op, not logical or */
/* Unary Operators */
%right		':'
%left		';'				/* end of statement or natural log */
%right		UMINUS
%left		'.'
%left		'[' ']'
%nonassoc	TYPECAST
%nonassoc	REDUCE
%left		UNION
328 329
%%

330
stmtblock:  stmtmulti
331
				{ parsetree = $1; }
332
		| stmt
333 334
				{ parsetree = lcons($1,NIL); }
		;
335

336
stmtmulti:  stmtmulti stmt ';'
337
				{ $$ = lappend($1, $2); }
338
		| stmtmulti stmt
339
				{ $$ = lappend($1, $2); }
340
		| stmt ';'
341 342
				{ $$ = lcons($1,NIL); }
		;
343 344

stmt :	  AddAttrStmt
345
		| AlterUserStmt
346 347 348
		| ClosePortalStmt
		| CopyStmt
		| CreateStmt
349
		| CreateAsStmt
350
		| CreateSeqStmt
351
		| CreatePLangStmt
352
		| CreateTrigStmt
353
		| CreateUserStmt
354 355 356
		| ClusterStmt
		| DefineStmt
		| DestroyStmt
357
		| DropPLangStmt
358
		| DropTrigStmt
359
		| DropUserStmt
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
		| ExtendStmt
		| ExplainStmt
		| FetchStmt
		| GrantStmt
		| IndexStmt
		| ListenStmt
		| ProcedureStmt
		| RecipeStmt
		| RemoveAggrStmt
		| RemoveOperStmt
		| RemoveFuncStmt
		| RemoveStmt
		| RenameStmt
		| RevokeStmt
		| OptimizableStmt
		| RuleStmt
		| TransactionStmt
		| ViewStmt
		| LoadStmt
		| CreatedbStmt
		| DestroydbStmt
		| VacuumStmt
		| VariableSetStmt
		| VariableShowStmt
		| VariableResetStmt
		;
386

387 388
/*****************************************************************************
 *
389
 * Create a new Postgres DBMS user
390 391 392 393
 *
 *
 *****************************************************************************/

394 395 396 397 398 399 400 401 402 403 404 405 406
CreateUserStmt:  CREATE USER Id user_passwd_clause user_createdb_clause
			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;
				}
		;
407 408 409 410 411 412 413 414

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

415 416 417 418 419 420 421 422 423 424 425 426 427
AlterUserStmt:  ALTER USER Id user_passwd_clause user_createdb_clause
			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;
				}
		;
428 429 430 431 432 433 434 435 436

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

DropUserStmt:  DROP USER Id
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
				{
					DropUserStmt *n = makeNode(DropUserStmt);
					n->user = $3;
					$$ = (Node *)n;
				}
		;

user_passwd_clause:  WITH PASSWORD Id			{ $$ = $3; }
			| /*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; }
		;

user_group_list:  user_group_list ',' Id
				{
					$$ = lcons((void*)makeString($3), $1);
				}
			| Id
				{
					$$ = 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; }
		;
495

496
/*****************************************************************************
497
 *
498
 * Set PG internal variable
499
 *	  SET name TO 'var_value'
500 501
 * Include SQL92 syntax (thomas 1997-10-22):
 *    SET TIME ZONE 'var_value'
502
 *
503 504
 *****************************************************************************/

505
VariableSetStmt:  SET ColId TO var_value
506 507 508 509 510 511
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
512
		| SET ColId '=' var_value
513 514 515 516 517 518
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = $2;
					n->value = $4;
					$$ = (Node *) n;
				}
519
		| SET TIME ZONE zone_value
520 521 522 523 524 525 526 527
				{
					VariableSetStmt *n = makeNode(VariableSetStmt);
					n->name  = "timezone";
					n->value = $4;
					$$ = (Node *) n;
				}
		;

528
var_value:  Sconst			{ $$ = $1; }
529
		| DEFAULT			{ $$ = NULL; }
530 531
		;

532
zone_value:  Sconst			{ $$ = $1; }
533
		| DEFAULT			{ $$ = NULL; }
534
		| LOCAL				{ $$ = "default"; }
535
		;
536

537
VariableShowStmt:  SHOW ColId
538 539 540 541 542
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
543 544 545 546 547 548
		| SHOW TIME ZONE
				{
					VariableShowStmt *n = makeNode(VariableShowStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
549 550
		;

551
VariableResetStmt:	RESET ColId
552 553 554 555 556
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = $2;
					$$ = (Node *) n;
				}
557 558 559 560 561 562
		| RESET TIME ZONE
				{
					VariableResetStmt *n = makeNode(VariableResetStmt);
					n->name  = "timezone";
					$$ = (Node *) n;
				}
563
		;
564

565

566 567
/*****************************************************************************
 *
568 569
 *		QUERY :
 *				addattr ( attr1 = type1 .. attrn = typen ) to <relname> [*]
570 571 572
 *
 *****************************************************************************/

573
AddAttrStmt:  ALTER TABLE relation_name opt_inh_star alter_clause
574 575 576 577
				{
					AddAttrStmt *n = makeNode(AddAttrStmt);
					n->relname = $3;
					n->inh = $4;
578
					n->colDef = $5;
579 580 581
					$$ = (Node *)n;
				}
		;
582

583 584 585 586
alter_clause:  ADD opt_column columnDef
				{
					$$ = $3;
				}
587
			| ADD '(' OptTableElementList ')'
588
				{
589
					Node *lp = lfirst($3);
590 591

					if (length($3) != 1)
B
Bruce Momjian 已提交
592
						elog(ERROR,"ALTER TABLE/ADD() allows one column only");
593 594
					$$ = lp;
				}
595
			| DROP opt_column ColId
B
Bruce Momjian 已提交
596
				{	elog(ERROR,"ALTER TABLE/DROP COLUMN not yet implemented"); }
597
			| ALTER opt_column ColId SET DEFAULT default_expr
B
Bruce Momjian 已提交
598
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/SET DEFAULT not yet implemented"); }
599
			| ALTER opt_column ColId DROP DEFAULT
B
Bruce Momjian 已提交
600
				{	elog(ERROR,"ALTER TABLE/ALTER COLUMN/DROP DEFAULT not yet implemented"); }
601
			| ADD ConstraintElem
B
Bruce Momjian 已提交
602
				{	elog(ERROR,"ALTER TABLE/ADD CONSTRAINT not yet implemented"); }
603 604
		;

605 606

/*****************************************************************************
607
 *
608 609
 *		QUERY :
 *				close <optname>
610
 *
611 612
 *****************************************************************************/

613
ClosePortalStmt:  CLOSE opt_id
614 615 616 617 618 619
				{
					ClosePortalStmt *n = makeNode(ClosePortalStmt);
					n->portalname = $2;
					$$ = (Node *)n;
				}
		;
620 621 622 623


/*****************************************************************************
 *
624 625 626
 *		QUERY :
 *				COPY [BINARY] <relname> FROM/TO
 *				[USING DELIMITERS <delimiter>]
627 628 629
 *
 *****************************************************************************/

630
CopyStmt:  COPY opt_binary relation_name opt_with_copy copy_dirn copy_file_name copy_delimiter
631 632 633 634 635 636 637 638 639 640 641 642 643 644
				{
					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; }
645
		| FROM
646 647
				{ $$ = FROM; }
		;
648

649
/*
650 651
 * 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
652
 * stdout. We silently correct the "typo".		 - AY 9/94
653
 */
654 655 656 657
copy_file_name:  Sconst							{ $$ = $1; }
		| STDIN									{ $$ = NULL; }
		| STDOUT								{ $$ = NULL; }
		;
658

659 660
opt_binary:  BINARY								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
661
		;
662

663
opt_with_copy:	WITH OIDS						{ $$ = TRUE; }
664
		| /*EMPTY*/								{ $$ = FALSE; }
665
		;
666

667 668 669
/*
 * the default copy delimiter is tab but the user can configure it
 */
670 671
copy_delimiter:  USING DELIMITERS Sconst		{ $$ = $3; }
		| /*EMPTY*/								{ $$ = "\t"; }
672
		;
673 674 675 676


/*****************************************************************************
 *
677 678
 *		QUERY :
 *				CREATE relname
679 680 681 682
 *
 *****************************************************************************/

CreateStmt:  CREATE TABLE relation_name '(' OptTableElementList ')'
683
				OptInherit OptArchiveType
684 685 686 687 688
				{
					CreateStmt *n = makeNode(CreateStmt);
					n->relname = $3;
					n->tableElts = $5;
					n->inhRelnames = $7;
689
					n->constraints = NIL;
690 691 692 693
					$$ = (Node *)n;
				}
		;

694 695 696 697
OptTableElementList:  OptTableElementList ',' OptTableElement
												{ $$ = lappend($1, $3); }
			| OptTableElement					{ $$ = lcons($1, NIL); }
			| /*EMPTY*/							{ $$ = NULL; }
698
		;
699

700 701
OptTableElement:  columnDef						{ $$ = $1; }
			| TableConstraint					{ $$ = $1; }
702
		;
703

704
columnDef:  ColId Typename ColQualifier
705 706 707 708 709 710 711 712 713 714 715
				{
					ColumnDef *n = makeNode(ColumnDef);
					n->colname = $1;
					n->typename = $2;
					n->defval = NULL;
					n->is_not_null = FALSE;
					n->constraints = $3;
					$$ = (Node *)n;
				}
		;

716
ColQualifier:  ColQualList						{ $$ = $1; }
717
			| /*EMPTY*/							{ $$ = NULL; }
718
		;
719

720 721 722 723
ColQualList:  ColQualList ColConstraint			{ $$ = lappend($1,$2); }
			| ColConstraint						{ $$ = lcons($1, NIL); }
		;

724 725 726 727 728 729 730 731 732
ColConstraint:
		CONSTRAINT name ColConstraintElem
				{
						Constraint *n = (Constraint *)$3;
						n->name = fmtId($2);
						$$ = $3;
				}
		| ColConstraintElem
				{ $$ = $1; }
733
		;
734

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
ColConstraintElem:  CHECK '(' constraint_expr ')'
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| DEFAULT default_expr
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_DEFAULT;
					n->name = NULL;
					n->def = FlattenStringList($2);
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| NOT NULL_P
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_NOTNULL;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| UNIQUE
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_UNIQUE;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| PRIMARY KEY
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = NULL;
					$$ = (Node *)n;
				}
			| REFERENCES ColId opt_column_list key_match key_actions
				{
B
Bruce Momjian 已提交
782
					elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented");
783 784
					$$ = NULL;
				}
785
		;
V
Vadim B. Mikheev 已提交
786

787 788 789 790 791 792 793 794 795 796 797
default_list:  default_list ',' default_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| default_expr
				{
					$$ = $1;
				}
		;

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
default_expr:  AexprConst
				{	$$ = makeConstantList((A_Const *) $1); }
			| NULL_P
				{	$$ = lcons( makeString("NULL"), NIL); }
			| '-' default_expr %prec UMINUS
				{	$$ = lcons( makeString( "-"), $2); }
			| default_expr '+' default_expr
				{	$$ = nconc( $1, lcons( makeString( "+"), $3)); }
			| default_expr '-' default_expr
				{	$$ = nconc( $1, lcons( makeString( "-"), $3)); }
			| default_expr '/' default_expr
				{	$$ = nconc( $1, lcons( makeString( "/"), $3)); }
			| default_expr '*' default_expr
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
			| default_expr '=' default_expr
B
Bruce Momjian 已提交
813
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
814
			| default_expr '<' default_expr
B
Bruce Momjian 已提交
815
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
816
			| default_expr '>' default_expr
B
Bruce Momjian 已提交
817
				{	elog(ERROR,"boolean expressions not supported in DEFAULT"); }
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
			| ':' 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));
				}
			| CAST default_expr AS Typename
				{
					$4->name = fmtId($4->name);
					$$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
				}
			| '(' default_expr ')'
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
836
			| name '(' ')'
837 838 839 840
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
841
			| name '(' default_list ')'
842 843
				{
					$$ = makeList( makeString($1), makeString("("), -1);
844
					$$ = nconc( $$, $3);
845 846 847 848 849
					$$ = lappend( $$, makeString(")"));
				}
			| default_expr Op default_expr
				{
					if (!strcmp("<=", $2) || !strcmp(">=", $2))
B
Bruce Momjian 已提交
850
						elog(ERROR,"boolean expressions not supported in DEFAULT");
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
					$$ = 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); }
878 879
		;

880 881 882 883 884
/* 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
885
				{
886 887
						Constraint *n = (Constraint *)$3;
						n->name = fmtId($2);
888 889
						$$ = $3;
				}
890 891
		| ConstraintElem
				{ $$ = $1; }
892 893
		;

894
ConstraintElem:  CHECK '(' constraint_expr ')'
895
				{
896 897 898 899 900
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_CHECK;
					n->name = NULL;
					n->def = FlattenStringList($3);
					$$ = (Node *)n;
901
				}
902
		| UNIQUE '(' columnList ')'
903 904 905 906 907 908 909 910
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_UNIQUE;
					n->name = NULL;
					n->def = NULL;
					n->keys = $3;
					$$ = (Node *)n;
				}
911
		| PRIMARY KEY '(' columnList ')'
912 913 914 915 916 917 918 919
				{
					Constraint *n = makeNode(Constraint);
					n->contype = CONSTR_PRIMARY;
					n->name = NULL;
					n->def = NULL;
					n->keys = $4;
					$$ = (Node *)n;
				}
920
		| FOREIGN KEY '(' columnList ')' REFERENCES ColId opt_column_list key_match key_actions
B
Bruce Momjian 已提交
921
				{	elog(NOTICE,"CREATE TABLE/FOREIGN KEY clause ignored; not yet implemented"); }
922
		;
923

924 925 926 927 928 929 930 931 932 933 934
constraint_list:  constraint_list ',' constraint_expr
				{
					$$ = lappend($1,makeString(","));
					$$ = nconc($$, $3);
				}
			| constraint_expr
				{
					$$ = $1;
				}
		;

935
constraint_expr:  AexprConst
936
				{	$$ = makeConstantList((A_Const *) $1); }
937
			| NULL_P
938
				{	$$ = lcons( makeString("NULL"), NIL); }
939
			| ColId
940
				{
941
					$$ = lcons( makeString(fmtId($1)), NIL);
942
				}
943
			| '-' constraint_expr %prec UMINUS
944
				{	$$ = lcons( makeString( "-"), $2); }
945
			| constraint_expr '+' constraint_expr
946
				{	$$ = nconc( $1, lcons( makeString( "+"), $3)); }
947
			| constraint_expr '-' constraint_expr
948
				{	$$ = nconc( $1, lcons( makeString( "-"), $3)); }
949
			| constraint_expr '/' constraint_expr
950
				{	$$ = nconc( $1, lcons( makeString( "/"), $3)); }
951
			| constraint_expr '*' constraint_expr
952
				{	$$ = nconc( $1, lcons( makeString( "*"), $3)); }
953
			| constraint_expr '=' constraint_expr
954
				{	$$ = nconc( $1, lcons( makeString( "="), $3)); }
955
			| constraint_expr '<' constraint_expr
956
				{	$$ = nconc( $1, lcons( makeString( "<"), $3)); }
957
			| constraint_expr '>' constraint_expr
958
				{	$$ = nconc( $1, lcons( makeString( ">"), $3)); }
959
			| ':' constraint_expr
960
				{	$$ = lcons( makeString( ":"), $2); }
961
			| ';' constraint_expr
962
				{	$$ = lcons( makeString( ";"), $2); }
963
			| '|' constraint_expr
964
				{	$$ = lcons( makeString( "|"), $2); }
965
			| constraint_expr TYPECAST Typename
966
				{
967 968
					$3->name = fmtId($3->name);
					$$ = nconc( lcons( makeString( "CAST"), $1), makeList( makeString("AS"), $3, -1));
969
				}
970
			| CAST constraint_expr AS Typename
971
				{
972 973
					$4->name = fmtId($4->name);
					$$ = nconc( lcons( makeString( "CAST"), $2), makeList( makeString("AS"), $4, -1));
974
				}
975
			| '(' constraint_expr ')'
976
				{	$$ = lappend( lcons( makeString( "("), $2), makeString( ")")); }
977 978 979 980 981 982
			| name '(' ')'
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = lappend( $$, makeString(")"));
				}
			| name '(' constraint_list ')'
983 984 985 986 987
				{
					$$ = makeList( makeString($1), makeString("("), -1);
					$$ = nconc( $$, $3);
					$$ = lappend( $$, makeString(")"));
				}
988
			| constraint_expr Op constraint_expr
989
				{	$$ = nconc( $1, lcons( makeString( $2), $3)); }
990
			| constraint_expr AND constraint_expr
991
				{	$$ = nconc( $1, lcons( makeString( "AND"), $3)); }
992
			| constraint_expr OR constraint_expr
993
				{	$$ = nconc( $1, lcons( makeString( "OR"), $3)); }
994 995
			| NOT constraint_expr
				{	$$ = lcons( makeString( "NOT"), $2); }
996
			| Op constraint_expr
997
				{	$$ = lcons( makeString( $1), $2); }
998
			| constraint_expr Op
999
				{	$$ = lappend( $1, makeString( $2)); }
1000 1001 1002 1003 1004 1005 1006 1007
			| 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")); }
1008
			| constraint_expr IS TRUE_P
1009
				{	$$ = lappend( $1, makeString( "IS TRUE")); }
1010
			| constraint_expr IS FALSE_P
1011
				{	$$ = lappend( $1, makeString( "IS FALSE")); }
1012
			| constraint_expr IS NOT TRUE_P
1013
				{	$$ = lappend( $1, makeString( "IS NOT TRUE")); }
1014
			| constraint_expr IS NOT FALSE_P
1015
				{	$$ = lappend( $1, makeString( "IS NOT FALSE")); }
1016 1017
		;

1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
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; }
		;

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
OptInherit:  INHERITS '(' relation_name_list ')'		{ $$ = $3; }
		| /*EMPTY*/										{ $$ = NIL; }
		;

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

1050 1051
CreateAsStmt:  CREATE TABLE relation_name OptCreateAs AS SubSelect
				{
B
Bruce Momjian 已提交
1052
					SelectStmt *n = (SelectStmt *)$6;
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
					if ($4 != NIL)
						mapTargetColumns($4, n->targetList);
					n->into = $3;
					$$ = (Node *)n;
				}
		;

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

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

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

1080

V
Vadim B. Mikheev 已提交
1081 1082
/*****************************************************************************
 *
1083 1084
 *		QUERY :
 *				CREATE SEQUENCE seqname
V
Vadim B. Mikheev 已提交
1085 1086 1087
 *
 *****************************************************************************/

1088 1089 1090 1091 1092 1093 1094 1095
CreateSeqStmt:	CREATE SEQUENCE relation_name OptSeqList
				{
					CreateSeqStmt *n = makeNode(CreateSeqStmt);
					n->seqname = $3;
					n->options = $4;
					$$ = (Node *)n;
				}
		;
V
Vadim B. Mikheev 已提交
1096

1097
OptSeqList:
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
				OptSeqList OptSeqElem
				{ $$ = lappend($1, $2); }
		|		{ $$ = NIL; }
		;

OptSeqElem:		IDENT NumConst
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)$2;
				}
		|		IDENT
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)NULL;
				}
		;
V
Vadim B. Mikheev 已提交
1116

1117 1118 1119 1120 1121 1122 1123 1124
/*****************************************************************************
 *
 *		QUERIES :
 *				CREATE PROCEDURAL LANGUAGE ...
 *				DROP PROCEDURAL LANGUAGE ...
 *
 *****************************************************************************/

1125
CreatePLangStmt:  CREATE PLangTrusted PROCEDURAL LANGUAGE Sconst 
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
			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; }

1140
DropPLangStmt:  DROP PROCEDURAL LANGUAGE Sconst
1141 1142 1143 1144 1145 1146
			{
				DropPLangStmt *n = makeNode(DropPLangStmt);
				n->plname = $4;
				$$ = (Node *)n;
			}
		;
V
Vadim B. Mikheev 已提交
1147

V
Vadim B. Mikheev 已提交
1148 1149
/*****************************************************************************
 *
1150 1151 1152
 *		QUERIES :
 *				CREATE TRIGGER ...
 *				DROP TRIGGER ...
V
Vadim B. Mikheev 已提交
1153 1154 1155
 *
 *****************************************************************************/

1156
CreateTrigStmt:  CREATE TRIGGER name TriggerActionTime TriggerEvents ON
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
				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;
				}
		;

1172 1173
TriggerActionTime:		BEFORE	{ $$ = TRUE; }
				|		AFTER	{ $$ = FALSE; }
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		;

TriggerEvents:	TriggerOneEvent
					{
							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;
					}
		;

TriggerOneEvent:		INSERT	{ $$ = 'i'; }
				|		DELETE	{ $$ = 'd'; }
				|		UPDATE	{ $$ = 'u'; }
		;

1199
TriggerForSpec:  FOR name name
1200 1201
				{
						if ( strcmp ($2, "each") != 0 )
1202
								elog(ERROR,"parser: syntax error near %s",$2);
1203
						if ( strcmp ($3, "row") == 0 )
1204
								$$ = TRUE;
1205
						else if ( strcmp ($3, "statement") == 0 )
1206
								$$ = FALSE;
1207
						else
1208
								elog(ERROR,"parser: syntax error near %s",$3);
1209 1210
				}
		;
V
Vadim B. Mikheev 已提交
1211

1212
TriggerFuncArgs:  TriggerFuncArg
1213
				{ $$ = lcons($1, NIL); }
1214
		| TriggerFuncArgs ',' TriggerFuncArg
1215
				{ $$ = lappend($1, $3); }
1216 1217
		| /*EMPTY*/
				{ $$ = NIL; }
1218 1219
		;

1220
TriggerFuncArg:  ICONST
1221
					{
1222 1223 1224
						char *s = (char *) palloc (256);
						sprintf (s, "%d", $1);
						$$ = s;
1225 1226 1227 1228 1229 1230 1231
					}
				| FCONST
					{
						char *s = (char *) palloc (256);
						sprintf (s, "%g", $1);
						$$ = s;
					}
1232
				| Sconst		{  $$ = $1; }
1233
				| IDENT			{  $$ = $1; }
1234
		;
V
Vadim B. Mikheev 已提交
1235 1236

DropTrigStmt:	DROP TRIGGER name ON relation_name
1237 1238 1239 1240 1241 1242 1243
				{
					DropTrigStmt *n = makeNode(DropTrigStmt);
					n->trigname = $3;
					n->relname = $5;
					$$ = (Node *) n;
				}
		;
V
Vadim B. Mikheev 已提交
1244

1245

1246 1247
/*****************************************************************************
 *
1248 1249
 *		QUERY :
 *				define (type,operator,aggregate)
1250 1251 1252 1253
 *
 *****************************************************************************/

DefineStmt:  CREATE def_type def_rest
1254 1255 1256 1257 1258
				{
					$3->defType = $2;
					$$ = (Node *)$3;
				}
		;
1259

1260
def_rest:  def_name definition
1261 1262 1263 1264 1265 1266 1267
				{
					$$ = makeNode(DefineStmt);
					$$->defname = $1;
					$$->definition = $2;
				}
		;

1268
def_type:  OPERATOR							{ $$ = OPERATOR; }
1269
		| TYPE_P							{ $$ = TYPE_P; }
1270
		| AGGREGATE							{ $$ = AGGREGATE; }
1271
		;
1272

1273
def_name:  PROCEDURE						{ $$ = "procedure"; }
1274 1275 1276 1277
		| JOIN								{ $$ = "join"; }
		| ColId								{ $$ = $1; }
		| MathOp							{ $$ = $1; }
		| Op								{ $$ = $1; }
1278
		;
1279

1280
definition:  '(' def_list ')'				{ $$ = $2; }
1281
		;
1282

1283
def_list:  def_elem							{ $$ = lcons($1, NIL); }
1284
		| def_list ',' def_elem				{ $$ = lappend($1, $3); }
1285
		;
1286 1287

def_elem:  def_name '=' def_arg
1288 1289 1290 1291 1292
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)$3;
				}
1293
		| def_name
1294 1295 1296 1297 1298
				{
					$$ = makeNode(DefElem);
					$$->defname = $1;
					$$->arg = (Node *)NULL;
				}
1299
		| DEFAULT '=' def_arg
1300 1301
				{
					$$ = makeNode(DefElem);
1302
					$$->defname = "default";
1303 1304 1305 1306
					$$->arg = (Node *)$3;
				}
		;

1307 1308 1309 1310 1311
def_arg:  ColId							{  $$ = (Node *)makeString($1); }
		| all_Op						{  $$ = (Node *)makeString($1); }
		| NumConst						{  $$ = (Node *)$1; /* already a Value */ }
		| Sconst						{  $$ = (Node *)makeString($1); }
		| SETOF ColId
1312 1313 1314 1315 1316 1317 1318
				{
					TypeName *n = makeNode(TypeName);
					n->name = $2;
					n->setof = TRUE;
					n->arrayBounds = NULL;
					$$ = (Node *)n;
				}
1319
		| DOUBLE						{  $$ = (Node *)makeString("double"); }
1320
		;
1321 1322 1323 1324


/*****************************************************************************
 *
1325 1326
 *		QUERY:
 *				destroy <relname1> [, <relname2> .. <relnameN> ]
1327 1328 1329
 *
 *****************************************************************************/

1330
DestroyStmt:  DROP TABLE relation_name_list
1331 1332 1333
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1334
					n->sequence = FALSE;
1335 1336
					$$ = (Node *)n;
				}
1337
		| DROP SEQUENCE relation_name_list
1338 1339 1340
				{
					DestroyStmt *n = makeNode(DestroyStmt);
					n->relNames = $3;
1341
					n->sequence = TRUE;
1342 1343 1344
					$$ = (Node *)n;
				}
		;
1345 1346 1347 1348


/*****************************************************************************
 *
1349
 *		QUERY:
V
Vadim B. Mikheev 已提交
1350
 *			fetch/move [forward | backward] [number | all ] [ in <portalname> ]
1351 1352 1353
 *
 *****************************************************************************/

1354 1355 1356 1357 1358 1359
FetchStmt:	FETCH opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
V
Vadim B. Mikheev 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368
					n->ismove = false;
					$$ = (Node *)n;
				}
		|	MOVE opt_direction fetch_how_many opt_portal_name
				{
					FetchStmt *n = makeNode(FetchStmt);
					n->direction = $2;
					n->howMany = $3;
					n->portalname = $4;
1369
					n->ismove = TRUE;
1370 1371 1372 1373 1374 1375 1376 1377
					$$ = (Node *)n;
				}
		;

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

1379
fetch_how_many:  Iconst
1380
			   { $$ = $1;
B
Bruce Momjian 已提交
1381
				 if ($1 <= 0) elog(ERROR,"Please specify nonnegative count for fetch"); }
1382
		| ALL							{ $$ = 0; /* 0 means fetch all tuples*/ }
1383
		| /*EMPTY*/						{ $$ = 1; /*default*/ }
1384
		;
1385

1386
opt_portal_name:  IN name				{ $$ = $2; }
V
Vadim B. Mikheev 已提交
1387 1388 1389
		| /*EMPTY*/						{ $$ = NULL; }
		;

1390

1391 1392
/*****************************************************************************
 *
1393 1394
 *		QUERY:
 *				GRANT [privileges] ON [relation_name_list] TO [GROUP] grantee
1395 1396 1397
 *
 *****************************************************************************/

1398
GrantStmt:  GRANT privileges ON relation_name_list TO grantee opt_with_grant
1399 1400 1401 1402 1403 1404
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'+');
					free($2);
					free($6);
				}
		;
1405 1406

privileges:  ALL PRIVILEGES
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| ALL
				{
				 $$ = aclmakepriv("rwaR",0);
				}
		| operation_commalist
				{
				 $$ = $1;
				}
		;

1420
operation_commalist:  operation
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
				{
						$$ = aclmakepriv("",$1);
				}
		| operation_commalist ',' operation
				{
						$$ = aclmakepriv($1,$3);
						free($1);
				}
		;

1431
operation:  SELECT
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
				{
						$$ = ACL_MODE_RD_CHR;
				}
		| INSERT
				{
						$$ = ACL_MODE_AP_CHR;
				}
		| UPDATE
				{
						$$ = ACL_MODE_WR_CHR;
				}
		| DELETE
				{
						$$ = ACL_MODE_WR_CHR;
				}
1447
		| RULE
1448 1449 1450 1451 1452 1453 1454 1455 1456
				{
						$$ = ACL_MODE_RU_CHR;
				}
		;

grantee:  PUBLIC
				{
						$$ = aclmakeuser("A","");
				}
1457
		| GROUP ColId
1458 1459 1460
				{
						$$ = aclmakeuser("G",$2);
				}
1461
		| ColId
1462 1463 1464 1465
				{
						$$ = aclmakeuser("U",$1);
				}
		;
1466

1467
opt_with_grant:  WITH GRANT OPTION
1468 1469
				{
					yyerror("WITH GRANT OPTION is not supported.  Only relation owners can set privileges");
1470 1471
				 }
		| /*EMPTY*/
1472 1473
		;

1474

1475 1476
/*****************************************************************************
 *
1477 1478
 *		QUERY:
 *				REVOKE [privileges] ON [relation_name] FROM [user]
1479 1480 1481
 *
 *****************************************************************************/

1482
RevokeStmt:  REVOKE privileges ON relation_name_list FROM grantee
1483 1484 1485 1486 1487 1488
				{
					$$ = (Node*)makeAclStmt($2,$4,$6,'-');
					free($2);
					free($6);
				}
		;
1489

1490

1491 1492
/*****************************************************************************
 *
1493
 *		QUERY:
B
Bruce Momjian 已提交
1494
 *				create index <indexname> on <relname>
1495 1496
 *				  using <access> "(" (<col> with <op>)+ ")" [with
 *				  <target_list>]
1497
 *
1498
 *	[where <qual>] is not supported anymore
1499 1500
 *****************************************************************************/

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
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;
				}
		;

1518 1519
index_opt_unique:  UNIQUE						{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
1520 1521
		;

1522 1523
access_method_clause:  USING access_method		{ $$ = $2; }
		| /*EMPTY*/								{ $$ = "btree"; }
1524
		;
M
Marc G. Fournier 已提交
1525

1526 1527 1528 1529 1530 1531 1532 1533
index_params:  index_list						{ $$ = $1; }
		| func_index							{ $$ = lcons($1,NIL); }
		;

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

1534
func_index:  name '(' name_list ')' opt_type opt_class
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = $3;
					$$->class = $6;
					$$->tname = $5;
				}
		  ;

index_elem:  attr_name opt_type opt_class
				{
					$$ = makeNode(IndexElem);
					$$->name = $1;
					$$->args = NIL;
					$$->class = $3;
					$$->tname = $2;
				}
		;

1554 1555 1556
opt_type:  ':' Typename							{ $$ = $2; }
		| FOR Typename							{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL; }
1557 1558 1559 1560 1561 1562
		;

/* 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
1563
 *		| WITH class							{ $$ = $2; }
1564 1565
 */
opt_class:  class								{ $$ = $1; }
1566
		| USING class							{ $$ = $2; }
1567 1568 1569 1570
		| /*EMPTY*/								{ $$ = NULL; }
		;


1571 1572
/*****************************************************************************
 *
1573 1574
 *		QUERY:
 *				extend index <indexname> [where <qual>]
1575 1576 1577 1578
 *
 *****************************************************************************/

ExtendStmt:  EXTEND INDEX index_name where_clause
1579 1580 1581 1582 1583 1584 1585
				{
					ExtendStmt *n = makeNode(ExtendStmt);
					n->idxname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
1586

1587

1588 1589
/*****************************************************************************
 *
1590 1591
 *		QUERY:
 *				execute recipe <recipeName>
1592 1593 1594
 *
 *****************************************************************************/

1595
RecipeStmt:  EXECUTE RECIPE recipe_name
1596 1597 1598
				{
					RecipeStmt *n;
					if (!IsTransactionBlock())
B
Bruce Momjian 已提交
1599
						elog(ERROR,"EXECUTE RECIPE may only be used in begin/end transaction blocks");
1600 1601 1602 1603 1604 1605

					n = makeNode(RecipeStmt);
					n->recipeName = $3;
					$$ = (Node *)n;
				}
		;
1606 1607 1608 1609


/*****************************************************************************
 *
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
 *		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>
1621 1622 1623
 *
 *****************************************************************************/

1624
ProcedureStmt:	CREATE FUNCTION def_name def_args
1625
			 RETURNS def_arg opt_with AS Sconst LANGUAGE Sconst
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
				{
					ProcedureStmt *n = makeNode(ProcedureStmt);
					n->funcname = $3;
					n->defArgs = $4;
					n->returnType = (Node *)$6;
					n->withClause = $7;
					n->as = $9;
					n->language = $11;
					$$ = (Node *)n;
				};

opt_with:  WITH definition						{ $$ = $2; }
1638
		| /*EMPTY*/							{ $$ = NIL; }
1639 1640 1641
		;

def_args:  '(' def_name_list ')'				{ $$ = $2; }
1642
		| '(' ')'								{ $$ = NIL; }
1643
		;
1644

1645
def_name_list:	name_list;
1646 1647 1648

/*****************************************************************************
 *
1649
 *		QUERY:
1650
 *
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
 *		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")
1661 1662 1663 1664
 *
 *****************************************************************************/

RemoveStmt:  DROP remove_type name
1665 1666 1667 1668 1669 1670 1671 1672
				{
					RemoveStmt *n = makeNode(RemoveStmt);
					n->removeType = $2;
					n->name = $3;
					$$ = (Node *)n;
				}
		;

1673
remove_type:  TYPE_P							{  $$ = TYPE_P; }
1674 1675 1676
		| INDEX									{  $$ = INDEX; }
		| RULE									{  $$ = RULE; }
		| VIEW									{  $$ = VIEW; }
1677
		;
1678

1679
RemoveAggrStmt:  DROP AGGREGATE name aggr_argtype
1680 1681 1682 1683 1684 1685 1686 1687 1688
				{
						RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
						n->aggname = $3;
						n->aggtype = $4;
						$$ = (Node *)n;
				}
		;

aggr_argtype:  name								{ $$ = $1; }
1689
		| '*'									{ $$ = NULL; }
1690
		;
1691

1692
RemoveFuncStmt:  DROP FUNCTION name '(' func_argtypes ')'
1693 1694 1695 1696 1697 1698 1699 1700 1701
				{
					RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
					n->funcname = $3;
					n->args = $5;
					$$ = (Node *)n;
				}
		;

func_argtypes:	name_list						{ $$ = $1; }
1702
		| /*EMPTY*/								{ $$ = NIL; }
1703
		;
1704 1705

RemoveOperStmt:  DROP OPERATOR all_Op '(' oper_argtypes ')'
1706 1707 1708 1709 1710 1711 1712
				{
					RemoveOperStmt *n = makeNode(RemoveOperStmt);
					n->opname = $3;
					n->args = $5;
					$$ = (Node *)n;
				}
		;
1713

1714
all_Op:  Op | MathOp;
1715

1716
MathOp:	'+'				{ $$ = "+"; }
1717 1718 1719 1720 1721 1722
		| '-'			{ $$ = "-"; }
		| '*'			{ $$ = "*"; }
		| '/'			{ $$ = "/"; }
		| '<'			{ $$ = "<"; }
		| '>'			{ $$ = ">"; }
		| '='			{ $$ = "="; }
1723 1724 1725 1726
		;

oper_argtypes:	name
				{
B
Bruce Momjian 已提交
1727
				   elog(ERROR,"parser: argument type missing (use NONE for unary operators)");
1728 1729 1730 1731 1732 1733 1734 1735
				}
		| 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); }
		;
1736

1737

1738 1739
/*****************************************************************************
 *
1740 1741 1742
 *		QUERY:
 *				rename <attrname1> in <relname> [*] to <attrname2>
 *				rename <relname1> to <relname2>
1743
 *
1744 1745
 *****************************************************************************/

1746
RenameStmt:  ALTER TABLE relation_name opt_inh_star
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
				  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; }
1759
		| /*EMPTY*/						{ $$ = NULL; }
1760 1761 1762 1763 1764
		;

opt_column:  COLUMN						{ $$ = COLUMN; }
		| /*EMPTY*/						{ $$ = 0; }
		;
1765 1766 1767


/*****************************************************************************
1768
 *
1769 1770
 *		QUERY:	Define Rewrite Rule , Define Tuple Rule
 *				Define Rule <old rules >
1771
 *
1772
 *		only rewrite rule is supported -- ay 9/94
1773
 *
1774 1775
 *****************************************************************************/

1776
RuleStmt:  CREATE RULE name AS
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
		   { QueryIsRule=TRUE; }
		   ON event TO event_object where_clause
		   DO opt_instead OptStmtList
				{
					RuleStmt *n = makeNode(RuleStmt);
					n->rulename = $3;
					n->event = $7;
					n->object = $9;
					n->whereClause = $10;
					n->instead = $12;
					n->actions = $13;
					$$ = (Node *)n;
				}
		;

OptStmtList:  NOTHING					{ $$ = NIL; }
		| OptimizableStmt				{ $$ = lcons($1, NIL); }
		| '[' OptStmtBlock ']'			{ $$ = $2; }
		;
1796

1797
OptStmtBlock:  OptStmtMulti
1798
				{  $$ = $1; }
1799
		| OptimizableStmt
1800 1801
				{ $$ = lcons($1, NIL); }
		;
1802

1803
OptStmtMulti:  OptStmtMulti OptimizableStmt ';'
1804
				{  $$ = lappend($1, $2); }
1805
		| OptStmtMulti OptimizableStmt
1806
				{  $$ = lappend($1, $2); }
1807
		| OptimizableStmt ';'
1808 1809
				{ $$ = lcons($1, NIL); }
		;
1810

1811
event_object:  relation_name '.' attr_name
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
				{
					$$ = 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;
				}
		;
1828 1829

/* change me to select, update, etc. some day */
1830 1831 1832 1833 1834
event:	SELECT							{ $$ = CMD_SELECT; }
		| UPDATE						{ $$ = CMD_UPDATE; }
		| DELETE						{ $$ = CMD_DELETE; }
		| INSERT						{ $$ = CMD_INSERT; }
		 ;
1835

1836
opt_instead:  INSTEAD					{ $$ = TRUE; }
1837
		| /*EMPTY*/					{ $$ = FALSE; }
1838
		;
1839 1840 1841 1842


/*****************************************************************************
 *
1843 1844 1845
 *		QUERY:
 *				NOTIFY <relation_name>	can appear both in rule bodies and
 *				as a query-level command
1846 1847 1848
 *
 *****************************************************************************/

1849
NotifyStmt:  NOTIFY relation_name
1850 1851 1852 1853 1854 1855
				{
					NotifyStmt *n = makeNode(NotifyStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
		;
1856

1857
ListenStmt:  LISTEN relation_name
1858 1859 1860 1861 1862
				{
					ListenStmt *n = makeNode(ListenStmt);
					n->relname = $2;
					$$ = (Node *)n;
				}
1863 1864 1865 1866 1867
;


/*****************************************************************************
 *
1868
 *		Transactions:
1869
 *
1870 1871 1872 1873 1874 1875
 *		abort transaction
 *				(ABORT)
 *		begin transaction
 *				(BEGIN)
 *		end transaction
 *				(END)
1876
 *
1877 1878 1879
 *****************************************************************************/

TransactionStmt:  ABORT_TRANS TRANSACTION
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
		| BEGIN_TRANS TRANSACTION
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = BEGIN_TRANS;
					$$ = (Node *)n;
				}
		| BEGIN_TRANS WORK
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = BEGIN_TRANS;
					$$ = (Node *)n;
				}
		| COMMIT WORK
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
		| END_TRANS TRANSACTION
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
		| ROLLBACK WORK
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}

		| ABORT_TRANS
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
		| BEGIN_TRANS
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = BEGIN_TRANS;
					$$ = (Node *)n;
				}
		| COMMIT
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}

		| END_TRANS
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = END_TRANS;
					$$ = (Node *)n;
				}
		| ROLLBACK
				{
					TransactionStmt *n = makeNode(TransactionStmt);
					n->command = ABORT_TRANS;
					$$ = (Node *)n;
				}
		;
1948 1949 1950 1951


/*****************************************************************************
 *
1952 1953
 *		QUERY:
 *				define view <viewname> '('target-list ')' [where <quals> ]
1954 1955 1956
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
1957
ViewStmt:  CREATE VIEW name AS SelectStmt
1958 1959 1960 1961
				{
					ViewStmt *n = makeNode(ViewStmt);
					n->viewname = $3;
					n->query = (Query *)$5;
B
Bruce Momjian 已提交
1962 1963
					if (((SelectStmt *)n->query)->sortClause != NULL)
						elog(ERROR,"Order by and Distinct on views is not implemented.");
B
Bruce Momjian 已提交
1964
					if (((SelectStmt *)n->query)->unionClause != NULL)
B
Bruce Momjian 已提交
1965
						elog(ERROR,"Views on unions not implemented.");
1966 1967 1968
					$$ = (Node *)n;
				}
		;
1969 1970 1971 1972


/*****************************************************************************
 *
1973 1974
 *		QUERY:
 *				load "filename"
1975 1976 1977
 *
 *****************************************************************************/

1978
LoadStmt:  LOAD file_name
1979 1980 1981 1982 1983 1984
				{
					LoadStmt *n = makeNode(LoadStmt);
					n->filename = $2;
					$$ = (Node *)n;
				}
		;
1985 1986 1987 1988


/*****************************************************************************
 *
1989 1990
 *		QUERY:
 *				createdb dbname
1991 1992 1993
 *
 *****************************************************************************/

1994
CreatedbStmt:  CREATE DATABASE database_name opt_database
1995 1996 1997
				{
					CreatedbStmt *n = makeNode(CreatedbStmt);
					n->dbname = $3;
1998
					n->dbpath = $4;
1999 2000 2001
					$$ = (Node *)n;
				}
		;
2002

2003 2004 2005 2006 2007 2008 2009 2010
opt_database:  WITH LOCATION '=' location		{ $$ = $4; }
		| /*EMPTY*/								{ $$ = NULL; }
		;

location:  Sconst								{ $$ = $1; }
		| DEFAULT								{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
		;
2011 2012 2013

/*****************************************************************************
 *
2014 2015
 *		QUERY:
 *				destroydb dbname
2016 2017 2018
 *
 *****************************************************************************/

2019 2020 2021 2022 2023 2024 2025
DestroydbStmt:	DROP DATABASE database_name
				{
					DestroydbStmt *n = makeNode(DestroydbStmt);
					n->dbname = $3;
					$$ = (Node *)n;
				}
		;
2026 2027 2028 2029


/*****************************************************************************
 *
2030 2031
 *		QUERY:
 *				cluster <index_name> on <relation_name>
2032 2033 2034
 *
 *****************************************************************************/

2035
ClusterStmt:  CLUSTER index_name ON relation_name
2036 2037 2038 2039 2040 2041 2042
				{
				   ClusterStmt *n = makeNode(ClusterStmt);
				   n->relname = $4;
				   n->indexname = $2;
				   $$ = (Node*)n;
				}
		;
2043

2044

2045 2046
/*****************************************************************************
 *
2047 2048
 *		QUERY:
 *				vacuum
2049 2050 2051
 *
 *****************************************************************************/

V
Vadim B. Mikheev 已提交
2052
VacuumStmt:  VACUUM opt_verbose opt_analyze
2053 2054 2055 2056 2057 2058 2059 2060
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
					n->analyze = $3;
					n->vacrel = NULL;
					n->va_spec = NIL;
					$$ = (Node *)n;
				}
B
Bruce Momjian 已提交
2061
		| VACUUM opt_verbose opt_analyze relation_name opt_va_list
2062 2063 2064
				{
					VacuumStmt *n = makeNode(VacuumStmt);
					n->verbose = $2;
B
Bruce Momjian 已提交
2065 2066
					n->analyze = $3;
					n->vacrel = $4;
2067 2068
					n->va_spec = $5;
					if ( $5 != NIL && !$4 )
B
Bruce Momjian 已提交
2069
						elog(ERROR,"parser: syntax error at or near \"(\"");
2070 2071 2072 2073
					$$ = (Node *)n;
				}
		;

2074
opt_verbose:  VERBOSE							{ $$ = TRUE; }
2075
		| /*EMPTY*/								{ $$ = FALSE; }
2076 2077
		;

2078
opt_analyze:  ANALYZE							{ $$ = TRUE; }
2079
		| /*EMPTY*/								{ $$ = FALSE; }
2080
		;
V
Vadim B. Mikheev 已提交
2081

2082
opt_va_list:  '(' va_list ')'
2083 2084 2085 2086
				{ $$ = $2; }
		| /* EMPTY */
				{ $$ = NIL; }
		;
V
Vadim B. Mikheev 已提交
2087

2088
va_list:  name
2089 2090 2091 2092
				{ $$=lcons($1,NIL); }
		| va_list ',' name
				{ $$=lappend($1,$3); }
		;
2093

2094

2095 2096
/*****************************************************************************
 *
2097 2098
 *		QUERY:
 *				EXPLAIN query
2099 2100 2101
 *
 *****************************************************************************/

2102
ExplainStmt:  EXPLAIN opt_verbose OptimizableStmt
2103 2104 2105 2106 2107 2108 2109
				{
					ExplainStmt *n = makeNode(ExplainStmt);
					n->verbose = $2;
					n->query = (Query*)$3;
					$$ = (Node *)n;
				}
		;
2110

2111

2112
/*****************************************************************************
2113 2114 2115 2116 2117 2118 2119 2120
 *																			 *
 *		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							 *
 *																			 *
2121 2122
 *****************************************************************************/

B
Bruce Momjian 已提交
2123
OptimizableStmt:  SelectStmt
2124
		| CursorStmt
B
Bruce Momjian 已提交
2125 2126
		| UpdateStmt
		| InsertStmt
2127 2128 2129
		| NotifyStmt
		| DeleteStmt					/* by default all are $$=$1 */
		;
2130 2131 2132 2133


/*****************************************************************************
 *
2134 2135
 *		QUERY:
 *				INSERT STATEMENTS
2136
 *
2137 2138
 *****************************************************************************/

B
Bruce Momjian 已提交
2139
InsertStmt:  INSERT INTO relation_name opt_column_list insert_rest
2140 2141 2142 2143 2144 2145
				{
					$5->relname = $3;
					$5->cols = $4;
					$$ = (Node *)$5;
				}
		;
2146

2147
insert_rest:  VALUES '(' res_target_list2 ')'
2148
				{
B
Bruce Momjian 已提交
2149
					$$ = makeNode(InsertStmt);
2150
					$$->unique = NULL;
2151 2152
					$$->targetList = $3;
					$$->fromClause = NIL;
2153
					$$->whereClause = NULL;
2154
					$$->groupClause = NIL;
2155
					$$->havingClause = NULL;
2156
					$$->unionClause = NIL;
2157
				}
2158 2159 2160 2161
		| SELECT opt_unique res_target_list2
			 from_clause where_clause
			 group_clause having_clause
			 union_clause
2162
				{
B
Bruce Momjian 已提交
2163
					$$ = makeNode(InsertStmt);
2164 2165 2166 2167 2168 2169 2170
					$$->unique = $2;
					$$->targetList = $3;
					$$->fromClause = $4;
					$$->whereClause = $5;
					$$->groupClause = $6;
					$$->havingClause = $7;
					$$->unionClause = $8;
2171 2172 2173
				}
		;

2174
opt_column_list:  '(' columnList ')'			{ $$ = $2; }
2175 2176
		| /*EMPTY*/								{ $$ = NIL; }
		;
2177

2178
columnList:
2179 2180 2181 2182 2183
		  columnList ',' columnElem
				{ $$ = lappend($1, $3); }
		| columnElem
				{ $$ = lcons($1, NIL); }
		;
2184

2185
columnElem:  ColId opt_indirection
2186 2187 2188 2189 2190 2191 2192
				{
					Ident *id = makeNode(Ident);
					id->name = $1;
					id->indirection = $2;
					$$ = (Node *)id;
				}
		;
2193

2194

2195 2196
/*****************************************************************************
 *
2197 2198
 *		QUERY:
 *				DELETE STATEMENTS
2199 2200
 *
 *****************************************************************************/
2201

2202
DeleteStmt:  DELETE FROM relation_name
2203 2204 2205 2206 2207 2208 2209 2210
			 where_clause
				{
					DeleteStmt *n = makeNode(DeleteStmt);
					n->relname = $3;
					n->whereClause = $4;
					$$ = (Node *)n;
				}
		;
2211 2212 2213 2214


/*****************************************************************************
 *
2215
 *		QUERY:
B
Bruce Momjian 已提交
2216
 *				UpdateStmt (UPDATE)
2217 2218 2219
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2220
UpdateStmt:  UPDATE relation_name
2221 2222 2223 2224
			  SET res_target_list
			  from_clause
			  where_clause
				{
B
Bruce Momjian 已提交
2225
					UpdateStmt *n = makeNode(UpdateStmt);
2226 2227 2228 2229 2230 2231 2232
					n->relname = $2;
					n->targetList = $4;
					n->fromClause = $5;
					n->whereClause = $6;
					$$ = (Node *)n;
				}
		;
2233

2234 2235 2236

/*****************************************************************************
 *
2237 2238
 *		QUERY:
 *				CURSOR STATEMENTS
2239 2240
 *
 *****************************************************************************/
2241
CursorStmt:  DECLARE name opt_binary CURSOR FOR
2242 2243 2244 2245
 			 SELECT opt_unique res_target_list2
			 from_clause where_clause
			 group_clause having_clause
			 union_clause sort_clause
2246
				{
2247
					SelectStmt *n = makeNode(SelectStmt);
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257

					/* from PORTAL name */
					/*
					 *	15 august 1991 -- since 3.0 postgres does locking
					 *	right, we discovered that portals were violating
					 *	locking protocol.  portal locks cannot span xacts.
					 *	as a short-term fix, we installed the check here.
					 *							-- mao
					 */
					if (!IsTransactionBlock())
B
Bruce Momjian 已提交
2258
						elog(ERROR,"Named portals may only be used in begin/end transaction blocks");
2259 2260 2261 2262 2263 2264 2265 2266

					n->portalname = $2;
					n->binary = $3;
					n->unique = $7;
					n->targetList = $8;
					n->fromClause = $9;
					n->whereClause = $10;
					n->groupClause = $11;
2267 2268 2269
					n->havingClause = $12;
					n->unionClause = $13;
					n->sortClause = $14;
2270 2271 2272
					$$ = (Node *)n;
				}
		;
2273 2274 2275 2276


/*****************************************************************************
 *
2277 2278
 *		QUERY:
 *				SELECT STATEMENTS
2279 2280 2281
 *
 *****************************************************************************/

B
Bruce Momjian 已提交
2282
SelectStmt:  SELECT opt_unique res_target_list2
2283 2284 2285
			 result from_clause where_clause
			 group_clause having_clause
			 union_clause sort_clause
2286
				{
B
Bruce Momjian 已提交
2287
					SelectStmt *n = makeNode(SelectStmt);
2288 2289 2290 2291 2292 2293 2294
					n->unique = $2;
					n->targetList = $3;
					n->into = $4;
					n->fromClause = $5;
					n->whereClause = $6;
					n->groupClause = $7;
					n->havingClause = $8;
2295
					n->unionClause = $9;
2296 2297 2298 2299 2300
					n->sortClause = $10;
					$$ = (Node *)n;
				}
		;

2301 2302
union_clause:  UNION opt_union select_list
				{
B
Bruce Momjian 已提交
2303
					SelectStmt *n = (SelectStmt *)lfirst($3);
2304 2305 2306 2307 2308
					n->unionall = $2;
					$$ = $3;
				}
		| /*EMPTY*/
				{ $$ = NIL; }
2309
		;
2310

2311
select_list:  select_list UNION opt_union SubSelect
2312
				{
B
Bruce Momjian 已提交
2313
					SelectStmt *n = (SelectStmt *)$4;
2314 2315 2316
					n->unionall = $3;
					$$ = lappend($1, $4);
				}
2317 2318 2319 2320 2321
		| SubSelect
				{ $$ = lcons($1, NIL); }
		;

SubSelect:	SELECT opt_unique res_target_list2
2322
			 from_clause where_clause
2323
			 group_clause having_clause
2324
				{
B
Bruce Momjian 已提交
2325
					SelectStmt *n = makeNode(SelectStmt);
2326
					n->unique = $2;
2327
					n->unionall = FALSE;
2328
					n->targetList = $3;
2329 2330 2331 2332
					n->fromClause = $4;
					n->whereClause = $5;
					n->groupClause = $6;
					n->havingClause = $7;
2333 2334 2335
					$$ = (Node *)n;
				}
		;
2336

2337
result:  INTO TABLE relation_name
2338
				{	$$= $3; }
2339
		| /*EMPTY*/
2340
				{	$$ = NULL; }
2341
		;
2342

2343 2344 2345 2346
opt_union:  ALL									{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
		;

2347 2348
opt_unique:  DISTINCT							{ $$ = "*"; }
		| DISTINCT ON ColId						{ $$ = $3; }
2349 2350
		| ALL									{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL; }
2351
		;
2352

2353
sort_clause:  ORDER BY sortby_list				{ $$ = $3; }
2354
		| /*EMPTY*/								{ $$ = NIL; }
2355
		;
2356

2357 2358
sortby_list:  sortby							{ $$ = lcons($1, NIL); }
		| sortby_list ',' sortby				{ $$ = lappend($1, $3); }
2359
		;
2360

2361
sortby:  ColId OptUseOp
2362 2363 2364 2365 2366 2367 2368
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = NULL;
					$$->name = $1;
					$$->useOp = $2;
				}
2369
		| ColId '.' ColId OptUseOp
2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = $1;
					$$->name = $3;
					$$->useOp = $4;
				}
		| Iconst OptUseOp
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = $1;
					$$->range = NULL;
					$$->name = NULL;
					$$->useOp = $2;
				}
		;

OptUseOp:  USING Op								{ $$ = $2; }
2388 2389 2390 2391 2392
		| USING '<'								{ $$ = "<"; }
		| USING '>'								{ $$ = ">"; }
		| ASC									{ $$ = "<"; }
		| DESC									{ $$ = ">"; }
		| /*EMPTY*/								{ $$ = "<"; /*default*/ }
2393
		;
2394 2395

/*
2396 2397
 *	jimmy bell-style recursive queries aren't supported in the
 *	current system.
2398
 *
2399 2400
 *	...however, recursive addattr and rename supported.  make special
 *	cases for these.
2401
 */
2402 2403
opt_inh_star:  '*'								{ $$ = TRUE; }
		| /*EMPTY*/								{ $$ = FALSE; }
2404
		;
2405

2406
relation_name_list:  name_list;
2407

2408
name_list:  name
2409
				{	$$ = lcons(makeString($1),NIL); }
2410
		| name_list ',' name
2411
				{	$$ = lappend($1,makeString($3)); }
2412
		;
2413

2414
group_clause:  GROUP BY groupby_list			{ $$ = $3; }
2415 2416
		| /*EMPTY*/								{ $$ = NIL; }
		;
2417

2418
groupby_list:  groupby							{ $$ = lcons($1, NIL); }
2419 2420
		| groupby_list ',' groupby				{ $$ = lappend($1, $3); }
		;
2421

2422
groupby:  ColId
2423 2424 2425 2426 2427 2428 2429
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = NULL;
					$$->name = $1;
					$$->useOp = NULL;
				}
2430
		| ColId '.' ColId
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = $1;
					$$->name = $3;
					$$->useOp = NULL;
				}
		| Iconst
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = $1;
					$$->range = NULL;
					$$->name = NULL;
					$$->useOp = NULL;
				}
		;

2448
having_clause:  HAVING a_expr					{ $$ = $2; }
2449 2450
		| /*EMPTY*/								{ $$ = NULL; }
		;
2451

2452

2453
/*****************************************************************************
2454
 *
2455 2456 2457
 *	clauses common to all Optimizable Stmts:
 *		from_clause		-
 *		where_clause	-
2458
 *
2459 2460
 *****************************************************************************/

2461
from_clause:  FROM '(' relation_expr join_expr JOIN relation_expr join_spec ')'
2462 2463
				{
					$$ = NIL;
B
Bruce Momjian 已提交
2464
					elog(ERROR,"JOIN not yet implemented");
2465 2466 2467 2468 2469 2470 2471
				}
		| FROM from_list						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NIL; }
		;

from_list:	from_list ',' from_val
				{ $$ = lappend($1, $3); }
2472
		| from_val CROSS JOIN from_val
B
Bruce Momjian 已提交
2473
				{ elog(ERROR,"CROSS JOIN not yet implemented"); }
2474
		| from_val
2475 2476
				{ $$ = lcons($1, NIL); }
		;
2477

2478
from_val:  relation_expr AS ColLabel
2479 2480 2481 2482 2483
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $3;
				}
2484
		| relation_expr ColId
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = $2;
				}
		| relation_expr
				{
					$$ = makeNode(RangeVar);
					$$->relExpr = $1;
					$$->name = NULL;
				}
		;
2497

2498
join_expr:  NATURAL join_expr					{ $$ = NULL; }
2499
		| FULL join_outer
B
Bruce Momjian 已提交
2500
				{ elog(ERROR,"FULL OUTER JOIN not yet implemented"); }
2501
		| LEFT join_outer
B
Bruce Momjian 已提交
2502
				{ elog(ERROR,"LEFT OUTER JOIN not yet implemented"); }
2503
		| RIGHT join_outer
B
Bruce Momjian 已提交
2504
				{ elog(ERROR,"RIGHT OUTER JOIN not yet implemented"); }
2505
		| OUTER_P
B
Bruce Momjian 已提交
2506
				{ elog(ERROR,"OUTER JOIN not yet implemented"); }
2507
		| INNER_P
B
Bruce Momjian 已提交
2508
				{ elog(ERROR,"INNER JOIN not yet implemented"); }
2509
		| UNION
B
Bruce Momjian 已提交
2510
				{ elog(ERROR,"UNION JOIN not yet implemented"); }
2511
		| /*EMPTY*/
B
Bruce Momjian 已提交
2512
				{ elog(ERROR,"INNER JOIN not yet implemented"); }
2513 2514
		;

2515 2516
join_outer:  OUTER_P							{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2517 2518
		;

2519 2520 2521
join_spec:	ON '(' a_expr ')'					{ $$ = NULL; }
		| USING '(' join_list ')'				{ $$ = NULL; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2522 2523
		;

2524
join_list:  join_using							{ $$ = lcons($1, NIL); }
2525 2526
		| join_list ',' join_using				{ $$ = lappend($1, $3); }
		;
2527

2528
join_using:  ColId
2529 2530 2531 2532 2533 2534 2535
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = NULL;
					$$->name = $1;
					$$->useOp = NULL;
				}
2536
		| ColId '.' ColId
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = 0;
					$$->range = $1;
					$$->name = $3;
					$$->useOp = NULL;
				}
		| Iconst
				{
					$$ = makeNode(SortGroupBy);
					$$->resno = $1;
					$$->range = NULL;
					$$->name = NULL;
					$$->useOp = NULL;
				}
		;

2554 2555
where_clause:  WHERE a_expr						{ $$ = $2; }
		| /*EMPTY*/								{ $$ = NULL;  /* no qualifiers */ }
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
		;

relation_expr:	relation_name
				{
					/* normal relations */
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = FALSE;
				}
		| relation_name '*'				  %prec '='
				{
2567
					/* inheritance query */
2568 2569 2570 2571
					$$ = makeNode(RelExpr);
					$$->relname = $1;
					$$->inh = TRUE;
				}
2572 2573

opt_array_bounds:  '[' ']' nest_array_bounds
2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
				{  $$ = lcons(makeInteger(-1), $3); }
		| '[' Iconst ']' nest_array_bounds
				{  $$ = lcons(makeInteger($2), $4); }
		| /* EMPTY */
				{  $$ = NIL; }
		;

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

2589

2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
/*****************************************************************************
 *
 *	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;
2604 2605 2606 2607

					/* Is this the name of a complex type? If so, implement
					 * it as a set.
					 */
2608
					if (!strcmp(saved_relname, $$->name))
2609 2610 2611 2612 2613
						/* This attr is the same type as the relation
						 * being defined. The classic example: create
						 * emp(name=text,mgr=emp)
						 */
						$$->setof = TRUE;
2614
					else if (typeTypeRelid(typenameType($$->name)) != InvalidOid)
2615 2616 2617 2618 2619 2620 2621
						 /* (Eventually add in here that the set can only
						  * contain one element.)
						  */
						$$->setof = TRUE;
					else
						$$->setof = FALSE;
				}
2622 2623
		| Character
		| SETOF Array
2624
				{
2625
					$$ = $2;
2626 2627 2628 2629
					$$->setof = TRUE;
				}
		;

2630 2631 2632
Array:  Generic
		| Datetime
		| Numeric
2633
		;
2634

2635
Generic:  generic
2636
				{
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($1);
				}
		;

generic:  Id									{ $$ = $1; }
		| TYPE_P								{ $$ = xlateSqlType("type"); }
		| DOUBLE PRECISION						{ $$ = xlateSqlType("float8"); }
		;

/* SQL92 numeric data types
 * Check FLOAT() precision limits assuming IEEE floating types.
 * Provide rudimentary DECIMAL() and NUMERIC() implementations
 *  by checking parameters and making sure they match what is possible with INTEGER.
 * - thomas 1997-09-18
 */
Numeric:  FLOAT opt_float
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType($2);
2657
				}
2658
		| DECIMAL opt_decimal
2659
				{
2660
					$$ = makeNode(TypeName);
2661 2662 2663 2664 2665 2666 2667 2668
					$$->name = xlateSqlType("integer");
				}
		| NUMERIC opt_numeric
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("integer");
				}
		;
2669

2670 2671 2672
opt_float:  '(' Iconst ')'
				{
					if ($2 < 1)
B
Bruce Momjian 已提交
2673
						elog(ERROR,"precision for FLOAT must be at least 1");
2674 2675 2676 2677 2678
					else if ($2 < 7)
						$$ = xlateSqlType("float4");
					else if ($2 < 16)
						$$ = xlateSqlType("float8");
					else
B
Bruce Momjian 已提交
2679
						elog(ERROR,"precision for FLOAT must be less than 16");
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
				}
		| /*EMPTY*/
				{
					$$ = xlateSqlType("float8");
				}
		;

opt_numeric:  '(' Iconst ',' Iconst ')'
				{
					if ($2 != 9)
2690
						elog(ERROR,"NUMERIC precision %d must be 9",$2);
2691
					if ($4 != 0)
2692
						elog(ERROR,"NUMERIC scale %d must be zero",$4);
2693 2694 2695 2696
				}
		| '(' Iconst ')'
				{
					if ($2 != 9)
2697
						elog(ERROR,"NUMERIC precision %d must be 9",$2);
2698 2699 2700 2701 2702 2703 2704 2705 2706 2707
				}
		| /*EMPTY*/
				{
					$$ = NULL;
				}
		;

opt_decimal:  '(' Iconst ',' Iconst ')'
				{
					if ($2 > 9)
2708
						elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2709
					if ($4 != 0)
2710
						elog(ERROR,"DECIMAL scale %d must be zero",$4);
2711 2712 2713 2714 2715
					$$ = NULL;
				}
		| '(' Iconst ')'
				{
					if ($2 > 9)
2716
						elog(ERROR,"DECIMAL precision %d exceeds implementation limit of 9",$2);
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741
					$$ = NULL;
				}
		| /*EMPTY*/
				{
					$$ = NULL;
				}
		;

/* SQL92 character data types
 * The following implements CHAR() and VARCHAR().
 * We do it here instead of the 'Generic' production
 * because we don't want to allow arrays of VARCHAR().
 * I haven't thought about whether that will work or not.
 *								- ay 6/95
 */
Character:  character '(' Iconst ')'
				{
					$$ = makeNode(TypeName);
					if (!strcasecmp($1, "char"))
						$$->name = xlateSqlType("bpchar");
					else if (!strcasecmp($1, "varchar"))
						$$->name = xlateSqlType("varchar");
					else
						yyerror("parse error");
					if ($3 < 1)
2742
						elog(ERROR,"length for '%s' type must be at least 1",$1);
2743 2744 2745 2746 2747
					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
2748
						 */
2749
						elog(ERROR,"length for type '%s' cannot exceed 4096",$1);
2750 2751 2752 2753 2754 2755

					/* 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
					 */
2756
					$$->typmod = VARHDRSZ + $3;
2757
				}
2758
		| character
2759 2760
				{
					$$ = makeNode(TypeName);
2761 2762 2763
					$$->name = xlateSqlType($1);
				}
		;
2764

2765 2766 2767 2768 2769 2770
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");
2771
					} else {
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
						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)
2782
					elog(ERROR,"COLLATE %s not yet implemented",$4);
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 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
					$$ = 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);
				}
		| TIMESTAMP opt_timezone
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("timestamp");
					$$->timezone = $2;
				}
		| TIME
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("time");
				}
		| INTERVAL opt_interval
				{
					$$ = makeNode(TypeName);
					$$->name = xlateSqlType("interval");
2823
				}
2824
		;
2825

2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844
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; }
2845
		| /*EMPTY*/								{ $$ = NIL; }
2846 2847
		;

2848 2849 2850

/*****************************************************************************
 *
2851
 *	expression grammar, still needs some cleanup
2852 2853 2854
 *
 *****************************************************************************/

2855
a_expr_or_null:  a_expr
2856
				{ $$ = $1; }
2857
		| NULL_P
2858 2859 2860 2861 2862
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Null;
					$$ = (Node *)n;
				}
2863 2864 2865 2866 2867 2868 2869 2870
		;

/* Expressions using row descriptors
 * Define row_descriptor to allow yacc to break the reduce/reduce conflict
 *  with singleton expressions.
 */
row_expr: '(' row_descriptor ')' IN '(' SubSelect ')'
				{
2871 2872 2873 2874 2875
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->subLinkType = IN_SUBLINK;
					n->subselect = $6;
					$$ = (Node *)n;
2876 2877 2878
				}
		| '(' row_descriptor ')' NOT IN '(' SubSelect ')'
				{
2879 2880 2881 2882 2883
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->subLinkType = NOTIN_SUBLINK;
					n->subselect = $7;
					$$ = (Node *)n;
2884
				}
2885 2886
/* We accept all Operators? */
		| '(' row_descriptor ')' Op '(' SubSelect ')'
2887
				{
2888 2889 2890 2891 2892 2893
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->subLinkType = OPER_SUBLINK;
					n->oper = lcons($4, NIL);
					n->subselect = $6;
					$$ = (Node *)n;
2894
				}
2895
/* Do we need this?
2896 2897
		| '(' row_descriptor ')' Op '(' row_descriptor ')'
				{
2898 2899 2900 2901 2902 2903
					SubLink *n = makeNode(SubLink);
					n->lefthand = $2;
					n->subLinkType = OPER_SUBLINK;
					n->oper = lcons($4, NIL);
					n->subselect = $6;
					$$ = (Node *)n;
2904
				}
2905
*/
2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
		;

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

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

2924
a_expr:  attr opt_indirection
2925 2926 2927 2928
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
2929 2930
		| row_expr
				{	$$ = $1;  }
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
		| AexprConst
				{	$$ = $1;  }
		| '-' a_expr %prec UMINUS
				{	$$ = makeA_Expr(OP, "-", NULL, $2); }
		| a_expr '+' a_expr
				{	$$ = makeA_Expr(OP, "+", $1, $3); }
		| a_expr '-' a_expr
				{	$$ = makeA_Expr(OP, "-", $1, $3); }
		| a_expr '/' a_expr
				{	$$ = makeA_Expr(OP, "/", $1, $3); }
		| a_expr '*' a_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| a_expr '<' a_expr
				{	$$ = makeA_Expr(OP, "<", $1, $3); }
		| a_expr '>' a_expr
				{	$$ = makeA_Expr(OP, ">", $1, $3); }
		| a_expr '=' a_expr
				{	$$ = makeA_Expr(OP, "=", $1, $3); }
		| ':' a_expr
				{	$$ = makeA_Expr(OP, ":", NULL, $2); }
		| ';' a_expr
				{	$$ = makeA_Expr(OP, ";", NULL, $2); }
		| '|' a_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
2955
		| a_expr TYPECAST Typename
2956
				{
2957
					$$ = (Node *)$1;
2958
					/* AexprConst can be either A_Const or ParamNo */
2959
					if (nodeTag($1) == T_A_Const) {
2960
						((A_Const *)$1)->typename = $3;
2961
					} else if (nodeTag($1) == T_Param) {
2962
						((ParamNo *)$1)->typename = $3;
2963 2964 2965 2966 2967 2968 2969
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
2970
				}
2971
		| CAST a_expr AS Typename
2972
				{
2973
					$$ = (Node *)$2;
2974
					/* AexprConst can be either A_Const or ParamNo */
2975
					if (nodeTag($2) == T_A_Const) {
2976
						((A_Const *)$2)->typename = $4;
2977
					} else if (nodeTag($2) == T_Param) {
2978
						((ParamNo *)$2)->typename = $4;
2979 2980 2981 2982 2983 2984 2985
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $4->name;
						n->args = lcons($2,NIL);
						$$ = (Node *)n;
					}
2986 2987 2988 2989
				}
		| '(' a_expr_or_null ')'
				{	$$ = $2; }
		| a_expr Op a_expr
2990
				{	$$ = makeIndexable($2,$1,$3);	}
2991
		| a_expr LIKE a_expr
2992
				{	$$ = makeIndexable("~~", $1, $3); }
2993 2994 2995 2996 2997 2998
		| 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); }
2999
		| ColId
3000 3001 3002 3003 3004 3005 3006 3007 3008
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
		| name '(' '*' ')'
				{
3009
					/* cheap hack for aggregate (eg. count) */
3010
					FuncCall *n = makeNode(FuncCall);
3011
					A_Const *star = makeNode(A_Const);
3012

3013 3014
					star->val.type = T_String;
					star->val.val.str = "";
3015 3016 3017 3018 3019 3020 3021 3022 3023
					n->funcname = $1;
					n->args = lcons(star, NIL);
					$$ = (Node *)n;
				}
		| name '(' ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
3024 3025
					$$ = (Node *)n;
				}
3026 3027 3028 3029 3030 3031 3032
		| name '(' expr_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060
		| 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;

					$$ = (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;

					$$ = (Node *)n;
				}
3061
		| CURRENT_TIME '(' Iconst ')'
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
				{
					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;

3077
					if ($3 != 0)
3078
						elog(NOTICE,"CURRENT_TIME(%d) precision not implemented; zero used instead",$3);
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095

					$$ = (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;

					$$ = (Node *)n;
				}
3096
		| CURRENT_TIMESTAMP '(' Iconst ')'
3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
				{
					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;

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

3115 3116
					$$ = (Node *)n;
				}
3117
		| CURRENT_USER
3118 3119 3120 3121 3122 3123
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = "getpgusername";
					n->args = NIL;
					$$ = (Node *)n;
				}
3124 3125 3126 3127 3128 3129 3130
		/* We probably need to define an "exists" node,
		 *	since the optimizer could choose to find only one match.
		 * Perhaps the first implementation could just check for
		 *	count(*) > 0? - thomas 1997-07-19
		 */
		| EXISTS '(' SubSelect ')'
				{
3131 3132 3133 3134 3135 3136
					SubLink *n = makeNode(SubLink);
					n->lefthand = NIL;
					n->subLinkType = EXISTS_SUBLINK;
					n->oper = NIL;
					n->subselect = $3;
					$$ = (Node *)n;
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 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
				}
		| 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); }
3190
		| a_expr IS NULL_P
3191 3192 3193
				{	$$ = makeA_Expr(ISNULL, NULL, $1, NULL); }
		| a_expr NOTNULL
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3194
		| a_expr IS NOT NULL_P
3195
				{	$$ = makeA_Expr(NOTNULL, NULL, $1, NULL); }
3196 3197 3198 3199 3200
		/* 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
		 */
3201 3202
		| a_expr IS TRUE_P
				{
3203 3204 3205 3206 3207 3208
					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");
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3209
				}
3210
		| a_expr IS NOT FALSE_P
3211
				{
3212 3213 3214 3215 3216 3217
					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");
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3218
				}
3219
		| a_expr IS FALSE_P
3220
				{
3221 3222 3223 3224 3225 3226
					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");
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3227
				}
3228
		| a_expr IS NOT TRUE_P
3229
				{
3230 3231 3232 3233 3234 3235
					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");
					$$ = makeA_Expr(OP, "=", $1,(Node *)n);
3236
				}
3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
		| a_expr BETWEEN AexprConst AND AexprConst
				{
					$$ = makeA_Expr(AND, NULL,
						makeA_Expr(OP, ">=", $1, $3),
						makeA_Expr(OP, "<=", $1, $5));
				}
		| a_expr NOT BETWEEN AexprConst AND AexprConst
				{
					$$ = makeA_Expr(OR, NULL,
						makeA_Expr(OP, "<", $1, $4),
						makeA_Expr(OP, ">", $1, $6));
				}
3249
		| a_expr IN { saved_In_Expr = $1; } '(' in_expr ')'
3250 3251 3252 3253 3254 3255 3256 3257 3258
				{
					if (nodeTag($5) == T_SubLink)
					{
						((SubLink *)$5)->lefthand = lcons($1, NIL);
						((SubLink *)$5)->subLinkType = IN_SUBLINK;
						$$ = (Node *)$5;
					}
					else	$$ = $5;
				}
3259
		| a_expr NOT IN { saved_In_Expr = $1; } '(' not_in_expr ')'
3260 3261 3262 3263 3264 3265 3266 3267 3268
				{
					if (nodeTag($6) == T_SubLink)
					{
						((SubLink *)$6)->lefthand = lcons($1, NIL);
						((SubLink *)$6)->subLinkType = NOTIN_SUBLINK;
						$$ = (Node *)$6;
					}
					else	$$ = $6;
				}
3269 3270 3271 3272 3273 3274 3275
		| 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); }
		;
3276

3277
opt_indirection:  '[' a_expr ']' opt_indirection
3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
				{
					A_Indices *ai = makeNode(A_Indices);
					ai->lidx = NULL;
					ai->uidx = $2;
					$$ = lcons(ai, $4);
				}
		| '[' a_expr ':' a_expr ']' opt_indirection
				{
					A_Indices *ai = makeNode(A_Indices);
					ai->lidx = $2;
					ai->uidx = $4;
					$$ = lcons(ai, $6);
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
3294

3295
expr_list:  a_expr_or_null
3296
				{ $$ = lcons($1, NIL); }
3297
		| expr_list ',' a_expr_or_null
3298
				{ $$ = lappend($1, $3); }
3299
		| expr_list USING a_expr
3300 3301
				{ $$ = lappend($1, $3); }
		;
3302

3303
extract_list:  datetime FROM a_expr
3304 3305 3306 3307 3308 3309 3310 3311 3312
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = $1;
					$$ = lappend(lcons((Node *)n,NIL), $3);
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
3313

3314
position_list:  position_expr IN position_expr
3315
				{	$$ = makeList($3, $1, -1); }
3316 3317 3318
		| /* EMPTY */
				{	$$ = NIL; }
		;
3319

3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338
position_expr:  attr opt_indirection
				{
					$1->indirection = $2;
					$$ = (Node *)$1;
				}
		| AexprConst
				{	$$ = $1;  }
		| '-' position_expr %prec UMINUS
				{	$$ = makeA_Expr(OP, "-", NULL, $2); }
		| position_expr '+' position_expr
				{	$$ = makeA_Expr(OP, "+", $1, $3); }
		| position_expr '-' position_expr
				{	$$ = makeA_Expr(OP, "-", $1, $3); }
		| position_expr '/' position_expr
				{	$$ = makeA_Expr(OP, "/", $1, $3); }
		| position_expr '*' position_expr
				{	$$ = makeA_Expr(OP, "*", $1, $3); }
		| '|' position_expr
				{	$$ = makeA_Expr(OP, "|", NULL, $2); }
3339
		| position_expr TYPECAST Typename
3340
				{
3341
					$$ = (Node *)$1;
3342
					/* AexprConst can be either A_Const or ParamNo */
3343
					if (nodeTag($1) == T_A_Const) {
3344
						((A_Const *)$1)->typename = $3;
3345
					} else if (nodeTag($1) == T_Param) {
3346
						((ParamNo *)$1)->typename = $3;
3347 3348 3349 3350 3351 3352 3353
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $3->name;
						n->args = lcons($1,NIL);
						$$ = (Node *)n;
					}
3354
				}
3355
		| CAST position_expr AS Typename
3356
				{
3357
					$$ = (Node *)$2;
3358
					/* AexprConst can be either A_Const or ParamNo */
3359
					if (nodeTag($2) == T_A_Const) {
3360
						((A_Const *)$2)->typename = $4;
3361
					} else if (nodeTag($2) == T_Param) {
3362
						((ParamNo *)$2)->typename = $4;
3363 3364 3365 3366 3367 3368 3369
					/* otherwise, try to transform to a function call */
					} else {
						FuncCall *n = makeNode(FuncCall);
						n->funcname = $4->name;
						n->args = lcons($2,NIL);
						$$ = (Node *)n;
					}
3370 3371 3372 3373 3374 3375 3376 3377 3378
				}
		| '(' 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); }
3379
		| ColId
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393
				{
					/* could be a column name or a relation_name */
					Ident *n = makeNode(Ident);
					n->name = $1;
					n->indirection = NULL;
					$$ = (Node *)n;
				}
		| name '(' ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = NIL;
					$$ = (Node *)n;
				}
3394 3395 3396 3397 3398 3399 3400
		| name '(' expr_list ')'
				{
					FuncCall *n = makeNode(FuncCall);
					n->funcname = $1;
					n->args = $3;
					$$ = (Node *)n;
				}
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
		| 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;
				}
		;

3446
substr_list:  expr_list substr_from substr_for
3447
				{
3448
					$$ = nconc(nconc($1,$2),$3);
3449 3450 3451 3452
				}
		| /* EMPTY */
				{	$$ = NIL; }
		;
3453

3454
substr_from:  FROM expr_list
3455 3456
				{	$$ = $2; }
		| /* EMPTY */
3457 3458 3459 3460 3461 3462
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_Integer;
					n->val.val.ival = 1;
					$$ = lcons((Node *)n,NIL);
				}
3463
		;
3464

3465
substr_for:  FOR expr_list
3466 3467 3468 3469
				{	$$ = $2; }
		| /* EMPTY */
				{	$$ = NIL; }
		;
3470

3471
trim_list:  a_expr FROM expr_list
3472
				{ $$ = lappend($3, $1); }
3473
		| FROM expr_list
3474
				{ $$ = $2; }
3475
		| expr_list
3476 3477
				{ $$ = $1; }
		;
3478

3479 3480
in_expr:  SubSelect
				{
3481 3482 3483
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
3484 3485 3486 3487 3488
				}
		| in_expr_nodes
				{	$$ = $1; }
		;

3489
in_expr_nodes:  AexprConst
3490
				{	$$ = makeA_Expr(OP, "=", saved_In_Expr, $1); }
3491
		| in_expr_nodes ',' AexprConst
3492 3493 3494 3495
				{	$$ = makeA_Expr(OR, NULL, $1,
						makeA_Expr(OP, "=", saved_In_Expr, $3));
				}
		;
3496

3497 3498
not_in_expr:  SubSelect
				{
3499 3500 3501
					SubLink *n = makeNode(SubLink);
					n->subselect = $1;
					$$ = (Node *)n;
3502 3503 3504 3505 3506
				}
		| not_in_expr_nodes
				{	$$ = $1; }
		;

3507
not_in_expr_nodes:  AexprConst
3508
				{	$$ = makeA_Expr(OP, "<>", saved_In_Expr, $1); }
3509
		| not_in_expr_nodes ',' AexprConst
3510 3511 3512 3513
				{	$$ = makeA_Expr(AND, NULL, $1,
						makeA_Expr(OP, "<>", saved_In_Expr, $3));
				}
		;
3514

3515
attr:  relation_name '.' attrs
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540
				{
					$$ = 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("*")); }
		;

3541 3542 3543

/*****************************************************************************
 *
3544
 *	target lists
3545 3546 3547
 *
 *****************************************************************************/

3548
res_target_list:  res_target_list ',' res_target_el
3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
				{	$$ = 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);
				}
		;
3566

3567
res_target_el:  ColId opt_indirection '=' a_expr_or_null
3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593
				{
					$$ = 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;
				}
		;
3594 3595 3596 3597 3598 3599

/*
** target list for select.
** should get rid of the other but is still needed by the defunct retrieve into
** and update (uses a subset)
*/
3600
res_target_list2:  res_target_list2 ',' res_target_el2
3601 3602 3603 3604
				{	$$ = lappend($1, $3);  }
		| res_target_el2
				{	$$ = lcons($1, NIL);  }
		;
3605 3606

/* AS is not optional because shift/red conflict with unary ops */
3607
res_target_el2:  a_expr_or_null AS ColLabel
3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
				{
					$$ = 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;
				}
		;

3647
opt_id:  ColId									{ $$ = $1; }
3648 3649 3650 3651 3652 3653
		| /* EMPTY */							{ $$ = NULL; }
		;

relation_name:	SpecialRuleRelation
				{
					$$ = $1;
3654
					StrNCpy(saved_relname, $1, NAMEDATALEN);
3655 3656 3657
				}
		| ColId
				{
3658
					/* disallow refs to variable system tables */
3659
					if (strcmp(LogRelationName, $1) == 0
3660
					   || strcmp(VariableRelationName, $1) == 0)
3661
						elog(ERROR,"%s cannot be accessed by users",$1);
3662 3663
					else
						$$ = $1;
3664
					StrNCpy(saved_relname, $1, NAMEDATALEN);
3665 3666 3667
				}
		;

3668
database_name:			ColId			{ $$ = $1; };
3669 3670 3671
access_method:			Id				{ $$ = $1; };
attr_name:				ColId			{ $$ = $1; };
class:					Id				{ $$ = $1; };
3672
index_name:				ColId			{ $$ = $1; };
3673

3674 3675 3676 3677 3678
/* Functions
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
 */
name:					ColId			{ $$ = $1; };
3679 3680 3681

file_name:				Sconst			{ $$ = $1; };
recipe_name:			Id				{ $$ = $1; };
3682

3683 3684 3685
/* Constants
 * Include TRUE/FALSE for SQL3 support. - thomas 1997-10-24
 */
3686
AexprConst:  Iconst
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706
				{
					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;
				}
3707 3708 3709 3710 3711 3712 3713 3714
		| Typename Sconst
				{
					A_Const *n = makeNode(A_Const);
					n->typename = $1;
					n->val.type = T_String;
					n->val.val.str = $2;
					$$ = (Node *)n;
				}
3715 3716
		| ParamNo
				{	$$ = (Node *)$1;  }
3717 3718 3719 3720 3721
		| TRUE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "t";
3722 3723
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3724 3725 3726 3727 3728 3729 3730
					$$ = (Node *)n;
				}
		| FALSE_P
				{
					A_Const *n = makeNode(A_Const);
					n->val.type = T_String;
					n->val.val.str = "f";
3731 3732
					n->typename = makeNode(TypeName);
					n->typename->name = xlateSqlType("bool");
3733 3734
					$$ = (Node *)n;
				}
3735
		;
3736 3737

ParamNo:  PARAM
3738 3739 3740 3741 3742
				{
					$$ = makeNode(ParamNo);
					$$->number = $1;
				}
		;
3743

3744
NumConst:  Iconst						{ $$ = makeInteger($1); }
3745
		| FCONST						{ $$ = makeFloat($1); }
3746
		;
3747

3748 3749
Iconst:  ICONST							{ $$ = $1; };
Sconst:  SCONST							{ $$ = $1; };
3750

3751 3752 3753 3754 3755
/* 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
 */
3756
Id:  IDENT								{ $$ = $1; };
3757

3758 3759 3760
/* Column identifier
 * Include date/time keywords as SQL92 extension.
 * Include TYPE as a SQL92 unreserved keyword. - thomas 1997-10-05
3761 3762 3763 3764
 * 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
3765
 */
3766 3767
ColId:  Id								{ $$ = $1; }
		| datetime						{ $$ = $1; }
3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
		| ACTION						{ $$ = "action"; }
		| DATABASE						{ $$ = "database"; }
		| DELIMITERS					{ $$ = "delimiters"; }
		| FUNCTION						{ $$ = "function"; }
		| INDEX							{ $$ = "index"; }
		| KEY							{ $$ = "key"; }
		| LANGUAGE						{ $$ = "language"; }
		| LOCATION						{ $$ = "location"; }
		| MATCH							{ $$ = "match"; }
		| OPERATOR						{ $$ = "operator"; }
		| OPTION						{ $$ = "option"; }
		| PRIVILEGES					{ $$ = "privileges"; }
		| RECIPE						{ $$ = "recipe"; }
3781
		| TIME							{ $$ = "time"; }
3782
		| TRIGGER						{ $$ = "trigger"; }
3783
		| TYPE_P						{ $$ = "type"; }
3784 3785
		| VERSION						{ $$ = "version"; }
		| ZONE							{ $$ = "zone"; }
3786 3787 3788 3789 3790 3791 3792 3793
		;

/* 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
3794 3795 3796
 * 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
3797 3798
 */
ColLabel:  ColId						{ $$ = $1; }
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810
		| ARCHIVE						{ $$ = "archive"; }
		| CLUSTER						{ $$ = "cluster"; }
		| CONSTRAINT					{ $$ = "constraint"; }
		| CROSS							{ $$ = "cross"; }
		| FOREIGN						{ $$ = "foreign"; }
		| GROUP							{ $$ = "group"; }
		| LOAD							{ $$ = "load"; }
		| ORDER							{ $$ = "order"; }
		| POSITION						{ $$ = "position"; }
		| PRECISION						{ $$ = "precision"; }
		| TABLE							{ $$ = "table"; }
		| TRANSACTION					{ $$ = "transaction"; }
3811 3812
		| TRUE_P						{ $$ = "true"; }
		| FALSE_P						{ $$ = "false"; }
3813
		;
3814 3815

SpecialRuleRelation:  CURRENT
3816 3817 3818 3819
				{
					if (QueryIsRule)
						$$ = "*CURRENT*";
					else
B
Bruce Momjian 已提交
3820
						elog(ERROR,"CURRENT used in non-rule query");
3821 3822 3823 3824 3825 3826
				}
		| NEW
				{
					if (QueryIsRule)
						$$ = "*NEW*";
					else
B
Bruce Momjian 已提交
3827
						elog(ERROR,"NEW used in non-rule query");
3828 3829
				}
		;
3830 3831 3832

%%

3833 3834
static Node *
makeA_Expr(int oper, char *opname, Node *lexpr, Node *rexpr)
3835
{
3836 3837 3838 3839 3840 3841
	A_Expr *a = makeNode(A_Expr);
	a->oper = oper;
	a->opname = opname;
	a->lexpr = lexpr;
	a->rexpr = rexpr;
	return (Node *)a;
3842 3843
}

3844 3845 3846 3847 3848 3849 3850
void
mapTargetColumns(List *src, List *dst)
{
	ColumnDef *s;
	ResTarget *d;

	if (length(src) != length(dst))
B
Bruce Momjian 已提交
3851
		elog(ERROR,"CREATE TABLE/AS SELECT has mismatched column count");
3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865

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

3867 3868 3869 3870 3871
static Node *makeIndexable(char *opname, Node *lexpr, Node *rexpr)
{
	Node *result = NULL;

	/* we do this so indexes can be used */
3872 3873
	if (strcmp(opname,"~") == 0 ||
		strcmp(opname,"~*") == 0)
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890
	{
		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] == '[' ||
3891
					n->val.val.str[pos] == '$' ||
3892
					(strcmp(opname,"~*") == 0 && isalpha(n->val.val.str[pos])))
3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 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 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970
		     		break;
		     	if (n->val.val.str[pos] == '\\')
					pos++;
				match_least[match_pos] = n->val.val.str[pos];
				match_most[match_pos++] = n->val.val.str[pos];
			}

			if (match_pos != 0)
			{
				A_Const *least = makeNode(A_Const);
				A_Const *most = makeNode(A_Const);
				
				/* make strings to be used in index use */
				match_least[match_pos] = '\0';
				match_most[match_pos] = '\377';
				match_most[match_pos+1] = '\0';
				least->val.type = T_String;
				least->val.val.str = match_least;
				most->val.type = T_String;
				most->val.val.str = match_most;
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~", lexpr, rexpr),
						makeA_Expr(AND, NULL,
							makeA_Expr(OP, ">=", lexpr, (Node *)least),
							makeA_Expr(OP, "<=", lexpr, (Node *)most)));
			}
		}
	}
	else if (strcmp(opname,"~~") == 0)
	{
		if (nodeTag(rexpr) == T_A_Const &&
		   ((A_Const *)rexpr)->val.type == T_String)
		{
			A_Const *n = (A_Const *)rexpr;
			char *match_least = palloc(strlen(n->val.val.str)+2);
			char *match_most = palloc(strlen(n->val.val.str)+2);
			int pos, match_pos=0;
	
			for (pos = 0; n->val.val.str[pos]; pos++)
			{
				if ((n->val.val.str[pos] == '%' &&
					 n->val.val.str[pos+1] != '%') ||
				    (n->val.val.str[pos] == '_' &&
		     		 n->val.val.str[pos+1] != '_'))
		     		break;
		     	if (n->val.val.str[pos] == '%' ||
				    n->val.val.str[pos] == '_' ||
				    n->val.val.str[pos] == '\\')
					pos++;
				match_least[match_pos] = n->val.val.str[pos];
				match_most[match_pos++] = n->val.val.str[pos];
			}
	
			if (match_pos != 0)
			{
				A_Const *least = makeNode(A_Const);
				A_Const *most = makeNode(A_Const);
				
				/* make strings to be used in index use */
				match_least[match_pos] = '\0';
				match_most[match_pos] = '\377';
				match_most[match_pos+1] = '\0';
				least->val.type = T_String;
				least->val.val.str = match_least;
				most->val.type = T_String;
				most->val.val.str = match_most;
				result = makeA_Expr(AND, NULL,
						makeA_Expr(OP, "~~", lexpr, rexpr),
						makeA_Expr(AND, NULL,
							makeA_Expr(OP, ">=", lexpr, (Node *)least),
							makeA_Expr(OP, "<=", lexpr, (Node *)most)));
			}
		}
	}
	
	if (result == NULL)
		result = makeA_Expr(OP, opname, lexpr, rexpr);
	return result;
3971
} /* makeIndexable() */
3972 3973


3974 3975
/* xlateSqlType()
 * Convert alternate type names to internal Postgres types.
3976 3977
 * Do not convert "float", since that is handled elsewhere
 *  for FLOAT(p) syntax.
3978
 */
3979 3980 3981
static char *
xlateSqlType(char *name)
{
3982 3983
	if (!strcasecmp(name,"int")
	 || !strcasecmp(name,"integer"))
3984
		return "int4";
3985 3986
	else if (!strcasecmp(name, "smallint"))
		return "int2";
3987
	else if (!strcasecmp(name, "real"))
3988 3989 3990
		return "float8";
	else if (!strcasecmp(name, "interval"))
		return "timespan";
3991 3992
	else if (!strcasecmp(name, "boolean"))
		return "bool";
3993 3994
	else
		return name;
3995
} /* xlateSqlName() */
3996

3997

3998 3999
void parser_init(Oid *typev, int nargs)
{
4000
	QueryIsRule = FALSE;
4001 4002
	saved_relname[0]= '\0';
	saved_In_Expr = NULL;
4003

4004
	param_type_init(typev, nargs);
4005
}
4006

4007

4008 4009 4010
/* FlattenStringList()
 * Traverse list of string nodes and convert to a single string.
 * Used for reconstructing string form of complex expressions.
4011 4012
 *
 * Allocate at least one byte for terminator.
4013 4014 4015 4016
 */
static char *
FlattenStringList(List *list)
{
4017 4018
	List *l;
	Value *v;
4019 4020
	char *s;
	char *sp;
4021
	int nlist, len = 0;
4022 4023 4024 4025

	nlist = length(list);
	l = list;
	while(l != NIL) {
4026 4027
		v = (Value *)lfirst(l);
		sp = v->val.str;
4028
		l = lnext(l);
4029
		len += strlen(sp);
4030
	};
4031
	len += nlist;
4032

4033
	s = (char*) palloc(len+1);
4034 4035 4036 4037
	*s = '\0';

	l = list;
	while(l != NIL) {
4038 4039
		v = (Value *)lfirst(l);
		sp = v->val.str;
4040 4041
		l = lnext(l);
		strcat(s,sp);
4042
		if (l != NIL) strcat(s," ");
4043
	};
4044
	*(s+len) = '\0';
4045 4046 4047 4048 4049 4050 4051 4052

#ifdef PARSEDEBUG
printf( "flattened string is \"%s\"\n", s);
#endif

	return(s);
} /* FlattenStringList() */

4053

4054 4055 4056 4057 4058 4059 4060 4061
/* makeConstantList()
 * Convert constant value node into string node.
 */
static List *
makeConstantList( A_Const *n)
{
	char *defval = NULL;
	if (nodeTag(n) != T_A_Const) {
B
Bruce Momjian 已提交
4062
		elog(ERROR,"Cannot handle non-constant parameter");
4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078

	} else if (n->val.type == T_Float) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%g", n->val.val.dval);

	} else if (n->val.type == T_Integer) {
		defval = (char*) palloc(20+1);
		sprintf( defval, "%ld", n->val.val.ival);

	} 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, "'");

	} else {
B
Bruce Momjian 已提交
4079
		elog(ERROR,"Internal error in makeConstantList(): cannot encode node");
4080 4081 4082 4083 4084 4085 4086 4087
	};

#ifdef PARSEDEBUG
printf( "AexprConst argument is \"%s\"\n", defval);
#endif

	return( lcons( makeString(defval), NIL));
} /* makeConstantList() */
4088

4089

4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115
/* fmtId()
 * Check input string for non-lowercase/non-numeric characters.
 * Returns either input string or input surrounded by double quotes.
 */
static char *
fmtId(char *rawid)
{
	static char *cp;

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

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

#ifdef PARSEDEBUG
printf("fmtId- %sconvert %s to %s\n", ((cp == rawid)? "do not ": ""), rawid, cp);
#endif

	return(cp);
4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136
}

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