-
Notifications
You must be signed in to change notification settings - Fork 120
fix: re-enable timestamp column for data skipping based on max values stats #1333
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: main
Are you sure you want to change the base?
Changes from 1 commit
068be65
8f2e4bc
dd67824
0f80b97
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 |
|---|---|---|
|
|
@@ -842,6 +842,26 @@ pub trait DataSkippingPredicateEvaluator { | |
| inverted: bool, | ||
| ) -> Option<Self::Output> { | ||
| let max = self.get_max_stat(col, &val.data_type())?; | ||
|
|
||
| // Delta Lake min/max stats are stored with millisecond precision (truncated, not rounded up), so we can't use direct comparison. | ||
| // For max stats comparison, we adjust timestamp values by subtracting 999 microseconds from the value to ensure | ||
| // that comparisons against max stats are correct. Any rows that pass this filter will be re-evaluated later for exact matches. | ||
| // See: | ||
| // - https://github.com/delta-io/delta-kernel-rs/pull/1003 | ||
| if matches!(val, Scalar::Timestamp(_) | Scalar::TimestampNtz(_)) { | ||
| match ord { | ||
| Ordering::Greater | Ordering::Less => { | ||
| let max_ts_adjusted = timestamp_subtract(val, 999); | ||
| tracing::debug!( | ||
| "Adjusted timestamp value for col {col} for max stat comparison from {val:?} to {max_ts_adjusted:?}" | ||
| ); | ||
| return self.eval_partial_cmp(ord, max, &max_ts_adjusted, inverted); | ||
| } | ||
| // Equality comparison can't be applied as max stats is truncated to milliseconds, so actual microsecond value is unknown. | ||
| Ordering::Equal => return None, | ||
sgrebnov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| self.eval_partial_cmp(ord, max, val, inverted) | ||
| } | ||
|
|
||
|
|
@@ -986,3 +1006,12 @@ impl<T: DataSkippingPredicateEvaluator + ?Sized> KernelPredicateEvaluator for T | |
| self.finish_eval_pred_junction(op, preds, inverted) | ||
| } | ||
| } | ||
|
|
||
| /// Adjust timestamp value by subtracting the given interval in microseconds. | ||
| fn timestamp_subtract(val: &Scalar, interval_micros: i64) -> Scalar { | ||
| match val { | ||
| Scalar::Timestamp(ts) => Scalar::Timestamp(*ts - interval_micros), | ||
| Scalar::TimestampNtz(ts) => Scalar::TimestampNtz(*ts - interval_micros), | ||
| _ => val.clone(), | ||
| } | ||
| } | ||
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.
should this always be subtracting the val?
Am I missing something here? Could you add some extra details/context in comments to serve as a proof?
Uh oh!
There was an error while loading. Please reload this page.
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.
My guess is that we're never doing a comparison
value > maxsince we only ever want to checkvalue <= max=>! (value < max).EDIT: should be
value <= max=>! (value > max).Uh oh!
There was an error while loading. Please reload this page.
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.
@OussamaSaoudi - thank you for the deep dive and review! Yeah, I was thinking about the same when working on the fix - we only do
max > valueor!(max < value), decreasingvalhelps keep more records in both cases. Please let me know if you would like me to be more specific and add match forord=Greater, inverted=falseandord=Less, inverted=trueonlyOther reasoning I used:
(max + 999) operator valthen(max + 999-999) operator val-999is valid as well so we have our current logic(max operator val-999case.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.
Good idea! Let's handle by cases.
max > valueormax >= value. Represented by: (ord=Greater, inverted=false) and (ord=Less, inverted=true)max >= value + 999, it must be the case thatmax >= valuemax < valueormax <= value. (ord=Less, inverted=false) and (ord=Greater, inverted=true)max <= value - 999, then it must be the case thatmax <= valuemax + 999 <= valueto avoid underflowThere 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.
CC @scovich Would love your eyes on this.
Uh oh!
There was an error while loading. Please reload this page.
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.
AFAIK, we only have four rewrites for inequalities:
col < val=>stats.min.col < valNOT(col < val)=>NOT(stats.max.col < val)col > val=>stats.max.col > valNOT(col > val)=>NOT(stats.min.col > val)If I understand correctly, the two involving max stats would be changed to:
col > val=>stats.max.col > val - 999NOT(col < val)=>NOT(stats.max.col < val - 999)And because this is data skipping, we only care whether the new rewrite might produce a
FALSEwhere the old rewrite did not produceFALSE. Because that corresponds to wrongly skipping a file.For the first: if
stats.max.col > val - 999is FALSE, then the max-value is "too small" andstats.max.col > valmust also return FALSE.For the second, let's simplify a bit by pushing down the NOT:
NOT(stats.max.col < val - 999)=>stats.max.col >= val - 999If
stats.max.col >= val - 999is FALSE, then the max-value is again "too small" andstats.max.col >= valmust also return FALSE.AFAICT, the rewrite is sound, because any time it returns FALSE the original predicate also returned FALSE.