Skip to content

Commit

Permalink
fix fe ut
Browse files Browse the repository at this point in the history
  • Loading branch information
yujun777 committed Nov 28, 2024
1 parent e5163da commit f35ba0b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static Expression processComparisonPredicateDateTimeV2Literal(
// but current fold constant rule can't handle such complex expr with null literal
// before supporting complex conjuncts with null literal folding rules,
// we use a trick way like this:
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
} else if (comparisonPredicate instanceof NullSafeEqual) {
long originValue = right.getMicroSecond();
Expand Down Expand Up @@ -245,7 +245,7 @@ private static Expression processDecimalV3TypeCoercion(ComparisonPredicate compa
// but current fold constant rule can't handle such complex expr with null literal
// before supporting complex conjuncts with null literal folding rules,
// we use a trick way like this:
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
} else if (comparisonPredicate instanceof NullSafeEqual) {
try {
Expand Down Expand Up @@ -287,7 +287,7 @@ private static Expression processIntegerDecimalLiteralComparison(
// but current fold constant rule can't handle such complex expr with null literal
// before supporting complex conjuncts with null literal folding rules,
// we use a trick way like this:
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
} else if (comparisonPredicate instanceof NullSafeEqual) {
return BooleanLiteral.of(false);
} else if (comparisonPredicate instanceof GreaterThan
Expand Down Expand Up @@ -318,7 +318,7 @@ private static Expression processTypeRangeLimitComparison(ComparisonPredicate cp
left = ((Cast) left).child();
}

// cmp float like have lost precision
// cmp float like have lost precision, for example float.max_value + 0.01 still eval to float.max_value
DataType leftType = left.getDataType();
if (!(leftType.isIntegerLikeType() || leftType.isDecimalV3Type())) {
return cp;
Expand All @@ -333,45 +333,45 @@ private static Expression processTypeRangeLimitComparison(ComparisonPredicate cp
int cmpMax = literal.compareTo(minMaxOpt.get().second);
if (cp instanceof EqualTo) {
if (cmpMin < 0 || cmpMax > 0) {
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
} else if (cp instanceof NullSafeEqual) {
if (cmpMin < 0 || cmpMax > 0) {
return BooleanLiteral.of(false);
}
} else if (cp instanceof GreaterThan) {
if (cmpMin < 0) {
return ExpressionUtils.getTrue(left);
return ExpressionUtils.trueOfNull(left);
}
if (cmpMax >= 0) {
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
} else if (cp instanceof GreaterThanEqual) {
if (cmpMin <= 0) {
return ExpressionUtils.getTrue(left);
return ExpressionUtils.trueOfNull(left);
}
if (cmpMax == 0) {
return new EqualTo(cp.left(), cp.right());
}
if (cmpMax > 0) {
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
} else if (cp instanceof LessThan) {
if (cmpMin <= 0) {
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
if (cmpMax > 0) {
return ExpressionUtils.getTrue(left);
return ExpressionUtils.trueOfNull(left);
}
} else if (cp instanceof LessThanEqual) {
if (cmpMin < 0) {
return ExpressionUtils.getFalse(left);
return ExpressionUtils.falseOrNull(left);
}
if (cmpMin == 0) {
return new EqualTo(cp.left(), cp.right());
}
if (cmpMax >= 0) {
return ExpressionUtils.getTrue(left);
return ExpressionUtils.trueOfNull(left);
}
}
return cp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ public static Expression or(Collection<Expression> expressions) {
return combineAsLeftDeepTree(Or.class, expressions);
}

public static Expression getFalse(Expression expression) {
public static Expression falseOrNull(Expression expression) {
if (expression.nullable()) {
return new And(new IsNull(expression), new NullLiteral(BooleanType.INSTANCE));
} else {
return BooleanLiteral.FALSE;
}
}

public static Expression getTrue(Expression expression) {
public static Expression trueOfNull(Expression expression) {
if (expression.nullable()) {
return new Or(new Not(new IsNull(expression)), new NullLiteral(BooleanType.INSTANCE));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -1815,39 +1816,22 @@ public static Optional<Pair<BigDecimal, BigDecimal>> getDataTypeMinMaxValue(Data
} else if (dataType.isLargeIntType()) {
return Optional.of(Pair.of(new BigDecimal(LargeIntType.MIN_VALUE), new BigDecimal(LargeIntType.MAX_VALUE)));
} else if (dataType.isFloatType()) {
//minVal = BigDecimal.valueOf(-Float.MAX_VALUE);
return Optional.of(Pair.of(new BigDecimal(String.valueOf(Float.MIN_VALUE)),
new BigDecimal(String.valueOf(Float.MAX_VALUE))));
return Optional.of(Pair.of(BigDecimal.valueOf(-Float.MAX_VALUE), new BigDecimal(Float.MAX_VALUE)));
} else if (dataType.isDoubleType()) {
//minVal = BigDecimal.valueOf(-Double.MAX_VALUE);
return Optional.of(Pair.of(new BigDecimal(String.valueOf(Double.MIN_VALUE)),
new BigDecimal(String.valueOf(Double.MAX_VALUE))));
} else if (dataType.isDecimalLikeType()) {
int precision = -1;
int scale = -1;
if (dataType instanceof DecimalV2Type) {
DecimalV2Type type = (DecimalV2Type) dataType;
precision = type.getPrecision();
scale = type.getScale();
}
if (dataType instanceof DecimalV3Type) {
DecimalV3Type type = (DecimalV3Type) dataType;
precision = type.getPrecision();
scale = type.getScale();
}
return Optional.of(Pair.of(BigDecimal.valueOf(-Double.MAX_VALUE), new BigDecimal(Double.MAX_VALUE)));
} else if (dataType.isDecimalV3Type()) {
DecimalV3Type type = (DecimalV3Type) dataType;
int precision = type.getPrecision();
int scale = type.getScale();
if (scale >= 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < precision - scale; i++) {
sb.append('9');
}
sb.append(StringUtils.repeat('9', precision - scale));
if (sb.length() == 0) {
sb.append('0');
}
if (scale > 0) {
sb.append('.');
for (int i = 0; i < scale; i++) {
sb.append('9');
}
sb.append(StringUtils.repeat('9', scale));
}
return Optional.of(Pair.of(new BigDecimal("-" + sb.toString()), new BigDecimal(sb.toString())));
}
Expand Down
Loading

0 comments on commit f35ba0b

Please sign in to comment.