Search in sources :

Example 1 with EquatableValueSet

use of io.trino.spi.predicate.EquatableValueSet in project trino by trinodb.

the class KuduClientSession method addConstraintPredicates.

/**
 * translates TupleDomain to KuduPredicates.
 */
private void addConstraintPredicates(KuduTable table, KuduScanToken.KuduScanTokenBuilder builder, TupleDomain<ColumnHandle> constraintSummary) {
    verify(!constraintSummary.isNone(), "constraintSummary is none");
    if (constraintSummary.isAll()) {
        return;
    }
    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();
        verify(!domain.isNone(), "Domain is none");
        if (domain.isAll()) {
        // no restriction
        } else if (domain.isOnlyNull()) {
            builder.addPredicate(KuduPredicate.newIsNullPredicate(columnSchema));
        } else if (!domain.getValues().isNone() && domain.isNullAllowed()) {
        // no restriction
        } 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();
                List<Range> rangeList = ranges.getOrderedRanges();
                if (rangeList.stream().allMatch(Range::isSingleValue)) {
                    io.trino.spi.type.Type type = TypeHelper.fromKuduColumn(columnSchema);
                    List<Object> javaValues = rangeList.stream().map(range -> TypeHelper.getJavaValue(type, range.getSingleValue())).collect(toImmutableList());
                    KuduPredicate predicate = KuduPredicate.newInListPredicate(columnSchema, javaValues);
                    builder.addPredicate(predicate);
                } else {
                    Range span = ranges.getSpan();
                    if (!span.isLowUnbounded()) {
                        KuduPredicate.ComparisonOp op = span.isLowInclusive() ? GREATER_EQUAL : GREATER;
                        KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getLowBoundedValue());
                        builder.addPredicate(predicate);
                    }
                    if (!span.isHighUnbounded()) {
                        KuduPredicate.ComparisonOp op = span.isHighInclusive() ? LESS_EQUAL : LESS;
                        KuduPredicate predicate = createComparisonPredicate(columnSchema, op, span.getHighBoundedValue());
                        builder.addPredicate(predicate);
                    }
                }
            } else {
                throw new IllegalStateException("Unexpected domain: " + domain);
            }
        }
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Ranges(io.trino.spi.predicate.Ranges) Schema(org.apache.kudu.Schema) ColumnSchema(org.apache.kudu.ColumnSchema) HashBucketSchema(org.apache.kudu.client.PartitionSchema.HashBucketSchema) EquatableValueSet(io.trino.spi.predicate.EquatableValueSet) ColumnSchema(org.apache.kudu.ColumnSchema) Range(io.trino.spi.predicate.Range) KuduPredicate(org.apache.kudu.client.KuduPredicate) Type(org.apache.kudu.Type) DecimalType(io.trino.spi.type.DecimalType) TupleDomain(io.trino.spi.predicate.TupleDomain) SortedRangeSet(io.trino.spi.predicate.SortedRangeSet) DiscreteValues(io.trino.spi.predicate.DiscreteValues) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) EquatableValueSet(io.trino.spi.predicate.EquatableValueSet) ValueSet(io.trino.spi.predicate.ValueSet)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ColumnHandle (io.trino.spi.connector.ColumnHandle)1 DiscreteValues (io.trino.spi.predicate.DiscreteValues)1 Domain (io.trino.spi.predicate.Domain)1 EquatableValueSet (io.trino.spi.predicate.EquatableValueSet)1 Range (io.trino.spi.predicate.Range)1 Ranges (io.trino.spi.predicate.Ranges)1 SortedRangeSet (io.trino.spi.predicate.SortedRangeSet)1 TupleDomain (io.trino.spi.predicate.TupleDomain)1 ValueSet (io.trino.spi.predicate.ValueSet)1 DecimalType (io.trino.spi.type.DecimalType)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 ColumnSchema (org.apache.kudu.ColumnSchema)1 Schema (org.apache.kudu.Schema)1 Type (org.apache.kudu.Type)1 KuduPredicate (org.apache.kudu.client.KuduPredicate)1 HashBucketSchema (org.apache.kudu.client.PartitionSchema.HashBucketSchema)1