Skip to content

Commit f64d670

Browse files
authored
Added defaults args values in PgCall (#7421)
1 parent f319dde commit f64d670

File tree

7 files changed

+68
-23
lines changed

7 files changed

+68
-23
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ IGraphTransformer::TStatus PgCallWrapper(const TExprNode::TPtr& input, TExprNode
307307
if (isResolved) {
308308
auto procId = FromString<ui32>(input->Child(1)->Content());
309309
const auto& proc = NPg::LookupProc(procId, argTypes);
310-
if (proc.Name != name) {
310+
if (!AsciiEqualsIgnoreCase(proc.Name, name)) {
311311
ctx.Expr.AddError(TIssue(ctx.Expr.GetPosition(input->Pos()),
312-
TStringBuilder() << "Mismatch of resolved function name, expected: " << name << ", but got:" << proc.Name));
312+
TStringBuilder() << "Mismatch of resolved function name, expected: " << name << ", but got: " << proc.Name));
313313
return IGraphTransformer::TStatus::Error;
314314
}
315315

@@ -378,6 +378,27 @@ IGraphTransformer::TStatus PgCallWrapper(const TExprNode::TPtr& input, TExprNode
378378
children[i+3] = WrapWithPgCast(std::move(children[i+3]), targetType, ctx.Expr);
379379
}
380380
}
381+
382+
if (argTypes.size() < fargTypes.size()) {
383+
YQL_ENSURE(fargTypes.size() - argTypes.size() <= (*procPtr)->DefaultArgs.size());
384+
for (size_t i = argTypes.size(); i < fargTypes.size(); ++i) {
385+
const auto& value = (*procPtr)->DefaultArgs[i + (*procPtr)->DefaultArgs.size() - fargTypes.size()];
386+
TExprNode::TPtr defNode;
387+
if (!value) {
388+
defNode = ctx.Expr.NewCallable(input->Pos(), "Null", {});
389+
} else {
390+
defNode = ctx.Expr.Builder(input->Pos())
391+
.Callable("PgConst")
392+
.Atom(0, *value)
393+
.Callable(1, "PgType")
394+
.Atom(0, NPg::LookupType(fargTypes[i]).Name)
395+
.Seal()
396+
.Seal()
397+
.Build();
398+
}
399+
children.insert(children.end(), defNode);
400+
}
401+
}
381402
output = ctx.Expr.NewCallable(input->Pos(), "PgResolvedCall", std::move(children));
382403
} else if (const auto* typePtr = std::get_if<const NPg::TTypeDesc*>(&procOrType)) {
383404
output = WrapWithPgCast(std::move(children[2]), (*typePtr)->TypeId, ctx.Expr);

ydb/library/yql/minikql/mkql_type_builder.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ namespace {
2626
static const TString UdfName("UDF");
2727

2828
class TPgTypeIndex {
29-
static constexpr ui32 MaxOid = 15000;
30-
using TUdfTypes = std::array<NYql::NUdf::TPgTypeDescription, MaxOid>;
29+
using TUdfTypes = TVector<NYql::NUdf::TPgTypeDescription>;
3130
TUdfTypes Types;
3231

3332
public:
3433
TPgTypeIndex() {
34+
Rebuild();
35+
}
36+
37+
void Rebuild() {
38+
Types.clear();
39+
ui32 maxTypeId = 0;
40+
NYql::NPg::EnumTypes([&](ui32 typeId, const NYql::NPg::TTypeDesc&) {
41+
maxTypeId = Max(maxTypeId, typeId);
42+
});
43+
44+
Types.resize(maxTypeId + 1);
3545
NYql::NPg::EnumTypes([&](ui32 typeId, const NYql::NPg::TTypeDesc& t) {
36-
Y_ABORT_UNLESS(typeId < Types.size());
3746
auto& e = Types[typeId];
3847
e.Name = t.Name;
3948
e.TypeId = t.TypeId;
@@ -2680,5 +2689,9 @@ TType* TTypeBuilder::NewVariantType(TType* underlyingType) const {
26802689
return TVariantType::Create(underlyingType, Env);
26812690
}
26822691

2692+
void RebuildTypeIndex() {
2693+
HugeSingleton<TPgTypeIndex>()->Rebuild();
2694+
}
2695+
26832696
} // namespace NMiniKQL
26842697
} // namespace Nkikimr

ydb/library/yql/minikql/mkql_type_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ class TTypeBuilder : public TMoveOnly {
324324
bool UseNullType = true;
325325
};
326326

327+
void RebuildTypeIndex();
327328

328329
} // namespace NMiniKQL
329330
} // namespace Nkikimr

ydb/library/yql/parser/pg_catalog/catalog.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,13 +2529,14 @@ bool IsExactMatch(const TVector<ui32>& procArgTypes, ui32 procVariadicType, cons
25292529
return true;
25302530
}
25312531

2532-
ui64 CalcProcScore(const TVector<ui32>& procArgTypes, ui32 procVariadicType, const TVector<ui32>& argTypeIds, const TCatalog& catalog) {
2532+
ui64 CalcProcScore(const TVector<ui32>& procArgTypes, ui32 procVariadicType, ui32 procDefArgs, const TVector<ui32>& argTypeIds, const TCatalog& catalog) {
25332533
ui64 result = 0UL;
25342534
if (!procVariadicType) {
25352535
++result;
25362536
}
25372537

2538-
if (argTypeIds.size() < procArgTypes.size()) {
2538+
Y_ENSURE(procArgTypes.size() >= procDefArgs);
2539+
if (argTypeIds.size() < procArgTypes.size() - procDefArgs) {
25392540
return ArgTypeMismatch;
25402541
}
25412542

@@ -2804,7 +2805,7 @@ std::variant<const TProcDesc*, const TTypeDesc*> LookupProcWithCasts(const TStri
28042805
}
28052806

28062807
// https://www.postgresql.org/docs/14/typeconv-func.html, steps 4.a, 4.c, 4.d
2807-
auto score = NPrivate::CalcProcScore(d->ArgTypes, d->VariadicType, argTypeIds, catalog);
2808+
auto score = NPrivate::CalcProcScore(d->ArgTypes, d->VariadicType, d->DefaultArgs.size(), argTypeIds, catalog);
28082809
if (bestScore < score) {
28092810
bestScore = score;
28102811

@@ -3169,7 +3170,7 @@ const TAggregateDesc& LookupAggregation(const TString& name, const TVector<ui32>
31693170
}
31703171

31713172
// https://www.postgresql.org/docs/14/typeconv-func.html, steps 4.a, 4.c, 4.d
3172-
auto score = NPrivate::CalcProcScore(d->ArgTypes, 0, argTypeIds, catalog);
3173+
auto score = NPrivate::CalcProcScore(d->ArgTypes, 0, 0, argTypeIds, catalog);
31733174

31743175
if (bestScore < score) {
31753176
bestScore = score;

ydb/library/yql/parser/pg_catalog/catalog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ struct TProcDesc {
7272
ui32 VariadicType = 0;
7373
ui32 VariadicArgType = 0;
7474
TString VariadicArgName;
75+
TVector<TMaybe<TString>> DefaultArgs;
7576
ui32 ExtensionIndex = 0;
7677
};
7778

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

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern "C" {
3636
#include <ydb/library/yql/parser/pg_wrapper/postgresql/src/backend/catalog/pg_type_d.h>
3737
#include <ydb/library/yql/parser/pg_catalog/catalog.h>
3838
#include <ydb/library/yql/providers/common/provider/yql_provider_names.h>
39+
#include <ydb/library/yql/minikql/mkql_type_builder.h>
3940
#include <ydb/library/yql/core/issue/yql_issue.h>
4041
#include <ydb/library/yql/core/yql_callable_names.h>
4142
#include <ydb/library/yql/parser/pg_catalog/catalog.h>
@@ -5423,15 +5424,30 @@ class TExtensionHandler : public IPGParseEvents {
54235424
}
54245425

54255426
bool hasArgNames = false;
5426-
ui32 defArgsCount = 0;
54275427
for (int i = 0; i < ListLength(value->parameters); ++i) {
54285428
auto node = LIST_CAST_NTH(FunctionParameter, value->parameters, i);
54295429
hasArgNames = hasArgNames || (node->name != nullptr);
54305430
if (node->mode == FUNC_PARAM_IN || node->mode == FUNC_PARAM_DEFAULT) {
54315431
if (node->defexpr) {
5432-
++defArgsCount;
5432+
desc.DefaultArgs.emplace_back();
5433+
auto& value = desc.DefaultArgs.back();
5434+
auto expr = node->defexpr;
5435+
if (NodeTag(expr) == T_TypeCast) {
5436+
expr = CAST_NODE(TypeCast, expr)->arg;
5437+
}
5438+
5439+
if (NodeTag(expr) != T_A_Const) {
5440+
return false;
5441+
}
5442+
5443+
auto pgConst = GetValueNType(CAST_NODE(A_Const, expr));
5444+
if (!pgConst) {
5445+
return false;
5446+
}
5447+
5448+
value = pgConst->value;
54335449
} else {
5434-
Y_ENSURE(!defArgsCount);
5450+
Y_ENSURE(desc.DefaultArgs.empty());
54355451
}
54365452

54375453
desc.InputArgNames.push_back(node->name ? node->name : "");
@@ -5470,17 +5486,6 @@ class TExtensionHandler : public IPGParseEvents {
54705486
}
54715487

54725488
Builder.CreateProc(desc);
5473-
if (defArgsCount) {
5474-
Y_ENSURE(!desc.VariadicType);
5475-
for (ui32 i = 0; i < defArgsCount; ++i) {
5476-
desc.ArgTypes.pop_back();
5477-
if (!desc.InputArgNames.empty()) {
5478-
desc.InputArgNames.pop_back();
5479-
}
5480-
5481-
Builder.CreateProc(desc);
5482-
}
5483-
}
54845489
return true;
54855490
}
54865491

@@ -5584,6 +5589,8 @@ class TExtensionSqlParser : public NYql::NPg::IExtensionSqlParser {
55845589
for (const auto& sql : sqls) {
55855590
NYql::PGParse(sql, handler);
55865591
}
5592+
5593+
NKikimr::NMiniKQL::RebuildTypeIndex();
55875594
}
55885595
};
55895596

ydb/library/yql/sql/pg/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ PEERDIR(
88
ydb/library/yql/ast
99
ydb/library/yql/core
1010
ydb/library/yql/parser/pg_catalog
11+
ydb/library/yql/minikql
1112
ydb/library/yql/sql/settings
1213
ydb/public/api/protos
1314
)

0 commit comments

Comments
 (0)