2020-12-10 22:16:58 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Processors/ISimpleTransform.h>
|
|
|
|
|
2020-12-15 00:36:03 +00:00
|
|
|
#include <Interpreters/AggregateDescription.h>
|
|
|
|
|
|
|
|
#include <Common/AlignedBuffer.h>
|
|
|
|
|
2020-12-10 22:16:58 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ExpressionActions;
|
|
|
|
using ExpressionActionsPtr = std::shared_ptr<ExpressionActions>;
|
|
|
|
|
2020-12-15 00:36:03 +00:00
|
|
|
class Arena;
|
|
|
|
|
2020-12-18 17:13:28 +00:00
|
|
|
// Runtime data for computing one window function
|
2020-12-15 00:36:03 +00:00
|
|
|
struct WindowFunctionWorkspace
|
|
|
|
{
|
|
|
|
WindowFunctionDescription window_function;
|
|
|
|
AlignedBuffer aggregate_function_state;
|
|
|
|
std::vector<size_t> argument_column_indices;
|
2020-12-21 21:23:45 +00:00
|
|
|
|
|
|
|
// Argument and result columns. Be careful, they are per-chunk.
|
2020-12-15 00:36:03 +00:00
|
|
|
std::vector<const IColumn *> argument_columns;
|
2020-12-21 21:23:45 +00:00
|
|
|
MutableColumnPtr result_column;
|
2020-12-15 00:36:03 +00:00
|
|
|
};
|
|
|
|
|
2020-12-18 17:13:28 +00:00
|
|
|
/*
|
|
|
|
* Computes several window functions that share the same window. The input must
|
|
|
|
* be sorted correctly for this window (PARTITION BY, then ORDER BY).
|
|
|
|
*/
|
2020-12-10 22:16:58 +00:00
|
|
|
class WindowTransform : public ISimpleTransform
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WindowTransform(
|
2020-12-22 01:41:02 +00:00
|
|
|
const Block & input_header_,
|
|
|
|
const Block & output_header_,
|
|
|
|
const WindowDescription & window_description_,
|
|
|
|
const std::vector<WindowFunctionDescription> &
|
2020-12-22 04:02:39 +00:00
|
|
|
window_function_descriptions);
|
2020-12-15 00:36:03 +00:00
|
|
|
|
|
|
|
~WindowTransform() override;
|
2020-12-10 22:16:58 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return "WindowTransform";
|
|
|
|
}
|
|
|
|
|
|
|
|
static Block transformHeader(Block header, const ExpressionActionsPtr & expression);
|
|
|
|
|
|
|
|
void transform(Chunk & chunk) override;
|
|
|
|
|
2020-12-15 00:36:03 +00:00
|
|
|
public:
|
|
|
|
Block input_header;
|
|
|
|
|
|
|
|
WindowDescription window_description;
|
2020-12-18 17:13:28 +00:00
|
|
|
|
2020-12-16 12:57:47 +00:00
|
|
|
// Indices of the PARTITION BY columns in block.
|
2020-12-15 00:36:03 +00:00
|
|
|
std::vector<size_t> partition_by_indices;
|
2020-12-18 17:13:28 +00:00
|
|
|
|
2020-12-16 12:57:47 +00:00
|
|
|
// The columns for PARTITION BY and the row in these columns where the
|
|
|
|
// current partition started. They might be in some of the previous blocks,
|
|
|
|
// so we have to keep the shared ownership of the columns. We don't keep the
|
|
|
|
// entire block to save memory, only the needed columns, in the same order
|
|
|
|
// as the partition_by_indices array.
|
|
|
|
// Can be empty if there is no PARTITION BY.
|
|
|
|
// Columns are nullptr when it is the first partition.
|
|
|
|
std::vector<ColumnPtr> partition_start_columns;
|
|
|
|
size_t partition_start_row = 0;
|
2020-12-15 00:36:03 +00:00
|
|
|
|
2020-12-18 17:13:28 +00:00
|
|
|
// Data for computing the window functions.
|
2020-12-15 00:36:03 +00:00
|
|
|
std::vector<WindowFunctionWorkspace> workspaces;
|
|
|
|
|
|
|
|
std::unique_ptr<Arena> arena;
|
2020-12-10 22:16:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|