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

[FLINK-15602][table-planner-blink] Padding TIMESTAMP type to respect … #10877

Closed
wants to merge 3 commits into from

Conversation

docete
Copy link
Contributor

@docete docete commented Jan 17, 2020

…the precision when casting timestamp to varchar

What is the purpose of the change

According to SQL 2011 Part 2 Section 6.13 General Rules 11) d)

If SD is a datetime data type or an interval data type then let Y be the shortest character string that
conforms to the definition of in Subclause 5.3, “”, and such that the interpreted value of Y is SV and the interpreted precision of Y is the precision of SD.

This PR padding the TIMESTAMP type to respect the precision when casting timestamp to varchar.

Brief change log

  • c53d150 Padding TIMESTAMP type to respect precision

Verifying this change

This change is already covered by existing tests, such as TempralTypeTests.

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): (yes / no)
  • The public API, i.e., is any changed class annotated with @Public(Evolving): (yes / no)
  • The serializers: (yes / no / don't know)
  • The runtime per-record code paths (performance sensitive): (yes / no / don't know)
  • Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Yarn/Mesos, ZooKeeper: (yes / no / don't know)
  • The S3 file system connector: (yes / no / don't know)

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)

…the precision when casting timestamp to varchar
@docete docete requested a review from wuchong January 17, 2020 06:16
@flinkbot
Copy link
Collaborator

Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
to review your pull request. We will use this comment to track the progress of the review.

Automated Checks

Last check on commit c53d150 (Fri Jan 17 06:19:25 UTC 2020)

Warnings:

  • No documentation files were touched! Remember to keep the Flink docs up to date!

Mention the bot in a comment to re-run the automated checks.

Review Progress

  • ❓ 1. The [description] looks good.
  • ❓ 2. There is [consensus] that the contribution should go into to Flink.
  • ❓ 3. Needs [attention] from.
  • ❓ 4. The change fits into the overall [architecture].
  • ❓ 5. Overall code [quality] is good.

Please see the Pull Request Review Guide for a full explanation of the review process.


The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required Bot commands
The @flinkbot bot supports the following commands:

  • @flinkbot approve description to approve one or more aspects (aspects: description, consensus, architecture and quality)
  • @flinkbot approve all to approve all aspects
  • @flinkbot approve-until architecture to approve everything until architecture
  • @flinkbot attention @username1 [@username2 ..] to require somebody's attention
  • @flinkbot disapprove architecture to remove an approval you gave earlier

@flinkbot
Copy link
Collaborator

flinkbot commented Jan 17, 2020

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run travis re-run the last Travis build
  • @flinkbot run azure re-run the last Azure build

"MAP[TIME '14:15:16', TIMESTAMP '1985-04-11 14:15:16', " +
"TIME '17:18:19', TIMESTAMP '2018-07-26 17:18:19']",
"TIME '17:18:19', TIMESTAMP '2018-07-26 17:18:19']",
Copy link
Member

Choose a reason for hiding this comment

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

I find that we have a lot of this changes that remove the toTimestamp from the tests.

I'm wondering could we align the toTimestamp with the SQL timestmap literal?

Currently "2018-07-26 17:18:19".toTimestamp actually calls "2018-07-26 17:18:19".cast(DataTypes.TIMESTAMP(3)), however, according to the javadoc of toTimestamp, it should be the same with SQL TIMESTAMP string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently toTimestamp in Table API can't align to Timestamp literal, because it use SqlTimeTypeInfo.TIMESTAMP as the target 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.

There is no such thing in table API which align to Timestamp literal in SQL. I will add some comments to clarify this.

@@ -795,8 +794,8 @@ class TemporalTypesTest extends ExpressionTestBase {
def testTemporalShanghai(): Unit = {
config.setLocalTimeZone(ZoneId.of("Asia/Shanghai"))

testSqlApi(timestampTz("2018-03-14 19:01:02.123"), "2018-03-14 19:01:02.123")
testSqlApi(timestampTz("2018-03-14 19:00:00.010"), "2018-03-14 19:00:00.01")
testSqlApi(timestampTz("2018-03-14 19:01:02.123"), "2018-03-14 19:01:02.123000")
Copy link
Member

Choose a reason for hiding this comment

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

Currently, we don't have a way to construct TIMESTAMP WITH LOCAL TIME ZONE literal. timestampTz will lose the original timestamp literal precision and use a default 6 precision. The tests are counter-intuitive because we are padding zeros for a timestamp literal.

Could we improve the timestampTz a little bit? It can extract precision from the literal string, and cast to the local timestamp with the extracted precison.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

timestampTz(expr) is a shortcut of CAST(expr AS TIMESTAMP WITH LOCAL TIME ZONE), Do you mean we should provide a builtin UDF to convert TIMESTAMP literal to TIMESTAMP WITH LOCAL TIME ZONE?

Copy link
Member

Choose a reason for hiding this comment

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

I mean we should improve timestampTz to convert timestamp without precision loss.

private def timestampTz(str: String) = {
  val precision = extractPrecision(str)
   s"CAST(TIMESTAMP '$str' AS TIMESTAMP($precision) WITH LOCAL TIME ZONE)"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, According to SQL standard, for timestamp literal, the length of the second fraction should be the precision.

wuchong pushed a commit to wuchong/flink that referenced this pull request Jan 23, 2020
…o respect the precision when casting timestamp to varchar

This closes apache#10877
wuchong pushed a commit to wuchong/flink that referenced this pull request Jan 23, 2020
…o respect the precision when casting timestamp to varchar

This closes apache#10877
Copy link
Member

@wuchong wuchong left a comment

Choose a reason for hiding this comment

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

LGTM. I also verified the e2e test in my local machine, need some modification on the e2e tests.

Will merge this.

wuchong pushed a commit to wuchong/flink that referenced this pull request Jan 23, 2020
…o respect the precision when casting timestamp to varchar

This closes apache#10877
@wuchong wuchong closed this in 84e76b0 Jan 23, 2020
wuchong pushed a commit that referenced this pull request Jan 23, 2020
…o respect the precision when casting timestamp to varchar

This closes #10877
JTaky pushed a commit to JTaky/flink that referenced this pull request Feb 20, 2020
…o respect the precision when casting timestamp to varchar

This closes apache#10877
jnh5y pushed a commit to jnh5y/flink that referenced this pull request Dec 18, 2023
…o respect the precision when casting timestamp to varchar

This closes apache#10877
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants