Search code examples
azure-data-explorerkqlkusto-explorer

How to print all rows if no parameter is passed in Kusto Function


I have a requirement to print all rows if parameter is null is passed and if not null, use it in where clause

I am writing a Kusto function where I want to have some fixed parameters to the function which will be used in the "where" clause. I want to dynamically handle this. For example: If the value is present, then my where clause should consider that column filter, else it should not be included in the "where" clause>

I saw this question which seems to be doing it but unbale to fit in my requirement to the pseudo code in the this post.

My Table Name: SecurityGroups My where clause operator is GroupType My Current Sample Kusto Function:

.create-or-alter function fnGetSecurityGroups(groupType:string) {
SecurityGroups | where GroupType =~ groupType
}


Solution

  • The same query by @Yoni L. in the post which you have provided will work for you.

    As per the Documentation,

    null values are not supported for string data type.

    So, use the empty string("") as the default value for the string parameter in your function.

    As you want to print the rows, after the where clause, don't add anything.

    My sample:

    let r = datatable(name:string, game:string)
    [   
        "Rakesh","TT",
        "Laddu","BB",
        "Virat","Cricket"
    ];
    
    let F1 = (_name: string = "") {
        r
        | where isempty(_name) or name == _name
    };
    
    F1("Rakesh");
    

    Here, as we are using or operator,

    • first it will check whether the given value is empty string or not. If it is empty string(True), then it won't check the next condition in where and it will directly print all the rows.
    • If it is not empty string, then it will check the where column condition and prints the rows as per the condition.

    My output when no value provided:

    enter image description here

    When value provided:

    enter image description here