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

Optimize single traversal coalesce #1758

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 14 additions & 5 deletions src/Core/Queries/GremlinQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,20 @@ private TReturnQuery Coalesce<TTargetQuery, TReturnQuery>(params Func<GremlinQue

if (!traversals.All(static traversal => traversal.IsIdentity()))
{
builder = builder
.AddStep(new CoalesceStep(traversals
.ToImmutableArray()))
.WithNewProjection(traversals
.LowestProjection());
if (traversals is [var singleTraversal])
{
builder = builder
.AddSteps(singleTraversal)
.WithNewProjection(singleTraversal.Projection);
}
else
{
builder = builder
.AddStep(new CoalesceStep(traversals
.ToImmutableArray()))
.WithNewProjection(traversals
.LowestProjection());
}
}

return builder
Expand Down
8 changes: 4 additions & 4 deletions src/Core/Queries/GremlinQuery.tt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace ExRam.Gremlinq.Core
IMapGremlinQuery<(<#= GetArgumentList("TItem{0}", i) #>)> IMapGremlinQueryBase<T1>.Select<<#= GetArgumentList("TItem{0}", i) #>>(<#= GetArgumentList("Expression<Func<T1, TItem{0}>> projection{0}", i) #>) => Select<IMapGremlinQuery<(<#= GetArgumentList("TItem{0}", i) #>)>>(<#= GetArgumentList("projection{0}", i) #>);
<# } #>

<# foreach(var iface in Untyped(substitutedBaseInterfaces)){ #>
<# foreach (var iface in Untyped(substitutedBaseInterfaces)){ #>
<#= ChangeType(iface, "TResult").Replace("Base", "") #> <#= iface #>.Cast<TResult>() => CloneAs<<#= ChangeType(iface, "TResult").Replace("Base", "") #>>();
<# } #>

<# foreach(var iface in Typed(substitutedBaseInterfaces))
<# foreach (var iface in Typed(substitutedBaseInterfaces))
{ #>
TTargetQuery IGremlinQueryBaseRec<T1, <#= iface #>>.Aggregate<TTargetQuery>(Func<<#= iface #>, StepLabel<IArrayGremlinQuery<T1[], T1, <#= iface #>>, T1[]>, TTargetQuery> continuation) => Aggregate(Scope.Global, continuation);
TTargetQuery IGremlinQueryBaseRec<T1, <#= iface #>>.AggregateLocal<TTargetQuery>(Func<<#= iface #>, StepLabel<IArrayGremlinQuery<T1[], T1, <#= iface #>>, T1[]>, TTargetQuery> continuation) => Aggregate(Scope.Local, continuation);
Expand Down Expand Up @@ -126,12 +126,12 @@ namespace ExRam.Gremlinq.Core
<# } #>


<# foreach(var iface in Untyped(EdgeOrVertex(substitutedBaseInterfaces))){ #>
<# foreach (var iface in Untyped(EdgeOrVertex(substitutedBaseInterfaces))){ #>
<#= ChangeType(iface, "TTarget").Replace("Base", "") #> <#= iface #>.OfType<TTarget>() => OfType<TTarget, <#= ChangeType(iface, "TTarget").Replace("Base", "") #>>(<#= iface.Contains("VertexGremlinQuery") ? "Environment.Model.VerticesModel" : "Environment.Model.EdgesModel" #>);
<# } #>


<# foreach(var iface in Typed(Element(substitutedBaseInterfaces))){ #>
<# foreach (var iface in Typed(Element(substitutedBaseInterfaces))){ #>
<#= iface #> IElementGremlinQueryBaseRec<<#= iface #>>.Property(string key, object? value) => Property(key, value);
<#= iface #> IElementGremlinQueryBaseRec<<#= iface #>>.Property(string key, Func<<#= iface #>, IGremlinQueryBase> valueTransformation) => Property(key, valueTransformation);

Expand Down
6 changes: 3 additions & 3 deletions src/Providers.Core/Extensions/GremlinqClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IAsyncEnumerable<ResponseMessage<TResult>> SubmitAsync<TResult>(RequestMe

static async IAsyncEnumerable<ResponseMessage<TResult>> Core(RequestMessage requestMessage, RequestInterceptingGremlinqClient @this, [EnumeratorCancellation] CancellationToken ct = default)
{
await foreach(var item in @this._baseClient.SubmitAsync<TResult>(await @this._transformation(requestMessage, ct)).WithCancellation(ct))
await foreach (var item in @this._baseClient.SubmitAsync<TResult>(await @this._transformation(requestMessage, ct)).WithCancellation(ct))
{
yield return item;
}
Expand Down Expand Up @@ -83,9 +83,9 @@ public LoggingGremlinqClient(IGremlinqClient client, IGremlinQueryEnvironment en

public IAsyncEnumerable<ResponseMessage<TResult>> SubmitAsync<TResult>(RequestMessage requestMessage)
{
return LoggingCore(requestMessage, this);
return Core(requestMessage, this);

static async IAsyncEnumerable<ResponseMessage<TResult>> LoggingCore(RequestMessage requestMessage, LoggingGremlinqClient @this, [EnumeratorCancellation] CancellationToken ct = default)
static async IAsyncEnumerable<ResponseMessage<TResult>> Core(RequestMessage requestMessage, LoggingGremlinqClient @this, [EnumeratorCancellation] CancellationToken ct = default)
{
var enumerable = @this._client
.SubmitAsync<TResult>(requestMessage);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().coalesce(__.out()).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().out().project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!application/vnd.gremlin-v2.0+json{"requestId":"12345678-9012-3456-7890-123456789012","op":"bytecode","processor":"traversal","args":{"gremlin":{"@type":"g:Bytecode","@value":{"step":[["V"],["coalesce",{"@type":"g:Bytecode","@value":{"step":[["out"]]}}],["project","id","label","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["properties"],["group"],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["project","id","label","value","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:T","@value":"value"}],["by",{"@type":"g:Bytecode","@value":{"step":[["valueMap"]]}}],["fold"]]}}]]}}]]}},"aliases":{"g":"g"}}}
!application/vnd.gremlin-v2.0+json{"requestId":"12345678-9012-3456-7890-123456789012","op":"bytecode","processor":"traversal","args":{"gremlin":{"@type":"g:Bytecode","@value":{"step":[["V"],["out"],["project","id","label","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["properties"],["group"],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["project","id","label","value","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:T","@value":"value"}],["by",{"@type":"g:Bytecode","@value":{"step":[["valueMap"]]}}],["fold"]]}}]]}}]]}},"aliases":{"g":"g"}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!application/vnd.gremlin-v3.0+json{"requestId":"12345678-9012-3456-7890-123456789012","op":"bytecode","processor":"traversal","args":{"gremlin":{"@type":"g:Bytecode","@value":{"step":[["V"],["coalesce",{"@type":"g:Bytecode","@value":{"step":[["out"]]}}],["project","id","label","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["properties"],["group"],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["project","id","label","value","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:T","@value":"value"}],["by",{"@type":"g:Bytecode","@value":{"step":[["valueMap"]]}}],["fold"]]}}]]}}]]}},"aliases":{"g":"g"}}}
!application/vnd.gremlin-v3.0+json{"requestId":"12345678-9012-3456-7890-123456789012","op":"bytecode","processor":"traversal","args":{"gremlin":{"@type":"g:Bytecode","@value":{"step":[["V"],["out"],["project","id","label","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["properties"],["group"],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:Bytecode","@value":{"step":[["project","id","label","value","properties"],["by",{"@type":"g:T","@value":"id"}],["by",{"@type":"g:T","@value":"label"}],["by",{"@type":"g:T","@value":"value"}],["by",{"@type":"g:Bytecode","@value":{"step":[["valueMap"]]}}],["fold"]]}}]]}}]]}},"aliases":{"g":"g"}}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
Script: g.V().coalesce(__.out()).project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold())),
Script: g.V().out().project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold())),
Bindings: {
_a: id,
_b: label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
_c: properties,
_d: value
},
gremlin: g.V().coalesce(__.out()).project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold()))
gremlin: g.V().out().project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold()))
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().coalesce(__.out())
g.V().out()
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
aliases: {
g: g
},
gremlin: g.V().coalesce(__.out())
gremlin: g.V().out()
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
Script: g.V().coalesce(__.out())
Script: g.V().out()
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
g.V().coalesce(__.out()).project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
g.V().out().project('id','label','properties').by(id).by(label).by(__.properties().group().by(label).by(__.project('id','label','value','properties').by(id).by(label).by(value).by(__.valueMap()).fold()))
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
log: {
Level: Debug,
Category: LoggingFixture,
Message: Executing Gremlin query 12345678-9012-3456-7890-123456789012 with groovy script g.V().coalesce(__.out()).project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold())).,
Message: Executing Gremlin query 12345678-9012-3456-7890-123456789012 with groovy script g.V().out().project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold())).,
State: [
{
requestId: 12345678-9012-3456-7890-123456789012
},
{
script: g.V().coalesce(__.out()).project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold()))
script: g.V().out().project(_a,_b,_c).by(id).by(label).by(__.properties().group().by(label).by(__.project(_a,_b,_d,_c).by(id).by(label).by(value).by(__.valueMap()).fold()))
},
{
{OriginalFormat}: Executing Gremlin query {requestId} with groovy script {script}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@
OperatorName: V
},
{
OperatorName: coalesce,
Arguments: [
{
StepInstructions: [
{
OperatorName: out
}
]
}
]
OperatorName: out
},
{
OperatorName: project,
Expand Down