-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Quin Lynch
committed
May 27, 2024
1 parent
d625ba7
commit a75fd35
Showing
16 changed files
with
266 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/EdgeDB.Net.QueryBuilder/Extensions/IEnumerableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace EdgeDB; | ||
|
||
public static class EnumerableExtensions | ||
{ | ||
public static Dictionary<T, LinkedList<U>> ToBucketedDictionary<T, U, V>(this IEnumerable<V> collection, | ||
Func<V, T> selectKey, Func<V, U> selectValue) | ||
where T: notnull | ||
{ | ||
var dict = new Dictionary<T, LinkedList<U>>(); | ||
|
||
foreach (var item in collection) | ||
{ | ||
var key = selectKey(item); | ||
var value = selectValue(item); | ||
|
||
if (!dict.TryGetValue(key, out var bucket)) | ||
dict[key] = bucket = new(); | ||
|
||
bucket.AddLast(value); | ||
} | ||
|
||
return dict; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/EdgeDB.Net.QueryBuilder/Grammar/Reducers/SelectShapeReducer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using EdgeDB.QueryNodes; | ||
|
||
namespace EdgeDB; | ||
|
||
internal sealed class SelectShapeReducer : IReducer | ||
{ | ||
public void Reduce(IQueryBuilder builder, QueryWriter writer) | ||
{ | ||
// return early if theres no query nodes | ||
if (!writer.Markers.MarkersByType.TryGetValue(MarkerType.QueryNode, out var selects)) | ||
return; | ||
|
||
foreach (var select in selects.Where(x => x.Metadata is QueryNodeMetadata {Node: SelectNode})) | ||
{ | ||
var shape = writer.Markers.GetDirectChildrenOfType(select, MarkerType.Shape).FirstOrDefault(); | ||
|
||
if (shape is null) | ||
continue; | ||
|
||
var parents = writer.Markers.GetParents(select).ToBucketedDictionary(x => x.Type, x => x); | ||
|
||
// shapes are non-persistent in with statements | ||
if (parents.TryGetValue(MarkerType.GlobalDeclaration, out _)) | ||
RemoveShape(writer, shape); | ||
// shapes are not used in functions that don't return the provided input | ||
else if (parents.TryGetValue(MarkerType.Function, out var functions)) | ||
{ | ||
// if the function contains no args, return early | ||
if (!parents.TryGetValue(MarkerType.FunctionArg, out var argMarkers)) | ||
continue; | ||
|
||
foreach (var function in functions) | ||
{ | ||
// pull the argument marker that represents our query node | ||
var ourArgument = argMarkers.MinBy(x => x.SizeDistance(function)); | ||
|
||
if (ourArgument?.Metadata is not FunctionArgumentMetadata argumentMetadata || | ||
function.Metadata is not FunctionMetadata functionMetadata) | ||
continue; | ||
|
||
// get all the arguments of the function | ||
var args = writer.Markers.GetDirectChildrenOfType(function, MarkerType.FunctionArg).ToList(); | ||
|
||
// resolve the method info for the function | ||
if (!functionMetadata.TryResolveExactFunctionInfo(args, out var methodInfo)) | ||
continue; | ||
|
||
// remove the shape if the return type of the function doesn't include the result of the select | ||
if (!methodInfo.ReturnType.References(methodInfo.GetParameters()[argumentMetadata.Index] | ||
.ParameterType)) | ||
RemoveShape(writer, shape); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void RemoveShape(QueryWriter writer, Marker marker) | ||
{ | ||
// remove whitespace around the shape | ||
WhitespaceReducer.TrimWhitespaceAround(writer, marker); | ||
|
||
marker.Remove(); | ||
marker.Kill(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.