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

CSE optimization overwrites struct field name #19253

Closed
2 tasks done
cmdlineluser opened this issue Oct 15, 2024 · 1 comment · Fixed by #19280
Closed
2 tasks done

CSE optimization overwrites struct field name #19253

cmdlineluser opened this issue Oct 15, 2024 · 1 comment · Fixed by #19280
Assignees
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars

Comments

@cmdlineluser
Copy link
Contributor

cmdlineluser commented Oct 15, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

import polars as pl

df = pl.LazyFrame({"x": [1, 2], "y": [4, 5]})

(df
  .with_columns(
      q1 = pl.struct(pl.col.x - pl.col.y.mean()),
      q2 = pl.struct(pl.col.x - pl.col.y.mean().over("y"))
  )
  .with_columns(
      pl.col("q1").map_batches(lambda x: [print(f"{x.struct.fields=}"), x][-1])
  )
  .collect()
)

Log output

No response

Issue description

The CSE temp column name ends up as the struct field name - instead of the original LHS expression.

x.struct.fields=['__POLARS_CSER_0x9926168f0d727f4b']

With .collect(comm_subexpr_elim=False)

x.struct.fields=['x']

Expected behavior

Correct field name.

Installed versions

main
@cmdlineluser cmdlineluser added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Oct 15, 2024
@orlp orlp added accepted Ready for implementation P-high Priority: high and removed needs triage Awaiting prioritization by a maintainer labels Oct 16, 2024
@ritchie46
Copy link
Member

map_batches is a red herring:

df = pl.LazyFrame({"x": [1, 2], "y": [4, 5]})

(df
  .with_columns(
      q1 = pl.struct(pl.col.x - pl.col.y.mean()),
      q2 = pl.struct(pl.col.x - pl.col.y.mean().over("y"))
  )
  .collect()
).to_dict(as_series=False)
{'x': [1, 2],
 'y': [4, 5],
 'q1': [{'__POLARS_CSER_0x8af40ccf78f1851d': -3.5},
  {'__POLARS_CSER_0x8af40ccf78f1851d': -2.5}],
 'q2': [{'__POLARS_CSER_0x8af40ccf78f1851d': -3.0},
  {'__POLARS_CSER_0x8af40ccf78f1851d': -3.0}]}

@cmdlineluser cmdlineluser changed the title struct + map_batches CSE optimization overwrites field name CSE optimization overwrites struct field name Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants