Help to Return Only Non-Empty Array from Filter Activity in Set Variable Expression

libpekin 166 Reputation points
2024-09-22T00:26:09.9633333+00:00

Hello,

I have a pipeline that retrieves metadata from a data lake, filters for files ending in "A", and then sets an external table creation script by concatenating the DDL, folder path, and file name with the suffix "A".

I'm encountering an error stating "...cannot be evaluated because array index '0' cannot be selected from an empty array." It appears that folders without an "A" are being returned as empty, which causes the error. Is there a method to return only the non-empty array element.User's image

@concat(item().DDL,'''',variables('FolderPath'),activity('Filter for File Ending with A').output.value[0].name)
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
{count} votes

2 answers

Sort by: Most helpful
  1. libpekin 166 Reputation points
    2024-09-23T10:03:41.0233333+00:00

    Here's how I solved this. To ensure I didn't return an empty filter activity array, the workaround is to use nested or functions. Below is the modified filter expression:

    {

    "type": "Expression",
    
    "value": "@or(contains(item().name, 'M'), or(contains(item().name, '31'), contains(item().name, '30')))"
    

    }

    This ensures I get at least three files returned in the filter array output. Although, I still have the issue of picking the right file index in my set variable expression, but the above gets me closer to an ideal solution.


  2. Chandra Boorla 14,585 Reputation points Microsoft External Staff Moderator
    2024-09-23T10:22:52.34+00:00

    Hi @libpekin

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to accept the answer.

    Issue:

    I have a pipeline that retrieves metadata from a data lake, filters for files ending in "A", and then sets an external table creation script by concatenating the DDL, folder path, and file name with the suffix "A".

    I'm encountering an error stating "...cannot be evaluated because array index '0' cannot be selected from an empty array." It appears that folders without an "A" are being returned as empty, which causes the error. Is there a method to return only the non-empty array element.

    @concat(item().DDL,'''',variables('FolderPath'),activity('Filter for File Ending with A').output.value[0].name)
    
    

    Solution:

    Here's how I solved this. To ensure I didn't return an empty filter activity array, the workaround is to use nested or functions. Below is the modified filter expression:

    {

    "type": "Expression",
    
    "value": "@or(contains(item().name, 'M'), or(contains(item().name, '31'), contains(item().name, '30')))"
    

    }

    This ensures I get at least three files returned in the filter array output. Although, I still have the issue of picking the right file index in my set variable expression, but the above gets me closer to an ideal solution.

    If I missed anything please let me know and I'd be happy to add it to my answer, or feel free to comment below with any additional information.

    Hope this helps. Do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.