mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 23:21:59 +00:00
Fix database engine attach, more tests
This commit is contained in:
parent
d250d79e33
commit
18c2abaaf9
@ -4,7 +4,10 @@
|
||||
|
||||
#include <common/logger_useful.h>
|
||||
#include <DataTypes/DataTypesNumber.h>
|
||||
#include <DataTypes/DataTypeNullable.h>
|
||||
#include <Databases/SQLite/fetchSQLiteTableStructure.h>
|
||||
#include <Parsers/ASTCreateQuery.h>
|
||||
#include <Parsers/ASTColumnDeclaration.h>
|
||||
#include <Interpreters/Context.h>
|
||||
#include <Storages/StorageSQLite.h>
|
||||
|
||||
@ -136,6 +139,7 @@ StoragePtr DatabaseSQLite::fetchTable(const String & table_name, ContextPtr loca
|
||||
return storage;
|
||||
}
|
||||
|
||||
|
||||
ASTPtr DatabaseSQLite::getCreateDatabaseQuery() const
|
||||
{
|
||||
const auto & create_query = std::make_shared<ASTCreateQuery>();
|
||||
@ -144,6 +148,62 @@ ASTPtr DatabaseSQLite::getCreateDatabaseQuery() const
|
||||
return create_query;
|
||||
}
|
||||
|
||||
|
||||
ASTPtr DatabaseSQLite::getCreateTableQueryImpl(const String & table_name, ContextPtr local_context, bool throw_on_error) const
|
||||
{
|
||||
auto storage = fetchTable(table_name, local_context, false);
|
||||
if (!storage)
|
||||
{
|
||||
if (throw_on_error)
|
||||
throw Exception(ErrorCodes::UNKNOWN_TABLE, "SQLite table {}.{} does not exist", database_name, table_name);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto create_table_query = std::make_shared<ASTCreateQuery>();
|
||||
auto table_storage_define = database_engine_define->clone();
|
||||
create_table_query->set(create_table_query->storage, table_storage_define);
|
||||
|
||||
auto columns_declare_list = std::make_shared<ASTColumns>();
|
||||
auto columns_expression_list = std::make_shared<ASTExpressionList>();
|
||||
|
||||
columns_declare_list->set(columns_declare_list->columns, columns_expression_list);
|
||||
create_table_query->set(create_table_query->columns_list, columns_declare_list);
|
||||
|
||||
/// init create query.
|
||||
auto table_id = storage->getStorageID();
|
||||
create_table_query->table = table_id.table_name;
|
||||
create_table_query->database = table_id.database_name;
|
||||
|
||||
auto metadata_snapshot = storage->getInMemoryMetadataPtr();
|
||||
for (const auto & column_type_and_name : metadata_snapshot->getColumns().getOrdinary())
|
||||
{
|
||||
const auto & column_declaration = std::make_shared<ASTColumnDeclaration>();
|
||||
column_declaration->name = column_type_and_name.name;
|
||||
column_declaration->type = getColumnDeclaration(column_type_and_name.type);
|
||||
columns_expression_list->children.emplace_back(column_declaration);
|
||||
}
|
||||
|
||||
ASTStorage * ast_storage = table_storage_define->as<ASTStorage>();
|
||||
ASTs storage_children = ast_storage->children;
|
||||
auto storage_engine_arguments = ast_storage->engine->arguments;
|
||||
|
||||
/// Add table_name to engine arguments
|
||||
storage_engine_arguments->children.insert(storage_engine_arguments->children.begin() + 1, std::make_shared<ASTLiteral>(table_id.table_name));
|
||||
|
||||
return create_table_query;
|
||||
}
|
||||
|
||||
|
||||
ASTPtr DatabaseSQLite::getColumnDeclaration(const DataTypePtr & data_type) const
|
||||
{
|
||||
WhichDataType which(data_type);
|
||||
|
||||
if (which.isNullable())
|
||||
return makeASTFunction("Nullable", getColumnDeclaration(typeid_cast<const DataTypeNullable *>(data_type.get())->getNestedType()));
|
||||
|
||||
return std::make_shared<ASTIdentifier>(data_type->getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -41,6 +41,9 @@ public:
|
||||
|
||||
void shutdown() override {}
|
||||
|
||||
protected:
|
||||
ASTPtr getCreateTableQueryImpl(const String & table_name, ContextPtr context, bool throw_on_error) const override;
|
||||
|
||||
private:
|
||||
ASTPtr database_engine_define;
|
||||
|
||||
@ -53,6 +56,8 @@ private:
|
||||
NameSet fetchTablesList() const;
|
||||
|
||||
StoragePtr fetchTable(const String & table_name, ContextPtr context, bool table_checked) const;
|
||||
|
||||
ASTPtr getColumnDeclaration(const DataTypePtr & data_type) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,29 @@
|
||||
1 hi
|
||||
2 bye
|
||||
1
|
||||
2
|
||||
hi
|
||||
bye
|
||||
show database tables:
|
||||
table1
|
||||
table2
|
||||
describe table:
|
||||
col1 Nullable(String)
|
||||
col2 Nullable(Int64)
|
||||
col1 Nullable(Int64)
|
||||
col2 Nullable(String)
|
||||
describe table:
|
||||
CREATE TABLE SQLite.table1\n(\n `col1` Nullable(String),\n `col2` Nullable(Int64)\n)\nENGINE = SQLite
|
||||
CREATE TABLE SQLite.table2\n(\n `col1` Nullable(Int64),\n `col2` Nullable(String)\n)\nENGINE = SQLite
|
||||
select *:
|
||||
line1 1
|
||||
line2 2
|
||||
line3 3
|
||||
1 text1
|
||||
2 text2
|
||||
3 text3
|
||||
test insert:
|
||||
line4 4
|
||||
4 text4
|
||||
after delete:
|
||||
after detach:
|
||||
line1 1
|
||||
line2 2
|
||||
line3 3
|
||||
1 text1
|
||||
2 text2
|
||||
3 text3
|
||||
|
@ -4,20 +4,51 @@ CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
# shellcheck source=../shell_config.sh
|
||||
. "$CUR_DIR"/../shell_config.sh
|
||||
|
||||
DATA_FILE=$CUR_DIR/data_sqlite/test.sqlite3
|
||||
DATA_FILE=$CUR_DIR/data_sqlite/db1
|
||||
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='DROP DATABASE IF EXISTS sqlite_database'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query="CREATE DATABASE sqlite_database ENGINE = SQLite('${DATA_FILE}')"
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.`Some table`'
|
||||
${CLICKHOUSE_CLIENT} --query="select 'show database tables:'";
|
||||
${CLICKHOUSE_CLIENT} --query='SHOW TABLES FROM sqlite_database;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT `some field` FROM sqlite_database.`Some table`;'
|
||||
${CLICKHOUSE_CLIENT} --query="select 'describe table:'";
|
||||
${CLICKHOUSE_CLIENT} --query='DESCRIBE TABLE sqlite_database.table1;'
|
||||
${CLICKHOUSE_CLIENT} --query='DESCRIBE TABLE sqlite_database.table2;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT `string field` FROM sqlite_database.`Some table`;'
|
||||
${CLICKHOUSE_CLIENT} --query="select 'describe table:'";
|
||||
${CLICKHOUSE_CLIENT} --query='SHOW CREATE TABLE sqlite_database.table1;' | sed -r 's/(.*SQLite)(.*)/\1/'
|
||||
${CLICKHOUSE_CLIENT} --query='SHOW CREATE TABLE sqlite_database.table2;' | sed -r 's/(.*SQLite)(.*)/\1/'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.`Empty table`;'
|
||||
${CLICKHOUSE_CLIENT} --query="select 'select *:'";
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table1 ORDER BY col2'
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table2 ORDER BY col1;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT field2 FROM sqlite_database.`Empty table`;'
|
||||
${CLICKHOUSE_CLIENT} --query="select 'test insert:'";
|
||||
${CLICKHOUSE_CLIENT} --query="INSERT INTO sqlite_database.table1 VALUES ('line4', 4);"
|
||||
${CLICKHOUSE_CLIENT} --query="INSERT INTO sqlite_database.table2 VALUES (4, 'text4');"
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table1 WHERE col2 > 3;'
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table2 WHERE col1 > 3;'
|
||||
|
||||
sqlite3 data_sqlite/db1 'DELETE FROM table1 WHERE col2 > 3'
|
||||
sqlite3 data_sqlite/db1 'DELETE FROM table2 WHERE col1 > 3'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query="select 'after delete:'";
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table1 WHERE col2 > 3;'
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table2 WHERE col1 > 3;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query="select 'after detach:'";
|
||||
${CLICKHOUSE_CLIENT} --query='DETACH DATABASE sqlite_database;'
|
||||
${CLICKHOUSE_CLIENT} --query='ATTACH DATABASE sqlite_database;'
|
||||
#${CLICKHOUSE_CLIENT} --query='DETACH TABLE sqlite_database.table1;'
|
||||
#${CLICKHOUSE_CLIENT} --query='DETACH TABLE sqlite_database.table2;'
|
||||
#${CLICKHOUSE_CLIENT} --query='ATTACH TABLE sqlite_database.table1;'
|
||||
#${CLICKHOUSE_CLIENT} --query='ATTACH TABLE sqlite_database.table2;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table1 ORDER BY col2'
|
||||
${CLICKHOUSE_CLIENT} --query='SELECT * FROM sqlite_database.table2 ORDER BY col1;'
|
||||
|
||||
${CLICKHOUSE_CLIENT} --query='DROP DATABASE IF EXISTS sqlite_database;'
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user