Skip to content

Commit 78de4ad

Browse files
authored
[function](feature) Implement CRC32 function (#38204)
1 parent 33c878f commit 78de4ad

File tree

7 files changed

+122
-2
lines changed

7 files changed

+122
-2
lines changed

be/src/vec/functions/function_string.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct NameQuoteImpl {
8282
}
8383
};
8484

85-
struct NameStringLenght {
85+
struct NameStringLength {
8686
static constexpr auto name = "length";
8787
};
8888

@@ -104,6 +104,28 @@ struct StringLengthImpl {
104104
}
105105
};
106106

107+
struct NameCrc32 {
108+
static constexpr auto name = "crc32";
109+
};
110+
111+
struct Crc32Impl {
112+
using ReturnType = DataTypeInt64;
113+
static constexpr auto TYPE_INDEX = TypeIndex::String;
114+
using Type = String;
115+
using ReturnColumnType = ColumnVector<Int64>;
116+
117+
static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
118+
PaddedPODArray<Int64>& res) {
119+
auto size = offsets.size();
120+
res.resize(size);
121+
for (int i = 0; i < size; ++i) {
122+
res[i] = crc32_z(0L, (const unsigned char*)data.data() + offsets[i - 1],
123+
offsets[i] - offsets[i - 1]);
124+
}
125+
return Status::OK();
126+
}
127+
};
128+
107129
struct NameStringUtf8Length {
108130
static constexpr auto name = "char_length";
109131
};
@@ -942,7 +964,8 @@ using StringFindInSetImpl = StringFunctionImpl<LeftDataType, RightDataType, Find
942964

943965
// ready for regist function
944966
using FunctionStringASCII = FunctionUnaryToType<StringASCII, NameStringASCII>;
945-
using FunctionStringLength = FunctionUnaryToType<StringLengthImpl, NameStringLenght>;
967+
using FunctionStringLength = FunctionUnaryToType<StringLengthImpl, NameStringLength>;
968+
using FunctionCrc32 = FunctionUnaryToType<Crc32Impl, NameCrc32>;
946969
using FunctionStringUTF8Length = FunctionUnaryToType<StringUtf8LengthImpl, NameStringUtf8Length>;
947970
using FunctionStringSpace = FunctionUnaryToType<StringSpace, NameStringSpace>;
948971
using FunctionStringStartsWith =
@@ -977,6 +1000,7 @@ using FunctionStringRPad = FunctionStringPad<StringRPad>;
9771000
void register_function_string(SimpleFunctionFactory& factory) {
9781001
factory.register_function<FunctionStringASCII>();
9791002
factory.register_function<FunctionStringLength>();
1003+
factory.register_function<FunctionCrc32>();
9801004
factory.register_function<FunctionStringUTF8Length>();
9811005
factory.register_function<FunctionStringSpace>();
9821006
factory.register_function<FunctionStringStartsWith>();

fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cosh;
126126
import org.apache.doris.nereids.trees.expressions.functions.scalar.CosineDistance;
127127
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
128+
import org.apache.doris.nereids.trees.expressions.functions.scalar.Crc32;
128129
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap;
129130
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateNamedStruct;
130131
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateStruct;
@@ -732,6 +733,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
732733
scalar(Least.class, "least"),
733734
scalar(Left.class, "left"),
734735
scalar(Length.class, "length"),
736+
scalar(Crc32.class, "crc32"),
735737
scalar(Like.class, "like"),
736738
scalar(Ln.class, "ln"),
737739
scalar(Locate.class, "locate"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.trees.expressions.functions.scalar;
19+
20+
import org.apache.doris.catalog.FunctionSignature;
21+
import org.apache.doris.nereids.trees.expressions.Expression;
22+
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
23+
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
24+
import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
25+
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
26+
import org.apache.doris.nereids.types.BigIntType;
27+
import org.apache.doris.nereids.types.StringType;
28+
import org.apache.doris.nereids.types.VarcharType;
29+
30+
import com.google.common.base.Preconditions;
31+
import com.google.common.collect.ImmutableList;
32+
33+
import java.util.List;
34+
35+
/**
36+
* ScalarFunction 'crc32'. This class is generated by GenerateFunction.
37+
*/
38+
public class Crc32 extends ScalarFunction
39+
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {
40+
41+
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
42+
FunctionSignature.ret(BigIntType.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
43+
FunctionSignature.ret(BigIntType.INSTANCE).args(StringType.INSTANCE)
44+
);
45+
46+
/**
47+
* constructor with 1 argument.
48+
*/
49+
public Crc32(Expression arg) {
50+
super("crc32", arg);
51+
}
52+
53+
/**
54+
* withChildren.
55+
*/
56+
@Override
57+
public Crc32 withChildren(List<Expression> children) {
58+
Preconditions.checkArgument(children.size() == 1);
59+
return new Crc32(children.get(0));
60+
}
61+
62+
@Override
63+
public List<FunctionSignature> getSignatures() {
64+
return SIGNATURES;
65+
}
66+
67+
@Override
68+
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
69+
return visitor.visitCrc32(this, context);
70+
}
71+
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cosh;
133133
import org.apache.doris.nereids.trees.expressions.functions.scalar.CosineDistance;
134134
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
135+
import org.apache.doris.nereids.trees.expressions.functions.scalar.Crc32;
135136
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap;
136137
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateNamedStruct;
137138
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateStruct;
@@ -1477,6 +1478,10 @@ default R visitLength(Length length, C context) {
14771478
return visitScalarFunction(length, context);
14781479
}
14791480

1481+
default R visitCrc32(Crc32 crc32, C context) {
1482+
return visitScalarFunction(crc32, context);
1483+
}
1484+
14801485
default R visitLike(Like like, C context) {
14811486
return visitStringRegexPredicate(like, context);
14821487
}

gensrc/script/doris_builtins_functions.py

+2
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@
16191619
[['rpad'], 'VARCHAR', ['VARCHAR', 'INT', 'VARCHAR'], 'ALWAYS_NULLABLE'],
16201620
[['append_trailing_char_if_absent'], 'VARCHAR', ['VARCHAR', 'VARCHAR'], 'ALWAYS_NULLABLE'],
16211621
[['length'], 'INT', ['VARCHAR'], ''],
1622+
[['crc32'], 'BIGINT', ['VARCHAR'], ''],
16221623
[['bit_length'], 'INT', ['VARCHAR'], ''],
16231624

16241625
[['char_length', 'character_length'], 'INT', ['VARCHAR'], ''],
@@ -1685,6 +1686,7 @@
16851686
[['rpad'], 'STRING', ['STRING', 'INT', 'STRING'], 'ALWAYS_NULLABLE'],
16861687
[['append_trailing_char_if_absent'], 'STRING', ['STRING', 'STRING'], 'ALWAYS_NULLABLE'],
16871688
[['length'], 'INT', ['STRING'], ''],
1689+
[['crc32'], 'BIGINT', ['STRING'], ''],
16881690
[['bit_length'], 'INT', ['STRING'], ''],
16891691

16901692
[['char_length', 'character_length'], 'INT', ['STRING'], ''],

regression-test/data/query_p0/sql_functions/string_functions/test_string_function_like.out

+12
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,15 @@ bb
245245

246246
-- !sql --
247247

248+
-- !crc32_1 --
249+
348606243
250+
251+
-- !crc32_2 --
252+
130583814
253+
254+
-- !crc32_3 --
255+
2707236321
256+
257+
-- !crc32_4 --
258+
\N
259+

regression-test/suites/query_p0/sql_functions/string_functions/test_string_function_like.groovy

+4
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,8 @@ suite("test_string_function_like") {
9090
qt_sql "SELECT k FROM ${tbName} WHERE NOT LIKE(k, \"%\") ORDER BY k;"
9191

9292
// sql "DROP TABLE ${tbName};"
93+
qt_crc32_1 "select crc32(\"DORIS\");"
94+
qt_crc32_2 "select crc32(\"APACHE DORIS\");"
95+
qt_crc32_3 "select crc32(10);"
96+
qt_crc32_4 "select crc32(NULL);"
9397
}

0 commit comments

Comments
 (0)