Skip to content

Commit

Permalink
misc(query): Adding supporting function to get the logical plan tree …
Browse files Browse the repository at this point in the history
…in string form (#1894)
  • Loading branch information
sandeep6189 authored Nov 28, 2024
1 parent 0456eca commit 186e69c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,17 @@ object LogicalPlanUtils extends StrictLogging {
.distinct
.map(_.toSeq)
}

def getLogicalPlanTreeStringRepresentation(logicalPlan: LogicalPlan) : String = {
// iterate over all the children of the logical plan and get the string representation
val children = logicalPlan match {
case nl: NonLeafLogicalPlan => nl.children.map(getLogicalPlanTreeStringRepresentation)
case _ => Seq()
}
if (children.isEmpty) {
logicalPlan.getClass.getSimpleName
} else {
s"${logicalPlan.getClass.getSimpleName}(${children.mkString(",")})"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,14 @@ class LogicalPlanUtilsSpec extends AnyFunSpec with Matchers {
getMaxLookbackInMillis(query1) shouldEqual 900000 // max of 15 and 10m
getMaxLookbackInMillis(query2) shouldEqual 300000 // default lookback is 5m
}

it ("getLogicalPlanTreeStringRepresentation should return result as expected") {
val timeParamsSec = TimeStepParams(1000, 10, 10000)
val query1 = """rate(test_metric{_ns_="test-ns", _ws_="test-ns", cluster="test1"}[15m]) + rate(test_metric{_ns_="test-ns", _ws_="test-ns", cluster="test1"}[10m])"""
val query2 = """test_metric{_ns_="test-ns", _ws_="test-ns", cluster="test1"} offset 1d * 1000"""
val lp = Parser.queryRangeToLogicalPlan(query1, timeParamsSec)
LogicalPlanUtils.getLogicalPlanTreeStringRepresentation(lp) shouldEqual "BinaryJoin(PeriodicSeriesWithWindowing(RawSeries),PeriodicSeriesWithWindowing(RawSeries))"
val lp2 = Parser.queryToLogicalPlan(query2, 100, 10)
LogicalPlanUtils.getLogicalPlanTreeStringRepresentation(lp2) shouldEqual "ScalarVectorBinaryOperation(PeriodicSeries(RawSeries),ScalarFixedDoublePlan)"
}
}

0 comments on commit 186e69c

Please sign in to comment.