-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1445b2
[Enhancement] Simplify date/datetime column predicate in certain cases
kaijianding d1dc51f
verify right constant is date
kaijianding 37d7988
fix days_add fn
kaijianding 0868c23
fix ut
kaijianding 65df445
verify right constant is valid date
kaijianding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 242 additions & 0 deletions
242
...in/java/com/starrocks/sql/optimizer/rewrite/scalar/SimplifiedDateColumnPredicateRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
// Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.starrocks.sql.optimizer.rewrite.scalar; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.starrocks.analysis.BinaryType; | ||
import com.starrocks.analysis.Expr; | ||
import com.starrocks.catalog.Function; | ||
import com.starrocks.catalog.FunctionSet; | ||
import com.starrocks.catalog.PrimitiveType; | ||
import com.starrocks.catalog.Type; | ||
import com.starrocks.sql.optimizer.operator.scalar.BinaryPredicateOperator; | ||
import com.starrocks.sql.optimizer.operator.scalar.CallOperator; | ||
import com.starrocks.sql.optimizer.operator.scalar.CastOperator; | ||
import com.starrocks.sql.optimizer.operator.scalar.ConstantOperator; | ||
import com.starrocks.sql.optimizer.operator.scalar.ScalarOperator; | ||
import com.starrocks.sql.optimizer.rewrite.ScalarOperatorFunctions; | ||
import com.starrocks.sql.optimizer.rewrite.ScalarOperatorRewriteContext; | ||
|
||
import java.time.DateTimeException; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* 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(cast(t as varchar), 1, 10) >= '2023-03-27' -> `t` >= '2023-03-27' | ||
* substring(cast(t as varchar), 1, 10) >= '2023-03-27' -> `t` >= '2023-03-27' | ||
* replace(substring(cast(t as varchar), 1, 10), "-", "") >= '20230327' -> `t` >= '20230327' | ||
* | ||
* if it 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) | ||
*/ | ||
public class SimplifiedDateColumnPredicateRule extends BottomUpScalarOperatorRewriteRule { | ||
private static final String DATE_PATTERN1 = "%Y%m%d"; | ||
private static final String DATE_PATTERN2 = "%Y-%m-%d"; | ||
private static final Pattern DATE_PATTERN_REG = Pattern.compile("\\d{8}"); | ||
private static final Pattern DATE_PATTERN_REG2 = Pattern.compile("\\d{4}-\\d{2}-\\d{2}"); | ||
|
||
@Override | ||
public ScalarOperator visitBinaryPredicate(BinaryPredicateOperator predicate, | ||
ScalarOperatorRewriteContext context) { | ||
ScalarOperator op = predicate.getChild(0); | ||
if (!(op instanceof CallOperator)) { | ||
return predicate; | ||
} | ||
ScalarOperator right = predicate.getChild(1); | ||
if (!(right instanceof ConstantOperator)) { | ||
return predicate; | ||
} | ||
ScalarOperator columnRef = null; | ||
Extractor extractor = newExtractor((CallOperator) op, (ConstantOperator) right); | ||
if (extractor != null && extractor.check()) { | ||
columnRef = extractor.extractColumn(); | ||
} | ||
if (columnRef == null) { | ||
return predicate; | ||
} | ||
BinaryType binaryType = predicate.getBinaryType(); | ||
if (columnRef.getType().getPrimitiveType() == PrimitiveType.DATE) { | ||
return new BinaryPredicateOperator(binaryType, columnRef, right); | ||
} | ||
// if t is datetime, | ||
if (columnRef.getType().getPrimitiveType() == PrimitiveType.DATETIME) { | ||
// date_format(t, '%Y-%m-%d') >/>=/< '2023-03-27' -> t >/>=/< '2023-03-27' | ||
if (binaryType == BinaryType.GT || binaryType == BinaryType.GE || binaryType == BinaryType.LT) { | ||
return new BinaryPredicateOperator(binaryType, columnRef, right); | ||
} | ||
// date_format(t, '%Y-%m-%d') <= '2023-03-27' -> t < days_add('2023-03-27', 1) | ||
if (binaryType == BinaryType.LE) { | ||
Function daysAddFn = Expr.getBuiltinFunction(FunctionSet.DAYS_ADD, | ||
new Type[] {Type.DATETIME, Type.INT}, Function.CompareMode.IS_IDENTICAL); | ||
return new BinaryPredicateOperator(BinaryType.LT, columnRef, new CallOperator( | ||
FunctionSet.DAYS_ADD, Type.DATETIME, | ||
ImmutableList.of(right, ConstantOperator.createInt(1)), daysAddFn)); | ||
} | ||
} | ||
return predicate; | ||
} | ||
|
||
private static boolean isSubstrFn(CallOperator call) { | ||
return FunctionSet.SUBSTR.equalsIgnoreCase(call.getFnName()) | ||
|| FunctionSet.SUBSTRING.equalsIgnoreCase(call.getFnName()); | ||
} | ||
|
||
private Extractor newExtractor(CallOperator call, ConstantOperator value) { | ||
if (FunctionSet.DATE_FORMAT.equalsIgnoreCase(call.getFnName())) { | ||
return new DateFormatExtractor(call, value); | ||
} else if (isSubstrFn(call)) { | ||
return new SubstrExtractor(call, value, DATE_PATTERN2); | ||
} else if (FunctionSet.REPLACE.equalsIgnoreCase(call.getFnName())) { | ||
return new ReplaceAndSubstrExtractor(call, value); | ||
} | ||
return null; | ||
} | ||
|
||
private static boolean isDatePattern(String datePattern, ConstantOperator date) { | ||
if (DATE_PATTERN1.equalsIgnoreCase(datePattern) && DATE_PATTERN_REG.matcher(date.getVarchar()).matches()) { | ||
return verifyDate(datePattern, date); | ||
} | ||
if (DATE_PATTERN2.equalsIgnoreCase(datePattern) && DATE_PATTERN_REG2.matcher(date.getVarchar()).matches()) { | ||
return verifyDate(datePattern, date); | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean verifyDate(String datePattern, ConstantOperator date) { | ||
try { | ||
ScalarOperatorFunctions.str2Date(date, ConstantOperator.createVarchar(datePattern)); | ||
} catch (DateTimeException ignore) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private interface Extractor { | ||
ScalarOperator extractColumn(); | ||
|
||
boolean check(); | ||
} | ||
|
||
/** | ||
* date_format(t, '%Y%m%d') -> t | ||
*/ | ||
private static class DateFormatExtractor implements Extractor { | ||
private final CallOperator call; | ||
private final ConstantOperator value; | ||
|
||
public DateFormatExtractor(CallOperator call, ConstantOperator value) { | ||
this.call = call; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public boolean check() { | ||
if (!call.getChild(1).isConstantRef()) { | ||
return false; | ||
} | ||
ScalarOperator dateColumn = call.getChild(0); | ||
return dateColumn.getType().getPrimitiveType() == PrimitiveType.DATE | ||
|| dateColumn.getType().getPrimitiveType() == PrimitiveType.DATETIME; | ||
} | ||
|
||
@Override | ||
public ScalarOperator extractColumn() { | ||
ScalarOperator dateColumn = call.getChild(0); | ||
String pattern = ((ConstantOperator) call.getChild(1)).getVarchar(); | ||
if (isDatePattern(pattern, value)) { | ||
return dateColumn; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* substr(cast(t as varchar), 1, 10) -> t | ||
*/ | ||
private static class SubstrExtractor implements Extractor { | ||
private final CallOperator call; | ||
private final ConstantOperator value; | ||
private final String datePattern; | ||
|
||
public SubstrExtractor(CallOperator call, ConstantOperator value, String datePattern) { | ||
this.call = call; | ||
this.value = value; | ||
this.datePattern = datePattern; | ||
} | ||
|
||
@Override | ||
public boolean check() { | ||
if (!(call.getChild(1).isConstantRef() && ((ConstantOperator) call.getChild(1)).getInt() == 1) | ||
|| !(call.getChild(2).isConstantRef() && ((ConstantOperator) call.getChild(2)).getInt() == 10)) { | ||
return false; | ||
} | ||
if (!(call.getChild(0) instanceof CastOperator)) { | ||
return false; | ||
} | ||
CastOperator op = call.getChild(0).cast(); | ||
return op.getChild(0).getType().getPrimitiveType() == PrimitiveType.DATE | ||
|| op.getChild(0).getType().getPrimitiveType() == PrimitiveType.DATETIME; | ||
} | ||
|
||
@Override | ||
public ScalarOperator extractColumn() { | ||
CastOperator castOperator = call.getChild(0).cast(); | ||
ScalarOperator dateColumn = castOperator.getChild(0); | ||
if (isDatePattern(datePattern, value)) { | ||
return dateColumn; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* replace(substring(cast(t as varchar), 1, 10), "-", "") -> t | ||
*/ | ||
private static class ReplaceAndSubstrExtractor implements Extractor { | ||
private final CallOperator call; | ||
private final ConstantOperator value; | ||
|
||
public ReplaceAndSubstrExtractor(CallOperator call, ConstantOperator value) { | ||
this.call = call; | ||
this.value = value; | ||
} | ||
|
||
@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()) { | ||
return false; | ||
} | ||
// yyyyMMdd | ||
if (value.getVarchar().length() != 8) { | ||
return false; | ||
} | ||
return new SubstrExtractor((CallOperator) call.getChild(0), value, DATE_PATTERN1).check(); | ||
} | ||
|
||
@Override | ||
public ScalarOperator extractColumn() { | ||
return new SubstrExtractor((CallOperator) call.getChild(0), value, DATE_PATTERN1).extractColumn(); | ||
} | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
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.