| 1 | #include <Functions/IFunctionImpl.h> |
|---|---|
| 2 | #include <Functions/FunctionFactory.h> |
| 3 | #include <DataTypes/DataTypeLowCardinality.h> |
| 4 | #include <Columns/ColumnLowCardinality.h> |
| 5 | #include <Common/typeid_cast.h> |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | namespace ErrorCodes |
| 12 | { |
| 13 | extern const int ILLEGAL_TYPE_OF_ARGUMENT; |
| 14 | } |
| 15 | |
| 16 | |
| 17 | class FunctionLowCardinalityKeys: public IFunction |
| 18 | { |
| 19 | public: |
| 20 | static constexpr auto name = "lowCardinalityKeys"; |
| 21 | static FunctionPtr create(const Context &) { return std::make_shared<FunctionLowCardinalityKeys>(); } |
| 22 | |
| 23 | String getName() const override { return name; } |
| 24 | |
| 25 | size_t getNumberOfArguments() const override { return 1; } |
| 26 | |
| 27 | bool useDefaultImplementationForNulls() const override { return false; } |
| 28 | bool useDefaultImplementationForConstants() const override { return true; } |
| 29 | bool useDefaultImplementationForLowCardinalityColumns() const override { return false; } |
| 30 | |
| 31 | DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override |
| 32 | { |
| 33 | auto * type = typeid_cast<const DataTypeLowCardinality *>(arguments[0].get()); |
| 34 | if (!type) |
| 35 | throw Exception("First first argument of function lowCardinalityKeys must be ColumnLowCardinality, but got" |
| 36 | + arguments[0]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
| 37 | |
| 38 | return type->getDictionaryType(); |
| 39 | } |
| 40 | |
| 41 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override |
| 42 | { |
| 43 | auto arg_num = arguments[0]; |
| 44 | const auto & arg = block.getByPosition(arg_num); |
| 45 | auto & res = block.getByPosition(result); |
| 46 | const auto * low_cardinality_column = typeid_cast<const ColumnLowCardinality *>(arg.column.get()); |
| 47 | res.column = low_cardinality_column->getDictionary().getNestedColumn()->cloneResized(arg.column->size()); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | void registerFunctionLowCardinalityKeys(FunctionFactory & factory) |
| 53 | { |
| 54 | factory.registerFunction<FunctionLowCardinalityKeys>(); |
| 55 | } |
| 56 | |
| 57 | } |
| 58 |