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

SNOW-1032874: Fix an Overflow Issue for Integer-store Timestamp #673

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -728,20 +728,27 @@ static BigInteger validateAndParseTime(
* @throws NumberFormatException If the input in not a valid long
*/
private static Instant parseInstantGuessScale(String input) {
long epochNanos;
long val = Long.parseLong(input);

if (val > -SECONDS_LIMIT_FOR_EPOCH && val < SECONDS_LIMIT_FOR_EPOCH) {
epochNanos = val * Power10.intTable[9];
} else if (val > -MILLISECONDS_LIMIT_FOR_EPOCH && val < MILLISECONDS_LIMIT_FOR_EPOCH) {
epochNanos = val * Power10.intTable[6];
} else if (val > -MICROSECONDS_LIMIT_FOR_EPOCH && val < MICROSECONDS_LIMIT_FOR_EPOCH) {
epochNanos = val * Power10.intTable[3];
} else {
epochNanos = val;
BigInteger epochNanos;
try {
long val = Long.parseLong(input);

if (val > -SECONDS_LIMIT_FOR_EPOCH && val < SECONDS_LIMIT_FOR_EPOCH) {
epochNanos = BigInteger.valueOf(val).multiply(BigInteger.valueOf(Power10.intTable[9]));
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, use Power10.sb16Table[9]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

} else if (val > -MILLISECONDS_LIMIT_FOR_EPOCH && val < MILLISECONDS_LIMIT_FOR_EPOCH) {
epochNanos = BigInteger.valueOf(val).multiply(BigInteger.valueOf(Power10.intTable[6]));
} else if (val > -MICROSECONDS_LIMIT_FOR_EPOCH && val < MICROSECONDS_LIMIT_FOR_EPOCH) {
epochNanos = BigInteger.valueOf(val).multiply(BigInteger.valueOf(Power10.intTable[3]));
} else {
epochNanos = BigInteger.valueOf(val);
}
} catch (NumberFormatException e) {
// The input is bigger than max long value, treat it as nano-seconds directly
epochNanos = new BigInteger(input);
}

return Instant.ofEpochSecond(
epochNanos / Power10.intTable[9], epochNanos % Power10.intTable[9]);
epochNanos.divide(BigInteger.valueOf(Power10.intTable[9])).longValue(),
epochNanos.remainder(BigInteger.valueOf(Power10.intTable[9])).longValue());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BigInteger toBinary(boolean includeTimezone) {
}

/** Get epoch in seconds */
public long getEpoch() {
public long getEpochSecond() {
return epoch;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -251,20 +253,47 @@ public void testValidateAndParseTime() {
}

@Test
public void testValidateAndParseTimestamp() {
public void testValidateAndParseTimestamp() throws ParseException {
TimestampWrapper wrapper =
DataValidationUtil.validateAndParseTimestamp(
"COL", "2021-01-01T01:00:00.123+01:00", 4, UTC, false, 0);
assertEquals(1609459200, wrapper.getEpoch());
assertEquals(1609459200, wrapper.getEpochSecond());
assertEquals(123000000, wrapper.getFraction());
assertEquals(3600, wrapper.getTimezoneOffsetSeconds());
assertEquals(1500, wrapper.getTimeZoneIndex());

wrapper = validateAndParseTimestamp("COL", " 2021-01-01T01:00:00.123 \t\n", 9, UTC, true, 0);
Assert.assertEquals(1609462800, wrapper.getEpoch());
Assert.assertEquals(1609462800, wrapper.getEpochSecond());
Assert.assertEquals(123000000, wrapper.getFraction());
Assert.assertEquals(new BigInteger("1609462800123000000"), wrapper.toBinary(false));

// Test integer-stored time and scale guessing
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we also need integration test coverage, which would test JDBC compatibility. We have a bunch of IT tests for scale guessing in DateTimeIT.java, just not for these edge cases. Could you add tests along the lines of these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

assertEquals(
BigInteger.valueOf(df.parse("1971-01-01 00:00:00.001").getTime())
.multiply(BigInteger.valueOf(1000000)),
validateAndParseTimestamp("COL", "31536000001", 9, UTC, true, 0).toBinary(false));

assertEquals(
BigInteger.valueOf(df.parse("2969-05-02 23:59:59.999").getTime())
.multiply(BigInteger.valueOf(1000000)),
validateAndParseTimestamp("COL", "31535999999999", 9, UTC, true, 0).toBinary(false));

assertEquals(
BigInteger.valueOf(df.parse("1971-01-01 00:00:00.000").getTime())
.multiply(BigInteger.valueOf(1000000)),
validateAndParseTimestamp("COL", "31536000000000", 9, UTC, true, 0).toBinary(false));

assertEquals(
BigInteger.valueOf(df.parse("2969-05-02 23:59:59.999").getTime())
.multiply(BigInteger.valueOf(1000000)),
validateAndParseTimestamp("COL", "31535999999999", 9, UTC, true, 0).toBinary(false));

assertEquals(
BigInteger.valueOf(df.parse("1971-01-01 00:00:00.000").getTime())
.multiply(BigInteger.valueOf(1000000)),
validateAndParseTimestamp("COL", "31536000000000000", 9, UTC, true, 0).toBinary(false));

// Time input is not supported
expectError(
ErrorCode.INVALID_VALUE_ROW,
Expand Down
Loading