Skip to content

[BugFix] fix wrong order by scope for distinct query #37910

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 4 commits into from
Dec 28, 2023

Conversation

packy92
Copy link
Contributor

@packy92 packy92 commented Dec 27, 2023

Why I'm doing:
Fail to execute select distinct * from tbl order by col.
Root Casue:
The scope for order by is like `scope([], parentScope(tbl.col1, tbl.col2, ...)), so the analyze result of col is a field from parent scope filed. When check whether the col is a valid expr for aggregation expr, the col should be in the original order by scope instead of its parent scope. If not, error happens.
Actually, the scope for order should be the output of the distinct and cannot 'see' its parent scope.

What I'm doing:

  • combine output scope and parent scope when distinct and set partent scope to null.
  • add a enable_strict_order_by session variable default value is true to keep same behavior in 2.3 version.
  • add a more loose de-duplicate operatiton (only save the first unique col name field) when set enable_strict_order_by = false to execute sql like select distinct *, abs(v1) v1 from tbl order by v1 which is a valid sql in MySQL.

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.2
    • 3.1
    • 3.0
    • 2.5

allFields.add(field);
}
return allFields;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Inconsistent behavior when parsing ORDER BY expression for queries with SELECT DISTINCT.

You can modify the code like this:

// In analyzeOrderBy method, adjust the part where aggregations are checked within a SELECT DISTINCT context

if (isDistinct && !aggregations.isEmpty()) {
    // Here we need to not only throw an exception if there are aggregations,
    // but also ensure that columns in the ORDER BY clause are present in the SELECT list.
    boolean orderByNotInSelectList = !outputExpressions.containsAll(expression.toExprList());
    if(orderByNotInSelectList) {
        throw new SemanticException("for SELECT DISTINCT, ORDER BY expressions must appear in the select list",
                expression.getPos());
    }
}

// Later in the same method, we also need to ensure that the ORDER BY fields are consistently validated
// against the correct scope when SELECT DISTINCT is used.

if (!isDistinct) {
    analyzeExpression(expression, analyzeState, orderByScope);
} else {
    analyzeExpression(expression, analyzeState, orderByScope.getParent());
}

This addresses the issue where the ORDER BY clause might reference expressions not in the SELECT list when using DISTINCT, which should result in an error as implemented in this proposed fix. Additionally, when SELECT DISTINCT is true, the ORDER BY expressions would correctly be analyzed against the parent scope rather than the possibly incorrect orderByScope.

@packy92 packy92 force-pushed the main_distinct_transfrom branch from 726a50c to 5eb931e Compare December 27, 2023 09:41
Copy link

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

4 New issues
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

[FE Incremental Coverage Report]

pass : 36 / 44 (81.82%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/qe/SessionVariable.java 2 4 50.00% [3019, 3020]
🔵 com/starrocks/sql/analyzer/AggregationAnalyzer.java 3 4 75.00% [130]
🔵 com/starrocks/sql/analyzer/SelectAnalyzer.java 30 35 85.71% [340, 341, 343, 351, 352]
🔵 com/starrocks/sql/ast/FieldReference.java 1 1 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@kangkaisen kangkaisen merged commit fa72214 into StarRocks:main Dec 28, 2023
Copy link

@Mergifyio backport branch-3.2

@github-actions github-actions bot removed the 3.2 label Dec 28, 2023
Copy link

@Mergifyio backport branch-3.1

Copy link

@Mergifyio backport branch-3.0

Copy link
Contributor

mergify bot commented Dec 28, 2023

backport branch-3.2

✅ Backports have been created

Copy link

@Mergifyio backport branch-2.5

Copy link
Contributor

mergify bot commented Dec 28, 2023

backport branch-3.1

✅ Backports have been created

@github-actions github-actions bot removed the 2.5 label Dec 28, 2023
Copy link
Contributor

mergify bot commented Dec 28, 2023

backport branch-3.0

✅ Backports have been created

Copy link
Contributor

mergify bot commented Dec 28, 2023

backport branch-2.5

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 28, 2023
Signed-off-by: packy92 <[email protected]>
(cherry picked from commit fa72214)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AggregationAnalyzer.java
mergify bot pushed a commit that referenced this pull request Dec 28, 2023
Signed-off-by: packy92 <[email protected]>
(cherry picked from commit fa72214)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/qe/SessionVariable.java
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AggregationAnalyzer.java
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/OrderByTest.java
mergify bot pushed a commit that referenced this pull request Dec 28, 2023
Signed-off-by: packy92 <[email protected]>
(cherry picked from commit fa72214)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/qe/SessionVariable.java
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AggregationAnalyzer.java
#	fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/OrderByTest.java
mergify bot pushed a commit that referenced this pull request Dec 28, 2023
Signed-off-by: packy92 <[email protected]>
(cherry picked from commit fa72214)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/qe/SessionVariable.java
#	fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AggregationAnalyzer.java
#	fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java
#	fe/fe-core/src/test/java/com/starrocks/sql/plan/OrderByTest.java
packy92 added a commit to packy92/starrocks that referenced this pull request Dec 28, 2023
packy92 added a commit to packy92/starrocks that referenced this pull request Dec 28, 2023
Seaven pushed a commit that referenced this pull request Dec 29, 2023
packy92 added a commit to packy92/starrocks that referenced this pull request Dec 29, 2023
packy92 added a commit that referenced this pull request Dec 29, 2023
packy92 added a commit to packy92/starrocks that referenced this pull request Dec 29, 2023
packy92 added a commit that referenced this pull request Dec 29, 2023
Seaven pushed a commit that referenced this pull request Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants