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

Refactored DefaultIfEmptyContext usage. #4554

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed nullability issue when optimizer visor in Transform Mode.
  • Loading branch information
sdanyliv committed Jul 2, 2024
commit 177eae6a14c35a6e45b1c02a2110496dbbc7c659
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ protected override IQueryElement VisitSqlQuery(SelectQuery selectQuery)

var result = base.VisitSqlQuery(selectQuery);

if (!ReferenceEquals(result, selectQuery))
{
_nullabilityContext.RegisterReplacement(selectQuery, (SelectQuery)result);
}

_isInsideNot = saveInsideNot;

return result;
Expand Down
65 changes: 59 additions & 6 deletions Source/LinqToDB/SqlQuery/NullabilityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public NullabilityContext(SelectQuery inQuery) : this(inQuery, null)
return _nullabilityCache.IsNullableSource(query, source);
}

public void RegisterReplacement(SelectQuery oldQuery, SelectQuery newQuery)
{
_nullabilityCache ??= new();

_nullabilityCache.RegisterReplacement(oldQuery, newQuery);
}

/// <summary>
/// Returns wether expression could contain null values or not.
/// </summary>
Expand Down Expand Up @@ -96,8 +103,9 @@ sealed class NullabilityCache
[DebuggerDisplay("Q[{InQuery.SourceID}] -> TS[{Source.SourceID}]")]
record struct NullabilityKey(SelectQuery InQuery, ISqlTableSource Source);

Dictionary<NullabilityKey, bool>? _nullableSources;
HashSet<SelectQuery>? _processedQueries;
Dictionary<NullabilityKey, bool>? _nullableSources;
HashSet<SelectQuery>? _processedQueries;
Dictionary<SelectQuery, SelectQuery>? _replacements;

/// <summary>
/// Returns nullability status of <paramref name="source"/> in specific <paramref name="inQuery"/>.
Expand All @@ -111,17 +119,62 @@ record struct NullabilityKey(SelectQuery InQuery, ISqlTableSource Source);
/// </returns>
public bool? IsNullableSource(SelectQuery inQuery, ISqlTableSource source)
{
_nullableSources ??= new();
EnsureInitialized(inQuery);

if (_nullableSources!.TryGetValue(new(inQuery, source), out var isNullable))
{
return isNullable;
}

if (_replacements != null && source is SelectQuery sourceQuery)
{
var oldSource = GetReplacement(sourceQuery) ?? source;
var oldInQuery = GetReplacement(inQuery) ?? inQuery;

if (!ReferenceEquals(oldSource, source) || !ReferenceEquals(oldInQuery, inQuery))
{
if (_nullableSources!.TryGetValue(new(oldInQuery, oldSource), out isNullable))
{
return isNullable;
}
}
}

return null;
}

void EnsureInitialized(SelectQuery inQuery)
{
_nullableSources ??= new();
_processedQueries ??= new HashSet<SelectQuery>();

ProcessQuery(new Stack<SelectQuery>(), inQuery);
}

public void RegisterReplacement(SelectQuery oldQuery, SelectQuery newQuery)
{
_replacements ??= new();

if (_nullableSources.TryGetValue(new(inQuery, source), out var isNullable))
_replacements[newQuery] = oldQuery;
}

public SelectQuery? GetReplacement(SelectQuery newQuery)
{
if (_replacements == null)
return null;

if (!_replacements.TryGetValue(newQuery, out var oldQuery))
return null;

while (true)
{
return isNullable;
if (!_replacements.TryGetValue(oldQuery, out var foundOldQuery))
break;

oldQuery = foundOldQuery;
}

return null;
return oldQuery;
}

/// <summary>
Expand Down