Skip to content

[SPARK-2737] Add retag() method for changing RDDs' ClassTags. - #1639

Closed
JoshRosen wants to merge 4 commits into
apache:masterfrom
JoshRosen:SPARK-2737
Closed

JoshRosen wants to merge 4 commits into
apache:masterfrom
JoshRosen:SPARK-2737

Conversation

@JoshRosen

Copy link
Copy Markdown
Contributor

The Java API's use of fake ClassTags doesn't seem to cause any problems for Java users, but it can lead to issues when passing JavaRDDs' underlying RDDs to Scala code (e.g. in the MLlib Java API wrapper code). If we call collect() on a Scala RDD with an incorrect ClassTag, this causes ClassCastExceptions when we try to allocate an array of the wrong type (for example, see SPARK-2197).

There are a few possible fixes here. An API-breaking fix would be to completely remove the fake ClassTags and require Java API users to pass java.lang.Class instances to all parallelize() calls and add returnClass fields to all Function implementations. This would be extremely verbose.

Instead, this patch adds internal APIs to "repair" a Scala RDD with an incorrect ClassTag by wrapping it and overriding its ClassTag. This should be okay for cases where the Scala code that calls collect() knows what type of array should be allocated, which is the case in the MLlib wrappers.

@JoshRosen

Copy link
Copy Markdown
Contributor Author

/cc @mengxr @jkbradley @mateiz

@SparkQA

SparkQA commented Jul 29, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17381/consoleFull

@SparkQA

SparkQA commented Jul 29, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch FAILED unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17381/consoleFull

The Java API's use of fake ClassTags doesn't seem to cause any problems for
Java users, but it can lead to issues when passing JavaRDDs' underlying RDDs to
Scala code (e.g. in the MLlib Java API wrapper code). If we call collect() on
a Scala RDD with an incorrect ClassTag, this causes ClassCastExceptions when we
try to allocate an array of the wrong type (for example, see SPARK-2197).

There are a few possible fixes here. An API-breaking fix would be to completely
remove the fake ClassTags and require Java API users to pass java.lang.Class
instances to all parallelize() calls and add returnClass fields to all Function
implementations. This would be extremely verbose.

Instead, this patch adds internal APIs to "repair" a Scala RDD with an
incorrect ClassTag by wrapping it and overriding its ClassTag. This should be
okay for cases where the Scala code that calls collect() knows what type of
array should be allocated, which is the case in the MLlib wrappers.
@SparkQA

SparkQA commented Jul 29, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17383/consoleFull

@SparkQA

SparkQA commented Jul 29, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17383/consoleFull

@marmbrus

Copy link
Copy Markdown
Contributor

Another option would be to add collectSeq or something similar that returns a type with reasonable variance semantics.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You also need to preserve the Partitioner and such. It would be better to do this via this.mapPartitions with the preservePartitioning option set to true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would there be any performance impact of running mapPartitions(identity, preservesPartitioning = true)(classTag)? If we have an RDD that's persisted in a serialized format, wouldn't this extra map force an unnecessary deserialization?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, the fix with just passing the partitioner also works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually compute just works at the iterator level, so I don't think mapPartitions would hurt. All you do is pass through the parent's iterator. When you call compute() you're already deserializing the RDD, this won't create extra work.

@mateiz

mateiz commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

I'm okay with either this or collectSeq actually.

@JoshRosen

Copy link
Copy Markdown
Contributor Author

I'm going to take another pass on this to see if I can implicitly grab the ClassTag from the caller's scope, so hold off on merging this for a bit.

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17418/consoleFull

@JoshRosen

Copy link
Copy Markdown
Contributor Author

This method is intended to be called by Scala classes that implement Java-friendly wrappers for the Spark Scala API. For instance, MLlib has APIs that accept RDD[LabelledPoint]. Ideally, the Java wrapper code can simply call the underlying Scala methods without having to worry about how they're implemented. Therefore, I think we should prefer the retag()-based approach, since collectSeq would require us to modify the Scala consumer of the RDD.

Since this is a private, internal API, we should be able to revisit this decision if we change our minds later.

@JoshRosen

Copy link
Copy Markdown
Contributor Author

My last commit made classTag implicit in the retag() method, so in many cases the Scala code can be written as someJavaRDD.rdd.retag.[...].collect().

@mateiz

mateiz commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

Sure, sounds good. Did you see my comments on preserving partitions too though?

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17418/consoleFull

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17426/consoleFull

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch FAILED unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17426/consoleFull

@mateiz

mateiz commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

In case you don't see the hidden comment above: I don't think mapPartitions would hurt performance here. All you do is pass through the parent's iterator. When you call compute() you're already deserializing the RDD, so this won't create extra work in that case.

@mateiz

mateiz commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

Basically it's a shorter way of writing what you wrote. Take a look at MapPartitionsRDD.

@JoshRosen

Copy link
Copy Markdown
Contributor Author

I've updated this to use mapPartitions().

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17458/consoleFull

@mateiz

mateiz commented Jul 30, 2014

Copy link
Copy Markdown
Contributor

LGTM, feel free to merge it when it passes tests

@JoshRosen

Copy link
Copy Markdown
Contributor Author

Jenkins, retest this please.

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17474/consoleFull

@SparkQA

SparkQA commented Jul 30, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch FAILED unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17474/consoleFull

@JoshRosen

Copy link
Copy Markdown
Contributor Author

Jenkins, retest this please.

@SparkQA

SparkQA commented Jul 31, 2014

Copy link
Copy Markdown

QA tests have started for PR 1639. This patch merges cleanly.
View progress: https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17550/consoleFull

@SparkQA

SparkQA commented Jul 31, 2014

Copy link
Copy Markdown

QA results for PR 1639:
- This patch PASSES unit tests.
- This patch merges cleanly
- This patch adds no public classes

For more information see test ouptut:
https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/17550/consoleFull

@JoshRosen

Copy link
Copy Markdown
Contributor Author

Alright, I've merged this. Thanks for the review!

@asfgit asfgit closed this in 4fb2593 Jul 31, 2014
xiliu82 pushed a commit to xiliu82/spark that referenced this pull request Sep 4, 2014
The Java API's use of fake ClassTags doesn't seem to cause any problems for Java users, but it can lead to issues when passing JavaRDDs' underlying RDDs to Scala code (e.g. in the MLlib Java API wrapper code). If we call collect() on a Scala RDD with an incorrect ClassTag, this causes ClassCastExceptions when we try to allocate an array of the wrong type (for example, see SPARK-2197).

There are a few possible fixes here. An API-breaking fix would be to completely remove the fake ClassTags and require Java API users to pass java.lang.Class instances to all parallelize() calls and add returnClass fields to all Function implementations. This would be extremely verbose.

Instead, this patch adds internal APIs to "repair" a Scala RDD with an incorrect ClassTag by wrapping it and overriding its ClassTag. This should be okay for cases where the Scala code that calls collect() knows what type of array should be allocated, which is the case in the MLlib wrappers.

Author: Josh Rosen <joshrosen@apache.org>

Closes apache#1639 from JoshRosen/SPARK-2737 and squashes the following commits:

572b4c8 [Josh Rosen] Replace newRDD[T] with mapPartitions().
469d941 [Josh Rosen] Preserve partitioner in retag().
af78816 [Josh Rosen] Allow retag() to get classTag implicitly.
d1d54e6 [Josh Rosen] [SPARK-2737] Add retag() method for changing RDDs' ClassTags.
sunchao added a commit to sunchao/spark that referenced this pull request Jun 2, 2023
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Nov 23, 2026
…che#1639)

<!-- title: [VARKA] Add the scripts that gate, push and watch a pull request -->
### What changes were proposed in this pull request?

The operations scripts that take a branch to an open pull request with a green option matrix are now in `dev/varka_ops/`. Until now they lived only in a private ops directory.

- Fourteen scripts. `pre-pr.sh` is the local gate: merge master, regenerate (or skip that), the extra lints and suites, the push check, and the arm sweep. `pre-pr-slot.sh` queues it, and `mark-reviewed.sh` and `review-covers.sh` record and check the review. `open-pr.sh`, `push-ci.sh` and `push-no-ci.sh` push. `start-waiter.sh`, `matrix-after-build-link.sh` and `matrix-link.sh` dispatch and link the matrix. `watch-any-ready.sh`, `watch-infra.sh` and `ops-status.sh` watch.
- `common.sh` holds the shared configuration. Personal paths and the fork owner are now variables: `VARKA_OPS_HOME` (default `~/.varka-ops`), `VARKA_WORKTREES` (the parent of the main checkout), `VARKA_FORK` (read from the `fork` remote), `VARKA_FORK_REMOTE`, `VARKA_REPO` (`vecbricks/varka`), `VARKA_VENV` and `VARKA_TRAILER`.
- The scripts call each other through their own directory, start with `#!/usr/bin/env bash` and `set -u`, and carry the ASF header.
- `README.md` explains the flow, the one-gate-at-a-time rule, the light gate, the exclusive mode, the regen skip and the configuration. `INDEX.md` lists each tool's inputs, outputs, callers and exit codes. `AGENTS.md` (with a `CLAUDE.md` link) gives the rules for agents, the order of steps and the traps of 22-23 November.
- `sql/varka/AGENTS.md` points to the index in one line.

<details>
<summary>Behaviour changes against the private copies</summary>

With `VARKA_OPS_HOME=~/varka-ops` and a `fork` remote, the scripts use the same stamps, locks and logs as before. These are the only changes:

- A worktree argument may be a path or a bare name in every script. An existing directory wins. `open-pr.sh` and `push-no-ci.sh` took only a name before.
- Running gates and watchers are counted from `ps` lines that run `pre-pr.sh` under bash in any directory and any spelling of bash. The old pattern only matched `/bin/bash` plus the private path. The new pattern also counts gates of the private copies, so both can run during the switch.
- The ops home's subdirectories are created when missing. Missing arguments print usage and exit 2.
- `push-ci.sh` and `push-no-ci.sh` stop when the push fails. Before, `| tail -1` hid the failure. `push-no-ci.sh` then cancelled the previous head's runs.
- `matrix-after-build-link.sh` links the run from its own dispatch output, not the newest URL in its log. It exits 1 when the dispatch fails.
- `watch-infra.sh` reruns nothing when no failed job was classified, for example when only a report job failed.
- `matrix-link.sh` stops when no pull request is found. Before, it got `null` and posted to `issues/null`.
- `watch-any-ready.sh` has no default `SKIP`. The old default, apache#1609, is merged.

</details>

### Why are the changes needed?

Every pull request now goes through these scripts, but only one machine had them. Other people and agents could not run the gate or read the rules it enforces.

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

No. Developer tooling and docs only.

### How was this patch tested?

- `bash -n` and `shellcheck -S warning` pass on every script. `dev/varka_precommit.sh` passes.
- A dry run with `VARKA_OPS_HOME` in a scratch directory and a scratch repository covered `pre-pr.sh`'s usage, `-j` without a value, a missing worktree and a dirty worktree. The duplicate-waiter refusal (exit 3) worked. `review-covers.sh` passed four cases: no stamp, stamp at HEAD, a regen commit after it and a master merge after it. It failed a fix commit after the stamp. The fork was parsed from ssh and https URLs.
- The process pattern was checked against sample `ps` lines. It counted a running gate of the private copies.
- The private `pre-pr-slot.sh` gate passed on this branch. It was a light gate, so there was no regen and no sweep.
- Not run: the push, PR and watcher scripts against GitHub.

### Review

`/code-review high` found 8 important items and 2 nits. I fixed three: the lost push status, the matrix linked from an older run, and the infra rerun with no classified job. The other seven are kept as the private copies behave; they are listed below. The size is 968 counted lines.

<details>
<summary>Kept as they are, for a later change</summary>

- `watch-infra.sh` treats a 429 or `toomanyrequests` anywhere in a job log as infrastructure, even when a retry recovered and a test failed later.
- `matrix-link.sh` does not record its follower's pid. An older run's follower can keep editing the shared comment.
- `review-covers.sh` trusts every merge commit, a hand-resolved conflict included. Its regen-subject match is not anchored.
- A stale exclusive marker left by a kill or reboot blocks the queue. Two exclusive waiters can race.
- The regen-skip filter does not count the generated `.tsv` files under `sql/varka/plans/` as inputs.
- `ops-status.sh` looks for `watch-ready.sh` and `watch-failed.sh` by default, which are not in this directory.
- The default commit trailer names one model.

</details>

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

Generated-by: Claude Code (Claude Opus 5.5)
MaxGekk added a commit to MaxGekk/spark that referenced this pull request Nov 23, 2026
… m8 (apache#1666)

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

This adds two rows to `sql/varka/plans/m8/PLAN.md` for two option-matrix findings of 23 November 2026: one random failure and one skip that flips. It also returns row 638 and the first step of row 637 to milestone 8.

- Row 643: `VarkaFallbackReasonSuite`'s test "a planted reason is counted under its own metric..." failed in bin b001 (`isa=avx2`) of apache#1646's matrix, run 45118992922, and passed on rerun. It threw `IllegalStateException: no SQL execution of the session reports the node's metrics` (`VarkaFallbackReasons.java:89`). Row 402 fixed a failure of the same helper on 26 October, with a cause that was guessed and never confirmed. This throw is the branch that fix added. It means no execution in the store held the node's metrics at all, so the guessed cause does not explain it. The row asks for a failure message that lists the store's executions and their metric ids, then the real cause and a fix shown against it.
- Row 644: `VarkaGroupingBoundSuite`'s test "under the exact grouping no shape declines, and none falls back to the greedy grouping" has a `skips.tsv` line under `driverOutputTable=false` that names composition 8. Matrices 45118008614 (apache#1644) and 45123550199 (apache#1643) called the line stale. On apache#1644's head with master merged in, the test fails again, and now composition 16 falls back. The skip list is keyed only by configuration, suite and test name. The test draws 60 compositions with a fixed seed from the coverage table, so every new coverage row re-draws all of them, and the index that falls back moves. The row asks for a verdict that does not depend on which composition falls back: remove the fallback, or excuse projections by their content, never by an index.
- Row 638 (the arm sweep on vecbricks/varka's runners) comes back from `m9/SCOPE.md` item 138 by the owner's decision of 23 November 2026. The local gate's sweep is the throughput limit. The row loses its "Moved" note and gets a dated one. `VARKA-638.md` moves back to `m8/`, and its citations of rows 637 and 522 now point at `m9/`. Item 138 and `PLAN.md` section 8 drop the row and now count 41 rows. Row 638 depends on row 637's step 1, the first stage, which comes back too (next item). On merge the issue mirror (`dev/varka_issues.py`) should reopen apache#1654, the row's issue, which apache#1656 closed.
- Row 637's step 1 (the first stage of a pull request's option matrix: `dev/varka_matrix_scope.py --suites`, the `first` and `rest` lists, the plan artifact, the `stages` input, the two bin jobs and the summary line) comes back to milestone 8 by the owner's decision of 23 November 2026. Row 638 plans from its `dev/varka_matrix_scope.py`, and the fork's 40 slots are the other limit on throughput. Steps 2 and 3 (the optional path tiers, `--replay`, the `auto` default) stay in milestone 9. The row's status is Planned again with a dated note, and in milestone 8 it is done on step 1's done-when: the two test dispatches have run, one live week is committed and predictions 1 to 4 are scored. `VARKA-637.md` moves back to `m8/`, and its citation of row 522 now points at `m9/`. Item 138 keeps only 637's steps 2 and 3 and now counts 40 whole rows; `PLAN.md` section 8 does the same. Row 638 and `VARKA-638.md` no longer say that their dependency is in milestone 9. `VARKA-637.md` 3.1 gains a note: on 23 November the local gate's arm sweep was narrowed to the defaults, `useAVX=2`/`useAVX=3` and the configurations that name a changed option (`varka-gate-sweep-scope`, apache#1667). So `first` keeps "the changed suites under every configuration" on CI as its own definition, not as "`pre-pr.sh`'s arm sweep". On merge the issue mirror should reopen apache#1653, the row's issue, which apache#1656 closed. A dry run of `dev/varka_issues.py` shows both reopens.

Rows 643 and 644 move no census cell. They stay in milestone 8 because each turns red the option matrix that every milestone 8 pull request must pass before merge. Row 644 flips on the very pull requests that add coverage rows.

### Why are the changes needed?

Two tests fail the option matrix at random or as coverage grows, and no open row owns either one. Each red run costs a rerun and holds up a merge. Row 638 comes back because the owner decided that the laptop's sweep is now the limit on how fast pull requests land. Row 637's first step comes back with it: row 638 builds on it, and the fork's slots are the other limit.

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

No. Plan files only.

### How was this patch tested?

Documents only. `dev/varka_plan_lint.py` (also `--check --offline`), `dev/varka_quote_check.py`, `dev/varka_catalogue_move.sh --check 8 9` and the pre-commit hook pass. The failure texts were read from the matrix report jobs of the three runs.

### Review

`/code-review high` ran three times on 23 November 2026: on rows 643 and 644, after row 638's return was added, and after row 637's step 1 was returned. The third round fixed: `VARKA-637.md` 3.1's note says apache#1667 is still open, and `first` keeps its own definition either way; `VARKA-638.md` says its sweep stays the full one and is timed against the full laptop sweeps of 22 November; the tier clause in row 637's done-when is marked as milestone 9's steps 2 and 3; one paragraph was rewrapped. Earlier rounds fixed: Fixed: row 644 now says the test always draws 60 compositions and every coverage row re-draws them, so an excuse must name projections by content, and its done-when needs the same verdict over several draws; the composition-16 reading is marked as having no run id; row 643 closes only on a reproduction or a diagnostic message, not on passing runs; item 138 records that it first listed 42 rows and that row 638 went back; one section reference and one wrapped line were fixed.

Left as they are:
- Both reviews asked to file rows 643 and 644 in milestone 9 under 12.3. They stay in milestone 8, with the reason written in each row: each one turns red the matrix every milestone 8 pull request must pass.
- Not from this branch: `VARKA-638.md` 3.5 and its step 4 still treat `pre-pr.sh` as a `~/varka-ops` script, but apache#1639 moved it into the tree as `dev/varka_ops/pre-pr.sh`. Row 638's build should change the copy in the tree and count it toward the size cap.
- Also not from this branch: `dev/varka_issues.py` does not treat **Filed** as a status, so every Filed row's issue is titled "[VARKA-n] Filed" (622, 631, 632 and 636 already are), and 643 and 644 will be too. Row 356's status still says it is not merged, though apache#1567 merged on 21 November.

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

Generated-by: Claude Code (Claude Opus 5.5)
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.

4 participants