2018-01-10 15:45:05 +00:00
|
|
|
#pragma once
|
2017-10-12 19:58:39 +00:00
|
|
|
|
2019-06-21 15:31:37 +00:00
|
|
|
#include "Algorithms.h"
|
|
|
|
#include "ArraySourceVisitor.h"
|
|
|
|
#include "ArraySinkVisitor.h"
|
|
|
|
#include "ValueSourceVisitor.h"
|
2019-05-01 21:43:05 +00:00
|
|
|
|
2018-01-10 15:45:05 +00:00
|
|
|
|
2020-02-25 19:05:15 +00:00
|
|
|
namespace DB
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
2020-02-25 19:05:15 +00:00
|
|
|
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2020-02-25 19:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace GatherUtils
|
|
|
|
{
|
|
|
|
|
2017-10-12 19:58:39 +00:00
|
|
|
/// Base classes which selects template function implementation with concrete ArraySource or ArraySink
|
2018-01-17 19:22:09 +00:00
|
|
|
/// Derived classes should implement selectImpl for ArraySourceSelector and ArraySinkSelector,
|
|
|
|
/// selectSourceSink for ArraySinkSourceSelector and selectSourcePair for ArraySourcePairSelector
|
2017-10-12 19:58:39 +00:00
|
|
|
|
2018-01-17 19:22:09 +00:00
|
|
|
template <typename Base, typename Tuple, int index, typename ... Args>
|
|
|
|
void callSelectMemberFunctionWithTupleArgument(Tuple & tuple, Args && ... args)
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
if constexpr (index == std::tuple_size<Tuple>::value)
|
|
|
|
Base::selectImpl(args ...);
|
|
|
|
else
|
|
|
|
callSelectMemberFunctionWithTupleArgument<Base, Tuple, index + 1>(tuple, args ..., std::get<index>(tuple));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Base, typename ... Args>
|
|
|
|
struct ArraySourceSelectorVisitor : public ArraySourceVisitorImpl<ArraySourceSelectorVisitor<Base, Args ...>>
|
|
|
|
{
|
|
|
|
explicit ArraySourceSelectorVisitor(Args && ... args) : packed_args(args ...) {}
|
|
|
|
|
|
|
|
using Tuple = std::tuple<Args && ...>;
|
|
|
|
|
|
|
|
template <typename Source>
|
|
|
|
void visitImpl(Source & source)
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
callSelectMemberFunctionWithTupleArgument<Base, Tuple, 0>(packed_args, source);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
2018-01-17 19:22:09 +00:00
|
|
|
|
|
|
|
Tuple packed_args;
|
2017-10-12 19:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Base>
|
2018-01-17 19:22:09 +00:00
|
|
|
struct ArraySourceSelector
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
|
|
|
template <typename ... Args>
|
|
|
|
static void select(IArraySource & source, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySourceSelectorVisitor<Base, Args ...> visitor(args ...);
|
|
|
|
source.accept(visitor);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-17 19:22:09 +00:00
|
|
|
template <typename Base, typename ... Args>
|
|
|
|
struct ArraySinkSelectorVisitor : public ArraySinkVisitorImpl<ArraySinkSelectorVisitor<Base, Args ...>>
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
explicit ArraySinkSelectorVisitor(Args && ... args) : packed_args(args ...) {}
|
|
|
|
|
|
|
|
using Tuple = std::tuple<Args && ...>;
|
|
|
|
|
|
|
|
template <typename Sink>
|
|
|
|
void visitImpl(Sink & sink)
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
callSelectMemberFunctionWithTupleArgument<Base, Tuple, 0>(packed_args, sink);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
2018-01-17 19:22:09 +00:00
|
|
|
|
|
|
|
Tuple packed_args;
|
2017-10-12 19:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Base>
|
2018-01-17 19:22:09 +00:00
|
|
|
struct ArraySinkSelector
|
2017-10-12 19:58:39 +00:00
|
|
|
{
|
|
|
|
template <typename ... Args>
|
|
|
|
static void select(IArraySink & sink, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySinkSelectorVisitor<Base, Args ...> visitor(args ...);
|
|
|
|
sink.accept(visitor);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-19 15:19:02 +00:00
|
|
|
|
|
|
|
template <typename Base, typename ... Args>
|
|
|
|
struct ValueSourceSelectorVisitor : public ValueSourceVisitorImpl<ValueSourceSelectorVisitor<Base, Args ...>>
|
|
|
|
{
|
|
|
|
explicit ValueSourceSelectorVisitor(Args && ... args) : packed_args(args ...) {}
|
|
|
|
|
|
|
|
using Tuple = std::tuple<Args && ...>;
|
|
|
|
|
|
|
|
template <typename Source>
|
|
|
|
void visitImpl(Source & source)
|
|
|
|
{
|
|
|
|
callSelectMemberFunctionWithTupleArgument<Base, Tuple, 0>(packed_args, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
Tuple packed_args;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Base>
|
|
|
|
struct ValueSourceSelector
|
|
|
|
{
|
|
|
|
template <typename ... Args>
|
|
|
|
static void select(IValueSource & source, Args && ... args)
|
|
|
|
{
|
|
|
|
ValueSourceSelectorVisitor<Base, Args ...> visitor(args ...);
|
|
|
|
source.accept(visitor);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-12 19:58:39 +00:00
|
|
|
template <typename Base>
|
|
|
|
struct ArraySinkSourceSelector
|
|
|
|
{
|
|
|
|
template <typename ... Args>
|
|
|
|
static void select(IArraySource & source, IArraySink & sink, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySinkSelector<Base>::select(sink, source, args ...);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Sink, typename ... Args>
|
|
|
|
static void selectImpl(Sink && sink, IArraySource & source, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySourceSelector<Base>::select(source, sink, args ...);
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Source, typename Sink, typename ... Args>
|
|
|
|
static void selectImpl(Source && source, Sink && sink, Args && ... args)
|
|
|
|
{
|
|
|
|
Base::selectSourceSink(source, sink, args ...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-09 15:24:27 +00:00
|
|
|
template <typename Base>
|
|
|
|
struct ArraySourcePairSelector
|
|
|
|
{
|
|
|
|
template <typename ... Args>
|
|
|
|
static void select(IArraySource & first, IArraySource & second, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySourceSelector<Base>::select(first, second, args ...);
|
2018-01-09 15:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FirstSource, typename ... Args>
|
|
|
|
static void selectImpl(FirstSource && first, IArraySource & second, Args && ... args)
|
|
|
|
{
|
2018-01-17 19:22:09 +00:00
|
|
|
ArraySourceSelector<Base>::select(second, first, args ...);
|
2018-01-09 15:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SecondSource, typename FirstSource, typename ... Args>
|
|
|
|
static void selectImpl(SecondSource && second, FirstSource && first, Args && ... args)
|
|
|
|
{
|
|
|
|
Base::selectSourcePair(first, second, args ...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-19 15:19:02 +00:00
|
|
|
template <typename Base>
|
|
|
|
struct ArrayAndValueSourceSelectorBySink : public ArraySinkSelector<ArrayAndValueSourceSelectorBySink<Base>>
|
|
|
|
{
|
|
|
|
template <typename Sink, typename ... Args>
|
|
|
|
static void selectImpl(Sink && sink, IArraySource & array_source, IValueSource & value_source, Args && ... args)
|
|
|
|
{
|
|
|
|
using SynkType = typename std::decay<Sink>::type;
|
|
|
|
using ArraySource = typename SynkType::CompatibleArraySource;
|
|
|
|
using ValueSource = typename SynkType::CompatibleValueSource;
|
|
|
|
|
|
|
|
auto checkType = [] (auto source_ptr)
|
|
|
|
{
|
|
|
|
if (source_ptr == nullptr)
|
|
|
|
throw Exception(demangle(typeid(Base).name()) + " expected "
|
|
|
|
+ demangle(typeid(typename SynkType::CompatibleArraySource).name())
|
|
|
|
+ " or " + demangle(typeid(ConstSource<typename SynkType::CompatibleArraySource>).name())
|
|
|
|
+ " or " + demangle(typeid(typename SynkType::CompatibleValueSource).name()) +
|
|
|
|
+ " or " + demangle(typeid(ConstSource<typename SynkType::CompatibleValueSource>).name())
|
|
|
|
+ " but got " + demangle(typeid(*source_ptr).name()), ErrorCodes::LOGICAL_ERROR);
|
|
|
|
};
|
|
|
|
auto checkTypeAndCallConcat = [& sink, & checkType, & args ...] (auto array_source_ptr, auto value_source_ptr)
|
|
|
|
{
|
|
|
|
checkType(array_source_ptr);
|
|
|
|
checkType(value_source_ptr);
|
|
|
|
|
|
|
|
Base::selectArrayAndValueSourceBySink(*array_source_ptr, *value_source_ptr, sink, args ...);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (array_source.isConst() && value_source.isConst())
|
|
|
|
checkTypeAndCallConcat(typeid_cast<ConstSource<ArraySource> *>(&array_source),
|
|
|
|
typeid_cast<ConstSource<ValueSource> *>(&value_source));
|
2018-11-23 18:52:00 +00:00
|
|
|
else if (array_source.isConst())
|
2018-01-19 15:19:02 +00:00
|
|
|
checkTypeAndCallConcat(typeid_cast<ConstSource<ArraySource> *>(&array_source),
|
|
|
|
typeid_cast<ValueSource *>(&value_source));
|
|
|
|
else if (value_source.isConst())
|
|
|
|
checkTypeAndCallConcat(typeid_cast<ArraySource *>(&array_source),
|
|
|
|
typeid_cast<ConstSource<ValueSource> *>(&value_source));
|
|
|
|
else
|
|
|
|
checkTypeAndCallConcat(typeid_cast<ArraySource *>(&array_source),
|
|
|
|
typeid_cast<ValueSource *>(&value_source));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-12 19:58:39 +00:00
|
|
|
}
|
2020-02-26 14:14:25 +00:00
|
|
|
|
|
|
|
}
|