Skip to content

Conversation

@hurelhuyag
Copy link
Contributor

java.time.LocalTime to Cassandra time datatype mapping fix

@pivotal-issuemaster
Copy link

@hurelhuyag Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@hurelhuyag Thank you for signing the Contributor License Agreement!

@mp911de mp911de changed the title master DATACASS-694 - Fix LocalTime conversion Oct 24, 2019
@mp911de
Copy link
Member

mp911de commented Oct 24, 2019

Care to add an integration test to org.springframework.data.cassandra.core.convert.CassandraTypeMappingIntegrationTests that asserts the Cassandra-specific representation of time?

@hurelhuyag
Copy link
Contributor Author

Read and Write test will work fine. Not needed to add more. Only way to check that is use cqlsh

@mp911de
Copy link
Member

mp911de commented Oct 24, 2019

We have existing tests that prove that read and write of LocalTime works as expected. Applying a change to the conversion logic does not break existing tests and this is something to worry about. The reason tests didn't break is because conversion follows the same contract and our tests verify values after conversion.

Previously, the representation in Cassandra (Row) didn't meet how Cassandra represents time, therefore it is indeed necessary to add further tests to verify the Cassandra representation as seen through the driver.

@hurelhuyag
Copy link
Contributor Author

On the other hand datastax documented time datatype is nanoseconds not milliseconds.

Value is encoded as a 64-bit signed integer representing the number of nanoseconds since midnight. Values can be represented as strings, such as 13:30:54.234.

https://docs.datastax.com/en/archived/cql/3.3/cql/cql_reference/cql_data_types_c.html#cql_data_types_c__cqlDataTypes

@hurelhuyag
Copy link
Contributor Author

hurelhuyag commented Oct 25, 2019

And Cassandra Row.getTime() method returns long in nanoseconds

long getTime(int i)
Returns the ith value as a long in nanoseconds since midnight.
This method uses the CodecRegistry to find a codec to convert the underlying CQL type to a Java long (for CQL type time, this will be the built-in codec).

https://docs.datastax.com/en/drivers/java/3.7/com/datastax/driver/core/GettableByIndexData.html#getTime-int-

@hurelhuyag
Copy link
Contributor Author

And I'm not sure this kind of test is needed or not

@Test	public void shouldTest(){	assumeTrue(cassandraVersion.isGreaterThanOrEqualTo(VERSION_3_10));	AllPossibleTypes entity = new AllPossibleTypes("1");	entity.setLocalTime(java.time.LocalTime.of(1, 2, 3));	operations.insert(entity);	ResultSet resultSet = session.execute("SELECT localTime FROM AllPossibleTypes WHERE id = '1'");	Iterator<Row> rowIt = resultSet.iterator();	while (rowIt.hasNext()){	Row row = rowIt.next();	long timeNanos = row.getTime(0);	assertThat(timeNanos).isEqualTo(3723000000000L);	}	} 
@mp911de
Copy link
Member

mp911de commented Oct 25, 2019

@hurelhuyag it is needed to prevent that this type of bug comes again.

@hurelhuyag
Copy link
Contributor Author

2 integration test added

mp911de pushed a commit that referenced this pull request Oct 28, 2019
LocalTime now correctly uses nanoseconds. Original pull request: #166.
mp911de added a commit that referenced this pull request Oct 28, 2019
Add author tags. Reformat code. Adapt remaining tests. Original pull request: #166.
mp911de pushed a commit that referenced this pull request Oct 28, 2019
LocalTime now correctly uses nanoseconds. Original pull request: #166.
mp911de added a commit that referenced this pull request Oct 28, 2019
Add author tags. Reformat code. Adapt remaining tests. Original pull request: #166.
mp911de pushed a commit that referenced this pull request Oct 28, 2019
LocalTime now correctly uses nanoseconds. Original pull request: #166.
mp911de added a commit that referenced this pull request Oct 28, 2019
Add author tags. Reformat code. Adapt remaining tests. Original pull request: #166.
@mp911de
Copy link
Member

mp911de commented Oct 28, 2019

Thanks a lot for your contribution. That's squashed, merged, polished, and backported now.

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

Labels

None yet

3 participants