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] Simplify date/datetime column predicate in certain cases #50643

Merged
merged 5 commits into from
Sep 11, 2024

Conversation

kaijianding
Copy link
Contributor

@kaijianding kaijianding commented Sep 3, 2024

Why I'm doing:

Some user use date/datetime column as string, in this case sparse index is not used.
This pr will do these rewrite

 --if t is date
 date_format(t, '%Y%m%d') >= '20230327' -> `t` >= '20230327'
 date_format(t, '%Y-%m-%d') >= '2023-03-27' -> `t` >= '2023-03-27'
 substr(t, 1, 10) >= '2023-03-27' -> `t` >= '2023-03-27'
 substring(t, 1, 10) >= '2023-03-27' -> `t` >= '2023-03-27'
 replace(substring(t, 1, 10), "-", "") >= '20230327' -> `t` >= '20230327'
 
 --if t is datetime
 date_format(t, '%Y-%m-%d') >/>=/< '2023-03-27' -> `t` >/>=/< '2023-03-27'
 date_format(t, '%Y-%m-%d') <= '2023-03-27' -> `t` < days_add('2023-03-27', 1)

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.

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

@kaijianding kaijianding requested a review from a team as a code owner September 3, 2024 15:02
@github-actions github-actions bot added the 3.3 label Sep 3, 2024
return new SubstrExtractor((CallOperator) call.getChild(0), value, 8).extractColumn();
}
}
}
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:
Mismatch of function name in ReplaceAndSubstrExtractor#check when checking the empty string.
You can modify the code like this:

+        @Override
+        public boolean check() {
+            if (!(call.getChild(0) instanceof CallOperator && isSubstrFn((CallOperator) call.getChild(0)))) {
+                return false;
+            }
+            if (!call.getChild(1).isConstantRef() || !((ConstantOperator) call.getChild(1)).getVarchar().equals("-")) {
+                return false;
+            }
-            if (!call.getChild(2).isConstantRef() || !((ConstantOperator) call.getChild(2)).getVarchar().isEmpty()) {
+            if (!call.getChild(2).isConstantRef() || !((ConstantOperator) call.getChild(2)).getVarchar().equals("")) {
                return false;
+            }
+            // yyyyMMdd
+            if (value.getVarchar().length() != 8) {
+                return false;
+            }
+            return new SubstrExtractor((CallOperator) call.getChild(0), value, 8).check();
+        }

Explanation:
The original condition for verifying that the third argument to replace is an empty string used .isEmpty() method. However, to ensure consistency and correctness with the other comparisons, we should use .equals("") to explicitly check for an empty string.

@packy92
Copy link
Contributor

packy92 commented Sep 4, 2024

please test date_format(t, '%Y%m%d') >= '20230400'. If the right constant value is a illegal date string, is the rewriten result same with before?

@kaijianding
Copy link
Contributor Author

kaijianding commented Sep 4, 2024

please test date_format(t, '%Y%m%d') >= '20230400'. If the right constant value is a illegal date string, is the rewriten result same with before?

PREDICATES: date_format(2: t, '%Y%m%d') >= '2023030700' is the result, no rewritten.

But I will use regex to verify the right constant.

Signed-off-by: kaijian.ding <[email protected]>
Signed-off-by: kaijian.ding <[email protected]>
@packy92
Copy link
Contributor

packy92 commented Sep 6, 2024

please test date_format(t, '%Y%m%d') >= '20230400'. If the right constant value is a illegal date string, is the rewriten result same with before?

PREDICATES: date_format(2: t, '%Y%m%d') >= '2023030700' is the result, no rewritten.

But I will use regex to verify the right constant.

I means clients may use 20230400 as the right constant value, this value seems to pass the regex check. But it's not a valid date value. Does the rule will rewrite it to dt >= cast('20230400' as date) ? If yes, the right cast returns a null may cause different behavior from before.

@kaijianding
Copy link
Contributor Author

@packy92 U r right, if right constant is not valid date, the result is not consitent with that before rewrite. I will verify the right constant is valid date

Copy link

sonarcloud bot commented Sep 6, 2024

Copy link

github-actions bot commented Sep 6, 2024

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

github-actions bot commented Sep 6, 2024

[FE Incremental Coverage Report]

pass : 88 / 91 (96.70%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/sql/optimizer/rewrite/scalar/SimplifiedDateColumnPredicateRule.java 88 91 96.70% [150, 222, 225]

Copy link

github-actions bot commented Sep 6, 2024

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@kaijianding
Copy link
Contributor Author

@packy92 any more comment?

@kangkaisen kangkaisen merged commit a8af0fc into StarRocks:main Sep 11, 2024
49 checks passed
Copy link

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label Sep 11, 2024
Copy link
Contributor

mergify bot commented Sep 11, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 11, 2024
@kaijianding kaijianding deleted the date branch September 11, 2024 06:36
HangyuanLiu pushed a commit to HangyuanLiu/starrocks that referenced this pull request Sep 12, 2024
wanpengfei-git pushed a commit that referenced this pull request Sep 18, 2024
renzhimin7 pushed a commit to renzhimin7/starrocks that referenced this pull request Nov 7, 2024
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