Skip to content
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

[Enhancement] Use sql_mode to be compatible with legacy group_concat #36150

Merged
merged 2 commits into from
Dec 4, 2023

Conversation

liuyehcf
Copy link
Contributor

@liuyehcf liuyehcf commented Nov 30, 2023

Why I'm doing:

After #28778, the signature of group_concat has been changed. So for users that already used this feature, they need to update the sql as well which may include a lot of work.

What I'm doing:

Add a sql mode GROUP_CONCAT_LEGACY to support the syntax the way the were. make it work well with the legacy sql base.

MySQL [email protected]:test> select group_concat(value) from group_concat;
+-----------------------------------+
| group_concat(value SEPARATOR ',') |
+-----------------------------------+
| fruit,meat,seafood,fruit,drinks   |
+-----------------------------------+
1 row in set
Time: 0.017s
MySQL [email protected]:test> select group_concat(value,'-') from group_concat;
+---------------------------------------+
| group_concat(value,'-' SEPARATOR ',') |
+---------------------------------------+
| fruit-,drinks-,fruit-,meat-,seafood-  |
+---------------------------------------+
1 row in set
Time: 0.017s
MySQL [email protected]:test> set sql_mode='GROUP_CONCAT_LEGACY'
Query OK, 0 rows affected
Time: 0.001s
MySQL [email protected]:test> select group_concat(value) from group_concat;
+-------------------------------------+
| group_concat(value SEPARATOR ', ')  |
+-------------------------------------+
| fruit, drinks, fruit, meat, seafood |
+-------------------------------------+
1 row in set
Time: 0.018s
MySQL [email protected]:test> select group_concat(value,'-') from group_concat;
+-----------------------------------+
| group_concat(value SEPARATOR '-') |
+-----------------------------------+
| fruit-meat-seafood-fruit-drinks   |
+-----------------------------------+

Limitation

This work around process happens at parser stage. so var hint like /*+ SET_VAR(sql_mode = 'GROUP_CONCAT_LEGACY')*/ won't work

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

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

@liuyehcf liuyehcf requested a review from a team as a code owner November 30, 2023 07:41
String sep = ",";
sepExpr = new StringLiteral(sep, pos);
exprs.add(sepExpr);
}
}
if (!orderByElements.isEmpty()) {
int exprSize = exprs.size();
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:
Logic for checking isLegacyGroupConcat and adding the separator expression may cause unwanted behavior in cases where exprs.size() does not equal 1.

You can modify the code like this:

if (isLegacyGroupConcat) {
    // The original logic assumes there should be only a single expression
    // for legacy group concat, but does not handle other cases.
    // We need to define what should happen if exprs.size() is not 1.
    Expr sepExpr;
    String sep = exprs.size() == 1 ? ", " : /* some default or error handling */;
    sepExpr = new StringLiteral(sep, pos);
    exprs.add(sepExpr);
} else {
    Expr sepExpr;
    String sep = ",";
    sepExpr = new StringLiteral(sep, pos);
    exprs.add(sepExpr);
}

Assuming that the case where exprs.size() is not 1 is either an error condition or requires a default fallback, the modification ensures safe handling of all possible sizes of exprs. Please note that it's important to decide on the expected behavior when exprs.size() does not equal 1 - my suggestion is to handle this with either an error or a default value, but this depends on specific application logic.

kangkaisen
kangkaisen previously approved these changes Nov 30, 2023
Signed-off-by: liuyehcf <[email protected]>
Copy link

sonarcloud bot commented Dec 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 5 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

warning The version of Java (11.0.21) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 17.
Read more here

Copy link

github-actions bot commented Dec 1, 2023

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Dec 1, 2023

[FE Incremental Coverage Report]

pass : 1 / 1 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/qe/SqlModeHelper.java 1 1 100.00% []

@kangkaisen kangkaisen merged commit 11ece99 into StarRocks:main Dec 4, 2023
56 checks passed
Copy link

github-actions bot commented Dec 4, 2023

@Mergifyio backport branch-3.2

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

github-actions bot commented Dec 4, 2023

@Mergifyio backport branch-3.1

@github-actions github-actions bot removed the 3.1 label Dec 4, 2023
Copy link
Contributor

mergify bot commented Dec 4, 2023

backport branch-3.2

✅ Backports have been created

Copy link

github-actions bot commented Dec 4, 2023

@Mergifyio backport branch-3.0

@github-actions github-actions bot removed the 3.0 label Dec 4, 2023
Copy link
Contributor

mergify bot commented Dec 4, 2023

backport branch-3.1

✅ Backports have been created

Copy link
Contributor

mergify bot commented Dec 4, 2023

backport branch-3.0

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 4, 2023
mergify bot pushed a commit that referenced this pull request Dec 4, 2023
mergify bot pushed a commit that referenced this pull request Dec 4, 2023
…36150)

Signed-off-by: liuyehcf <[email protected]>
(cherry picked from commit 11ece99)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java
liuyehcf added a commit that referenced this pull request Dec 4, 2023
liuyehcf added a commit that referenced this pull request Dec 4, 2023
liuyehcf added a commit that referenced this pull request Dec 4, 2023
liuyehcf added a commit that referenced this pull request Dec 4, 2023
dingxin-tech pushed a commit to dingxin-tech/starrocks-odps that referenced this pull request Dec 12, 2023
@liuyehcf
Copy link
Contributor Author

liuyehcf commented Jan 9, 2024

version >= 3.0.9 or >=3.1.6 or >=3.2.1

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.

4 participants