support alter on storagehive

This commit is contained in:
lgbo-ustc 2022-06-20 12:19:50 +08:00
parent 1a67740cd3
commit efec6f9ac5
3 changed files with 86 additions and 0 deletions

View File

@ -31,6 +31,7 @@
#include <Processors/Formats/IInputFormat.h>
#include <Processors/Executors/PullingPipelineExecutor.h>
#include <Processors/Transforms/AddingDefaultsTransform.h>
#include <Storages/AlterCommands.h>
#include <Storages/HDFS/ReadBufferFromHDFS.h>
#include <Storages/HDFS/AsynchronousReadBufferFromHDFS.h>
#include <Storages/Hive/HiveSettings.h>
@ -889,6 +890,28 @@ std::optional<UInt64> StorageHive::totalRowsByPartitionPredicate(const SelectQue
return totalRowsImpl(context_->getSettingsRef(), query_info, context_, PruneLevel::Partition);
}
void StorageHive::checkAlterIsPossible(const AlterCommands & commands, ContextPtr /*local_context*/) const
{
for (const auto & command : commands)
{
if (command.type != AlterCommand::Type::ADD_COLUMN && command.type != AlterCommand::Type::MODIFY_COLUMN
&& command.type != AlterCommand::Type::DROP_COLUMN && command.type != AlterCommand::Type::COMMENT_COLUMN
&& command.type != AlterCommand::Type::COMMENT_TABLE)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Alter of type '{}' is not supported by storage {}", command.type, getName());
}
}
void StorageHive::alter(const AlterCommands & params, ContextPtr local_context, AlterLockHolder & /*alter_lock_holder*/)
{
auto table_id = getStorageID();
checkAlterIsPossible(params, local_context);
auto metadata_snapshot = getInMemoryMetadataPtr();
StorageInMemoryMetadata new_metadata = *metadata_snapshot;
params.apply(new_metadata, local_context);
DatabaseCatalog::instance().getDatabase(table_id.database_name)->alterTable(local_context, table_id, new_metadata);
setInMemoryMetadata(new_metadata);
}
std::optional<UInt64>
StorageHive::totalRowsImpl(const Settings & settings, const SelectQueryInfo & query_info, ContextPtr context_, PruneLevel prune_level) const
{

View File

@ -67,6 +67,8 @@ public:
std::optional<UInt64> totalRows(const Settings & settings) const override;
std::optional<UInt64> totalRowsByPartitionPredicate(const SelectQueryInfo & query_info, ContextPtr context_) const override;
void checkAlterIsPossible(const AlterCommands & commands, ContextPtr local_context) const override;
void alter(const AlterCommands & params, ContextPtr local_context, AlterLockHolder & alter_lock_holder) override;
private:
using FileFormat = IHiveFile::FileFormat;

View File

@ -400,3 +400,64 @@ def test_cache_dir_use(started_cluster):
["bash", "-c", "ls /tmp/clickhouse_local_cache1 | wc -l"]
)
assert result0 != "0" and result1 != "0"
def test_table_alter_add(started_cluster):
node = started_cluster.instances["h0_0_0"]
result = node.query("DROP TABLE IF EXISTS default.demo_parquet_1")
result = node.query(
"""
CREATE TABLE IF NOT EXISTS default.demo_parquet_1 (`score` Nullable(Int32), `day` Nullable(String)) ENGINE = Hive('thrift://hivetest:9083', 'test', 'demo') PARTITION BY(day)
"""
)
result = node.query(
"""
ALTER TABLE default.demo_parquet_1 ADD COLUMN id Nullable(String) FIRST
"""
)
result = node.query(
"""DESC default.demo_parquet_1 FORMAT TSV"""
)
expected_result = "id\tNullable(String)\t\t\t\t\t\nscore\tNullable(Int32)\t\t\t\t\t\nday\tNullable(String)"
assert result.strip() == expected_result
def test_table_alter_drop(started_cluster):
node = started_cluster.instances["h0_0_0"]
result = node.query("DROP TABLE IF EXISTS default.demo_parquet_1")
result = node.query(
"""
CREATE TABLE IF NOT EXISTS default.demo_parquet_1 (`id` Nullable(String), `score` Nullable(Int32), `day` Nullable(String)) ENGINE = Hive('thrift://hivetest:9083', 'test', 'demo') PARTITION BY(day)
"""
)
result = node.query(
"""
ALTER TABLE default.demo_parquet_1 DROP COLUMN id
"""
)
result = node.query(
"""DESC default.demo_parquet_1 FORMAT TSV"""
)
expected_result = """score\tNullable(Int32)\t\t\t\t\t\nday\tNullable(String)"""
assert result.strip() == expected_result
def test_table_alter_comment(started_cluster):
node = started_cluster.instances["h0_0_0"]
result = node.query("DROP TABLE IF EXISTS default.demo_parquet_1")
result = node.query(
"""
CREATE TABLE IF NOT EXISTS default.demo_parquet_1 (`id` Nullable(String), `score` Nullable(Int32), `day` Nullable(String)) ENGINE = Hive('thrift://hivetest:9083', 'test', 'demo') PARTITION BY(day)
"""
)
result = node.query(
"""ALTER TABLE default.demo_parquet_1 COMMENT COLUMN id 'Text comment'"""
)
result = node.query(
"""DESC default.demo_parquet_1 FORMAT TSV"""
)
expected_result = """id\tNullable(String)\t\t\tText comment\t\t\nscore\tNullable(Int32)\t\t\t\t\t\nday\tNullable(String)"""
assert result.strip() == expected_result