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;
|
|
|
|
// Be careful, this is per-chunk.
|
|
|
|
std::vector<const IColumn *> argument_columns;
|
|
|
|
};
|
|
|
|
|
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-15 00:36:03 +00:00
|
|
|
const Block & input_header,
|
|
|
|
const Block & output_header,
|
|
|
|
const WindowDescription & window_description,
|
|
|
|
const std::vector<WindowFunctionDescription> & window_functions);
|
|
|
|
|
|
|
|
~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
|
|
|
};
|
|
|
|
|
|
|
|
}
|