Class Expressions

java.lang.Object
org.apache.druid.sql.calcite.expression.Expressions

public class Expressions extends Object
A collection of functions for translating from Calcite expressions into Druid objects.
  • Method Details

    • fromFieldAccess

      @Deprecated public static org.apache.calcite.rex.RexNode fromFieldAccess(RowSignature rowSignature, @Nullable org.apache.calcite.rel.core.Project project, int fieldNumber)
      Deprecated.
      Old method used to translate a field access, possibly through a projection, to an underlying Druid dataSource. This exists to provide API compatibility to extensions, but is deprecated because there is a 4 argument version that should be used instead. Call sites should have access to a RexBuilder instance that they can get the typeFactory from.
      Parameters:
      rowSignature - row signature of underlying Druid dataSource
      project - projection, or null
      fieldNumber - number of the field to access
      Returns:
      row expression
    • fromFieldAccess

      public static org.apache.calcite.rex.RexNode fromFieldAccess(org.apache.calcite.rel.type.RelDataTypeFactory typeFactory, RowSignature rowSignature, @Nullable org.apache.calcite.rel.core.Project project, int fieldNumber)
      Translate a field access, possibly through a projection, to an underlying Druid dataSource.
      Parameters:
      typeFactory - factory for creating SQL types
      rowSignature - row signature of underlying Druid dataSource
      project - projection, or null
      fieldNumber - number of the field to access
      Returns:
      row expression
    • toDruidExpressions

      @Nullable public static List<DruidExpression> toDruidExpressions(PlannerContext plannerContext, RowSignature rowSignature, List<org.apache.calcite.rex.RexNode> rexNodes)
      Translate a list of Calcite RexNode to Druid expressions.
      Parameters:
      plannerContext - SQL planner context
      rowSignature - signature of the rows to be extracted from
      rexNodes - list of Calcite expressions meant to be applied on top of the rows
      Returns:
      list of Druid expressions in the same order as rexNodes, or null if not possible. If a non-null list is returned, all elements will be non-null.
    • toDruidExpressionsWithPostAggOperands

      @Nullable public static List<DruidExpression> toDruidExpressionsWithPostAggOperands(PlannerContext plannerContext, RowSignature rowSignature, List<org.apache.calcite.rex.RexNode> rexNodes, PostAggregatorVisitor postAggregatorVisitor)
      Translate a list of Calcite RexNode to Druid expressions, with the possibility of having postagg operands.
      Parameters:
      plannerContext - SQL planner context
      rowSignature - signature of the rows to be extracted from
      rexNodes - list of Calcite expressions meant to be applied on top of the rows
      postAggregatorVisitor - visitor that manages postagg names and tracks postaggs that were created as by the translation
      Returns:
      list of Druid expressions in the same order as rexNodes, or null if not possible. If a non-null list is returned, all elements will be non-null.
    • toDruidExpression

      @Nullable public static DruidExpression toDruidExpression(PlannerContext plannerContext, RowSignature rowSignature, org.apache.calcite.rex.RexNode rexNode)
      Translate a Calcite RexNode to a Druid expression for projections or the aggregators that don't require numeric inputs. Consider using Aggregations.toDruidExpressionForNumericAggregator(org.apache.druid.sql.calcite.planner.PlannerContext, org.apache.druid.segment.column.RowSignature, org.apache.calcite.rex.RexNode) for the aggregators that require numeric inputs.
      Parameters:
      plannerContext - SQL planner context
      rowSignature - signature of the rows to be extracted from
      rexNode - expression meant to be applied on top of the rows
      Returns:
      DruidExpression referring to fields in rowOrder, or null if not possible to translate
    • toDruidExpressionWithPostAggOperands

      @Nullable public static DruidExpression toDruidExpressionWithPostAggOperands(PlannerContext plannerContext, RowSignature rowSignature, org.apache.calcite.rex.RexNode rexNode, @Nullable PostAggregatorVisitor postAggregatorVisitor)
    • calciteLiteralToDruidLiteral

      @Nullable public static DruidLiteral calciteLiteralToDruidLiteral(PlannerContext plannerContext, org.apache.calcite.rex.RexNode rexNode)
      Create a DruidLiteral from a literal RexNode. Necessary because Calcite represents literals using different Java classes than Druid does.
      Parameters:
      plannerContext - planner context
      rexNode - Calcite literal
      Returns:
      converted literal, or null if the literal cannot be converted
    • toFilter

      @Nullable public static DimFilter toFilter(PlannerContext plannerContext, RowSignature rowSignature, @Nullable VirtualColumnRegistry virtualColumnRegistry, org.apache.calcite.rex.RexNode expression)
      Translates "condition" to a Druid filter, or returns null if we cannot translate the condition.
      Parameters:
      plannerContext - planner context
      rowSignature - input row signature
      virtualColumnRegistry - re-usable virtual column references, may be null if virtual columns aren't allowed
      expression - Calcite row expression
    • toQueryGranularity

      @Nullable public static Granularity toQueryGranularity(DruidExpression expression, ExpressionParser parser)
      Converts an expression to a Granularity, if possible. This is possible if, and only if, the expression is a timestamp_floor function on the __time column with literal parameters for period, origin, and timeZone.
      Returns:
      granularity or null if not possible
    • asTimestampFloorExpr

      @Nullable public static TimestampFloorExprMacro.TimestampFloorExpr asTimestampFloorExpr(DruidExpression expression, ExpressionParser parser)