Skip to content

Revert D96106074#27373

Closed
shelton408 wants to merge 1 commit intoprestodb:release-0.297-edge19from
shelton408:release-0.297-edge19
Closed

Revert D96106074#27373
shelton408 wants to merge 1 commit intoprestodb:release-0.297-edge19from
shelton408:release-0.297-edge19

Conversation

@shelton408
Copy link
Copy Markdown
Contributor

@shelton408 shelton408 commented Mar 19, 2026

Summary:
This reverts the following PR
GitHub Pull Request: #27199

Differential Revision: D97181706

Description

Motivation and Context

Impact

Test Plan

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.
  • If adding new dependencies, verified they have an OpenSSF Scorecard score of 5.0 or higher (or obtained explicit TSC approval for lower scores).

Release Notes

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

== RELEASE NOTES ==

General Changes
* ... 
* ... 

Hive Connector Changes
* ... 
* ... 

If release note is NOT required, use:

== NO RELEASE NOTE ==

Summary by Sourcery

Revert support for referencing SELECT aliases in HAVING clauses and restore previous semantic analysis behavior.

Enhancements:

  • Simplify ORDER BY expression rewriting by removing generic clause handling.

Tests:

  • Update analyzer tests to expect failures when HAVING references output aliases and remove now-unsupported HAVING alias test cases.

Summary:
This reverts the following PR
GitHub Pull Request: prestodb#27199

Differential Revision: D97181706
@shelton408 shelton408 requested review from a team, feilong-liu and jaystarshot as code owners March 19, 2026 02:03
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 19, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR reverts a previous change that allowed SELECT output aliases to be referenced from HAVING, restoring the prior semantics where HAVING cannot reference output column aliases and simplifying the OrderByExpressionRewriter back to ORDER BY–only behavior.

Sequence diagram for HAVING clause analysis without SELECT alias rewriting

sequenceDiagram
    participant SA as StatementAnalyzer
    participant QS as QuerySpecification
    participant EA as ExpressionAnalyzer
    participant AN as Analysis

    SA->>QS: getHaving()
    alt HAVING present
        QS-->>SA: Optional with predicate
        SA->>EA: analyzeExpression(predicate, scope)
        EA-->>SA: ExpressionAnalysis
        SA->>AN: recordSubqueries(QS, ExpressionAnalysis)
        SA->>EA: getType(predicate)
        EA-->>SA: Type predicateType
        alt predicateType not BOOLEAN or UNKNOWN
            SA->>SA: Throw SemanticException TYPE_MISMATCH
        else predicateType valid
            SA->>AN: setHaving(QS, predicate)
        end
        SA->>SA: visitQuerySpecification builds sourceExpressions
        SA->>QS: getHaving()
        QS-->>SA: Optional with predicate
        SA->>SA: sourceExpressions.add(predicate)
        SA->>SA: analyzeGroupingOperations(node, sourceExpressions, orderByExpressions)
        SA->>SA: analyzeAggregations(node, sourceExpressions, orderByExpressions)
    else HAVING absent
        QS-->>SA: Optional.empty
        SA->>SA: Analyze without HAVING
    end
Loading

Class diagram for StatementAnalyzer HAVING and ORDER BY alias handling

classDiagram
    class StatementAnalyzer {
        +Scope visitQuerySpecification(QuerySpecification node, Optional~Scope~ scope)
        -void analyzeHaving(QuerySpecification node, Scope scope)
    }

    class OrderByExpressionRewriter {
        -Multimap~QualifiedName, Expression~ assignments
        +OrderByExpressionRewriter(Multimap~QualifiedName, Expression~ assignments)
        +Expression rewriteIdentifier(Identifier reference, Void context, ExpressionTreeRewriter~Void~ treeRewriter)
    }

    class Analysis {
        +void setOrderByExpressions(QuerySpecification node, List~Expression~ orderByExpressions)
        +void setHaving(QuerySpecification node, Expression predicate)
        +Expression getHaving(QuerySpecification node)
        +void recordSubqueries(Node node, ExpressionAnalysis expressionAnalysis)
    }

    class QuerySpecification {
        +Optional~Expression~ getHaving()
        +Select getSelect()
    }

    class ExpressionAnalysis {
        +Type getType(Expression expression)
        +Collection~FunctionCall~ getWindowFunctions()
    }

    class SemanticException {
    }

    class ExpressionTreeRewriter~T~ {
    }

    StatementAnalyzer --> Analysis : uses
    StatementAnalyzer --> QuerySpecification : analyzes
    StatementAnalyzer --> ExpressionAnalysis : obtains
    StatementAnalyzer --> OrderByExpressionRewriter : previously used for HAVING
    OrderByExpressionRewriter --> SemanticException : throws on ambiguous alias
    ExpressionAnalysis --> SemanticException : used in type checks

    %% Highlight behavior change
    %% HAVING now analyzed as original predicate, not via OrderByExpressionRewriter
Loading

File-Level Changes

Change Details Files
Revert support for SELECT output aliases in HAVING clause and restore original HAVING analysis behavior.
  • In query analysis, stop adding the rewritten HAVING expression into the list of source expressions used for grouping and aggregation analysis and instead add the raw HAVING expression when present.
  • In HAVING analysis, remove the rewrite step that resolved SELECT aliases via OrderByExpressionRewriter and analyze the original HAVING predicate directly.
  • Restore use of the original HAVING predicate for type checking, error reporting, and storage in Analysis (analysis.setHaving).
presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java
Simplify OrderByExpressionRewriter back to supporting only ORDER BY clause and error messages.
  • Remove the clauseName field and overload constructor from OrderByExpressionRewriter, leaving a single constructor that implicitly targets ORDER BY.
  • Revert ambiguous attribute error message formatting to always reference ORDER BY explicitly instead of a generic clause name.
presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/StatementAnalyzer.java
Revert tests that asserted HAVING alias support and restore the previous failure expectation.
  • Replace multiple positive tests that validated HAVING’s ability to reference SELECT aliases and related error cases with a single assertion that referencing an output alias in HAVING fails with MISSING_ATTRIBUTE.
presto-main-base/src/test/java/com/facebook/presto/sql/analyzer/TestAnalyzer.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
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 - 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.

@shelton408 shelton408 closed this Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants