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-12-10 23:56:57 +00:00
|
|
|
#include <Interpreters/ExpressionAnalyzer.h>
|
|
|
|
#include <Interpreters/TreeRewriter.h>
|
2020-08-17 10:11:50 +00:00
|
|
|
|
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
|
|
|
|
2020-09-14 19:25:02 +00:00
|
|
|
#include <Databases/MySQL/DatabaseMaterializeMySQL.h>
|
2020-12-10 23:56:57 +00:00
|
|
|
#include <Storages/SelectQueryInfo.h>
|
2020-09-14 19:25:02 +00:00
|
|
|
|
2020-06-12 04:21:43 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-09-15 13:30:30 +00:00
|
|
|
StorageMaterializeMySQL::StorageMaterializeMySQL(const StoragePtr & nested_storage_, const IDatabase * database_)
|
2020-10-14 12:19:29 +00:00
|
|
|
: StorageProxy(nested_storage_->getStorageID()), nested_storage(nested_storage_), database(database_)
|
2020-06-12 04:21:43 +00:00
|
|
|
{
|
2020-07-01 05:00:50 +00:00
|
|
|
StorageInMemoryMetadata in_memory_metadata;
|
2021-02-03 20:52:06 +00:00
|
|
|
in_memory_metadata = nested_storage->getInMemoryMetadata();
|
2020-07-01 05:00:50 +00:00
|
|
|
setInMemoryMetadata(in_memory_metadata);
|
2020-06-12 04:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 10:35:55 +00:00
|
|
|
bool StorageMaterializeMySQL::needRewriteQueryWithFinal(const Names & column_names) const
|
|
|
|
{
|
|
|
|
const StorageMetadataPtr & nested_metadata = nested_storage->getInMemoryMetadataPtr();
|
|
|
|
Block nested_header = nested_metadata->getSampleBlock();
|
|
|
|
ColumnWithTypeAndName & version_column = nested_header.getByPosition(nested_header.columns() - 1);
|
|
|
|
return std::find(column_names.begin(), column_names.end(), version_column.name) == column_names.end();
|
|
|
|
}
|
|
|
|
|
2020-09-15 13:30:30 +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-09-20 17:52:17 +00:00
|
|
|
SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2020-06-12 04:21:43 +00:00
|
|
|
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.
|
2020-09-15 13:30:30 +00:00
|
|
|
rethrowSyncExceptionIfNeed(database);
|
2020-08-06 11:56:49 +00:00
|
|
|
|
2020-06-24 05:28:27 +00:00
|
|
|
NameSet column_names_set = NameSet(column_names.begin(), column_names.end());
|
2021-04-10 23:33:54 +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);
|
2020-06-12 04:21:43 +00:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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-10-16 17:16:58 +00:00
|
|
|
pipe.addTableLock(lock);
|
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());
|
2021-04-20 12:38:02 +00:00
|
|
|
ExpressionActionsPtr expression_actions = ExpressionAnalyzer(expressions, syntax, context).getActions(true /* add_aliases */, false /* project_result */);
|
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
|
|
|
}
|
|
|
|
|
2020-09-15 13:30:30 +00:00
|
|
|
NamesAndTypesList StorageMaterializeMySQL::getVirtuals() const
|
2020-06-24 05:28:27 +00:00
|
|
|
{
|
2020-08-06 11:56:49 +00:00
|
|
|
/// If the background synchronization thread has exception.
|
2020-09-15 13:30:30 +00:00
|
|
|
rethrowSyncExceptionIfNeed(database);
|
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
|