Skip to content

Conversation

infvg
Copy link
Contributor

@infvg infvg commented Oct 9, 2025

Description

The thrift implementation for the tpcds connector can be generalized so that it can be reused.

Motivation and Context

We can use this new interface in other connectors to add support for thrift serialization

Impact

None

Test Plan

The current thrift tpcds UTs

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.

Release Notes

== NO RELEASE NOTE ==

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Oct 9, 2025
Copy link
Contributor

sourcery-ai bot commented Oct 9, 2025

Reviewer's Guide

This PR extracts and centralizes Thrift serialization logic into a new reusable connector toolkit module, extends SPI and server components to support ColumnHandle codecs, and updates the TPCDS connector and project poms to integrate the new toolkit.

Class diagram for new ThriftCodecProvider and GenericThriftCodec

classDiagram
    class ThriftCodecProvider {
        -ThriftCodecManager thriftCodecManager
        -Optional<Type> connectorSplitType
        -Optional<Type> connectorTransactionHandle
        -Optional<Type> connectorTableLayoutHandle
        -Optional<Type> connectorTableHandle
        -Optional<Type> connectorOutputTableHandle
        -Optional<Type> connectorInsertTableHandle
        -Optional<Type> connectorDeleteTableHandle
        -Optional<Type> connectorColumnHandle
        +getConnectorSplitCodec()
        +getConnectorTransactionHandleCodec()
        +getConnectorTableLayoutHandleCodec()
        +getConnectorTableHandleCodec()
        +getConnectorOutputTableHandleCodec()
        +getConnectorInsertTableHandleCodec()
        +getConnectorDeleteTableHandleCodec()
        +getColumnHandleCodec()
        +getThriftCodecManager()
    }
    class ThriftCodecProvider.Builder {
        -ThriftCodecManager thriftCodecManager
        -Optional<Type> connectorSplitType
        -Optional<Type> connectorTransactionHandle
        -Optional<Type> connectorTableLayoutHandle
        -Optional<Type> connectorTableHandle
        -Optional<Type> connectorOutputTableHandle
        -Optional<Type> connectorInsertTableHandle
        -Optional<Type> connectorDeleteTableHandle
        -Optional<Type> connectorColumnHandle
        +setThriftCodecManager()
        +setConnectorSplitType()
        +setConnectorTransactionHandle()
        +setConnectorTableLayoutHandle()
        +setConnectorTableHandle()
        +setConnectorOutputTableHandle()
        +setConnectorInsertTableHandle()
        +setConnectorDeleteTableHandle()
        +setConnectorColumnHandle()
        +build()
    }
    ThriftCodecProvider.Builder --> ThriftCodecProvider
    ThriftCodecProvider --> GenericThriftCodec
    class GenericThriftCodec<T> {
        -ThriftCodec<T> thriftCodec
        +serialize(T value)
        +deserialize(byte[] bytes)
    }
    GenericThriftCodec <|.. ConnectorCodec
    ThriftCodecProvider ..> ThriftCodecManager
    GenericThriftCodec ..> ThriftCodecManager
Loading

Class diagram for updated ConnectorCodecManager and ConnectorCodecProvider

classDiagram
    class ConnectorCodecManager {
        -Map<String, ConnectorCodecProvider> connectorCodecProviders
        +getTableHandleCodec(String connectorId)
        +getColumnHandleCodec(String connectorId)
    }
    class ConnectorCodecProvider {
        +getConnectorTableHandleCodec()
        +getColumnHandleCodec()
    }
    ConnectorCodecManager --> ConnectorCodecProvider
Loading

File-Level Changes

Change Details Files
Introduce new generic thrift connector toolkit module
  • Create presto-thrift-connector-toolkit module with its pom
  • Add ThriftCodecProvider with Builder for generic connector codec registration
  • Implement GenericThriftCodec for common serialize/deserialize logic
  • Move ThriftCodecUtils into the new toolkit
presto-thrift-connector-toolkit/pom.xml
presto-thrift-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/ThriftCodecProvider.java
presto-thrift-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/GenericThriftCodec.java
presto-thrift-connector-toolkit/src/main/java/com/facebook/presto/thrift/codec/ThriftCodecUtils.java
Add ColumnHandle support to connector SPI and manager
  • Define getColumnHandleCodec() default in ConnectorCodecProvider
  • Expose getColumnHandleCodec() in ConnectorCodecManager
presto-spi/src/main/java/com/facebook/presto/spi/connector/ConnectorCodecProvider.java
presto-main-base/src/main/java/com/facebook/presto/connector/ConnectorCodecManager.java
Register ColumnHandleThriftCodec in server and tests
  • Bind ColumnHandleThriftCodec and JSON codec in HandleThriftModule
  • Add ColumnHandleThriftCodec binding in TestHttpRemoteTask modules
presto-main-base/src/main/java/com/facebook/presto/server/thrift/HandleThriftModule.java
presto-main/src/test/java/com/facebook/presto/server/remotetask/TestHttpRemoteTask.java
presto-main/src/test/java/com/facebook/presto/server/remotetask/TestHttpRemoteTaskWithEventLoop.java
presto-main-base/src/main/java/com/facebook/presto/server/thrift/ColumnHandleThriftCodec.java
Refactor TPCDS connector to leverage generic toolkit
  • Replace TpcdsCodecProvider instantiation with ThriftCodecProvider.Builder
  • Update presto-tpcds pom to depend on new toolkit artifact
  • Remove old TPCDS-specific thrift codec classes
presto-tpcds/src/main/java/com/facebook/presto/tpcds/TpcdsConnectorFactory.java
presto-tpcds/pom.xml
presto-tpcds/src/main/java/com/facebook/presto/tpcds/thrift/TpcdsCodecProvider.java
presto-tpcds/src/main/java/com/facebook/presto/tpcds/thrift/TpcdsSplitCodec.java
presto-tpcds/src/main/java/com/facebook/presto/tpcds/thrift/TpcdsTableHandleCodec.java
presto-tpcds/src/main/java/com/facebook/presto/tpcds/thrift/TpcdsTableLayoutHandleCodec.java
presto-tpcds/src/main/java/com/facebook/presto/tpcds/thrift/TpcdsTransactionHandleCodec.java
Update project modules and dependency management
  • Add presto-thrift-connector-toolkit to root modules list
  • Declare toolkit in dependencyManagement for testing
  • Swap drift-codec/protocol deps to new toolkit in TPCDS pom
pom.xml
presto-tpcds/pom.xml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@infvg infvg force-pushed the thrift-tpcds-rework branch from d88dfc6 to e84d692 Compare October 9, 2025 07:32
@infvg infvg marked this pull request as ready for review October 9, 2025 07:33
@infvg infvg requested review from a team as code owners October 9, 2025 07:33
@prestodb-ci prestodb-ci requested review from a team, ShahimSharafudeen and NivinCS and removed request for a team October 9, 2025 07:33
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Replace the assert (byteBuffer.position() == 0) in ColumnHandleThriftCodec with explicit validation and error handling, since assertions may be disabled in production.
  • Consider tightening GenericThriftCodec’s constructor to accept Class directly instead of a generic Type to eliminate the runtime cast and provide a clearer API.
  • ThriftCodecProvider.Builder exposes one setter per handle type; if more codecs are needed in the future consider consolidating into a registration map or varargs-based API to reduce boilerplate.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Replace the `assert (byteBuffer.position() == 0)` in ColumnHandleThriftCodec with explicit validation and error handling, since assertions may be disabled in production.
- Consider tightening GenericThriftCodec’s constructor to accept Class<T> directly instead of a generic Type to eliminate the runtime cast and provide a clearer API.
- ThriftCodecProvider.Builder exposes one setter per handle type; if more codecs are needed in the future consider consolidating into a registration map or varargs-based API to reduce boilerplate.

## Individual Comments

### Comment 1
<location> `presto-main-base/src/main/java/com/facebook/presto/server/thrift/ColumnHandleThriftCodec.java:63-65` </location>
<code_context>
+    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);
+    }
</code_context>

<issue_to_address>
**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.
</issue_to_address>

### Comment 2
<location> `presto-main-base/src/main/java/com/facebook/presto/server/thrift/ColumnHandleThriftCodec.java:74` </location>
<code_context>
+            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))));
+    }
+
</code_context>

<issue_to_address>
**suggestion:** Exception message could be more informative.

Consider including connectorId and value type in the exception to improve debugging.

```suggestion
        writer.writeBinary(ByteBuffer.wrap(
            connectorCodecManager.getColumnHandleCodec(connectorId)
                .map(codec -> codec.serialize(value))
                .orElseThrow(() -> new IllegalArgumentException(
                    String.format(
                        "Can not serialize value '%s' of type '%s' for connectorId '%s'",
                        value,
                        value.getClass().getName(),
                        connectorId
                    )
                ))
        ));
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +63 to +65
ByteBuffer byteBuffer = reader.readBinary();
assert (byteBuffer.position() == 0);
byte[] bytes = byteBuffer.array();
Copy link
Contributor

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.

@infvg infvg force-pushed the thrift-tpcds-rework branch 2 times, most recently from 91a16e8 to 47af2d5 Compare October 9, 2025 08:08
@infvg infvg force-pushed the thrift-tpcds-rework branch 2 times, most recently from 47af2d5 to 40d9829 Compare October 9, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from:IBM PR from IBM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants