Skip to content

Commit

Permalink
[fix](delete) Fix parsing error of delete where date statement (#22690)
Browse files Browse the repository at this point in the history
  • Loading branch information
luozenglin authored and xiaokang committed Aug 11, 2023
1 parent 5fe9aa9 commit 8fdd91b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,7 @@ public Type castAllToCompatibleType(List<Expr> exprs) throws AnalysisException {
for (int i = 1; i < exprs.size(); ++i) {
exprs.get(i).analyze(this);
if (compatibleType.isDateV2() && exprs.get(i) instanceof StringLiteral
&& ((StringLiteral) exprs.get(i)).canConvertToDateV2(compatibleType)) {
&& ((StringLiteral) exprs.get(i)).canConvertToDateType(compatibleType)) {
// If string literal can be converted to dateV2, we use datev2 as the compatible type
// instead of datetimev2.
} else if (exprs.get(i).isConstantImpl()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ private Type getCmpType() throws AnalysisException {
return getChild(1).getType();
} else if (getChild(0).getType().isDateV2()
&& (getChild(1).getType().isStringType() && getChild(1) instanceof StringLiteral)) {
if (((StringLiteral) getChild(1)).canConvertToDateV2(Type.DATEV2)) {
if (((StringLiteral) getChild(1)).canConvertToDateType(Type.DATEV2)) {
return Type.DATEV2;
} else {
return Type.DATETIMEV2;
}
} else if (getChild(1).getType().isDateV2()
&& (getChild(0).getType().isStringType() && getChild(0) instanceof StringLiteral)) {
if (((StringLiteral) getChild(0)).canConvertToDateV2(Type.DATEV2)) {
if (((StringLiteral) getChild(0)).canConvertToDateType(Type.DATEV2)) {
return Type.DATEV2;
} else {
return Type.DATETIMEV2;
Expand All @@ -397,6 +397,12 @@ private Type getCmpType() throws AnalysisException {
} else if (getChild(1).getType().isDatetimeV2()
&& (getChild(0).getType().isStringType() && getChild(0) instanceof StringLiteral)) {
return getChild(1).getType();
} else if (getChild(0).getType().isDate()
&& (getChild(1).getType().isStringType() && getChild(1) instanceof StringLiteral)) {
return ((StringLiteral) getChild(1)).canConvertToDateType(Type.DATE) ? Type.DATE : Type.DATETIME;
} else if (getChild(1).getType().isDate()
&& (getChild(0).getType().isStringType() && getChild(0) instanceof StringLiteral)) {
return ((StringLiteral) getChild(0)).canConvertToDateType(Type.DATE) ? Type.DATE : Type.DATETIME;
} else {
return Type.DATETIME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,14 @@ void analyzePredicate(Expr predicate, Analyzer analyzer) throws AnalysisExceptio
Expr leftExpr = binaryPredicate.getChild(0);
if (!(leftExpr instanceof SlotRef)) {
throw new AnalysisException(
"Left expr of binary predicate should be column name, predicate=" + binaryPredicate.toSql());
"Left expr of binary predicate should be column name, predicate: " + binaryPredicate.toSql()
+ ", left expr type:" + leftExpr.getType());
}
Expr rightExpr = binaryPredicate.getChild(1);
if (!(rightExpr instanceof LiteralExpr)) {
throw new AnalysisException(
"Right expr of binary predicate should be value, predicate=" + binaryPredicate.toSql());
"Right expr of binary predicate should be value, predicate: " + binaryPredicate.toSql()
+ ", right expr type:" + rightExpr.getType());
}
deleteConditions.add(binaryPredicate);
} else if (predicate instanceof CompoundPredicate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ public LiteralExpr convertToDate(Type targetType) throws AnalysisException {
return newLiteral;
}

public boolean canConvertToDateV2(Type targetType) {
public boolean canConvertToDateType(Type targetType) {
try {
Preconditions.checkArgument(targetType.isDateV2());
Preconditions.checkArgument(targetType.isDateType());
new DateLiteral(value, targetType);
return true;
} catch (AnalysisException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.doris.common.Config;
import org.apache.doris.planner.PartitionColumnFilter;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -245,7 +246,8 @@ public long realValue() {
}

private Date getDateValue(LiteralExpr expr) {
value = expr.getLongValue() / 1000000;
Preconditions.checkArgument(expr.getType() == Type.DATE || expr.getType() == Type.DATEV2);
value = expr.getLongValue();
Date dt = null;
try {
dt = Date.from(LocalDate.parse(String.valueOf(value), df8).atStartOfDay().atZone(ZoneId.systemDefault())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,9 @@ public void testSqlCacheKey() {

SqlCache sqlCache = (SqlCache) ca.getCache();
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "SELECT <slot 2> `eventdate` AS `eventdate`, <slot 3> count(`userid`) "
+ "AS `count(``userid``)` FROM `testCluster:testDb`.`appevent` WHERE `eventdate` "
+ ">= '2020-01-12 00:00:00' AND `eventdate` <= '2020-01-14 00:00:00' GROUP BY `eventdate`|");
Assert.assertEquals(cacheKey, "SELECT <slot 2> `eventdate` AS `eventdate`, <slot 3> count(`userid`) AS "
+ "`count(``userid``)` FROM `testCluster:testDb`.`appevent` WHERE `eventdate` >= '2020-01-12' AND "
+ "`eventdate` <= '2020-01-14' GROUP BY `eventdate`|");
}

@Test
Expand Down Expand Up @@ -1121,8 +1121,8 @@ public void testSqlCacheKeyWithSubSelectView() {
String cacheKey = sqlCache.getSqlWithViewStmt();
Assert.assertEquals(cacheKey, "SELECT `origin`.`eventdate` AS `eventdate`, `origin`.`userid` AS "
+ "`userid` FROM (SELECT `view2`.`eventdate` AS `eventdate`, `view2`.`userid` AS `userid` FROM "
+ "`testCluster:testDb`.`view2` view2 WHERE `view2`.`eventdate` >= '2020-01-12 00:00:00' AND `view2`.`eventdate`"
+ " <= '2020-01-14 00:00:00') origin|select eventdate, userid FROM appevent");
+ "`testCluster:testDb`.`view2` view2 WHERE `view2`.`eventdate` >= '2020-01-12' AND `view2`.`eventdate` "
+ "<= '2020-01-14') origin|select eventdate, userid FROM appevent");
}

@Test
Expand Down

0 comments on commit 8fdd91b

Please sign in to comment.