Skip to content

Conversation

raunaqmorarka
Copy link
Member

@raunaqmorarka raunaqmorarka commented Oct 15, 2025

Description

Moves appendRawBlockRange to BlockBuilder to make it public.
Used it in Array operators

Additional context and related issues

Release notes

(x) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

## Section
* Fix some things. ({issue}`issuenumber`)

Summary by Sourcery

Expose bulk block appending in BlockBuilder and update array/map builders and related functions to use the new API for more concise block range appends.

New Features:

  • Expose a public bulk appendRawBlockRange API on BlockBuilder

Enhancements:

  • Replace manual loops over ValueBlock with appendRawBlockRange across array/map writers and concat/flatten functions
  • Remove redundant appendRawBlockRange implementation from BlockUtil and eliminate ValueBlock imports

@cla-bot cla-bot bot added the cla-signed label Oct 15, 2025
Copy link

sourcery-ai bot commented Oct 15, 2025

Reviewer's Guide

This PR promotes the internal bulk copy method appendRawBlockRange to a public BlockBuilder API and refactors array and map code to use this API instead of manual loops over ValueBlock.

Class diagram for BlockBuilder and related block types after appendRawBlockRange promotion

classDiagram
class BlockBuilder {
  +newBlockBuilderLike(blockBuilderStatus)
  +appendRawBlockRange(rawBlock, offset, length, blockBuilder)
}
class RunLengthEncodedBlock
class DictionaryBlock
class ValueBlock
BlockBuilder <|.. RunLengthEncodedBlock
BlockBuilder <|.. DictionaryBlock
BlockBuilder <|.. ValueBlock
BlockBuilder o-- "appendRawBlockRange()" Block
RunLengthEncodedBlock <|-- Block
DictionaryBlock <|-- Block
ValueBlock <|-- Block
Loading

Class diagram for ArrayConcatUtils after refactor to use appendRawBlockRange

classDiagram
class ArrayConcatUtils {
  +appendElement(Type, Block, long)
  +appendElement(Type, Block, boolean)
  +appendElement(Type, Block, double)
  +appendElement(Type, Block, Slice)
  +appendElement(Type, Block, Object)
  +prependElement(Type, Slice, Block)
  +prependElement(Type, Object, Block)
  +prependElement(Type, long, Block)
  +prependElement(Type, boolean, Block)
  +prependElement(Type, double, Block)
}
ArrayConcatUtils --> BlockBuilder : uses
ArrayConcatUtils --> Block : uses
ArrayConcatUtils --> Type : uses
ArrayConcatUtils ..> "appendRawBlockRange()" BlockBuilder
Loading

Class diagram for ArrayType and MapType writeObject refactor

classDiagram
class ArrayType {
  +writeObject(BlockBuilder, Object)
}
class MapType {
  +writeObject(BlockBuilder, Object)
}
ArrayType ..> "appendRawBlockRange()" BlockBuilder : uses
MapType ..> "appendRawBlockRange()" BlockBuilder : uses
Loading

File-Level Changes

Change Details Files
Expose appendRawBlockRange as public API on BlockBuilder and remove duplicate implementation
  • Move appendRawBlockRange from BlockUtil into BlockBuilder
  • Delete duplicate appendRawBlockRange in BlockUtil
  • Adjust imports accordingly
core/trino-spi/src/main/java/io/trino/spi/block/BlockBuilder.java
core/trino-spi/src/main/java/io/trino/spi/block/BlockUtil.java
Refactor array and map operations to leverage appendRawBlockRange
  • Replace manual loops in ArrayConcatUtils with appendRawBlockRange calls
  • Update ArrayConcatFunction.concat to use appendRawBlockRange
  • Update ArrayFlattenFunction.flatten to use appendRawBlockRange
  • Use appendRawBlockRange in ArrayType.writeObject and MapType.writeObject
core/trino-main/src/main/java/io/trino/operator/scalar/ArrayConcatUtils.java
core/trino-main/src/main/java/io/trino/operator/scalar/ArrayConcatFunction.java
core/trino-main/src/main/java/io/trino/operator/scalar/ArrayFlattenFunction.java
core/trino-spi/src/main/java/io/trino/spi/type/ArrayType.java
core/trino-spi/src/main/java/io/trino/spi/type/MapType.java

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

Copy link

@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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `core/trino-spi/src/main/java/io/trino/spi/block/BlockBuilder.java:95-99` </location>
<code_context>
         return newBlockBuilderLike(calculateBlockResetSize(getPositionCount()), blockBuilderStatus);
     }
+
+    static void appendRawBlockRange(Block rawBlock, int offset, int length, BlockBuilder blockBuilder)
+    {
+        switch (rawBlock) {
+            case RunLengthEncodedBlock rleBlock -> blockBuilder.appendRepeated(rleBlock.getValue(), 0, length);
+            case DictionaryBlock dictionaryBlock -> blockBuilder.appendPositions(dictionaryBlock.getDictionary(), dictionaryBlock.getRawIds(), offset, length);
</code_context>

<issue_to_address>
**suggestion:** Consider handling unexpected Block subtypes in appendRawBlockRange.

Adding a default case to the switch statement that throws an exception will ensure that any unsupported Block subtype is caught immediately, improving debuggability and maintainability.

```suggestion
        switch (rawBlock) {
            case RunLengthEncodedBlock rleBlock -> blockBuilder.appendRepeated(rleBlock.getValue(), 0, length);
            case DictionaryBlock dictionaryBlock -> blockBuilder.appendPositions(dictionaryBlock.getDictionary(), dictionaryBlock.getRawIds(), offset, length);
            case ValueBlock valueBlock -> blockBuilder.appendRange(valueBlock, offset, length);
            default -> throw new IllegalArgumentException("Unsupported Block subtype: " + rawBlock.getClass().getName());
        }
```
</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.

return newBlockBuilderLike(calculateBlockResetSize(getPositionCount()), blockBuilderStatus);
}

static void appendRawBlockRange(Block rawBlock, int offset, int length, BlockBuilder blockBuilder)
Copy link
Member

Choose a reason for hiding this comment

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

In the other PR, I mentioned that I wasn't sure if we want to make this public. My reson for this is because I can't think of a good name for this funcetion. My thought is that this should just be a default method on the interface and not a static method, but we already used the name appendRange for value blocks, which should be preferred as they are way faster. Since ValueBlock extends from Block we don't wan to have the same name as it creates overloading confustion in performance sensitive code. I guess we could go with appendRawRange, but I don't really like that. Maybe someone else has an idea here. Regardless this method should have javadoc explaining that the other methods are preferred (unless your code will just have the same switch statement)

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated this to be default void appendBlockRange(Block rawBlock, int offset, int length) and added javadoc.
PTAL

@raunaqmorarka raunaqmorarka requested a review from Copilot October 15, 2025 19:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Expose a public bulk appendBlockRange API on BlockBuilder and replace manual element-copy loops with this method for arrays, maps, and related scalar functions.

  • Introduces BlockBuilder.appendBlockRange default method supporting ValueBlock, DictionaryBlock, and RunLengthEncodedBlock.
  • Replaces prior private utility (BlockUtil.appendRawBlockRange) and manual loops in array/map builders and functions with appendBlockRange.
  • Cleans up imports and removes now-unneeded helper logic.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
MapType.java Replaces manual key/value loop with appendBlockRange for map entry construction.
ArrayType.java Simplifies array writeObject logic using appendBlockRange.
MapBlockBuilder.java Switches from BlockUtil helper to new BlockBuilder.appendBlockRange for key/value appends.
BlockUtil.java Removes obsolete appendRawBlockRange helper.
BlockBuilder.java Adds new public default appendBlockRange method with pattern matching over Block subtypes.
ArrayBlockBuilder.java Uses appendBlockRange for element copying in append and appendRange.
ArrayFlattenFunction.java Replaces inner element loop with appendBlockRange.
ArrayConcatUtils.java Consolidates repeated element-copy loops to appendBlockRange across append/prepend helpers.
ArrayConcatFunction.java Uses appendBlockRange for concatenating arrays.
MergePages.java Replaces custom appendRawBlock logic with appendBlockRange for page merging.

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@raunaqmorarka raunaqmorarka merged commit ba225ba into master Oct 15, 2025
103 checks passed
@raunaqmorarka raunaqmorarka deleted the raunaq/bulk-block branch October 15, 2025 19:55
@github-actions github-actions bot added this to the 478 milestone Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants