Skip to content

Commit

Permalink
[Enhancement] Convert the constant value to target type while making …
Browse files Browse the repository at this point in the history
…kudu predicate. (StarRocks#53936)

Signed-off-by: Song Jiacheng <[email protected]>
  • Loading branch information
Jcnessss authored Jan 14, 2025
1 parent e9f711c commit 0a443f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import com.google.common.collect.Lists;
import com.starrocks.analysis.BinaryType;
import com.starrocks.catalog.Type;
import com.starrocks.connector.ColumnTypeConverter;
import com.starrocks.sql.optimizer.operator.scalar.BinaryPredicateOperator;
import com.starrocks.sql.optimizer.operator.scalar.CastOperator;
import com.starrocks.sql.optimizer.operator.scalar.ColumnRefOperator;
Expand Down Expand Up @@ -103,7 +105,9 @@ public List<KuduPredicate> visitBinaryPredicate(BinaryPredicateOperator operator
if (columnName == null) {
return Lists.newArrayList();
}
Object literal = getLiteral(operator.getChild(1));
ColumnSchema column = schema.getColumn(columnName);
Type targetType = ColumnTypeConverter.fromKuduType(column);
Object literal = getLiteral(operator.getChild(1), targetType);
if (literal == null) {
return Lists.newArrayList();
}
Expand Down Expand Up @@ -137,10 +141,12 @@ public List<KuduPredicate> visitInPredicate(InPredicateOperator operator, Void c
if (columnName == null) {
return Lists.newArrayList();
}
ColumnSchema column = schema.getColumn(columnName);
Type targetType = ColumnTypeConverter.fromKuduType(column);
List<ScalarOperator> valuesOperatorList = operator.getListChildren();
List<Object> literalValues = new ArrayList<>(valuesOperatorList.size());
for (ScalarOperator valueOperator : valuesOperatorList) {
Object literal = getLiteral(valueOperator);
Object literal = getLiteral(valueOperator, targetType);
if (literal == null) {
return Lists.newArrayList();
}
Expand All @@ -154,11 +160,18 @@ public List<KuduPredicate> visitInPredicate(InPredicateOperator operator, Void c
return Lists.newArrayList();
}

private Object getLiteral(ScalarOperator operator) {
private Object getLiteral(ScalarOperator operator, Type targetType) {
if (!(operator instanceof ConstantOperator)) {
return null;
}

ConstantOperator constValue = (ConstantOperator) operator;
Optional<ConstantOperator> casted = constValue.castTo(targetType);
if (!casted.isPresent()) {
// Illegal cast. Do not push down the predicate.
return null;
}
constValue = casted.get();
switch (constValue.getType().getPrimitiveType()) {
case BOOLEAN:
return constValue.getBoolean();
Expand Down Expand Up @@ -202,7 +215,7 @@ public List<KuduPredicate> visitLikePredicateOperator(LikePredicateOperator oper
}

private String getColumnName(ScalarOperator operator) {
if (operator == null || operator instanceof CastOperator) {
if (operator == null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void testEq() {

@Test
public void testEqCast() {
ConstantOperator value = ConstantOperator.createInt(5);
ConstantOperator value = ConstantOperator.createVarchar("5");
ScalarOperator op = new BinaryPredicateOperator(BinaryType.EQ, F0_CAST, value);
List<KuduPredicate> result = CONVERTER.convert(op);
Assert.assertEquals(result.size(), 0);
Assert.assertEquals(result.get(0).toString(), "`f0` = 5");
}

@Test
Expand Down

0 comments on commit 0a443f0

Please sign in to comment.