-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
ComputeError: cannot aggregate a literal
for literals in .over()
#16343
Comments
Do you have a "real-world-example" where this is useful? Seems like you want to know the number of rows per group? No computer access currently but this might work and is very easy: |
This works indeed. We do allow literals in |
Here is a non-contrived use-case I have. I want to define this function
So the user may do:
Here, the first line does not work due to @ritchie46, I understand now that Thanks! |
I thought something in >>> pl.col("x").meta.is_column()
True But I guess it just checks for >>> (pl.col("x") + 1).meta.is_column()
False I'm not sure if there is a way to test if something consists only of literals? A single >>> pl.lit(1.0).meta.serialize()
'{"Literal":{"Float":1.0}}' But perhaps not: >>> (pl.lit(1.0) + pl.lit(2.0)).meta.serialize()
'{"BinaryExpr":{"left":{"Literal":{"Float":1.0}},"op":"Plus","right":{"Literal":{"Float":2.0}}}}' |
Can the user just set |
There are many workarounds ( |
Sounds quite like a riddle, how about applying a tautological identity of existing columns like |
Yep! For now I'm just passing the burden/choice onto the user; if they want to use a constant weight, they will either need to do Neither is as aesthetically pleasing as |
I just noticed an interesting PR for Perhaps a Can we say if none of the Column-type nodes are contained anywhere in the expression then it is a Literal? demo.pydef is_literal(expr):
non_literal = {
"Column", "Columns", "DtypeColumn", "Exclude",
"IndexColumn", "Nth", "Selector", "Wildcard"
}
found = False
def _is_literal_impl(obj):
nonlocal found
if found: return
found = bool(obj.keys() & non_literal)
if found: return
return obj
__import__("json").loads(
expr.meta.serialize(),
object_hook=_is_literal_impl
)
return not found >>> is_literal(pl.lit(1.0) + pl.lit(2) + pl.col("foo"))
False
>>> is_literal(pl.lit(1.0) + pl.lit(2))
True Not sure if there are any other possible cases. |
The caller shouldn't pass a literal, but something of the groups length: |
|
Checks
Reproducible example
The error is
Log output
No response
Issue description
This is similar to this bug.
It seems sensible to allow literals in an
.over()
context, to allow for eitherpl.col("...")
orpl.lit(1)
for user defined functions.Expected behavior
The behavior should be the same as using
Installed versions
The text was updated successfully, but these errors were encountered: