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: Enable typed strings expressions for VALUES clause #3018

Merged
merged 2 commits into from
Aug 3, 2022

Conversation

stuartcarnie
Copy link
Contributor

Which issue does this PR close?

Closes #3017.

What changes are included in this PR?

Support for transforming typed string literal expressions to CAST expressions in a VALUES clause.

Are there any user-facing changes?

This extends the supported expressions in a VALUES list clause.

@github-actions github-actions bot added the sql SQL Planner label Aug 3, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #3018 (c46d61d) into master (e23925c) will decrease coverage by 0.00%.
The diff coverage is 85.71%.

@@            Coverage Diff             @@
##           master    #3018      +/-   ##
==========================================
- Coverage   85.84%   85.84%   -0.01%     
==========================================
  Files         283      283              
  Lines       51658    51665       +7     
==========================================
+ Hits        44346    44352       +6     
- Misses       7312     7313       +1     
Impacted Files Coverage Δ
datafusion/sql/src/planner.rs 81.92% <85.71%> (+0.01%) ⬆️
datafusion/expr/src/logical_plan/plan.rs 77.43% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it 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.

Looks great @stuartcarnie . Thanks!

I also tested in the datafusion-cli to make sure that nothing extra in the physical plan was needed and it all looked good 👍

DataFusion CLI v10.0.0SELECT col1, col2 FROM (VALUES (TIMESTAMP '2021-06-10 17:01:00Z', DATE '2004-04-09')) as t (col1, col2)
;
+---------------------+------------+
| col1                | col2       |
+---------------------+------------+
| 2021-06-10 17:01:00 | 2004-04-09 |
+---------------------+------------+
1 row in set. Query took 0.053 seconds.

Comment on lines 1616 to 1622
SQLExpr::TypedString {
ref data_type,
ref value,
} => Ok(Expr::Cast {
expr: Box::new(lit(&**value)),
data_type: convert_data_type(data_type)?,
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

In case you wanted to make this work with somewhat fewer * and & you can do something like:

Suggested change
SQLExpr::TypedString {
ref data_type,
ref value,
} => Ok(Expr::Cast {
expr: Box::new(lit(&**value)),
data_type: convert_data_type(data_type)?,
}),
SQLExpr::TypedString {
data_type,
value,
} => Ok(Expr::Cast {
expr: Box::new(lit(value)),
data_type: convert_data_type(&data_type)?,
}),

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool – I'll make that change as it is much cleaner! I copied this pattern from another part of the file:

https://github.com/apache/arrow-datafusion/blob/c46d61d45eb73f11ac39d8646b2b25a4fbbc80eb/datafusion/sql/src/planner.rs#L1818-L1824

so I've updated that too.

@alamb
Copy link
Contributor

alamb commented Aug 3, 2022

Not that it matters, but I found it quite neat that these casts are evaluated at plan time (as in after simplify_expressions we see that timestamps have been resolved to 1623344460000000000 etc. 🎉

❯ explain verbose SELECT col1, col2 FROM (VALUES (TIMESTAMP '2021-06-10 17:01:00Z', DATE '2004-04-09')) as t (col1, col2);
+-------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| plan_type                                             | plan                                                                                                                                                                                       |
+-------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| initial_logical_plan                                  | Projection: #t.col1, #t.col2                                                                                                                                                               |
|                                                       |   Projection: #t.column1 AS col1, #t.column2 AS col2, alias=t                                                                                                                              |
|                                                       |     Projection: #column1, #column2, alias=t                                                                                                                                                |
|                                                       |       Values: (CAST(Utf8("2021-06-10 17:01:00Z") AS Timestamp(Nanosecond, None)), CAST(Utf8("2004-04-09") AS Date32))                                                                      |
| logical_plan after simplify_expressions               | Projection: #t.col1, #t.col2                                                                                                                                                               |
|                                                       |   Projection: #t.column1 AS col1, #t.column2 AS col2, alias=t                                                                                                                              |
|                                                       |     Projection: #column1, #column2, alias=t                                                                                                                                                |
|                                                       |       Values: (TimestampNanosecond(1623344460000000000, None) AS CAST(Utf8("2021-06-10 17:01:00Z") AS Timestamp(Nanosecond, None)), Date32("12517") AS CAST(Utf8("2004-04-09") AS Date32)) |

@stuartcarnie
Copy link
Contributor Author

Thanks for the review, @alamb – I made those changes you suggested, thanks!

TIL: explain verbose. I've used explain and explain analyse (like PostgreSQL has), but verbose is 😍

@alamb alamb merged commit c57fc86 into apache:master Aug 3, 2022
@alamb
Copy link
Contributor

alamb commented Aug 3, 2022

Thanks again @stuartcarnie

@ursabot
Copy link

ursabot commented Aug 3, 2022

Benchmark runs are scheduled for baseline = 9a5f17e and contender = c57fc86. c57fc86 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@stuartcarnie stuartcarnie deleted the sgc/feat/typed_string_values branch August 4, 2022 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Enable typed strings expressions for VALUES clause
4 participants