Skip to content

Commit 34ddb63

Browse files
authored
Fix nullif implementation (#3392)
1 parent d42a420 commit 34ddb63

File tree

10 files changed

+91
-20
lines changed

10 files changed

+91
-20
lines changed

ydb/library/yql/core/peephole_opt/yql_opt_peephole_physical.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,28 @@ TExprNode::TPtr ExpandPgArrayOp(const TExprNode::TPtr& input, TExprContext& ctx)
26252625
.Build();
26262626
}
26272627

2628+
TExprNode::TPtr ExpandPgNullIf(const TExprNode::TPtr& input, TExprContext& ctx) {
2629+
auto pred = ctx.Builder(input->Pos())
2630+
.Callable("Coalesce")
2631+
.Callable(0, "FromPg")
2632+
.Callable( 0, "PgOp")
2633+
.Atom(0, "=")
2634+
.Add(1, input->Child(0))
2635+
.Add(2, input->Child(1))
2636+
.Seal()
2637+
.Seal()
2638+
.Callable( 1, "Bool")
2639+
.Atom(0, "false")
2640+
.Seal()
2641+
.Seal().Build();
2642+
return ctx.Builder(input->Pos())
2643+
.Callable("If")
2644+
.Add(0, pred)
2645+
.Callable(1, "Null").Seal()
2646+
.Add(2, input->Child(0))
2647+
.Seal().Build();
2648+
}
2649+
26282650
template <bool Flat, bool List>
26292651
TExprNode::TPtr ExpandContainerIf(const TExprNode::TPtr& input, TExprContext& ctx) {
26302652
YQL_CLOG(DEBUG, CorePeepHole) << "Expand " << input->Content();
@@ -7868,6 +7890,7 @@ struct TPeepHoleRules {
78687890
{"Contains", &RewriteSearchByKeyForTypesMismatch<true>},
78697891
{"ListHas", &ExpandListHas},
78707892
{"PgAnyResolvedOp", &ExpandPgArrayOp},
7893+
{"PgNullIf", &ExpandPgNullIf},
78717894
{"PgAllResolvedOp", &ExpandPgArrayOp},
78727895
{"Map", &CleckClosureOnUpperLambdaOverList},
78737896
{"OrderedMap", &CleckClosureOnUpperLambdaOverList},

ydb/library/yql/core/type_ann/type_ann_core.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12283,6 +12283,7 @@ template <NKikimr::NUdf::EDataSlot DataSlot>
1228312283
Functions["FromPg"] = &FromPgWrapper;
1228412284
Functions["ToPg"] = &ToPgWrapper;
1228512285
Functions["PgClone"] = &PgCloneWrapper;
12286+
Functions["PgNullIf"] = &PgNullIfWrapper;
1228612287
ExtFunctions["PgAgg"] = &PgAggWrapper;
1228712288
ExtFunctions["PgAggWindowCall"] = &PgAggWrapper;
1228812289
ExtFunctions["PgCall"] = &PgCallWrapper;

ydb/library/yql/core/type_ann/type_ann_pg.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,55 @@ IGraphTransformer::TStatus PgAggWrapper(const TExprNode::TPtr& input, TExprNode:
917917
return IGraphTransformer::TStatus::Ok;
918918
}
919919

920+
IGraphTransformer::TStatus PgNullIfWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TContext& ctx) {
921+
Y_UNUSED(output);
922+
if (!EnsureArgsCount(*input, 2, ctx.Expr)) {
923+
return IGraphTransformer::TStatus::Error;
924+
}
925+
926+
TVector<ui32> types(2);
927+
bool replaced = false;
928+
929+
for (ui32 i = 0; i < 2; ++i) {
930+
auto* item = input->Child(i);
931+
auto type = item->GetTypeAnn();
932+
ui32 argType;
933+
bool convertToPg;
934+
const auto pos = item->Pos();
935+
if (!ExtractPgType(type, argType, convertToPg, pos, ctx.Expr)) {
936+
return IGraphTransformer::TStatus::Error;
937+
}
938+
939+
if (convertToPg) {
940+
replaced = true;
941+
input->ChildRef(i) = ctx.Expr.NewCallable(input->Child(2)->Pos(), "ToPg", { input->ChildPtr(i) });
942+
}
943+
944+
types[i] = argType;
945+
}
946+
947+
if (replaced) {
948+
return IGraphTransformer::TStatus::Repeat;
949+
}
950+
951+
const NPg::TTypeDesc* commonType;
952+
if (const auto issue = NPg::LookupCommonType(types,
953+
[&input, &ctx](size_t i) {
954+
return ctx.Expr.GetPosition(input->Child(i)->Pos());
955+
}, commonType))
956+
{
957+
ctx.Expr.AddError(*issue);
958+
return IGraphTransformer::TStatus::Error;
959+
}
960+
if (IsCastRequired(commonType->TypeId, types[0])) {
961+
input->ChildRef(0) = WrapWithPgCast(std::move(input->ChildRef(0)), commonType->TypeId, ctx.Expr);
962+
return IGraphTransformer::TStatus::Repeat;
963+
}
964+
965+
input->SetTypeAnn(ctx.Expr.MakeType<TPgExprType>(commonType->TypeId));
966+
return IGraphTransformer::TStatus::Ok;
967+
}
968+
920969
IGraphTransformer::TStatus PgQualifiedStarWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TContext& ctx) {
921970
Y_UNUSED(output);
922971
if (!EnsureArgsCount(*input, 1, ctx.Expr)) {

ydb/library/yql/core/type_ann/type_ann_pg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ IGraphTransformer::TStatus PgCloneWrapper(const TExprNode::TPtr& input, TExprNod
2828
IGraphTransformer::TStatus PgOpWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExtContext& ctx);
2929
IGraphTransformer::TStatus PgArrayOpWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExtContext& ctx);
3030
IGraphTransformer::TStatus PgWindowCallWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExtContext& ctx);
31+
IGraphTransformer::TStatus PgNullIfWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TContext& ctx);
3132
IGraphTransformer::TStatus PgAggWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TExtContext& ctx);
3233
IGraphTransformer::TStatus PgQualifiedStarWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TContext& ctx);
3334
IGraphTransformer::TStatus PgColumnRefWrapper(const TExprNode::TPtr& input, TExprNode::TPtr& output, TContext& ctx);

ydb/library/yql/sql/pg/pg_sql.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,10 +4395,7 @@ class TConverter : public IPGParseEvents {
43954395
if (!lhs || !rhs) {
43964396
return nullptr;
43974397
}
4398-
auto pred = L(A("Coalesce"),
4399-
L(A("FromPg"), L(A("PgOp"), QA("="), lhs, rhs)),
4400-
L(A("Bool"), QA("false")));
4401-
return L(A("If"), pred, lhs, L(A("Null")));
4398+
return L(A("PgNullIf"), lhs, rhs);
44024399
}
44034400

44044401
TAstNode* ParseAExprIn(const A_Expr* value, const TExprSettings& settings) {

ydb/library/yql/tests/sql/dq_file/part14/canondata/result.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,9 +2180,9 @@
21802180
],
21812181
"test.test[pg-nullif-default.txt-Debug]": [
21822182
{
2183-
"checksum": "a19676d0aca7eeb4d766d30dbaa027a9",
2184-
"size": 615,
2185-
"uri": "https://{canondata_backend}/1937027/9074da5ec3159ab717d6f0fee0639313448b4579/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql_patched"
2183+
"checksum": "15878810614467bf104f998fba1655bd",
2184+
"size": 737,
2185+
"uri": "https://{canondata_backend}/1775319/aeec36fe6eb29fa67191adf64023416d42730ad7/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql_patched"
21862186
}
21872187
],
21882188
"test.test[pg-nullif-default.txt-Plan]": [

ydb/library/yql/tests/sql/hybrid_file/part6/canondata/result.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,9 +1709,9 @@
17091709
],
17101710
"test.test[pg-nullif-default.txt-Debug]": [
17111711
{
1712-
"checksum": "9144cd982be865aa47ca681f7608114e",
1713-
"size": 614,
1714-
"uri": "https://{canondata_backend}/1937001/a770b7e950bbaeaf08ef4bbb336b7e3683a914ce/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql_patched"
1712+
"checksum": "4dcb2b695a7bd270235ed841f2987d61",
1713+
"size": 736,
1714+
"uri": "https://{canondata_backend}/1814674/0638f81fbaf2b2ba2ce2e6de8b8a96988a67d749/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql_patched"
17151715
}
17161716
],
17171717
"test.test[pg-nullif-default.txt-Plan]": [

ydb/library/yql/tests/sql/sql2yql/canondata/result.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11390,9 +11390,9 @@
1139011390
],
1139111391
"test_sql2yql.test[pg-nullif]": [
1139211392
{
11393-
"checksum": "dae077581c71350f52e42fed360dc646",
11394-
"size": 725,
11395-
"uri": "https://{canondata_backend}/937458/4426696fc50768982e8cf8f63bc4bda5708da9f4/resource.tar.gz#test_sql2yql.test_pg-nullif_/sql.yql"
11393+
"checksum": "5ad161b9dc9cb7a9697cdbf6486d4967",
11394+
"size": 957,
11395+
"uri": "https://{canondata_backend}/1689644/9b14941c017976925995bc2ca0ac4cd046275eea/resource.tar.gz#test_sql2yql.test_pg-nullif_/sql.yql"
1139611396
}
1139711397
],
1139811398
"test_sql2yql.test[pg-order_by_agg_extra_for_keys]": [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
--!syntax_pg
2-
select nullif(1, 1) as x, nullif(2, 1) as y;
2+
select nullif(1, 1), nullif(1, 2.2), nullif(2, 1), nullif(1.2, '7'), nullif(1.2, '1.2');

ydb/library/yql/tests/sql/yt_native_file/part14/canondata/result.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,9 +2120,9 @@
21202120
],
21212121
"test.test[pg-nullif-default.txt-Debug]": [
21222122
{
2123-
"checksum": "a772808658bb9749b940ccafd9cbd0c8",
2124-
"size": 554,
2125-
"uri": "https://{canondata_backend}/1599023/7a3b3fda17e3f5ea5a2102f6dc324ab02a3ba67c/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql"
2123+
"checksum": "e92ad4e8c9de59bdc528ffc9f7547611",
2124+
"size": 677,
2125+
"uri": "https://{canondata_backend}/1925842/1ad87feba078148d2bd94b42ca0bec2e4edbaee6/resource.tar.gz#test.test_pg-nullif-default.txt-Debug_/opt.yql"
21262126
}
21272127
],
21282128
"test.test[pg-nullif-default.txt-Plan]": [
@@ -2134,9 +2134,9 @@
21342134
],
21352135
"test.test[pg-nullif-default.txt-Results]": [
21362136
{
2137-
"checksum": "a04c967abb8d43ec8e21592c6f6a5817",
2138-
"size": 938,
2139-
"uri": "https://{canondata_backend}/1599023/7a3b3fda17e3f5ea5a2102f6dc324ab02a3ba67c/resource.tar.gz#test.test_pg-nullif-default.txt-Results_/results.txt"
2137+
"checksum": "d765d0c205914bea1a9f3c1f546d7368",
2138+
"size": 1829,
2139+
"uri": "https://{canondata_backend}/1925842/1ad87feba078148d2bd94b42ca0bec2e4edbaee6/resource.tar.gz#test.test_pg-nullif-default.txt-Results_/results.txt"
21402140
}
21412141
],
21422142
"test.test[pg-range_function_multi-default.txt-Debug]": [

0 commit comments

Comments
 (0)