// 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 "olap/aggregate_func.h" namespace std { namespace { // algorithm from boost: http://www.boost.org/doc/libs/1_61_0/doc/html/hash/reference.html#boost.hash_combine template inline void hash_combine(std::size_t& seed, T const& v) { seed ^= std::hash()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } template ::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl::apply(seed, tuple); hash_combine(seed, std::get(tuple)); } }; template struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; } // namespace template struct hash> { size_t operator()(std::tuple const& tt) const { size_t seed = 0; HashValueImpl>::apply(seed, tt); return seed; } }; } // namespace std namespace doris { template AggregateInfo::AggregateInfo(const Traits& traits) : _init_fn(traits.init), _update_fn(traits.update), _finalize_fn(traits.finalize), _agg_method(traits.agg_method) {} class AggregateFuncResolver { DECLARE_SINGLETON(AggregateFuncResolver); public: const AggregateInfo* get_aggregate_info(const FieldAggregationMethod agg_method, const FieldType field_type, const FieldType sub_type) const { auto pair = _infos_mapping.find(std::make_tuple(agg_method, field_type, sub_type)); if (pair != _infos_mapping.end()) { return pair->second; } else { return nullptr; } } template void add_aggregate_mapping() { _infos_mapping.emplace( std::make_tuple(agg_method, field_type, sub_type), new AggregateInfo(AggregateTraits())); } private: typedef std::tuple key_t; std::unordered_map _infos_mapping; DISALLOW_COPY_AND_ASSIGN(AggregateFuncResolver); }; AggregateFuncResolver::AggregateFuncResolver() { // None Aggregate Function, no-ops add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // array types has sub type like array field type is array, subtype is int add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // Min Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // Max Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // Sum Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // Replace Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // ReplaceIfNotNull Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); add_aggregate_mapping(); // Hyperloglog Aggregate Function add_aggregate_mapping(); // Bitmap Aggregate Function add_aggregate_mapping(); add_aggregate_mapping(); //for backward compatibility // quantile_state Aggregate Function add_aggregate_mapping(); } AggregateFuncResolver::~AggregateFuncResolver() { for (auto& iter : _infos_mapping) { delete iter.second; } } const AggregateInfo* get_aggregate_info(const FieldAggregationMethod agg_method, const FieldType field_type, const FieldType sub_type) { return AggregateFuncResolver::instance()->get_aggregate_info(agg_method, field_type, sub_type); } } // namespace doris