Skip to content

Bug: Statements silently dropped when CQL has no library declaration + DateTime constructors#1706

Merged
JPercival merged 3 commits intomainfrom
fix/datetime-operators-chunk-error
Mar 16, 2026
Merged

Bug: Statements silently dropped when CQL has no library declaration + DateTime constructors#1706
JPercival merged 3 commits intomainfrom
fix/datetime-operators-chunk-error

Conversation

@JPercival
Copy link
Copy Markdown
Contributor

Summary

When a CQL file has no library declaration and uses DateTime() constructors, the translator's chunk tracking fails after the first expression definition with:

Child chunk cannot be added because it is not contained within the parent chunk.

All subsequent statements are silently dropped from the ELM output. For example, OperatorTests/DateTimeOperators.cql (71 define statements) produces only 1 statement in the ELM.

Repro

Minimal CQL file (no library, no using, no context):

define First: DateTime(2014, 1, 1) + 1 day
define Second: DateTime(2014, 12, 31) after DateTime(2014, 1, 1)
define Third: DateTime(2014, 1, 1) before DateTime(2014, 12, 31)

Expected: 3 statements in ELM, no errors
Actual: 1 statement (First), 1 error ("Child chunk cannot be added...")

Test plan

  • ChunkErrorTest reproduces the bug — asserts all 3 statements emitted and no errors
  • Test currently fails (this PR is the repro, not the fix)

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 16, 2026

Related Issues

The following open issues may be related to this PR:

Issue Title Score Matched Terms
#1400 patientBirthDatePropertyName marked as deprecated but still required for SystemFunctionResolver 19.5 "cql library", com, define, within, code, context, because, library, https, translator, elm, cannot, example, error (path), cqframework (path), java (path), src (path), cql2elm (path), main (path), org (path), cql (path), model (path)
#1430 Correctly support CQL to FHIR (and vice versa) type mapping 18.5 "cql library", "context cql", definition, com, code, context, library, https, expected, currently, example, resources (path), cqframework (path), java (path), src (path), main (path), org (path), cql (path)
#841 Translator (2.3.0) Unions Same Retrieve With Itself 16 "cql library", definition, define, bug, context, library, expression, https, translator, expected, elm, produces, example, org (path), cql (path)
#1181 Support direct-reference codes for plural terminology targets 15.5 "cql library", "cql define", tracking, define, bug, code, context, library, cannot, example, error (path), org (path), cql (path)
#856 Setting DataRequirement.type value in CQL library errors 15 "cql file", "cql library", file, define, bug, code, errors, repro, library, error (path), org (path), cql (path), model (path)

Tip: If this PR addresses any of these issues, please link them using Closes #NNN or Refs #NNN in the PR description.

@github-actions
Copy link
Copy Markdown

Formatting check succeeded!

@JPercival JPercival force-pushed the fix/datetime-operators-chunk-error branch from a845233 to 91169fd Compare March 16, 2026 16:33
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.96%. Comparing base (7dc65a1) to head (2b1d3c4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../kotlin/org/cqframework/cql/cql2elm/model/Chunk.kt 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1706      +/-   ##
============================================
- Coverage     61.97%   61.96%   -0.01%     
  Complexity     3940     3940              
============================================
  Files           210      210              
  Lines         20366    20367       +1     
  Branches       3879     3879              
============================================
  Hits          12621    12621              
- Misses         6138     6139       +1     
  Partials       1607     1607              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…eclaration

When a CQL file has no `library` declaration and uses DateTime
constructors, Chunk.addChunk() throws IllegalArgumentException because
the child chunk's source interval falls outside the parent's bounds.
This exception is caught by the generic error handler in
CqlPreprocessorElmCommonVisitor.visit(), which records it as a
compilation error and replaces the expression with Null — silently
dropping the statement from ELM output.

Root cause: forward reference or function resolution during expression
processing can produce chunks with intervals that extend beyond the
library chunk's original interval. The require() in addChunk was too
strict for a best-effort annotation/narrative system.

Fix: expand the parent chunk interval to accommodate the child rather
than failing. The chunk system supports source annotation and narrative
generation — it should not block compilation.

Repro: 3-statement CQL without library declaration using DateTime().
Before: 1 statement emitted, 1 error.
After: all 3 statements emitted, no errors.

Also fixes OperatorTests/DateTimeOperators.cql (was: 1/71, now: 71/71).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@JPercival JPercival force-pushed the fix/datetime-operators-chunk-error branch from 91169fd to 51a178f Compare March 16, 2026 17:50
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@JPercival JPercival merged commit 75aa1b6 into main Mar 16, 2026
9 of 10 checks passed
@JPercival JPercival deleted the fix/datetime-operators-chunk-error branch March 16, 2026 19:29
JPercival added a commit that referenced this pull request Mar 16, 2026
- properly between: PR #1704 merged
- div/truncated divide: PR #1705 merged
- chunk-tracking error: PR #1706 merged

Renamed to "Translator Issues" since these aren't "legacy" yet.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
JPercival added a commit that referenced this pull request Mar 28, 2026
- properly between: PR #1704 merged
- div/truncated divide: PR #1705 merged
- chunk-tracking error: PR #1706 merged

Renamed to "Translator Issues" since these aren't "legacy" yet.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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