Added create index

This commit is contained in:
Jan Mühlig 2020-10-22 09:49:28 +02:00
parent a906cc80bb
commit c2f5ba9857
5 changed files with 91 additions and 2 deletions

View File

@ -123,6 +123,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
hsql::LimitDescription* limit; hsql::LimitDescription* limit;
hsql::ColumnDefinition* column_t; hsql::ColumnDefinition* column_t;
hsql::ColumnType column_type_t; hsql::ColumnType column_type_t;
hsql::IndexDataStructureType index_data_structure_type_t;
hsql::ImportType import_type_t; hsql::ImportType import_type_t;
hsql::GroupByDescription* group_t; hsql::GroupByDescription* group_t;
hsql::UpdateClause* update_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 ARRAY CONCAT ILIKE SECOND MINUTE HOUR DAY MONTH YEAR
%token TRUE FALSE %token TRUE FALSE
%token TRANSACTION BEGIN COMMIT ROLLBACK %token TRANSACTION BEGIN COMMIT ROLLBACK
%token BTREE HASHTABLE SKIPLIST
/********************************* /*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html) ** 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_stmt> show_statement %type <show_stmt> show_statement
%type <table_name> table_name %type <table_name> table_name
%type <sval> file_path prepare_target_query %type <sval> file_path prepare_target_query
%type <bval> opt_not_exists opt_exists opt_distinct opt_column_nullable opt_all %type <bval> opt_not_exists opt_exists opt_distinct opt_column_nullable opt_all opt_unique
%type <uval> opt_join_type %type <uval> opt_join_type
%type <table> opt_from_clause from_clause table_ref table_ref_atomic table_ref_name nonjoin_table_ref_atomic %type <table> opt_from_clause from_clause table_ref table_ref_atomic table_ref_name nonjoin_table_ref_atomic
%type <table> join_clause table_ref_name_no_alias %type <table> 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> datetime_field %type <datetime_field> datetime_field
%type <column_t> column_def %type <column_t> column_def
%type <column_type_t> column_type %type <column_type_t> column_type
%type <index_data_structure_type_t> opt_index_type
%type <update_t> update_clause %type <update_t> update_clause
%type <group_t> opt_group %type <group_t> opt_group
%type <alias_t> opt_table_alias table_alias opt_alias alias %type <alias_t> opt_table_alias table_alias opt_alias alias
@ -534,6 +537,15 @@ create_statement:
$$->viewColumns = $5; $$->viewColumns = $5;
$$->select = $7; $$->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: opt_not_exists:
@ -541,6 +553,19 @@ opt_not_exists:
| /* empty */ { $$ = false; } | /* 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_commalist:
column_def { $$ = new std::vector<ColumnDefinition*>(); $$->push_back($1); } column_def { $$ = new std::vector<ColumnDefinition*>(); $$->push_back($1); }
| column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; } | column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; }

View File

@ -197,6 +197,9 @@ TRANSACTION TOKEN(TRANSACTION)
BEGIN TOKEN(BEGIN) BEGIN TOKEN(BEGIN)
ROLLBACK TOKEN(ROLLBACK) ROLLBACK TOKEN(ROLLBACK)
COMMIT TOKEN(COMMIT) COMMIT TOKEN(COMMIT)
BTREE TOKEN(BTREE)
HASHTABLE TOKEN(HASHTABLE)
SKIPLIST TOKEN(SKIPLIST)
/* Allow =/== see https://sqlite.org/lang_expr.html#collateop */ /* Allow =/== see https://sqlite.org/lang_expr.html#collateop */
"==" TOKEN(EQUALS) "==" TOKEN(EQUALS)

View File

@ -3,6 +3,7 @@
#include "SQLStatement.h" #include "SQLStatement.h"
#include "ColumnType.h" #include "ColumnType.h"
#include "IndexType.h"
#include <ostream> #include <ostream>
@ -23,7 +24,8 @@ namespace hsql {
enum CreateType { enum CreateType {
kCreateTable, kCreateTable,
kCreateTableFromTbl, // Hyrise file format kCreateTableFromTbl, // Hyrise file format
kCreateView kCreateView,
kCreateIndex
}; };
// Represents SQL Create statements. // Represents SQL Create statements.
@ -37,9 +39,11 @@ namespace hsql {
char* filePath; // default: nullptr char* filePath; // default: nullptr
char* schema; // default: nullptr char* schema; // default: nullptr
char* tableName; // default: nullptr char* tableName; // default: nullptr
char* indexName; // default: nullptr
std::vector<ColumnDefinition*>* columns; // default: nullptr std::vector<ColumnDefinition*>* columns; // default: nullptr
std::vector<char*>* viewColumns; std::vector<char*>* viewColumns;
SelectStatement* select; SelectStatement* select;
IndexType indexType;
}; };
} // namespace hsql } // namespace hsql

29
src/sql/IndexType.h Executable file
View File

@ -0,0 +1,29 @@
#ifndef SQLPARSER_INDEX_TYPE_H
#define SQLPARSER_INDEX_TYPE_H
#include <ostream>
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

View File

@ -64,6 +64,33 @@ namespace hsql {
return stream; 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 // CreateStatemnet
CreateStatement::CreateStatement(CreateType type) : CreateStatement::CreateStatement(CreateType type) :
@ -73,6 +100,7 @@ namespace hsql {
filePath(nullptr), filePath(nullptr),
schema(nullptr), schema(nullptr),
tableName(nullptr), tableName(nullptr),
indexName(nullptr),
columns(nullptr), columns(nullptr),
viewColumns(nullptr), viewColumns(nullptr),
select(nullptr) {}; select(nullptr) {};