Skip to content

Commit

Permalink
fixed query with star throws exception, other fixes around stringify (#…
Browse files Browse the repository at this point in the history
…68)

* fixed query with star throws exception, other fixes around stringify

* Update test results badge [skip ci]

---------

Co-authored-by: github-actions <[email protected]>
  • Loading branch information
Puchaczov and github-actions authored Sep 5, 2024
1 parent c0621c6 commit c756b86
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 36 deletions.
56 changes: 56 additions & 0 deletions Musoq.Evaluator.Tests/ColumnsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,62 @@ namespace Musoq.Evaluator.Tests;
[TestClass]
public class ColumnsTests : BasicEntityTestBase
{
[TestMethod]
public void WhenStarUnfoldToMultipleColumns_AndExplicitColumnIsUsedWithinWhere_ShouldPass()
{
const string query = @"select * from #A.entities() a where a.Month = 'january'";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A", new[]
{
new BasicEntity("january", 50m),
new BasicEntity("february", 100m),
new BasicEntity("march", 150m)
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);

var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual(13, table.Columns.Count());

Assert.AreEqual("a.Month", table.Columns.ElementAt(6).ColumnName);
Assert.AreEqual("january", table[0].Values[6]);
}

[TestMethod]
public void WhenStarUnfoldToMultipleColumns_AndExplicitColumnIsUsedWithinSelect_ShouldPass()
{
const string query = @"with p as (select * from #A.entities() a where a.Month = 'january') select * from p";

var sources = new Dictionary<string, IEnumerable<BasicEntity>>
{
{
"#A", new[]
{
new BasicEntity("january", 50m),
new BasicEntity("february", 100m),
new BasicEntity("march", 150m)
}
}
};

var vm = CreateAndRunVirtualMachine(query, sources);

var table = vm.Run();

Assert.AreEqual(1, table.Count);
Assert.AreEqual(13, table.Columns.Count());

Assert.AreEqual("a.Month", table.Columns.ElementAt(6).ColumnName);
Assert.AreEqual("january", table[0].Values[6]);
}

[TestMethod]
public void WhenComplexObjectAccessNonExistingProperty_ShouldFail()
{
Expand Down
2 changes: 1 addition & 1 deletion Musoq.Evaluator.Tests/Schema/Basic/BasicEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public BasicEntity(DateTime time)

public int Id { get; set; }

public int[] Array => new[] {0, 1, 2};
public int[] Array => [0, 1, 2];

public Dictionary<string, string> Dictionary => new()
{
Expand Down
17 changes: 10 additions & 7 deletions Musoq.Evaluator.Tests/Schema/Basic/BasicEntityTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ namespace Musoq.Evaluator.Tests.Schema.Basic
{
public class BasicEntityTable : ISchemaTable
{
public BasicEntityTable()
{
}

public ISchemaColumn[] Columns { get; } = {
public ISchemaColumn[] Columns { get; } =
[
new SchemaColumn(nameof(BasicEntity.Name), 10,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.Name))!.PropertyType),
new SchemaColumn(nameof(BasicEntity.City), 11,
Expand All @@ -30,8 +27,14 @@ public BasicEntityTable()
new SchemaColumn(nameof(BasicEntity.Id), 18,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.Id))!.PropertyType),
new SchemaColumn(nameof(BasicEntity.NullableValue), 19,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.NullableValue))!.PropertyType)
};
typeof(BasicEntity).GetProperty(nameof(BasicEntity.NullableValue))!.PropertyType),
new SchemaColumn(nameof(BasicEntity.Array), 20,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.Array))!.PropertyType),
new SchemaColumn(nameof(BasicEntity.Other), 21,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.Other))!.PropertyType),
new SchemaColumn(nameof(BasicEntity.Dictionary), 22,
typeof(BasicEntity).GetProperty(nameof(BasicEntity.Dictionary))!.PropertyType),
];

public ISchemaColumn GetColumnByName(string name)
{
Expand Down
28 changes: 21 additions & 7 deletions Musoq.Evaluator.Tests/SingleSchemaEvaluatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Musoq.Evaluator.Exceptions;
using Musoq.Evaluator.Tests.Schema.Basic;

Expand Down Expand Up @@ -586,12 +588,21 @@ public void SimpleShowAllColumnsTest()

Assert.AreEqual("NullableValue", table.Columns.ElementAt(10).ColumnName);
Assert.AreEqual(typeof(int?), table.Columns.ElementAt(10).ColumnType);

Assert.AreEqual("Array", table.Columns.ElementAt(11).ColumnName);
Assert.AreEqual(typeof(int[]), table.Columns.ElementAt(11).ColumnType);

Assert.AreEqual("Other", table.Columns.ElementAt(12).ColumnName);
Assert.AreEqual(typeof(BasicEntity), table.Columns.ElementAt(12).ColumnType);

Assert.AreEqual("Dictionary", table.Columns.ElementAt(13).ColumnName);
Assert.AreEqual(typeof(Dictionary<string, string>), table.Columns.ElementAt(13).ColumnType);

Assert.AreEqual("Name2", table.Columns.ElementAt(11).ColumnName);
Assert.AreEqual(typeof(string), table.Columns.ElementAt(11).ColumnType);
Assert.AreEqual("Name2", table.Columns.ElementAt(14).ColumnName);
Assert.AreEqual(typeof(string), table.Columns.ElementAt(14).ColumnType);

Assert.AreEqual("SelfString", table.Columns.ElementAt(12).ColumnName);
Assert.AreEqual(typeof(string), table.Columns.ElementAt(12).ColumnType);
Assert.AreEqual("SelfString", table.Columns.ElementAt(15).ColumnName);
Assert.AreEqual(typeof(string), table.Columns.ElementAt(15).ColumnType);

Assert.AreEqual(1, table.Count);

Expand All @@ -606,8 +617,11 @@ public void SimpleShowAllColumnsTest()
Assert.AreEqual(DateTime.MaxValue, table[0].Values[8]);
Assert.AreEqual(5, table[0].Values[9]);
Assert.AreEqual(null, table[0].Values[10]);
Assert.AreEqual("ABBA", table[0].Values[11]);
Assert.AreEqual("TEST STRING", table[0].Values[12]);
Assert.IsNotNull(table[0].Values[11]);
Assert.AreEqual(entity, table[0].Values[12]);
Assert.IsNotNull(table[0].Values[13]);
Assert.AreEqual("ABBA", table[0].Values[14]);
Assert.AreEqual("TEST STRING", table[0].Values[15]);
}

[TestMethod]
Expand Down
1 change: 1 addition & 0 deletions Musoq.Evaluator.Tests/StringifyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public class StringifyTests : BasicEntityTestBase
[DataRow("select s.Column1, s.Column2 from #some.thing() s where s.Column2 like 'A%' order by s.Column1 skip 4 take 8")]
[DataRow("select s.Column1, s.Column2 from #some.thing() s group by s.Column1, s.Column2 order by s.Column1 skip 5 take 10")]
[DataRow("select s.Column1, s.Column2 from #some.thing() s where s.Column1 > 0 group by s.Column2 order by s.Column1 skip 2 take 5")]
[DataRow("select t.Name, Count(t.Name) from #some.thing(true) t group by t.Name having Count(t.Name) > 1")]

public void WhenToStringCalled_ShouldReturnSameQuery(string query)
{
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 @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>6.0.4</Version>
<Version>6.0.5</Version>
<Authors>Jakub Puchała</Authors>
<Product>Musoq</Product>
<PackageProjectUrl>https://github.com/Puchaczov/Musoq</PackageProjectUrl>
Expand Down
6 changes: 3 additions & 3 deletions Musoq.Evaluator/Visitors/BuildMetadataAndInferTypeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void Visit(AllColumnsNode node)
TextSpan.Empty
),
i,
tableSymbol.HasAlias ? _identifier : column.ColumnName);
tableSymbol.HasAlias ? $"{_identifier}.{column.ColumnName}" : column.ColumnName);
}

Nodes.Push(node);
Expand Down Expand Up @@ -752,8 +752,8 @@ public void Visit(SchemaFromNode node)
_queryAlias = AliasGenerator.CreateAliasIfEmpty(node.Alias, _generatedAliases, _schemaFromKey.ToString());
_generatedAliases.Add(_queryAlias);

var table = _currentScope.Name != "Desc"
? schema.GetTableByName(
var isDesc = _currentScope.Name == "Desc";
var table = !isDesc ? schema.GetTableByName(
node.Method,
new RuntimeContext(
CancellationToken.None,
Expand Down
14 changes: 7 additions & 7 deletions Musoq.Evaluator/Visitors/ToCSharpRewriteTreeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public class ToCSharpRewriteTreeVisitor : IToCSharpTranslationExpressionVisitor
private const char EscapeQuoteStringCharacterReplacement = '\'';

private readonly Dictionary<string, int> _inMemoryTableIndexes = new();
private readonly List<string> _loadedAssemblies = new();
private readonly List<string> _loadedAssemblies = [];

private readonly List<SyntaxNode> _members = new();
private readonly List<SyntaxNode> _members = [];
private readonly Stack<string> _methodNames = new();

private readonly List<string> _namespaces = new();
private readonly List<string> _namespaces = [];
private readonly IDictionary<string, int[]> _setOperatorFieldIndexes;

private readonly Dictionary<string, Type> _typesToInstantiate = new();
Expand Down Expand Up @@ -948,11 +948,11 @@ public void Visit(ArgsListNode node)
for (var i = 0; i < node.Args.Length; i++)
args = args.Add(SyntaxFactory.Argument((ExpressionSyntax) Nodes.Pop()));

var rargs = SyntaxFactory.SeparatedList<ArgumentSyntax>();
var rArgs = SyntaxFactory.SeparatedList<ArgumentSyntax>();

for (var i = args.Count - 1; i >= 0; i--) rargs = rargs.Add(args[i]);
for (var i = args.Count - 1; i >= 0; i--) rArgs = rArgs.Add(args[i]);

Nodes.Push(SyntaxFactory.ArgumentList(rargs));
Nodes.Push(SyntaxFactory.ArgumentList(rArgs));
}


Expand Down Expand Up @@ -1350,7 +1350,7 @@ public void Visit(JoinInMemoryWithSourceTableFromNode node)
}

foreach (var column in fullTransitionTable.GetColumns(
fullTransitionTable.CompoundTables[fullTransitionTable.CompoundTables.Length - 1]))
fullTransitionTable.CompoundTables[^1]))
{
expressions.Add(
SyntaxFactory.ElementAccessExpression(
Expand Down
2 changes: 1 addition & 1 deletion Musoq.Parser/Musoq.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>3.0.10</Version>
<Version>3.0.11</Version>
<Authors>Jakub Puchała</Authors>
<Product>Musoq</Product>
<PackageProjectUrl>https://github.com/Puchaczov/Musoq</PackageProjectUrl>
Expand Down
2 changes: 1 addition & 1 deletion Musoq.Parser/Nodes/BooleanNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public BooleanNode(bool value)

public override string ToString()
{
return Value.ToString(CultureInfo.InvariantCulture);
return Value ? "true" : "false";
}

public override void Accept(IExpressionVisitor visitor)
Expand Down
14 changes: 8 additions & 6 deletions Musoq.Parser/Nodes/GroupByNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Musoq.Parser.Nodes
public class GroupByNode : Node
{
public GroupByNode(FieldNode[] fields, HavingNode node)
{
{
Fields = fields;
Having = node;
var fieldsIds = fields.Length == 0 ? string.Empty : fields.Select(f => f.Id).Aggregate((a, b) => a + b);
Expand All @@ -28,11 +28,13 @@ public override void Accept(IExpressionVisitor visitor)

public override string ToString()
{
var fields = Fields.Length == 0
? string.Empty
: Fields.Select(f => f.ToString()).Aggregate((a, b) => $"{a.ToString()}, {b.ToString()}");
return
$"group by {fields}{Having?.ToString()}";
var fields = Fields.Length == 0 ? string.Empty : Fields.Select(f => f.ToString()).Aggregate((a, b) => $"{a.ToString()}, {b.ToString()}");
var groupBy = $"group by {fields}";

if (Having == null)
return groupBy;

return $"{groupBy} {Having.ToString()}";
}
}
}
3 changes: 2 additions & 1 deletion Musoq.Parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ private GroupByNode ComposeGroupByNode()
Consume(TokenType.GroupBy);

var fields = ComposeFields();


if (fields.Length == 0) throw new NotSupportedException("Group by clause does not have any fields.");
if (Current.TokenType != TokenType.Having) return new GroupByNode(fields, null);

Consume(TokenType.Having);
Expand Down
2 changes: 1 addition & 1 deletion badges/tests-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c756b86

Please sign in to comment.