Skip to content

[SPARK-58080][PYTHON][TESTS] Add ASV microbenchmark for SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE#57189

Closed
Yicong-Huang wants to merge 1 commit into
apache:masterfrom
Yicong-Huang:bench-apply-in-pandas-with-state
Closed

[SPARK-58080][PYTHON][TESTS] Add ASV microbenchmark for SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE#57189
Yicong-Huang wants to merge 1 commit into
apache:masterfrom
Yicong-Huang:bench-apply-in-pandas-with-state

Conversation

@Yicong-Huang

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Add an ASV micro-benchmark for SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE (applyInPandasWithState) to bench_eval_type.py.

Unlike TransformWithState, no state server socket is involved: ApplyInPandasWithStateSerializer reconstructs each GroupState entirely from a metadata column carried inline in the Arrow stream. The benchmark emits the data columns followed by one trailing struct column (__state, matching the JVM ApplyInPandasWithStateWriter.STATE_METADATA_SCHEMA), and faithfully reproduces the JVM writer's bin-packing and cross-batch chunking (isLastChunk set only on a group's final chunk), so a group split across batches reassembles into one GroupState.

Scenarios cover few/many groups, small/large group sizes, wide columns, mixed value types (string/binary/boolean) and a nested struct column. UDFs: identity_udf, sort_udf, count_udf. identity_udf/sort_udf pass values through; count_udf exercises the per-group state read/write path (reads the running count via getOption, writes it back via update) and re-emits the key plus the count.

Why are the changes needed?

Part of SPARK-55724. Establishes a performance baseline before refactoring SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE.

Does this PR introduce any user-facing change?

No

How was this patch tested?

COLUMNS=120 asv run --python=same --bench "ApplyInPandasWithState" -a "repeat=3" (one of two stable runs):

ApplyInPandasWithStateUDFTimeBench:

================ ============== ============ ============
--                                 udf
---------------- ----------------------------------------
    scenario      identity_udf    sort_udf    count_udf
================ ============== ============ ============
 few_groups_sm      1.02+-0s      1.06+-0.01s   128+-1ms
 few_groups_lg     6.98+-0.05s    7.18+-0.05s   762+-3ms
 many_groups_sm    6.76+-0.04s    7.62+-0.06s  3.14+-0.01s
 many_groups_lg    4.80+-0.1s     4.95+-0.08s   934+-3ms
   wide_cols       5.92+-0.02s    6.17+-0.1s    878+-20ms
   mixed_cols      4.49+-0.01s    4.76+-0.04s   704+-5ms
 nested_struct     9.17+-0.1s     10.8+-0.07s  3.32+-0.02s
================ ============== ============ ============

ApplyInPandasWithStateUDFPeakmemBench:

================ ============== ========== ===========
--                                udf
---------------- -------------------------------------
    scenario      identity_udf   sort_udf   count_udf
================ ============== ========== ===========
 few_groups_sm        118M         119M        106M
 few_groups_lg        272M         280M        221M
 many_groups_sm       172M         173M        144M
 many_groups_lg       172M         178M        140M
   wide_cols          284M         282M        233M
   mixed_cols         187M         188M        182M
 nested_struct        227M         228M        211M
================ ============== ========== ===========

Was this patch authored or co-authored using generative AI tooling?

No

@uros-b uros-b left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Yicong-Huang and @HyukjinKwon!

@Yicong-Huang Yicong-Huang force-pushed the bench-apply-in-pandas-with-state branch from 1512ec8 to 6f97f04 Compare July 14, 2026 23:16
@Yicong-Huang

Copy link
Copy Markdown
Contributor Author

Thanks @HyukjinKwon and @uros-b, merging now

Yicong-Huang added a commit that referenced this pull request Jul 15, 2026
…P_PANDAS_UDF_WITH_STATE

### What changes were proposed in this pull request?

Add an ASV micro-benchmark for `SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE` (`applyInPandasWithState`) to `bench_eval_type.py`.

Unlike TransformWithState, no state server socket is involved: `ApplyInPandasWithStateSerializer` reconstructs each `GroupState` entirely from a metadata column carried inline in the Arrow stream. The benchmark emits the data columns followed by one trailing struct column (`__state`, matching the JVM `ApplyInPandasWithStateWriter.STATE_METADATA_SCHEMA`), and faithfully reproduces the JVM writer's bin-packing and cross-batch chunking (`isLastChunk` set only on a group's final chunk), so a group split across batches reassembles into one `GroupState`.

Scenarios cover few/many groups, small/large group sizes, wide columns, mixed value types (string/binary/boolean) and a nested struct column. UDFs: `identity_udf`, `sort_udf`, `count_udf`. `identity_udf`/`sort_udf` pass values through; `count_udf` exercises the per-group state read/write path (reads the running count via `getOption`, writes it back via `update`) and re-emits the key plus the count.

### Why are the changes needed?

Part of [SPARK-55724](https://issues.apache.org/jira/browse/SPARK-55724). Establishes a performance baseline before refactoring `SQL_GROUPED_MAP_PANDAS_UDF_WITH_STATE`.

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

`COLUMNS=120 asv run --python=same --bench "ApplyInPandasWithState" -a "repeat=3"` (one of two stable runs):

`ApplyInPandasWithStateUDFTimeBench`:

```text
================ ============== ============ ============
--                                 udf
---------------- ----------------------------------------
    scenario      identity_udf    sort_udf    count_udf
================ ============== ============ ============
 few_groups_sm      1.02+-0s      1.06+-0.01s   128+-1ms
 few_groups_lg     6.98+-0.05s    7.18+-0.05s   762+-3ms
 many_groups_sm    6.76+-0.04s    7.62+-0.06s  3.14+-0.01s
 many_groups_lg    4.80+-0.1s     4.95+-0.08s   934+-3ms
   wide_cols       5.92+-0.02s    6.17+-0.1s    878+-20ms
   mixed_cols      4.49+-0.01s    4.76+-0.04s   704+-5ms
 nested_struct     9.17+-0.1s     10.8+-0.07s  3.32+-0.02s
================ ============== ============ ============
```

`ApplyInPandasWithStateUDFPeakmemBench`:

```text
================ ============== ========== ===========
--                                udf
---------------- -------------------------------------
    scenario      identity_udf   sort_udf   count_udf
================ ============== ========== ===========
 few_groups_sm        118M         119M        106M
 few_groups_lg        272M         280M        221M
 many_groups_sm       172M         173M        144M
 many_groups_lg       172M         178M        140M
   wide_cols          284M         282M        233M
   mixed_cols         187M         188M        182M
 nested_struct        227M         228M        211M
================ ============== ========== ===========
```

### Was this patch authored or co-authored using generative AI tooling?

No

Closes #57189 from Yicong-Huang/bench-apply-in-pandas-with-state.

Authored-by: Yicong Huang <17627829+Yicong-Huang@users.noreply.github.com>
Signed-off-by: Yicong-Huang <17627829+Yicong-Huang@users.noreply.github.com>
(cherry picked from commit 4bea4ad)
Signed-off-by: Yicong-Huang <17627829+Yicong-Huang@users.noreply.github.com>
@Yicong-Huang

Copy link
Copy Markdown
Contributor Author

Merge Summary:

Posted by merge_spark_pr.py

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

Successfully merging this pull request may close these issues.

3 participants