Skip to content

Add substrait tests for comparison and boolean functions #629

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/FlowtideDotNet.Core/ColumnStore/DataValues/BoolValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,10 @@ public void CopyToContainer(DataValueContainer container)
container._type = ArrowTypeId.Boolean;
container._boolValue = this;
}

public override string ToString()
{
return value ? "true" : "false";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ public static void AddComparisonFunctions(IFunctionsRegister functionsRegister)
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.Equal, typeof(BuiltInComparisonFunctions), nameof(EqualImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.NotEqual, typeof(BuiltInComparisonFunctions), nameof(NotEqualImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.GreaterThan, typeof(BuiltInComparisonFunctions), nameof(GreaterThanImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.GreaterThanOrEqual, typeof(BuiltInComparisonFunctions), nameof(GreaterThanOrEqualImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.LessThan, typeof(BuiltInComparisonFunctions), nameof(LessThanImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.LessThanOrEqual, typeof(BuiltInComparisonFunctions), nameof(LessThanOrEqualImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.IsNull, typeof(BuiltInComparisonFunctions), nameof(IsNullImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.IsNotNull, typeof(BuiltInComparisonFunctions), nameof(IsNotNullImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.Between, typeof(BuiltInComparisonFunctions), nameof(BetweenImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.IsFinite, typeof(BuiltInComparisonFunctions), nameof(IsFiniteImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.isInfinite, typeof(BuiltInComparisonFunctions), nameof(IsInfiniteImplementation));
functionsRegister.RegisterScalarMethod(FunctionsComparison.Uri, FunctionsComparison.IsNan, typeof(BuiltInComparisonFunctions), nameof(IsNanImplementation));

functionsRegister.RegisterColumnScalarFunction(FunctionsComparison.Uri, FunctionsComparison.Coalesce,
(scalarFunction, parametersInfo, visitor) =>
Expand Down Expand Up @@ -143,6 +150,78 @@ private static IDataValue GreaterThanImplementation<T1, T2>(in T1 x, in T2 y, in
}
}

private static IDataValue GreaterThanOrEqualImplementation<T1, T2>(in T1 x, in T2 y, in DataValueContainer result)
where T1 : IDataValue
where T2 : IDataValue
{
// If either is null, return null
if (x.IsNull || y.IsNull)
{
result._type = ArrowTypeId.Null;
return result;
}
else if (DataValueComparer.CompareTo(x, y) >= 0)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
}

private static IDataValue LessThanImplementation<T1, T2>(in T1 x, in T2 y, in DataValueContainer result)
where T1 : IDataValue
where T2 : IDataValue
{
// If either is null, return null
if (x.IsNull || y.IsNull)
{
result._type = ArrowTypeId.Null;
return result;
}
else if (DataValueComparer.CompareTo(x, y) < 0)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
}

private static IDataValue LessThanOrEqualImplementation<T1, T2>(in T1 x, in T2 y, in DataValueContainer result)
where T1 : IDataValue
where T2 : IDataValue
{
// If either is null, return null
if (x.IsNull || y.IsNull)
{
result._type = ArrowTypeId.Null;
return result;
}
else if (DataValueComparer.CompareTo(x, y) <= 0)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
}

private static IDataValue IsNullImplementation<T>(in T x, in DataValueContainer result)
where T : IDataValue
{
Expand All @@ -151,11 +230,24 @@ private static IDataValue IsNullImplementation<T>(in T x, in DataValueContainer
return result;
}

private static IDataValue IsNotNullImplementation<T>(in T x, in DataValueContainer result)
where T : IDataValue
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(!x.IsNull);
return result;
}

private static IDataValue BetweenImplementation<T1, T2, T3>(in T1 expr, in T2 low, in T3 high, in DataValueContainer result)
where T1 : IDataValue
where T2 : IDataValue
where T3 : IDataValue
{
if (expr.IsNull || low.IsNull || high.IsNull)
{
result._type = ArrowTypeId.Null;
return result;
}

if (DataValueComparer.CompareTo(expr, low) >= 0 && DataValueComparer.CompareTo(expr, high) <= 0)
{
Expand All @@ -170,5 +262,113 @@ private static IDataValue BetweenImplementation<T1, T2, T3>(in T1 expr, in T2 lo
return result;
}
}

private static IDataValue IsFiniteImplementation<T>(in T x, in DataValueContainer result)
where T : IDataValue
{
if (x.Type == ArrowTypeId.Double)
{
var val = x.AsDouble;
if (val == double.PositiveInfinity || val == double.NegativeInfinity || double.IsNaN(val))
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
}
else if (x.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else if (x.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}

result._type = ArrowTypeId.Null;
return result;
}

private static IDataValue IsInfiniteImplementation<T>(in T x, in DataValueContainer result)
where T : IDataValue
{
if (x.Type == ArrowTypeId.Double)
{
var val = x.AsDouble;
if (val == double.PositiveInfinity || val == double.NegativeInfinity)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
}
else if (x.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
else if (x.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}

result._type = ArrowTypeId.Null;
return result;
}

private static IDataValue IsNanImplementation<T>(in T x, in DataValueContainer result)
where T : IDataValue
{
if (x.Type == ArrowTypeId.Double)
{
var val = x.AsDouble;
if (double.IsNaN(val))
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(true);
return result;
}
else
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
}
else if (x.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}
else if (x.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Boolean;
result._boolValue = new BoolValue(false);
return result;
}

result._type = ArrowTypeId.Null;
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public async Task IsInfiniteStringFalse()
GenerateData();
await StartStream("INSERT INTO output SELECT is_infinite(firstName) FROM users");
await WaitForUpdate();
AssertCurrentDataEqual(Users.Select(x => new { val = false }));
AssertCurrentDataEqual(Users.Select(x => new { val = default(string) }));
}

[Fact]
Expand Down Expand Up @@ -140,7 +140,7 @@ INSERT INTO output
is_nan(0/0), is_nan(1/2), is_nan(userkey), is_nan(nullablestring)
FROM users u");
await WaitForUpdate();
AssertCurrentDataEqual(Users.Select(x => new { nan = true, not_nan = false, userkey = false, nullString = x.NullableString == null ? default(bool?) : true}));
AssertCurrentDataEqual(Users.Select(x => new { nan = true, not_nan = false, userkey = false, nullString = default(string)}));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion tests/FlowtideDotNet.AcceptanceTests/TypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ INSERT INTO output
SELECT
Money
FROM orders
WHERE money < 500
WHERE money < cast(500 as decimal)
");
await WaitForUpdate();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FlowtideDotNet.ComputeTests.SourceGenerator.Internal
{
class ErrorStrategy : DefaultErrorStrategy
{
public override void ReportError(Parser recognizer, RecognitionException e)
{
NotifyErrorListeners(recognizer, e.Message, e);
//base.ReportError(recognizer, e);
}
}
class ErrorReporter : IAntlrErrorListener<IToken>
{
private readonly string path;
private readonly SourceProductionContext sourceContext;

public bool ErrorReported { get; private set; }

public ErrorReporter(string path, SourceProductionContext sourceContext)
{
this.path = path;
this.sourceContext = sourceContext;
}

public void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
{
ErrorReported = true;
sourceContext.ReportDiagnostic(Diagnostic.Create(
"TESTGEN001",
"Test",
new TestLocalizableString(msg),
DiagnosticSeverity.Error,
DiagnosticSeverity.Error,
true,
0,
location: Location.Create(path, new Microsoft.CodeAnalysis.Text.TextSpan(), new Microsoft.CodeAnalysis.Text.LinePositionSpan(new LinePosition(line - 1, charPositionInLine), new LinePosition(line - 1, charPositionInLine)))
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FlowtideDotNet.ComputeTests
{
internal class TestCaseParser
{
public TestDocument Parse(string text)
public TestDocument Parse(string text, IAntlrErrorListener<IToken> errorListener = default)
{
ICharStream stream = CharStreams.fromString(text);
var lexer = new FuncTestCaseLexer(stream);
Expand All @@ -26,6 +26,12 @@ public TestDocument Parse(string text)
{
BuildParseTree = true
};
parser.RemoveErrorListeners();
if (errorListener != null)
{
parser.AddErrorListener(errorListener);
}


var context = parser.doc();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Microsoft.CodeAnalysis;
using System;
using System.Collections.Generic;
using System.Text;

namespace FlowtideDotNet.ComputeTests.SourceGenerator.Internal
{
internal class TestLocalizableString : LocalizableString
{
private readonly string msg;

public TestLocalizableString(string msg)
{
this.msg = msg;
}
protected override bool AreEqual(object other)
{
return other is TestLocalizableString testLocalizableString && testLocalizableString.msg == msg;
}

protected override int GetHash()
{
return msg.GetHashCode();
}

protected override string GetText(IFormatProvider formatProvider)
{
return msg;
}
}
}
Loading