Skip to content
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
1 change: 1 addition & 0 deletions be/src/vec/aggregate_functions/aggregate_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class IDataType;

struct AggregateFunctionAttr {
bool enable_decimal256 {false};
std::vector<std::pair<std::string, bool>> column_infos;
};

template <bool nullable, typename ColVecType>
Expand Down
29 changes: 29 additions & 0 deletions be/src/vec/aggregate_functions/aggregate_function_approx_top.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

#pragma once

#include "vec/core/types.h"

namespace doris::vectorized {

class AggregateFunctionApproxTop {
public:
static inline constexpr UInt64 TOP_K_MAX_SIZE = 0xFFFFFF;
};

} // namespace doris::vectorized
85 changes: 85 additions & 0 deletions be/src/vec/aggregate_functions/aggregate_function_approx_top_k.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 "vec/aggregate_functions/aggregate_function_approx_top_k.h"

#include "common/exception.h"
#include "vec/aggregate_functions/aggregate_function_simple_factory.h"
#include "vec/aggregate_functions/helpers.h"
#include "vec/data_types/data_type.h"

namespace doris::vectorized {

int32_t is_valid_const_columns(const std::vector<bool>& is_const_columns) {
int32_t true_count = 0;
bool found_false_after_true = false;
for (int32_t i = is_const_columns.size() - 1; i >= 0; --i) {
if (is_const_columns[i]) {
true_count++;
if (found_false_after_true) {
return false;
}
} else {
if (true_count > 2) {
return false;
}
found_false_after_true = true;
}
}
if (true_count > 2) {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid is_const_columns configuration");
}
return true_count;
}

AggregateFunctionPtr create_aggregate_function_approx_top_k(const std::string& name,
const DataTypes& argument_types,
const bool result_is_nullable,
const AggregateFunctionAttr& attr) {
if (argument_types.empty()) {
return nullptr;
}

std::vector<bool> is_const_columns;
std::vector<std::string> column_names;
for (const auto& [name, is_const] : attr.column_infos) {
is_const_columns.push_back(is_const);
if (!is_const) {
column_names.push_back(name);
}
}

int32_t true_count = is_valid_const_columns(is_const_columns);
if (true_count == 0) {
return creator_without_type::create<AggregateFunctionApproxTopK<0>>(
argument_types, result_is_nullable, column_names);
} else if (true_count == 1) {
return creator_without_type::create<AggregateFunctionApproxTopK<1>>(
argument_types, result_is_nullable, column_names);
} else if (true_count == 2) {
return creator_without_type::create<AggregateFunctionApproxTopK<2>>(
argument_types, result_is_nullable, column_names);
} else {
return nullptr;
}
}

void register_aggregate_function_approx_top_k(AggregateFunctionSimpleFactory& factory) {
factory.register_function_both("approx_top_k", create_aggregate_function_approx_top_k);
}

} // namespace doris::vectorized
Loading