From c2f5ba9857ecd932e4b7e24ed1f6eedb854cd759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=BChlig?= Date: Thu, 22 Oct 2020 09:49:28 +0200 Subject: [PATCH] Added create index --- src/parser/bison_parser.y | 27 ++++++++++++++++++++++++++- src/parser/flex_lexer.l | 3 +++ src/sql/CreateStatement.h | 6 +++++- src/sql/IndexType.h | 29 +++++++++++++++++++++++++++++ src/sql/statements.cpp | 28 ++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100755 src/sql/IndexType.h diff --git a/src/parser/bison_parser.y b/src/parser/bison_parser.y index 8d92e0d..bbe959a 100755 --- a/src/parser/bison_parser.y +++ b/src/parser/bison_parser.y @@ -123,6 +123,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha hsql::LimitDescription* limit; hsql::ColumnDefinition* column_t; hsql::ColumnType column_type_t; + hsql::IndexDataStructureType index_data_structure_type_t; hsql::ImportType import_type_t; hsql::GroupByDescription* group_t; hsql::UpdateClause* update_t; @@ -183,6 +184,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha %token ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR %token TRUE FALSE %token TRANSACTION BEGIN COMMIT ROLLBACK +%token BTREE HASHTABLE SKIPLIST /********************************* ** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html) @@ -203,7 +205,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha %type show_statement %type table_name %type file_path prepare_target_query -%type opt_not_exists opt_exists opt_distinct opt_column_nullable opt_all +%type opt_not_exists opt_exists opt_distinct opt_column_nullable opt_all opt_unique %type opt_join_type %type opt_from_clause from_clause table_ref table_ref_atomic table_ref_name nonjoin_table_ref_atomic %type
join_clause table_ref_name_no_alias @@ -218,6 +220,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha %type datetime_field %type column_def %type column_type +%type opt_index_type %type update_clause %type opt_group %type opt_table_alias table_alias opt_alias alias @@ -534,12 +537,34 @@ create_statement: $$->viewColumns = $5; $$->select = $7; } + | CREATE opt_unique INDEX opt_not_exists table_name ON table_name opt_column_list opt_index_type { + $$ = new CreateStatement(kCreateIndex); + $$->ifNotExists = $4; + $$->schema = $7.schema; + $$->tableName = $7.name; + $$->viewColumns = $8; + $$->indexName = $5.name; + $$->indexType = IndexType($9, $2); + } ; opt_not_exists: IF NOT EXISTS { $$ = true; } | /* empty */ { $$ = false; } ; + +opt_unique: + UNIQUE { $$ = true; } + | /* empty */ { $$ = false; } + ; + +opt_index_type: + USING BTREE { $$ = IndexDataStructureType::BTREE; } + | USING HASHTABLE { $$ = IndexDataStructureType::HASHTABLE; } + | USING SKIPLIST { $$ = IndexDataStructureType::SKIPLIST; } + | /* empty */ { $$ = IndexDataStructureType::BTREE; } + ; + column_def_commalist: column_def { $$ = new std::vector(); $$->push_back($1); } diff --git a/src/parser/flex_lexer.l b/src/parser/flex_lexer.l index 48710e5..60af4a2 100644 --- a/src/parser/flex_lexer.l +++ b/src/parser/flex_lexer.l @@ -197,6 +197,9 @@ TRANSACTION TOKEN(TRANSACTION) BEGIN TOKEN(BEGIN) ROLLBACK TOKEN(ROLLBACK) COMMIT TOKEN(COMMIT) +BTREE TOKEN(BTREE) +HASHTABLE TOKEN(HASHTABLE) +SKIPLIST TOKEN(SKIPLIST) /* Allow =/== see https://sqlite.org/lang_expr.html#collateop */ "==" TOKEN(EQUALS) diff --git a/src/sql/CreateStatement.h b/src/sql/CreateStatement.h index e88324f..50c9e68 100755 --- a/src/sql/CreateStatement.h +++ b/src/sql/CreateStatement.h @@ -3,6 +3,7 @@ #include "SQLStatement.h" #include "ColumnType.h" +#include "IndexType.h" #include @@ -23,7 +24,8 @@ namespace hsql { enum CreateType { kCreateTable, kCreateTableFromTbl, // Hyrise file format - kCreateView + kCreateView, + kCreateIndex }; // Represents SQL Create statements. @@ -37,9 +39,11 @@ namespace hsql { char* filePath; // default: nullptr char* schema; // default: nullptr char* tableName; // default: nullptr + char* indexName; // default: nullptr std::vector* columns; // default: nullptr std::vector* viewColumns; SelectStatement* select; + IndexType indexType; }; } // namespace hsql diff --git a/src/sql/IndexType.h b/src/sql/IndexType.h new file mode 100755 index 0000000..dd1eddc --- /dev/null +++ b/src/sql/IndexType.h @@ -0,0 +1,29 @@ +#ifndef SQLPARSER_INDEX_TYPE_H +#define SQLPARSER_INDEX_TYPE_H + +#include + +namespace hsql { + enum class IndexDataStructureType { + BTREE, + HASHTABLE, + SKIPLIST + }; + + struct IndexType { + IndexType() = default; + IndexType(IndexDataStructureType data_structure_type_, bool is_unique_) + : data_structure_type(data_structure_type_), is_unique(is_unique_) { } + IndexType& operator=(const IndexType&) = default; + ~IndexType() = default; + IndexDataStructureType data_structure_type; + bool is_unique; + }; + + bool operator==(const IndexType& lhs, const IndexType& rhs); + bool operator!=(const IndexType& lhs, const IndexType& rhs); + std::ostream& operator<<(std::ostream&, const IndexType&); + +} // namespace hsql + +#endif diff --git a/src/sql/statements.cpp b/src/sql/statements.cpp index 6b422c3..da7762b 100755 --- a/src/sql/statements.cpp +++ b/src/sql/statements.cpp @@ -63,6 +63,33 @@ namespace hsql { } return stream; } + + // IndexDefinition + bool operator==(const IndexType& lhs, const IndexType& rhs) { + return lhs.data_structure_type == rhs.data_structure_type && lhs.is_unique == rhs.is_unique; + } + + bool operator!=(const IndexType& lhs, const IndexType& rhs) { + return !(lhs == rhs); + } + + std::ostream& operator<<(std::ostream& stream, const IndexType& index_type) { + if (index_type.is_unique) { + stream << "UNIQUE "; + } + switch (index_type.data_structure_type) { + case IndexDataStructureType::BTREE: + stream << "BTREE"; + break; + case IndexDataStructureType::HASHTABLE: + stream << "HASHTABLE"; + break; + case IndexDataStructureType::SKIPLIST: + stream << "SKIPLIST"; + break; + } + return stream; + } // CreateStatemnet @@ -73,6 +100,7 @@ namespace hsql { filePath(nullptr), schema(nullptr), tableName(nullptr), + indexName(nullptr), columns(nullptr), viewColumns(nullptr), select(nullptr) {};