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

Query: Adds support for multi-value Group By query for LINQ #4481

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 74 additions & 36 deletions Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,29 @@ private static SqlObjectProperty[] CreateInitializers(ReadOnlyCollection<Express
result[i] = prop;
}

return result;
}

private static SqlSelectItem[] CreateSelectItems(ReadOnlyCollection<Expression> arguments, ReadOnlyCollection<MemberInfo> members, TranslationContext context)
{
if (arguments.Count != members.Count)
{
throw new InvalidOperationException("Expected same number of arguments as members");
}

SqlSelectItem[] result = new SqlSelectItem[arguments.Count];
for (int i = 0; i < arguments.Count; i++)
{
Expression arg = arguments[i];
MemberInfo member = members[i];
SqlScalarExpression selectExpression = ExpressionToSql.VisitScalarExpression(arg, context);

string memberName = member.GetMemberName(context);
SqlIdentifier alias = SqlIdentifier.Create(memberName);
SqlSelectItem prop = SqlSelectItem.Create(selectExpression, alias);
result[i] = prop;
}

return result;
}

Expand Down Expand Up @@ -1735,48 +1758,63 @@ private static Collection VisitGroupBy(Type returnElementType, ReadOnlyCollectio
switch (valueSelectorExpression.NodeType)
{
case ExpressionType.Constant:
{
ConstantExpression constantExpression = (ConstantExpression)valueSelectorExpression;
SqlScalarExpression selectExpression = ExpressionToSql.VisitConstant(constantExpression, context);

SqlSelectSpec sqlSpec = SqlSelectValueSpec.Create(selectExpression);
SqlSelectClause select = SqlSelectClause.Create(sqlSpec, null);
context.CurrentQuery = context.CurrentQuery.AddSelectClause(select, context);
break;
}
{
ConstantExpression constantExpression = (ConstantExpression)valueSelectorExpression;
SqlScalarExpression selectExpression = ExpressionToSql.VisitConstant(constantExpression, context);
SqlSelectSpec sqlSpec = SqlSelectValueSpec.Create(selectExpression);
SqlSelectClause select = SqlSelectClause.Create(sqlSpec, null);
context.CurrentQuery = context.CurrentQuery.AddSelectClause(select, context);
break;
}
case ExpressionType.Parameter:
{
ParameterExpression parameterValueExpression = (ParameterExpression)valueSelectorExpression;
SqlScalarExpression selectExpression = ExpressionToSql.VisitParameter(parameterValueExpression, context);
{
ParameterExpression parameterValueExpression = (ParameterExpression)valueSelectorExpression;
SqlScalarExpression selectExpression = ExpressionToSql.VisitParameter(parameterValueExpression, context);

SqlSelectSpec sqlSpec = SqlSelectValueSpec.Create(selectExpression);
SqlSelectClause select = SqlSelectClause.Create(sqlSpec, null);
context.CurrentQuery = context.CurrentQuery.AddSelectClause(select, context);
break;
}
SqlSelectSpec sqlSpec = SqlSelectValueSpec.Create(selectExpression);
SqlSelectClause select = SqlSelectClause.Create(sqlSpec, null);
context.CurrentQuery = context.CurrentQuery.AddSelectClause(select, context);
break;
}
case ExpressionType.Call:
{
// Single Value Selector
MethodCallExpression methodCallExpression = (MethodCallExpression)valueSelectorExpression;
switch (methodCallExpression.Method.Name)
{
case LinqMethods.Max:
case LinqMethods.Min:
case LinqMethods.Average:
case LinqMethods.Count:
case LinqMethods.Sum:
ExpressionToSql.VisitMethodCall(methodCallExpression, context);
break;
default:
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}
// Single Value Selector
MethodCallExpression methodCallExpression = (MethodCallExpression)valueSelectorExpression;
switch (methodCallExpression.Method.Name)
{
case LinqMethods.Max:
case LinqMethods.Min:
case LinqMethods.Average:
case LinqMethods.Count:
case LinqMethods.Sum:
ExpressionToSql.VisitMethodCall(methodCallExpression, context);
break;
default:
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.MethodNotSupported, methodCallExpression.Method.Name));
}

break;
}
case ExpressionType.New:
// TODO: Multi Value Selector
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.ExpressionTypeIsNotSupported, ExpressionType.New));
break;
}
case ExpressionType.New:
Copy link
Contributor

@adityasa adityasa May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not able to make the 1:1 connection between new expression and multi-value group by.
IOW, it's not clear why every new expression here is a multi-value GROUP BY and vice-versa. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
// Add select item clause at the end of this method
NewExpression newExpression = (NewExpression)valueSelectorExpression;
// Get the list of items and the bindings

if (newExpression.Members == null)
{
throw new DocumentQueryException(ClientResources.ConstructorInvocationNotSupported);
}

SqlSelectItem[] selectItems = ExpressionToSql.CreateSelectItems(newExpression.Arguments, newExpression.Members, context);

SqlSelectListSpec sqlSpec = SqlSelectListSpec.Create(selectItems);
SqlSelectClause select = SqlSelectClause.Create(sqlSpec, null);
context.CurrentQuery = context.CurrentQuery.AddSelectClause(select, context);

break;
}
default:
throw new DocumentQueryException(string.Format(CultureInfo.CurrentCulture, ClientResources.ExpressionTypeIsNotSupported, valueSelectorExpression.NodeType));
}
Expand Down