| 1 | #include <Functions/IFunctionImpl.h> |
| 2 | #include <Functions/FunctionFactory.h> |
| 3 | #include <DataTypes/DataTypesNumber.h> |
| 4 | |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | |
| 10 | /** The `indexHint` function takes any number of any arguments and always returns one. |
| 11 | * |
| 12 | * This function has a special meaning (see ExpressionAnalyzer, KeyCondition) |
| 13 | * - the expressions inside it are not evaluated; |
| 14 | * - but when analyzing the index (selecting ranges for reading), this function is treated the same way, |
| 15 | * as if instead of using it the expression itself would be. |
| 16 | * |
| 17 | * Example: WHERE something AND indexHint(CounterID = 34) |
| 18 | * - do not read or calculate CounterID = 34, but select ranges in which the CounterID = 34 expression can be true. |
| 19 | * |
| 20 | * The function can be used for debugging purposes, as well as for (hidden from the user) query conversions. |
| 21 | */ |
| 22 | class FunctionIndexHint : public IFunction |
| 23 | { |
| 24 | public: |
| 25 | static constexpr auto name = "indexHint" ; |
| 26 | static FunctionPtr create(const Context &) |
| 27 | { |
| 28 | return std::make_shared<FunctionIndexHint>(); |
| 29 | } |
| 30 | |
| 31 | bool isVariadic() const override |
| 32 | { |
| 33 | return true; |
| 34 | } |
| 35 | size_t getNumberOfArguments() const override |
| 36 | { |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | bool useDefaultImplementationForNulls() const override { return false; } |
| 41 | |
| 42 | String getName() const override |
| 43 | { |
| 44 | return name; |
| 45 | } |
| 46 | DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override |
| 47 | { |
| 48 | return std::make_shared<DataTypeUInt8>(); |
| 49 | } |
| 50 | |
| 51 | void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override |
| 52 | { |
| 53 | block.getByPosition(result).column = DataTypeUInt8().createColumnConst(input_rows_count, 1u); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | void registerFunctionIndexHint(FunctionFactory & factory) |
| 59 | { |
| 60 | factory.registerFunction<FunctionIndexHint>(); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |