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

plan: distribute more binary joins #472

Merged
merged 1 commit into from
Aug 5, 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
28 changes: 23 additions & 5 deletions logicalplan/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package logicalplan
import (
"fmt"
"math"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -493,7 +494,7 @@ func isDistributive(expr *Node, skipBinaryPushdown bool, engineLabels map[string
case Deduplicate, RemoteExecution:
return false
case *Binary:
return isBinaryExpressionWithOneScalarSide(e) || (!skipBinaryPushdown && isBinaryExpressionWithDistributableMatching(e))
return isBinaryExpressionWithOneScalarSide(e) || (!skipBinaryPushdown && isBinaryExpressionWithDistributableMatching(e, engineLabels))
case *Aggregation:
// Certain aggregations are currently not supported.
if _, ok := distributiveAggregations[e.Op]; !ok {
Expand All @@ -518,14 +519,31 @@ func isBinaryExpressionWithOneScalarSide(expr *Binary) bool {
return lhsConstant || rhsConstant
}

func isBinaryExpressionWithDistributableMatching(expr *Binary) bool {
func isBinaryExpressionWithDistributableMatching(expr *Binary, engineLabels map[string]struct{}) bool {
if expr.VectorMatching == nil {
return false
}
// TODO: think about "or" but for safety we dont push it down for now.
if expr.Op == parser.LOR {
return false
}

// we can distribute if the vector matching contains the external labels so that
// all potential matching partners are contained in one engine
return !expr.VectorMatching.On && len(expr.VectorMatching.MatchingLabels) == 0
if expr.VectorMatching.On {
// on (...) - if ... contains all partition labels we can distribute
for lbl := range engineLabels {
if !slices.Contains(expr.VectorMatching.MatchingLabels, lbl) {
return false
}
}
return true
}
// ignoring (...) - if ... does contain any engine labels we cannot distribute
for lbl := range engineLabels {
if slices.Contains(expr.VectorMatching.MatchingLabels, lbl) {
return false
}
}
return true
}

// matchesExternalLabels returns false if given matchers are not matching external labels.
Expand Down
21 changes: 20 additions & 1 deletion logicalplan/distribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/efficientgo/core/testutil"
"github.com/prometheus/prometheus/model/labels"

"github.com/prometheus/prometheus/promql/parser"

"github.com/thanos-io/promql-engine/api"
Expand Down Expand Up @@ -363,6 +362,26 @@ sum_over_time(max(dedup(
expr: `sum by (pod) (rate(http_requests_total{region="south"}[2m]))`,
expected: `sum by (pod) (dedup(remote(sum by (pod, region) (rate(http_requests_total{region="south"}[2m])))))`,
},
{
name: "binary matching where hash contains partitioning label with on",
expr: `X * on (region) Y`,
expected: `dedup(remote(X * on (region) Y), remote(X * on (region) Y))`,
},
{
name: "binary matching where hash contains partitioning label with ignoring",
expr: `X * ignoring (foo) Y`,
expected: `dedup(remote(X * ignoring (foo) Y), remote(X * ignoring (foo) Y))`,
},
{
name: "binary matching where hash doesnt contain partitioning label with ignoring",
expr: `X * ignoring (region) Y`,
expected: `dedup(remote(X), remote(X)) * ignoring (region) dedup(remote(Y), remote(Y))`,
},
{
name: "binary matching where hash doesnt contain partitioning label with on",
expr: `X * on (foo) Y`,
expected: `dedup(remote(X), remote(X)) * on (foo) dedup(remote(Y), remote(Y))`,
},
}

engines := []api.RemoteEngine{
Expand Down
Loading