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

[FIX](function) fix size function for array map #23920

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix size function for array map
  • Loading branch information
amorynan committed Sep 5, 2023
commit e98a2fab5537157d6fc3bf89fa05e07ab94dbc58
2 changes: 0 additions & 2 deletions be/src/vec/functions/array/function_array_register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void register_function_array_shuffle(SimpleFunctionFactory&);
void register_function_array_exists(SimpleFunctionFactory&);
void register_function_array_element(SimpleFunctionFactory&);
void register_function_array_index(SimpleFunctionFactory&);
void register_function_array_size(SimpleFunctionFactory&);
void register_function_array_aggregation(SimpleFunctionFactory&);
void register_function_array_distance(SimpleFunctionFactory&);
void register_function_array_distinct(SimpleFunctionFactory&);
Expand Down Expand Up @@ -61,7 +60,6 @@ void register_function_array(SimpleFunctionFactory& factory) {
register_function_array_exists(factory);
register_function_array_element(factory);
register_function_array_index(factory);
register_function_array_size(factory);
register_function_array_aggregation(factory);
register_function_array_distance(factory);
register_function_array_distinct(factory);
Expand Down
30 changes: 0 additions & 30 deletions be/src/vec/functions/array/function_array_size.cpp

This file was deleted.

89 changes: 0 additions & 89 deletions be/src/vec/functions/array/function_array_size.h

This file was deleted.

59 changes: 0 additions & 59 deletions be/src/vec/functions/function_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,64 +146,6 @@ class FunctionMap : public IFunction {
}
};

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

/// Get function name.
String get_name() const override { return name; }

bool is_variadic() const override { return false; }

size_t get_number_of_arguments() const override { return 1; }

DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DataTypePtr datatype = arguments[0];
if (datatype->is_nullable()) {
datatype = assert_cast<const DataTypeNullable*>(datatype.get())->get_nested_type();
}
DCHECK(is_map(datatype)) << "first argument for function: " << name
<< " should be DataTypeMap";
return std::make_shared<DataTypeInt64>();
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) override {
const auto& [left_column, left_const] =
unpack_if_const(block.get_by_position(arguments[0]).column);
const ColumnMap* map_column = nullptr;
// const UInt8* map_null_map = nullptr;
if (left_column->is_nullable()) {
auto nullable_column = reinterpret_cast<const ColumnNullable*>(left_column.get());
map_column = check_and_get_column<ColumnMap>(nullable_column->get_nested_column());
// map_null_map = nullable_column->get_null_map_column().get_data().data();
} else {
map_column = check_and_get_column<ColumnMap>(*left_column.get());
}
if (!map_column) {
return Status::RuntimeError("unsupported types for function {}({})", get_name(),
block.get_by_position(arguments[0]).type->get_name());
}

auto dst_column = ColumnInt64::create(input_rows_count);
auto& dst_data = dst_column->get_data();

if (left_const) {
for (size_t i = 0; i < map_column->size(); i++) {
dst_data[i] = map_column->size_at(0);
}
} else {
for (size_t i = 0; i < map_column->size(); i++) {
dst_data[i] = map_column->size_at(i);
}
}

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

template <bool is_key>
class FunctionMapContains : public IFunction {
public:
Expand Down Expand Up @@ -354,7 +296,6 @@ class FunctionMapEntries : public IFunction {

void register_function_map(SimpleFunctionFactory& factory) {
factory.register_function<FunctionMap>();
factory.register_function<FunctionMapSize>();
factory.register_function<FunctionMapContains<true>>();
factory.register_function<FunctionMapContains<false>>();
factory.register_function<FunctionMapEntries<true>>();
Expand Down
107 changes: 107 additions & 0 deletions be/src/vec/functions/function_size.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 "simple_function_factory.h"
#include "vec/columns/column_array.h"
#include "vec/columns/column_map.h"
#include "vec/data_types/data_type_array.h"
#include "vec/data_types/data_type_number.h"
#include "vec/functions/array/function_array_utils.h"
#include "vec/functions/function.h"
#include "vec/functions/function_helpers.h"

namespace doris::vectorized {

// size function for size with map and array
class FunctionSize : public IFunction {
public:
static constexpr auto name = "size";
static FunctionPtr create() { return std::make_shared<FunctionSize>(); }
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 {
DataTypePtr datatype = arguments[0];
if (datatype->is_nullable()) {
datatype = assert_cast<const DataTypeNullable*>(datatype.get())->get_nested_type();
}
DCHECK(is_map(datatype) || is_array(datatype)) << "first argument for function: " << name
<< " should be DataTypeMap or DataTypeArray";
return std::make_shared<DataTypeInt64>();
}

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) override {
const auto& [left_column, left_const] =
unpack_if_const(block.get_by_position(arguments[0]).column);
const auto type = block.get_by_position(arguments[0]).type;
const ColumnArray* array_column = nullptr;
const ColumnMap* map_column = nullptr;
if (is_array(type)) {
if (left_column->is_nullable()) {
auto nullable_column = reinterpret_cast<const ColumnNullable*>(left_column.get());
array_column =
check_and_get_column<ColumnArray>(nullable_column->get_nested_column());
} else {
array_column = check_and_get_column<ColumnArray>(*left_column.get());
}
} else if (is_map(type)) {
if (left_column->is_nullable()) {
auto nullable_column = reinterpret_cast<const ColumnNullable*>(left_column.get());
map_column = check_and_get_column<ColumnMap>(nullable_column->get_nested_column());
} else {
map_column = check_and_get_column<ColumnMap>(*left_column.get());
}
}

auto dst_column = ColumnInt64::create(input_rows_count);
auto& dst_data = dst_column->get_data();

if (left_const && map_column) {
for (size_t i = 0; i < map_column->size(); i++) {
dst_data[i] = map_column->size_at(0);
}
} else if (left_const && array_column) {
for (size_t i = 0; i < array_column->size(); i++) {
dst_data[i] = array_column->size_at(0);
}
} else if (map_column) {
for (size_t i = 0; i < map_column->size(); i++) {
dst_data[i] = map_column->size_at(i);
}
} else if (array_column) {
for (size_t i = 0; i < array_column->size(); i++) {
dst_data[i] = array_column->size_at(i);
}
} else {
return Status::RuntimeError("unsupported types for function {}({})", get_name(),
block.get_by_position(arguments[0]).type->get_name());
}

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

void register_function_size(SimpleFunctionFactory& factory) {
factory.register_function<FunctionSize>();
factory.register_alias(FunctionSize::name, "map_size");
factory.register_alias(FunctionSize::name, "cardinality");
factory.register_alias(FunctionSize::name, "array_size");
}
} // 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 @@ -32,6 +32,7 @@ namespace doris::vectorized {

class SimpleFunctionFactory;

void register_function_size(SimpleFunctionFactory& factory);
void register_function_comparison(SimpleFunctionFactory& factory);
void register_function_comparison_eq_for_null(SimpleFunctionFactory& factory);
void register_function_hll_cardinality(SimpleFunctionFactory& factory);
Expand Down Expand Up @@ -207,6 +208,7 @@ class SimpleFunctionFactory {
static std::once_flag oc;
static SimpleFunctionFactory instance;
std::call_once(oc, []() {
register_function_size(instance);
register_function_bitmap(instance);
register_function_quantile_state(instance);
register_function_bitmap_variadic(instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
4 ["aaa", "bbb", NULL, "fff"] ["aaa", "bbb", NULL, "fff"]

-- !sql --
4 ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"] ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"]

-- !sql --
4 ["aaa", "bbb", NULL, "fff"] ["aaa", "bbb", NULL, "fff"]

-- !sql --
4 ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"] ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"]

-- !sql --
4 ["aaa", "bbb", NULL, "fff"] ["aaa", "bbb", NULL, "fff"]

-- !sql --
4 ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"] ["2020-01-02", "2021-01-01", "2022-01-03", "1996-04-17"]

-- !sql --
2

-- !sql --
2

-- !select_00 --
1 3 3
5 3 0
5 3 3
4 3 2

Loading