Replies: 5 comments 1 reply
-
It's not typically how the GlobalState is used, but you can add members to the current database like this. globals = globals.AddOrReplaceCluster(globals.Cluster.AddOrUpdateDatabase(globals.Database.AddMembers(function))); Creating the function symbol to add from the previous command is a bit more difficult. You can create a new function symbol given the name of the function, the parameters and the body. You can specify all three of these as strings. This is how the database functions are stored and retrieved from the database to be loaded as schema into the GlobalState. All you need to do is pick out the correct parts of the create function command and use them to create the FunctionSymbol, and then add that function symbol to the current database symbol, etc. However, since the individual commands don't have their own syntax node types currently, you'll have to search for the individual parts separately using syntax node types that are known. Fortunately, the create function command shares some of the nodes of the query language, for just the parts you will need. For the name you can search for the first Here's some code that should work for you. var globals = GlobalState.Default; // or your starting globals state
var code1 = KustoCode.ParseAndAnalyze(".create-or-alter function A() { print x=10, y='ten' }", globals);
var functionName = code1.Syntax.GetFirstDescendant<NameDeclaration>().SimpleName;
var functionParameters = code1.Syntax.GetFirstDescendant<FunctionParameters>().ToString();
var functionBody = code1.Syntax.GetFirstDescendant<FunctionBody>().ToString();
var function = new FunctionSymbol(functionName, functionParameters, functionBody);
var globals2 = globals.AddOrReplaceCluster(globals.Cluster.AddOrUpdateDatabase(globals.Database.AddMembers(function)));
var code2 = KustoCode.ParseAndAnalyze(".create-or-alter function B() { A() | take 1 }", globals2); |
Beta Was this translation helpful? Give feedback.
-
I've added a new feature and published a new version to help you and others with similar scenarios. Version 1.4.0 now has the var newGlobals = globals.ApplyCommand(".create table T (x: long, y: string)"); |
Beta Was this translation helpful? Give feedback.
-
Thank you. In your previous comment, instead of
can I try
? I also want to keep track of the functions I'm evaluating and from your sample, it looks like the parsed |
Beta Was this translation helpful? Give feedback.
-
The previous example was how you can create a function symbol from the create function command so you can add it to the global state to make it available for the analysis of another command/query. Otherwise, yes, you can simply search for all the function symbol references in a query to discover which functions are used. |
Beta Was this translation helpful? Give feedback.
-
Thanks. Is DocString and Folder also exposed in FunctionSymbol? |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm using https://github.com/mattwar/Kusto.Toolkit/blob/master/src/Toolkit/docs/SymbolResolver.md to analyze syntax of multiple commands. Sometimes a command relies on previous ones, e.g.
then later
How do I inject A into the
GlobalState
so I won't get false positive error thatA doesn't refer to a known function
?Beta Was this translation helpful? Give feedback.
All reactions