Skip to content

Do not try to collect extra sort columns if ORDER BY is ignored #10417

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
Oct 15, 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: 4 additions & 0 deletions ydb/library/yql/sql/v1/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ void INode::UseAsInner() {
AsInner = true;
}

void INode::DisableSort() {
DisableSort_ = true;
}

bool INode::UsedSubquery() const {
return false;
}
Expand Down
2 changes: 2 additions & 0 deletions ydb/library/yql/sql/v1/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ namespace NSQLTranslationV1 {
virtual bool SetViewName(TContext& ctx, TPosition pos, const TString& view);
virtual bool SetPrimaryView(TContext& ctx, TPosition pos);
void UseAsInner();
void DisableSort();
virtual bool UsedSubquery() const;
virtual bool IsSelect() const;
virtual bool HasSelectResult() const;
Expand Down Expand Up @@ -275,6 +276,7 @@ namespace NSQLTranslationV1 {
bool ImplicitLabel = false;
mutable TNodeState State;
bool AsInner = false;
bool DisableSort_ = false;
};
typedef INode::TPtr TNodePtr;

Expand Down
5 changes: 3 additions & 2 deletions ydb/library/yql/sql/v1/select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ class TSelectCore: public IRealSource, public IComposableSource {

TNodePtr BuildSort(TContext& ctx, const TString& label) override {
Y_UNUSED(ctx);
if (OrderBy.empty()) {
if (OrderBy.empty() || DisableSort_) {
return nullptr;
}

Expand Down Expand Up @@ -1846,7 +1846,7 @@ class TSelectCore: public IRealSource, public IComposableSource {
if (!maybeExist.Defined()) {
return maybeExist;
}
if (!aggregated && column.GetColumnName() && IsMissingInProjection(ctx, column)) {
if (!DisableSort_ && !aggregated && column.GetColumnName() && IsMissingInProjection(ctx, column)) {
ExtraSortColumns[FullColumnName(column)] = &column;
}
return maybeExist;
Expand Down Expand Up @@ -2964,6 +2964,7 @@ class TSelect: public IProxySource {
}

if (IgnoreSort()) {
Source->DisableSort();
ctx.Warning(Source->GetPos(), TIssuesIds::YQL_ORDER_BY_WITHOUT_LIMIT_IN_SUBQUERY) << "ORDER BY without LIMIT in subquery will be ignored";
}

Expand Down
11 changes: 11 additions & 0 deletions ydb/library/yql/sql/v1/sql_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4312,6 +4312,17 @@ select FormatType($f());
"<main>:6:39: Error: Unknown correlation name: t\n");
}

Y_UNIT_TEST(ErrOrderByIgnoredButCheckedForMissingColumns) {
auto req = "$src = SELECT key FROM (SELECT 1 as key, 2 as subkey) ORDER BY x; SELECT * FROM $src;";
ExpectFailWithError(req, "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
"<main>:1:64: Error: Column x is not in source column set\n");

req = "$src = SELECT key FROM plato.Input ORDER BY x; SELECT * FROM $src;";
auto res = SqlToYql(req);
UNIT_ASSERT(res.Root);
UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n");
}

Y_UNIT_TEST(InvalidTtlInterval) {
auto req = R"(
USE plato;
Expand Down
11 changes: 11 additions & 0 deletions ydb/library/yql/sql/v1/sql_ut_antlr4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4309,6 +4309,17 @@ select FormatType($f());
"<main>:6:39: Error: Unknown correlation name: t\n");
}

Y_UNIT_TEST(ErrOrderByIgnoredButCheckedForMissingColumns) {
auto req = "$src = SELECT key FROM (SELECT 1 as key, 2 as subkey) ORDER BY x; SELECT * FROM $src;";
ExpectFailWithError(req, "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n"
"<main>:1:64: Error: Column x is not in source column set\n");

req = "$src = SELECT key FROM plato.Input ORDER BY x; SELECT * FROM $src;";
auto res = SqlToYql(req);
UNIT_ASSERT(res.Root);
UNIT_ASSERT_NO_DIFF(Err2Str(res), "<main>:1:8: Warning: ORDER BY without LIMIT in subquery will be ignored, code: 4504\n");
}

Y_UNIT_TEST(InvalidTtlInterval) {
auto req = R"(
USE plato;
Expand Down
14 changes: 14 additions & 0 deletions ydb/library/yql/tests/sql/sql2yql/canondata/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -11780,6 +11780,13 @@
"uri": "https://{canondata_backend}/1937001/6c1efa96a2ae4050b12a9381285624ab71389741/resource.tar.gz#test_sql2yql.test_order_by-order_by_missing_project_column_as_table_/sql.yql"
}
],
"test_sql2yql.test[order_by-order_by_missing_project_column_ignored]": [
{
"checksum": "f844bc6856fa7358172cb5bf2442f9e6",
"size": 6492,
"uri": "https://{canondata_backend}/1871182/b560fdc93cb96ca9e574901b22abd265bdefb213/resource.tar.gz#test_sql2yql.test_order_by-order_by_missing_project_column_ignored_/sql.yql"
}
],
"test_sql2yql.test[order_by-order_by_missing_project_column_join2]": [
{
"checksum": "98039874784e044ceb5ba77e2e8158b8",
Expand Down Expand Up @@ -31499,6 +31506,13 @@
"uri": "https://{canondata_backend}/1937001/6c1efa96a2ae4050b12a9381285624ab71389741/resource.tar.gz#test_sql_format.test_order_by-order_by_missing_project_column_as_table_/formatted.sql"
}
],
"test_sql_format.test[order_by-order_by_missing_project_column_ignored]": [
{
"checksum": "f3d38e1afa4667e4a86dbe72c0a9de94",
"size": 779,
"uri": "https://{canondata_backend}/1871182/b560fdc93cb96ca9e574901b22abd265bdefb213/resource.tar.gz#test_sql_format.test_order_by-order_by_missing_project_column_ignored_/formatted.sql"
}
],
"test_sql_format.test[order_by-order_by_missing_project_column_join2]": [
{
"checksum": "2c530f4a349a9abe39d58c9251c325bb",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
providers yt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* syntax version 1 */
/* postgres can not */
/* dq can not */
/* dqfile can not */
/* yt can not */
pragma warning("disable", "4504");

$t = [<|k:1, v:2|>];

$src = select k FROM as_table($t) order by x;
select * from $src;

$src = select a.k as key from as_table($t) as a join as_table($t) as b on a.k=b.k order by b.u;
select * from $src;

$src = select a.k as key from as_table($t) as a join as_table($t) as b on a.k=b.k order by v;
select * from $src;

$src = select a.k as key from as_table($t) as a join as_table($t) as b on a.k=b.k order by z;
select * from $src;
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,27 @@
"uri": "https://{canondata_backend}/1942100/33d51fa00fd086c78b2c0087e9e9f2249eef2d76/resource.tar.gz#test.test_order_by-order_by_dot_column-default.txt-Results_/results.txt"
}
],
"test.test[order_by-order_by_missing_project_column_ignored--Debug]": [
{
"checksum": "71d6905dd2e8fc6e3b80190e45d4f056",
"size": 636,
"uri": "https://{canondata_backend}/1881367/6f52d48e883a42b65664e9e69900a025414071f8/resource.tar.gz#test.test_order_by-order_by_missing_project_column_ignored--Debug_/opt.yql"
}
],
"test.test[order_by-order_by_missing_project_column_ignored--Plan]": [
{
"checksum": "55515ae638f317612d048052be489bfd",
"size": 1740,
"uri": "https://{canondata_backend}/1881367/6f52d48e883a42b65664e9e69900a025414071f8/resource.tar.gz#test.test_order_by-order_by_missing_project_column_ignored--Plan_/plan.txt"
}
],
"test.test[order_by-order_by_missing_project_column_ignored--Results]": [
{
"checksum": "34aa47c9e627357ce32cc88b1e8814eb",
"size": 2756,
"uri": "https://{canondata_backend}/1881367/6f52d48e883a42b65664e9e69900a025414071f8/resource.tar.gz#test.test_order_by-order_by_missing_project_column_ignored--Results_/results.txt"
}
],
"test.test[order_by-order_by_tablepath_column--Debug]": [
{
"checksum": "57839c0ee7f085e35c24a04cff8de49e",
Expand Down
Loading