aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/comp_nodes/yql_makecode.cpp
blob: f4cb30debbaea35934e6e9b55d402761ce45933e (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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "yql_makecode.h"
#include "yql_type_resource.h"
#include "yql_position.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>

namespace NKikimr {
namespace NMiniKQL {

template <NYql::TExprNode::EType Type>
struct TMakeCodeArgs;

template <>
struct TMakeCodeArgs<NYql::TExprNode::World> {
    static constexpr size_t MinValue = 0;
    static constexpr size_t MaxValue = 0;
};

template <>
struct TMakeCodeArgs<NYql::TExprNode::Atom> {
    static constexpr size_t MinValue = 1;
    static constexpr size_t MaxValue = 1;
};

template <>
struct TMakeCodeArgs<NYql::TExprNode::List> {
    static constexpr size_t MinValue = 0;
    static constexpr size_t MaxValue = Max<size_t>();
};

template <>
struct TMakeCodeArgs<NYql::TExprNode::Callable> {
    static constexpr size_t MinValue = 1;
    static constexpr size_t MaxValue = Max<size_t>();
};

template <>
struct TMakeCodeArgs<NYql::TExprNode::Lambda> {
    static constexpr size_t MinValue = 2;
    static constexpr size_t MaxValue = Max<size_t>();
};

template <NYql::TExprNode::EType Type>
class TMakeCodeWrapper : public TMutableComputationNode<TMakeCodeWrapper<Type>> {
    typedef TMutableComputationNode<TMakeCodeWrapper<Type>> TBaseComputation;
public:
    TMakeCodeWrapper(TComputationMutables& mutables, TVector<IComputationNode*>&& args, ui32 exprCtxMutableIndex, NYql::TPosition pos)
        : TBaseComputation(mutables)
        , Args_(std::move(args))
        , ExprCtxMutableIndex_(exprCtxMutableIndex)
        , Pos_(pos)
    {}

    NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
        auto exprCtxPtr = GetExprContextPtr(ctx, ExprCtxMutableIndex_);
        NYql::TExprNode::TPtr retNode;
        switch (Type) {
        case NYql::TExprNode::Atom: {
            auto strValue = Args_[0]->GetValue(ctx);
            retNode = exprCtxPtr->NewAtom(Pos_, strValue.AsStringRef());
            break;
        }

        case NYql::TExprNode::List: {
            NYql::TExprNode::TListType items;
            for (ui32 i = 0; i < Args_.size(); ++i) {
                auto argValue = Args_[i]->GetValue(ctx);
                auto iter = argValue.GetListIterator();
                NUdf::TUnboxedValue codeValue;
                while (iter.Next(codeValue)) {
                    auto code = GetYqlCode(codeValue);
                    items.push_back(code);
                }
            }

            retNode = exprCtxPtr->NewList(Pos_, std::move(items));
            break;
        }

        case NYql::TExprNode::Callable: {
            NYql::TExprNode::TListType items;
            auto strValue = Args_[0]->GetValue(ctx);
            for (ui32 i = 1; i < Args_.size(); ++i) {
                auto argValue = Args_[i]->GetValue(ctx);
                auto iter = argValue.GetListIterator();
                NUdf::TUnboxedValue codeValue;
                while (iter.Next(codeValue)) {
                    auto code = GetYqlCode(codeValue);
                    items.push_back(code);
                }
            }

            retNode = exprCtxPtr->NewCallable(Pos_, strValue.AsStringRef(), std::move(items));
            break;
        }

        case NYql::TExprNode::Lambda: {
            auto countValue = Args_[0]->GetValue(ctx);
            const bool fixedArgs = !countValue;
            const ui32 argsCount = fixedArgs ? Args_.size() - 2 : countValue.template Get<ui32>();
            NYql::TExprNode::TListType argNodes;
            for (ui32 i = 0; i < argsCount; ++i) {
                argNodes.push_back(exprCtxPtr->NewArgument(Pos_, "arg" + ToString(i)));
            }
            auto argsNode = exprCtxPtr->NewArguments(Pos_, std::move(argNodes));

            // fill external nodes with arguments
            if (fixedArgs) {
                for (ui32 i = 0; i < argsCount; ++i) {
                    auto arg = argsNode->ChildPtr(i);
                    dynamic_cast<IComputationExternalNode*>(Args_[i + 1])->SetValue(ctx,
                        NUdf::TUnboxedValuePod(new TYqlCodeResource(exprCtxPtr, arg)));
                }
            } else {
                NUdf::TUnboxedValue* inplace = nullptr;
                NUdf::TUnboxedValue array = ctx.HolderFactory.CreateDirectArrayHolder(argsCount, inplace);
                for (ui32 i = 0; i < argsCount; ++i) {
                    auto arg = argsNode->ChildPtr(i);
                    inplace[i] = NUdf::TUnboxedValuePod(new TYqlCodeResource(exprCtxPtr, arg));
                }

                dynamic_cast<IComputationExternalNode*>(Args_[1])->SetValue(ctx, std::move(array));
            }

            // get body of lambda
            auto bodyValue = Args_.back()->GetValue(ctx);
            retNode = exprCtxPtr->NewLambda(Pos_, std::move(argsNode), GetYqlCode(bodyValue));
            break;
        }

        case NYql::TExprNode::World: {
            retNode = exprCtxPtr->NewWorld(Pos_);
            break;
        }

        default:
            MKQL_ENSURE(false, "Unsupported type:" << Type);
        }

        return NUdf::TUnboxedValuePod(new TYqlCodeResource(exprCtxPtr, retNode));
    }

    void RegisterDependencies() const override {
        if (Type == NYql::TExprNode::Lambda) {
            this->DependsOn(Args_.front());
            for (size_t i = 1; i < Args_.size() - 1; ++i) {
                this->Own(dynamic_cast<IComputationExternalNode*>(Args_[i]));
            }
            this->DependsOn(Args_.back());
        } else {
            for (auto arg : Args_) {
                this->DependsOn(arg);
            }
        }
    }

private:
    TVector<IComputationNode*> Args_;
    ui32 ExprCtxMutableIndex_;
    NYql::TPosition Pos_;
};

template <NYql::TExprNode::EType Type>
IComputationNode* WrapMakeCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
    constexpr size_t expectedMinArgsCount = 3 + TMakeCodeArgs<Type>::MinValue;
    constexpr size_t expectedMaxArgsCount = TMakeCodeArgs<Type>::MaxValue != Max<size_t>() ?
        3 + TMakeCodeArgs<Type>::MaxValue : Max<size_t>();
    MKQL_ENSURE(callable.GetInputsCount() >= expectedMinArgsCount
        && callable.GetInputsCount() <= expectedMaxArgsCount, "Expected from " << expectedMinArgsCount << " to "
        << expectedMaxArgsCount << " arg(s), but got: " << callable.GetInputsCount());
    auto pos = ExtractPosition(callable);
    TVector<IComputationNode*> args;
    args.reserve(callable.GetInputsCount() - 3);
    for (size_t i = 0; i < callable.GetInputsCount() - 3; ++i) {
        args.push_back(LocateNode(ctx.NodeLocator, callable, i + 3));
    }

    return new TMakeCodeWrapper<Type>(ctx.Mutables, std::move(args), exprCtxMutableIndex, pos);
}

template IComputationNode* WrapMakeCode<NYql::TExprNode::World>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

template IComputationNode* WrapMakeCode<NYql::TExprNode::Atom>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

template IComputationNode* WrapMakeCode<NYql::TExprNode::List>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

template IComputationNode* WrapMakeCode<NYql::TExprNode::Callable>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

template IComputationNode* WrapMakeCode<NYql::TExprNode::Lambda>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

}
}