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

fix(query) Corner case for Set Operator OR with on() #1689

Merged
merged 3 commits into from
Dec 1, 2023
Merged
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
22 changes: 14 additions & 8 deletions query/src/main/scala/filodb/query/exec/SetOperatorExec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,33 @@ final case class SetOperatorExec(queryContext: QueryContext,
}
IteratorBackedRangeVector(x.key, rowsCursor, x.outputRange)
})
// This order is important, when lhs Rvs are executed they set a bitmap which is used when iterating RHS
// DONOT flip this order
val mergedRvs = lhsRvs ++ rhsRvs

val lhsRvMap = lhsRvs.foldLeft(Map.empty[Map[Utf8Str, Utf8Str], List[RangeVector]]) {
case (acc, rv) =>
val key = rv.key.labelValues
val groupedRvs = acc.get(key).map(rv :: _).getOrElse(List(rv))
acc + (key -> groupedRvs)
}

// Dedupe the LHS and RHS rvs by keys, if same key is found for multiple RVs, stitch them
// For e.g sum(foo{}) OR sum(bar{}), both have empty RV Key, we want them to return one RV
// instead of two both with empty RV Keys
val mergedGroupedRvs = mergedRvs.foldLeft(Map.empty[Map[Utf8Str, Utf8Str], List[RangeVector]]){
val mergedGroupedRvs = rhsRvs.foldLeft(lhsRvMap){
case (acc, rv) =>
val key = rv.key.labelValues
val groupedRvs = acc.get(key).map(rv :: _).getOrElse(List(rv))
acc + (key -> groupedRvs)
acc.get(key) match {
case Some(lhsRvs) => acc + (key -> (rv :: lhsRvs))
case None => acc
}
}

mergedGroupedRvs.map {
case (_, rv :: Nil) => rv
case (key, rvs) => IteratorBackedRangeVector(CustomRangeVectorKey(key),
StitchRvsExec.merge(rvs.reverse.map(_.rows()), outputRvRange),
outputRvRange)
}
}.toSeq
}.toSeq ++ rhsRvs.filter(rv => !mergedGroupedRvs.contains(rv.key.labelValues))
}
}


Expand Down
Loading