Search in sources :

Example 46 with DruidExpression

use of org.apache.druid.sql.calcite.expression.DruidExpression in project druid by druid-io.

the class ContainsOperatorConversion method toDruidFilter.

@Nullable
@Override
public DimFilter toDruidFilter(PlannerContext plannerContext, RowSignature rowSignature, @Nullable VirtualColumnRegistry virtualColumnRegistry, RexNode rexNode) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final DruidExpression druidExpression = Expressions.toDruidExpression(plannerContext, rowSignature, operands.get(0));
    if (druidExpression == null) {
        return null;
    }
    final String search = RexLiteral.stringValue(operands.get(1));
    final SearchQuerySpec spec = new ContainsSearchQuerySpec(search, caseSensitive);
    if (druidExpression.isSimpleExtraction()) {
        return new SearchQueryDimFilter(druidExpression.getSimpleExtraction().getColumn(), spec, druidExpression.getSimpleExtraction().getExtractionFn(), null);
    } else if (virtualColumnRegistry != null) {
        String v = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(druidExpression, operands.get(0).getType());
        return new SearchQueryDimFilter(v, spec, null, null);
    } else {
        return null;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) SearchQuerySpec(org.apache.druid.query.search.SearchQuerySpec) ContainsSearchQuerySpec(org.apache.druid.query.search.ContainsSearchQuerySpec) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) ContainsSearchQuerySpec(org.apache.druid.query.search.ContainsSearchQuerySpec) SearchQueryDimFilter(org.apache.druid.query.filter.SearchQueryDimFilter) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 47 with DruidExpression

use of org.apache.druid.sql.calcite.expression.DruidExpression in project druid by druid-io.

the class ExtractOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    // EXTRACT(timeUnit FROM arg)
    final RexCall call = (RexCall) rexNode;
    final RexLiteral flag = (RexLiteral) call.getOperands().get(0);
    final TimeUnitRange calciteUnit = (TimeUnitRange) flag.getValue();
    final RexNode arg = call.getOperands().get(1);
    final DruidExpression input = Expressions.toDruidExpression(plannerContext, rowSignature, arg);
    if (input == null) {
        return null;
    }
    final TimestampExtractExprMacro.Unit druidUnit = EXTRACT_UNIT_MAP.get(calciteUnit);
    if (druidUnit == null) {
        // Don't know how to extract this time unit.
        return null;
    }
    return TimeExtractOperatorConversion.applyTimeExtract(input, druidUnit, plannerContext.getTimeZone());
}
Also used : RexCall(org.apache.calcite.rex.RexCall) RexLiteral(org.apache.calcite.rex.RexLiteral) TimestampExtractExprMacro(org.apache.druid.query.expression.TimestampExtractExprMacro) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) RexNode(org.apache.calcite.rex.RexNode)

Example 48 with DruidExpression

use of org.apache.druid.sql.calcite.expression.DruidExpression in project druid by druid-io.

the class QueryLookupOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    return OperatorConversions.convertDirectCallWithExtraction(plannerContext, rowSignature, rexNode, StringUtils.toLowerCase(calciteOperator().getName()), inputExpressions -> {
        final DruidExpression arg = inputExpressions.get(0);
        final Expr lookupNameExpr = inputExpressions.get(1).parse(plannerContext.getExprMacroTable());
        if (arg.isSimpleExtraction() && lookupNameExpr.isLiteral()) {
            return arg.getSimpleExtraction().cascade(new RegisteredLookupExtractionFn(lookupExtractorFactoryContainerProvider, (String) lookupNameExpr.getLiteralValue(), false, null, null, true));
        } else {
            return null;
        }
    });
}
Also used : DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) Expr(org.apache.druid.math.expr.Expr) RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn)

Example 49 with DruidExpression

use of org.apache.druid.sql.calcite.expression.DruidExpression in project druid by druid-io.

the class LeftOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final DruidExpression input = Expressions.toDruidExpression(plannerContext, rowSignature, call.getOperands().get(0));
    if (input == null) {
        return null;
    }
    if (call.getOperands().size() != 2) {
        return null;
    }
    return OperatorConversions.convertDirectCall(plannerContext, rowSignature, rexNode, "left");
}
Also used : RexCall(org.apache.calcite.rex.RexCall) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression)

Example 50 with DruidExpression

use of org.apache.druid.sql.calcite.expression.DruidExpression in project druid by druid-io.

the class CastOperatorConversion method toDruidExpression.

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode) {
    final RexNode operand = ((RexCall) rexNode).getOperands().get(0);
    final DruidExpression operandExpression = Expressions.toDruidExpression(plannerContext, rowSignature, operand);
    if (operandExpression == null) {
        return null;
    }
    final SqlTypeName fromType = operand.getType().getSqlTypeName();
    final SqlTypeName toType = rexNode.getType().getSqlTypeName();
    if (SqlTypeName.CHAR_TYPES.contains(fromType) && SqlTypeName.DATETIME_TYPES.contains(toType)) {
        return castCharToDateTime(plannerContext, operandExpression, toType, Calcites.getColumnTypeForRelDataType(rexNode.getType()));
    } else if (SqlTypeName.DATETIME_TYPES.contains(fromType) && SqlTypeName.CHAR_TYPES.contains(toType)) {
        return castDateTimeToChar(plannerContext, operandExpression, fromType, Calcites.getColumnTypeForRelDataType(rexNode.getType()));
    } else {
        // Handle other casts.
        final ExprType fromExprType = EXPRESSION_TYPES.get(fromType);
        final ExprType toExprType = EXPRESSION_TYPES.get(toType);
        if (fromExprType == null || toExprType == null) {
            // We have no runtime type for these SQL types.
            return null;
        }
        final DruidExpression typeCastExpression;
        if (fromExprType != toExprType) {
            // Ignore casts for simple extractions (use Function.identity) since it is ok in many cases.
            typeCastExpression = operandExpression.map(Function.identity(), expression -> StringUtils.format("CAST(%s, '%s')", expression, toExprType.toString()));
        } else {
            typeCastExpression = operandExpression;
        }
        if (toType == SqlTypeName.DATE) {
            // Floor to day when casting to DATE.
            return TimeFloorOperatorConversion.applyTimestampFloor(typeCastExpression, new PeriodGranularity(Period.days(1), null, plannerContext.getTimeZone()), plannerContext.getExprMacroTable());
        } else {
            return typeCastExpression;
        }
    }
}
Also used : DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ExprType(org.apache.druid.math.expr.ExprType) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

DruidExpression (org.apache.druid.sql.calcite.expression.DruidExpression)53 RexNode (org.apache.calcite.rex.RexNode)34 Nullable (javax.annotation.Nullable)30 RexCall (org.apache.calcite.rex.RexCall)20 ColumnType (org.apache.druid.segment.column.ColumnType)15 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)14 RexLiteral (org.apache.calcite.rex.RexLiteral)12 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)12 List (java.util.List)11 RowSignature (org.apache.druid.segment.column.RowSignature)11 RelDataType (org.apache.calcite.rel.type.RelDataType)10 Expressions (org.apache.druid.sql.calcite.expression.Expressions)10 SqlKind (org.apache.calcite.sql.SqlKind)9 PlannerContext (org.apache.druid.sql.calcite.planner.PlannerContext)9 RexBuilder (org.apache.calcite.rex.RexBuilder)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)8 SqlFunctionCategory (org.apache.calcite.sql.SqlFunctionCategory)8 ExprMacroTable (org.apache.druid.math.expr.ExprMacroTable)8 Collectors (java.util.stream.Collectors)7 AggregateCall (org.apache.calcite.rel.core.AggregateCall)7