Skip to content

#763 Implement relaxed type restrictions on which field can be used for record length. #764

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

Merged
merged 2 commits into from
May 30, 2025

Conversation

yruslan
Copy link
Collaborator

@yruslan yruslan commented May 30, 2025

Closes #763

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of decimal data types with zero scale, allowing them to be treated as integral types in record length and dependency logic.
    • Enhanced error reporting for invalid string values in record length fields, providing clearer messages when parsing fails.
    • Replaced certain exceptions with warning logs for non-integral record length fields, improving user feedback.
  • Tests

    • Added new test cases to cover decimal depending fields, numeric mappings, and string value handling in record length scenarios.
    • Removed a test that previously checked for exceptions with fractional length fields.

Copy link

coderabbitai bot commented May 30, 2025

"""

Walkthrough

The changes update Cobrix to support decimal fields with zero scale as valid record length fields, especially when the strict_integral_precision option is enabled. This includes modifications to type checks, error handling, and logging, as well as the addition and adjustment of related test cases to verify correct handling of decimal record length fields.

Changes

File(s) Change Summary
.../parser/asttransform/DependencyMarker.scala Updated logic to treat Decimal fields with scale 0 as valid for dependee marking alongside Integral types.
.../reader/extractors/raw/FixedWithRecordLengthExprRawRecordExtractor.scala Enhanced type handling for record length fields: support for BigDecimal, improved error reporting for String.
.../reader/validator/ReaderParametersValidator.scala Replaced exception with warning log for non-integral record length fields, added SLF4J logger instance.
.../reader/iterator/VRLRecordReaderSpec.scala Removed test that expected exception for fractional record length fields.
.../source/integration/Test37RecordLengthMappingSpec.scala Added test for numeric mapping with strict integral precision enabled.
.../source/regression/Test21VariableOccursForTextFiles.scala Added test for variable occurs with decimal depending fields and strict integral precision.
.../source/regression/Test26FixLengthWithIdGeneration.scala Added/updated tests for segment ID generation with strict integral precision and string value handling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CobrixReader
    participant Validator
    participant Extractor

    User->>CobrixReader: Read data with record length field (may be Decimal)
    CobrixReader->>Validator: Validate record length field type
    Validator-->>CobrixReader: Log warning if not integral/decimal(scale=0), allow Decimal(scale=0)
    CobrixReader->>Extractor: Extract record length
    Extractor->>Extractor: Accept Integral or Decimal(scale=0), convert as needed
    Extractor-->>CobrixReader: Return record length or error if invalid
    CobrixReader-->>User: Data read or error
Loading

Assessment against linked issues

Objective Addressed Explanation
Support record length fields with decimal type (scale=0) when strict_integral_precision=true (#763)
Remove/adjust exception for decimal record length fields, update validation and error handling (#763)
Add/adjust tests to verify decimal record length fields are accepted and processed correctly (#763)

Poem

A decimal hop, a scale of naught,
Now record lengths are easily caught!
With tests anew and warnings clear,
No fraction left to cause us fear.
Cobrix leaps, precision true—
This bunny celebrates with you!
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c9c3986 and 42f5c67.

📒 Files selected for processing (1)
  • spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/regression/Test26FixLengthWithIdGeneration.scala (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/regression/Test26FixLengthWithIdGeneration.scala
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Spark 3.5.5 on Scala 2.12.20
  • GitHub Check: Spark 3.5.5 on Scala 2.13.16
  • GitHub Check: Spark 2.4.8 on Scala 2.11.12
  • GitHub Check: Spark 3.4.4 on Scala 2.12.20
  • GitHub Check: test (2.12.20, 2.12, 3.3.4, 0, 80, 20)

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or {PR Summary} to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented May 30, 2025

JaCoCo code coverage report - 'cobol-parser'

Overall Project 91.37% -0.23% 🍏
Files changed 22.16% 🍏

File Coverage
DependencyMarker.scala 92.86% 🍏
FixedWithRecordLengthExprRawRecordExtractor.scala 69.3% -14.89% 🍏
ReaderParametersValidator.scala 65.84% -8.42% 🍏

Copy link

JaCoCo code coverage report - 'spark-cobol'

There is no coverage information present for the Files changed

Total Project Coverage 81.99% 🍏

@yruslan yruslan requested a review from Copilot May 30, 2025 07:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR relaxes type checks for record length fields, allowing zero-scale decimals and improving handling of non-integral values, while updating tests to cover these scenarios.

  • Allow decimal types with scale 0 as valid record length fields
  • Replace hard errors with warnings for non-integral fields and improve parsing error messages
  • Add tests for numeric mappings, string-based lengths, and variable-length occurs

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/regression/Test26FixLengthWithIdGeneration.scala Added tests for string record length handling and updated temp file naming
spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/regression/Test21VariableOccursForTextFiles.scala Added a variable-occurs test with decimal depending fields
spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/integration/Test37RecordLengthMappingSpec.scala Added a test for numeric record-length mappings under strict mode
cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/reader/validator/ReaderParametersValidator.scala Switched exception to warning for non-integral length fields
cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/reader/extractors/raw/FixedWithRecordLengthExprRawRecordExtractor.scala Enhanced string parsing with Try and added BigDecimal support
cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/asttransform/DependencyMarker.scala Allowed zero-scale decimals to mark record-length dependencies
Comments suppressed due to low confidence (2)

spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/source/regression/Test26FixLengthWithIdGeneration.scala:197

  • [nitpick] This test reuses the same description as an earlier one. Rename it to clearly distinguish the string-handling scenario from the EBCDIC numeric tests.
"correctly work with segment id generation option with length field" in {

cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/reader/extractors/raw/FixedWithRecordLengthExprRawRecordExtractor.scala:128

  • [nitpick] The exception message ends with a period inside the quotes and includes mixed punctuation. For consistency, standardize on a format without the trailing period or match other messages in style.
case s: String     => Try{ s.toInt }.getOrElse(throw new IllegalStateException(s"Record length value of the field ${lengthAST.name} must be an integral type, encountered: '$s'."))

case i: Int => i
case l: Long => l.toInt
case s: String => Try{ s.toInt }.getOrElse(throw new IllegalStateException(s"Record length value of the field ${lengthAST.name} must be an integral type, encountered: '$s'."))
case d: BigDecimal => d.toInt
Copy link
Preview

Copilot AI May 30, 2025

Choose a reason for hiding this comment

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

The BigDecimal branch unconditionally truncates values, which can silently drop fractional parts. Consider validating that d.scale == 0 or using d.intValueExact() to ensure an error is thrown for non-zero scale.

Suggested change
case d: BigDecimal => d.toInt
case d: BigDecimal => Try { d.intValueExact() }.getOrElse(throw new IllegalStateException(s"Record length value of the field ${lengthAST.name} must be an integral type, encountered: '$d'."))

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tested toInt vs toIntExact. The behavior of toInt is still what I want in this context. When decimals are used as record lengths teh fractional part needs to be truncated, not rounded.

@yruslan yruslan merged commit 7b21b21 into master May 30, 2025
7 checks passed
@yruslan yruslan deleted the bugfix/763-record-length-strict-precision branch May 30, 2025 07:46
@yruslan yruslan mentioned this pull request Jun 5, 2025
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.

Record length columns cannot be used when strict_integral_precision=true
1 participant