KNDM
KNDM

Reputation: 49

Kusto: Handling null parameter in user defined funtion

I am writing a custom User defined function in kusto where I want to have some optional 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>

Eg (psuedo code where value is not null):

function Calculate(string:test)
{
  T | where test == test | order by timestamp 
}

Eg (psuedo code where value is null or empty. My final query should look like this):

function Calculate(string:test)
{
  T | order by timestamp 
}

What is the efficient way to implement this. I will call this function from my c# class.

Upvotes: 1

Views: 2026

Answers (1)

Yoni L.
Yoni L.

Reputation: 25955

you can define a function with a default value for its argument, and use the logical or operator and the isempty() function to implement the condition you've described.

for example:

(note: the following is demonstrated using a let statement, but can be applied similarly to stored functions)

let T = range x from 1 to 5 step 1 | project x = tostring(x)
;
let F = (_x: string = "") {
    T
    | where isempty(_x) or _x == x
    | order by x desc
}
;
F("abc")
  • if you run F() or F("") (i.e. no argument, or an empty string as the argument) - it will return all values 1-5.
  • if you run F("3") - it will return a single record with the value 3.
  • if you run F("abc") - it will return no records.

Upvotes: 0

Related Questions