2023-01-19 02:19:04 +00:00
|
|
|
#include "Interpreters/Context_fwd.h"
|
|
|
|
|
|
|
|
#include <Storages/StorageURLCluster.h>
|
|
|
|
|
|
|
|
#include <Core/QueryProcessingStage.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Interpreters/getHeaderForProcessingStage.h>
|
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2023-04-21 17:24:37 +00:00
|
|
|
#include <Interpreters/TranslateQualifiedNamesVisitor.h>
|
|
|
|
#include <Interpreters/AddDefaultDatabaseVisitor.h>
|
2023-01-19 02:19:04 +00:00
|
|
|
#include <QueryPipeline/RemoteQueryExecutor.h>
|
|
|
|
|
|
|
|
#include <Processors/Transforms/AddingDefaultsTransform.h>
|
|
|
|
|
|
|
|
#include <Processors/Sources/RemoteSource.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#include <Parsers/queryToString.h>
|
2023-01-19 02:19:04 +00:00
|
|
|
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Storages/SelectQueryInfo.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#include <Storages/StorageURL.h>
|
|
|
|
#include <Storages/extractTableFunctionArgumentsFromSelectQuery.h>
|
2023-08-17 16:54:43 +00:00
|
|
|
#include <Storages/VirtualColumnUtils.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
|
|
|
|
#include <TableFunctions/TableFunctionURLCluster.h>
|
2023-01-19 02:19:04 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-05-15 16:39:26 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2023-01-19 02:19:04 +00:00
|
|
|
StorageURLCluster::StorageURLCluster(
|
2024-01-22 22:55:50 +00:00
|
|
|
const ContextPtr & context,
|
2023-04-24 13:20:04 +00:00
|
|
|
const String & cluster_name_,
|
|
|
|
const String & uri_,
|
|
|
|
const String & format_,
|
2024-01-22 22:55:50 +00:00
|
|
|
const String & compression_method,
|
2023-01-19 02:19:04 +00:00
|
|
|
const StorageID & table_id_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
2024-01-22 22:55:50 +00:00
|
|
|
const StorageURL::Configuration & configuration_)
|
2024-01-26 14:50:16 +00:00
|
|
|
: IStorageCluster(cluster_name_, table_id_, getLogger("StorageURLCluster (" + table_id_.table_name + ")"))
|
2024-01-22 22:55:50 +00:00
|
|
|
, uri(uri_), format_name(format_)
|
2023-01-19 02:19:04 +00:00
|
|
|
{
|
2024-01-22 22:55:50 +00:00
|
|
|
context->getRemoteHostFilter().checkURL(Poco::URI(uri));
|
|
|
|
context->getHTTPHeaderFilter().checkHeaders(configuration_.headers);
|
2023-01-19 08:19:39 +00:00
|
|
|
|
2023-01-19 02:19:04 +00:00
|
|
|
StorageInMemoryMetadata storage_metadata;
|
|
|
|
|
|
|
|
if (columns_.empty())
|
|
|
|
{
|
2024-01-22 22:55:50 +00:00
|
|
|
ColumnsDescription columns;
|
|
|
|
if (format_name == "auto")
|
|
|
|
std::tie(columns, format_name) = StorageURL::getTableStructureAndFormatFromData(
|
|
|
|
uri, chooseCompressionMethod(Poco::URI(uri).getPath(), compression_method), configuration_.headers, std::nullopt, context);
|
|
|
|
else
|
|
|
|
columns = StorageURL::getTableStructureFromData(
|
|
|
|
format_, uri, chooseCompressionMethod(Poco::URI(uri).getPath(), compression_method), configuration_.headers, std::nullopt, context);
|
|
|
|
|
2023-01-19 02:19:04 +00:00
|
|
|
storage_metadata.setColumns(columns);
|
|
|
|
}
|
|
|
|
else
|
2024-01-22 22:55:50 +00:00
|
|
|
{
|
|
|
|
if (format_name == "auto")
|
|
|
|
format_name = StorageURL::getTableStructureAndFormatFromData(
|
|
|
|
uri, chooseCompressionMethod(Poco::URI(uri).getPath(), compression_method), configuration_.headers, std::nullopt, context).second;
|
|
|
|
|
2023-01-19 02:19:04 +00:00
|
|
|
storage_metadata.setColumns(columns_);
|
2024-01-22 22:55:50 +00:00
|
|
|
}
|
2023-01-19 02:19:04 +00:00
|
|
|
|
|
|
|
storage_metadata.setConstraints(constraints_);
|
|
|
|
setInMemoryMetadata(storage_metadata);
|
2024-02-29 18:01:54 +00:00
|
|
|
setVirtuals(VirtualColumnUtils::getVirtualsForFileLikeStorage(storage_metadata.getColumns()));
|
2023-01-19 02:19:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:55:50 +00:00
|
|
|
void StorageURLCluster::updateQueryToSendIfNeeded(ASTPtr & query, const StorageSnapshotPtr & storage_snapshot, const ContextPtr & context)
|
2023-04-24 13:20:04 +00:00
|
|
|
{
|
2023-05-12 13:58:45 +00:00
|
|
|
ASTExpressionList * expression_list = extractTableFunctionArgumentsFromSelectQuery(query);
|
|
|
|
if (!expression_list)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected SELECT query from table function urlCluster, got '{}'", queryToString(query));
|
|
|
|
|
2024-01-22 22:55:50 +00:00
|
|
|
TableFunctionURLCluster::updateStructureAndFormatArgumentsIfNeeded(
|
|
|
|
expression_list->children, storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription(), format_name, context);
|
2023-04-24 13:20:04 +00:00
|
|
|
}
|
|
|
|
|
2024-01-02 17:14:16 +00:00
|
|
|
RemoteQueryExecutor::Extension StorageURLCluster::getTaskIteratorExtension(const ActionsDAG::Node * predicate, const ContextPtr & context) const
|
2023-01-19 02:19:04 +00:00
|
|
|
{
|
2024-03-01 22:29:56 +00:00
|
|
|
auto iterator = std::make_shared<StorageURLSource::DisclosedGlobIterator>(uri, context->getSettingsRef().glob_expansion_max_elements, predicate, getVirtualsList(), context);
|
2023-04-21 17:24:37 +00:00
|
|
|
auto callback = std::make_shared<TaskIterator>([iter = std::move(iterator)]() mutable -> String { return iter->next(); });
|
2023-01-19 02:19:04 +00:00
|
|
|
return RemoteQueryExecutor::Extension{.task_iterator = std::move(callback)};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|