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;
}
}
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());
}
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;
}
});
}
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");
}
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;
}
}
}
Aggregations