aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/ast/yql_constraint_ut.cpp
blob: 6c305d06222fb3ef96ef80e055f57d7237ffb39e (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "yql_constraint.h"
#include "yql_expr.h"

#include <yql/essentials/utils/yql_panic.h>

#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/yson/node/node_io.h>


namespace NYql {

Y_UNIT_TEST_SUITE(TSerializeConstrains) {

    Y_UNIT_TEST(SerializeSorted) {
        TExprContext ctx;
        auto c = ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
            std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}}, true},
            std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}}, false},
            std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"e"}, TPartOfConstraintBase::TPathType{"f", "g"}}, false},
        });
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[[["a";"b"]];%true];[["d"];%false];[["e";["f";"g"]];%false]])");
        auto c2 = ctx.MakeConstraint<TSortedConstraintNode>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    Y_UNIT_TEST(SerializeChopped) {
        TExprContext ctx;
        auto c = ctx.MakeConstraint<TChoppedConstraintNode>(TPartOfConstraintBase::TSetOfSetsType{
            TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
            TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}},
            TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c", "d"}, TPartOfConstraintBase::TPathType{"e"}},
        });
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"(["a";[["a";"b"]];[["c";"d"];"e"]])");
        auto c2 = ctx.MakeConstraint<TChoppedConstraintNode>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    template <class TUniqueConstraint>
    void CheckSerializeUnique() {
        TExprContext ctx;
        auto c = ctx.MakeConstraint<TUniqueConstraint>(typename TUniqueConstraint::TContentType{
            TConstraintWithFieldsNode::TSetOfSetsType{
                TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
                TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a", "b"}}
            },
            TConstraintWithFieldsNode::TSetOfSetsType{
                TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
                TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
            },
        });
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([["a";[["a";"b"]]];["c";["d";"e"]]])");
        auto c2 = ctx.MakeConstraint<TUniqueConstraint>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    Y_UNIT_TEST(SerializeUnique) {
        CheckSerializeUnique<TUniqueConstraintNode>();
    }

    Y_UNIT_TEST(SerializeDistint) {
        CheckSerializeUnique<TDistinctConstraintNode>();
    }

    Y_UNIT_TEST(SerializeEmpty) {
        TExprContext ctx;
        auto c = ctx.MakeConstraint<TEmptyConstraintNode>();
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"(#)");
        auto c2 = ctx.MakeConstraint<TEmptyConstraintNode>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    Y_UNIT_TEST(SerializeVarIndex) {
        TExprContext ctx;
        auto c = ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
            std::pair{1u, 3u},
            std::pair{0u, 1u},
        });
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[0u;1u];[1u;3u]])");
        auto c2 = ctx.MakeConstraint<TVarIndexConstraintNode>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    Y_UNIT_TEST(SerializeMulti) {
        TExprContext ctx;

        TConstraintSet s1;
        s1.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
        s1.AddConstraint(
            ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
                std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}}, true},
                std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}, false},
            })
        );

        TConstraintSet s2;
        s2.AddConstraint(
            ctx.MakeConstraint<TUniqueConstraintNode>(typename TUniqueConstraintNode::TContentType{
                TConstraintWithFieldsNode::TSetOfSetsType{
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}
                },
                TConstraintWithFieldsNode::TSetOfSetsType{
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
                },
            })
        );
        s2.AddConstraint(
            ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
                std::pair{0u, 1u},
                std::pair{1u, 2u},
            })
        );

        auto c = ctx.MakeConstraint<TMultiConstraintNode>(TMultiConstraintNode::TMapType{
            std::pair{0u, s1},
            std::pair{1u, s2},
        });
        auto yson = c->ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"([[0u;{"Empty"=#;"Sorted"=[[["a"];%true];[["b"];%false]]}];[1u;{"Unique"=[["a";"b"];["c";["d";"e"]]];"VarIndex"=[[0u;1u];[1u;2u]]}]])");
        auto c2 = ctx.MakeConstraint<TMultiConstraintNode>(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(c2->ToYson()));
        UNIT_ASSERT_EQUAL(c, c2);
    }

    Y_UNIT_TEST(SerializeConstrainSet) {
        TExprContext ctx;

        TConstraintSet s;
        s.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
        s.AddConstraint(
            ctx.MakeConstraint<TSortedConstraintNode>(TSortedConstraintNode::TContainerType{
                std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}}, true},
                std::pair{TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}, false},
            })
        );
        s.AddConstraint(
            ctx.MakeConstraint<TUniqueConstraintNode>(typename TUniqueConstraintNode::TContentType{
                TConstraintWithFieldsNode::TSetOfSetsType{
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"a"}},
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"b"}}
                },
                TConstraintWithFieldsNode::TSetOfSetsType{
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"c"}},
                    TPartOfConstraintBase::TSetType{TPartOfConstraintBase::TPathType{"d"}, TPartOfConstraintBase::TPathType{"e"}}
                },
            })
        );
        s.AddConstraint(
            ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
                std::pair{0u, 1u},
                std::pair{1u, 2u},
            })
        );
        s.AddConstraint(
            ctx.MakeConstraint<TVarIndexConstraintNode>(TVarIndexConstraintNode::TMapType{
                std::pair{0u, 1u},
                std::pair{1u, 2u},
            })
        );

        TConstraintSet inner;
        inner.AddConstraint(ctx.MakeConstraint<TEmptyConstraintNode>());
        s.AddConstraint(
            ctx.MakeConstraint<TMultiConstraintNode>(TMultiConstraintNode::TMapType{
                std::pair{0u, inner},
            })
        );

        auto yson = s.ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"({"Empty"=#;"Multi"=[[0u;{"Empty"=#}]];"Sorted"=[[["a"];%true];[["b"];%false]];"Unique"=[["a";"b"];["c";["d";"e"]]];"VarIndex"=[[0u;1u];[1u;2u]]})");
        auto s2 = ctx.MakeConstraintSet(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(s2.ToYson()));
        UNIT_ASSERT_EQUAL(s, s2);

        s.Clear();
        yson = s.ToYson();
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), R"({})");
        auto s3 = ctx.MakeConstraintSet(yson);
        UNIT_ASSERT_VALUES_EQUAL(NYT::NodeToCanonicalYsonString(yson), NYT::NodeToCanonicalYsonString(s3.ToYson()));
        UNIT_ASSERT_EQUAL(s, s3);
    }

    Y_UNIT_TEST(DeserializeBadConstrainSet) {
        TExprContext ctx;
        UNIT_ASSERT_EXCEPTION(
            ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"(#)")),
            NYql::TYqlPanic
        );
        UNIT_ASSERT_EXCEPTION(
            ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"({"Unknown"=[]})")),
            NYql::TYqlPanic
        );
        UNIT_ASSERT_EXCEPTION(
            ctx.MakeConstraintSet(NYT::NodeFromYsonString(R"({"Empty"=1u})")),
            NYql::TYqlPanic
        );
    }
}

} // namespace NYql