aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/services/yql_out_transformers.cpp
blob: cc7b6bfd4e3c1557bad8208a7170cff00bdcab6a (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
#include "yql_out_transformers.h"

#include <yql/essentials/ast/yql_expr.h>

#include <library/cpp/yson/writer.h>

#include <util/stream/null.h>
#include <util/stream/str.h>

namespace NYql {

IGraphTransformer::TStatus TExprOutputTransformer::operator()(
    const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx)
{
    Y_UNUSED(ctx);
    output = input;
    if (!ExprRoot_) {
        return IGraphTransformer::TStatus::Ok;
    }

    auto ast = ConvertToAst(*ExprRoot_, ctx, WithTypes_ ? TExprAnnotationFlags::Types : TExprAnnotationFlags::None, true);
    ui32 prettyFlags = TAstPrintFlags::ShortQuote;
    if (!WithTypes_) {
        prettyFlags |= TAstPrintFlags::PerLine;
    }

    if (DirectOut_) {
        ast.Root->PrettyPrintTo(*DirectOut_, prettyFlags);
    }
    return IGraphTransformer::TStatus::Ok;
}

IGraphTransformer::TStatus TPlanOutputTransformer::operator()(
    const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx)
{
    Y_UNUSED(ctx);
    output = input;
    if (DirectOut_) {
        NYson::TYsonWriter writer(DirectOut_, OutputFormat_);
        Builder_.WritePlan(writer, input, PlanSettings_);
    } else {
        TNullOutput null;
        NYson::TYsonWriter writer(&null, OutputFormat_);
        Builder_.WritePlan(writer, input, PlanSettings_);
    }

    return IGraphTransformer::TStatus::Ok;
}

IGraphTransformer::TStatus TExprLogTransformer::operator()(
    const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx)
{
    Y_UNUSED(ctx);
    output = input;
    if (YQL_CVLOG_ACTIVE(Level, Component)) {
        TConvertToAstSettings settings;
        settings.AllowFreeArgs = true;
        settings.RefAtoms = true;
        auto ast = ConvertToAst(*input, ctx, settings);
        TStringStream out;
        ast.Root->PrettyPrintTo(out, TAstPrintFlags::ShortQuote | TAstPrintFlags::PerLine);
        YQL_CVLOG(Level, Component) << Description << ":\n" << out.Str();
    }
    return IGraphTransformer::TStatus::Ok;
}

}