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

[BugFix] Fix the bug that partition by range(str2date) does not check parameters #45838

Merged
merged 1 commit into from
May 20, 2024

Conversation

HangyuanLiu
Copy link
Contributor

@HangyuanLiu HangyuanLiu commented May 17, 2024

Why I'm doing:

What I'm doing:

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.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

@HangyuanLiu HangyuanLiu requested review from a team as code owners May 17, 2024 10:31
if (!PartitionFunctionChecker.checkStr2date(expr)) {
throw new SemanticException("partition function check fail," +
" only support str2date(VARCHAR str, VARCHAR format)");
}
}
} else {
throw new AnalysisException("Unsupported expr:" + expr.toSql());
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:
Missing null check for slotRef after calling AnalyzerUtils.getSlotRefFromFunctionCall(expr)

You can modify the code like this:

@@ -105,9 +108,20 @@ public void analyze(List<ColumnDef> columnDefs, Map<String, String> otherPropert
                     partitionType = ((CastExpr) expr).getTargetTypeDef().getType();
                 } else if (expr instanceof FunctionCallExpr) {
                     slotRef = AnalyzerUtils.getSlotRefFromFunctionCall(expr);
+
+                    // Ensure slotRef is not null before proceeding
+                    if (slotRef == null) {
+                        throw new AnalysisException("Failed to obtain SlotRef from function call.");
+                    }
+
                     Optional<ColumnDef> columnDef = columnDefs.stream()
                             .filter(c -> c.getName().equals(slotRef.getColumnName())).findFirst();
                     Preconditions.checkState(columnDef.isPresent());
                     slotRef.setType(columnDef.get().getType());
+
                     String functionName = ((FunctionCallExpr) expr).getFnName().getFunction().toLowerCase();
                     if (functionName.equals(FunctionSet.STR2DATE)) {
                         partitionType = Type.DATE;
+                        if (!PartitionFunctionChecker.checkStr2date(expr)) {
+                            throw new SemanticException("partition function check fail," +
+                                    " only support str2date(VARCHAR str, VARCHAR format)");
+                        }
                     }
                 } else {
                     throw an AnalysisException("Unsupported expr:" + expr.toSql());

This change ensures that a null slotRef (which could result from AnalyzerUtils.getSlotRefFromFunctionCall(expr) returning null, for expressions that do not directly involve a column reference) will be caught and handled properly, preventing potential NullPointerExceptions later in the code when operations on slotRef are attempted.

@github-actions github-actions bot added the 3.3 label May 17, 2024
Copy link

sonarcloud bot commented May 20, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarCloud

Copy link

[FE Incremental Coverage Report]

pass : 67 / 80 (83.75%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/analyzer/PartitionFunctionChecker.java 51 64 79.69% [31, 50, 55, 59, 83, 88, 94, 95, 101, 106, 117, 123, 128]
🔵 com/starrocks/sql/ast/ExpressionPartitionDesc.java 13 13 100.00% []
🔵 com/starrocks/sql/analyzer/MaterializedViewAnalyzer.java 3 3 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@Astralidea Astralidea merged commit 8ae5186 into StarRocks:main May 20, 2024
54 of 55 checks passed
Copy link

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label May 20, 2024
Copy link
Contributor

mergify bot commented May 20, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request May 20, 2024
… parameters (#45838)

Signed-off-by: HangyuanLiu <460660596@qq.com>
(cherry picked from commit 8ae5186)
wanpengfei-git pushed a commit that referenced this pull request May 20, 2024
… parameters (backport #45838) (#45938)

Co-authored-by: HangyuanLiu <460660596@qq.com>
@HangyuanLiu
Copy link
Contributor Author

https://github.com/Mergifyio backport branch-3.2

Copy link
Contributor

mergify bot commented May 21, 2024

backport branch-3.2

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request May 21, 2024
… parameters (#45838)

Signed-off-by: HangyuanLiu <460660596@qq.com>
(cherry picked from commit 8ae5186)
wanpengfei-git pushed a commit that referenced this pull request May 21, 2024
… parameters (backport #45838) (#46001)

Co-authored-by: HangyuanLiu <460660596@qq.com>
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.

None yet

5 participants