-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
feat: Support Substrait's IntervalCompound type/literal instead of interval-month-day-nano UDT #12112
Merged
Merged
feat: Support Substrait's IntervalCompound type/literal instead of interval-month-day-nano UDT #12112
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e18ec17
feat(substrait): use IntervalCompound instead of interval-month-day-n…
Blizzara 8b628e0
clippy
Blizzara 36402bc
more clippy
Blizzara 94ee836
even more clippy
Blizzara 79f6341
fix precision exponent
Blizzara ea8bc4a
add a test
Blizzara d527b4e
update deprecation version
Blizzara 8c215ea
update deprecation comments
Blizzara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,17 +42,18 @@ use crate::variation_const::{ | |
DATE_32_TYPE_VARIATION_REF, DATE_64_TYPE_VARIATION_REF, | ||
DECIMAL_128_TYPE_VARIATION_REF, DECIMAL_256_TYPE_VARIATION_REF, | ||
DEFAULT_CONTAINER_TYPE_VARIATION_REF, DEFAULT_TYPE_VARIATION_REF, | ||
INTERVAL_MONTH_DAY_NANO_TYPE_NAME, LARGE_CONTAINER_TYPE_VARIATION_REF, | ||
UNSIGNED_INTEGER_TYPE_VARIATION_REF, VIEW_CONTAINER_TYPE_VARIATION_REF, | ||
LARGE_CONTAINER_TYPE_VARIATION_REF, UNSIGNED_INTEGER_TYPE_VARIATION_REF, | ||
VIEW_CONTAINER_TYPE_VARIATION_REF, | ||
}; | ||
#[allow(deprecated)] | ||
use crate::variation_const::{ | ||
INTERVAL_DAY_TIME_TYPE_REF, INTERVAL_MONTH_DAY_NANO_TYPE_REF, | ||
INTERVAL_YEAR_MONTH_TYPE_REF, TIMESTAMP_MICRO_TYPE_VARIATION_REF, | ||
TIMESTAMP_MILLI_TYPE_VARIATION_REF, TIMESTAMP_NANO_TYPE_VARIATION_REF, | ||
TIMESTAMP_SECOND_TYPE_VARIATION_REF, | ||
INTERVAL_DAY_TIME_TYPE_REF, INTERVAL_MONTH_DAY_NANO_TYPE_NAME, | ||
INTERVAL_MONTH_DAY_NANO_TYPE_REF, INTERVAL_YEAR_MONTH_TYPE_REF, | ||
TIMESTAMP_MICRO_TYPE_VARIATION_REF, TIMESTAMP_MILLI_TYPE_VARIATION_REF, | ||
TIMESTAMP_NANO_TYPE_VARIATION_REF, TIMESTAMP_SECOND_TYPE_VARIATION_REF, | ||
}; | ||
use datafusion::arrow::array::{new_empty_array, AsArray}; | ||
use datafusion::arrow::temporal_conversions::NANOSECONDS; | ||
use datafusion::common::scalar::ScalarStructBuilder; | ||
use datafusion::dataframe::DataFrame; | ||
use datafusion::logical_expr::expr::InList; | ||
|
@@ -71,10 +72,10 @@ use datafusion::{ | |
use std::collections::HashSet; | ||
use std::sync::Arc; | ||
use substrait::proto::exchange_rel::ExchangeKind; | ||
use substrait::proto::expression::literal::interval_day_to_second::PrecisionMode; | ||
use substrait::proto::expression::literal::user_defined::Val; | ||
use substrait::proto::expression::literal::{ | ||
IntervalDayToSecond, IntervalYearToMonth, UserDefined, | ||
interval_day_to_second, IntervalCompound, IntervalDayToSecond, IntervalYearToMonth, | ||
UserDefined, | ||
}; | ||
use substrait::proto::expression::subquery::SubqueryType; | ||
use substrait::proto::expression::{self, FieldReference, Literal, ScalarFunction}; | ||
|
@@ -1831,9 +1832,14 @@ fn from_substrait_type( | |
Ok(DataType::Interval(IntervalUnit::YearMonth)) | ||
} | ||
r#type::Kind::IntervalDay(_) => Ok(DataType::Interval(IntervalUnit::DayTime)), | ||
r#type::Kind::IntervalCompound(_) => { | ||
Ok(DataType::Interval(IntervalUnit::MonthDayNano)) | ||
} | ||
r#type::Kind::UserDefined(u) => { | ||
if let Some(name) = extensions.types.get(&u.type_reference) { | ||
#[allow(deprecated)] | ||
match name.as_ref() { | ||
// Kept for backwards compatibility, producers should use IntervalCompound instead | ||
INTERVAL_MONTH_DAY_NANO_TYPE_NAME => Ok(DataType::Interval(IntervalUnit::MonthDayNano)), | ||
_ => not_impl_err!( | ||
"Unsupported Substrait user defined type with ref {} and variation {}", | ||
|
@@ -1842,18 +1848,17 @@ fn from_substrait_type( | |
), | ||
} | ||
} else { | ||
// Kept for backwards compatibility, new plans should include the extension instead | ||
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. minor: I would suggest updating this warning here and further down to something like // Kept for backwards compatibility
// Producers should use IntervalCompound instead to make it clearer what/who should be using the new type. 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. |
||
#[allow(deprecated)] | ||
match u.type_reference { | ||
// Kept for backwards compatibility, use IntervalYear instead | ||
// Kept for backwards compatibility, producers should use IntervalYear instead | ||
INTERVAL_YEAR_MONTH_TYPE_REF => { | ||
Ok(DataType::Interval(IntervalUnit::YearMonth)) | ||
} | ||
// Kept for backwards compatibility, use IntervalDay instead | ||
// Kept for backwards compatibility, producers should use IntervalDay instead | ||
INTERVAL_DAY_TIME_TYPE_REF => { | ||
Ok(DataType::Interval(IntervalUnit::DayTime)) | ||
} | ||
// Not supported yet by Substrait | ||
// Kept for backwards compatibility, producers should use IntervalCompound instead | ||
INTERVAL_MONTH_DAY_NANO_TYPE_REF => { | ||
Ok(DataType::Interval(IntervalUnit::MonthDayNano)) | ||
} | ||
|
@@ -2275,6 +2280,7 @@ fn from_substrait_literal( | |
subseconds, | ||
precision_mode, | ||
})) => { | ||
use interval_day_to_second::PrecisionMode; | ||
// DF only supports millisecond precision, so for any more granular type we lose precision | ||
let milliseconds = match precision_mode { | ||
Some(PrecisionMode::Microseconds(ms)) => ms / 1000, | ||
|
@@ -2299,6 +2305,35 @@ fn from_substrait_literal( | |
Some(LiteralType::IntervalYearToMonth(IntervalYearToMonth { years, months })) => { | ||
ScalarValue::new_interval_ym(*years, *months) | ||
} | ||
Some(LiteralType::IntervalCompound(IntervalCompound { | ||
interval_year_to_month, | ||
interval_day_to_second, | ||
})) => match (interval_year_to_month, interval_day_to_second) { | ||
( | ||
Some(IntervalYearToMonth { years, months }), | ||
Some(IntervalDayToSecond { | ||
days, | ||
seconds, | ||
subseconds, | ||
precision_mode: | ||
Some(interval_day_to_second::PrecisionMode::Precision(p)), | ||
}), | ||
) => { | ||
if *p < 0 || *p > 9 { | ||
return plan_err!( | ||
"Unsupported Substrait interval day to second precision: {}", | ||
p | ||
); | ||
} | ||
let nanos = *subseconds * i64::pow(10, (9 - p) as u32); | ||
ScalarValue::new_interval_mdn( | ||
*years * 12 + months, | ||
*days, | ||
*seconds as i64 * NANOSECONDS + nanos, | ||
) | ||
} | ||
_ => return plan_err!("Substrait compound interval missing components"), | ||
}, | ||
Some(LiteralType::FixedChar(c)) => ScalarValue::Utf8(Some(c.clone())), | ||
Some(LiteralType::UserDefined(user_defined)) => { | ||
// Helper function to prevent duplicating this code - can be inlined once the non-extension path is removed | ||
|
@@ -2329,6 +2364,8 @@ fn from_substrait_literal( | |
|
||
if let Some(name) = extensions.types.get(&user_defined.type_reference) { | ||
match name.as_ref() { | ||
// Kept for backwards compatibility - producers should use IntervalCompound instead | ||
#[allow(deprecated)] | ||
INTERVAL_MONTH_DAY_NANO_TYPE_NAME => { | ||
interval_month_day_nano(user_defined)? | ||
} | ||
|
@@ -2341,10 +2378,9 @@ fn from_substrait_literal( | |
} | ||
} | ||
} else { | ||
// Kept for backwards compatibility - new plans should include extension instead | ||
#[allow(deprecated)] | ||
match user_defined.type_reference { | ||
// Kept for backwards compatibility, use IntervalYearToMonth instead | ||
// Kept for backwards compatibility, producers should useIntervalYearToMonth instead | ||
INTERVAL_YEAR_MONTH_TYPE_REF => { | ||
let Some(Val::Value(raw_val)) = user_defined.val.as_ref() else { | ||
return substrait_err!("Interval year month value is empty"); | ||
|
@@ -2359,7 +2395,7 @@ fn from_substrait_literal( | |
value_slice, | ||
))) | ||
} | ||
// Kept for backwards compatibility, use IntervalDayToSecond instead | ||
// Kept for backwards compatibility, producers should useIntervalDayToSecond instead | ||
INTERVAL_DAY_TIME_TYPE_REF => { | ||
let Some(Val::Value(raw_val)) = user_defined.val.as_ref() else { | ||
return substrait_err!("Interval day time value is empty"); | ||
|
@@ -2379,6 +2415,7 @@ fn from_substrait_literal( | |
milliseconds, | ||
})) | ||
} | ||
// Kept for backwards compatibility, producers should useIntervalCompound instead | ||
INTERVAL_MONTH_DAY_NANO_TYPE_REF => { | ||
interval_month_day_nano(user_defined)? | ||
} | ||
|
@@ -2727,3 +2764,52 @@ impl BuiltinExprBuilder { | |
})) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::extensions::Extensions; | ||
use crate::logical_plan::consumer::from_substrait_literal_without_names; | ||
use arrow_buffer::IntervalMonthDayNano; | ||
use datafusion::error::Result; | ||
use datafusion::scalar::ScalarValue; | ||
use substrait::proto::expression::literal::{ | ||
interval_day_to_second, IntervalCompound, IntervalDayToSecond, | ||
IntervalYearToMonth, LiteralType, | ||
}; | ||
use substrait::proto::expression::Literal; | ||
|
||
#[test] | ||
fn interval_compound_different_precision() -> Result<()> { | ||
// DF producer (and thus roundtrip) always uses precision = 9, | ||
// this test exists to test with some other value. | ||
let substrait = Literal { | ||
nullable: false, | ||
type_variation_reference: 0, | ||
literal_type: Some(LiteralType::IntervalCompound(IntervalCompound { | ||
interval_year_to_month: Some(IntervalYearToMonth { | ||
years: 1, | ||
months: 2, | ||
}), | ||
interval_day_to_second: Some(IntervalDayToSecond { | ||
days: 3, | ||
seconds: 4, | ||
subseconds: 5, | ||
precision_mode: Some( | ||
interval_day_to_second::PrecisionMode::Precision(6), | ||
), | ||
}), | ||
})), | ||
}; | ||
|
||
assert_eq!( | ||
from_substrait_literal_without_names(&substrait, &Extensions::default())?, | ||
ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano { | ||
months: 14, | ||
days: 3, | ||
nanoseconds: 4_000_005_000 | ||
})) | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Possibly out of scope of this PR, but do we need to checking the precision on this type now?
Based on https://github.com/substrait-io/substrait/blob/683f4179a058c2c99c04501b920a48ff372356ff/proto/substrait/type.proto#L132-L140 DataFusion can only handle this type with precision loss when
precision
is set to 0 or 3 (seconds or milliseconds).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.
Yup see #12112 (comment). I think we should change it, maybe with some option so that user can decide if they want to throw or accept the precision loss. But will be a separate PR :)