2020-08-07 16:14:10 +00:00
|
|
|
#if !defined(ARCADIA_BUILD)
|
|
|
|
# include "config_core.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_MYSQL
|
|
|
|
|
2020-06-12 04:21:43 +00:00
|
|
|
#include <Storages/StorageMaterializeMySQL.h>
|
|
|
|
|
2020-08-17 10:11:50 +00:00
|
|
|
#include <Core/Settings.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
|
2020-06-23 17:45:55 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2020-06-12 04:21:43 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
|
2020-06-23 17:45:55 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
|
2020-06-12 04:21:43 +00:00
|
|
|
#include <Processors/Pipe.h>
|
2020-06-23 17:45:55 +00:00
|
|
|
#include <Processors/Transforms/FilterTransform.h>
|
2020-06-12 04:21:43 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-08-06 11:56:49 +00:00
|
|
|
StorageMaterializeMySQL::StorageMaterializeMySQL(const StoragePtr & nested_storage_, const DatabaseMaterializeMySQL * database_)
|
2020-10-14 10:59:29 +00:00
|
|
|
: IStorage(nested_storage_->getStorageID()), nested_storage(nested_storage_), database(database_)
|
2020-06-12 04:21:43 +00:00
|
|
|
{
|
2020-08-10 14:32:05 +00:00
|
|
|
auto nested_memory_metadata = nested_storage->getInMemoryMetadata();
|
2020-07-01 05:00:50 +00:00
|
|
|
StorageInMemoryMetadata in_memory_metadata;
|
2020-08-10 14:32:05 +00:00
|
|
|
in_memory_metadata.setColumns(nested_memory_metadata.getColumns());
|
2020-07-01 05:00:50 +00:00
|
|
|
setInMemoryMetadata(in_memory_metadata);
|
2020-06-12 04:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 05:28:26 +00:00
|
|
|
Pipe StorageMaterializeMySQL::read(
|
2020-06-12 04:21:43 +00:00
|
|
|
const Names & column_names,
|
2020-07-01 05:00:50 +00:00
|
|
|
const StorageMetadataPtr & /*metadata_snapshot*/,
|
2020-06-12 04:21:43 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum processed_stage,
|
|
|
|
size_t max_block_size,
|
|
|
|
unsigned int num_streams)
|
|
|
|
{
|
2020-08-06 11:56:49 +00:00
|
|
|
/// If the background synchronization thread has exception.
|
|
|
|
database->rethrowExceptionIfNeed();
|
|
|
|
|
2020-06-24 05:28:27 +00:00
|
|
|
NameSet column_names_set = NameSet(column_names.begin(), column_names.end());
|
2020-08-17 05:14:02 +00:00
|
|
|
auto lock = nested_storage->lockForShare(context.getCurrentQueryId(), context.getSettingsRef().lock_acquire_timeout);
|
2020-07-01 05:00:50 +00:00
|
|
|
const StorageMetadataPtr & nested_metadata = nested_storage->getInMemoryMetadataPtr();
|
2020-06-24 05:28:27 +00:00
|
|
|
|
2020-08-11 09:03:43 +00:00
|
|
|
Block nested_header = nested_metadata->getSampleBlock();
|
2020-06-24 05:28:27 +00:00
|
|
|
ColumnWithTypeAndName & sign_column = nested_header.getByPosition(nested_header.columns() - 2);
|
|
|
|
ColumnWithTypeAndName & version_column = nested_header.getByPosition(nested_header.columns() - 1);
|
|
|
|
|
|
|
|
if (ASTSelectQuery * select_query = query_info.query->as<ASTSelectQuery>(); select_query && !column_names_set.count(version_column.name))
|
2020-06-12 04:21:43 +00:00
|
|
|
{
|
|
|
|
auto & tables_in_select_query = select_query->tables()->as<ASTTablesInSelectQuery &>();
|
|
|
|
|
|
|
|
if (!tables_in_select_query.children.empty())
|
|
|
|
{
|
|
|
|
auto & tables_element = tables_in_select_query.children[0]->as<ASTTablesInSelectQueryElement &>();
|
|
|
|
|
|
|
|
if (tables_element.table_expression)
|
|
|
|
tables_element.table_expression->as<ASTTableExpression &>().final = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 05:28:27 +00:00
|
|
|
String filter_column_name;
|
2020-06-12 04:21:43 +00:00
|
|
|
Names require_columns_name = column_names;
|
2020-06-24 05:28:27 +00:00
|
|
|
ASTPtr expressions = std::make_shared<ASTExpressionList>();
|
|
|
|
if (column_names_set.empty() || !column_names_set.count(sign_column.name))
|
|
|
|
{
|
2020-06-12 04:21:43 +00:00
|
|
|
require_columns_name.emplace_back(sign_column.name);
|
|
|
|
|
2020-06-24 05:28:27 +00:00
|
|
|
const auto & sign_column_name = std::make_shared<ASTIdentifier>(sign_column.name);
|
|
|
|
const auto & fetch_sign_value = std::make_shared<ASTLiteral>(Field(Int8(1)));
|
|
|
|
|
|
|
|
expressions->children.emplace_back(makeASTFunction("equals", sign_column_name, fetch_sign_value));
|
|
|
|
filter_column_name = expressions->children.back()->getColumnName();
|
|
|
|
|
|
|
|
for (const auto & column_name : column_names)
|
|
|
|
expressions->children.emplace_back(std::make_shared<ASTIdentifier>(column_name));
|
|
|
|
}
|
|
|
|
|
2020-08-14 05:28:26 +00:00
|
|
|
Pipe pipe = nested_storage->read(require_columns_name, nested_metadata, query_info, context, processed_stage, max_block_size, num_streams);
|
2020-06-23 17:45:55 +00:00
|
|
|
|
2020-08-14 05:28:26 +00:00
|
|
|
if (!expressions->children.empty() && !pipe.empty())
|
2020-06-23 17:45:55 +00:00
|
|
|
{
|
2020-08-14 05:28:26 +00:00
|
|
|
Block pipe_header = pipe.getHeader();
|
2020-07-23 08:17:07 +00:00
|
|
|
auto syntax = TreeRewriter(context).analyze(expressions, pipe_header.getNamesAndTypesList());
|
2020-06-24 05:28:27 +00:00
|
|
|
ExpressionActionsPtr expression_actions = ExpressionAnalyzer(expressions, syntax, context).getActions(true);
|
2020-06-23 17:45:55 +00:00
|
|
|
|
2020-08-14 13:16:36 +00:00
|
|
|
pipe.addSimpleTransform([&](const Block & header)
|
|
|
|
{
|
|
|
|
return std::make_shared<FilterTransform>(header, expression_actions, filter_column_name, false);
|
|
|
|
});
|
2020-06-24 05:28:27 +00:00
|
|
|
}
|
2020-06-23 17:45:55 +00:00
|
|
|
|
2020-08-14 05:28:26 +00:00
|
|
|
return pipe;
|
2020-06-24 05:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NamesAndTypesList StorageMaterializeMySQL::getVirtuals() const
|
|
|
|
{
|
2020-08-06 11:56:49 +00:00
|
|
|
/// If the background synchronization thread has exception.
|
|
|
|
database->rethrowExceptionIfNeed();
|
2020-08-10 14:32:05 +00:00
|
|
|
return nested_storage->getVirtuals();
|
2020-06-12 04:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:39:24 +00:00
|
|
|
IStorage::ColumnSizeByName StorageMaterializeMySQL::getColumnSizes() const
|
|
|
|
{
|
|
|
|
auto sizes = nested_storage->getColumnSizes();
|
|
|
|
auto nested_header = nested_storage->getInMemoryMetadataPtr()->getSampleBlock();
|
|
|
|
String sign_column_name = nested_header.getByPosition(nested_header.columns() - 2).name;
|
|
|
|
String version_column_name = nested_header.getByPosition(nested_header.columns() - 1).name;
|
|
|
|
sizes.erase(sign_column_name);
|
|
|
|
sizes.erase(version_column_name);
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
2020-06-12 04:21:43 +00:00
|
|
|
}
|
2020-08-07 16:14:10 +00:00
|
|
|
|
|
|
|
#endif
|