issue with substring in new node "Expression"

Hi everybody,
I am trying to subtract a string with this new node "Expression " and I get an error!
I attached the screenshot to this post.
Any thoughts?
regards,
Peyman

I encountered the same issue when recreating a simple example:

It appears that it doesn’t like to get the length argument as a column - if I replace with a “hardcoded” number it works… :

Feels like a bug

2 Likes

It might not like that the arguments you supplied can be missing because it comes from a column and not from a literal value. (Indicated by the question mark after the type I think – I don’t have experience with the language yet, but it seems intuitive to me that this is the case).

Edit: i tested it and it works if you wrap the column in parse_int(string($["column"])) but based on the documentation, I am not sure why it works:

  • string(...) produces "MISSING" for a missing value
  • parse_int(...) produces a missing value if the input is not an integer
    So it should end up with a missing value either way – the output signature of parse_int also mentions it: INTEGER | MISSING… which I assume is the long-form for INTEGER?.
1 Like

based on @hotzm answer I suppose I could have missing value somewhere down in my data and command recognize that missing value so it doesnt let me to use the command

Is this what you mean?

In my simple example it still produces an error - at least if the column is of INTEGER type.

If I change the column to string it also does not seem to work:

When hovering over the function I see this documentation:

Arguments
string: Original string
start: Start index (inclusive, 1-indexed)
length: Length - if unspecified, or bigger than the string, get entire string after the start
Return value
STRING | MISSING
Extracted substring

Description
Get a substring of the string. If the length is unspecified or bigger than the remaining characters in the string after the start index, it returns the entire substring after the start index. The start index begins at 1.

**If any of the arguments are MISSING, the result is also MISSING.**

The last sentence sounds like it should be working even if there is a risk of missing values in any of the input arguments…

1 Like

yeah you are right so now I don’t have any idea why I got an error

Is this what you mean?

parse_int doesn’t take an Integer? (as it says), so I converted the Integer column to a string with string(...).

With my int->string->int dance I think I was able to satisfy the expression engine.

So, it is a bug, that the function refuses the length argument from a column directly (internal ticket AP-23042). Thanks for reporting!

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Hi everyone,

I wanted to wrap up our discussion on the bugs we’ve encountered and explain how they will be addressed in version 5.3.2.

  1. substring Does Not Accept INTEGER | MISSING as Length

    As we’ve discussed, substring($value, 0, $target_length) currently fails because $target_length can be MISSING. While this behavior was intentional, it’s not ideal. In version 5.3.2, this will be fixed so that if $target_length is MISSING, the function will behave as if the length argument is omitted, returning the substring from the start to the end of the input.

  2. parse_int Type Inference Incorrectly Returns INTEGER for Non-Optional Input

    The expression parse_int("not an integer") should result in MISSING, but due to a bug, the type inference mistakenly returns INTEGER, which does not accommodate MISSING values. This will be corrected in 5.3.2, ensuring that the type inference accurately reflects when MISSING is possible.

    For example, the workaround discussed earlier:

    substring($value, 0, parse_int(string($target_length)))
    

    currently works because of the type inference bug. However, once the bug is fixed in 5.3.2, this workaround will no longer be possible (but also unnecessary, as Bug 1 will be resolved).

As a more robust approach, I’d also like to mention the “Missing coalescing operator” (??). This operator is designed to handle missing values and could be used as a workaround like this:

substring($value, 0, $target_length ?? 0)

Once 5.3.2 is released, these bugs will be fixed, and the need for hacky workarounds should be eliminated.

3 Likes