Skip to content

#847 Fix array flattening in Spark 4+.#848

Merged
yruslan merged 2 commits into
masterfrom
bugfix/847-fix-schema-flattenning-on-spark4
May 11, 2026
Merged

#847 Fix array flattening in Spark 4+.#848
yruslan merged 2 commits into
masterfrom
bugfix/847-fix-schema-flattenning-on-spark4

Conversation

@yruslan
Copy link
Copy Markdown
Collaborator

@yruslan yruslan commented May 7, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Improved array flattening to correctly handle arrays and nested arrays across Spark versions (including Spark 4+), fixing incorrect projections of primitive array elements.
    • Enhanced detection of Spark version to choose the appropriate array access method, preserving compatibility with legacy Spark releases.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6a76b4b7-0a95-4f16-8df7-28718bfecd26

📥 Commits

Reviewing files that changed from the base of the PR and between 4518dea and fb7d49e.

📒 Files selected for processing (1)
  • spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala
🚧 Files skipped from review as they are similar to previous changes (1)
  • spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala

Walkthrough

flattenSchema's array-handling now detects Spark 4+ and generates version-appropriate array access expressions (get(path, index) vs path[index]) via a helper, and applies this consistently when flattening struct arrays, nested arrays, and when selecting expr/col for primitive paths.

Changes

Spark 4+ Array Indexing Compatibility

Layer / File(s) Summary
Version Check & Helper Function
spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala
Spark session version check (isUseArrayGet) and getArrayIndexExpr helper introduced to generate get(path, index) expressions for Spark 4+ or bracket indexing path[index] for earlier versions.
Struct Array Flattening
spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala
flattenStructArray updated to use version-aware getArrayIndexExpr for constructing array element field expressions, with corresponding adjustments to primitive projections and stringFields generation.
Nested Array Flattening
spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala
flattenNestedArrays updated to use version-aware array element access when recursing into nested structures and when generating primitive projections and their stringFields expressions.
Primitive Path Expression Selection
spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala
flattenGroup's check for using expr(...) vs col(...) expanded to treat ( like [ so newly generated get(...) strings are handled correctly.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Poem

🐰 Spark strides on; arrays now speak in two tongues,
Bracket or get, the helper quietly hums.
Fields unfold tidy where indices align,
Cross-version carrots for schemas divine,
A hop, a patch, and flat rows for our runs!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing array flattening for Spark 4+, which directly aligns with the changeset's focus on Spark-version-aware array element access expressions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/847-fix-schema-flattenning-on-spark4

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala (1)

198-203: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

path.contains('[') log-path selector misses Spark 4 get(…) expressions.

In flattenGroup, when a path arrives from flattenStructArray or flattenNestedArrays on Spark 4, it will look like get(parentfield, 0). — no [ present — so path.contains('[') evaluates to false and the log entry uses col(...) syntax even though the path is a SQL expression that col cannot evaluate. This only affects the stringFields log output (line 209), not actual execution, but the logged "flattening code" becomes incorrect/misleading for Spark 4 paths.

🔧 Proposed fix
-          if (path.contains('['))
+          if (path.contains('[') || path.contains('('))
             stringFields += s"""expr("$path`${field.name}` AS `$newFieldName`")"""
           else
             stringFields += s"""col("$path`${field.name}`").as("$newFieldName")"""
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala`
around lines 198 - 203, The log-path selector in flattenGroup currently checks
only path.contains('[') so Spark 4 SQL-style paths like get(parent`field`, 0)
are misclassified; update the condition that decides whether to append an
expr(...) vs col(...) to stringFields to also detect SQL/get-style expressions
(for example check path.contains("get(") or otherwise detect '(' in the path)
and use the expr(...) branch for those cases; locate the logic in flattenGroup
where stringFields is appended (related symbols: flattenGroup,
flattenStructArray, flattenNestedArrays, path, stringFields, fields,
getNewFieldName) and change the conditional so Spark 4 get(...) expressions are
logged with expr(...) rather than col(...).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala`:
- Around line 129-133: In flattenStructArray's primitive-array branch the actual
Column added to fields still uses bracket indexing
(expr(s"$path`${structField.name}`[$i]")) which fails on Spark 4; change the
Column expression to use getArrayIndexExpr(fieldName, i) like stringFields does
so fields and stringFields are consistent; update the fields += expr(...) call
inside flattenStructArray to build the Column via
expr(getArrayIndexExpr(fieldName, i)) and keep the existing
.as(getNewFieldName(...), structField.metadata) so the selection matches the
logger output.

---

Outside diff comments:
In
`@spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala`:
- Around line 198-203: The log-path selector in flattenGroup currently checks
only path.contains('[') so Spark 4 SQL-style paths like get(parent`field`, 0)
are misclassified; update the condition that decides whether to append an
expr(...) vs col(...) to stringFields to also detect SQL/get-style expressions
(for example check path.contains("get(") or otherwise detect '(' in the path)
and use the expr(...) branch for those cases; locate the logic in flattenGroup
where stringFields is appended (related symbols: flattenGroup,
flattenStructArray, flattenNestedArrays, path, stringFields, fields,
getNewFieldName) and change the conditional so Spark 4 get(...) expressions are
logged with expr(...) rather than col(...).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 89b865c8-138f-40cc-808c-24df907d5622

📥 Commits

Reviewing files that changed from the base of the PR and between 672bde3 and 4518dea.

📒 Files selected for processing (1)
  • spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/utils/SparkUtils.scala

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

JaCoCo code coverage report - 'cobol-parser'

Overall Project 91.05% 🍏

There is no coverage information present for the Files changed

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

JaCoCo code coverage report - 'spark-cobol'

Overall Project 83.4% -0.7% 🍏
Files changed 72.97%

File Coverage
SparkUtils.scala 92.21% -9.92%

@yruslan yruslan merged commit e7a09b5 into master May 11, 2026
7 checks passed
@yruslan yruslan deleted the bugfix/847-fix-schema-flattenning-on-spark4 branch May 11, 2026 13:12
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.

1 participant