#include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int ILLEGAL_COLUMN; } namespace { /** in(x, set) - function for evaluating the IN * notIn(x, set) - and NOT IN. */ template struct FunctionInName; template <> struct FunctionInName { static constexpr auto name = "in"; }; template <> struct FunctionInName { static constexpr auto name = "globalIn"; }; template <> struct FunctionInName { static constexpr auto name = "notIn"; }; template <> struct FunctionInName { static constexpr auto name = "globalNotIn"; }; template <> struct FunctionInName { static constexpr auto name = "nullIn"; }; template <> struct FunctionInName { static constexpr auto name = "globalNullIn"; }; template <> struct FunctionInName { static constexpr auto name = "notNullIn"; }; template <> struct FunctionInName { static constexpr auto name = "globalNotNullIn"; }; template <> struct FunctionInName { static constexpr auto name = "inIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "globalInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "notInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "globalNotInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "nullInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "globalNullInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "notNullInIgnoreSet"; }; template <> struct FunctionInName { static constexpr auto name = "globalNotNullInIgnoreSet"; }; template class FunctionIn : public IFunction { public: /// ignore_set flag means that we don't use set from the second argument, just return zero column. /// It is needed to perform type analysis without creation of set. static constexpr auto name = FunctionInName::name; static FunctionPtr create(const Context &) { return std::make_shared(); } String getName() const override { return name; } size_t getNumberOfArguments() const override { return 2; } DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override { return std::make_shared(); } bool useDefaultImplementationForConstants() const override { /// Never return constant for -IgnoreSet functions to avoid constant folding. return !ignore_set; } bool useDefaultImplementationForNulls() const override { return null_is_skipped; } void executeImpl(ColumnsWithTypeAndName & columns, const ColumnNumbers & arguments, size_t result, [[maybe_unused]] size_t input_rows_count) const override { if constexpr (ignore_set) { columns[result].column = ColumnUInt8::create(input_rows_count, 0u); return; } /// Second argument must be ColumnSet. ColumnPtr column_set_ptr = columns[arguments[1]].column; const ColumnSet * column_set = checkAndGetColumnConstData(column_set_ptr.get()); if (!column_set) column_set = checkAndGetColumn(column_set_ptr.get()); if (!column_set) throw Exception("Second argument for function '" + getName() + "' must be Set; found " + column_set_ptr->getName(), ErrorCodes::ILLEGAL_COLUMN); DB::Block columns_of_key_columns; /// First argument may be a tuple or a single column. const ColumnWithTypeAndName & left_arg = columns[arguments[0]]; const ColumnTuple * tuple = typeid_cast(left_arg.column.get()); const ColumnConst * const_tuple = checkAndGetColumnConst(left_arg.column.get()); const DataTypeTuple * type_tuple = typeid_cast(left_arg.type.get()); ColumnPtr materialized_tuple; if (const_tuple) { materialized_tuple = const_tuple->convertToFullColumn(); tuple = typeid_cast(materialized_tuple.get()); } auto set = column_set->getData(); auto set_types = set->getDataTypes(); if (tuple && (set_types.size() != 1 || !set_types[0]->equals(*type_tuple))) { const auto & tuple_columns = tuple->getColumns(); const DataTypes & tuple_types = type_tuple->getElements(); size_t tuple_size = tuple_columns.size(); for (size_t i = 0; i < tuple_size; ++i) columns_of_key_columns.insert({ tuple_columns[i], tuple_types[i], "" }); } else columns_of_key_columns.insert(left_arg); columns[result].column = set->execute(columns_of_key_columns, negative); } }; template void registerFunctionsInImpl(FunctionFactory & factory) { factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); factory.registerFunction>(); } } void registerFunctionsIn(FunctionFactory & factory) { registerFunctionsInImpl(factory); registerFunctionsInImpl(factory); } }