Substring Fails When the Length Is Not a Compile-Time Constant

Open Beginner friendly
#4,824 0 comments 0 reactions 0 assignees View on GitHub

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

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

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 samply/blaze

All issues in samply/blaze

Similar issues

More Clojure issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.