Length throws TypeError when its null operand is not an As expression (e.g. an expression reference)

Open
#412 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
api, backend

Research direction

Start with Length.exec in src/elm/overloaded.ts and inspect the translated ELM signature and operand result type mentioned in the issue. Reproduce with the LengthNull examples, checking both expression references and typed null literals. Done when Length(S) returns null and Length(LS) returns 0 without throwing; the issue does not name a test file.

Written by the indexing model from the issue text.

Description

Length throws a TypeError when its argument is null and the operand is not an As expression, for example an expression reference. The typed null literal forms work.

library LengthNull version '1.0.0'

define S: null as String
define LS: null as List<Integer>

define LengthOfNullStringLiteral: Length(null as String)          // null
define LengthOfNullStringRef: Length(S)                            // TypeError
define LengthOfNullListLiteral: Length(null as List<Integer>)      // 0
define LengthOfNullListRef: Length(LS)                             // TypeError

Error for the two failing definitions:

Encountered unexpected error during execution.

	Error Message:	Cannot read properties of undefined (reading 'type')
	CQL Library:	LengthNull|1.0.0
	Expression:	Length

Expected:

  • Length(S): null, as for a null String (Appendix B; cql-tests LengthNullString);
  • Length(LS): 0, as for a null List (Appendix B; see also #168).

Cause: for a null argument, Length.exec reads this.arg.asTypeSpecifier.type. Only an As operand has asTypeSpecifier; for an ExpressionRef it is undefined:

https://github.com/cqframework/cql-execution/blob/master/src/elm/overloaded.ts#L322

The ELM already carries what is needed to choose the overload. In the translated library above, every Length element has a signature (NamedTypeSpecifier String or ListTypeSpecifier), and the operand has resultTypeName / resultTypeSpecifier. A possible fix is to use the signature or the operand's result type, and not to throw when no type information is present.

Environment:

  • cql-execution 3.3.2 and 4.0.0-beta.1 (same line in both);
  • ELM from @cqframework/cql 5.8.0 (npm), with EnableResultTypes;
  • Node 24, Windows 11.

The reference engine has the same pattern: cqframework/clinical_quality_language#1976.

Dominant language
JavaScript
Stars
86
Forks
40
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cqframework/cql-execution

All issues in cqframework/cql-execution

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.