Skip to content
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

IsIn generate wrong column name from select expression #102

Closed
alberthajdu opened this issue Mar 6, 2018 · 2 comments
Closed

IsIn generate wrong column name from select expression #102

alberthajdu opened this issue Mar 6, 2018 · 2 comments

Comments

@alberthajdu
Copy link

Repro:
CoreTests.ShouldQueryInnerSelect currently have this assert:
Assert.Equal(1, await session.Query<Person, PersonByAge>().Where(x => x.Name.IsIn<PersonByName>(y => y.Name, y => y.Name.StartsWith("B") || y.Name.StartsWith("C"))).CountAsync());
Which generates this query:

SELECT  FROM "tpDocument" 
INNER JOIN "tpPersonByAge" 
	ON "tpPersonByAge"."DocumentId" = "tpDocument"."Id"  
	WHERE "tpPersonByAge"."Name" 
	IN (SELECT **"tpPersonByName"."Name"** 
		FROM "tpPersonByName"
		WHERE (("tpPersonByName"."Name" like @p0) or ("tpPersonByName"."Name" like @p1))) 
GROUP BY  HAVING  ORDER BY

Modify the select expression to return Id instead of Name:
Assert.Equal(1, await session.Query<Person, PersonByAge>().Where(x => x.Name.IsIn<PersonByName>(y => y.Id, y => y.Id==0)).CountAsync());
And it will generate this query:

SELECT  FROM "tpDocument" 
INNER JOIN "tpPersonByAge"
	ON "tpPersonByAge"."DocumentId" = "tpDocument"."Id"  
	WHERE "tpPersonByAge"."Name"
	IN (SELECT **"tpPersonByName"."Name"** 
		FROM "tpPersonByName" 
		WHERE ("tpPersonByName"."Id" = @p0))  
GROUP BY  HAVING  ORDER BY

I think in the inner select it should be "tpPersonByName"."Id" but it generates "tpPersonByName"."Name".

The issue comes from DefaultQuery MethodMappings[typeof(DefaultQueryExtensionsIndex).GetMethod("IsIn")] where the select fragment is generated:

// Build inner query
                    var _builder = new StringBuilder();

                    sqlBuilder.Select();

                    query.ConvertFragment(_builder, expression.Arguments[0]);
                    sqlBuilder.Selector(_builder.ToString());
                    _builder.Clear();

                    sqlBuilder.Table(((LambdaExpression)((UnaryExpression)selector).Operand).Parameters[0].Type.Name);
                    query.ConvertPredicate(_builder, ((LambdaExpression)((UnaryExpression)predicate).Operand).Body);
                    sqlBuilder.WhereAlso(_builder.ToString());

                    query._bound.RemoveAt(query._bound.Count - 1); 

And here in the ConvertFragment the expression's 1. argument should be used intead of the 0. I checked what will happen if I use the 1. argument but the ConvertFragment method is too complex for me but I think you'll know what to do.
query.ConvertFragment(_builder, expression.Arguments[0]);

Related to OrchardCMS/OrchardCore#1568

@alberthajdu
Copy link
Author

@sebastienros does the above make sense? If not I'll try again.

Could you please verify that this is an existing bug?

@sebastienros
Copy link
Owner

Looking into it right now

sebastienros added a commit that referenced this issue Mar 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants