use of org.apache.drill.common.expression.FunctionCall in project drill by apache.
the class WindowFrameRecordBatch method createFramers.
private void createFramers(VectorAccessible batch) throws SchemaChangeException, IOException, ClassTransformationException {
assert framers == null : "createFramer should only be called once";
logger.trace("creating framer(s)");
final List<LogicalExpression> keyExprs = Lists.newArrayList();
final List<LogicalExpression> orderExprs = Lists.newArrayList();
boolean requireFullPartition = false;
// at least one window function uses the DefaultFrameTemplate
boolean useDefaultFrame = false;
// at least one window function uses the CustomFrameTemplate
boolean useCustomFrame = false;
hasOrderBy = popConfig.getOrderings().size() > 0;
// all existing vectors will be transferred to the outgoing container in framer.doWork()
for (final VectorWrapper<?> wrapper : batch) {
container.addOrGet(wrapper.getField());
}
// add aggregation vectors to the container, and materialize corresponding expressions
for (final NamedExpression ne : popConfig.getAggregations()) {
if (!(ne.getExpr() instanceof FunctionCall)) {
throw UserException.functionError().message("Unsupported window function '%s'", ne.getExpr()).build(logger);
}
final FunctionCall call = (FunctionCall) ne.getExpr();
final WindowFunction winfun = WindowFunction.fromExpression(call);
if (winfun.materialize(ne, container, context.getFunctionRegistry())) {
functions.add(winfun);
requireFullPartition |= winfun.requiresFullPartition(popConfig);
if (winfun.supportsCustomFrames()) {
useCustomFrame = true;
} else {
useDefaultFrame = true;
}
}
}
container.buildSchema(BatchSchema.SelectionVectorMode.NONE);
container.setRecordCount(0);
// materialize partition by expressions
for (final NamedExpression ne : popConfig.getWithins()) {
keyExprs.add(ExpressionTreeMaterializer.materializeAndCheckErrors(ne.getExpr(), batch, context.getFunctionRegistry()));
}
// materialize order by expressions
for (final Order.Ordering oe : popConfig.getOrderings()) {
orderExprs.add(ExpressionTreeMaterializer.materializeAndCheckErrors(oe.getExpr(), batch, context.getFunctionRegistry()));
}
// count how many framers we need
int numFramers = useDefaultFrame ? 1 : 0;
numFramers += useCustomFrame ? 1 : 0;
assert numFramers > 0 : "No framer was needed!";
framers = new WindowFramer[numFramers];
int index = 0;
if (useDefaultFrame) {
framers[index] = generateFramer(keyExprs, orderExprs, functions, false);
framers[index].setup(batches, container, oContext, requireFullPartition, popConfig);
index++;
}
if (useCustomFrame) {
framers[index] = generateFramer(keyExprs, orderExprs, functions, true);
framers[index].setup(batches, container, oContext, requireFullPartition, popConfig);
}
}
use of org.apache.drill.common.expression.FunctionCall in project drill by apache.
the class TypeInferenceUtils method convertSqlOperatorBindingToFunctionCall.
/**
* Given a SqlOperatorBinding, convert it to FunctionCall
* @param opBinding the given SqlOperatorBinding
* @return FunctionCall the converted FunctionCall
*/
public static FunctionCall convertSqlOperatorBindingToFunctionCall(final SqlOperatorBinding opBinding) {
final List<LogicalExpression> args = Lists.newArrayList();
for (int i = 0; i < opBinding.getOperandCount(); ++i) {
final RelDataType type = opBinding.getOperandType(i);
final TypeProtos.MinorType minorType = getDrillTypeFromCalciteType(type);
final TypeProtos.MajorType majorType;
if (type.isNullable()) {
majorType = Types.optional(minorType);
} else {
majorType = Types.required(minorType);
}
args.add(new MajorTypeInLogicalExpression(majorType));
}
final String drillFuncName = FunctionCallFactory.replaceOpWithFuncName(opBinding.getOperator().getName());
final FunctionCall functionCall = new FunctionCall(drillFuncName, args, ExpressionPosition.UNKNOWN);
return functionCall;
}
use of org.apache.drill.common.expression.FunctionCall in project drill by apache.
the class TypeInferenceUtils method resolveDrillFuncHolder.
private static DrillFuncHolder resolveDrillFuncHolder(final SqlOperatorBinding opBinding, final List<DrillFuncHolder> functions) {
final FunctionCall functionCall = convertSqlOperatorBindingToFunctionCall(opBinding);
final FunctionResolver functionResolver = FunctionResolverFactory.getResolver(functionCall);
final DrillFuncHolder func = functionResolver.getBestMatch(functions, functionCall);
// if no DrillFuncHolder matched for the given list of operand types
if (func == null) {
String operandTypes = "";
for (int i = 0; i < opBinding.getOperandCount(); ++i) {
operandTypes += opBinding.getOperandType(i).getSqlTypeName();
if (i < opBinding.getOperandCount() - 1) {
operandTypes += ",";
}
}
throw UserException.functionError().message(String.format("%s does not support operand types (%s)", opBinding.getOperator().getName(), operandTypes)).build(logger);
}
return func;
}
use of org.apache.drill.common.expression.FunctionCall in project drill by apache.
the class ExpressionTreeMaterializer method addCastExpression.
public static LogicalExpression addCastExpression(LogicalExpression fromExpr, MajorType toType, FunctionLookupContext functionLookupContext, ErrorCollector errorCollector, boolean exactResolver) {
String castFuncName = CastFunctions.getCastFunc(toType.getMinorType());
List<LogicalExpression> castArgs = Lists.newArrayList();
//input_expr
castArgs.add(fromExpr);
if (fromExpr.getMajorType().getMinorType() == MinorType.UNION && toType.getMinorType() == MinorType.UNION) {
return fromExpr;
}
if (!Types.isFixedWidthType(toType) && !Types.isUnion(toType)) {
/* We are implicitly casting to VARCHAR so we don't have a max length,
* using an arbitrary value. We trim down the size of the stored bytes
* to the actual size so this size doesn't really matter.
*/
castArgs.add(new ValueExpressions.LongExpression(Types.MAX_VARCHAR_LENGTH, null));
} else if (CoreDecimalUtility.isDecimalType(toType)) {
// Add the scale and precision to the arguments of the implicit cast
castArgs.add(new ValueExpressions.LongExpression(toType.getPrecision(), null));
castArgs.add(new ValueExpressions.LongExpression(toType.getScale(), null));
}
FunctionCall castCall = new FunctionCall(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
FunctionResolver resolver;
if (exactResolver) {
resolver = FunctionResolverFactory.getExactResolver(castCall);
} else {
resolver = FunctionResolverFactory.getResolver(castCall);
}
DrillFuncHolder matchedCastFuncHolder = functionLookupContext.findDrillFunction(resolver, castCall);
if (matchedCastFuncHolder == null) {
logFunctionResolutionError(errorCollector, castCall);
return NullExpression.INSTANCE;
}
return matchedCastFuncHolder.getExpr(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
}
use of org.apache.drill.common.expression.FunctionCall in project drill by apache.
the class DrillWindowRel method toDrill.
protected LogicalExpression toDrill(AggregateCall call, List<String> fn) {
List<LogicalExpression> args = Lists.newArrayList();
for (Integer i : call.getArgList()) {
args.add(new FieldReference(fn.get(i)));
}
// for count(1).
if (args.isEmpty()) {
args.add(new ValueExpressions.LongExpression(1l));
}
LogicalExpression expr = new FunctionCall(call.getAggregation().getName().toLowerCase(), args, ExpressionPosition.UNKNOWN);
return expr;
}
Aggregations