Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ public Expression visit(Expression expr, ExpressionRewriteContext ctx) {
// TODO: add other expression visitor function to do type coercion if necessary.

@Override
public Expression visitBinaryOperator(BinaryOperator binaryOperator, ExpressionRewriteContext context) {
Expression left = rewrite(binaryOperator.left(), context);
Expression right = rewrite(binaryOperator.right(), context);
public Expression visitBinaryOperator(BinaryOperator op, ExpressionRewriteContext context) {
Expression left = rewrite(op.left(), context);
Expression right = rewrite(op.right(), context);

return Optional.of(TypeCoercionUtils.canHandleTypeCoercion(left.getDataType(), right.getDataType()))
.filter(Boolean::booleanValue)
.map(b -> TypeCoercionUtils.findTightestCommonType(left.getDataType(), right.getDataType()))
.map(b -> TypeCoercionUtils.findTightestCommonType(op, left.getDataType(), right.getDataType()))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(ct -> binaryOperator.inputType().acceptsType(ct))
.filter(ct -> op.inputType().acceptsType(ct))
.filter(ct -> !left.getDataType().equals(ct) || !right.getDataType().equals(ct))
.map(commonType -> {
Expression newLeft = TypeCoercionUtils.castIfNotSameType(left, commonType);
Expression newRight = TypeCoercionUtils.castIfNotSameType(right, commonType);
return binaryOperator.withChildren(newLeft, newRight);
return op.withChildren(newLeft, newRight);
})
.orElse(binaryOperator.withChildren(left, right));
.orElse(op.withChildren(left, right));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public DataType getDataType() throws UnboundException {
try {
return TypeCoercionUtils.findCommonNumericsType(left().getDataType(), right().getDataType());
} catch (Exception e) {
return TypeCoercionUtils.findTightestCommonType(left().getDataType(), right().getDataType())
return TypeCoercionUtils.findTightestCommonType(this,
left().getDataType(), right().getDataType())
.orElseGet(() -> left().getDataType());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private List<List<Expression>> castCommonDataTypeOutputs(List<Slot> resetNullabl
Slot right = child(1).getOutput().get(i);
if (TypeCoercionUtils.canHandleTypeCoercion(left.getDataType(), right.getDataType())) {
Optional<DataType> tightestCommonType =
TypeCoercionUtils.findTightestCommonType(left.getDataType(), right.getDataType());
TypeCoercionUtils.findTightestCommonType(null, left.getDataType(), right.getDataType());
if (tightestCommonType.isPresent()) {
Expression newLeft = TypeCoercionUtils.castIfNotSameType(left, tightestCommonType.get());
Expression newRight = TypeCoercionUtils.castIfNotSameType(right, tightestCommonType.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.apache.doris.nereids.annotation.Developing;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.trees.expressions.BinaryArithmetic;
import org.apache.doris.nereids.trees.expressions.BinaryOperator;
import org.apache.doris.nereids.trees.expressions.Cast;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
Expand Down Expand Up @@ -186,7 +188,8 @@ public static boolean hasCharacterType(DataType dataType) {
* find the tightest common type for two type
*/
@Developing
public static Optional<DataType> findTightestCommonType(DataType left, DataType right) {
public static Optional<DataType> findTightestCommonType(BinaryOperator binaryOperator,
DataType left, DataType right) {
// TODO: compatible with origin planner and BE
// TODO: when add new type, add it to here
DataType tightestCommonType = null;
Expand Down Expand Up @@ -234,9 +237,12 @@ public static Optional<DataType> findTightestCommonType(DataType left, DataType
} else if (left instanceof DateV2Type || right instanceof DateV2Type) {
tightestCommonType = DateV2Type.INSTANCE;
}
} else if ((left instanceof DateLikeType && right instanceof IntegralType)
|| (right instanceof DateLikeType && left instanceof IntegralType)) {
tightestCommonType = BigIntType.INSTANCE;
} else if (canCompareDate(left, right)) {
if (binaryOperator instanceof BinaryArithmetic) {
tightestCommonType = IntegerType.INSTANCE;
} else {
tightestCommonType = DateTimeType.INSTANCE;
}
}
return tightestCommonType == null ? Optional.of(DoubleType.INSTANCE) : Optional.of(tightestCommonType);
}
Expand Down Expand Up @@ -302,7 +308,7 @@ public static Optional<DataType> findWiderTypeForTwo(DataType left, DataType rig
// TODO: need to rethink how to handle char and varchar to return char or varchar as much as possible.
return Stream
.<Supplier<Optional<DataType>>>of(
() -> findTightestCommonType(left, right),
() -> findTightestCommonType(null, left, right),
() -> findWiderTypeForDecimal(left, right),
() -> characterPromotion(left, right),
() -> findTypeForComplex(left, right))
Expand Down Expand Up @@ -381,4 +387,17 @@ public static Expression castIfNotSameType(Expression input, DataType dataType)
return new Cast(input, dataType);
}
}

private static boolean canCompareDate(DataType left, DataType right) {
if (left instanceof DateLikeType) {
return right.isDateType() || right.isStringType() || right instanceof TinyIntType
|| right instanceof SmallIntType || right instanceof IntegerType
|| right instanceof BigIntType;
} else if (right instanceof DateLikeType) {
return left.isStringType() || left instanceof TinyIntType
|| left instanceof SmallIntType || left instanceof IntegerType
|| left instanceof BigIntType;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ public List<RuntimeFilter> getRuntimeFilters() {
return analyzer.getAssignedRuntimeFilter();
}

/**
*/
private void setResultExprScale(Analyzer analyzer, ArrayList<Expr> outputExprs) {
for (TupleDescriptor tupleDesc : analyzer.getDescTbl().getTupleDescs()) {
for (SlotDescriptor slotDesc : tupleDesc.getSlots()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public void testFindTightestCommonType() {
}

private void testFindTightestCommonType(DataType commonType, DataType left, DataType right) {
Assertions.assertEquals(Optional.ofNullable(commonType), TypeCoercionUtils.findTightestCommonType(left, right));
Assertions.assertEquals(Optional.ofNullable(commonType), TypeCoercionUtils.findTightestCommonType(null,
left, right));
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions regression-test/data/nereids_syntax_p0/test_convert.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql --
2

50 changes: 50 additions & 0 deletions regression-test/suites/nereids_syntax_p0/test_convert.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://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.

suite("test_implicit_convert") {

sql """
DROP TABLE IF EXISTS part_by_date
"""
sql """
CREATE TABLE `part_by_date`
(
`date` date NOT NULL COMMENT '',
`id` int(11) NOT NULL COMMENT ''
) ENGINE=OLAP
UNIQUE KEY(`date`, `id`)
PARTITION BY RANGE(`date`)
(PARTITION p201912 VALUES [('0000-01-01'), ('2020-01-01')),
PARTITION p202001 VALUES [('2020-01-01'), ('2020-02-01')))
DISTRIBUTED BY HASH(`id`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1"
);
"""

sql """
INSERT INTO part_by_date VALUES('0001-02-01', 1),('2020-01-15', 2);
"""

qt_sql """
SELECT
id
FROM
part_by_date
WHERE date > 20200101;
"""
}