Skip to content

Commit 45d2ed0

Browse files
authored
API: Simplify sanitization of literals in predicates (#15224)
1 parent d065444 commit 45d2ed0

1 file changed

Lines changed: 5 additions & 46 deletions

File tree

api/src/main/java/org/apache/iceberg/expressions/ExpressionUtil.java

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
3737
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
3838
import org.apache.iceberg.transforms.Transforms;
39-
import org.apache.iceberg.types.Type;
4039
import org.apache.iceberg.types.Types;
4140
import org.apache.iceberg.util.DateTimeUtil;
4241
import org.apache.iceberg.variants.PhysicalType;
@@ -305,15 +304,13 @@ public <T> Expression predicate(BoundPredicate<T> pred) {
305304
} else if (pred.isLiteralPredicate()) {
306305
BoundLiteralPredicate<T> bound = (BoundLiteralPredicate<T>) pred;
307306
return new UnboundPredicate<>(
308-
pred.op(),
309-
unbind(pred.term()),
310-
(T) sanitize(bound.term().type(), bound.literal(), now, today));
307+
pred.op(), unbind(pred.term()), (T) sanitize(bound.literal(), now, today));
311308
} else if (pred.isSetPredicate()) {
312309
BoundSetPredicate<T> bound = (BoundSetPredicate<T>) pred;
313310
Iterable<T> iter =
314311
() ->
315312
bound.literalSet().stream()
316-
.map(lit -> (T) sanitize(bound.term().type(), lit, now, today))
313+
.map(lit -> (T) sanitize((Literal<?>) lit, now, today))
317314
.iterator();
318315
return new UnboundPredicate<>(pred.op(), unbind(pred.term()), iter);
319316
}
@@ -390,7 +387,7 @@ public String or(String leftResult, String rightResult) {
390387
}
391388

392389
private String value(BoundLiteralPredicate<?> pred) {
393-
return sanitize(pred.term().type(), pred.literal().value(), nowMicros, today);
390+
return sanitize(pred.literal(), nowMicros, today);
394391
}
395392

396393
@Override
@@ -422,7 +419,7 @@ public <T> String predicate(BoundPredicate<T> pred) {
422419
+ " IN "
423420
+ abbreviateValues(
424421
pred.asSetPredicate().literalSet().stream()
425-
.map(lit -> sanitize(pred.term().type(), lit, nowMicros, today))
422+
.map(lit -> sanitize((Literal<?>) lit, nowMicros, today))
426423
.collect(Collectors.toList()))
427424
.stream()
428425
.collect(Collectors.joining(", ", "(", ")"));
@@ -431,7 +428,7 @@ public <T> String predicate(BoundPredicate<T> pred) {
431428
+ " NOT IN "
432429
+ abbreviateValues(
433430
pred.asSetPredicate().literalSet().stream()
434-
.map(lit -> sanitize(pred.term().type(), lit, nowMicros, today))
431+
.map(lit -> sanitize((Literal<?>) lit, nowMicros, today))
435432
.collect(Collectors.toList()))
436433
.stream()
437434
.collect(Collectors.joining(", ", "(", ")"));
@@ -518,44 +515,6 @@ private static List<String> abbreviateValues(List<String> sanitizedValues) {
518515
return sanitizedValues;
519516
}
520517

521-
private static String sanitize(Type type, Literal<?> lit, long now, int today) {
522-
return sanitize(type, lit.value(), now, today);
523-
}
524-
525-
private static String sanitize(Type type, Object value, long now, int today) {
526-
switch (type.typeId()) {
527-
case INTEGER:
528-
case LONG:
529-
return sanitizeNumber((Number) value, "int");
530-
case FLOAT:
531-
case DOUBLE:
532-
return sanitizeNumber((Number) value, "float");
533-
case DATE:
534-
return sanitizeDate((int) value, today);
535-
case TIME:
536-
return "(time)";
537-
case TIMESTAMP:
538-
return sanitizeTimestamp((long) value, now);
539-
case TIMESTAMP_NANO:
540-
return sanitizeTimestamp(DateTimeUtil.nanosToMicros((long) value / 1000), now);
541-
case STRING:
542-
return sanitizeString((CharSequence) value, now, today);
543-
case VARIANT:
544-
return sanitizeVariant((Variant) value, now, today);
545-
case UNKNOWN:
546-
return "(unknown)";
547-
case BOOLEAN:
548-
case UUID:
549-
case DECIMAL:
550-
case FIXED:
551-
case BINARY:
552-
// for boolean, uuid, decimal, fixed, unknown, and binary, match the string result
553-
return sanitizeSimpleString(value.toString());
554-
}
555-
throw new UnsupportedOperationException(
556-
String.format("Cannot sanitize value for unsupported type %s: %s", type, value));
557-
}
558-
559518
private static String sanitize(Literal<?> literal, long now, int today) {
560519
if (literal instanceof Literals.StringLiteral) {
561520
return sanitizeString(((Literals.StringLiteral) literal).value(), now, today);

0 commit comments

Comments
 (0)