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

java.sql.Timestamp not persisted with full precision #889

Closed
purpledrazi opened this issue Mar 16, 2022 · 2 comments · Fixed by #890
Closed

java.sql.Timestamp not persisted with full precision #889

purpledrazi opened this issue Mar 16, 2022 · 2 comments · Fixed by #890
Labels

Comments

@purpledrazi
Copy link
Contributor

Describe the bug
java.sql.Timestamp supports nanosecond precision, but data is truncated to milliseconds after being serialised and deserialised.

To Reproduce

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.sql.Timestamp;
import java.time.Instant;

import org.junit.jupiter.api.Test;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

class KryoTimestampTest {

  @Test
  void timestampRoundtrip() {
    
    var k = new Kryo();
    k.setRegistrationRequired(false);
    
    var bytes = new ByteArrayOutputStream();
    
    var originalTimestamp  = new Timestamp(Instant.parse("2022-03-16T17:03:05Z").toEpochMilli());
    originalTimestamp.setNanos(123456789);
    
    assertThat(originalTimestamp.toInstant()).isEqualTo(Instant.parse("2022-03-16T17:03:05.123456789Z"));
    
    try(var out = new Output(bytes)) {
      k.writeClassAndObject(out, originalTimestamp);
    }
    
    try(var in = new Input(new ByteArrayInputStream(bytes.toByteArray()))) {
      var deserialisedTimestamp = (Timestamp) k.readClassAndObject(in);
      assertThat(deserialisedTimestamp.toInstant()).isEqualTo(Instant.parse("2022-03-16T17:03:05.123456789Z"));
    }
  }
}

Environment:

  • OS: Debian
  • JDK Version: 17.0.2
  • Kryo Version: 5.3.0

Additional context
None

@theigl
Copy link
Collaborator

theigl commented Mar 16, 2022

@purpledrazi: Thanks for reporting this. The DateSerializer is quite generic and can only handle millisecond precision.

We need a custom serializer for java.sql.Timestamp that stores the nanos part as well. Since this would a backwards incompatible change, we won't be able to register this serializer by default in Kryo 5, but we could add it to the codebase.

Do you want to create a PR for this?

purpledrazi added a commit to purpledrazi/kryo that referenced this issue Mar 19, 2022
@purpledrazi
Copy link
Contributor Author

purpledrazi commented Mar 19, 2022

Raised PR #890 for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants