use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.
the class DrillDecimalSetScaleFuncHolder method getReturnType.
@Override
public MajorType getReturnType(List<LogicalExpression> args) {
TypeProtos.DataMode mode = returnValue.type.getMode();
int scale = 0;
int precision = 0;
int i = 0;
if (nullHandling == NullHandling.NULL_IF_NULL) {
// if any one of the input types is nullable, then return nullable return type
for (LogicalExpression e : args) {
precision = Math.max(precision, e.getMajorType().getPrecision());
if (e.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
mode = TypeProtos.DataMode.OPTIONAL;
}
}
/* Used by functions like round, truncate which specify the scale for
* the output as the second argument
*/
assert (args.size() == 2) && (args.get(1) instanceof ValueExpressions.IntExpression);
// Get the scale from the second argument which should be a constant
scale = ((ValueExpressions.IntExpression) args.get(1)).getInt();
}
return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(scale).setPrecision(precision).setMode(mode).build());
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.
the class DrillDecimalZeroScaleFuncHolder method getReturnType.
/* This function scope is used when we need to remove the scale part.
* trunc and round functions with single argument use this
*/
@Override
public MajorType getReturnType(List<LogicalExpression> args) {
int precision = 0;
TypeProtos.DataMode mode = returnValue.type.getMode();
if (nullHandling == NullHandling.NULL_IF_NULL) {
// if any one of the input types is nullable, then return nullable return type
for (LogicalExpression e : args) {
if (e.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
mode = TypeProtos.DataMode.OPTIONAL;
}
precision = Math.max(precision, e.getMajorType().getPrecision());
}
}
return (TypeProtos.MajorType.newBuilder().setMinorType(returnValue.type.getMinorType()).setScale(0).setPrecision(precision).setMode(mode).build());
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.
the class FunctionGenerationHelper method getOrderingComparator.
/**
* Finds ordering comparator ("compare_to...") FunctionHolderExpression with
* a specified ordering for NULL (and considering NULLS <i>equal</i>).
* @param null_high whether NULL should compare as the lowest value (if
* {@code false}) or the highest value (if {@code true})
* @param left ...
* @param right ...
* @param functionLookupContext ...
* @return
* FunctionHolderExpression containing the found function implementation
*/
public static LogicalExpression getOrderingComparator(boolean null_high, HoldingContainer left, HoldingContainer right, FunctionLookupContext functionLookupContext) {
final String comparator_name = null_high ? COMPARE_TO_NULLS_HIGH : COMPARE_TO_NULLS_LOW;
if (!isComparableType(left.getMajorType()) || !isComparableType(right.getMajorType())) {
throw new UnsupportedOperationException(formatCanNotCompareMsg(left.getMajorType(), right.getMajorType()));
}
LogicalExpression comparisonFunctionExpression = getFunctionExpression(comparator_name, left, right);
ErrorCollector collector = new ErrorCollectorImpl();
if (!isUnionType(left.getMajorType()) && !isUnionType(right.getMajorType())) {
return ExpressionTreeMaterializer.materialize(comparisonFunctionExpression, null, collector, functionLookupContext);
} else {
LogicalExpression typeComparisonFunctionExpression = getTypeComparisonFunction(comparisonFunctionExpression, left, right);
return ExpressionTreeMaterializer.materialize(typeComparisonFunctionExpression, null, collector, functionLookupContext);
}
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.
the class FunctionGenerationHelper method getTypeComparisonFunction.
/**
* Wraps the comparison function in an If-statement which compares the types first, evaluating the comaprison function only
* if the types are equivialent
*
* @param comparisonFunction
* @param args
* @return
*/
private static LogicalExpression getTypeComparisonFunction(LogicalExpression comparisonFunction, HoldingContainer... args) {
List<LogicalExpression> argExpressions = Lists.newArrayList();
List<MajorType> argTypes = Lists.newArrayList();
for (HoldingContainer c : args) {
argTypes.add(c.getMajorType());
argExpressions.add(new HoldingContainerExpression(c));
}
FunctionCall call = new FunctionCall("compareType", argExpressions, ExpressionPosition.UNKNOWN);
List<LogicalExpression> newArgs = Lists.newArrayList();
newArgs.add(call);
newArgs.add(new IntExpression(0, ExpressionPosition.UNKNOWN));
FunctionCall notEqual = new FunctionCall("not_equal", newArgs, ExpressionPosition.UNKNOWN);
IfExpression.IfCondition ifCondition = new IfCondition(notEqual, call);
IfExpression ifExpression = IfExpression.newBuilder().setIfCondition(ifCondition).setElse(comparisonFunction).build();
return ifExpression;
}
use of org.apache.drill.common.expression.LogicalExpression in project drill by axbaretto.
the class ExpressionTreeMaterializer method materializeFilterExpr.
public static LogicalExpression materializeFilterExpr(LogicalExpression expr, Map<SchemaPath, ColumnStatistics> fieldTypes, ErrorCollector errorCollector, FunctionLookupContext functionLookupContext) {
final FilterMaterializeVisitor filterMaterializeVisitor = new FilterMaterializeVisitor(fieldTypes, errorCollector);
LogicalExpression out = expr.accept(filterMaterializeVisitor, functionLookupContext);
return out;
}
Aggregations