-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Destinations V2: clean up AirbyteType code (#28430)
* general cleanup - move stuff around, add more comments * guarantee `getAirbyteProtocolType` won't handle array values for `type` * rename OneOf to Union * simplify union ordering logic * update testChooseUnion * fix docs typos * Automated Commit - Format and Process Resources Changes * address comments * Automated Commit - Format and Process Resources Changes --------- Co-authored-by: cynthiaxyin <cynthiaxyin@users.noreply.github.com>
- Loading branch information
1 parent
225bfc4
commit 7e4797d
Showing
15 changed files
with
504 additions
and
564 deletions.
There are no files selected for viewing
119 changes: 59 additions & 60 deletions
119
...java/io/airbyte/integrations/base/destination/typing_deduping/BaseTypingDedupingTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
107 changes: 54 additions & 53 deletions
107
.../src/main/java/io/airbyte/integrations/base/destination/typing_deduping/RecordDiffer.java
Large diffs are not rendered by default.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
...in/java/io/airbyte/integrations/base/destination/typing_deduping/AirbyteProtocolType.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,78 @@ | ||
/* | ||
* Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.integrations.base.destination.typing_deduping; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* Protocol types are ordered by precedence in the case of a Union that contains multiple types. | ||
* Priority is given to wider scope types over narrower ones. (Note that because of dedup logic in | ||
* {@link AirbyteType#fromJsonSchema(JsonNode)}, at most one string or date/time type can exist in a | ||
* Union.) | ||
*/ | ||
public enum AirbyteProtocolType implements AirbyteType { | ||
|
||
STRING, | ||
DATE, | ||
TIME_WITHOUT_TIMEZONE, | ||
TIME_WITH_TIMEZONE, | ||
TIMESTAMP_WITHOUT_TIMEZONE, | ||
TIMESTAMP_WITH_TIMEZONE, | ||
NUMBER, | ||
INTEGER, | ||
BOOLEAN, | ||
UNKNOWN; | ||
|
||
private static AirbyteProtocolType matches(final String type) { | ||
try { | ||
return AirbyteProtocolType.valueOf(type.toUpperCase()); | ||
} catch (final IllegalArgumentException e) { | ||
LOGGER.error(String.format("Could not find matching AirbyteProtocolType for \"%s\": %s", type, e)); | ||
return UNKNOWN; | ||
} | ||
} | ||
|
||
// Extracts the appropriate protocol type from the representative JSON | ||
protected static AirbyteProtocolType fromJson(final JsonNode node) { | ||
// JSON could be a string (ex: "number") | ||
if (node.isTextual()) { | ||
return matches(node.asText()); | ||
} | ||
|
||
// or, JSON could be a node with fields | ||
final JsonNode propertyType = node.get("type"); | ||
final JsonNode airbyteType = node.get("airbyte_type"); | ||
final JsonNode format = node.get("format"); | ||
|
||
if (AirbyteType.nodeMatches(propertyType, "boolean")) { | ||
return BOOLEAN; | ||
} else if (AirbyteType.nodeMatches(propertyType, "integer")) { | ||
return INTEGER; | ||
} else if (AirbyteType.nodeMatches(propertyType, "number")) { | ||
return AirbyteType.nodeMatches(airbyteType, "integer") ? INTEGER : NUMBER; | ||
} else if (AirbyteType.nodeMatches(propertyType, "string")) { | ||
if (AirbyteType.nodeMatches(format, "date")) { | ||
return DATE; | ||
} else if (AirbyteType.nodeMatches(format, "time")) { | ||
if (AirbyteType.nodeMatches(airbyteType, "time_without_timezone")) { | ||
return TIME_WITHOUT_TIMEZONE; | ||
} else if (AirbyteType.nodeMatches(airbyteType, "time_with_timezone")) { | ||
return TIME_WITH_TIMEZONE; | ||
} | ||
} else if (AirbyteType.nodeMatches(format, "date-time")) { | ||
if (AirbyteType.nodeMatches(airbyteType, "timestamp_without_timezone")) { | ||
return TIMESTAMP_WITHOUT_TIMEZONE; | ||
} else if (airbyteType == null || AirbyteType.nodeMatches(airbyteType, "timestamp_with_timezone")) { | ||
return TIMESTAMP_WITH_TIMEZONE; | ||
} | ||
} else { | ||
return STRING; | ||
} | ||
} | ||
|
||
return UNKNOWN; | ||
} | ||
|
||
} |
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
Oops, something went wrong.