├── .github
└── workflows
│ ├── build-main.yml
│ └── build-pr.yml
├── .gitignore
├── BabyKusto.sln
├── CODE_OF_CONDUCT.md
├── Directory.Build.Props
├── LICENSE
├── README.md
├── SECURITY.md
├── SUPPORT.md
├── docs
└── internal-representation.png
├── eng
└── targets
│ └── EmbedProjectRefs.targets
├── global.json
├── opensource
└── modified
│ └── t-digest-csharp
│ ├── LICENSE
│ ├── OPENSOURCE.TXT
│ ├── README.md
│ ├── TDigest.Tests
│ ├── MergingDigestTest.cs
│ ├── ScaleFunctionTests.cs
│ ├── TDigest.Tests.csproj
│ └── TDigestTest.cs
│ └── TDigest
│ ├── AbstractTDigest.cs
│ ├── Centroid.cs
│ ├── Dist.cs
│ ├── MergingDigest.cs
│ ├── ScaleFunction.cs
│ ├── Sorts.cs
│ ├── TDigest.cs
│ └── TDigest.csproj
├── samples
├── Sample.HelloWorld
│ ├── Program.cs
│ └── Sample.HelloWorld.csproj
├── Sample.ProcessesCli
│ ├── ProcessesTable.cs
│ ├── Program.cs
│ ├── README.md
│ └── Sample.ProcessesCli.csproj
└── Sample.ProcessesServer
│ ├── Controllers
│ ├── MgmtController.cs
│ └── QueryController.cs
│ ├── Program.cs
│ ├── Properties
│ └── launchSettings.json
│ ├── Sample.ProcessesServer.csproj
│ ├── appsettings.Development.json
│ └── appsettings.json
├── src
├── BabyKusto.Core
│ ├── BabyKusto.Core.csproj
│ ├── BabyKustoEngine.cs
│ ├── DataSource
│ │ ├── Column.cs
│ │ ├── Extensions
│ │ │ ├── ChunkExtensions.cs
│ │ │ └── TableSourceExtensions.cs
│ │ ├── ITableChunk.cs
│ │ ├── ITableSource.cs
│ │ ├── InMemoryTableSource.cs
│ │ └── TableChunk.cs
│ ├── Evaluation
│ │ ├── BabyKustoEvaluator.cs
│ │ ├── BuiltIns
│ │ │ ├── Aggregates
│ │ │ │ ├── Avg.cs
│ │ │ │ ├── Count.cs
│ │ │ │ ├── CountIf.cs
│ │ │ │ ├── DCount.cs
│ │ │ │ ├── DCountIf.cs
│ │ │ │ ├── Helpers
│ │ │ │ │ └── JsonArrayHelper.cs
│ │ │ │ ├── MakeList.cs
│ │ │ │ ├── MakeListIf.cs
│ │ │ │ ├── MakeListWithNulls.cs
│ │ │ │ ├── MakeSet.cs
│ │ │ │ ├── MakeSetIf.cs
│ │ │ │ ├── Max.cs
│ │ │ │ ├── Min.cs
│ │ │ │ ├── Percentile.cs
│ │ │ │ ├── Sum.cs
│ │ │ │ ├── SumIf.cs
│ │ │ │ └── TakeAny.cs
│ │ │ ├── BuiltInAggregates.cs
│ │ │ ├── BuiltInComparers.cs
│ │ │ ├── BuiltInOperators.cs
│ │ │ ├── BuiltInScalarFunctions.cs
│ │ │ ├── BuiltInWindowFunctions.cs
│ │ │ ├── BuiltInsHelper.cs
│ │ │ ├── Comparers
│ │ │ │ ├── Bool.cs
│ │ │ │ ├── DateTime.cs
│ │ │ │ ├── Double.cs
│ │ │ │ ├── Int.cs
│ │ │ │ ├── Long.cs
│ │ │ │ ├── String.cs
│ │ │ │ └── TimeSpan.cs
│ │ │ ├── Infra
│ │ │ │ ├── AggregateInfo.cs
│ │ │ │ ├── AggregateOverloadInfo.cs
│ │ │ │ ├── IAggregateImpl.cs
│ │ │ │ ├── IScalarFunctionImpl.cs
│ │ │ │ ├── IWindowFunctionImpl.cs
│ │ │ │ ├── OverloadInfoBase.cs
│ │ │ │ ├── ScalarFunctionInfo.cs
│ │ │ │ ├── ScalarOverloadInfo.cs
│ │ │ │ ├── WindowFunctionInfo.cs
│ │ │ │ └── WindowOverloadInfo.cs
│ │ │ ├── Operators
│ │ │ │ ├── Add.cs
│ │ │ │ ├── Contains.cs
│ │ │ │ ├── ContainsCs.cs
│ │ │ │ ├── Divide.cs
│ │ │ │ ├── EndsWith.cs
│ │ │ │ ├── EndsWithCs.cs
│ │ │ │ ├── Equal.cs
│ │ │ │ ├── GreaterThan.cs
│ │ │ │ ├── GreaterThanOrEqual.cs
│ │ │ │ ├── LessThan.cs
│ │ │ │ ├── LessThanOrEqual.cs
│ │ │ │ ├── LogicalAnd.cs
│ │ │ │ ├── LogicalOr.cs
│ │ │ │ ├── MatchRegex.cs
│ │ │ │ ├── Modulo.cs
│ │ │ │ ├── Multiply.cs
│ │ │ │ ├── NotContains.cs
│ │ │ │ ├── NotContainsCs.cs
│ │ │ │ ├── NotEndsWith.cs
│ │ │ │ ├── NotEndsWithCs.cs
│ │ │ │ ├── NotEqual.cs
│ │ │ │ ├── NotStartsWith.cs
│ │ │ │ ├── NotStartsWithCs.cs
│ │ │ │ ├── StartsWith.cs
│ │ │ │ ├── StartsWithCs.cs
│ │ │ │ ├── Subtract.cs
│ │ │ │ └── UnaryMinus.cs
│ │ │ ├── ScalarFunctions
│ │ │ │ ├── Ago.cs
│ │ │ │ ├── ArrayLength.cs
│ │ │ │ ├── ArraySort.cs
│ │ │ │ ├── Bin.cs
│ │ │ │ ├── Casts.cs
│ │ │ │ ├── Coalesce.cs
│ │ │ │ ├── DayOfMonth.cs
│ │ │ │ ├── DayOfWeek.cs
│ │ │ │ ├── DayOfYear.cs
│ │ │ │ ├── EndOfDay.cs
│ │ │ │ ├── EndOfMonth.cs
│ │ │ │ ├── EndOfWeek.cs
│ │ │ │ ├── EndOfYear.cs
│ │ │ │ ├── Exp.cs
│ │ │ │ ├── ExtractRegex.cs
│ │ │ │ ├── GeoDistance2Points.cs
│ │ │ │ ├── Iff.cs
│ │ │ │ ├── IsEmpty.cs
│ │ │ │ ├── IsNull.cs
│ │ │ │ ├── Log.cs
│ │ │ │ ├── Log10.cs
│ │ │ │ ├── Log2.cs
│ │ │ │ ├── MinOf.cs
│ │ │ │ ├── Not.cs
│ │ │ │ ├── Now.cs
│ │ │ │ ├── ParseJson.cs
│ │ │ │ ├── Pow.cs
│ │ │ │ ├── ReplaceString.cs
│ │ │ │ ├── Sqrt.cs
│ │ │ │ ├── StartOfDay.cs
│ │ │ │ ├── StartOfMonth.cs
│ │ │ │ ├── StartOfWeek.cs
│ │ │ │ ├── StartOfYear.cs
│ │ │ │ ├── Strcat.cs
│ │ │ │ ├── Strlen.cs
│ │ │ │ ├── Substring.cs
│ │ │ │ ├── ToString.cs
│ │ │ │ ├── UrlDecode.cs
│ │ │ │ └── UrlEncodeComponent.cs
│ │ │ └── WindowFunctions
│ │ │ │ └── RowCumSum.cs
│ │ ├── Infra
│ │ │ ├── DerivedTableSourceBase.cs
│ │ │ ├── EvaluationContext.cs
│ │ │ └── LocalScope.cs
│ │ ├── TreeEvaluator.Expressions.Cast.cs
│ │ ├── TreeEvaluator.Expressions.FunctionCalls.cs
│ │ ├── TreeEvaluator.Expressions.Materialize.cs
│ │ ├── TreeEvaluator.Expressions.Operators.cs
│ │ ├── TreeEvaluator.Expressions.cs
│ │ ├── TreeEvaluator.QueryOperators.Filter.cs
│ │ ├── TreeEvaluator.QueryOperators.Join.cs
│ │ ├── TreeEvaluator.QueryOperators.Print.cs
│ │ ├── TreeEvaluator.QueryOperators.Project.cs
│ │ ├── TreeEvaluator.QueryOperators.Render.cs
│ │ ├── TreeEvaluator.QueryOperators.Sort.cs
│ │ ├── TreeEvaluator.QueryOperators.Summarize.cs
│ │ ├── TreeEvaluator.QueryOperators.Take.cs
│ │ ├── TreeEvaluator.QueryOperators.ToScalar.cs
│ │ ├── TreeEvaluator.QueryOperators.Union.cs
│ │ └── TreeEvaluator.cs
│ ├── EvaluationResult.cs
│ ├── InternalRepresentation
│ │ ├── DefaultIRNodeVisitor.cs
│ │ ├── IRNode.cs
│ │ ├── IRNodeVisitor.cs
│ │ ├── Nodes
│ │ │ ├── Expressions
│ │ │ │ ├── IRAggregateCallNode.cs
│ │ │ │ ├── IRBinaryExpressionNode.cs
│ │ │ │ ├── IRBuiltInScalarFunctionCallNode.cs
│ │ │ │ ├── IRBuiltInWindowFunctionCallNode.cs
│ │ │ │ ├── IRCastExpressionNode.cs
│ │ │ │ ├── IRDataTableExpression.cs
│ │ │ │ ├── IRExpressionNode.cs
│ │ │ │ ├── IRFunctionDeclarationNode.cs
│ │ │ │ ├── IRJoinOnClause.cs
│ │ │ │ ├── IRLiteralExpressionNode.cs
│ │ │ │ ├── IRMaterializeExpressionNode.cs
│ │ │ │ ├── IRMemberAccessNode.cs
│ │ │ │ ├── IRNameReferenceNode.cs
│ │ │ │ ├── IROrderedExpressionNode.cs
│ │ │ │ ├── IROutputColumnNode.cs
│ │ │ │ ├── IRPipeExpressionNode.cs
│ │ │ │ ├── IRRowScopeNameReferenceNode.cs
│ │ │ │ ├── IRToScalarExpressionNode.cs
│ │ │ │ ├── IRUnaryExpressionNode.cs
│ │ │ │ ├── IRUserFunctionCallNode.cs
│ │ │ │ └── QueryOperators
│ │ │ │ │ ├── IRFilterOperatorNode.cs
│ │ │ │ │ ├── IRJoinOperatorNode.cs
│ │ │ │ │ ├── IRPrintOperatorNode.cs
│ │ │ │ │ ├── IRProjectOperatorNode.cs
│ │ │ │ │ ├── IRQueryOperatorNode.cs
│ │ │ │ │ ├── IRRenderOperatorNode.cs
│ │ │ │ │ ├── IRSortOperatorNode.cs
│ │ │ │ │ ├── IRSummarizeOperatorNode.cs
│ │ │ │ │ ├── IRTakeOperatorNode.cs
│ │ │ │ │ └── IRUnionOperatorNode.cs
│ │ │ ├── IRListNode.cs
│ │ │ ├── Other
│ │ │ │ ├── IRFunctionBodyNode.cs
│ │ │ │ └── IRQueryBlockNode.cs
│ │ │ └── Statements
│ │ │ │ ├── IRExpressionStatementNode.cs
│ │ │ │ ├── IRLetStatementNode.cs
│ │ │ │ └── IRStatementNode.cs
│ │ └── Translation
│ │ │ ├── IRTranslator.Count.cs
│ │ │ ├── IRTranslator.Distinct.cs
│ │ │ ├── IRTranslator.Expressions.cs
│ │ │ ├── IRTranslator.Filter.cs
│ │ │ ├── IRTranslator.FunctionBody.cs
│ │ │ ├── IRTranslator.Join.cs
│ │ │ ├── IRTranslator.Print.cs
│ │ │ ├── IRTranslator.Project.cs
│ │ │ ├── IRTranslator.Render.cs
│ │ │ ├── IRTranslator.Sort.cs
│ │ │ ├── IRTranslator.Statements.cs
│ │ │ ├── IRTranslator.Summarize.cs
│ │ │ ├── IRTranslator.Take.cs
│ │ │ ├── IRTranslator.Union.cs
│ │ │ └── IRTranslator.cs
│ ├── README-NUGET.md
│ ├── Util
│ │ ├── ColumnBuilder.cs
│ │ ├── ColumnHelpers.cs
│ │ ├── EvaluationResultExtensions.cs
│ │ ├── IsExternalInit.cs
│ │ ├── PolyfillExtensions.cs
│ │ └── TypeSymbolExtensions.cs
│ └── opensource
│ │ └── unmodified
│ │ └── dotnet-runtime
│ │ ├── LICENSE.TXT
│ │ ├── OPENSOURCE.TXT
│ │ └── TypeNameHelper.cs
└── BabyKusto.Server
│ ├── BabyKusto.Server.csproj
│ ├── ConfigurationExtensions.cs
│ ├── Contract
│ ├── KustoApiColumnDescription.cs
│ ├── KustoApiMgmtRequestBody.cs
│ ├── KustoApiQueryRequestBody.cs
│ ├── KustoApiResult.cs
│ ├── KustoApiTableResult.cs
│ └── KustoApiV2ColumnDescription.cs
│ ├── README-NUGET.md
│ └── Service
│ ├── BabyKustoServerOptions.cs
│ ├── BabyKustoServerState.cs
│ ├── IBabyKustoServerState.cs
│ ├── ITablesProvider.cs
│ ├── JsonSchemaHelper.cs
│ ├── KustoQueryV2ResponseTableKind.cs
│ ├── KustoQueryV2ResponseTableWriter.cs
│ ├── KustoQueryV2ResponseWriter.cs
│ ├── ManagementEndpointHelper.cs
│ ├── QueryEndpointHelper.cs
│ └── QueryV2EndpointHelper.cs
└── test
├── BabyKusto.Core.Tests
├── BabyKusto.Core.Tests.csproj
├── DynamicTests.cs
├── EndToEndTests.cs
├── KustoDocsTests.cs
├── NegativeTests.cs
├── TranslationTests.cs
└── Util
│ └── TypeSymbolExtensionsTests.cs
└── BabyKusto.Server.Tests
├── BabyKusto.Server.Tests.csproj
├── KustoQueryV2ResponseWriterTests.cs
├── ManagementEndpointHelperTests.cs
└── QueryV2EndpointHelperTests.cs
/.github/workflows/build-main.yml:
--------------------------------------------------------------------------------
1 | name: Build main
2 | on:
3 | push:
4 | branches:
5 | - main
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 | - name: Setup .NET SDK
13 | uses: actions/setup-dotnet@v2
14 | with:
15 | dotnet-version: 6.0.x
16 | - name: Restore
17 | run: dotnet restore
18 | - name: Build
19 | run: dotnet build --no-restore --configuration Release
20 | - name: Test
21 | run: dotnet test --no-restore
22 | - name: Pack
23 | run: dotnet pack --no-restore --configuration Release
24 |
--------------------------------------------------------------------------------
/.github/workflows/build-pr.yml:
--------------------------------------------------------------------------------
1 | name: PR Build
2 | on:
3 | pull_request:
4 | branches:
5 | - main
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 | - name: Setup .NET SDK
13 | uses: actions/setup-dotnet@v2
14 | with:
15 | dotnet-version: 6.0.x
16 | - name: Restore
17 | run: dotnet restore
18 | - name: Build
19 | run: dotnet build --no-restore
20 | - name: Test
21 | run: dotnet test --no-restore --no-build
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | #Ignore thumbnails created by Windows
3 | Thumbs.db
4 | #Ignore files built by Visual Studio
5 | *.obj
6 | *.exe
7 | *.pdb
8 | *.user
9 | *.aps
10 | *.pch
11 | *.vspscc
12 | *_i.c
13 | *_p.c
14 | *.ncb
15 | *.suo
16 | *.tlb
17 | *.tlh
18 | *.bak
19 | *.cache
20 | *.ilk
21 | *.log
22 | [Bb]in
23 | [Dd]ebug*/
24 | *.lib
25 | *.sbr
26 | obj/
27 | [Rr]elease*/
28 | _ReSharper*/
29 | [Tt]est[Rr]esult*
30 | .vs/
31 | #Nuget packages folder
32 | packages/
33 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Microsoft Open Source Code of Conduct
2 |
3 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
4 |
5 | Resources:
6 |
7 | - [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
8 | - [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
9 | - Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns
10 |
--------------------------------------------------------------------------------
/Directory.Build.Props:
--------------------------------------------------------------------------------
1 |
2 |
3 | true
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Microsoft Corporation.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE
22 |
--------------------------------------------------------------------------------
/SUPPORT.md:
--------------------------------------------------------------------------------
1 | # Support
2 |
3 | ## How to file issues and get help
4 |
5 | This project uses GitHub Issues to track bugs and feature requests. Please search the existing
6 | issues before filing new issues to avoid duplicates. For new issues, file your bug or
7 | feature request as a new Issue.
8 |
9 | ## Microsoft Support Policy
10 |
11 | Support for this **PROJECT or PRODUCT** is limited to the resources listed above.
12 |
--------------------------------------------------------------------------------
/docs/internal-representation.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/davidnx/baby-kusto-csharp/2b9cc3b98e9cedc02de8af34f5b5ffb2eeaed976/docs/internal-representation.png
--------------------------------------------------------------------------------
/eng/targets/EmbedProjectRefs.targets:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage
7 |
8 |
9 |
10 |
11 |
12 | <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))"/>
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "6.0.408",
4 | "rollForward": "latestFeature"
5 | }
6 | }
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Heath Milligan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/OPENSOURCE.TXT:
--------------------------------------------------------------------------------
1 | Taken from:
2 | Repo: https://github.com/Cyral/t-digest-csharp
3 | Commit ID: 5442c5b71461f4ee737045b924664c6aa3eef6c7
4 |
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/TDigest.Tests/MergingDigestTest.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using Xunit.Abstractions;
3 |
4 | namespace TDigest.Tests {
5 | public class MergingDigestTest : DigestTest {
6 | protected override IDigestFactory Factory(double compression) {
7 | return new MergeDigestFactory(compression);
8 | }
9 |
10 | protected override AbstractTDigest fromBytes(BinaryReader reader) {
11 | return MergingDigest.FromBytes(reader);
12 | }
13 |
14 | class MergeDigestFactory : IDigestFactory {
15 | private double compression;
16 | public MergeDigestFactory(double compression) {
17 | this.compression = compression;
18 | }
19 |
20 | public Digest Create() {
21 | return new MergingDigest(compression);
22 | }
23 | }
24 |
25 | public MergingDigestTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { }
26 | }
27 | }
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/TDigest.Tests/ScaleFunctionTests.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xunit;
3 | using Xunit.Abstractions;
4 |
5 | namespace TDigest.Tests {
6 | public class ScaleFunctionTests {
7 | private readonly ITestOutputHelper testOutputHelper;
8 | public ScaleFunctionTests(ITestOutputHelper testOutputHelper) {
9 | this.testOutputHelper = testOutputHelper;
10 | }
11 |
12 | [Fact]
13 | public void AsinApproximation() {
14 | for (double x = 0; x < 1; x += 1e-4) {
15 | // TODO: I had to lower the threshold for the C# implementation, for some reason
16 | // it is giving slightly different results compared to the Java implementation.
17 | Assert.Equal(Math.Asin(x), ScaleFunction.FastAsin(x), 2);
18 | }
19 | Assert.Equal(Math.Asin(1), ScaleFunction.FastAsin(1), 0);
20 | Assert.True(double.IsNaN(ScaleFunction.FastAsin(1.0001)));
21 | }
22 |
23 | [Fact]
24 | public void TestApproximation() {
25 | double worst = 0;
26 | var old = double.NegativeInfinity;
27 | for (double x = -1; x < 1; x += 0.00001) {
28 | var ex = Math.Asin(x);
29 | var actual = ScaleFunction.FastAsin(x);
30 | var error = ex - actual;
31 | // System.out.printf("%.8f, %.8f, %.8f, %.12f\n", x, ex, actual, error * 1e6);
32 | Assert.Equal(0, error, 5);
33 | Assert.True(actual >= old);
34 | worst = Math.Max(worst, Math.Abs(error));
35 | old = actual;
36 | }
37 | Assert.Equal(Math.Asin(1), ScaleFunction.FastAsin(1), 0);
38 | testOutputHelper.WriteLine($"worst = {worst}");
39 | }
40 | }
41 | }
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/TDigest.Tests/TDigest.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | net6.0
4 | false
5 |
6 |
7 |
8 |
9 |
10 |
11 | runtime; build; native; contentfiles; analyzers; buildtransitive
12 | all
13 |
14 |
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 | all
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/TDigest/Dist.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Collections.ObjectModel;
4 |
5 | namespace TDigest {
6 | /**
7 | * Reference implementations for cdf and quantile if we have all data.
8 | */
9 | public class Dist {
10 | public static double Cdf(double x, double[] data) {
11 | var n1 = 0;
12 | var n2 = 0;
13 | foreach (var v in data) {
14 | n1 += (v < x) ? 1 : 0;
15 | n2 += (v == x) ? 1 : 0;
16 | }
17 | return (n1 + n2 / 2.0) / data.Length;
18 | }
19 |
20 | public static double Cdf(double x, Collection data) {
21 | var n1 = 0;
22 | var n2 = 0;
23 | foreach (var v in data) {
24 | n1 += (v < x) ? 1 : 0;
25 | n2 += (v == x) ? 1 : 0;
26 | }
27 | return (n1 + n2 / 2.0) / data.Count;
28 | }
29 |
30 | public static double Quantile(double q, double[] data) {
31 | var n = data.Length;
32 | if (n == 0) {
33 | return double.NaN;
34 | }
35 | var index = q * n;
36 | if (index < 0) {
37 | index = 0;
38 | }
39 | if (index > n - 1) {
40 | index = n - 1;
41 | }
42 | return data[(int) Math.Floor(index)];
43 | }
44 |
45 | public static double Quantile(double q, List data) {
46 | var n = data.Count;
47 | if (n == 0) {
48 | return Double.NaN;
49 | }
50 | var index = q * n;
51 | if (index < 0) {
52 | index = 0;
53 | }
54 | if (index > n - 1) {
55 | index = n - 1;
56 | }
57 | return data[(int) Math.Floor(index)];
58 | }
59 | }
60 | }
--------------------------------------------------------------------------------
/opensource/modified/t-digest-csharp/TDigest/TDigest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 | netstandard2.0
4 | false
5 |
6 |
7 |
--------------------------------------------------------------------------------
/samples/Sample.HelloWorld/Program.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using BabyKusto.Core;
6 | using BabyKusto.Core.Extensions;
7 |
8 | string query = @"
9 | let me = 'baby';
10 | datatable(v:int) [ 0, 5, 2, 1, 4, 3 ]
11 | | project countDown = v + 1
12 | | order by countDown desc
13 | | extend greeting=substring(strcat('Hello ', me, '-kusto!'), countDown - 1, 100)
14 | ";
15 |
16 | var engine = new BabyKustoEngine();
17 | var result = engine.Evaluate(query);
18 | result.Dump(Console.Out);
19 |
--------------------------------------------------------------------------------
/samples/Sample.HelloWorld/Sample.HelloWorld.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesCli/ProcessesTable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Threading;
6 | using BabyKusto.Core;
7 | using BabyKusto.Core.Util;
8 | using Kusto.Language.Symbols;
9 |
10 | namespace BabyKusto.ProcessQuerier
11 | {
12 | public class ProcessesTable : ITableSource
13 | {
14 | public ProcessesTable(string tableName)
15 | {
16 | Type = new TableSymbol(
17 | tableName,
18 | new ColumnSymbol("pid", ScalarTypes.Int),
19 | new ColumnSymbol("name", ScalarTypes.String),
20 | new ColumnSymbol("numThreads", ScalarTypes.Int),
21 | new ColumnSymbol("workingSet", ScalarTypes.Long)
22 | );
23 | }
24 |
25 | public TableSymbol Type { get; }
26 |
27 | public IEnumerable GetData()
28 | {
29 | var pids = new ColumnBuilder(ScalarTypes.Int);
30 | var names = new ColumnBuilder(ScalarTypes.String);
31 | var numThreads = new ColumnBuilder(ScalarTypes.Int);
32 | var workingSets = new ColumnBuilder(ScalarTypes.Long);
33 |
34 | foreach (var p in Process.GetProcesses())
35 | {
36 | pids.Add(p.Id);
37 | names.Add(p.ProcessName);
38 | numThreads.Add(p.Threads.Count);
39 | workingSets.Add(p.WorkingSet64);
40 | }
41 |
42 | var builders = new ColumnBuilder[] { pids, names, numThreads, workingSets };
43 | yield return new TableChunk(this, builders.Select(b => b.ToColumn()).ToArray());
44 | }
45 |
46 | public IAsyncEnumerable GetDataAsync(CancellationToken cancellation = default)
47 | {
48 | throw new NotSupportedException();
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/samples/Sample.ProcessesCli/README.md:
--------------------------------------------------------------------------------
1 | ## BabyKusto.ProcessQuerier
2 |
3 | A trivial example of using BabyKusto with real data,
4 | serving as a tool to explore live processes running on the current machine
5 | with KQL queries.
6 |
7 | What follows is an example of the output produced by running this sample.
8 | It serves as a REPL and new queries can be evaluated at will.
9 |
10 |
11 | ```
12 | /----------------------------------------------------------------\
13 | | Welcome to BabyKusto.ProcessQuerier. You can write KQL queries |
14 | | and explore the live list of processes on your machine. |
15 | \----------------------------------------------------------------/
16 |
17 | Example: counting the total number of processes:
18 | > Processes | count
19 |
20 | Count:long
21 | ------------------
22 | 257
23 |
24 | Example: Find the process using the most memory:
25 | > Processes | project name, memMB=workingSet/1024/1024 | order by memMB desc | take 1
26 |
27 | name:string; memMB:long
28 | ------------------
29 | MsMpEng; 1500
30 |
31 | >
32 | ```
33 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesCli/Sample.ProcessesCli.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/Controllers/MgmtController.cs:
--------------------------------------------------------------------------------
1 | using BabyKusto.Server.Contract;
2 | using BabyKusto.Server.Service;
3 | using Microsoft.AspNetCore.Mvc;
4 |
5 | namespace BabyKusto.SampleServer.Controllers
6 | {
7 | [ApiController]
8 | public class MgmtController : ControllerBase
9 | {
10 | private readonly ManagementEndpointHelper _managementEndpointHelper;
11 | private readonly ILogger _logger;
12 |
13 | public MgmtController(ManagementEndpointHelper managementEndpointHelper, ILogger logger)
14 | {
15 | _managementEndpointHelper = managementEndpointHelper ?? throw new ArgumentNullException(nameof(managementEndpointHelper));
16 | _logger = logger ?? throw new ArgumentNullException(nameof(logger));
17 | }
18 |
19 | [HttpPost]
20 | [Route("/v1/rest/mgmt")]
21 | public IActionResult Execute(KustoApiMgmtRequestBody body)
22 | {
23 | if (body == null)
24 | {
25 | return this.BadRequest();
26 | }
27 |
28 | try
29 | {
30 | var result = _managementEndpointHelper.Process(body);
31 | return this.Ok(result);
32 | }
33 | catch (InvalidOperationException ex)
34 | {
35 | _logger.LogError(ex, "Error processing mgmt api request.");
36 | return this.BadRequest(ex.ToString());
37 | }
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/Program.cs:
--------------------------------------------------------------------------------
1 | using BabyKusto.Core;
2 | using BabyKusto.ProcessQuerier;
3 | using BabyKusto.Server;
4 | using BabyKusto.Server.Service;
5 |
6 | namespace BabyKusto.SampleServer
7 | {
8 | public class Program
9 | {
10 | public static void Main(string[] args)
11 | {
12 | var builder = WebApplication.CreateBuilder(args);
13 |
14 | // Add services to the container.
15 |
16 | builder.Services.AddControllers();
17 |
18 | builder.Services.AddSingleton();
19 | builder.Services.AddBabyKustoServer();
20 |
21 | var app = builder.Build();
22 |
23 | // Configure the HTTP request pipeline.
24 |
25 | app.UseHttpsRedirection();
26 | app.UseRouting();
27 |
28 | app.MapControllers();
29 |
30 | app.Run();
31 | }
32 |
33 | private class SimpleTableProvider : ITablesProvider
34 | {
35 | private readonly List _tables;
36 |
37 | public SimpleTableProvider()
38 | {
39 | _tables = new List
40 | {
41 | new ProcessesTable("Processes"),
42 | };
43 | }
44 |
45 | public List GetTables()
46 | {
47 | return _tables;
48 | }
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/launchsettings.json",
3 | "profiles": {
4 | "BabyKusto.SampleServer": {
5 | "commandName": "Project",
6 | "dotnetRunMessages": true,
7 | "launchBrowser": false,
8 | "applicationUrl": "https://localhost:7275;http://localhost:5275",
9 | "environmentVariables": {
10 | "ASPNETCORE_ENVIRONMENT": "Development"
11 | }
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/Sample.ProcessesServer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 | enable
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Information"
6 | }
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/samples/Sample.ProcessesServer/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft.AspNetCore": "Warning"
6 | }
7 | },
8 | "AllowedHosts": "*"
9 | }
10 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/BabyKusto.Core.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;netstandard2.1
5 |
6 |
7 | annotations
8 | enable
9 | 10
10 |
11 |
12 |
13 | 0.1.8-preview
14 | kusto;kql
15 |
16 | A self-contained execution engine for the Kusto Query Language (KQL) written in C#.
17 |
18 | https://github.com/davidnx/baby-kusto-csharp
19 | MIT
20 | README-NUGET.md
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/DataSource/Extensions/ChunkExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.Extensions
5 | {
6 | internal static class ChunkExtensions
7 | {
8 | public static TableChunk ReParent(this ITableChunk chunk, ITableSource newOwner)
9 | {
10 | return new TableChunk(newOwner, chunk.Columns);
11 | }
12 | }
13 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Core/DataSource/ITableChunk.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core
5 | {
6 | public interface ITableChunk
7 | {
8 | ITableSource Table { get; }
9 |
10 | Column[] Columns { get; }
11 |
12 | int RowCount { get; }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/DataSource/ITableSource.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using System.Threading;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core
9 | {
10 | public interface ITableSource
11 | {
12 | TableSymbol Type { get; }
13 |
14 | IEnumerable GetData();
15 | IAsyncEnumerable GetDataAsync(CancellationToken cancellation = default);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/DataSource/InMemoryTableSource.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using System.Threading;
7 | using Kusto.Language.Symbols;
8 |
9 | namespace BabyKusto.Core
10 | {
11 | public class InMemoryTableSource : ITableSource
12 | {
13 | private readonly ITableChunk[] _data;
14 |
15 | public InMemoryTableSource(TableSymbol type, Column[] columns)
16 | {
17 | Type = type;
18 | _data = new ITableChunk[] { new TableChunk(this, columns) };
19 | }
20 |
21 | public TableSymbol Type { get; }
22 |
23 | public IEnumerable GetData() => _data;
24 | public IAsyncEnumerable GetDataAsync(CancellationToken cancellation = default) => _data.ToAsyncEnumerable();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BabyKustoEvaluator.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using BabyKusto.Core.InternalRepresentation;
5 |
6 | namespace BabyKusto.Core.Evaluation
7 | {
8 | internal static class BabyKustoEvaluator
9 | {
10 | internal static EvaluationResult? Evaluate(IRNode root, LocalScope scope)
11 | {
12 | var evaluator = new TreeEvaluator();
13 | return root.Accept(evaluator, new EvaluationContext(scope));
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Aggregates/Count.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class CountFunctionImpl : IAggregateImpl
10 | {
11 | public ScalarResult Invoke(ITableChunk chunk, ColumnarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 0);
14 | return new ScalarResult(ScalarTypes.Long, (long)chunk.RowCount);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Aggregates/CountIf.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class CountIfFunctionImpl : IAggregateImpl
10 | {
11 | public ScalarResult Invoke(ITableChunk chunk, ColumnarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 1);
14 | var column = (Column)arguments[0].Column;
15 | long count = 0;
16 | for (int i = 0; i < column.RowCount; i++)
17 | {
18 | if (column[i] == true)
19 | {
20 | count++;
21 | }
22 | }
23 |
24 | return new ScalarResult(ScalarTypes.Long, count);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Aggregates/Helpers/JsonArrayHelper.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using System.Text.Json.Nodes;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal static class JsonArrayHelper
10 | {
11 | internal static JsonArray From(ICollection source)
12 | {
13 | var array = new JsonNode?[source.Count];
14 | int i = 0;
15 | foreach (var item in source)
16 | {
17 | array[i++] = item == null ? null : JsonValue.Create(item);
18 | }
19 |
20 | return new JsonArray(array);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Aggregates/TakeAny.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
7 | {
8 | internal class TakeAnyFunctionImpl : IAggregateImpl
9 | {
10 | public ScalarResult Invoke(ITableChunk chunk, ColumnarResult[] arguments)
11 | {
12 | Debug.Assert(arguments.Length == 1);
13 | var column = arguments[0].Column;
14 | return new ScalarResult(column.Type, column.RowCount > 0 ? column.RawData.GetValue(0) : null);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/Bool.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
7 | {
8 | internal class BoolAscNullsFirstComparer : IComparer
9 | {
10 | public int Compare(object? a, object? b)
11 | {
12 | return
13 | (a == null && b == null)
14 | ? 0
15 | : a == null
16 | ? -1
17 | : b == null
18 | ? 1
19 | : ((bool)a).CompareTo((bool)b);
20 | }
21 | }
22 |
23 | internal class BoolAscNullsLastComparer : IComparer
24 | {
25 | public int Compare(object? a, object? b)
26 | {
27 | return
28 | (a == null && b == null)
29 | ? 0
30 | : a == null
31 | ? 1
32 | : b == null
33 | ? -1
34 | : ((bool)a).CompareTo((bool)b);
35 | }
36 | }
37 |
38 | internal class BoolDescNullsFirstComparer : IComparer
39 | {
40 | public int Compare(object? a, object? b)
41 | {
42 | return
43 | (a == null && b == null)
44 | ? 0
45 | : a == null
46 | ? -1
47 | : b == null
48 | ? 1
49 | : ((bool)b).CompareTo((bool)a);
50 | }
51 | }
52 |
53 | internal class BoolDescNullsLastComparer : IComparer
54 | {
55 | public int Compare(object? a, object? b)
56 | {
57 | return
58 | (a == null && b == null)
59 | ? 0
60 | : a == null
61 | ? 1
62 | : b == null
63 | ? -1
64 | : ((bool)b).CompareTo((bool)a);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/DateTime.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class DateTimeAscNullsFirstComparer : IComparer
10 | {
11 | public int Compare(object? a, object? b)
12 | {
13 | return
14 | (a == null && b == null)
15 | ? 0
16 | : a == null
17 | ? -1
18 | : b == null
19 | ? 1
20 | : ((DateTime)a).CompareTo((DateTime)b);
21 | }
22 | }
23 |
24 | internal class DateTimeAscNullsLastComparer : IComparer
25 | {
26 | public int Compare(object? a, object? b)
27 | {
28 | return
29 | (a == null && b == null)
30 | ? 0
31 | : a == null
32 | ? 1
33 | : b == null
34 | ? -1
35 | : ((DateTime)a).CompareTo((DateTime)b);
36 | }
37 | }
38 |
39 | internal class DateTimeDescNullsFirstComparer : IComparer
40 | {
41 | public int Compare(object? a, object? b)
42 | {
43 | return
44 | (a == null && b == null)
45 | ? 0
46 | : a == null
47 | ? -1
48 | : b == null
49 | ? 1
50 | : ((DateTime)b).CompareTo((DateTime)a);
51 | }
52 | }
53 |
54 | internal class DateTimeDescNullsLastComparer : IComparer
55 | {
56 | public int Compare(object? a, object? b)
57 | {
58 | return
59 | (a == null && b == null)
60 | ? 0
61 | : a == null
62 | ? 1
63 | : b == null
64 | ? -1
65 | : ((DateTime)b).CompareTo((DateTime)a);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/Double.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
7 | {
8 | internal class DoubleAscNullsFirstComparer : IComparer
9 | {
10 | public int Compare(object? a, object? b)
11 | {
12 | return
13 | (a == null && b == null)
14 | ? 0
15 | : a == null
16 | ? -1
17 | : b == null
18 | ? 1
19 | : ((double)a).CompareTo((double)b);
20 | }
21 | }
22 |
23 | internal class DoubleAscNullsLastComparer : IComparer
24 | {
25 | public int Compare(object? a, object? b)
26 | {
27 | return
28 | (a == null && b == null)
29 | ? 0
30 | : a == null
31 | ? 1
32 | : b == null
33 | ? -1
34 | : ((double)a).CompareTo((double)b);
35 | }
36 | }
37 |
38 | internal class DoubleDescNullsFirstComparer : IComparer
39 | {
40 | public int Compare(object? a, object? b)
41 | {
42 | return
43 | (a == null && b == null)
44 | ? 0
45 | : a == null
46 | ? -1
47 | : b == null
48 | ? 1
49 | : ((double)b).CompareTo((double)a);
50 | }
51 | }
52 |
53 | internal class DoubleDescNullsLastComparer : IComparer
54 | {
55 | public int Compare(object? a, object? b)
56 | {
57 | return
58 | (a == null && b == null)
59 | ? 0
60 | : a == null
61 | ? 1
62 | : b == null
63 | ? -1
64 | : ((double)b).CompareTo((double)a);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/Int.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
7 | {
8 | internal class IntAscNullsFirstComparer : IComparer
9 | {
10 | public int Compare(object? a, object? b)
11 | {
12 | return
13 | (a == null && b == null)
14 | ? 0
15 | : a == null
16 | ? -1
17 | : b == null
18 | ? 1
19 | : ((int)a).CompareTo((int)b);
20 | }
21 | }
22 |
23 | internal class IntAscNullsLastComparer : IComparer
24 | {
25 | public int Compare(object? a, object? b)
26 | {
27 | return
28 | (a == null && b == null)
29 | ? 0
30 | : a == null
31 | ? 1
32 | : b == null
33 | ? -1
34 | : ((int)a).CompareTo((int)b);
35 | }
36 | }
37 |
38 | internal class IntDescNullsFirstComparer : IComparer
39 | {
40 | public int Compare(object? a, object? b)
41 | {
42 | return
43 | (a == null && b == null)
44 | ? 0
45 | : a == null
46 | ? -1
47 | : b == null
48 | ? 1
49 | : ((int)b).CompareTo((int)a);
50 | }
51 | }
52 |
53 | internal class IntDescNullsLastComparer : IComparer
54 | {
55 | public int Compare(object? a, object? b)
56 | {
57 | return
58 | (a == null && b == null)
59 | ? 0
60 | : a == null
61 | ? 1
62 | : b == null
63 | ? -1
64 | : ((int)b).CompareTo((int)a);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/Long.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
7 | {
8 | internal class LongAscNullsFirstComparer : IComparer
9 | {
10 | public int Compare(object? a, object? b)
11 | {
12 | return
13 | (a == null && b == null)
14 | ? 0
15 | : a == null
16 | ? -1
17 | : b == null
18 | ? 1
19 | : ((long)a).CompareTo((long)b);
20 | }
21 | }
22 |
23 | internal class LongAscNullsLastComparer : IComparer
24 | {
25 | public int Compare(object? a, object? b)
26 | {
27 | return
28 | (a == null && b == null)
29 | ? 0
30 | : a == null
31 | ? 1
32 | : b == null
33 | ? -1
34 | : ((long)a).CompareTo((long)b);
35 | }
36 | }
37 |
38 | internal class LongDescNullsFirstComparer : IComparer
39 | {
40 | public int Compare(object? a, object? b)
41 | {
42 | return
43 | (a == null && b == null)
44 | ? 0
45 | : a == null
46 | ? -1
47 | : b == null
48 | ? 1
49 | : ((long)b).CompareTo((long)a);
50 | }
51 | }
52 |
53 | internal class LongDescNullsLastComparer : IComparer
54 | {
55 | public int Compare(object? a, object? b)
56 | {
57 | return
58 | (a == null && b == null)
59 | ? 0
60 | : a == null
61 | ? 1
62 | : b == null
63 | ? -1
64 | : ((long)b).CompareTo((long)a);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/String.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class StringAscNullsFirstComparer : IComparer
10 | {
11 | public int Compare(object? a, object? b)
12 | {
13 | return
14 | (a == null && b == null)
15 | ? 0
16 | : a == null
17 | ? -1
18 | : b == null
19 | ? 1
20 | : StringComparer.Ordinal.Compare((string)a, (string)b);
21 | }
22 | }
23 |
24 | internal class StringAscNullsLastComparer : IComparer
25 | {
26 | public int Compare(object? a, object? b)
27 | {
28 | return
29 | (a == null && b == null)
30 | ? 0
31 | : a == null
32 | ? 1
33 | : b == null
34 | ? -1
35 | : StringComparer.Ordinal.Compare((string)a, (string)b);
36 | }
37 | }
38 |
39 | internal class StringDescNullsFirstComparer : IComparer
40 | {
41 | public int Compare(object? a, object? b)
42 | {
43 | return
44 | (a == null && b == null)
45 | ? 0
46 | : a == null
47 | ? -1
48 | : b == null
49 | ? 1
50 | : StringComparer.Ordinal.Compare((string)b, (string)a);
51 | }
52 | }
53 |
54 | internal class StringDescNullsLastComparer : IComparer
55 | {
56 | public int Compare(object? a, object? b)
57 | {
58 | return
59 | (a == null && b == null)
60 | ? 0
61 | : a == null
62 | ? 1
63 | : b == null
64 | ? -1
65 | : StringComparer.Ordinal.Compare((string)b, (string)a);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Comparers/TimeSpan.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class TimeSpanAscNullsFirstComparer : IComparer
10 | {
11 | public int Compare(object? a, object? b)
12 | {
13 | return
14 | (a == null && b == null)
15 | ? 0
16 | : a == null
17 | ? -1
18 | : b == null
19 | ? 1
20 | : ((TimeSpan)a).CompareTo((TimeSpan)b);
21 | }
22 | }
23 |
24 | internal class TimeSpanAscNullsLastComparer : IComparer
25 | {
26 | public int Compare(object? a, object? b)
27 | {
28 | return
29 | (a == null && b == null)
30 | ? 0
31 | : a == null
32 | ? 1
33 | : b == null
34 | ? -1
35 | : ((TimeSpan)a).CompareTo((TimeSpan)b);
36 | }
37 | }
38 |
39 | internal class TimeSpanDescNullsFirstComparer : IComparer
40 | {
41 | public int Compare(object? a, object? b)
42 | {
43 | return
44 | (a == null && b == null)
45 | ? 0
46 | : a == null
47 | ? -1
48 | : b == null
49 | ? 1
50 | : ((TimeSpan)b).CompareTo((TimeSpan)a);
51 | }
52 | }
53 |
54 | internal class TimeSpanDescNullsLastComparer : IComparer
55 | {
56 | public int Compare(object? a, object? b)
57 | {
58 | return
59 | (a == null && b == null)
60 | ? 0
61 | : a == null
62 | ? 1
63 | : b == null
64 | ? -1
65 | : ((TimeSpan)b).CompareTo((TimeSpan)a);
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/AggregateInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal class AggregateInfo
9 | {
10 | public AggregateInfo(params AggregateOverloadInfo[] overloads)
11 | {
12 | Overloads = overloads;
13 | }
14 |
15 | public IReadOnlyList Overloads { get; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/AggregateOverloadInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal sealed class AggregateOverloadInfo : OverloadInfoBase
9 | {
10 | public AggregateOverloadInfo(IAggregateImpl aggregateImpl, TypeSymbol returnType, params TypeSymbol[] parameterTypes)
11 | : base(returnType, parameterTypes)
12 | {
13 | AggregateImpl = aggregateImpl;
14 | }
15 |
16 | public IAggregateImpl AggregateImpl { get; }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/IAggregateImpl.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.Evaluation.BuiltIns
5 | {
6 | internal interface IAggregateImpl
7 | {
8 | ScalarResult Invoke(ITableChunk chunk, ColumnarResult[] arguments);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/IScalarFunctionImpl.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.Evaluation.BuiltIns
5 | {
6 | ///
7 | /// A scalar function in Kusto is defined as follows ():
8 | /// - Has zero input arguments, or all its input arguments are scalar values
9 | /// - Produces a single scalar value
10 | /// - Can be used wherever a scalar expression is allowed
11 | /// - May only use the row context in which it is defined
12 | /// - Can only refer to tables (and views) that are in the accessible schema
13 | ///
14 | internal interface IScalarFunctionImpl
15 | {
16 | ScalarResult InvokeScalar(ScalarResult[] arguments);
17 |
18 | ///
19 | /// Executes the scalar function on columnar arguments.
20 | /// The results are the element-wise evaluation of the function for each row.
21 | ///
22 | ColumnarResult InvokeColumnar(ColumnarResult[] arguments);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/IWindowFunctionImpl.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.Evaluation.BuiltIns
5 | {
6 | internal interface IWindowFunctionImpl
7 | {
8 | ColumnarResult InvokeWindow(ColumnarResult[] thisWindowArguments, ColumnarResult[]? previousWindowArguments, ColumnarResult? previousWindowResult);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/OverloadInfoBase.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns
8 | {
9 | internal abstract class OverloadInfoBase
10 | {
11 | protected OverloadInfoBase(TypeSymbol returnType, params TypeSymbol[] parameterTypes)
12 | {
13 | ReturnType = returnType;
14 | ParameterTypes = parameterTypes;
15 | }
16 |
17 | public TypeSymbol ReturnType { get; }
18 | public IReadOnlyList ParameterTypes { get; }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/ScalarFunctionInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal class ScalarFunctionInfo
9 | {
10 | public ScalarFunctionInfo(params ScalarOverloadInfo[] overloads)
11 | {
12 | Overloads = overloads;
13 | }
14 |
15 | public IReadOnlyList Overloads { get; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/ScalarOverloadInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal sealed class ScalarOverloadInfo : OverloadInfoBase
9 | {
10 | public ScalarOverloadInfo(IScalarFunctionImpl impl, TypeSymbol returnType, params TypeSymbol[] parameterTypes)
11 | : base(returnType, parameterTypes)
12 | {
13 | ScalarImpl = impl;
14 | }
15 |
16 | public IScalarFunctionImpl ScalarImpl { get; }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/WindowFunctionInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal class WindowFunctionInfo
9 | {
10 | public WindowFunctionInfo(params WindowOverloadInfo[] overloads)
11 | {
12 | Overloads = overloads;
13 | }
14 |
15 | public IReadOnlyList Overloads { get; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Infra/WindowOverloadInfo.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.Evaluation.BuiltIns
7 | {
8 | internal sealed class WindowOverloadInfo : OverloadInfoBase
9 | {
10 | public WindowOverloadInfo(IWindowFunctionImpl impl, TypeSymbol returnType, params TypeSymbol[] parameterTypes)
11 | : base(returnType, parameterTypes)
12 | {
13 | Impl = impl;
14 | }
15 |
16 | public IWindowFunctionImpl Impl { get; }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/Contains.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class ContainsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).ToUpperInvariant().Contains((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).ToUpperInvariant().Contains((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/ContainsCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class ContainsCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).Contains(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).Contains(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/EndsWith.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class EndsWithOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).ToUpperInvariant().EndsWith((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).ToUpperInvariant().EndsWith((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/EndsWithCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class EndsWithCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).EndsWith(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).EndsWith(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotContains.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotContainsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).ToUpperInvariant().Contains((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).ToUpperInvariant().Contains((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotContainsCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotContainsCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).Contains(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).Contains(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotEndsWith.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotEndsWithOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).ToUpperInvariant().EndsWith((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).ToUpperInvariant().EndsWith((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotEndsWithCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotEndsWithCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).EndsWith(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).EndsWith(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotStartsWith.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotStartsWithOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).ToUpperInvariant().StartsWith((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).ToUpperInvariant().StartsWith((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/NotStartsWithCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotStartsWithCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, !(left ?? string.Empty).StartsWith(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = !(left[i] ?? string.Empty).StartsWith(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/StartsWith.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class StartsWithOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).ToUpperInvariant().StartsWith((right ?? string.Empty).ToUpperInvariant()));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).ToUpperInvariant().StartsWith((right[i] ?? string.Empty).ToUpperInvariant());
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/Operators/StartsWithCs.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class StartsWithCsOperatorImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 2);
14 | var left = (string?)arguments[0].Value;
15 | var right = (string?)arguments[1].Value;
16 | return new ScalarResult(ScalarTypes.Bool, (left ?? string.Empty).StartsWith(right ?? string.Empty));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 2);
22 | Debug.Assert(arguments[0].Column.RowCount == arguments[1].Column.RowCount);
23 | var left = (Column)(arguments[0].Column);
24 | var right = (Column)(arguments[1].Column);
25 |
26 | var data = new bool?[left.RowCount];
27 | for (int i = 0; i < left.RowCount; i++)
28 | {
29 | data[i] = (left[i] ?? string.Empty).StartsWith(right[i] ?? string.Empty);
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Ago.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class AgoFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var ago = (TimeSpan?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, ago.HasValue ? DateTime.UtcNow - ago : null);
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var column = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[column.RowCount];
25 | var now = DateTime.UtcNow;
26 | for (int i = 0; i < column.RowCount; i++)
27 | {
28 | var item = column[i];
29 | if (item.HasValue)
30 | {
31 | data[i] = now - item.Value;
32 | }
33 | }
34 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/ArrayLength.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using System.Text.Json.Nodes;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class ArrayLengthFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var value = (JsonNode?)arguments[0].Value;
16 | var array = value as JsonArray;
17 | return new ScalarResult(ScalarTypes.Long, array == null ? (long?)null : array.Count);
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var column = (Column)arguments[0].Column;
24 |
25 | var data = new long?[column.RowCount];
26 | for (int i = 0; i < column.RowCount; i++)
27 | {
28 | var array = column[i] as JsonArray;
29 | data[i] = array == null ? null : array.Count;
30 | }
31 | return new ColumnarResult(Column.Create(ScalarTypes.Long, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/DayOfMonth.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class DayOfMonthFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.Int, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new int?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.Int, data));
30 | }
31 |
32 | private static int? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return input.Value.Day;
37 | }
38 |
39 | return null;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/DayOfWeek.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class DayOfWeekFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.TimeSpan, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new TimeSpan?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.TimeSpan, data));
30 | }
31 |
32 | private static TimeSpan? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | // Sunday: 0, Monday: 1, etc...
37 | // Luckily, this matches how enum DayOfWeek is defined
38 | return TimeSpan.FromDays((int)input.Value.DayOfWeek);
39 | }
40 |
41 | return null;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/DayOfYear.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class DayOfYearFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.Int, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new int?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.Int, data));
30 | }
31 |
32 | private static int? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | var startOfYear = new DateTime(
37 | year: input.Value.Year,
38 | month: 1,
39 | day: 1,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | return (int)(input.Value - startOfYear).TotalDays + 1;
45 | }
46 |
47 | return null;
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/EndOfDay.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class EndOfDayFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: input.Value.Day,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind).AddDays(1).AddTicks(-1);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/EndOfMonth.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class EndOfMonthFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: 1,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind).AddMonths(1).AddTicks(-1);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/EndOfWeek.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class EndOfWeekFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | var startOfDay = new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: input.Value.Day,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | var startOfWeek = startOfDay.AddDays(-(int)startOfDay.DayOfWeek);
45 | return startOfWeek.AddDays(7).AddTicks(-1);
46 | }
47 |
48 | return null;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/EndOfYear.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class EndOfYearFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: 1,
39 | day: 1,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind).AddYears(1).AddTicks(-1);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Exp.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class ExpFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var value = (double?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.Real, Impl(value));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var valueCol = (Column)(arguments[0].Column);
24 |
25 | var data = new double?[valueCol.RowCount];
26 | for (int i = 0; i < valueCol.RowCount; i++)
27 | {
28 | var value = valueCol[i];
29 | data[i] = Impl(value);
30 | }
31 |
32 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
33 | }
34 |
35 | private static double? Impl(double? input)
36 | {
37 | if (input.HasValue)
38 | {
39 | return Math.Exp(input.Value);
40 | }
41 |
42 | return null;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/IsEmpty.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class IsEmptyFunctionImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 1);
14 | var value = (string?)arguments[0].Value;
15 |
16 | return new ScalarResult(ScalarTypes.Bool, string.IsNullOrEmpty(value));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var valueCol = (Column)(arguments[0].Column);
23 |
24 | var data = new bool?[valueCol.RowCount];
25 | for (int i = 0; i < valueCol.RowCount; i++)
26 | {
27 | var value = valueCol[i];
28 | data[i] = string.IsNullOrEmpty(value);
29 | }
30 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Log.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class LogFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var value = (double?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.Real, Impl(value));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var valueCol = (Column)(arguments[0].Column);
24 |
25 | var data = new double?[valueCol.RowCount];
26 | for (int i = 0; i < valueCol.RowCount; i++)
27 | {
28 | var value = valueCol[i];
29 | data[i] = Impl(value);
30 | }
31 |
32 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
33 | }
34 |
35 | private static double? Impl(double? input)
36 | {
37 | if (input.HasValue)
38 | {
39 | return Math.Log(input.Value);
40 | }
41 |
42 | return null;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Log10.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class Log10FunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var value = (double?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.Real, Impl(value));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var valueCol = (Column)(arguments[0].Column);
24 |
25 | var data = new double?[valueCol.RowCount];
26 | for (int i = 0; i < valueCol.RowCount; i++)
27 | {
28 | var value = valueCol[i];
29 | data[i] = Impl(value);
30 | }
31 |
32 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
33 | }
34 |
35 | private static double? Impl(double? input)
36 | {
37 | if (input.HasValue)
38 | {
39 | return Math.Log10(input.Value);
40 | }
41 |
42 | return null;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Log2.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class Log2FunctionImpl : IScalarFunctionImpl
11 | {
12 | private static readonly double Log2 = Math.Log(2);
13 |
14 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
15 | {
16 | Debug.Assert(arguments.Length == 1);
17 | var value = (double?)arguments[0].Value;
18 |
19 | return new ScalarResult(ScalarTypes.Real, Impl(value));
20 | }
21 |
22 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
23 | {
24 | Debug.Assert(arguments.Length == 1);
25 | var valueCol = (Column)(arguments[0].Column);
26 |
27 | var data = new double?[valueCol.RowCount];
28 | for (int i = 0; i < valueCol.RowCount; i++)
29 | {
30 | var value = valueCol[i];
31 | data[i] = Impl(value);
32 | }
33 |
34 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
35 | }
36 |
37 | private static double? Impl(double? input)
38 | {
39 | if (input.HasValue)
40 | {
41 | return Math.Log(input.Value) / Log2;
42 | }
43 |
44 | return null;
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Not.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class NotFunctionImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 1);
14 | var value = (bool?)arguments[0].Value;
15 |
16 | return new ScalarResult(ScalarTypes.Bool, value.HasValue ? !value.Value : null);
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var valueCol = (Column)(arguments[0].Column);
23 |
24 | var data = new bool?[valueCol.RowCount];
25 | for (int i = 0; i < valueCol.RowCount; i++)
26 | {
27 | var value = valueCol[i];
28 | data[i] = value.HasValue ? !value.Value : null;
29 | }
30 |
31 | return new ColumnarResult(Column.Create(ScalarTypes.Bool, data));
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Now.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class NowFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 0);
15 | return new ScalarResult(ScalarTypes.DateTime, DateTime.UtcNow);
16 | }
17 |
18 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
19 | {
20 | throw new NotSupportedException();
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Pow.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class PowFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 2);
15 | var x = (double?)arguments[0].Value;
16 | var y = (double?)arguments[1].Value;
17 |
18 | return new ScalarResult(ScalarTypes.Real, Impl(x, y));
19 | }
20 |
21 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
22 | {
23 | Debug.Assert(arguments.Length == 2);
24 | var xCol = (Column)(arguments[0].Column);
25 | var yCol = (Column)(arguments[1].Column);
26 |
27 | var data = new double?[xCol.RowCount];
28 | for (int i = 0; i < xCol.RowCount; i++)
29 | {
30 | data[i] = Impl(xCol[i], yCol[i]);
31 | }
32 |
33 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
34 | }
35 |
36 | private static double? Impl(double? x, double? y)
37 | {
38 | if (x.HasValue && y.HasValue)
39 | {
40 | return Math.Pow(x.Value, y.Value);
41 | }
42 |
43 | return null;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/ReplaceString.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class ReplaceStringFunctionImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 3);
14 | var text = (string?)arguments[0].Value;
15 | var lookup = (string?)arguments[1].Value;
16 | var rewrite = (string?)arguments[2].Value;
17 |
18 | return new ScalarResult(ScalarTypes.String, Impl(text, lookup, rewrite));
19 | }
20 |
21 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
22 | {
23 | Debug.Assert(arguments.Length == 3);
24 | var textCol = (Column)arguments[0].Column;
25 | var lookupCol = (Column)arguments[1].Column;
26 | var rewriteCol = (Column)arguments[2].Column;
27 |
28 | var data = new string?[textCol.RowCount];
29 | for (int i = 0; i < textCol.RowCount; i++)
30 | {
31 | data[i] = Impl(textCol[i], lookupCol[i], rewriteCol[i]);
32 | }
33 |
34 | return new ColumnarResult(Column.Create(ScalarTypes.String, data));
35 | }
36 |
37 | static string? Impl(string? text, string? lookup, string? rewrite)
38 | {
39 | if (string.IsNullOrEmpty(text))
40 | {
41 | return string.Empty;
42 | }
43 |
44 | if (string.IsNullOrEmpty(lookup))
45 | {
46 | return text;
47 | }
48 |
49 | return text.Replace(lookup, rewrite ?? string.Empty);
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Sqrt.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class SqrtFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var value = (double?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.Real, Impl(value));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var valueCol = (Column)(arguments[0].Column);
24 |
25 | var data = new double?[valueCol.RowCount];
26 | for (int i = 0; i < valueCol.RowCount; i++)
27 | {
28 | var value = valueCol[i];
29 | data[i] = Impl(value);
30 | }
31 |
32 | return new ColumnarResult(Column.Create(ScalarTypes.Real, data));
33 | }
34 |
35 | private static double? Impl(double? input)
36 | {
37 | if (input.HasValue)
38 | {
39 | return Math.Sqrt(input.Value);
40 | }
41 |
42 | return null;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/StartOfDay.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class StartOfDayFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: input.Value.Day,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/StartOfMonth.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class StartOfMonthFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: 1,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/StartOfWeek.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class StartOfWeekFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | var startOfDay = new DateTime(
37 | year: input.Value.Year,
38 | month: input.Value.Month,
39 | day: input.Value.Day,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | var startOfWeek = startOfDay.AddDays(-(int)startOfDay.DayOfWeek);
45 | return startOfWeek;
46 | }
47 |
48 | return null;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/StartOfYear.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class StartOfYearFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var date = (DateTime?)arguments[0].Value;
16 | return new ScalarResult(ScalarTypes.DateTime, Impl(date));
17 | }
18 |
19 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
20 | {
21 | Debug.Assert(arguments.Length == 1);
22 | var dates = (Column)arguments[0].Column;
23 |
24 | var data = new DateTime?[dates.RowCount];
25 | for (int i = 0; i < dates.RowCount; i++)
26 | {
27 | data[i] = Impl(dates[i]);
28 | }
29 | return new ColumnarResult(Column.Create(ScalarTypes.DateTime, data));
30 | }
31 |
32 | private static DateTime? Impl(DateTime? input)
33 | {
34 | if (input.HasValue)
35 | {
36 | return new DateTime(
37 | year: input.Value.Year,
38 | month: 1,
39 | day: 1,
40 | hour: 0,
41 | minute: 0,
42 | second: 0,
43 | kind: input.Value.Kind);
44 | }
45 |
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Strcat.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using System.Text;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class StrcatFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length > 0);
15 | var builder = new StringBuilder();
16 | for (int i = 0; i < arguments.Length; i++)
17 | {
18 | builder.Append((string?)arguments[i].Value);
19 | }
20 |
21 | return new ScalarResult(ScalarTypes.String, builder.ToString());
22 | }
23 |
24 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
25 | {
26 | Debug.Assert(arguments.Length > 0);
27 | var columns = new Column[arguments.Length];
28 | for (int i = 0; i < arguments.Length; i++)
29 | {
30 | columns[i] = (Column)arguments[i].Column;
31 | }
32 |
33 | var data = new string?[columns[0].RowCount];
34 | var builder = new StringBuilder();
35 | for (int i = 0; i < columns[0].RowCount; i++)
36 | {
37 | for (int j = 0; j < columns.Length; j++)
38 | {
39 | builder.Append(columns[j][i]);
40 | }
41 | data[i] = builder.ToString();
42 |
43 | builder.Clear();
44 | }
45 |
46 | return new ColumnarResult(Column.Create(ScalarTypes.String, data));
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Strlen.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
8 | {
9 | internal class StrlenFunctionImpl : IScalarFunctionImpl
10 | {
11 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
12 | {
13 | Debug.Assert(arguments.Length == 1);
14 | var text = (string?)arguments[0].Value;
15 | return new ScalarResult(ScalarTypes.Long, (long)(text ?? string.Empty).Length);
16 | }
17 |
18 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
19 | {
20 | Debug.Assert(arguments.Length == 1);
21 | var column = (Column)arguments[0].Column;
22 |
23 | var data = new long?[column.RowCount];
24 | for (int i = 0; i < column.RowCount; i++)
25 | {
26 | data[i] = (long)(column[i] ?? string.Empty).Length;
27 | }
28 | return new ColumnarResult(Column.Create(ScalarTypes.Long, data));
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/Substring.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class SubstringFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 3);
15 | var input = (string?)arguments[0].Value;
16 | var start = (long?)arguments[1].Value;
17 | var length = (long?)arguments[2].Value;
18 |
19 | return new ScalarResult(ScalarTypes.String, Impl(input, start, length));
20 | }
21 |
22 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
23 | {
24 | Debug.Assert(arguments.Length == 3);
25 | var inputCol = (Column)arguments[0].Column;
26 | var startCol = (Column)arguments[1].Column;
27 | var lengthCol = (Column)arguments[2].Column;
28 |
29 | var data = new string?[inputCol.RowCount];
30 | for (int i = 0; i < inputCol.RowCount; i++)
31 | {
32 | data[i] = Impl(inputCol[i], startCol[i], lengthCol[i]);
33 | }
34 |
35 | return new ColumnarResult(Column.Create(ScalarTypes.String, data));
36 | }
37 |
38 | static string? Impl(string? input, long? start, long? length)
39 | {
40 | if (string.IsNullOrEmpty(input))
41 | {
42 | return string.Empty;
43 | }
44 |
45 | var effectiveStart = start.HasValue ? Math.Max(0, Math.Min(start.Value, input.Length)) : 0;
46 | var maxAllowableLength = input.Length - effectiveStart;
47 | var effectiveLength = length.HasValue ? Math.Max(0, Math.Min(length.Value, maxAllowableLength)) : maxAllowableLength;
48 | return input.Substring((int)effectiveStart, (int)effectiveLength);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/UrlDecode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class UrlDecodeFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var url = (string?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.String, Impl(url));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var urlCol = (Column)arguments[0].Column;
24 |
25 | var data = new string?[urlCol.RowCount];
26 | for (int i = 0; i < urlCol.RowCount; i++)
27 | {
28 | data[i] = Impl(urlCol[i]);
29 | }
30 |
31 | return new ColumnarResult(Column.Create(ScalarTypes.String, data));
32 | }
33 |
34 | static string? Impl(string? url)
35 | {
36 | if (string.IsNullOrEmpty(url))
37 | {
38 | return string.Empty;
39 | }
40 |
41 | return Uri.UnescapeDataString(url);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/BuiltIns/ScalarFunctions/UrlEncodeComponent.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation.BuiltIns.Impl
9 | {
10 | internal class UrlEncodeComponentFunctionImpl : IScalarFunctionImpl
11 | {
12 | public ScalarResult InvokeScalar(ScalarResult[] arguments)
13 | {
14 | Debug.Assert(arguments.Length == 1);
15 | var url = (string?)arguments[0].Value;
16 |
17 | return new ScalarResult(ScalarTypes.String, Impl(url));
18 | }
19 |
20 | public ColumnarResult InvokeColumnar(ColumnarResult[] arguments)
21 | {
22 | Debug.Assert(arguments.Length == 1);
23 | var urlCol = (Column)arguments[0].Column;
24 |
25 | var data = new string?[urlCol.RowCount];
26 | for (int i = 0; i < urlCol.RowCount; i++)
27 | {
28 | data[i] = Impl(urlCol[i]);
29 | }
30 |
31 | return new ColumnarResult(Column.Create(ScalarTypes.String, data));
32 | }
33 |
34 | static string? Impl(string? url)
35 | {
36 | if (string.IsNullOrEmpty(url))
37 | {
38 | return string.Empty;
39 | }
40 |
41 | return Uri.EscapeDataString(url);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/Infra/EvaluationContext.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.Evaluation
5 | {
6 | internal record EvaluationContext(LocalScope Scope, TabularResult? Left = null, ITableChunk? Chunk = null);
7 | }
8 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/Infra/LocalScope.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.Evaluation
9 | {
10 | internal class LocalScope
11 | {
12 | private readonly LocalScope? _outer;
13 | private readonly Dictionary _locals = new();
14 |
15 | public LocalScope(LocalScope? outer = null)
16 | {
17 | _outer = outer;
18 | }
19 |
20 | public void AddSymbol(Symbol symbol, EvaluationResult? value)
21 | {
22 | if (Lookup(symbol.Name) != null)
23 | {
24 | throw new InvalidOperationException($"A symbol with name {symbol.Name} is already defined.");
25 | }
26 |
27 | _locals.Add(symbol.Name, (symbol, value));
28 | }
29 |
30 | public (Symbol Symbol, EvaluationResult? Value)? Lookup(string name)
31 | {
32 | if (_locals.TryGetValue(name, out var symbol))
33 | {
34 | return symbol;
35 | }
36 |
37 | return _outer?.Lookup(name);
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/TreeEvaluator.Expressions.Operators.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using BabyKusto.Core.Evaluation.BuiltIns;
6 | using BabyKusto.Core.InternalRepresentation;
7 |
8 | namespace BabyKusto.Core.Evaluation
9 | {
10 | internal partial class TreeEvaluator
11 | {
12 | public override EvaluationResult? VisitUnaryExpression(IRUnaryExpressionNode node, EvaluationContext context)
13 | {
14 | var impl = node.GetOrSetCache(
15 | () =>
16 | {
17 | var argumentExpressions = new IRExpressionNode[] { node.Expression };
18 | return BuiltInsHelper.GetScalarImplementation(argumentExpressions, node.OverloadInfo.ScalarImpl, node.ResultKind, node.ResultType);
19 | });
20 |
21 | var val = node.Expression.Accept(this, context);
22 | Debug.Assert(val != null);
23 | var arguments = new[] { val };
24 | return impl(arguments);
25 | }
26 |
27 | public override EvaluationResult? VisitBinaryExpression(IRBinaryExpressionNode node, EvaluationContext context)
28 | {
29 | var impl = node.GetOrSetCache(
30 | () =>
31 | {
32 | var argumentExpressions = new IRExpressionNode[] { node.Left, node.Right };
33 | return BuiltInsHelper.GetScalarImplementation(argumentExpressions, node.OverloadInfo.ScalarImpl, node.ResultKind, node.ResultType);
34 | });
35 |
36 | var leftVal = node.Left.Accept(this, context);
37 | var rightVal = node.Right.Accept(this, context);
38 | Debug.Assert(leftVal != null && rightVal != null);
39 | var arguments = new[] { leftVal, rightVal };
40 | return impl(arguments);
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/TreeEvaluator.QueryOperators.Print.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using BabyKusto.Core.InternalRepresentation;
6 | using BabyKusto.Core.Util;
7 | using Kusto.Language.Symbols;
8 |
9 | namespace BabyKusto.Core.Evaluation
10 | {
11 | internal partial class TreeEvaluator
12 | {
13 | public override EvaluationResult? VisitPrintOperator(IRPrintOperatorNode node, EvaluationContext context)
14 | {
15 | var tableSymbol = (TableSymbol)node.ResultType;
16 |
17 | var columns = new Column[node.Expressions.ChildCount];
18 | for (int i = 0; i < node.Expressions.ChildCount; i++)
19 | {
20 | var expression = node.Expressions.GetChild(i);
21 | var expressionResult = expression.Accept(this, context);
22 | Debug.Assert(expressionResult != null);
23 | var scalarResult = (ScalarResult)expressionResult;
24 | columns[i] = ColumnHelpers.CreateFromScalar(scalarResult.Value, tableSymbol.Columns[i].Type, 1);
25 | }
26 |
27 | var result = new InMemoryTableSource(tableSymbol, columns);
28 | return new TabularResult(result, visualizationState: null);
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/TreeEvaluator.QueryOperators.Render.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using BabyKusto.Core.InternalRepresentation;
6 |
7 | namespace BabyKusto.Core.Evaluation
8 | {
9 | internal partial class TreeEvaluator
10 | {
11 | public override EvaluationResult? VisitRenderOperator(IRRenderOperatorNode node, EvaluationContext context)
12 | {
13 | Debug.Assert(context.Left != null);
14 |
15 | return new TabularResult(
16 | value: context.Left.Value,
17 | visualizationState: new VisualizationState(ChartType: node.ChartType, ChartKind: node.Kind));
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/TreeEvaluator.QueryOperators.ToScalar.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using BabyKusto.Core.Extensions;
7 | using BabyKusto.Core.InternalRepresentation;
8 |
9 | namespace BabyKusto.Core.Evaluation
10 | {
11 | internal partial class TreeEvaluator
12 | {
13 | public override EvaluationResult? VisitToScalarExpressionNode(IRToScalarExpressionNode node, EvaluationContext context)
14 | {
15 | var result = node.Expression.Accept(this, context);
16 | Debug.Assert(result != null);
17 |
18 | if (node.Expression.ResultKind == EvaluatedExpressionKind.Table)
19 | {
20 | var table = ((TabularResult)result).Value;
21 |
22 | // TODO: We should have a way to evaluate this asynchronously as well...
23 | foreach (var chunk in table.GetData())
24 | {
25 | if (chunk.Columns[0].RowCount > 0)
26 | {
27 | Debug.Assert(chunk.Columns[0].Type.Simplify() == node.ResultType.Simplify());
28 | return new ScalarResult(chunk.Columns[0].Type, chunk.Columns[0].RawData.GetValue(0));
29 | }
30 | }
31 |
32 | return new ScalarResult(node.ResultType, null);
33 | }
34 | else if (node.Expression.ResultKind == EvaluatedExpressionKind.Scalar)
35 | {
36 | var scalarValue = (ScalarResult)result;
37 | return scalarValue;
38 | }
39 | else
40 | {
41 | throw new InvalidOperationException($"Unexpected result kind {node.Expression.ResultKind}");
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Evaluation/TreeEvaluator.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Diagnostics;
5 | using BabyKusto.Core.InternalRepresentation;
6 |
7 | namespace BabyKusto.Core.Evaluation
8 | {
9 | internal partial class TreeEvaluator : DefaultIRNodeVisitor
10 | {
11 | public override EvaluationResult? VisitQueryBlock(IRQueryBlockNode node, EvaluationContext context)
12 | {
13 | EvaluationResult? lastResult = null;
14 | for (int i = 0; i < node.Statements.ChildCount; i++)
15 | {
16 | var statement = node.Statements.GetChild(i);
17 | lastResult = statement.Accept(this, context);
18 | }
19 |
20 | return lastResult;
21 | }
22 |
23 | public override EvaluationResult? VisitLetStatement(IRLetStatementNode node, EvaluationContext context)
24 | {
25 | var value = node.Expression.Accept(this, context);
26 | context.Scope.AddSymbol(node.Symbol, value);
27 | return null;
28 | }
29 |
30 | public override EvaluationResult? VisitFunctionDeclaration(IRFunctionDeclarationNode node, EvaluationContext context)
31 | {
32 | // Kusto lib has already done the heavy lifting, we can skip...
33 | return null;
34 | }
35 |
36 | public override EvaluationResult? VisitExpressionStatement(IRExpressionStatementNode node, EvaluationContext context)
37 | {
38 | return node.Expression.Accept(this, context);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/EvaluationResult.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.Evaluation
8 | {
9 | public abstract class EvaluationResult
10 | {
11 | protected EvaluationResult(TypeSymbol type)
12 | {
13 | Type = type;
14 | }
15 |
16 | public TypeSymbol Type { get; }
17 |
18 | public bool IsScalar => this is ScalarResult;
19 | public bool IsColumnar => this is ColumnarResult;
20 | public bool IsTabular => this is TabularResult;
21 |
22 | public override string ToString()
23 | {
24 | return $"{this.GetType().Name}: {SchemaDisplay.GetText(Type)}";
25 | }
26 | }
27 |
28 | public sealed class ScalarResult : EvaluationResult
29 | {
30 | public ScalarResult(TypeSymbol type, object? value)
31 | : base(type)
32 | {
33 | if (!type.IsScalar)
34 | {
35 | throw new InvalidOperationException($"Expected scalar type for {nameof(ScalarResult)} value, got '{SchemaDisplay.GetText(type)}'.");
36 | }
37 |
38 | Value = value;
39 | }
40 |
41 | public object? Value { get; }
42 | }
43 |
44 | public sealed class ColumnarResult : EvaluationResult
45 | {
46 | public ColumnarResult(Column column)
47 | : base(column.Type)
48 | {
49 | Column = column;
50 | }
51 |
52 | public Column Column { get; }
53 | }
54 |
55 | public sealed class TabularResult : EvaluationResult
56 | {
57 | public TabularResult(ITableSource value, VisualizationState? visualizationState)
58 | : base(value.Type)
59 | {
60 | Value = value;
61 | VisualizationState = visualizationState;
62 | }
63 |
64 | public ITableSource Value { get; }
65 |
66 | public VisualizationState? VisualizationState { get; }
67 | }
68 |
69 | public record VisualizationState(string ChartType, string? ChartKind)
70 | {
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/IRNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal abstract class IRNode
9 | {
10 | public virtual int ChildCount => 0;
11 | public virtual IRNode GetChild(int index) => throw new ArgumentOutOfRangeException();
12 |
13 | public abstract TResult? Accept(IRNodeVisitor visitor, TContext context)
14 | where TResult: class;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRAggregateCallNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using BabyKusto.Core.Evaluation.BuiltIns;
7 | using Kusto.Language.Symbols;
8 |
9 | namespace BabyKusto.Core.InternalRepresentation
10 | {
11 | internal class IRAggregateCallNode : IRExpressionNode
12 | {
13 | public IRAggregateCallNode(Signature signature, AggregateOverloadInfo overloadInfo, IReadOnlyList parameters, IRListNode arguments, TypeSymbol resultType)
14 | : base(resultType, EvaluatedExpressionKind.Scalar)
15 | {
16 | Signature = signature ?? throw new ArgumentNullException(nameof(signature));
17 | OverloadInfo = overloadInfo ?? throw new ArgumentNullException(nameof(overloadInfo));
18 | Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
19 | Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
20 | }
21 |
22 | public Signature Signature { get; }
23 | public AggregateOverloadInfo OverloadInfo { get; }
24 | public IReadOnlyList Parameters { get; }
25 | public IRListNode Arguments { get; }
26 |
27 | public override int ChildCount => 1;
28 | public override IRNode GetChild(int index) =>
29 | index switch
30 | {
31 | 0 => Arguments,
32 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
33 | };
34 |
35 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
36 | where TResult : class
37 | {
38 | return visitor.VisitAggregateCallNode(this, context);
39 | }
40 |
41 | public override string ToString()
42 | {
43 | return $"AggregateCall({SchemaDisplay.GetText(Signature.Symbol)}): {SchemaDisplay.GetText(ResultType)}";
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRBinaryExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using BabyKusto.Core.Evaluation.BuiltIns;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.InternalRepresentation
9 | {
10 | internal class IRBinaryExpressionNode : IRExpressionNode
11 | {
12 | public IRBinaryExpressionNode(Signature signature, ScalarOverloadInfo overloadInfo, IRExpressionNode left, IRExpressionNode right, TypeSymbol resultType)
13 | : base(resultType, GetResultKind(left.ResultKind, right.ResultKind))
14 | {
15 | Signature = signature ?? throw new ArgumentNullException(nameof(signature));
16 | OverloadInfo = overloadInfo ?? throw new ArgumentNullException(nameof(overloadInfo));
17 | Left = left ?? throw new ArgumentNullException(nameof(left));
18 | Right = right ?? throw new ArgumentNullException(nameof(right));
19 | }
20 |
21 | public Signature Signature { get; }
22 | public ScalarOverloadInfo OverloadInfo { get; }
23 | public IRExpressionNode Left { get; }
24 | public IRExpressionNode Right { get; }
25 |
26 | public override int ChildCount => 2;
27 | public override IRNode GetChild(int index) =>
28 | index switch
29 | {
30 | 0 => Left,
31 | 1 => Right,
32 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
33 | };
34 |
35 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
36 | where TResult : class
37 | {
38 | return visitor.VisitBinaryExpression(this, context);
39 | }
40 |
41 | public override string ToString()
42 | {
43 | return $"BinaryExpression({SchemaDisplay.GetText(Signature.Symbol)}): {SchemaDisplay.GetText(ResultType)}";
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRBuiltInScalarFunctionCallNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using BabyKusto.Core.Evaluation.BuiltIns;
7 | using Kusto.Language.Symbols;
8 |
9 | namespace BabyKusto.Core.InternalRepresentation
10 | {
11 | internal class IRBuiltInScalarFunctionCallNode : IRExpressionNode
12 | {
13 | public IRBuiltInScalarFunctionCallNode(Signature signature, ScalarOverloadInfo overloadInfo, IReadOnlyList parameters, IRListNode arguments, TypeSymbol resultType)
14 | : base(resultType, GetResultKind(arguments))
15 | {
16 | Signature = signature ?? throw new ArgumentNullException(nameof(signature));
17 | OverloadInfo = overloadInfo ?? throw new ArgumentNullException(nameof(overloadInfo));
18 | Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
19 | Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
20 | }
21 |
22 | public Signature Signature { get; }
23 | public ScalarOverloadInfo OverloadInfo { get; }
24 | public IReadOnlyList Parameters { get; }
25 | public IRListNode Arguments { get; }
26 |
27 | public override int ChildCount => 1;
28 | public override IRNode GetChild(int index) =>
29 | index switch
30 | {
31 | 0 => Arguments,
32 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
33 | };
34 |
35 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
36 | where TResult : class
37 | {
38 | return visitor.VisitBuiltInScalarFunctionCall(this, context);
39 | }
40 |
41 | public override string ToString()
42 | {
43 | return $"BuiltInScalarFunctionCall({SchemaDisplay.GetText(Signature.Symbol)}): {SchemaDisplay.GetText(ResultType)}";
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRBuiltInWindowFunctionCallNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using BabyKusto.Core.Evaluation.BuiltIns;
7 | using Kusto.Language.Symbols;
8 |
9 | namespace BabyKusto.Core.InternalRepresentation
10 | {
11 | internal class IRBuiltInWindowFunctionCallNode : IRExpressionNode
12 | {
13 | public IRBuiltInWindowFunctionCallNode(Signature signature, WindowOverloadInfo overloadInfo, IReadOnlyList parameters, IRListNode arguments, TypeSymbol resultType)
14 | : base(resultType, GetResultKind(arguments))
15 | {
16 | Signature = signature ?? throw new ArgumentNullException(nameof(signature));
17 | OverloadInfo = overloadInfo ?? throw new ArgumentNullException(nameof(overloadInfo));
18 | Parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
19 | Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
20 | }
21 |
22 | public Signature Signature { get; }
23 | public WindowOverloadInfo OverloadInfo { get; }
24 | public IReadOnlyList Parameters { get; }
25 | public IRListNode Arguments { get; }
26 |
27 | public override int ChildCount => 1;
28 | public override IRNode GetChild(int index) =>
29 | index switch
30 | {
31 | 0 => Arguments,
32 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
33 | };
34 |
35 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
36 | where TResult : class
37 | {
38 | return visitor.VisitBuiltInWindowFunctionCall(this, context);
39 | }
40 |
41 | public override string ToString()
42 | {
43 | return $"BuiltInWindowFunctionCall({SchemaDisplay.GetText(Signature.Symbol)}): {SchemaDisplay.GetText(ResultType)}";
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRCastExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRCastExpressionNode : IRExpressionNode
10 | {
11 | public IRCastExpressionNode(IRExpressionNode expression, TypeSymbol resultType)
12 | : base(resultType, expression.ResultKind)
13 | {
14 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | }
16 |
17 | public IRExpressionNode Expression { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expression,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitCastExpression(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"CastExpression: {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRDataTableExpression.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRDataTableExpression : IRExpressionNode
10 | {
11 | public IRDataTableExpression(object?[] data, TypeSymbol resultType)
12 | : base(resultType, EvaluatedExpressionKind.Table)
13 | {
14 | Data = data ?? throw new ArgumentNullException(nameof(data));
15 | if (data.Length % resultType.Members.Count != 0)
16 | {
17 | throw new ArgumentException($"Invalid number of columns in data. Expected a multiple of {resultType.Members.Count}, found {data.Length}.");
18 | }
19 | }
20 |
21 | public object?[] Data { get; }
22 |
23 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
24 | where TResult : class
25 | {
26 | return visitor.VisitDataTableExpression(this, context);
27 | }
28 |
29 | public override string ToString()
30 | {
31 | return $"DataTableExpression: {SchemaDisplay.GetText(ResultType)}";
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRFunctionDeclarationNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal class IRFunctionDeclarationNode : IRExpressionNode
9 | {
10 | public IRFunctionDeclarationNode(TypeSymbol resultType)
11 | : base(resultType, EvaluatedExpressionKind.Scalar)
12 | {
13 | }
14 |
15 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
16 | where TResult : class
17 | {
18 | return visitor.VisitFunctionDeclaration(this, context);
19 | }
20 |
21 | public override string ToString()
22 | {
23 | return $"FunctionDeclaration: {SchemaDisplay.GetText(ResultType)}";
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRJoinOnClause.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRJoinOnClause
10 | {
11 | public IRJoinOnClause(IRRowScopeNameReferenceNode left, IRRowScopeNameReferenceNode right)
12 | {
13 | Left = left ?? throw new ArgumentNullException(nameof(left));
14 | Right = right ?? throw new ArgumentNullException(nameof(right));
15 | }
16 |
17 | public IRRowScopeNameReferenceNode Left { get; }
18 | public IRRowScopeNameReferenceNode Right { get; }
19 |
20 | public override string ToString()
21 | {
22 | return $"JoinOnClause({SchemaDisplay.GetText(Left.ReferencedSymbol)}, {SchemaDisplay.GetText(Right.ReferencedSymbol)})";
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRLiteralExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal class IRLiteralExpressionNode : IRExpressionNode
9 | {
10 | public IRLiteralExpressionNode(object? value, TypeSymbol resultType)
11 | : base(resultType, EvaluatedExpressionKind.Scalar)
12 | {
13 | Value = value;
14 | }
15 |
16 | public object? Value { get; }
17 |
18 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
19 | where TResult : class
20 | {
21 | return visitor.VisitLiteralExpression(this, context);
22 | }
23 |
24 | public override string ToString()
25 | {
26 | return $"LiteralExpression: {SchemaDisplay.GetText(ResultType)} = {Value}";
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRMaterializeExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRMaterializeExpressionNode : IRExpressionNode
10 | {
11 | public IRMaterializeExpressionNode(IRExpressionNode expression, TypeSymbol resultType)
12 | : base(resultType, expression.ResultKind)
13 | {
14 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | }
16 |
17 | public IRExpressionNode Expression { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expression,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitMaterializeExpression(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return "MaterializeExpression";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRMemberAccessNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRMemberAccessNode : IRExpressionNode
10 | {
11 | public IRMemberAccessNode(IRExpressionNode expression, string memberName, TypeSymbol resultType)
12 | : base(resultType, expression.ResultKind)
13 | {
14 | this.Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 |
16 | if (string.IsNullOrEmpty(memberName))
17 | {
18 | throw new ArgumentNullException(nameof(memberName));
19 | }
20 |
21 | this.MemberName = memberName;
22 | }
23 |
24 | public IRExpressionNode Expression { get; }
25 | public string MemberName { get; }
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitMemberAccess(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"IRMemberAccess({MemberName}): {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRNameReferenceNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRNameReferenceNode : IRExpressionNode
10 | {
11 | public IRNameReferenceNode(Symbol referencedSymbol, TypeSymbol resultType, EvaluatedExpressionKind resultKind)
12 | : base(resultType, resultKind)
13 | {
14 | this.ReferencedSymbol = referencedSymbol ?? throw new ArgumentNullException(nameof(referencedSymbol));
15 | }
16 |
17 | public IRNameReferenceNode(Symbol referencedSymbol, TypeSymbol resultType)
18 | : this(referencedSymbol, resultType, GetResultKind(resultType))
19 | {
20 | }
21 |
22 | public Symbol ReferencedSymbol { get; }
23 |
24 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
25 | where TResult : class
26 | {
27 | return visitor.VisitNameReference(this, context);
28 | }
29 |
30 | public override string ToString()
31 | {
32 | return $"NameReference({ReferencedSymbol.Name}): {SchemaDisplay.GetText(ResultType)}";
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IROutputColumnNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IROutputColumnNode : IRExpressionNode
10 | {
11 | public IROutputColumnNode(ColumnSymbol symbol, IRExpressionNode expression)
12 | : base(symbol.Type, expression.ResultKind)
13 | {
14 | this.Symbol = symbol;
15 | this.Expression = expression;
16 | }
17 |
18 | public ColumnSymbol Symbol { get; }
19 | public IRExpressionNode Expression { get; }
20 |
21 | public override int ChildCount => 1;
22 | public override IRNode GetChild(int index) =>
23 | index switch
24 | {
25 | 0 => Expression,
26 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
27 | };
28 |
29 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
30 | where TResult : class
31 | {
32 | return visitor.VisitOutputColumn(this, context);
33 | }
34 |
35 | public override string ToString()
36 | {
37 | return $"OutputColumn({Symbol.Name}: {SchemaDisplay.GetText(ResultType)})";
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRPipeExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRPipeExpressionNode : IRExpressionNode
10 | {
11 | public IRPipeExpressionNode(IRExpressionNode expression, IRQueryOperatorNode @operator, TypeSymbol resultType)
12 | : base(resultType, EvaluatedExpressionKind.Table)
13 | {
14 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | Operator = @operator ?? throw new ArgumentNullException(nameof(@operator));
16 | }
17 |
18 | public IRExpressionNode Expression { get; }
19 | public IRQueryOperatorNode Operator { get; }
20 |
21 | public override int ChildCount => 2;
22 | public override IRNode GetChild(int index) =>
23 | index switch
24 | {
25 | 0 => Expression,
26 | 1 => Operator,
27 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
28 | };
29 |
30 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
31 | where TResult : class
32 | {
33 | return visitor.VisitPipeExpression(this, context);
34 | }
35 |
36 | public override string ToString()
37 | {
38 | return $"PipeExpression: {SchemaDisplay.GetText(ResultType)}";
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRRowScopeNameReferenceNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRRowScopeNameReferenceNode : IRExpressionNode
10 | {
11 | public IRRowScopeNameReferenceNode(Symbol referencedSymbol, TypeSymbol resultType, int referencedColumnIndex)
12 | : base(resultType, EvaluatedExpressionKind.Columnar)
13 | {
14 | if (referencedColumnIndex < 0)
15 | {
16 | throw new ArgumentOutOfRangeException(nameof(referencedColumnIndex));
17 | }
18 |
19 | ReferencedSymbol = referencedSymbol ?? throw new ArgumentNullException(nameof(referencedSymbol));
20 | ReferencedColumnIndex = referencedColumnIndex;
21 | }
22 |
23 | public Symbol ReferencedSymbol { get; }
24 | public int ReferencedColumnIndex { get; }
25 |
26 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
27 | where TResult : class
28 | {
29 | return visitor.VisitRowScopeNameReferenceNode(this, context);
30 | }
31 |
32 | public override string ToString()
33 | {
34 | return $"RowScopeNameReferenceNode({ReferencedSymbol.Name}=[{ReferencedColumnIndex}]): {SchemaDisplay.GetText(ResultType)}";
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRToScalarExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRToScalarExpressionNode : IRExpressionNode
10 | {
11 | public IRToScalarExpressionNode(IRExpressionNode expression, TypeSymbol resultType)
12 | : base(resultType, expression.ResultKind)
13 | {
14 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | }
16 |
17 | public IRExpressionNode Expression { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expression,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitToScalarExpressionNode(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"ToScalarExpression: {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/IRUnaryExpressionNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using BabyKusto.Core.Evaluation.BuiltIns;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.InternalRepresentation
9 | {
10 | internal class IRUnaryExpressionNode : IRExpressionNode
11 | {
12 | public IRUnaryExpressionNode(Signature signature, ScalarOverloadInfo overloadInfo, IRExpressionNode expression, TypeSymbol resultType)
13 | : base(resultType, expression.ResultKind)
14 | {
15 | Signature = signature ?? throw new ArgumentNullException(nameof(signature));
16 | OverloadInfo = overloadInfo ?? throw new ArgumentNullException(nameof(overloadInfo));
17 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
18 | }
19 |
20 | public Signature Signature { get; }
21 | public ScalarOverloadInfo OverloadInfo { get; }
22 | public IRExpressionNode Expression { get; }
23 |
24 | public override int ChildCount => 1;
25 | public override IRNode GetChild(int index) =>
26 | index switch
27 | {
28 | 0 => Expression,
29 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
30 | };
31 |
32 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
33 | where TResult : class
34 | {
35 | return visitor.VisitUnaryExpression(this, context);
36 | }
37 |
38 | public override string ToString()
39 | {
40 | return $"UnaryExpression({SchemaDisplay.GetText(Signature.Symbol)}): {SchemaDisplay.GetText(ResultType)}";
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRFilterOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRFilterOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRFilterOperatorNode(IRExpressionNode condition, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | Condition = condition ?? throw new ArgumentNullException(nameof(condition));
15 | }
16 |
17 | public IRExpressionNode Condition;
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Condition,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitFilterOperator(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"FilterOperator: {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRJoinOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using Kusto.Language.Symbols;
7 |
8 | namespace BabyKusto.Core.InternalRepresentation
9 | {
10 | internal enum IRJoinKind
11 | {
12 | InnerUnique,
13 | Inner,
14 | LeftOuter,
15 | RightOuter,
16 | FullOuter,
17 | LeftSemi,
18 | RightSemi,
19 | LeftAnti,
20 | RightAnti,
21 | }
22 |
23 | internal class IRJoinOperatorNode : IRQueryOperatorNode
24 | {
25 | public IRJoinOperatorNode(IRJoinKind kind, IRExpressionNode expression, List onClauses, TypeSymbol resultType)
26 | : base(resultType)
27 | {
28 | Kind = kind;
29 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
30 | OnClauses = onClauses ?? throw new ArgumentNullException(nameof(onClauses));
31 | }
32 |
33 | public IRJoinKind Kind { get; }
34 | public IRExpressionNode Expression { get; }
35 | public List OnClauses { get; }
36 |
37 | public override int ChildCount => 1;
38 | public override IRNode GetChild(int index) =>
39 | index switch
40 | {
41 | 0 => Expression,
42 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
43 | };
44 |
45 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
46 | where TResult : class
47 | {
48 | return visitor.VisitJoinOperator(this, context);
49 | }
50 |
51 | public override string ToString()
52 | {
53 | return $"JoinOperator(kind={Kind}, {string.Join(", ", OnClauses)}): {SchemaDisplay.GetText(ResultType)}";
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRPrintOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRPrintOperatorNode : IRQueryOperatorNode
10 | {
11 |
12 | public IRPrintOperatorNode(IRListNode expressions, TypeSymbol resultType)
13 | : base(resultType)
14 | {
15 | Expressions = expressions ?? throw new ArgumentNullException(nameof(expressions));
16 | }
17 |
18 | public IRListNode Expressions { get; }
19 |
20 | public override int ChildCount => 1;
21 | public override IRNode GetChild(int index) =>
22 | index switch
23 | {
24 | 0 => Expressions,
25 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
26 | };
27 |
28 |
29 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
30 | where TResult : class
31 | {
32 | return visitor.VisitPrintOperator(this, context);
33 | }
34 |
35 | public override string ToString()
36 | {
37 | return $"PrintOperator: {SchemaDisplay.GetText(ResultType)}";
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRProjectOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRProjectOperatorNode : IRQueryOperatorNode
10 | {
11 |
12 | public IRProjectOperatorNode(IRListNode columns, TypeSymbol resultType)
13 | : base(resultType)
14 | {
15 | Columns = columns ?? throw new ArgumentNullException(nameof(columns));
16 | }
17 |
18 | public IRListNode Columns { get; }
19 |
20 | public override int ChildCount => 1;
21 | public override IRNode GetChild(int index) =>
22 | index switch
23 | {
24 | 0 => Columns,
25 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
26 | };
27 |
28 |
29 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
30 | where TResult : class
31 | {
32 | return visitor.VisitProjectOperator(this, context);
33 | }
34 |
35 | public override string ToString()
36 | {
37 | return $"ProjectOperator: {SchemaDisplay.GetText(ResultType)}";
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRQueryOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal abstract class IRQueryOperatorNode : IRExpressionNode
9 | {
10 | protected IRQueryOperatorNode(TypeSymbol resultType)
11 | : base(resultType, EvaluatedExpressionKind.Table)
12 | {
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRRenderOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRRenderOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRRenderOperatorNode(string chartType, string? kind, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | ChartType = chartType;
15 | Kind = kind;
16 | }
17 |
18 | public string ChartType { get; }
19 | public string? Kind { get; }
20 |
21 | public override int ChildCount => 0;
22 | public override IRNode GetChild(int index) => throw new ArgumentOutOfRangeException(nameof(index));
23 |
24 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
25 | where TResult : class
26 | {
27 | return visitor.VisitRenderOperator(this, context);
28 | }
29 |
30 | public override string ToString()
31 | {
32 | return $"RenderOperator";
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRSortOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRSortOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRSortOperatorNode(IRListNode expressions, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | this.Expressions = expressions ?? throw new ArgumentNullException(nameof(expressions));
15 | }
16 |
17 | public IRListNode Expressions { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expressions,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitSortOperator(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"SortOperator: {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRSummarizeOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRSummarizeOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRSummarizeOperatorNode(IRListNode aggregations, IRListNode byColumns, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | Aggregations = aggregations ?? throw new ArgumentNullException(nameof(aggregations));
15 | ByColumns = byColumns ?? throw new ArgumentNullException(nameof(byColumns));
16 | }
17 |
18 | public IRListNode Aggregations { get; }
19 | public IRListNode ByColumns { get; }
20 |
21 |
22 | public override int ChildCount => 2;
23 | public override IRNode GetChild(int index) =>
24 | index switch
25 | {
26 | 0 => Aggregations,
27 | 1 => ByColumns,
28 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
29 | };
30 |
31 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
32 | where TResult : class
33 | {
34 | return visitor.VisitSummarizeOperator(this, context);
35 | }
36 |
37 | public override string ToString()
38 | {
39 | return $"SummarizeOperator: {SchemaDisplay.GetText(ResultType)}";
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRTakeOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRTakeOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRTakeOperatorNode(IRExpressionNode expression, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | this.Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | }
16 |
17 | public IRExpressionNode Expression { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expression,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitTakeOperator(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"TakeOperator";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Expressions/QueryOperators/IRUnionOperatorNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRUnionOperatorNode : IRQueryOperatorNode
10 | {
11 | public IRUnionOperatorNode(IRListNode expressions, TypeSymbol resultType)
12 | : base(resultType)
13 | {
14 | this.Expressions = expressions ?? throw new ArgumentNullException(nameof(expressions));
15 | }
16 |
17 | public IRListNode Expressions { get; }
18 |
19 | public override int ChildCount => 1;
20 | public override IRNode GetChild(int index) =>
21 | index switch
22 | {
23 | 0 => Expressions,
24 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
25 | };
26 |
27 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
28 | where TResult : class
29 | {
30 | return visitor.VisitUnionOperator(this, context);
31 | }
32 |
33 | public override string ToString()
34 | {
35 | return $"UnionOperator: {SchemaDisplay.GetText(ResultType)}";
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/IRListNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal abstract class IRListNode : IRNode
10 | {
11 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
12 | where TResult : class
13 | {
14 | return visitor.VisitList(this, context);
15 | }
16 |
17 | public static IRListNode From(IReadOnlyList items)
18 | where T : IRNode
19 | {
20 | return new IRListNode(items);
21 | }
22 | }
23 |
24 | internal class IRListNode : IRListNode
25 | where T : IRNode
26 | {
27 | private readonly IReadOnlyList _children;
28 |
29 | public static IRListNode Empty = new IRListNode(new List());
30 |
31 | public IRListNode(IReadOnlyList children)
32 | {
33 | this._children = children ?? throw new ArgumentNullException(nameof(children));
34 | }
35 |
36 | public override int ChildCount => _children.Count;
37 | public override IRNode GetChild(int index) => GetTypedChild(index);
38 | public T GetTypedChild(int index) => _children[index];
39 |
40 | public override string ToString()
41 | {
42 | return $"{PurgePrefixSuffix(typeof(T).Name)}[]";
43 |
44 | static string PurgePrefixSuffix(string name)
45 | {
46 | const string Prefix = "IR";
47 | if (name.StartsWith(Prefix))
48 | {
49 | name = name.Substring(Prefix.Length);
50 | }
51 |
52 | const string Suffix = "Node";
53 | if (name.EndsWith(Suffix))
54 | {
55 | name = name.Substring(0, name.Length - Suffix.Length);
56 | }
57 |
58 | return name;
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Other/IRFunctionBodyNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRFunctionBodyNode : IRNode
10 | {
11 | public IRFunctionBodyNode(IRListNode statements, IRExpressionNode expression)
12 | {
13 | Statements = statements ?? throw new ArgumentNullException(nameof(statements));
14 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
15 | }
16 |
17 | public IRListNode Statements { get; }
18 | public IRExpressionNode Expression { get; }
19 |
20 | public override int ChildCount => 2;
21 | public override IRNode GetChild(int index) =>
22 | index switch
23 | {
24 | 0 => Statements,
25 | 1 => Expression,
26 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
27 | };
28 |
29 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
30 | where TResult : class
31 | {
32 | return visitor.VisitFunctionBody(this, context);
33 | }
34 |
35 | public override string ToString()
36 | {
37 | return $"FunctionBody: {SchemaDisplay.GetText(Expression.ResultType)}";
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Other/IRQueryBlockNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal class IRQueryBlockNode : IRNode
9 | {
10 | public IRQueryBlockNode(IRListNode statements)
11 | {
12 | Statements = statements ?? throw new ArgumentNullException(nameof(statements));
13 | }
14 |
15 | public IRListNode Statements { get; }
16 |
17 | public override int ChildCount => 1;
18 | public override IRNode GetChild(int index) =>
19 | index switch
20 | {
21 | 0 => Statements,
22 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
23 | };
24 |
25 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
26 | where TResult : class
27 | {
28 | return visitor.VisitQueryBlock(this, context);
29 | }
30 |
31 | public override string ToString()
32 | {
33 | return "QueryBlock";
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Statements/IRExpressionStatementNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal class IRExpressionStatementNode : IRStatementNode
9 | {
10 | public IRExpressionStatementNode(IRNode expression)
11 | {
12 | Expression = expression ?? throw new ArgumentNullException(nameof(expression));
13 | }
14 |
15 | public IRNode Expression { get; }
16 |
17 | public override int ChildCount => 1;
18 | public override IRNode GetChild(int index) =>
19 | index switch
20 | {
21 | 0 => Expression,
22 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
23 | };
24 |
25 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
26 | where TResult : class
27 | {
28 | return visitor.VisitExpressionStatement(this, context);
29 | }
30 |
31 | public override string ToString()
32 | {
33 | return "ExpressionStatement";
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Statements/IRLetStatementNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using Kusto.Language.Symbols;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal class IRLetStatementNode : IRStatementNode
10 | {
11 | public IRLetStatementNode(Symbol symbol, IRExpressionNode expression)
12 | {
13 | Symbol = symbol;
14 | Expression = expression;
15 | }
16 |
17 | public Symbol Symbol { get; }
18 | public IRExpressionNode Expression { get; }
19 |
20 | public override int ChildCount => 1;
21 | public override IRNode GetChild(int index) =>
22 | index switch
23 | {
24 | 0 => Expression,
25 | _ => throw new ArgumentOutOfRangeException(nameof(index)),
26 | };
27 |
28 | public override TResult? Accept(IRNodeVisitor visitor, TContext context)
29 | where TResult : class
30 | {
31 | return visitor.VisitLetStatement(this, context);
32 | }
33 |
34 | public override string ToString()
35 | {
36 | return $"LetStatement {{{Symbol.Name} = ...}}";
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Nodes/Statements/IRStatementNode.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace BabyKusto.Core.InternalRepresentation
5 | {
6 | internal abstract class IRStatementNode : IRNode
7 | {
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Count.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using BabyKusto.Core.Evaluation.BuiltIns;
6 | using Kusto.Language;
7 | using Kusto.Language.Symbols;
8 | using Kusto.Language.Syntax;
9 |
10 | namespace BabyKusto.Core.InternalRepresentation
11 | {
12 | internal partial class IRTranslator
13 | {
14 | public override IRNode VisitCountOperator(CountOperator node)
15 | {
16 | // `T | count` is equivalent to `T | summarize count()` (except for the output column name)
17 | var aggregations = new List
18 | {
19 | new IRAggregateCallNode(
20 | Aggregates.Count.Signatures[0],
21 | BuiltInAggregates.GetOverload(Aggregates.Count, new IRExpressionNode[0], new List()),
22 | new List(),
23 | IRListNode.Empty,
24 | ScalarTypes.Long),
25 | };
26 | return new IRSummarizeOperatorNode(aggregations: IRListNode.From(aggregations), byColumns: IRListNode.Empty, node.ResultType);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Distinct.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using Kusto.Language.Symbols;
8 | using Kusto.Language.Syntax;
9 |
10 | namespace BabyKusto.Core.InternalRepresentation
11 | {
12 | internal partial class IRTranslator
13 | {
14 | public override IRNode VisitDistinctOperator(DistinctOperator node)
15 | {
16 | var irExpressions = new List();
17 | if (node.Expressions.Any(e => e.Element is StarExpression))
18 | {
19 | if (node.Expressions.Count != 1)
20 | {
21 | throw new InvalidOperationException($"Expected 'distinct' operator with at most one '*' expression.");
22 | }
23 |
24 | var table = (TableSymbol)node.ResultType;
25 | for (int i = 0; i < table.Columns.Count; i++)
26 | {
27 | var column = table.Columns[i];
28 | irExpressions.Add(new IRRowScopeNameReferenceNode(column, column.Type, i));
29 | }
30 | }
31 | else
32 | {
33 | foreach (var expression in node.Expressions)
34 | {
35 | irExpressions.Add((IRExpressionNode)expression.Element.Accept(this));
36 | }
37 | }
38 |
39 | return new IRSummarizeOperatorNode(aggregations: IRListNode.Empty, byColumns: IRListNode.From(irExpressions), node.ResultType);
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Filter.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Syntax;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal partial class IRTranslator
9 | {
10 | public override IRNode VisitFilterOperator(FilterOperator node)
11 | {
12 | var irCondition = (IRExpressionNode)node.Condition.Accept(this);
13 | return new IRFilterOperatorNode(irCondition, node.ResultType);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.FunctionBody.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using Kusto.Language.Syntax;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal partial class IRTranslator
10 | {
11 | public override IRNode VisitFunctionBody(FunctionBody node)
12 | {
13 | var statements = new List();
14 | foreach (var statement in node.Statements)
15 | {
16 | var irStatement = (IRStatementNode)statement.Element.Accept(this);
17 | statements.Add(irStatement);
18 | }
19 |
20 | var irExpression = (IRExpressionNode)node.Expression.Accept(this);
21 |
22 | return new IRFunctionBodyNode(IRListNode.From(statements), irExpression);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Print.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using Kusto.Language.Syntax;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal partial class IRTranslator
10 | {
11 | public override IRNode VisitPrintOperator(PrintOperator node)
12 | {
13 | var irExpressions = new List();
14 | foreach (var expression in node.Expressions)
15 | {
16 | var irExpression = (IRExpressionNode)expression.Element.Accept(this);
17 | irExpressions.Add(irExpression);
18 | }
19 |
20 | return new IRPrintOperatorNode(IRListNode.From(irExpressions), node.ResultType);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Render.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using Kusto.Language.Syntax;
7 | using Microsoft.Extensions.Internal;
8 |
9 | namespace BabyKusto.Core.InternalRepresentation
10 | {
11 | internal partial class IRTranslator
12 | {
13 | public override IRNode VisitRenderOperator(RenderOperator node)
14 | {
15 | string chartType = node.ChartType.Text;
16 | string? kind = null;
17 |
18 | if (node.WithClause != null)
19 | {
20 | foreach (var prop in node.WithClause.Properties)
21 | {
22 | var element = prop.Element;
23 | if (element.Name.SimpleName == "kind")
24 | {
25 | var literalExpression = element.Expression as LiteralExpression;
26 | if (literalExpression == null)
27 | {
28 | throw new InvalidOperationException($"Expected render operator with-clause property expression to be {TypeNameHelper.GetTypeDisplayName(typeof(LiteralExpression))}, but found {TypeNameHelper.GetTypeDisplayName(element.Expression)}");
29 | }
30 |
31 | kind = (string?)literalExpression.LiteralValue;
32 | }
33 | }
34 | }
35 |
36 | return new IRRenderOperatorNode(chartType: chartType, kind: kind, node.ResultType);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Statements.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Syntax;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal partial class IRTranslator : DefaultSyntaxVisitor
9 | {
10 | public override IRNode VisitLetStatement(LetStatement node)
11 | {
12 | var expression = (IRExpressionNode)node.Expression.Accept(this);
13 | return new IRLetStatementNode(node.Name.ReferencedSymbol, expression);
14 | }
15 |
16 | public override IRNode VisitFunctionDeclaration(FunctionDeclaration node)
17 | {
18 | return new IRFunctionDeclarationNode(node.ResultType);
19 | }
20 |
21 | public override IRNode VisitExpressionStatement(ExpressionStatement node)
22 | {
23 | var irExpression = node.Expression.Accept(this);
24 | return new IRExpressionStatementNode(irExpression);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Summarize.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using Kusto.Language.Syntax;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal partial class IRTranslator
10 | {
11 | public override IRNode VisitSummarizeOperator(SummarizeOperator node)
12 | {
13 | List byColumns = new();
14 | if (node.ByClause != null)
15 | {
16 | int byExpressionsCount = node.ByClause.Expressions.Count;
17 | for (int i = 0; i < byExpressionsCount; i++)
18 | {
19 | var expression = node.ByClause.Expressions[i].Element;
20 | var irExpression = (IRExpressionNode)expression.Accept(this);
21 | byColumns.Add(irExpression);
22 | }
23 | }
24 |
25 | List aggregations = new();
26 | for (int i = 0; i < node.Aggregates.Count; i++)
27 | {
28 | var expression = node.Aggregates[i].Element;
29 | var irExpression = (IRExpressionNode)expression.Accept(this);
30 | aggregations.Add(irExpression);
31 | }
32 |
33 | return new IRSummarizeOperatorNode(IRListNode.From(aggregations), IRListNode.From(byColumns), node.ResultType);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Take.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Syntax;
5 |
6 | namespace BabyKusto.Core.InternalRepresentation
7 | {
8 | internal partial class IRTranslator
9 | {
10 | public override IRNode VisitTakeOperator(TakeOperator node)
11 | {
12 | var irExpression = (IRExpressionNode)node.Expression.Accept(this);
13 | return new IRTakeOperatorNode(irExpression, node.ResultType);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.Union.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using Kusto.Language.Syntax;
6 |
7 | namespace BabyKusto.Core.InternalRepresentation
8 | {
9 | internal partial class IRTranslator
10 | {
11 | public override IRNode VisitUnionOperator(UnionOperator node)
12 | {
13 | var expressions = new List();
14 | foreach (var expression in node.Expressions)
15 | {
16 | var irExpression = (IRExpressionNode)expression.Element.Accept(this);
17 | expressions.Add(irExpression);
18 | }
19 |
20 | return new IRUnionOperatorNode(IRListNode.From(expressions), node.ResultType);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/InternalRepresentation/Translation/IRTranslator.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using Kusto.Language.Symbols;
7 | using Kusto.Language.Syntax;
8 | using Microsoft.Extensions.Internal;
9 |
10 | namespace BabyKusto.Core.InternalRepresentation
11 | {
12 | internal partial class IRTranslator : DefaultSyntaxVisitor
13 | {
14 | private readonly Dictionary _inScopeSymbolInfos = new();
15 | private TableSymbol? _rowScope;
16 |
17 | internal IRTranslator()
18 | {
19 | }
20 |
21 | private void SetInScopeSymbolInfo(string name, EvaluatedExpressionKind resultKind)
22 | {
23 | _inScopeSymbolInfos.Add(name, resultKind);
24 | }
25 |
26 | protected override IRNode DefaultVisit(SyntaxNode node)
27 | {
28 | throw new NotImplementedException(TypeNameHelper.GetTypeDisplayName(node));
29 | }
30 |
31 | public override IRNode VisitQueryBlock(QueryBlock node)
32 | {
33 | var irStatements = new List();
34 | foreach (var se in node.Statements)
35 | {
36 | var statement = se.Element;
37 | var irStatement = (IRStatementNode)statement.Accept(this);
38 | irStatements.Add(irStatement);
39 | }
40 |
41 | var listNode = IRListNode.From(irStatements);
42 | return new IRQueryBlockNode(listNode);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Util/EvaluationResultExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.IO;
6 | using BabyKusto.Core.Evaluation;
7 | using Kusto.Language.Symbols;
8 | using Microsoft.Extensions.Internal;
9 |
10 | namespace BabyKusto.Core.Extensions
11 | {
12 | public static class EvaluationResultExtensions
13 | {
14 | public static void Dump(this EvaluationResult? result, TextWriter writer, int indent = 0)
15 | {
16 | switch (result)
17 | {
18 | case TabularResult tabularResult:
19 | tabularResult.Value.Dump(writer, indent);
20 | break;
21 | case ScalarResult scalarResult:
22 | if (indent > 0)
23 | {
24 | writer.Write(new string(' ', indent));
25 | }
26 | writer.Write(scalarResult.Value);
27 | writer.Write(" (");
28 | writer.Write(SchemaDisplay.GetText(scalarResult.Type));
29 | writer.WriteLine(")");
30 | break;
31 | default:
32 | throw new NotSupportedException($"Unsupported evaluation result type cannot be dumped: {TypeNameHelper.GetTypeDisplayName(result)}");
33 | }
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Util/IsExternalInit.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | namespace System.Runtime.CompilerServices
5 | {
6 | public static class IsExternalInit
7 | {
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Util/PolyfillExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | #if !NETSTANDARD2_1_OR_GREATER
5 | namespace System.Collections.Generic
6 | {
7 | internal static class PolyfillDictionaryExtensions
8 | {
9 | internal static bool TryAdd(this Dictionary dictionary, TKey key, TValue value)
10 | {
11 | try
12 | {
13 | dictionary.Add(key, value);
14 | return true;
15 | }
16 | catch (ArgumentException ex) when (ex is not ArgumentNullException)
17 | {
18 | return false;
19 | }
20 | }
21 | }
22 | }
23 | #endif
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/Util/TypeSymbolExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using Kusto.Language.Symbols;
5 |
6 | namespace BabyKusto.Core.Extensions
7 | {
8 | internal static class TypeSymbolExtensions
9 | {
10 | #if NETSTANDARD2_1_OR_GREATER
11 | [return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("type")]
12 | #endif
13 | internal static TypeSymbol? Simplify(this TypeSymbol? type)
14 | {
15 | return
16 | type == null ? null :
17 | type.Name == ScalarTypes.Dynamic.Name ? ScalarTypes.Dynamic :
18 | type;
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/opensource/unmodified/dotnet-runtime/LICENSE.TXT:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) .NET Foundation and Contributors
4 |
5 | All rights reserved.
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in all
15 | copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | SOFTWARE.
24 |
--------------------------------------------------------------------------------
/src/BabyKusto.Core/opensource/unmodified/dotnet-runtime/OPENSOURCE.TXT:
--------------------------------------------------------------------------------
1 | Taken from:
2 | Repo: https://github.com/dotnet/runtime
3 | File path: /src/libraries/Common/src/Extensions/TypeNameHelper/TypeNameHelper.cs
4 | Commit ID: 10d8964fb00f8a24586f562d83feac9dbb0cafac
5 | Permalink: https://github.com/dotnet/runtime/blob/10d8964fb00f8a24586f562d83feac9dbb0cafac/src/libraries/Common/src/Extensions/TypeNameHelper/TypeNameHelper.cs
6 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/BabyKusto.Server.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 |
7 |
8 |
9 | 0.1.8-preview
10 | kusto;kql;aspnetcore
11 |
12 | Host your own self-contained local Kusto server, powered by BabyKusto and ASP .NET Core.
13 |
14 | https://github.com/davidnx/baby-kusto-csharp
15 | MIT
16 | README-NUGET.md
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/ConfigurationExtensions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using BabyKusto.Server.Service;
5 | using Microsoft.Extensions.DependencyInjection;
6 |
7 | namespace BabyKusto.Server
8 | {
9 | public static class ConfigurationExtensions
10 | {
11 | public static IServiceCollection AddBabyKustoServer(this IServiceCollection services)
12 | {
13 | services.AddSingleton();
14 | services.AddSingleton();
15 | services.AddSingleton();
16 | services.AddSingleton();
17 |
18 | return services;
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Contract/KustoApiMgmtRequestBody.cs:
--------------------------------------------------------------------------------
1 | using System.Text.Json.Nodes;
2 | using System.Text.Json.Serialization;
3 |
4 | namespace BabyKusto.Server.Contract
5 | {
6 | public class KustoApiMgmtRequestBody
7 | {
8 | [JsonPropertyName("csl")]
9 | public string? Csl { get; set; }
10 |
11 | [JsonPropertyName("db")]
12 | public string? DB { get; set; }
13 |
14 | [JsonPropertyName("options")]
15 | public JsonObject? Options { get; set; }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Contract/KustoApiQueryRequestBody.cs:
--------------------------------------------------------------------------------
1 | using System.Text.Json.Nodes;
2 | using System.Text.Json.Serialization;
3 |
4 | namespace BabyKusto.Server.Contract
5 | {
6 | public class KustoApiQueryRequestBody
7 | {
8 | [JsonPropertyName("csl")]
9 | public string? Csl { get; set; }
10 |
11 | [JsonPropertyName("db")]
12 | public string? DB { get; set; }
13 |
14 | [JsonPropertyName("options")]
15 | public JsonObject? Options { get; set; }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Contract/KustoApiResult.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Text.Json.Serialization;
4 |
5 | namespace BabyKusto.Server.Contract
6 | {
7 | public class KustoApiResult
8 | {
9 | [JsonPropertyName("Tables")]
10 | public List Tables { get; } = new();
11 |
12 | public override string ToString()
13 | {
14 | return $"{Tables.Count} table{(Tables.Count == 1 ? "" : "s")}: {string.Join(", ", Tables.Select(t => t.TableName))}";
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Contract/KustoApiTableResult.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Text.Json.Nodes;
4 | using System.Text.Json.Serialization;
5 |
6 | namespace BabyKusto.Server.Contract
7 | {
8 | public class KustoApiTableResult
9 | {
10 | [JsonPropertyName("TableName")]
11 | public string? TableName { get; set; }
12 |
13 | [JsonPropertyName("Columns")]
14 | public List Columns { get; } = new();
15 |
16 | [JsonPropertyName("Rows")]
17 | public List Rows { get; } = new();
18 |
19 | public override string ToString()
20 | {
21 | return $"{TableName}: ({string.Join(", ", Columns.Select(c => $"{c.ColumnName}: {c.ColumnType}"))}), {Rows.Count} rows";
22 | }
23 | }
24 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Contract/KustoApiV2ColumnDescription.cs:
--------------------------------------------------------------------------------
1 | using System.Text.Json.Serialization;
2 |
3 | namespace BabyKusto.Server.Contract
4 | {
5 | public class KustoApiV2ColumnDescription
6 | {
7 | [JsonPropertyName("ColumnName")]
8 | public string? ColumnName { get; set; }
9 |
10 | [JsonPropertyName("ColumnType")]
11 | public string? ColumnType { get; set; }
12 |
13 | public override string ToString()
14 | {
15 | return $"{ColumnName}: {ColumnType}";
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/README-NUGET.md:
--------------------------------------------------------------------------------
1 | # BabyKusto.Server
2 |
3 | An implementation of the Kusto REST API protocol (v1 and v2). Use this library to create a server that Azure Data Explorer can connect to as if it was a real Kusto cluster.
4 |
5 |
6 | ## How to use
7 | Use the provided [**Sample.ProcessesServer**](https://github.com/davidnx/baby-kusto-csharp/tree/main/samples/Sample.ProcessesServer) as inspiration.
8 | This sample is an ASP .NET Core-based web server that implements the Kusto REST API and exposes a table `Processes` that shows the list of running processes on your environment. You can connect to the local Kusto cluster using the official Kusto client (Azure Data Explorer).
9 |
10 | ## Contributing
11 |
12 | This project welcomes contributions and suggestions. Most contributions require you to agree to a
13 | Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
14 | the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
15 |
16 | When you submit a pull request, a CLA bot will automatically determine whether you need to provide
17 | a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
18 | provided by the bot. You will only need to do this once across all repos using our CLA.
19 |
20 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
21 | For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
22 | contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
23 |
24 | ## Trademarks
25 |
26 | This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
27 | trademarks or logos is subject to and must follow
28 | [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
29 | Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
30 | Any use of third-party trademarks or logos are subject to those third-party's policies.
31 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Service/BabyKustoServerOptions.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 |
6 | namespace BabyKusto.Server.Service
7 | {
8 | public class BabyKustoServerOptions
9 | {
10 | public string DatabaseName { get; set; } = "BabyKusto";
11 | public string DatabaseId { get; set; } = Guid.NewGuid().ToString();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Service/BabyKustoServerState.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using BabyKusto.Core;
8 | using Kusto.Language;
9 | using Kusto.Language.Symbols;
10 | using Microsoft.Extensions.Options;
11 |
12 | namespace BabyKusto.Server.Service
13 | {
14 | internal class BabyKustoServerState : IBabyKustoServerState
15 | {
16 | public BabyKustoServerState(IOptions options, ITablesProvider tablesProvider)
17 | {
18 | _ = tablesProvider ?? throw new ArgumentNullException(nameof(tablesProvider));
19 |
20 | Options = options?.Value ?? throw new ArgumentNullException(nameof(options));
21 | Tables = tablesProvider.GetTables();
22 | Globals = GlobalState.Default.WithDatabase(
23 | new DatabaseSymbol(Options.DatabaseName, Tables.Select(t => t.Type).ToArray()));
24 |
25 | var engine = new BabyKustoEngine();
26 | foreach (var table in Tables)
27 | {
28 | engine.AddGlobalTable(table);
29 | }
30 | Engine = engine;
31 | }
32 |
33 | public BabyKustoServerOptions Options { get; }
34 | public List Tables { get; }
35 | public GlobalState Globals { get; }
36 | public BabyKustoEngine Engine { get; }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Service/IBabyKustoServerState.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using BabyKusto.Core;
6 | using Kusto.Language;
7 |
8 | namespace BabyKusto.Server.Service
9 | {
10 | public interface IBabyKustoServerState
11 | {
12 | GlobalState Globals { get; }
13 | BabyKustoServerOptions Options { get; }
14 | List Tables { get; }
15 |
16 | BabyKustoEngine Engine { get; }
17 | }
18 | }
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Service/ITablesProvider.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System.Collections.Generic;
5 | using BabyKusto.Core;
6 |
7 | namespace BabyKusto.Server.Service
8 | {
9 | public interface ITablesProvider
10 | {
11 | List GetTables();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/BabyKusto.Server/Service/KustoQueryV2ResponseTableKind.cs:
--------------------------------------------------------------------------------
1 | namespace BabyKusto.Server.Service
2 | {
3 | public enum KustoQueryV2ResponseTableKind
4 | {
5 | QueryCompletionInformation,
6 | QueryProperties,
7 | PrimaryResult,
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/test/BabyKusto.Core.Tests/BabyKusto.Core.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 |
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 | all
17 |
18 |
19 | runtime; build; native; contentfiles; analyzers; buildtransitive
20 | all
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/test/BabyKusto.Core.Tests/NegativeTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using System;
5 | using System.Diagnostics;
6 | using BabyKusto.Core;
7 | using BabyKusto.Core.Evaluation;
8 | using BabyKusto.Core.Extensions;
9 | using FluentAssertions;
10 | using Xunit;
11 |
12 | namespace KustoExecutionEngine.Core.Tests
13 | {
14 | public class NegativeTests
15 | {
16 | [Fact(Skip = "Not supported yet")]
17 | public void A()
18 | {
19 | // Per docs this should fail, but seems to be accepted.
20 | // The engine fails to process this at this time, but not for the expected reasons...
21 | // See: https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/functions/user-defined-functions
22 | // > Not supported:
23 | // > f is a scalar function that references the tabular expression Table1,
24 | // > and is invoked with a reference to the current row context f(Column):
25 |
26 | // Arrange
27 | string query = @"
28 | let Table1 = datatable(xdate:datetime)[datetime(1970-01-01)];
29 | let Table2 = datatable(Column:long)[1235];
30 | let f = (hours:long) { toscalar(Table1 | summarize min(xdate) - hours*1h) };
31 | Table2 | where Column != 123 | project d = f(Column)
32 | ";
33 |
34 | // Act & Assert
35 | Test(query, String.Empty);
36 | }
37 |
38 | private static void Test(string query, string expectedOutput)
39 | {
40 | var engine = new BabyKustoEngine();
41 | var result = (TabularResult?)engine.Evaluate(query);
42 | Debug.Assert(result != null);
43 | var stringified = result.Value.DumpToString();
44 |
45 | var canonicalOutput = stringified.Trim().Replace("\r\n", "\n");
46 | var canonicalExpectedOutput = expectedOutput.Trim().Replace("\r\n", "\n");
47 |
48 | canonicalOutput.Should().Be(canonicalExpectedOutput);
49 | }
50 | }
51 | }
--------------------------------------------------------------------------------
/test/BabyKusto.Core.Tests/Util/TypeSymbolExtensionsTests.cs:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft Corporation.
2 | // Licensed under the MIT License.
3 |
4 | using FluentAssertions;
5 | using Kusto.Language.Symbols;
6 | using Xunit;
7 |
8 | namespace BabyKusto.Core.Extensions.Tests
9 | {
10 | public class TypeSymbolExtensionsTests
11 | {
12 | public static object[][] NonDynamicTypes = new object[][]
13 | {
14 | new object[] { ScalarTypes.Bool },
15 | new object[] { ScalarTypes.Int },
16 | new object[] { ScalarTypes.Long },
17 | new object[] { ScalarTypes.Real },
18 | new object[] { ScalarTypes.String },
19 | new object[] { ScalarTypes.TimeSpan },
20 | new object[] { ScalarTypes.DateTime },
21 | };
22 |
23 | public static object[][] DynamicTypes = new object[][]
24 | {
25 | new object[] { ScalarTypes.Dynamic },
26 | new object[] { ScalarTypes.DynamicBag },
27 | new object[] { ScalarTypes.DynamicArrayOfLong },
28 | new object[] { ScalarTypes.DynamicArrayOfReal },
29 | new object[] { ScalarTypes.DynamicArrayOfArray },
30 | new object[] { ScalarTypes.DynamicArrayOfString },
31 | new object[] { ScalarTypes.GetDynamic(ScalarTypes.DateTime) },
32 | new object[] { ScalarTypes.GetDynamicArray(ScalarTypes.DateTime) },
33 | };
34 |
35 | [Theory]
36 | [MemberData(nameof(NonDynamicTypes))]
37 | public void NonDynamicTypes_Works(TypeSymbol type)
38 | {
39 | // Act
40 | var simplified = type.Simplify();
41 |
42 | // Assert
43 | simplified.Should().BeSameAs(type);
44 | }
45 |
46 | [Theory]
47 | [MemberData(nameof(DynamicTypes))]
48 | public void DynamicTypes_Works(TypeSymbol type)
49 | {
50 | // Act
51 | var simplified = type.Simplify();
52 |
53 | // Assert
54 | simplified.Should().BeSameAs(ScalarTypes.Dynamic);
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/test/BabyKusto.Server.Tests/BabyKusto.Server.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 | enable
6 |
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | runtime; build; native; contentfiles; analyzers; buildtransitive
16 | all
17 |
18 |
19 | runtime; build; native; contentfiles; analyzers; buildtransitive
20 | all
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------