Question on 'cast' and 'typeof'

In some of the SAIL recipes, I sometimes see a construct like:

union(local!datasubset.data.x, cast(typeof(local!datasubset.data.x),{}))

I'm not sure why 'cast' and 'typeof' are used in this case. In all of the use cases I've run , I get the same results if I do:

union(local!datasubset.data.x, local!datasubset.data.x) to get 'distinct' values

Am I wrong? Missing something important?

OriginalPostID-195870

OriginalPostID-195870

  Discussion posts and replies are publicly visible

  • @richard.nolan You can obtain unique values by comparing an array that holds valid values with an array that holds empty values, but both arrays should be of same type.

    Here goes an example at https://forum.appian.com/suite/help/16.1/Set_Functions.html#union.28.29:

    Remove the duplicates from an array by comparing it with an empty array of the same type:
    union({1, 2, 3, 4, 1, 2}, tointeger({})) returns and array with 1, 2, 3, 4

    So, if we observe above example, following can be understood:
    1. {1, 2, 3, 4, 1, 2} is a dataset of type Number Integer Array.
    2. Initially {} is an empty array. Applying tointeger() over the empty array turns it into an Number Integer Array.

    Let's correlate the above example with union(local!datasubset.data.x, cast(typeof(local!datasubset.data.x),{}))

    So, if we observe union(local!datasubset.data.x, cast(typeof(local!datasubset.data.x),{})), following can be understood:
    1. local!datasubset.data.x is a dataset of some custom data type and it holds some valid data as well.
    2. Initially {} is an empty array. And further we are converting the data type of this empty array, that is, {}, to the data type of the first array. cast() is a function which will aid us in doing this conversion, and typeof() is the function which will help us in determining the data type.


    There is also an other approach to get distinct values, that is, comparing an array with itself. This is what you have opted for, which is as follows:
    union(local!datasubset.data.x, local!datasubset.data.x)

    There isn't anything wrong but I would like to suggest you to compare the array with an empty array, i.e. union(local!datasubset.data.x, cast(typeof(local!datasubset.data.x),{})). Comparing a array with an array is useful from my perspective only when both of the arrays are different.

    As per my knowledge, in case of union(local!datasubset.data.x, local!datasubset.data.x) Appian needs to work on two datasets individually to remove duplicate values in each and one more time on the resultant dataset to remove duplicate values. Whereas, in case of union(local!datasubset.data.x, cast(typeof(local!datasubset.data.x),{})), working on a single dataset is sufficient. But let's see if any other practitioners can comment on this from the performance perspective.

    If you aren't aware of the Casting, I would like to suggest to have an idea over it as it would be extremely helpful in SAIL interfaces.
  • Holy smokes, I hadn't even considered the performance potential. That was an incredibly thorough description!

    Thanks, it is much clearer now!