Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add materialized views support in Kusto Service Layer #2028

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.Kusto.ServiceLayer.DataSource.Intellisense
{
public class ShowMaterializedViewSchemaResult
{
public string TableName;
public string Schema;
public string DatabaseName;
public string Folder;
public string DocString;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.Kusto.ServiceLayer.DataSource.Intellisense
{
public class ShowMaterializedViewsResult
{
public string Name;
public string SourceTable;
public string Query;
public string MaterializedTo;
public string LastRun;
public string LastRunResult;
public bool IsHealthy;
public bool IsEnabled;
public string Folder;
public string DocString;
public bool AutoUpdateSchema;
public string EffectiveDateTime;
public string LookBack;
}
}
298 changes: 240 additions & 58 deletions src/Microsoft.Kusto.ServiceLayer/DataSource/Kusto/KustoDataSource.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -24,16 +25,17 @@ public KustoIntellisenseClient(IKustoClient kustoClient)
_kustoClient = kustoClient;
schemaState = LoadSchemaState(kustoClient.DatabaseName, kustoClient.ClusterName);
}

public override void UpdateDatabase(string databaseName)
{
schemaState = LoadSchemaState(databaseName, _kustoClient.ClusterName);
}

private GlobalState LoadSchemaState(string databaseName, string clusterName)
{
IEnumerable<ShowDatabaseSchemaResult> tableSchemas = Enumerable.Empty<ShowDatabaseSchemaResult>();
IEnumerable<ShowFunctionsResult> functionSchemas = Enumerable.Empty<ShowFunctionsResult>();
var materializedViewSchemas = new ConcurrentBag<ShowMaterializedViewSchemaResult>();

if (!string.IsNullOrWhiteSpace(databaseName))
{
Expand All @@ -46,26 +48,43 @@ private GlobalState LoadSchemaState(string databaseName, string clusterName)
() =>
{
functionSchemas = _kustoClient.ExecuteQueryAsync<ShowFunctionsResult>(".show functions", source.Token, databaseName).Result;
},
() =>
{
var materializedViews = _kustoClient.ExecuteQueryAsync<ShowMaterializedViewsResult>(".show materialized-views", source.Token, databaseName).Result;
Parallel.ForEach(materializedViews, materializedView =>
{
var materializedViewSchema = _kustoClient
.ExecuteQueryAsync<ShowMaterializedViewSchemaResult>(
$".show materialized-view {materializedView.Name} cslschema", source.Token,
databaseName).Result
.FirstOrDefault();

if (materializedViewSchema != null)
{
materializedViewSchemas.Add(materializedViewSchema);
}
});
});
}

return AddOrUpdateDatabase(tableSchemas, functionSchemas, GlobalState.Default, databaseName,
return AddOrUpdateDatabase(tableSchemas, functionSchemas, materializedViewSchemas, GlobalState.Default, databaseName,
clusterName);
}

/// <summary>
/// Loads the schema for the specified database and returns a new <see cref="GlobalState"/> with the database added or updated.
/// </summary>
private GlobalState AddOrUpdateDatabase(IEnumerable<ShowDatabaseSchemaResult> tableSchemas,
IEnumerable<ShowFunctionsResult> functionSchemas, GlobalState globals,
string databaseName, string clusterName)
IEnumerable<ShowFunctionsResult> functionSchemas, IEnumerable<ShowMaterializedViewSchemaResult> materializedViewSchemas,
GlobalState globals, string databaseName, string clusterName)
{
// try and show error from here.
DatabaseSymbol databaseSymbol = null;

if (databaseName != null)
{
databaseSymbol = LoadDatabase(tableSchemas, functionSchemas, databaseName);
databaseSymbol = LoadDatabase(tableSchemas, functionSchemas, materializedViewSchemas, databaseName);
}

if (databaseSymbol == null)
Expand All @@ -76,7 +95,7 @@ private GlobalState AddOrUpdateDatabase(IEnumerable<ShowDatabaseSchemaResult> ta
var cluster = globals.GetCluster(clusterName);
if (cluster == null)
{
cluster = new ClusterSymbol(clusterName, new[] {databaseSymbol}, isOpen: true);
cluster = new ClusterSymbol(clusterName, new[] { databaseSymbol }, isOpen: true);
globals = globals.AddOrUpdateCluster(cluster);
}
else
Expand All @@ -87,12 +106,12 @@ private GlobalState AddOrUpdateDatabase(IEnumerable<ShowDatabaseSchemaResult> ta

return globals.WithCluster(cluster).WithDatabase(databaseSymbol);
}

/// <summary>
/// Loads the schema for the specified database into a <see cref="DatabaseSymbol"/>.
/// </summary>
private DatabaseSymbol LoadDatabase(IEnumerable<ShowDatabaseSchemaResult> tableSchemas,
IEnumerable<ShowFunctionsResult> functionSchemas,
IEnumerable<ShowFunctionsResult> functionSchemas, IEnumerable<ShowMaterializedViewSchemaResult> materializedViewSchemas,
string databaseName)
{
if (tableSchemas == null)
Expand All @@ -112,21 +131,34 @@ private DatabaseSymbol LoadDatabase(IEnumerable<ShowDatabaseSchemaResult> tableS
members.Add(tableSymbol);
}

if (functionSchemas == null)
if (functionSchemas != null)
{
return null;
foreach (var fun in functionSchemas)
{
var parameters = TranslateParameters(fun.Parameters);
var functionSymbol = new FunctionSymbol(fun.Name, fun.Body, parameters);
members.Add(functionSymbol);
}
}

foreach (var fun in functionSchemas)
if (materializedViewSchemas != null)
{
var parameters = TranslateParameters(fun.Parameters);
var functionSymbol = new FunctionSymbol(fun.Name, fun.Body, parameters);
members.Add(functionSymbol);
foreach (var view in materializedViewSchemas)
{
var columns = view.Schema.Split(',')
.Select(col =>
{
var nameType = col.Split(':');
return new ColumnSymbol(nameType[0], ScalarTypes.GetSymbol(nameType[1]));
});
var viewSymbol = new TableSymbol(view.TableName, columns);
members.Add(viewSymbol);
}
}

return new DatabaseSymbol(databaseName, members);
}

/// <summary>
/// Convert CLR type name into a Kusto scalar type.
/// </summary>
Expand Down Expand Up @@ -200,7 +232,7 @@ private ScalarSymbol GetKustoType(string clrTypeName)
throw new InvalidOperationException($"Unhandled clr type: {clrTypeName}");
}
}

/// <summary>
/// Translate Kusto parameter list declaration into into list of <see cref="Parameter"/> instances.
/// </summary>
Expand All @@ -226,7 +258,7 @@ private IReadOnlyList<Parameter> TranslateParameters(string parameters)
var query = "let fn = " + parameters + " { };";
var code = KustoCode.ParseAndAnalyze(query);
var let = code.Syntax.GetFirstDescendant<LetStatement>();

FunctionSymbol function = let.Name.ReferencedSymbol is VariableSymbol variable
? variable.Type as FunctionSymbol
: let.Name.ReferencedSymbol as FunctionSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum DataSourceMetadataType
Table = 2,
Column = 3,
Function = 4,
Folder = 5
Folder = 5,
MaterializedView = 6
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public static List<ObjectMetadata> ConvertToObjectMetadata(IEnumerable<DataSourc
ObjectMetadata dbChildInfo = new ObjectMetadata();
dbChildInfo.Name = childDetail.PrettyName;
dbChildInfo.MetadataTypeName = childDetail.MetadataTypeName;
dbChildInfo.MetadataType = MetadataType.Table; // Add mapping here.
dbChildInfo.MetadataType = childDetail.MetadataType == DataSourceMetadataType.MaterializedView ?
MetadataType.View :
MetadataType.Table;
databaseChildDetails.Add(dbChildInfo);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System.Collections.Generic;

namespace Microsoft.Kusto.ServiceLayer.DataSource.Models
{
public class MaterializedViewSchemaInfo
{
public string Name { get; set; }

public string Folder { get; set; }

public IEnumerable<MaterializedViewColumnInfo> OrderedColumns { get; set; }
}

public class MaterializedViewColumnInfo
{
public string Name { get; set; }

public string Type { get; set; }

public string CslType { get; set; }
}
}
Loading