Skip to content

Conversation

kasiafi
Copy link
Member

@kasiafi kasiafi commented Oct 17, 2025

Description

Before this change, the logical navigation function FIRST could return position out of bounds of the current match in the running semantics. Now it is restricted to the current match.

Additional context and related issues

Fixes: #26981

Release notes

( ) 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.
(X) Release notes are required, with the following suggested text:

## General
* In row pattern matching, restrict logical navigations to current match in running semantics. ({issue}`26981`)

Summary by Sourcery

Restrict logical navigation functions to the current match bounds in running semantics and update tests accordingly.

Bug Fixes:

  • Prevent FIRST and LAST navigations from returning positions outside the current match in running semantics

Tests:

  • Adjust expected results for FIRST navigation to null when the match is not yet complete in running semantics

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

sourcery-ai bot commented Oct 17, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Restrict logical FIRST and LAST navigations to the active match when running semantics are enabled by computing a dynamic boundary (patternEndInclusive) and propagating it through navigation methods, and update tests to expect null for out-of-match accesses.

Sequence diagram for restricted logical navigation in running semantics

sequenceDiagram
    participant "resolvePosition()"
    participant "findFirstAndForward()"
    participant "findLastAndBackwards()"
    participant "ArrayView"
    "resolvePosition()"->>"findFirstAndForward()": if !last, pass patternEndInclusive
    "resolvePosition()"->>"findLastAndBackwards()": if last, pass patternEndInclusive
    "findFirstAndForward()"->>"ArrayView": iterate up to patternEndInclusive
    "findLastAndBackwards()"->>"ArrayView": iterate down to 0 from patternEndInclusive
    "resolvePosition()"->>"adjustPosition()": adjust final position
Loading

Class diagram for updated LogicalIndexNavigation methods

classDiagram
    class LogicalIndexNavigation {
        +resolvePosition(currentRow, matchedLabels, searchStart, searchEnd)
        -findLastAndBackwards(patternEndInclusive, matchedLabels)
        -findFirstAndForward(matchedLabels, patternEndInclusive)
    }
    LogicalIndexNavigation : int logicalOffset
    LogicalIndexNavigation : Set<String> labels
    LogicalIndexNavigation : boolean last
    LogicalIndexNavigation : boolean running
    LogicalIndexNavigation : int adjustPosition(relativePosition, patternStart, searchStart, searchEnd)
    LogicalIndexNavigation --> ArrayView
    ArrayView : int length()
    ArrayView : String get(int)
Loading

File-Level Changes

Change Details Files
Constrain logical navigation range based on current match
  • Introduce patternEndInclusive for running semantics bound
  • Refactor findLastAndBackwards and findFirstAndForward to use patternEndInclusive
  • Update navigation loops to prevent out-of-match positions
core/trino-main/src/main/java/io/trino/operator/window/pattern/LogicalIndexNavigation.java
Update tests for restricted navigation behavior
  • Change expected FIRST(value,2) results to null for out-of-bounds cases
  • Adjust TestRowPatternMatching assertions accordingly
core/trino-main/src/test/java/io/trino/sql/query/TestRowPatternMatching.java

Possibly linked issues


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!


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.

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.

match_recognize: Logical navigation function FIRST yields unexpected output

1 participant