-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for functors that accept different types.
Slight refactoring.
- Loading branch information
Showing
6 changed files
with
255 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using MetadataManager; | ||
using PageManager; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace QueryProcessing | ||
{ | ||
static class FunctorArgChecks | ||
{ | ||
public static void CheckInputArguments(MetadataColumn[] sourceArguments, ColumnType[] acceptedColumnTypes) | ||
{ | ||
if (sourceArguments.Length != acceptedColumnTypes.Length) | ||
{ | ||
throw new ArgumentException("Invalid number of arguments"); | ||
} | ||
|
||
foreach (MetadataColumn md in sourceArguments) | ||
{ | ||
if (!acceptedColumnTypes.Any(acc => md.ColumnType.ColumnType == acc)) | ||
{ | ||
throw new ArgumentException($"Type {md.ColumnType.ColumnType} is not accepted by Add functor"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class AddFunctorOutputMappingHandler | ||
{ | ||
public static MetadataColumn GetMetadataInfoForOutput(Sql.columnSelect.Func func, MetadataColumn[] metadataColumns) | ||
{ | ||
Sql.scalarArgs args = func.Item.Item2; | ||
|
||
if (!args.IsArgs2) | ||
{ | ||
throw new ArgumentException("Add accepts two args"); | ||
} | ||
|
||
var args2 = ((Sql.scalarArgs.Args2)args).Item; | ||
Sql.value argOne = args2.Item1; | ||
Sql.value argTwo = args2.Item2; | ||
|
||
if (!argOne.IsId || !argTwo.IsId) | ||
{ | ||
throw new NotImplementedException("Currently we only support ids in as arguments"); | ||
} | ||
|
||
Sql.value.Id idOne = (Sql.value.Id)argOne; | ||
Sql.value.Id idTwo = (Sql.value.Id)argTwo; | ||
|
||
ColumnInfo argOneMd = QueryProcessingAccessors.GetMetadataColumn(idOne.Item, metadataColumns).ColumnType; | ||
ColumnInfo argTwoMd = QueryProcessingAccessors.GetMetadataColumn(idTwo.Item, metadataColumns).ColumnType; | ||
|
||
// both need to be double or int. | ||
// TODO: Need generic way to express this. | ||
if (!((argOneMd.ColumnType == ColumnType.Double || argOneMd.ColumnType == ColumnType.Int) && | ||
argTwoMd.ColumnType == ColumnType.Double || argTwoMd.ColumnType == ColumnType.Int)) | ||
{ | ||
throw new ArgumentException("Invalid argument type for add"); | ||
} | ||
|
||
if (argOneMd.ColumnType == ColumnType.Double || argTwoMd.ColumnType == ColumnType.Double) | ||
{ | ||
// If one of them is double map result to double. | ||
return new MetadataColumn(0, 0, "ADD_Result", new ColumnInfo(ColumnType.Double)); | ||
} | ||
else | ||
{ | ||
return new MetadataColumn(0, 0, "ADD_Result", new ColumnInfo(ColumnType.Int)); | ||
} | ||
} | ||
|
||
public static IFunctionCall MapToFunctor(MetadataColumn arg1, MetadataColumn arg2) | ||
{ | ||
return ((arg1.ColumnType.ColumnType, arg2.ColumnType.ColumnType)) switch | ||
{ | ||
(ColumnType.Int, ColumnType.Int) => new AddFunctorInt(), | ||
(ColumnType.Int, ColumnType.Double) => new AddFunctorIntDouble(), | ||
(ColumnType.Double, ColumnType.Int) => new AddFunctorDoubleInt(), | ||
(ColumnType.Double, ColumnType.Double) => new AddFunctorDouble(), | ||
_ => throw new ArgumentException("Invalid type"), | ||
}; | ||
} | ||
} | ||
|
||
public class AddFunctorInt : IFunctionCall | ||
{ | ||
public void ExecCompute(RowHolder inputRowHolder, RowHolder outputRowHolder, MetadataColumn[] sourceArguments, int outputPosition) | ||
{ | ||
FunctorArgChecks.CheckInputArguments(sourceArguments, new[] { ColumnType.Int, ColumnType.Int }); | ||
int argOneExtracted = inputRowHolder.GetField<int>(sourceArguments[0].ColumnId); | ||
int argTwoExtracted = inputRowHolder.GetField<int>(sourceArguments[1].ColumnId); | ||
|
||
int res = argOneExtracted + argTwoExtracted; | ||
|
||
outputRowHolder.SetField<int>(outputPosition, res); | ||
} | ||
} | ||
|
||
public class AddFunctorDouble : IFunctionCall | ||
{ | ||
public void ExecCompute(RowHolder inputRowHolder, RowHolder outputRowHolder, MetadataColumn[] sourceArguments, int outputPosition) | ||
{ | ||
FunctorArgChecks.CheckInputArguments(sourceArguments, new[] { ColumnType.Double, ColumnType.Double}); | ||
|
||
double argOneExtracted = inputRowHolder.GetField<double>(sourceArguments[0].ColumnId); | ||
double argTwoExtracted = inputRowHolder.GetField<double>(sourceArguments[1].ColumnId); | ||
|
||
double res = argOneExtracted + argTwoExtracted; | ||
|
||
outputRowHolder.SetField<double>(outputPosition, res); | ||
} | ||
} | ||
|
||
public class AddFunctorDoubleInt : IFunctionCall | ||
{ | ||
public void ExecCompute(RowHolder inputRowHolder, RowHolder outputRowHolder, MetadataColumn[] sourceArguments, int outputPosition) | ||
{ | ||
FunctorArgChecks.CheckInputArguments(sourceArguments, new[] { ColumnType.Double, ColumnType.Int}); | ||
|
||
double argOneExtracted = inputRowHolder.GetField<double>(sourceArguments[0].ColumnId); | ||
double argTwoExtracted = inputRowHolder.GetField<int>(sourceArguments[1].ColumnId); | ||
|
||
double res = argOneExtracted + argTwoExtracted; | ||
|
||
outputRowHolder.SetField<double>(outputPosition, res); | ||
} | ||
} | ||
|
||
public class AddFunctorIntDouble : IFunctionCall | ||
{ | ||
public void ExecCompute(RowHolder inputRowHolder, RowHolder outputRowHolder, MetadataColumn[] sourceArguments, int outputPosition) | ||
{ | ||
FunctorArgChecks.CheckInputArguments(sourceArguments, new[] { ColumnType.Double, ColumnType.Int}); | ||
|
||
double argOneExtracted = inputRowHolder.GetField<int>(sourceArguments[0].ColumnId); | ||
double argTwoExtracted = inputRowHolder.GetField<double>(sourceArguments[1].ColumnId); | ||
|
||
double res = argOneExtracted + argTwoExtracted; | ||
|
||
outputRowHolder.SetField<double>(outputPosition, res); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using MetadataManager; | ||
using PageManager; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace QueryProcessing | ||
{ | ||
/// <summary> | ||
/// Responsible for mapping function calls from syntax tree to functor class. | ||
/// </summary> | ||
public static class FuncCallMapper | ||
{ | ||
public static MetadataColumn GetMetadataInfoForOutput(Sql.columnSelect.Func func, MetadataColumn[] sourceInput) | ||
{ | ||
Sql.FuncType funcType = func.Item.Item1; | ||
|
||
if (funcType.IsAdd) | ||
{ | ||
return AddFunctorOutputMappingHandler.GetMetadataInfoForOutput(func, sourceInput); | ||
} | ||
|
||
throw new NotImplementedException(); | ||
} | ||
|
||
public static Action<RowHolder, RowHolder> BuildFunctor(Sql.columnSelect.Func func, int outputPosition, MetadataColumn[] sourceColumns) | ||
{ | ||
Sql.FuncType funcType = func.Item.Item1; | ||
Sql.scalarArgs args = func.Item.Item2; | ||
|
||
if (funcType.IsAdd) | ||
{ | ||
Sql.scalarArgs.Args2 argsExtracted = (Sql.scalarArgs.Args2)args; | ||
Sql.value arg1 = argsExtracted.Item.Item1; | ||
Sql.value arg2 = argsExtracted.Item.Item2; | ||
|
||
if (!arg1.IsId || !arg2.IsId) | ||
{ | ||
// TODO: | ||
throw new Exception("Only support for ids as function arguments"); | ||
} | ||
|
||
Sql.value.Id arg1Id = (Sql.value.Id)(arg1); | ||
Sql.value.Id arg2Id = (Sql.value.Id)(arg2); | ||
|
||
MetadataColumn mc1 = QueryProcessingAccessors.GetMetadataColumn(arg1Id.Item, sourceColumns); | ||
MetadataColumn mc2 = QueryProcessingAccessors.GetMetadataColumn(arg2Id.Item, sourceColumns); | ||
var functor = AddFunctorOutputMappingHandler.MapToFunctor(mc1, mc2); | ||
|
||
return (RowHolder inputRh, RowHolder outputRh) => | ||
{ | ||
functor.ExecCompute( | ||
inputRh, | ||
outputRh, | ||
new MetadataColumn[] { mc1, mc2 }, | ||
outputPosition | ||
); | ||
}; | ||
} | ||
|
||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using MetadataManager; | ||
using PageManager; | ||
|
||
namespace QueryProcessing | ||
{ | ||
public interface IFunctionCall | ||
{ | ||
void ExecCompute(RowHolder inputRowHolder, RowHolder outputRowHolder, MetadataColumn[] sourceArguments, int outputPosition); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.