dbms: cut coupling between dbms and dbms_functions. [#METR-12739]

This commit is contained in:
Andrey Mironov 2014-10-23 17:53:16 +04:00
parent 934149d59a
commit ec5aa734f9
9 changed files with 76 additions and 43 deletions

View File

@ -66,9 +66,6 @@ public:
void erase(const String & name);
/// Добавляет в блок недостающие столбцы со значениями по-умолчанию
void addDefaults(const NamesAndTypesList & required_columns);
void addDefaults(const NamesAndTypesList & required_columns,
const ColumnDefaults & column_defaults,
const Context & context);
ColumnWithNameAndType & getByPosition(size_t position);
const ColumnWithNameAndType & getByPosition(size_t position) const;

View File

@ -3,6 +3,7 @@
#include <Poco/SharedPtr.h>
#include <DB/DataStreams/IProfilingBlockInputStream.h>
#include <DB/Interpreters/evaluateMissingDefaults.h>
#include <DB/Columns/ColumnConst.h>
#include <DB/Storages/ColumnDefault.h>
@ -54,7 +55,8 @@ protected:
Block res = children.back()->read();
if (!res)
return res;
res.addDefaults(*required_columns, column_defaults, context);
evaluateMissingDefaults(res, *required_columns, column_defaults, context);
res.addDefaults(*required_columns);
return res;
}

View File

@ -7,6 +7,7 @@
#include <DB/Storages/ColumnDefault.h>
#include <DB/Interpreters/Context.h>
#include <DB/Interpreters/evaluateMissingDefaults.h>
namespace DB
@ -38,7 +39,8 @@ public:
void write(const Block & block) override
{
Block res = block;
res.addDefaults(*required_columns, column_defaults, context);
evaluateMissingDefaults(res, *required_columns, column_defaults, context);
res.addDefaults(*required_columns);
output->write(res);
}

View File

@ -0,0 +1,49 @@
#pragma once
#include <DB/Core/Block.h>
#include <DB/Storages/ColumnDefault.h>
#include <DB/Interpreters/ExpressionAnalyzer.h>
#include <utility>
namespace DB
{
inline void evaluateMissingDefaults(Block & block,
const NamesAndTypesList & required_columns,
const ColumnDefaults & column_defaults,
const Context & context)
{
if (column_defaults.empty())
return;
ASTPtr default_expr_list{stdext::make_unique<ASTExpressionList>().release()};
for (const auto & column : required_columns)
{
if (block.has(column.name))
continue;
const auto it = column_defaults.find(column.name);
/// expressions must be cloned to prevent modification by the ExpressionAnalyzer
if (it != column_defaults.end())
default_expr_list->children.emplace_back(
setAlias(it->second.expression->clone(), it->first));
}
/// nothing to evaluate
if (default_expr_list->children.empty())
return;
/** ExpressionAnalyzer eliminates "unused" columns, in order to ensure their safety
* we are going to operate on a copy instead of the original block */
Block copy_block{block};
/// evaluate default values for defaulted columns
ExpressionAnalyzer{default_expr_list, context, required_columns}.getActions(true)->execute(copy_block);
/// move evaluated columns to the original block
for (auto & column_name_type : copy_block.getColumns())
block.insert(std::move(column_name_type));
}
}

View File

@ -4,7 +4,6 @@
#include <DB/Parsers/formatAST.h>
#include <DB/Parsers/ExpressionListParsers.h>
#include <DB/IO/WriteBufferFromString.h>
#include <statdaemons/ext/range.hpp>
#include <unordered_map>
namespace DB

View File

@ -10,6 +10,7 @@
#include <DB/IO/CompressedReadBufferFromFile.h>
#include <DB/Columns/ColumnArray.h>
#include <DB/Columns/ColumnNested.h>
#include <DB/Interpreters/evaluateMissingDefaults.h>
namespace DB
@ -195,7 +196,7 @@ public:
}
/// evaluate defaulted columns
res.addDefaults(columns, storage.column_defaults, storage.context);
evaluateMissingDefaults(res, columns, storage.column_defaults, storage.context);
}
catch (const Exception & e)
{

View File

@ -148,6 +148,12 @@ public:
const ColumnDefaults & column_defaults_,
size_t max_compress_block_size_ = DEFAULT_MAX_COMPRESS_BLOCK_SIZE);
static StoragePtr create(
const std::string & path_,
const std::string & name_,
NamesAndTypesListPtr columns_,
size_t max_compress_block_size_ = DEFAULT_MAX_COMPRESS_BLOCK_SIZE);
std::string getName() const { return "Log"; }
std::string getTableName() const { return name; }

View File

@ -34,42 +34,6 @@ void Block::addDefaults(const NamesAndTypesList & required_columns)
insertDefault(column.name, column.type);
}
void Block::addDefaults(const NamesAndTypesList & required_columns,
const ColumnDefaults & column_defaults,
const Context & context)
{
ASTPtr default_expr_list{stdext::make_unique<ASTExpressionList>().release()};
for (const auto & column : required_columns)
{
if (has(column.name))
continue;
const auto it = column_defaults.find(column.name);
/// expressions must be cloned to prevent modification by the ExpressionAnalyzer
if (it == column_defaults.end())
insertDefault(column.name, column.type);
else
default_expr_list->children.emplace_back(
setAlias(it->second.expression->clone(), it->first));
}
/// nothing to evaluate
if (default_expr_list->children.empty())
return;
/** ExpressionAnalyzer eliminates "unused" columns, in order to ensure their safety
* we are going to operate on a copy instead of the original block */
Block copy_block{*this};
/// evaluate default values for defaulted columns
ExpressionAnalyzer{default_expr_list, context, required_columns}.getActions(true)->execute(copy_block);
/// move evaluated columns to the original block
for (auto & column_name_type : copy_block.getColumns())
insert(std::move(column_name_type));
}
Block & Block::operator= (const Block & other)
{
data = other.data;

View File

@ -450,6 +450,19 @@ StoragePtr StorageLog::create(
})->thisPtr();
}
StoragePtr StorageLog::create(
const std::string & path_,
const std::string & name_,
NamesAndTypesListPtr columns_,
size_t max_compress_block_size_)
{
return (new StorageLog{
path_, name_, columns_,
{}, {}, {},
max_compress_block_size_
})->thisPtr();
}
void StorageLog::addFile(const String & column_name, const IDataType & type, size_t level)
{