microsoft / Kusto-Query-Language

Kusto Query Language is a simple and productive language for querying Big Data.
Apache License 2.0
562 stars 104 forks source link

List output columns/schema from query #90

Closed sunilkr closed 2 years ago

sunilkr commented 2 years ago

This seems to be harder than I thought, but is there a way to find the output schema of the query (or list of Columns) using C# package? Tried the following but I am not getting any ideas how to do this.

using Kusto.Language;
using Kusto.Language.Syntax;

var query = "T |extend d = a+b | summarize take_any(d) by c | where d > 10";
var code = KustoCode.Parse(query);
Console.WriteLine(code.MaxDepth);

SyntaxElement.WalkNodes(code.Syntax,
    n =>
    {
        Console.WriteLine($"{n}: {n.GetType()}, {n.ChildCount}, {n.Depth}/{code.MaxDepth}");
    }
);

I was hoping that edge nodes will not have children, so those could be the ones, but that's not true. I could not find anyway to do this from README and there aren't much on stackoverflow on this.

mattwar commented 2 years ago

The KustoCode instance (code in your example) has the property ResultType that tells you the result type of the query. The property itself is typed as Symbol, but it is typically a tabular result represented by the derived type TableSymbol. TableSymbol has a collection of columns.

var columns = ((TableSymbol)code.ResultType).Columns;

You should probably be more cautious than my example, as it is possible that a query may not describe a tabular result. Also, it is possible to have a query result in multiple result tables. This happens if there is more than one query expression in the text of the query. The KustoCode.ResultType property only tells you about the result type of the last query expression in the text of the query. If you need to know all of them, you'll have to fish them out yourself. This can be done by looking into the KustoCode.Syntax property. If the text of the query is actually a query, and not a command or directive, then the root syntax node type will be a QueryBlock. QueryBlock's have a Statements collection that lists all the top-level statements, including ExpressionStatement's. Expression statements have an Expression property, and all expressions have a ResultType property, which will probably be TableSymbol's.

sunilkr commented 2 years ago

@mattwar

Thanks for the solution. Unfortunately, code.ResultType is null for this example.

mattwar commented 2 years ago

You need to use the ParseAndAnalyze method (instead of just Parse method) to get the semantic information populated.