Let's say I have a KQL query that uses several tables to retrieve the data. I need to write some code in C#, that will take all the tables used by a given KQL query, and put all those table names into a list.
Simply put: I need to analyze each KQL query to know from which tables it gets the data.
I already tried doing so by writing this code:
var query = "Table1 | project a ,b,c";
var code = KustoCode.Parse(query);
var parseCode = code.Analyze();
Console.WriteLine(parseCode.ResultType.Display.ToString());
But this doesn't return the tables names, but instead it returns the columns names that this query used, which is not what I want.
If you could help me solve this I would greatly appreciate it!
Thanks for the help! I finally was able to find a solution for this so here is my code:
var code = KustoCode.Parse(query).Analyze();
SyntaxElement.WalkNodes(code.Syntax,
Operator =>
{
if (Operator is Expression e && e.RawResultType is TableSymbol && Operator.Kind.ToString() == "NameReference")
tables.Add(e.RawResult.Name);
})
Note that very simple query expressions such as "mytable" will not be interpreted correctly since they are inherently ambiguous.