Skip to content

don't parametrize default values for columns #1679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ydb/library/yql/sql/pg/pg_sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class TConverter : public IPGParseEvents {
bool AllowOver = false;
bool AllowReturnSet = false;
bool AllowSubLinks = false;
bool AutoParametrizeEnabled = true;
TVector<TAstNode*>* WindowItems = nullptr;
TString Scope;
};
Expand Down Expand Up @@ -1709,6 +1710,7 @@ class TConverter : public IPGParseEvents {
TExprSettings settings;
settings.AllowColumns = false;
settings.Scope = "DEFAULT";
settings.AutoParametrizeEnabled = false;
cinfo.Default = ParseExpr(constraintNode->raw_expr, settings);
if (!cinfo.Default) {
return false;
Expand Down Expand Up @@ -3107,7 +3109,7 @@ class TConverter : public IPGParseEvents {
? L(A("PgType"), QA(TPgConst::ToString(valueNType->type)))
: L(A("PgType"), QA("unknown"));

if (Settings.AutoParametrizeEnabled && !Settings.AutoParametrizeExprDisabledScopes.contains(settings.Scope)) {
if (Settings.AutoParametrizeEnabled && settings.AutoParametrizeEnabled) {
return AutoParametrizeConst(std::move(valueNType.GetRef()), pgTypeNode);
}

Expand Down
40 changes: 19 additions & 21 deletions ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
UNIT_ASSERT_C(res.Issues.Empty(), "Failed to parse statement, issues: " + res.Issues.ToString());
UNIT_ASSERT_C(res.PgAutoParamValues.Empty(), "Expected no auto parametrization");
}
Y_UNIT_TEST(AutoParamValues_DifferentTypes) {

Y_UNIT_TEST(AutoParamValues_NoParametersWithDefaults) {
TTranslationSettings settings;
settings.AutoParametrizeEnabled = true;
settings.AutoParametrizeValuesStmt = true;
settings.AutoParametrizeExprDisabledScopes = {"VALUES"};
auto res = SqlToYqlWithMode(
R"(insert into plato.Output values (1,2,3), (1,2.0,3))",
R"(CREATE TABLE t (a int PRIMARY KEY, b int DEFAULT 0))",
NSQLTranslation::ESqlMode::QUERY,
10,
{},
Expand All @@ -33,10 +32,10 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
}

using TUsedParamsGetter = std::function<void(TSet<TString>&, const NYql::TAstNode& node)>;

void GetUsedParamsInValues(TSet<TString>& usedParams, const NYql::TAstNode& node) {
const bool isPgSetItem =
node.IsListOfSize(2) && node.GetChild(0)->IsAtom()
const bool isPgSetItem =
node.IsListOfSize(2) && node.GetChild(0)->IsAtom()
&& node.GetChild(0)->GetContent() == "PgSetItem";
if (!isPgSetItem) {
return;
Expand All @@ -45,7 +44,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {

for (const auto* pgOption : pgSetItemOptions->GetChildren()) {
const bool isQuotedList =
pgOption->IsListOfSize(2) && pgOption->GetChild(0)->IsAtom()
pgOption->IsListOfSize(2) && pgOption->GetChild(0)->IsAtom()
&& pgOption->GetChild(0)->GetContent() == "quote";
if (!isQuotedList) {
continue;
Expand All @@ -54,7 +53,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
const auto* option = pgOption->GetChild(1);
const auto* optionName = option->GetChild(0);

const bool isValuesNode =
const bool isValuesNode =
optionName->IsListOfSize(2) && optionName->GetChild(0)->IsAtom()
&& optionName->GetChild(0)->GetContent() == "quote"
&& optionName->GetChild(1)->GetContent() == "values";
Expand All @@ -68,11 +67,10 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
}
}

void TestAutoParam(const TString& query, const THashMap<TString, TString>& expectedParamNameToJsonYdbVal, const TMap<TString, TString>& expectedParamTypes, TUsedParamsGetter usedParamsGetter, THashSet<TString> disabledParametrizeScopes = {}) {
void TestAutoParam(const TString& query, const THashMap<TString, TString>& expectedParamNameToJsonYdbVal, const TMap<TString, TString>& expectedParamTypes, TUsedParamsGetter usedParamsGetter) {
TTranslationSettings settings;
settings.AutoParametrizeEnabled = true;
settings.AutoParametrizeValuesStmt = true;
settings.AutoParametrizeExprDisabledScopes = disabledParametrizeScopes;
auto res = SqlToYqlWithMode(
query,
NSQLTranslation::ESqlMode::QUERY,
Expand All @@ -87,9 +85,9 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
TSet<TString> declaredParams;
TMap<TString, TString> actualParamTypes;
VisitAstNodes(*res.Root, [&declaredParams, &actualParamTypes] (const NYql::TAstNode& node) {
const bool isDeclareNode =
node.IsList() && node.GetChildrenCount() > 0
&& node.GetChild(0)->IsAtom()
const bool isDeclareNode =
node.IsList() && node.GetChildrenCount() > 0
&& node.GetChild(0)->IsAtom()
&& node.GetChild(0)->GetContent() == "declare";
if (isDeclareNode) {
UNIT_ASSERT_VALUES_EQUAL(node.GetChildrenCount(), 3);
Expand All @@ -114,7 +112,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {

auto actualParam = res.PgAutoParamValues.GetRef()[expectedParamName];
UNIT_ASSERT_STRINGS_EQUAL(expectedParam.ShortUtf8DebugString(), actualParam.ShortUtf8DebugString());
UNIT_ASSERT_C(declaredParams.contains(expectedParamName),
UNIT_ASSERT_C(declaredParams.contains(expectedParamName),
"Declared params don't contain expected param name: " << expectedParamName);
}

Expand Down Expand Up @@ -184,7 +182,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
TString type = "(ListType (TupleType (PgType 'unknown) (PgType 'int4)))";
TestAutoParam(query, {{"a0", expectedParamJson}}, {{"a0", type}}, GetUsedParamsInValues);
}

Y_UNIT_TEST(AutoParamConsts_Where) {
TString query = R"(
select * from plato.Output where key > 1
Expand All @@ -201,8 +199,8 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
return;
}
const auto quotedVal = maybeQuote.GetRef();
const bool isWhere =
quotedVal->IsListOfSize(2) && quotedVal->GetChild(1)->IsListOfSize(3)
const bool isWhere =
quotedVal->IsListOfSize(2) && quotedVal->GetChild(1)->IsListOfSize(3)
&& quotedVal->GetChild(1)->IsListOfSize(3)
&& quotedVal->GetChild(1)->GetChild(0)->IsAtom()
&& quotedVal->GetChild(1)->GetChild(0)->GetContent() == "PgWhere";
Expand All @@ -224,7 +222,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
TString type = "(PgType 'int4)";
TestAutoParam(query, {{"a0", expectedParamJson}}, {{"a0", type}}, usedInWhereComp);
}

Y_UNIT_TEST(AutoParamConsts_Select) {
TString query = R"(
select 1, 'test', B'10001'
Expand All @@ -250,7 +248,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
{"a2", "(PgType 'bit)"},
};
TestAutoParam(query, {
{"a0", expectedParamJsonInt4},
{"a0", expectedParamJsonInt4},
{"a1", expectedParamJsonText},
{"a2", expectedParamJsonBit},
}, expectedParamTypes, dummyGetter);
Expand All @@ -270,7 +268,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) {
const TUsedParamsGetter dummyGetter = [] (TSet<TString>& usedParams, const NYql::TAstNode&) {
usedParams = {"a0", "a1"};
};
TestAutoParam(query, {{"a0", expectedParamJsonInt4}, {"a1", expectedParamJsonText}}, paramToType, dummyGetter, {});
TestAutoParam(query, {{"a0", expectedParamJsonInt4}, {"a1", expectedParamJsonText}}, paramToType, dummyGetter);
}

}
3 changes: 1 addition & 2 deletions ydb/library/yql/sql/settings/translation_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NSQLTranslation {
};

enum class EBindingsMode {
// raise error
// raise error
DISABLED,
// classic support for bindings
ENABLED,
Expand Down Expand Up @@ -111,7 +111,6 @@ namespace NSQLTranslation {
TVector<ui32> PgParameterTypeOids;
bool AutoParametrizeEnabled = false;
bool AutoParametrizeValuesStmt = false;
THashSet<TString> AutoParametrizeExprDisabledScopes = {};

TGUCSettings::TPtr GUCSettings = std::make_shared<TGUCSettings>();
bool UnicodeLiterals = false;
Expand Down