Skip to content
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] Convert the constant value to target type while making kudu predicate. #53936

Merged
merged 1 commit into from
Jan 14, 2025
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 @@ -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();
}
Jcnessss marked this conversation as resolved.
Show resolved Hide resolved
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
Loading