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::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_stmt> show_statement
%type <table_name> table_name
%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 <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
@ -218,6 +220,7 @@ int yyerror(YYLTYPE* llocp, SQLParserResult* result, yyscan_t scanner, const cha
%type <datetime_field> datetime_field
%type <column_t> column_def
%type <column_type_t> column_type
%type <index_data_structure_type_t> opt_index_type
%type <update_t> update_clause
%type <group_t> opt_group
%type <alias_t> 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<ColumnDefinition*>(); $$->push_back($1); }

View File

@ -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)

View File

@ -3,6 +3,7 @@
#include "SQLStatement.h"
#include "ColumnType.h"
#include "IndexType.h"
#include <ostream>
@ -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<ColumnDefinition*>* columns; // default: nullptr
std::vector<char*>* viewColumns;
SelectStatement* select;
IndexType indexType;
};
} // 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

@ -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) {};