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

Fix #1647: support negative CqlDuration/duration values #1648

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ public static String toISO8601Duration(CqlDuration value) {
int days = value.getDays();
long nanoSeconds = value.getNanoseconds();

final StringBuilder sb = new StringBuilder("P");
// Negative value? To detect see if any value negative (all must be 0 or negative
// if one is -- but `CqlDuration` ensures that invariant). Bit silly there's no
// `CqlDuration.isNegative()` (or `CqlDuration.negate()` to create opposite value)
tatu-at-datastax marked this conversation as resolved.
Show resolved Hide resolved
// or such but it is what it is.
final StringBuilder sb;
boolean negative = (months < 0) || (days < 0) || (nanoSeconds < 0L);
if (negative) {
// and if we do have negative value, we need to prepend minus sign, negate all parts
sb = new StringBuilder("-P");
months = -months;
days = -days;
nanoSeconds = -nanoSeconds;
} else {
sb = new StringBuilder("P");
}

// Do we have date part?
if (months > 0 || days > 0) { // yes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public abstract class JSONCodecs {
GenericType.STRING,
DataTypes.DURATION,
// CqlDuration.from() accepts 2 formats; ISO-8601 ("P1H30M") and "1h30m" (Cassandra
// compact) format
// compact) format. Note: negative values (preceding "-") also accepted
JSONCodec.ToCQL.safeFromString(CqlDuration::from),
// But since we must produce ISO-8601, need custom JSON serialization
(objectMapper, fromCQLType, value) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,35 @@ void insertEJSONTimestampValue() {
.hasJSONField("data.document", outputJSON);
}

@Test
void insertValidNegativeDurationValue() {
assertTableCommand(keyspaceName, TABLE_WITH_DATETIME_COLUMNS)
.templated()
.insertOne(
"""
{
"id": "%s",
"durationValue": "%s"
}
"""
.formatted("datetimeNegDuration", "-8h10m"))
.wasSuccessful();
assertTableCommand(keyspaceName, TABLE_WITH_DATETIME_COLUMNS)
.postFindOne(
"""
{ "filter": { "id": "datetimeNegDuration" },
"projection": { "durationValue": 1 }
}
""")
.wasSuccessful()
.hasJSONField(
"data.document",
"""
{ "durationValue": "%s" }
"""
.formatted("-PT8H10M"));
}

@Test
void failOnInvalidDateValue() {
assertTableCommand(keyspaceName, TABLE_WITH_DATETIME_COLUMNS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ private static Stream<Arguments> validCodecToCQLTestCasesDatetime() {
DataTypes.DURATION,
TEST_DATA.DURATION_VALID_STR_ISO8601,
CqlDuration.from(TEST_DATA.DURATION_VALID_STR_ISO8601)),
// Should also support negative duration values:
Arguments.of(
DataTypes.DURATION,
"-" + TEST_DATA.DURATION_VALID_STR_CASS,
CqlDuration.from("-" + TEST_DATA.DURATION_VALID_STR_CASS)),
Arguments.of(
DataTypes.DURATION,
"-" + TEST_DATA.DURATION_VALID_STR_ISO8601,
CqlDuration.from("-" + TEST_DATA.DURATION_VALID_STR_ISO8601)),
Arguments.of(
DataTypes.TIME, TEST_DATA.TIME_VALID_STR, LocalTime.parse(TEST_DATA.TIME_VALID_STR)),
Arguments.of(
Expand Down Expand Up @@ -578,6 +587,11 @@ private static Stream<Arguments> validCodecToJSONTestCasesDatetime() {
DataTypes.DURATION,
CqlDuration.from(TEST_DATA.DURATION_VALID_STR_CASS),
JSONS.textNode(TEST_DATA.DURATION_VALID_STR_ISO8601)),
// And then negative variant of same duration
Arguments.of(
DataTypes.DURATION,
CqlDuration.from("-" + TEST_DATA.DURATION_VALID_STR_CASS),
JSONS.textNode("-" + TEST_DATA.DURATION_VALID_STR_ISO8601)),
Arguments.of(
DataTypes.TIME,
LocalTime.parse(TEST_DATA.TIME_VALID_STR),
Expand Down