Skip to content

Commit

Permalink
Merge pull request #100 from NeilMacMullen/moreFuncs
Browse files Browse the repository at this point in the history
Add isFinite etc
  • Loading branch information
NeilMacMullen authored Jan 18, 2025
2 parents 99d64d2 + 62997f8 commit 9f3b551
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ static BuiltInScalarFunctions()

IsEmptyFunction.Register(functions);

IsNotEmptyFunction.Register(functions);
IsAsciiFunction.Register(functions);
IsFiniteFunction.Register(functions);
IsInfFunction.Register(functions);
IsNanFunction.Register(functions);
IsUtf8Function.Register(functions);
ReverseFunction.Register(functions);
functions.Add(
Functions.MinOf,
new ScalarFunctionInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ internal partial class DivideFunction
: a / b;

private static double? DoubleImpl(double a, double b)
=> b == 0
? null
: a / b;
=> a / b;

private static TimeSpan? TsLongImpl(TimeSpan a, long b)
=> b == 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Linq;
using System.Text;
using System.Text.Unicode;

namespace KustoLoco.Core.Evaluation.BuiltIns.Impl;
// ReSharper disable PartialTypeWithSinglePart
[KustoImplementation(Keyword = "Functions.IsAscii")]
internal partial class IsAsciiFunction
{
internal static bool Impl(string s) => Ascii.IsValid(s);
}


// ReSharper disable PartialTypeWithSinglePart
[KustoImplementation(Keyword = "Functions.Reverse")]
internal partial class ReverseFunction
{
internal static string Impl(string s) => new string(s.Reverse().ToArray());
}

[KustoImplementation(Keyword = "Functions.IsUtf8")]
internal partial class IsUtf8Function
{
//it'a a little unclear how this could ever return false.
//In theory we can have some bytes that are not valid utf8,
//but how would the get turned into a string in the tables?
internal static bool Impl(string s) => true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
internal partial class IsEmptyFunction
{
internal static bool Impl(string s) => s.Length == 0;
}
}


// ReSharper disable PartialTypeWithSinglePart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace KustoLoco.Core.Evaluation.BuiltIns.Impl;

[KustoImplementation(Keyword = "Functions.IsFinite")]
internal partial class IsFiniteFunction
{
internal static bool Impl(double v) =>
double.IsFinite(v);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace KustoLoco.Core.Evaluation.BuiltIns.Impl;

[KustoImplementation(Keyword = "Functions.IsInf")]
internal partial class IsInfFunction
{
internal static bool Impl(double v) =>
double.IsInfinity(v);
}

[KustoImplementation(Keyword = "Functions.IsNan")]
internal partial class IsNanFunction
{
internal static bool Impl(double v) =>
double.IsNaN(v);
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace KustoLoco.Core.Evaluation.BuiltIns.Impl;

// ReSharper disable PartialTypeWithSinglePart
[KustoImplementation(Keyword = "Functions.IsNotEmpty")]
internal partial class IsNotEmptyFunction
{
internal static bool Impl(string s) => s is { Length: > 0 };
}
2 changes: 1 addition & 1 deletion libraries/lokql-engine/InteractiveTableExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public async Task RunNextBlock(BlockSequence blocks)
.Trim();

//support comments
if (query.StartsWith("#") || query.StartsWith("//") || query.IsBlank()) return;
if (query.StartsWith("#") || query.IsBlank()) return;

try
{
Expand Down
2 changes: 1 addition & 1 deletion libraries/lokql-engine/lokql-engine.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<RootNamespace>Lokql.Engine</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
74 changes: 74 additions & 0 deletions test/BasicTests/SimpleFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,78 @@ public async Task MultiStringCat()
result.Should().Be("123456789012345");
}

[TestMethod]
public async Task IsEmpty()
{
var query = "print isempty('')";
var result = await LastLineOfResult(query);
result.Should().Be("True");
}

[TestMethod]
public async Task IsEmpty2()
{
var query = "print isempty(' ')";
var result = await LastLineOfResult(query);
result.Should().Be("False");
}

[TestMethod]
public async Task IsNotEmpty()
{
var query = "print isnotempty('')";
var result = await LastLineOfResult(query);
result.Should().Be("False");
}

[TestMethod]
public async Task IsNotEmpty2()
{
var query = "print isnotempty(' ')";
var result = await LastLineOfResult(query);
result.Should().Be("True");
}


[TestMethod]
public async Task IsAscii()
{
var query = "print isascii('blahhh')";
var result = await LastLineOfResult(query);
result.Should().Be("True");
}

[TestMethod]
public async Task IsUtf8()
{
var query = "print isutf8('blahhh')";
var result = await LastLineOfResult(query);
result.Should().Be("True");
}

[TestMethod]
public async Task Reverse()
{
var query = "print reverse('acdef')";
var result = await LastLineOfResult(query);
result.Should().Be("fedca");
}

[TestMethod]
public async Task IsFinite()
{
var query = "print isfinite(1.0/10)";
var result = await LastLineOfResult(query);
result.Should().Be("True");
}

//[Ignore("TODO - isFinite needs work")]
[TestMethod]
public async Task IsFinite2()
{
var query = "print isfinite(1.0/0.0)";
var result = await LastLineOfResult(query);
result.Should().Be("False");
}

}

0 comments on commit 9f3b551

Please sign in to comment.