Skip to content
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

[function](feature) Implement CRC32 function #38204

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 26 additions & 2 deletions be/src/vec/functions/function_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct NameQuoteImpl {
}
};

struct NameStringLenght {
struct NameStringLength {
static constexpr auto name = "length";
};

Expand All @@ -104,6 +104,28 @@ struct StringLengthImpl {
}
};

struct NameCrc32 {
static constexpr auto name = "crc32";
};

struct Crc32Impl {
using ReturnType = DataTypeInt64;
static constexpr auto TYPE_INDEX = TypeIndex::String;
using Type = String;
using ReturnColumnType = ColumnVector<Int64>;

static Status vector(const ColumnString::Chars& data, const ColumnString::Offsets& offsets,
PaddedPODArray<Int64>& res) {
auto size = offsets.size();
res.resize(size);
for (int i = 0; i < size; ++i) {
res[i] = crc32_z(0L, (const unsigned char*)data.data() + offsets[i - 1],
offsets[i] - offsets[i - 1]);
}
return Status::OK();
}
};

struct NameStringUtf8Length {
static constexpr auto name = "char_length";
};
Expand Down Expand Up @@ -941,7 +963,8 @@ using StringFindInSetImpl = StringFunctionImpl<LeftDataType, RightDataType, Find

// ready for regist function
using FunctionStringASCII = FunctionUnaryToType<StringASCII, NameStringASCII>;
using FunctionStringLength = FunctionUnaryToType<StringLengthImpl, NameStringLenght>;
using FunctionStringLength = FunctionUnaryToType<StringLengthImpl, NameStringLength>;
using FunctionCrc32 = FunctionUnaryToType<Crc32Impl, NameCrc32>;
using FunctionStringUTF8Length = FunctionUnaryToType<StringUtf8LengthImpl, NameStringUtf8Length>;
using FunctionStringSpace = FunctionUnaryToType<StringSpace, NameStringSpace>;
using FunctionStringStartsWith =
Expand Down Expand Up @@ -976,6 +999,7 @@ using FunctionStringRPad = FunctionStringPad<StringRPad>;
void register_function_string(SimpleFunctionFactory& factory) {
factory.register_function<FunctionStringASCII>();
factory.register_function<FunctionStringLength>();
factory.register_function<FunctionCrc32>();
factory.register_function<FunctionStringUTF8Length>();
factory.register_function<FunctionStringSpace>();
factory.register_function<FunctionStringStartsWith>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cosh;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CosineDistance;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Crc32;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateNamedStruct;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateStruct;
Expand Down Expand Up @@ -731,6 +732,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(Least.class, "least"),
scalar(Left.class, "left"),
scalar(Length.class, "length"),
scalar(Crc32.class, "crc32"),
scalar(Like.class, "like"),
scalar(Ln.class, "ln"),
scalar(Locate.class, "locate"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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.BigIntType;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.VarcharType;

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

import java.util.List;

/**
* ScalarFunction 'crc32'. This class is generated by GenerateFunction.
*/
public class Crc32 extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(BigIntType.INSTANCE).args(VarcharType.SYSTEM_DEFAULT),
FunctionSignature.ret(BigIntType.INSTANCE).args(StringType.INSTANCE)
);

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

/**
* withChildren.
*/
@Override
public Crc32 withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() == 1);
return new Crc32(children.get(0));
}

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

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitCrc32(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.Cosh;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CosineDistance;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CountEqual;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Crc32;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateNamedStruct;
import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateStruct;
Expand Down Expand Up @@ -1476,6 +1477,10 @@ default R visitLength(Length length, C context) {
return visitScalarFunction(length, context);
}

default R visitCrc32(Crc32 crc32, C context) {
return visitScalarFunction(crc32, context);
}

default R visitLike(Like like, C context) {
return visitStringRegexPredicate(like, context);
}
Expand Down
2 changes: 2 additions & 0 deletions gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,7 @@
[['rpad'], 'VARCHAR', ['VARCHAR', 'INT', 'VARCHAR'], 'ALWAYS_NULLABLE'],
[['append_trailing_char_if_absent'], 'VARCHAR', ['VARCHAR', 'VARCHAR'], 'ALWAYS_NULLABLE'],
[['length'], 'INT', ['VARCHAR'], ''],
[['crc32'], 'BIGINT', ['VARCHAR'], ''],
[['bit_length'], 'INT', ['VARCHAR'], ''],

[['char_length', 'character_length'], 'INT', ['VARCHAR'], ''],
Expand Down Expand Up @@ -1685,6 +1686,7 @@
[['rpad'], 'STRING', ['STRING', 'INT', 'STRING'], 'ALWAYS_NULLABLE'],
[['append_trailing_char_if_absent'], 'STRING', ['STRING', 'STRING'], 'ALWAYS_NULLABLE'],
[['length'], 'INT', ['STRING'], ''],
[['crc32'], 'BIGINT', ['STRING'], ''],
[['bit_length'], 'INT', ['STRING'], ''],

[['char_length', 'character_length'], 'INT', ['STRING'], ''],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,15 @@ bb

-- !sql --

-- !crc32_1 --
348606243

-- !crc32_2 --
130583814

-- !crc32_3 --
2707236321

-- !crc32_4 --
\N

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

// sql "DROP TABLE ${tbName};"
qt_crc32_1 "select crc32(\"DORIS\");"
qt_crc32_2 "select crc32(\"APACHE DORIS\");"
qt_crc32_3 "select crc32(10);"
qt_crc32_4 "select crc32(NULL);"
}
Loading