2018-06-11 12:13:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <ext/shared_ptr_helper.h>
|
2020-04-28 00:56:44 +00:00
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
2020-12-10 22:05:02 +00:00
|
|
|
#include <Formats/FormatSettings.h>
|
2020-04-28 00:56:44 +00:00
|
|
|
#include <IO/CompressionMethod.h>
|
2021-04-21 12:32:57 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
2018-06-11 12:13:00 +00:00
|
|
|
|
2019-08-24 21:20:20 +00:00
|
|
|
|
2018-06-11 12:13:00 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-12-10 22:05:02 +00:00
|
|
|
|
|
|
|
struct ConnectionTimeouts;
|
|
|
|
|
2018-06-11 12:13:00 +00:00
|
|
|
/**
|
|
|
|
* This class represents table engine for external urls.
|
|
|
|
* It sends HTTP GET to server when select is called and
|
|
|
|
* HTTP POST when insert is called. In POST request the data is send
|
|
|
|
* using Chunked transfer encoding, so server have to support it.
|
|
|
|
*/
|
2018-08-09 18:49:05 +00:00
|
|
|
class IStorageURLBase : public IStorage
|
2018-06-11 12:13:00 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-08-03 13:54:14 +00:00
|
|
|
Pipe read(
|
2019-08-24 21:20:20 +00:00
|
|
|
const Names & column_names,
|
2020-06-15 19:08:58 +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,
|
2018-06-25 12:21:54 +00:00
|
|
|
QueryProcessingStage::Enum processed_stage,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2018-06-11 12:13:00 +00:00
|
|
|
unsigned num_streams) override;
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
BlockOutputStreamPtr write(const ASTPtr & query, const StorageMetadataPtr & /*metadata_snapshot*/, ContextPtr context) override;
|
2018-06-11 12:13:00 +00:00
|
|
|
|
|
|
|
protected:
|
2019-08-24 21:20:20 +00:00
|
|
|
IStorageURLBase(
|
|
|
|
const Poco::URI & uri_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context_,
|
2019-12-04 16:06:55 +00:00
|
|
|
const StorageID & id_,
|
2018-06-11 12:13:00 +00:00
|
|
|
const String & format_name_,
|
2020-11-07 08:53:39 +00:00
|
|
|
const std::optional<FormatSettings> & format_settings_,
|
2019-08-24 21:20:20 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2019-11-19 12:46:07 +00:00
|
|
|
const ConstraintsDescription & constraints_,
|
2021-04-23 12:18:23 +00:00
|
|
|
const String & comment,
|
2019-11-19 12:46:07 +00:00
|
|
|
const String & compression_method_);
|
2018-06-11 12:13:00 +00:00
|
|
|
|
|
|
|
Poco::URI uri;
|
2019-11-19 12:46:07 +00:00
|
|
|
String compression_method;
|
2018-06-11 12:13:00 +00:00
|
|
|
String format_name;
|
2020-11-05 11:28:20 +00:00
|
|
|
// For URL engine, we use format settings from server context + `SETTINGS`
|
|
|
|
// clause of the `CREATE` query. In this case, format_settings is set.
|
|
|
|
// For `url` table function, we use settings from current query context.
|
|
|
|
// In this case, format_settings is not set.
|
|
|
|
std::optional<FormatSettings> format_settings;
|
2018-06-11 12:13:00 +00:00
|
|
|
|
2018-08-09 18:49:05 +00:00
|
|
|
virtual std::string getReadMethod() const;
|
|
|
|
|
2019-08-24 21:20:20 +00:00
|
|
|
virtual std::vector<std::pair<std::string, std::string>> getReadURIParams(
|
|
|
|
const Names & column_names,
|
2020-06-17 16:39:58 +00:00
|
|
|
const StorageMetadataPtr & metadata_snapshot,
|
2018-08-09 18:49:05 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2018-08-09 18:49:05 +00:00
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2019-02-19 00:48:52 +00:00
|
|
|
size_t max_block_size) const;
|
2018-08-09 18:49:05 +00:00
|
|
|
|
2019-08-24 21:20:20 +00:00
|
|
|
virtual std::function<void(std::ostream &)> getReadPOSTDataCallback(
|
|
|
|
const Names & column_names,
|
2020-06-17 16:39:58 +00:00
|
|
|
const StorageMetadataPtr & /*metadata_snapshot*/,
|
2018-08-09 18:49:05 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2018-08-09 18:49:05 +00:00
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
2019-02-19 00:48:52 +00:00
|
|
|
size_t max_block_size) const;
|
2018-09-22 14:58:03 +00:00
|
|
|
|
2021-04-21 14:36:04 +00:00
|
|
|
private:
|
2020-06-16 14:25:08 +00:00
|
|
|
virtual Block getHeaderBlock(const Names & column_names, const StorageMetadataPtr & metadata_snapshot) const = 0;
|
2018-08-09 18:49:05 +00:00
|
|
|
};
|
|
|
|
|
2020-04-28 00:56:44 +00:00
|
|
|
class StorageURLBlockOutputStream : public IBlockOutputStream
|
|
|
|
{
|
|
|
|
public:
|
2020-06-17 16:39:58 +00:00
|
|
|
StorageURLBlockOutputStream(
|
|
|
|
const Poco::URI & uri,
|
|
|
|
const String & format,
|
2020-11-07 08:53:39 +00:00
|
|
|
const std::optional<FormatSettings> & format_settings,
|
2020-06-17 16:39:58 +00:00
|
|
|
const Block & sample_block_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2020-06-17 16:39:58 +00:00
|
|
|
const ConnectionTimeouts & timeouts,
|
2021-04-10 23:33:54 +00:00
|
|
|
CompressionMethod compression_method);
|
2020-04-28 00:56:44 +00:00
|
|
|
|
|
|
|
Block getHeader() const override
|
|
|
|
{
|
|
|
|
return sample_block;
|
|
|
|
}
|
|
|
|
|
2020-07-09 01:00:16 +00:00
|
|
|
void write(const Block & block) override;
|
|
|
|
void writePrefix() override;
|
|
|
|
void writeSuffix() override;
|
2020-04-28 00:56:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Block sample_block;
|
|
|
|
std::unique_ptr<WriteBuffer> write_buf;
|
|
|
|
BlockOutputStreamPtr writer;
|
|
|
|
};
|
2019-08-24 21:20:20 +00:00
|
|
|
|
2021-04-21 14:36:04 +00:00
|
|
|
class StorageURL : public ext::shared_ptr_helper<StorageURL>, public IStorageURLBase
|
2018-08-09 18:49:05 +00:00
|
|
|
{
|
2019-08-26 19:07:29 +00:00
|
|
|
friend struct ext::shared_ptr_helper<StorageURL>;
|
2018-08-09 18:49:05 +00:00
|
|
|
public:
|
2021-04-23 12:18:23 +00:00
|
|
|
StorageURL(
|
|
|
|
const Poco::URI & uri_,
|
|
|
|
const StorageID & table_id_,
|
|
|
|
const String & format_name_,
|
|
|
|
const std::optional<FormatSettings> & format_settings_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
|
|
|
const String & comment,
|
|
|
|
ContextPtr context_,
|
|
|
|
const String & compression_method_);
|
2018-08-09 18:49:05 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return "URL";
|
|
|
|
}
|
2018-09-22 14:58:03 +00:00
|
|
|
|
2020-06-16 14:25:08 +00:00
|
|
|
Block getHeaderBlock(const Names & /*column_names*/, const StorageMetadataPtr & metadata_snapshot) const override
|
2018-09-22 14:58:03 +00:00
|
|
|
{
|
2020-06-16 14:25:08 +00:00
|
|
|
return metadata_snapshot->getSampleBlock();
|
2018-09-22 14:58:03 +00:00
|
|
|
}
|
2021-04-21 12:32:57 +00:00
|
|
|
|
|
|
|
static FormatSettings getFormatSettingsFromArgs(const StorageFactory::Arguments & args);
|
2018-06-11 12:13:00 +00:00
|
|
|
};
|
2021-04-21 14:36:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// StorageURLWithFailover is allowed only for URL table function, not as a separate storage.
|
|
|
|
class StorageURLWithFailover final : public StorageURL
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageURLWithFailover(
|
|
|
|
const std::vector<String> & uri_options_,
|
|
|
|
const StorageID & table_id_,
|
|
|
|
const String & format_name_,
|
|
|
|
const std::optional<FormatSettings> & format_settings_,
|
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
|
|
|
ContextPtr context_,
|
|
|
|
const String & compression_method_);
|
|
|
|
|
|
|
|
Pipe read(
|
|
|
|
const Names & column_names,
|
|
|
|
const StorageMetadataPtr & /*metadata_snapshot*/,
|
|
|
|
SelectQueryInfo & query_info,
|
|
|
|
ContextPtr context,
|
|
|
|
QueryProcessingStage::Enum processed_stage,
|
|
|
|
size_t max_block_size,
|
|
|
|
unsigned num_streams) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<Poco::URI> uri_options;
|
2018-06-11 12:13:00 +00:00
|
|
|
};
|
|
|
|
}
|