-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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 timestamp type in mysql jdbc connector #21937
Support timestamp type in mysql jdbc connector #21937
Conversation
20da0e1
to
b0ff5f5
Compare
@@ -165,6 +169,22 @@ else if (DATE.equals(type)) { | |||
long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis); | |||
statement.setDate(parameter, new Date(localMillis)); | |||
} | |||
else if (type instanceof TimestampType) { | |||
long timestampValue = type.getLong(block, position); | |||
int nanos = ((TimestampType) type).getPrecision() == MILLISECONDS ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thinking would it make sense to move get nanos
and get millis
to com.facebook.presto.common.type.TimestampType
although this would be the only place to use them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea! These logics are common for timestamp
and may be used somewhere else.
To make it more universal, I have extracted getEpochSecond(long timestamp)
which returns timestamp's number of total seconds, and getNanos(long timestamp)
which returns the timestamp's nanosecond portion. Please take a look. Any thoughts please let me know, thanks!
b0ff5f5
to
ad06658
Compare
* Returns: | ||
* this timestamp's fractional seconds component | ||
*/ | ||
public int getNanos(long timestamp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious that any reason you return int
here instead of long
. I saw the API for Instant.ofEpochSecond
is (long, long)
.
Another thing I found is that java.util.concurrent.TimeUnit
provides MICROSECONDS.toNanos(long)
and MILLISECONDS.toNanos(long)
not sure if that's relevant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious that any reason you return
int
here instead oflong
. I saw the API forInstant.ofEpochSecond
is(long, long)
.
I consulted the API definition of java.time.Instant.getNano()
, int
type would be enough as the return value of getNanos
would always less than 1000,000,000. What's your opinion?
Another thing I found is that
java.util.concurrent.TimeUnit
providesMICROSECONDS.toNanos(long)
andMILLISECONDS.toNanos(long)
not sure if that's relevant.
Thanks, that could be useful. Consulting the implementation in DecodeTimestampOptions
, I have modified the code to use them. Please take a look!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way, seems the second parameter of Instant.ofEpochSecond(long, long)
could be nanos representing longer than one second, so its type was declared as long
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I believe for the current usage, it only reduces the stack size but not the heap because java.sql.Timestamp
is still using long
and consumes the same amount of heap size.
However, if this method is used by other code and the constructed object is using int, then it does reduce the heap usage. You can leave it as int
.
ad06658
to
9fec3b9
Compare
{ | ||
return this.precision == MILLISECONDS ? | ||
MILLISECONDS.toSeconds(timestamp) : | ||
MICROSECONDS.toSeconds(timestamp); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this.precision.toSeconds(timestamp)
?
If that's the case, I start to wonder do we need this function. sorry for the hassle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
this.precision.toSeconds(timestamp)
?
Oh, it's better, I have fixed it. Thanks a lot for your suggestion that lead to the continuous improvement in our implementation.
I think this function still has some value for providing the utility logic and the corresponding comment, as the usage TimestampType.getPrecision().toSeconds(long)
from other place is not so straightforward. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure! sold.
* Returns: | ||
* this timestamp's fractional seconds component | ||
*/ | ||
public int getNanos(long timestamp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I believe for the current usage, it only reduces the stack size but not the heap because java.sql.Timestamp
is still using long
and consumes the same amount of heap size.
However, if this method is used by other code and the constructed object is using int, then it does reduce the heap usage. You can leave it as int
.
9fec3b9
to
86cad4d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
It looks like this PR only supports milliseconds precision for timestamp type. I will raise a github issue and link with this PR. |
@pratyakshsharma Sure, thank you for pointing out the problems if there is any. I think this change can internally support microseconds precision as well. So please describe the problematic scenario in the new issue. |
Description
Fix issue #21731. Support column type: timestamp in mysql connector.
Test Plan
Contributor checklist
Release Notes