aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/providers/common/comp_nodes/yql_reprcode.cpp
blob: a1c52fc7fc2d99e6a8b501c8f52450ada7af9efd (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
#include "yql_formatcode.h"
#include "yql_type_resource.h"
#include "yql_position.h"

#include <yql/essentials/providers/common/schema/expr/yql_expr_schema.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/providers/common/codec/yql_codec.h>

namespace NKikimr {
namespace NMiniKQL {

class TReprCodeWrapper : public TMutableComputationNode<TReprCodeWrapper> {
    typedef TMutableComputationNode<TReprCodeWrapper> TBaseComputation;
public:
    TReprCodeWrapper(TComputationMutables& mutables, IComputationNode* value, const TString& yson, ui32 exprCtxMutableIndex, NYql::TPosition pos)
        : TBaseComputation(mutables)
        , Value_(value)
        , Yson_(yson)
        , ExprCtxMutableIndex_(exprCtxMutableIndex)
        , Pos_(pos)
    {}

    NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
        auto exprCtxPtr = GetExprContextPtr(ctx, ExprCtxMutableIndex_);
        const NYql::TTypeAnnotationNode* type = NYql::NCommon::ParseTypeFromYson(TStringBuf{Yson_}, *exprCtxPtr, Pos_);
        auto value = Value_->GetValue(ctx);
        auto node = NYql::NCommon::ValueToExprLiteral(type, value, *exprCtxPtr, exprCtxPtr->AppendPosition(Pos_));
        return NUdf::TUnboxedValuePod(new TYqlCodeResource(exprCtxPtr, node));
    }

    void RegisterDependencies() const override {
        DependsOn(Value_);
    }

private:
    IComputationNode* Value_;
    TString Yson_;
    ui32 ExprCtxMutableIndex_;
    NYql::TPosition Pos_;
};

IComputationNode* WrapReprCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
    MKQL_ENSURE(callable.GetInputsCount() == 5, "Expected 5 args");
    auto pos = ExtractPosition(callable);
    auto value = LocateNode(ctx.NodeLocator, callable, 3);
    TString yson(TStringBuf(AS_VALUE(TDataLiteral, callable.GetInput(4))->AsValue().AsStringRef()));
    return new TReprCodeWrapper(ctx.Mutables, value, yson, exprCtxMutableIndex, pos);
}

}
}