use of org.apache.drill.common.expression.ErrorCollectorImpl in project drill by axbaretto.
the class ExpressionTreeMaterializerTest method testMaterializingLateboundTree.
@Test
public void testMaterializingLateboundTree() throws SchemaChangeException {
final RecordBatch batch = mock(RecordBatch.class);
when(batch.getValueVectorId(SchemaPath.getSimplePath("test"))).thenReturn(new TypedFieldId(Types.required(MinorType.BIT), -4));
when(batch.getValueVectorId(SchemaPath.getSimplePath("test1"))).thenReturn(new TypedFieldId(Types.required(MinorType.BIGINT), -5));
ErrorCollector ec = new ErrorCollectorImpl();
LogicalExpression elseExpression = new IfExpression.Builder().setElse(new ValueExpressions.LongExpression(1L, ExpressionPosition.UNKNOWN)).setIfCondition(new IfExpression.IfCondition(new ValueExpressions.BooleanExpression("true", ExpressionPosition.UNKNOWN), new FieldReference("test1", ExpressionPosition.UNKNOWN))).build();
LogicalExpression expr = new IfExpression.Builder().setIfCondition(new IfExpression.IfCondition(new FieldReference("test", ExpressionPosition.UNKNOWN), new ValueExpressions.LongExpression(2L, ExpressionPosition.UNKNOWN))).setElse(elseExpression).build();
LogicalExpression newExpr = ExpressionTreeMaterializer.materialize(expr, batch, ec, registry);
assertTrue(newExpr instanceof IfExpression);
IfExpression newIfExpr = (IfExpression) newExpr;
IfExpression.IfCondition ifCondition = newIfExpr.ifCondition;
assertTrue(newIfExpr.elseExpression instanceof IfExpression);
assertEquals(bigIntType, ifCondition.expression.getMajorType());
assertEquals(true, ((ValueExpressions.BooleanExpression) ((IfExpression) (newIfExpr.elseExpression)).ifCondition.condition).value);
if (ec.hasErrors()) {
System.out.println(ec.toErrorString());
}
assertFalse(ec.hasErrors());
}
use of org.apache.drill.common.expression.ErrorCollectorImpl in project drill by axbaretto.
the class DrillConstExecutor method reduce.
@Override
public void reduce(final RexBuilder rexBuilder, List<RexNode> constExps, final List<RexNode> reducedValues) {
for (final RexNode newCall : constExps) {
LogicalExpression logEx = DrillOptiq.toDrill(new DrillParseContext(plannerSettings), (RelNode) null, /* input rel */
newCall);
ErrorCollectorImpl errors = new ErrorCollectorImpl();
final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(logEx, null, errors, funcImplReg);
if (errors.getErrorCount() != 0) {
String message = String.format("Failure while materializing expression in constant expression evaluator [%s]. Errors: %s", newCall.toString(), errors.toString());
throw UserException.planError().message(message).build(logger);
}
if (NON_REDUCIBLE_TYPES.contains(materializedExpr.getMajorType().getMinorType())) {
logger.debug("Constant expression not folded due to return type {}, complete expression: {}", materializedExpr.getMajorType(), ExpressionStringBuilder.toString(materializedExpr));
reducedValues.add(newCall);
continue;
}
ValueHolder output = InterpreterEvaluator.evaluateConstantExpr(udfUtilities, materializedExpr);
final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL && TypeHelper.isNull(output)) {
SqlTypeName sqlTypeName = TypeInferenceUtils.getCalciteTypeFromDrillType(materializedExpr.getMajorType().getMinorType());
if (sqlTypeName == null) {
String message = String.format("Error reducing constant expression, unsupported type: %s.", materializedExpr.getMajorType().getMinorType());
throw UserException.unsupportedError().message(message).build(logger);
}
reducedValues.add(rexBuilder.makeNullLiteral(sqlTypeName));
continue;
}
Function<ValueHolder, RexNode> literator = new Function<ValueHolder, RexNode>() {
@Override
public RexNode apply(ValueHolder output) {
switch(materializedExpr.getMajorType().getMinorType()) {
case INT:
{
int value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableIntHolder) output).value : ((IntHolder) output).value;
return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTEGER, newCall.getType().isNullable()), false);
}
case BIGINT:
{
long value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableBigIntHolder) output).value : ((BigIntHolder) output).value;
return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.BIGINT, newCall.getType().isNullable()), false);
}
case FLOAT4:
{
float value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableFloat4Holder) output).value : ((Float4Holder) output).value;
return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.FLOAT, newCall.getType().isNullable()), false);
}
case FLOAT8:
{
double value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableFloat8Holder) output).value : ((Float8Holder) output).value;
return rexBuilder.makeLiteral(new BigDecimal(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DOUBLE, newCall.getType().isNullable()), false);
}
case VARCHAR:
{
String value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? StringFunctionHelpers.getStringFromVarCharHolder((NullableVarCharHolder) output) : StringFunctionHelpers.getStringFromVarCharHolder((VarCharHolder) output);
return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.VARCHAR, newCall.getType().isNullable()), false);
}
case BIT:
{
boolean value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? ((NullableBitHolder) output).value == 1 : ((BitHolder) output).value == 1;
return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.BOOLEAN, newCall.getType().isNullable()), false);
}
case DATE:
{
Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableDateHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((DateHolder) output).value, DateTimeZone.UTC).toCalendar(null);
return rexBuilder.makeLiteral(DateString.fromCalendarFields(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DATE, newCall.getType().isNullable()), false);
}
case DECIMAL9:
{
long value;
int scale;
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
NullableDecimal9Holder decimal9Out = (NullableDecimal9Holder) output;
value = decimal9Out.value;
scale = decimal9Out.scale;
} else {
Decimal9Holder decimal9Out = (Decimal9Holder) output;
value = decimal9Out.value;
scale = decimal9Out.scale;
}
return rexBuilder.makeLiteral(new BigDecimal(BigInteger.valueOf(value), scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
}
case DECIMAL18:
{
long value;
int scale;
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
NullableDecimal18Holder decimal18Out = (NullableDecimal18Holder) output;
value = decimal18Out.value;
scale = decimal18Out.scale;
} else {
Decimal18Holder decimal18Out = (Decimal18Holder) output;
value = decimal18Out.value;
scale = decimal18Out.scale;
}
return rexBuilder.makeLiteral(new BigDecimal(BigInteger.valueOf(value), scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
}
case DECIMAL28SPARSE:
{
DrillBuf buffer;
int start;
int scale;
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
NullableDecimal28SparseHolder decimal28Out = (NullableDecimal28SparseHolder) output;
buffer = decimal28Out.buffer;
start = decimal28Out.start;
scale = decimal28Out.scale;
} else {
Decimal28SparseHolder decimal28Out = (Decimal28SparseHolder) output;
buffer = decimal28Out.buffer;
start = decimal28Out.start;
scale = decimal28Out.scale;
}
return rexBuilder.makeLiteral(org.apache.drill.exec.util.DecimalUtility.getBigDecimalFromSparse(buffer, start * 20, 5, scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
}
case DECIMAL38SPARSE:
{
DrillBuf buffer;
int start;
int scale;
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
NullableDecimal38SparseHolder decimal38Out = (NullableDecimal38SparseHolder) output;
buffer = decimal38Out.buffer;
start = decimal38Out.start;
scale = decimal38Out.scale;
} else {
Decimal38SparseHolder decimal38Out = (Decimal38SparseHolder) output;
buffer = decimal38Out.buffer;
start = decimal38Out.start;
scale = decimal38Out.scale;
}
return rexBuilder.makeLiteral(org.apache.drill.exec.util.DecimalUtility.getBigDecimalFromSparse(buffer, start * 24, 6, scale), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.DECIMAL, newCall.getType().isNullable()), false);
}
case TIME:
{
Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableTimeHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((TimeHolder) output).value, DateTimeZone.UTC).toCalendar(null);
return rexBuilder.makeLiteral(TimeString.fromCalendarFields(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.TIME, newCall.getType().isNullable()), false);
}
case TIMESTAMP:
{
Calendar value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new DateTime(((NullableTimeStampHolder) output).value, DateTimeZone.UTC).toCalendar(null) : new DateTime(((TimeStampHolder) output).value, DateTimeZone.UTC).toCalendar(null);
return rexBuilder.makeLiteral(TimestampString.fromCalendarFields(value), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.TIMESTAMP, newCall.getType().isNullable()), false);
}
case INTERVALYEAR:
{
BigDecimal value = (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) ? new BigDecimal(((NullableIntervalYearHolder) output).value) : new BigDecimal(((IntervalYearHolder) output).value);
return rexBuilder.makeLiteral(value, TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTERVAL_YEAR_MONTH, newCall.getType().isNullable()), false);
}
case INTERVALDAY:
{
int days;
int milliseconds;
if (materializedExpr.getMajorType().getMode() == TypeProtos.DataMode.OPTIONAL) {
NullableIntervalDayHolder intervalDayOut = (NullableIntervalDayHolder) output;
days = intervalDayOut.days;
milliseconds = intervalDayOut.milliseconds;
} else {
IntervalDayHolder intervalDayOut = (IntervalDayHolder) output;
days = intervalDayOut.days;
milliseconds = intervalDayOut.milliseconds;
}
return rexBuilder.makeLiteral(new BigDecimal(days * (long) DateUtilities.daysToStandardMillis + milliseconds), TypeInferenceUtils.createCalciteTypeWithNullability(typeFactory, SqlTypeName.INTERVAL_DAY, newCall.getType().isNullable()), false);
}
// as new types may be added in the future.
default:
logger.debug("Constant expression not folded due to return type {}, complete expression: {}", materializedExpr.getMajorType(), ExpressionStringBuilder.toString(materializedExpr));
return newCall;
}
}
};
reducedValues.add(literator.apply(output));
}
}
use of org.apache.drill.common.expression.ErrorCollectorImpl in project drill by axbaretto.
the class ParquetGroupScan method applyFilter.
public GroupScan applyFilter(LogicalExpression filterExpr, UdfUtilities udfUtilities, FunctionImplementationRegistry functionImplementationRegistry, OptionManager optionManager) {
if (rowGroupInfos.size() == 1 || !(parquetTableMetadata.isRowGroupPrunable()) || rowGroupInfos.size() > optionManager.getOption(PlannerSettings.PARQUET_ROWGROUP_FILTER_PUSHDOWN_PLANNING_THRESHOLD)) {
// - # of row groups is beyond PARQUET_ROWGROUP_FILTER_PUSHDOWN_PLANNING_THRESHOLD.
return null;
}
final Set<SchemaPath> schemaPathsInExpr = filterExpr.accept(new ParquetRGFilterEvaluator.FieldReferenceFinder(), null);
final List<RowGroupInfo> qualifiedRGs = new ArrayList<>(rowGroupInfos.size());
// HashSet keeps a fileName unique.
Set<String> qualifiedFileNames = Sets.newHashSet();
ParquetFilterPredicate filterPredicate = null;
for (RowGroupInfo rowGroup : rowGroupInfos) {
final ColumnExplorer columnExplorer = new ColumnExplorer(optionManager, this.columns);
Map<String, String> implicitColValues = columnExplorer.populateImplicitColumns(rowGroup.getPath(), selectionRoot);
ParquetMetaStatCollector statCollector = new ParquetMetaStatCollector(parquetTableMetadata, rowGroup.getColumns(), implicitColValues);
Map<SchemaPath, ColumnStatistics> columnStatisticsMap = statCollector.collectColStat(schemaPathsInExpr);
if (filterPredicate == null) {
ErrorCollector errorCollector = new ErrorCollectorImpl();
LogicalExpression materializedFilter = ExpressionTreeMaterializer.materializeFilterExpr(filterExpr, columnStatisticsMap, errorCollector, functionImplementationRegistry);
if (errorCollector.hasErrors()) {
logger.error("{} error(s) encountered when materialize filter expression : {}", errorCollector.getErrorCount(), errorCollector.toErrorString());
return null;
}
// logger.debug("materializedFilter : {}", ExpressionStringBuilder.toString(materializedFilter));
Set<LogicalExpression> constantBoundaries = ConstantExpressionIdentifier.getConstantExpressionSet(materializedFilter);
filterPredicate = (ParquetFilterPredicate) ParquetFilterBuilder.buildParquetFilterPredicate(materializedFilter, constantBoundaries, udfUtilities);
if (filterPredicate == null) {
return null;
}
}
if (ParquetRGFilterEvaluator.canDrop(filterPredicate, columnStatisticsMap, rowGroup.getRowCount())) {
continue;
}
qualifiedRGs.add(rowGroup);
// TODO : optimize when 1 file contains m row groups.
qualifiedFileNames.add(rowGroup.getPath());
}
if (qualifiedRGs.size() == rowGroupInfos.size()) {
// There is no reduction of rowGroups. Return the original groupScan.
logger.debug("applyFilter does not have any pruning!");
return null;
} else if (qualifiedFileNames.size() == 0) {
logger.warn("All rowgroups have been filtered out. Add back one to get schema from scannner");
RowGroupInfo rg = rowGroupInfos.iterator().next();
qualifiedFileNames.add(rg.getPath());
qualifiedRGs.add(rg);
}
try {
FileSelection newSelection = new FileSelection(null, Lists.newArrayList(qualifiedFileNames), getSelectionRoot(), cacheFileRoot, false);
logger.info("applyFilter {} reduce parquet rowgroup # from {} to {}", ExpressionStringBuilder.toString(filterExpr), rowGroupInfos.size(), qualifiedRGs.size());
ParquetGroupScan clonegroupscan = this.clone(newSelection);
clonegroupscan.rowGroupInfos = qualifiedRGs;
clonegroupscan.updatePartitionColTypeMap();
return clonegroupscan;
} catch (IOException e) {
logger.warn("Could not apply filter prune due to Exception : {}", e);
return null;
}
}
use of org.apache.drill.common.expression.ErrorCollectorImpl in project drill by axbaretto.
the class ParquetRGFilterEvaluator method canDrop.
public static boolean canDrop(LogicalExpression expr, Map<SchemaPath, ColumnStatistics> columnStatisticsMap, long rowCount, UdfUtilities udfUtilities, FunctionLookupContext functionImplementationRegistry) {
ErrorCollector errorCollector = new ErrorCollectorImpl();
LogicalExpression materializedFilter = ExpressionTreeMaterializer.materializeFilterExpr(expr, columnStatisticsMap, errorCollector, functionImplementationRegistry);
if (errorCollector.hasErrors()) {
logger.error("{} error(s) encountered when materialize filter expression : {}", errorCollector.getErrorCount(), errorCollector.toErrorString());
return false;
}
Set<LogicalExpression> constantBoundaries = ConstantExpressionIdentifier.getConstantExpressionSet(materializedFilter);
ParquetFilterPredicate parquetPredicate = (ParquetFilterPredicate) ParquetFilterBuilder.buildParquetFilterPredicate(materializedFilter, constantBoundaries, udfUtilities);
return canDrop(parquetPredicate, columnStatisticsMap, rowCount);
}
use of org.apache.drill.common.expression.ErrorCollectorImpl in project drill by axbaretto.
the class ExpressionTest method getExpressionCode.
private String getExpressionCode(String expression, RecordBatch batch) throws Exception {
final LogicalExpression expr = parseExpr(expression);
final ErrorCollector error = new ErrorCollectorImpl();
final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(expr, batch, error, registry);
if (error.getErrorCount() != 0) {
logger.error("Failure while materializing expression [{}]. Errors: {}", expression, error);
assertEquals(0, error.getErrorCount());
}
FunctionImplementationRegistry funcReg = new FunctionImplementationRegistry(DrillConfig.create());
final ClassGenerator<Projector> cg = CodeGenerator.get(Projector.TEMPLATE_DEFINITION, null).getRoot();
cg.addExpr(new ValueVectorWriteExpression(new TypedFieldId(materializedExpr.getMajorType(), -1), materializedExpr));
return cg.getCodeGenerator().generateAndGet();
}
Aggregations