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

[Kernel][Expressions] Add IS NOT DISTINCT FROM support #2830

Closed
wants to merge 4 commits into from
Closed
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 @@ -38,6 +38,7 @@
import static io.delta.kernel.defaults.internal.expressions.DefaultExpressionUtils.booleanWrapperVector;
import static io.delta.kernel.defaults.internal.expressions.DefaultExpressionUtils.childAt;
import static io.delta.kernel.defaults.internal.expressions.DefaultExpressionUtils.compare;
import static io.delta.kernel.defaults.internal.expressions.DefaultExpressionUtils.compareNullSafe;
import static io.delta.kernel.defaults.internal.expressions.DefaultExpressionUtils.evalNullability;
import static io.delta.kernel.defaults.internal.expressions.ImplicitCastExpression.canCastTo;

Expand Down Expand Up @@ -156,6 +157,19 @@ ExpressionTransformResult visitComparator(Predicate predicate) {
}
}

@Override
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be combined into the visitComparator above? That way we can avoid an additional method on this visitor.

Copy link
Contributor Author

@zzl-7 zzl-7 Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #3230

ExpressionTransformResult visitNullSafeComparator(Predicate predicate) {
switch (predicate.getName()) {
case "<=>":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the new operator to the Java doc of Predicate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if we should just use IS NOT DISTINCT FROM which seems like is in the ANSI SQL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vkorukanti isIS NOT DISTINCT FROM currently supported? I can't seem to find it in the source code

return new ExpressionTransformResult(
transformBinaryComparator(predicate),
BooleanType.BOOLEAN);
default:
throw DeltaErrors.unsupportedExpression(
predicate, Optional.of("unsupported expression encountered"));
}
}

@Override
ExpressionTransformResult visitLiteral(Literal literal) {
// nothing to validate or rewrite
Expand Down Expand Up @@ -445,6 +459,27 @@ ColumnVector visitComparator(Predicate predicate) {
return new DefaultBooleanVector(numRows, Optional.of(nullability), result);
}

@Override
ColumnVector visitNullSafeComparator(Predicate predicate) {
PredicateChildrenEvalResult argResults = evalBinaryExpressionChildren(predicate);
int numRows = argResults.rowCount;
boolean[] result = new boolean[numRows];
int[] compareResult = compareNullSafe(argResults.leftResult, argResults.rightResult);
Comment on lines +465 to +467
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to get away from this model to lazy compute model (#2541)

this can be rewritten as

// Create a comparator
// Basically this comparator is based on the data type.
Comparator comparator = ...

return new ColumnVector() {
   public getDataType() { return BooleanType.BOOLEAN;}

   public getSize() { argResults.leftResult.getSize(); }

   public isNullAt(int rowId) {
       boolean isLeftNull = argResults.leftResult.isNullAt(rowId);
       boolean isRightNull = argResults.rightResult.isNullAt(rowId);
       return (isLeftNull && !isRightNull) || (!isLeftNull && isRightNull);
   }

  public getBoolean(int rowId) {
      // check both are null. if yes return true.
      compartor.compare(leftResult, rightResult, rowId)
   }

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused on

   public isNullAt(int rowId) {
       boolean isLeftNull = argResults.leftResult.isNullAt(rowId);
       boolean isRightNull = argResults.rightResult.isNullAt(rowId);
       return (isLeftNull && !isRightNull) || (!isLeftNull && isRightNull);
   }

Shouldn't isNullAt always return false based on https://spark.apache.org/docs/latest/sql-ref-null-semantics.html#comparison-operators-
because <=> can never return null?

switch (predicate.getName()) {
case "<=>":
for (int rowId = 0; rowId < numRows; rowId++) {
result[rowId] = compareResult[rowId] == 0;
}
break;
default:
throw DeltaErrors.unsupportedExpression(
predicate,
Optional.of("unsupported expression encountered"));
}

return new DefaultBooleanVector(numRows, Optional.empty(), result);
}

@Override
ColumnVector visitLiteral(Literal literal) {
DataType dataType = literal.getDataType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
* Utility methods used by the default expression evaluator.
*/
class DefaultExpressionUtils {

static final Comparator<byte[]> binaryComparator = (leftOp, rightOp) -> {
int i = 0;
while (i < leftOp.length && i < rightOp.length) {
if (leftOp[i] != rightOp[i]) {
return Byte.compare(leftOp[i], rightOp[i]);
}
i++;
}
return Integer.compare(leftOp.length, rightOp.length);
};

static final Comparator<BigDecimal> bigDecimalComparator = Comparator.naturalOrder();
static final Comparator<String> stringComparator = Comparator.naturalOrder();
private DefaultExpressionUtils() {}

/**
Expand Down Expand Up @@ -127,6 +141,53 @@ static int[] compare(ColumnVector left, ColumnVector right) {
return result;
}

static int[] compareNullSafe(ColumnVector left, ColumnVector right) {
checkArgument(
left.getSize() == right.getSize(),
"Left and right operand have different vector sizes.");
DataType dataType = left.getDataType();
int numRows = left.getSize();
int[] result = new int[numRows];
for (int rowId = 0; rowId < left.getSize(); rowId++) {
if (left.isNullAt(rowId) && right.isNullAt(rowId)) {
result[rowId] = 0;
} else if (left.isNullAt(rowId) || right.isNullAt(rowId)) {
result[rowId] = 1;
} else {
if (dataType instanceof BooleanType) {
result[rowId] =
Boolean.compare(left.getBoolean(rowId), right.getBoolean(rowId));
} else if (dataType instanceof ByteType) {
result[rowId] = Byte.compare(left.getByte(rowId), right.getByte(rowId));
} else if (dataType instanceof ShortType) {
result[rowId] = Short.compare(left.getShort(rowId), right.getShort(rowId));
} else if (dataType instanceof IntegerType || dataType instanceof DateType) {
result[rowId] = Integer.compare(left.getInt(rowId), right.getInt(rowId));
} else if (dataType instanceof LongType || dataType instanceof TimestampType) {
result[rowId] = Long.compare(left.getLong(rowId), right.getLong(rowId));
} else if (dataType instanceof FloatType) {
result[rowId] = Float.compare(left.getFloat(rowId), right.getFloat(rowId));
} else if (dataType instanceof DoubleType) {
result[rowId] = Double.compare(
left.getDouble(rowId), right.getDouble(rowId));
} else if (dataType instanceof DecimalType) {
result[rowId] = bigDecimalComparator.compare(
left.getDecimal(rowId), right.getDecimal(rowId));
} else if (dataType instanceof StringType) {
result[rowId] = stringComparator.compare(
left.getString(rowId), right.getString(rowId));
} else if (dataType instanceof BinaryType) {
result[rowId] = binaryComparator.compare(
left.getBinary(rowId), right.getBinary(rowId));
} else {
throw new UnsupportedOperationException(dataType + " can not be compared.");
}
}
}
return result;
}


static void compareBoolean(ColumnVector left, ColumnVector right, int[] result) {
for (int rowId = 0; rowId < left.getSize(); rowId++) {
if (!left.isNullAt(rowId) && !right.isNullAt(rowId)) {
Expand Down Expand Up @@ -202,19 +263,10 @@ static void compareDecimal(ColumnVector left, ColumnVector right, int[] result)
}

static void compareBinary(ColumnVector left, ColumnVector right, int[] result) {
Comparator<byte[]> comparator = (leftOp, rightOp) -> {
int i = 0;
while (i < leftOp.length && i < rightOp.length) {
if (leftOp[i] != rightOp[i]) {
return Byte.compare(leftOp[i], rightOp[i]);
}
i++;
}
return Integer.compare(leftOp.length, rightOp.length);
};
for (int rowId = 0; rowId < left.getSize(); rowId++) {
if (!left.isNullAt(rowId) && !right.isNullAt(rowId)) {
result[rowId] = comparator.compare(left.getBinary(rowId), right.getBinary(rowId));
result[rowId] = binaryComparator.compare(
left.getBinary(rowId), right.getBinary(rowId));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class ExpressionVisitor<R> {

abstract R visitComparator(Predicate predicate);

abstract R visitNullSafeComparator(Predicate predicate);

abstract R visitLiteral(Literal literal);

abstract R visitColumn(Column column);
Expand Down Expand Up @@ -95,6 +97,8 @@ private R visitScalarExpression(ScalarExpression expression) {
case ">":
case ">=":
return visitComparator(new Predicate(name, children));
case "<=>":
return visitNullSafeComparator(new Predicate(name, children));
case "ELEMENT_AT":
return visitElementAt(expression);
case "NOT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class DefaultExpressionEvaluatorSuite extends AnyFunSuite with ExpressionSuiteBa
"Coalesce is only supported for boolean type expressions")
}

test("evaluate expression: comparators (=, <, <=, >, >=)") {
test("evaluate expression: comparators (=, <, <=, >, >=, <=>)") {
// Literals for each data type from the data type value range, used as inputs to comparator
// (small, big, small, null)
val literals = Seq(
Expand Down Expand Up @@ -350,7 +350,8 @@ class DefaultExpressionEvaluatorSuite extends AnyFunSuite with ExpressionSuiteBa
"<=" -> Seq(true, false, true, null, null, null),
">" -> Seq(false, true, false, null, null, null),
">=" -> Seq(false, true, true, null, null, null),
"=" -> Seq(false, false, true, null, null, null)
"=" -> Seq(false, false, true, null, null, null),
"<=>" -> Seq(false, false, true, false, false, true)
)

literals.foreach {
Expand Down
Loading