Skip to content

Commit

Permalink
DRILL-6964: Implement CREATE / DROP SCHEMA commands
Browse files Browse the repository at this point in the history
Note: this PR only adds support for CREATE / DROP SCHEMA commands which allow to store and delete schema. Schema usage during querying the data will be covered in other PRs.

1. Added parser methods / handles to parse CREATE / DROP schema commands.
2. Added SchemaProviders classes to separate ways of schema provision (file, table function).
3. Added schema parsing using ANTLR4 (lexer, parser, visitors).
4. Added appropriate unit tests.

close #1615
  • Loading branch information
arina-ielchiieva authored and Aman Sinha committed Feb 1, 2019
1 parent b1594cb commit 0a61b8f
Show file tree
Hide file tree
Showing 36 changed files with 3,207 additions and 87 deletions.
21 changes: 21 additions & 0 deletions exec/java-exec/pom.xml
Expand Up @@ -545,6 +545,10 @@
<groupId>sqlline</groupId>
<artifactId>sqlline</artifactId>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
</dependencies>

<profiles>
Expand Down Expand Up @@ -794,6 +798,23 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>${antlr.version}</version>
<configuration>
<listener>false</listener>
<visitor>true</visitor>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down
@@ -0,0 +1,90 @@
lexer grammar SchemaLexer;

@header {
/*
* 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.
*/
}

// data types
// https://drill.apache.org/docs/supported-data-types/
INT: 'INT';
INTEGER: 'INTEGER';
BIGINT: 'BIGINT';

FLOAT: 'FLOAT';
DOUBLE: 'DOUBLE';

DEC: 'DEC';
DECIMAL: 'DECIMAL';
NUMERIC: 'NUMERIC';

BOOLEAN: 'BOOLEAN';

CHAR: 'CHAR';
CHARACTER: 'CHARACTER';
VARYING: 'VARYING';
VARCHAR: 'VARCHAR';
BINARY: 'BINARY';
VARBINARY: 'VARBINARY';

TIME: 'TIME';
DATE: 'DATE';
TIMESTAMP: 'TIMESTAMP';
INTERVAL: 'INTERVAL';

YEAR: 'YEAR';
MONTH: 'MONTH';
DAY: 'DAY';
HOUR: 'HOUR';
MINUTE: 'MINUTE';
SECOND: 'SECOND';

MAP: 'MAP';
ARRAY: 'ARRAY';

// symbols
COMMA: ',';
REVERSE_QUOTE: '`';
LEFT_PAREN: '(';
RIGHT_PAREN: ')';
LEFT_ANGLE_BRACKET: '<';
RIGHT_ANGLE_BRACKET: '>';

NOT: 'NOT';
NULL: 'NULL';
AS: 'AS';

NUMBER: [1-9] DIGIT* | '0';
fragment DIGIT: [0-9];

// identifiers

// column name can start with any letter, dollar sign ($) or underscore (_),
// consequently can contain any letter, dollar sign ($), underscore (_) or digit
// if any other symbols are present, use QUOTED_ID
ID: ([A-Z$_]) ([A-Z$_] | DIGIT)*;

// column name should be enclosed into backticks, can contain any symbols including space
// if contains backtick, it should be escaped with backslash (`a\\`b` -> a`b)
// if contains backslash, it should be escaped as well (`a\\\\b` -> a\b)
QUOTED_ID: REVERSE_QUOTE (~[`\\] | '\\' [`\\])* REVERSE_QUOTE;

// skip
LINE_COMMENT: '//' ~[\r\n]* -> skip;
BLOCK_COMMENT: '/*' .*? '*/' -> skip;
SPACE: [ \n\t\r\u000C]+ -> skip;
@@ -0,0 +1,72 @@
parser grammar SchemaParser;

options {
language=Java;
tokenVocab=SchemaLexer;
}

@header {
/*
* 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.
*/
}

schema: (columns | LEFT_PAREN columns RIGHT_PAREN) EOF;

columns: column (COMMA column)*;

column: (primitive_column | map_column | simple_array_column | complex_array_column);

primitive_column: column_id simple_type nullability?;

simple_array_column: column_id simple_array_type nullability?;

map_column: column_id map_type nullability?;

complex_array_column: column_id complex_array_type nullability?;

column_id
: ID # id
| QUOTED_ID # quoted_id
;

simple_type
: (INT | INTEGER) # int
| BIGINT # bigint
| FLOAT # float
| DOUBLE # double
| (DEC | DECIMAL | NUMERIC) (LEFT_PAREN NUMBER (COMMA NUMBER)? RIGHT_PAREN)? # decimal
| BOOLEAN # boolean
| (CHAR | VARCHAR | CHARACTER VARYING?) (LEFT_PAREN NUMBER RIGHT_PAREN)? # varchar
| (BINARY | VARBINARY) (LEFT_PAREN NUMBER RIGHT_PAREN)? # binary
| TIME (LEFT_PAREN NUMBER RIGHT_PAREN)? # time
| DATE # date
| TIMESTAMP (LEFT_PAREN NUMBER RIGHT_PAREN)? # timestamp
| INTERVAL (YEAR | MONTH) # interval_year
| INTERVAL (DAY | HOUR | MINUTE | SECOND) # interval_day
| INTERVAL # interval
;

complex_type: (simple_array_type | complex_array_type);

simple_array_type: ARRAY LEFT_ANGLE_BRACKET (simple_type | map_type) RIGHT_ANGLE_BRACKET;

complex_array_type: ARRAY LEFT_ANGLE_BRACKET complex_type RIGHT_ANGLE_BRACKET;

map_type: MAP LEFT_ANGLE_BRACKET columns RIGHT_ANGLE_BRACKET;

nullability: NOT NULL;
9 changes: 4 additions & 5 deletions exec/java-exec/src/main/codegen/data/Parser.tdd
Expand Up @@ -36,7 +36,8 @@
"REFRESH",
"METADATA",
"IF",
"JAR"
"JAR",
"PROPERTIES"
]

# List of methods for parsing custom SQL statements.
Expand All @@ -46,11 +47,9 @@
"SqlDescribeSchema()"
"SqlDescribeTable()",
"SqlUseSchema()",
"SqlCreateOrReplaceView()",
"SqlDropView()",
"SqlCreateOrReplace()"
"SqlDrop()",
"SqlShowFiles()",
"SqlCreateTable()",
"SqlDropTable()",
"SqlRefreshMetadata()",
"SqlCreateFunction()",
"SqlDropFunction()"
Expand Down

0 comments on commit 0a61b8f

Please sign in to comment.