-
Notifications
You must be signed in to change notification settings - Fork 3.1k
push down min/max/count to iceberg #6622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
634826f
push down min/max/count to iceberg
huaxingao 2b6137b
nit
huaxingao 20697e8
remove unused import
huaxingao 4886e55
fix test failure
huaxingao b23da4f
Co-Authored-By: Ryan Blue<blue@apache.org>
huaxingao 02ec7fb
get snapshotId from readConf for time travel
huaxingao 9a2b736
address comments
huaxingao 93a5440
not push down complex type + remove NaN count
huaxingao cb4e5c7
fix
huaxingao fb5e9aa
spotlessApply
huaxingao c0abc76
get data files from scan planning + better error message
huaxingao 7f6715c
address comments
huaxingao e4919e5
fix style violation
huaxingao 14c5937
address comments
huaxingao 9f72b0f
add hasValue to indicate if the Aggregator has enough stats to calcul…
huaxingao 91bfe6d
address comments
huaxingao 8e63b57
fix
huaxingao fc74696
fix check style error
huaxingao 6922888
address comments
huaxingao d659a51
address comments
huaxingao caebfae
fix build failure
huaxingao a9ec2a3
address comments
huaxingao c371e70
fix test failure
huaxingao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
api/src/main/java/org/apache/iceberg/expressions/AggregateEvaluator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * 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.iceberg.expressions; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.StructLike; | ||
| import org.apache.iceberg.expressions.BoundAggregate.Aggregator; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
||
| /** | ||
| * A class for evaluating aggregates. It evaluates each of the aggregates and updates the aggregated | ||
| * value. The final aggregated result can be returned by {@link #result()}. | ||
| */ | ||
| public class AggregateEvaluator { | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public static AggregateEvaluator create(Schema schema, List<Expression> aggregates) { | ||
| return create(schema.asStruct(), aggregates); | ||
| } | ||
|
|
||
| public static AggregateEvaluator create(List<BoundAggregate<?, ?>> aggregates) { | ||
| return new AggregateEvaluator(aggregates); | ||
| } | ||
|
|
||
| private static AggregateEvaluator create(Types.StructType struct, List<Expression> aggregates) { | ||
| List<BoundAggregate<?, ?>> boundAggregates = | ||
| aggregates.stream() | ||
| .map(expr -> Binder.bind(struct, expr)) | ||
| .map(bound -> (BoundAggregate<?, ?>) bound) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new AggregateEvaluator(boundAggregates); | ||
| } | ||
|
|
||
| private final List<Aggregator<?>> aggregators; | ||
| private final Types.StructType resultType; | ||
| private final List<BoundAggregate<?, ?>> aggregates; | ||
|
|
||
| private AggregateEvaluator(List<BoundAggregate<?, ?>> aggregates) { | ||
| ImmutableList.Builder<Aggregator<?>> aggregatorsBuilder = ImmutableList.builder(); | ||
| List<Types.NestedField> resultFields = Lists.newArrayList(); | ||
|
|
||
| for (int pos = 0; pos < aggregates.size(); pos += 1) { | ||
rdblue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| BoundAggregate<?, ?> aggregate = aggregates.get(pos); | ||
| aggregatorsBuilder.add(aggregate.newAggregator()); | ||
| resultFields.add(Types.NestedField.optional(pos, aggregate.describe(), aggregate.type())); | ||
| } | ||
|
|
||
| this.aggregators = aggregatorsBuilder.build(); | ||
| this.resultType = Types.StructType.of(resultFields); | ||
| this.aggregates = aggregates; | ||
| } | ||
|
|
||
| public void update(StructLike struct) { | ||
| for (Aggregator<?> aggregator : aggregators) { | ||
| aggregator.update(struct); | ||
| } | ||
| } | ||
|
|
||
| public void update(DataFile file) { | ||
| for (Aggregator<?> aggregator : aggregators) { | ||
| aggregator.update(file); | ||
| } | ||
| } | ||
|
|
||
| public Types.StructType resultType() { | ||
| return resultType; | ||
| } | ||
|
|
||
| public boolean allAggregatorsValid() { | ||
| return aggregators.stream().allMatch(BoundAggregate.Aggregator::isValid); | ||
| } | ||
|
|
||
| public StructLike result() { | ||
| Object[] results = | ||
| aggregators.stream().map(BoundAggregate.Aggregator::result).toArray(Object[]::new); | ||
| return new ArrayStructLike(results); | ||
| } | ||
|
|
||
| public List<BoundAggregate<?, ?>> aggregates() { | ||
| return aggregates; | ||
| } | ||
|
|
||
| private static class ArrayStructLike implements StructLike { | ||
| private final Object[] values; | ||
|
|
||
| private ArrayStructLike(Object[] values) { | ||
| this.values = values; | ||
| } | ||
|
|
||
| public int size() { | ||
| return values.length; | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T get(int pos, Class<T> javaClass) { | ||
| return javaClass.cast(values[pos]); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> void set(int pos, T value) { | ||
| values[pos] = value; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
api/src/main/java/org/apache/iceberg/expressions/CountAggregate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * 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.iceberg.expressions; | ||
|
|
||
| import org.apache.iceberg.DataFile; | ||
| import org.apache.iceberg.StructLike; | ||
|
|
||
| public class CountAggregate<T> extends BoundAggregate<T, Long> { | ||
| protected CountAggregate(Operation op, BoundTerm<T> term) { | ||
| super(op, term); | ||
| } | ||
|
|
||
| @Override | ||
| public Long eval(StructLike struct) { | ||
| return countFor(struct); | ||
| } | ||
|
|
||
| @Override | ||
| public Long eval(DataFile file) { | ||
| return countFor(file); | ||
| } | ||
|
|
||
| protected Long countFor(StructLike row) { | ||
| throw new UnsupportedOperationException( | ||
| this.getClass().getName() + " does not implement countFor(StructLike)"); | ||
| } | ||
|
|
||
| protected Long countFor(DataFile file) { | ||
| throw new UnsupportedOperationException( | ||
| this.getClass().getName() + " does not implement countFor(DataFile)"); | ||
| } | ||
|
|
||
| @Override | ||
| public Aggregator<Long> newAggregator() { | ||
| return new CountAggregator<>(this); | ||
| } | ||
|
|
||
| private static class CountAggregator<T> extends NullSafeAggregator<T, Long> { | ||
| private Long count = 0L; | ||
|
|
||
| CountAggregator(BoundAggregate<T, Long> aggregate) { | ||
| super(aggregate); | ||
| } | ||
|
|
||
| @Override | ||
| protected void update(Long value) { | ||
| count += value; | ||
| } | ||
|
|
||
| @Override | ||
| protected Long current() { | ||
| return count; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have to add this if we already have
includeColumnStatsdefined inScan?I think we should be able to use that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Sorry I didn't notice there is already an existing method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem at all. I forgot about that one initially too.