Skip to content

Fix lambda return type coercion#26155

Draft
tdcmeehan wants to merge 1 commit intoprestodb:masterfrom
tdcmeehan:coerc
Draft

Fix lambda return type coercion#26155
tdcmeehan wants to merge 1 commit intoprestodb:masterfrom
tdcmeehan:coerc

Conversation

@tdcmeehan
Copy link
Contributor

@tdcmeehan tdcmeehan commented Sep 25, 2025

Description

Currently, Presto won't apply any coercion on the return type within a lambda. We should make it more flexible.

Motivation and Context

Recently, array_sort with transform was added to Presto, and this can cause conflicts with the older comparator approach.

Impact

Fix issues with array_sort and potential future issues with lambdas.

Test Plan

Included unit tests.

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

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label Sep 25, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Sep 25, 2025

Reviewer's Guide

Enhance lambda return type handling by catching argument count mismatches, applying coercion within ExpressionAnalyzer when actual and expected return types differ, enabling return type coercion in the SignatureBinder, updating comparator function signatures to use bigint, and adding targeted tests for array_sort lambda return type coercion.

File-Level Changes

Change Details Files
Implement lambda return type coercion in ExpressionAnalyzer
  • Wrap inner lambda analysis in try/catch to handle LambdaArgumentCountMismatchException
  • Return a generic unknown type signature for invalid lambdas
  • After processing lambdas, detect return type mismatches and insert expression coercion when permissible
  • Update lambda expression type to the expected FunctionType
ExpressionAnalyzer.java
Introduce LambdaArgumentCountMismatchException
  • Add new exception class extending SemanticException for mismatched lambda arguments
  • Replace direct SemanticException throw in visitLambdaExpression with the new exception
LambdaArgumentCountMismatchException.java
ExpressionAnalyzer.java
Enable lambda return type coercion in SignatureBinder
  • Allow coercion in appendTypeRelationshipConstraintSolver and appendConstraintSolvers for lambda return types
  • Remove the previous hard stop preventing function type coercion
SignatureBinder.java
Update comparator function return type annotations
  • Change @SqlType for ComparatorXxxLambda parameters from int to bigint across all sort methods
ArraySortComparatorFunction.java
Add tests for array_sort lambda return type coercion
  • Create testArraySortLambdaReturnTypeCoercion with various assertFunction cases covering coercion scenarios
TestArrayOperators.java

Possibly linked issues

  • Add simple HashAggregation #1: The PR directly fixes the array_sort lambda comparator signature to accept bigint, resolving the type mismatch error described in the issue.

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

@kgpai
Copy link
Contributor

kgpai commented Sep 26, 2025

@tdcmeehan This is a simple fix if you change the Velox lambda function signature to return integer (from bigint). We validated that it works with the side car.

@tdcmeehan
Copy link
Contributor Author

@tdcmeehan This is a simple fix if you change the Velox lambda function signature to return integer (from bigint). We validated that it works with the side car.

@kgpai is anyone working to add that to Velox?

@Dilli-Babu-Godari
Copy link
Contributor

@tdcmeehan This is a simple fix if you change the Velox lambda function signature to return integer (from bigint). We validated that it works with the side car.

I agree — changing the Velox lambda function signature to return integer instead of bigint is a straightforward fix. That said, I also think we should support type coercion in lambdas more generally, so this kind of mismatch doesn’t surface again.

@kgpai
Copy link
Contributor

kgpai commented Sep 26, 2025

@tdcmeehan Yes I am working on it - will have a PR out soon.

@tdcmeehan
Copy link
Contributor Author

Sounds good, so I'll remove the first commit from this PR.

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