aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/FunctionStringReplace.h
blob: 4d723a5632c232a523bc1baaf077dc8939521eff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once

#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnConst.h>
#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int ILLEGAL_COLUMN;
}

template <typename Impl, typename Name>
class FunctionStringReplace : public IFunction
{
public:
    static constexpr auto name = Name::name;

    static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionStringReplace>(); }

    String getName() const override { return name; }

    size_t getNumberOfArguments() const override { return 3; }

    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }

    bool useDefaultImplementationForConstants() const override { return true; }

    DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
    {
        FunctionArgumentDescriptors args{
            {"haystack", &isStringOrFixedString<IDataType>, nullptr, "String or FixedString"},
            {"pattern", &isString<IDataType>, nullptr, "String"},
            {"replacement", &isString<IDataType>, nullptr, "String"}
        };

        validateFunctionArgumentTypes(*this, arguments, args);

        return std::make_shared<DataTypeString>();
    }

    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
    {
        ColumnPtr column_haystack = arguments[0].column;
        column_haystack = column_haystack->convertToFullColumnIfConst();

        const ColumnPtr column_needle = arguments[1].column;
        const ColumnPtr column_replacement = arguments[2].column;

        const ColumnString * col_haystack = checkAndGetColumn<ColumnString>(column_haystack.get());
        const ColumnFixedString * col_haystack_fixed = checkAndGetColumn<ColumnFixedString>(column_haystack.get());

        const ColumnString * col_needle_vector = checkAndGetColumn<ColumnString>(column_needle.get());
        const ColumnConst * col_needle_const = checkAndGetColumn<ColumnConst>(column_needle.get());

        const ColumnString * col_replacement_vector = checkAndGetColumn<ColumnString>(column_replacement.get());
        const ColumnConst * col_replacement_const = checkAndGetColumn<ColumnConst>(column_replacement.get());

        auto col_res = ColumnString::create();

        if (col_haystack && col_needle_const && col_replacement_const)
        {
            Impl::vectorConstantConstant(
                col_haystack->getChars(), col_haystack->getOffsets(),
                col_needle_const->getValue<String>(),
                col_replacement_const->getValue<String>(),
                col_res->getChars(), col_res->getOffsets());
            return col_res;
        }
        else if (col_haystack && col_needle_vector && col_replacement_const)
        {
            Impl::vectorVectorConstant(
                col_haystack->getChars(), col_haystack->getOffsets(),
                col_needle_vector->getChars(), col_needle_vector->getOffsets(),
                col_replacement_const->getValue<String>(),
                col_res->getChars(), col_res->getOffsets());
            return col_res;
        }
        else if (col_haystack && col_needle_const && col_replacement_vector)
        {
            Impl::vectorConstantVector(
                col_haystack->getChars(), col_haystack->getOffsets(),
                col_needle_const->getValue<String>(),
                col_replacement_vector->getChars(), col_replacement_vector->getOffsets(),
                col_res->getChars(), col_res->getOffsets());
            return col_res;
        }
        else if (col_haystack && col_needle_vector && col_replacement_vector)
        {
            Impl::vectorVectorVector(
                col_haystack->getChars(), col_haystack->getOffsets(),
                col_needle_vector->getChars(), col_needle_vector->getOffsets(),
                col_replacement_vector->getChars(), col_replacement_vector->getOffsets(),
                col_res->getChars(), col_res->getOffsets());
            return col_res;
        }
        else if (col_haystack_fixed && col_needle_const && col_replacement_const)
        {
            Impl::vectorFixedConstantConstant(
                col_haystack_fixed->getChars(), col_haystack_fixed->getN(),
                col_needle_const->getValue<String>(),
                col_replacement_const->getValue<String>(),
                col_res->getChars(), col_res->getOffsets());
            return col_res;
        }
        else
            throw Exception(
                ErrorCodes::ILLEGAL_COLUMN,
                "Illegal column {} of first argument of function {}",
                arguments[0].column->getName(), getName());
    }
};

}