Skip to content

[SPARK-58911][ML] Use DataFrame aggregations in CountVectorizer fit - #57755

Closed
zhengruifeng wants to merge 6 commits into
apache:masterfrom
zhengruifeng:count-vectorizer-dataframe-fit-dev3
Closed

[SPARK-58911][ML] Use DataFrame aggregations in CountVectorizer fit#57755
zhengruifeng wants to merge 6 commits into
apache:masterfrom
zhengruifeng:count-vectorizer-dataframe-fit-dev3

Conversation

@zhengruifeng

@zhengruifeng zhengruifeng commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

This PR rewrites CountVectorizer.fit to stay in the DataFrame execution path instead of
converting the input column to an RDD.

The new implementation:

  • computes fractional minDF / maxDF thresholds with a scalar subquery over the input size;
  • computes word counts with explode and grouped aggregation;
  • computes document frequency, when DF filtering is requested, with a two-stage aggregation:
    first by (docId, word) and then by word;
  • uses DataFrame ordering and limit to select the vocabulary.

Why are the changes needed?

The current implementation builds per-document maps manually and combines them with reduceByKey.
Keeping the fit logic in DataFrame operations lets Spark plan the aggregations natively while
preserving the existing CountVectorizer semantics for word counts, document frequency filtering,
and deterministic vocabulary ordering.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Added regression coverage for document frequency when a document contains repeated words.

./build/sbt "mllib/testOnly org.apache.spark.ml.feature.CountVectorizerSuite"

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

Generated-by: Codex (GPT-5)

@zhengruifeng zhengruifeng changed the title [WIP][MLLIB] Rewrite CountVectorizer fitting with DataFrame APIs [SPARK-58911][ML] Rewrite CountVectorizer fitting with DataFrame APIs Aug 20, 2026
@zhengruifeng
zhengruifeng marked this pull request as ready for review August 20, 2026 15:02
@zhengruifeng zhengruifeng changed the title [SPARK-58911][ML] Rewrite CountVectorizer fitting with DataFrame APIs [SPARK-58911][ML] Use DataFrame aggregations in CountVectorizer fit Aug 20, 2026
zhengruifeng added a commit that referenced this pull request Aug 21, 2026
### What changes were proposed in this pull request?

This PR rewrites `CountVectorizer.fit` to stay in the DataFrame execution path instead of
converting the input column to an RDD.

The new implementation:

- computes fractional `minDF` / `maxDF` thresholds with a scalar subquery over the input size;
- computes word counts with `explode` and grouped aggregation;
- computes document frequency, when DF filtering is requested, with a two-stage aggregation:
  first by `(docId, word)` and then by `word`;
- uses DataFrame ordering and `limit` to select the vocabulary.

### Why are the changes needed?

The current implementation builds per-document maps manually and combines them with `reduceByKey`.
Keeping the fit logic in DataFrame operations lets Spark plan the aggregations natively while
preserving the existing CountVectorizer semantics for word counts, document frequency filtering,
and deterministic vocabulary ordering.

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

No.

### How was this patch tested?

Added regression coverage for document frequency when a document contains repeated words.

```
./build/sbt "mllib/testOnly org.apache.spark.ml.feature.CountVectorizerSuite"
```

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

Generated-by: Codex (GPT-5)

Closes #57755 from zhengruifeng/count-vectorizer-dataframe-fit-dev3.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>
(cherry picked from commit 619c067)
Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>
@zhengruifeng

Copy link
Copy Markdown
Contributor Author

Merge Summary:

Posted by merge_spark_pr.py

@zhengruifeng
zhengruifeng deleted the count-vectorizer-dataframe-fit-dev3 branch August 21, 2026 00:51
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.

2 participants