aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/library/yql/providers/common/comp_nodes/yql_formatcode.cpp
blob: c6ec91c0d5d45bef6293ae1e17ecbe45a655d80f (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
#include "yql_formatcode.h"
#include "yql_type_resource.h"
#include <ydb/library/yql/minikql/computation/mkql_computation_node_impl.h>
#include <ydb/library/yql/minikql/mkql_string_util.h>
#include <ydb/library/yql/ast/serialize/yql_expr_serialize.h>

namespace NKikimr {
namespace NMiniKQL {

class TFormatCodeWrapper : public TMutableComputationNode<TFormatCodeWrapper> {
    typedef TMutableComputationNode<TFormatCodeWrapper> TBaseComputation;
public:
    TFormatCodeWrapper(TComputationMutables& mutables, IComputationNode* code, bool annotatePosition, ui32 exprCtxMutableIndex)
        : TBaseComputation(mutables)
        , Code_(code)
        , AnnotatePosition_(annotatePosition)
        , ExprCtxMutableIndex_(exprCtxMutableIndex)
    {}

    NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
        auto codeValue = Code_->GetValue(ctx);
        auto code = GetYqlCode(codeValue);
        NYql::TExprContext& exprCtx = GetExprContext(ctx, ExprCtxMutableIndex_);
        auto ast = NYql::ConvertToAst(*code, exprCtx, AnnotatePosition_ ?
            NYql::TExprAnnotationFlags::Position :
            NYql::TExprAnnotationFlags::None, true);
        auto str = ast.Root->ToString(NYql::TAstPrintFlags::PerLine | NYql::TAstPrintFlags::ShortQuote);
        return MakeString(str);
    }

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

private:
    IComputationNode* Code_;
    bool AnnotatePosition_;
    const ui32 ExprCtxMutableIndex_;
};

class TSerializeCodeWrapper : public TMutableComputationNode<TSerializeCodeWrapper> {
    typedef TMutableComputationNode<TSerializeCodeWrapper> TBaseComputation;
public:
    TSerializeCodeWrapper(TComputationMutables& mutables, IComputationNode* code, ui32 exprCtxMutableIndex)
        : TBaseComputation(mutables)
        , Code_(code)
        , ExprCtxMutableIndex_(exprCtxMutableIndex)
    {}

    NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
        auto codeValue = Code_->GetValue(ctx);
        auto code = GetYqlCode(codeValue);
        NYql::TExprContext& exprCtx = GetExprContext(ctx, ExprCtxMutableIndex_);
        auto str = NYql::SerializeGraph(*code, exprCtx,
            NYql::TSerializedExprGraphComponents::Graph |
            NYql::TSerializedExprGraphComponents::Positions);
        return MakeString(str);
    }

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

private:
    IComputationNode* Code_;
    const ui32 ExprCtxMutableIndex_;
};

template <bool AnnotatePosition>
IComputationNode* WrapFormatCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
    MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
    auto code = LocateNode(ctx.NodeLocator, callable, 0);
    return new TFormatCodeWrapper(ctx.Mutables, code, AnnotatePosition, exprCtxMutableIndex);
}

template IComputationNode* WrapFormatCode<false>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

template IComputationNode* WrapFormatCode<true>
    (TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex);

IComputationNode* WrapSerializeCode(TCallable& callable, const TComputationNodeFactoryContext& ctx, ui32 exprCtxMutableIndex) {
    MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
    auto code = LocateNode(ctx.NodeLocator, callable, 0);
    return new TSerializeCodeWrapper(ctx.Mutables, code, exprCtxMutableIndex);
}

}
}