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

lightning: support importing timestamp from Hive parquet #37685

Merged
merged 10 commits into from Sep 13, 2022

Conversation

buchuitoudegou
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #37536

Problem Summary:

What is changed and how it works?

The timestamp exported from Hive will be encoded as INT96 which is natively supported by neither parquet-go nor Golang itself. Therefore, this type of timestamp will be stored as strings in lightning. Because we cannot extract timestamps from this byte string, the import result is kind of fallacious.

This PR is about to identify this type of timestamp and decode true time from it.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Sep 7, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • D3Hunter
  • lance6716

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none do-not-merge/needs-triage-completed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 7, 2022
@buchuitoudegou
Copy link
Contributor Author

/component lightning
/cc @dsdashun @D3Hunter @lance6716

@ti-chi-bot ti-chi-bot added the component/lightning This issue is related to Lightning of TiDB. label Sep 7, 2022
@@ -446,6 +449,13 @@ func setDatumByString(d *types.Datum, v string, meta *parquet.SchemaElement) {
if meta.LogicalType != nil && meta.LogicalType.DECIMAL != nil {
v = binaryToDecimalStr([]byte(v), int(meta.LogicalType.DECIMAL.Scale))
}
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 12 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 12 {
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len(v) == 12 {

Or maybe use 96/8 to replace 12 😂

Copy link
Contributor

Choose a reason for hiding this comment

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

what's the value of LogicalType if enter this branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No logical type is set (nil).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 12 {
ts := int96ToTime([]byte(v))
ts = ts.UTC()
tsStr := ts.Format("2006-01-02 15:04:05.999999Z")
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we choose this layout?

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 from

timeStr := formatTime(v, logicalType.TIMESTAMP.Unit, "2006-01-02 15:04:05.999999",
"2006-01-02 15:04:05.999999Z", logicalType.TIMESTAMP.IsAdjustedToUTC)

This layout is used when setting a UTC timestamp.

return time.Unix(sec, int64(nsec))
}

func int96ToTime(parquetDate []byte) time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you refer to a parquet encoding for timestamp as comment?

Copy link
Contributor

@lance6716 lance6716 Sep 7, 2022

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.

@@ -26,6 +27,8 @@ const (
// if a parquet if small than this threshold, parquet will load the whole file in a byte slice to
// optimize the read performance
smallParquetFileThreshold = 256 * 1024 * 1024
jan011970 = 2440588
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the meaning of 2440588

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 decoding of the INT96 timestamp refers to a parquet library called parquet-go. Will paste some comments here tomorrow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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


func jdToTime(jd int32, nsec int64) time.Time {
sec := int64(jd-jan011970) * secPerDay
return time.Unix(sec, int64(nsec))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return time.Unix(sec, int64(nsec))
return time.Unix(sec, nsec)

And can you add unit tests for nsec outside [0, 999,999,999]? (see the comment of time.Unix)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will take a look that this later.

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

@D3Hunter D3Hunter left a comment

Choose a reason for hiding this comment

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

rest lgtm

@@ -446,6 +449,13 @@ func setDatumByString(d *types.Datum, v string, meta *parquet.SchemaElement) {
if meta.LogicalType != nil && meta.LogicalType.DECIMAL != nil {
v = binaryToDecimalStr([]byte(v), int(meta.LogicalType.DECIMAL.Scale))
}
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 12 {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the value of LogicalType if enter this branch

br/pkg/lightning/mydump/parquet_parser.go Outdated Show resolved Hide resolved
Copy link
Contributor

@lance6716 lance6716 left a comment

Choose a reason for hiding this comment

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

rest lgtm

br/pkg/lightning/mydump/parquet_parser.go Outdated Show resolved Hide resolved
br/pkg/lightning/mydump/parquet_parser.go Outdated Show resolved Hide resolved
@@ -446,6 +451,11 @@ func setDatumByString(d *types.Datum, v string, meta *parquet.SchemaElement) {
if meta.LogicalType != nil && meta.LogicalType.DECIMAL != nil {
v = binaryToDecimalStr([]byte(v), int(meta.LogicalType.DECIMAL.Scale))
}
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 96/8 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 96/8 {
if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len(v) == 96/8 {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if meta.Type != nil && *meta.Type == parquet.Type_INT96 && len([]byte(v)) == 96/8 {
ts := int96ToTime([]byte(v))
ts = ts.UTC()
v = ts.Format("2006-01-02 15:04:05.999999Z")
Copy link
Contributor

Choose a reason for hiding this comment

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

this variable can be extracted as a constant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

br/pkg/lightning/mydump/parquet_parser.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 8, 2022
buchuitoudegou and others added 4 commits September 8, 2022 12:01
Co-authored-by: lance6716 <lance6716@gmail.com>
Co-authored-by: lance6716 <lance6716@gmail.com>
Co-authored-by: lance6716 <lance6716@gmail.com>
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 8, 2022
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 8, 2022
@buchuitoudegou
Copy link
Contributor Author

/run-integration-br-test

@hawkingrei
Copy link
Member

/run-all-tests

@hawkingrei
Copy link
Member

/run-mysql-test

@lance6716
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 63dfb26

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Sep 13, 2022
@ti-chi-bot ti-chi-bot merged commit 08cab41 into pingcap:master Sep 13, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Sep 13, 2022

TiDB MergeCI notify

✅ Well Done! New fixed [1] after this pr merged.

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci-tidb/integration-common-test 🔴 failed 2, success 15, total 17 9 min 13 sec Existing failure
idc-jenkins-ci-tidb/integration-ddl-test ✅ all 6 tests passed 22 min Fixed
idc-jenkins-ci/integration-cdc-test 🟢 all 37 tests passed 29 min Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 11 tests passed 9 min 58 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 32 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 4 min 30 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 4 min 23 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 4 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 2 min 42 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/lightning This issue is related to Lightning of TiDB. release-note-none size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lightning: ts discrepancy between parquet file from Hive and imported data
6 participants