I recently start learning Calcite(1.8.0) and i have following use case:
i need to transform a list of strings like "SUM(x) / SUM(y) as myRatio
", "MAX(z) as maxZ
" or "CASE WHEN …
” to AggCall
s and use it in RelBuilder
later.
So seems like i should do following: convert string -> sql node -> rel node. I converted String to SqlNode using SqlParser.parseExpression
and tried Planner.rel(sqlNode)
to convert SqlNode to RelNode but no luck. Also tried to create SqlToRelConverter
myself, but cannot find out how to create validator, catalog reader and cluster from config created with just JdbcSchema.
It's a bit tricky, because you're dealing with calls to aggregate functions, and these are not ordinary expressions. Note that an AggCall
, used by RelBuilder
is neither a SqlNode
nor a RexNode
.
So, the simplest thing is probably to convert your expression string into a valid query string. E.g. "SUM(x) / SUM(y) as myRatio"
" becomes "SELECT SUM(x) / SUM(y) as myRatio FROM (VALUES (0, 0)) AS t(x, y)
". Then parse this and convert it to RelNode
. PlannerTest.testParseAndConvert
does exactly this; it uses only a few lines of code, so you could copy-paste from that method.