~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/parser/gram.y

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
 
 
3
/*#define YYDEBUG 1*/
 
4
/*-------------------------------------------------------------------------
 
5
 *
 
6
 * gram.y
 
7
 *        POSTGRES SQL YACC rules/actions
 
8
 *
 
9
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
10
 * Portions Copyright (c) 1994, Regents of the University of California
 
11
 *
 
12
 *
 
13
 * IDENTIFICATION
 
14
 *        $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.481 2004-12-31 22:00:27 pgsql Exp $
 
15
 *
 
16
 * HISTORY
 
17
 *        AUTHOR                        DATE                    MAJOR EVENT
 
18
 *        Andrew Yu                     Sept, 1994              POSTQUEL to SQL conversion
 
19
 *        Andrew Yu                     Oct, 1994               lispy code conversion
 
20
 *
 
21
 * NOTES
 
22
 *        CAPITALS are used to represent terminal symbols.
 
23
 *        non-capitals are used to represent non-terminals.
 
24
 *        SQL92-specific syntax is separated from plain SQL/Postgres syntax
 
25
 *        to help isolate the non-extensible portions of the parser.
 
26
 *
 
27
 *        In general, nothing in this file should initiate database accesses
 
28
 *        nor depend on changeable state (such as SET variables).  If you do
 
29
 *        database accesses, your code will fail when we have aborted the
 
30
 *        current transaction and are just parsing commands to find the next
 
31
 *        ROLLBACK or COMMIT.  If you make use of SET variables, then you
 
32
 *        will do the wrong thing in multi-query strings like this:
 
33
 *                      SET SQL_inheritance TO off; SELECT * FROM foo;
 
34
 *        because the entire string is parsed by gram.y before the SET gets
 
35
 *        executed.  Anything that depends on the database or changeable state
 
36
 *        should be handled inside parse_analyze() so that it happens at the
 
37
 *        right time not the wrong time.  The handling of SQL_inheritance is
 
38
 *        a good example.
 
39
 *
 
40
 * WARNINGS
 
41
 *        If you use a list, make sure the datum is a node so that the printing
 
42
 *        routines work.
 
43
 *
 
44
 *        Sometimes we assign constants to makeStrings. Make sure we don't free
 
45
 *        those.
 
46
 *
 
47
 *-------------------------------------------------------------------------
 
48
 */
 
49
#include "postgres.h"
 
50
 
 
51
#include <ctype.h>
 
52
#include <limits.h>
 
53
 
 
54
#include "catalog/index.h"
 
55
#include "catalog/namespace.h"
 
56
#include "nodes/makefuncs.h"
 
57
#include "parser/gramparse.h"
 
58
#include "storage/lmgr.h"
 
59
#include "utils/date.h"
 
60
#include "utils/datetime.h"
 
61
#include "utils/numeric.h"
 
62
 
 
63
 
 
64
extern List *parsetree;                 /* final parse result is delivered here */
 
65
 
 
66
static bool QueryIsRule = FALSE;
 
67
 
 
68
/*
 
69
 * If you need access to certain yacc-generated variables and find that
 
70
 * they're static by default, uncomment the next line.  (this is not a
 
71
 * problem, yet.)
 
72
 */
 
73
/*#define __YYSCLASS*/
 
74
 
 
75
static Node *makeColumnRef(char *relname, List *indirection);
 
76
static Node *makeTypeCast(Node *arg, TypeName *typename);
 
77
static Node *makeStringConst(char *str, TypeName *typename);
 
78
static Node *makeIntConst(int val);
 
79
static Node *makeFloatConst(char *str);
 
80
static Node *makeAConst(Value *v);
 
81
static Node *makeRowNullTest(NullTestType test, RowExpr *row);
 
82
static DefElem *makeDefElem(char *name, Node *arg);
 
83
static A_Const *makeBoolAConst(bool state);
 
84
static FuncCall *makeOverlaps(List *largs, List *rargs);
 
85
static void check_qualified_name(List *names);
 
86
static List *check_func_name(List *names);
 
87
static List *extractArgTypes(List *parameters);
 
88
static SelectStmt *findLeftmostSelect(SelectStmt *node);
 
89
static void insertSelectOptions(SelectStmt *stmt,
 
90
                                                                List *sortClause, List *forUpdate,
 
91
                                                                Node *limitOffset, Node *limitCount);
 
92
static Node *makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg);
 
93
static Node *doNegate(Node *n);
 
94
static void doNegateFloat(Value *v);
 
95
 
 
96
%}
 
97
 
 
98
 
 
99
%union
 
100
{
 
101
        int                                     ival;
 
102
        char                            chr;
 
103
        char                            *str;
 
104
        const char                      *keyword;
 
105
        bool                            boolean;
 
106
        JoinType                        jtype;
 
107
        DropBehavior            dbehavior;
 
108
        OnCommitAction          oncommit;
 
109
        ContainsOids            withoids;
 
110
        List                            *list;
 
111
        Node                            *node;
 
112
        Value                           *value;
 
113
        ObjectType                      objtype;
 
114
 
 
115
        TypeName                        *typnam;
 
116
        FunctionParameter   *fun_param;
 
117
        DefElem                         *defelt;
 
118
        SortBy                          *sortby;
 
119
        JoinExpr                        *jexpr;
 
120
        IndexElem                       *ielem;
 
121
        Alias                           *alias;
 
122
        RangeVar                        *range;
 
123
        A_Indices                       *aind;
 
124
        ResTarget                       *target;
 
125
        PrivTarget                      *privtarget;
 
126
 
 
127
        InsertStmt                      *istmt;
 
128
        VariableSetStmt         *vsetstmt;
 
129
}
 
130
 
 
131
%type <node>    stmt schema_stmt
 
132
                AlterDatabaseSetStmt AlterDomainStmt AlterGroupStmt AlterOwnerStmt
 
133
                AlterSeqStmt AlterTableStmt AlterUserStmt AlterUserSetStmt
 
134
                AnalyzeStmt ClosePortalStmt ClusterStmt CommentStmt
 
135
                ConstraintsSetStmt CopyStmt CreateAsStmt CreateCastStmt
 
136
                CreateDomainStmt CreateGroupStmt CreateOpClassStmt CreatePLangStmt
 
137
                CreateSchemaStmt CreateSeqStmt CreateStmt CreateTableSpaceStmt
 
138
                CreateAssertStmt CreateTrigStmt CreateUserStmt
 
139
                CreatedbStmt DeclareCursorStmt DefineStmt DeleteStmt
 
140
                DropGroupStmt DropOpClassStmt DropPLangStmt DropStmt
 
141
                DropAssertStmt DropTrigStmt DropRuleStmt DropCastStmt
 
142
                DropUserStmt DropdbStmt DropTableSpaceStmt ExplainStmt FetchStmt
 
143
                GrantStmt IndexStmt InsertStmt ListenStmt LoadStmt
 
144
                LockStmt NotifyStmt ExplainableStmt PreparableStmt
 
145
                CreateFunctionStmt ReindexStmt RemoveAggrStmt
 
146
                RemoveFuncStmt RemoveOperStmt RenameStmt RevokeStmt
 
147
                RuleActionStmt RuleActionStmtOrEmpty RuleStmt
 
148
                SelectStmt TransactionStmt TruncateStmt
 
149
                UnlistenStmt UpdateStmt VacuumStmt
 
150
                VariableResetStmt VariableSetStmt VariableShowStmt
 
151
                ViewStmt CheckPointStmt CreateConversionStmt
 
152
                DeallocateStmt PrepareStmt ExecuteStmt
 
153
 
 
154
%type <node>    select_no_parens select_with_parens select_clause
 
155
                                simple_select
 
156
 
 
157
%type <node>    alter_column_default opclass_item alter_using
 
158
%type <ival>    add_drop
 
159
 
 
160
%type <node>    alter_table_cmd alter_rel_cmd
 
161
%type <list>    alter_table_cmds alter_rel_cmds
 
162
 
 
163
%type <dbehavior>       opt_drop_behavior
 
164
 
 
165
%type <list>    createdb_opt_list copy_opt_list transaction_mode_list
 
166
%type <defelt>  createdb_opt_item copy_opt_item transaction_mode_item
 
167
 
 
168
%type <ival>    opt_lock lock_type cast_context
 
169
%type <boolean> opt_force opt_or_replace
 
170
                                opt_grant_grant_option opt_revoke_grant_option
 
171
                                opt_nowait
 
172
 
 
173
%type <boolean> like_including_defaults
 
174
 
 
175
%type <list>    user_list
 
176
 
 
177
%type <list>    OptGroupList
 
178
%type <defelt>  OptGroupElem
 
179
 
 
180
%type <list>    OptUserList
 
181
%type <defelt>  OptUserElem
 
182
 
 
183
%type <str>             OptSchemaName
 
184
%type <list>    OptSchemaEltList
 
185
 
 
186
%type <boolean> TriggerActionTime TriggerForSpec opt_trusted
 
187
%type <str>             opt_lancompiler
 
188
 
 
189
%type <str>             TriggerEvents
 
190
%type <value>   TriggerFuncArg
 
191
 
 
192
%type <str>             relation_name copy_file_name
 
193
                                database_name access_method_clause access_method attr_name
 
194
                                index_name name function_name file_name
 
195
 
 
196
%type <list>    func_name handler_name qual_Op qual_all_Op subquery_Op
 
197
                                opt_class opt_validator
 
198
 
 
199
%type <range>   qualified_name OptConstrFromTable
 
200
 
 
201
%type <str>             all_Op MathOp SpecialRuleRelation
 
202
 
 
203
%type <str>             iso_level opt_encoding
 
204
%type <node>    grantee
 
205
%type <list>    grantee_list
 
206
%type <ival>    privilege
 
207
%type <list>    privileges privilege_list
 
208
%type <privtarget> privilege_target
 
209
%type <node>    function_with_argtypes
 
210
%type <list>    function_with_argtypes_list
 
211
%type <chr>     TriggerOneEvent
 
212
 
 
213
%type <list>    stmtblock stmtmulti
 
214
                                OptTableElementList TableElementList OptInherit definition
 
215
                                opt_distinct opt_definition func_args
 
216
                                func_args_list func_as createfunc_opt_list
 
217
                                oper_argtypes RuleActionList RuleActionMulti
 
218
                                opt_column_list columnList opt_name_list
 
219
                                sort_clause opt_sort_clause sortby_list index_params
 
220
                                name_list from_clause from_list opt_array_bounds
 
221
                                qualified_name_list any_name any_name_list
 
222
                                any_operator expr_list attrs
 
223
                                target_list update_target_list insert_column_list
 
224
                                insert_target_list def_list indirection opt_indirection
 
225
                                group_clause TriggerFuncArgs select_limit
 
226
                                opt_select_limit opclass_item_list
 
227
                                transaction_mode_list_or_empty
 
228
                                TableFuncElementList
 
229
                                prep_type_clause prep_type_list
 
230
                                execute_param_clause
 
231
 
 
232
%type <range>   into_clause OptTempTableName
 
233
 
 
234
%type <defelt>  createfunc_opt_item
 
235
%type <fun_param> func_arg
 
236
%type <typnam>  func_return func_type aggr_argtype
 
237
 
 
238
%type <boolean> arg_class TriggerForType OptTemp
 
239
%type <oncommit> OnCommitOption
 
240
%type <withoids> OptWithOids WithOidsAs
 
241
 
 
242
%type <list>    for_update_clause opt_for_update_clause update_list
 
243
%type <boolean> opt_all
 
244
 
 
245
%type <node>    join_outer join_qual
 
246
%type <jtype>   join_type
 
247
 
 
248
%type <list>    extract_list overlay_list position_list
 
249
%type <list>    substr_list trim_list
 
250
%type <ival>    opt_interval
 
251
%type <node>    overlay_placing substr_from substr_for
 
252
 
 
253
%type <boolean> opt_instead opt_analyze
 
254
%type <boolean> index_opt_unique opt_verbose opt_full
 
255
%type <boolean> opt_freeze opt_default opt_recheck
 
256
%type <defelt>  opt_binary opt_oids copy_delimiter
 
257
 
 
258
%type <boolean> copy_from opt_hold
 
259
 
 
260
%type <ival>    fetch_count     opt_column event cursor_options
 
261
%type <objtype> reindex_type drop_type comment_type
 
262
 
 
263
%type <node>    fetch_direction select_limit_value select_offset_value
 
264
 
 
265
%type <list>    OptSeqList
 
266
%type <defelt>  OptSeqElem
 
267
 
 
268
%type <istmt>   insert_rest
 
269
 
 
270
%type <vsetstmt> set_rest
 
271
 
 
272
%type <node>    TableElement ConstraintElem TableFuncElement
 
273
%type <node>    columnDef
 
274
%type <defelt>  def_elem
 
275
%type <node>    def_arg columnElem where_clause
 
276
                                a_expr b_expr c_expr func_expr AexprConst indirection_el
 
277
                                columnref in_expr having_clause func_table array_expr
 
278
%type <list>    row type_list array_expr_list
 
279
%type <node>    case_expr case_arg when_clause case_default
 
280
%type <list>    when_clause_list
 
281
%type <ival>    sub_type
 
282
%type <list>    OptCreateAs CreateAsList
 
283
%type <node>    CreateAsElement
 
284
%type <value>   NumericOnly FloatOnly IntegerOnly
 
285
%type <alias>   alias_clause
 
286
%type <sortby>  sortby
 
287
%type <ielem>   index_elem
 
288
%type <node>    table_ref
 
289
%type <jexpr>   joined_table
 
290
%type <range>   relation_expr
 
291
%type <target>  target_el insert_target_el update_target_el insert_column_item
 
292
 
 
293
%type <typnam>  Typename SimpleTypename ConstTypename
 
294
                                GenericType Numeric opt_float
 
295
                                Character ConstCharacter
 
296
                                CharacterWithLength CharacterWithoutLength
 
297
                                ConstDatetime ConstInterval
 
298
                                Bit ConstBit BitWithLength BitWithoutLength
 
299
%type <str>             character
 
300
%type <str>             extract_arg
 
301
%type <str>             opt_charset
 
302
%type <ival>    opt_numeric opt_decimal
 
303
%type <boolean> opt_varying opt_timezone
 
304
 
 
305
%type <ival>    Iconst
 
306
%type <str>             Sconst comment_text
 
307
%type <str>             UserId opt_boolean ColId_or_Sconst
 
308
%type <list>    var_list var_list_or_default
 
309
%type <str>             ColId ColLabel var_name type_name param_name
 
310
%type <node>    var_value zone_value
 
311
 
 
312
%type <keyword> unreserved_keyword func_name_keyword
 
313
%type <keyword> col_name_keyword reserved_keyword
 
314
 
 
315
%type <node>    TableConstraint TableLikeClause
 
316
%type <list>    ColQualList
 
317
%type <node>    ColConstraint ColConstraintElem ConstraintAttr
 
318
%type <ival>    key_actions key_delete key_match key_update key_action
 
319
%type <ival>    ConstraintAttributeSpec ConstraintDeferrabilitySpec
 
320
                                ConstraintTimeSpec
 
321
 
 
322
%type <list>    constraints_set_list
 
323
%type <boolean> constraints_set_mode
 
324
%type <str>             OptTableSpace OptConsTableSpace OptTableSpaceOwner
 
325
 
 
326
 
 
327
/*
 
328
 * If you make any token changes, update the keyword table in
 
329
 * parser/keywords.c and add new keywords to the appropriate one of
 
330
 * the reserved-or-not-so-reserved keyword lists, below.
 
331
 */
 
332
 
 
333
/* ordinary key words in alphabetical order */
 
334
%token <keyword> ABORT_P ABSOLUTE_P ACCESS ACTION ADD AFTER
 
335
        AGGREGATE ALL ALSO ALTER ANALYSE ANALYZE AND ANY ARRAY AS ASC
 
336
        ASSERTION ASSIGNMENT AT AUTHORIZATION
 
337
 
 
338
        BACKWARD BEFORE BEGIN_P BETWEEN BIGINT BINARY BIT
 
339
        BOOLEAN_P BOTH BY
 
340
 
 
341
        CACHE CALLED CASCADE CASE CAST CHAIN CHAR_P
 
342
        CHARACTER CHARACTERISTICS CHECK CHECKPOINT CLASS CLOSE
 
343
        CLUSTER COALESCE COLLATE COLUMN COMMENT COMMIT
 
344
        COMMITTED CONSTRAINT CONSTRAINTS CONVERSION_P CONVERT COPY CREATE CREATEDB
 
345
        CREATEUSER CROSS CSV CURRENT_DATE CURRENT_TIME
 
346
        CURRENT_TIMESTAMP CURRENT_USER CURSOR CYCLE
 
347
 
 
348
        DATABASE DAY_P DEALLOCATE DEC DECIMAL_P DECLARE DEFAULT DEFAULTS
 
349
        DEFERRABLE DEFERRED DEFINER DELETE_P DELIMITER DELIMITERS
 
350
        DESC DISTINCT DO DOMAIN_P DOUBLE_P DROP
 
351
 
 
352
        EACH ELSE ENCODING ENCRYPTED END_P ESCAPE EXCEPT EXCLUDING
 
353
        EXCLUSIVE EXECUTE EXISTS EXPLAIN EXTERNAL EXTRACT
 
354
 
 
355
        FALSE_P FETCH FIRST_P FLOAT_P FOR FORCE FOREIGN FORWARD
 
356
        FREEZE FROM FULL FUNCTION
 
357
 
 
358
        GLOBAL GRANT GROUP_P
 
359
 
 
360
        HANDLER HAVING HOLD HOUR_P
 
361
 
 
362
        ILIKE IMMEDIATE IMMUTABLE IMPLICIT_P IN_P INCLUDING INCREMENT
 
363
        INDEX INHERITS INITIALLY INNER_P INOUT INPUT_P
 
364
        INSENSITIVE INSERT INSTEAD INT_P INTEGER INTERSECT
 
365
        INTERVAL INTO INVOKER IS ISNULL ISOLATION
 
366
 
 
367
        JOIN
 
368
 
 
369
        KEY
 
370
 
 
371
        LANCOMPILER LANGUAGE LARGE_P LAST_P LEADING LEFT LEVEL LIKE LIMIT
 
372
        LISTEN LOAD LOCAL LOCALTIME LOCALTIMESTAMP LOCATION
 
373
        LOCK_P
 
374
 
 
375
        MATCH MAXVALUE MINUTE_P MINVALUE MODE MONTH_P MOVE
 
376
 
 
377
        NAMES NATIONAL NATURAL NCHAR NEW NEXT NO NOCREATEDB
 
378
        NOCREATEUSER NONE NOT NOTHING NOTIFY NOTNULL NOWAIT NULL_P
 
379
        NULLIF NUMERIC
 
380
 
 
381
        OBJECT_P OF OFF OFFSET OIDS OLD ON ONLY OPERATOR OPTION OR
 
382
        ORDER OUT_P OUTER_P OVERLAPS OVERLAY OWNER
 
383
 
 
384
        PARTIAL PASSWORD PLACING POSITION
 
385
        PRECISION PRESERVE PREPARE PRIMARY
 
386
        PRIOR PRIVILEGES PROCEDURAL PROCEDURE
 
387
 
 
388
        QUOTE
 
389
 
 
390
        READ REAL RECHECK REFERENCES REINDEX RELATIVE_P RELEASE RENAME
 
391
        REPEATABLE REPLACE RESET RESTART RESTRICT RETURNS REVOKE RIGHT
 
392
        ROLLBACK ROW ROWS RULE
 
393
 
 
394
        SAVEPOINT SCHEMA SCROLL SECOND_P SECURITY SELECT SEQUENCE
 
395
        SERIALIZABLE SESSION SESSION_USER SET SETOF SHARE
 
396
        SHOW SIMILAR SIMPLE SMALLINT SOME STABLE START STATEMENT
 
397
        STATISTICS STDIN STDOUT STORAGE STRICT_P SUBSTRING SYSID
 
398
 
 
399
        TABLE TABLESPACE TEMP TEMPLATE TEMPORARY THEN TIME TIMESTAMP
 
400
        TO TOAST TRAILING TRANSACTION TREAT TRIGGER TRIM TRUE_P
 
401
        TRUNCATE TRUSTED TYPE_P
 
402
 
 
403
        UNCOMMITTED UNENCRYPTED UNION UNIQUE UNKNOWN UNLISTEN UNTIL
 
404
        UPDATE USAGE USER USING
 
405
 
 
406
        VACUUM VALID VALIDATOR VALUES VARCHAR VARYING
 
407
        VERBOSE VIEW VOLATILE
 
408
 
 
409
        WHEN WHERE WITH WITHOUT WORK WRITE
 
410
 
 
411
        YEAR_P
 
412
 
 
413
        ZONE
 
414
 
 
415
/* The grammar thinks these are keywords, but they are not in the keywords.c
 
416
 * list and so can never be entered directly.  The filter in parser.c
 
417
 * creates these tokens when required.
 
418
 */
 
419
%token                  UNIONJOIN
 
420
 
 
421
/* Special token types, not actually keywords - see the "lex" file */
 
422
%token <str>    IDENT FCONST SCONST BCONST XCONST Op
 
423
%token <ival>   ICONST PARAM
 
424
 
 
425
/* precedence: lowest to highest */
 
426
%left           UNION EXCEPT
 
427
%left           INTERSECT
 
428
%left           OR
 
429
%left           AND
 
430
%right          NOT
 
431
%right          '='
 
432
%nonassoc       '<' '>'
 
433
%nonassoc       LIKE ILIKE SIMILAR
 
434
%nonassoc       ESCAPE
 
435
%nonassoc       OVERLAPS
 
436
%nonassoc       BETWEEN
 
437
%nonassoc       IN_P
 
438
%left           POSTFIXOP               /* dummy for postfix Op rules */
 
439
%left           Op OPERATOR             /* multi-character ops and user-defined operators */
 
440
%nonassoc       NOTNULL
 
441
%nonassoc       ISNULL
 
442
%nonassoc       IS NULL_P TRUE_P FALSE_P UNKNOWN /* sets precedence for IS NULL, etc */
 
443
%left           '+' '-'
 
444
%left           '*' '/' '%'
 
445
%left           '^'
 
446
/* Unary Operators */
 
447
%left           AT ZONE                 /* sets precedence for AT TIME ZONE */
 
448
%right          UMINUS
 
449
%left           '[' ']'
 
450
%left           '(' ')'
 
451
%left           TYPECAST
 
452
%left           '.'
 
453
/*
 
454
 * These might seem to be low-precedence, but actually they are not part
 
455
 * of the arithmetic hierarchy at all in their use as JOIN operators.
 
456
 * We make them high-precedence to support their use as function names.
 
457
 * They wouldn't be given a precedence at all, were it not that we need
 
458
 * left-associativity among the JOIN rules themselves.
 
459
 */
 
460
%left           JOIN UNIONJOIN CROSS LEFT FULL RIGHT INNER_P NATURAL
 
461
%%
 
462
 
 
463
/*
 
464
 *      Handle comment-only lines, and ;; SELECT * FROM pg_class ;;;
 
465
 *      psql already handles such cases, but other interfaces don't.
 
466
 *      bjm 1999/10/05
 
467
 */
 
468
stmtblock:      stmtmulti                                                               { parsetree = $1; }
 
469
                ;
 
470
 
 
471
/* the thrashing around here is to discard "empty" statements... */
 
472
stmtmulti:      stmtmulti ';' stmt
 
473
                                { if ($3 != NULL)
 
474
                                        $$ = lappend($1, $3);
 
475
                                  else
 
476
                                        $$ = $1;
 
477
                                }
 
478
                        | stmt
 
479
                                        { if ($1 != NULL)
 
480
                                                $$ = list_make1($1);
 
481
                                          else
 
482
                                                $$ = NIL;
 
483
                                        }
 
484
                ;
 
485
 
 
486
stmt :
 
487
                        AlterDatabaseSetStmt
 
488
                        | AlterDomainStmt
 
489
                        | AlterGroupStmt
 
490
                        | AlterOwnerStmt
 
491
                        | AlterSeqStmt
 
492
                        | AlterTableStmt
 
493
                        | AlterUserSetStmt
 
494
                        | AlterUserStmt
 
495
                        | AnalyzeStmt
 
496
                        | CheckPointStmt
 
497
                        | ClosePortalStmt
 
498
                        | ClusterStmt
 
499
                        | CommentStmt
 
500
                        | ConstraintsSetStmt
 
501
                        | CopyStmt
 
502
                        | CreateAsStmt
 
503
                        | CreateAssertStmt
 
504
                        | CreateCastStmt
 
505
                        | CreateConversionStmt
 
506
                        | CreateDomainStmt
 
507
                        | CreateFunctionStmt
 
508
                        | CreateGroupStmt
 
509
                        | CreateOpClassStmt
 
510
                        | CreatePLangStmt
 
511
                        | CreateSchemaStmt
 
512
                        | CreateSeqStmt
 
513
                        | CreateStmt
 
514
                        | CreateTableSpaceStmt
 
515
                        | CreateTrigStmt
 
516
                        | CreateUserStmt
 
517
                        | CreatedbStmt
 
518
                        | DeallocateStmt
 
519
                        | DeclareCursorStmt
 
520
                        | DefineStmt
 
521
                        | DeleteStmt
 
522
                        | DropAssertStmt
 
523
                        | DropCastStmt
 
524
                        | DropGroupStmt
 
525
                        | DropOpClassStmt
 
526
                        | DropPLangStmt
 
527
                        | DropRuleStmt
 
528
                        | DropStmt
 
529
                        | DropTableSpaceStmt
 
530
                        | DropTrigStmt
 
531
                        | DropUserStmt
 
532
                        | DropdbStmt
 
533
                        | ExecuteStmt
 
534
                        | ExplainStmt
 
535
                        | FetchStmt
 
536
                        | GrantStmt
 
537
                        | IndexStmt
 
538
                        | InsertStmt
 
539
                        | ListenStmt
 
540
                        | LoadStmt
 
541
                        | LockStmt
 
542
                        | NotifyStmt
 
543
                        | PrepareStmt
 
544
                        | ReindexStmt
 
545
                        | RemoveAggrStmt
 
546
                        | RemoveFuncStmt
 
547
                        | RemoveOperStmt
 
548
                        | RenameStmt
 
549
                        | RevokeStmt
 
550
                        | RuleStmt
 
551
                        | SelectStmt
 
552
                        | TransactionStmt
 
553
                        | TruncateStmt
 
554
                        | UnlistenStmt
 
555
                        | UpdateStmt
 
556
                        | VacuumStmt
 
557
                        | VariableResetStmt
 
558
                        | VariableSetStmt
 
559
                        | VariableShowStmt
 
560
                        | ViewStmt
 
561
                        | /*EMPTY*/
 
562
                                { $$ = NULL; }
 
563
                ;
 
564
 
 
565
/*****************************************************************************
 
566
 *
 
567
 * Create a new Postgres DBMS user
 
568
 *
 
569
 *
 
570
 *****************************************************************************/
 
571
 
 
572
CreateUserStmt:
 
573
                        CREATE USER UserId opt_with OptUserList
 
574
                                {
 
575
                                        CreateUserStmt *n = makeNode(CreateUserStmt);
 
576
                                        n->user = $3;
 
577
                                        n->options = $5;
 
578
                                        $$ = (Node *)n;
 
579
                                }
 
580
                ;
 
581
 
 
582
 
 
583
opt_with:       WITH                                                                    {}
 
584
                        | /*EMPTY*/                                                             {}
 
585
                ;
 
586
 
 
587
/*****************************************************************************
 
588
 *
 
589
 * Alter a postgresql DBMS user
 
590
 *
 
591
 *
 
592
 *****************************************************************************/
 
593
 
 
594
AlterUserStmt:
 
595
                        ALTER USER UserId opt_with OptUserList
 
596
                                 {
 
597
                                        AlterUserStmt *n = makeNode(AlterUserStmt);
 
598
                                        n->user = $3;
 
599
                                        n->options = $5;
 
600
                                        $$ = (Node *)n;
 
601
                                 }
 
602
                ;
 
603
 
 
604
 
 
605
AlterUserSetStmt:
 
606
                        ALTER USER UserId SET set_rest
 
607
                                {
 
608
                                        AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
 
609
                                        n->user = $3;
 
610
                                        n->variable = $5->name;
 
611
                                        n->value = $5->args;
 
612
                                        $$ = (Node *)n;
 
613
                                }
 
614
                        | ALTER USER UserId VariableResetStmt
 
615
                                {
 
616
                                        AlterUserSetStmt *n = makeNode(AlterUserSetStmt);
 
617
                                        n->user = $3;
 
618
                                        n->variable = ((VariableResetStmt *)$4)->name;
 
619
                                        n->value = NIL;
 
620
                                        $$ = (Node *)n;
 
621
                                }
 
622
                        ;
 
623
 
 
624
 
 
625
/*****************************************************************************
 
626
 *
 
627
 * Drop a postgresql DBMS user
 
628
 *
 
629
 * XXX Ideally this would have CASCADE/RESTRICT options, but since a user
 
630
 * might own objects in multiple databases, there is presently no way to
 
631
 * implement either cascading or restricting.  Caveat DBA.
 
632
 *****************************************************************************/
 
633
 
 
634
DropUserStmt:
 
635
                        DROP USER user_list
 
636
                                {
 
637
                                        DropUserStmt *n = makeNode(DropUserStmt);
 
638
                                        n->users = $3;
 
639
                                        $$ = (Node *)n;
 
640
                                }
 
641
                        ;
 
642
 
 
643
/*
 
644
 * Options for CREATE USER and ALTER USER
 
645
 */
 
646
OptUserList:
 
647
                        OptUserList OptUserElem                                 { $$ = lappend($1, $2); }
 
648
                        | /* EMPTY */                                                   { $$ = NIL; }
 
649
                ;
 
650
 
 
651
OptUserElem:
 
652
                        PASSWORD Sconst
 
653
                                {
 
654
                                        $$ = makeDefElem("password", (Node *)makeString($2));
 
655
                                }
 
656
                        | ENCRYPTED PASSWORD Sconst
 
657
                                {
 
658
                                        $$ = makeDefElem("encryptedPassword", (Node *)makeString($3));
 
659
                                }
 
660
                        | UNENCRYPTED PASSWORD Sconst
 
661
                                {
 
662
                                        $$ = makeDefElem("unencryptedPassword", (Node *)makeString($3));
 
663
                                }
 
664
                        | SYSID Iconst
 
665
                                {
 
666
                                        $$ = makeDefElem("sysid", (Node *)makeInteger($2));
 
667
                                }
 
668
                        | CREATEDB
 
669
                                {
 
670
                                        $$ = makeDefElem("createdb", (Node *)makeInteger(TRUE));
 
671
                                }
 
672
                        | NOCREATEDB
 
673
                                {
 
674
                                        $$ = makeDefElem("createdb", (Node *)makeInteger(FALSE));
 
675
                                }
 
676
                        | CREATEUSER
 
677
                                {
 
678
                                        $$ = makeDefElem("createuser", (Node *)makeInteger(TRUE));
 
679
                                }
 
680
                        | NOCREATEUSER
 
681
                                {
 
682
                                        $$ = makeDefElem("createuser", (Node *)makeInteger(FALSE));
 
683
                                }
 
684
                        | IN_P GROUP_P user_list
 
685
                                {
 
686
                                        $$ = makeDefElem("groupElts", (Node *)$3);
 
687
                                }
 
688
                        | VALID UNTIL Sconst
 
689
                                {
 
690
                                        $$ = makeDefElem("validUntil", (Node *)makeString($3));
 
691
                                }
 
692
                ;
 
693
 
 
694
user_list:      user_list ',' UserId            { $$ = lappend($1, makeString($3)); }
 
695
                        | UserId                                        { $$ = list_make1(makeString($1)); }
 
696
                ;
 
697
 
 
698
 
 
699
 
 
700
/*****************************************************************************
 
701
 *
 
702
 * Create a postgresql group
 
703
 *
 
704
 *
 
705
 *****************************************************************************/
 
706
 
 
707
CreateGroupStmt:
 
708
                        CREATE GROUP_P UserId opt_with OptGroupList
 
709
                                {
 
710
                                        CreateGroupStmt *n = makeNode(CreateGroupStmt);
 
711
                                        n->name = $3;
 
712
                                        n->options = $5;
 
713
                                        $$ = (Node *)n;
 
714
                                }
 
715
                ;
 
716
 
 
717
/*
 
718
 * Options for CREATE GROUP
 
719
 */
 
720
OptGroupList:
 
721
                        OptGroupList OptGroupElem                               { $$ = lappend($1, $2); }
 
722
                        | /* EMPTY */                                                   { $$ = NIL; }
 
723
                ;
 
724
 
 
725
OptGroupElem:
 
726
                        USER user_list
 
727
                                {
 
728
                                        $$ = makeDefElem("userElts", (Node *)$2);
 
729
                                }
 
730
                        | SYSID Iconst
 
731
                                {
 
732
                                        $$ = makeDefElem("sysid", (Node *)makeInteger($2));
 
733
                                }
 
734
                ;
 
735
 
 
736
 
 
737
/*****************************************************************************
 
738
 *
 
739
 * Alter a postgresql group
 
740
 *
 
741
 *
 
742
 *****************************************************************************/
 
743
 
 
744
AlterGroupStmt:
 
745
                        ALTER GROUP_P UserId add_drop USER user_list
 
746
                                {
 
747
                                        AlterGroupStmt *n = makeNode(AlterGroupStmt);
 
748
                                        n->name = $3;
 
749
                                        n->action = $4;
 
750
                                        n->listUsers = $6;
 
751
                                        $$ = (Node *)n;
 
752
                                }
 
753
                ;
 
754
 
 
755
add_drop:       ADD                                                                             { $$ = +1; }
 
756
                        | DROP                                                                  { $$ = -1; }
 
757
                ;
 
758
 
 
759
 
 
760
/*****************************************************************************
 
761
 *
 
762
 * Drop a postgresql group
 
763
 *
 
764
 * XXX see above notes about cascading DROP USER; groups have same problem.
 
765
 *****************************************************************************/
 
766
 
 
767
DropGroupStmt:
 
768
                        DROP GROUP_P UserId
 
769
                                {
 
770
                                        DropGroupStmt *n = makeNode(DropGroupStmt);
 
771
                                        n->name = $3;
 
772
                                        $$ = (Node *)n;
 
773
                                }
 
774
                ;
 
775
 
 
776
 
 
777
/*****************************************************************************
 
778
 *
 
779
 * Manipulate a schema
 
780
 *
 
781
 *****************************************************************************/
 
782
 
 
783
CreateSchemaStmt:
 
784
                        CREATE SCHEMA OptSchemaName AUTHORIZATION UserId OptSchemaEltList
 
785
                                {
 
786
                                        CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
 
787
                                        /* One can omit the schema name or the authorization id. */
 
788
                                        if ($3 != NULL)
 
789
                                                n->schemaname = $3;
 
790
                                        else
 
791
                                                n->schemaname = $5;
 
792
                                        n->authid = $5;
 
793
                                        n->schemaElts = $6;
 
794
                                        $$ = (Node *)n;
 
795
                                }
 
796
                        | CREATE SCHEMA ColId OptSchemaEltList
 
797
                                {
 
798
                                        CreateSchemaStmt *n = makeNode(CreateSchemaStmt);
 
799
                                        /* ...but not both */
 
800
                                        n->schemaname = $3;
 
801
                                        n->authid = NULL;
 
802
                                        n->schemaElts = $4;
 
803
                                        $$ = (Node *)n;
 
804
                                }
 
805
                ;
 
806
 
 
807
OptSchemaName:
 
808
                        ColId                                                                   { $$ = $1; }
 
809
                        | /* EMPTY */                                                   { $$ = NULL; }
 
810
                ;
 
811
 
 
812
OptSchemaEltList:
 
813
                        OptSchemaEltList schema_stmt                    { $$ = lappend($1, $2); }
 
814
                        | /* EMPTY */                                                   { $$ = NIL; }
 
815
                ;
 
816
 
 
817
/*
 
818
 *      schema_stmt are the ones that can show up inside a CREATE SCHEMA
 
819
 *      statement (in addition to by themselves).
 
820
 */
 
821
schema_stmt:
 
822
                        CreateStmt
 
823
                        | IndexStmt
 
824
                        | CreateSeqStmt
 
825
                        | CreateTrigStmt
 
826
                        | GrantStmt
 
827
                        | ViewStmt
 
828
                ;
 
829
 
 
830
 
 
831
/*****************************************************************************
 
832
 *
 
833
 * Set PG internal variable
 
834
 *        SET name TO 'var_value'
 
835
 * Include SQL92 syntax (thomas 1997-10-22):
 
836
 *        SET TIME ZONE 'var_value'
 
837
 *
 
838
 *****************************************************************************/
 
839
 
 
840
VariableSetStmt:
 
841
                        SET set_rest
 
842
                                {
 
843
                                        VariableSetStmt *n = $2;
 
844
                                        n->is_local = false;
 
845
                                        $$ = (Node *) n;
 
846
                                }
 
847
                        | SET LOCAL set_rest
 
848
                                {
 
849
                                        VariableSetStmt *n = $3;
 
850
                                        n->is_local = true;
 
851
                                        $$ = (Node *) n;
 
852
                                }
 
853
                        | SET SESSION set_rest
 
854
                                {
 
855
                                        VariableSetStmt *n = $3;
 
856
                                        n->is_local = false;
 
857
                                        $$ = (Node *) n;
 
858
                                }
 
859
                ;
 
860
 
 
861
set_rest:  var_name TO var_list_or_default
 
862
                                {
 
863
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
864
                                        n->name = $1;
 
865
                                        n->args = $3;
 
866
                                        $$ = n;
 
867
                                }
 
868
                        | var_name '=' var_list_or_default
 
869
                                {
 
870
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
871
                                        n->name = $1;
 
872
                                        n->args = $3;
 
873
                                        $$ = n;
 
874
                                }
 
875
                        | TIME ZONE zone_value
 
876
                                {
 
877
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
878
                                        n->name = "timezone";
 
879
                                        if ($3 != NULL)
 
880
                                                n->args = list_make1($3);
 
881
                                        $$ = n;
 
882
                                }
 
883
                        | TRANSACTION transaction_mode_list
 
884
                                {
 
885
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
886
                                        n->name = "TRANSACTION";
 
887
                                        n->args = $2;
 
888
                                        $$ = n;
 
889
                                }
 
890
                        | SESSION CHARACTERISTICS AS TRANSACTION transaction_mode_list
 
891
                                {
 
892
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
893
                                        n->name = "SESSION CHARACTERISTICS";
 
894
                                        n->args = $5;
 
895
                                        $$ = n;
 
896
                                }
 
897
                        | NAMES opt_encoding
 
898
                                {
 
899
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
900
                                        n->name = "client_encoding";
 
901
                                        if ($2 != NULL)
 
902
                                                n->args = list_make1(makeStringConst($2, NULL));
 
903
                                        $$ = n;
 
904
                                }
 
905
                        | SESSION AUTHORIZATION ColId_or_Sconst
 
906
                                {
 
907
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
908
                                        n->name = "session_authorization";
 
909
                                        n->args = list_make1(makeStringConst($3, NULL));
 
910
                                        $$ = n;
 
911
                                }
 
912
                        | SESSION AUTHORIZATION DEFAULT
 
913
                                {
 
914
                                        VariableSetStmt *n = makeNode(VariableSetStmt);
 
915
                                        n->name = "session_authorization";
 
916
                                        n->args = NIL;
 
917
                                        $$ = n;
 
918
                                }
 
919
                ;
 
920
 
 
921
var_name:
 
922
                        ColId                                                           { $$ = $1; }
 
923
                        | var_name '.' ColId
 
924
                                {
 
925
                                        int qLen = strlen($1);
 
926
                                        char* qualName = palloc(qLen + strlen($3) + 2);
 
927
                                        strcpy(qualName, $1);
 
928
                                        qualName[qLen] = '.';
 
929
                                        strcpy(qualName + qLen + 1, $3);
 
930
                                        $$ = qualName;
 
931
                                }
 
932
                ;
 
933
 
 
934
var_list_or_default:
 
935
                        var_list                                                                { $$ = $1; }
 
936
                        | DEFAULT                                                               { $$ = NIL; }
 
937
                ;
 
938
 
 
939
var_list:       var_value                                                               { $$ = list_make1($1); }
 
940
                        | var_list ',' var_value                                { $$ = lappend($1, $3); }
 
941
                ;
 
942
 
 
943
var_value:      opt_boolean
 
944
                                { $$ = makeStringConst($1, NULL); }
 
945
                        | ColId_or_Sconst
 
946
                                { $$ = makeStringConst($1, NULL); }
 
947
                        | NumericOnly
 
948
                                { $$ = makeAConst($1); }
 
949
                ;
 
950
 
 
951
iso_level:      READ UNCOMMITTED                                                { $$ = "read uncommitted"; }
 
952
                        | READ COMMITTED                                                { $$ = "read committed"; }
 
953
                        | REPEATABLE READ                                               { $$ = "repeatable read"; }
 
954
                        | SERIALIZABLE                                                  { $$ = "serializable"; }
 
955
                ;
 
956
 
 
957
opt_boolean:
 
958
                        TRUE_P                                                                  { $$ = "true"; }
 
959
                        | FALSE_P                                                               { $$ = "false"; }
 
960
                        | ON                                                                    { $$ = "on"; }
 
961
                        | OFF                                                                   { $$ = "off"; }
 
962
                ;
 
963
 
 
964
/* Timezone values can be:
 
965
 * - a string such as 'pst8pdt'
 
966
 * - an identifier such as "pst8pdt"
 
967
 * - an integer or floating point number
 
968
 * - a time interval per SQL99
 
969
 * ColId gives reduce/reduce errors against ConstInterval and LOCAL,
 
970
 * so use IDENT and reject anything which is a reserved word.
 
971
 */
 
972
zone_value:
 
973
                        Sconst
 
974
                                {
 
975
                                        $$ = makeStringConst($1, NULL);
 
976
                                }
 
977
                        | IDENT
 
978
                                {
 
979
                                        $$ = makeStringConst($1, NULL);
 
980
                                }
 
981
                        | ConstInterval Sconst opt_interval
 
982
                                {
 
983
                                        A_Const *n = (A_Const *) makeStringConst($2, $1);
 
984
                                        if ($3 != INTERVAL_FULL_RANGE)
 
985
                                        {
 
986
                                                if (($3 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0)
 
987
                                                        ereport(ERROR,
 
988
                                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
989
                                                                         errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
 
990
                                                n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
 
991
                                        }
 
992
                                        $$ = (Node *)n;
 
993
                                }
 
994
                        | ConstInterval '(' Iconst ')' Sconst opt_interval
 
995
                                {
 
996
                                        A_Const *n = (A_Const *) makeStringConst($5, $1);
 
997
                                        if ($3 < 0)
 
998
                                                ereport(ERROR,
 
999
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
1000
                                                                 errmsg("INTERVAL(%d) precision must not be negative",
 
1001
                                                                                $3)));
 
1002
                                        if ($3 > MAX_INTERVAL_PRECISION)
 
1003
                                        {
 
1004
                                                ereport(WARNING,
 
1005
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
1006
                                                                 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
 
1007
                                                                                $3, MAX_INTERVAL_PRECISION)));
 
1008
                                                $3 = MAX_INTERVAL_PRECISION;
 
1009
                                        }
 
1010
 
 
1011
                                        if (($6 != INTERVAL_FULL_RANGE)
 
1012
                                                && (($6 & ~(INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE))) != 0))
 
1013
                                                ereport(ERROR,
 
1014
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
1015
                                                                 errmsg("time zone interval must be HOUR or HOUR TO MINUTE")));
 
1016
 
 
1017
                                        n->typename->typmod = INTERVAL_TYPMOD($3, $6);
 
1018
 
 
1019
                                        $$ = (Node *)n;
 
1020
                                }
 
1021
                        | NumericOnly                                                   { $$ = makeAConst($1); }
 
1022
                        | DEFAULT                                                               { $$ = NULL; }
 
1023
                        | LOCAL                                                                 { $$ = NULL; }
 
1024
                ;
 
1025
 
 
1026
opt_encoding:
 
1027
                        Sconst                                                                  { $$ = $1; }
 
1028
                        | DEFAULT                                                               { $$ = NULL; }
 
1029
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1030
                ;
 
1031
 
 
1032
ColId_or_Sconst:
 
1033
                        ColId                                                                   { $$ = $1; }
 
1034
                        | SCONST                                                                { $$ = $1; }
 
1035
                ;
 
1036
 
 
1037
 
 
1038
VariableShowStmt:
 
1039
                        SHOW ColId
 
1040
                                {
 
1041
                                        VariableShowStmt *n = makeNode(VariableShowStmt);
 
1042
                                        n->name = $2;
 
1043
                                        $$ = (Node *) n;
 
1044
                                }
 
1045
                        | SHOW TIME ZONE
 
1046
                                {
 
1047
                                        VariableShowStmt *n = makeNode(VariableShowStmt);
 
1048
                                        n->name = "timezone";
 
1049
                                        $$ = (Node *) n;
 
1050
                                }
 
1051
                        | SHOW TRANSACTION ISOLATION LEVEL
 
1052
                                {
 
1053
                                        VariableShowStmt *n = makeNode(VariableShowStmt);
 
1054
                                        n->name = "transaction_isolation";
 
1055
                                        $$ = (Node *) n;
 
1056
                                }
 
1057
                        | SHOW SESSION AUTHORIZATION
 
1058
                                {
 
1059
                                        VariableShowStmt *n = makeNode(VariableShowStmt);
 
1060
                                        n->name = "session_authorization";
 
1061
                                        $$ = (Node *) n;
 
1062
                                }
 
1063
                        | SHOW ALL
 
1064
                                {
 
1065
                                        VariableShowStmt *n = makeNode(VariableShowStmt);
 
1066
                                        n->name = "all";
 
1067
                                        $$ = (Node *) n;
 
1068
                                }
 
1069
                ;
 
1070
 
 
1071
VariableResetStmt:
 
1072
                        RESET ColId
 
1073
                                {
 
1074
                                        VariableResetStmt *n = makeNode(VariableResetStmt);
 
1075
                                        n->name = $2;
 
1076
                                        $$ = (Node *) n;
 
1077
                                }
 
1078
                        | RESET TIME ZONE
 
1079
                                {
 
1080
                                        VariableResetStmt *n = makeNode(VariableResetStmt);
 
1081
                                        n->name = "timezone";
 
1082
                                        $$ = (Node *) n;
 
1083
                                }
 
1084
                        | RESET TRANSACTION ISOLATION LEVEL
 
1085
                                {
 
1086
                                        VariableResetStmt *n = makeNode(VariableResetStmt);
 
1087
                                        n->name = "transaction_isolation";
 
1088
                                        $$ = (Node *) n;
 
1089
                                }
 
1090
                        | RESET SESSION AUTHORIZATION
 
1091
                                {
 
1092
                                        VariableResetStmt *n = makeNode(VariableResetStmt);
 
1093
                                        n->name = "session_authorization";
 
1094
                                        $$ = (Node *) n;
 
1095
                                }
 
1096
                        | RESET ALL
 
1097
                                {
 
1098
                                        VariableResetStmt *n = makeNode(VariableResetStmt);
 
1099
                                        n->name = "all";
 
1100
                                        $$ = (Node *) n;
 
1101
                                }
 
1102
                ;
 
1103
 
 
1104
 
 
1105
ConstraintsSetStmt:
 
1106
                        SET CONSTRAINTS constraints_set_list constraints_set_mode
 
1107
                                {
 
1108
                                        ConstraintsSetStmt *n = makeNode(ConstraintsSetStmt);
 
1109
                                        n->constraints = $3;
 
1110
                                        n->deferred    = $4;
 
1111
                                        $$ = (Node *) n;
 
1112
                                }
 
1113
                ;
 
1114
 
 
1115
constraints_set_list:
 
1116
                        ALL                                                                             { $$ = NIL; }
 
1117
                        | name_list                                                             { $$ = $1; }
 
1118
                ;
 
1119
 
 
1120
constraints_set_mode:
 
1121
                        DEFERRED                                                                { $$ = TRUE; }
 
1122
                        | IMMEDIATE                                                             { $$ = FALSE; }
 
1123
                ;
 
1124
 
 
1125
 
 
1126
/*
 
1127
 * Checkpoint statement
 
1128
 */
 
1129
CheckPointStmt:
 
1130
                        CHECKPOINT
 
1131
                                {
 
1132
                                        CheckPointStmt *n = makeNode(CheckPointStmt);
 
1133
                                        $$ = (Node *)n;
 
1134
                                }
 
1135
                ;
 
1136
 
 
1137
 
 
1138
/*****************************************************************************
 
1139
 *
 
1140
 *      ALTER [ TABLE | INDEX ] variations
 
1141
 *
 
1142
 *****************************************************************************/
 
1143
 
 
1144
AlterTableStmt:
 
1145
                        ALTER TABLE relation_expr alter_table_cmds
 
1146
                                {
 
1147
                                        AlterTableStmt *n = makeNode(AlterTableStmt);
 
1148
                                        n->relation = $3;
 
1149
                                        n->cmds = $4;
 
1150
                                        n->relkind = OBJECT_TABLE; 
 
1151
                                        $$ = (Node *)n;
 
1152
                                }
 
1153
                |       ALTER INDEX relation_expr alter_rel_cmds
 
1154
                                {
 
1155
                                        AlterTableStmt *n = makeNode(AlterTableStmt);
 
1156
                                        n->relation = $3;
 
1157
                                        n->cmds = $4;
 
1158
                                        n->relkind = OBJECT_INDEX;
 
1159
                                        $$ = (Node *)n;
 
1160
                                }
 
1161
                ;
 
1162
 
 
1163
alter_table_cmds:
 
1164
                        alter_table_cmd                                                 { $$ = list_make1($1); }
 
1165
                        | alter_table_cmds ',' alter_table_cmd  { $$ = lappend($1, $3); }
 
1166
                ;
 
1167
 
 
1168
/* Subcommands that are for ALTER TABLE only */
 
1169
alter_table_cmd:
 
1170
                        /* ALTER TABLE <relation> ADD [COLUMN] <coldef> */
 
1171
                        ADD opt_column columnDef
 
1172
                                {
 
1173
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1174
                                        n->subtype = AT_AddColumn;
 
1175
                                        n->def = $3;
 
1176
                                        $$ = (Node *)n;
 
1177
                                }
 
1178
                        /* ALTER TABLE <relation> ALTER [COLUMN] <colname> {SET DEFAULT <expr>|DROP DEFAULT} */
 
1179
                        | ALTER opt_column ColId alter_column_default
 
1180
                                {
 
1181
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1182
                                        n->subtype = AT_ColumnDefault;
 
1183
                                        n->name = $3;
 
1184
                                        n->def = $4;
 
1185
                                        $$ = (Node *)n;
 
1186
                                }
 
1187
                        /* ALTER TABLE <relation> ALTER [COLUMN] <colname> DROP NOT NULL */
 
1188
                        | ALTER opt_column ColId DROP NOT NULL_P
 
1189
                                {
 
1190
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1191
                                        n->subtype = AT_DropNotNull;
 
1192
                                        n->name = $3;
 
1193
                                        $$ = (Node *)n;
 
1194
                                }
 
1195
                        /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET NOT NULL */
 
1196
                        | ALTER opt_column ColId SET NOT NULL_P
 
1197
                                {
 
1198
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1199
                                        n->subtype = AT_SetNotNull;
 
1200
                                        n->name = $3;
 
1201
                                        $$ = (Node *)n;
 
1202
                                }
 
1203
                        /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STATISTICS <IntegerOnly> */
 
1204
                        | ALTER opt_column ColId SET STATISTICS IntegerOnly
 
1205
                                {
 
1206
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1207
                                        n->subtype = AT_SetStatistics;
 
1208
                                        n->name = $3;
 
1209
                                        n->def = (Node *) $6;
 
1210
                                        $$ = (Node *)n;
 
1211
                                }
 
1212
                        /* ALTER TABLE <relation> ALTER [COLUMN] <colname> SET STORAGE <storagemode> */
 
1213
                        | ALTER opt_column ColId SET STORAGE ColId
 
1214
                                {
 
1215
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1216
                                        n->subtype = AT_SetStorage;
 
1217
                                        n->name = $3;
 
1218
                                        n->def = (Node *) makeString($6);
 
1219
                                        $$ = (Node *)n;
 
1220
                                }
 
1221
                        /* ALTER TABLE <relation> DROP [COLUMN] <colname> [RESTRICT|CASCADE] */
 
1222
                        | DROP opt_column ColId opt_drop_behavior
 
1223
                                {
 
1224
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1225
                                        n->subtype = AT_DropColumn;
 
1226
                                        n->name = $3;
 
1227
                                        n->behavior = $4;
 
1228
                                        $$ = (Node *)n;
 
1229
                                }
 
1230
                        /*
 
1231
                         * ALTER TABLE <relation> ALTER [COLUMN] <colname> TYPE <typename>
 
1232
                         *              [ USING <expression> ]
 
1233
                         */
 
1234
                        | ALTER opt_column ColId TYPE_P Typename alter_using
 
1235
                                {
 
1236
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1237
                                        n->subtype = AT_AlterColumnType;
 
1238
                                        n->name = $3;
 
1239
                                        n->def = (Node *) $5;
 
1240
                                        n->transform = $6;
 
1241
                                        $$ = (Node *)n;
 
1242
                                }
 
1243
                        /* ALTER TABLE <relation> ADD CONSTRAINT ... */
 
1244
                        | ADD TableConstraint
 
1245
                                {
 
1246
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1247
                                        n->subtype = AT_AddConstraint;
 
1248
                                        n->def = $2;
 
1249
                                        $$ = (Node *)n;
 
1250
                                }
 
1251
                        /* ALTER TABLE <relation> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
 
1252
                        | DROP CONSTRAINT name opt_drop_behavior
 
1253
                                {
 
1254
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1255
                                        n->subtype = AT_DropConstraint;
 
1256
                                        n->name = $3;
 
1257
                                        n->behavior = $4;
 
1258
                                        $$ = (Node *)n;
 
1259
                                }
 
1260
                        /* ALTER TABLE <relation> SET WITHOUT OIDS  */
 
1261
                        | SET WITHOUT OIDS
 
1262
                                {
 
1263
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1264
                                        n->subtype = AT_DropOids;
 
1265
                                        $$ = (Node *)n;
 
1266
                                }
 
1267
                        /* ALTER TABLE <name> CREATE TOAST TABLE -- ONLY */
 
1268
                        | CREATE TOAST TABLE
 
1269
                                {
 
1270
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1271
                                        n->subtype = AT_ToastTable;
 
1272
                                        $$ = (Node *)n;
 
1273
                                }
 
1274
                        /* ALTER TABLE <name> CLUSTER ON <indexname> */
 
1275
                        | CLUSTER ON name
 
1276
                                {
 
1277
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1278
                                        n->subtype = AT_ClusterOn;
 
1279
                                        n->name = $3;
 
1280
                                        $$ = (Node *)n;
 
1281
                                }
 
1282
                        /* ALTER TABLE <name> SET WITHOUT CLUSTER */
 
1283
                        | SET WITHOUT CLUSTER
 
1284
                                {
 
1285
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1286
                                        n->subtype = AT_DropCluster;
 
1287
                                        n->name = NULL;
 
1288
                                        $$ = (Node *)n;
 
1289
                                }
 
1290
                        | alter_rel_cmd
 
1291
                                {
 
1292
                                        $$ = $1;
 
1293
                                }
 
1294
                ;
 
1295
 
 
1296
alter_rel_cmds:
 
1297
                        alter_rel_cmd                                                   { $$ = list_make1($1); }
 
1298
                        | alter_rel_cmds ',' alter_rel_cmd              { $$ = lappend($1, $3); }
 
1299
                ;
 
1300
 
 
1301
/* Subcommands that are for ALTER TABLE or ALTER INDEX */
 
1302
alter_rel_cmd:
 
1303
                        /* ALTER [TABLE|INDEX] <name> OWNER TO UserId */
 
1304
                        OWNER TO UserId
 
1305
                                {
 
1306
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1307
                                        n->subtype = AT_ChangeOwner;
 
1308
                                        n->name = $3;
 
1309
                                        $$ = (Node *)n;
 
1310
                                }
 
1311
                        /* ALTER [TABLE|INDEX] <name> SET TABLESPACE <tablespacename> */
 
1312
                        | SET TABLESPACE name
 
1313
                                {
 
1314
                                        AlterTableCmd *n = makeNode(AlterTableCmd);
 
1315
                                        n->subtype = AT_SetTableSpace;
 
1316
                                        n->name = $3;
 
1317
                                        $$ = (Node *)n;
 
1318
                                }
 
1319
                ;
 
1320
 
 
1321
alter_column_default:
 
1322
                        SET DEFAULT a_expr
 
1323
                                {
 
1324
                                        /* Treat SET DEFAULT NULL the same as DROP DEFAULT */
 
1325
                                        if (exprIsNullConstant($3))
 
1326
                                                $$ = NULL;
 
1327
                                        else
 
1328
                                                $$ = $3;
 
1329
                                }
 
1330
                        | DROP DEFAULT                          { $$ = NULL; }
 
1331
                ;
 
1332
 
 
1333
opt_drop_behavior:
 
1334
                        CASCADE                                         { $$ = DROP_CASCADE; }
 
1335
                        | RESTRICT                                      { $$ = DROP_RESTRICT; }
 
1336
                        | /* EMPTY */                           { $$ = DROP_RESTRICT; /* default */ }
 
1337
                ;
 
1338
 
 
1339
alter_using:
 
1340
                        USING a_expr                            { $$ = $2; }
 
1341
                        | /* EMPTY */                           { $$ = NULL; }
 
1342
                ;
 
1343
 
 
1344
 
 
1345
 
 
1346
/*****************************************************************************
 
1347
 *
 
1348
 *              QUERY :
 
1349
 *                              close <portalname>
 
1350
 *
 
1351
 *****************************************************************************/
 
1352
 
 
1353
ClosePortalStmt:
 
1354
                        CLOSE name
 
1355
                                {
 
1356
                                        ClosePortalStmt *n = makeNode(ClosePortalStmt);
 
1357
                                        n->portalname = $2;
 
1358
                                        $$ = (Node *)n;
 
1359
                                }
 
1360
                ;
 
1361
 
 
1362
 
 
1363
/*****************************************************************************
 
1364
 *
 
1365
 *              QUERY :
 
1366
 *                              COPY <relname> ['(' columnList ')'] FROM/TO [WITH options]
 
1367
 *
 
1368
 *                              BINARY, OIDS, and DELIMITERS kept in old locations
 
1369
 *                              for backward compatibility.  2002-06-18
 
1370
 *
 
1371
 *****************************************************************************/
 
1372
 
 
1373
CopyStmt:       COPY opt_binary qualified_name opt_column_list opt_oids
 
1374
                        copy_from copy_file_name copy_delimiter opt_with copy_opt_list
 
1375
                                {
 
1376
                                        CopyStmt *n = makeNode(CopyStmt);
 
1377
                                        n->relation = $3;
 
1378
                                        n->attlist = $4;
 
1379
                                        n->is_from = $6;
 
1380
                                        n->filename = $7;
 
1381
 
 
1382
                                        n->options = NIL;
 
1383
                                        /* Concatenate user-supplied flags */
 
1384
                                        if ($2)
 
1385
                                                n->options = lappend(n->options, $2);
 
1386
                                        if ($5)
 
1387
                                                n->options = lappend(n->options, $5);
 
1388
                                        if ($8)
 
1389
                                                n->options = lappend(n->options, $8);
 
1390
                                        if ($10)
 
1391
                                                n->options = list_concat(n->options, $10);
 
1392
                                        $$ = (Node *)n;
 
1393
                                }
 
1394
                ;
 
1395
 
 
1396
copy_from:
 
1397
                        FROM                                                                    { $$ = TRUE; }
 
1398
                        | TO                                                                    { $$ = FALSE; }
 
1399
                ;
 
1400
 
 
1401
/*
 
1402
 * copy_file_name NULL indicates stdio is used. Whether stdin or stdout is
 
1403
 * used depends on the direction. (It really doesn't make sense to copy from
 
1404
 * stdout. We silently correct the "typo".               - AY 9/94
 
1405
 */
 
1406
copy_file_name:
 
1407
                        Sconst                                                                  { $$ = $1; }
 
1408
                        | STDIN                                                                 { $$ = NULL; }
 
1409
                        | STDOUT                                                                { $$ = NULL; }
 
1410
                ;
 
1411
 
 
1412
 
 
1413
 
 
1414
copy_opt_list:
 
1415
                        copy_opt_list copy_opt_item                             { $$ = lappend($1, $2); }
 
1416
                        | /* EMPTY */                                                   { $$ = NIL; }
 
1417
                ;
 
1418
 
 
1419
 
 
1420
copy_opt_item:
 
1421
                        BINARY
 
1422
                                {
 
1423
                                        $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
 
1424
                                }
 
1425
                        | OIDS
 
1426
                                {
 
1427
                                        $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
 
1428
                                }
 
1429
                        | DELIMITER opt_as Sconst
 
1430
                                {
 
1431
                                        $$ = makeDefElem("delimiter", (Node *)makeString($3));
 
1432
                                }
 
1433
                        | NULL_P opt_as Sconst
 
1434
                                {
 
1435
                                        $$ = makeDefElem("null", (Node *)makeString($3));
 
1436
                                }
 
1437
                        | CSV
 
1438
                                {
 
1439
                                        $$ = makeDefElem("csv", (Node *)makeInteger(TRUE));
 
1440
                                }
 
1441
                        | QUOTE opt_as Sconst
 
1442
                                {
 
1443
                                        $$ = makeDefElem("quote", (Node *)makeString($3));
 
1444
                                }
 
1445
                        | ESCAPE opt_as Sconst
 
1446
                                {
 
1447
                                        $$ = makeDefElem("escape", (Node *)makeString($3));
 
1448
                                }
 
1449
                        | FORCE QUOTE columnList
 
1450
                                {
 
1451
                                        $$ = makeDefElem("force_quote", (Node *)$3);
 
1452
                                }
 
1453
                        | FORCE NOT NULL_P columnList
 
1454
                                {
 
1455
                                        $$ = makeDefElem("force_notnull", (Node *)$4);
 
1456
                                }
 
1457
                ;
 
1458
 
 
1459
/* The following exist for backward compatibility */
 
1460
 
 
1461
opt_binary:
 
1462
                        BINARY
 
1463
                                {
 
1464
                                        $$ = makeDefElem("binary", (Node *)makeInteger(TRUE));
 
1465
                                }
 
1466
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1467
                ;
 
1468
 
 
1469
opt_oids:
 
1470
                        WITH OIDS
 
1471
                                {
 
1472
                                        $$ = makeDefElem("oids", (Node *)makeInteger(TRUE));
 
1473
                                }
 
1474
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1475
                ;
 
1476
 
 
1477
copy_delimiter:
 
1478
                        /* USING DELIMITERS kept for backward compatibility. 2002-06-15 */
 
1479
                        opt_using DELIMITERS Sconst
 
1480
                                {
 
1481
                                        $$ = makeDefElem("delimiter", (Node *)makeString($3));
 
1482
                                }
 
1483
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1484
                ;
 
1485
 
 
1486
opt_using:
 
1487
                        USING                                                                   {}
 
1488
                        | /*EMPTY*/                                                             {}
 
1489
                ;
 
1490
 
 
1491
 
 
1492
/*****************************************************************************
 
1493
 *
 
1494
 *              QUERY :
 
1495
 *                              CREATE TABLE relname
 
1496
 *
 
1497
 *****************************************************************************/
 
1498
 
 
1499
CreateStmt:     CREATE OptTemp TABLE qualified_name '(' OptTableElementList ')'
 
1500
                        OptInherit OptWithOids OnCommitOption OptTableSpace
 
1501
                                {
 
1502
                                        CreateStmt *n = makeNode(CreateStmt);
 
1503
                                        $4->istemp = $2;
 
1504
                                        n->relation = $4;
 
1505
                                        n->tableElts = $6;
 
1506
                                        n->inhRelations = $8;
 
1507
                                        n->constraints = NIL;
 
1508
                                        n->hasoids = $9;
 
1509
                                        n->oncommit = $10;
 
1510
                                        n->tablespacename = $11;
 
1511
                                        $$ = (Node *)n;
 
1512
                                }
 
1513
                | CREATE OptTemp TABLE qualified_name OF qualified_name
 
1514
                        '(' OptTableElementList ')' OptWithOids OnCommitOption OptTableSpace
 
1515
                                {
 
1516
                                        /* SQL99 CREATE TABLE OF <UDT> (cols) seems to be satisfied
 
1517
                                         * by our inheritance capabilities. Let's try it...
 
1518
                                         */
 
1519
                                        CreateStmt *n = makeNode(CreateStmt);
 
1520
                                        $4->istemp = $2;
 
1521
                                        n->relation = $4;
 
1522
                                        n->tableElts = $8;
 
1523
                                        n->inhRelations = list_make1($6);
 
1524
                                        n->constraints = NIL;
 
1525
                                        n->hasoids = $10;
 
1526
                                        n->oncommit = $11;
 
1527
                                        n->tablespacename = $12;
 
1528
                                        $$ = (Node *)n;
 
1529
                                }
 
1530
                ;
 
1531
 
 
1532
/*
 
1533
 * Redundancy here is needed to avoid shift/reduce conflicts,
 
1534
 * since TEMP is not a reserved word.  See also OptTempTableName.
 
1535
 *
 
1536
 * NOTE: we accept both GLOBAL and LOCAL options; since we have no modules
 
1537
 * the LOCAL keyword is really meaningless.
 
1538
 */
 
1539
OptTemp:        TEMPORARY                                               { $$ = TRUE; }
 
1540
                        | TEMP                                                  { $$ = TRUE; }
 
1541
                        | LOCAL TEMPORARY                               { $$ = TRUE; }
 
1542
                        | LOCAL TEMP                                    { $$ = TRUE; }
 
1543
                        | GLOBAL TEMPORARY                              { $$ = TRUE; }
 
1544
                        | GLOBAL TEMP                                   { $$ = TRUE; }
 
1545
                        | /*EMPTY*/                                             { $$ = FALSE; }
 
1546
                ;
 
1547
 
 
1548
OptTableElementList:
 
1549
                        TableElementList                                        { $$ = $1; }
 
1550
                        | /*EMPTY*/                                                     { $$ = NIL; }
 
1551
                ;
 
1552
 
 
1553
TableElementList:
 
1554
                        TableElement
 
1555
                                {
 
1556
                                        $$ = list_make1($1);
 
1557
                                }
 
1558
                        | TableElementList ',' TableElement
 
1559
                                {
 
1560
                                        $$ = lappend($1, $3);
 
1561
                                }
 
1562
                ;
 
1563
 
 
1564
TableElement:
 
1565
                        columnDef                                                       { $$ = $1; }
 
1566
                        | TableLikeClause                                       { $$ = $1; }
 
1567
                        | TableConstraint                                       { $$ = $1; }
 
1568
                ;
 
1569
 
 
1570
columnDef:      ColId Typename ColQualList
 
1571
                                {
 
1572
                                        ColumnDef *n = makeNode(ColumnDef);
 
1573
                                        n->colname = $1;
 
1574
                                        n->typename = $2;
 
1575
                                        n->constraints = $3;
 
1576
                                        n->is_local = true;
 
1577
                                        $$ = (Node *)n;
 
1578
                                }
 
1579
                ;
 
1580
 
 
1581
ColQualList:
 
1582
                        ColQualList ColConstraint                               { $$ = lappend($1, $2); }
 
1583
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
1584
                ;
 
1585
 
 
1586
ColConstraint:
 
1587
                        CONSTRAINT name ColConstraintElem
 
1588
                                {
 
1589
                                        switch (nodeTag($3))
 
1590
                                        {
 
1591
                                                case T_Constraint:
 
1592
                                                        {
 
1593
                                                                Constraint *n = (Constraint *)$3;
 
1594
                                                                n->name = $2;
 
1595
                                                        }
 
1596
                                                        break;
 
1597
                                                case T_FkConstraint:
 
1598
                                                        {
 
1599
                                                                FkConstraint *n = (FkConstraint *)$3;
 
1600
                                                                n->constr_name = $2;
 
1601
                                                        }
 
1602
                                                        break;
 
1603
                                                default:
 
1604
                                                        break;
 
1605
                                        }
 
1606
                                        $$ = $3;
 
1607
                                }
 
1608
                        | ColConstraintElem                                             { $$ = $1; }
 
1609
                        | ConstraintAttr                                                { $$ = $1; }
 
1610
                ;
 
1611
 
 
1612
/* DEFAULT NULL is already the default for Postgres.
 
1613
 * But define it here and carry it forward into the system
 
1614
 * to make it explicit.
 
1615
 * - thomas 1998-09-13
 
1616
 *
 
1617
 * WITH NULL and NULL are not SQL92-standard syntax elements,
 
1618
 * so leave them out. Use DEFAULT NULL to explicitly indicate
 
1619
 * that a column may have that value. WITH NULL leads to
 
1620
 * shift/reduce conflicts with WITH TIME ZONE anyway.
 
1621
 * - thomas 1999-01-08
 
1622
 *
 
1623
 * DEFAULT expression must be b_expr not a_expr to prevent shift/reduce
 
1624
 * conflict on NOT (since NOT might start a subsequent NOT NULL constraint,
 
1625
 * or be part of a_expr NOT LIKE or similar constructs).
 
1626
 */
 
1627
ColConstraintElem:
 
1628
                        NOT NULL_P
 
1629
                                {
 
1630
                                        Constraint *n = makeNode(Constraint);
 
1631
                                        n->contype = CONSTR_NOTNULL;
 
1632
                                        n->name = NULL;
 
1633
                                        n->raw_expr = NULL;
 
1634
                                        n->cooked_expr = NULL;
 
1635
                                        n->keys = NULL;
 
1636
                                        n->indexspace = NULL;
 
1637
                                        $$ = (Node *)n;
 
1638
                                }
 
1639
                        | NULL_P
 
1640
                                {
 
1641
                                        Constraint *n = makeNode(Constraint);
 
1642
                                        n->contype = CONSTR_NULL;
 
1643
                                        n->name = NULL;
 
1644
                                        n->raw_expr = NULL;
 
1645
                                        n->cooked_expr = NULL;
 
1646
                                        n->keys = NULL;
 
1647
                                        n->indexspace = NULL;
 
1648
                                        $$ = (Node *)n;
 
1649
                                }
 
1650
                        | UNIQUE OptConsTableSpace
 
1651
                                {
 
1652
                                        Constraint *n = makeNode(Constraint);
 
1653
                                        n->contype = CONSTR_UNIQUE;
 
1654
                                        n->name = NULL;
 
1655
                                        n->raw_expr = NULL;
 
1656
                                        n->cooked_expr = NULL;
 
1657
                                        n->keys = NULL;
 
1658
                                        n->indexspace = $2;
 
1659
                                        $$ = (Node *)n;
 
1660
                                }
 
1661
                        | PRIMARY KEY OptConsTableSpace
 
1662
                                {
 
1663
                                        Constraint *n = makeNode(Constraint);
 
1664
                                        n->contype = CONSTR_PRIMARY;
 
1665
                                        n->name = NULL;
 
1666
                                        n->raw_expr = NULL;
 
1667
                                        n->cooked_expr = NULL;
 
1668
                                        n->keys = NULL;
 
1669
                                        n->indexspace = $3;
 
1670
                                        $$ = (Node *)n;
 
1671
                                }
 
1672
                        | CHECK '(' a_expr ')'
 
1673
                                {
 
1674
                                        Constraint *n = makeNode(Constraint);
 
1675
                                        n->contype = CONSTR_CHECK;
 
1676
                                        n->name = NULL;
 
1677
                                        n->raw_expr = $3;
 
1678
                                        n->cooked_expr = NULL;
 
1679
                                        n->keys = NULL;
 
1680
                                        n->indexspace = NULL;
 
1681
                                        $$ = (Node *)n;
 
1682
                                }
 
1683
                        | DEFAULT b_expr
 
1684
                                {
 
1685
                                        Constraint *n = makeNode(Constraint);
 
1686
                                        n->contype = CONSTR_DEFAULT;
 
1687
                                        n->name = NULL;
 
1688
                                        if (exprIsNullConstant($2))
 
1689
                                        {
 
1690
                                                /* DEFAULT NULL should be reported as empty expr */
 
1691
                                                n->raw_expr = NULL;
 
1692
                                        }
 
1693
                                        else
 
1694
                                        {
 
1695
                                                n->raw_expr = $2;
 
1696
                                        }
 
1697
                                        n->cooked_expr = NULL;
 
1698
                                        n->keys = NULL;
 
1699
                                        n->indexspace = NULL;
 
1700
                                        $$ = (Node *)n;
 
1701
                                }
 
1702
                        | REFERENCES qualified_name opt_column_list key_match key_actions
 
1703
                                {
 
1704
                                        FkConstraint *n = makeNode(FkConstraint);
 
1705
                                        n->constr_name          = NULL;
 
1706
                                        n->pktable                      = $2;
 
1707
                                        n->fk_attrs                     = NIL;
 
1708
                                        n->pk_attrs                     = $3;
 
1709
                                        n->fk_matchtype         = $4;
 
1710
                                        n->fk_upd_action        = (char) ($5 >> 8);
 
1711
                                        n->fk_del_action        = (char) ($5 & 0xFF);
 
1712
                                        n->deferrable           = FALSE;
 
1713
                                        n->initdeferred         = FALSE;
 
1714
                                        $$ = (Node *)n;
 
1715
                                }
 
1716
                ;
 
1717
 
 
1718
/*
 
1719
 * ConstraintAttr represents constraint attributes, which we parse as if
 
1720
 * they were independent constraint clauses, in order to avoid shift/reduce
 
1721
 * conflicts (since NOT might start either an independent NOT NULL clause
 
1722
 * or an attribute).  analyze.c is responsible for attaching the attribute
 
1723
 * information to the preceding "real" constraint node, and for complaining
 
1724
 * if attribute clauses appear in the wrong place or wrong combinations.
 
1725
 *
 
1726
 * See also ConstraintAttributeSpec, which can be used in places where
 
1727
 * there is no parsing conflict.
 
1728
 */
 
1729
ConstraintAttr:
 
1730
                        DEFERRABLE
 
1731
                                {
 
1732
                                        Constraint *n = makeNode(Constraint);
 
1733
                                        n->contype = CONSTR_ATTR_DEFERRABLE;
 
1734
                                        $$ = (Node *)n;
 
1735
                                }
 
1736
                        | NOT DEFERRABLE
 
1737
                                {
 
1738
                                        Constraint *n = makeNode(Constraint);
 
1739
                                        n->contype = CONSTR_ATTR_NOT_DEFERRABLE;
 
1740
                                        $$ = (Node *)n;
 
1741
                                }
 
1742
                        | INITIALLY DEFERRED
 
1743
                                {
 
1744
                                        Constraint *n = makeNode(Constraint);
 
1745
                                        n->contype = CONSTR_ATTR_DEFERRED;
 
1746
                                        $$ = (Node *)n;
 
1747
                                }
 
1748
                        | INITIALLY IMMEDIATE
 
1749
                                {
 
1750
                                        Constraint *n = makeNode(Constraint);
 
1751
                                        n->contype = CONSTR_ATTR_IMMEDIATE;
 
1752
                                        $$ = (Node *)n;
 
1753
                                }
 
1754
                ;
 
1755
 
 
1756
 
 
1757
/*
 
1758
 * SQL99 supports wholesale borrowing of a table definition via the LIKE clause.
 
1759
 * This seems to be a poor man's inheritance capability, with the resulting
 
1760
 * tables completely decoupled except for the original commonality in definitions.
 
1761
 *
 
1762
 * This is very similar to CREATE TABLE AS except for the INCLUDING DEFAULTS extension
 
1763
 * which is a part of SQL 200N
 
1764
 */
 
1765
TableLikeClause:
 
1766
                        LIKE qualified_name like_including_defaults
 
1767
                                {
 
1768
                                        InhRelation *n = makeNode(InhRelation);
 
1769
                                        n->relation = $2;
 
1770
                                        n->including_defaults = $3;
 
1771
 
 
1772
                                        $$ = (Node *)n;
 
1773
                                }
 
1774
                ;
 
1775
 
 
1776
like_including_defaults:
 
1777
                                INCLUDING DEFAULTS              { $$ = true; }
 
1778
                                | EXCLUDING DEFAULTS            { $$ = false; }
 
1779
                                | /* EMPTY */                           { $$ = false; }
 
1780
                ;
 
1781
 
 
1782
 
 
1783
/* ConstraintElem specifies constraint syntax which is not embedded into
 
1784
 *      a column definition. ColConstraintElem specifies the embedded form.
 
1785
 * - thomas 1997-12-03
 
1786
 */
 
1787
TableConstraint:
 
1788
                        CONSTRAINT name ConstraintElem
 
1789
                                {
 
1790
                                        switch (nodeTag($3))
 
1791
                                        {
 
1792
                                                case T_Constraint:
 
1793
                                                        {
 
1794
                                                                Constraint *n = (Constraint *)$3;
 
1795
                                                                n->name = $2;
 
1796
                                                        }
 
1797
                                                        break;
 
1798
                                                case T_FkConstraint:
 
1799
                                                        {
 
1800
                                                                FkConstraint *n = (FkConstraint *)$3;
 
1801
                                                                n->constr_name = $2;
 
1802
                                                        }
 
1803
                                                        break;
 
1804
                                                default:
 
1805
                                                        break;
 
1806
                                        }
 
1807
                                        $$ = $3;
 
1808
                                }
 
1809
                        | ConstraintElem                                                { $$ = $1; }
 
1810
                ;
 
1811
 
 
1812
ConstraintElem:
 
1813
                        CHECK '(' a_expr ')'
 
1814
                                {
 
1815
                                        Constraint *n = makeNode(Constraint);
 
1816
                                        n->contype = CONSTR_CHECK;
 
1817
                                        n->name = NULL;
 
1818
                                        n->raw_expr = $3;
 
1819
                                        n->cooked_expr = NULL;
 
1820
                                        n->indexspace = NULL;
 
1821
                                        $$ = (Node *)n;
 
1822
                                }
 
1823
                        | UNIQUE '(' columnList ')' OptConsTableSpace
 
1824
                                {
 
1825
                                        Constraint *n = makeNode(Constraint);
 
1826
                                        n->contype = CONSTR_UNIQUE;
 
1827
                                        n->name = NULL;
 
1828
                                        n->raw_expr = NULL;
 
1829
                                        n->cooked_expr = NULL;
 
1830
                                        n->keys = $3;
 
1831
                                        n->indexspace = $5;
 
1832
                                        $$ = (Node *)n;
 
1833
                                }
 
1834
                        | PRIMARY KEY '(' columnList ')' OptConsTableSpace
 
1835
                                {
 
1836
                                        Constraint *n = makeNode(Constraint);
 
1837
                                        n->contype = CONSTR_PRIMARY;
 
1838
                                        n->name = NULL;
 
1839
                                        n->raw_expr = NULL;
 
1840
                                        n->cooked_expr = NULL;
 
1841
                                        n->keys = $4;
 
1842
                                        n->indexspace = $6;
 
1843
                                        $$ = (Node *)n;
 
1844
                                }
 
1845
                        | FOREIGN KEY '(' columnList ')' REFERENCES qualified_name
 
1846
                                opt_column_list key_match key_actions ConstraintAttributeSpec
 
1847
                                {
 
1848
                                        FkConstraint *n = makeNode(FkConstraint);
 
1849
                                        n->constr_name          = NULL;
 
1850
                                        n->pktable                      = $7;
 
1851
                                        n->fk_attrs                     = $4;
 
1852
                                        n->pk_attrs                     = $8;
 
1853
                                        n->fk_matchtype         = $9;
 
1854
                                        n->fk_upd_action        = (char) ($10 >> 8);
 
1855
                                        n->fk_del_action        = (char) ($10 & 0xFF);
 
1856
                                        n->deferrable           = ($11 & 1) != 0;
 
1857
                                        n->initdeferred         = ($11 & 2) != 0;
 
1858
                                        $$ = (Node *)n;
 
1859
                                }
 
1860
                ;
 
1861
 
 
1862
opt_column_list:
 
1863
                        '(' columnList ')'                                              { $$ = $2; }
 
1864
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
1865
                ;
 
1866
 
 
1867
columnList:
 
1868
                        columnElem                                                              { $$ = list_make1($1); }
 
1869
                        | columnList ',' columnElem                             { $$ = lappend($1, $3); }
 
1870
                ;
 
1871
 
 
1872
columnElem: ColId
 
1873
                                {
 
1874
                                        $$ = (Node *) makeString($1);
 
1875
                                }
 
1876
                ;
 
1877
 
 
1878
key_match:  MATCH FULL
 
1879
                        {
 
1880
                                $$ = FKCONSTR_MATCH_FULL;
 
1881
                        }
 
1882
                | MATCH PARTIAL
 
1883
                        {
 
1884
                                ereport(ERROR,
 
1885
                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
1886
                                                 errmsg("MATCH PARTIAL not yet implemented")));
 
1887
                                $$ = FKCONSTR_MATCH_PARTIAL;
 
1888
                        }
 
1889
                | MATCH SIMPLE
 
1890
                        {
 
1891
                                $$ = FKCONSTR_MATCH_UNSPECIFIED;
 
1892
                        }
 
1893
                | /*EMPTY*/
 
1894
                        {
 
1895
                                $$ = FKCONSTR_MATCH_UNSPECIFIED;
 
1896
                        }
 
1897
                ;
 
1898
 
 
1899
/*
 
1900
 * We combine the update and delete actions into one value temporarily
 
1901
 * for simplicity of parsing, and then break them down again in the
 
1902
 * calling production.  update is in the left 8 bits, delete in the right.
 
1903
 * Note that NOACTION is the default.
 
1904
 */
 
1905
key_actions:
 
1906
                        key_update
 
1907
                                { $$ = ($1 << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
 
1908
                        | key_delete
 
1909
                                { $$ = (FKCONSTR_ACTION_NOACTION << 8) | ($1 & 0xFF); }
 
1910
                        | key_update key_delete
 
1911
                                { $$ = ($1 << 8) | ($2 & 0xFF); }
 
1912
                        | key_delete key_update
 
1913
                                { $$ = ($2 << 8) | ($1 & 0xFF); }
 
1914
                        | /*EMPTY*/
 
1915
                                { $$ = (FKCONSTR_ACTION_NOACTION << 8) | (FKCONSTR_ACTION_NOACTION & 0xFF); }
 
1916
                ;
 
1917
 
 
1918
key_update: ON UPDATE key_action                { $$ = $3; }
 
1919
                ;
 
1920
 
 
1921
key_delete: ON DELETE_P key_action              { $$ = $3; }
 
1922
                ;
 
1923
 
 
1924
key_action:
 
1925
                        NO ACTION                                       { $$ = FKCONSTR_ACTION_NOACTION; }
 
1926
                        | RESTRICT                                      { $$ = FKCONSTR_ACTION_RESTRICT; }
 
1927
                        | CASCADE                                       { $$ = FKCONSTR_ACTION_CASCADE; }
 
1928
                        | SET NULL_P                            { $$ = FKCONSTR_ACTION_SETNULL; }
 
1929
                        | SET DEFAULT                           { $$ = FKCONSTR_ACTION_SETDEFAULT; }
 
1930
                ;
 
1931
 
 
1932
OptInherit: INHERITS '(' qualified_name_list ')'        { $$ = $3; }
 
1933
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
1934
                ;
 
1935
 
 
1936
OptWithOids:
 
1937
                        WITH OIDS                                                               { $$ = MUST_HAVE_OIDS; }
 
1938
                        | WITHOUT OIDS                                                  { $$ = MUST_NOT_HAVE_OIDS; }
 
1939
                        | /*EMPTY*/                                                             { $$ = DEFAULT_OIDS; }
 
1940
                ;
 
1941
 
 
1942
OnCommitOption:  ON COMMIT DROP                         { $$ = ONCOMMIT_DROP; }
 
1943
                        | ON COMMIT DELETE_P ROWS               { $$ = ONCOMMIT_DELETE_ROWS; }
 
1944
                        | ON COMMIT PRESERVE ROWS               { $$ = ONCOMMIT_PRESERVE_ROWS; }
 
1945
                        | /*EMPTY*/                                             { $$ = ONCOMMIT_NOOP; }
 
1946
                ;
 
1947
 
 
1948
OptTableSpace:   TABLESPACE name                                        { $$ = $2; }
 
1949
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1950
                ;
 
1951
 
 
1952
OptConsTableSpace:   USING INDEX TABLESPACE name        { $$ = $4; }
 
1953
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
1954
                ;
 
1955
 
 
1956
 
 
1957
/*
 
1958
 * Note: CREATE TABLE ... AS SELECT ... is just another spelling for
 
1959
 * SELECT ... INTO.
 
1960
 */
 
1961
 
 
1962
CreateAsStmt:
 
1963
                        CREATE OptTemp TABLE qualified_name OptCreateAs WithOidsAs SelectStmt
 
1964
                                {
 
1965
                                        /*
 
1966
                                         * When the SelectStmt is a set-operation tree, we must
 
1967
                                         * stuff the INTO information into the leftmost component
 
1968
                                         * Select, because that's where analyze.c will expect
 
1969
                                         * to find it.  Similarly, the output column names must
 
1970
                                         * be attached to that Select's target list.
 
1971
                                         */
 
1972
                                        SelectStmt *n = findLeftmostSelect((SelectStmt *) $7);
 
1973
                                        if (n->into != NULL)
 
1974
                                                ereport(ERROR,
 
1975
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
1976
                                                                 errmsg("CREATE TABLE AS may not specify INTO")));
 
1977
                                        $4->istemp = $2;
 
1978
                                        n->into = $4;
 
1979
                                        n->intoColNames = $5;
 
1980
                                        n->intoHasOids = $6;
 
1981
                                        $$ = $7;
 
1982
                                }
 
1983
                ;
 
1984
 
 
1985
/*
 
1986
 * To avoid a shift/reduce conflict in CreateAsStmt, we need to
 
1987
 * include the 'AS' terminal in the parsing of WITH/WITHOUT
 
1988
 * OIDS. Unfortunately that means this production is effectively a
 
1989
 * duplicate of OptWithOids.
 
1990
 */
 
1991
WithOidsAs:
 
1992
                        WITH OIDS AS                                                    { $$ = MUST_HAVE_OIDS; }
 
1993
                        | WITHOUT OIDS AS                                               { $$ = MUST_NOT_HAVE_OIDS; }
 
1994
                        | AS                                                                    { $$ = DEFAULT_OIDS; }
 
1995
                        ;
 
1996
 
 
1997
OptCreateAs:
 
1998
                        '(' CreateAsList ')'                                    { $$ = $2; }
 
1999
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
2000
                ;
 
2001
 
 
2002
CreateAsList:
 
2003
                        CreateAsElement                                                 { $$ = list_make1($1); }
 
2004
                        | CreateAsList ',' CreateAsElement              { $$ = lappend($1, $3); }
 
2005
                ;
 
2006
 
 
2007
CreateAsElement:
 
2008
                        ColId
 
2009
                                {
 
2010
                                        ColumnDef *n = makeNode(ColumnDef);
 
2011
                                        n->colname = $1;
 
2012
                                        n->typename = NULL;
 
2013
                                        n->inhcount = 0;
 
2014
                                        n->is_local = true;
 
2015
                                        n->is_not_null = false;
 
2016
                                        n->raw_default = NULL;
 
2017
                                        n->cooked_default = NULL;
 
2018
                                        n->constraints = NIL;
 
2019
                                        n->support = NULL;
 
2020
                                        $$ = (Node *)n;
 
2021
                                }
 
2022
                ;
 
2023
 
 
2024
 
 
2025
/*****************************************************************************
 
2026
 *
 
2027
 *              QUERY :
 
2028
 *                              CREATE SEQUENCE seqname
 
2029
 *                              ALTER SEQUENCE seqname
 
2030
 *
 
2031
 *****************************************************************************/
 
2032
 
 
2033
CreateSeqStmt:
 
2034
                        CREATE OptTemp SEQUENCE qualified_name OptSeqList
 
2035
                                {
 
2036
                                        CreateSeqStmt *n = makeNode(CreateSeqStmt);
 
2037
                                        $4->istemp = $2;
 
2038
                                        n->sequence = $4;
 
2039
                                        n->options = $5;
 
2040
                                        $$ = (Node *)n;
 
2041
                                }
 
2042
                ;
 
2043
 
 
2044
AlterSeqStmt:
 
2045
                        ALTER SEQUENCE qualified_name OptSeqList
 
2046
                                {
 
2047
                                        AlterSeqStmt *n = makeNode(AlterSeqStmt);
 
2048
                                        n->sequence = $3;
 
2049
                                        n->options = $4;
 
2050
                                        $$ = (Node *)n;
 
2051
                                }
 
2052
                ;
 
2053
 
 
2054
OptSeqList: OptSeqList OptSeqElem                                       { $$ = lappend($1, $2); }
 
2055
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
2056
                ;
 
2057
 
 
2058
OptSeqElem: CACHE NumericOnly
 
2059
                                {
 
2060
                                        $$ = makeDefElem("cache", (Node *)$2);
 
2061
                                }
 
2062
                        | CYCLE
 
2063
                                {
 
2064
                                        $$ = makeDefElem("cycle", (Node *)makeInteger(TRUE));
 
2065
                                }
 
2066
                        | NO CYCLE
 
2067
                                {
 
2068
                                        $$ = makeDefElem("cycle", (Node *)makeInteger(FALSE));
 
2069
                                }
 
2070
                        | INCREMENT opt_by NumericOnly
 
2071
                                {
 
2072
                                        $$ = makeDefElem("increment", (Node *)$3);
 
2073
                                }
 
2074
                        | MAXVALUE NumericOnly
 
2075
                                {
 
2076
                                        $$ = makeDefElem("maxvalue", (Node *)$2);
 
2077
                                }
 
2078
                        | MINVALUE NumericOnly
 
2079
                                {
 
2080
                                        $$ = makeDefElem("minvalue", (Node *)$2);
 
2081
                                }
 
2082
                        | NO MAXVALUE
 
2083
                                {
 
2084
                                        $$ = makeDefElem("maxvalue", NULL);
 
2085
                                }
 
2086
                        | NO MINVALUE
 
2087
                                {
 
2088
                                        $$ = makeDefElem("minvalue", NULL);
 
2089
                                }
 
2090
                        | START opt_with NumericOnly
 
2091
                                {
 
2092
                                        $$ = makeDefElem("start", (Node *)$3);
 
2093
                                }
 
2094
                        | RESTART opt_with NumericOnly
 
2095
                                {
 
2096
                                        $$ = makeDefElem("restart", (Node *)$3);
 
2097
                                }
 
2098
                ;
 
2099
 
 
2100
opt_by:         BY                              {}
 
2101
                        | /* empty */   {}
 
2102
          ;
 
2103
 
 
2104
NumericOnly:
 
2105
                        FloatOnly                                                               { $$ = $1; }
 
2106
                        | IntegerOnly                                                   { $$ = $1; }
 
2107
                ;
 
2108
 
 
2109
FloatOnly:      FCONST                                                                  { $$ = makeFloat($1); }
 
2110
                        | '-' FCONST
 
2111
                                {
 
2112
                                        $$ = makeFloat($2);
 
2113
                                        doNegateFloat($$);
 
2114
                                }
 
2115
                ;
 
2116
 
 
2117
IntegerOnly:
 
2118
                        Iconst
 
2119
                                {
 
2120
                                        $$ = makeInteger($1);
 
2121
                                }
 
2122
                        | '-' Iconst
 
2123
                                {
 
2124
                                        $$ = makeInteger($2);
 
2125
                                        $$->val.ival = - $$->val.ival;
 
2126
                                }
 
2127
                ;
 
2128
 
 
2129
/*****************************************************************************
 
2130
 *
 
2131
 *              QUERIES :
 
2132
 *                              CREATE PROCEDURAL LANGUAGE ...
 
2133
 *                              DROP PROCEDURAL LANGUAGE ...
 
2134
 *
 
2135
 *****************************************************************************/
 
2136
 
 
2137
CreatePLangStmt:
 
2138
                        CREATE opt_trusted opt_procedural LANGUAGE ColId_or_Sconst
 
2139
                        HANDLER handler_name opt_validator opt_lancompiler
 
2140
                        {
 
2141
                                CreatePLangStmt *n = makeNode(CreatePLangStmt);
 
2142
                                n->plname = $5;
 
2143
                                n->plhandler = $7;
 
2144
                                n->plvalidator = $8;
 
2145
                                n->pltrusted = $2;
 
2146
                                $$ = (Node *)n;
 
2147
                        }
 
2148
                ;
 
2149
 
 
2150
opt_trusted:
 
2151
                        TRUSTED                                                                 { $$ = TRUE; }
 
2152
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
2153
                ;
 
2154
 
 
2155
/* This ought to be just func_name, but that causes reduce/reduce conflicts
 
2156
 * (CREATE LANGUAGE is the only place where func_name isn't followed by '(').
 
2157
 * Work around by using simple names, instead.
 
2158
 */
 
2159
handler_name:
 
2160
                        name                                            { $$ = list_make1(makeString($1)); }
 
2161
                        | name attrs                            { $$ = lcons(makeString($1), $2); }
 
2162
                ;
 
2163
 
 
2164
opt_lancompiler:
 
2165
                        LANCOMPILER Sconst                                              { $$ = $2; }
 
2166
                        | /*EMPTY*/                                                             { $$ = ""; }
 
2167
                ;
 
2168
 
 
2169
opt_validator:
 
2170
                        VALIDATOR handler_name { $$ = $2; }
 
2171
                        | /*EMPTY*/ { $$ = NULL; }
 
2172
                ;
 
2173
 
 
2174
DropPLangStmt:
 
2175
                        DROP opt_procedural LANGUAGE ColId_or_Sconst opt_drop_behavior
 
2176
                                {
 
2177
                                        DropPLangStmt *n = makeNode(DropPLangStmt);
 
2178
                                        n->plname = $4;
 
2179
                                        n->behavior = $5;
 
2180
                                        $$ = (Node *)n;
 
2181
                                }
 
2182
                ;
 
2183
 
 
2184
opt_procedural:
 
2185
                        PROCEDURAL                                                              {}
 
2186
                        | /*EMPTY*/                                                             {}
 
2187
                ;
 
2188
 
 
2189
/*****************************************************************************
 
2190
 *
 
2191
 *              QUERY:
 
2192
 *             CREATE TABLESPACE tablespace LOCATION '/path/to/tablespace/'
 
2193
 *
 
2194
 *****************************************************************************/
 
2195
 
 
2196
CreateTableSpaceStmt: CREATE TABLESPACE name OptTableSpaceOwner LOCATION Sconst
 
2197
                                {
 
2198
                                        CreateTableSpaceStmt *n = makeNode(CreateTableSpaceStmt);
 
2199
                                        n->tablespacename = $3;
 
2200
                                        n->owner = $4;
 
2201
                                        n->location = $6;
 
2202
                                        $$ = (Node *) n;
 
2203
                                }
 
2204
                ;
 
2205
 
 
2206
OptTableSpaceOwner: OWNER name                  { $$ = $2; }
 
2207
                        | /*EMPTY */                            { $$ = NULL; }
 
2208
                ;
 
2209
 
 
2210
/*****************************************************************************
 
2211
 *
 
2212
 *              QUERY :
 
2213
 *                              DROP TABLESPACE <tablespace>
 
2214
 *
 
2215
 *              No need for drop behaviour as we cannot implement dependencies for
 
2216
 *              objects in other databases; we can only support RESTRICT.
 
2217
 *
 
2218
 ****************************************************************************/
 
2219
 
 
2220
DropTableSpaceStmt: DROP TABLESPACE name
 
2221
                                {
 
2222
                                        DropTableSpaceStmt *n = makeNode(DropTableSpaceStmt);
 
2223
                                        n->tablespacename = $3;
 
2224
                                        $$ = (Node *) n;
 
2225
                                }
 
2226
                ;
 
2227
 
 
2228
/*****************************************************************************
 
2229
 *
 
2230
 *              QUERIES :
 
2231
 *                              CREATE TRIGGER ...
 
2232
 *                              DROP TRIGGER ...
 
2233
 *
 
2234
 *****************************************************************************/
 
2235
 
 
2236
CreateTrigStmt:
 
2237
                        CREATE TRIGGER name TriggerActionTime TriggerEvents ON
 
2238
                        qualified_name TriggerForSpec EXECUTE PROCEDURE
 
2239
                        func_name '(' TriggerFuncArgs ')'
 
2240
                                {
 
2241
                                        CreateTrigStmt *n = makeNode(CreateTrigStmt);
 
2242
                                        n->trigname = $3;
 
2243
                                        n->relation = $7;
 
2244
                                        n->funcname = $11;
 
2245
                                        n->args = $13;
 
2246
                                        n->before = $4;
 
2247
                                        n->row = $8;
 
2248
                                        memcpy(n->actions, $5, 4);
 
2249
                                        n->isconstraint  = FALSE;
 
2250
                                        n->deferrable    = FALSE;
 
2251
                                        n->initdeferred  = FALSE;
 
2252
                                        n->constrrel = NULL;
 
2253
                                        $$ = (Node *)n;
 
2254
                                }
 
2255
                        | CREATE CONSTRAINT TRIGGER name AFTER TriggerEvents ON
 
2256
                        qualified_name OptConstrFromTable
 
2257
                        ConstraintAttributeSpec
 
2258
                        FOR EACH ROW EXECUTE PROCEDURE
 
2259
                        func_name '(' TriggerFuncArgs ')'
 
2260
                                {
 
2261
                                        CreateTrigStmt *n = makeNode(CreateTrigStmt);
 
2262
                                        n->trigname = $4;
 
2263
                                        n->relation = $8;
 
2264
                                        n->funcname = $16;
 
2265
                                        n->args = $18;
 
2266
                                        n->before = FALSE;
 
2267
                                        n->row = TRUE;
 
2268
                                        memcpy(n->actions, $6, 4);
 
2269
                                        n->isconstraint  = TRUE;
 
2270
                                        n->deferrable = ($10 & 1) != 0;
 
2271
                                        n->initdeferred = ($10 & 2) != 0;
 
2272
 
 
2273
                                        n->constrrel = $9;
 
2274
                                        $$ = (Node *)n;
 
2275
                                }
 
2276
                ;
 
2277
 
 
2278
TriggerActionTime:
 
2279
                        BEFORE                                                                  { $$ = TRUE; }
 
2280
                        | AFTER                                                                 { $$ = FALSE; }
 
2281
                ;
 
2282
 
 
2283
TriggerEvents:
 
2284
                        TriggerOneEvent
 
2285
                                {
 
2286
                                        char *e = palloc(4);
 
2287
                                        e[0] = $1; e[1] = '\0';
 
2288
                                        $$ = e;
 
2289
                                }
 
2290
                        | TriggerOneEvent OR TriggerOneEvent
 
2291
                                {
 
2292
                                        char *e = palloc(4);
 
2293
                                        e[0] = $1; e[1] = $3; e[2] = '\0';
 
2294
                                        $$ = e;
 
2295
                                }
 
2296
                        | TriggerOneEvent OR TriggerOneEvent OR TriggerOneEvent
 
2297
                                {
 
2298
                                        char *e = palloc(4);
 
2299
                                        e[0] = $1; e[1] = $3; e[2] = $5; e[3] = '\0';
 
2300
                                        $$ = e;
 
2301
                                }
 
2302
                ;
 
2303
 
 
2304
TriggerOneEvent:
 
2305
                        INSERT                                                                  { $$ = 'i'; }
 
2306
                        | DELETE_P                                                              { $$ = 'd'; }
 
2307
                        | UPDATE                                                                { $$ = 'u'; }
 
2308
                ;
 
2309
 
 
2310
TriggerForSpec:
 
2311
                        FOR TriggerForOpt TriggerForType
 
2312
                                {
 
2313
                                        $$ = $3;
 
2314
                                }
 
2315
                        | /* EMPTY */
 
2316
                                {
 
2317
                                        /*
 
2318
                                         * If ROW/STATEMENT not specified, default to
 
2319
                                         * STATEMENT, per SQL
 
2320
                                         */
 
2321
                                        $$ = FALSE;
 
2322
                                }
 
2323
                ;
 
2324
 
 
2325
TriggerForOpt:
 
2326
                        EACH                                                                    {}
 
2327
                        | /*EMPTY*/                                                             {}
 
2328
                ;
 
2329
 
 
2330
TriggerForType:
 
2331
                        ROW                                                                             { $$ = TRUE; }
 
2332
                        | STATEMENT                                                             { $$ = FALSE; }
 
2333
                ;
 
2334
 
 
2335
TriggerFuncArgs:
 
2336
                        TriggerFuncArg                                                  { $$ = list_make1($1); }
 
2337
                        | TriggerFuncArgs ',' TriggerFuncArg    { $$ = lappend($1, $3); }
 
2338
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
2339
                ;
 
2340
 
 
2341
TriggerFuncArg:
 
2342
                        ICONST
 
2343
                                {
 
2344
                                        char buf[64];
 
2345
                                        snprintf(buf, sizeof(buf), "%d", $1);
 
2346
                                        $$ = makeString(pstrdup(buf));
 
2347
                                }
 
2348
                        | FCONST                                                                { $$ = makeString($1); }
 
2349
                        | Sconst                                                                { $$ = makeString($1); }
 
2350
                        | BCONST                                                                { $$ = makeString($1); }
 
2351
                        | XCONST                                                                { $$ = makeString($1); }
 
2352
                        | ColId                                                                 { $$ = makeString($1); }
 
2353
                ;
 
2354
 
 
2355
OptConstrFromTable:
 
2356
                        FROM qualified_name                                             { $$ = $2; }
 
2357
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
2358
                ;
 
2359
 
 
2360
ConstraintAttributeSpec:
 
2361
                        ConstraintDeferrabilitySpec
 
2362
                                { $$ = $1; }
 
2363
                        | ConstraintDeferrabilitySpec ConstraintTimeSpec
 
2364
                                {
 
2365
                                        if ($1 == 0 && $2 != 0)
 
2366
                                                ereport(ERROR,
 
2367
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
2368
                                                                 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
 
2369
                                        $$ = $1 | $2;
 
2370
                                }
 
2371
                        | ConstraintTimeSpec
 
2372
                                {
 
2373
                                        if ($1 != 0)
 
2374
                                                $$ = 3;
 
2375
                                        else
 
2376
                                                $$ = 0;
 
2377
                                }
 
2378
                        | ConstraintTimeSpec ConstraintDeferrabilitySpec
 
2379
                                {
 
2380
                                        if ($2 == 0 && $1 != 0)
 
2381
                                                ereport(ERROR,
 
2382
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
2383
                                                                 errmsg("constraint declared INITIALLY DEFERRED must be DEFERRABLE")));
 
2384
                                        $$ = $1 | $2;
 
2385
                                }
 
2386
                        | /*EMPTY*/
 
2387
                                { $$ = 0; }
 
2388
                ;
 
2389
 
 
2390
ConstraintDeferrabilitySpec:
 
2391
                        NOT DEFERRABLE                                                  { $$ = 0; }
 
2392
                        | DEFERRABLE                                                    { $$ = 1; }
 
2393
                ;
 
2394
 
 
2395
ConstraintTimeSpec:
 
2396
                        INITIALLY IMMEDIATE                                             { $$ = 0; }
 
2397
                        | INITIALLY DEFERRED                                    { $$ = 2; }
 
2398
                ;
 
2399
 
 
2400
 
 
2401
DropTrigStmt:
 
2402
                        DROP TRIGGER name ON qualified_name opt_drop_behavior
 
2403
                                {
 
2404
                                        DropPropertyStmt *n = makeNode(DropPropertyStmt);
 
2405
                                        n->relation = $5;
 
2406
                                        n->property = $3;
 
2407
                                        n->behavior = $6;
 
2408
                                        n->removeType = OBJECT_TRIGGER;
 
2409
                                        $$ = (Node *) n;
 
2410
                                }
 
2411
                ;
 
2412
 
 
2413
 
 
2414
/*****************************************************************************
 
2415
 *
 
2416
 *              QUERIES :
 
2417
 *                              CREATE ASSERTION ...
 
2418
 *                              DROP ASSERTION ...
 
2419
 *
 
2420
 *****************************************************************************/
 
2421
 
 
2422
CreateAssertStmt:
 
2423
                        CREATE ASSERTION name CHECK '(' a_expr ')'
 
2424
                        ConstraintAttributeSpec
 
2425
                                {
 
2426
                                        CreateTrigStmt *n = makeNode(CreateTrigStmt);
 
2427
                                        n->trigname = $3;
 
2428
                                        n->args = list_make1($6);
 
2429
                                        n->isconstraint  = TRUE;
 
2430
                                        n->deferrable = ($8 & 1) != 0;
 
2431
                                        n->initdeferred = ($8 & 2) != 0;
 
2432
 
 
2433
                                        ereport(ERROR,
 
2434
                                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
2435
                                                         errmsg("CREATE ASSERTION is not yet implemented")));
 
2436
 
 
2437
                                        $$ = (Node *)n;
 
2438
                                }
 
2439
                ;
 
2440
 
 
2441
DropAssertStmt:
 
2442
                        DROP ASSERTION name opt_drop_behavior
 
2443
                                {
 
2444
                                        DropPropertyStmt *n = makeNode(DropPropertyStmt);
 
2445
                                        n->relation = NULL;
 
2446
                                        n->property = $3;
 
2447
                                        n->behavior = $4;
 
2448
                                        n->removeType = OBJECT_TRIGGER; /* XXX */
 
2449
                                        ereport(ERROR,
 
2450
                                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
2451
                                                         errmsg("DROP ASSERTION is not yet implemented")));
 
2452
                                        $$ = (Node *) n;
 
2453
                                }
 
2454
                ;
 
2455
 
 
2456
 
 
2457
/*****************************************************************************
 
2458
 *
 
2459
 *              QUERY :
 
2460
 *                              define (aggregate,operator,type)
 
2461
 *
 
2462
 *****************************************************************************/
 
2463
 
 
2464
DefineStmt:
 
2465
                        CREATE AGGREGATE func_name definition
 
2466
                                {
 
2467
                                        DefineStmt *n = makeNode(DefineStmt);
 
2468
                                        n->kind = OBJECT_AGGREGATE;
 
2469
                                        n->defnames = $3;
 
2470
                                        n->definition = $4;
 
2471
                                        $$ = (Node *)n;
 
2472
                                }
 
2473
                        | CREATE OPERATOR any_operator definition
 
2474
                                {
 
2475
                                        DefineStmt *n = makeNode(DefineStmt);
 
2476
                                        n->kind = OBJECT_OPERATOR;
 
2477
                                        n->defnames = $3;
 
2478
                                        n->definition = $4;
 
2479
                                        $$ = (Node *)n;
 
2480
                                }
 
2481
                        | CREATE TYPE_P any_name definition
 
2482
                                {
 
2483
                                        DefineStmt *n = makeNode(DefineStmt);
 
2484
                                        n->kind = OBJECT_TYPE;
 
2485
                                        n->defnames = $3;
 
2486
                                        n->definition = $4;
 
2487
                                        $$ = (Node *)n;
 
2488
                                }
 
2489
                        | CREATE TYPE_P any_name AS '(' TableFuncElementList ')'
 
2490
                                {
 
2491
                                        CompositeTypeStmt *n = makeNode(CompositeTypeStmt);
 
2492
                                        RangeVar *r = makeNode(RangeVar);
 
2493
 
 
2494
                                        /* can't use qualified_name, sigh */
 
2495
                                        switch (list_length($3))
 
2496
                                        {
 
2497
                                                case 1:
 
2498
                                                        r->catalogname = NULL;
 
2499
                                                        r->schemaname = NULL;
 
2500
                                                        r->relname = strVal(linitial($3));
 
2501
                                                        break;
 
2502
                                                case 2:
 
2503
                                                        r->catalogname = NULL;
 
2504
                                                        r->schemaname = strVal(linitial($3));
 
2505
                                                        r->relname = strVal(lsecond($3));
 
2506
                                                        break;
 
2507
                                                case 3:
 
2508
                                                        r->catalogname = strVal(linitial($3));
 
2509
                                                        r->schemaname = strVal(lsecond($3));
 
2510
                                                        r->relname = strVal(lthird($3));
 
2511
                                                        break;
 
2512
                                                default:
 
2513
                                                        ereport(ERROR,
 
2514
                                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
2515
                                                                         errmsg("improper qualified name (too many dotted names): %s",
 
2516
                                                                                        NameListToString($3))));
 
2517
                                                        break;
 
2518
                                        }
 
2519
                                        n->typevar = r;
 
2520
                                        n->coldeflist = $6;
 
2521
                                        $$ = (Node *)n;
 
2522
                                }
 
2523
                ;
 
2524
 
 
2525
definition: '(' def_list ')'                                            { $$ = $2; }
 
2526
                ;
 
2527
 
 
2528
def_list:       def_elem                                                                { $$ = list_make1($1); }
 
2529
                        | def_list ',' def_elem                                 { $$ = lappend($1, $3); }
 
2530
                ;
 
2531
 
 
2532
def_elem:  ColLabel '=' def_arg
 
2533
                                {
 
2534
                                        $$ = makeDefElem($1, (Node *)$3);
 
2535
                                }
 
2536
                        | ColLabel
 
2537
                                {
 
2538
                                        $$ = makeDefElem($1, NULL);
 
2539
                                }
 
2540
                ;
 
2541
 
 
2542
/* Note: any simple identifier will be returned as a type name! */
 
2543
def_arg:        func_return                                             { $$ = (Node *)$1; }
 
2544
                        | qual_all_Op                                   { $$ = (Node *)$1; }
 
2545
                        | NumericOnly                                   { $$ = (Node *)$1; }
 
2546
                        | Sconst                                                { $$ = (Node *)makeString($1); }
 
2547
                ;
 
2548
 
 
2549
 
 
2550
/*****************************************************************************
 
2551
 *
 
2552
 *              QUERIES :
 
2553
 *                              CREATE OPERATOR CLASS ...
 
2554
 *                              DROP OPERATOR CLASS ...
 
2555
 *
 
2556
 *****************************************************************************/
 
2557
 
 
2558
CreateOpClassStmt:
 
2559
                        CREATE OPERATOR CLASS any_name opt_default FOR TYPE_P Typename
 
2560
                        USING access_method AS opclass_item_list
 
2561
                                {
 
2562
                                        CreateOpClassStmt *n = makeNode(CreateOpClassStmt);
 
2563
                                        n->opclassname = $4;
 
2564
                                        n->isDefault = $5;
 
2565
                                        n->datatype = $8;
 
2566
                                        n->amname = $10;
 
2567
                                        n->items = $12;
 
2568
                                        $$ = (Node *) n;
 
2569
                                }
 
2570
                ;
 
2571
 
 
2572
opclass_item_list:
 
2573
                        opclass_item                                                    { $$ = list_make1($1); }
 
2574
                        | opclass_item_list ',' opclass_item    { $$ = lappend($1, $3); }
 
2575
                ;
 
2576
 
 
2577
opclass_item:
 
2578
                        OPERATOR Iconst any_operator opt_recheck
 
2579
                                {
 
2580
                                        CreateOpClassItem *n = makeNode(CreateOpClassItem);
 
2581
                                        n->itemtype = OPCLASS_ITEM_OPERATOR;
 
2582
                                        n->name = $3;
 
2583
                                        n->args = NIL;
 
2584
                                        n->number = $2;
 
2585
                                        n->recheck = $4;
 
2586
                                        $$ = (Node *) n;
 
2587
                                }
 
2588
                        | OPERATOR Iconst any_operator '(' oper_argtypes ')' opt_recheck
 
2589
                                {
 
2590
                                        CreateOpClassItem *n = makeNode(CreateOpClassItem);
 
2591
                                        n->itemtype = OPCLASS_ITEM_OPERATOR;
 
2592
                                        n->name = $3;
 
2593
                                        n->args = $5;
 
2594
                                        n->number = $2;
 
2595
                                        n->recheck = $7;
 
2596
                                        $$ = (Node *) n;
 
2597
                                }
 
2598
                        | FUNCTION Iconst func_name func_args
 
2599
                                {
 
2600
                                        CreateOpClassItem *n = makeNode(CreateOpClassItem);
 
2601
                                        n->itemtype = OPCLASS_ITEM_FUNCTION;
 
2602
                                        n->name = $3;
 
2603
                                        n->args = extractArgTypes($4);
 
2604
                                        n->number = $2;
 
2605
                                        $$ = (Node *) n;
 
2606
                                }
 
2607
                        | STORAGE Typename
 
2608
                                {
 
2609
                                        CreateOpClassItem *n = makeNode(CreateOpClassItem);
 
2610
                                        n->itemtype = OPCLASS_ITEM_STORAGETYPE;
 
2611
                                        n->storedtype = $2;
 
2612
                                        $$ = (Node *) n;
 
2613
                                }
 
2614
                ;
 
2615
 
 
2616
opt_default:    DEFAULT { $$ = TRUE; }
 
2617
                        | /*EMPTY*/     { $$ = FALSE; }
 
2618
                ;
 
2619
 
 
2620
opt_recheck:    RECHECK { $$ = TRUE; }
 
2621
                        | /*EMPTY*/     { $$ = FALSE; }
 
2622
                ;
 
2623
 
 
2624
 
 
2625
DropOpClassStmt:
 
2626
                        DROP OPERATOR CLASS any_name USING access_method opt_drop_behavior
 
2627
                                {
 
2628
                                        RemoveOpClassStmt *n = makeNode(RemoveOpClassStmt);
 
2629
                                        n->opclassname = $4;
 
2630
                                        n->amname = $6;
 
2631
                                        n->behavior = $7;
 
2632
                                        $$ = (Node *) n;
 
2633
                                }
 
2634
                ;
 
2635
 
 
2636
 
 
2637
/*****************************************************************************
 
2638
 *
 
2639
 *              QUERY:
 
2640
 *
 
2641
 *              DROP itemtype itemname [, itemname ...] [ RESTRICT | CASCADE ]
 
2642
 *
 
2643
 *****************************************************************************/
 
2644
 
 
2645
DropStmt:       DROP drop_type any_name_list opt_drop_behavior
 
2646
                                {
 
2647
                                        DropStmt *n = makeNode(DropStmt);
 
2648
                                        n->removeType = $2;
 
2649
                                        n->objects = $3;
 
2650
                                        n->behavior = $4;
 
2651
                                        $$ = (Node *)n;
 
2652
                                }
 
2653
                ;
 
2654
 
 
2655
drop_type:      TABLE                                                                   { $$ = OBJECT_TABLE; }
 
2656
                        | SEQUENCE                                                              { $$ = OBJECT_SEQUENCE; }
 
2657
                        | VIEW                                                                  { $$ = OBJECT_VIEW; }
 
2658
                        | INDEX                                                                 { $$ = OBJECT_INDEX; }
 
2659
                        | TYPE_P                                                                { $$ = OBJECT_TYPE; }
 
2660
                        | DOMAIN_P                                                              { $$ = OBJECT_DOMAIN; }
 
2661
                        | CONVERSION_P                                                  { $$ = OBJECT_CONVERSION; }
 
2662
                        | SCHEMA                                                                { $$ = OBJECT_SCHEMA; }
 
2663
                ;
 
2664
 
 
2665
any_name_list:
 
2666
                        any_name                                                                { $$ = list_make1($1); }
 
2667
                        | any_name_list ',' any_name                    { $$ = lappend($1, $3); }
 
2668
                ;
 
2669
 
 
2670
any_name:       ColId                                           { $$ = list_make1(makeString($1)); }
 
2671
                        | ColId attrs                           { $$ = lcons(makeString($1), $2); }
 
2672
                ;
 
2673
 
 
2674
attrs:          '.' attr_name
 
2675
                                        { $$ = list_make1(makeString($2)); }
 
2676
                        | attrs '.' attr_name
 
2677
                                        { $$ = lappend($1, makeString($3)); }
 
2678
                ;
 
2679
 
 
2680
 
 
2681
/*****************************************************************************
 
2682
 *
 
2683
 *              QUERY:
 
2684
 *                              truncate table relname
 
2685
 *
 
2686
 *****************************************************************************/
 
2687
 
 
2688
TruncateStmt:
 
2689
                        TRUNCATE opt_table qualified_name
 
2690
                                {
 
2691
                                        TruncateStmt *n = makeNode(TruncateStmt);
 
2692
                                        n->relation = $3;
 
2693
                                        $$ = (Node *)n;
 
2694
                                }
 
2695
                ;
 
2696
 
 
2697
/*****************************************************************************
 
2698
 *
 
2699
 *      The COMMENT ON statement can take different forms based upon the type of
 
2700
 *      the object associated with the comment. The form of the statement is:
 
2701
 *
 
2702
 *      COMMENT ON [ [ DATABASE | DOMAIN | INDEX | SEQUENCE | TABLE | TYPE | VIEW |
 
2703
 *                                 CONVERSION | LANGUAGE | OPERATOR CLASS | LARGE OBJECT |
 
2704
 *                                 CAST ] <objname> |
 
2705
 *                               AGGREGATE <aggname> (<aggtype>) |
 
2706
 *                               FUNCTION <funcname> (arg1, arg2, ...) |
 
2707
 *                               OPERATOR <op> (leftoperand_typ, rightoperand_typ) |
 
2708
 *                               TRIGGER <triggername> ON <relname> |
 
2709
 *                               RULE <rulename> ON <relname> ]
 
2710
 *                         IS 'text'
 
2711
 *
 
2712
 *****************************************************************************/
 
2713
 
 
2714
CommentStmt:
 
2715
                        COMMENT ON comment_type any_name IS comment_text
 
2716
                                {
 
2717
                                        CommentStmt *n = makeNode(CommentStmt);
 
2718
                                        n->objtype = $3;
 
2719
                                        n->objname = $4;
 
2720
                                        n->objargs = NIL;
 
2721
                                        n->comment = $6;
 
2722
                                        $$ = (Node *) n;
 
2723
                                }
 
2724
                        | COMMENT ON AGGREGATE func_name '(' aggr_argtype ')'
 
2725
                        IS comment_text
 
2726
                                {
 
2727
                                        CommentStmt *n = makeNode(CommentStmt);
 
2728
                                        n->objtype = OBJECT_AGGREGATE;
 
2729
                                        n->objname = $4;
 
2730
                                        n->objargs = list_make1($6);
 
2731
                                        n->comment = $9;
 
2732
                                        $$ = (Node *) n;
 
2733
                                }
 
2734
                        | COMMENT ON FUNCTION func_name func_args IS comment_text
 
2735
                                {
 
2736
                                        CommentStmt *n = makeNode(CommentStmt);
 
2737
                                        n->objtype = OBJECT_FUNCTION;
 
2738
                                        n->objname = $4;
 
2739
                                        n->objargs = extractArgTypes($5);
 
2740
                                        n->comment = $7;
 
2741
                                        $$ = (Node *) n;
 
2742
                                }
 
2743
                        | COMMENT ON OPERATOR any_operator '(' oper_argtypes ')'
 
2744
                        IS comment_text
 
2745
                                {
 
2746
                                        CommentStmt *n = makeNode(CommentStmt);
 
2747
                                        n->objtype = OBJECT_OPERATOR;
 
2748
                                        n->objname = $4;
 
2749
                                        n->objargs = $6;
 
2750
                                        n->comment = $9;
 
2751
                                        $$ = (Node *) n;
 
2752
                                }
 
2753
                        | COMMENT ON CONSTRAINT name ON any_name IS comment_text
 
2754
                                {
 
2755
                                        CommentStmt *n = makeNode(CommentStmt);
 
2756
                                        n->objtype = OBJECT_CONSTRAINT;
 
2757
                                        n->objname = lappend($6, makeString($4));
 
2758
                                        n->objargs = NIL;
 
2759
                                        n->comment = $8;
 
2760
                                        $$ = (Node *) n;
 
2761
                                }
 
2762
                        | COMMENT ON RULE name ON any_name IS comment_text
 
2763
                                {
 
2764
                                        CommentStmt *n = makeNode(CommentStmt);
 
2765
                                        n->objtype = OBJECT_RULE;
 
2766
                                        n->objname = lappend($6, makeString($4));
 
2767
                                        n->objargs = NIL;
 
2768
                                        n->comment = $8;
 
2769
                                        $$ = (Node *) n;
 
2770
                                }
 
2771
                        | COMMENT ON RULE name IS comment_text
 
2772
                                {
 
2773
                                        /* Obsolete syntax supported for awhile for compatibility */
 
2774
                                        CommentStmt *n = makeNode(CommentStmt);
 
2775
                                        n->objtype = OBJECT_RULE;
 
2776
                                        n->objname = list_make1(makeString($4));
 
2777
                                        n->objargs = NIL;
 
2778
                                        n->comment = $6;
 
2779
                                        $$ = (Node *) n;
 
2780
                                }
 
2781
                        | COMMENT ON TRIGGER name ON any_name IS comment_text
 
2782
                                {
 
2783
                                        CommentStmt *n = makeNode(CommentStmt);
 
2784
                                        n->objtype = OBJECT_TRIGGER;
 
2785
                                        n->objname = lappend($6, makeString($4));
 
2786
                                        n->objargs = NIL;
 
2787
                                        n->comment = $8;
 
2788
                                        $$ = (Node *) n;
 
2789
                                }
 
2790
                        | COMMENT ON OPERATOR CLASS any_name USING access_method IS comment_text
 
2791
                                {
 
2792
                                        CommentStmt *n = makeNode(CommentStmt);
 
2793
                                        n->objtype = OBJECT_OPCLASS;
 
2794
                                        n->objname = $5;
 
2795
                                        n->objargs = list_make1(makeString($7));
 
2796
                                        n->comment = $9;
 
2797
                                        $$ = (Node *) n;
 
2798
                                }
 
2799
                        | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text
 
2800
                                {
 
2801
                                        CommentStmt *n = makeNode(CommentStmt);
 
2802
                                        n->objtype = OBJECT_LARGEOBJECT;
 
2803
                                        n->objname = list_make1($5);
 
2804
                                        n->objargs = NIL;
 
2805
                                        n->comment = $7;
 
2806
                                        $$ = (Node *) n;
 
2807
                                }
 
2808
                        | COMMENT ON CAST '(' Typename AS Typename ')' IS comment_text
 
2809
                                {
 
2810
                                        CommentStmt *n = makeNode(CommentStmt);
 
2811
                                        n->objtype = OBJECT_CAST;
 
2812
                                        n->objname = list_make1($5);
 
2813
                                        n->objargs = list_make1($7);
 
2814
                                        n->comment = $10;
 
2815
                                        $$ = (Node *) n;
 
2816
                                }
 
2817
                        | COMMENT ON opt_procedural LANGUAGE any_name IS comment_text
 
2818
                                {
 
2819
                                        CommentStmt *n = makeNode(CommentStmt);
 
2820
                                        n->objtype = OBJECT_LANGUAGE;
 
2821
                                        n->objname = $5;
 
2822
                                        n->objargs = NIL;
 
2823
                                        n->comment = $7;
 
2824
                                        $$ = (Node *) n;
 
2825
                                }
 
2826
                ;
 
2827
 
 
2828
comment_type:
 
2829
                        COLUMN                                                          { $$ = OBJECT_COLUMN; }
 
2830
                        | DATABASE                                                      { $$ = OBJECT_DATABASE; }
 
2831
                        | SCHEMA                                                        { $$ = OBJECT_SCHEMA; }
 
2832
                        | INDEX                                                         { $$ = OBJECT_INDEX; }
 
2833
                        | SEQUENCE                                                      { $$ = OBJECT_SEQUENCE; }
 
2834
                        | TABLE                                                         { $$ = OBJECT_TABLE; }
 
2835
                        | DOMAIN_P                                                      { $$ = OBJECT_TYPE; }
 
2836
                        | TYPE_P                                                        { $$ = OBJECT_TYPE; }
 
2837
                        | VIEW                                                          { $$ = OBJECT_VIEW; }
 
2838
                        | CONVERSION_P                                          { $$ = OBJECT_CONVERSION; }
 
2839
                ;
 
2840
 
 
2841
comment_text:
 
2842
                        Sconst                                                          { $$ = $1; }
 
2843
                        | NULL_P                                                        { $$ = NULL; }
 
2844
                ;
 
2845
 
 
2846
/*****************************************************************************
 
2847
 *
 
2848
 *              QUERY:
 
2849
 *                      fetch/move
 
2850
 *
 
2851
 *****************************************************************************/
 
2852
 
 
2853
FetchStmt:      FETCH fetch_direction from_in name
 
2854
                                {
 
2855
                                        FetchStmt *n = (FetchStmt *) $2;
 
2856
                                        n->portalname = $4;
 
2857
                                        n->ismove = FALSE;
 
2858
                                        $$ = (Node *)n;
 
2859
                                }
 
2860
                        | FETCH name
 
2861
                                {
 
2862
                                        FetchStmt *n = makeNode(FetchStmt);
 
2863
                                        n->direction = FETCH_FORWARD;
 
2864
                                        n->howMany = 1;
 
2865
                                        n->portalname = $2;
 
2866
                                        n->ismove = FALSE;
 
2867
                                        $$ = (Node *)n;
 
2868
                                }
 
2869
                        | MOVE fetch_direction from_in name
 
2870
                                {
 
2871
                                        FetchStmt *n = (FetchStmt *) $2;
 
2872
                                        n->portalname = $4;
 
2873
                                        n->ismove = TRUE;
 
2874
                                        $$ = (Node *)n;
 
2875
                                }
 
2876
                        | MOVE name
 
2877
                                {
 
2878
                                        FetchStmt *n = makeNode(FetchStmt);
 
2879
                                        n->direction = FETCH_FORWARD;
 
2880
                                        n->howMany = 1;
 
2881
                                        n->portalname = $2;
 
2882
                                        n->ismove = TRUE;
 
2883
                                        $$ = (Node *)n;
 
2884
                                }
 
2885
                ;
 
2886
 
 
2887
fetch_direction:
 
2888
                        /*EMPTY*/
 
2889
                                {
 
2890
                                        FetchStmt *n = makeNode(FetchStmt);
 
2891
                                        n->direction = FETCH_FORWARD;
 
2892
                                        n->howMany = 1;
 
2893
                                        $$ = (Node *)n;
 
2894
                                }
 
2895
                        | NEXT
 
2896
                                {
 
2897
                                        FetchStmt *n = makeNode(FetchStmt);
 
2898
                                        n->direction = FETCH_FORWARD;
 
2899
                                        n->howMany = 1;
 
2900
                                        $$ = (Node *)n;
 
2901
                                }
 
2902
                        | PRIOR
 
2903
                                {
 
2904
                                        FetchStmt *n = makeNode(FetchStmt);
 
2905
                                        n->direction = FETCH_BACKWARD;
 
2906
                                        n->howMany = 1;
 
2907
                                        $$ = (Node *)n;
 
2908
                                }
 
2909
                        | FIRST_P
 
2910
                                {
 
2911
                                        FetchStmt *n = makeNode(FetchStmt);
 
2912
                                        n->direction = FETCH_ABSOLUTE;
 
2913
                                        n->howMany = 1;
 
2914
                                        $$ = (Node *)n;
 
2915
                                }
 
2916
                        | LAST_P
 
2917
                                {
 
2918
                                        FetchStmt *n = makeNode(FetchStmt);
 
2919
                                        n->direction = FETCH_ABSOLUTE;
 
2920
                                        n->howMany = -1;
 
2921
                                        $$ = (Node *)n;
 
2922
                                }
 
2923
                        | ABSOLUTE_P fetch_count
 
2924
                                {
 
2925
                                        FetchStmt *n = makeNode(FetchStmt);
 
2926
                                        n->direction = FETCH_ABSOLUTE;
 
2927
                                        n->howMany = $2;
 
2928
                                        $$ = (Node *)n;
 
2929
                                }
 
2930
                        | RELATIVE_P fetch_count
 
2931
                                {
 
2932
                                        FetchStmt *n = makeNode(FetchStmt);
 
2933
                                        n->direction = FETCH_RELATIVE;
 
2934
                                        n->howMany = $2;
 
2935
                                        $$ = (Node *)n;
 
2936
                                }
 
2937
                        | fetch_count
 
2938
                                {
 
2939
                                        FetchStmt *n = makeNode(FetchStmt);
 
2940
                                        n->direction = FETCH_FORWARD;
 
2941
                                        n->howMany = $1;
 
2942
                                        $$ = (Node *)n;
 
2943
                                }
 
2944
                        | ALL
 
2945
                                {
 
2946
                                        FetchStmt *n = makeNode(FetchStmt);
 
2947
                                        n->direction = FETCH_FORWARD;
 
2948
                                        n->howMany = FETCH_ALL;
 
2949
                                        $$ = (Node *)n;
 
2950
                                }
 
2951
                        | FORWARD
 
2952
                                {
 
2953
                                        FetchStmt *n = makeNode(FetchStmt);
 
2954
                                        n->direction = FETCH_FORWARD;
 
2955
                                        n->howMany = 1;
 
2956
                                        $$ = (Node *)n;
 
2957
                                }
 
2958
                        | FORWARD fetch_count
 
2959
                                {
 
2960
                                        FetchStmt *n = makeNode(FetchStmt);
 
2961
                                        n->direction = FETCH_FORWARD;
 
2962
                                        n->howMany = $2;
 
2963
                                        $$ = (Node *)n;
 
2964
                                }
 
2965
                        | FORWARD ALL
 
2966
                                {
 
2967
                                        FetchStmt *n = makeNode(FetchStmt);
 
2968
                                        n->direction = FETCH_FORWARD;
 
2969
                                        n->howMany = FETCH_ALL;
 
2970
                                        $$ = (Node *)n;
 
2971
                                }
 
2972
                        | BACKWARD
 
2973
                                {
 
2974
                                        FetchStmt *n = makeNode(FetchStmt);
 
2975
                                        n->direction = FETCH_BACKWARD;
 
2976
                                        n->howMany = 1;
 
2977
                                        $$ = (Node *)n;
 
2978
                                }
 
2979
                        | BACKWARD fetch_count
 
2980
                                {
 
2981
                                        FetchStmt *n = makeNode(FetchStmt);
 
2982
                                        n->direction = FETCH_BACKWARD;
 
2983
                                        n->howMany = $2;
 
2984
                                        $$ = (Node *)n;
 
2985
                                }
 
2986
                        | BACKWARD ALL
 
2987
                                {
 
2988
                                        FetchStmt *n = makeNode(FetchStmt);
 
2989
                                        n->direction = FETCH_BACKWARD;
 
2990
                                        n->howMany = FETCH_ALL;
 
2991
                                        $$ = (Node *)n;
 
2992
                                }
 
2993
                ;
 
2994
 
 
2995
fetch_count:
 
2996
                        Iconst                                                                  { $$ = $1; }
 
2997
                        | '-' Iconst                                                    { $$ = - $2; }
 
2998
                ;
 
2999
 
 
3000
from_in:        FROM                                                                    {}
 
3001
                        | IN_P                                                                  {}
 
3002
                ;
 
3003
 
 
3004
 
 
3005
/*****************************************************************************
 
3006
 *
 
3007
 * GRANT and REVOKE statements
 
3008
 *
 
3009
 *****************************************************************************/
 
3010
 
 
3011
GrantStmt:      GRANT privileges ON privilege_target TO grantee_list
 
3012
                        opt_grant_grant_option
 
3013
                                {
 
3014
                                        GrantStmt *n = makeNode(GrantStmt);
 
3015
                                        n->is_grant = true;
 
3016
                                        n->privileges = $2;
 
3017
                                        n->objtype = ($4)->objtype;
 
3018
                                        n->objects = ($4)->objs;
 
3019
                                        n->grantees = $6;
 
3020
                                        n->grant_option = $7;
 
3021
                                        $$ = (Node*)n;
 
3022
                                }
 
3023
                ;
 
3024
 
 
3025
RevokeStmt: REVOKE opt_revoke_grant_option privileges ON privilege_target
 
3026
                        FROM grantee_list opt_drop_behavior
 
3027
                                {
 
3028
                                        GrantStmt *n = makeNode(GrantStmt);
 
3029
                                        n->is_grant = false;
 
3030
                                        n->privileges = $3;
 
3031
                                        n->objtype = ($5)->objtype;
 
3032
                                        n->objects = ($5)->objs;
 
3033
                                        n->grantees = $7;
 
3034
                                        n->grant_option = $2;
 
3035
                                        n->behavior = $8;
 
3036
 
 
3037
                                        $$ = (Node *)n;
 
3038
                                }
 
3039
                ;
 
3040
 
 
3041
 
 
3042
/* either ALL [PRIVILEGES] or a list of individual privileges */
 
3043
privileges: privilege_list                              { $$ = $1; }
 
3044
                        | ALL                                           { $$ = list_make1_int(ACL_ALL_RIGHTS); }
 
3045
                        | ALL PRIVILEGES                        { $$ = list_make1_int(ACL_ALL_RIGHTS); }
 
3046
                ;
 
3047
 
 
3048
privilege_list:
 
3049
                        privilege                                                               { $$ = list_make1_int($1); }
 
3050
                        | privilege_list ',' privilege                  { $$ = lappend_int($1, $3); }
 
3051
                ;
 
3052
 
 
3053
/* Not all of these privilege types apply to all objects, but that
 
3054
 * gets sorted out later.
 
3055
 */
 
3056
privilege:      SELECT                                                                  { $$ = ACL_SELECT; }
 
3057
                        | INSERT                                                                { $$ = ACL_INSERT; }
 
3058
                        | UPDATE                                                                { $$ = ACL_UPDATE; }
 
3059
                        | DELETE_P                                                              { $$ = ACL_DELETE; }
 
3060
                        | RULE                                                                  { $$ = ACL_RULE; }
 
3061
                        | REFERENCES                                                    { $$ = ACL_REFERENCES; }
 
3062
                        | TRIGGER                                                               { $$ = ACL_TRIGGER; }
 
3063
                        | EXECUTE                                                               { $$ = ACL_EXECUTE; }
 
3064
                        | USAGE                                                                 { $$ = ACL_USAGE; }
 
3065
                        | CREATE                                                                { $$ = ACL_CREATE; }
 
3066
                        | TEMPORARY                                                             { $$ = ACL_CREATE_TEMP; }
 
3067
                        | TEMP                                                                  { $$ = ACL_CREATE_TEMP; }
 
3068
                ;
 
3069
 
 
3070
 
 
3071
/* Don't bother trying to fold the first two rules into one using
 
3072
   opt_table.  You're going to get conflicts. */
 
3073
privilege_target:
 
3074
                        qualified_name_list
 
3075
                                {
 
3076
                                        PrivTarget *n = makeNode(PrivTarget);
 
3077
                                        n->objtype = ACL_OBJECT_RELATION;
 
3078
                                        n->objs = $1;
 
3079
                                        $$ = n;
 
3080
                                }
 
3081
                        | TABLE qualified_name_list
 
3082
                                {
 
3083
                                        PrivTarget *n = makeNode(PrivTarget);
 
3084
                                        n->objtype = ACL_OBJECT_RELATION;
 
3085
                                        n->objs = $2;
 
3086
                                        $$ = n;
 
3087
                                }
 
3088
                        | FUNCTION function_with_argtypes_list
 
3089
                                {
 
3090
                                        PrivTarget *n = makeNode(PrivTarget);
 
3091
                                        n->objtype = ACL_OBJECT_FUNCTION;
 
3092
                                        n->objs = $2;
 
3093
                                        $$ = n;
 
3094
                                }
 
3095
                        | DATABASE name_list
 
3096
                                {
 
3097
                                        PrivTarget *n = makeNode(PrivTarget);
 
3098
                                        n->objtype = ACL_OBJECT_DATABASE;
 
3099
                                        n->objs = $2;
 
3100
                                        $$ = n;
 
3101
                                }
 
3102
                        | LANGUAGE name_list
 
3103
                                {
 
3104
                                        PrivTarget *n = makeNode(PrivTarget);
 
3105
                                        n->objtype = ACL_OBJECT_LANGUAGE;
 
3106
                                        n->objs = $2;
 
3107
                                        $$ = n;
 
3108
                                }
 
3109
                        | SCHEMA name_list
 
3110
                                {
 
3111
                                        PrivTarget *n = makeNode(PrivTarget);
 
3112
                                        n->objtype = ACL_OBJECT_NAMESPACE;
 
3113
                                        n->objs = $2;
 
3114
                                        $$ = n;
 
3115
                                }
 
3116
                        | TABLESPACE name_list
 
3117
                                {
 
3118
                                        PrivTarget *n = makeNode(PrivTarget);
 
3119
                                        n->objtype = ACL_OBJECT_TABLESPACE;
 
3120
                                        n->objs = $2;
 
3121
                                        $$ = n;
 
3122
                                }
 
3123
                ;
 
3124
 
 
3125
 
 
3126
grantee_list:
 
3127
                        grantee                                                                 { $$ = list_make1($1); }
 
3128
                        | grantee_list ',' grantee                              { $$ = lappend($1, $3); }
 
3129
                ;
 
3130
 
 
3131
grantee:        ColId
 
3132
                                {
 
3133
                                        PrivGrantee *n = makeNode(PrivGrantee);
 
3134
                                        /* This hack lets us avoid reserving PUBLIC as a keyword*/
 
3135
                                        if (strcmp($1, "public") == 0)
 
3136
                                                n->username = NULL;
 
3137
                                        else
 
3138
                                                n->username = $1;
 
3139
                                        n->groupname = NULL;
 
3140
                                        $$ = (Node *)n;
 
3141
                                }
 
3142
                        | GROUP_P ColId
 
3143
                                {
 
3144
                                        PrivGrantee *n = makeNode(PrivGrantee);
 
3145
                                        /* Treat GROUP PUBLIC as a synonym for PUBLIC */
 
3146
                                        if (strcmp($2, "public") == 0)
 
3147
                                                n->groupname = NULL;
 
3148
                                        else
 
3149
                                                n->groupname = $2;
 
3150
                                        n->username = NULL;
 
3151
                                        $$ = (Node *)n;
 
3152
                                }
 
3153
                ;
 
3154
 
 
3155
 
 
3156
opt_grant_grant_option:
 
3157
                        WITH GRANT OPTION { $$ = TRUE; }
 
3158
                        | /*EMPTY*/ { $$ = FALSE; }
 
3159
                ;
 
3160
 
 
3161
opt_revoke_grant_option:
 
3162
                        GRANT OPTION FOR { $$ = TRUE; }
 
3163
                        | /*EMPTY*/ { $$ = FALSE; }
 
3164
                ;
 
3165
 
 
3166
 
 
3167
function_with_argtypes_list:
 
3168
                        function_with_argtypes                                  { $$ = list_make1($1); }
 
3169
                        | function_with_argtypes_list ',' function_with_argtypes
 
3170
                                                                                                        { $$ = lappend($1, $3); }
 
3171
                ;
 
3172
 
 
3173
function_with_argtypes:
 
3174
                        func_name func_args
 
3175
                                {
 
3176
                                        FuncWithArgs *n = makeNode(FuncWithArgs);
 
3177
                                        n->funcname = $1;
 
3178
                                        n->funcargs = extractArgTypes($2);
 
3179
                                        $$ = (Node *)n;
 
3180
                                }
 
3181
                ;
 
3182
 
 
3183
 
 
3184
/*****************************************************************************
 
3185
 *
 
3186
 *              QUERY:
 
3187
 *                              create index <indexname> on <relname>
 
3188
 *                                [ using <access> ] "(" ( <col> [ using <opclass> ] )+ ")"
 
3189
 *                                [ tablespace <tablespacename> ] [ where <predicate> ]
 
3190
 *
 
3191
 * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
 
3192
 * willing to make TABLESPACE a fully reserved word.
 
3193
 *****************************************************************************/
 
3194
 
 
3195
IndexStmt:      CREATE index_opt_unique INDEX index_name ON qualified_name
 
3196
                        access_method_clause '(' index_params ')' OptTableSpace where_clause
 
3197
                                {
 
3198
                                        IndexStmt *n = makeNode(IndexStmt);
 
3199
                                        n->unique = $2;
 
3200
                                        n->idxname = $4;
 
3201
                                        n->relation = $6;
 
3202
                                        n->accessMethod = $7;
 
3203
                                        n->indexParams = $9;
 
3204
                                        n->tableSpace = $11;
 
3205
                                        n->whereClause = $12;
 
3206
                                        $$ = (Node *)n;
 
3207
                                }
 
3208
                ;
 
3209
 
 
3210
index_opt_unique:
 
3211
                        UNIQUE                                                                  { $$ = TRUE; }
 
3212
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
3213
                ;
 
3214
 
 
3215
access_method_clause:
 
3216
                        USING access_method                                             { $$ = $2; }
 
3217
                        | /*EMPTY*/                                                             { $$ = DEFAULT_INDEX_TYPE; }
 
3218
                ;
 
3219
 
 
3220
index_params:   index_elem                                                      { $$ = list_make1($1); }
 
3221
                        | index_params ',' index_elem                   { $$ = lappend($1, $3); }
 
3222
                ;
 
3223
 
 
3224
/*
 
3225
 * Index attributes can be either simple column references, or arbitrary
 
3226
 * expressions in parens.  For backwards-compatibility reasons, we allow
 
3227
 * an expression that's just a function call to be written without parens.
 
3228
 */
 
3229
index_elem:     ColId opt_class
 
3230
                                {
 
3231
                                        $$ = makeNode(IndexElem);
 
3232
                                        $$->name = $1;
 
3233
                                        $$->expr = NULL;
 
3234
                                        $$->opclass = $2;
 
3235
                                }
 
3236
                        | func_expr opt_class
 
3237
                                {
 
3238
                                        $$ = makeNode(IndexElem);
 
3239
                                        $$->name = NULL;
 
3240
                                        $$->expr = $1;
 
3241
                                        $$->opclass = $2;
 
3242
                                }
 
3243
                        | '(' a_expr ')' opt_class
 
3244
                                {
 
3245
                                        $$ = makeNode(IndexElem);
 
3246
                                        $$->name = NULL;
 
3247
                                        $$->expr = $2;
 
3248
                                        $$->opclass = $4;
 
3249
                                }
 
3250
                ;
 
3251
 
 
3252
opt_class:      any_name                                                                { $$ = $1; }
 
3253
                        | USING any_name                                                { $$ = $2; }
 
3254
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
3255
                ;
 
3256
 
 
3257
/*****************************************************************************
 
3258
 *
 
3259
 *              QUERY:
 
3260
 *                              create [or replace] function <fname>
 
3261
 *                                              [(<type-1> { , <type-n>})]
 
3262
 *                                              returns <type-r>
 
3263
 *                                              as <filename or code in language as appropriate>
 
3264
 *                                              language <lang> [with parameters]
 
3265
 *
 
3266
 *****************************************************************************/
 
3267
 
 
3268
CreateFunctionStmt:
 
3269
                        CREATE opt_or_replace FUNCTION func_name func_args
 
3270
                        RETURNS func_return createfunc_opt_list opt_definition
 
3271
                                {
 
3272
                                        CreateFunctionStmt *n = makeNode(CreateFunctionStmt);
 
3273
                                        n->replace = $2;
 
3274
                                        n->funcname = $4;
 
3275
                                        n->parameters = $5;
 
3276
                                        n->returnType = $7;
 
3277
                                        n->options = $8;
 
3278
                                        n->withClause = $9;
 
3279
                                        $$ = (Node *)n;
 
3280
                                }
 
3281
                ;
 
3282
 
 
3283
opt_or_replace:
 
3284
                        OR REPLACE                                                              { $$ = TRUE; }
 
3285
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
3286
                ;
 
3287
 
 
3288
func_args:      '(' func_args_list ')'                                  { $$ = $2; }
 
3289
                        | '(' ')'                                                               { $$ = NIL; }
 
3290
                ;
 
3291
 
 
3292
func_args_list:
 
3293
                        func_arg                                                                { $$ = list_make1($1); }
 
3294
                        | func_args_list ',' func_arg                   { $$ = lappend($1, $3); }
 
3295
                ;
 
3296
 
 
3297
/* We can catch over-specified arguments here if we want to,
 
3298
 * but for now better to silently swallow typmod, etc.
 
3299
 * - thomas 2000-03-22
 
3300
 */
 
3301
func_arg:
 
3302
                        arg_class param_name func_type
 
3303
                                {
 
3304
                                        FunctionParameter *n = makeNode(FunctionParameter);
 
3305
                                        n->name = $2;
 
3306
                                        n->argType = $3;
 
3307
                                        $$ = n;
 
3308
                                }
 
3309
                        | arg_class func_type
 
3310
                                {
 
3311
                                        FunctionParameter *n = makeNode(FunctionParameter);
 
3312
                                        n->name = NULL;
 
3313
                                        n->argType = $2;
 
3314
                                        $$ = n;
 
3315
                                }
 
3316
                ;
 
3317
 
 
3318
arg_class:      IN_P                                                                    { $$ = FALSE; }
 
3319
                        | OUT_P
 
3320
                                {
 
3321
                                        ereport(ERROR,
 
3322
                                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
3323
                                                         errmsg("CREATE FUNCTION / OUT parameters are not implemented")));
 
3324
                                        $$ = TRUE;
 
3325
                                }
 
3326
                        | INOUT
 
3327
                                {
 
3328
                                        ereport(ERROR,
 
3329
                                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
3330
                                                         errmsg("CREATE FUNCTION / INOUT parameters are not implemented")));
 
3331
                                        $$ = FALSE;
 
3332
                                }
 
3333
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
3334
                ;
 
3335
 
 
3336
/*
 
3337
 * Ideally param_name should be ColId, but that causes too many conflicts.
 
3338
 */
 
3339
param_name:     function_name
 
3340
                ;
 
3341
 
 
3342
func_return:
 
3343
                        func_type
 
3344
                                {
 
3345
                                        /* We can catch over-specified arguments here if we want to,
 
3346
                                         * but for now better to silently swallow typmod, etc.
 
3347
                                         * - thomas 2000-03-22
 
3348
                                         */
 
3349
                                        $$ = $1;
 
3350
                                }
 
3351
                ;
 
3352
 
 
3353
/*
 
3354
 * We would like to make the second production here be ColId attrs etc,
 
3355
 * but that causes reduce/reduce conflicts.  type_name is next best choice.
 
3356
 */
 
3357
func_type:      Typename                                                                { $$ = $1; }
 
3358
                        | type_name attrs '%' TYPE_P
 
3359
                                {
 
3360
                                        $$ = makeNode(TypeName);
 
3361
                                        $$->names = lcons(makeString($1), $2);
 
3362
                                        $$->pct_type = true;
 
3363
                                        $$->typmod = -1;
 
3364
                                }
 
3365
                ;
 
3366
 
 
3367
 
 
3368
createfunc_opt_list:
 
3369
                        /* Must be at least one to prevent conflict */
 
3370
                        createfunc_opt_item                     { $$ = list_make1($1); }
 
3371
                        | createfunc_opt_list createfunc_opt_item { $$ = lappend($1, $2); }
 
3372
        ;
 
3373
 
 
3374
createfunc_opt_item:
 
3375
                        AS func_as
 
3376
                                {
 
3377
                                        $$ = makeDefElem("as", (Node *)$2);
 
3378
                                }
 
3379
                        | LANGUAGE ColId_or_Sconst
 
3380
                                {
 
3381
                                        $$ = makeDefElem("language", (Node *)makeString($2));
 
3382
                                }
 
3383
                        | IMMUTABLE
 
3384
                                {
 
3385
                                        $$ = makeDefElem("volatility", (Node *)makeString("immutable"));
 
3386
                                }
 
3387
                        | STABLE
 
3388
                                {
 
3389
                                        $$ = makeDefElem("volatility", (Node *)makeString("stable"));
 
3390
                                }
 
3391
                        | VOLATILE
 
3392
                                {
 
3393
                                        $$ = makeDefElem("volatility", (Node *)makeString("volatile"));
 
3394
                                }
 
3395
                        | CALLED ON NULL_P INPUT_P
 
3396
                                {
 
3397
                                        $$ = makeDefElem("strict", (Node *)makeInteger(FALSE));
 
3398
                                }
 
3399
                        | RETURNS NULL_P ON NULL_P INPUT_P
 
3400
                                {
 
3401
                                        $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
 
3402
                                }
 
3403
                        | STRICT_P
 
3404
                                {
 
3405
                                        $$ = makeDefElem("strict", (Node *)makeInteger(TRUE));
 
3406
                                }
 
3407
                        | EXTERNAL SECURITY DEFINER
 
3408
                                {
 
3409
                                        $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
 
3410
                                }
 
3411
                        | EXTERNAL SECURITY INVOKER
 
3412
                                {
 
3413
                                        $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
 
3414
                                }
 
3415
                        | SECURITY DEFINER
 
3416
                                {
 
3417
                                        $$ = makeDefElem("security", (Node *)makeInteger(TRUE));
 
3418
                                }
 
3419
                        | SECURITY INVOKER
 
3420
                                {
 
3421
                                        $$ = makeDefElem("security", (Node *)makeInteger(FALSE));
 
3422
                                }
 
3423
                ;
 
3424
 
 
3425
func_as:        Sconst                                          { $$ = list_make1(makeString($1)); }
 
3426
                        | Sconst ',' Sconst
 
3427
                                {
 
3428
                                        $$ = list_make2(makeString($1), makeString($3));
 
3429
                                }
 
3430
                ;
 
3431
 
 
3432
opt_definition:
 
3433
                        WITH definition                                                 { $$ = $2; }
 
3434
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
3435
                ;
 
3436
 
 
3437
 
 
3438
/*****************************************************************************
 
3439
 *
 
3440
 *              QUERY:
 
3441
 *
 
3442
 *              DROP FUNCTION funcname (arg1, arg2, ...) [ RESTRICT | CASCADE ]
 
3443
 *              DROP AGGREGATE aggname (aggtype) [ RESTRICT | CASCADE ]
 
3444
 *              DROP OPERATOR opname (leftoperand_typ, rightoperand_typ) [ RESTRICT | CASCADE ]
 
3445
 *
 
3446
 *****************************************************************************/
 
3447
 
 
3448
RemoveFuncStmt:
 
3449
                        DROP FUNCTION func_name func_args opt_drop_behavior
 
3450
                                {
 
3451
                                        RemoveFuncStmt *n = makeNode(RemoveFuncStmt);
 
3452
                                        n->funcname = $3;
 
3453
                                        n->args = extractArgTypes($4);
 
3454
                                        n->behavior = $5;
 
3455
                                        $$ = (Node *)n;
 
3456
                                }
 
3457
                ;
 
3458
 
 
3459
RemoveAggrStmt:
 
3460
                        DROP AGGREGATE func_name '(' aggr_argtype ')' opt_drop_behavior
 
3461
                                {
 
3462
                                                RemoveAggrStmt *n = makeNode(RemoveAggrStmt);
 
3463
                                                n->aggname = $3;
 
3464
                                                n->aggtype = $5;
 
3465
                                                n->behavior = $7;
 
3466
                                                $$ = (Node *)n;
 
3467
                                }
 
3468
                ;
 
3469
 
 
3470
aggr_argtype:
 
3471
                        Typename                                                                { $$ = $1; }
 
3472
                        | '*'                                                                   { $$ = NULL; }
 
3473
                ;
 
3474
 
 
3475
RemoveOperStmt:
 
3476
                        DROP OPERATOR any_operator '(' oper_argtypes ')' opt_drop_behavior
 
3477
                                {
 
3478
                                        RemoveOperStmt *n = makeNode(RemoveOperStmt);
 
3479
                                        n->opname = $3;
 
3480
                                        n->args = $5;
 
3481
                                        n->behavior = $7;
 
3482
                                        $$ = (Node *)n;
 
3483
                                }
 
3484
                ;
 
3485
 
 
3486
oper_argtypes:
 
3487
                        Typename
 
3488
                                {
 
3489
                                   ereport(ERROR,
 
3490
                                                   (errcode(ERRCODE_SYNTAX_ERROR),
 
3491
                                                        errmsg("missing argument"),
 
3492
                                                        errhint("Use NONE to denote the missing argument of a unary operator.")));
 
3493
                                }
 
3494
                        | Typename ',' Typename
 
3495
                                        { $$ = list_make2($1, $3); }
 
3496
                        | NONE ',' Typename /* left unary */
 
3497
                                        { $$ = list_make2(NULL, $3); }
 
3498
                        | Typename ',' NONE /* right unary */
 
3499
                                        { $$ = list_make2($1, NULL); }
 
3500
                ;
 
3501
 
 
3502
any_operator:
 
3503
                        all_Op
 
3504
                                        { $$ = list_make1(makeString($1)); }
 
3505
                        | ColId '.' any_operator
 
3506
                                        { $$ = lcons(makeString($1), $3); }
 
3507
                ;
 
3508
 
 
3509
 
 
3510
/*****************************************************************************
 
3511
 *
 
3512
 *              CREATE CAST / DROP CAST
 
3513
 *
 
3514
 *****************************************************************************/
 
3515
 
 
3516
CreateCastStmt: CREATE CAST '(' Typename AS Typename ')'
 
3517
                                        WITH FUNCTION function_with_argtypes cast_context
 
3518
                                {
 
3519
                                        CreateCastStmt *n = makeNode(CreateCastStmt);
 
3520
                                        n->sourcetype = $4;
 
3521
                                        n->targettype = $6;
 
3522
                                        n->func = (FuncWithArgs *) $10;
 
3523
                                        n->context = (CoercionContext) $11;
 
3524
                                        $$ = (Node *)n;
 
3525
                                }
 
3526
                        | CREATE CAST '(' Typename AS Typename ')'
 
3527
                                        WITHOUT FUNCTION cast_context
 
3528
                                {
 
3529
                                        CreateCastStmt *n = makeNode(CreateCastStmt);
 
3530
                                        n->sourcetype = $4;
 
3531
                                        n->targettype = $6;
 
3532
                                        n->func = NULL;
 
3533
                                        n->context = (CoercionContext) $10;
 
3534
                                        $$ = (Node *)n;
 
3535
                                }
 
3536
                ;
 
3537
 
 
3538
cast_context:  AS IMPLICIT_P                                    { $$ = COERCION_IMPLICIT; }
 
3539
                | AS ASSIGNMENT                                                 { $$ = COERCION_ASSIGNMENT; }
 
3540
                | /*EMPTY*/                                                             { $$ = COERCION_EXPLICIT; }
 
3541
                ;
 
3542
 
 
3543
 
 
3544
DropCastStmt: DROP CAST '(' Typename AS Typename ')' opt_drop_behavior
 
3545
                                {
 
3546
                                        DropCastStmt *n = makeNode(DropCastStmt);
 
3547
                                        n->sourcetype = $4;
 
3548
                                        n->targettype = $6;
 
3549
                                        n->behavior = $8;
 
3550
                                        $$ = (Node *)n;
 
3551
                                }
 
3552
                ;
 
3553
 
 
3554
 
 
3555
 
 
3556
/*****************************************************************************
 
3557
 *
 
3558
 *              QUERY:
 
3559
 *
 
3560
 *              REINDEX type <typename> [FORCE] [ALL]
 
3561
 *
 
3562
 *****************************************************************************/
 
3563
 
 
3564
ReindexStmt:
 
3565
                        REINDEX reindex_type qualified_name opt_force
 
3566
                                {
 
3567
                                        ReindexStmt *n = makeNode(ReindexStmt);
 
3568
                                        n->kind = $2;
 
3569
                                        n->relation = $3;
 
3570
                                        n->name = NULL;
 
3571
                                        n->force = $4;
 
3572
                                        $$ = (Node *)n;
 
3573
                                }
 
3574
                        | REINDEX DATABASE name opt_force
 
3575
                                {
 
3576
                                        ReindexStmt *n = makeNode(ReindexStmt);
 
3577
                                        n->kind = OBJECT_DATABASE;
 
3578
                                        n->name = $3;
 
3579
                                        n->relation = NULL;
 
3580
                                        n->force = $4;
 
3581
                                        $$ = (Node *)n;
 
3582
                                }
 
3583
                ;
 
3584
 
 
3585
reindex_type:
 
3586
                        INDEX                                                                   { $$ = OBJECT_INDEX; }
 
3587
                        | TABLE                                                                 { $$ = OBJECT_TABLE; }
 
3588
                ;
 
3589
 
 
3590
opt_force:      FORCE                                                                   {  $$ = TRUE; }
 
3591
                        | /* EMPTY */                                                   {  $$ = FALSE; }
 
3592
                ;
 
3593
 
 
3594
 
 
3595
/*****************************************************************************
 
3596
 *
 
3597
 * ALTER THING name RENAME TO newname
 
3598
 *
 
3599
 *****************************************************************************/
 
3600
 
 
3601
RenameStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' RENAME TO name
 
3602
                                {
 
3603
                                        RenameStmt *n = makeNode(RenameStmt);
 
3604
                                        n->renameType = OBJECT_AGGREGATE;
 
3605
                                        n->object = $3;
 
3606
                                        n->objarg = list_make1($5);
 
3607
                                        n->newname = $9;
 
3608
                                        $$ = (Node *)n;
 
3609
                                }
 
3610
                        | ALTER CONVERSION_P any_name RENAME TO name
 
3611
                                {
 
3612
                                        RenameStmt *n = makeNode(RenameStmt);
 
3613
                                        n->renameType = OBJECT_CONVERSION;
 
3614
                                        n->object = $3;
 
3615
                                        n->newname = $6;
 
3616
                                        $$ = (Node *)n;
 
3617
                                }
 
3618
                        | ALTER DATABASE database_name RENAME TO database_name
 
3619
                                {
 
3620
                                        RenameStmt *n = makeNode(RenameStmt);
 
3621
                                        n->renameType = OBJECT_DATABASE;
 
3622
                                        n->subname = $3;
 
3623
                                        n->newname = $6;
 
3624
                                        $$ = (Node *)n;
 
3625
                                }
 
3626
                        | ALTER FUNCTION func_name func_args RENAME TO name
 
3627
                                {
 
3628
                                        RenameStmt *n = makeNode(RenameStmt);
 
3629
                                        n->renameType = OBJECT_FUNCTION;
 
3630
                                        n->object = $3;
 
3631
                                        n->objarg = extractArgTypes($4);
 
3632
                                        n->newname = $7;
 
3633
                                        $$ = (Node *)n;
 
3634
                                }
 
3635
                        | ALTER GROUP_P UserId RENAME TO UserId
 
3636
                                {
 
3637
                                        RenameStmt *n = makeNode(RenameStmt);
 
3638
                                        n->renameType = OBJECT_GROUP;
 
3639
                                        n->subname = $3;
 
3640
                                        n->newname = $6;
 
3641
                                        $$ = (Node *)n;
 
3642
                                }
 
3643
                        | ALTER LANGUAGE name RENAME TO name
 
3644
                                {
 
3645
                                        RenameStmt *n = makeNode(RenameStmt);
 
3646
                                        n->renameType = OBJECT_LANGUAGE;
 
3647
                                        n->subname = $3;
 
3648
                                        n->newname = $6;
 
3649
                                        $$ = (Node *)n;
 
3650
                                }
 
3651
                        | ALTER OPERATOR CLASS any_name USING access_method RENAME TO name
 
3652
                                {
 
3653
                                        RenameStmt *n = makeNode(RenameStmt);
 
3654
                                        n->renameType = OBJECT_OPCLASS;
 
3655
                                        n->object = $4;
 
3656
                                        n->subname = $6;
 
3657
                                        n->newname = $9;
 
3658
                                        $$ = (Node *)n;
 
3659
                                }
 
3660
                        | ALTER SCHEMA name RENAME TO name
 
3661
                                {
 
3662
                                        RenameStmt *n = makeNode(RenameStmt);
 
3663
                                        n->renameType = OBJECT_SCHEMA;
 
3664
                                        n->subname = $3;
 
3665
                                        n->newname = $6;
 
3666
                                        $$ = (Node *)n;
 
3667
                                }
 
3668
                        | ALTER TABLE relation_expr RENAME TO name
 
3669
                                {
 
3670
                                        RenameStmt *n = makeNode(RenameStmt);
 
3671
                                        n->renameType = OBJECT_TABLE;
 
3672
                                        n->relation = $3;
 
3673
                                        n->subname = NULL;
 
3674
                                        n->newname = $6;
 
3675
                                        $$ = (Node *)n;
 
3676
                                }
 
3677
                        | ALTER INDEX relation_expr RENAME TO name
 
3678
                                {
 
3679
                                        RenameStmt *n = makeNode(RenameStmt);
 
3680
                                        n->renameType = OBJECT_INDEX;
 
3681
                                        n->relation = $3;
 
3682
                                        n->subname = NULL;
 
3683
                                        n->newname = $6;
 
3684
                                        $$ = (Node *)n;
 
3685
                                }
 
3686
                        | ALTER TABLE relation_expr RENAME opt_column name TO name
 
3687
                                {
 
3688
                                        RenameStmt *n = makeNode(RenameStmt);
 
3689
                                        n->renameType = OBJECT_COLUMN;
 
3690
                                        n->relation = $3;
 
3691
                                        n->subname = $6;
 
3692
                                        n->newname = $8;
 
3693
                                        $$ = (Node *)n;
 
3694
                                }
 
3695
                        | ALTER TRIGGER name ON relation_expr RENAME TO name
 
3696
                                {
 
3697
                                        RenameStmt *n = makeNode(RenameStmt);
 
3698
                                        n->relation = $5;
 
3699
                                        n->subname = $3;
 
3700
                                        n->newname = $8;
 
3701
                                        n->renameType = OBJECT_TRIGGER;
 
3702
                                        $$ = (Node *)n;
 
3703
                                }
 
3704
                        | ALTER USER UserId RENAME TO UserId
 
3705
                                {
 
3706
                                        RenameStmt *n = makeNode(RenameStmt);
 
3707
                                        n->renameType = OBJECT_USER;
 
3708
                                        n->subname = $3;
 
3709
                                        n->newname = $6;
 
3710
                                        $$ = (Node *)n;
 
3711
                                }
 
3712
                        | ALTER TABLESPACE name RENAME TO name
 
3713
                                {
 
3714
                                        RenameStmt *n = makeNode(RenameStmt);
 
3715
                                        n->renameType = OBJECT_TABLESPACE;
 
3716
                                        n->subname = $3;
 
3717
                                        n->newname = $6;
 
3718
                                        $$ = (Node *)n;
 
3719
                                }
 
3720
                ;
 
3721
 
 
3722
opt_column: COLUMN                                                                      { $$ = COLUMN; }
 
3723
                        | /*EMPTY*/                                                             { $$ = 0; }
 
3724
                ;
 
3725
 
 
3726
 
 
3727
/*****************************************************************************
 
3728
 *
 
3729
 * ALTER THING name OWNER TO newname.  
 
3730
 *
 
3731
 *****************************************************************************/
 
3732
 
 
3733
AlterOwnerStmt: ALTER AGGREGATE func_name '(' aggr_argtype ')' OWNER TO UserId
 
3734
                                {
 
3735
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3736
                                        n->objectType = OBJECT_AGGREGATE;
 
3737
                                        n->object = $3;
 
3738
                                        n->objarg = list_make1($5);
 
3739
                                        n->newowner = $9;
 
3740
                                        $$ = (Node *)n;
 
3741
                                }
 
3742
                        | ALTER CONVERSION_P any_name OWNER TO UserId
 
3743
                                {
 
3744
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3745
                                        n->objectType = OBJECT_CONVERSION;
 
3746
                                        n->object = $3;
 
3747
                                        n->newowner = $6;
 
3748
                                        $$ = (Node *)n;
 
3749
                                }
 
3750
                        | ALTER DATABASE database_name OWNER TO UserId
 
3751
                                {
 
3752
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3753
                                        n->objectType = OBJECT_DATABASE;
 
3754
                                        n->object = list_make1($3);
 
3755
                                        n->newowner = $6;
 
3756
                                        $$ = (Node *)n;
 
3757
                                }
 
3758
                        | ALTER DOMAIN_P any_name OWNER TO UserId
 
3759
                                {
 
3760
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3761
                                        n->objectType = OBJECT_DOMAIN;
 
3762
                                        n->object = $3;
 
3763
                                        n->newowner = $6;
 
3764
                                        $$ = (Node *)n;
 
3765
                                }
 
3766
                        | ALTER FUNCTION func_name func_args OWNER TO UserId
 
3767
                                {
 
3768
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3769
                                        n->objectType = OBJECT_FUNCTION;
 
3770
                                        n->object = $3;
 
3771
                                        n->objarg = extractArgTypes($4);
 
3772
                                        n->newowner = $7;
 
3773
                                        $$ = (Node *)n;
 
3774
                                }
 
3775
                        | ALTER OPERATOR any_operator '(' oper_argtypes ')' OWNER TO UserId
 
3776
                                {
 
3777
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3778
                                        n->objectType = OBJECT_OPERATOR;
 
3779
                                        n->object = $3;
 
3780
                                        n->objarg = $5;
 
3781
                                        n->newowner = $9;
 
3782
                                        $$ = (Node *)n;
 
3783
                                }
 
3784
                        | ALTER OPERATOR CLASS any_name USING access_method OWNER TO UserId
 
3785
                                {
 
3786
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3787
                                        n->objectType = OBJECT_OPCLASS;
 
3788
                                        n->object = $4;
 
3789
                                        n->addname = $6;
 
3790
                                        n->newowner = $9;
 
3791
                                        $$ = (Node *)n;
 
3792
                                }
 
3793
                        | ALTER SCHEMA name OWNER TO UserId
 
3794
                                {
 
3795
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3796
                                        n->objectType = OBJECT_SCHEMA;
 
3797
                                        n->object = list_make1($3);
 
3798
                                        n->newowner = $6;
 
3799
                                        $$ = (Node *)n;
 
3800
                                }
 
3801
                        | ALTER TYPE_P any_name OWNER TO UserId
 
3802
                                {
 
3803
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3804
                                        n->objectType = OBJECT_TYPE;
 
3805
                                        n->object = $3;
 
3806
                                        n->newowner = $6;
 
3807
                                        $$ = (Node *)n;
 
3808
                                }
 
3809
                        | ALTER TABLESPACE name OWNER TO UserId
 
3810
                                {
 
3811
                                        AlterOwnerStmt *n = makeNode(AlterOwnerStmt);
 
3812
                                        n->objectType = OBJECT_TABLESPACE;
 
3813
                                        n->object = list_make1($3);
 
3814
                                        n->newowner = $6;
 
3815
                                        $$ = (Node *)n;
 
3816
                                }
 
3817
                ;
 
3818
 
 
3819
 
 
3820
/*****************************************************************************
 
3821
 *
 
3822
 *              QUERY:  Define Rewrite Rule
 
3823
 *
 
3824
 *****************************************************************************/
 
3825
 
 
3826
RuleStmt:       CREATE opt_or_replace RULE name AS
 
3827
                        { QueryIsRule=TRUE; }
 
3828
                        ON event TO qualified_name where_clause
 
3829
                        DO opt_instead RuleActionList
 
3830
                                {
 
3831
                                        RuleStmt *n = makeNode(RuleStmt);
 
3832
                                        n->replace = $2;
 
3833
                                        n->relation = $10;
 
3834
                                        n->rulename = $4;
 
3835
                                        n->whereClause = $11;
 
3836
                                        n->event = $8;
 
3837
                                        n->instead = $13;
 
3838
                                        n->actions = $14;
 
3839
                                        $$ = (Node *)n;
 
3840
                                        QueryIsRule=FALSE;
 
3841
                                }
 
3842
                ;
 
3843
 
 
3844
RuleActionList:
 
3845
                        NOTHING                                                                 { $$ = NIL; }
 
3846
                        | RuleActionStmt                                                { $$ = list_make1($1); }
 
3847
                        | '(' RuleActionMulti ')'                               { $$ = $2; }
 
3848
                ;
 
3849
 
 
3850
/* the thrashing around here is to discard "empty" statements... */
 
3851
RuleActionMulti:
 
3852
                        RuleActionMulti ';' RuleActionStmtOrEmpty
 
3853
                                { if ($3 != NULL)
 
3854
                                        $$ = lappend($1, $3);
 
3855
                                  else
 
3856
                                        $$ = $1;
 
3857
                                }
 
3858
                        | RuleActionStmtOrEmpty
 
3859
                                { if ($1 != NULL)
 
3860
                                        $$ = list_make1($1);
 
3861
                                  else
 
3862
                                        $$ = NIL;
 
3863
                                }
 
3864
                ;
 
3865
 
 
3866
RuleActionStmt:
 
3867
                        SelectStmt
 
3868
                        | InsertStmt
 
3869
                        | UpdateStmt
 
3870
                        | DeleteStmt
 
3871
                        | NotifyStmt
 
3872
                ;
 
3873
 
 
3874
RuleActionStmtOrEmpty:
 
3875
                        RuleActionStmt                                                  { $$ = $1; }
 
3876
                        |       /*EMPTY*/                                                       { $$ = NULL; }
 
3877
                ;
 
3878
 
 
3879
/* change me to select, update, etc. some day */
 
3880
event:          SELECT                                                                  { $$ = CMD_SELECT; }
 
3881
                        | UPDATE                                                                { $$ = CMD_UPDATE; }
 
3882
                        | DELETE_P                                                              { $$ = CMD_DELETE; }
 
3883
                        | INSERT                                                                { $$ = CMD_INSERT; }
 
3884
                 ;
 
3885
 
 
3886
opt_instead:
 
3887
                        INSTEAD                                                                 { $$ = TRUE; }
 
3888
                        | ALSO                                                                  { $$ = FALSE; }
 
3889
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
3890
                ;
 
3891
 
 
3892
 
 
3893
DropRuleStmt:
 
3894
                        DROP RULE name ON qualified_name opt_drop_behavior
 
3895
                                {
 
3896
                                        DropPropertyStmt *n = makeNode(DropPropertyStmt);
 
3897
                                        n->relation = $5;
 
3898
                                        n->property = $3;
 
3899
                                        n->behavior = $6;
 
3900
                                        n->removeType = OBJECT_RULE;
 
3901
                                        $$ = (Node *) n;
 
3902
                                }
 
3903
                ;
 
3904
 
 
3905
 
 
3906
/*****************************************************************************
 
3907
 *
 
3908
 *              QUERY:
 
3909
 *                              NOTIFY <qualified_name> can appear both in rule bodies and
 
3910
 *                              as a query-level command
 
3911
 *
 
3912
 *****************************************************************************/
 
3913
 
 
3914
NotifyStmt: NOTIFY qualified_name
 
3915
                                {
 
3916
                                        NotifyStmt *n = makeNode(NotifyStmt);
 
3917
                                        n->relation = $2;
 
3918
                                        $$ = (Node *)n;
 
3919
                                }
 
3920
                ;
 
3921
 
 
3922
ListenStmt: LISTEN qualified_name
 
3923
                                {
 
3924
                                        ListenStmt *n = makeNode(ListenStmt);
 
3925
                                        n->relation = $2;
 
3926
                                        $$ = (Node *)n;
 
3927
                                }
 
3928
                ;
 
3929
 
 
3930
UnlistenStmt:
 
3931
                        UNLISTEN qualified_name
 
3932
                                {
 
3933
                                        UnlistenStmt *n = makeNode(UnlistenStmt);
 
3934
                                        n->relation = $2;
 
3935
                                        $$ = (Node *)n;
 
3936
                                }
 
3937
                        | UNLISTEN '*'
 
3938
                                {
 
3939
                                        UnlistenStmt *n = makeNode(UnlistenStmt);
 
3940
                                        n->relation = makeNode(RangeVar);
 
3941
                                        n->relation->relname = "*";
 
3942
                                        n->relation->schemaname = NULL;
 
3943
                                        $$ = (Node *)n;
 
3944
                                }
 
3945
                ;
 
3946
 
 
3947
 
 
3948
/*****************************************************************************
 
3949
 *
 
3950
 *              Transactions:
 
3951
 *
 
3952
 *              BEGIN / COMMIT / ROLLBACK
 
3953
 *              (also older versions END / ABORT)
 
3954
 *
 
3955
 *****************************************************************************/
 
3956
 
 
3957
TransactionStmt:
 
3958
                        ABORT_P opt_transaction
 
3959
                                {
 
3960
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3961
                                        n->kind = TRANS_STMT_ROLLBACK;
 
3962
                                        n->options = NIL;
 
3963
                                        $$ = (Node *)n;
 
3964
                                }
 
3965
                        | BEGIN_P opt_transaction transaction_mode_list_or_empty
 
3966
                                {
 
3967
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3968
                                        n->kind = TRANS_STMT_BEGIN;
 
3969
                                        n->options = $3;
 
3970
                                        $$ = (Node *)n;
 
3971
                                }
 
3972
                        | START TRANSACTION transaction_mode_list_or_empty
 
3973
                                {
 
3974
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3975
                                        n->kind = TRANS_STMT_START;
 
3976
                                        n->options = $3;
 
3977
                                        $$ = (Node *)n;
 
3978
                                }
 
3979
                        | COMMIT opt_transaction
 
3980
                                {
 
3981
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3982
                                        n->kind = TRANS_STMT_COMMIT;
 
3983
                                        n->options = NIL;
 
3984
                                        $$ = (Node *)n;
 
3985
                                }
 
3986
                        | END_P opt_transaction
 
3987
                                {
 
3988
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3989
                                        n->kind = TRANS_STMT_COMMIT;
 
3990
                                        n->options = NIL;
 
3991
                                        $$ = (Node *)n;
 
3992
                                }
 
3993
                        | ROLLBACK opt_transaction
 
3994
                                {
 
3995
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
3996
                                        n->kind = TRANS_STMT_ROLLBACK;
 
3997
                                        n->options = NIL;
 
3998
                                        $$ = (Node *)n;
 
3999
                                }
 
4000
                        | SAVEPOINT ColId
 
4001
                                {
 
4002
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
4003
                                        n->kind = TRANS_STMT_SAVEPOINT;
 
4004
                                        n->options = list_make1(makeDefElem("savepoint_name",
 
4005
                                                                                                                (Node *)makeString($2)));
 
4006
                                        $$ = (Node *)n;
 
4007
                                }
 
4008
                        | RELEASE SAVEPOINT ColId
 
4009
                                {
 
4010
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
4011
                                        n->kind = TRANS_STMT_RELEASE;
 
4012
                                        n->options = list_make1(makeDefElem("savepoint_name",
 
4013
                                                                                                                (Node *)makeString($3)));
 
4014
                                        $$ = (Node *)n;
 
4015
                                }
 
4016
                        | RELEASE ColId
 
4017
                                {
 
4018
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
4019
                                        n->kind = TRANS_STMT_RELEASE;
 
4020
                                        n->options = list_make1(makeDefElem("savepoint_name",
 
4021
                                                                                                                (Node *)makeString($2)));
 
4022
                                        $$ = (Node *)n;
 
4023
                                }
 
4024
                        | ROLLBACK opt_transaction TO SAVEPOINT ColId
 
4025
                                {
 
4026
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
4027
                                        n->kind = TRANS_STMT_ROLLBACK_TO;
 
4028
                                        n->options = list_make1(makeDefElem("savepoint_name",
 
4029
                                                                                                                (Node *)makeString($5)));
 
4030
                                        $$ = (Node *)n;
 
4031
                                }
 
4032
                        | ROLLBACK opt_transaction TO ColId
 
4033
                                {
 
4034
                                        TransactionStmt *n = makeNode(TransactionStmt);
 
4035
                                        n->kind = TRANS_STMT_ROLLBACK_TO;
 
4036
                                        n->options = list_make1(makeDefElem("savepoint_name",
 
4037
                                                                                                                (Node *)makeString($4)));
 
4038
                                        $$ = (Node *)n;
 
4039
                                }
 
4040
                ;
 
4041
 
 
4042
opt_transaction:        WORK                                                    {}
 
4043
                        | TRANSACTION                                                   {}
 
4044
                        | /*EMPTY*/                                                             {}
 
4045
                ;
 
4046
 
 
4047
transaction_mode_item:
 
4048
                        ISOLATION LEVEL iso_level
 
4049
                                        { $$ = makeDefElem("transaction_isolation",
 
4050
                                                                           makeStringConst($3, NULL)); }
 
4051
                        | READ ONLY
 
4052
                                        { $$ = makeDefElem("transaction_read_only",
 
4053
                                                                           makeIntConst(TRUE)); }
 
4054
                        | READ WRITE
 
4055
                                        { $$ = makeDefElem("transaction_read_only",
 
4056
                                                                           makeIntConst(FALSE)); }
 
4057
                ;
 
4058
 
 
4059
/* Syntax with commas is SQL-spec, without commas is Postgres historical */
 
4060
transaction_mode_list:
 
4061
                        transaction_mode_item
 
4062
                                        { $$ = list_make1($1); }
 
4063
                        | transaction_mode_list ',' transaction_mode_item
 
4064
                                        { $$ = lappend($1, $3); }
 
4065
                        | transaction_mode_list transaction_mode_item
 
4066
                                        { $$ = lappend($1, $2); }
 
4067
                ;
 
4068
 
 
4069
transaction_mode_list_or_empty:
 
4070
                        transaction_mode_list
 
4071
                        | /* EMPTY */
 
4072
                                        { $$ = NIL; }
 
4073
                ;
 
4074
 
 
4075
 
 
4076
/*****************************************************************************
 
4077
 *
 
4078
 *              QUERY:
 
4079
 *                              create view <viewname> '('target-list ')' AS <query>
 
4080
 *
 
4081
 *****************************************************************************/
 
4082
 
 
4083
ViewStmt:       CREATE opt_or_replace VIEW qualified_name opt_column_list
 
4084
                                AS SelectStmt
 
4085
                                {
 
4086
                                        ViewStmt *n = makeNode(ViewStmt);
 
4087
                                        n->replace = $2;
 
4088
                                        n->view = $4;
 
4089
                                        n->aliases = $5;
 
4090
                                        n->query = (Query *) $7;
 
4091
                                        $$ = (Node *)n;
 
4092
                                }
 
4093
                ;
 
4094
 
 
4095
 
 
4096
/*****************************************************************************
 
4097
 *
 
4098
 *              QUERY:
 
4099
 *                              load "filename"
 
4100
 *
 
4101
 *****************************************************************************/
 
4102
 
 
4103
LoadStmt:       LOAD file_name
 
4104
                                {
 
4105
                                        LoadStmt *n = makeNode(LoadStmt);
 
4106
                                        n->filename = $2;
 
4107
                                        $$ = (Node *)n;
 
4108
                                }
 
4109
                ;
 
4110
 
 
4111
 
 
4112
/*****************************************************************************
 
4113
 *
 
4114
 *              CREATE DATABASE
 
4115
 *
 
4116
 *****************************************************************************/
 
4117
 
 
4118
CreatedbStmt:
 
4119
                        CREATE DATABASE database_name opt_with createdb_opt_list
 
4120
                                {
 
4121
                                        CreatedbStmt *n = makeNode(CreatedbStmt);
 
4122
                                        n->dbname = $3;
 
4123
                                        n->options = $5;
 
4124
                                        $$ = (Node *)n;
 
4125
                                }
 
4126
                ;
 
4127
 
 
4128
createdb_opt_list:
 
4129
                        createdb_opt_list createdb_opt_item             { $$ = lappend($1, $2); }
 
4130
                        | /* EMPTY */                                                   { $$ = NIL; }
 
4131
                ;
 
4132
 
 
4133
createdb_opt_item:
 
4134
                        TABLESPACE opt_equal name
 
4135
                                {
 
4136
                                        $$ = makeDefElem("tablespace", (Node *)makeString($3));
 
4137
                                }
 
4138
                        | TABLESPACE opt_equal DEFAULT
 
4139
                                {
 
4140
                                        $$ = makeDefElem("tablespace", NULL);
 
4141
                                }
 
4142
                        | LOCATION opt_equal Sconst
 
4143
                                {
 
4144
                                        $$ = makeDefElem("location", (Node *)makeString($3));
 
4145
                                }
 
4146
                        | LOCATION opt_equal DEFAULT
 
4147
                                {
 
4148
                                        $$ = makeDefElem("location", NULL);
 
4149
                                }
 
4150
                        | TEMPLATE opt_equal name
 
4151
                                {
 
4152
                                        $$ = makeDefElem("template", (Node *)makeString($3));
 
4153
                                }
 
4154
                        | TEMPLATE opt_equal DEFAULT
 
4155
                                {
 
4156
                                        $$ = makeDefElem("template", NULL);
 
4157
                                }
 
4158
                        | ENCODING opt_equal Sconst
 
4159
                                {
 
4160
                                        $$ = makeDefElem("encoding", (Node *)makeString($3));
 
4161
                                }
 
4162
                        | ENCODING opt_equal Iconst
 
4163
                                {
 
4164
                                        $$ = makeDefElem("encoding", (Node *)makeInteger($3));
 
4165
                                }
 
4166
                        | ENCODING opt_equal DEFAULT
 
4167
                                {
 
4168
                                        $$ = makeDefElem("encoding", NULL);
 
4169
                                }
 
4170
                        | OWNER opt_equal name
 
4171
                                {
 
4172
                                        $$ = makeDefElem("owner", (Node *)makeString($3));
 
4173
                                }
 
4174
                        | OWNER opt_equal DEFAULT
 
4175
                                {
 
4176
                                        $$ = makeDefElem("owner", NULL);
 
4177
                                }
 
4178
                ;
 
4179
 
 
4180
/*
 
4181
 *      Though the equals sign doesn't match other WITH options, pg_dump uses
 
4182
 *      equals for backward compability, and it doesn't seem worth removing it.
 
4183
 *      2002-02-25
 
4184
 */
 
4185
opt_equal:      '='                                                                             {}
 
4186
                        | /*EMPTY*/                                                             {}
 
4187
                ;
 
4188
 
 
4189
 
 
4190
/*****************************************************************************
 
4191
 *
 
4192
 *              ALTER DATABASE
 
4193
 *
 
4194
 *****************************************************************************/
 
4195
 
 
4196
AlterDatabaseSetStmt:
 
4197
                        ALTER DATABASE database_name SET set_rest
 
4198
                                {
 
4199
                                        AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
 
4200
                                        n->dbname = $3;
 
4201
                                        n->variable = $5->name;
 
4202
                                        n->value = $5->args;
 
4203
                                        $$ = (Node *)n;
 
4204
                                }
 
4205
                        | ALTER DATABASE database_name VariableResetStmt
 
4206
                                {
 
4207
                                        AlterDatabaseSetStmt *n = makeNode(AlterDatabaseSetStmt);
 
4208
                                        n->dbname = $3;
 
4209
                                        n->variable = ((VariableResetStmt *)$4)->name;
 
4210
                                        n->value = NIL;
 
4211
                                        $$ = (Node *)n;
 
4212
                                }
 
4213
                ;
 
4214
 
 
4215
 
 
4216
/*****************************************************************************
 
4217
 *
 
4218
 *              DROP DATABASE
 
4219
 *
 
4220
 * This is implicitly CASCADE, no need for drop behavior
 
4221
 *****************************************************************************/
 
4222
 
 
4223
DropdbStmt: DROP DATABASE database_name
 
4224
                                {
 
4225
                                        DropdbStmt *n = makeNode(DropdbStmt);
 
4226
                                        n->dbname = $3;
 
4227
                                        $$ = (Node *)n;
 
4228
                                }
 
4229
                ;
 
4230
 
 
4231
 
 
4232
/*****************************************************************************
 
4233
 *
 
4234
 * Manipulate a domain
 
4235
 *
 
4236
 *****************************************************************************/
 
4237
 
 
4238
CreateDomainStmt:
 
4239
                        CREATE DOMAIN_P any_name opt_as Typename ColQualList
 
4240
                                {
 
4241
                                        CreateDomainStmt *n = makeNode(CreateDomainStmt);
 
4242
                                        n->domainname = $3;
 
4243
                                        n->typename = $5;
 
4244
                                        n->constraints = $6;
 
4245
                                        $$ = (Node *)n;
 
4246
                                }
 
4247
                ;
 
4248
 
 
4249
AlterDomainStmt:
 
4250
                        /* ALTER DOMAIN <domain> {SET DEFAULT <expr>|DROP DEFAULT} */
 
4251
                        ALTER DOMAIN_P any_name alter_column_default
 
4252
                                {
 
4253
                                        AlterDomainStmt *n = makeNode(AlterDomainStmt);
 
4254
                                        n->subtype = 'T';
 
4255
                                        n->typename = $3;
 
4256
                                        n->def = $4;
 
4257
                                        $$ = (Node *)n;
 
4258
                                }
 
4259
                        /* ALTER DOMAIN <domain> DROP NOT NULL */
 
4260
                        | ALTER DOMAIN_P any_name DROP NOT NULL_P
 
4261
                                {
 
4262
                                        AlterDomainStmt *n = makeNode(AlterDomainStmt);
 
4263
                                        n->subtype = 'N';
 
4264
                                        n->typename = $3;
 
4265
                                        $$ = (Node *)n;
 
4266
                                }
 
4267
                        /* ALTER DOMAIN <domain> SET NOT NULL */
 
4268
                        | ALTER DOMAIN_P any_name SET NOT NULL_P
 
4269
                                {
 
4270
                                        AlterDomainStmt *n = makeNode(AlterDomainStmt);
 
4271
                                        n->subtype = 'O';
 
4272
                                        n->typename = $3;
 
4273
                                        $$ = (Node *)n;
 
4274
                                }
 
4275
                        /* ALTER DOMAIN <domain> ADD CONSTRAINT ... */
 
4276
                        | ALTER DOMAIN_P any_name ADD TableConstraint
 
4277
                                {
 
4278
                                        AlterDomainStmt *n = makeNode(AlterDomainStmt);
 
4279
                                        n->subtype = 'C';
 
4280
                                        n->typename = $3;
 
4281
                                        n->def = $5;
 
4282
                                        $$ = (Node *)n;
 
4283
                                }
 
4284
                        /* ALTER DOMAIN <domain> DROP CONSTRAINT <name> [RESTRICT|CASCADE] */
 
4285
                        | ALTER DOMAIN_P any_name DROP CONSTRAINT name opt_drop_behavior
 
4286
                                {
 
4287
                                        AlterDomainStmt *n = makeNode(AlterDomainStmt);
 
4288
                                        n->subtype = 'X';
 
4289
                                        n->typename = $3;
 
4290
                                        n->name = $6;
 
4291
                                        n->behavior = $7;
 
4292
                                        $$ = (Node *)n;
 
4293
                                }
 
4294
                        ;
 
4295
 
 
4296
opt_as:         AS                                                                              {}
 
4297
                        | /* EMPTY */                                                   {}
 
4298
                ;
 
4299
 
 
4300
 
 
4301
/*****************************************************************************
 
4302
 *
 
4303
 * Manipulate a conversion
 
4304
 *
 
4305
 *              CREATE [DEFAULT] CONVERSION <conversion_name>
 
4306
 *              FOR <encoding_name> TO <encoding_name> FROM <func_name>
 
4307
 *
 
4308
 *****************************************************************************/
 
4309
 
 
4310
CreateConversionStmt:
 
4311
                        CREATE opt_default CONVERSION_P any_name FOR Sconst
 
4312
                        TO Sconst FROM any_name
 
4313
                        {
 
4314
                          CreateConversionStmt *n = makeNode(CreateConversionStmt);
 
4315
                          n->conversion_name = $4;
 
4316
                          n->for_encoding_name = $6;
 
4317
                          n->to_encoding_name = $8;
 
4318
                          n->func_name = $10;
 
4319
                          n->def = $2;
 
4320
                          $$ = (Node *)n;
 
4321
                        }
 
4322
                ;
 
4323
 
 
4324
/*****************************************************************************
 
4325
 *
 
4326
 *              QUERY:
 
4327
 *                              cluster <index_name> on <qualified_name>
 
4328
 *                              cluster <qualified_name>
 
4329
 *                              cluster
 
4330
 *
 
4331
 *****************************************************************************/
 
4332
 
 
4333
ClusterStmt:
 
4334
                        CLUSTER index_name ON qualified_name
 
4335
                                {
 
4336
                                   ClusterStmt *n = makeNode(ClusterStmt);
 
4337
                                   n->relation = $4;
 
4338
                                   n->indexname = $2;
 
4339
                                   $$ = (Node*)n;
 
4340
                                }
 
4341
                        | CLUSTER qualified_name
 
4342
                                {
 
4343
                               ClusterStmt *n = makeNode(ClusterStmt);
 
4344
                                   n->relation = $2;
 
4345
                                   n->indexname = NULL;
 
4346
                                   $$ = (Node*)n;
 
4347
                                }
 
4348
                        | CLUSTER
 
4349
                            {
 
4350
                                   ClusterStmt *n = makeNode(ClusterStmt);
 
4351
                                   n->relation = NULL;
 
4352
                                   n->indexname = NULL;
 
4353
                                   $$ = (Node*)n;
 
4354
                                }
 
4355
                ;
 
4356
 
 
4357
/*****************************************************************************
 
4358
 *
 
4359
 *              QUERY:
 
4360
 *                              vacuum
 
4361
 *                              analyze
 
4362
 *
 
4363
 *****************************************************************************/
 
4364
 
 
4365
VacuumStmt: VACUUM opt_full opt_freeze opt_verbose
 
4366
                                {
 
4367
                                        VacuumStmt *n = makeNode(VacuumStmt);
 
4368
                                        n->vacuum = true;
 
4369
                                        n->analyze = false;
 
4370
                                        n->full = $2;
 
4371
                                        n->freeze = $3;
 
4372
                                        n->verbose = $4;
 
4373
                                        n->relation = NULL;
 
4374
                                        n->va_cols = NIL;
 
4375
                                        $$ = (Node *)n;
 
4376
                                }
 
4377
                        | VACUUM opt_full opt_freeze opt_verbose qualified_name
 
4378
                                {
 
4379
                                        VacuumStmt *n = makeNode(VacuumStmt);
 
4380
                                        n->vacuum = true;
 
4381
                                        n->analyze = false;
 
4382
                                        n->full = $2;
 
4383
                                        n->freeze = $3;
 
4384
                                        n->verbose = $4;
 
4385
                                        n->relation = $5;
 
4386
                                        n->va_cols = NIL;
 
4387
                                        $$ = (Node *)n;
 
4388
                                }
 
4389
                        | VACUUM opt_full opt_freeze opt_verbose AnalyzeStmt
 
4390
                                {
 
4391
                                        VacuumStmt *n = (VacuumStmt *) $5;
 
4392
                                        n->vacuum = true;
 
4393
                                        n->full = $2;
 
4394
                                        n->freeze = $3;
 
4395
                                        n->verbose |= $4;
 
4396
                                        $$ = (Node *)n;
 
4397
                                }
 
4398
                ;
 
4399
 
 
4400
AnalyzeStmt:
 
4401
                        analyze_keyword opt_verbose
 
4402
                                {
 
4403
                                        VacuumStmt *n = makeNode(VacuumStmt);
 
4404
                                        n->vacuum = false;
 
4405
                                        n->analyze = true;
 
4406
                                        n->full = false;
 
4407
                                        n->freeze = false;
 
4408
                                        n->verbose = $2;
 
4409
                                        n->relation = NULL;
 
4410
                                        n->va_cols = NIL;
 
4411
                                        $$ = (Node *)n;
 
4412
                                }
 
4413
                        | analyze_keyword opt_verbose qualified_name opt_name_list
 
4414
                                {
 
4415
                                        VacuumStmt *n = makeNode(VacuumStmt);
 
4416
                                        n->vacuum = false;
 
4417
                                        n->analyze = true;
 
4418
                                        n->full = false;
 
4419
                                        n->freeze = false;
 
4420
                                        n->verbose = $2;
 
4421
                                        n->relation = $3;
 
4422
                                        n->va_cols = $4;
 
4423
                                        $$ = (Node *)n;
 
4424
                                }
 
4425
                ;
 
4426
 
 
4427
analyze_keyword:
 
4428
                        ANALYZE                                                                 {}
 
4429
                        | ANALYSE /* British */                                 {}
 
4430
                ;
 
4431
 
 
4432
opt_verbose:
 
4433
                        VERBOSE                                                                 { $$ = TRUE; }
 
4434
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
4435
                ;
 
4436
 
 
4437
opt_full:       FULL                                                                    { $$ = TRUE; }
 
4438
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
4439
                ;
 
4440
 
 
4441
opt_freeze: FREEZE                                                                      { $$ = TRUE; }
 
4442
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
4443
                ;
 
4444
 
 
4445
opt_name_list:
 
4446
                        '(' name_list ')'                                               { $$ = $2; }
 
4447
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
4448
                ;
 
4449
 
 
4450
 
 
4451
/*****************************************************************************
 
4452
 *
 
4453
 *              QUERY:
 
4454
 *                              EXPLAIN [ANALYZE] [VERBOSE] query
 
4455
 *
 
4456
 *****************************************************************************/
 
4457
 
 
4458
ExplainStmt: EXPLAIN opt_analyze opt_verbose ExplainableStmt
 
4459
                                {
 
4460
                                        ExplainStmt *n = makeNode(ExplainStmt);
 
4461
                                        n->analyze = $2;
 
4462
                                        n->verbose = $3;
 
4463
                                        n->query = (Query*)$4;
 
4464
                                        $$ = (Node *)n;
 
4465
                                }
 
4466
                ;
 
4467
 
 
4468
ExplainableStmt:
 
4469
                        SelectStmt
 
4470
                        | InsertStmt
 
4471
                        | UpdateStmt
 
4472
                        | DeleteStmt
 
4473
                        | DeclareCursorStmt
 
4474
                        | ExecuteStmt                                   /* by default all are $$=$1 */
 
4475
                ;
 
4476
 
 
4477
opt_analyze:
 
4478
                        analyze_keyword                 { $$ = TRUE; }
 
4479
                        | /* EMPTY */                   { $$ = FALSE; }
 
4480
                ;
 
4481
 
 
4482
/*****************************************************************************
 
4483
 *
 
4484
 *              QUERY:
 
4485
 *                              PREPARE <plan_name> [(args, ...)] AS <query>
 
4486
 *
 
4487
 *****************************************************************************/
 
4488
 
 
4489
PrepareStmt: PREPARE name prep_type_clause AS PreparableStmt
 
4490
                                {
 
4491
                                        PrepareStmt *n = makeNode(PrepareStmt);
 
4492
                                        n->name = $2;
 
4493
                                        n->argtypes = $3;
 
4494
                                        n->query = (Query *) $5;
 
4495
                                        $$ = (Node *) n;
 
4496
                                }
 
4497
                ;
 
4498
 
 
4499
prep_type_clause: '(' prep_type_list ')'        { $$ = $2; }
 
4500
                                | /* EMPTY */                           { $$ = NIL; }
 
4501
                ;
 
4502
 
 
4503
prep_type_list: Typename                        { $$ = list_make1($1); }
 
4504
                          | prep_type_list ',' Typename
 
4505
                                                                        { $$ = lappend($1, $3); }
 
4506
                ;
 
4507
 
 
4508
PreparableStmt:
 
4509
                        SelectStmt
 
4510
                        | InsertStmt
 
4511
                        | UpdateStmt
 
4512
                        | DeleteStmt                                    /* by default all are $$=$1 */
 
4513
                ;
 
4514
 
 
4515
/*****************************************************************************
 
4516
 *
 
4517
 * EXECUTE <plan_name> [(params, ...)]
 
4518
 * CREATE TABLE <name> AS EXECUTE <plan_name> [(params, ...)]
 
4519
 *
 
4520
 *****************************************************************************/
 
4521
 
 
4522
ExecuteStmt: EXECUTE name execute_param_clause
 
4523
                                {
 
4524
                                        ExecuteStmt *n = makeNode(ExecuteStmt);
 
4525
                                        n->name = $2;
 
4526
                                        n->params = $3;
 
4527
                                        n->into = NULL;
 
4528
                                        $$ = (Node *) n;
 
4529
                                }
 
4530
                        | CREATE OptTemp TABLE qualified_name OptCreateAs AS EXECUTE name execute_param_clause
 
4531
                                {
 
4532
                                        ExecuteStmt *n = makeNode(ExecuteStmt);
 
4533
                                        n->name = $8;
 
4534
                                        n->params = $9;
 
4535
                                        $4->istemp = $2;
 
4536
                                        n->into = $4;
 
4537
                                        if ($5)
 
4538
                                                ereport(ERROR,
 
4539
                                                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
4540
                                                                 errmsg("column name list not allowed in CREATE TABLE / AS EXECUTE")));
 
4541
                                        /* ... because it's not implemented, but it could be */
 
4542
                                        $$ = (Node *) n;
 
4543
                                }
 
4544
                ;
 
4545
 
 
4546
execute_param_clause: '(' expr_list ')'                         { $$ = $2; }
 
4547
                                        | /* EMPTY */                                   { $$ = NIL; }
 
4548
                                        ;
 
4549
 
 
4550
/*****************************************************************************
 
4551
 *
 
4552
 *              QUERY:
 
4553
 *                              DEALLOCATE [PREPARE] <plan_name>
 
4554
 *
 
4555
 *****************************************************************************/
 
4556
 
 
4557
DeallocateStmt: DEALLOCATE name
 
4558
                                        {
 
4559
                                                DeallocateStmt *n = makeNode(DeallocateStmt);
 
4560
                                                n->name = $2;
 
4561
                                                $$ = (Node *) n;
 
4562
                                        }
 
4563
                                | DEALLOCATE PREPARE name
 
4564
                                        {
 
4565
                                                DeallocateStmt *n = makeNode(DeallocateStmt);
 
4566
                                                n->name = $3;
 
4567
                                                $$ = (Node *) n;
 
4568
                                        }
 
4569
                ;
 
4570
 
 
4571
/*****************************************************************************
 
4572
 *
 
4573
 *              QUERY:
 
4574
 *                              INSERT STATEMENTS
 
4575
 *
 
4576
 *****************************************************************************/
 
4577
 
 
4578
InsertStmt:
 
4579
                        INSERT INTO qualified_name insert_rest
 
4580
                                {
 
4581
                                        $4->relation = $3;
 
4582
                                        $$ = (Node *) $4;
 
4583
                                }
 
4584
                ;
 
4585
 
 
4586
insert_rest:
 
4587
                        VALUES '(' insert_target_list ')'
 
4588
                                {
 
4589
                                        $$ = makeNode(InsertStmt);
 
4590
                                        $$->cols = NIL;
 
4591
                                        $$->targetList = $3;
 
4592
                                        $$->selectStmt = NULL;
 
4593
                                }
 
4594
                        | DEFAULT VALUES
 
4595
                                {
 
4596
                                        $$ = makeNode(InsertStmt);
 
4597
                                        $$->cols = NIL;
 
4598
                                        $$->targetList = NIL;
 
4599
                                        $$->selectStmt = NULL;
 
4600
                                }
 
4601
                        | SelectStmt
 
4602
                                {
 
4603
                                        $$ = makeNode(InsertStmt);
 
4604
                                        $$->cols = NIL;
 
4605
                                        $$->targetList = NIL;
 
4606
                                        $$->selectStmt = $1;
 
4607
                                }
 
4608
                        | '(' insert_column_list ')' VALUES '(' insert_target_list ')'
 
4609
                                {
 
4610
                                        $$ = makeNode(InsertStmt);
 
4611
                                        $$->cols = $2;
 
4612
                                        $$->targetList = $6;
 
4613
                                        $$->selectStmt = NULL;
 
4614
                                }
 
4615
                        | '(' insert_column_list ')' SelectStmt
 
4616
                                {
 
4617
                                        $$ = makeNode(InsertStmt);
 
4618
                                        $$->cols = $2;
 
4619
                                        $$->targetList = NIL;
 
4620
                                        $$->selectStmt = $4;
 
4621
                                }
 
4622
                ;
 
4623
 
 
4624
insert_column_list:
 
4625
                        insert_column_item
 
4626
                                        { $$ = list_make1($1); }
 
4627
                        | insert_column_list ',' insert_column_item
 
4628
                                        { $$ = lappend($1, $3); }
 
4629
                ;
 
4630
 
 
4631
insert_column_item:
 
4632
                        ColId opt_indirection
 
4633
                                {
 
4634
                                        $$ = makeNode(ResTarget);
 
4635
                                        $$->name = $1;
 
4636
                                        $$->indirection = $2;
 
4637
                                        $$->val = NULL;
 
4638
                                }
 
4639
                ;
 
4640
 
 
4641
 
 
4642
/*****************************************************************************
 
4643
 *
 
4644
 *              QUERY:
 
4645
 *                              DELETE STATEMENTS
 
4646
 *
 
4647
 *****************************************************************************/
 
4648
 
 
4649
DeleteStmt: DELETE_P FROM relation_expr where_clause
 
4650
                                {
 
4651
                                        DeleteStmt *n = makeNode(DeleteStmt);
 
4652
                                        n->relation = $3;
 
4653
                                        n->whereClause = $4;
 
4654
                                        $$ = (Node *)n;
 
4655
                                }
 
4656
                ;
 
4657
 
 
4658
LockStmt:       LOCK_P opt_table qualified_name_list opt_lock opt_nowait
 
4659
                                {
 
4660
                                        LockStmt *n = makeNode(LockStmt);
 
4661
 
 
4662
                                        n->relations = $3;
 
4663
                                        n->mode = $4;
 
4664
                                        n->nowait = $5;
 
4665
                                        $$ = (Node *)n;
 
4666
                                }
 
4667
                ;
 
4668
 
 
4669
opt_lock:       IN_P lock_type MODE                     { $$ = $2; }
 
4670
                        | /*EMPTY*/                                             { $$ = AccessExclusiveLock; }
 
4671
                ;
 
4672
 
 
4673
lock_type:      ACCESS SHARE                                    { $$ = AccessShareLock; }
 
4674
                        | ROW SHARE                                             { $$ = RowShareLock; }
 
4675
                        | ROW EXCLUSIVE                                 { $$ = RowExclusiveLock; }
 
4676
                        | SHARE UPDATE EXCLUSIVE                { $$ = ShareUpdateExclusiveLock; }
 
4677
                        | SHARE                                                 { $$ = ShareLock; }
 
4678
                        | SHARE ROW EXCLUSIVE                   { $$ = ShareRowExclusiveLock; }
 
4679
                        | EXCLUSIVE                                             { $$ = ExclusiveLock; }
 
4680
                        | ACCESS EXCLUSIVE                              { $$ = AccessExclusiveLock; }
 
4681
                ;
 
4682
 
 
4683
opt_nowait:     NOWAIT                  { $$ = TRUE; }
 
4684
                        | /*EMPTY*/                                             { $$ = FALSE; }
 
4685
                ;
 
4686
 
 
4687
 
 
4688
/*****************************************************************************
 
4689
 *
 
4690
 *              QUERY:
 
4691
 *                              UpdateStmt (UPDATE)
 
4692
 *
 
4693
 *****************************************************************************/
 
4694
 
 
4695
UpdateStmt: UPDATE relation_expr
 
4696
                        SET update_target_list
 
4697
                        from_clause
 
4698
                        where_clause
 
4699
                                {
 
4700
                                        UpdateStmt *n = makeNode(UpdateStmt);
 
4701
                                        n->relation = $2;
 
4702
                                        n->targetList = $4;
 
4703
                                        n->fromClause = $5;
 
4704
                                        n->whereClause = $6;
 
4705
                                        $$ = (Node *)n;
 
4706
                                }
 
4707
                ;
 
4708
 
 
4709
 
 
4710
/*****************************************************************************
 
4711
 *
 
4712
 *              QUERY:
 
4713
 *                              CURSOR STATEMENTS
 
4714
 *
 
4715
 *****************************************************************************/
 
4716
DeclareCursorStmt: DECLARE name cursor_options CURSOR opt_hold FOR SelectStmt
 
4717
                                {
 
4718
                                        DeclareCursorStmt *n = makeNode(DeclareCursorStmt);
 
4719
                                        n->portalname = $2;
 
4720
                                        n->options = $3;
 
4721
                                        n->query = $7;
 
4722
                                        if ($5)
 
4723
                                                n->options |= CURSOR_OPT_HOLD;
 
4724
                                        $$ = (Node *)n;
 
4725
                                }
 
4726
                ;
 
4727
 
 
4728
cursor_options: /*EMPTY*/                                       { $$ = 0; }
 
4729
                        | cursor_options NO SCROLL              { $$ = $1 | CURSOR_OPT_NO_SCROLL; }
 
4730
                        | cursor_options SCROLL                 { $$ = $1 | CURSOR_OPT_SCROLL; }
 
4731
                        | cursor_options BINARY                 { $$ = $1 | CURSOR_OPT_BINARY; }
 
4732
                        | cursor_options INSENSITIVE    { $$ = $1 | CURSOR_OPT_INSENSITIVE; }
 
4733
                ;
 
4734
 
 
4735
opt_hold: /* EMPTY */                                           { $$ = FALSE; }
 
4736
                        | WITH HOLD                                             { $$ = TRUE; }
 
4737
                        | WITHOUT HOLD                                  { $$ = FALSE; }
 
4738
                ;
 
4739
 
 
4740
/*****************************************************************************
 
4741
 *
 
4742
 *              QUERY:
 
4743
 *                              SELECT STATEMENTS
 
4744
 *
 
4745
 *****************************************************************************/
 
4746
 
 
4747
/* A complete SELECT statement looks like this.
 
4748
 *
 
4749
 * The rule returns either a single SelectStmt node or a tree of them,
 
4750
 * representing a set-operation tree.
 
4751
 *
 
4752
 * There is an ambiguity when a sub-SELECT is within an a_expr and there
 
4753
 * are excess parentheses: do the parentheses belong to the sub-SELECT or
 
4754
 * to the surrounding a_expr?  We don't really care, but yacc wants to know.
 
4755
 * To resolve the ambiguity, we are careful to define the grammar so that
 
4756
 * the decision is staved off as long as possible: as long as we can keep
 
4757
 * absorbing parentheses into the sub-SELECT, we will do so, and only when
 
4758
 * it's no longer possible to do that will we decide that parens belong to
 
4759
 * the expression.      For example, in "SELECT (((SELECT 2)) + 3)" the extra
 
4760
 * parentheses are treated as part of the sub-select.  The necessity of doing
 
4761
 * it that way is shown by "SELECT (((SELECT 2)) UNION SELECT 2)".      Had we
 
4762
 * parsed "((SELECT 2))" as an a_expr, it'd be too late to go back to the
 
4763
 * SELECT viewpoint when we see the UNION.
 
4764
 *
 
4765
 * This approach is implemented by defining a nonterminal select_with_parens,
 
4766
 * which represents a SELECT with at least one outer layer of parentheses,
 
4767
 * and being careful to use select_with_parens, never '(' SelectStmt ')',
 
4768
 * in the expression grammar.  We will then have shift-reduce conflicts
 
4769
 * which we can resolve in favor of always treating '(' <select> ')' as
 
4770
 * a select_with_parens.  To resolve the conflicts, the productions that
 
4771
 * conflict with the select_with_parens productions are manually given
 
4772
 * precedences lower than the precedence of ')', thereby ensuring that we
 
4773
 * shift ')' (and then reduce to select_with_parens) rather than trying to
 
4774
 * reduce the inner <select> nonterminal to something else.  We use UMINUS
 
4775
 * precedence for this, which is a fairly arbitrary choice.
 
4776
 *
 
4777
 * To be able to define select_with_parens itself without ambiguity, we need
 
4778
 * a nonterminal select_no_parens that represents a SELECT structure with no
 
4779
 * outermost parentheses.  This is a little bit tedious, but it works.
 
4780
 *
 
4781
 * In non-expression contexts, we use SelectStmt which can represent a SELECT
 
4782
 * with or without outer parentheses.
 
4783
 */
 
4784
 
 
4785
SelectStmt: select_no_parens                    %prec UMINUS
 
4786
                        | select_with_parens            %prec UMINUS
 
4787
                ;
 
4788
 
 
4789
select_with_parens:
 
4790
                        '(' select_no_parens ')'                                { $$ = $2; }
 
4791
                        | '(' select_with_parens ')'                    { $$ = $2; }
 
4792
                ;
 
4793
 
 
4794
/*
 
4795
 *      FOR UPDATE may be before or after LIMIT/OFFSET.
 
4796
 *      In <=7.2.X, LIMIT/OFFSET had to be after FOR UPDATE
 
4797
 *      We now support both orderings, but prefer LIMIT/OFFSET before FOR UPDATE
 
4798
 *      2002-08-28 bjm
 
4799
 */
 
4800
select_no_parens:
 
4801
                        simple_select                                           { $$ = $1; }
 
4802
                        | select_clause sort_clause
 
4803
                                {
 
4804
                                        insertSelectOptions((SelectStmt *) $1, $2, NIL,
 
4805
                                                                                NULL, NULL);
 
4806
                                        $$ = $1;
 
4807
                                }
 
4808
                        | select_clause opt_sort_clause for_update_clause opt_select_limit
 
4809
                                {
 
4810
                                        insertSelectOptions((SelectStmt *) $1, $2, $3,
 
4811
                                                                                list_nth($4, 0), list_nth($4, 1));
 
4812
                                        $$ = $1;
 
4813
                                }
 
4814
                        | select_clause opt_sort_clause select_limit opt_for_update_clause
 
4815
                                {
 
4816
                                        insertSelectOptions((SelectStmt *) $1, $2, $4,
 
4817
                                                                                list_nth($3, 0), list_nth($3, 1));
 
4818
                                        $$ = $1;
 
4819
                                }
 
4820
                ;
 
4821
 
 
4822
select_clause:
 
4823
                        simple_select                                                   { $$ = $1; }
 
4824
                        | select_with_parens                                    { $$ = $1; }
 
4825
                ;
 
4826
 
 
4827
/*
 
4828
 * This rule parses SELECT statements that can appear within set operations,
 
4829
 * including UNION, INTERSECT and EXCEPT.  '(' and ')' can be used to specify
 
4830
 * the ordering of the set operations.  Without '(' and ')' we want the
 
4831
 * operations to be ordered per the precedence specs at the head of this file.
 
4832
 *
 
4833
 * As with select_no_parens, simple_select cannot have outer parentheses,
 
4834
 * but can have parenthesized subclauses.
 
4835
 *
 
4836
 * Note that sort clauses cannot be included at this level --- SQL92 requires
 
4837
 *              SELECT foo UNION SELECT bar ORDER BY baz
 
4838
 * to be parsed as
 
4839
 *              (SELECT foo UNION SELECT bar) ORDER BY baz
 
4840
 * not
 
4841
 *              SELECT foo UNION (SELECT bar ORDER BY baz)
 
4842
 * Likewise FOR UPDATE and LIMIT.  Therefore, those clauses are described
 
4843
 * as part of the select_no_parens production, not simple_select.
 
4844
 * This does not limit functionality, because you can reintroduce sort and
 
4845
 * limit clauses inside parentheses.
 
4846
 *
 
4847
 * NOTE: only the leftmost component SelectStmt should have INTO.
 
4848
 * However, this is not checked by the grammar; parse analysis must check it.
 
4849
 */
 
4850
simple_select:
 
4851
                        SELECT opt_distinct target_list
 
4852
                        into_clause from_clause where_clause
 
4853
                        group_clause having_clause
 
4854
                                {
 
4855
                                        SelectStmt *n = makeNode(SelectStmt);
 
4856
                                        n->distinctClause = $2;
 
4857
                                        n->targetList = $3;
 
4858
                                        n->into = $4;
 
4859
                                        n->intoColNames = NIL;
 
4860
                                        n->intoHasOids = DEFAULT_OIDS;
 
4861
                                        n->fromClause = $5;
 
4862
                                        n->whereClause = $6;
 
4863
                                        n->groupClause = $7;
 
4864
                                        n->havingClause = $8;
 
4865
                                        $$ = (Node *)n;
 
4866
                                }
 
4867
                        | select_clause UNION opt_all select_clause
 
4868
                                {
 
4869
                                        $$ = makeSetOp(SETOP_UNION, $3, $1, $4);
 
4870
                                }
 
4871
                        | select_clause INTERSECT opt_all select_clause
 
4872
                                {
 
4873
                                        $$ = makeSetOp(SETOP_INTERSECT, $3, $1, $4);
 
4874
                                }
 
4875
                        | select_clause EXCEPT opt_all select_clause
 
4876
                                {
 
4877
                                        $$ = makeSetOp(SETOP_EXCEPT, $3, $1, $4);
 
4878
                                }
 
4879
                ;
 
4880
 
 
4881
into_clause:
 
4882
                        INTO OptTempTableName                                   { $$ = $2; }
 
4883
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
4884
                ;
 
4885
 
 
4886
/*
 
4887
 * Redundancy here is needed to avoid shift/reduce conflicts,
 
4888
 * since TEMP is not a reserved word.  See also OptTemp.
 
4889
 */
 
4890
OptTempTableName:
 
4891
                        TEMPORARY opt_table qualified_name
 
4892
                                {
 
4893
                                        $$ = $3;
 
4894
                                        $$->istemp = true;
 
4895
                                }
 
4896
                        | TEMP opt_table qualified_name
 
4897
                                {
 
4898
                                        $$ = $3;
 
4899
                                        $$->istemp = true;
 
4900
                                }
 
4901
                        | LOCAL TEMPORARY opt_table qualified_name
 
4902
                                {
 
4903
                                        $$ = $4;
 
4904
                                        $$->istemp = true;
 
4905
                                }
 
4906
                        | LOCAL TEMP opt_table qualified_name
 
4907
                                {
 
4908
                                        $$ = $4;
 
4909
                                        $$->istemp = true;
 
4910
                                }
 
4911
                        | GLOBAL TEMPORARY opt_table qualified_name
 
4912
                                {
 
4913
                                        $$ = $4;
 
4914
                                        $$->istemp = true;
 
4915
                                }
 
4916
                        | GLOBAL TEMP opt_table qualified_name
 
4917
                                {
 
4918
                                        $$ = $4;
 
4919
                                        $$->istemp = true;
 
4920
                                }
 
4921
                        | TABLE qualified_name
 
4922
                                {
 
4923
                                        $$ = $2;
 
4924
                                        $$->istemp = false;
 
4925
                                }
 
4926
                        | qualified_name
 
4927
                                {
 
4928
                                        $$ = $1;
 
4929
                                        $$->istemp = false;
 
4930
                                }
 
4931
                ;
 
4932
 
 
4933
opt_table:      TABLE                                                                   {}
 
4934
                        | /*EMPTY*/                                                             {}
 
4935
                ;
 
4936
 
 
4937
opt_all:        ALL                                                                             { $$ = TRUE; }
 
4938
                        | DISTINCT                                                              { $$ = FALSE; }
 
4939
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
4940
                ;
 
4941
 
 
4942
/* We use (NIL) as a placeholder to indicate that all target expressions
 
4943
 * should be placed in the DISTINCT list during parsetree analysis.
 
4944
 */
 
4945
opt_distinct:
 
4946
                        DISTINCT                                                                { $$ = list_make1(NIL); }
 
4947
                        | DISTINCT ON '(' expr_list ')'                 { $$ = $4; }
 
4948
                        | ALL                                                                   { $$ = NIL; }
 
4949
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
4950
                ;
 
4951
 
 
4952
opt_sort_clause:
 
4953
                        sort_clause                                                             { $$ = $1;}
 
4954
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
4955
                ;
 
4956
 
 
4957
sort_clause:
 
4958
                        ORDER BY sortby_list                                    { $$ = $3; }
 
4959
                ;
 
4960
 
 
4961
sortby_list:
 
4962
                        sortby                                                                  { $$ = list_make1($1); }
 
4963
                        | sortby_list ',' sortby                                { $$ = lappend($1, $3); }
 
4964
                ;
 
4965
 
 
4966
sortby:         a_expr USING qual_all_Op
 
4967
                                {
 
4968
                                        $$ = makeNode(SortBy);
 
4969
                                        $$->node = $1;
 
4970
                                        $$->sortby_kind = SORTBY_USING;
 
4971
                                        $$->useOp = $3;
 
4972
                                }
 
4973
                        | a_expr ASC
 
4974
                                {
 
4975
                                        $$ = makeNode(SortBy);
 
4976
                                        $$->node = $1;
 
4977
                                        $$->sortby_kind = SORTBY_ASC;
 
4978
                                        $$->useOp = NIL;
 
4979
                                }
 
4980
                        | a_expr DESC
 
4981
                                {
 
4982
                                        $$ = makeNode(SortBy);
 
4983
                                        $$->node = $1;
 
4984
                                        $$->sortby_kind = SORTBY_DESC;
 
4985
                                        $$->useOp = NIL;
 
4986
                                }
 
4987
                        | a_expr
 
4988
                                {
 
4989
                                        $$ = makeNode(SortBy);
 
4990
                                        $$->node = $1;
 
4991
                                        $$->sortby_kind = SORTBY_ASC;   /* default */
 
4992
                                        $$->useOp = NIL;
 
4993
                                }
 
4994
                ;
 
4995
 
 
4996
 
 
4997
select_limit:
 
4998
                        LIMIT select_limit_value OFFSET select_offset_value
 
4999
                                { $$ = list_make2($4, $2); }
 
5000
                        | OFFSET select_offset_value LIMIT select_limit_value
 
5001
                                { $$ = list_make2($2, $4); }
 
5002
                        | LIMIT select_limit_value
 
5003
                                { $$ = list_make2(NULL, $2); }
 
5004
                        | OFFSET select_offset_value
 
5005
                                { $$ = list_make2($2, NULL); }
 
5006
                        | LIMIT select_limit_value ',' select_offset_value
 
5007
                                {
 
5008
                                        /* Disabled because it was too confusing, bjm 2002-02-18 */
 
5009
                                        ereport(ERROR,
 
5010
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
5011
                                                         errmsg("LIMIT #,# syntax is not supported"),
 
5012
                                                         errhint("Use separate LIMIT and OFFSET clauses.")));
 
5013
                                }
 
5014
                ;
 
5015
 
 
5016
opt_select_limit:
 
5017
                        select_limit                                                    { $$ = $1; }
 
5018
                        | /* EMPTY */
 
5019
                                        { $$ = list_make2(NULL,NULL); }
 
5020
                ;
 
5021
 
 
5022
select_limit_value:
 
5023
                        a_expr                                                                  { $$ = $1; }
 
5024
                        | ALL
 
5025
                                {
 
5026
                                        /* LIMIT ALL is represented as a NULL constant */
 
5027
                                        A_Const *n = makeNode(A_Const);
 
5028
                                        n->val.type = T_Null;
 
5029
                                        $$ = (Node *)n;
 
5030
                                }
 
5031
                ;
 
5032
 
 
5033
select_offset_value:
 
5034
                        a_expr                                                                  { $$ = $1; }
 
5035
                ;
 
5036
 
 
5037
/*
 
5038
 *      jimmy bell-style recursive queries aren't supported in the
 
5039
 *      current system.
 
5040
 *
 
5041
 *      ...however, recursive addattr and rename supported.  make special
 
5042
 *      cases for these.
 
5043
 */
 
5044
 
 
5045
group_clause:
 
5046
                        GROUP_P BY expr_list                                    { $$ = $3; }
 
5047
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
5048
                ;
 
5049
 
 
5050
having_clause:
 
5051
                        HAVING a_expr                                                   { $$ = $2; }
 
5052
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
5053
                ;
 
5054
 
 
5055
for_update_clause:
 
5056
                        FOR UPDATE update_list                                  { $$ = $3; }
 
5057
                        | FOR READ ONLY                                                 { $$ = NULL; }
 
5058
                ;
 
5059
 
 
5060
opt_for_update_clause:
 
5061
                        for_update_clause                                               { $$ = $1; }
 
5062
                        | /* EMPTY */                                                   { $$ = NULL; }
 
5063
                ;
 
5064
 
 
5065
update_list:
 
5066
                        OF name_list                                                    { $$ = $2; }
 
5067
                        | /* EMPTY */                                                   { $$ = list_make1(NULL); }
 
5068
                ;
 
5069
 
 
5070
/*****************************************************************************
 
5071
 *
 
5072
 *      clauses common to all Optimizable Stmts:
 
5073
 *              from_clause             - allow list of both JOIN expressions and table names
 
5074
 *              where_clause    - qualifications for joins or restrictions
 
5075
 *
 
5076
 *****************************************************************************/
 
5077
 
 
5078
from_clause:
 
5079
                        FROM from_list                                                  { $$ = $2; }
 
5080
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
5081
                ;
 
5082
 
 
5083
from_list:
 
5084
                        table_ref                                                               { $$ = list_make1($1); }
 
5085
                        | from_list ',' table_ref                               { $$ = lappend($1, $3); }
 
5086
                ;
 
5087
 
 
5088
/*
 
5089
 * table_ref is where an alias clause can be attached.  Note we cannot make
 
5090
 * alias_clause have an empty production because that causes parse conflicts
 
5091
 * between table_ref := '(' joined_table ')' alias_clause
 
5092
 * and joined_table := '(' joined_table ')'.  So, we must have the
 
5093
 * redundant-looking productions here instead.
 
5094
 */
 
5095
table_ref:      relation_expr
 
5096
                                {
 
5097
                                        $$ = (Node *) $1;
 
5098
                                }
 
5099
                        | relation_expr alias_clause
 
5100
                                {
 
5101
                                        $1->alias = $2;
 
5102
                                        $$ = (Node *) $1;
 
5103
                                }
 
5104
                        | func_table
 
5105
                                {
 
5106
                                        RangeFunction *n = makeNode(RangeFunction);
 
5107
                                        n->funccallnode = $1;
 
5108
                                        n->coldeflist = NIL;
 
5109
                                        $$ = (Node *) n;
 
5110
                                }
 
5111
                        | func_table alias_clause
 
5112
                                {
 
5113
                                        RangeFunction *n = makeNode(RangeFunction);
 
5114
                                        n->funccallnode = $1;
 
5115
                                        n->alias = $2;
 
5116
                                        n->coldeflist = NIL;
 
5117
                                        $$ = (Node *) n;
 
5118
                                }
 
5119
                        | func_table AS '(' TableFuncElementList ')'
 
5120
                                {
 
5121
                                        RangeFunction *n = makeNode(RangeFunction);
 
5122
                                        n->funccallnode = $1;
 
5123
                                        n->coldeflist = $4;
 
5124
                                        $$ = (Node *) n;
 
5125
                                }
 
5126
                        | func_table AS ColId '(' TableFuncElementList ')'
 
5127
                                {
 
5128
                                        RangeFunction *n = makeNode(RangeFunction);
 
5129
                                        Alias *a = makeNode(Alias);
 
5130
                                        n->funccallnode = $1;
 
5131
                                        a->aliasname = $3;
 
5132
                                        n->alias = a;
 
5133
                                        n->coldeflist = $5;
 
5134
                                        $$ = (Node *) n;
 
5135
                                }
 
5136
                        | func_table ColId '(' TableFuncElementList ')'
 
5137
                                {
 
5138
                                        RangeFunction *n = makeNode(RangeFunction);
 
5139
                                        Alias *a = makeNode(Alias);
 
5140
                                        n->funccallnode = $1;
 
5141
                                        a->aliasname = $2;
 
5142
                                        n->alias = a;
 
5143
                                        n->coldeflist = $4;
 
5144
                                        $$ = (Node *) n;
 
5145
                                }
 
5146
                        | select_with_parens
 
5147
                                {
 
5148
                                        /*
 
5149
                                         * The SQL spec does not permit a subselect
 
5150
                                         * (<derived_table>) without an alias clause,
 
5151
                                         * so we don't either.  This avoids the problem
 
5152
                                         * of needing to invent a unique refname for it.
 
5153
                                         * That could be surmounted if there's sufficient
 
5154
                                         * popular demand, but for now let's just implement
 
5155
                                         * the spec and see if anyone complains.
 
5156
                                         * However, it does seem like a good idea to emit
 
5157
                                         * an error message that's better than "syntax error".
 
5158
                                         */
 
5159
                                        ereport(ERROR,
 
5160
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
5161
                                                         errmsg("subquery in FROM must have an alias"),
 
5162
                                                         errhint("For example, FROM (SELECT ...) [AS] foo.")));
 
5163
                                        $$ = NULL;
 
5164
                                }
 
5165
                        | select_with_parens alias_clause
 
5166
                                {
 
5167
                                        RangeSubselect *n = makeNode(RangeSubselect);
 
5168
                                        n->subquery = $1;
 
5169
                                        n->alias = $2;
 
5170
                                        $$ = (Node *) n;
 
5171
                                }
 
5172
                        | joined_table
 
5173
                                {
 
5174
                                        $$ = (Node *) $1;
 
5175
                                }
 
5176
                        | '(' joined_table ')' alias_clause
 
5177
                                {
 
5178
                                        $2->alias = $4;
 
5179
                                        $$ = (Node *) $2;
 
5180
                                }
 
5181
                ;
 
5182
 
 
5183
 
 
5184
/*
 
5185
 * It may seem silly to separate joined_table from table_ref, but there is
 
5186
 * method in SQL92's madness: if you don't do it this way you get reduce-
 
5187
 * reduce conflicts, because it's not clear to the parser generator whether
 
5188
 * to expect alias_clause after ')' or not.  For the same reason we must
 
5189
 * treat 'JOIN' and 'join_type JOIN' separately, rather than allowing
 
5190
 * join_type to expand to empty; if we try it, the parser generator can't
 
5191
 * figure out when to reduce an empty join_type right after table_ref.
 
5192
 *
 
5193
 * Note that a CROSS JOIN is the same as an unqualified
 
5194
 * INNER JOIN, and an INNER JOIN/ON has the same shape
 
5195
 * but a qualification expression to limit membership.
 
5196
 * A NATURAL JOIN implicitly matches column names between
 
5197
 * tables and the shape is determined by which columns are
 
5198
 * in common. We'll collect columns during the later transformations.
 
5199
 */
 
5200
 
 
5201
joined_table:
 
5202
                        '(' joined_table ')'
 
5203
                                {
 
5204
                                        $$ = $2;
 
5205
                                }
 
5206
                        | table_ref CROSS JOIN table_ref
 
5207
                                {
 
5208
                                        /* CROSS JOIN is same as unqualified inner join */
 
5209
                                        JoinExpr *n = makeNode(JoinExpr);
 
5210
                                        n->jointype = JOIN_INNER;
 
5211
                                        n->isNatural = FALSE;
 
5212
                                        n->larg = $1;
 
5213
                                        n->rarg = $4;
 
5214
                                        n->using = NIL;
 
5215
                                        n->quals = NULL;
 
5216
                                        $$ = n;
 
5217
                                }
 
5218
                        | table_ref UNIONJOIN table_ref
 
5219
                                {
 
5220
                                        /* UNION JOIN is made into 1 token to avoid shift/reduce
 
5221
                                         * conflict against regular UNION keyword.
 
5222
                                         */
 
5223
                                        JoinExpr *n = makeNode(JoinExpr);
 
5224
                                        n->jointype = JOIN_UNION;
 
5225
                                        n->isNatural = FALSE;
 
5226
                                        n->larg = $1;
 
5227
                                        n->rarg = $3;
 
5228
                                        n->using = NIL;
 
5229
                                        n->quals = NULL;
 
5230
                                        $$ = n;
 
5231
                                }
 
5232
                        | table_ref join_type JOIN table_ref join_qual
 
5233
                                {
 
5234
                                        JoinExpr *n = makeNode(JoinExpr);
 
5235
                                        n->jointype = $2;
 
5236
                                        n->isNatural = FALSE;
 
5237
                                        n->larg = $1;
 
5238
                                        n->rarg = $4;
 
5239
                                        if ($5 != NULL && IsA($5, List))
 
5240
                                                n->using = (List *) $5; /* USING clause */
 
5241
                                        else
 
5242
                                                n->quals = $5; /* ON clause */
 
5243
                                        $$ = n;
 
5244
                                }
 
5245
                        | table_ref JOIN table_ref join_qual
 
5246
                                {
 
5247
                                        /* letting join_type reduce to empty doesn't work */
 
5248
                                        JoinExpr *n = makeNode(JoinExpr);
 
5249
                                        n->jointype = JOIN_INNER;
 
5250
                                        n->isNatural = FALSE;
 
5251
                                        n->larg = $1;
 
5252
                                        n->rarg = $3;
 
5253
                                        if ($4 != NULL && IsA($4, List))
 
5254
                                                n->using = (List *) $4; /* USING clause */
 
5255
                                        else
 
5256
                                                n->quals = $4; /* ON clause */
 
5257
                                        $$ = n;
 
5258
                                }
 
5259
                        | table_ref NATURAL join_type JOIN table_ref
 
5260
                                {
 
5261
                                        JoinExpr *n = makeNode(JoinExpr);
 
5262
                                        n->jointype = $3;
 
5263
                                        n->isNatural = TRUE;
 
5264
                                        n->larg = $1;
 
5265
                                        n->rarg = $5;
 
5266
                                        n->using = NIL; /* figure out which columns later... */
 
5267
                                        n->quals = NULL; /* fill later */
 
5268
                                        $$ = n;
 
5269
                                }
 
5270
                        | table_ref NATURAL JOIN table_ref
 
5271
                                {
 
5272
                                        /* letting join_type reduce to empty doesn't work */
 
5273
                                        JoinExpr *n = makeNode(JoinExpr);
 
5274
                                        n->jointype = JOIN_INNER;
 
5275
                                        n->isNatural = TRUE;
 
5276
                                        n->larg = $1;
 
5277
                                        n->rarg = $4;
 
5278
                                        n->using = NIL; /* figure out which columns later... */
 
5279
                                        n->quals = NULL; /* fill later */
 
5280
                                        $$ = n;
 
5281
                                }
 
5282
                ;
 
5283
 
 
5284
alias_clause:
 
5285
                        AS ColId '(' name_list ')'
 
5286
                                {
 
5287
                                        $$ = makeNode(Alias);
 
5288
                                        $$->aliasname = $2;
 
5289
                                        $$->colnames = $4;
 
5290
                                }
 
5291
                        | AS ColId
 
5292
                                {
 
5293
                                        $$ = makeNode(Alias);
 
5294
                                        $$->aliasname = $2;
 
5295
                                }
 
5296
                        | ColId '(' name_list ')'
 
5297
                                {
 
5298
                                        $$ = makeNode(Alias);
 
5299
                                        $$->aliasname = $1;
 
5300
                                        $$->colnames = $3;
 
5301
                                }
 
5302
                        | ColId
 
5303
                                {
 
5304
                                        $$ = makeNode(Alias);
 
5305
                                        $$->aliasname = $1;
 
5306
                                }
 
5307
                ;
 
5308
 
 
5309
join_type:      FULL join_outer                                                 { $$ = JOIN_FULL; }
 
5310
                        | LEFT join_outer                                               { $$ = JOIN_LEFT; }
 
5311
                        | RIGHT join_outer                                              { $$ = JOIN_RIGHT; }
 
5312
                        | INNER_P                                                               { $$ = JOIN_INNER; }
 
5313
                ;
 
5314
 
 
5315
/* OUTER is just noise... */
 
5316
join_outer: OUTER_P                                                                     { $$ = NULL; }
 
5317
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
5318
                ;
 
5319
 
 
5320
/* JOIN qualification clauses
 
5321
 * Possibilities are:
 
5322
 *      USING ( column list ) allows only unqualified column names,
 
5323
 *                                                which must match between tables.
 
5324
 *      ON expr allows more general qualifications.
 
5325
 *
 
5326
 * We return USING as a List node, while an ON-expr will not be a List.
 
5327
 */
 
5328
 
 
5329
join_qual:      USING '(' name_list ')'                                 { $$ = (Node *) $3; }
 
5330
                        | ON a_expr                                                             { $$ = $2; }
 
5331
                ;
 
5332
 
 
5333
 
 
5334
relation_expr:
 
5335
                        qualified_name
 
5336
                                {
 
5337
                                        /* default inheritance */
 
5338
                                        $$ = $1;
 
5339
                                        $$->inhOpt = INH_DEFAULT;
 
5340
                                        $$->alias = NULL;
 
5341
                                }
 
5342
                        | qualified_name '*'
 
5343
                                {
 
5344
                                        /* inheritance query */
 
5345
                                        $$ = $1;
 
5346
                                        $$->inhOpt = INH_YES;
 
5347
                                        $$->alias = NULL;
 
5348
                                }
 
5349
                        | ONLY qualified_name
 
5350
                                {
 
5351
                                        /* no inheritance */
 
5352
                                        $$ = $2;
 
5353
                                        $$->inhOpt = INH_NO;
 
5354
                                        $$->alias = NULL;
 
5355
                                }
 
5356
                        | ONLY '(' qualified_name ')'
 
5357
                                {
 
5358
                                        /* no inheritance, SQL99-style syntax */
 
5359
                                        $$ = $3;
 
5360
                                        $$->inhOpt = INH_NO;
 
5361
                                        $$->alias = NULL;
 
5362
                                }
 
5363
                ;
 
5364
 
 
5365
 
 
5366
func_table: func_expr                                                           { $$ = $1; }
 
5367
                ;
 
5368
 
 
5369
 
 
5370
where_clause:
 
5371
                        WHERE a_expr                                                    { $$ = $2; }
 
5372
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
5373
                ;
 
5374
 
 
5375
 
 
5376
TableFuncElementList:
 
5377
                        TableFuncElement
 
5378
                                {
 
5379
                                        $$ = list_make1($1);
 
5380
                                }
 
5381
                        | TableFuncElementList ',' TableFuncElement
 
5382
                                {
 
5383
                                        $$ = lappend($1, $3);
 
5384
                                }
 
5385
                ;
 
5386
 
 
5387
TableFuncElement:       ColId Typename
 
5388
                                {
 
5389
                                        ColumnDef *n = makeNode(ColumnDef);
 
5390
                                        n->colname = $1;
 
5391
                                        n->typename = $2;
 
5392
                                        n->constraints = NIL;
 
5393
                                        n->is_local = true;
 
5394
                                        $$ = (Node *)n;
 
5395
                                }
 
5396
                ;
 
5397
 
 
5398
/*****************************************************************************
 
5399
 *
 
5400
 *      Type syntax
 
5401
 *              SQL92 introduces a large amount of type-specific syntax.
 
5402
 *              Define individual clauses to handle these cases, and use
 
5403
 *               the generic case to handle regular type-extensible Postgres syntax.
 
5404
 *              - thomas 1997-10-10
 
5405
 *
 
5406
 *****************************************************************************/
 
5407
 
 
5408
Typename:       SimpleTypename opt_array_bounds
 
5409
                                {
 
5410
                                        $$ = $1;
 
5411
                                        $$->arrayBounds = $2;
 
5412
                                }
 
5413
                        | SETOF SimpleTypename opt_array_bounds
 
5414
                                {
 
5415
                                        $$ = $2;
 
5416
                                        $$->arrayBounds = $3;
 
5417
                                        $$->setof = TRUE;
 
5418
                                }
 
5419
                        | SimpleTypename ARRAY '[' Iconst ']'
 
5420
                                {
 
5421
                                        /* SQL99's redundant syntax */
 
5422
                                        $$ = $1;
 
5423
                                        $$->arrayBounds = list_make1(makeInteger($4));
 
5424
                                }
 
5425
                        | SETOF SimpleTypename ARRAY '[' Iconst ']'
 
5426
                                {
 
5427
                                        /* SQL99's redundant syntax */
 
5428
                                        $$ = $2;
 
5429
                                        $$->arrayBounds = list_make1(makeInteger($5));
 
5430
                                        $$->setof = TRUE;
 
5431
                                }
 
5432
                ;
 
5433
 
 
5434
opt_array_bounds:
 
5435
                        opt_array_bounds '[' ']'
 
5436
                                        {  $$ = lappend($1, makeInteger(-1)); }
 
5437
                        | opt_array_bounds '[' Iconst ']'
 
5438
                                        {  $$ = lappend($1, makeInteger($3)); }
 
5439
                        | /*EMPTY*/
 
5440
                                        {  $$ = NIL; }
 
5441
                ;
 
5442
 
 
5443
/*
 
5444
 * XXX ideally, the production for a qualified typename should be ColId attrs
 
5445
 * (there's no obvious reason why the first name should need to be restricted)
 
5446
 * and should be an alternative of GenericType (so that it can be used to
 
5447
 * specify a type for a literal in AExprConst).  However doing either causes
 
5448
 * reduce/reduce conflicts that I haven't been able to find a workaround
 
5449
 * for.  FIXME later.
 
5450
 */
 
5451
SimpleTypename:
 
5452
                        GenericType                                                             { $$ = $1; }
 
5453
                        | Numeric                                                               { $$ = $1; }
 
5454
                        | Bit                                                                   { $$ = $1; }
 
5455
                        | Character                                                             { $$ = $1; }
 
5456
                        | ConstDatetime                                                 { $$ = $1; }
 
5457
                        | ConstInterval opt_interval
 
5458
                                {
 
5459
                                        $$ = $1;
 
5460
                                        if ($2 != INTERVAL_FULL_RANGE)
 
5461
                                                $$->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $2);
 
5462
                                }
 
5463
                        | ConstInterval '(' Iconst ')' opt_interval
 
5464
                                {
 
5465
                                        $$ = $1;
 
5466
                                        if ($3 < 0)
 
5467
                                                ereport(ERROR,
 
5468
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5469
                                                                 errmsg("INTERVAL(%d) precision must not be negative",
 
5470
                                                                                $3)));
 
5471
                                        if ($3 > MAX_INTERVAL_PRECISION)
 
5472
                                        {
 
5473
                                                ereport(WARNING,
 
5474
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5475
                                                                 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
 
5476
                                                                                $3, MAX_INTERVAL_PRECISION)));
 
5477
                                                $3 = MAX_INTERVAL_PRECISION;
 
5478
                                        }
 
5479
                                        $$->typmod = INTERVAL_TYPMOD($3, $5);
 
5480
                                }
 
5481
                        | type_name attrs
 
5482
                                {
 
5483
                                        $$ = makeNode(TypeName);
 
5484
                                        $$->names = lcons(makeString($1), $2);
 
5485
                                        $$->typmod = -1;
 
5486
                                }
 
5487
                ;
 
5488
 
 
5489
/* We have a separate ConstTypename to allow defaulting fixed-length
 
5490
 * types such as CHAR() and BIT() to an unspecified length.
 
5491
 * SQL9x requires that these default to a length of one, but this
 
5492
 * makes no sense for constructs like CHAR 'hi' and BIT '0101',
 
5493
 * where there is an obvious better choice to make.
 
5494
 * Note that ConstInterval is not included here since it must
 
5495
 * be pushed up higher in the rules to accomodate the postfix
 
5496
 * options (e.g. INTERVAL '1' YEAR).
 
5497
 */
 
5498
ConstTypename:
 
5499
                        GenericType                                                             { $$ = $1; }
 
5500
                        | Numeric                                                               { $$ = $1; }
 
5501
                        | ConstBit                                                              { $$ = $1; }
 
5502
                        | ConstCharacter                                                { $$ = $1; }
 
5503
                        | ConstDatetime                                                 { $$ = $1; }
 
5504
                ;
 
5505
 
 
5506
GenericType:
 
5507
                        type_name
 
5508
                                {
 
5509
                                        $$ = makeTypeName($1);
 
5510
                                }
 
5511
                ;
 
5512
 
 
5513
/* SQL92 numeric data types
 
5514
 * Check FLOAT() precision limits assuming IEEE floating types.
 
5515
 * - thomas 1997-09-18
 
5516
 * Provide real DECIMAL() and NUMERIC() implementations now - Jan 1998-12-30
 
5517
 */
 
5518
Numeric:        INT_P
 
5519
                                {
 
5520
                                        $$ = SystemTypeName("int4");
 
5521
                                }
 
5522
                        | INTEGER
 
5523
                                {
 
5524
                                        $$ = SystemTypeName("int4");
 
5525
                                }
 
5526
                        | SMALLINT
 
5527
                                {
 
5528
                                        $$ = SystemTypeName("int2");
 
5529
                                }
 
5530
                        | BIGINT
 
5531
                                {
 
5532
                                        $$ = SystemTypeName("int8");
 
5533
                                }
 
5534
                        | REAL
 
5535
                                {
 
5536
                                        $$ = SystemTypeName("float4");
 
5537
                                }
 
5538
                        | FLOAT_P opt_float
 
5539
                                {
 
5540
                                        $$ = $2;
 
5541
                                }
 
5542
                        | DOUBLE_P PRECISION
 
5543
                                {
 
5544
                                        $$ = SystemTypeName("float8");
 
5545
                                }
 
5546
                        | DECIMAL_P opt_decimal
 
5547
                                {
 
5548
                                        $$ = SystemTypeName("numeric");
 
5549
                                        $$->typmod = $2;
 
5550
                                }
 
5551
                        | DEC opt_decimal
 
5552
                                {
 
5553
                                        $$ = SystemTypeName("numeric");
 
5554
                                        $$->typmod = $2;
 
5555
                                }
 
5556
                        | NUMERIC opt_numeric
 
5557
                                {
 
5558
                                        $$ = SystemTypeName("numeric");
 
5559
                                        $$->typmod = $2;
 
5560
                                }
 
5561
                        | BOOLEAN_P
 
5562
                                {
 
5563
                                        $$ = SystemTypeName("bool");
 
5564
                                }
 
5565
                ;
 
5566
 
 
5567
opt_float:      '(' Iconst ')'
 
5568
                                {
 
5569
                                        if ($2 < 1)
 
5570
                                                ereport(ERROR,
 
5571
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5572
                                                                 errmsg("precision for type float must be at least 1 bit")));
 
5573
                                        else if ($2 <= 24)
 
5574
                                                $$ = SystemTypeName("float4");
 
5575
                                        else if ($2 <= 53)
 
5576
                                                $$ = SystemTypeName("float8");
 
5577
                                        else
 
5578
                                                ereport(ERROR,
 
5579
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5580
                                                                 errmsg("precision for type float must be less than 54 bits")));
 
5581
                                }
 
5582
                        | /*EMPTY*/
 
5583
                                {
 
5584
                                        $$ = SystemTypeName("float8");
 
5585
                                }
 
5586
                ;
 
5587
 
 
5588
opt_numeric:
 
5589
                        '(' Iconst ',' Iconst ')'
 
5590
                                {
 
5591
                                        if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
 
5592
                                                ereport(ERROR,
 
5593
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5594
                                                                 errmsg("NUMERIC precision %d must be between 1 and %d",
 
5595
                                                                                $2, NUMERIC_MAX_PRECISION)));
 
5596
                                        if ($4 < 0 || $4 > $2)
 
5597
                                                ereport(ERROR,
 
5598
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5599
                                                                 errmsg("NUMERIC scale %d must be between 0 and precision %d",
 
5600
                                                                                $4, $2)));
 
5601
 
 
5602
                                        $$ = (($2 << 16) | $4) + VARHDRSZ;
 
5603
                                }
 
5604
                        | '(' Iconst ')'
 
5605
                                {
 
5606
                                        if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
 
5607
                                                ereport(ERROR,
 
5608
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5609
                                                                 errmsg("NUMERIC precision %d must be between 1 and %d",
 
5610
                                                                                $2, NUMERIC_MAX_PRECISION)));
 
5611
 
 
5612
                                        $$ = ($2 << 16) + VARHDRSZ;
 
5613
                                }
 
5614
                        | /*EMPTY*/
 
5615
                                {
 
5616
                                        /* Insert "-1" meaning "no limit" */
 
5617
                                        $$ = -1;
 
5618
                                }
 
5619
                ;
 
5620
 
 
5621
opt_decimal:
 
5622
                        '(' Iconst ',' Iconst ')'
 
5623
                                {
 
5624
                                        if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
 
5625
                                                ereport(ERROR,
 
5626
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5627
                                                                 errmsg("DECIMAL precision %d must be between 1 and %d",
 
5628
                                                                                $2, NUMERIC_MAX_PRECISION)));
 
5629
                                        if ($4 < 0 || $4 > $2)
 
5630
                                                ereport(ERROR,
 
5631
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5632
                                                                 errmsg("DECIMAL scale %d must be between 0 and precision %d",
 
5633
                                                                                $4, $2)));
 
5634
 
 
5635
                                        $$ = (($2 << 16) | $4) + VARHDRSZ;
 
5636
                                }
 
5637
                        | '(' Iconst ')'
 
5638
                                {
 
5639
                                        if ($2 < 1 || $2 > NUMERIC_MAX_PRECISION)
 
5640
                                                ereport(ERROR,
 
5641
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5642
                                                                 errmsg("DECIMAL precision %d must be between 1 and %d",
 
5643
                                                                                $2, NUMERIC_MAX_PRECISION)));
 
5644
 
 
5645
                                        $$ = ($2 << 16) + VARHDRSZ;
 
5646
                                }
 
5647
                        | /*EMPTY*/
 
5648
                                {
 
5649
                                        /* Insert "-1" meaning "no limit" */
 
5650
                                        $$ = -1;
 
5651
                                }
 
5652
                ;
 
5653
 
 
5654
 
 
5655
/*
 
5656
 * SQL92 bit-field data types
 
5657
 * The following implements BIT() and BIT VARYING().
 
5658
 */
 
5659
Bit:            BitWithLength
 
5660
                                {
 
5661
                                        $$ = $1;
 
5662
                                }
 
5663
                        | BitWithoutLength
 
5664
                                {
 
5665
                                        $$ = $1;
 
5666
                                }
 
5667
                ;
 
5668
 
 
5669
/* ConstBit is like Bit except "BIT" defaults to unspecified length */
 
5670
/* See notes for ConstCharacter, which addresses same issue for "CHAR" */
 
5671
ConstBit:       BitWithLength
 
5672
                                {
 
5673
                                        $$ = $1;
 
5674
                                }
 
5675
                        | BitWithoutLength
 
5676
                                {
 
5677
                                        $$ = $1;
 
5678
                                        $$->typmod = -1;
 
5679
                                }
 
5680
                ;
 
5681
 
 
5682
BitWithLength:
 
5683
                        BIT opt_varying '(' Iconst ')'
 
5684
                                {
 
5685
                                        char *typname;
 
5686
 
 
5687
                                        typname = $2 ? "varbit" : "bit";
 
5688
                                        $$ = SystemTypeName(typname);
 
5689
                                        if ($4 < 1)
 
5690
                                                ereport(ERROR,
 
5691
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5692
                                                                 errmsg("length for type %s must be at least 1",
 
5693
                                                                                typname)));
 
5694
                                        else if ($4 > (MaxAttrSize * BITS_PER_BYTE))
 
5695
                                                ereport(ERROR,
 
5696
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5697
                                                                 errmsg("length for type %s cannot exceed %d",
 
5698
                                                                                typname, MaxAttrSize * BITS_PER_BYTE)));
 
5699
                                        $$->typmod = $4;
 
5700
                                }
 
5701
                ;
 
5702
 
 
5703
BitWithoutLength:
 
5704
                        BIT opt_varying
 
5705
                                {
 
5706
                                        /* bit defaults to bit(1), varbit to no limit */
 
5707
                                        if ($2)
 
5708
                                        {
 
5709
                                                $$ = SystemTypeName("varbit");
 
5710
                                                $$->typmod = -1;
 
5711
                                        }
 
5712
                                        else
 
5713
                                        {
 
5714
                                                $$ = SystemTypeName("bit");
 
5715
                                                $$->typmod = 1;
 
5716
                                        }
 
5717
                                }
 
5718
                ;
 
5719
 
 
5720
 
 
5721
/*
 
5722
 * SQL92 character data types
 
5723
 * The following implements CHAR() and VARCHAR().
 
5724
 */
 
5725
Character:  CharacterWithLength
 
5726
                                {
 
5727
                                        $$ = $1;
 
5728
                                }
 
5729
                        | CharacterWithoutLength
 
5730
                                {
 
5731
                                        $$ = $1;
 
5732
                                }
 
5733
                ;
 
5734
 
 
5735
ConstCharacter:  CharacterWithLength
 
5736
                                {
 
5737
                                        $$ = $1;
 
5738
                                }
 
5739
                        | CharacterWithoutLength
 
5740
                                {
 
5741
                                        /* Length was not specified so allow to be unrestricted.
 
5742
                                         * This handles problems with fixed-length (bpchar) strings
 
5743
                                         * which in column definitions must default to a length
 
5744
                                         * of one, but should not be constrained if the length
 
5745
                                         * was not specified.
 
5746
                                         */
 
5747
                                        $$ = $1;
 
5748
                                        $$->typmod = -1;
 
5749
                                }
 
5750
                ;
 
5751
 
 
5752
CharacterWithLength:  character '(' Iconst ')' opt_charset
 
5753
                                {
 
5754
                                        if (($5 != NULL) && (strcmp($5, "sql_text") != 0))
 
5755
                                        {
 
5756
                                                char *type;
 
5757
 
 
5758
                                                type = palloc(strlen($1) + 1 + strlen($5) + 1);
 
5759
                                                strcpy(type, $1);
 
5760
                                                strcat(type, "_");
 
5761
                                                strcat(type, $5);
 
5762
                                                $1 = type;
 
5763
                                        }
 
5764
 
 
5765
                                        $$ = SystemTypeName($1);
 
5766
 
 
5767
                                        if ($3 < 1)
 
5768
                                                ereport(ERROR,
 
5769
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5770
                                                                 errmsg("length for type %s must be at least 1",
 
5771
                                                                                $1)));
 
5772
                                        else if ($3 > MaxAttrSize)
 
5773
                                                ereport(ERROR,
 
5774
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5775
                                                                 errmsg("length for type %s cannot exceed %d",
 
5776
                                                                                $1, MaxAttrSize)));
 
5777
 
 
5778
                                        /* we actually implement these like a varlen, so
 
5779
                                         * the first 4 bytes is the length. (the difference
 
5780
                                         * between these and "text" is that we blank-pad and
 
5781
                                         * truncate where necessary)
 
5782
                                         */
 
5783
                                        $$->typmod = VARHDRSZ + $3;
 
5784
                                }
 
5785
                ;
 
5786
 
 
5787
CharacterWithoutLength:  character opt_charset
 
5788
                                {
 
5789
                                        if (($2 != NULL) && (strcmp($2, "sql_text") != 0))
 
5790
                                        {
 
5791
                                                char *type;
 
5792
 
 
5793
                                                type = palloc(strlen($1) + 1 + strlen($2) + 1);
 
5794
                                                strcpy(type, $1);
 
5795
                                                strcat(type, "_");
 
5796
                                                strcat(type, $2);
 
5797
                                                $1 = type;
 
5798
                                        }
 
5799
 
 
5800
                                        $$ = SystemTypeName($1);
 
5801
 
 
5802
                                        /* char defaults to char(1), varchar to no limit */
 
5803
                                        if (strcmp($1, "bpchar") == 0)
 
5804
                                                $$->typmod = VARHDRSZ + 1;
 
5805
                                        else
 
5806
                                                $$->typmod = -1;
 
5807
                                }
 
5808
                ;
 
5809
 
 
5810
character:      CHARACTER opt_varying
 
5811
                                                                                { $$ = $2 ? "varchar": "bpchar"; }
 
5812
                        | CHAR_P opt_varying
 
5813
                                                                                { $$ = $2 ? "varchar": "bpchar"; }
 
5814
                        | VARCHAR
 
5815
                                                                                { $$ = "varchar"; }
 
5816
                        | NATIONAL CHARACTER opt_varying
 
5817
                                                                                { $$ = $3 ? "varchar": "bpchar"; }
 
5818
                        | NATIONAL CHAR_P opt_varying
 
5819
                                                                                { $$ = $3 ? "varchar": "bpchar"; }
 
5820
                        | NCHAR opt_varying
 
5821
                                                                                { $$ = $2 ? "varchar": "bpchar"; }
 
5822
                ;
 
5823
 
 
5824
opt_varying:
 
5825
                        VARYING                                                                 { $$ = TRUE; }
 
5826
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
5827
                ;
 
5828
 
 
5829
opt_charset:
 
5830
                        CHARACTER SET ColId                                             { $$ = $3; }
 
5831
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
5832
                ;
 
5833
 
 
5834
ConstDatetime:
 
5835
                        TIMESTAMP '(' Iconst ')' opt_timezone
 
5836
                                {
 
5837
                                        if ($5)
 
5838
                                                $$ = SystemTypeName("timestamptz");
 
5839
                                        else
 
5840
                                                $$ = SystemTypeName("timestamp");
 
5841
                                        /* XXX the timezone field seems to be unused
 
5842
                                         * - thomas 2001-09-06
 
5843
                                         */
 
5844
                                        $$->timezone = $5;
 
5845
                                        if ($3 < 0)
 
5846
                                                ereport(ERROR,
 
5847
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5848
                                                                 errmsg("TIMESTAMP(%d)%s precision must not be negative",
 
5849
                                                                                $3, ($5 ? " WITH TIME ZONE": ""))));
 
5850
                                        if ($3 > MAX_TIMESTAMP_PRECISION)
 
5851
                                        {
 
5852
                                                ereport(WARNING,
 
5853
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5854
                                                                 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
 
5855
                                                                                $3, ($5 ? " WITH TIME ZONE": ""),
 
5856
                                                                                MAX_TIMESTAMP_PRECISION)));
 
5857
                                                $3 = MAX_TIMESTAMP_PRECISION;
 
5858
                                        }
 
5859
                                        $$->typmod = $3;
 
5860
                                }
 
5861
                        | TIMESTAMP opt_timezone
 
5862
                                {
 
5863
                                        if ($2)
 
5864
                                                $$ = SystemTypeName("timestamptz");
 
5865
                                        else
 
5866
                                                $$ = SystemTypeName("timestamp");
 
5867
                                        /* XXX the timezone field seems to be unused
 
5868
                                         * - thomas 2001-09-06
 
5869
                                         */
 
5870
                                        $$->timezone = $2;
 
5871
                                        /* SQL99 specified a default precision of six
 
5872
                                         * for schema definitions. But for timestamp
 
5873
                                         * literals we don't want to throw away precision
 
5874
                                         * so leave this as unspecified for now.
 
5875
                                         * Later, we may want a different production
 
5876
                                         * for schemas. - thomas 2001-12-07
 
5877
                                         */
 
5878
                                        $$->typmod = -1;
 
5879
                                }
 
5880
                        | TIME '(' Iconst ')' opt_timezone
 
5881
                                {
 
5882
                                        if ($5)
 
5883
                                                $$ = SystemTypeName("timetz");
 
5884
                                        else
 
5885
                                                $$ = SystemTypeName("time");
 
5886
                                        if ($3 < 0)
 
5887
                                                ereport(ERROR,
 
5888
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5889
                                                                 errmsg("TIME(%d)%s precision must not be negative",
 
5890
                                                                                $3, ($5 ? " WITH TIME ZONE": ""))));
 
5891
                                        if ($3 > MAX_TIME_PRECISION)
 
5892
                                        {
 
5893
                                                ereport(WARNING,
 
5894
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
5895
                                                                 errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
 
5896
                                                                                $3, ($5 ? " WITH TIME ZONE": ""),
 
5897
                                                                                MAX_TIME_PRECISION)));
 
5898
                                                $3 = MAX_TIME_PRECISION;
 
5899
                                        }
 
5900
                                        $$->typmod = $3;
 
5901
                                }
 
5902
                        | TIME opt_timezone
 
5903
                                {
 
5904
                                        if ($2)
 
5905
                                                $$ = SystemTypeName("timetz");
 
5906
                                        else
 
5907
                                                $$ = SystemTypeName("time");
 
5908
                                        /* SQL99 specified a default precision of zero.
 
5909
                                         * See comments for timestamp above on why we will
 
5910
                                         * leave this unspecified for now. - thomas 2001-12-07
 
5911
                                         */
 
5912
                                        $$->typmod = -1;
 
5913
                                }
 
5914
                ;
 
5915
 
 
5916
ConstInterval:
 
5917
                        INTERVAL                                                                { $$ = SystemTypeName("interval"); }
 
5918
                ;
 
5919
 
 
5920
opt_timezone:
 
5921
                        WITH TIME ZONE                                                  { $$ = TRUE; }
 
5922
                        | WITHOUT TIME ZONE                                             { $$ = FALSE; }
 
5923
                        | /*EMPTY*/                                                             { $$ = FALSE; }
 
5924
                ;
 
5925
 
 
5926
opt_interval:
 
5927
                        YEAR_P                                                                  { $$ = INTERVAL_MASK(YEAR); }
 
5928
                        | MONTH_P                                                               { $$ = INTERVAL_MASK(MONTH); }
 
5929
                        | DAY_P                                                                 { $$ = INTERVAL_MASK(DAY); }
 
5930
                        | HOUR_P                                                                { $$ = INTERVAL_MASK(HOUR); }
 
5931
                        | MINUTE_P                                                              { $$ = INTERVAL_MASK(MINUTE); }
 
5932
                        | SECOND_P                                                              { $$ = INTERVAL_MASK(SECOND); }
 
5933
                        | YEAR_P TO MONTH_P
 
5934
                                        { $$ = INTERVAL_MASK(YEAR) | INTERVAL_MASK(MONTH); }
 
5935
                        | DAY_P TO HOUR_P
 
5936
                                        { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR); }
 
5937
                        | DAY_P TO MINUTE_P
 
5938
                                        { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
 
5939
                                                | INTERVAL_MASK(MINUTE); }
 
5940
                        | DAY_P TO SECOND_P
 
5941
                                        { $$ = INTERVAL_MASK(DAY) | INTERVAL_MASK(HOUR)
 
5942
                                                | INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
 
5943
                        | HOUR_P TO MINUTE_P
 
5944
                                        { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE); }
 
5945
                        | HOUR_P TO SECOND_P
 
5946
                                        { $$ = INTERVAL_MASK(HOUR) | INTERVAL_MASK(MINUTE)
 
5947
                                                | INTERVAL_MASK(SECOND); }
 
5948
                        | MINUTE_P TO SECOND_P
 
5949
                                        { $$ = INTERVAL_MASK(MINUTE) | INTERVAL_MASK(SECOND); }
 
5950
                        | /*EMPTY*/                                                             { $$ = INTERVAL_FULL_RANGE; }
 
5951
                ;
 
5952
 
 
5953
 
 
5954
/*****************************************************************************
 
5955
 *
 
5956
 *      expression grammar
 
5957
 *
 
5958
 *****************************************************************************/
 
5959
 
 
5960
/*
 
5961
 * General expressions
 
5962
 * This is the heart of the expression syntax.
 
5963
 *
 
5964
 * We have two expression types: a_expr is the unrestricted kind, and
 
5965
 * b_expr is a subset that must be used in some places to avoid shift/reduce
 
5966
 * conflicts.  For example, we can't do BETWEEN as "BETWEEN a_expr AND a_expr"
 
5967
 * because that use of AND conflicts with AND as a boolean operator.  So,
 
5968
 * b_expr is used in BETWEEN and we remove boolean keywords from b_expr.
 
5969
 *
 
5970
 * Note that '(' a_expr ')' is a b_expr, so an unrestricted expression can
 
5971
 * always be used by surrounding it with parens.
 
5972
 *
 
5973
 * c_expr is all the productions that are common to a_expr and b_expr;
 
5974
 * it's factored out just to eliminate redundant coding.
 
5975
 */
 
5976
a_expr:         c_expr                                                                  { $$ = $1; }
 
5977
                        | a_expr TYPECAST Typename
 
5978
                                        { $$ = makeTypeCast($1, $3); }
 
5979
                        | a_expr AT TIME ZONE c_expr
 
5980
                                {
 
5981
                                        FuncCall *n = makeNode(FuncCall);
 
5982
                                        n->funcname = SystemFuncName("timezone");
 
5983
                                        n->args = list_make2($5, $1);
 
5984
                                        n->agg_star = FALSE;
 
5985
                                        n->agg_distinct = FALSE;
 
5986
                                        $$ = (Node *) n;
 
5987
                                }
 
5988
                /*
 
5989
                 * These operators must be called out explicitly in order to make use
 
5990
                 * of yacc/bison's automatic operator-precedence handling.  All other
 
5991
                 * operator names are handled by the generic productions using "Op",
 
5992
                 * below; and all those operators will have the same precedence.
 
5993
                 *
 
5994
                 * If you add more explicitly-known operators, be sure to add them
 
5995
                 * also to b_expr and to the MathOp list above.
 
5996
                 */
 
5997
                        | '+' a_expr                                    %prec UMINUS
 
5998
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
 
5999
                        | '-' a_expr                                    %prec UMINUS
 
6000
                                { $$ = doNegate($2); }
 
6001
                        | '%' a_expr
 
6002
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", NULL, $2); }
 
6003
                        | '^' a_expr
 
6004
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", NULL, $2); }
 
6005
                        | a_expr '%'
 
6006
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, NULL); }
 
6007
                        | a_expr '^'
 
6008
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, NULL); }
 
6009
                        | a_expr '+' a_expr
 
6010
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
 
6011
                        | a_expr '-' a_expr
 
6012
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
 
6013
                        | a_expr '*' a_expr
 
6014
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
 
6015
                        | a_expr '/' a_expr
 
6016
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
 
6017
                        | a_expr '%' a_expr
 
6018
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
 
6019
                        | a_expr '^' a_expr
 
6020
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
 
6021
                        | a_expr '<' a_expr
 
6022
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
 
6023
                        | a_expr '>' a_expr
 
6024
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
 
6025
                        | a_expr '=' a_expr
 
6026
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
 
6027
 
 
6028
                        | a_expr qual_Op a_expr                         %prec Op
 
6029
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
 
6030
                        | qual_Op a_expr                                        %prec Op
 
6031
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
 
6032
                        | a_expr qual_Op                                        %prec POSTFIXOP
 
6033
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
 
6034
 
 
6035
                        | a_expr AND a_expr
 
6036
                                { $$ = (Node *) makeA_Expr(AEXPR_AND, NIL, $1, $3); }
 
6037
                        | a_expr OR a_expr
 
6038
                                { $$ = (Node *) makeA_Expr(AEXPR_OR, NIL, $1, $3); }
 
6039
                        | NOT a_expr
 
6040
                                { $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, $2); }
 
6041
 
 
6042
                        | a_expr LIKE a_expr
 
6043
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, $3); }
 
6044
                        | a_expr LIKE a_expr ESCAPE a_expr
 
6045
                                {
 
6046
                                        FuncCall *n = makeNode(FuncCall);
 
6047
                                        n->funcname = SystemFuncName("like_escape");
 
6048
                                        n->args = list_make2($3, $5);
 
6049
                                        n->agg_star = FALSE;
 
6050
                                        n->agg_distinct = FALSE;
 
6051
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~", $1, (Node *) n);
 
6052
                                }
 
6053
                        | a_expr NOT LIKE a_expr
 
6054
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, $4); }
 
6055
                        | a_expr NOT LIKE a_expr ESCAPE a_expr
 
6056
                                {
 
6057
                                        FuncCall *n = makeNode(FuncCall);
 
6058
                                        n->funcname = SystemFuncName("like_escape");
 
6059
                                        n->args = list_make2($4, $6);
 
6060
                                        n->agg_star = FALSE;
 
6061
                                        n->agg_distinct = FALSE;
 
6062
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~", $1, (Node *) n);
 
6063
                                }
 
6064
                        | a_expr ILIKE a_expr
 
6065
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, $3); }
 
6066
                        | a_expr ILIKE a_expr ESCAPE a_expr
 
6067
                                {
 
6068
                                        FuncCall *n = makeNode(FuncCall);
 
6069
                                        n->funcname = SystemFuncName("like_escape");
 
6070
                                        n->args = list_make2($3, $5);
 
6071
                                        n->agg_star = FALSE;
 
6072
                                        n->agg_distinct = FALSE;
 
6073
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~~*", $1, (Node *) n);
 
6074
                                }
 
6075
                        | a_expr NOT ILIKE a_expr
 
6076
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, $4); }
 
6077
                        | a_expr NOT ILIKE a_expr ESCAPE a_expr
 
6078
                                {
 
6079
                                        FuncCall *n = makeNode(FuncCall);
 
6080
                                        n->funcname = SystemFuncName("like_escape");
 
6081
                                        n->args = list_make2($4, $6);
 
6082
                                        n->agg_star = FALSE;
 
6083
                                        n->agg_distinct = FALSE;
 
6084
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~~*", $1, (Node *) n);
 
6085
                                }
 
6086
 
 
6087
                        | a_expr SIMILAR TO a_expr                              %prec SIMILAR
 
6088
                                {
 
6089
                                        A_Const *c = makeNode(A_Const);
 
6090
                                        FuncCall *n = makeNode(FuncCall);
 
6091
                                        c->val.type = T_Null;
 
6092
                                        n->funcname = SystemFuncName("similar_escape");
 
6093
                                        n->args = list_make2($4, (Node *) c);
 
6094
                                        n->agg_star = FALSE;
 
6095
                                        n->agg_distinct = FALSE;
 
6096
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
 
6097
                                }
 
6098
                        | a_expr SIMILAR TO a_expr ESCAPE a_expr
 
6099
                                {
 
6100
                                        FuncCall *n = makeNode(FuncCall);
 
6101
                                        n->funcname = SystemFuncName("similar_escape");
 
6102
                                        n->args = list_make2($4, $6);
 
6103
                                        n->agg_star = FALSE;
 
6104
                                        n->agg_distinct = FALSE;
 
6105
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "~", $1, (Node *) n);
 
6106
                                }
 
6107
                        | a_expr NOT SIMILAR TO a_expr                  %prec SIMILAR
 
6108
                                {
 
6109
                                        A_Const *c = makeNode(A_Const);
 
6110
                                        FuncCall *n = makeNode(FuncCall);
 
6111
                                        c->val.type = T_Null;
 
6112
                                        n->funcname = SystemFuncName("similar_escape");
 
6113
                                        n->args = list_make2($5, (Node *) c);
 
6114
                                        n->agg_star = FALSE;
 
6115
                                        n->agg_distinct = FALSE;
 
6116
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
 
6117
                                }
 
6118
                        | a_expr NOT SIMILAR TO a_expr ESCAPE a_expr
 
6119
                                {
 
6120
                                        FuncCall *n = makeNode(FuncCall);
 
6121
                                        n->funcname = SystemFuncName("similar_escape");
 
6122
                                        n->args = list_make2($5, $7);
 
6123
                                        n->agg_star = FALSE;
 
6124
                                        n->agg_distinct = FALSE;
 
6125
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "!~", $1, (Node *) n);
 
6126
                                }
 
6127
 
 
6128
                        /* NullTest clause
 
6129
                         * Define SQL92-style Null test clause.
 
6130
                         * Allow two forms described in the standard:
 
6131
                         *      a IS NULL
 
6132
                         *      a IS NOT NULL
 
6133
                         * Allow two SQL extensions
 
6134
                         *      a ISNULL
 
6135
                         *      a NOTNULL
 
6136
                         */
 
6137
                        | a_expr ISNULL
 
6138
                                {
 
6139
                                        if (IsA($1, RowExpr))
 
6140
                                                $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
 
6141
                                        else
 
6142
                                        {
 
6143
                                                NullTest *n = makeNode(NullTest);
 
6144
                                                n->arg = (Expr *) $1;
 
6145
                                                n->nulltesttype = IS_NULL;
 
6146
                                                $$ = (Node *)n;
 
6147
                                        }
 
6148
                                }
 
6149
                        | a_expr IS NULL_P
 
6150
                                {
 
6151
                                        if (IsA($1, RowExpr))
 
6152
                                                $$ = makeRowNullTest(IS_NULL, (RowExpr *) $1);
 
6153
                                        else
 
6154
                                        {
 
6155
                                                NullTest *n = makeNode(NullTest);
 
6156
                                                n->arg = (Expr *) $1;
 
6157
                                                n->nulltesttype = IS_NULL;
 
6158
                                                $$ = (Node *)n;
 
6159
                                        }
 
6160
                                }
 
6161
                        | a_expr NOTNULL
 
6162
                                {
 
6163
                                        if (IsA($1, RowExpr))
 
6164
                                                $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
 
6165
                                        else
 
6166
                                        {
 
6167
                                                NullTest *n = makeNode(NullTest);
 
6168
                                                n->arg = (Expr *) $1;
 
6169
                                                n->nulltesttype = IS_NOT_NULL;
 
6170
                                                $$ = (Node *)n;
 
6171
                                        }
 
6172
                                }
 
6173
                        | a_expr IS NOT NULL_P
 
6174
                                {
 
6175
                                        if (IsA($1, RowExpr))
 
6176
                                                $$ = makeRowNullTest(IS_NOT_NULL, (RowExpr *) $1);
 
6177
                                        else
 
6178
                                        {
 
6179
                                                NullTest *n = makeNode(NullTest);
 
6180
                                                n->arg = (Expr *) $1;
 
6181
                                                n->nulltesttype = IS_NOT_NULL;
 
6182
                                                $$ = (Node *)n;
 
6183
                                        }
 
6184
                                }
 
6185
                        | row OVERLAPS row
 
6186
                                {
 
6187
                                        $$ = (Node *)makeOverlaps($1, $3);
 
6188
                                }
 
6189
                        | a_expr IS TRUE_P
 
6190
                                {
 
6191
                                        BooleanTest *b = makeNode(BooleanTest);
 
6192
                                        b->arg = (Expr *) $1;
 
6193
                                        b->booltesttype = IS_TRUE;
 
6194
                                        $$ = (Node *)b;
 
6195
                                }
 
6196
                        | a_expr IS NOT TRUE_P
 
6197
                                {
 
6198
                                        BooleanTest *b = makeNode(BooleanTest);
 
6199
                                        b->arg = (Expr *) $1;
 
6200
                                        b->booltesttype = IS_NOT_TRUE;
 
6201
                                        $$ = (Node *)b;
 
6202
                                }
 
6203
                        | a_expr IS FALSE_P
 
6204
                                {
 
6205
                                        BooleanTest *b = makeNode(BooleanTest);
 
6206
                                        b->arg = (Expr *) $1;
 
6207
                                        b->booltesttype = IS_FALSE;
 
6208
                                        $$ = (Node *)b;
 
6209
                                }
 
6210
                        | a_expr IS NOT FALSE_P
 
6211
                                {
 
6212
                                        BooleanTest *b = makeNode(BooleanTest);
 
6213
                                        b->arg = (Expr *) $1;
 
6214
                                        b->booltesttype = IS_NOT_FALSE;
 
6215
                                        $$ = (Node *)b;
 
6216
                                }
 
6217
                        | a_expr IS UNKNOWN
 
6218
                                {
 
6219
                                        BooleanTest *b = makeNode(BooleanTest);
 
6220
                                        b->arg = (Expr *) $1;
 
6221
                                        b->booltesttype = IS_UNKNOWN;
 
6222
                                        $$ = (Node *)b;
 
6223
                                }
 
6224
                        | a_expr IS NOT UNKNOWN
 
6225
                                {
 
6226
                                        BooleanTest *b = makeNode(BooleanTest);
 
6227
                                        b->arg = (Expr *) $1;
 
6228
                                        b->booltesttype = IS_NOT_UNKNOWN;
 
6229
                                        $$ = (Node *)b;
 
6230
                                }
 
6231
                        | a_expr IS DISTINCT FROM a_expr                        %prec IS
 
6232
                                {
 
6233
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
 
6234
                                }
 
6235
                        | a_expr IS OF '(' type_list ')'                        %prec IS
 
6236
                                {
 
6237
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
 
6238
                                }
 
6239
                        | a_expr IS NOT OF '(' type_list ')'            %prec IS
 
6240
                                {
 
6241
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
 
6242
                                }
 
6243
                        | a_expr BETWEEN b_expr AND b_expr                      %prec BETWEEN
 
6244
                                {
 
6245
                                        $$ = (Node *) makeA_Expr(AEXPR_AND, NIL,
 
6246
                                                (Node *) makeSimpleA_Expr(AEXPR_OP, ">=", $1, $3),
 
6247
                                                (Node *) makeSimpleA_Expr(AEXPR_OP, "<=", $1, $5));
 
6248
                                }
 
6249
                        | a_expr NOT BETWEEN b_expr AND b_expr          %prec BETWEEN
 
6250
                                {
 
6251
                                        $$ = (Node *) makeA_Expr(AEXPR_OR, NIL,
 
6252
                                                (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $4),
 
6253
                                                (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $6));
 
6254
                                }
 
6255
                        | a_expr IN_P in_expr
 
6256
                                {
 
6257
                                        /* in_expr returns a SubLink or a list of a_exprs */
 
6258
                                        if (IsA($3, SubLink))
 
6259
                                        {
 
6260
                                                        SubLink *n = (SubLink *)$3;
 
6261
                                                        n->subLinkType = ANY_SUBLINK;
 
6262
                                                        if (IsA($1, RowExpr))
 
6263
                                                                n->lefthand = ((RowExpr *) $1)->args;
 
6264
                                                        else
 
6265
                                                                n->lefthand = list_make1($1);
 
6266
                                                        n->operName = list_make1(makeString("="));
 
6267
                                                        $$ = (Node *)n;
 
6268
                                        }
 
6269
                                        else
 
6270
                                        {
 
6271
                                                Node *n = NULL;
 
6272
                                                ListCell *l;
 
6273
                                                foreach(l, (List *) $3)
 
6274
                                                {
 
6275
                                                        Node *cmp;
 
6276
                                                        cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, lfirst(l));
 
6277
                                                        if (n == NULL)
 
6278
                                                                n = cmp;
 
6279
                                                        else
 
6280
                                                                n = (Node *) makeA_Expr(AEXPR_OR, NIL, n, cmp);
 
6281
                                                }
 
6282
                                                $$ = n;
 
6283
                                        }
 
6284
                                }
 
6285
                        | a_expr NOT IN_P in_expr
 
6286
                                {
 
6287
                                        /* in_expr returns a SubLink or a list of a_exprs */
 
6288
                                        if (IsA($4, SubLink))
 
6289
                                        {
 
6290
                                                /* Make an IN node */
 
6291
                                                SubLink *n = (SubLink *)$4;
 
6292
                                                n->subLinkType = ANY_SUBLINK;
 
6293
                                                if (IsA($1, RowExpr))
 
6294
                                                        n->lefthand = ((RowExpr *) $1)->args;
 
6295
                                                else
 
6296
                                                        n->lefthand = list_make1($1);
 
6297
                                                n->operName = list_make1(makeString("="));
 
6298
                                                /* Stick a NOT on top */
 
6299
                                                $$ = (Node *) makeA_Expr(AEXPR_NOT, NIL, NULL, (Node *) n);
 
6300
                                        }
 
6301
                                        else
 
6302
                                        {
 
6303
                                                Node *n = NULL;
 
6304
                                                ListCell *l;
 
6305
                                                foreach(l, (List *) $4)
 
6306
                                                {
 
6307
                                                        Node *cmp;
 
6308
                                                        cmp = (Node *) makeSimpleA_Expr(AEXPR_OP, "<>", $1, lfirst(l));
 
6309
                                                        if (n == NULL)
 
6310
                                                                n = cmp;
 
6311
                                                        else
 
6312
                                                                n = (Node *) makeA_Expr(AEXPR_AND, NIL, n, cmp);
 
6313
                                                }
 
6314
                                                $$ = n;
 
6315
                                        }
 
6316
                                }
 
6317
                        | a_expr subquery_Op sub_type select_with_parens %prec Op
 
6318
                                {
 
6319
                                        SubLink *n = makeNode(SubLink);
 
6320
                                        n->subLinkType = $3;
 
6321
                                        if (IsA($1, RowExpr))
 
6322
                                                n->lefthand = ((RowExpr *) $1)->args;
 
6323
                                        else
 
6324
                                                n->lefthand = list_make1($1);
 
6325
                                        n->operName = $2;
 
6326
                                        n->subselect = $4;
 
6327
                                        $$ = (Node *)n;
 
6328
                                }
 
6329
                        | a_expr subquery_Op sub_type '(' a_expr ')' %prec Op
 
6330
                                {
 
6331
                                        if ($3 == ANY_SUBLINK)
 
6332
                                                $$ = (Node *) makeA_Expr(AEXPR_OP_ANY, $2, $1, $5);
 
6333
                                        else
 
6334
                                                $$ = (Node *) makeA_Expr(AEXPR_OP_ALL, $2, $1, $5);
 
6335
                                }
 
6336
                        | UNIQUE select_with_parens %prec Op
 
6337
                                {
 
6338
                                        /* Not sure how to get rid of the parentheses
 
6339
                                         * but there are lots of shift/reduce errors without them.
 
6340
                                         *
 
6341
                                         * Should be able to implement this by plopping the entire
 
6342
                                         * select into a node, then transforming the target expressions
 
6343
                                         * from whatever they are into count(*), and testing the
 
6344
                                         * entire result equal to one.
 
6345
                                         * But, will probably implement a separate node in the executor.
 
6346
                                         */
 
6347
                                        ereport(ERROR,
 
6348
                                                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 
6349
                                                         errmsg("UNIQUE predicate is not yet implemented")));
 
6350
                                }
 
6351
                ;
 
6352
 
 
6353
/*
 
6354
 * Restricted expressions
 
6355
 *
 
6356
 * b_expr is a subset of the complete expression syntax defined by a_expr.
 
6357
 *
 
6358
 * Presently, AND, NOT, IS, and IN are the a_expr keywords that would
 
6359
 * cause trouble in the places where b_expr is used.  For simplicity, we
 
6360
 * just eliminate all the boolean-keyword-operator productions from b_expr.
 
6361
 */
 
6362
b_expr:         c_expr
 
6363
                                { $$ = $1; }
 
6364
                        | b_expr TYPECAST Typename
 
6365
                                { $$ = makeTypeCast($1, $3); }
 
6366
                        | '+' b_expr                                    %prec UMINUS
 
6367
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", NULL, $2); }
 
6368
                        | '-' b_expr                                    %prec UMINUS
 
6369
                                { $$ = doNegate($2); }
 
6370
                        | '%' b_expr
 
6371
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", NULL, $2); }
 
6372
                        | '^' b_expr
 
6373
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", NULL, $2); }
 
6374
                        | b_expr '%'
 
6375
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, NULL); }
 
6376
                        | b_expr '^'
 
6377
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, NULL); }
 
6378
                        | b_expr '+' b_expr
 
6379
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "+", $1, $3); }
 
6380
                        | b_expr '-' b_expr
 
6381
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "-", $1, $3); }
 
6382
                        | b_expr '*' b_expr
 
6383
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "*", $1, $3); }
 
6384
                        | b_expr '/' b_expr
 
6385
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "/", $1, $3); }
 
6386
                        | b_expr '%' b_expr
 
6387
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "%", $1, $3); }
 
6388
                        | b_expr '^' b_expr
 
6389
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "^", $1, $3); }
 
6390
                        | b_expr '<' b_expr
 
6391
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "<", $1, $3); }
 
6392
                        | b_expr '>' b_expr
 
6393
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, ">", $1, $3); }
 
6394
                        | b_expr '=' b_expr
 
6395
                                { $$ = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", $1, $3); }
 
6396
                        | b_expr qual_Op b_expr                         %prec Op
 
6397
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, $3); }
 
6398
                        | qual_Op b_expr                                        %prec Op
 
6399
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $1, NULL, $2); }
 
6400
                        | b_expr qual_Op                                        %prec POSTFIXOP
 
6401
                                { $$ = (Node *) makeA_Expr(AEXPR_OP, $2, $1, NULL); }
 
6402
                        | b_expr IS DISTINCT FROM b_expr        %prec IS
 
6403
                                {
 
6404
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_DISTINCT, "=", $1, $5);
 
6405
                                }
 
6406
                        | b_expr IS OF '(' type_list ')'        %prec IS
 
6407
                                {
 
6408
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "=", $1, (Node *) $5);
 
6409
                                }
 
6410
                        | b_expr IS NOT OF '(' type_list ')'    %prec IS
 
6411
                                {
 
6412
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_OF, "!=", $1, (Node *) $6);
 
6413
                                }
 
6414
                ;
 
6415
 
 
6416
/*
 
6417
 * Productions that can be used in both a_expr and b_expr.
 
6418
 *
 
6419
 * Note: productions that refer recursively to a_expr or b_expr mostly
 
6420
 * cannot appear here.  However, it's OK to refer to a_exprs that occur
 
6421
 * inside parentheses, such as function arguments; that cannot introduce
 
6422
 * ambiguity to the b_expr syntax.
 
6423
 */
 
6424
c_expr:         columnref                                                               { $$ = $1; }
 
6425
                        | AexprConst                                                    { $$ = $1; }
 
6426
                        | PARAM opt_indirection
 
6427
                                {
 
6428
                                        ParamRef *p = makeNode(ParamRef);
 
6429
                                        p->number = $1;
 
6430
                                        if ($2)
 
6431
                                        {
 
6432
                                                A_Indirection *n = makeNode(A_Indirection);
 
6433
                                                n->arg = (Node *) p;
 
6434
                                                n->indirection = $2;
 
6435
                                                $$ = (Node *) n;
 
6436
                                        }
 
6437
                                        else
 
6438
                                                $$ = (Node *) p;
 
6439
                                }
 
6440
                        | '(' a_expr ')' opt_indirection
 
6441
                                {
 
6442
                                        if ($4)
 
6443
                                        {
 
6444
                                                A_Indirection *n = makeNode(A_Indirection);
 
6445
                                                n->arg = $2;
 
6446
                                                n->indirection = $4;
 
6447
                                                $$ = (Node *)n;
 
6448
                                        }
 
6449
                                        else
 
6450
                                                $$ = $2;
 
6451
                                }
 
6452
                        | case_expr
 
6453
                                { $$ = $1; }
 
6454
                        | func_expr
 
6455
                                { $$ = $1; }
 
6456
                        | select_with_parens                    %prec UMINUS
 
6457
                                {
 
6458
                                        SubLink *n = makeNode(SubLink);
 
6459
                                        n->subLinkType = EXPR_SUBLINK;
 
6460
                                        n->lefthand = NIL;
 
6461
                                        n->operName = NIL;
 
6462
                                        n->subselect = $1;
 
6463
                                        $$ = (Node *)n;
 
6464
                                }
 
6465
                        | EXISTS select_with_parens
 
6466
                                {
 
6467
                                        SubLink *n = makeNode(SubLink);
 
6468
                                        n->subLinkType = EXISTS_SUBLINK;
 
6469
                                        n->lefthand = NIL;
 
6470
                                        n->operName = NIL;
 
6471
                                        n->subselect = $2;
 
6472
                                        $$ = (Node *)n;
 
6473
                                }
 
6474
                        | ARRAY select_with_parens
 
6475
                                {
 
6476
                                        SubLink *n = makeNode(SubLink);
 
6477
                                        n->subLinkType = ARRAY_SUBLINK;
 
6478
                                        n->lefthand = NIL;
 
6479
                                        n->operName = NIL;
 
6480
                                        n->subselect = $2;
 
6481
                                        $$ = (Node *)n;
 
6482
                                }
 
6483
                        | ARRAY array_expr
 
6484
                                {       $$ = $2;        }
 
6485
                        | row
 
6486
                                {
 
6487
                                        RowExpr *r = makeNode(RowExpr);
 
6488
                                        r->args = $1;
 
6489
                                        r->row_typeid = InvalidOid;     /* not analyzed yet */
 
6490
                                        $$ = (Node *)r;
 
6491
                                }
 
6492
                ;
 
6493
 
 
6494
/*
 
6495
 * func_expr is split out from c_expr just so that we have a classification
 
6496
 * for "everything that is a function call or looks like one".  This isn't
 
6497
 * very important, but it saves us having to document which variants are
 
6498
 * legal in the backwards-compatible functional-index syntax for CREATE INDEX.
 
6499
 * (Note that many of the special SQL functions wouldn't actually make any
 
6500
 * sense as functional index entries, but we ignore that consideration here.)
 
6501
 */
 
6502
func_expr:      func_name '(' ')'
 
6503
                                {
 
6504
                                        FuncCall *n = makeNode(FuncCall);
 
6505
                                        n->funcname = $1;
 
6506
                                        n->args = NIL;
 
6507
                                        n->agg_star = FALSE;
 
6508
                                        n->agg_distinct = FALSE;
 
6509
                                        $$ = (Node *)n;
 
6510
                                }
 
6511
                        | func_name '(' expr_list ')'
 
6512
                                {
 
6513
                                        FuncCall *n = makeNode(FuncCall);
 
6514
                                        n->funcname = $1;
 
6515
                                        n->args = $3;
 
6516
                                        n->agg_star = FALSE;
 
6517
                                        n->agg_distinct = FALSE;
 
6518
                                        $$ = (Node *)n;
 
6519
                                }
 
6520
                        | func_name '(' ALL expr_list ')'
 
6521
                                {
 
6522
                                        FuncCall *n = makeNode(FuncCall);
 
6523
                                        n->funcname = $1;
 
6524
                                        n->args = $4;
 
6525
                                        n->agg_star = FALSE;
 
6526
                                        n->agg_distinct = FALSE;
 
6527
                                        /* Ideally we'd mark the FuncCall node to indicate
 
6528
                                         * "must be an aggregate", but there's no provision
 
6529
                                         * for that in FuncCall at the moment.
 
6530
                                         */
 
6531
                                        $$ = (Node *)n;
 
6532
                                }
 
6533
                        | func_name '(' DISTINCT expr_list ')'
 
6534
                                {
 
6535
                                        FuncCall *n = makeNode(FuncCall);
 
6536
                                        n->funcname = $1;
 
6537
                                        n->args = $4;
 
6538
                                        n->agg_star = FALSE;
 
6539
                                        n->agg_distinct = TRUE;
 
6540
                                        $$ = (Node *)n;
 
6541
                                }
 
6542
                        | func_name '(' '*' ')'
 
6543
                                {
 
6544
                                        /*
 
6545
                                         * For now, we transform AGGREGATE(*) into AGGREGATE(1).
 
6546
                                         *
 
6547
                                         * This does the right thing for COUNT(*) (in fact,
 
6548
                                         * any certainly-non-null expression would do for COUNT),
 
6549
                                         * and there are no other aggregates in SQL92 that accept
 
6550
                                         * '*' as parameter.
 
6551
                                         *
 
6552
                                         * The FuncCall node is also marked agg_star = true,
 
6553
                                         * so that later processing can detect what the argument
 
6554
                                         * really was.
 
6555
                                         */
 
6556
                                        FuncCall *n = makeNode(FuncCall);
 
6557
                                        A_Const *star = makeNode(A_Const);
 
6558
 
 
6559
                                        star->val.type = T_Integer;
 
6560
                                        star->val.val.ival = 1;
 
6561
                                        n->funcname = $1;
 
6562
                                        n->args = list_make1(star);
 
6563
                                        n->agg_star = TRUE;
 
6564
                                        n->agg_distinct = FALSE;
 
6565
                                        $$ = (Node *)n;
 
6566
                                }
 
6567
                        | CURRENT_DATE
 
6568
                                {
 
6569
                                        /*
 
6570
                                         * Translate as "'now'::text::date".
 
6571
                                         *
 
6572
                                         * We cannot use "'now'::date" because coerce_type() will
 
6573
                                         * immediately reduce that to a constant representing
 
6574
                                         * today's date.  We need to delay the conversion until
 
6575
                                         * runtime, else the wrong things will happen when
 
6576
                                         * CURRENT_DATE is used in a column default value or rule.
 
6577
                                         *
 
6578
                                         * This could be simplified if we had a way to generate
 
6579
                                         * an expression tree representing runtime application
 
6580
                                         * of type-input conversion functions...
 
6581
                                         */
 
6582
                                        A_Const *s = makeNode(A_Const);
 
6583
                                        TypeName *d;
 
6584
 
 
6585
                                        s->val.type = T_String;
 
6586
                                        s->val.val.str = "now";
 
6587
                                        s->typename = SystemTypeName("text");
 
6588
 
 
6589
                                        d = SystemTypeName("date");
 
6590
 
 
6591
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6592
                                }
 
6593
                        | CURRENT_TIME
 
6594
                                {
 
6595
                                        /*
 
6596
                                         * Translate as "'now'::text::timetz".
 
6597
                                         * See comments for CURRENT_DATE.
 
6598
                                         */
 
6599
                                        A_Const *s = makeNode(A_Const);
 
6600
                                        TypeName *d;
 
6601
 
 
6602
                                        s->val.type = T_String;
 
6603
                                        s->val.val.str = "now";
 
6604
                                        s->typename = SystemTypeName("text");
 
6605
 
 
6606
                                        d = SystemTypeName("timetz");
 
6607
                                        /* SQL99 mandates a default precision of zero for TIME
 
6608
                                         * fields in schemas. However, for CURRENT_TIME
 
6609
                                         * let's preserve the microsecond precision we
 
6610
                                         * might see from the system clock. - thomas 2001-12-07
 
6611
                                         */
 
6612
                                        d->typmod = 6;
 
6613
 
 
6614
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6615
                                }
 
6616
                        | CURRENT_TIME '(' Iconst ')'
 
6617
                                {
 
6618
                                        /*
 
6619
                                         * Translate as "'now'::text::timetz(n)".
 
6620
                                         * See comments for CURRENT_DATE.
 
6621
                                         */
 
6622
                                        A_Const *s = makeNode(A_Const);
 
6623
                                        TypeName *d;
 
6624
 
 
6625
                                        s->val.type = T_String;
 
6626
                                        s->val.val.str = "now";
 
6627
                                        s->typename = SystemTypeName("text");
 
6628
                                        d = SystemTypeName("timetz");
 
6629
                                        if ($3 < 0)
 
6630
                                                ereport(ERROR,
 
6631
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6632
                                                                 errmsg("CURRENT_TIME(%d) precision must not be negative",
 
6633
                                                                                $3)));
 
6634
                                        if ($3 > MAX_TIME_PRECISION)
 
6635
                                        {
 
6636
                                                ereport(WARNING,
 
6637
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6638
                                                                 errmsg("CURRENT_TIME(%d) precision reduced to maximum allowed, %d",
 
6639
                                                                                $3, MAX_TIME_PRECISION)));
 
6640
                                                $3 = MAX_TIME_PRECISION;
 
6641
                                        }
 
6642
                                        d->typmod = $3;
 
6643
 
 
6644
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6645
                                }
 
6646
                        | CURRENT_TIMESTAMP
 
6647
                                {
 
6648
                                        /*
 
6649
                                         * Translate as "'now'::text::timestamptz".
 
6650
                                         * See comments for CURRENT_DATE.
 
6651
                                         */
 
6652
                                        A_Const *s = makeNode(A_Const);
 
6653
                                        TypeName *d;
 
6654
 
 
6655
                                        s->val.type = T_String;
 
6656
                                        s->val.val.str = "now";
 
6657
                                        s->typename = SystemTypeName("text");
 
6658
 
 
6659
                                        d = SystemTypeName("timestamptz");
 
6660
                                        /* SQL99 mandates a default precision of 6 for timestamp.
 
6661
                                         * Also, that is about as precise as we will get since
 
6662
                                         * we are using a microsecond time interface.
 
6663
                                         * - thomas 2001-12-07
 
6664
                                         */
 
6665
                                        d->typmod = 6;
 
6666
 
 
6667
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6668
                                }
 
6669
                        | CURRENT_TIMESTAMP '(' Iconst ')'
 
6670
                                {
 
6671
                                        /*
 
6672
                                         * Translate as "'now'::text::timestamptz(n)".
 
6673
                                         * See comments for CURRENT_DATE.
 
6674
                                         */
 
6675
                                        A_Const *s = makeNode(A_Const);
 
6676
                                        TypeName *d;
 
6677
 
 
6678
                                        s->val.type = T_String;
 
6679
                                        s->val.val.str = "now";
 
6680
                                        s->typename = SystemTypeName("text");
 
6681
 
 
6682
                                        d = SystemTypeName("timestamptz");
 
6683
                                        if ($3 < 0)
 
6684
                                                ereport(ERROR,
 
6685
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6686
                                                                 errmsg("CURRENT_TIMESTAMP(%d) precision must not be negative",
 
6687
                                                                                $3)));
 
6688
                                        if ($3 > MAX_TIMESTAMP_PRECISION)
 
6689
                                        {
 
6690
                                                ereport(WARNING,
 
6691
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6692
                                                                 errmsg("CURRENT_TIMESTAMP(%d) precision reduced to maximum allowed, %d",
 
6693
                                                                                $3, MAX_TIMESTAMP_PRECISION)));
 
6694
                                                $3 = MAX_TIMESTAMP_PRECISION;
 
6695
                                        }
 
6696
                                        d->typmod = $3;
 
6697
 
 
6698
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6699
                                }
 
6700
                        | LOCALTIME
 
6701
                                {
 
6702
                                        /*
 
6703
                                         * Translate as "'now'::text::time".
 
6704
                                         * See comments for CURRENT_DATE.
 
6705
                                         */
 
6706
                                        A_Const *s = makeNode(A_Const);
 
6707
                                        TypeName *d;
 
6708
 
 
6709
                                        s->val.type = T_String;
 
6710
                                        s->val.val.str = "now";
 
6711
                                        s->typename = SystemTypeName("text");
 
6712
 
 
6713
                                        d = SystemTypeName("time");
 
6714
                                        /* SQL99 mandates a default precision of zero for TIME
 
6715
                                         * fields in schemas. However, for LOCALTIME
 
6716
                                         * let's preserve the microsecond precision we
 
6717
                                         * might see from the system clock. - thomas 2001-12-07
 
6718
                                         */
 
6719
                                        d->typmod = 6;
 
6720
 
 
6721
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6722
                                }
 
6723
                        | LOCALTIME '(' Iconst ')'
 
6724
                                {
 
6725
                                        /*
 
6726
                                         * Translate as "'now'::text::time(n)".
 
6727
                                         * See comments for CURRENT_DATE.
 
6728
                                         */
 
6729
                                        A_Const *s = makeNode(A_Const);
 
6730
                                        TypeName *d;
 
6731
 
 
6732
                                        s->val.type = T_String;
 
6733
                                        s->val.val.str = "now";
 
6734
                                        s->typename = SystemTypeName("text");
 
6735
                                        d = SystemTypeName("time");
 
6736
                                        if ($3 < 0)
 
6737
                                                ereport(ERROR,
 
6738
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6739
                                                                 errmsg("LOCALTIME(%d) precision must not be negative",
 
6740
                                                                                $3)));
 
6741
                                        if ($3 > MAX_TIME_PRECISION)
 
6742
                                        {
 
6743
                                                ereport(WARNING,
 
6744
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6745
                                                                 errmsg("LOCALTIME(%d) precision reduced to maximum allowed, %d",
 
6746
                                                                                $3, MAX_TIME_PRECISION)));
 
6747
                                                $3 = MAX_TIME_PRECISION;
 
6748
                                        }
 
6749
                                        d->typmod = $3;
 
6750
 
 
6751
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6752
                                }
 
6753
                        | LOCALTIMESTAMP
 
6754
                                {
 
6755
                                        /*
 
6756
                                         * Translate as "'now'::text::timestamp".
 
6757
                                         * See comments for CURRENT_DATE.
 
6758
                                         */
 
6759
                                        A_Const *s = makeNode(A_Const);
 
6760
                                        TypeName *d;
 
6761
 
 
6762
                                        s->val.type = T_String;
 
6763
                                        s->val.val.str = "now";
 
6764
                                        s->typename = SystemTypeName("text");
 
6765
 
 
6766
                                        d = SystemTypeName("timestamp");
 
6767
                                        /* SQL99 mandates a default precision of 6 for timestamp.
 
6768
                                         * Also, that is about as precise as we will get since
 
6769
                                         * we are using a microsecond time interface.
 
6770
                                         * - thomas 2001-12-07
 
6771
                                         */
 
6772
                                        d->typmod = 6;
 
6773
 
 
6774
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6775
                                }
 
6776
                        | LOCALTIMESTAMP '(' Iconst ')'
 
6777
                                {
 
6778
                                        /*
 
6779
                                         * Translate as "'now'::text::timestamp(n)".
 
6780
                                         * See comments for CURRENT_DATE.
 
6781
                                         */
 
6782
                                        A_Const *s = makeNode(A_Const);
 
6783
                                        TypeName *d;
 
6784
 
 
6785
                                        s->val.type = T_String;
 
6786
                                        s->val.val.str = "now";
 
6787
                                        s->typename = SystemTypeName("text");
 
6788
 
 
6789
                                        d = SystemTypeName("timestamp");
 
6790
                                        if ($3 < 0)
 
6791
                                                ereport(ERROR,
 
6792
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6793
                                                                 errmsg("LOCALTIMESTAMP(%d) precision must not be negative",
 
6794
                                                                                $3)));
 
6795
                                        if ($3 > MAX_TIMESTAMP_PRECISION)
 
6796
                                        {
 
6797
                                                ereport(WARNING,
 
6798
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
6799
                                                                 errmsg("LOCALTIMESTAMP(%d) precision reduced to maximum allowed, %d",
 
6800
                                                                                $3, MAX_TIMESTAMP_PRECISION)));
 
6801
                                                $3 = MAX_TIMESTAMP_PRECISION;
 
6802
                                        }
 
6803
                                        d->typmod = $3;
 
6804
 
 
6805
                                        $$ = (Node *)makeTypeCast((Node *)s, d);
 
6806
                                }
 
6807
                        | CURRENT_USER
 
6808
                                {
 
6809
                                        FuncCall *n = makeNode(FuncCall);
 
6810
                                        n->funcname = SystemFuncName("current_user");
 
6811
                                        n->args = NIL;
 
6812
                                        n->agg_star = FALSE;
 
6813
                                        n->agg_distinct = FALSE;
 
6814
                                        $$ = (Node *)n;
 
6815
                                }
 
6816
                        | SESSION_USER
 
6817
                                {
 
6818
                                        FuncCall *n = makeNode(FuncCall);
 
6819
                                        n->funcname = SystemFuncName("session_user");
 
6820
                                        n->args = NIL;
 
6821
                                        n->agg_star = FALSE;
 
6822
                                        n->agg_distinct = FALSE;
 
6823
                                        $$ = (Node *)n;
 
6824
                                }
 
6825
                        | USER
 
6826
                                {
 
6827
                                        FuncCall *n = makeNode(FuncCall);
 
6828
                                        n->funcname = SystemFuncName("current_user");
 
6829
                                        n->args = NIL;
 
6830
                                        n->agg_star = FALSE;
 
6831
                                        n->agg_distinct = FALSE;
 
6832
                                        $$ = (Node *)n;
 
6833
                                }
 
6834
                        | CAST '(' a_expr AS Typename ')'
 
6835
                                { $$ = makeTypeCast($3, $5); }
 
6836
                        | EXTRACT '(' extract_list ')'
 
6837
                                {
 
6838
                                        FuncCall *n = makeNode(FuncCall);
 
6839
                                        n->funcname = SystemFuncName("date_part");
 
6840
                                        n->args = $3;
 
6841
                                        n->agg_star = FALSE;
 
6842
                                        n->agg_distinct = FALSE;
 
6843
                                        $$ = (Node *)n;
 
6844
                                }
 
6845
                        | OVERLAY '(' overlay_list ')'
 
6846
                                {
 
6847
                                        /* overlay(A PLACING B FROM C FOR D) is converted to
 
6848
                                         * substring(A, 1, C-1) || B || substring(A, C+1, C+D)
 
6849
                                         * overlay(A PLACING B FROM C) is converted to
 
6850
                                         * substring(A, 1, C-1) || B || substring(A, C+1, C+char_length(B))
 
6851
                                         */
 
6852
                                        FuncCall *n = makeNode(FuncCall);
 
6853
                                        n->funcname = SystemFuncName("overlay");
 
6854
                                        n->args = $3;
 
6855
                                        n->agg_star = FALSE;
 
6856
                                        n->agg_distinct = FALSE;
 
6857
                                        $$ = (Node *)n;
 
6858
                                }
 
6859
                        | POSITION '(' position_list ')'
 
6860
                                {
 
6861
                                        /* position(A in B) is converted to position(B, A) */
 
6862
                                        FuncCall *n = makeNode(FuncCall);
 
6863
                                        n->funcname = SystemFuncName("position");
 
6864
                                        n->args = $3;
 
6865
                                        n->agg_star = FALSE;
 
6866
                                        n->agg_distinct = FALSE;
 
6867
                                        $$ = (Node *)n;
 
6868
                                }
 
6869
                        | SUBSTRING '(' substr_list ')'
 
6870
                                {
 
6871
                                        /* substring(A from B for C) is converted to
 
6872
                                         * substring(A, B, C) - thomas 2000-11-28
 
6873
                                         */
 
6874
                                        FuncCall *n = makeNode(FuncCall);
 
6875
                                        n->funcname = SystemFuncName("substring");
 
6876
                                        n->args = $3;
 
6877
                                        n->agg_star = FALSE;
 
6878
                                        n->agg_distinct = FALSE;
 
6879
                                        $$ = (Node *)n;
 
6880
                                }
 
6881
                        | TREAT '(' a_expr AS Typename ')'
 
6882
                                {
 
6883
                                        /* TREAT(expr AS target) converts expr of a particular type to target,
 
6884
                                         * which is defined to be a subtype of the original expression.
 
6885
                                         * In SQL99, this is intended for use with structured UDTs,
 
6886
                                         * but let's make this a generally useful form allowing stronger
 
6887
                                         * coersions than are handled by implicit casting.
 
6888
                                         */
 
6889
                                        FuncCall *n = makeNode(FuncCall);
 
6890
                                        /* Convert SystemTypeName() to SystemFuncName() even though
 
6891
                                         * at the moment they result in the same thing.
 
6892
                                         */
 
6893
                                        n->funcname = SystemFuncName(((Value *)llast($5->names))->val.str);
 
6894
                                        n->args = list_make1($3);
 
6895
                                        $$ = (Node *)n;
 
6896
                                }
 
6897
                        | TRIM '(' BOTH trim_list ')'
 
6898
                                {
 
6899
                                        /* various trim expressions are defined in SQL92
 
6900
                                         * - thomas 1997-07-19
 
6901
                                         */
 
6902
                                        FuncCall *n = makeNode(FuncCall);
 
6903
                                        n->funcname = SystemFuncName("btrim");
 
6904
                                        n->args = $4;
 
6905
                                        n->agg_star = FALSE;
 
6906
                                        n->agg_distinct = FALSE;
 
6907
                                        $$ = (Node *)n;
 
6908
                                }
 
6909
                        | TRIM '(' LEADING trim_list ')'
 
6910
                                {
 
6911
                                        FuncCall *n = makeNode(FuncCall);
 
6912
                                        n->funcname = SystemFuncName("ltrim");
 
6913
                                        n->args = $4;
 
6914
                                        n->agg_star = FALSE;
 
6915
                                        n->agg_distinct = FALSE;
 
6916
                                        $$ = (Node *)n;
 
6917
                                }
 
6918
                        | TRIM '(' TRAILING trim_list ')'
 
6919
                                {
 
6920
                                        FuncCall *n = makeNode(FuncCall);
 
6921
                                        n->funcname = SystemFuncName("rtrim");
 
6922
                                        n->args = $4;
 
6923
                                        n->agg_star = FALSE;
 
6924
                                        n->agg_distinct = FALSE;
 
6925
                                        $$ = (Node *)n;
 
6926
                                }
 
6927
                        | TRIM '(' trim_list ')'
 
6928
                                {
 
6929
                                        FuncCall *n = makeNode(FuncCall);
 
6930
                                        n->funcname = SystemFuncName("btrim");
 
6931
                                        n->args = $3;
 
6932
                                        n->agg_star = FALSE;
 
6933
                                        n->agg_distinct = FALSE;
 
6934
                                        $$ = (Node *)n;
 
6935
                                }
 
6936
                        | CONVERT '(' a_expr USING any_name ')'
 
6937
                                {
 
6938
                                        FuncCall *n = makeNode(FuncCall);
 
6939
                                        A_Const *c = makeNode(A_Const);
 
6940
 
 
6941
                                        c->val.type = T_String;
 
6942
                                        c->val.val.str = NameListToQuotedString($5);
 
6943
 
 
6944
                                        n->funcname = SystemFuncName("convert_using");
 
6945
                                        n->args = list_make2($3, c);
 
6946
                                        n->agg_star = FALSE;
 
6947
                                        n->agg_distinct = FALSE;
 
6948
                                        $$ = (Node *)n;
 
6949
                                }
 
6950
                        | CONVERT '(' expr_list ')'
 
6951
                                {
 
6952
                                        FuncCall *n = makeNode(FuncCall);
 
6953
                                        n->funcname = SystemFuncName("convert");
 
6954
                                        n->args = $3;
 
6955
                                        n->agg_star = FALSE;
 
6956
                                        n->agg_distinct = FALSE;
 
6957
                                        $$ = (Node *)n;
 
6958
                                }
 
6959
                        | NULLIF '(' a_expr ',' a_expr ')'
 
6960
                                {
 
6961
                                        $$ = (Node *) makeSimpleA_Expr(AEXPR_NULLIF, "=", $3, $5);
 
6962
                                }
 
6963
                        | COALESCE '(' expr_list ')'
 
6964
                                {
 
6965
                                        CoalesceExpr *c = makeNode(CoalesceExpr);
 
6966
                                        c->args = $3;
 
6967
                                        $$ = (Node *)c;
 
6968
                                }
 
6969
                ;
 
6970
 
 
6971
/*
 
6972
 * Supporting nonterminals for expressions.
 
6973
 */
 
6974
 
 
6975
/* Explicit row production.
 
6976
 *
 
6977
 * SQL99 allows an optional ROW keyword, so we can now do single-element rows
 
6978
 * without conflicting with the parenthesized a_expr production.  Without the
 
6979
 * ROW keyword, there must be more than one a_expr inside the parens.
 
6980
 */
 
6981
row:            ROW '(' expr_list ')'                                   { $$ = $3; }
 
6982
                        | ROW '(' ')'                                                   { $$ = NIL; }
 
6983
                        | '(' expr_list ',' a_expr ')'                  { $$ = lappend($2, $4); }
 
6984
                ;
 
6985
 
 
6986
sub_type:       ANY                                                                             { $$ = ANY_SUBLINK; }
 
6987
                        | SOME                                                                  { $$ = ANY_SUBLINK; }
 
6988
                        | ALL                                                                   { $$ = ALL_SUBLINK; }
 
6989
                ;
 
6990
 
 
6991
all_Op:         Op                                                                              { $$ = $1; }
 
6992
                        | MathOp                                                                { $$ = $1; }
 
6993
                ;
 
6994
 
 
6995
MathOp:          '+'                                                                    { $$ = "+"; }
 
6996
                        | '-'                                                                   { $$ = "-"; }
 
6997
                        | '*'                                                                   { $$ = "*"; }
 
6998
                        | '/'                                                                   { $$ = "/"; }
 
6999
                        | '%'                                                                   { $$ = "%"; }
 
7000
                        | '^'                                                                   { $$ = "^"; }
 
7001
                        | '<'                                                                   { $$ = "<"; }
 
7002
                        | '>'                                                                   { $$ = ">"; }
 
7003
                        | '='                                                                   { $$ = "="; }
 
7004
                ;
 
7005
 
 
7006
qual_Op:        Op
 
7007
                                        { $$ = list_make1(makeString($1)); }
 
7008
                        | OPERATOR '(' any_operator ')'
 
7009
                                        { $$ = $3; }
 
7010
                ;
 
7011
 
 
7012
qual_all_Op:
 
7013
                        all_Op
 
7014
                                        { $$ = list_make1(makeString($1)); }
 
7015
                        | OPERATOR '(' any_operator ')'
 
7016
                                        { $$ = $3; }
 
7017
                ;
 
7018
 
 
7019
subquery_Op:
 
7020
                        all_Op
 
7021
                                        { $$ = list_make1(makeString($1)); }
 
7022
                        | OPERATOR '(' any_operator ')'
 
7023
                                        { $$ = $3; }
 
7024
                        | LIKE
 
7025
                                        { $$ = list_make1(makeString("~~")); }
 
7026
                        | NOT LIKE
 
7027
                                        { $$ = list_make1(makeString("!~~")); }
 
7028
                        | ILIKE
 
7029
                                        { $$ = list_make1(makeString("~~*")); }
 
7030
                        | NOT ILIKE
 
7031
                                        { $$ = list_make1(makeString("!~~*")); }
 
7032
/* cannot put SIMILAR TO here, because SIMILAR TO is a hack.
 
7033
 * the regular expression is preprocessed by a function (similar_escape),
 
7034
 * and the ~ operator for posix regular expressions is used.
 
7035
 *        x SIMILAR TO y     ->    x ~ similar_escape(y)
 
7036
 * this transformation is made on the fly by the parser upwards.
 
7037
 * however the SubLink structure which handles any/some/all stuff
 
7038
 * is not ready for such a thing.
 
7039
 */
 
7040
                        ;
 
7041
 
 
7042
expr_list:      a_expr
 
7043
                                {
 
7044
                                        $$ = list_make1($1);
 
7045
                                }
 
7046
                        | expr_list ',' a_expr
 
7047
                                {
 
7048
                                        $$ = lappend($1, $3);
 
7049
                                }
 
7050
                ;
 
7051
 
 
7052
extract_list:
 
7053
                        extract_arg FROM a_expr
 
7054
                                {
 
7055
                                        A_Const *n = makeNode(A_Const);
 
7056
                                        n->val.type = T_String;
 
7057
                                        n->val.val.str = $1;
 
7058
                                        $$ = list_make2((Node *) n, $3);
 
7059
                                }
 
7060
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
7061
                ;
 
7062
 
 
7063
type_list:  type_list ',' Typename
 
7064
                                {
 
7065
                                        $$ = lappend($1, $3);
 
7066
                                }
 
7067
                        | Typename
 
7068
                                {
 
7069
                                        $$ = list_make1($1);
 
7070
                                }
 
7071
                ;
 
7072
 
 
7073
array_expr_list: array_expr
 
7074
                                {       $$ = list_make1($1);            }
 
7075
                        | array_expr_list ',' array_expr
 
7076
                                {       $$ = lappend($1, $3);   }
 
7077
                ;
 
7078
 
 
7079
array_expr: '[' expr_list ']'
 
7080
                                {
 
7081
                                        ArrayExpr *n = makeNode(ArrayExpr);
 
7082
                                        n->elements = $2;
 
7083
                                        $$ = (Node *)n;
 
7084
                                }
 
7085
                        | '[' array_expr_list ']'
 
7086
                                {
 
7087
                                        ArrayExpr *n = makeNode(ArrayExpr);
 
7088
                                        n->elements = $2;
 
7089
                                        $$ = (Node *)n;
 
7090
                                }
 
7091
                ;
 
7092
 
 
7093
/* Allow delimited string SCONST in extract_arg as an SQL extension.
 
7094
 * - thomas 2001-04-12
 
7095
 */
 
7096
 
 
7097
extract_arg:
 
7098
                        IDENT                                                                   { $$ = $1; }
 
7099
                        | YEAR_P                                                                { $$ = "year"; }
 
7100
                        | MONTH_P                                                               { $$ = "month"; }
 
7101
                        | DAY_P                                                                 { $$ = "day"; }
 
7102
                        | HOUR_P                                                                { $$ = "hour"; }
 
7103
                        | MINUTE_P                                                              { $$ = "minute"; }
 
7104
                        | SECOND_P                                                              { $$ = "second"; }
 
7105
                        | SCONST                                                                { $$ = $1; }
 
7106
                ;
 
7107
 
 
7108
/* OVERLAY() arguments
 
7109
 * SQL99 defines the OVERLAY() function:
 
7110
 * o overlay(text placing text from int for int)
 
7111
 * o overlay(text placing text from int)
 
7112
 */
 
7113
overlay_list:
 
7114
                        a_expr overlay_placing substr_from substr_for
 
7115
                                {
 
7116
                                        $$ = list_make4($1, $2, $3, $4);
 
7117
                                }
 
7118
                        | a_expr overlay_placing substr_from
 
7119
                                {
 
7120
                                        $$ = list_make3($1, $2, $3);
 
7121
                                }
 
7122
                ;
 
7123
 
 
7124
overlay_placing:
 
7125
                        PLACING a_expr
 
7126
                                { $$ = $2; }
 
7127
                ;
 
7128
 
 
7129
/* position_list uses b_expr not a_expr to avoid conflict with general IN */
 
7130
 
 
7131
position_list:
 
7132
                        b_expr IN_P b_expr                                              { $$ = list_make2($3, $1); }
 
7133
                        | /*EMPTY*/                                                             { $$ = NIL; }
 
7134
                ;
 
7135
 
 
7136
/* SUBSTRING() arguments
 
7137
 * SQL9x defines a specific syntax for arguments to SUBSTRING():
 
7138
 * o substring(text from int for int)
 
7139
 * o substring(text from int) get entire string from starting point "int"
 
7140
 * o substring(text from pattern) get entire string matching pattern
 
7141
 * o substring(text for int) get first "int" characters of string
 
7142
 * We also want to implement generic substring functions which accept
 
7143
 * the usual generic list of arguments. So we will accept both styles
 
7144
 * here, and convert the SQL9x style to the generic list for further
 
7145
 * processing. - thomas 2000-11-28
 
7146
 */
 
7147
substr_list:
 
7148
                        a_expr substr_from substr_for
 
7149
                                {
 
7150
                                        $$ = list_make3($1, $2, $3);
 
7151
                                }
 
7152
                        | a_expr substr_for substr_from
 
7153
                                {
 
7154
                                        $$ = list_make3($1, $3, $2);
 
7155
                                }
 
7156
                        | a_expr substr_from
 
7157
                                {
 
7158
                                        $$ = list_make2($1, $2);
 
7159
                                }
 
7160
                        | a_expr substr_for
 
7161
                                {
 
7162
                                        A_Const *n = makeNode(A_Const);
 
7163
                                        n->val.type = T_Integer;
 
7164
                                        n->val.val.ival = 1;
 
7165
                                        $$ = list_make3($1, (Node *)n, $2);
 
7166
                                }
 
7167
                        | expr_list
 
7168
                                {
 
7169
                                        $$ = $1;
 
7170
                                }
 
7171
                        | /*EMPTY*/
 
7172
                                { $$ = NIL; }
 
7173
                ;
 
7174
 
 
7175
substr_from:
 
7176
                        FROM a_expr                                                             { $$ = $2; }
 
7177
                ;
 
7178
 
 
7179
substr_for: FOR a_expr                                                          { $$ = $2; }
 
7180
                ;
 
7181
 
 
7182
trim_list:      a_expr FROM expr_list                                   { $$ = lappend($3, $1); }
 
7183
                        | FROM expr_list                                                { $$ = $2; }
 
7184
                        | expr_list                                                             { $$ = $1; }
 
7185
                ;
 
7186
 
 
7187
in_expr:        select_with_parens
 
7188
                                {
 
7189
                                        SubLink *n = makeNode(SubLink);
 
7190
                                        n->subselect = $1;
 
7191
                                        /* other fields will be filled later */
 
7192
                                        $$ = (Node *)n;
 
7193
                                }
 
7194
                        | '(' expr_list ')'                                             { $$ = (Node *)$2; }
 
7195
                ;
 
7196
 
 
7197
/*
 
7198
 * Define SQL92-style case clause.
 
7199
 * - Full specification
 
7200
 *      CASE WHEN a = b THEN c ... ELSE d END
 
7201
 * - Implicit argument
 
7202
 *      CASE a WHEN b THEN c ... ELSE d END
 
7203
 */
 
7204
case_expr:      CASE case_arg when_clause_list case_default END_P
 
7205
                                {
 
7206
                                        CaseExpr *c = makeNode(CaseExpr);
 
7207
                                        c->casetype = InvalidOid; /* not analyzed yet */
 
7208
                                        c->arg = (Expr *) $2;
 
7209
                                        c->args = $3;
 
7210
                                        c->defresult = (Expr *) $4;
 
7211
                                        $$ = (Node *)c;
 
7212
                                }
 
7213
                ;
 
7214
 
 
7215
when_clause_list:
 
7216
                        /* There must be at least one */
 
7217
                        when_clause                                                             { $$ = list_make1($1); }
 
7218
                        | when_clause_list when_clause                  { $$ = lappend($1, $2); }
 
7219
                ;
 
7220
 
 
7221
when_clause:
 
7222
                        WHEN a_expr THEN a_expr
 
7223
                                {
 
7224
                                        CaseWhen *w = makeNode(CaseWhen);
 
7225
                                        w->expr = (Expr *) $2;
 
7226
                                        w->result = (Expr *) $4;
 
7227
                                        $$ = (Node *)w;
 
7228
                                }
 
7229
                ;
 
7230
 
 
7231
case_default:
 
7232
                        ELSE a_expr                                                             { $$ = $2; }
 
7233
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
7234
                ;
 
7235
 
 
7236
case_arg:       a_expr                                                                  { $$ = $1; }
 
7237
                        | /*EMPTY*/                                                             { $$ = NULL; }
 
7238
                ;
 
7239
 
 
7240
/*
 
7241
 * columnref starts with relation_name not ColId, so that OLD and NEW
 
7242
 * references can be accepted.  Note that when there are more than two
 
7243
 * dotted names, the first name is not actually a relation name...
 
7244
 */
 
7245
columnref:      relation_name
 
7246
                                {
 
7247
                                        $$ = makeColumnRef($1, NIL);
 
7248
                                }
 
7249
                        | relation_name indirection
 
7250
                                {
 
7251
                                        $$ = makeColumnRef($1, $2);
 
7252
                                }
 
7253
                ;
 
7254
 
 
7255
indirection_el:
 
7256
                        '.' attr_name
 
7257
                                {
 
7258
                                        $$ = (Node *) makeString($2);
 
7259
                                }
 
7260
                        | '.' '*'
 
7261
                                {
 
7262
                                        $$ = (Node *) makeString("*");
 
7263
                                }
 
7264
                        | '[' a_expr ']'
 
7265
                                {
 
7266
                                        A_Indices *ai = makeNode(A_Indices);
 
7267
                                        ai->lidx = NULL;
 
7268
                                        ai->uidx = $2;
 
7269
                                        $$ = (Node *) ai;
 
7270
                                }
 
7271
                        | '[' a_expr ':' a_expr ']'
 
7272
                                {
 
7273
                                        A_Indices *ai = makeNode(A_Indices);
 
7274
                                        ai->lidx = $2;
 
7275
                                        ai->uidx = $4;
 
7276
                                        $$ = (Node *) ai;
 
7277
                                }
 
7278
                ;
 
7279
 
 
7280
indirection:
 
7281
                        indirection_el                                                  { $$ = list_make1($1); }
 
7282
                        | indirection indirection_el                    { $$ = lappend($1, $2); }
 
7283
                ;
 
7284
 
 
7285
opt_indirection:
 
7286
                        /*EMPTY*/                                                               { $$ = NIL; }
 
7287
                        | opt_indirection indirection_el                { $$ = lappend($1, $2); }
 
7288
                ;
 
7289
 
 
7290
 
 
7291
/*****************************************************************************
 
7292
 *
 
7293
 *      target lists for SELECT, UPDATE, INSERT
 
7294
 *
 
7295
 *****************************************************************************/
 
7296
 
 
7297
target_list:
 
7298
                        target_el                                                               { $$ = list_make1($1); }
 
7299
                        | target_list ',' target_el                             { $$ = lappend($1, $3); }
 
7300
                ;
 
7301
 
 
7302
/* AS is not optional because shift/red conflict with unary ops */
 
7303
target_el:      a_expr AS ColLabel
 
7304
                                {
 
7305
                                        $$ = makeNode(ResTarget);
 
7306
                                        $$->name = $3;
 
7307
                                        $$->indirection = NIL;
 
7308
                                        $$->val = (Node *)$1;
 
7309
                                }
 
7310
                        | a_expr
 
7311
                                {
 
7312
                                        $$ = makeNode(ResTarget);
 
7313
                                        $$->name = NULL;
 
7314
                                        $$->indirection = NIL;
 
7315
                                        $$->val = (Node *)$1;
 
7316
                                }
 
7317
                        | '*'
 
7318
                                {
 
7319
                                        ColumnRef *n = makeNode(ColumnRef);
 
7320
                                        n->fields = list_make1(makeString("*"));
 
7321
 
 
7322
                                        $$ = makeNode(ResTarget);
 
7323
                                        $$->name = NULL;
 
7324
                                        $$->indirection = NIL;
 
7325
                                        $$->val = (Node *)n;
 
7326
                                }
 
7327
                ;
 
7328
 
 
7329
update_target_list:
 
7330
                        update_target_el                                                { $$ = list_make1($1); }
 
7331
                        | update_target_list ',' update_target_el { $$ = lappend($1,$3); }
 
7332
                ;
 
7333
 
 
7334
update_target_el:
 
7335
                        ColId opt_indirection '=' a_expr
 
7336
                                {
 
7337
                                        $$ = makeNode(ResTarget);
 
7338
                                        $$->name = $1;
 
7339
                                        $$->indirection = $2;
 
7340
                                        $$->val = (Node *) $4;
 
7341
                                }
 
7342
                        | ColId opt_indirection '=' DEFAULT
 
7343
                                {
 
7344
                                        $$ = makeNode(ResTarget);
 
7345
                                        $$->name = $1;
 
7346
                                        $$->indirection = $2;
 
7347
                                        $$->val = (Node *) makeNode(SetToDefault);
 
7348
                                }
 
7349
 
 
7350
                ;
 
7351
 
 
7352
insert_target_list:
 
7353
                        insert_target_el                                                { $$ = list_make1($1); }
 
7354
                        | insert_target_list ',' insert_target_el { $$ = lappend($1, $3); }
 
7355
                ;
 
7356
 
 
7357
insert_target_el:
 
7358
                        a_expr
 
7359
                                {
 
7360
                                        $$ = makeNode(ResTarget);
 
7361
                                        $$->name = NULL;
 
7362
                                        $$->indirection = NIL;
 
7363
                                        $$->val = (Node *)$1;
 
7364
                                }
 
7365
                        | DEFAULT
 
7366
                                {
 
7367
                                        $$ = makeNode(ResTarget);
 
7368
                                        $$->name = NULL;
 
7369
                                        $$->indirection = NIL;
 
7370
                                        $$->val = (Node *) makeNode(SetToDefault);
 
7371
                                }
 
7372
                ;
 
7373
 
 
7374
 
 
7375
/*****************************************************************************
 
7376
 *
 
7377
 *      Names and constants
 
7378
 *
 
7379
 *****************************************************************************/
 
7380
 
 
7381
relation_name:
 
7382
                        SpecialRuleRelation                                             { $$ = $1; }
 
7383
                        | ColId                                                                 { $$ = $1; }
 
7384
                ;
 
7385
 
 
7386
qualified_name_list:
 
7387
                        qualified_name                                                  { $$ = list_make1($1); }
 
7388
                        | qualified_name_list ',' qualified_name { $$ = lappend($1, $3); }
 
7389
                ;
 
7390
 
 
7391
/*
 
7392
 * The production for a qualified relation name has to exactly match the
 
7393
 * production for a qualified func_name, because in a FROM clause we cannot
 
7394
 * tell which we are parsing until we see what comes after it ('(' for a
 
7395
 * func_name, something else for a relation). Therefore we allow 'indirection'
 
7396
 * which may contain subscripts, and reject that case in the C code.
 
7397
 */
 
7398
qualified_name:
 
7399
                        relation_name
 
7400
                                {
 
7401
                                        $$ = makeNode(RangeVar);
 
7402
                                        $$->catalogname = NULL;
 
7403
                                        $$->schemaname = NULL;
 
7404
                                        $$->relname = $1;
 
7405
                                }
 
7406
                        | relation_name indirection
 
7407
                                {
 
7408
                                        check_qualified_name($2);
 
7409
                                        $$ = makeNode(RangeVar);
 
7410
                                        switch (list_length($2))
 
7411
                                        {
 
7412
                                                case 1:
 
7413
                                                        $$->catalogname = NULL;
 
7414
                                                        $$->schemaname = $1;
 
7415
                                                        $$->relname = strVal(linitial($2));
 
7416
                                                        break;
 
7417
                                                case 2:
 
7418
                                                        $$->catalogname = $1;
 
7419
                                                        $$->schemaname = strVal(linitial($2));
 
7420
                                                        $$->relname = strVal(lsecond($2));
 
7421
                                                        break;
 
7422
                                                default:
 
7423
                                                        ereport(ERROR,
 
7424
                                                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
7425
                                                                         errmsg("improper qualified name (too many dotted names): %s",
 
7426
                                                                                        NameListToString(lcons(makeString($1), $2)))));
 
7427
                                                        break;
 
7428
                                        }
 
7429
                                }
 
7430
                ;
 
7431
 
 
7432
name_list:      name
 
7433
                                        { $$ = list_make1(makeString($1)); }
 
7434
                        | name_list ',' name
 
7435
                                        { $$ = lappend($1, makeString($3)); }
 
7436
                ;
 
7437
 
 
7438
 
 
7439
name:           ColId                                                                   { $$ = $1; };
 
7440
 
 
7441
database_name:
 
7442
                        ColId                                                                   { $$ = $1; };
 
7443
 
 
7444
access_method:
 
7445
                        ColId                                                                   { $$ = $1; };
 
7446
 
 
7447
attr_name:      ColLabel                                                                { $$ = $1; };
 
7448
 
 
7449
index_name: ColId                                                                       { $$ = $1; };
 
7450
 
 
7451
file_name:      Sconst                                                                  { $$ = $1; };
 
7452
 
 
7453
/*
 
7454
 * The production for a qualified func_name has to exactly match the
 
7455
 * production for a qualified columnref, because we cannot tell which we
 
7456
 * are parsing until we see what comes after it ('(' for a func_name,
 
7457
 * anything else for a columnref).  Therefore we allow 'indirection' which
 
7458
 * may contain subscripts, and reject that case in the C code.  (If we
 
7459
 * ever implement SQL99-like methods, such syntax may actually become legal!)
 
7460
 */
 
7461
func_name:      function_name
 
7462
                                        { $$ = list_make1(makeString($1)); }
 
7463
                        | relation_name indirection
 
7464
                                        { $$ = check_func_name(lcons(makeString($1), $2)); }
 
7465
                ;
 
7466
 
 
7467
 
 
7468
/*
 
7469
 * Constants
 
7470
 */
 
7471
AexprConst: Iconst
 
7472
                                {
 
7473
                                        A_Const *n = makeNode(A_Const);
 
7474
                                        n->val.type = T_Integer;
 
7475
                                        n->val.val.ival = $1;
 
7476
                                        $$ = (Node *)n;
 
7477
                                }
 
7478
                        | FCONST
 
7479
                                {
 
7480
                                        A_Const *n = makeNode(A_Const);
 
7481
                                        n->val.type = T_Float;
 
7482
                                        n->val.val.str = $1;
 
7483
                                        $$ = (Node *)n;
 
7484
                                }
 
7485
                        | Sconst
 
7486
                                {
 
7487
                                        A_Const *n = makeNode(A_Const);
 
7488
                                        n->val.type = T_String;
 
7489
                                        n->val.val.str = $1;
 
7490
                                        $$ = (Node *)n;
 
7491
                                }
 
7492
                        | BCONST
 
7493
                                {
 
7494
                                        A_Const *n = makeNode(A_Const);
 
7495
                                        n->val.type = T_BitString;
 
7496
                                        n->val.val.str = $1;
 
7497
                                        $$ = (Node *)n;
 
7498
                                }
 
7499
                        | XCONST
 
7500
                                {
 
7501
                                        /* This is a bit constant per SQL99:
 
7502
                                         * Without Feature F511, "BIT data type",
 
7503
                                         * a <general literal> shall not be a
 
7504
                                         * <bit string literal> or a <hex string literal>.
 
7505
                                         */
 
7506
                                        A_Const *n = makeNode(A_Const);
 
7507
                                        n->val.type = T_BitString;
 
7508
                                        n->val.val.str = $1;
 
7509
                                        $$ = (Node *)n;
 
7510
                                }
 
7511
                        | ConstTypename Sconst
 
7512
                                {
 
7513
                                        A_Const *n = makeNode(A_Const);
 
7514
                                        n->typename = $1;
 
7515
                                        n->val.type = T_String;
 
7516
                                        n->val.val.str = $2;
 
7517
                                        $$ = (Node *)n;
 
7518
                                }
 
7519
                        | ConstInterval Sconst opt_interval
 
7520
                                {
 
7521
                                        A_Const *n = makeNode(A_Const);
 
7522
                                        n->typename = $1;
 
7523
                                        n->val.type = T_String;
 
7524
                                        n->val.val.str = $2;
 
7525
                                        /* precision is not specified, but fields may be... */
 
7526
                                        if ($3 != INTERVAL_FULL_RANGE)
 
7527
                                                n->typename->typmod = INTERVAL_TYPMOD(INTERVAL_FULL_PRECISION, $3);
 
7528
                                        $$ = (Node *)n;
 
7529
                                }
 
7530
                        | ConstInterval '(' Iconst ')' Sconst opt_interval
 
7531
                                {
 
7532
                                        A_Const *n = makeNode(A_Const);
 
7533
                                        n->typename = $1;
 
7534
                                        n->val.type = T_String;
 
7535
                                        n->val.val.str = $5;
 
7536
                                        /* precision specified, and fields may be... */
 
7537
                                        if ($3 < 0)
 
7538
                                                ereport(ERROR,
 
7539
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
7540
                                                                 errmsg("INTERVAL(%d) precision must not be negative",
 
7541
                                                                                $3)));
 
7542
                                        if ($3 > MAX_INTERVAL_PRECISION)
 
7543
                                        {
 
7544
                                                ereport(WARNING,
 
7545
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
 
7546
                                                                 errmsg("INTERVAL(%d) precision reduced to maximum allowed, %d",
 
7547
                                                                                $3, MAX_INTERVAL_PRECISION)));
 
7548
                                                $3 = MAX_INTERVAL_PRECISION;
 
7549
                                        }
 
7550
                                        n->typename->typmod = INTERVAL_TYPMOD($3, $6);
 
7551
                                        $$ = (Node *)n;
 
7552
                                }
 
7553
                        | TRUE_P
 
7554
                                {
 
7555
                                        $$ = (Node *)makeBoolAConst(TRUE);
 
7556
                                }
 
7557
                        | FALSE_P
 
7558
                                {
 
7559
                                        $$ = (Node *)makeBoolAConst(FALSE);
 
7560
                                }
 
7561
                        | NULL_P
 
7562
                                {
 
7563
                                        A_Const *n = makeNode(A_Const);
 
7564
                                        n->val.type = T_Null;
 
7565
                                        $$ = (Node *)n;
 
7566
                                }
 
7567
                ;
 
7568
 
 
7569
Iconst:         ICONST                                                                  { $$ = $1; };
 
7570
Sconst:         SCONST                                                                  { $$ = $1; };
 
7571
UserId:         ColId                                                                   { $$ = $1; };
 
7572
 
 
7573
/*
 
7574
 * Name classification hierarchy.
 
7575
 *
 
7576
 * IDENT is the lexeme returned by the lexer for identifiers that match
 
7577
 * no known keyword.  In most cases, we can accept certain keywords as
 
7578
 * names, not only IDENTs.      We prefer to accept as many such keywords
 
7579
 * as possible to minimize the impact of "reserved words" on programmers.
 
7580
 * So, we divide names into several possible classes.  The classification
 
7581
 * is chosen in part to make keywords acceptable as names wherever possible.
 
7582
 */
 
7583
 
 
7584
/* Column identifier --- names that can be column, table, etc names.
 
7585
 */
 
7586
ColId:          IDENT                                                                   { $$ = $1; }
 
7587
                        | unreserved_keyword                                    { $$ = pstrdup($1); }
 
7588
                        | col_name_keyword                                              { $$ = pstrdup($1); }
 
7589
                ;
 
7590
 
 
7591
/* Type identifier --- names that can be type names.
 
7592
 */
 
7593
type_name:      IDENT                                                                   { $$ = $1; }
 
7594
                        | unreserved_keyword                                    { $$ = pstrdup($1); }
 
7595
                ;
 
7596
 
 
7597
/* Function identifier --- names that can be function names.
 
7598
 */
 
7599
function_name:
 
7600
                        IDENT                                                                   { $$ = $1; }
 
7601
                        | unreserved_keyword                                    { $$ = pstrdup($1); }
 
7602
                        | func_name_keyword                                             { $$ = pstrdup($1); }
 
7603
                ;
 
7604
 
 
7605
/* Column label --- allowed labels in "AS" clauses.
 
7606
 * This presently includes *all* Postgres keywords.
 
7607
 */
 
7608
ColLabel:       IDENT                                                                   { $$ = $1; }
 
7609
                        | unreserved_keyword                                    { $$ = pstrdup($1); }
 
7610
                        | col_name_keyword                                              { $$ = pstrdup($1); }
 
7611
                        | func_name_keyword                                             { $$ = pstrdup($1); }
 
7612
                        | reserved_keyword                                              { $$ = pstrdup($1); }
 
7613
                ;
 
7614
 
 
7615
 
 
7616
/*
 
7617
 * Keyword classification lists.  Generally, every keyword present in
 
7618
 * the Postgres grammar should appear in exactly one of these lists.
 
7619
 *
 
7620
 * Put a new keyword into the first list that it can go into without causing
 
7621
 * shift or reduce conflicts.  The earlier lists define "less reserved"
 
7622
 * categories of keywords.
 
7623
 */
 
7624
 
 
7625
/* "Unreserved" keywords --- available for use as any kind of name.
 
7626
 */
 
7627
unreserved_keyword:
 
7628
                          ABORT_P
 
7629
                        | ABSOLUTE_P
 
7630
                        | ACCESS
 
7631
                        | ACTION
 
7632
                        | ADD
 
7633
                        | AFTER
 
7634
                        | AGGREGATE
 
7635
                        | ALSO
 
7636
                        | ALTER
 
7637
                        | ASSERTION
 
7638
                        | ASSIGNMENT
 
7639
                        | AT
 
7640
                        | BACKWARD
 
7641
                        | BEFORE
 
7642
                        | BEGIN_P
 
7643
                        | BY
 
7644
                        | CACHE
 
7645
                        | CALLED
 
7646
                        | CASCADE
 
7647
                        | CHAIN
 
7648
                        | CHARACTERISTICS
 
7649
                        | CHECKPOINT
 
7650
                        | CLASS
 
7651
                        | CLOSE
 
7652
                        | CLUSTER
 
7653
                        | COMMENT
 
7654
                        | COMMIT
 
7655
                        | COMMITTED
 
7656
                        | CONSTRAINTS
 
7657
                        | CONVERSION_P
 
7658
                        | COPY
 
7659
                        | CREATEDB
 
7660
                        | CREATEUSER
 
7661
                        | CSV
 
7662
                        | CURSOR
 
7663
                        | CYCLE
 
7664
                        | DATABASE
 
7665
                        | DAY_P
 
7666
                        | DEALLOCATE
 
7667
                        | DECLARE
 
7668
                        | DEFAULTS
 
7669
                        | DEFERRED
 
7670
                        | DEFINER
 
7671
                        | DELETE_P
 
7672
                        | DELIMITER
 
7673
                        | DELIMITERS
 
7674
                        | DOMAIN_P
 
7675
                        | DOUBLE_P
 
7676
                        | DROP
 
7677
                        | EACH
 
7678
                        | ENCODING
 
7679
                        | ENCRYPTED
 
7680
                        | ESCAPE
 
7681
                        | EXCLUDING
 
7682
                        | EXCLUSIVE
 
7683
                        | EXECUTE
 
7684
                        | EXPLAIN
 
7685
                        | EXTERNAL
 
7686
                        | FETCH
 
7687
                        | FIRST_P
 
7688
                        | FORCE
 
7689
                        | FORWARD
 
7690
                        | FUNCTION
 
7691
                        | GLOBAL
 
7692
                        | HANDLER
 
7693
                        | HOLD
 
7694
                        | HOUR_P
 
7695
                        | IMMEDIATE
 
7696
                        | IMMUTABLE
 
7697
                        | IMPLICIT_P
 
7698
                        | INCLUDING
 
7699
                        | INCREMENT
 
7700
                        | INDEX
 
7701
                        | INHERITS
 
7702
                        | INPUT_P
 
7703
                        | INSENSITIVE
 
7704
                        | INSERT
 
7705
                        | INSTEAD
 
7706
                        | INVOKER
 
7707
                        | ISOLATION
 
7708
                        | KEY
 
7709
                        | LANCOMPILER
 
7710
                        | LANGUAGE
 
7711
                        | LARGE_P
 
7712
                        | LAST_P
 
7713
                        | LEVEL
 
7714
                        | LISTEN
 
7715
                        | LOAD
 
7716
                        | LOCAL
 
7717
                        | LOCATION
 
7718
                        | LOCK_P
 
7719
                        | MATCH
 
7720
                        | MAXVALUE
 
7721
                        | MINUTE_P
 
7722
                        | MINVALUE
 
7723
                        | MODE
 
7724
                        | MONTH_P
 
7725
                        | MOVE
 
7726
                        | NAMES
 
7727
                        | NEXT
 
7728
                        | NO
 
7729
                        | NOCREATEDB
 
7730
                        | NOCREATEUSER
 
7731
                        | NOTHING
 
7732
                        | NOTIFY
 
7733
                        | NOWAIT
 
7734
                        | OBJECT_P
 
7735
                        | OF
 
7736
                        | OIDS
 
7737
                        | OPERATOR
 
7738
                        | OPTION
 
7739
                        | OWNER
 
7740
                        | PARTIAL
 
7741
                        | PASSWORD
 
7742
                        | PREPARE
 
7743
                        | PRESERVE
 
7744
                        | PRIOR
 
7745
                        | PRIVILEGES
 
7746
                        | PROCEDURAL
 
7747
                        | PROCEDURE
 
7748
                        | QUOTE
 
7749
                        | READ
 
7750
                        | RECHECK
 
7751
                        | REINDEX
 
7752
                        | RELATIVE_P
 
7753
                        | RELEASE
 
7754
                        | RENAME
 
7755
                        | REPEATABLE
 
7756
                        | REPLACE
 
7757
                        | RESET
 
7758
                        | RESTART
 
7759
                        | RESTRICT
 
7760
                        | RETURNS
 
7761
                        | REVOKE
 
7762
                        | ROLLBACK
 
7763
                        | ROWS
 
7764
                        | RULE
 
7765
                        | SAVEPOINT
 
7766
                        | SCHEMA
 
7767
                        | SCROLL
 
7768
                        | SECOND_P
 
7769
                        | SECURITY
 
7770
                        | SEQUENCE
 
7771
                        | SERIALIZABLE
 
7772
                        | SESSION
 
7773
                        | SET
 
7774
                        | SHARE
 
7775
                        | SHOW
 
7776
                        | SIMPLE
 
7777
                        | STABLE
 
7778
                        | START
 
7779
                        | STATEMENT
 
7780
                        | STATISTICS
 
7781
                        | STDIN
 
7782
                        | STDOUT
 
7783
                        | STORAGE
 
7784
                        | SYSID
 
7785
                        | STRICT_P
 
7786
                        | TABLESPACE
 
7787
                        | TEMP
 
7788
                        | TEMPLATE
 
7789
                        | TEMPORARY
 
7790
                        | TOAST
 
7791
                        | TRANSACTION
 
7792
                        | TRIGGER
 
7793
                        | TRUNCATE
 
7794
                        | TRUSTED
 
7795
                        | TYPE_P
 
7796
                        | UNCOMMITTED
 
7797
                        | UNENCRYPTED
 
7798
                        | UNKNOWN
 
7799
                        | UNLISTEN
 
7800
                        | UNTIL
 
7801
                        | UPDATE
 
7802
                        | USAGE
 
7803
                        | VACUUM
 
7804
                        | VALID
 
7805
                        | VALIDATOR
 
7806
                        | VALUES
 
7807
                        | VARYING
 
7808
                        | VIEW
 
7809
                        | VOLATILE
 
7810
                        | WITH
 
7811
                        | WITHOUT
 
7812
                        | WORK
 
7813
                        | WRITE
 
7814
                        | YEAR_P
 
7815
                        | ZONE
 
7816
                ;
 
7817
 
 
7818
/* Column identifier --- keywords that can be column, table, etc names.
 
7819
 *
 
7820
 * Many of these keywords will in fact be recognized as type or function
 
7821
 * names too; but they have special productions for the purpose, and so
 
7822
 * can't be treated as "generic" type or function names.
 
7823
 *
 
7824
 * The type names appearing here are not usable as function names
 
7825
 * because they can be followed by '(' in typename productions, which
 
7826
 * looks too much like a function call for an LR(1) parser.
 
7827
 */
 
7828
col_name_keyword:
 
7829
                          BIGINT
 
7830
                        | BIT
 
7831
                        | BOOLEAN_P
 
7832
                        | CHAR_P
 
7833
                        | CHARACTER
 
7834
                        | COALESCE
 
7835
                        | CONVERT
 
7836
                        | DEC
 
7837
                        | DECIMAL_P
 
7838
                        | EXISTS
 
7839
                        | EXTRACT
 
7840
                        | FLOAT_P
 
7841
                        | INOUT
 
7842
                        | INT_P
 
7843
                        | INTEGER
 
7844
                        | INTERVAL
 
7845
                        | NATIONAL
 
7846
                        | NCHAR
 
7847
                        | NONE
 
7848
                        | NULLIF
 
7849
                        | NUMERIC
 
7850
                        | OUT_P
 
7851
                        | OVERLAY
 
7852
                        | POSITION
 
7853
                        | PRECISION
 
7854
                        | REAL
 
7855
                        | ROW
 
7856
                        | SETOF
 
7857
                        | SMALLINT
 
7858
                        | SUBSTRING
 
7859
                        | TIME
 
7860
                        | TIMESTAMP
 
7861
                        | TREAT
 
7862
                        | TRIM
 
7863
                        | VARCHAR
 
7864
                ;
 
7865
 
 
7866
/* Function identifier --- keywords that can be function names.
 
7867
 *
 
7868
 * Most of these are keywords that are used as operators in expressions;
 
7869
 * in general such keywords can't be column names because they would be
 
7870
 * ambiguous with variables, but they are unambiguous as function identifiers.
 
7871
 *
 
7872
 * Do not include POSITION, SUBSTRING, etc here since they have explicit
 
7873
 * productions in a_expr to support the goofy SQL9x argument syntax.
 
7874
 * - thomas 2000-11-28
 
7875
 */
 
7876
func_name_keyword:
 
7877
                          AUTHORIZATION
 
7878
                        | BETWEEN
 
7879
                        | BINARY
 
7880
                        | CROSS
 
7881
                        | FREEZE
 
7882
                        | FULL
 
7883
                        | ILIKE
 
7884
                        | INNER_P
 
7885
                        | IS
 
7886
                        | ISNULL
 
7887
                        | JOIN
 
7888
                        | LEFT
 
7889
                        | LIKE
 
7890
                        | NATURAL
 
7891
                        | NOTNULL
 
7892
                        | OUTER_P
 
7893
                        | OVERLAPS
 
7894
                        | RIGHT
 
7895
                        | SIMILAR
 
7896
                        | VERBOSE
 
7897
                ;
 
7898
 
 
7899
/* Reserved keyword --- these keywords are usable only as a ColLabel.
 
7900
 *
 
7901
 * Keywords appear here if they could not be distinguished from variable,
 
7902
 * type, or function names in some contexts.  Don't put things here unless
 
7903
 * forced to.
 
7904
 */
 
7905
reserved_keyword:
 
7906
                          ALL
 
7907
                        | ANALYSE
 
7908
                        | ANALYZE
 
7909
                        | AND
 
7910
                        | ANY
 
7911
                        | ARRAY
 
7912
                        | AS
 
7913
                        | ASC
 
7914
                        | BOTH
 
7915
                        | CASE
 
7916
                        | CAST
 
7917
                        | CHECK
 
7918
                        | COLLATE
 
7919
                        | COLUMN
 
7920
                        | CONSTRAINT
 
7921
                        | CREATE
 
7922
                        | CURRENT_DATE
 
7923
                        | CURRENT_TIME
 
7924
                        | CURRENT_TIMESTAMP
 
7925
                        | CURRENT_USER
 
7926
                        | DEFAULT
 
7927
                        | DEFERRABLE
 
7928
                        | DESC
 
7929
                        | DISTINCT
 
7930
                        | DO
 
7931
                        | ELSE
 
7932
                        | END_P
 
7933
                        | EXCEPT
 
7934
                        | FALSE_P
 
7935
                        | FOR
 
7936
                        | FOREIGN
 
7937
                        | FROM
 
7938
                        | GRANT
 
7939
                        | GROUP_P
 
7940
                        | HAVING
 
7941
                        | IN_P
 
7942
                        | INITIALLY
 
7943
                        | INTERSECT
 
7944
                        | INTO
 
7945
                        | LEADING
 
7946
                        | LIMIT
 
7947
                        | LOCALTIME
 
7948
                        | LOCALTIMESTAMP
 
7949
                        | NEW
 
7950
                        | NOT
 
7951
                        | NULL_P
 
7952
                        | OFF
 
7953
                        | OFFSET
 
7954
                        | OLD
 
7955
                        | ON
 
7956
                        | ONLY
 
7957
                        | OR
 
7958
                        | ORDER
 
7959
                        | PLACING
 
7960
                        | PRIMARY
 
7961
                        | REFERENCES
 
7962
                        | SELECT
 
7963
                        | SESSION_USER
 
7964
                        | SOME
 
7965
                        | TABLE
 
7966
                        | THEN
 
7967
                        | TO
 
7968
                        | TRAILING
 
7969
                        | TRUE_P
 
7970
                        | UNION
 
7971
                        | UNIQUE
 
7972
                        | USER
 
7973
                        | USING
 
7974
                        | WHEN
 
7975
                        | WHERE
 
7976
                ;
 
7977
 
 
7978
 
 
7979
SpecialRuleRelation:
 
7980
                        OLD
 
7981
                                {
 
7982
                                        if (QueryIsRule)
 
7983
                                                $$ = "*OLD*";
 
7984
                                        else
 
7985
                                                ereport(ERROR,
 
7986
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
7987
                                                                 errmsg("OLD used in query that is not in a rule")));
 
7988
                                }
 
7989
                        | NEW
 
7990
                                {
 
7991
                                        if (QueryIsRule)
 
7992
                                                $$ = "*NEW*";
 
7993
                                        else
 
7994
                                                ereport(ERROR,
 
7995
                                                                (errcode(ERRCODE_SYNTAX_ERROR),
 
7996
                                                                 errmsg("NEW used in query that is not in a rule")));
 
7997
                                }
 
7998
                ;
 
7999
 
 
8000
%%
 
8001
 
 
8002
static Node *
 
8003
makeColumnRef(char *relname, List *indirection)
 
8004
{
 
8005
        /*
 
8006
         * Generate a ColumnRef node, with an A_Indirection node added if there
 
8007
         * is any subscripting in the specified indirection list.  However,
 
8008
         * any field selection at the start of the indirection list must be
 
8009
         * transposed into the "fields" part of the ColumnRef node.
 
8010
         */
 
8011
        ColumnRef  *c = makeNode(ColumnRef);
 
8012
        int             nfields = 0;
 
8013
        ListCell *l;
 
8014
 
 
8015
        foreach(l, indirection)
 
8016
        {
 
8017
                if (IsA(lfirst(l), A_Indices))
 
8018
                {
 
8019
                        A_Indirection *i = makeNode(A_Indirection);
 
8020
 
 
8021
                        if (nfields == 0)
 
8022
                        {
 
8023
                                /* easy case - all indirection goes to A_Indirection */
 
8024
                                c->fields = list_make1(makeString(relname));
 
8025
                                i->indirection = indirection;
 
8026
                        }
 
8027
                        else
 
8028
                        {
 
8029
                                /* got to split the list in two */
 
8030
                                i->indirection = list_copy_tail(indirection, nfields);
 
8031
                                indirection = list_truncate(indirection, nfields);
 
8032
                                c->fields = lcons(makeString(relname), indirection);
 
8033
                        }
 
8034
                        i->arg = (Node *) c;
 
8035
                        return (Node *) i;
 
8036
                }
 
8037
                nfields++;
 
8038
        }
 
8039
        /* No subscripting, so all indirection gets added to field list */
 
8040
        c->fields = lcons(makeString(relname), indirection);
 
8041
        return (Node *) c;
 
8042
}
 
8043
 
 
8044
static Node *
 
8045
makeTypeCast(Node *arg, TypeName *typename)
 
8046
{
 
8047
        /*
 
8048
         * Simply generate a TypeCast node.
 
8049
         *
 
8050
         * Earlier we would determine whether an A_Const would
 
8051
         * be acceptable, however Domains require coerce_type()
 
8052
         * to process them -- applying constraints as required.
 
8053
         */
 
8054
        TypeCast *n = makeNode(TypeCast);
 
8055
        n->arg = arg;
 
8056
        n->typename = typename;
 
8057
        return (Node *) n;
 
8058
}
 
8059
 
 
8060
static Node *
 
8061
makeStringConst(char *str, TypeName *typename)
 
8062
{
 
8063
        A_Const *n = makeNode(A_Const);
 
8064
 
 
8065
        n->val.type = T_String;
 
8066
        n->val.val.str = str;
 
8067
        n->typename = typename;
 
8068
 
 
8069
        return (Node *)n;
 
8070
}
 
8071
 
 
8072
static Node *
 
8073
makeIntConst(int val)
 
8074
{
 
8075
        A_Const *n = makeNode(A_Const);
 
8076
        n->val.type = T_Integer;
 
8077
        n->val.val.ival = val;
 
8078
        n->typename = SystemTypeName("int4");
 
8079
 
 
8080
        return (Node *)n;
 
8081
}
 
8082
 
 
8083
static Node *
 
8084
makeFloatConst(char *str)
 
8085
{
 
8086
        A_Const *n = makeNode(A_Const);
 
8087
 
 
8088
        n->val.type = T_Float;
 
8089
        n->val.val.str = str;
 
8090
        n->typename = SystemTypeName("float8");
 
8091
 
 
8092
        return (Node *)n;
 
8093
}
 
8094
 
 
8095
static Node *
 
8096
makeAConst(Value *v)
 
8097
{
 
8098
        Node *n;
 
8099
 
 
8100
        switch (v->type)
 
8101
        {
 
8102
                case T_Float:
 
8103
                        n = makeFloatConst(v->val.str);
 
8104
                        break;
 
8105
 
 
8106
                case T_Integer:
 
8107
                        n = makeIntConst(v->val.ival);
 
8108
                        break;
 
8109
 
 
8110
                case T_String:
 
8111
                default:
 
8112
                        n = makeStringConst(v->val.str, NULL);
 
8113
                        break;
 
8114
        }
 
8115
 
 
8116
        return n;
 
8117
}
 
8118
 
 
8119
/* makeDefElem()
 
8120
 * Create a DefElem node and set contents.
 
8121
 * Could be moved to nodes/makefuncs.c if this is useful elsewhere.
 
8122
 */
 
8123
static DefElem *
 
8124
makeDefElem(char *name, Node *arg)
 
8125
{
 
8126
        DefElem *f = makeNode(DefElem);
 
8127
        f->defname = name;
 
8128
        f->arg = arg;
 
8129
        return f;
 
8130
}
 
8131
 
 
8132
/* makeBoolAConst()
 
8133
 * Create an A_Const node and initialize to a boolean constant.
 
8134
 */
 
8135
static A_Const *
 
8136
makeBoolAConst(bool state)
 
8137
{
 
8138
        A_Const *n = makeNode(A_Const);
 
8139
        n->val.type = T_String;
 
8140
        n->val.val.str = (state? "t": "f");
 
8141
        n->typename = SystemTypeName("bool");
 
8142
        return n;
 
8143
}
 
8144
 
 
8145
/* makeRowNullTest()
 
8146
 * Generate separate operator nodes for a single row descriptor test.
 
8147
 *
 
8148
 * Eventually this should be eliminated in favor of making the NullTest
 
8149
 * node type capable of handling it directly.
 
8150
 */
 
8151
static Node *
 
8152
makeRowNullTest(NullTestType test, RowExpr *row)
 
8153
{
 
8154
        Node            *result = NULL;
 
8155
        ListCell        *arg;
 
8156
 
 
8157
        foreach(arg, row->args)
 
8158
        {
 
8159
                NullTest *n;
 
8160
 
 
8161
                n = makeNode(NullTest);
 
8162
                n->arg = (Expr *) lfirst(arg);
 
8163
                n->nulltesttype = test;
 
8164
 
 
8165
                if (result == NULL)
 
8166
                        result = (Node *) n;
 
8167
                else if (test == IS_NOT_NULL)
 
8168
                        result = (Node *) makeA_Expr(AEXPR_OR, NIL, result, (Node *)n);
 
8169
                else
 
8170
                        result = (Node *) makeA_Expr(AEXPR_AND, NIL, result, (Node *)n);
 
8171
        }
 
8172
 
 
8173
        if (result == NULL)
 
8174
        {
 
8175
                /* zero-length rows?  Generate constant TRUE or FALSE */
 
8176
                result = (Node *) makeBoolAConst(test == IS_NULL);
 
8177
        }
 
8178
 
 
8179
        return result;
 
8180
}
 
8181
 
 
8182
/* makeOverlaps()
 
8183
 * Create and populate a FuncCall node to support the OVERLAPS operator.
 
8184
 */
 
8185
static FuncCall *
 
8186
makeOverlaps(List *largs, List *rargs)
 
8187
{
 
8188
        FuncCall *n = makeNode(FuncCall);
 
8189
        n->funcname = SystemFuncName("overlaps");
 
8190
        if (list_length(largs) == 1)
 
8191
                largs = lappend(largs, largs);
 
8192
        else if (list_length(largs) != 2)
 
8193
                ereport(ERROR,
 
8194
                                (errcode(ERRCODE_SYNTAX_ERROR),
 
8195
                                 errmsg("wrong number of parameters on left side of OVERLAPS expression")));
 
8196
        if (list_length(rargs) == 1)
 
8197
                rargs = lappend(rargs, rargs);
 
8198
        else if (list_length(rargs) != 2)
 
8199
                ereport(ERROR,
 
8200
                                (errcode(ERRCODE_SYNTAX_ERROR),
 
8201
                                 errmsg("wrong number of parameters on right side of OVERLAPS expression")));
 
8202
        n->args = list_concat(largs, rargs);
 
8203
        n->agg_star = FALSE;
 
8204
        n->agg_distinct = FALSE;
 
8205
        return n;
 
8206
}
 
8207
 
 
8208
/* check_qualified_name --- check the result of qualified_name production
 
8209
 *
 
8210
 * It's easiest to let the grammar production for qualified_name allow
 
8211
 * subscripts and '*', which we then must reject here.
 
8212
 */
 
8213
static void
 
8214
check_qualified_name(List *names)
 
8215
{
 
8216
        ListCell   *i;
 
8217
 
 
8218
        foreach(i, names)
 
8219
        {
 
8220
                if (!IsA(lfirst(i), String))
 
8221
                        yyerror("syntax error");
 
8222
                else if (strcmp(strVal(lfirst(i)), "*") == 0)
 
8223
                        yyerror("syntax error");
 
8224
        }
 
8225
}
 
8226
 
 
8227
/* check_func_name --- check the result of func_name production
 
8228
 *
 
8229
 * It's easiest to let the grammar production for func_name allow subscripts
 
8230
 * and '*', which we then must reject here.
 
8231
 */
 
8232
static List *
 
8233
check_func_name(List *names)
 
8234
{
 
8235
        ListCell   *i;
 
8236
 
 
8237
        foreach(i, names)
 
8238
        {
 
8239
                if (!IsA(lfirst(i), String))
 
8240
                        yyerror("syntax error");
 
8241
                else if (strcmp(strVal(lfirst(i)), "*") == 0)
 
8242
                        yyerror("syntax error");
 
8243
        }
 
8244
        return names;
 
8245
}
 
8246
 
 
8247
/* extractArgTypes()
 
8248
 * Given a list of FunctionParameter nodes, extract a list of just the
 
8249
 * argument types (TypeNames).  Most of the productions using func_args
 
8250
 * don't currently want the full FunctionParameter data, so we use this
 
8251
 * rather than having two sets of productions.
 
8252
 */
 
8253
static List *
 
8254
extractArgTypes(List *parameters)
 
8255
{
 
8256
        List       *result = NIL;
 
8257
        ListCell   *i;
 
8258
 
 
8259
        foreach(i, parameters)
 
8260
        {
 
8261
                FunctionParameter *p = (FunctionParameter *) lfirst(i);
 
8262
 
 
8263
                result = lappend(result, p->argType);
 
8264
        }
 
8265
        return result;
 
8266
}
 
8267
 
 
8268
/* findLeftmostSelect()
 
8269
 * Find the leftmost component SelectStmt in a set-operation parsetree.
 
8270
 */
 
8271
static SelectStmt *
 
8272
findLeftmostSelect(SelectStmt *node)
 
8273
{
 
8274
        while (node && node->op != SETOP_NONE)
 
8275
                node = node->larg;
 
8276
        Assert(node && IsA(node, SelectStmt) && node->larg == NULL);
 
8277
        return node;
 
8278
}
 
8279
 
 
8280
/* insertSelectOptions()
 
8281
 * Insert ORDER BY, etc into an already-constructed SelectStmt.
 
8282
 *
 
8283
 * This routine is just to avoid duplicating code in SelectStmt productions.
 
8284
 */
 
8285
static void
 
8286
insertSelectOptions(SelectStmt *stmt,
 
8287
                                        List *sortClause, List *forUpdate,
 
8288
                                        Node *limitOffset, Node *limitCount)
 
8289
{
 
8290
        /*
 
8291
         * Tests here are to reject constructs like
 
8292
         *      (SELECT foo ORDER BY bar) ORDER BY baz
 
8293
         */
 
8294
        if (sortClause)
 
8295
        {
 
8296
                if (stmt->sortClause)
 
8297
                        ereport(ERROR,
 
8298
                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
8299
                                         errmsg("multiple ORDER BY clauses not allowed")));
 
8300
                stmt->sortClause = sortClause;
 
8301
        }
 
8302
        if (forUpdate)
 
8303
        {
 
8304
                if (stmt->forUpdate)
 
8305
                        ereport(ERROR,
 
8306
                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
8307
                                         errmsg("multiple FOR UPDATE clauses not allowed")));
 
8308
                stmt->forUpdate = forUpdate;
 
8309
        }
 
8310
        if (limitOffset)
 
8311
        {
 
8312
                if (stmt->limitOffset)
 
8313
                        ereport(ERROR,
 
8314
                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
8315
                                         errmsg("multiple OFFSET clauses not allowed")));
 
8316
                stmt->limitOffset = limitOffset;
 
8317
        }
 
8318
        if (limitCount)
 
8319
        {
 
8320
                if (stmt->limitCount)
 
8321
                        ereport(ERROR,
 
8322
                                        (errcode(ERRCODE_SYNTAX_ERROR),
 
8323
                                         errmsg("multiple LIMIT clauses not allowed")));
 
8324
                stmt->limitCount = limitCount;
 
8325
        }
 
8326
}
 
8327
 
 
8328
static Node *
 
8329
makeSetOp(SetOperation op, bool all, Node *larg, Node *rarg)
 
8330
{
 
8331
        SelectStmt *n = makeNode(SelectStmt);
 
8332
 
 
8333
        n->op = op;
 
8334
        n->all = all;
 
8335
        n->larg = (SelectStmt *) larg;
 
8336
        n->rarg = (SelectStmt *) rarg;
 
8337
        return (Node *) n;
 
8338
}
 
8339
 
 
8340
/* SystemFuncName()
 
8341
 * Build a properly-qualified reference to a built-in function.
 
8342
 */
 
8343
List *
 
8344
SystemFuncName(char *name)
 
8345
{
 
8346
        return list_make2(makeString("pg_catalog"), makeString(name));
 
8347
}
 
8348
 
 
8349
/* SystemTypeName()
 
8350
 * Build a properly-qualified reference to a built-in type.
 
8351
 *
 
8352
 * typmod is defaulted, but may be changed afterwards by caller.
 
8353
 */
 
8354
TypeName *
 
8355
SystemTypeName(char *name)
 
8356
{
 
8357
        TypeName   *n = makeNode(TypeName);
 
8358
 
 
8359
        n->names = list_make2(makeString("pg_catalog"), makeString(name));
 
8360
        n->typmod = -1;
 
8361
        return n;
 
8362
}
 
8363
 
 
8364
/* parser_init()
 
8365
 * Initialize to parse one query string
 
8366
 */
 
8367
void
 
8368
parser_init(void)
 
8369
{
 
8370
        QueryIsRule = FALSE;
 
8371
}
 
8372
 
 
8373
/* exprIsNullConstant()
 
8374
 * Test whether an a_expr is a plain NULL constant or not.
 
8375
 */
 
8376
bool
 
8377
exprIsNullConstant(Node *arg)
 
8378
{
 
8379
        if (arg && IsA(arg, A_Const))
 
8380
        {
 
8381
                A_Const *con = (A_Const *) arg;
 
8382
 
 
8383
                if (con->val.type == T_Null &&
 
8384
                        con->typename == NULL)
 
8385
                        return TRUE;
 
8386
        }
 
8387
        return FALSE;
 
8388
}
 
8389
 
 
8390
/* doNegate()
 
8391
 * Handle negation of a numeric constant.
 
8392
 *
 
8393
 * Formerly, we did this here because the optimizer couldn't cope with
 
8394
 * indexquals that looked like "var = -4" --- it wants "var = const"
 
8395
 * and a unary minus operator applied to a constant didn't qualify.
 
8396
 * As of Postgres 7.0, that problem doesn't exist anymore because there
 
8397
 * is a constant-subexpression simplifier in the optimizer.  However,
 
8398
 * there's still a good reason for doing this here, which is that we can
 
8399
 * postpone committing to a particular internal representation for simple
 
8400
 * negative constants.  It's better to leave "-123.456" in string form
 
8401
 * until we know what the desired type is.
 
8402
 */
 
8403
static Node *
 
8404
doNegate(Node *n)
 
8405
{
 
8406
        if (IsA(n, A_Const))
 
8407
        {
 
8408
                A_Const *con = (A_Const *)n;
 
8409
 
 
8410
                if (con->val.type == T_Integer)
 
8411
                {
 
8412
                        con->val.val.ival = -con->val.val.ival;
 
8413
                        return n;
 
8414
                }
 
8415
                if (con->val.type == T_Float)
 
8416
                {
 
8417
                        doNegateFloat(&con->val);
 
8418
                        return n;
 
8419
                }
 
8420
        }
 
8421
 
 
8422
        return (Node *) makeSimpleA_Expr(AEXPR_OP, "-", NULL, n);
 
8423
}
 
8424
 
 
8425
static void
 
8426
doNegateFloat(Value *v)
 
8427
{
 
8428
        char   *oldval = v->val.str;
 
8429
 
 
8430
        Assert(IsA(v, Float));
 
8431
        if (*oldval == '+')
 
8432
                oldval++;
 
8433
        if (*oldval == '-')
 
8434
                v->val.str = oldval+1;  /* just strip the '-' */
 
8435
        else
 
8436
        {
 
8437
                char   *newval = (char *) palloc(strlen(oldval) + 2);
 
8438
 
 
8439
                *newval = '-';
 
8440
                strcpy(newval+1, oldval);
 
8441
                v->val.str = newval;
 
8442
        }
 
8443
}
 
8444
 
 
8445
#include "scan.c"