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

feat: add transform_literal #287

Merged
merged 3 commits into from
Mar 27, 2024
Merged

Conversation

ZENOTME
Copy link
Contributor

@ZENOTME ZENOTME commented Mar 20, 2024

complete #283

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Mar 20, 2024

@marvinlanhenke
Copy link
Contributor

@ZENOTME
Thank you for this PR. I think I can work with that.

One question though, regarding the input: &Literal - I guess we do this, due to the greater flexibility?

However, for #264 I'm working with BoundPredicates as input.

This means I'd still have to expose the underlying Datum and its PrimitiveLiteral in order to use fn transform_literal. Also, when creating a new UnboundPredicate I'd have to construct a new Datum from the transformed literal.

If we don't need the extra flexibility, changing fn transform_literal to

fn transform_literal(&self, input: &Datum) -> Result<Option<Datum>>

would make the implementation of #264 a lot easier. Perhaps it should be named transform_datum then.

Anyway, thanks again - I'd appreciate your thoughts on this.

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Mar 21, 2024

@ZENOTME Thank you for this PR. I think I can work with that.

One question though, regarding the input: &Literal - I guess we do this, due to the greater flexibility?

However, for #264 I'm working with BoundPredicates as input.

This means I'd still have to expose the underlying Datum and its PrimitiveLiteral in order to use fn transform_literal. Also, when creating a new UnboundPredicate I'd have to construct a new Datum from the transformed literal.

If we don't need the extra flexibility, changing fn transform_literal to

fn transform_literal(&self, input: &Datum) -> Result<Option<Datum>>

would make the implementation of #264 a lot easier. Perhaps it should be named transform_datum then.

Anyway, thanks again - I'd appreciate your thoughts on this.

For now, transform does not rely on the type info, so it can do transform without type info. And transform_literal will also be used in manifest metadata, and manifest store the literal directly. So I think in here what we need is a function extract the reference of literal🤔

@marvinlanhenke
Copy link
Contributor

So I think in here what we need is a function extract the reference of literal🤔

I guess I can do that when implementing #264 since I need this anyway? Other than that thanks again for the PR.

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Mar 21, 2024

I guess I can do that when implementing #264 since I need this anyway?

LGTM. Thanks!

Copy link
Collaborator

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

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

Thanks @ZENOTME for this pr, it looks great! I found some small problems and left some comments.

@@ -31,6 +34,8 @@ pub trait TransformFunction: Send {
/// The implementation of this function will need to check and downcast the input to specific
/// type.
fn transform(&self, input: ArrayRef) -> Result<ArrayRef>;
/// transform_literal will take an input literal and transform it into a new literal.
fn transform_literal(&self, input: &Literal) -> Result<Option<Literal>>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree with @marvinlanhenke that we should accept Datum and return Datum. In theory Literal should be physical value only, while Datum is type + value. I'm thinking reducing literal enums in future.

Literal::Primitive(PrimitiveLiteral::TimestampTZ(v)) => Ok(Some(Literal::Primitive(
PrimitiveLiteral::Int(Self::timestamp_to_year(*v)),
))),
_ => unreachable!("Should not call internally for unsupported literal type"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not panic here, we should return error.

Self::truncate_str(v, len).to_string(),
))
})),
_ => unreachable!("Truncate transform only supports (int,long,decimal,string) types"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto

.unwrap()
.signed_duration_since(DateTime::from_timestamp(0, 0).unwrap())
.num_days()
/ 365) as i32
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is incorrect, leap year maybe not 365 days. We should use NaiveDate::years_since

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or you can use NaiveTime::year

}

/// Extract a date or timestamp month, as months from 1970-01-01
#[derive(Debug)]
pub struct Month;

impl Month {
#[inline]
fn timestamp_to_month(timestamp: i64) -> i32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@ZENOTME
Copy link
Contributor Author

ZENOTME commented Mar 25, 2024

Thanks for your review! Have fixed it. @liurenjie1024 @marvinlanhenke

Copy link
Collaborator

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

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

Thanks @ZENOTME This looks great to me! We just need to fix some unnecessary panic.

impl Year {
#[inline]
fn timestamp_to_year(timestamp: i64) -> i32 {
DateTime::from_timestamp_micros(timestamp).unwrap().year() - UNIX_EPOCH_YEAR
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should not panic here. This is user input, and we should throw error.

// unix epoch date: 1970-01-01
// if date > unix epoch date, delta month = (aa - 1) + 12 * (aaaa-1970)
// if date < unix epoch date, delta month = (12 - (aa - 1)) + 12 * (1970-aaaa-1)
let date = DateTime::from_timestamp_micros(timestamp).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto, timestamp maybe user input and we should not panic.

crates/iceberg/src/transform/temporal.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @ZENOTME !

@liurenjie1024 liurenjie1024 merged commit bbc8578 into apache:main Mar 27, 2024
7 checks passed
@ZENOTME ZENOTME deleted the transform_literal branch March 27, 2024 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants