Search in sources :

Example 1 with Marker

use of com.facebook.presto.common.predicate.Marker in project presto by prestodb.

the class ExpressionConverter method toIcebergExpression.

private static Expression toIcebergExpression(String columnName, Type type, Domain domain) {
    if (domain.isAll()) {
        return alwaysTrue();
    }
    if (domain.getValues().isNone()) {
        return domain.isNullAllowed() ? isNull(columnName) : alwaysFalse();
    }
    if (domain.getValues().isAll()) {
        return domain.isNullAllowed() ? alwaysTrue() : not(isNull(columnName));
    }
    // Skip structural types. TODO: Evaluate Apache Iceberg's support for predicate on structural types
    if (type instanceof ArrayType || type instanceof MapType || type instanceof RowType) {
        return alwaysTrue();
    }
    ValueSet domainValues = domain.getValues();
    Expression expression = null;
    if (domain.isNullAllowed()) {
        expression = isNull(columnName);
    }
    if (domainValues instanceof SortedRangeSet) {
        List<Range> orderedRanges = ((SortedRangeSet) domainValues).getOrderedRanges();
        expression = firstNonNull(expression, alwaysFalse());
        for (Range range : orderedRanges) {
            Marker low = range.getLow();
            Marker high = range.getHigh();
            Marker.Bound lowBound = low.getBound();
            Marker.Bound highBound = high.getBound();
            // case col <> 'val' is represented as (col < 'val' or col > 'val')
            if (lowBound == EXACTLY && highBound == EXACTLY) {
                // case ==
                if (getIcebergLiteralValue(type, low).equals(getIcebergLiteralValue(type, high))) {
                    expression = or(expression, equal(columnName, getIcebergLiteralValue(type, low)));
                } else {
                    // case between
                    Expression between = and(greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)), lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
                    expression = or(expression, between);
                }
            } else {
                if (lowBound == EXACTLY && low.getValueBlock().isPresent()) {
                    // case >=
                    expression = or(expression, greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)));
                } else if (lowBound == ABOVE && low.getValueBlock().isPresent()) {
                    // case >
                    expression = or(expression, greaterThan(columnName, getIcebergLiteralValue(type, low)));
                }
                if (highBound == EXACTLY && high.getValueBlock().isPresent()) {
                    // case <=
                    if (low.getValueBlock().isPresent()) {
                        expression = and(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
                    } else {
                        expression = or(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
                    }
                } else if (highBound == BELOW && high.getValueBlock().isPresent()) {
                    // case <
                    if (low.getValueBlock().isPresent()) {
                        expression = and(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
                    } else {
                        expression = or(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
                    }
                }
            }
        }
        return expression;
    }
    throw new VerifyException("Did not expect a domain value set other than SortedRangeSet but got " + domainValues.getClass().getSimpleName());
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) SortedRangeSet(com.facebook.presto.common.predicate.SortedRangeSet) Expression(org.apache.iceberg.expressions.Expression) VerifyException(com.google.common.base.VerifyException) RowType(com.facebook.presto.common.type.RowType) Marker(com.facebook.presto.common.predicate.Marker) Range(com.facebook.presto.common.predicate.Range) ValueSet(com.facebook.presto.common.predicate.ValueSet) MapType(com.facebook.presto.common.type.MapType)

Example 2 with Marker

use of com.facebook.presto.common.predicate.Marker in project presto by prestodb.

the class KafkaMetadata method getTableLayouts.

@Override
public List<ConnectorTableLayoutResult> getTableLayouts(ConnectorSession session, ConnectorTableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns) {
    KafkaTableHandle handle = convertTableHandle(table);
    long startTimestamp = 0;
    long endTimestamp = 0;
    Optional<Map<ColumnHandle, Domain>> domains = constraint.getSummary().getDomains();
    if (domains.isPresent()) {
        Map<ColumnHandle, Domain> columnHandleDomainMap = domains.get();
        for (Map.Entry<ColumnHandle, Domain> entry : columnHandleDomainMap.entrySet()) {
            if (entry.getKey() instanceof KafkaColumnHandle && ((KafkaColumnHandle) entry.getKey()).getName().equals(KafkaInternalFieldDescription.OFFSET_TIMESTAMP_FIELD.getColumnName())) {
                Range span = entry.getValue().getValues().getRanges().getSpan();
                Marker low = span.getLow();
                Marker high = span.getHigh();
                if (!low.isLowerUnbounded()) {
                    startTimestamp = (long) low.getValue();
                }
                if (!high.isUpperUnbounded()) {
                    endTimestamp = (long) high.getValue();
                }
            }
        }
    }
    ConnectorTableLayout layout = new ConnectorTableLayout(new KafkaTableLayoutHandle(handle, startTimestamp, endTimestamp));
    return ImmutableList.of(new ConnectorTableLayoutResult(layout, constraint.getSummary()));
}
Also used : KafkaHandleResolver.convertColumnHandle(com.facebook.presto.kafka.KafkaHandleResolver.convertColumnHandle) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Marker(com.facebook.presto.common.predicate.Marker) Range(com.facebook.presto.common.predicate.Range) ConnectorTableLayoutResult(com.facebook.presto.spi.ConnectorTableLayoutResult) ConnectorTableLayout(com.facebook.presto.spi.ConnectorTableLayout) Domain(com.facebook.presto.common.predicate.Domain) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with Marker

use of com.facebook.presto.common.predicate.Marker in project presto by prestodb.

the class KuduClientSession method addConstraintPredicates.

/**
 * translates TupleDomain to KuduPredicates.
 *
 * @return false if TupleDomain or one of its domains is none
 */
private boolean addConstraintPredicates(KuduTable table, KuduScanToken.KuduScanTokenBuilder builder, TupleDomain<ColumnHandle> constraintSummary) {
    if (constraintSummary.isNone()) {
        return false;
    } else if (!constraintSummary.isAll()) {
        Schema schema = table.getSchema();
        for (TupleDomain.ColumnDomain<ColumnHandle> columnDomain : constraintSummary.getColumnDomains().get()) {
            int position = ((KuduColumnHandle) columnDomain.getColumn()).getOrdinalPosition();
            ColumnSchema columnSchema = schema.getColumnByIndex(position);
            Domain domain = columnDomain.getDomain();
            if (domain.isNone()) {
                return false;
            } else if (domain.isAll()) {
            // no restriction
            } else if (domain.isOnlyNull()) {
                builder.addPredicate(KuduPredicate.newIsNullPredicate(columnSchema));
            } else if (domain.getValues().isAll() && domain.isNullAllowed()) {
                builder.addPredicate(KuduPredicate.newIsNotNullPredicate(columnSchema));
            } else if (domain.isSingleValue()) {
                KuduPredicate predicate = createEqualsPredicate(columnSchema, domain.getSingleValue());
                builder.addPredicate(predicate);
            } else {
                ValueSet valueSet = domain.getValues();
                if (valueSet instanceof EquatableValueSet) {
                    DiscreteValues discreteValues = valueSet.getDiscreteValues();
                    KuduPredicate predicate = createInListPredicate(columnSchema, discreteValues);
                    builder.addPredicate(predicate);
                } else if (valueSet instanceof SortedRangeSet) {
                    Ranges ranges = ((SortedRangeSet) valueSet).getRanges();
                    Range span = ranges.getSpan();
                    Marker low = span.getLow();
                    if (!low.isLowerUnbounded()) {
                        KuduPredicate.ComparisonOp op = (low.getBound() == Marker.Bound.ABOVE) ? KuduPredicate.ComparisonOp.GREATER : KuduPredicate.ComparisonOp.GREATER_EQUAL;
                        KuduPredicate predicate = createComparisonPredicate(columnSchema, op, low.getValue());
                        builder.addPredicate(predicate);
                    }
                    Marker high = span.getHigh();
                    if (!high.isUpperUnbounded()) {
                        KuduPredicate.ComparisonOp op = (low.getBound() == Marker.Bound.BELOW) ? KuduPredicate.ComparisonOp.LESS : KuduPredicate.ComparisonOp.LESS_EQUAL;
                        KuduPredicate predicate = createComparisonPredicate(columnSchema, op, high.getValue());
                        builder.addPredicate(predicate);
                    }
                } else {
                    throw new IllegalStateException("Unexpected domain: " + domain);
                }
            }
        }
    }
    return true;
}
Also used : Ranges(com.facebook.presto.common.predicate.Ranges) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) EquatableValueSet(com.facebook.presto.common.predicate.EquatableValueSet) ColumnSchema(org.apache.kudu.ColumnSchema) Marker(com.facebook.presto.common.predicate.Marker) Range(com.facebook.presto.common.predicate.Range) KuduPredicate(org.apache.kudu.client.KuduPredicate) SortedRangeSet(com.facebook.presto.common.predicate.SortedRangeSet) DiscreteValues(com.facebook.presto.common.predicate.DiscreteValues) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) EquatableValueSet(com.facebook.presto.common.predicate.EquatableValueSet) ValueSet(com.facebook.presto.common.predicate.ValueSet)

Aggregations

Marker (com.facebook.presto.common.predicate.Marker)3 Range (com.facebook.presto.common.predicate.Range)3 Domain (com.facebook.presto.common.predicate.Domain)2 SortedRangeSet (com.facebook.presto.common.predicate.SortedRangeSet)2 ValueSet (com.facebook.presto.common.predicate.ValueSet)2 DiscreteValues (com.facebook.presto.common.predicate.DiscreteValues)1 EquatableValueSet (com.facebook.presto.common.predicate.EquatableValueSet)1 Ranges (com.facebook.presto.common.predicate.Ranges)1 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 MapType (com.facebook.presto.common.type.MapType)1 RowType (com.facebook.presto.common.type.RowType)1 KafkaHandleResolver.convertColumnHandle (com.facebook.presto.kafka.KafkaHandleResolver.convertColumnHandle)1 ColumnHandle (com.facebook.presto.spi.ColumnHandle)1 ConnectorTableLayout (com.facebook.presto.spi.ConnectorTableLayout)1 ConnectorTableLayoutResult (com.facebook.presto.spi.ConnectorTableLayoutResult)1 VerifyException (com.google.common.base.VerifyException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Expression (org.apache.iceberg.expressions.Expression)1