Generate local table from string

Copper Contributor

Hi,

I have a table where one column consists of strings with point coordinates. I would like to evaluate the content of this point list for queries. To do this, i wanted to use a custom function that splits the string, performs queries (here: is the origin present in the point list) and returns a scalar.

Therefore i am expanding a dummy table with the string content:

 

let primitives=datatable(name:string, point_string:string)
[
    "prim0", "[[0 0],[1 1],[2 0]]",
    "prim1", "[[1 0],[1 1],[2 2],[3 3]]"
];
let unwrap= (array:string) { substring(array,1, string_size(array)-2)
};
let count_points_with_xy_zero = (point_string:string) {
    toscalar( datatable(dummy:int)[1] 
    | extend points=split( unwrap(point_string),",")
    | mv-expand points
    | extend px = todecimal(split(unwrap(points)," ")[0])
    | extend py = todecimal(split(unwrap(points)," ")[1])
    | where px == 0 and py == 0
    | count)
};
primitives
 | where count_points_with_xy_zero(point_string) > 0

Unfortunately, i get an error: " Semantic error: Unresolved reference binding: 'point_string'".

The custom function works, if i pass a string directly, e.g.:

 | where count_points_with_xy_zero("[[0 0],[1 1],[2 0]]") > 0

 I re-wrote the query with an inline expansion which works as well:

let primitives=datatable(name:string, point_string:string)
[
    "prim0", "[[0 0],[1 1],[2 0]]",
    "prim1", "[[1 0],[1 1],[2 2],[3 3]]"
];
let unwrap= (array:string) { substring(array,1, string_size(array)-2)
};
primitives
 | extend points=split( unwrap(point_string),",")
 | mv-expand points
 | extend px = todecimal(split(unwrap(points)," ")[0])
 | extend py = todecimal(split(unwrap(points)," ")[1])
 | where px == 0 and py == 0
 | project-away points, px, py
 | summarize take_any(*) by name

Is there a way to implement the desired functionality in a custom function or is this outside the specification of a custom function?

 

Thanks in advance,

Henning

0 Replies