Skip to content
Open
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
134 changes: 134 additions & 0 deletions be/src/vec/functions/function_interval.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// 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 <algorithm>
#include <cstdint>
#include <limits>
#include <memory>
#include <vector>

#include "vec/columns/column.h"
#include "vec/columns/column_const.h"
#include "vec/columns/column_nullable.h"
#include "vec/columns/column_vector.h"
#include "vec/common/assert_cast.h"
#include "vec/core/block.h"
#include "vec/core/column_numbers.h"
#include "vec/core/column_with_type_and_name.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/data_type_number.h"
#include "vec/functions/function.h"
#include "vec/functions/simple_function_factory.h"

namespace doris::vectorized {

class FunctionInterval : public IFunction {
public:
static constexpr auto name = "interval";
static FunctionPtr create() { return std::make_shared<FunctionInterval>(); }

String get_name() const override { return name; }
bool is_variadic() const override { return true; }
size_t get_number_of_arguments() const override { return 0; }

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

Status execute_impl(FunctionContext* /*context*/, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
if (arguments.size() < 2) [[unlikely]] {
return Status::InternalError("interval requires at least 2 arguments");
}

auto res_col = ColumnInt32::create();
auto& res_data = res_col->get_data();
res_data.resize(input_rows_count);

auto compare_cwn = block.get_by_position(arguments[0]);
ColumnPtr compare_col_ptr = ColumnPtr {};
bool compare_is_const = false;
compare_is_const = is_column_const(*block.get_by_position(arguments[0]).column);
default_preprocess_parameter_columns(&compare_col_ptr, &compare_is_const, {0}, block,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default_preprocess_parameter_columns should process parameter columns, that is, column 1 .. n-1

arguments);

switch (compare_cwn.type->get_primitive_type()) {
case PrimitiveType::TYPE_TINYINT:
compute_interval<ColumnInt8>(block, arguments, *compare_col_ptr, compare_is_const,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then, you can reference FunctionMonthsBetween, there's 3 possibility: data const, parameters all const, non const

res_data);
break;
case PrimitiveType::TYPE_SMALLINT:
compute_interval<ColumnInt16>(block, arguments, *compare_col_ptr, compare_is_const,
res_data);
break;
case PrimitiveType::TYPE_INT:
compute_interval<ColumnInt32>(block, arguments, *compare_col_ptr, compare_is_const,
res_data);
break;
case PrimitiveType::TYPE_BIGINT:
compute_interval<ColumnInt64>(block, arguments, *compare_col_ptr, compare_is_const,
res_data);
break;
case PrimitiveType::TYPE_LARGEINT:
compute_interval<ColumnInt128>(block, arguments, *compare_col_ptr, compare_is_const,
res_data);
break;
default:
[[unlikely]] return Status::InternalError(
"interval only supports integer numeric types for the first argument");
}

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

private:
template <typename ColType>
static void compute_interval(Block& block, const ColumnNumbers& arguments,
const IColumn& compare_col, bool compare_is_const,
PaddedPODArray<Int32>& res) {
const auto& compare_data = assert_cast<const ColType&>(compare_col).get_data();
const size_t rows = res.size();
const size_t num_thresholds = arguments.size() - 1;

for (size_t row = 0; row < rows; ++row) {
auto compare_val = compare_data[index_check_const(row, compare_is_const)];

std::vector<typename ColType::value_type> thresholds;
thresholds.reserve(num_thresholds);

for (size_t i = 1; i < arguments.size(); ++i) {
const auto& col_cwn = block.get_by_position(arguments[i]);
ColumnPtr col_ptr = col_cwn.column;
bool is_const = false;
std::tie(col_ptr, is_const) = unpack_if_const(col_ptr);
const auto& th_col = assert_cast<const ColType&>(*col_ptr);
thresholds.push_back(th_col.get_data()[index_check_const(row, is_const)]);
}

auto it = std::upper_bound(thresholds.begin(), thresholds.end(), compare_val);
res[row] = static_cast<Int32>(it - thresholds.begin());
}
}
};

void register_function_interval(SimpleFunctionFactory& factory) {
factory.register_function<FunctionInterval>();
}

} // 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 @@ -102,6 +102,7 @@ void register_function_geo(SimpleFunctionFactory& factory);
void register_function_multi_string_position(SimpleFunctionFactory& factory);
void register_function_multi_string_search(SimpleFunctionFactory& factory);
void register_function_width_bucket(SimpleFunctionFactory& factory);
void register_function_interval(SimpleFunctionFactory& factory);
void register_function_ignore(SimpleFunctionFactory& factory);
void register_function_encryption(SimpleFunctionFactory& factory);
void register_function_regexp_extract(SimpleFunctionFactory& factory);
Expand Down Expand Up @@ -327,6 +328,7 @@ class SimpleFunctionFactory {
register_function_multi_string_position(instance);
register_function_multi_string_search(instance);
register_function_width_bucket(instance);
register_function_interval(instance);
register_function_match(instance);
register_function_ip(instance);
register_function_tokenize(instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.InnerProduct;
import org.apache.doris.nereids.trees.expressions.functions.scalar.InnerProductApproximate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Instr;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Interval;
import org.apache.doris.nereids.trees.expressions.functions.scalar.InttoUuid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ipv4CIDRToRange;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ipv4NumToString;
Expand Down Expand Up @@ -771,6 +772,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
scalar(InnerProductApproximate.class, "inner_product_approximate"),
scalar(InnerProduct.class, "inner_product"),
scalar(Instr.class, "instr"),
scalar(Interval.class, "interval"),
scalar(InttoUuid.class, "int_to_uuid"),
scalar(Ipv4NumToString.class, "ipv4_num_to_string", "inet_ntoa"),
scalar(Ipv4StringToNum.class, "ipv4_string_to_num"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.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;

/**
* ScalarFunction 'interval'.
*/
public class Interval extends ScalarFunction implements ExplicitlyCastableSignature, PropagateNullable {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(IntegerType.INSTANCE).varArgs(TinyIntType.INSTANCE, TinyIntType.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).varArgs(SmallIntType.INSTANCE, SmallIntType.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).varArgs(IntegerType.INSTANCE, IntegerType.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).varArgs(BigIntType.INSTANCE, BigIntType.INSTANCE),
FunctionSignature.ret(IntegerType.INSTANCE).varArgs(LargeIntType.INSTANCE, LargeIntType.INSTANCE));

public Interval(Expression arg, Expression... varArgs) {
super("interval", ExpressionUtils.mergeArguments(arg, varArgs));
}

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

@Override
public List<FunctionSignature> getSignatures() {
return SIGNATURES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
import org.apache.doris.nereids.trees.expressions.functions.scalar.InnerProduct;
import org.apache.doris.nereids.trees.expressions.functions.scalar.InnerProductApproximate;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Instr;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Interval;
import org.apache.doris.nereids.trees.expressions.functions.scalar.InttoUuid;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ipv4CIDRToRange;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Ipv4NumToString;
Expand Down Expand Up @@ -1429,6 +1430,10 @@ default R visitInstr(Instr instr, C context) {
return visitScalarFunction(instr, context);
}

default R visitInterval(Interval interval, C context) {
return visitScalarFunction(interval, context);
}

default R visitIpv4NumToString(Ipv4NumToString ipv4NumToString, C context) {
return visitScalarFunction(ipv4NumToString, context);
}
Expand Down
Loading