Skip to content

Commit 01a61d9

Browse files
fedor-mironFiodar Miron
authored andcommitted
YQL-17145: fix autoparametrization of null values and refactor settings (ydb-platform#555)
* fix autoparametrization of null values and refactor settings * fix ut --------- Co-authored-by: Fiodar Miron <[email protected]>
1 parent e1fd652 commit 01a61d9

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

ydb/core/kqp/host/kqp_translate.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ NSQLTranslation::TTranslationSettings GetTranslationSettings(NYql::EKikimrQueryT
6565

6666
if (settings.PgParser) {
6767
settings.AutoParametrizeEnabled = isEnablePgConstsToParams;
68-
settings.AutoParametrizeEnabledScopes = {"WHERE", "VALUES", "DISTINCT ON", "JOIN ON", "GROUP BY",
69-
"HAVING", "SELECT", "LIMIT", "OFFSET", "RANGE FUNCTION", "GROUPING", "SUBLINK TEST",
70-
"PARTITITON BY", "FRAME", "ORDER BY"};
68+
settings.AutoParametrizeValuesStmt = isEnablePgConstsToParams;
7169
}
7270

7371
if (queryType == NYql::EKikimrQueryType::Scan || queryType == NYql::EKikimrQueryType::Query) {

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ class TConverter : public IPGParseEvents {
300300
typeOid != UNKNOWNOID ? NPg::LookupType(typeOid).Name : DEFAULT_PARAM_TYPE;
301301
ParamNameToPgTypeName[paramName] = typeName;
302302
}
303+
303304
}
304305

305306
void OnResult(const List* raw) {
@@ -452,7 +453,7 @@ class TConverter : public IPGParseEvents {
452453
Y_ABORT_UNLESS(rawValuesLists);
453454
size_t rows = ListLength(rawValuesLists);
454455

455-
if (rows == 0 || !Settings.AutoParametrizeEnabled) {
456+
if (rows == 0 || !Settings.AutoParametrizeEnabled || !Settings.AutoParametrizeValuesStmt) {
456457
return false;
457458
}
458459

@@ -580,8 +581,8 @@ class TConverter : public IPGParseEvents {
580581
}
581582

582583
TVector<TPgConst> pgConsts;
583-
bool allValsAreLiteral = ExtractPgConstsForAutoParam(valuesLists, pgConsts);
584-
if (allValsAreLiteral) {
584+
bool canAutoparametrize = ExtractPgConstsForAutoParam(valuesLists, pgConsts);
585+
if (canAutoparametrize) {
585586
auto maybeColumnTypes = InferColumnTypesForValuesStmt(pgConsts, valNames.size());
586587
if (maybeColumnTypes) {
587588
auto valuesNode = MakeValuesStmtAutoParam(std::move(pgConsts), std::move(maybeColumnTypes.GetRef()));
@@ -2928,11 +2929,10 @@ class TConverter : public IPGParseEvents {
29282929
typedValue.mutable_type()->mutable_pg_type()->set_oid(oid);
29292930

29302931
auto* value = typedValue.mutable_value();
2931-
if (valueNType.value) {
2932-
value->set_text_value(std::move(valueNType.value.GetRef()));
2933-
} else {
2934-
Y_ABORT_UNLESS(valueNType.type == TPgConst::Type::unknown, "NULL is allowed to only be of unknown type");
2932+
if (valueNType.type == TPgConst::Type::nil) {
29352933
value->set_null_flag_value(NProtoBuf::NULL_VALUE);
2934+
} else {
2935+
value->set_text_value(std::move(valueNType.value.GetRef()));
29362936
}
29372937

29382938
const auto& paramName = AddAutoParam(std::move(typedValue));
@@ -2955,8 +2955,7 @@ class TConverter : public IPGParseEvents {
29552955
? L(A("PgType"), QA(TPgConst::ToString(valueNType->type)))
29562956
: L(A("PgType"), QA("unknown"));
29572957

2958-
if (Settings.AutoParametrizeEnabled &&
2959-
Settings.AutoParametrizeEnabledScopes.contains(settings.Scope)) {
2958+
if (Settings.AutoParametrizeEnabled && !Settings.AutoParametrizeExprDisabledScopes.contains(settings.Scope)) {
29602959
return AutoParametrizeConst(std::move(valueNType.GetRef()), pgTypeNode);
29612960
}
29622961

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

+23-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
1717
Y_UNIT_TEST(AutoParamValues_DifferentTypes) {
1818
TTranslationSettings settings;
1919
settings.AutoParametrizeEnabled = true;
20+
settings.AutoParametrizeValuesStmt = true;
2021
auto res = SqlToYqlWithMode(
2122
R"(insert into plato.Output values (1,2,3), (1,2.0,3))",
2223
NSQLTranslation::ESqlMode::QUERY,
@@ -66,10 +67,11 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
6667
}
6768
}
6869

69-
void TestAutoParam(const TString& query, const THashMap<TString, TString>& expectedParamNameToJsonYdbVal, const TMap<TString, TString>& expectedParamTypes, TUsedParamsGetter usedParamsGetter, THashSet<TString> enabledParametrizeScopes = {}) {
70+
void TestAutoParam(const TString& query, const THashMap<TString, TString>& expectedParamNameToJsonYdbVal, const TMap<TString, TString>& expectedParamTypes, TUsedParamsGetter usedParamsGetter, THashSet<TString> disabledParametrizeScopes = {}) {
7071
TTranslationSettings settings;
7172
settings.AutoParametrizeEnabled = true;
72-
settings.AutoParametrizeEnabledScopes = enabledParametrizeScopes;
73+
settings.AutoParametrizeValuesStmt = true;
74+
settings.AutoParametrizeExprDisabledScopes = disabledParametrizeScopes;
7375
auto res = SqlToYqlWithMode(
7476
query,
7577
NSQLTranslation::ESqlMode::QUERY,
@@ -190,7 +192,6 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
190192
{"type":{"pg_type": {"oid": 23}},
191193
"value":{"text_value": "1"}}
192194
)";
193-
THashSet<TString> enabledScopes {"WHERE"};
194195

195196
// We expect: (PgOp '">" (PgColumnRef '"key") a0)
196197
const TUsedParamsGetter usedInWhereComp = [] (TSet<TString>& usedParams, const NYql::TAstNode& node) {
@@ -220,7 +221,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
220221
usedParams.insert(TString(pgBinOpSecondArg->GetContent()));
221222
};
222223
TString type = "(PgType 'int4)";
223-
TestAutoParam(query, {{"a0", expectedParamJson}}, {{"a0", type}}, usedInWhereComp, enabledScopes);
224+
TestAutoParam(query, {{"a0", expectedParamJson}}, {{"a0", type}}, usedInWhereComp);
224225
}
225226

226227
Y_UNIT_TEST(AutoParamConsts_Select) {
@@ -239,7 +240,6 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
239240
{"type":{"pg_type": {"oid": 1560}},
240241
"value":{"text_value": "b10001"}}
241242
)";
242-
THashSet<TString> enabledScopes {"SELECT"};
243243
const TUsedParamsGetter dummyGetter = [] (TSet<TString>& usedParams, const NYql::TAstNode&) {
244244
usedParams = {"a0", "a1", "a2"};
245245
};
@@ -252,7 +252,24 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
252252
{"a0", expectedParamJsonInt4},
253253
{"a1", expectedParamJsonText},
254254
{"a2", expectedParamJsonBit},
255-
}, expectedParamTypes, dummyGetter, enabledScopes);
255+
}, expectedParamTypes, dummyGetter);
256+
}
257+
258+
Y_UNIT_TEST(AutoParamValues_FailToInferColumnType) {
259+
const auto query = R"(INSERT INTO test VALUES (1), ('2');)";
260+
TMap<TString, TString> paramToType = {{"a0", "(PgType 'int4)"}, {"a1", "(PgType 'unknown)"}};
261+
TString expectedParamJsonInt4 = R"(
262+
{"type":{"pg_type": {"oid": 23}},
263+
"value":{"text_value": "1"}}
264+
)";
265+
TString expectedParamJsonText = R"(
266+
{"type":{"pg_type": {"oid": 705}},
267+
"value":{"text_value": "2"}}
268+
)";
269+
const TUsedParamsGetter dummyGetter = [] (TSet<TString>& usedParams, const NYql::TAstNode&) {
270+
usedParams = {"a0", "a1"};
271+
};
272+
TestAutoParam(query, {{"a0", expectedParamJsonInt4}, {"a1", expectedParamJsonText}}, paramToType, dummyGetter, {});
256273
}
257274

258275
}

ydb/library/yql/sql/settings/translation_settings.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ namespace NSQLTranslation {
107107

108108
TVector<ui32> PgParameterTypeOids;
109109
bool AutoParametrizeEnabled = false;
110-
THashSet<TString> AutoParametrizeEnabledScopes = {};
110+
bool AutoParametrizeValuesStmt = false;
111+
THashSet<TString> AutoParametrizeExprDisabledScopes = {};
111112
};
112113

113114
bool ParseTranslationSettings(const TString& query, NSQLTranslation::TTranslationSettings& settings, NYql::TIssues& issues);

0 commit comments

Comments
 (0)