Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions be/src/vec/functions/function_soundex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <cctype>

#include "common/status.h"
#include "vec/columns/column_string.h"
#include "vec/data_types/data_type_string.h"
#include "vec/functions/function.h"
#include "vec/functions/simple_function_factory.h"

namespace doris::vectorized {
#include "common/compile_check_begin.h"

class FunctionSoundex : public IFunction {
public:
static constexpr auto name = "soundex";

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

String get_name() const override { return name; }

size_t get_number_of_arguments() const override { return 1; }

DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return std::make_shared<DataTypeString>();
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
const ColumnPtr col_ptr = block.get_by_position(arguments[0]).column;

auto res_column = ColumnString::create();
for (size_t i = 0; i < input_rows_count; ++i) {
StringRef ref = col_ptr->get_data_at(i);
std::string soundex_code = calculate_soundex(ref);
res_column->insert_data(soundex_code.c_str(), soundex_code.length());
}

block.replace_by_position(result, std::move(res_column));
return Status::OK();
}

private:
std::string calculate_soundex(const StringRef& ref) const {
if (ref.empty()) {
return "";
}

std::string result;
result.reserve(4);
char pre_code = '\0';
for (size_t i = 0; i < ref.size; ++i) {
auto c = static_cast<unsigned char>(ref.data[i]);

if (c > 0x7f) {
throw Exception(ErrorCode::INVALID_ARGUMENT, "soundex only supports ASCII");
}
if (!std::isalpha(c)) {
continue;
}

c = static_cast<char>(std::toupper(c));
if (result.empty()) {
result += c;
pre_code = (SOUNDEX_TABLE[c - 'A'] == 'N') ? '\0' : SOUNDEX_TABLE[c - 'A'];
} else if (char code = SOUNDEX_TABLE[c - 'A']; code != 'N') {
if (code != 'V' && code != pre_code) {
result += code;
if (result.size() == 4) {
return result;
}
}

pre_code = code;
}
}

while (!result.empty() && result.size() < 4) {
result += '0';
}

return result;
}

/** 1. If a vowel (A, E, I, O, U) separates two consonants that have the same soundex code
* the consonant to the right of the vowel is coded. Here we use 'V' to represent vowels.
* eg : **Tymczak** is coded as T-522 (T, 5 for the M, 2 for the C, Z ignored , 2 for the K).
* Since the vowel "A" separates the Z and K, the K is coded.
*
* 2. If "H" or "W" separate two consonants that have the same soundex code, the consonant to the right of the vowel is NOT coded.
* Here we use 'N' to represent these two characters.
* eg : **Ashcraft** is coded A-261 (A, 2 for the S, C ignored, 6 for the R, 1 for the F). It is not coded A-226.
*/
static constexpr char SOUNDEX_TABLE[26] = {'V', '1', '2', '3', 'V', '1', '2', 'N', 'V',
'2', '2', '4', '5', '5', 'V', '1', '2', '6',
'2', '3', 'V', '1', 'N', '2', 'V', '2'};
};

void register_function_soundex(SimpleFunctionFactory& factory) {
factory.register_function<FunctionSoundex>();
}

#include "common/compile_check_end.h"
} // namespace doris::vectorized
2 changes: 2 additions & 0 deletions be/src/vec/functions/simple_function_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ void register_function_dict_get_many(SimpleFunctionFactory& factory);
void register_function_ai(SimpleFunctionFactory& factory);
void register_function_score(SimpleFunctionFactory& factory);
void register_function_variant_type(SimpleFunctionFactory& factory);
void register_function_soundex(SimpleFunctionFactory& factory);

#if defined(BE_TEST) && !defined(BE_BENCHMARK)
void register_function_throw_exception(SimpleFunctionFactory& factory);
Expand Down Expand Up @@ -336,6 +337,7 @@ class SimpleFunctionFactory {
register_function_dict_get_many(instance);
register_function_ai(instance);
register_function_score(instance);
register_function_soundex(instance);
#if defined(BE_TEST) && !defined(BE_BENCHMARK)
register_function_throw_exception(instance);
#endif
Expand Down
69 changes: 69 additions & 0 deletions be/test/vec/function/function_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3607,4 +3607,73 @@ TEST(function_string_test, function_count_substring_test) {
check_function_all_arg_comb<DataTypeInt32, true>(func_name, input_types, data_set);
}
}

TEST(function_string_test, soundex_test) {
std::string func_name = "soundex";

{
InputTypeSet input_types = {PrimitiveType::TYPE_VARCHAR};

DataSet data_set = {
{{std::string("Doris")}, std::string("D620")},
{{std::string("ApacheDoris中文测试")}, std::string("A123")},
{{std::string("Robert")}, std::string("R163")},
{{std::string("Rupert")}, std::string("R163")},
{{std::string("Smith")}, std::string("S530")},
{{std::string("Smyth")}, std::string("S530")},
{{std::string("Johnson")}, std::string("J525")},
{{std::string("Jackson")}, std::string("J250")},
{{std::string("Ashcraft")}, std::string("A261")},
{{std::string("Ashcroft")}, std::string("A261")},
{{std::string("Washington")}, std::string("W252")},
{{std::string("Lee")}, std::string("L000")},
{{std::string("Gutierrez")}, std::string("G362")},
{{std::string("Pfister")}, std::string("P236")},
{{std::string("Honeyman")}, std::string("H555")},
{{std::string("Lloyd")}, std::string("L300")},
{{std::string("Tymczak")}, std::string("T522")},

{{std::string("A")}, std::string("A000")},
{{std::string("B")}, std::string("B000")},
{{std::string("Z")}, std::string("Z000")},

{{std::string("robert")}, std::string("R163")},
{{std::string("ROBERT")}, std::string("R163")},
{{std::string("RoBerT")}, std::string("R163")},

{{std::string("R@bert")}, std::string("R163")},
{{std::string("Rob3rt")}, std::string("R163")},
{{std::string("Rob-ert")}, std::string("R163")},
{{std::string("123Robert")}, std::string("R163")},
{{std::string("123")}, std::string("")},
{{std::string("@#$")}, std::string("")},
{{std::string(" ")}, std::string("")},
{{std::string("")}, std::string("")},
{{std::string("Ab_+ %*^cdefghijklmnopqrstuvwxyz")}, std::string("A123")},

{{std::string("Euler")}, std::string("E460")},
{{std::string("Gauss")}, std::string("G200")},
{{std::string("Hilbert")}, std::string("H416")},
{{std::string("Knuth")}, std::string("K530")},
{{std::string("Lloyd")}, std::string("L300")},
{{std::string("Lukasiewicz")}, std::string("L222")},

{{std::string("Huang")}, std::string("H520")},
{{std::string("Zhang")}, std::string("Z520")},
{{std::string("Wang")}, std::string("W520")}};

static_cast<void>(check_function<DataTypeString, true>(func_name, input_types, data_set));
}

{
InputTypeSet input_types = {PrimitiveType::TYPE_VARCHAR};

DataSet data_set = {{{std::string("中文测试")}, std::string("")},
{{std::string("abc 你好")}, std::string("")}};

static_cast<void>(check_function<DataTypeString, true>(func_name, input_types, data_set, -1,
-1, true));
}
}

} // namespace doris::vectorized
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm3sum;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm4Decrypt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm4Encrypt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Soundex;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Space;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SplitByChar;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SplitByRegexp;
Expand Down Expand Up @@ -918,6 +919,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(Sm3sum.class, "sm3sum"),
scalar(Sm4Decrypt.class, "sm4_decrypt"),
scalar(Sm4Encrypt.class, "sm4_encrypt"),
scalar(Soundex.class, "soundex"),
scalar(Space.class, "space"),
scalar(SplitByChar.class, "split_by_char"),
scalar(SplitByRegexp.class, "split_by_regexp"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.functions.scalar;

import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.VarcharType;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.List;

/**
* Scalar function 'Soundex'
*/
public class Soundex extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT).args(VarcharType.SYSTEM_DEFAULT)
);

/**
* constructor with 1 argument.
*/
public Soundex(Expression arg) {
super("soundex", arg);
}

/** constructor for withChildren and reuse signature */
private Soundex(ScalarFunctionParams functionParams) {
super(functionParams);
}

@Override
public Soundex withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Soundex(getFunctionParams(children));
}

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitSoundex(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm3sum;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm4Decrypt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Sm4Encrypt;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Soundex;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Space;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SplitByChar;
import org.apache.doris.nereids.trees.expressions.functions.scalar.SplitByRegexp;
Expand Down Expand Up @@ -2030,6 +2031,10 @@ default R visitSm4Encrypt(Sm4Encrypt sm4Encrypt, C context) {
return visitScalarFunction(sm4Encrypt, context);
}

default R visitSoundex(Soundex soundex, C context) {
return visitScalarFunction(soundex, context);
}

default R visitSpace(Space space, C context) {
return visitScalarFunction(space, context);
}
Expand Down
Loading