check that sharding_key exists in the table and has numeric type (#392)

* check that sharding_key exists in the table and has numeric type [#CLICKHOUSE-2191]

* style

* infer type of sharding expression [#CLICKHOUSE-2191]
This commit is contained in:
Artemkin Pavel 2017-01-27 02:47:33 +05:00 committed by alexey-milovidov
parent 20d6c20a72
commit 06d1509da1
2 changed files with 24 additions and 6 deletions

View File

@ -66,12 +66,11 @@ std::vector<IColumn::Filter> DistributedBlockOutputStream::createFilters(Block b
/// check that key column has valid type
const auto it = creators.find(key_column.type->getName());
return it != std::end(creators)
? (*it->second)(block.rows(), key_column.column.get(), *cluster)
: throw Exception{
"Sharding key expression does not evaluate to an integer type",
ErrorCodes::TYPE_MISMATCH
};
if (it == std::end(creators))
throw Exception{"Sharding key expression does not evaluate to an integer type",
ErrorCodes::TYPE_MISMATCH};
return (*it->second)(block.rows(), key_column.column.get(), *cluster);
}
void DistributedBlockOutputStream::writeSplit(const Block & block)

View File

@ -5,11 +5,13 @@
#include <DB/Common/StringUtils.h>
#include <DB/Parsers/ASTCreateQuery.h>
#include <DB/Parsers/ASTFunction.h>
#include <DB/Parsers/ASTIdentifier.h>
#include <DB/Parsers/ASTLiteral.h>
#include <DB/Interpreters/Context.h>
#include <DB/Interpreters/evaluateConstantExpression.h>
#include <DB/Interpreters/ExpressionAnalyzer.h>
#include <DB/Interpreters/getClusterName.h>
#include <DB/Storages/StorageLog.h>
@ -49,6 +51,7 @@ namespace ErrorCodes
extern const int UNKNOWN_ELEMENT_IN_CONFIG;
extern const int UNKNOWN_IDENTIFIER;
extern const int FUNCTION_CANNOT_HAVE_PARAMETERS;
extern const int TYPE_MISMATCH;
}
@ -464,6 +467,22 @@ StoragePtr StorageFactory::get(
const auto & sharding_key = args.size() == 4 ? args[3] : nullptr;
/// Check that sharding_key exists in the table and has numeric type.
if (sharding_key)
{
auto sharding_expr = ExpressionAnalyzer(sharding_key, context, nullptr, *columns).getActions(false);
const Block & block = sharding_expr->getSampleBlock();
if (block.columns() != 0)
{
auto type = block.getColumns().back().type;
if (!type->isNumeric())
throw Exception("Sharding expression has type " + type->getName() +
", but should be one of integer type", ErrorCodes::TYPE_MISMATCH);
}
}
return StorageDistributed::create(
table_name, columns,
materialized_columns, alias_columns, column_defaults,