-
Notifications
You must be signed in to change notification settings - Fork 5.5k
refactor(protocol): Add new generic thrift toolkit module for connectors #26259
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
Open
infvg
wants to merge
1
commit into
prestodb:master
Choose a base branch
from
infvg:thrift-tpcds-rework
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
82 changes: 82 additions & 0 deletions
82
...to-main-base/src/main/java/com/facebook/presto/server/thrift/ColumnHandleThriftCodec.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,82 @@ | ||
/* | ||
* 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.server.thrift; | ||
|
||
import com.facebook.airlift.json.JsonCodec; | ||
import com.facebook.drift.codec.CodecThriftType; | ||
import com.facebook.drift.codec.metadata.ThriftType; | ||
import com.facebook.drift.protocol.TProtocolReader; | ||
import com.facebook.drift.protocol.TProtocolWriter; | ||
import com.facebook.presto.connector.ConnectorCodecManager; | ||
import com.facebook.presto.metadata.HandleResolver; | ||
import com.facebook.presto.spi.ColumnHandle; | ||
|
||
import javax.inject.Inject; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
public class ColumnHandleThriftCodec | ||
extends AbstractTypedThriftCodec<ColumnHandle> | ||
{ | ||
private static final ThriftType THRIFT_TYPE = createThriftType(ColumnHandle.class); | ||
private final ConnectorCodecManager connectorCodecManager; | ||
|
||
@Inject | ||
public ColumnHandleThriftCodec(HandleResolver handleResolver, ConnectorCodecManager connectorCodecManager, JsonCodec<ColumnHandle> jsonCodec) | ||
{ | ||
super(ColumnHandle.class, | ||
requireNonNull(jsonCodec, "jsonCodec is null"), | ||
requireNonNull(handleResolver, "handleResolver is null")::getId, | ||
handleResolver::getColumnHandleClass); | ||
this.connectorCodecManager = requireNonNull(connectorCodecManager, "connectorThriftCodecManager is null"); | ||
} | ||
|
||
@CodecThriftType | ||
public static ThriftType getThriftType() | ||
{ | ||
return THRIFT_TYPE; | ||
} | ||
|
||
@Override | ||
public ThriftType getType() | ||
{ | ||
return THRIFT_TYPE; | ||
} | ||
|
||
@Override | ||
public ColumnHandle readConcreteValue(String connectorId, TProtocolReader reader) | ||
throws Exception | ||
{ | ||
ByteBuffer byteBuffer = reader.readBinary(); | ||
assert (byteBuffer.position() == 0); | ||
byte[] bytes = byteBuffer.array(); | ||
return connectorCodecManager.getColumnHandleCodec(connectorId).map(codec -> codec.deserialize(bytes)).orElse(null); | ||
} | ||
|
||
@Override | ||
public void writeConcreteValue(String connectorId, ColumnHandle value, TProtocolWriter writer) | ||
throws Exception | ||
{ | ||
requireNonNull(value, "value is null"); | ||
writer.writeBinary(ByteBuffer.wrap(connectorCodecManager.getColumnHandleCodec(connectorId).map(codec -> codec.serialize(value)).orElseThrow(() -> new IllegalArgumentException("Can not serialize " + value)))); | ||
} | ||
|
||
@Override | ||
public boolean isThriftCodecAvailable(String connectorId) | ||
{ | ||
return connectorCodecManager.getColumnHandleCodec(connectorId).isPresent(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.facebook.presto</groupId> | ||
<artifactId>presto-root</artifactId> | ||
<version>0.296-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>presto-thrift-connector-toolkit</artifactId> | ||
<name>presto-thrift-toolkit</name> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<air.main.basedir>${project.parent.basedir}</air.main.basedir> | ||
<air.check.skip-modernizer>true</air.check.skip-modernizer> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.facebook.airlift.drift</groupId> | ||
<artifactId>drift-codec</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.facebook.airlift.drift</groupId> | ||
<artifactId>drift-protocol</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.facebook.presto</groupId> | ||
<artifactId>presto-spi</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
73 changes: 73 additions & 0 deletions
73
...-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/GenericThriftCodec.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,73 @@ | ||
/* | ||
* 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.thrift.codec; | ||
|
||
import com.facebook.drift.codec.ThriftCodec; | ||
import com.facebook.drift.codec.ThriftCodecManager; | ||
import com.facebook.drift.protocol.TProtocolException; | ||
import com.facebook.presto.spi.ConnectorCodec; | ||
import com.facebook.presto.spi.PrestoException; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import static com.facebook.presto.spi.StandardErrorCode.INVALID_ARGUMENTS; | ||
import static com.facebook.presto.thrift.codec.ThriftCodecUtils.fromThrift; | ||
import static com.facebook.presto.thrift.codec.ThriftCodecUtils.toThrift; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class GenericThriftCodec<T> | ||
implements ConnectorCodec<T> | ||
{ | ||
private final ThriftCodec<T> thriftCodec; | ||
|
||
public GenericThriftCodec(ThriftCodecManager codecManager, Type javaType, Class<T> expectedType) | ||
{ | ||
requireNonNull(codecManager, "codecManager is null"); | ||
requireNonNull(javaType, "javaType is null"); | ||
requireNonNull(expectedType, "expectedType is null"); | ||
|
||
if (!(javaType instanceof Class<?>)) { | ||
throw new IllegalArgumentException("Expected a Class type for javaType, but got: " + javaType.getTypeName()); | ||
} | ||
|
||
Class<?> clazz = (Class<?>) javaType; | ||
if (!expectedType.isAssignableFrom(clazz)) { | ||
throw new IllegalArgumentException("javaType must be a subclass of " + expectedType.getName() + ", but got: " + clazz.getName()); | ||
} | ||
|
||
this.thriftCodec = (ThriftCodec<T>) codecManager.getCodec(clazz); | ||
} | ||
|
||
@Override | ||
public byte[] serialize(T value) | ||
{ | ||
try { | ||
return toThrift(value, thriftCodec); | ||
} | ||
catch (TProtocolException e) { | ||
throw new PrestoException(INVALID_ARGUMENTS, "Unable to serialize object of type " + value.getClass().getSimpleName(), e); | ||
} | ||
} | ||
|
||
@Override | ||
public T deserialize(byte[] bytes) | ||
{ | ||
try { | ||
return fromThrift(bytes, thriftCodec); | ||
} | ||
catch (TProtocolException e) { | ||
throw new PrestoException(INVALID_ARGUMENTS, "Unable to deserialize bytes to object of expected type", e); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Potential risk with ByteBuffer.array() usage.
Directly accessing byteBuffer.array() may cause issues if the buffer is not array-backed or contains extra data. To ensure correct deserialization, use byteBuffer.get() with byteBuffer.remaining() to extract only the relevant bytes.