use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class DruidJoinRule method decomposeAnd.
@VisibleForTesting
static List<RexNode> decomposeAnd(final RexNode condition) {
final List<RexNode> retVal = new ArrayList<>();
final Stack<RexNode> stack = new Stack<>();
stack.push(condition);
while (!stack.empty()) {
final RexNode current = stack.pop();
if (current.isA(SqlKind.AND)) {
final List<RexNode> operands = ((RexCall) current).getOperands();
// Add right-to-left, so when we unwind the stack, the operands are in the original order.
for (int i = operands.size() - 1; i >= 0; i--) {
stack.push(operands.get(i));
}
} else {
retVal.add(current);
}
}
return retVal;
}
use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class OperatorConversions method convertCall.
@Nullable
public static DruidExpression convertCall(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final DruidExpression.DruidExpressionBuilder expressionFunction) {
final RexCall call = (RexCall) rexNode;
final List<DruidExpression> druidExpressions = Expressions.toDruidExpressions(plannerContext, rowSignature, call.getOperands());
if (druidExpressions == null) {
return null;
}
return expressionFunction.buildExpression(druidExpressions);
}
use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class OperatorConversions method convertCallWithPostAggOperands.
@Nullable
public static DruidExpression convertCallWithPostAggOperands(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final DruidExpression.DruidExpressionBuilder expressionFunction, final PostAggregatorVisitor postAggregatorVisitor) {
final RexCall call = (RexCall) rexNode;
final List<DruidExpression> druidExpressions = Expressions.toDruidExpressionsWithPostAggOperands(plannerContext, rowSignature, call.getOperands(), postAggregatorVisitor);
if (druidExpressions == null) {
return null;
}
return expressionFunction.buildExpression(druidExpressions);
}
use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class OperatorConversions method toPostAggregator.
/**
* Translate a Calcite {@code RexNode} to a Druid PostAggregator
*
* @param plannerContext SQL planner context
* @param rowSignature signature of the rows to be extracted from
* @param rexNode expression meant to be applied on top of the rows
* @param postAggregatorVisitor visitor that manages postagg names and tracks postaggs that were created
* by the translation
*
* @return rexNode referring to fields in rowOrder, or null if not possible
*/
@Nullable
public static PostAggregator toPostAggregator(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final PostAggregatorVisitor postAggregatorVisitor) {
final SqlKind kind = rexNode.getKind();
if (kind == SqlKind.INPUT_REF) {
// Translate field references.
final RexInputRef ref = (RexInputRef) rexNode;
final String columnName = rowSignature.getColumnName(ref.getIndex());
if (columnName == null) {
throw new ISE("PostAggregator referred to nonexistent index[%d]", ref.getIndex());
}
return new FieldAccessPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), columnName);
} else if (rexNode instanceof RexCall) {
final SqlOperator operator = ((RexCall) rexNode).getOperator();
final SqlOperatorConversion conversion = plannerContext.getOperatorTable().lookupOperatorConversion(operator);
if (conversion == null) {
return null;
} else {
return conversion.toPostAggregator(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
}
} else if (kind == SqlKind.LITERAL) {
return null;
} else {
throw new IAE("Unknown rexnode kind: " + kind);
}
}
use of org.apache.calcite.rex.RexCall in project druid by druid-io.
the class Expressions method rexCallToDruidExpression.
private static DruidExpression rexCallToDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final PostAggregatorVisitor postAggregatorVisitor) {
final SqlOperator operator = ((RexCall) rexNode).getOperator();
final SqlOperatorConversion conversion = plannerContext.getOperatorTable().lookupOperatorConversion(operator);
if (conversion == null) {
plannerContext.setPlanningError("SQL query requires '%s' operator that is not supported.", operator.getName());
return null;
} else {
if (postAggregatorVisitor != null) {
// try making postagg first
PostAggregator postAggregator = conversion.toPostAggregator(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
if (postAggregator != null) {
postAggregatorVisitor.addPostAgg(postAggregator);
String exprName = postAggregator.getName();
return DruidExpression.ofColumn(postAggregator.getType(rowSignature), exprName);
}
}
DruidExpression expression = conversion.toDruidExpressionWithPostAggOperands(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
return expression;
}
}
Aggregations