2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Storages/getStructureOfRemoteTable.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2020-06-24 16:37:04 +00:00
|
|
|
extern const int UNKNOWN_TABLE;
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
/** Usage:
|
|
|
|
* hasColumnInTable(['hostname'[, 'username'[, 'password']],] 'database', 'table', 'column')
|
|
|
|
*/
|
2021-06-01 12:20:52 +00:00
|
|
|
class FunctionHasColumnInTable : public IFunction, WithContext
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "hasColumnInTable";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
return std::make_shared<FunctionHasColumnInTable>(context_->getGlobalContext());
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionHasColumnInTable(ContextPtr global_context_) : WithContext(global_context_)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override;
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override;
|
2018-09-08 22:04:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DataTypePtr FunctionHasColumnInTable::getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const
|
|
|
|
{
|
|
|
|
if (arguments.size() < 3 || arguments.size() > 6)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Invalid number of arguments for function {}", getName());
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
static const std::string arg_pos_description[] = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth"};
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
const ColumnWithTypeAndName & argument = arguments[i];
|
|
|
|
|
|
|
|
if (!checkColumnConst<ColumnString>(argument.column.get()))
|
|
|
|
{
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "{} argument for function {} must be const String.",
|
|
|
|
arg_pos_description[i], getName());
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr FunctionHasColumnInTable::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2020-11-17 13:24:45 +00:00
|
|
|
auto get_string_from_columns = [&](const ColumnWithTypeAndName & column) -> String
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const ColumnConst * const_column = checkAndGetColumnConst<ColumnString>(column.column.get());
|
2018-09-08 22:04:39 +00:00
|
|
|
return const_column->getValue<String>();
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t arg = 0;
|
|
|
|
String host_name;
|
|
|
|
String user_name;
|
|
|
|
String password;
|
|
|
|
|
|
|
|
if (arguments.size() > 3)
|
2020-10-14 14:04:50 +00:00
|
|
|
host_name = get_string_from_columns(arguments[arg++]);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
if (arguments.size() > 4)
|
2020-10-14 14:04:50 +00:00
|
|
|
user_name = get_string_from_columns(arguments[arg++]);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
if (arguments.size() > 5)
|
2020-10-14 14:04:50 +00:00
|
|
|
password = get_string_from_columns(arguments[arg++]);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2020-10-14 14:04:50 +00:00
|
|
|
String database_name = get_string_from_columns(arguments[arg++]);
|
|
|
|
String table_name = get_string_from_columns(arguments[arg++]);
|
|
|
|
String column_name = get_string_from_columns(arguments[arg++]);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2020-06-24 16:37:04 +00:00
|
|
|
if (table_name.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::UNKNOWN_TABLE, "Table name is empty");
|
2020-06-24 16:37:04 +00:00
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
bool has_column;
|
|
|
|
if (host_name.empty())
|
|
|
|
{
|
2021-05-28 16:44:59 +00:00
|
|
|
// FIXME this (probably) needs a non-constant access to query context,
|
|
|
|
// because it might initialized a storage. Ideally, the tables required
|
|
|
|
// by the query should be initialized at an earlier stage.
|
|
|
|
const StoragePtr & table = DatabaseCatalog::instance().getTable(
|
|
|
|
{database_name, table_name},
|
|
|
|
const_pointer_cast<Context>(getContext()));
|
2020-06-17 16:39:58 +00:00
|
|
|
auto table_metadata = table->getInMemoryMetadataPtr();
|
|
|
|
has_column = table_metadata->getColumns().hasPhysical(column_name);
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<std::vector<String>> host_names = {{ host_name }};
|
|
|
|
|
2021-07-23 11:16:35 +00:00
|
|
|
bool treat_local_as_remote = false;
|
|
|
|
bool treat_local_port_as_remote = getContext()->getApplicationType() == Context::ApplicationType::LOCAL;
|
2023-04-07 16:26:23 +00:00
|
|
|
ClusterConnectionParameters params{
|
2018-09-08 22:04:39 +00:00
|
|
|
!user_name.empty() ? user_name : "default",
|
|
|
|
password,
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->getTCPPort(),
|
2021-07-23 11:16:35 +00:00
|
|
|
treat_local_as_remote,
|
2023-04-07 16:26:23 +00:00
|
|
|
treat_local_port_as_remote,
|
|
|
|
/* secure= */ false,
|
2023-06-07 16:25:52 +00:00
|
|
|
/* priority= */ Priority{1},
|
2023-04-07 16:26:23 +00:00
|
|
|
/* cluster_name= */ "",
|
|
|
|
/* password= */ ""
|
|
|
|
};
|
|
|
|
auto cluster = std::make_shared<Cluster>(getContext()->getSettings(), host_names, params);
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2021-05-28 16:44:59 +00:00
|
|
|
// FIXME this (probably) needs a non-constant access to query context,
|
|
|
|
// because it might initialized a storage. Ideally, the tables required
|
|
|
|
// by the query should be initialized at an earlier stage.
|
|
|
|
auto remote_columns = getStructureOfRemoteTable(*cluster,
|
|
|
|
{database_name, table_name},
|
|
|
|
const_pointer_cast<Context>(getContext()));
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
has_column = remote_columns.hasPhysical(column_name);
|
|
|
|
}
|
|
|
|
|
2022-04-18 08:18:31 +00:00
|
|
|
return DataTypeUInt8().createColumnConst(input_rows_count, Field{static_cast<UInt64>(has_column)});
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(HasColumnInTable)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionHasColumnInTable>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|