Skip to content

Commit

Permalink
[feature](function)support bit_test function (#42099)
Browse files Browse the repository at this point in the history
support bit_test function
doc: apache/doris-website#1214
  • Loading branch information
zhangstar333 authored Oct 22, 2024
1 parent 4c09663 commit e183114
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 1 deletion.
156 changes: 156 additions & 0 deletions be/src/vec/functions/function_bit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// 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 <bit>
#include <bitset>

#include "common/status.h"
#include "vec/columns/column.h"
#include "vec/columns/column_vector.h"
#include "vec/common/assert_cast.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_number.h"
#include "vec/functions/cast_type_to_either.h"
#include "vec/functions/simple_function_factory.h"

namespace doris::vectorized {

class FunctionBitTest : public IFunction {
public:
static constexpr auto name = "bit_test";

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

String get_name() const override { return name; }

size_t get_number_of_arguments() const override { return 0; }

bool is_variadic() const override { return true; }

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

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) const override {
bool valid =
cast_type(block.get_by_position(arguments[0]).type.get(), [&](const auto& type) {
using DataType = std::decay_t<decltype(type)>;
using T = typename DataType::FieldType;
if (auto col = check_and_get_column<ColumnVector<T>>(
block.get_by_position(arguments[0]).column.get()) ||
is_column_const(*block.get_by_position(arguments[0]).column)) {
execute_inner<T>(block, arguments, result, input_rows_count);
return true;
}
return false;
});
if (!valid) {
return Status::RuntimeError(
"{}'s argument does not match the expected data type, type: {}, column: {}",
get_name(), block.get_by_position(arguments[0]).type->get_name(),
block.get_by_position(arguments[0]).column->dump_structure());
}
return Status::OK();
}

template <typename F>
static bool cast_type(const IDataType* type, F&& f) {
return cast_type_to_either<DataTypeInt8, DataTypeInt16, DataTypeInt32, DataTypeInt64,
DataTypeInt128>(type, std::forward<F>(f));
}

template <typename T>
void execute_inner(Block& block, const ColumnNumbers& arguments, size_t result,
size_t input_rows_count) const {
size_t argument_size = arguments.size();
std::vector<ColumnPtr> argument_columns(argument_size);
auto result_data_column = ColumnInt8::create(input_rows_count, 1);
auto& res_data = result_data_column->get_data();

// maybe most user is bit_test(column, const), so only handle this case
if (argument_size == 2) {
std::vector<uint8_t> is_consts(argument_size);
std::tie(argument_columns[0], is_consts[0]) =
unpack_if_const(block.get_by_position(arguments[0]).column);
std::tie(argument_columns[1], is_consts[1]) =
unpack_if_const(block.get_by_position(arguments[1]).column);
execute_for_two_argument<T>(argument_columns, is_consts, res_data, input_rows_count);
} else {
for (size_t i = 0; i < argument_size; ++i) {
argument_columns[i] = block.get_by_position(arguments[i])
.column->convert_to_full_column_if_const();
}
execute_for_others_arg<T>(argument_columns, res_data, argument_size, input_rows_count);
}

block.replace_by_position(result, std::move(result_data_column));
}

template <typename T>
void execute_for_two_argument(std::vector<ColumnPtr>& argument_columns,
std::vector<uint8_t>& is_consts, ColumnInt8::Container& res_data,
size_t input_rows_count) const {
const auto& first_column_data =
assert_cast<const ColumnVector<T>&>(*argument_columns[0].get()).get_data();
const auto& second_column_data =
assert_cast<const ColumnVector<T>&>(*argument_columns[1].get()).get_data();
for (int i = 0; i < input_rows_count; ++i) {
auto first_value = first_column_data[index_check_const(i, is_consts[0])];
auto second_value = second_column_data[index_check_const(i, is_consts[1])];
// the pos is invalid, set result = 0
if (second_value < 0 || second_value >= sizeof(T) * 8) {
res_data[i] = 0;
continue;
}
res_data[i] = ((first_value >> second_value) & 1);
}
}

template <typename T>
void execute_for_others_arg(std::vector<ColumnPtr>& argument_columns,
ColumnInt8::Container& res_data, size_t argument_size,
size_t input_rows_count) const {
const auto& first_column_data =
assert_cast<const ColumnVector<T>&>(*argument_columns[0].get()).get_data();
for (int i = 0; i < input_rows_count; ++i) {
auto first_value = first_column_data[i];
for (int col = 1; col < argument_size; ++col) {
const auto& arg_column_data =
assert_cast<const ColumnVector<T>&>(*argument_columns[col].get())
.get_data();
// the pos is invalid, set result = 0
if (arg_column_data[i] < 0 || arg_column_data[i] >= sizeof(T) * 8) {
res_data[i] = 0;
break;
}
// if one of pos & result is 0, could set res = 0, and return directly.
if (!((first_value >> arg_column_data[i]) & 1)) {
res_data[i] = 0;
break;
}
}
}
}
};

void register_function_bit_test(SimpleFunctionFactory& factory) {
factory.register_function<FunctionBitTest>();
factory.register_alias("bit_test", "bit_test_all");
}

} // 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 @@ -108,6 +108,7 @@ void register_function_ip(SimpleFunctionFactory& factory);
void register_function_multi_match(SimpleFunctionFactory& factory);
void register_function_split_by_regexp(SimpleFunctionFactory& factory);
void register_function_assert_true(SimpleFunctionFactory& factory);
void register_function_bit_test(SimpleFunctionFactory& factory);

class SimpleFunctionFactory {
using Creator = std::function<FunctionBuilderPtr()>;
Expand Down Expand Up @@ -290,6 +291,7 @@ class SimpleFunctionFactory {
register_function_multi_match(instance);
register_function_split_by_regexp(instance);
register_function_assert_true(instance);
register_function_bit_test(instance);
});
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitShiftLeft;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitShiftRight;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitTest;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAnd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNot;
Expand Down Expand Up @@ -578,6 +579,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(BitmapXorCount.class, "bitmap_xor_count"),
scalar(BitShiftLeft.class, "bit_shift_left"),
scalar(BitShiftRight.class, "bit_shift_right"),
scalar(BitTest.class, "bit_test", "bit_test_all"),
scalar(Cardinality.class, "array_size", "cardinality", "size"),
scalar(Cbrt.class, "cbrt"),
scalar(Ceil.class, "ceil", "ceiling"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.IntegerType;
import org.apache.doris.nereids.types.LargeIntType;
import org.apache.doris.nereids.types.SmallIntType;
import org.apache.doris.nereids.types.TinyIntType;
import org.apache.doris.nereids.util.ExpressionUtils;

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

import java.util.List;

/** BitTest function */

public class BitTest extends ScalarFunction
implements UnaryExpression, ExplicitlyCastableSignature, PropagateNullable {
public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(TinyIntType.INSTANCE).varArgs(TinyIntType.INSTANCE, TinyIntType.INSTANCE),
FunctionSignature.ret(TinyIntType.INSTANCE).varArgs(SmallIntType.INSTANCE, SmallIntType.INSTANCE),
FunctionSignature.ret(TinyIntType.INSTANCE).varArgs(IntegerType.INSTANCE, IntegerType.INSTANCE),
FunctionSignature.ret(TinyIntType.INSTANCE).varArgs(LargeIntType.INSTANCE, LargeIntType.INSTANCE),
FunctionSignature.ret(TinyIntType.INSTANCE).varArgs(BigIntType.INSTANCE, BigIntType.INSTANCE));

/**
* constructor with 2 or more arguments.
*/
public BitTest(Expression arg0, Expression arg1, Expression... varArgs) {
super("bit_test", ExpressionUtils.mergeArguments(arg0, arg1, varArgs));
}

/**
* withChildren.
*/
@Override
public BitTest withChildren(List<Expression> children) {
Preconditions.checkArgument(children.size() >= 2);
return new BitTest(children.get(0), children.get(1),
children.subList(2, children.size()).toArray(new Expression[0]));
}

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

@Override
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
return visitor.visitBitTest(this, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitLength;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitShiftLeft;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitShiftRight;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitTest;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAnd;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndCount;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapAndNot;
Expand Down Expand Up @@ -869,6 +870,10 @@ default R visitBitShiftRight(BitShiftRight bitShiftRight, C context) {
return visitScalarFunction(bitShiftRight, context);
}

default R visitBitTest(BitTest bitTest, C context) {
return visitScalarFunction(bitTest, context);
}

default R visitCardinality(Cardinality cardinality, C context) {
return visitScalarFunction(cardinality, context);
}
Expand Down
7 changes: 6 additions & 1 deletion gensrc/script/doris_builtins_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
[['bitnot'], 'LARGEINT', ['LARGEINT'], ''],

[['bit_shift_left'], 'BIGINT', ['BIGINT', 'TINYINT'], ''],
[['bit_shift_right'], 'BIGINT', ['BIGINT', 'TINYINT'], '']
[['bit_shift_right'], 'BIGINT', ['BIGINT', 'TINYINT'], ''],
[['bit_test','bit_test_all'], 'TINYINT', ['TINYINT','TINYINT','...'], ''],
[['bit_test','bit_test_all'], 'TINYINT', ['SMALLINT','SMALLINT','...'], ''],
[['bit_test','bit_test_all'], 'TINYINT', ['INT','INT','...'], ''],
[['bit_test','bit_test_all'], 'TINYINT', ['BIGINT','BIGINT','...'], ''],
[['bit_test','bit_test_all'], 'TINYINT', ['LARGEINT','LARGEINT','...'], '']
],

# map functions
Expand Down
Loading

0 comments on commit e183114

Please sign in to comment.