|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <Columns/ColumnString.h> |
| 4 | +#include <Columns/ColumnVector.h> |
| 5 | +#include <DataTypes/DataTypeString.h> |
| 6 | +#include <DataTypes/DataTypesNumber.h> |
| 7 | +#include <Functions/IFunction.h> |
| 8 | +#include <Common/BitHelpers.h> |
| 9 | + |
| 10 | +#include <cmath> |
| 11 | + |
| 12 | +namespace DB |
| 13 | +{ |
| 14 | + |
| 15 | +namespace ErrorCodes |
| 16 | +{ |
| 17 | +extern const int ILLEGAL_TYPE_OF_ARGUMENT; |
| 18 | +extern const int ILLEGAL_COLUMN; |
| 19 | +} |
| 20 | + |
| 21 | +template <typename Impl, typename Name> |
| 22 | +class FunctionStringBytes : public IFunction |
| 23 | +{ |
| 24 | +public: |
| 25 | + static constexpr auto name = Name::name; |
| 26 | + using ResultType = typename Impl::ResultType; |
| 27 | + |
| 28 | + static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionStringBytes>(); } |
| 29 | + |
| 30 | + String getName() const override { return name; } |
| 31 | + |
| 32 | + size_t getNumberOfArguments() const override { return 1; } |
| 33 | + |
| 34 | + bool useDefaultImplementationForConstants() const override { return true; } |
| 35 | + |
| 36 | + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return true; } |
| 37 | + |
| 38 | + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override |
| 39 | + { |
| 40 | + if (!isString(arguments[0].type)) |
| 41 | + throw Exception( |
| 42 | + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, |
| 43 | + "Illegal type {} of argument of function {}", |
| 44 | + arguments[0].type->getName(), |
| 45 | + getName()); |
| 46 | + |
| 47 | + if constexpr (std::is_same_v<ResultType, UInt16>) |
| 48 | + return std::make_shared<DataTypeUInt16>(); |
| 49 | + else if constexpr (std::is_same_v<ResultType, Float64>) |
| 50 | + return std::make_shared<DataTypeFloat64>(); |
| 51 | + } |
| 52 | + |
| 53 | + ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override |
| 54 | + { |
| 55 | + const ColumnPtr column = arguments[0].column; |
| 56 | + const ColumnString * col_str = checkAndGetColumn<ColumnString>(column.get()); |
| 57 | + |
| 58 | + if (!col_str) |
| 59 | + throw Exception( |
| 60 | + ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", arguments[0].column->getName(), getName()); |
| 61 | + |
| 62 | + auto col_res = ColumnVector<ResultType>::create(); |
| 63 | + auto & vec_res = col_res->getData(); |
| 64 | + vec_res.resize(input_rows_count); |
| 65 | + |
| 66 | + const ColumnString::Chars & data = col_str->getChars(); |
| 67 | + const ColumnString::Offsets & offsets = col_str->getOffsets(); |
| 68 | + |
| 69 | + size_t prev_offset = 0; |
| 70 | + for (size_t i = 0; i < input_rows_count; ++i) |
| 71 | + { |
| 72 | + const UInt8 * data_ptr = data.data() + prev_offset; |
| 73 | + const size_t size = offsets[i] - prev_offset - 1; |
| 74 | + |
| 75 | + vec_res[i] = Impl::process(data_ptr, size); |
| 76 | + prev_offset = offsets[i]; |
| 77 | + } |
| 78 | + |
| 79 | + return col_res; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +} |
0 commit comments