2018-07-18 12:17:48 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Storages/IStorage.h>
|
2019-10-10 16:30:30 +00:00
|
|
|
#include <Storages/MergeTree/IMergeTreeDataPart.h>
|
2018-07-18 12:17:48 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreeDataSelectExecutor.h>
|
2022-06-01 10:57:20 +00:00
|
|
|
#include <DataTypes/ObjectUtils.h>
|
2020-10-01 17:34:22 +00:00
|
|
|
#include <Processors/QueryPlan/QueryPlan.h>
|
2021-03-04 17:38:12 +00:00
|
|
|
#include <Processors/QueryPlan/Optimizations/QueryPlanOptimizationSettings.h>
|
|
|
|
#include <Processors/QueryPlan/BuildQueryPipelineSettings.h>
|
2021-10-16 14:03:50 +00:00
|
|
|
#include <QueryPipeline/QueryPipelineBuilder.h>
|
2018-07-18 12:17:48 +00:00
|
|
|
#include <Core/Defines.h>
|
2022-07-22 23:14:03 +00:00
|
|
|
#include <Common/Exception.h>
|
2018-07-18 12:17:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-07-22 23:14:03 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:17:48 +00:00
|
|
|
/// A Storage that allows reading from a single MergeTree data part.
|
2022-05-03 06:43:28 +00:00
|
|
|
class StorageFromMergeTreeDataPart final : public IStorage
|
2018-07-18 12:17:48 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-19 20:47:29 +00:00
|
|
|
/// Used in part mutation.
|
|
|
|
explicit StorageFromMergeTreeDataPart(const MergeTreeData::DataPartPtr & part_)
|
|
|
|
: IStorage(getIDFromPart(part_))
|
|
|
|
, parts({part_})
|
|
|
|
, storage(part_->storage)
|
|
|
|
, partition_id(part_->info.partition_id)
|
|
|
|
{
|
|
|
|
setInMemoryMetadata(storage.getInMemoryMetadata());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Used in queries with projection.
|
|
|
|
StorageFromMergeTreeDataPart(const MergeTreeData & storage_, MergeTreeDataSelectAnalysisResultPtr analysis_result_ptr_)
|
|
|
|
: IStorage(storage_.getStorageID()), storage(storage_), analysis_result_ptr(analysis_result_ptr_)
|
|
|
|
{
|
|
|
|
setInMemoryMetadata(storage.getInMemoryMetadata());
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:17:48 +00:00
|
|
|
String getName() const override { return "FromMergeTreeDataPart"; }
|
|
|
|
|
2022-06-01 10:57:20 +00:00
|
|
|
StorageSnapshotPtr getStorageSnapshot(
|
|
|
|
const StorageMetadataPtr & metadata_snapshot, ContextPtr /*query_context*/) const override
|
|
|
|
{
|
|
|
|
const auto & storage_columns = metadata_snapshot->getColumns();
|
2022-09-06 15:51:16 +00:00
|
|
|
if (!hasDynamicSubcolumns(storage_columns))
|
2022-06-01 10:57:20 +00:00
|
|
|
return std::make_shared<StorageSnapshot>(*this, metadata_snapshot);
|
|
|
|
|
2022-09-06 15:51:16 +00:00
|
|
|
auto object_columns = getConcreteObjectColumns(
|
2022-06-01 10:57:20 +00:00
|
|
|
parts.begin(), parts.end(),
|
|
|
|
storage_columns, [](const auto & part) -> const auto & { return part->getColumns(); });
|
|
|
|
|
|
|
|
return std::make_shared<StorageSnapshot>(*this, metadata_snapshot, object_columns);
|
|
|
|
}
|
|
|
|
|
2022-05-23 19:47:32 +00:00
|
|
|
void read(
|
|
|
|
QueryPlan & query_plan,
|
2018-07-18 12:17:48 +00:00
|
|
|
const Names & column_names,
|
2021-07-09 03:15:41 +00:00
|
|
|
const StorageSnapshotPtr & storage_snapshot,
|
2020-09-20 17:52:17 +00:00
|
|
|
SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2018-08-05 07:05:36 +00:00
|
|
|
QueryProcessingStage::Enum /*processed_stage*/,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t num_streams) override
|
2018-07-18 12:17:48 +00:00
|
|
|
{
|
2023-02-01 17:55:06 +00:00
|
|
|
query_plan.addStep(MergeTreeDataSelectExecutor(storage)
|
2021-04-25 02:26:36 +00:00
|
|
|
.readFromParts(
|
|
|
|
parts,
|
|
|
|
column_names,
|
2021-07-09 03:15:41 +00:00
|
|
|
storage_snapshot,
|
2021-04-25 02:26:36 +00:00
|
|
|
query_info,
|
|
|
|
context,
|
|
|
|
max_block_size,
|
2021-08-16 12:09:18 +00:00
|
|
|
num_streams,
|
|
|
|
nullptr,
|
|
|
|
analysis_result_ptr));
|
2018-07-18 12:17:48 +00:00
|
|
|
}
|
2019-10-01 16:50:08 +00:00
|
|
|
|
2021-07-07 05:01:30 +00:00
|
|
|
bool supportsPrewhere() const override { return true; }
|
2019-10-01 16:50:08 +00:00
|
|
|
|
2018-09-04 11:38:41 +00:00
|
|
|
bool supportsIndexForIn() const override { return true; }
|
|
|
|
|
2022-06-01 10:57:20 +00:00
|
|
|
bool supportsDynamicSubcolumns() const override { return true; }
|
|
|
|
|
2020-06-17 09:38:47 +00:00
|
|
|
bool mayBenefitFromIndexForIn(
|
2021-04-10 23:33:54 +00:00
|
|
|
const ASTPtr & left_in_operand, ContextPtr query_context, const StorageMetadataPtr & metadata_snapshot) const override
|
2018-09-04 11:38:41 +00:00
|
|
|
{
|
2021-08-17 04:32:49 +00:00
|
|
|
return storage.mayBenefitFromIndexForIn(left_in_operand, query_context, metadata_snapshot);
|
2018-09-04 11:38:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 10:38:57 +00:00
|
|
|
NamesAndTypesList getVirtuals() const override
|
2020-04-27 13:55:30 +00:00
|
|
|
{
|
2021-08-17 04:32:49 +00:00
|
|
|
return storage.getVirtuals();
|
2020-04-27 13:55:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 10:23:46 +00:00
|
|
|
String getPartitionId() const
|
|
|
|
{
|
2021-08-17 04:32:49 +00:00
|
|
|
return partition_id;
|
2020-11-10 10:23:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
String getPartitionIDFromQuery(const ASTPtr & ast, ContextPtr context) const
|
2020-11-10 10:23:46 +00:00
|
|
|
{
|
2021-08-17 04:32:49 +00:00
|
|
|
return storage.getPartitionIDFromQuery(ast, context);
|
2020-11-10 10:23:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 06:17:48 +00:00
|
|
|
bool materializeTTLRecalculateOnly() const
|
|
|
|
{
|
2022-07-22 23:14:03 +00:00
|
|
|
if (parts.empty())
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "parts must not be empty for materializeTTLRecalculateOnly");
|
2021-08-05 06:17:48 +00:00
|
|
|
return parts.front()->storage.getSettings()->materialize_ttl_recalculate_only;
|
|
|
|
}
|
2021-08-05 07:33:02 +00:00
|
|
|
|
2022-07-21 19:50:19 +00:00
|
|
|
bool hasLightweightDeletedMask() const override
|
2022-07-21 07:32:29 +00:00
|
|
|
{
|
2022-07-22 23:14:03 +00:00
|
|
|
return !parts.empty() && parts.front()->hasLightweightDelete();
|
2022-07-21 07:32:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 11:55:43 +00:00
|
|
|
bool supportsLightweightDelete() const override
|
|
|
|
{
|
|
|
|
return !parts.empty() && parts.front()->supportLightweightDeleteMutate();
|
|
|
|
}
|
|
|
|
|
2018-07-18 12:17:48 +00:00
|
|
|
private:
|
2022-07-22 23:14:03 +00:00
|
|
|
const MergeTreeData::DataPartsVector parts;
|
2021-08-17 04:32:49 +00:00
|
|
|
const MergeTreeData & storage;
|
2022-07-22 23:14:03 +00:00
|
|
|
const String partition_id;
|
|
|
|
const MergeTreeDataSelectAnalysisResultPtr analysis_result_ptr;
|
2019-12-10 20:47:05 +00:00
|
|
|
|
|
|
|
static StorageID getIDFromPart(const MergeTreeData::DataPartPtr & part_)
|
|
|
|
{
|
|
|
|
auto table_id = part_->storage.getStorageID();
|
|
|
|
return StorageID(table_id.database_name, table_id.table_name + " (part " + part_->name + ")");
|
|
|
|
}
|
2018-07-18 12:17:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|