Skip to content

refactor(protocol): Add new generic thrift toolkit module for connectors#26259

Merged
tdcmeehan merged 1 commit intoprestodb:masterfrom
infvg:thrift-tpcds-rework
Mar 27, 2026
Merged

refactor(protocol): Add new generic thrift toolkit module for connectors#26259
tdcmeehan merged 1 commit intoprestodb:masterfrom
infvg:thrift-tpcds-rework

Conversation

@infvg
Copy link
Copy Markdown
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
@sourcery-ai
Copy link
Copy Markdown
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, NivinCS and ShahimSharafudeen and removed request for a team October 9, 2025 07:33
Copy link
Copy Markdown
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
Copy Markdown
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 5 times, most recently from 034dd56 to 8069b21 Compare October 12, 2025 08:32
@tdcmeehan tdcmeehan requested a review from shangm2 October 12, 2025 09:44
@infvg infvg force-pushed the thrift-tpcds-rework branch 3 times, most recently from 29258b8 to 41830d5 Compare October 19, 2025 14:26
throws Exception
{
ByteBuffer byteBuffer = reader.readBinary();
assert (byteBuffer.position() == 0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's remove assert keyword and use checkArgument

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed

ByteBuffer byteBuffer = reader.readBinary();
assert (byteBuffer.position() == 0);
byte[] bytes = byteBuffer.array();
return connectorCodecManager.getColumnHandleCodec(connectorId).map(codec -> codec.deserialize(bytes)).orElse(null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I believe this should just throw to fail fast.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a throw

</parent>

<artifactId>presto-thrift-connector-toolkit</artifactId>
<name>presto-thrift-toolkit</name>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
<name>presto-thrift-toolkit</name>
<name>presto-thrift-connector-toolkit</name>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@tdcmeehan
Copy link
Copy Markdown
Contributor

How would the C++ side generate the same Thrift IDL?

@tdcmeehan tdcmeehan assigned infvg and unassigned infvg Oct 22, 2025
@infvg infvg force-pushed the thrift-tpcds-rework branch from 41830d5 to 28f281e Compare October 23, 2025 10:27
@infvg infvg requested a review from a team as a code owner October 23, 2025 10:27
@infvg infvg force-pushed the thrift-tpcds-rework branch 2 times, most recently from 61615ed to aa1112d Compare October 23, 2025 11:55
@infvg infvg force-pushed the thrift-tpcds-rework branch from aa1112d to 03bd54c Compare November 19, 2025 12:45
@infvg infvg force-pushed the thrift-tpcds-rework branch from 03bd54c to b385622 Compare December 18, 2025 09:43
@infvg
Copy link
Copy Markdown
Contributor Author

infvg commented Dec 18, 2025

@tdcmeehan I modified the PR - the thrift IDL hasn't changed now since I removed the Column Handle codec so no C++ changes are needed.

@infvg infvg force-pushed the thrift-tpcds-rework branch 2 times, most recently from 963655c to c08f4dc Compare December 18, 2025 09:52
@infvg infvg force-pushed the thrift-tpcds-rework branch 3 times, most recently from a0515fd to 3714272 Compare January 5, 2026 09:16
@infvg infvg force-pushed the thrift-tpcds-rework branch from 3714272 to 63712e0 Compare January 20, 2026 17:28
{
private final ThriftCodec<T> thriftCodec;

public GenericThriftCodec(ThriftCodecManager codecManager, Type javaType, Class<T> expectedType)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just take Class<T> directly instead of Type? The current signature requires callers to pass both javaType and expectedType when they're effectively the same thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

private Optional<Type> connectorOutputTableHandle = Optional.empty();
private Optional<Type> connectorInsertTableHandle = Optional.empty();
private Optional<Type> connectorDeleteTableHandle = Optional.empty();
private Optional<Type> connectorColumnHandle = Optional.empty();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This field is defined but never used - no setter in the Builder and no corresponding getConnectorColumnHandleCodec() method. Can you add it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed the column handle codec since it'll require Prestissimo changes & isn't required yet - removed this reference

<version>0.297-SNAPSHOT</version>
</parent>

<artifactId>presto-thrift-connector-toolkit</artifactId>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we'll be sharing these among modules, could we add some basic unit tests that aren't tied to any particular module?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added tests

return this;
}

public ThriftCodecProvider build()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider validating that thriftCodecManager is set before building - it's required but could be null if the caller forgets to call setThriftCodecManager().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a check

@infvg infvg force-pushed the thrift-tpcds-rework branch 2 times, most recently from a722092 to 1800369 Compare January 21, 2026 14:45
@tdcmeehan
Copy link
Copy Markdown
Contributor

Thanks a lot! Please rebase to pick up the new tests

@infvg infvg force-pushed the thrift-tpcds-rework branch from 1800369 to 1496f17 Compare March 3, 2026 10:07
@steveburnett
Copy link
Copy Markdown
Contributor

Should this change be documented in the Developer Guide section of the Presto documentation? Maybe in a new and different PR, and perhaps Connectors would be a good place for the doc if it doesn't need its own page.

@tdcmeehan tdcmeehan merged commit 9ae347c into prestodb:master Mar 27, 2026
111 of 112 checks passed
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.

4 participants