Search in sources :

Example 1 with Domain

use of io.prestosql.spi.predicate.Domain in project carbondata by apache.

the class PrestoFilterUtil method parseFilterExpression.

/**
 * Convert presto-TupleDomain predication into Carbon scan express condition
 *
 * @param originalConstraint presto-TupleDomain
 * @return
 */
static Expression parseFilterExpression(TupleDomain<HiveColumnHandle> originalConstraint) {
    Domain domain;
    if (originalConstraint.isNone()) {
        return null;
    }
    // final expression for the table,
    // returned by the method after combining all the column filters (colValueExpression).
    Expression finalFilters = null;
    for (HiveColumnHandle cdch : originalConstraint.getDomains().get().keySet()) {
        // Build ColumnExpression for Expression(Carbondata)
        HiveType type = cdch.getHiveType();
        DataType coltype = spi2CarbondataTypeMapper(cdch);
        Expression colExpression = new ColumnExpression(cdch.getName(), coltype);
        domain = originalConstraint.getDomains().get().get(cdch);
        checkArgument(domain.getType().isOrderable(), "Domain type must be orderable");
        List<Object> singleValues = new ArrayList<>();
        // combination of multiple rangeExpression for a single column,
        // in case of multiple range Filter on single column
        // else this is equal to rangeExpression, combined to create finalFilters
        Expression colValueExpression = null;
        for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
            if (range.isSingleValue()) {
                Object value = convertDataByType(range.getLow().getValue(), type);
                singleValues.add(value);
            } else {
                // generated for each range of column i.e. lessThan, greaterThan,
                // there can be multiple ranges for a single column. combined to create colValueExpression
                Expression rangeExpression = null;
                if (!range.getLow().isLowerUnbounded()) {
                    Object value = convertDataByType(range.getLow().getValue(), type);
                    switch(range.getLow().getBound()) {
                        case ABOVE:
                            rangeExpression = new GreaterThanExpression(colExpression, new LiteralExpression(value, coltype));
                            break;
                        case EXACTLY:
                            rangeExpression = new GreaterThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
                            break;
                        case BELOW:
                            throw new IllegalArgumentException("Low marker should never use BELOW bound");
                        default:
                            throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
                    }
                }
                if (!range.getHigh().isUpperUnbounded()) {
                    Expression lessThanExpression;
                    Object value = convertDataByType(range.getHigh().getValue(), type);
                    switch(range.getHigh().getBound()) {
                        case ABOVE:
                            throw new IllegalArgumentException("High marker should never use ABOVE bound");
                        case EXACTLY:
                            lessThanExpression = new LessThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
                            break;
                        case BELOW:
                            lessThanExpression = new LessThanExpression(colExpression, new LiteralExpression(value, coltype));
                            break;
                        default:
                            throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
                    }
                    rangeExpression = (rangeExpression == null ? lessThanExpression : new AndExpression(rangeExpression, lessThanExpression));
                }
                colValueExpression = (colValueExpression == null ? rangeExpression : new OrExpression(colValueExpression, rangeExpression));
            }
        }
        if (singleValues.size() == 1) {
            colValueExpression = new EqualToExpression(colExpression, new LiteralExpression(singleValues.get(0), coltype));
        } else if (singleValues.size() > 1) {
            List<Expression> exs = singleValues.stream().map((a) -> new LiteralExpression(a, coltype)).collect(toList());
            colValueExpression = new InExpression(colExpression, new ListExpression(exs));
        }
        if (colValueExpression != null) {
            finalFilters = (finalFilters == null ? colValueExpression : new AndExpression(finalFilters, colValueExpression));
        }
    }
    return finalFilters;
}
Also used : LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) EqualToExpression(org.apache.carbondata.core.scan.expression.conditional.EqualToExpression) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) InExpression(org.apache.carbondata.core.scan.expression.conditional.InExpression) ListExpression(org.apache.carbondata.core.scan.expression.conditional.ListExpression) ArrayList(java.util.ArrayList) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) Range(io.prestosql.spi.predicate.Range) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) OrExpression(org.apache.carbondata.core.scan.expression.logical.OrExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) LessThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) GreaterThanExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression) AndExpression(org.apache.carbondata.core.scan.expression.logical.AndExpression) GreaterThanEqualToExpression(org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression) Expression(org.apache.carbondata.core.scan.expression.Expression) EqualToExpression(org.apache.carbondata.core.scan.expression.conditional.EqualToExpression) ListExpression(org.apache.carbondata.core.scan.expression.conditional.ListExpression) OrExpression(org.apache.carbondata.core.scan.expression.logical.OrExpression) LiteralExpression(org.apache.carbondata.core.scan.expression.LiteralExpression) InExpression(org.apache.carbondata.core.scan.expression.conditional.InExpression) LessThanExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanExpression) ColumnExpression(org.apache.carbondata.core.scan.expression.ColumnExpression) DataType(org.apache.carbondata.core.metadata.datatype.DataType) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TupleDomain(io.prestosql.spi.predicate.TupleDomain) Domain(io.prestosql.spi.predicate.Domain) HiveType(io.prestosql.plugin.hive.HiveType) HiveColumnHandle(io.prestosql.plugin.hive.HiveColumnHandle) LessThanExpression(org.apache.carbondata.core.scan.expression.conditional.LessThanExpression)

Aggregations

HiveColumnHandle (io.prestosql.plugin.hive.HiveColumnHandle)1 HiveType (io.prestosql.plugin.hive.HiveType)1 Domain (io.prestosql.spi.predicate.Domain)1 Range (io.prestosql.spi.predicate.Range)1 TupleDomain (io.prestosql.spi.predicate.TupleDomain)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 DataType (org.apache.carbondata.core.metadata.datatype.DataType)1 ColumnExpression (org.apache.carbondata.core.scan.expression.ColumnExpression)1 Expression (org.apache.carbondata.core.scan.expression.Expression)1 LiteralExpression (org.apache.carbondata.core.scan.expression.LiteralExpression)1 EqualToExpression (org.apache.carbondata.core.scan.expression.conditional.EqualToExpression)1 GreaterThanEqualToExpression (org.apache.carbondata.core.scan.expression.conditional.GreaterThanEqualToExpression)1 GreaterThanExpression (org.apache.carbondata.core.scan.expression.conditional.GreaterThanExpression)1 InExpression (org.apache.carbondata.core.scan.expression.conditional.InExpression)1 LessThanEqualToExpression (org.apache.carbondata.core.scan.expression.conditional.LessThanEqualToExpression)1 LessThanExpression (org.apache.carbondata.core.scan.expression.conditional.LessThanExpression)1 ListExpression (org.apache.carbondata.core.scan.expression.conditional.ListExpression)1 AndExpression (org.apache.carbondata.core.scan.expression.logical.AndExpression)1