@Override protected ExprEval eval(ExprEval x, ExprEval y) { if (NullHandling.sqlCompatible() && x.value() == null) { return ExprEval.of(null); } ExprType castTo; try { castTo = ExprType.valueOf(StringUtils.toUpperCase(y.asString())); } catch (IllegalArgumentException e) { throw new IAE("invalid type '%s'", y.asString()); } return x.castTo(castTo); } }
/** * Returns true if a post-aggregation "expression" can be realized as a direct field access. This is true if it's * a direct column access that doesn't require an implicit cast. * * @param aggregateRowSignature signature of the aggregation * @param expression post-aggregation expression * @param rexNode RexNode for the post-aggregation expression * * @return yes or no */ private static boolean postAggregatorDirectColumnIsOk( final RowSignature aggregateRowSignature, final DruidExpression expression, final RexNode rexNode ) { if (!expression.isDirectColumnAccess()) { return false; } // Check if a cast is necessary. final ExprType toExprType = Expressions.exprTypeForValueType( aggregateRowSignature.getColumnType(expression.getDirectColumn()) ); final ExprType fromExprType = Expressions.exprTypeForValueType( Calcites.getValueTypeForSqlTypeName(rexNode.getType().getSqlTypeName()) ); return toExprType.equals(fromExprType); }
expression -> StringUtils.format("CAST(%s, '%s')", expression, toExprType.toString()) ); } else {
@Override protected ExprEval eval(ExprEval x, ExprEval y) { if (NullHandling.sqlCompatible() && x.value() == null) { return ExprEval.of(null); } ExprType castTo; try { castTo = ExprType.valueOf(StringUtils.toUpperCase(y.asString())); } catch (IllegalArgumentException e) { throw new IAE("invalid type '%s'", y.asString()); } return x.castTo(castTo); } }
/** * Returns true if a post-aggregation "expression" can be realized as a direct field access. This is true if it's * a direct column access that doesn't require an implicit cast. * * @param aggregateRowSignature signature of the aggregation * @param expression post-aggregation expression * @param rexNode RexNode for the post-aggregation expression * * @return yes or no */ private static boolean postAggregatorDirectColumnIsOk( final RowSignature aggregateRowSignature, final DruidExpression expression, final RexNode rexNode ) { if (!expression.isDirectColumnAccess()) { return false; } // Check if a cast is necessary. final ExprType toExprType = Expressions.exprTypeForValueType( aggregateRowSignature.getColumnType(expression.getDirectColumn()) ); final ExprType fromExprType = Expressions.exprTypeForValueType( Calcites.getValueTypeForSqlTypeName(rexNode.getType().getSqlTypeName()) ); return toExprType.equals(fromExprType); }
expression -> StringUtils.format("CAST(%s, '%s')", expression, toExprType.toString()) ); } else {