Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nondeterministic column order in optimized plans #3709

Closed
cpcloud opened this issue Jun 27, 2024 · 2 comments
Closed

nondeterministic column order in optimized plans #3709

cpcloud opened this issue Jun 27, 2024 · 2 comments
Assignees

Comments

@cpcloud
Copy link
Contributor

cpcloud commented Jun 27, 2024

sqlglot optimizer reports aggregation columns in an order that doesn't always match the input query:

import sqlglot as sg
import sqlglot.expressions as sge
import sqlglot.optimizer as sgo
import sqlglot.planner as sgp


raw_sql = """
SELECT
  AVG(bill_length_mm) AS avg_bill_length,
  AVG(bill_depth_mm) AS avg_bill_depth
FROM penguins
"""

catalog = {
    "penguins": {
        "bill_length_mm": sge.DataType(kind=sge.DataType.Type.DOUBLE),
        "bill_depth_mm": sge.DataType(kind=sge.DataType.Type.DOUBLE),
    },
}
expr = sg.parse_one(raw_sql, read="duckdb")
tree = sgo.optimize(expr, catalog)
plan = sgp.Plan(tree)
assert [agg.alias for agg in plan.root.aggregations] == [
    "avg_bill_length",
    "avg_bill_depth",
]

The assert only sometimes fails, so to reproduce you'll probably have to run the example more than once.

@tobymao tobymao self-assigned this Jun 27, 2024
@cpcloud
Copy link
Contributor Author

cpcloud commented Jun 27, 2024

Wow, bugs seem to just fix themselves around here, for fear that @tobymao might look in their general direction!

@cpcloud
Copy link
Contributor Author

cpcloud commented Jun 27, 2024

Also, thank you for fixing this so quickly 😂

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

No branches or pull requests

2 participants