-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Feature](func) Support function soundex #55731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4096eb9
[Feature](func) Support function soundex
linrrzqqq 8b4e835
fix
linrrzqqq e2cc5ed
mv ut to string_test && add chinese test
linrrzqqq 21e73c9
add nonASCII test
linrrzqqq e5937a3
fe fold
linrrzqqq 42de590
1
linrrzqqq 006038e
add fold constant test
linrrzqqq c62f342
add fold constant regression test
linrrzqqq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
linrrzqqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
block.replace_by_position(result, std::move(res_column)); | ||
return Status::OK(); | ||
} | ||
|
||
private: | ||
std::string calculate_soundex(const StringRef& ref) const { | ||
if (ref.empty()) { | ||
linrrzqqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return ""; | ||
} | ||
|
||
std::string result; | ||
linrrzqqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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"); | ||
linrrzqqq marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
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']; | ||
linrrzqqq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...re/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/Soundex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.