2021-04-12 21:42:52 +00:00
|
|
|
#include "Storages/StorageS3Cluster.h"
|
2021-03-19 21:49:18 +00:00
|
|
|
|
2021-03-24 21:02:21 +00:00
|
|
|
#if USE_AWS_S3
|
|
|
|
|
2021-03-22 17:12:31 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2023-02-03 10:54:49 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2022-12-02 14:43:29 +00:00
|
|
|
#include <Interpreters/AddDefaultDatabaseVisitor.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#include <Interpreters/InterpreterSelectQuery.h>
|
2021-04-06 19:18:45 +00:00
|
|
|
#include <Processors/Sources/RemoteSource.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#include <Processors/Transforms/AddingDefaultsTransform.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <QueryPipeline/RemoteQueryExecutor.h>
|
2021-03-26 15:33:14 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#include <Storages/StorageURL.h>
|
2021-03-26 15:33:14 +00:00
|
|
|
#include <Storages/SelectQueryInfo.h>
|
2022-09-30 16:59:17 +00:00
|
|
|
#include <Storages/StorageDictionary.h>
|
2023-05-12 13:58:45 +00:00
|
|
|
#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 <Common/Exception.h>
|
|
|
|
#include <Parsers/queryToString.h>
|
|
|
|
#include <TableFunctions/TableFunctionS3Cluster.h>
|
2021-03-22 17:12:31 +00:00
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2021-03-22 17:12:31 +00:00
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2022-09-30 16:59:17 +00:00
|
|
|
|
2023-05-15 16:39:26 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
StorageS3Cluster::StorageS3Cluster(
|
2023-04-21 17:24:37 +00:00
|
|
|
const String & cluster_name_,
|
|
|
|
const StorageS3::Configuration & configuration_,
|
2021-03-22 17:12:31 +00:00
|
|
|
const StorageID & table_id_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
2024-01-22 22:55:50 +00:00
|
|
|
const ContextPtr & context)
|
2024-01-25 21:11:07 +00:00
|
|
|
: IStorageCluster(cluster_name_, table_id_, getLogger("StorageS3Cluster (" + table_id_.table_name + ")"))
|
2023-02-06 14:36:47 +00:00
|
|
|
, s3_configuration{configuration_}
|
2021-03-19 21:49:18 +00:00
|
|
|
{
|
2024-01-22 22:55:50 +00:00
|
|
|
context->getGlobalContext()->getRemoteHostFilter().checkURL(configuration_.url.uri);
|
|
|
|
context->getGlobalContext()->getHTTPHeaderFilter().checkHeaders(configuration_.headers_from_ast);
|
2023-06-15 13:49:49 +00:00
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
StorageInMemoryMetadata storage_metadata;
|
2024-01-22 22:55:50 +00:00
|
|
|
updateConfigurationIfChanged(context);
|
2022-03-28 22:46:35 +00:00
|
|
|
|
|
|
|
if (columns_.empty())
|
|
|
|
{
|
2024-01-22 22:55:50 +00:00
|
|
|
ColumnsDescription columns;
|
2022-03-28 22:46:35 +00:00
|
|
|
/// `format_settings` is set to std::nullopt, because StorageS3Cluster is used only as table function
|
2024-01-22 22:55:50 +00:00
|
|
|
if (s3_configuration.format == "auto")
|
|
|
|
std::tie(columns, s3_configuration.format) = StorageS3::getTableStructureAndFormatFromData(s3_configuration, /*format_settings=*/std::nullopt, context);
|
|
|
|
else
|
|
|
|
columns = StorageS3::getTableStructureFromData(s3_configuration, /*format_settings=*/std::nullopt, context);
|
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
storage_metadata.setColumns(columns);
|
|
|
|
}
|
|
|
|
else
|
2024-01-22 22:55:50 +00:00
|
|
|
{
|
|
|
|
if (s3_configuration.format == "auto")
|
|
|
|
s3_configuration.format = StorageS3::getTableStructureAndFormatFromData(s3_configuration, /*format_settings=*/std::nullopt, context).second;
|
|
|
|
|
2022-03-28 22:46:35 +00:00
|
|
|
storage_metadata.setColumns(columns_);
|
2024-01-22 22:55:50 +00:00
|
|
|
}
|
2022-03-28 22:46:35 +00:00
|
|
|
|
2021-03-22 17:12:31 +00:00
|
|
|
storage_metadata.setConstraints(constraints_);
|
2021-03-19 21:49:18 +00:00
|
|
|
setInMemoryMetadata(storage_metadata);
|
2022-05-19 11:18:58 +00:00
|
|
|
|
2023-11-22 18:12:36 +00:00
|
|
|
virtual_columns = VirtualColumnUtils::getPathFileAndSizeVirtualsForStorage(storage_metadata.getSampleBlock().getNamesAndTypesList());
|
2021-03-19 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 22:55:50 +00:00
|
|
|
void StorageS3Cluster::updateQueryToSendIfNeeded(DB::ASTPtr & query, const DB::StorageSnapshotPtr & storage_snapshot, const DB::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 s3Cluster, got '{}'", queryToString(query));
|
|
|
|
|
2024-01-22 22:55:50 +00:00
|
|
|
TableFunctionS3Cluster::updateStructureAndFormatArgumentsIfNeeded(
|
|
|
|
expression_list->children,
|
|
|
|
storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription(),
|
|
|
|
s3_configuration.format,
|
|
|
|
context);
|
2023-04-24 13:20:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 13:39:59 +00:00
|
|
|
void StorageS3Cluster::updateConfigurationIfChanged(ContextPtr local_context)
|
|
|
|
{
|
|
|
|
s3_configuration.update(local_context);
|
|
|
|
}
|
|
|
|
|
2024-01-02 17:14:16 +00:00
|
|
|
RemoteQueryExecutor::Extension StorageS3Cluster::getTaskIteratorExtension(const ActionsDAG::Node * predicate, const ContextPtr & context) const
|
2023-01-09 12:30:32 +00:00
|
|
|
{
|
|
|
|
auto iterator = std::make_shared<StorageS3Source::DisclosedGlobIterator>(
|
2024-01-02 17:14:16 +00:00
|
|
|
*s3_configuration.client, s3_configuration.url, predicate, virtual_columns, context, nullptr, s3_configuration.request_settings, context->getFileProgressCallback());
|
2023-09-28 14:25:04 +00:00
|
|
|
|
2023-09-28 11:51:07 +00:00
|
|
|
auto callback = std::make_shared<std::function<String()>>([iterator]() mutable -> String
|
|
|
|
{
|
|
|
|
if (auto next = iterator->next())
|
|
|
|
return next->key;
|
|
|
|
return "";
|
|
|
|
});
|
2023-01-09 12:30:32 +00:00
|
|
|
return RemoteQueryExecutor::Extension{ .task_iterator = std::move(callback) };
|
|
|
|
}
|
|
|
|
|
2021-04-12 21:42:52 +00:00
|
|
|
NamesAndTypesList StorageS3Cluster::getVirtuals() const
|
2021-04-12 17:33:55 +00:00
|
|
|
{
|
2022-05-19 11:18:58 +00:00
|
|
|
return virtual_columns;
|
2021-04-12 17:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-19 21:49:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 21:02:21 +00:00
|
|
|
#endif
|