Return-Path: X-Original-To: apmail-tajo-commits-archive@minotaur.apache.org Delivered-To: apmail-tajo-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EDC62110B7 for ; Wed, 9 Jul 2014 06:21:33 +0000 (UTC) Received: (qmail 89727 invoked by uid 500); 9 Jul 2014 06:21:33 -0000 Delivered-To: apmail-tajo-commits-archive@tajo.apache.org Received: (qmail 89648 invoked by uid 500); 9 Jul 2014 06:21:33 -0000 Mailing-List: contact commits-help@tajo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@tajo.apache.org Delivered-To: mailing list commits@tajo.apache.org Received: (qmail 89525 invoked by uid 99); 9 Jul 2014 06:21:33 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Jul 2014 06:21:33 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 8A3B19A8E63; Wed, 9 Jul 2014 06:21:33 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hyunsik@apache.org To: commits@tajo.apache.org Date: Wed, 09 Jul 2014 06:21:37 -0000 Message-Id: <3b6d5c2aac934348b629fe0f1df27f2a@git.apache.org> In-Reply-To: <762ee8a351854463b70cdf8982ef3b89@git.apache.org> References: <762ee8a351854463b70cdf8982ef3b89@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [05/13] Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/tajo into window_function http://git-wip-us.apache.org/repos/asf/tajo/blob/1b3ec373/CHANGES.txt ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tajo/blob/1b3ec373/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 ---------------------------------------------------------------------- diff --cc tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 index 0000000,7fa7973..ece6f03 mode 000000,100644..100644 --- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 +++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 @@@ -1,0 -1,479 +1,496 @@@ + /* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + + lexer grammar SQLLexer; + + @header { + } + + @members { + } + + + /* + =============================================================================== + Tokens for Case Insensitive Keywords + =============================================================================== + */ + fragment A + : 'A' | 'a'; + + fragment B + : 'B' | 'b'; + + fragment C + : 'C' | 'c'; + + fragment D + : 'D' | 'd'; + + fragment E + : 'E' | 'e'; + + fragment F + : 'F' | 'f'; + + fragment G + : 'G' | 'g'; + + fragment H + : 'H' | 'h'; + + fragment I + : 'I' | 'i'; + + fragment J + : 'J' | 'j'; + + fragment K + : 'K' | 'k'; + + fragment L + : 'L' | 'l'; + + fragment M + : 'M' | 'm'; + + fragment N + : 'N' | 'n'; + + fragment O + : 'O' | 'o'; + + fragment P + : 'P' | 'p'; + + fragment Q + : 'Q' | 'q'; + + fragment R + : 'R' | 'r'; + + fragment S + : 'S' | 's'; + + fragment T + : 'T' | 't'; + + fragment U + : 'U' | 'u'; + + fragment V + : 'V' | 'v'; + + fragment W + : 'W' | 'w'; + + fragment X + : 'X' | 'x'; + + fragment Y + : 'Y' | 'y'; + + fragment Z + : 'Z' | 'z'; + + /* + =============================================================================== + Reserved Keywords + =============================================================================== + */ + + AS : A S; + ALL : A L L; + AND : A N D; + ANY : A N Y; + ASYMMETRIC : A S Y M M E T R I C; + ASC : A S C; + + + BOTH : B O T H; + + CASE : C A S E; + CAST : C A S T; + CREATE : C R E A T E; + CROSS : C R O S S; + + DESC : D E S C; + DISTINCT : D I S T I N C T; + + END : E N D; + ELSE : E L S E; + EXCEPT : E X C E P T; + + FALSE : F A L S E; + FULL : F U L L; + FROM : F R O M; + + GROUP : G R O U P; + + HAVING : H A V I N G; + + ILIKE : I L I K E; + IN : I N; + INNER : I N N E R; + INTERSECT : I N T E R S E C T; + INTO : I N T O; + IS : I S; + + JOIN : J O I N; + + LEADING : L E A D I N G; + LEFT : L E F T; + LIKE : L I K E; + LIMIT : L I M I T; + + NATURAL : N A T U R A L; + NOT : N O T; + NULL : N U L L; + + ON : O N; -OUTER : O U T E R; + OR : O R; + ORDER : O R D E R; ++OUTER : O U T E R; ++OVER : O V E R; + RIGHT : R I G H T; + SELECT : S E L E C T; + SOME : S O M E; + SYMMETRIC : S Y M M E T R I C; + + TABLE : T A B L E; + THEN : T H E N; + TRAILING : T R A I L I N G; + TRUE : T R U E; + + UNION : U N I O N; + UNIQUE : U N I Q U E; + USING : U S I N G; + + WHEN : W H E N; + WHERE : W H E R E; + WITH : W I T H; ++WINDOW : W I N D O W; + + /* + =============================================================================== + Non Reserved Keywords + =============================================================================== + */ + AVG : A V G; + ADD: A D D; + ALTER : A L T E R; + + BETWEEN : B E T W E E N; + BY : B Y; + + CENTURY : C E N T U R Y; + CHARACTER : C H A R A C T E R; + COLLECT : C O L L E C T; + COALESCE : C O A L E S C E; + COLUMN : C O L U M N; + COUNT : C O U N T; + CUBE : C U B E; ++CUME_DIST : C U M E UNDERLINE D I S T; ++CURRENT : C U R R E N T; + + DAY : D A Y; + DATABASE : D A T A B A S E; + DEC : D E C; + DECADE : D E C A D E; ++DENSE_RANK : D E N S E UNDERLINE R A N K; + DOW : D O W; + DOY : D O Y; + DROP : D R O P; + + EPOCH : E P O C H; + EVERY : E V E R Y; ++EXCLUDE : E X C L U D E; + EXISTS : E X I S T S; + EXPLAIN : E X P L A I N; + EXTERNAL : E X T E R N A L; + EXTRACT : E X T R A C T; + + FILTER : F I L T E R; + FIRST : F I R S T; ++FOLLOWING : F O L L O W I N G; + FORMAT : F O R M A T; + FUSION : F U S I O N; + + GROUPING : G R O U P I N G; + + HASH : H A S H; + HOUR : H O U R; + + IF : I F; + INDEX : I N D E X; + INSERT : I N S E R T; + INTERSECTION : I N T E R S E C T I O N; + ISODOW : I S O D O W; + ISOYEAR : I S O Y E A R; + + LAST : L A S T; + LESS : L E S S; + LIST : L I S T; + LOCATION : L O C A T I O N; + + MAX : M A X; + MAXVALUE : M A X V A L U E; + MICROSECONDS : M I C R O S E C O N D S; + MILLENNIUM : M I L L E N N I U M; + MILLISECONDS : M I L L I S E C O N D S; + MIN : M I N; + MINUTE : M I N U T E; + MONTH : M O N T H; + + NATIONAL : N A T I O N A L; + NULLIF : N U L L I F; ++NO : N O; + + OVERWRITE : O V E R W R I T E; ++OTHERS: O T H E R S; + + PARTITION : P A R T I T I O N; + PARTITIONS : P A R T I T I O N S; ++PERCENT_RANK : P E R C E N T UNDERLINE R A N K; ++PRECEDING : P R E C E D I N G; + PRECISION : P R E C I S I O N; + PURGE : P U R G E; + + QUARTER : Q U A R T E R; + + RANGE : R A N G E; ++RANK : R A N K; + REGEXP : R E G E X P; + RLIKE : R L I K E; + ROLLUP : R O L L U P; ++ROW : R O W; ++ROWS : R O W S; ++ROW_NUMBER : R O W UNDERLINE N U M B E R; + RENAME : R E N A M E; + + SECOND : S E C O N D; + SET : S E T; + SIMILAR : S I M I L A R; + STDDEV_POP : S T D D E V UNDERLINE P O P; + STDDEV_SAMP : S T D D E V UNDERLINE S A M P; + SUBPARTITION : S U B P A R T I T I O N; + SUM : S U M; + + TABLESPACE : T A B L E S P A C E; + THAN : T H A N; ++TIES : T I E S; + TIMEZONE: T I M E Z O N E; + TIMEZONE_HOUR: T I M E Z O N E UNDERLINE H O U R; + TIMEZONE_MINUTE: T I M E Z O N E UNDERLINE M I N U T E; + TRIM : T R I M; + TO : T O; + ++UNBOUNDED : U N B O U N D E D; + UNKNOWN : U N K N O W N; + + VALUES : V A L U E S; + VAR_SAMP : V A R UNDERLINE S A M P; + VAR_POP : V A R UNDERLINE P O P; + VARYING : V A R Y I N G; + + WEEK : W E E K; + + YEAR : Y E A R; + + ZONE : Z O N E; + + + /* + =============================================================================== + Data Type Tokens + =============================================================================== + */ + BOOLEAN : B O O L E A N; + BOOL : B O O L; + BIT : B I T; + VARBIT : V A R B I T; + + INT1 : I N T '1'; + INT2 : I N T '2'; + INT4 : I N T '4'; + INT8 : I N T '8'; + + TINYINT : T I N Y I N T; // alias for INT1 + SMALLINT : S M A L L I N T; // alias for INT2 + INT : I N T; // alias for INT4 + INTEGER : I N T E G E R; // alias - INT4 + BIGINT : B I G I N T; // alias for INT8 + + FLOAT4 : F L O A T '4'; + FLOAT8 : F L O A T '8'; + + REAL : R E A L; // alias for FLOAT4 + FLOAT : F L O A T; // alias for FLOAT8 + DOUBLE : D O U B L E; // alias for FLOAT8 + + NUMERIC : N U M E R I C; + DECIMAL : D E C I M A L; // alias for number + + CHAR : C H A R; + VARCHAR : V A R C H A R; + NCHAR : N C H A R; + NVARCHAR : N V A R C H A R; + + DATE : D A T E; + TIME : T I M E; + TIMETZ : T I M E T Z; + TIMESTAMP : T I M E S T A M P; + TIMESTAMPTZ : T I M E S T A M P T Z; + + TEXT : T E X T; + + BINARY : B I N A R Y; + VARBINARY : V A R B I N A R Y; + BLOB : B L O B; + BYTEA : B Y T E A; // alias for BLOB + + INET4 : I N E T '4'; + + // Operators + Similar_To : '~'; + Not_Similar_To : '!~'; + Similar_To_Case_Insensitive : '~*'; + Not_Similar_To_Case_Insensitive : '!~*'; + + // Cast Operator + CAST_EXPRESSION + : COLON COLON + ; + + ASSIGN : ':='; + EQUAL : '='; + COLON : ':'; + SEMI_COLON : ';'; + COMMA : ','; + CONCATENATION_OPERATOR : VERTICAL_BAR VERTICAL_BAR; + NOT_EQUAL : '<>' | '!=' | '~='| '^=' ; + LTH : '<' ; + LEQ : '<='; + GTH : '>'; + GEQ : '>='; + LEFT_PAREN : '('; + RIGHT_PAREN : ')'; + PLUS : '+'; + MINUS : '-'; + MULTIPLY: '*'; + DIVIDE : '/'; + MODULAR : '%'; + DOT : '.'; + UNDERLINE : '_'; + VERTICAL_BAR : '|'; + QUOTE : '\''; + DOUBLE_QUOTE : '"'; + + NUMBER : Digit+; + + fragment + Digit : '0'..'9'; + + REAL_NUMBER + : ('0'..'9')+ '.' ('0'..'9')* EXPONENT? + | '.' ('0'..'9')+ EXPONENT? + | ('0'..'9')+ EXPONENT + ; + + BlockComment + : '/*' .*? '*/' -> skip + ; + + LineComment + : '--' ~[\r\n]* -> skip + ; + + /* + =============================================================================== + Identifiers + =============================================================================== + */ + + Regular_Identifier + : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|Digit|'_')* { setText(getText().toLowerCase()); } + ; + + Quoted_Identifier + : DOUBLE_QUOTE ( ESC_SEQ | ~('\\'|'"') )* DOUBLE_QUOTE { setText(getText().substring(1, getText().length()-1)); } + ; + + /* + =============================================================================== + Literal + =============================================================================== + */ + + // Some Unicode Character Ranges + fragment + Control_Characters : '\u0001' .. '\u001F'; + fragment + Extended_Control_Characters : '\u0080' .. '\u009F'; + + Character_String_Literal + : QUOTE ( ESC_SEQ | ~('\\'|'\'') )* QUOTE + ; + + fragment + EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ; + + fragment + HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ; + + fragment + ESC_SEQ + : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') + | UNICODE_ESC + | OCTAL_ESC + ; + + fragment + OCTAL_ESC + : '\\' ('0'..'3') ('0'..'7') ('0'..'7') + | '\\' ('0'..'7') ('0'..'7') + | '\\' ('0'..'7') + ; + + fragment + UNICODE_ESC + : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT + ; + + + /* + =============================================================================== + Whitespace Tokens + =============================================================================== + */ + + Space + : ' ' -> skip + ; + + White_Space + : ( Control_Characters | Extended_Control_Characters )+ -> skip + ; + + + BAD + : . -> skip + ; http://git-wip-us.apache.org/repos/asf/tajo/blob/1b3ec373/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 ---------------------------------------------------------------------- diff --cc tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 index 0000000,f6385eb..3a70416 mode 000000,100644..100644 --- a/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 +++ b/tajo-core/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 @@@ -1,0 -1,1398 +1,1519 @@@ + /** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + parser grammar SQLParser; + + options { + language=Java; + tokenVocab=SQLLexer; + } + + @header { + } + + @members { + } + + /* + =============================================================================== + SQL statement (Start Symbol) + =============================================================================== + */ + sql + : (explain_clause)? statement (SEMI_COLON)? EOF + ; + + explain_clause + : EXPLAIN + ; + + statement + : data_statement + | data_change_statement + | schema_statement + | index_statement + ; + + data_statement + : query_expression + ; + + data_change_statement + : insert_statement + ; + + schema_statement + : database_definition + | drop_database_statement + | create_table_statement + | drop_table_statement + | alter_tablespace_statement + | alter_table_statement + ; + + index_statement + : CREATE (u=UNIQUE)? INDEX n=identifier ON t=table_name (m=method_specifier)? + LEFT_PAREN s=sort_specifier_list RIGHT_PAREN p=param_clause? + ; + + database_definition + : CREATE DATABASE (if_not_exists)? dbname = identifier + ; + + if_not_exists + : IF NOT EXISTS + ; + + drop_database_statement + : DROP DATABASE (if_exists)? dbname = identifier + ; + + if_exists + : IF EXISTS + ; + + create_table_statement + : CREATE EXTERNAL TABLE (if_not_exists)? table_name table_elements USING file_type=identifier + (param_clause)? (table_partitioning_clauses)? (LOCATION path=Character_String_Literal) + | CREATE TABLE (if_not_exists)? table_name table_elements (USING file_type=identifier)? + (param_clause)? (table_partitioning_clauses)? (AS query_expression)? + | CREATE TABLE (if_not_exists)? table_name (USING file_type=identifier)? + (param_clause)? (table_partitioning_clauses)? AS query_expression + ; + + table_elements + : LEFT_PAREN field_element (COMMA field_element)* RIGHT_PAREN + ; + + field_element + : name=identifier field_type + ; + + field_type + : data_type + ; + + param_clause + : WITH LEFT_PAREN param (COMMA param)* RIGHT_PAREN + ; + + param + : key=Character_String_Literal EQUAL value=numeric_value_expression + ; + + method_specifier + : USING m=identifier + ; + + table_space_specifier + : TABLESPACE table_space_name + ; + + table_space_name + : identifier + ; + + table_partitioning_clauses + : range_partitions + | hash_partitions + | list_partitions + | column_partitions + ; + + range_partitions + : PARTITION BY RANGE LEFT_PAREN column_reference_list RIGHT_PAREN + LEFT_PAREN range_value_clause_list RIGHT_PAREN + ; + + range_value_clause_list + : range_value_clause (COMMA range_value_clause)* + ; + + range_value_clause + : PARTITION partition_name VALUES LESS THAN (LEFT_PAREN value_expression RIGHT_PAREN | LEFT_PAREN? MAXVALUE RIGHT_PAREN?) + ; + + hash_partitions + : PARTITION BY HASH LEFT_PAREN column_reference_list RIGHT_PAREN + (LEFT_PAREN individual_hash_partitions RIGHT_PAREN | hash_partitions_by_quantity) + ; + + individual_hash_partitions + : individual_hash_partition (COMMA individual_hash_partition)* + ; + + individual_hash_partition + : PARTITION partition_name + ; + + hash_partitions_by_quantity + : PARTITIONS quantity = numeric_value_expression + ; + + list_partitions + : PARTITION BY LIST LEFT_PAREN column_reference_list RIGHT_PAREN LEFT_PAREN list_value_clause_list RIGHT_PAREN + ; + + list_value_clause_list + : list_value_partition (COMMA list_value_partition)* + ; + + list_value_partition + : PARTITION partition_name VALUES (IN)? LEFT_PAREN in_value_list RIGHT_PAREN + ; + + column_partitions + : PARTITION BY COLUMN table_elements + ; + + partition_name + : identifier + ; + + /* + =============================================================================== + 11.21 + =============================================================================== + */ + + drop_table_statement + : DROP TABLE (if_exists)? table_name (PURGE)? + ; + + /* + =============================================================================== + 5.2 + + Specifying lexical units (tokens and separators) that participate in SQL language + =============================================================================== + */ + + identifier + : Regular_Identifier + | nonreserved_keywords + | Quoted_Identifier + ; + + nonreserved_keywords + : ADD + | AVG + | ALTER + | BETWEEN + | BY + | CENTURY + | CHARACTER + | COALESCE + | COLLECT + | COLUMN + | COUNT + | CUBE ++ | CUME_DIST ++ | CURRENT + | DAY + | DEC + | DECADE ++ | DENSE_RANK + | DOW + | DOY + | DROP + | EPOCH + | EVERY + | EXISTS ++ | EXCLUDE + | EXPLAIN + | EXTERNAL + | EXTRACT + | FILTER + | FIRST ++ | FOLLOWING + | FORMAT + | FUSION + | GROUPING + | HASH + | INDEX + | INSERT + | INTERSECTION + | ISODOW + | ISOYEAR + | LAST + | LESS + | LIST + | LOCATION + | MAX + | MAXVALUE + | MICROSECONDS + | MILLENNIUM + | MILLISECONDS + | MIN + | MINUTE + | MONTH + | NATIONAL ++ | NO + | NULLIF + | OVERWRITE ++ | OTHERS + | PARTITION + | PARTITIONS ++ | PERCENT_RANK ++ | PRECEDING + | PRECISION + | PURGE + | QUARTER + | RANGE ++ | RANK + | REGEXP + | RENAME + | RLIKE + | ROLLUP ++ | ROW ++ | ROWS ++ | ROW_NUMBER + | SECOND + | SET + | SIMILAR + | STDDEV_POP + | STDDEV_SAMP + | SUBPARTITION + | SUM + | TABLESPACE + | THAN ++ | TIES + | TIMEZONE + | TIMEZONE_HOUR + | TIMEZONE_MINUTE + | TRIM + | TO ++ | UNBOUNDED + | UNKNOWN + | VALUES + | VAR_POP + | VAR_SAMP + | VARYING + | WEEK + | YEAR + | ZONE + + | BIGINT + | BIT + | BLOB + | BOOL + | BOOLEAN + | BYTEA + | CHAR + | DATE + | DECIMAL + | DOUBLE + | FLOAT + | FLOAT4 + | FLOAT8 + | INET4 + | INT + | INT1 + | INT2 + | INT4 + | INT8 + | INTEGER + | NCHAR + | NUMERIC + | NVARCHAR + | REAL + | SMALLINT + | TEXT + | TIME + | TIMESTAMP + | TIMESTAMPTZ + | TIMETZ + | TINYINT + | VARBINARY + | VARBIT + | VARCHAR + ; + + /* + =============================================================================== + 5.3 + =============================================================================== + */ + + unsigned_literal + : unsigned_numeric_literal + | general_literal + ; + + general_literal + : Character_String_Literal + | datetime_literal + | boolean_literal + ; + + datetime_literal + : timestamp_literal + | time_literal + | date_literal + ; + + time_literal + : TIME time_string=Character_String_Literal + ; + + timestamp_literal + : TIMESTAMP timestamp_string=Character_String_Literal + ; + + date_literal + : DATE date_string=Character_String_Literal + ; + + boolean_literal + : TRUE | FALSE | UNKNOWN + ; + + /* + =============================================================================== + 6.1 + =============================================================================== + */ + + data_type + : predefined_type + ; + + predefined_type + : character_string_type + | national_character_string_type + | binary_large_object_string_type + | numeric_type + | boolean_type + | datetime_type + | bit_type + | binary_type + | network_type + ; + + network_type + : INET4 + ; + + character_string_type + : CHARACTER type_length? + | CHAR type_length? + | CHARACTER VARYING type_length? + | CHAR VARYING type_length? + | VARCHAR type_length? + | TEXT + ; + + type_length + : LEFT_PAREN NUMBER RIGHT_PAREN + ; + + national_character_string_type + : NATIONAL CHARACTER type_length? + | NATIONAL CHAR type_length? + | NCHAR type_length? + | NATIONAL CHARACTER VARYING type_length? + | NATIONAL CHAR VARYING type_length? + | NCHAR VARYING type_length? + | NVARCHAR type_length? + ; + + binary_large_object_string_type + : BLOB type_length? + | BYTEA type_length? + ; + + numeric_type + : exact_numeric_type | approximate_numeric_type + ; + + exact_numeric_type + : NUMERIC (precision_param)? + | DECIMAL (precision_param)? + | DEC (precision_param)? + | INT1 + | TINYINT + | INT2 + | SMALLINT + | INT4 + | INT + | INTEGER + | INT8 + | BIGINT + ; + + approximate_numeric_type + : FLOAT (precision_param)? + | FLOAT4 + | REAL + | FLOAT8 + | DOUBLE + | DOUBLE PRECISION + ; + + precision_param + : LEFT_PAREN precision=NUMBER RIGHT_PAREN + | LEFT_PAREN precision=NUMBER COMMA scale=NUMBER RIGHT_PAREN + ; + + boolean_type + : BOOLEAN + | BOOL + ; + + datetime_type + : DATE + | TIME + | TIME WITH TIME ZONE + | TIMETZ + | TIMESTAMP + | TIMESTAMP WITH TIME ZONE + | TIMESTAMPTZ + ; + + bit_type + : BIT type_length? + | VARBIT type_length? + | BIT VARYING type_length? + ; + + binary_type + : BINARY type_length? + | BINARY VARYING type_length? + | VARBINARY type_length? + ; + + /* + =============================================================================== + 6.3 + =============================================================================== + */ + value_expression_primary + : parenthesized_value_expression + | nonparenthesized_value_expression_primary + ; + + parenthesized_value_expression + : LEFT_PAREN value_expression RIGHT_PAREN + ; + + nonparenthesized_value_expression_primary + : unsigned_value_specification + | column_reference + | set_function_specification ++ | window_function + | scalar_subquery + | case_expression + | cast_specification + | routine_invocation + ; + + /* + =============================================================================== + 6.4 + =============================================================================== + */ + + unsigned_value_specification + : unsigned_literal + ; + + unsigned_numeric_literal + : NUMBER + | REAL_NUMBER + ; + + signed_numerical_literal + : sign? unsigned_numeric_literal + ; + + /* + =============================================================================== + 6.9 + + Invoke an SQL-invoked routine. + =============================================================================== + */ + set_function_specification + : aggregate_function + ; + + aggregate_function + : COUNT LEFT_PAREN MULTIPLY RIGHT_PAREN + | general_set_function filter_clause? + ; + + general_set_function + : set_function_type LEFT_PAREN set_qualifier? value_expression RIGHT_PAREN + ; + + set_function_type + : AVG + | MAX + | MIN + | SUM + | EVERY + | ANY + | SOME + | COUNT + | STDDEV_POP + | STDDEV_SAMP + | VAR_SAMP + | VAR_POP + | COLLECT + | FUSION + | INTERSECTION + ; + + filter_clause + : FILTER LEFT_PAREN WHERE search_condition RIGHT_PAREN + ; + + grouping_operation + : GROUPING LEFT_PAREN column_reference_list RIGHT_PAREN + ; + + /* + =============================================================================== ++ 6.10 window function ++=============================================================================== ++*/ ++ ++window_function ++ : window_function_type OVER window_name_or_specification ++ ; ++ ++window_function_type ++ : rank_function_type LEFT_PAREN RIGHT_PAREN ++ | ROW_NUMBER LEFT_PAREN RIGHT_PAREN ++ | aggregate_function ++ ; ++ ++rank_function_type ++ : RANK | DENSE_RANK | PERCENT_RANK | CUME_DIST ++ ; ++ ++ ++window_name_or_specification ++ : window_name ++ | window_specification ++ ; ++ ++/* ++=============================================================================== + 6.11 + =============================================================================== + */ + + case_expression + : case_specification + ; + + case_abbreviation + : NULLIF LEFT_PAREN numeric_value_expression COMMA boolean_value_expression RIGHT_PAREN + | COALESCE LEFT_PAREN numeric_value_expression ( COMMA boolean_value_expression )+ RIGHT_PAREN + ; + + case_specification + : simple_case + | searched_case + ; + + simple_case + : CASE boolean_value_expression ( simple_when_clause )+ ( else_clause )? END + ; + + searched_case + : CASE (searched_when_clause)+ (else_clause)? END + ; + + simple_when_clause : WHEN search_condition THEN result ; + + searched_when_clause + : WHEN c=search_condition THEN r=result + ; + + else_clause + : ELSE r=result + ; + + result + : value_expression | NULL + ; + + /* + =============================================================================== + 6.12 + =============================================================================== + */ + + cast_specification + : CAST LEFT_PAREN cast_operand AS cast_target RIGHT_PAREN + ; + + cast_operand + : value_expression + ; + + cast_target + : data_type + ; + + /* + =============================================================================== + 6.25 + =============================================================================== + */ + value_expression + : common_value_expression + | row_value_expression + | boolean_value_expression + ; + + common_value_expression + : numeric_value_expression + | string_value_expression + | NULL + ; + + /* + =============================================================================== + 6.26 + + Specify a comparison of two row values. + =============================================================================== + */ + + numeric_value_expression + : left=term ((PLUS|MINUS) right=term)* + ; + + term + : left=factor ((MULTIPLY|DIVIDE|MODULAR) right=factor)* + ; + + factor + : (sign)? numeric_primary + ; + + array + : LEFT_PAREN numeric_value_expression (COMMA numeric_value_expression )* RIGHT_PAREN + ; + + numeric_primary + : value_expression_primary (CAST_EXPRESSION cast_target)* + | numeric_value_function + ; + + sign + : PLUS | MINUS + ; + + /* + =============================================================================== + 6.27 + =============================================================================== + */ + + numeric_value_function + : extract_expression + ; + + extract_expression + : EXTRACT LEFT_PAREN extract_field_string=extract_field FROM extract_source RIGHT_PAREN + ; + + extract_field + : primary_datetime_field + | time_zone_field + | extended_datetime_field + ; + + time_zone_field + : TIMEZONE | TIMEZONE_HOUR | TIMEZONE_MINUTE + ; + + extract_source + : column_reference + | datetime_literal + ; + + /* + =============================================================================== + 6.28 + =============================================================================== + */ + + string_value_expression + : character_value_expression + ; + + character_value_expression + : character_factor (CONCATENATION_OPERATOR character_factor)* + ; + + character_factor + : character_primary + ; + + character_primary + : value_expression_primary + | string_value_function + ; + + /* + =============================================================================== + 6.29 + =============================================================================== + */ + + string_value_function + : trim_function + ; + + trim_function + : TRIM LEFT_PAREN trim_operands RIGHT_PAREN + ; + + trim_operands + : ((trim_specification)? (trim_character=character_value_expression)? FROM)? trim_source=character_value_expression + | trim_source=character_value_expression COMMA trim_character=character_value_expression + ; + + trim_specification + : LEADING | TRAILING | BOTH + ; + + /* + =============================================================================== + 6.34 + =============================================================================== + */ + + boolean_value_expression + : or_predicate + ; + + or_predicate + : and_predicate (OR or_predicate)* + ; + + and_predicate + : boolean_factor (AND and_predicate)* + ; + + boolean_factor + : boolean_test + | NOT boolean_test + ; + + boolean_test + : boolean_primary is_clause? + ; + + is_clause + : IS NOT? t=truth_value + ; + + truth_value + : TRUE | FALSE | UNKNOWN + ; + + boolean_primary + : predicate + | boolean_predicand + ; + + boolean_predicand + : parenthesized_boolean_value_expression + | nonparenthesized_value_expression_primary + ; + + parenthesized_boolean_value_expression + : LEFT_PAREN boolean_value_expression RIGHT_PAREN + ; + + /* + =============================================================================== + 7.2 + =============================================================================== + */ + row_value_expression + : row_value_special_case + | explicit_row_value_constructor + ; + + row_value_special_case + : nonparenthesized_value_expression_primary + ; + + explicit_row_value_constructor + : NULL + ; + + row_value_predicand + : row_value_special_case + | row_value_constructor_predicand + ; + + row_value_constructor_predicand + : common_value_expression + | boolean_predicand + // | explicit_row_value_constructor + ; + + /* + =============================================================================== + 7.4 + =============================================================================== + */ + + table_expression + : from_clause + where_clause? + groupby_clause? + having_clause? + orderby_clause? ++ window_clause? + limit_clause? + ; + + /* + =============================================================================== + 7.5 + =============================================================================== + */ + + from_clause + : FROM table_reference_list + ; + + table_reference_list + :table_reference (COMMA table_reference)* + ; + + /* + =============================================================================== + 7.6
+ =============================================================================== + */ + + table_reference + : joined_table + | table_primary + ; + + /* + =============================================================================== + 7.7 + =============================================================================== + */ + + joined_table + : table_primary joined_table_primary+ + ; + + joined_table_primary + : CROSS JOIN right=table_primary + | (t=join_type)? JOIN right=table_primary s=join_specification + | NATURAL (t=join_type)? JOIN right=table_primary + | UNION JOIN right=table_primary + ; + + cross_join + : CROSS JOIN r=table_primary + ; + + qualified_join + : (t=join_type)? JOIN r=table_primary s=join_specification + ; + + natural_join + : NATURAL (t=join_type)? JOIN r=table_primary + ; + + union_join + : UNION JOIN r=table_primary + ; + + join_type + : INNER + | t=outer_join_type + ; + + outer_join_type + : outer_join_type_part2 OUTER? + ; + + outer_join_type_part2 + : LEFT + | RIGHT + | FULL + ; + + join_specification + : join_condition + | named_columns_join + ; + + join_condition + : ON search_condition + ; + + named_columns_join + : USING LEFT_PAREN f=column_reference_list RIGHT_PAREN + ; + + table_primary + : table_or_query_name ((AS)? alias=identifier)? (LEFT_PAREN column_name_list RIGHT_PAREN)? + | derived_table (AS)? name=identifier (LEFT_PAREN column_name_list RIGHT_PAREN)? + ; + + column_name_list + : identifier ( COMMA identifier )* + ; + + derived_table + : table_subquery + ; + + /* + =============================================================================== + 7.8 + =============================================================================== + */ + where_clause + : WHERE search_condition + ; + + search_condition + : value_expression // instead of boolean_value_expression, we use value_expression for more flexibility. + ; + + /* + =============================================================================== + 7.9 + =============================================================================== + */ + groupby_clause + : GROUP BY g=grouping_element_list + ; + + grouping_element_list + : grouping_element (COMMA grouping_element)* + ; + + grouping_element + : rollup_list + | cube_list + | empty_grouping_set + | ordinary_grouping_set + ; + + ordinary_grouping_set + : row_value_predicand + | LEFT_PAREN row_value_predicand_list RIGHT_PAREN + ; + + ordinary_grouping_set_list + : ordinary_grouping_set (COMMA ordinary_grouping_set)* + ; + + rollup_list + : ROLLUP LEFT_PAREN c=ordinary_grouping_set_list RIGHT_PAREN + ; + + cube_list + : CUBE LEFT_PAREN c=ordinary_grouping_set_list RIGHT_PAREN + ; + + empty_grouping_set + : LEFT_PAREN RIGHT_PAREN + ; + + having_clause + : HAVING boolean_value_expression + ; + + row_value_predicand_list + : row_value_predicand (COMMA row_value_predicand)* + ; + ++ ++ /* ++ =============================================================================== ++ 7.11 (p331) ++ =============================================================================== ++ */ ++ ++window_clause ++ : WINDOW window_definition_list; ++ ++window_definition_list ++ : window_definition (COMMA window_definition)* ++ ; ++ ++window_definition ++ : window_name AS window_specification ++ ; ++ ++window_name ++ : identifier ++ ; ++ ++window_specification ++ : LEFT_PAREN window_specification_details RIGHT_PAREN ++ ; ++ ++window_specification_details ++ : (existing_window_name)? (window_partition_clause)? (window_order_clause)? (window_frame_clause)? ++ ; ++ ++existing_window_name ++ : window_name ++ ; ++ ++window_partition_clause ++ : PARTITION BY row_value_predicand_list ++ ; ++ ++window_order_clause ++ : orderby_clause ++ ; ++ ++window_frame_clause ++ : window_frame_units window_frame_extent (window_frame_exclusion)? ++ ; ++ ++window_frame_units ++ : ROWS | RANGE ++ ; ++ ++window_frame_extent ++ : window_frame_start_bound ++ | window_frame_between ++ ; ++ ++window_frame_start_bound ++ : UNBOUNDED PRECEDING ++ | unsigned_value_specification PRECEDING // window_frame_preceding ++ | CURRENT ROW ++ ; ++ ++window_frame_between ++ : BETWEEN bound1=window_frame_start_bound AND bound2=window_frame_end_bound ++ ; ++ ++window_frame_end_bound ++ : UNBOUNDED FOLLOWING ++ | unsigned_value_specification FOLLOWING // window_frame_following FOLLOWING ++ | CURRENT ROW ++ ; ++ ++window_frame_exclusion ++ : EXCLUDE CURRENT ROW ++ | EXCLUDE GROUP ++ | EXCLUDE TIES ++ | EXCLUDE NO OTHERS ++ ; ++ + /* + =============================================================================== + 7.13 + =============================================================================== + */ + query_expression + : query_expression_body + ; + + query_expression_body + : non_join_query_expression + | joined_table + ; + + non_join_query_expression + : (non_join_query_term + | joined_table (UNION | EXCEPT) (ALL|DISTINCT)? query_term) + ((UNION | EXCEPT) (ALL|DISTINCT)? query_term)* + ; + + query_term + : non_join_query_term + | joined_table + ; + + non_join_query_term + : ( non_join_query_primary + | joined_table INTERSECT (ALL|DISTINCT)? query_primary) + (INTERSECT (ALL|DISTINCT)? query_primary)* + ; + + query_primary + : non_join_query_primary + | joined_table + ; + + non_join_query_primary + : simple_table + | LEFT_PAREN non_join_query_expression RIGHT_PAREN + ; + + simple_table + : query_specification + | explicit_table + ; + + explicit_table + : TABLE table_or_query_name + ; + + table_or_query_name + : table_name + | identifier + ; + + table_name + : identifier (DOT identifier ( DOT identifier)? )? + ; + + column_name + : identifier + ; + + query_specification + : SELECT set_qualifier? select_list table_expression? + ; + + select_list + : select_sublist (COMMA select_sublist)* + ; + + select_sublist + : derived_column + | qualified_asterisk + ; + + derived_column + : value_expression as_clause? + ; + + qualified_asterisk + : (tb_name=identifier DOT)? MULTIPLY + ; + + set_qualifier + : DISTINCT + | ALL + ; + + column_reference + : ((db_name = identifier DOT)? (tb_name=identifier DOT))? name=identifier + ; + + as_clause + : (AS)? identifier + ; + + column_reference_list + : column_reference (COMMA column_reference)* + ; + + /* + ============================================================================================== + 7.15 + + Specify a scalar value, a row, or a table derived from a query_expression . + ============================================================================================== + */ + + scalar_subquery + : subquery + ; + + row_subquery + : subquery + ; + + table_subquery + : subquery + ; + + subquery + : LEFT_PAREN query_expression RIGHT_PAREN + ; + + /* + =============================================================================== + 8.1 + =============================================================================== + */ + + predicate + : comparison_predicate + | between_predicate + | in_predicate + | pattern_matching_predicate // like predicate and other similar predicates + | null_predicate + | exists_predicate + ; + + /* + =============================================================================== + 8.2 + + Specify a comparison of two row values. + =============================================================================== + */ + comparison_predicate + : left=row_value_predicand c=comp_op right=row_value_predicand + ; + + comp_op + : EQUAL + | NOT_EQUAL + | LTH + | LEQ + | GTH + | GEQ + ; + + /* + =============================================================================== + 8.3 + =============================================================================== + */ + + between_predicate + : predicand=row_value_predicand between_predicate_part_2 + ; + + between_predicate_part_2 + : (NOT)? BETWEEN (ASYMMETRIC | SYMMETRIC)? begin=row_value_predicand AND end=row_value_predicand + ; + + + /* + =============================================================================== + 8.4 + =============================================================================== + */ + + in_predicate + : predicand=numeric_value_expression NOT? IN in_predicate_value + ; + + in_predicate_value + : table_subquery + | LEFT_PAREN in_value_list RIGHT_PAREN + ; + + in_value_list + : row_value_predicand ( COMMA row_value_predicand )* + ; + + /* + =============================================================================== + 8.5, 8.6 + + Specify a pattern-matching comparison. + =============================================================================== + */ + + pattern_matching_predicate + : f=row_value_predicand pattern_matcher s=Character_String_Literal + ; + + pattern_matcher + : NOT? negativable_matcher + | regex_matcher + ; + + negativable_matcher + : LIKE + | ILIKE + | SIMILAR TO + | REGEXP + | RLIKE + ; + + regex_matcher + : Similar_To + | Not_Similar_To + | Similar_To_Case_Insensitive + | Not_Similar_To_Case_Insensitive + ; + + /* + =============================================================================== + 8.7 + + Specify a test for a null value. + =============================================================================== + */ + + null_predicate + : predicand=row_value_predicand IS (n=NOT)? NULL + ; + + /* + ============================================================================================== + 8.8 + + Specify a quantified comparison. + ============================================================================================== + */ + + quantified_comparison_predicate + : l=numeric_value_expression c=comp_op q=quantifier s=table_subquery + ; + + quantifier : all | some ; + + all : ALL; + + some : SOME | ANY; + + /* + ============================================================================================== + 8.9 + + Specify a test for a non_empty set. + ============================================================================================== + */ + + exists_predicate + : NOT? EXISTS s=table_subquery + ; + + + /* + ============================================================================================== + 8.10 + + Specify a test for the absence of duplicate rows + ============================================================================================== + */ + + unique_predicate + : UNIQUE s=table_subquery + ; + + /* + =============================================================================== + 10.1 + + Specify the precision of an interval data type. + =============================================================================== + */ + + primary_datetime_field + : non_second_primary_datetime_field + | SECOND + ; + + non_second_primary_datetime_field + : YEAR | MONTH | DAY | HOUR | MINUTE + ; + + extended_datetime_field + : CENTURY | DECADE | DOW | DOY | EPOCH | ISODOW | ISOYEAR | MICROSECONDS | MILLENNIUM | MILLISECONDS | QUARTER | WEEK + ; + + /* + =============================================================================== + 10.4 + + Invoke an SQL-invoked routine. + =============================================================================== + */ + + routine_invocation + : function_name LEFT_PAREN sql_argument_list? RIGHT_PAREN + ; + + function_names_for_reserved_words + : LEFT + | RIGHT + ; + + function_name + : identifier + | function_names_for_reserved_words + ; + + sql_argument_list + : value_expression (COMMA value_expression)* + ; + + /* + =============================================================================== + 14.1 + =============================================================================== + */ + + orderby_clause + : ORDER BY sort_specifier_list + ; + + sort_specifier_list + : sort_specifier (COMMA sort_specifier)* + ; + + sort_specifier + : key=row_value_predicand order=order_specification? null_order=null_ordering? + ; + + order_specification + : ASC + | DESC + ; + + limit_clause + : LIMIT e=numeric_value_expression + ; + + null_ordering + : NULL FIRST + | NULL LAST + ; + + /* + =============================================================================== + 14.8 + =============================================================================== + */ + + insert_statement + : INSERT (OVERWRITE)? INTO table_name (LEFT_PAREN column_name_list RIGHT_PAREN)? query_expression + | INSERT (OVERWRITE)? INTO LOCATION path=Character_String_Literal (USING file_type=identifier (param_clause)?)? query_expression + ; + + /* + =============================================================================== + + =============================================================================== + */ + + alter_tablespace_statement + : ALTER TABLESPACE space_name=identifier LOCATION uri=Character_String_Literal + ; + + alter_table_statement + : ALTER TABLE table_name RENAME TO table_name + | ALTER TABLE table_name RENAME COLUMN column_name TO column_name + | ALTER TABLE table_name ADD COLUMN field_element + ;