Skip to content

Add column based rounding functions #630

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 1 commit 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 FlowtideDotNet.Core.ColumnStore;
using FlowtideDotNet.Substrait.FunctionExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlowtideDotNet.Core.Compute.Columnar.Functions
{
internal static class BuiltInRoundingFunctions
{
public static void AddRoundingFunctions(IFunctionsRegister functionsRegister)
{
functionsRegister.RegisterScalarMethod(FunctionsRounding.Uri, FunctionsRounding.Ceil, typeof(BuiltInRoundingFunctions), nameof(CeilImplementation));
functionsRegister.RegisterScalarMethod(FunctionsRounding.Uri, FunctionsRounding.Floor, typeof(BuiltInRoundingFunctions), nameof(FloorImplementation));
functionsRegister.RegisterScalarMethod(FunctionsRounding.Uri, FunctionsRounding.Round, typeof(BuiltInRoundingFunctions), nameof(RoundImplementation));
}

private static IDataValue CeilImplementation<T>(T value, DataValueContainer result)
where T : IDataValue
{
if (value.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Int64;
result._int64Value = new Int64Value(value.AsLong);
return result;
}
else if (value.Type == ArrowTypeId.Double)
{
result._type = ArrowTypeId.Double;
result._doubleValue = new DoubleValue((long)Math.Ceiling(value.AsDouble));
return result;
}
else if (value.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Decimal128;
result._decimalValue = new DecimalValue(Math.Ceiling(value.AsDecimal));
return result;
}
result._type = ArrowTypeId.Null;
return result;
}

private static IDataValue FloorImplementation<T>(T value, DataValueContainer result)
where T : IDataValue
{
if (value.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Int64;
result._int64Value = new Int64Value(value.AsLong);
return result;
}
else if (value.Type == ArrowTypeId.Double)
{
result._type = ArrowTypeId.Double;
result._doubleValue = new DoubleValue((long)Math.Floor(value.AsDouble));
return result;
}
else if (value.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Decimal128;
result._decimalValue = new DecimalValue(Math.Floor(value.AsDecimal));
return result;
}
result._type = ArrowTypeId.Null;
return result;
}

private static IDataValue RoundImplementation<T>(T value, DataValueContainer result)
where T : IDataValue
{
if (value.Type == ArrowTypeId.Int64)
{
result._type = ArrowTypeId.Int64;
result._int64Value = new Int64Value(value.AsLong);
return result;
}
else if (value.Type == ArrowTypeId.Double)
{
result._type = ArrowTypeId.Double;
result._doubleValue = new DoubleValue((long)Math.Round(value.AsDouble));
return result;
}
else if (value.Type == ArrowTypeId.Decimal128)
{
result._type = ArrowTypeId.Decimal128;
result._decimalValue = new DecimalValue(Math.Round(value.AsDecimal));
return result;
}
result._type = ArrowTypeId.Null;
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void RegisterFunctions(FunctionsRegister functionsRegister)
Columnar.Functions.BuiltInStringFunctions.RegisterFunctions(functionsRegister);
Columnar.Functions.BuiltInBooleanFunctions.AddBooleanFunctions(functionsRegister);
Columnar.Functions.BuiltInDatetimeFunctions.AddBuiltInDatetimeFunctions(functionsRegister);
Columnar.Functions.BuiltInRoundingFunctions.AddRoundingFunctions(functionsRegister);

BuiltInComparisonFunctions.AddComparisonFunctions(functionsRegister);
BuiltInBooleanFunctions.AddBooleanFunctions(functionsRegister);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<None Remove="Substrait\cases\comparison\lt.test" />
<None Remove="Substrait\cases\comparison\lte.test" />
<None Remove="Substrait\cases\comparison\not_equal.test" />
<None Remove="Substrait\cases\rounding\floor.test" />
<None Remove="Substrait\cases\rounding\round.test" />
<None Remove="Substrait\cases\string\char_length.test" />
<None Remove="Substrait\cases\string\like.test" />
<None Remove="Substrait\cases\string\lower.test" />
Expand Down Expand Up @@ -62,6 +64,7 @@
<AdditionalFiles Include="Substrait/cases/string/*.test" />
<AdditionalFiles Include="Substrait/cases/boolean/*.test" />
<AdditionalFiles Include="Substrait/cases/comparison/*.test" />
<AdditionalFiles Include="Substrait/cases/rounding/*.test" />
<AdditionalFiles Include="OtherCases\comparison\*.test" />
<AdditionalFiles Include="Substrait/cases/aggregate_generic/count.test" />
<AdditionalFiles Include="Substrait/cases/arithmetic/max.test" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### SUBSTRAIT_SCALAR_TEST: v1.0
### SUBSTRAIT_INCLUDE: '/extensions/functions_rounding.yaml'

# basic: Basic examples without any special cases
ceil(2.25::fp32) = 3::fp32
ceil(2.0000007152557373046875::fp64) = 3::fp64
ceil(-65.500000001223334444::fp64) = -65::fp64
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### SUBSTRAIT_SCALAR_TEST: v1.0
### SUBSTRAIT_INCLUDE: '/extensions/functions_rounding.yaml'

# basic: Basic examples without any special cases
floor(2.25::fp32) = 2::fp32
floor(2.0000007152557373046875::fp64) = 2::fp64
floor(-65.490000001223334444::fp64) = -66::fp64
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### SUBSTRAIT_SCALAR_TEST: v1.0
### SUBSTRAIT_INCLUDE: '/extensions/functions_rounding.yaml'

# basic: Basic examples without any special cases
floor(2.25::fp32) = 2::fp32
floor(2.0000007152557373046875::fp64) = 2::fp64
floor(-65.490000001223334444::fp64) = -66::fp64