-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(connector): Add support for custom connector-provided serialization codecs #26257
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ec6bf35
Add support for custom connector-provided serialization codecs
tdcmeehan d345328
Address code review comments, fix test cases
tdcmeehan 3df11c7
Add licenses, fix features config
tdcmeehan a17887f
Add a test case for mixed JSON/binary
tdcmeehan d1b1de4
Simplify test
tdcmeehan 43e73d9
Fix up test
tdcmeehan 1c881cc
Fix dependency
tdcmeehan cdb5321
Address review feedback
tdcmeehan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
121 changes: 121 additions & 0 deletions
121
presto-main-base/src/main/java/com/facebook/presto/metadata/CodecDeserializer.java
This file contains hidden or 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,121 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.facebook.presto.metadata; | ||
|
|
||
| import com.facebook.presto.spi.ConnectorCodec; | ||
| import com.facebook.presto.spi.ConnectorId; | ||
| import com.fasterxml.jackson.core.JsonParser; | ||
| import com.fasterxml.jackson.core.JsonToken; | ||
| import com.fasterxml.jackson.core.TreeNode; | ||
| import com.fasterxml.jackson.databind.DeserializationContext; | ||
| import com.fasterxml.jackson.databind.JsonDeserializer; | ||
| import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| class CodecDeserializer<T> | ||
| extends JsonDeserializer<T> | ||
| { | ||
| private final Function<String, Class<? extends T>> classResolver; | ||
| private final Function<ConnectorId, Optional<ConnectorCodec<T>>> codecExtractor; | ||
| private final String typePropertyName; | ||
| private final String dataPropertyName; | ||
|
|
||
| public CodecDeserializer( | ||
| String typePropertyName, | ||
| String dataPropertyName, | ||
| Function<ConnectorId, Optional<ConnectorCodec<T>>> codecExtractor, | ||
| Function<String, Class<? extends T>> classResolver) | ||
| { | ||
| this.classResolver = requireNonNull(classResolver, "classResolver is null"); | ||
| this.codecExtractor = requireNonNull(codecExtractor, "codecExtractor is null"); | ||
| this.typePropertyName = requireNonNull(typePropertyName, "typePropertyName is null"); | ||
| this.dataPropertyName = requireNonNull(dataPropertyName, "dataPropertyName is null"); | ||
| } | ||
|
|
||
| @Override | ||
| public T deserialize(JsonParser parser, DeserializationContext context) | ||
| throws IOException | ||
| { | ||
| if (parser.getCurrentToken() == JsonToken.VALUE_NULL) { | ||
| return null; | ||
| } | ||
|
|
||
| if (parser.getCurrentToken() != JsonToken.START_OBJECT) { | ||
| throw new IOException("Expected START_OBJECT, got " + parser.getCurrentToken()); | ||
| } | ||
|
|
||
| // Parse the JSON tree | ||
| TreeNode tree = parser.readValueAsTree(); | ||
|
|
||
| if (tree instanceof ObjectNode) { | ||
| ObjectNode node = (ObjectNode) tree; | ||
|
|
||
| // Get the @type field | ||
| if (!node.has(typePropertyName)) { | ||
| throw new IOException("Missing " + typePropertyName + " field"); | ||
| } | ||
| String connectorIdString = node.get(typePropertyName).asText(); | ||
| // Check if @data field is present (binary serialization) | ||
| if (node.has(dataPropertyName)) { | ||
| // Binary data is present, we need a codec to deserialize it | ||
| // Special handling for internal handles like "$remote" | ||
| if (!connectorIdString.startsWith("$")) { | ||
| ConnectorId connectorId = new ConnectorId(connectorIdString); | ||
| Optional<ConnectorCodec<T>> codec = codecExtractor.apply(connectorId); | ||
| if (codec.isPresent()) { | ||
| String base64Data = node.get(dataPropertyName).asText(); | ||
| byte[] data = Base64.getDecoder().decode(base64Data); | ||
| return codec.get().deserialize(data); | ||
| } | ||
| } | ||
| // @data field present but no codec available or internal handle | ||
| throw new IOException("Type " + connectorIdString + " has binary data (customSerializedValue field) but no codec available to deserialize it"); | ||
| } | ||
|
|
||
| // No @data field - use standard JSON deserialization | ||
| Class<? extends T> handleClass = classResolver.apply(connectorIdString); | ||
|
|
||
| // Remove the @type field and deserialize the remaining content | ||
| node.remove(typePropertyName); | ||
| return context.readTreeAsValue(node, handleClass); | ||
| } | ||
|
|
||
| throw new IOException("Unable to deserialize"); | ||
| } | ||
|
|
||
| @Override | ||
| public T deserializeWithType(JsonParser p, DeserializationContext ctxt, | ||
| TypeDeserializer typeDeserializer) | ||
| throws IOException | ||
| { | ||
| // We handle the type ourselves | ||
| return deserialize(p, ctxt); | ||
| } | ||
|
|
||
| @Override | ||
| public T deserializeWithType(JsonParser p, DeserializationContext ctxt, | ||
| TypeDeserializer typeDeserializer, T intoValue) | ||
| throws IOException | ||
| { | ||
| // We handle the type ourselves | ||
| return deserialize(p, ctxt); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.