Skip to content

Commit

Permalink
Follow up from #4554/#4548 reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Jul 17, 2024
1 parent bb59abc commit 67cdeb4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
10 changes: 5 additions & 5 deletions Source/LinqToDB/Linq/Builder/AllJoinsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
if (joinType is JoinType.Left or JoinType.Full)
{
result = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(buildInfo.Parent,
sequence : result,
nullabilitySequence : result,
defaultValue : null,
allowNullField : false,
isNullValidationDisabled : false);
sequence: result,
nullabilitySequence: result,
defaultValue: null,
allowNullField: false,
isNullValidationDisabled: false);
}

return BuildSequenceResult.FromContext(result);
Expand Down
26 changes: 22 additions & 4 deletions Source/LinqToDB/Linq/Builder/AllJoinsLinqBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,30 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
break;
}

if (joinType == JoinType.Right || joinType == JoinType.Full)
outerContext = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(buildInfo.Parent, outerContext, outerContext, null, false, false);
if (joinType is JoinType.Right or JoinType.Full)
{
outerContext = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(
buildInfo.Parent,
outerContext,
outerContext,
defaultValue: null,
allowNullField: false,
isNullValidationDisabled: false);
}

outerContext = new SubQueryContext(outerContext);

if (joinType == JoinType.Left || joinType == JoinType.Full)
innerContext = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(buildInfo.Parent, innerContext, innerContext, null, false, false);
if (joinType is JoinType.Left or JoinType.Full)
{
innerContext = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(
buildInfo.Parent,
innerContext,
innerContext,
defaultValue: null,
allowNullField: false,
isNullValidationDisabled: false);
}

innerContext = new SubQueryContext(innerContext);

var selector = methodCall.Arguments[^1].UnwrapLambda();
Expand Down
22 changes: 14 additions & 8 deletions Source/LinqToDB/Linq/Builder/DefaultIfEmptyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ public static bool CanBuildMethod(MethodCallExpression call, BuildInfo info, Exp

if (notNull.Count == 0)
{
/*
if (!allowNullField)
return null;
*/

if (builder.DataContext.SqlProviderFlags.IsAccessBuggyLeftJoinConstantNullability)
{
if (placeholders.Count == 0)
Expand All @@ -42,8 +37,13 @@ public static bool CanBuildMethod(MethodCallExpression call, BuildInfo info, Exp
}
else
{
notNull.Add(SequenceHelper.CreateSpecialProperty(new ContextRefExpression(notNullHandlerSequence.ElementType, notNullHandlerSequence), typeof(int?),
DefaultIfEmptyContext.NotNullPropName));
notNull.Add(
SequenceHelper.CreateSpecialProperty(
new ContextRefExpression(notNullHandlerSequence.ElementType, notNullHandlerSequence),
typeof(int?),
DefaultIfEmptyContext.NotNullPropName
)
);
}

}
Expand Down Expand Up @@ -93,7 +93,13 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
defaultValueContext.SelectQuery.Select.AddNew(new SqlValue(1));
}

var defaultIfEmptyContext = new DefaultIfEmptyContext(buildInfo.Parent, sequence, sequence, null, true, false);
var defaultIfEmptyContext = new DefaultIfEmptyContext(
buildInfo.Parent,
sequence,
sequence,
defaultValue: null,
allowNullField: true,
isNullValidationDisabled: false);

var notNullConditions = defaultIfEmptyContext.GetNotNullConditions();

Expand Down
9 changes: 8 additions & 1 deletion Source/LinqToDB/Linq/Builder/FirstSingleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,14 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder

if (buildInfo.Parent != null && (cardinality & SourceCardinality.Zero) != 0)
{
sequence = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(buildInfo.Parent, sequence, sequence, null, allowNullField: true, isNullValidationDisabled: false);
sequence = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(
buildInfo.Parent,
sequence,
sequence,
defaultValue: null,
allowNullField: true,
isNullValidationDisabled: false);

canBeWeak = true;
}

Expand Down
22 changes: 17 additions & 5 deletions Source/LinqToDB/Linq/Builder/SelectManyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
//
if (collectionInfo.JoinType == JoinType.Full || collectionInfo.JoinType == JoinType.Right)
{
sequence = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(buildInfo.Parent, sequence, collection, null, false, false);
sequence = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(
buildInfo.Parent,
sequence,
collection,
defaultValue: null,
allowNullField: false,
isNullValidationDisabled: false);
}

var collectionDefaultIfEmptyContext = SequenceHelper.GetDefaultIfEmptyContext(collection);
Expand All @@ -88,9 +94,11 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
_ => joinType
};

var projected = builder.BuildSqlExpression(collection,
new ContextRefExpression(collection.ElementType, collection), buildInfo.GetFlags(),
buildFlags : ExpressionBuilder.BuildFlags.ForceAssignments);
var projected = builder.BuildSqlExpression(
collection,
new ContextRefExpression(collection.ElementType, collection),
buildInfo.GetFlags(),
buildFlags: ExpressionBuilder.BuildFlags.ForceAssignments);

var expanded = builder.MakeExpression(sequence, new ContextRefExpression(collection.ElementType, collection), ProjectFlags.ExtractProjection);

Expand All @@ -100,7 +108,11 @@ protected override BuildSequenceResult BuildMethodCall(ExpressionBuilder builder
{
var collectionSelectContext = new SelectContext(buildInfo.Parent, builder, null, expanded, collection.SelectQuery, buildInfo.IsSubQuery);

collection = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(sequence, collectionSelectContext, collection, collectionDefaultIfEmptyContext.DefaultValue,
collection = new DefaultIfEmptyBuilder.DefaultIfEmptyContext(
sequence,
collectionSelectContext,
collection,
defaultValue: collectionDefaultIfEmptyContext.DefaultValue,
allowNullField: joinType is not (JoinType.Right or JoinType.RightApply or JoinType.Full or JoinType.FullApply),
isNullValidationDisabled: false);
}
Expand Down
6 changes: 1 addition & 5 deletions Source/LinqToDB/SqlProvider/BasicSqlOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,11 +1867,6 @@ protected IQueryElement OptimizeQueries(IQueryElement startFrom, IQueryElement r
#if DEBUG
// ReSharper disable once NotAccessedVariable

if (startFrom is SqlSelectStatement)
{

}

var sqlText = startFrom.DebugText;

if (startFrom is SqlStatement statementBefore)
Expand All @@ -1887,6 +1882,7 @@ protected IQueryElement OptimizeQueries(IQueryElement startFrom, IQueryElement r
if (startFrom is SqlStatement statementAfter)
QueryHelper.DebugCheckNesting(statementAfter, false);
#endif

return result;
}

Expand Down
11 changes: 0 additions & 11 deletions Source/LinqToDB/SqlQuery/QueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,6 @@ static bool IsDependsOn(IQueryElement testedRoot, IQueryElement onElement, HashS
var nullability = (SqlNullabilityExpression)expr;
return GetColumnDescriptor(nullability.SqlExpression);
}
case QueryElementType.SqlFunction:
{
var function = (SqlFunction)expr;

//TODO: probably remove, we have SqlCoalesceExpression
if (function is { Name: "Coalesce", Parameters.Length: 2 })
{
return GetColumnDescriptor(function.Parameters[0]);
}
break;
}
case QueryElementType.SqlCoalesce:
{
var coalesce = (SqlCoalesceExpression)expr;
Expand Down

0 comments on commit 67cdeb4

Please sign in to comment.