WindowTransform.cpp 4.8 KB
Newer Older
A
Alexander Kuzmenkov 已提交
1 2 3 4
#include <Processors/Transforms/WindowTransform.h>

#include <Interpreters/ExpressionActions.h>

A
Alexander Kuzmenkov 已提交
5 6
#include <Common/Arena.h>

A
Alexander Kuzmenkov 已提交
7 8 9
namespace DB
{

A
Alexander Kuzmenkov 已提交
10 11 12 13 14 15 16 17 18
WindowTransform::WindowTransform(const Block & input_header_,
        const Block & output_header_,
        const WindowDescription & window_description_,
        const std::vector<WindowFunctionDescription> & window_function_descriptions
        )
    : ISimpleTransform(input_header_, output_header_,
        false /* skip_empty_chunks */)
    , input_header(input_header_)
    , window_description(window_description_)
A
Alexander Kuzmenkov 已提交
19
{
A
Alexander Kuzmenkov 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    workspaces.reserve(window_function_descriptions.size());
    for (size_t i = 0; i < window_function_descriptions.size(); ++i)
    {
        WindowFunctionWorkspace workspace;
        workspace.window_function = window_function_descriptions[i];

        const auto & aggregate_function
            = workspace.window_function.aggregate_function;
        if (!arena && aggregate_function->allocatesMemoryInArena())
        {
            arena = std::make_unique<Arena>();
        }

        workspace.argument_column_indices.reserve(
            workspace.window_function.argument_names.size());
        workspace.argument_columns.reserve(
            workspace.window_function.argument_names.size());
        for (const auto & argument_name : workspace.window_function.argument_names)
        {
            workspace.argument_column_indices.push_back(
                input_header.getPositionByName(argument_name));
A
Alexander Kuzmenkov 已提交
41

A
Alexander Kuzmenkov 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        }

        workspace.aggregate_function_state.reset(aggregate_function->sizeOfData(),
            aggregate_function->alignOfData());
        aggregate_function->create(workspace.aggregate_function_state.data());

        workspaces.push_back(std::move(workspace));
    }

    partition_by_indices.reserve(window_description.partition_by.size());
    for (const auto & column : window_description.partition_by)
    {
        partition_by_indices.push_back(
            input_header.getPositionByName(column.column_name));
    }
}
A
Alexander Kuzmenkov 已提交
58

A
Alexander Kuzmenkov 已提交
59
WindowTransform::~WindowTransform()
A
Alexander Kuzmenkov 已提交
60
{
A
Alexander Kuzmenkov 已提交
61 62 63 64 65 66
    // Some states may be not created yet if the creation failed.
    for (size_t i = 0; i < workspaces.size(); i++)
    {
        workspaces[i].window_function.aggregate_function->destroy(
            workspaces[i].aggregate_function_state.data());
    }
A
Alexander Kuzmenkov 已提交
67 68 69 70
}

void WindowTransform::transform(Chunk & chunk)
{
A
Alexander Kuzmenkov 已提交
71
    const size_t num_rows = chunk.getNumRows();
A
Alexander Kuzmenkov 已提交
72 73
    auto block = getInputPort().getHeader().cloneWithColumns(chunk.detachColumns());

A
Alexander Kuzmenkov 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    for (auto & workspace : workspaces)
    {
        workspace.argument_columns.clear();
        for (const auto column_index : workspace.argument_column_indices)
        {
            workspace.argument_columns.push_back(
                block.getColumns()[column_index].get());
        }
    }

    for (auto & ws : workspaces)
    {
        const auto & f = ws.window_function;
        const auto * a = f.aggregate_function.get();

        //*
        ColumnWithTypeAndName column_with_type;
        column_with_type.name = f.column_name;
        column_with_type.type = a->getReturnType();
        auto c = column_with_type.type->createColumn();
        column_with_type.column.reset(c.get());

        for (size_t row = 0; row < num_rows; row++)
        {
            // Check whether the new partition has started and reinitialize the
            // aggregate function states.
            if (row > 0)
            {
                for (const size_t column_index : partition_by_indices)
                {
                    const auto * column = block.getColumns()[column_index].get();
                    if (column->compareAt(row, row - 1, *column,
                        1 /* nan_direction_hint */) != 0)
                    {
                        ws.window_function.aggregate_function->destroy(
                            ws.aggregate_function_state.data());
                        ws.window_function.aggregate_function->create(
                            ws.aggregate_function_state.data());
                        break;
                    }
                }
            }

            // Update the aggregate function state and save the result.
            a->add(ws.aggregate_function_state.data(),
                ws.argument_columns.data(),
                row,
                arena.get());

            a->insertResultInto(ws.aggregate_function_state.data(),
                *c,
                arena.get());
        }

        block.insert(column_with_type);
        /*/
        auto & column_with_type = block.getByName(f.column_name);
        auto c = IColumn::mutate(std::move(column_with_type.column));

        for (size_t i = 0; i < num_rows; i++)
        {
            c->insert(UInt64(i));
        }

        column_with_type.column.reset(c.get());
        //*/
    }

A
Alexander Kuzmenkov 已提交
142 143 144 145
    chunk.setColumns(block.getColumns(), num_rows);
}

}