aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/ForkProcessor.cpp
blob: f4e5a5be5f237163d6f0e043396badd0b1a48390 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <Processors/ForkProcessor.h>


namespace DB
{

ForkProcessor::Status ForkProcessor::prepare()
{
    auto & input = inputs.front();

    /// Check can output.

    bool all_can_push = true;
    size_t num_active_outputs = 0;

    for (const auto & output : outputs)
    {
        if (!output.isFinished())
        {
            ++num_active_outputs;

            /// The order is important.
            if (!output.canPush())
                all_can_push = false;
        }
    }

    if (0 == num_active_outputs)
    {
        input.close();
        return Status::Finished;
    }

    if (!all_can_push)
    {
        input.setNotNeeded();
        return Status::PortFull;
    }

    /// Check can input.

    if (input.isFinished())
    {
        for (auto & output : outputs)
            output.finish();

        return Status::Finished;
    }

    input.setNeeded();

    if (!input.hasData())
        return Status::NeedData;

    /// Move data.

    auto data = input.pull();
    size_t num_processed_outputs = 0;

    for (auto & output : outputs)
    {
        if (!output.isFinished())  /// Skip finished outputs.
        {
            ++num_processed_outputs;
            if (num_processed_outputs == num_active_outputs)
                output.push(std::move(data)); /// NOLINT Can push because no full or unneeded outputs.
            else
                output.push(data.clone()); /// NOLINT
        }
    }

    /// Now, we pulled from input. It must be empty.
    return Status::NeedData;
}

}