Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ CompletableFuture<Optional<GlobalIndexEvaluator.Evaluation>> evaluate(
new GlobalIndexEvaluator.Evaluation(
GlobalIndexResult.createEmpty(),
emptyResultFields,
null)));
null,
false)));
}
}
List<ContainsGroup> groupedContains = new ArrayList<>(groups.values());
Expand Down Expand Up @@ -171,7 +172,8 @@ CompletableFuture<Optional<GlobalIndexEvaluator.Evaluation>> evaluate(
GlobalIndexResult
.createEmpty(),
emptyResultFields,
null)));
null,
false)));
}
}

Expand Down Expand Up @@ -201,8 +203,10 @@ CompletableFuture<Optional<GlobalIndexEvaluator.Evaluation>> evaluate(
exactFutures
.size()
+ 1);
if (remainingEvaluation
.isPresent()) {
// An absent result for existing
// remaining conditions is not
// the same as no conditions.
if (!remaining.isEmpty()) {
results.add(
remainingEvaluation);
}
Expand Down Expand Up @@ -269,10 +273,15 @@ private CompletableFuture<Optional<GlobalIndexEvaluator.Evaluation>> visitContai
result ->
result.map(
value ->
// Legacy wrappers can retain only supported
// contains literals. This opaque result does not
// prove completeness; search already treats
// contains as candidate-only.
new GlobalIndexEvaluator.Evaluation(
value,
Collections.singleton(group.fieldId),
null)));
null,
false)));
}

private CompletableFuture<Optional<GlobalIndexResult>> intersectReaderResults(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,27 @@ private CompletableFuture<Optional<Evaluation>> visitLeafAsync(LeafPredicate pre
private CompletableFuture<Optional<Evaluation>> visitFieldAsync(
FieldRef fieldRef,
Function<GlobalIndexReader, CompletableFuture<Optional<GlobalIndexResult>>> visitor) {
return visitFieldWithCompletenessAsync(
fieldRef,
reader ->
visitor.apply(reader)
.thenApply(
result ->
result.map(
value ->
new GlobalIndexPredicateResult(
value, true))));
}

private CompletableFuture<Optional<Evaluation>> visitFieldWithCompletenessAsync(
FieldRef fieldRef,
Function<GlobalIndexReader, CompletableFuture<Optional<GlobalIndexPredicateResult>>>
visitor) {
int fieldId = rowType.getField(fieldRef.name()).id();
Collection<GlobalIndexReader> readers =
indexReadersCache.computeIfAbsent(fieldId, readersFunction::apply);

List<CompletableFuture<Optional<GlobalIndexResult>>> readerFutures =
List<CompletableFuture<Optional<GlobalIndexPredicateResult>>> readerFutures =
new ArrayList<>(readers.size());
for (GlobalIndexReader reader : readers) {
readerFutures.add(visitor.apply(reader));
Expand All @@ -171,26 +187,37 @@ private CompletableFuture<Optional<Evaluation>> visitFieldAsync(
.thenApply(
v -> {
Optional<GlobalIndexResult> compoundResult = Optional.empty();
for (CompletableFuture<Optional<GlobalIndexResult>> f : readerFutures) {
Optional<GlobalIndexResult> childResult = f.join();
boolean fullyEvaluated = false;
for (CompletableFuture<Optional<GlobalIndexPredicateResult>> f :
readerFutures) {
Optional<GlobalIndexPredicateResult> childResult = f.join();
if (!childResult.isPresent()) {
continue;
}
// One complete alternative index suffices for this predicate.
// Intersecting safe candidates cannot invalidate that proof.
fullyEvaluated |= childResult.get().fullyEvaluated();
if (compoundResult.isPresent()) {
compoundResult =
Optional.of(
compoundResult.get().and(childResult.get()));
compoundResult
.get()
.and(childResult.get().result()));
} else {
compoundResult = childResult;
compoundResult = Optional.of(childResult.get().result());
}
if (compoundResult.get().results().isEmpty()) {
break;
}
}
return compoundResult.map(
result ->
return compoundResult.isPresent()
? Optional.of(
new Evaluation(
result, Collections.singleton(fieldId), null));
compoundResult.get(),
Collections.singleton(fieldId),
null,
fullyEvaluated))
: Optional.empty();
});
}

Expand Down Expand Up @@ -225,10 +252,10 @@ private CompletableFuture<Optional<Evaluation>> visitCompoundAsync(
LeafPredicate upper = isLowerBound(first) ? second : first;
FieldRef field = first.fieldRefOptional().get();
range =
visitFieldAsync(
visitFieldWithCompletenessAsync(
field,
reader ->
reader.visitRange(
reader.visitRangeWithCompleteness(
field,
lower.literals().get(0),
upper.literals().get(0),
Expand Down Expand Up @@ -271,6 +298,11 @@ static boolean isLowerBound(LeafPredicate leaf) {
private Optional<Evaluation> combineResults(
List<Optional<Evaluation>> results, CompoundPredicate predicate) {
Set<Integer> contributingFieldIds = new HashSet<>();
// Field coverage is insufficient: a > 0 AND isNaN(a) still contributes field a even
// when the reader declines isNaN. Track every child, including before an empty shortcut.
boolean fullyEvaluated =
results.stream()
.allMatch(child -> child.isPresent() && child.get().fullyEvaluated());
if (predicate.function() instanceof Or) {
GlobalIndexResult compoundResult = GlobalIndexResult.createEmpty();
for (Optional<Evaluation> child : results) {
Expand All @@ -280,7 +312,8 @@ private Optional<Evaluation> combineResults(
compoundResult = compoundResult.or(child.get().result());
contributingFieldIds.addAll(child.get().contributingFieldIds());
}
return Optional.of(new Evaluation(compoundResult, contributingFieldIds, null));
return Optional.of(
new Evaluation(compoundResult, contributingFieldIds, null, fullyEvaluated));
} else {
Optional<GlobalIndexResult> compoundResult = Optional.empty();
for (Optional<Evaluation> child : results) {
Expand All @@ -297,7 +330,8 @@ private Optional<Evaluation> combineResults(
break;
}
}
return compoundResult.map(result -> new Evaluation(result, contributingFieldIds, null));
return compoundResult.map(
result -> new Evaluation(result, contributingFieldIds, null, fullyEvaluated));
}
}

Expand All @@ -309,12 +343,15 @@ public static final class Evaluation {
private final GlobalIndexResult result;
private final Set<Integer> contributingFieldIds;
@Nullable private final List<Range> coveredRanges;
private final boolean fullyEvaluated;

Evaluation(
GlobalIndexResult result,
Collection<Integer> contributingFieldIds,
@Nullable List<Range> coveredRanges) {
@Nullable List<Range> coveredRanges,
boolean fullyEvaluated) {
this.result = result;
this.fullyEvaluated = fullyEvaluated;
this.coveredRanges =
coveredRanges == null
? null
Expand All @@ -335,6 +372,14 @@ public List<Range> coveredRanges() {
public Set<Integer> contributingFieldIds() {
return contributingFieldIds;
}

/**
* Whether all necessary predicates were evaluated within the reported coverage. This does
* not prove full row-range coverage or exactness of candidate-only index operators.
*/
public boolean fullyEvaluated() {
return fullyEvaluated;
}
}

/** Shared normalization for eager evaluation and metadata-only index planning. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.paimon.globalindex;

/** Candidates and whether every condition sent to a reader was evaluated. */
public final class GlobalIndexPredicateResult {

private final GlobalIndexResult result;
private final boolean fullyEvaluated;

public GlobalIndexPredicateResult(GlobalIndexResult result, boolean fullyEvaluated) {
this.result = result;
this.fullyEvaluated = fullyEvaluated;
}

public GlobalIndexResult result() {
return result;
}

/** Completeness is necessary, but not sufficient, for an exact match set. */
public boolean fullyEvaluated() {
return fullyEvaluated;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ default CompletableFuture<Optional<GlobalIndexResult>> visitArrayContainsAll(
*/
default CompletableFuture<Optional<GlobalIndexResult>> visitRange(
FieldRef fieldRef, Object from, Object to, boolean fromInclusive, boolean toInclusive) {
return visitRangeWithCompleteness(fieldRef, from, to, fromInclusive, toInclusive)
.thenApply(result -> result.map(GlobalIndexPredicateResult::result));
}

/**
* Evaluate both range bounds without mistaking a supported single bound for the whole range.
* Readers with a combined scan can override this method to retain that optimization and report
* whether both bounds were evaluated. Existing readers default to evaluating the two leaves.
*/
default CompletableFuture<Optional<GlobalIndexPredicateResult>> visitRangeWithCompleteness(
FieldRef fieldRef, Object from, Object to, boolean fromInclusive, boolean toInclusive) {
CompletableFuture<Optional<GlobalIndexResult>> lower =
fromInclusive
? visitGreaterOrEqual(fieldRef, from)
Expand All @@ -99,9 +110,12 @@ default CompletableFuture<Optional<GlobalIndexResult>> visitRange(
upper,
(left, right) -> {
if (!left.isPresent()) {
return right;
return right.map(result -> new GlobalIndexPredicateResult(result, false));
}
return right.isPresent() ? Optional.of(left.get().and(right.get())) : left;
return Optional.of(
new GlobalIndexPredicateResult(
right.isPresent() ? left.get().and(right.get()) : left.get(),
right.isPresent()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ default boolean supportsFullTextSearch() {
return false;
}

/**
* Whether a grouped index definition can serve independent scalar lookups on its fields.
*
* <p>Grouped fields are queried independently, unlike the ordered columns of a composite key.
* Readers may still decline individual predicates; coverage must include only the shards that
* actually evaluated the predicate.
*/
default boolean supportsGroupedIndexScalarQueries() {
return false;
}

/**
* Select index files using metadata only. Implementations must retain every file that may
* match; the default keeps all files when no safe pruning is available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public static GlobalIndexerFactory load(String type) {
return globalIndexerFactory;
}

/** Unknown types must not opt grouped definitions into independent scalar lookups. */
public static boolean supportsGroupedIndexScalarQueries(String type) {
GlobalIndexerFactory factory = factories.get(type);
return factory != null && factory.supportsGroupedIndexScalarQueries();
}

/** Keep unrecognized index types for the reader to resolve at execution time. */
public static List<GlobalIndexIOMeta> selectFiles(
String type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ public CompletableFuture<Optional<GlobalIndexResult>> visitRange(
.thenApply(this::applyOffset);
}

@Override
public CompletableFuture<Optional<GlobalIndexPredicateResult>> visitRangeWithCompleteness(
FieldRef fieldRef, Object from, Object to, boolean fromInclusive, boolean toInclusive) {
return wrapped.visitRangeWithCompleteness(fieldRef, from, to, fromInclusive, toInclusive)
.thenApply(
result ->
result.map(
value ->
new GlobalIndexPredicateResult(
value.result().offset(offset),
value.fullyEvaluated())));
}

@Override
public CompletableFuture<Optional<GlobalIndexResult>> visitBetween(
FieldRef fieldRef, Object from, Object to) {
Expand Down
Loading
Loading