Skip to content
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 @@ -234,7 +234,7 @@ static Instant parseEpochDecimalToInstant(String epochDecimal) {
return null;
}

BigDecimal bd = new BigDecimal(epochDecimal).setScale(9, RoundingMode.DOWN);
BigDecimal bd = parseScaledEpochDecimal(epochDecimal, 9);
BigDecimal secondsBd = bd.setScale(0, RoundingMode.FLOOR);
BigDecimal fractionBd = bd.subtract(secondsBd);
int nanos = fractionBd.multiply(NANOS_PER_SECOND).intValue();
Expand All @@ -260,7 +260,7 @@ static String formatTimestampStringFromEpochDecimal(
int scale = enableTimestampPicos ? 12 : 6;
BigDecimal bd;
try {
bd = new BigDecimal(epochDecimal).setScale(scale, RoundingMode.DOWN);
bd = parseScaledEpochDecimal(epochDecimal, scale);
} catch (NumberFormatException e) {
try {
return formatTimestampStringFromIso(epochDecimal, enableTimestampPicos);
Expand All @@ -275,12 +275,34 @@ static String formatTimestampStringFromEpochDecimal(
BigDecimal multiplier = scale == 12 ? PICOS_PER_SECOND : MICROS_PER_SECOND;
long fractionVal = fractionBd.multiply(multiplier).setScale(0, RoundingMode.DOWN).longValue();

LocalDateTime dt = LocalDateTime.ofEpochSecond(epochSeconds, 0, ZoneOffset.UTC);
LocalDateTime dt;
try {
dt = LocalDateTime.ofEpochSecond(epochSeconds, 0, ZoneOffset.UTC);
} catch (DateTimeException e) {
throw new BigQueryJdbcException("Invalid timestamp value: " + epochDecimal, e);
}
StringBuilder sb = formatDateTimeBase(dt, scale);
appendPadded(sb, fractionVal, scale);
return sb.toString();
}

private static BigDecimal parseScaledEpochDecimal(String epochDecimal, int scale) {
if (epochDecimal.length() > 128) {
throw new NumberFormatException("Epoch timestamp string too long");
Comment thread
Neenu1995 marked this conversation as resolved.
Resolved
}
BigDecimal raw = new BigDecimal(epochDecimal);
if (raw.signum() == 0) {
return BigDecimal.ZERO.setScale(scale);
}
long integerDigits = (long) raw.precision() - (long) raw.scale();
if (integerDigits > 17) {
throw new NumberFormatException("Epoch timestamp out of range");
}
return integerDigits <= -scale
Comment thread
Neenu1995 marked this conversation as resolved.
Resolved
? BigDecimal.ZERO.setScale(scale)
: raw.setScale(scale, RoundingMode.DOWN);
}

/**
* Normalizes an ISO-8601 or civil timestamp string into a standard UTC JDBC timestamp string
* formatted with 6 or 12 fractional digits according to {@code enableTimestampPicos}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.cloud.bigquery.jdbc.utils.TestUtilities.rangeField;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;

import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldValue;
Expand All @@ -31,12 +32,14 @@
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Collections;
import java.util.TimeZone;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -388,6 +391,77 @@ public void testParseEpochDecimalToInstantInvalid() {
() -> BigQueryTemporalUtility.parseEpochDecimalToInstant("invalid_epoch"));
}

@ParameterizedTest
@ValueSource(
strings = {
"1e100000000",
"-1e100000000",
"1e2147483647",
"-1e2147483647",
"1e2147483648",
"-1e2147483648",
"1e18",
"-1e18",
"5e16",
"-5e16"
})
public void testExtremePositiveExponentEpochDecimalsFailFast(String input) {
assertTimeoutPreemptively(
Duration.ofSeconds(1),
() -> {
assertThrows(
BigQueryJdbcException.class,
() ->
BigQueryTypeRegistry.convert(
input, StandardSQLTypeName.STRING, Timestamp.class, null));
assertThrows(
BigQueryJdbcException.class,
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, false));
assertThrows(
BigQueryJdbcException.class,
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, true));
});
}

@ParameterizedTest
@ValueSource(
strings = {
"0e100000000",
"-0e100000000",
"1e-100000000",
"-1e-100000000",
"12345678901234567890e-100000000",
"-12345678901234567890e-100000000"
})
public void testExtremeSubUnitAndZeroExponentsTruncateInConstantTime(String input)
throws Exception {
assertTimeoutPreemptively(
Duration.ofSeconds(1),
() -> {
assertThat(BigQueryTemporalUtility.parseEpochDecimalToInstant(input))
.isEqualTo(Instant.EPOCH);
assertThat(BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, false))
.isEqualTo("1970-01-01 00:00:00.000000");
assertThat(BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(input, true))
.isEqualTo("1970-01-01 00:00:00.000000000000");
});
}

@Test
public void testOverlongEpochDecimalStringRejectedFast() {
String overlong = String.join("", Collections.nCopies(200, "0")) + ".5";
assertTimeoutPreemptively(
Duration.ofSeconds(1),
() -> {
assertThrows(
NumberFormatException.class,
() -> BigQueryTemporalUtility.parseEpochDecimalToInstant(overlong));
assertThrows(
BigQueryJdbcException.class,
() -> BigQueryTemporalUtility.formatTimestampStringFromEpochDecimal(overlong, false));
});
}

@Test
public void testFormatTimestampStringFromEpochDecimalWithPicosEnabled()
throws BigQueryJdbcException {
Expand Down
Loading