-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(spark): reject order-breaking casts and negative factors in data ... #19475
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,9 @@ | |
| package org.apache.spark.sql | ||
|
|
||
| import org.apache.spark.sql.HoodieSparkTypeUtils.isCastPreservingOrdering | ||
| import org.apache.spark.sql.catalyst.expressions.{Add, Attribute, AttributeReference, AttributeSet, BitwiseOr, Cast, DateAdd, DateDiff, DateFormatClass, DateSub, Divide, Exp, Expm1, Expression, FromUnixTime, FromUTCTimestamp, Log, Log10, Log1p, Log2, Lower, Multiply, PredicateHelper, ShiftLeft, ShiftRight, ToUnixTimestamp, ToUTCTimestamp, Upper} | ||
| import org.apache.spark.sql.catalyst.expressions.{Add, Attribute, AttributeReference, AttributeSet, BitwiseOr, Cast, DateAdd, DateDiff, DateFormatClass, DateSub, Divide, Exp, Expm1, Expression, FromUnixTime, FromUTCTimestamp, Literal, Log, Log10, Log1p, Log2, Lower, Multiply, PredicateHelper, ShiftLeft, ShiftRight, ToUnixTimestamp, ToUTCTimestamp, Upper} | ||
| import org.apache.spark.sql.execution.datasources.DataSourceStrategy | ||
| import org.apache.spark.sql.types.DataType | ||
| import org.apache.spark.sql.types.{DataType, Decimal} | ||
|
|
||
| /** | ||
| * Base implementation of [[HoodieCatalystExpressionUtils]] carrying the method bodies that are | ||
|
|
@@ -82,9 +82,9 @@ abstract class BaseHoodieCatalystExpressionUtils extends HoodieCatalystExpressio | |
| // Binary | ||
| case Add(OrderPreservingTransformation(attrRef), _, _) => Some(attrRef) | ||
| case Add(_, OrderPreservingTransformation(attrRef), _) => Some(attrRef) | ||
| case Multiply(OrderPreservingTransformation(attrRef), _, _) => Some(attrRef) | ||
| case Multiply(_, OrderPreservingTransformation(attrRef), _) => Some(attrRef) | ||
| case Divide(OrderPreservingTransformation(attrRef), _, _) => Some(attrRef) | ||
| case Multiply(OrderPreservingTransformation(attrRef), factor, _) if isPositiveNumericLiteral(factor) => Some(attrRef) | ||
| case Multiply(factor, OrderPreservingTransformation(attrRef), _) if isPositiveNumericLiteral(factor) => Some(attrRef) | ||
| case Divide(OrderPreservingTransformation(attrRef), divisor, _) if isPositiveNumericLiteral(divisor) => Some(attrRef) | ||
| case BitwiseOr(OrderPreservingTransformation(attrRef), _) => Some(attrRef) | ||
| case BitwiseOr(_, OrderPreservingTransformation(attrRef)) => Some(attrRef) | ||
| // Unary | ||
|
|
@@ -110,5 +110,24 @@ abstract class BaseHoodieCatalystExpressionUtils extends HoodieCatalystExpressio | |
| } | ||
| } | ||
| } | ||
|
|
||
| // Multiplying or dividing by a constant preserves ordering only when the constant is a | ||
| // strictly positive numeric literal: negative factors reverse the ordering, zero collapses | ||
| // it (and makes division undefined), and non-literal operands cannot be validated | ||
| // statically (the optimizer folds constant factors to literals before data skipping runs). | ||
| // Typed null literals carry a null value and fail the value match | ||
| private def isPositiveNumericLiteral(expr: Expression): Boolean = expr match { | ||
| case Literal(value, _) => value match { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 nit: the block comment above could be trimmed — the method name already says "positive numeric literal", and the |
||
| case b: Byte => b > 0 | ||
| case s: Short => s > 0 | ||
| case i: Int => i > 0 | ||
| case l: Long => l > 0 | ||
| case f: Float => f > 0 | ||
| case d: Double => d > 0 | ||
| case dec: Decimal => dec.toBigDecimal.signum > 0 | ||
| case _ => false | ||
| } | ||
| case _ => false | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A strictly positive factor is not sufficient to make multiplication order-preserving because integral arithmetic wraps in non-ANSI mode. For a bigint file containing
{51, Long.MaxValue},A * 2L > 100is accepted here and translated using the transformed max. Spark evaluatesLong.MaxValue * 2Las-2, so the file is pruned, although51 * 2L = 102matches the original predicate.Please restrict multiplication to analyzed types/factors for which overflow cannot break monotonicity, or conservatively reject the unsafe integral/decimal cases. This exact file-stats scenario should be added as a regression test.