forked from raystack/depot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: check timestamp only for partition column * fix: add timestamp check
- Loading branch information
Showing
4 changed files
with
108 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ plugins { | |
} | ||
|
||
group 'com.gotocompany' | ||
version '0.4.8' | ||
version '0.4.9' | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/gotocompany/depot/bigquery/storage/proto/TimeStampUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.gotocompany.depot.bigquery.storage.proto; | ||
|
||
import com.google.protobuf.Descriptors; | ||
import com.gotocompany.depot.config.BigQuerySinkConfig; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class TimeStampUtils { | ||
private static final long FIVE_YEARS_DAYS = 1825; | ||
private static final long ONE_YEAR_DAYS = 365; | ||
private static final Instant MIN_TIMESTAMP = Instant.parse("0001-01-01T00:00:00Z"); | ||
private static final Instant MAX_TIMESTAMP = Instant.parse("9999-12-31T23:59:59.999999Z"); | ||
|
||
public static long getBQInstant(Instant instant, Descriptors.FieldDescriptor fieldDescriptor, boolean isTopLevel, BigQuerySinkConfig config) { | ||
// Timestamp should be in microseconds | ||
long timeStamp = TimeUnit.SECONDS.toMicros(instant.getEpochSecond()) + TimeUnit.NANOSECONDS.toMicros(instant.getNano()); | ||
// Partition column is always top level | ||
if (isTopLevel && fieldDescriptor.getName().equals(config.getTablePartitionKey())) { | ||
Instant currentInstant = Instant.now(); | ||
boolean isValid; | ||
boolean isPastInstant = currentInstant.isAfter(instant); | ||
if (isPastInstant) { | ||
Instant fiveYearPast = currentInstant.minusMillis(TimeUnit.DAYS.toMillis(FIVE_YEARS_DAYS)); | ||
isValid = fiveYearPast.isBefore(instant); | ||
} else { | ||
Instant oneYearFuture = currentInstant.plusMillis(TimeUnit.DAYS.toMillis(ONE_YEAR_DAYS)); | ||
isValid = oneYearFuture.isAfter(instant); | ||
|
||
} | ||
if (!isValid) { | ||
throw new UnsupportedOperationException(instant + " for field " | ||
+ fieldDescriptor.getFullName() + " is outside the allowed bounds. " | ||
+ "You can only stream to date range within 1825 days in the past " | ||
+ "and 366 days in the future relative to the current date."); | ||
} | ||
return timeStamp; | ||
} else { | ||
// other timestamps should be in the limit specifies by BQ | ||
if (instant.isAfter(MIN_TIMESTAMP) && instant.isBefore(MAX_TIMESTAMP)) { | ||
return timeStamp; | ||
} else { | ||
throw new UnsupportedOperationException(instant | ||
+ " for field " | ||
+ fieldDescriptor.getFullName() | ||
+ " is outside the allowed bounds in BQ."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters