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

Aggregate with FILTER clause and alias errors #10878

Closed
jcrist opened this issue Jun 11, 2024 · 2 comments · Fixed by #11020
Closed

Aggregate with FILTER clause and alias errors #10878

jcrist opened this issue Jun 11, 2024 · 2 comments · Fixed by #11020
Assignees
Labels
bug Something isn't working regression

Comments

@jcrist
Copy link
Contributor

jcrist commented Jun 11, 2024

Describe the bug

See the following code:

import datafusion

print(f"datafusion: {datafusion.__version__}")

ctx = datafusion.SessionContext(
    datafusion.SessionConfig(
        {"datafusion.sql_parser.dialect": "PostgreSQL"}
    ).with_information_schema(True)
)

ctx.from_pydict({"a": [1, 3, 5]}, name="test")

# A query with no alias works
good = "SELECT MIN(a) FILTER (WHERE a > 1) FROM test"
print(ctx.sql(good))

# A query with an IN filter works
bad = "SELECT MIN(a) FILTER (WHERE a IN (1, 2)) AS x FROM test"
print(ctx.sql(bad))

# But one with an inequality and an alias fails
bad = "SELECT MIN(a) FILTER (WHERE a > 1) AS x FROM test"
print(ctx.sql(bad))

And results:

datafusion: 38.0.1
DataFrame()
+-------------+
| MIN(test.a) |
+-------------+
| 3           |
+-------------+
DataFrame()
+---+
| x |
+---+
| 1 |
+---+
Traceback (most recent call last):
  File "/home/jcristharif/Code/ibis/bug.py", line 23, in <module>
    print(ctx.sql(bad))
Exception: Internal error: Input field name MIN(test.a) does not match with the projection expression MIN(test.a) FILTER (WHERE test.a > Int64(1)).
This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker

All 3 queries do seem to work in versions < 38.0.1, so the bug appears to have been introduced in that version.

To Reproduce

No response

Expected behavior

No response

Additional context

No response

@Lordworms
Copy link
Contributor

take

@Lordworms
Copy link
Contributor

Lordworms commented Jun 19, 2024

The problem here is we added a check like
image
but for a plan like

Projection: MIN(test.a) FILTER (WHERE test.a > Int64(1)) AS x
  Aggregate: groupBy=[[]], aggr=[[MIN(test.a) FILTER (WHERE test.a > Int64(1))]]
    TableScan: test

the corresponding name is different like

image

we should change the generated name in Aggregate

Since the way to generate AggregateExpr is adding other information on suffix, I think we could using string.start_with to check the name here.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants