Support Create Table As Syntax (#127)

* support create table as select statement

* Generate bison code

* add test
This commit is contained in:
d-justen 2019-06-07 11:42:49 +02:00 committed by mrks
parent 6003ab58d1
commit de4f81bb18
5 changed files with 1204 additions and 1160 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,9 @@
/* A Bison parser, made by GNU Bison 3.0.4. */ /* A Bison parser, made by GNU Bison 3.3.2. */
/* Bison interface for Yacc-like parsers in C /* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2019 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -30,6 +31,9 @@
This special exception was added by the Free Software Foundation in This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */ version 2.2 of Bison. */
/* Undocumented macros, especially those whose name start with YY_,
are private implementation details. Do not rely on them. */
#ifndef YY_HSQL_BISON_PARSER_H_INCLUDED #ifndef YY_HSQL_BISON_PARSER_H_INCLUDED
# define YY_HSQL_BISON_PARSER_H_INCLUDED # define YY_HSQL_BISON_PARSER_H_INCLUDED
/* Debug traces. */ /* Debug traces. */
@ -48,7 +52,7 @@
extern int hsql_debug; extern int hsql_debug;
#endif #endif
/* "%code requires" blocks. */ /* "%code requires" blocks. */
#line 35 "bison_parser.y" /* yacc.c:1909 */ #line 35 "bison_parser.y" /* yacc.c:1921 */
// %code requires block // %code requires block
@ -72,7 +76,7 @@ extern int hsql_debug;
} \ } \
} }
#line 76 "bison_parser.h" /* yacc.c:1909 */ #line 80 "bison_parser.h" /* yacc.c:1921 */
/* Token type. */ /* Token type. */
#ifndef HSQL_TOKENTYPE #ifndef HSQL_TOKENTYPE
@ -230,7 +234,7 @@ extern int hsql_debug;
union HSQL_STYPE union HSQL_STYPE
{ {
#line 95 "bison_parser.y" /* yacc.c:1909 */ #line 95 "bison_parser.y" /* yacc.c:1921 */
double fval; double fval;
int64_t ival; int64_t ival;
@ -274,7 +278,7 @@ union HSQL_STYPE
std::vector<hsql::OrderDescription*>* order_vec; std::vector<hsql::OrderDescription*>* order_vec;
std::vector<hsql::WithDescription*>* with_description_vec; std::vector<hsql::WithDescription*>* with_description_vec;
#line 278 "bison_parser.h" /* yacc.c:1909 */ #line 282 "bison_parser.h" /* yacc.c:1921 */
}; };
typedef union HSQL_STYPE HSQL_STYPE; typedef union HSQL_STYPE HSQL_STYPE;

View File

@ -430,6 +430,13 @@ create_statement:
$$->tableName = $4.name; $$->tableName = $4.name;
$$->columns = $6; $$->columns = $6;
} }
| CREATE TABLE opt_not_exists table_name AS select_statement {
$$ = new CreateStatement(kCreateTable);
$$->ifNotExists = $3;
$$->schema = $4.schema;
$$->tableName = $4.name;
$$->select = $6;
}
| CREATE VIEW opt_not_exists table_name opt_column_list AS select_statement { | CREATE VIEW opt_not_exists table_name opt_column_list AS select_statement {
$$ = new CreateStatement(kCreateView); $$ = new CreateStatement(kCreateView);
$$->ifNotExists = $3; $$->ifNotExists = $3;

View File

@ -26,6 +26,8 @@ CREATE TABLE "table" FROM TBL FILE 'students.tbl'
CREATE TABLE IF NOT EXISTS "table" FROM TBL FILE 'students.tbl' CREATE TABLE IF NOT EXISTS "table" FROM TBL FILE 'students.tbl'
CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE) CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)
CREATE TABLE teachers (name VARCHAR(30), student_number LONG, city CHAR(10), grade FLOAT) CREATE TABLE teachers (name VARCHAR(30), student_number LONG, city CHAR(10), grade FLOAT)
CREATE TABLE students_2 AS SELECT * FROM students
CREATE TABLE students_3 AS SELECT city, grade FROM students WHERE grade > 3.0
# Multiple statements # Multiple statements
CREATE TABLE "table" FROM TBL FILE 'students.tbl'; SELECT * FROM "table"; CREATE TABLE "table" FROM TBL FILE 'students.tbl'; SELECT * FROM "table";
# INSERT # INSERT

View File

@ -58,6 +58,24 @@ TEST(CreateStatementTest) {
ASSERT_EQ(stmt->columns->at(3)->nullable, false); ASSERT_EQ(stmt->columns->at(3)->nullable, false);
} }
TEST(CreateAsSelectStatementTest) {
SQLParserResult result;
SQLParser::parse("CREATE TABLE students_2 AS SELECT student_number, grade FROM students", &result);
ASSERT(result.isValid());
ASSERT_EQ(result.size(), 1);
ASSERT_EQ(result.getStatement(0)->type(), kStmtCreate);
const CreateStatement* stmt = (const CreateStatement*) result.getStatement(0);
ASSERT_EQ(stmt->type, kCreateTable);
ASSERT_STREQ(stmt->tableName, "students_2");
ASSERT_NULL(stmt->columns);
ASSERT_NOTNULL(stmt->select);
ASSERT(stmt->select->selectList->at(0)->isType(kExprColumnRef));
ASSERT_STREQ(stmt->select->selectList->at(0)->getName(), "student_number");
ASSERT(stmt->select->selectList->at(1)->isType(kExprColumnRef));
ASSERT_STREQ(stmt->select->selectList->at(1)->getName(), "grade");
}
TEST(UpdateStatementTest) { TEST(UpdateStatementTest) {
SQLParserResult result; SQLParserResult result;