Substring Fails When the Length Is Not a Compile-Time Constant
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- clojure
- Domain
- backend-api-design
Research direction
Read modules/cql/src/blaze/elm/compiler/string_operators.clj and the related string_operators_test.clj tests. Trace how the three-argument substring-op evaluates its length argument, then check existing tests for expression evaluation. Done when runtime-computed lengths return the expected substring and the tests cover both PositionOf and a runtime-only length.
Written by the indexing model from the issue text.
Description
Problem
Substring with a length argument fails when the length is not a compile-time constant:
| Expression | Blaze 1.14.0 | Expected |
|---|---|---|
Substring('abc', 1, 1) |
'b' |
'b' |
Substring('mg/mL', 0, PositionOf('/', 'mg/mL')) |
error | 'mg' |
Substring('mg/mL', 0, (if Now() > @1900-01-01T then 2 else 2)) |
error | 'mg' |
The error is:
Error while evaluating the expression: class blaze.elm.compiler.string_operators$position_of_op$reify__… cannot be cast to class java.lang.Number
Any length that is not reduced to a constant at compile time fails, for example a PositionOf call or a value known only at run time.
Cause
modules/cql/src/blaze/elm/compiler/string_operators.clj (v1.14.0, unchanged on main), the three-argument substring-op:
(-eval [_ context resource scope]
(when-let [^String string (core/-eval string context resource scope)]
(when-let [start-index (core/-eval start-index context resource scope)]
(when (and (<= 0 start-index) (< start-index (count string)))
(subs string start-index (min (+ start-index length)
(count string)))))))
string and start-index are evaluated, but length is used as the compiled expression. A literal, or an expression folded at compile time, compiles to its value, so those lengths work. The tests in string_operators_test.clj evaluate Substring only with literal lengths.
Example in published CQL
The CDC Opioid Prescribing Support IG computes units this way. OMTKLogicMK2020.GetDailyDose uses Substring(strength.unit, 0, PositionOf('/', strength.unit)). OMTKLogic.StripPer uses Substring(unit, 0, Length(unit) - Length(split[splitCount - 1]) - 1).
- Dominant language
- Clojure
- Stars
- 230
- Forks
- 27
- Avg merge
- 9h 20m
- Merged PRs (30d)
- 149
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from samply/blaze
-
Fix Flaky integration-test-kds Load Data StepMay be free again A pull request for this issue was closed without being merged. Openci
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
samply/blaze#4694 · 1 comment ·
Maintainers usually reply within 1 day
-
module:cql
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
module:db
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
module:db module:terminology-service-local refactoring
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
Similar issues
-
SUM over a union column should skip an undefined slot, not throw ArrayIndexOutOfBoundsExceptionOpenexpression engine unshaved-yak
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
WG adoption
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
.Needs Triage .Team/UXWest Priority:P3 Type:Bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
metabase/metabase#88625 · 1 comment ·
Maintainers usually reply within 1 day
-
clojure easy first-contributor first-contributors good first issue help wanted up-for-grabs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
fork-commit-merge/fork-commit-merge#8749 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
unclebob/uml-viewer#18 ·