You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "ntjohnson1 (via GitHub)" <gi...@apache.org> on 2025/11/05 20:22:41 UTC
[I] first_value doesn't work when applied to window function output [datafusion-python]
ntjohnson1 opened a new issue, #1300:
URL: https://github.com/apache/datafusion-python/issues/1300
**Describe the bug**
A clear and concise description of what the bug is.
If I generate a column based on a window function then try to filter and select the first value it barfs
**To Reproduce**
Steps to reproduce the behavior:
```python
import datafusion as dfn
from datafusion import lit, col, functions as F
from datafusion.expr import Window, WindowFrame
def main() -> None:
ctx = dfn.SessionContext()
df = ctx.from_pydict(
{"any_row": list(range(10))},
)
df = df.select(
"any_row",
lit(1).alias("ones"),
)
df = df.select(
"any_row",
F.sum(col("ones"))\
.over(Window(window_frame=WindowFrame("rows", None, 0), order_by=col("any_row").sort(ascending=True))) \
.alias("forward_row_sum"),
F.sum(col("ones"))\
.over(Window(window_frame=WindowFrame("rows", None, 0), order_by=col("any_row").sort(ascending=False))) \
.alias("reverse_row_sum"),
)
df.collect()
df.select(
F.first_value(col("forward_row_sum"), order_by=col("any_row"))
).collect()
df.select(
F.last_value(col("reverse_row_sum"), filter=col("reverse_row_sum") >= 5, order_by=col("any_row").sort(ascending=True))
).collect()
if __name__ == "__main__":
main()
```
```console
Traceback (most recent call last):
File "/Users/nick/repos/bug.py", line 39, in <module>
main()
~~~~^^
File "/Users/nick/repos/bug.py", line 26, in main
).collect()
~~~~~~~^^
File "/Users/nick/repos/.venv/lib/python3.13/site-packages/datafusion/dataframe.py", line 681, in collect
return self.df.collect()
~~~~~~~~~~~~~~~^^
Exception: DataFusion error: NotImplemented("Physical plan does not support logical expression AggregateFunction(AggregateFunction { func: AggregateUDF { inner: FirstValue { name: \"first_value\", signature: Signature { type_signature: Any(1), volatility: Immutable }, accumulator: \"<FUNC>\" } }, params: AggregateFunctionParams { args: [Column(Column { relation: None, name: \"sum(ones) ORDER BY [c19e557aec20e49b985bb070e969ba68f.any_row ASC NULLS FIRST] ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING\" })], distinct: false, filter: None, order_by: [Sort { expr: Column(Column { relation: Some(Bare { table: \"c19e557aec20e49b985bb070e969ba68f\" }), name: \"any_row\" }), asc: true, nulls_first: true }], null_treatment: Some(RespectNulls) } })")
```
**Expected behavior**
A clear and concise description of what you expected to happen.
That I get the first (or last) value.
**Additional context**
Add any other context about the problem here.
```python
import datafusion as dfn
>>> dfn.__version__
'50.1.0'
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "ntjohnson1 (via GitHub)" <gi...@apache.org>.
ntjohnson1 commented on issue #1300:
URL: https://github.com/apache/datafusion-python/issues/1300#issuecomment-3503111544
Confirmed the work around
```python
df.select(
F.first_value(col("forward_row_sum"), order_by=col("any_row"))\
.over(Window(window_frame=WindowFrame("rows", None, 0), order_by=col("any_row").sort(ascending=True)))
).collect()
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer closed issue #1300: first_value doesn't work when applied to window function output
URL: https://github.com/apache/datafusion-python/issues/1300
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "AdMub (via GitHub)" <gi...@apache.org>.
AdMub commented on issue #1300:
URL: https://github.com/apache/datafusion-python/issues/1300#issuecomment-3782636297
I have investigated this issue. The crash happens because first_value (an aggregate function) is being called inside .select(), which is not supported by the physical planner.
I confirmed that using .aggregate([], [F.first_value(...)]) works correctly.
I would like to contribute a documentation fix to clarify that aggregate functions must be used within .aggregate() and not .select(). Can I be assigned?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "AdMub (via GitHub)" <gi...@apache.org>.
AdMub commented on issue #1300:
URL: https://github.com/apache/datafusion-python/issues/1300#issuecomment-3782333899
Hi, I would like to work on this issue. I have successfully reproduced the crash on the latest main branch using the script provided. Could you please assign it to me?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "timsaucer (via GitHub)" <gi...@apache.org>.
timsaucer commented on issue #1300:
URL: https://github.com/apache/datafusion-python/issues/1300#issuecomment-3493251502
You can work around this by setting the window on the function (using `.over()`), but yes this is clearly a bug.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] first_value doesn't work when applied to window function output [datafusion-python]
Posted by "ntjohnson1 (via GitHub)" <gi...@apache.org>.
ntjohnson1 commented on issue #1300:
URL: https://github.com/apache/datafusion-python/issues/1300#issuecomment-3522610618
Hmm on 50.1 this seems just generally broken or the [example in the docs](https://datafusion.apache.org/python/autoapi/datafusion/functions/index.html#datafusion.functions.last_value) is wrong
```python
ctx = dfn.SessionContext()
df = ctx.from_pydict(
{
"a": [0, 1, 2, 3, 4],
"b": [0, 1, 2, 3, 4]
}
)
df.select(F.last_value(col("a"), order_by="b")).collect()
```
```console
Exception: DataFusion error: NotImplemented("Physical plan does not support logical expression AggregateFunction(AggregateFunction { func: AggregateUDF { inner: LastValue { name: \"last_value\", signature: Signature { type_signature: Any(1), volatility: Immutable }, accumulator: \"<FUNC>\" } }, params: AggregateFunctionParams { args: [Column(Column { relation: Some(Bare { table: \"cfca6f9a4517040a69c659bb43f2a9617\" }), name: \"a\" })], distinct: false, filter: None, order_by: [Sort { expr: Column(Column { relation: Some(Bare { table: \"cfca6f9a4517040a69c659bb43f2a9617\" }), name: \"b\" }), asc: true, nulls_first: true }], null_treatment: Some(RespectNulls) } })")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org