Skip to content

Commit

Permalink
[core] Add toString for Predicate (apache#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zouxxyy authored Dec 19, 2023
1 parent e0f7548 commit 1308150
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public int hashCode() {
return Objects.hash(function, children);
}

@Override
public String toString() {
return function + "(" + children + ")";
}

/** Evaluate the predicate result based on multiple {@link Predicate}s. */
public abstract static class Function implements Serializable {

Expand All @@ -113,5 +118,10 @@ public boolean equals(Object o) {
}
return o != null && getClass() == o.getClass();
}

@Override
public String toString() {
return getClass().getSimpleName();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public boolean equals(Object o) {

public abstract <T> T visit(
FunctionVisitor<T> visitor, FieldRef fieldRef, List<Object> literals);

@Override
public String toString() {
return getClass().getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ public int hashCode() {
return Objects.hash(function, type, fieldIndex, fieldName, literals);
}

@Override
public String toString() {
String literalsStr;
if (literals == null || literals.isEmpty()) {
literalsStr = "";
} else if (literals.size() == 1) {
literalsStr = Objects.toString(literals.get(0));
} else {
literalsStr = literals.toString();
}
return literalsStr.isEmpty()
? function + "(" + fieldName + ")"
: function + "(" + fieldName + ", " + literalsStr + ")";
}

private ListSerializer<Object> objectsSerializer() {
return new ListSerializer<>(
NullableSerializer.wrapIfNullIsNotSupported(InternalSerializers.create(type)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,55 @@ public void testUnknownStats() {
assertThat(predicate.test(3, new FieldStats[] {new FieldStats(null, null, 4L)}))
.isEqualTo(true);
}

@Test
public void testPredicateToString() {
PredicateBuilder builder1 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p1 = builder1.equal(0, 5);
assertThat(p1.toString()).isEqualTo("Equal(f0, 5)");

PredicateBuilder builder2 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p2 = builder2.greaterThan(0, 5);
assertThat(p2.toString()).isEqualTo("GreaterThan(f0, 5)");

PredicateBuilder builder3 = new PredicateBuilder(RowType.of(new IntType(), new IntType()));
Predicate p3 = PredicateBuilder.and(builder3.equal(0, 3), builder3.equal(1, 5));
assertThat(p3.toString()).isEqualTo("And([Equal(f0, 3), Equal(f1, 5)])");

PredicateBuilder builder4 = new PredicateBuilder(RowType.of(new IntType(), new IntType()));
Predicate p4 = PredicateBuilder.or(builder4.equal(0, 3), builder4.equal(1, 5));
assertThat(p4.toString()).isEqualTo("Or([Equal(f0, 3), Equal(f1, 5)])");

PredicateBuilder builder5 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p5 = builder5.isNotNull(0);
assertThat(p5.toString()).isEqualTo("IsNotNull(f0)");

PredicateBuilder builder6 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p6 = builder6.in(0, Arrays.asList(1, null, 3, 4));
assertThat(p6.toString())
.isEqualTo(
"Or([Or([Or([Equal(f0, 1), Equal(f0, null)]), Equal(f0, 3)]), Equal(f0, 4)])");

PredicateBuilder builder7 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p7 = builder7.notIn(0, Arrays.asList(1, null, 3, 4));
assertThat(p7.toString())
.isEqualTo(
"And([And([And([NotEqual(f0, 1), NotEqual(f0, null)]), NotEqual(f0, 3)]), NotEqual(f0, 4)])");

PredicateBuilder builder8 = new PredicateBuilder(RowType.of(new IntType()));
List<Object> literals = new ArrayList<>();
for (int i = 1; i <= 21; i++) {
literals.add(i);
}
Predicate p8 = builder8.in(0, literals);
assertThat(p8.toString())
.isEqualTo(
"In(f0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])");

PredicateBuilder builder9 = new PredicateBuilder(RowType.of(new IntType()));
Predicate p9 = builder9.notIn(0, literals);
assertThat(p9.toString())
.isEqualTo(
"NotIn(f0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ import org.apache.spark.sql.types.StructType
case class PaimonScan(
table: Table,
requiredSchema: StructType,
filters: Array[(Filter, Predicate)],
filters: Array[Predicate],
pushDownLimit: Option[Int])
extends PaimonBaseScan(table, requiredSchema, filters, pushDownLimit)
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import scala.collection.JavaConverters._
abstract class PaimonBaseScan(
table: Table,
requiredSchema: StructType,
filters: Array[(Filter, Predicate)],
filters: Array[Predicate],
pushDownLimit: Option[Int])
extends Scan
with SupportsReportStatistics
Expand All @@ -56,7 +56,7 @@ abstract class PaimonBaseScan(
val projection = readSchema().fieldNames.map(field => tableRowType.getFieldNames.indexOf(field))
_readBuilder.withProjection(projection)
if (filters.nonEmpty) {
val pushedPredicate = PredicateBuilder.and(filters.map(_._2): _*)
val pushedPredicate = PredicateBuilder.and(filters: _*)
_readBuilder.withFilter(pushedPredicate)
}
pushDownLimit.foreach(_readBuilder.withLimit)
Expand Down Expand Up @@ -108,7 +108,7 @@ abstract class PaimonBaseScan(

override def description(): String = {
val pushedFiltersStr = if (filters.nonEmpty) {
", PushedFilters: [" + filters.map(_._1).mkString(",") + "]"
", PushedFilters: [" + filters.mkString(",") + "]"
} else {
""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.apache.paimon.spark

import org.apache.paimon.predicate.{PartitionPredicateVisitor, Predicate, PredicateBuilder}
import org.apache.paimon.predicate.{PartitionPredicateVisitor, Predicate}
import org.apache.paimon.table.Table

import org.apache.spark.internal.Logging
Expand All @@ -40,7 +40,7 @@ abstract class PaimonBaseScanBuilder(table: Table)
protected var pushDownLimit: Option[Int] = None

override def build(): Scan = {
PaimonScan(table, requiredSchema, pushed, pushDownLimit)
PaimonScan(table, requiredSchema, pushed.map(_._2), pushDownLimit)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import scala.collection.JavaConverters._
case class PaimonScan(
table: Table,
requiredSchema: StructType,
filters: Array[(Filter, Predicate)],
filters: Array[Predicate],
pushDownLimit: Option[Int])
extends PaimonBaseScan(table, requiredSchema, filters, pushDownLimit)
with SupportsRuntimeFiltering {
Expand Down

0 comments on commit 1308150

Please sign in to comment.