Skip to content

Commit

Permalink
[Fix](Planner) change non boolean return type to boolean (apache#20599)
Browse files Browse the repository at this point in the history
Problem: When using no boolean type as return type in where or having clause, the analyzer will check the return type and throw an error. But in some other databases, this usage is enable.

Solved: Cast return type to boolean in where clause and having clause. select *** from *** where case when *** then 1 else 0 end;
  • Loading branch information
LiBinfeng-01 authored Jul 7, 2023
1 parent 0b7b5dc commit dc44345
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.doris.catalog.DatabaseIf;
import org.apache.doris.catalog.FunctionSet;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.TableIf.TableType;
Expand Down Expand Up @@ -601,8 +602,8 @@ public void analyze(Analyzer analyzer) throws UserException {
}
}

whereClauseRewrite();
if (whereClause != null) {
whereClauseRewrite();
if (checkGroupingFn(whereClause)) {
throw new AnalysisException("grouping operations are not allowed in WHERE.");
}
Expand Down Expand Up @@ -851,6 +852,9 @@ private void whereClauseRewrite() {
} else {
whereClause = new BoolLiteral(true);
}
} else if (!whereClause.getType().isBoolean()) {
whereClause = new CastExpr(TypeDef.create(PrimitiveType.BOOLEAN), whereClause);
whereClause.setType(Type.BOOLEAN);
}
}

Expand Down Expand Up @@ -1263,6 +1267,9 @@ private void analyzeAggregation(Analyzer analyzer) throws AnalysisException {
havingClauseAfterAnalyzed = havingClause.substitute(aliasSMap, analyzer, false);
}
havingClauseAfterAnalyzed = rewriteQueryExprByMvColumnExpr(havingClauseAfterAnalyzed, analyzer);
if (!havingClauseAfterAnalyzed.getType().isBoolean()) {
havingClauseAfterAnalyzed = havingClauseAfterAnalyzed.castTo(Type.BOOLEAN);
}
havingClauseAfterAnalyzed.checkReturnsBool("HAVING clause", true);
if (groupingInfo != null) {
groupingInfo.substituteGroupingFn(Arrays.asList(havingClauseAfterAnalyzed), analyzer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.TableIf;
import org.apache.doris.catalog.Type;
import org.apache.doris.catalog.external.HMSExternalTable;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
Expand Down Expand Up @@ -651,6 +652,9 @@ public void analyzeJoin(Analyzer analyzer) throws AnalysisException {
analyzer.setVisibleSemiJoinedTuple(semiJoinedTupleId);
onClause.analyze(analyzer);
analyzer.setVisibleSemiJoinedTuple(null);
if (!onClause.getType().isBoolean()) {
onClause = onClause.castTo(Type.BOOLEAN);
}
onClause.checkReturnsBool("ON clause", true);
if (onClause.contains(Expr.isAggregatePredicate())) {
throw new AnalysisException(
Expand Down
58 changes: 57 additions & 1 deletion regression-test/suites/query_p0/cast/test_cast.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,60 @@ suite('test_cast') {
sql "select cast(${datetime} as int), cast(${datetime} as bigint), cast(${datetime} as float), cast(${datetime} as double)"
result([[869930357, 20200101123445l, ((float) 20200101123445l), ((double) 20200101123445l)]])
}
}

def tbl = "test_cast"

sql """ DROP TABLE IF EXISTS ${tbl}"""
sql """
CREATE TABLE IF NOT EXISTS ${tbl} (
`k0` int
)
DISTRIBUTED BY HASH(`k0`) BUCKETS 5 properties("replication_num" = "1")
"""
sql """ INSERT INTO ${tbl} VALUES (101);"""

test {
sql "select * from ${tbl} where case when k0 = 101 then 1 else 0 end"
result([[101]])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then 12 else 0 end"
result([[101]])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then -12 else 0 end"
result([[101]])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then 0 else 1 end"
result([])
}

test {
sql "select * from ${tbl} where case when k0 != 101 then 0 else 1 end"
result([[101]])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then '1' else 0 end"
result([[101]])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then '12' else 0 end"
result([])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then 'false' else 0 end"
result([])
}

test {
sql "select * from ${tbl} where case when k0 = 101 then 'true' else 1 end"
result([[101]])
}
}

0 comments on commit dc44345

Please sign in to comment.