2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2021-07-31 07:45:26 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Common/Macros.h>
|
|
|
|
#include <Core/Field.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
/** Get scalar value of sub queries from query context via IAST::Hash.
|
|
|
|
*/
|
2021-06-01 12:20:52 +00:00
|
|
|
class FunctionGetScalar : public IFunction, WithContext
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "__getScalar";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr context_)
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
return std::make_shared<FunctionGetScalar>(context_);
|
2019-10-19 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
explicit FunctionGetScalar(ContextPtr context_) : WithContext(context_) {}
|
2019-10-19 20:36:35 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-08-02 12:17:53 +00:00
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() 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
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
2019-11-03 18:44:56 +00:00
|
|
|
if (arguments.size() != 1 || !isString(arguments[0].type) || !arguments[0].column || !isColumnConst(*arguments[0].column))
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Function {} accepts one const string argument", getName());
|
2020-06-28 20:20:49 +00:00
|
|
|
auto scalar_name = assert_cast<const ColumnConst &>(*arguments[0].column).getValue<String>();
|
2021-06-01 12:20:52 +00:00
|
|
|
ContextPtr query_context = getContext()->hasQueryContext() ? getContext()->getQueryContext() : getContext();
|
2021-04-10 23:33:54 +00:00
|
|
|
scalar = query_context->getScalar(scalar_name).getByPosition(0);
|
2019-10-19 20:36:35 +00:00
|
|
|
return scalar.type;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
return ColumnConst::create(scalar.column, input_rows_count);
|
2019-10-19 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable ColumnWithTypeAndName scalar;
|
|
|
|
};
|
|
|
|
|
2021-07-31 07:45:26 +00:00
|
|
|
|
|
|
|
/** Get special scalar values
|
|
|
|
*/
|
|
|
|
template <typename Scalar>
|
2021-08-02 09:37:41 +00:00
|
|
|
class FunctionGetSpecialScalar : public IFunction
|
2021-07-31 07:45:26 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = Scalar::name;
|
|
|
|
static FunctionPtr create(ContextPtr context_)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionGetSpecialScalar<Scalar>>(context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ColumnWithTypeAndName createScalar(ContextPtr context_)
|
|
|
|
{
|
2024-04-04 20:00:22 +00:00
|
|
|
if (auto block = context_->tryGetSpecialScalar(Scalar::scalar_name))
|
2021-07-31 07:45:26 +00:00
|
|
|
return block->getByPosition(0);
|
|
|
|
else if (context_->hasQueryContext())
|
|
|
|
{
|
|
|
|
if (context_->getQueryContext()->hasScalar(Scalar::scalar_name))
|
|
|
|
return context_->getQueryContext()->getScalar(Scalar::scalar_name).getByPosition(0);
|
|
|
|
}
|
|
|
|
return {DataTypeUInt32().createColumnConst(1, 0), std::make_shared<DataTypeUInt32>(), Scalar::scalar_name};
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit FunctionGetSpecialScalar(ContextPtr context_)
|
2021-08-02 09:37:41 +00:00
|
|
|
: scalar(createScalar(context_)), is_distributed(context_->isDistributed())
|
2021-07-31 07:45:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
|
|
|
|
bool isSuitableForConstantFolding() const override { return !is_distributed; }
|
|
|
|
|
2021-08-10 11:31:15 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
2021-07-31 07:45:26 +00:00
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName &) const override
|
|
|
|
{
|
|
|
|
return scalar.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName &, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
2023-02-21 20:50:24 +00:00
|
|
|
auto result = ColumnConst::create(scalar.column, input_rows_count);
|
|
|
|
|
|
|
|
if (!isSuitableForConstantFolding())
|
|
|
|
return result->convertToFullColumnIfConst();
|
|
|
|
|
|
|
|
return result;
|
2021-07-31 07:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ColumnWithTypeAndName scalar;
|
|
|
|
bool is_distributed;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GetShardNum
|
|
|
|
{
|
|
|
|
static constexpr auto name = "shardNum";
|
|
|
|
static constexpr auto scalar_name = "_shard_num";
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GetShardCount
|
|
|
|
{
|
|
|
|
static constexpr auto name = "shardCount";
|
|
|
|
static constexpr auto scalar_name = "_shard_count";
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2019-10-19 20:36:35 +00:00
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(GetScalar)
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionGetScalar>();
|
2021-07-31 07:45:26 +00:00
|
|
|
factory.registerFunction<FunctionGetSpecialScalar<GetShardNum>>();
|
|
|
|
factory.registerFunction<FunctionGetSpecialScalar<GetShardCount>>();
|
2019-10-19 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|