You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "ptupitsyn (via GitHub)" <gi...@apache.org> on 2023/04/06 04:22:40 UTC

[GitHub] [ignite-3] ptupitsyn commented on a diff in pull request #1906: IGNITE-18120 .NET: Thin 3.0: Allow arbitrary MemberInit projections in LINQ

ptupitsyn commented on code in PR #1906:
URL: https://github.com/apache/ignite-3/pull/1906#discussion_r1159252061


##########
modules/platforms/dotnet/Apache.Ignite.Tests/Linq/LinqSqlGenerationTests.MemberInit.cs:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Tests.Linq;
+
+using System.Collections.Generic;
+using System.Linq;
+using Internal.Linq;
+using NUnit.Framework;
+using Table;
+
+/// <summary>
+/// Tests MemberInit.
+/// </summary>
+public partial class LinqSqlGenerationTests
+{
+    [Test]
+    public void Do() => AssertSql(

Review Comment:
   Let's try to come up with a more descriptive test names.



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Linq/IgniteQueryExpressionVisitor.cs:
##########
@@ -294,6 +294,41 @@ protected override Expression VisitMember(MemberExpression expression)
         return expression;
     }
 
+    /** <inheritdoc /> */
+    protected override Expression VisitMemberInit(MemberInitExpression expression)
+    {
+        var first = true;
+
+        if (expression.NewExpression.Arguments.Any())
+        {
+            VisitNew(expression.NewExpression);
+            first = false;
+        }
+
+        for (var i = 0; i < expression.Bindings.Count; i++)
+        {
+            var arg = (MemberAssignment)expression.Bindings[i]; // todo

Review Comment:
   todo?



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Linq/ResultSelector.cs:
##########
@@ -195,6 +207,65 @@ private static RowReader<T> EmitSingleColumnReader<T>(IColumnMetadata column, Re
         return (RowReader<T>)method.CreateDelegate(typeof(RowReader<T>));
     }
 
+    private static RowReader<T> EmitMemberInitReader<T>(
+        MemberInitCacheTarget target,
+        IReadOnlyList<IColumnMetadata> columns,
+        ResultSelectorOptions options)
+    {
+        var method = new DynamicMethod(
+            name: $"MemberInitFromBinaryTupleReader_{typeof(T).FullName}_{GetNextId()}",
+            returnType: typeof(T),
+            parameterTypes: new[] { typeof(IReadOnlyList<IColumnMetadata>), typeof(BinaryTupleReader).MakeByRefType() },
+            m: typeof(IIgnite).Module,
+            skipVisibility: true);
+
+        var il = method.GetILGenerator();
+        var ctorParams = target.CtorInfo.GetParameters();
+        var membersInitiated = target.PropertiesOrFields;
+
+        if (ctorParams.Length + membersInitiated.Count != columns.Count)
+        {
+            throw new InvalidOperationException("Constructor parameter count + initialized members parameter count" +

Review Comment:
   Is this situation possible?



##########
modules/platforms/dotnet/Apache.Ignite.Tests/Linq/LinqTests.MemberInit.cs:
##########
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Tests.Linq;
+
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Linq;
+using NUnit.Framework;
+
+/// <summary>
+/// Linq MemberInitTests.
+/// </summary>
+public partial class LinqTests
+{
+    [Test]
+    public void TestSelectMemberInitInitOnly()
+    {
+        var res = PocoView.AsQueryable()
+            .Where(x => x.Key == 2)
+            .Select(x => new CustomProjectionCtorAndInit { ValueProp = x.Key, RefProp = x.Val })
+            .ToArray();
+
+        Assert.AreEqual(1, res.Length);
+        Assert.AreEqual(2, res[0].ValueProp);
+        Assert.AreEqual("v-2", res[0].RefProp);
+    }
+
+    [Test]
+    public void TestSelectMemberInitCtorOnly()
+    {
+        var res = PocoView.AsQueryable()
+            .Where(x => x.Key == 2)
+            .Select(x => new CustomProjectionCtorAndInit(x.Key + 42, x.Val))
+            .ToArray();
+
+        Assert.AreEqual(1, res.Length);
+        Assert.AreEqual(44, res[0].Id);
+        Assert.AreEqual("v-2", res[0].RefProp);
+    }
+
+    [Test]
+    public void TestSelectMemberInitSupportsInitOnlyProps()
+    {
+        var res = PocoView.AsQueryable()
+            .Where(x => x.Key == 2)
+            .Select(x => new CustomProjectionCtorAndInit
+            {
+                RefPropInitOnly = x.Val,
+                ValuePropInitOnly = x.Key + 2,
+            })
+            .ToArray();
+
+        Assert.AreEqual(1, res.Length);
+        var resRow = res[0];
+        Assert.AreEqual("v-2", resRow.RefPropInitOnly);
+        Assert.AreEqual(4, resRow.ValuePropInitOnly);
+    }
+
+    [Test]
+    public void TestSelectMemberInitSupportsFields()
+    {
+        var res = PocoView.AsQueryable()
+            .Where(x => x.Key == 2)
+            .Select(x => new CustomProjectionCtorAndInit
+            {
+                RefField = x.Val,
+                ValueField = x.Key + 3
+            })
+            .ToArray();
+
+        Assert.AreEqual(1, res.Length);
+        var resRow = res[0];
+        Assert.AreEqual("v-2", resRow.RefField);
+        Assert.AreEqual(5, resRow.ValueField);
+    }
+
+    [Test]
+    public void TestSelectMemberInitCtorAndInit()
+    {
+        var res = PocoView.AsQueryable()
+            .Where(x => x.Key == 2)
+            .Select(x => new CustomProjectionCtorAndInit(x.Key)
+            {
+                RefProp = x.Val,
+                ValueProp = x.Key + 1,
+                RefPropInitOnly = x.Val,
+                ValuePropInitOnly = x.Key + 2,
+                RefField = x.Val,
+                ValueField = x.Key + 3
+            })
+            .ToArray();
+
+        Assert.AreEqual(1, res.Length);
+        var resRow = res[0];
+        Assert.AreEqual(2, resRow.Id);
+        Assert.AreEqual("v-2", resRow.RefProp);
+        Assert.AreEqual(3, resRow.ValueProp);
+        Assert.AreEqual("v-2", resRow.RefPropInitOnly);
+        Assert.AreEqual(4, resRow.ValuePropInitOnly);
+        Assert.AreEqual("v-2", resRow.RefField);
+        Assert.AreEqual(5, resRow.ValueField);
+    }
+
+    [Test]
+    public void TestSelectMemberInitFirstOrDefaultReturnsNull()

Review Comment:
   This test does not seem to involve MemberInit - can you please clarify?



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Linq/ResultSelector.cs:
##########
@@ -75,6 +77,16 @@ public static RowReader<T> Get<T>(IReadOnlyList<IColumnMetadata> columns, Expres
                 static k => EmitConstructorReader<T>(k.Target, k.Columns, k.Options));
         }
 
+        if (selectorExpression is MemberInitExpression memberInitExpression)
+        {
+            var ctorInfo = memberInitExpression.NewExpression.Constructor!;
+            var cacheTarget = new MemberInitCacheTarget(ctorInfo, memberInitExpression.Bindings.Select(b => b.Member).ToList());

Review Comment:
   Let's pass `memberInitExpression.Bindings` to the `MemberInitCacheTarget` constructor to avoid list allocation.



##########
modules/platforms/dotnet/Apache.Ignite.Tests/Linq/LinqSqlGenerationTests.MemberInit.cs:
##########
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Tests.Linq;
+
+using System.Collections.Generic;
+using System.Linq;
+using Internal.Linq;
+using NUnit.Framework;
+using Table;
+
+/// <summary>
+/// Tests MemberInit.
+/// </summary>
+public partial class LinqSqlGenerationTests
+{
+    [Test]
+    public void Do() => AssertSql(
+        "select _T0.KEY as KEY, _T0.VAL as VALUE from PUBLIC.tbl1 as _T0",
+        q => q.Select(p => new CustomProjection {Key = p.Key, Value = p.Val}).ToList());
+
+    [Test]
+    public void Do1() => AssertSql(

Review Comment:
   Same as above.



##########
modules/platforms/dotnet/Apache.Ignite/Internal/Linq/IgniteQueryExpressionVisitor.cs:
##########
@@ -294,6 +294,41 @@ protected override Expression VisitMember(MemberExpression expression)
         return expression;
     }
 
+    /** <inheritdoc /> */
+    protected override Expression VisitMemberInit(MemberInitExpression expression)
+    {
+        var first = true;
+
+        if (expression.NewExpression.Arguments.Any())
+        {
+            VisitNew(expression.NewExpression);
+            first = false;
+        }
+
+        for (var i = 0; i < expression.Bindings.Count; i++)

Review Comment:
   Convert to foreach?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org