mjsxbo
mjsxbo

Reputation: 2266

How to loop over a query in kusto?

I have a kusto function A which takes a string argument and returns a single row.

query: A("apples")
output: apples  123 32

I want to run this query over an array of strings as arguments and combine all the output rows to form a table.

query: list=["apples","banana","pear"]
       foreach s in list:
          A(s)
output should be: 
       apples   123 32
       banana   54  63
       pear     7   243

Is it possible to do this? I couldn't find any way to do a for-loop in kusto

Upvotes: 2

Views: 1686

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44941

KQL is a declarative language, similarly to SQL.
It does not support control flow commands such as loop, go, if-then-else etc.

That said, there is a work-around to achieve what you are seeking for.
The following solution is limited to 64 values (the max number of partitions supported by the legacy algorithm of the partition operator).
If needed this solution can be expanded to support more values.

let A = (x:string){ print x = x, y = hash(x, 991) ,z = hash(x, 997) };
let A_wrapper = (T:(x:string)){ let p_x = toscalar(T | project x); A(p_x); };
let list = dynamic(["apples","banana","pear"]);
print x = list
| mv-expand with_itemindex=i x to typeof(string)
| partition by i (invoke A_wrapper())
x y z
apples 842 528
banana 609 116
pear 112 631

Fiddle

Upvotes: 1

Related Questions