Skip to content

Commit ae1b51a

Browse files
authored
Fix handling of uncorrelated column name in ORDER BY (#9602)
1 parent 00db6fb commit ae1b51a

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

ydb/library/yql/sql/v1/node.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,12 +1336,30 @@ bool MaybeAutogenerated(const TString& name) {
13361336
return !suffix.empty() && AllOf(suffix, [](const auto c) { return std::isdigit(c); });
13371337
}
13381338

1339+
bool MatchDotSuffix(const TSet<TString>& columns, const TString& column) {
1340+
for (const auto& col: columns) {
1341+
const auto pos = col.find_first_of(".");
1342+
if (pos == TString::npos) {
1343+
continue;
1344+
}
1345+
if (column == col.substr(pos + 1)) {
1346+
return true;
1347+
}
1348+
}
1349+
return false;
1350+
}
1351+
13391352
}
13401353

13411354
bool TColumns::IsColumnPossible(TContext& ctx, const TString& name) const {
13421355
if (All || Real.contains(name) || Artificial.contains(name)) {
13431356
return true;
13441357
}
1358+
1359+
if (ctx.SimpleColumns && !name.Contains('.') && (MatchDotSuffix(Real, name) || MatchDotSuffix(Artificial, name))) {
1360+
return true;
1361+
}
1362+
13451363
if (QualifiedAll) {
13461364
if (ctx.SimpleColumns) {
13471365
return true;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11738,6 +11738,13 @@
1173811738
"uri": "https://{canondata_backend}/1937001/6c1efa96a2ae4050b12a9381285624ab71389741/resource.tar.gz#test_sql2yql.test_order_by-order_by_missing_project_column_as_table_/sql.yql"
1173911739
}
1174011740
],
11741+
"test_sql2yql.test[order_by-order_by_missing_project_column_join2]": [
11742+
{
11743+
"checksum": "98039874784e044ceb5ba77e2e8158b8",
11744+
"size": 3496,
11745+
"uri": "https://{canondata_backend}/1814674/b5f58d06faf616e33e81d1596c8a849f37284058/resource.tar.gz#test_sql2yql.test_order_by-order_by_missing_project_column_join2_/sql.yql"
11746+
}
11747+
],
1174111748
"test_sql2yql.test[order_by-order_by_missing_project_column_join]": [
1174211749
{
1174311750
"checksum": "74c9e8c341785c084195974f3649ccc6",
@@ -31415,6 +31422,13 @@
3141531422
"uri": "https://{canondata_backend}/1937001/6c1efa96a2ae4050b12a9381285624ab71389741/resource.tar.gz#test_sql_format.test_order_by-order_by_missing_project_column_as_table_/formatted.sql"
3141631423
}
3141731424
],
31425+
"test_sql_format.test[order_by-order_by_missing_project_column_join2]": [
31426+
{
31427+
"checksum": "2c530f4a349a9abe39d58c9251c325bb",
31428+
"size": 615,
31429+
"uri": "https://{canondata_backend}/1814674/b5f58d06faf616e33e81d1596c8a849f37284058/resource.tar.gz#test_sql_format.test_order_by-order_by_missing_project_column_join2_/formatted.sql"
31430+
}
31431+
],
3141831432
"test_sql_format.test[order_by-order_by_missing_project_column_join]": [
3141931433
{
3142031434
"checksum": "0ece4190b44e63e1633ee74eb8e135db",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
providers yt
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* syntax version 1 */
2+
/* postgres can not */
3+
/* dq can not */
4+
/* dqfile can not */
5+
/* yt can not */
6+
7+
$src = [
8+
<|a:5, b:50, zz:500|>,
9+
<|a:4, b:40, zz:400|>,
10+
<|a:3, b:30, zz:300|>,
11+
<|a:2, b:20, zz:200|>,
12+
<|a:1, b:10, zz:100|>,
13+
];
14+
15+
$src1 = [
16+
<|e:5, f:50|>,
17+
<|e:4, f:40|>,
18+
<|e:3, f:30|>,
19+
<|e:2, f:20|>,
20+
<|e:1, f:10|>,
21+
];
22+
23+
24+
$src = select * from as_table($src);
25+
$src1 = select * from as_table($src1);
26+
27+
select x.zz, x.b + y.f as col1 from $src as x cross join $src1 as y where x.a = y.e order by zz, col1;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,27 @@
17131713
"uri": "https://{canondata_backend}/1775059/d0c85f9ec0b8e84cf0b352e7062a1381f04420c8/resource.tar.gz#test.test_order_by-SortByOneFieldDesc--Results_/results.txt"
17141714
}
17151715
],
1716+
"test.test[order_by-order_by_missing_project_column_join2--Debug]": [
1717+
{
1718+
"checksum": "ad887ace16ff2a422f178c08fef0c383",
1719+
"size": 1406,
1720+
"uri": "https://{canondata_backend}/1920236/1cce7ab93473c0f2eaed7fabadf44cba4fa8d8ca/resource.tar.gz#test.test_order_by-order_by_missing_project_column_join2--Debug_/opt.yql"
1721+
}
1722+
],
1723+
"test.test[order_by-order_by_missing_project_column_join2--Plan]": [
1724+
{
1725+
"checksum": "b4dd508a329723c74293d80f0278c705",
1726+
"size": 505,
1727+
"uri": "https://{canondata_backend}/1920236/1cce7ab93473c0f2eaed7fabadf44cba4fa8d8ca/resource.tar.gz#test.test_order_by-order_by_missing_project_column_join2--Plan_/plan.txt"
1728+
}
1729+
],
1730+
"test.test[order_by-order_by_missing_project_column_join2--Results]": [
1731+
{
1732+
"checksum": "3d9e8767efbc8ca0d58672911026268f",
1733+
"size": 1376,
1734+
"uri": "https://{canondata_backend}/1920236/1cce7ab93473c0f2eaed7fabadf44cba4fa8d8ca/resource.tar.gz#test.test_order_by-order_by_missing_project_column_join2--Results_/results.txt"
1735+
}
1736+
],
17161737
"test.test[order_by-single_item_tuple-default.txt-Debug]": [
17171738
{
17181739
"checksum": "22e49fe5842f57474ff573719f20251a",

0 commit comments

Comments
 (0)