Skip to content

Conversation

@liukun4515
Copy link
Contributor

Which issue does this PR close?

from the #4726 (comment)

If we want to compare the timestamp with date, we just need to add logic in type_coercion, and don't need to do type coercion in the physical phase.

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates labels Dec 27, 2022
},
(Timestamp(_, tz), Utf8) => Some(Timestamp(TimeUnit::Nanosecond, tz.clone())),
(Utf8, Timestamp(_, tz)) => Some(Timestamp(TimeUnit::Nanosecond, tz.clone())),
// TODO: need to investigate the result type for the comparison between timestamp and date
Copy link
Contributor Author

Choose a reason for hiding this comment

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

#4644 (comment)
we need to get the behavior for the comparison in the PG.

Copy link
Contributor

Choose a reason for hiding this comment

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

@liukun4515 comparison is usually boolean type, did you mean what is the common datatype for casting between timestamp and date in diff scenarios?

Copy link
Contributor Author

@liukun4515 liukun4515 Dec 28, 2022

Choose a reason for hiding this comment

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

Yes
the result of comparison is bool type, but if left and right are not the same data type, we should get the common type for left and right through the type coercion.
For example, INT32>INT64, the common type is INT64, and we should cast the left to INT64.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type of type coercion is decided by input types and op.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@liukun4515 comparison is usually boolean type, did you mean what is the common datatype for casting between timestamp and date in diff scenarios?

I think it's not about casting from one type to other type, it is related the type inference for different input types and different operation or functions.

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 try the comparison timestamp with date in the Spark, and find the coerced type is timestamp.

spark-sql> explain extended select now()>cast('2000-01-01' as date);
== Parsed Logical Plan ==
'Project [unresolvedalias(('now() > cast(2000-01-01 as date)), None)]
+- OneRowRelation

== Analyzed Logical Plan ==
(now() > CAST(2000-01-01 AS DATE)): boolean
Project [(now() > cast(cast(2000-01-01 as date) as timestamp)) AS (now() > CAST(2000-01-01 AS DATE))#49]
+- OneRowRelation

== Optimized Logical Plan ==
Project [true AS (now() > CAST(2000-01-01 AS DATE))#49]
+- OneRowRelation

== Physical Plan ==
*(1) Project [true AS (now() > CAST(2000-01-01 AS DATE))#49]
+- *(1) Scan OneRowRelation[]

cc @comphead @alamb

But current implementation of coerced type is date, we can fix this in the follow up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Timestamp is probably consistent with other coercions that ensure no data is truncated (so going Date --> Timestamp is good as the the Timestamp could be losslessly converted back to a Date). However, going from Timestamp --> Date would truncate the precision of the original timesteamp,

@liukun4515
Copy link
Contributor Author

cc @alamb @comphead

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.

👍 I like it

(I made a similar version here #4745 before I found this PR)

Timestamp(TimeUnit::Nanosecond, None) => {
matches!(type_from, Null | Timestamp(_, None))
}
Date32 => {
Copy link
Contributor

Choose a reason for hiding this comment

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

why this change?

Though I admit that the fact that all tests still pass is concerning 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alamb alamb changed the title revert some code for #4726 revert some code for #4726 / remove unnecessary coercion in physical plans Dec 27, 2022
/// Returns a common coerced datatype between 2 given datatypes
///
/// See the module level documentation for more detail on coercion.
pub fn get_common_coerced_type(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this method can be of some interest, if the user wants to check the common coerced type between 2 datatypes?

Copy link
Contributor

Choose a reason for hiding this comment

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

It may be worth waiting for a specific use before leaving it in 🤔

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 think this method can be of some interest, if the user wants to check the common coerced type between 2 datatypes?

the common coerced type will be decided by input exprs/input types and op, if the op is different and the same input types/input exprs may get the different coerced type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's merge this PR then -- we can always recover get_common_coerced_type from this PR or git history if we need it.

@ursabot
Copy link

ursabot commented Dec 28, 2022

Benchmark runs are scheduled for baseline = d3ca9b0 and contender = 760f108. 760f108 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

@liukun4515 liukun4515 deleted the remove_type_coercion_in_phy branch December 29, 2022 06:09
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 physical-expr Changes to the physical-expr crates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants