Skip to content

Conversation

@mccanne
Copy link
Collaborator

@mccanne mccanne commented Jan 7, 2026

This commit fixes a bug where ORDER BY scope was improperly handled because the logic favored the input table over the column aliases but the materialized output table has priority over the input table once the SELECT body scope is left. This would typically be handled by first looking into the output table but we can't do this because we do expression matching on the sem-tree expressions and we can't mix and match input expressions with the output expressions. The fix is to look in the columns first (producing input table expressions) and matching those expressions to the grouped output table columns with the expression matcher.

This commit fixes a bug where ORDER BY scope was improperly
handled because the logic favored the input table over the
column aliases but the materialized output table has priority
over the input table once the SELECT body scope is left.
This would typically be handled by first looking into the output
table but we can't do this because we do expression matching
on the sem-tree expressions and we can't mix and match
input expressions with the output expressions.  The fix is
to look in the columns first (producing input table expressions)
and matching those expressions to the grouped output table
columns with the expression matcher.
@mccanne
Copy link
Collaborator Author

mccanne commented Jan 7, 2026

This should fix the intermittent test failure in #6496

// SELECT body (e.g., in ORDER BY), so we need to first check
// the select column names before the inputs. This is handled by
// other SQLs by looking in the output table, but we can't do that
// since we must can't mix output columns and input columns
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// since we must can't mix output columns and input columns
// since we can't mix output columns and input columns

inputs:
- name: query.spq
data: |
pragma pg=false
Copy link
Member

Choose a reason for hiding this comment

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

Is this what you want? I was expecting pg=true based on the file name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, you're right. The bug was when PG scope was off and it was the output table causing the problem. I'll rename the file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And remove pg=false

// the binding of identifiers changes and query results can change. By
// resolving lateral aliases first, we avoid this problem.
// resolving lateral aliases first, we avoid this problem. This can
// be overridden with "pragma pg" to favor Postgres precedence.
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
// be overridden with "pragma pg" to favor Postgres precedence.
// be overridden with "pragma pg" to favor PostgreSQL precedence.

@mccanne mccanne merged commit 2c66ce5 into main Jan 7, 2026
2 checks passed
@mccanne mccanne deleted the fix-lateral-scoping-bug branch January 7, 2026 14:57
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.

3 participants