Skip to content

Commit

Permalink
1. Group artifacts won't be used as a context for another queries
Browse files Browse the repository at this point in the history
2. IEnumerable<T> must much to IEnumerable<Concrete> when constructing generic method
3. When InMemoryTableFromNode used, it must also collect it's accessed columns.
  • Loading branch information
Puchaczov committed Jan 21, 2024
1 parent 3806166 commit 6bd899e
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 3 deletions.
176 changes: 176 additions & 0 deletions Musoq.Evaluator.Tests/CteTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Musoq.Evaluator.Tests.Schema.Basic;

Expand Down Expand Up @@ -641,5 +642,180 @@ with p as (
Assert.AreEqual("MUNICH", table[4].Values[0]);
Assert.AreEqual("GERMANY", table[4].Values[1]);
}

[TestMethod]
public void WhenMixedQueriesGroupingAreUsed_Variant1_ShouldPass()
{
var query = @"
with first as (
select 1 as Test from #A.entities()
), second as (
select 2 as Test from #B.entities() group by 'fake'
)
select c.GetBytes(c.Name) from first a
inner join #A.entities() c on 1 = 1
inner join second b on 1 = 1";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A",
new[]
{
new BasicEntity("First"),
}
},
{
"#B",
new[]
{
new BasicEntity("Second"),
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);
var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual("First", Encoding.UTF8.GetString((byte[])table[0].Values[0]));
}

[TestMethod]
public void WhenMixedQueriesGroupingAreUsed_Variant2_ShouldPass()
{
var query = @"
with first as (
select Name from #A.entities()
), second as (
select Name from #B.entities() group by Name
)
select
c.GetBytes(c.Name)
from first a
inner join #A.entities() c on 1 = 1
inner join second b on 1 = 1";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A",
new[]
{
new BasicEntity("First"),
}
},
{
"#B",
new[]
{
new BasicEntity("Second"),
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);
var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual("First", Encoding.UTF8.GetString((byte[])table[0].Values[0]));
}

[TestMethod]
public void WhenMixedQueriesGroupingAreUsed_Variant3_ShouldPass()
{
var query = @"
with first as (
select Name from #A.entities()
), second as (
select Name from #B.entities() group by Name
)
select
a.Name,
b.Name,
c.GetBytes(c.Name)
from first a
inner join #A.entities() c on 1 = 1
inner join second b on 1 = 1";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A",
new[]
{
new BasicEntity("First"),
}
},
{
"#B",
new[]
{
new BasicEntity("Second"),
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);
var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual("First", (string)table[0].Values[0]);
Assert.AreEqual("Second", (string)table[0].Values[1]);
Assert.AreEqual("First", Encoding.UTF8.GetString((byte[])table[0].Values[2]));
}

[TestMethod]
public void WhenMixedQueriesGroupingAreUsed_Variant4_ShouldPass()
{
var query = @"
with first as (
select a.Name as Name from #A.entities() a group by a.Name having Count(a.Name) > 0
), second as (
select b.Name() as Name from #B.entities() b inner join first a on 1 = 1
), third as (
select b.Name as Name from second b group by b.Name having Count(b.Name) > 0
), fourth as (
select
c.Name() as Name
from second b
inner join #A.entities() c on 1 = 1
inner join third d on 1 = 1
)
select
c.Name as Name
from fourth c";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A",
new[]
{
new BasicEntity("First"),
}
},
{
"#B",
new[]
{
new BasicEntity("Second"),
}
},
{
"#C",
new[]
{
new BasicEntity("Third"),
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);
var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual("First", (string)table[0].Values[0]);
}
}
}
27 changes: 27 additions & 0 deletions Musoq.Evaluator.Tests/GenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,31 @@ public void WhenRetrieveLengthFromArrayType_ShouldPass()
Assert.AreEqual(1, table.Count);
Assert.AreEqual(5, (int)table[0].Values[0]);
}

[TestMethod]
public void WhenSkipWithoutReductionRequired_ShouldPass()
{
var table = TestResultMethodTemplate("EnumerableToArray(Skip(GetBytes('test1'), 1))");

Assert.AreEqual(1, table.Count);
Assert.AreEqual("est1", Encoding.UTF8.GetString((byte[])table[0].Values[0]));
}

[TestMethod]
public void WhenTakeWithoutReductionRequired_ShouldPass()
{
var table = TestResultMethodTemplate("EnumerableToArray(Take(GetBytes('test1'), 1))");

Assert.AreEqual(1, table.Count);
Assert.AreEqual("t", Encoding.UTF8.GetString((byte[])table[0].Values[0]));
}

[TestMethod]
public void WhenSkipAndTakeWithoutReductionRequired_ShouldPass()
{
var table = TestResultMethodTemplate("EnumerableToArray(SkipAndTake(GetBytes('test1'), 1, 1))");

Assert.AreEqual(1, table.Count);
Assert.AreEqual("e", Encoding.UTF8.GetString((byte[])table[0].Values[0]));
}
}
2 changes: 1 addition & 1 deletion Musoq.Evaluator/Helpers/EvaluationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public GroupResolver(Group item)
_item = item;
}

public object[] Contexts => new object[] { _item };
public object[] Contexts => Array.Empty<object>();

public object this[string name]
{
Expand Down
2 changes: 1 addition & 1 deletion Musoq.Evaluator/Musoq.Evaluator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>5.6.6</Version>
<Version>5.6.7</Version>
<Authors>Jakub Puchała</Authors>
<Product>Musoq</Product>
<PackageProjectUrl>https://github.com/Puchaczov/Musoq</PackageProjectUrl>
Expand Down
9 changes: 8 additions & 1 deletion Musoq.Evaluator/Visitors/BuildMetadataAndInferTypeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,15 +1674,22 @@ private static bool TryConstructGenericMethod(MethodInfo methodInfo, ArgsListNod

foreach (var genericArgument in genericArguments)
{
for (int i = 0; i < parameters.Length; i++)
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
var returnType = args.Args.Where((arg, index) => index == i).Single().ReturnType;
var elementType = returnType.GetElementType();

if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == parameter.ParameterType.GetGenericTypeDefinition())
{
genericArgumentsDistinct.Add(returnType.GetGenericArguments()[0]);
continue;
}

if (parameter.ParameterType.IsGenericType && parameter.ParameterType.IsAssignableTo(typeof(IEnumerable<>).MakeGenericType(genericArgument)) && elementType is not null)
{
genericArgumentsDistinct.Add(elementType);
continue;
}

if (parameter.ParameterType.IsGenericType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ public override void Visit(SchemaFromNode node)

base.Visit(node);
}

public override void Visit(InMemoryTableFromNode node)
{
_accessColumns.TryAdd(node.Alias, new List<AccessColumnNode>());

base.Visit(node);
}
}

0 comments on commit 6bd899e

Please sign in to comment.