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

Support to unparse ScalarValue::IntervalMonthDayNano to String #10956

Merged

Conversation

goldmedal
Copy link
Contributor

Which issue does this PR close?

Closes #10795 .

Rationale for this change

I handle only the nanosecond, millisecond, second, minute, and hour units for human readability based on the nanosecond value.

What changes are included in this PR?

Are these changes tested?

yes

Are there any user-facing changes?

no

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions labels Jun 17, 2024
@goldmedal goldmedal force-pushed the feature/10795-IntervalMonthDayNano-to-string branch from 3de86df to 9ae676c Compare June 17, 2024 17:10
@@ -859,6 +878,35 @@ impl Unparser<'_> {
}
}

fn process_interval_nanosecond(nano: i64) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @alamb. It looks great. I'll address this today. :)

Copy link
Contributor Author

@goldmedal goldmedal Jun 18, 2024

Choose a reason for hiding this comment

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

@alamb After using ArrayFormatter, I noticed that the results contain many redundant words. For example:

input expr: interval_month_day_nano_lit("-3 MONTH"),
output result: r#"INTERVAL '0 YEARS -3 MONS 0 DAYS 0 HOURS 0 MINS 0.000000000 SECS'"#,
---
input expr: interval_month_day_nano_lit("1 YEAR 1 MONTH 1 DAY 3 HOUR 10 MINUTE 20 SECOND"),
output result: r#"INTERVAL '0 YEARS 13 MONS 1 DAYS 3 HOURS 10 MINS 20.000000000 SECS'"#,

I think both of these are valid SQL. They just don't look very smart, but it's okay with me.

I implemented the code like

            ScalarValue::IntervalMonthDayNano(Some(_i)) => {
                let wrap_array = v.to_array()?;
                let Some(result) = array_value_to_string(&wrap_array, 0).ok() else {
                    return internal_err!("Unable to convert IntervalMonthDayNano to string");
                };
                let interval = Interval {
                    value: Box::new(ast::Expr::Value(SingleQuotedString(result.to_uppercase()))),
                    leading_field: None,
                    leading_precision: None,
                    last_field: None,
                    fractional_seconds_precision: None,
                };
                Ok(ast::Expr::Interval(interval))
            }

What do you think? Does it make sense?

Copy link
Contributor Author

@goldmedal goldmedal Jun 18, 2024

Choose a reason for hiding this comment

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

Hmm... OK. I think it would be an enhancement for the ArrowFormatter. If we want to make it smarter, I think we should modify the behavior in the arrow-cast crate.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... OK. I think it would be an enhancement for the ArrowFormatter. If we want to make it smarter, I think we should modify the behavior in the arrow-cast crate.

I agree -- I filed apache/arrow-rs#5914 to track this suggestion

Comment on lines +1276 to +1281
(
interval_month_day_nano_lit(
"1 YEAR 1 MONTH 1 DAY 3 HOUR 10 MINUTE 20 SECOND",
),
r#"INTERVAL '0 YEARS 13 MONS 1 DAYS 3 HOURS 10 MINS 20.000000000 SECS'"#,
),
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 removed some tests because I think we use the formatter in arrow-cast now. We can assume the formatting behaviors are tested there. We don't need to keep so many tests here.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks again @goldmedal !

@@ -859,6 +878,35 @@ impl Unparser<'_> {
}
}

fn process_interval_nanosecond(nano: i64) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... OK. I think it would be an enhancement for the ArrowFormatter. If we want to make it smarter, I think we should modify the behavior in the arrow-cast crate.

I agree -- I filed apache/arrow-rs#5914 to track this suggestion

@alamb alamb merged commit 41a7882 into apache:main Jun 18, 2024
23 checks passed
@goldmedal goldmedal deleted the feature/10795-IntervalMonthDayNano-to-string branch June 18, 2024 14:13
@goldmedal
Copy link
Contributor Author

Thanks @alamb

y-f-u pushed a commit to spiceai/datafusion that referenced this pull request Jun 24, 2024
…che#10956)

* support to unparse ScalarValue::IntervalMonthDayNano to String

* use array formatter to format the interval string
y-f-u pushed a commit to spiceai/datafusion that referenced this pull request Jun 24, 2024
…che#10956)

* support to unparse ScalarValue::IntervalMonthDayNano to String

* use array formatter to format the interval string
y-f-u pushed a commit to spiceai/datafusion that referenced this pull request Jun 24, 2024
…che#10956)

* support to unparse ScalarValue::IntervalMonthDayNano to String

* use array formatter to format the interval string
findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
…che#10956)

* support to unparse ScalarValue::IntervalMonthDayNano to String

* use array formatter to format the interval string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement ScalarValue::IntervalMonthDayNano -> String Support
2 participants