-
Notifications
You must be signed in to change notification settings - Fork 17
6138 - Spark client #6137
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
Open
gaffer01
wants to merge
35
commits into
develop
Choose a base branch
from
spark-client
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
6138 - Spark client #6137
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
cded5e6
Initial work on reading a Sleeper table as a Spark Dataframe
gaffer01 5a2103c
Merge branch 'develop' into spark-client
gaffer01 03b75cd
Tidying spark classes
gaffer01 82fe0c4
Added ability to read as columnar batches
gaffer01 7f10d1d
Handle more Filter types in SleeperScanBuilder
gaffer01 e809b1c
Merge branch 'develop' into spark-client
gaffer01 0eb0a28
Merge branch 'develop' into spark-client
gaffer01 2815cd7
Spark - refactor methods to create regions matching filters
gaffer01 e56cd20
Merge branch 'develop' into spark-client
gaffer01 842adc8
Spark - more refactoring
gaffer01 daa514b
Merge branch 'develop' into spark-client
gaffer01 8eb725f
Merge branch 'develop' into spark-client
gaffer01 0357738
Merge branch 'develop' into spark-client
gaffer01 4b453d4
Spark - more tests
gaffer01 b9fc68e
Spark - code tidying and Javadoc
gaffer01 ac56c45
Spark - fix checkstyle issues
gaffer01 888f972
Spark - rename package
gaffer01 47e51cc
Spark - add unit test for FindFiltersToPush
gaffer01 70747b2
Spark - add unit test for SplitPushedFiltersIntoSingleAndMultiRegionF…
gaffer01 5437042
Spark - code tidy
gaffer01 97a403a
Merge branch 'develop' into spark-client
gaffer01 96c5106
Spark - minor fix
gaffer01 19f3632
Merge branch 'develop' into spark-client
gaffer01 1344eb3
Spark - Update pom.xml to 0.35.0-SNAPSHOT
gaffer01 43d5662
Update to chunk configuration
rtjd6554 06f023d
Merge branch 'develop' into spark-client
gaffer01 ab756e9
Merge branch 'develop' into spark-client
gaffer01 44239a4
Merge branch 'develop' into spark-client
gaffer01 f2087ea
Spark - remove dependency on clients module
gaffer01 b987509
Merge branch 'develop' into spark-client
gaffer01 300cc26
Merge branch 'develop' into spark-client
gaffer01 6c7f942
Spark - fixes based on comments on PR
gaffer01 7c5b73d
Merge branch 'develop' into spark-client
gaffer01 6118739
Merge branch 'develop' into spark-client
gaffer01 0057eb4
Spark - changes based on PR comments
gaffer01 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
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
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
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,86 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Copyright 2022-2025 Crown Copyright | ||
| ~ | ||
| ~ Licensed 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>aws</artifactId> | ||
| <groupId>sleeper</groupId> | ||
| <version>0.35.0-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>spark</artifactId> | ||
|
|
||
| <dependencies> | ||
| <!-- Spark --> | ||
| <dependency> | ||
| <groupId>org.apache.spark</groupId> | ||
| <artifactId>spark-sql_${scala.version}</artifactId> | ||
| <scope>provided</scope> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>org.apache.hadoop</groupId> | ||
| <artifactId>*</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
| <!-- Sleeper dependencies --> | ||
| <dependency> | ||
| <groupId>sleeper</groupId> | ||
| <artifactId>configuration</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>sleeper</groupId> | ||
| <artifactId>query-datafusion</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>sleeper</groupId> | ||
| <artifactId>statestore</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
patchwork01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-shade-plugin</artifactId> | ||
| <configuration> | ||
| <relocations> | ||
| <relocation> | ||
| <pattern>com.google</pattern> | ||
| <shadedPattern>sleeper.shaded.com.google</shadedPattern> | ||
| <excludes> | ||
| <exclude>com.google.common.util.concurrent.*</exclude> | ||
| </excludes> | ||
| </relocation> | ||
| <relocation> | ||
| <pattern>org.apache.parquet</pattern> | ||
| <shadedPattern>sleeper.shaded.apache.parquet</shadedPattern> | ||
| </relocation> | ||
| <relocation> | ||
| <pattern>shaded.parquet</pattern> | ||
| <shadedPattern>sleeper.shaded.parquet.shaded</shadedPattern> | ||
| </relocation> | ||
| </relocations> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
198 changes: 198 additions & 0 deletions
198
java/spark/src/main/java/sleeper/spark/CreateRegionFromFilter.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,198 @@ | ||
| /* | ||
| * Copyright 2022-2025 Crown Copyright | ||
| * | ||
| * Licensed 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 sleeper.spark; | ||
|
|
||
| import org.apache.spark.sql.sources.EqualTo; | ||
| import org.apache.spark.sql.sources.Filter; | ||
| import org.apache.spark.sql.sources.GreaterThan; | ||
| import org.apache.spark.sql.sources.GreaterThanOrEqual; | ||
| import org.apache.spark.sql.sources.In; | ||
| import org.apache.spark.sql.sources.LessThan; | ||
| import org.apache.spark.sql.sources.LessThanOrEqual; | ||
| import org.apache.spark.sql.sources.Or; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import sleeper.core.range.Range; | ||
| import sleeper.core.range.Range.RangeFactory; | ||
| import sleeper.core.range.Region; | ||
| import sleeper.core.schema.Field; | ||
| import sleeper.core.schema.Schema; | ||
| import sleeper.core.schema.type.PrimitiveType; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Creates Regions from filters. | ||
| */ | ||
| public class CreateRegionFromFilter { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(CreateRegionFromFilter.class); | ||
|
|
||
| private CreateRegionFromFilter() { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Converts a filter into an optional list of regions corresponding to that filter. | ||
| * | ||
| * @param filter the filter | ||
| * @param schema the schema of the Sleeper table | ||
| * @return an optional list of regions corresponding to the provided filter | ||
| */ | ||
| public static Optional<List<Region>> createRegionsFromFilter(Filter filter, Schema schema) { | ||
| if (filter instanceof Or) { | ||
| Or or = (Or) filter; | ||
| Region leftRegion = createRegionFromSimpleFilter(or.left(), schema); | ||
| Region rightRegion = createRegionFromSimpleFilter(or.right(), schema); | ||
| return Optional.of(List.of(leftRegion, rightRegion)); | ||
| } else if (filter instanceof In) { | ||
| In in = (In) filter; | ||
| List<Region> regions = new ArrayList<>(); | ||
| Object[] values = in.values(); | ||
| for (Object value : values) { | ||
| Filter equalFilter = new EqualTo(in.attribute(), value); | ||
| Region optional = createRegionFromSimpleFilter(equalFilter, schema); | ||
| regions.add(optional); | ||
| } | ||
| if (regions.isEmpty()) { | ||
| return Optional.empty(); | ||
| } else { | ||
| return Optional.of(regions); | ||
| } | ||
| } else { | ||
| Region optionalRegion = createRegionFromSimpleFilter(filter, schema); | ||
| return Optional.of(List.of(optionalRegion)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A region corresponding to the provided filter. | ||
| * | ||
| * @param filter the filter | ||
| * @param schema the schema of the Sleeper table | ||
| * @return a region corresponding to the provided filter | ||
| */ | ||
| public static Region createRegionFromSimpleFilter(Filter filter, Schema schema) { | ||
| MutableRegion mutableRegion = new MutableRegion(schema); | ||
| updateRegionWithFilter(mutableRegion, filter, new HashSet<>(schema.getRowKeyFieldNames())); | ||
| Region region = mutableRegion.getRegion(); | ||
| LOGGER.debug("Filter {} was converted to Region {}", filter, region); | ||
| return region; | ||
| } | ||
|
|
||
| /** | ||
| * An internal class used to allow a region to be updated as filters are added. | ||
| */ | ||
| private static class MutableRegion { | ||
| private final Map<String, MutableRange> rowKeyToMutableRange; | ||
|
|
||
| MutableRegion(Schema schema) { | ||
| this.rowKeyToMutableRange = new HashMap<>(); | ||
| RangeFactory rangeFactory = new RangeFactory(schema); | ||
| for (Field rowKeyField : schema.getRowKeyFields()) { | ||
| this.rowKeyToMutableRange.put(rowKeyField.getName(), new MutableRange(rowKeyField, rangeFactory)); | ||
| } | ||
| } | ||
|
|
||
| MutableRange getMutableRange(String rowKeyFieldName) { | ||
| return rowKeyToMutableRange.get(rowKeyFieldName); | ||
| } | ||
|
|
||
| Region getRegion() { | ||
| return new Region(rowKeyToMutableRange.values().stream().map(mr -> mr.getRange()).collect(Collectors.toList())); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An internal class used to allow a range to be updated as filters are added. | ||
| */ | ||
| private static class MutableRange { | ||
| private final String fieldName; | ||
| private final RangeFactory rangeFactory; | ||
| Object min; | ||
| boolean minInclusive = true; | ||
| Object max = null; | ||
| boolean maxInclusive = false; | ||
|
|
||
| MutableRange(Field field, RangeFactory rangeFactory) { | ||
| this.fieldName = field.getName(); | ||
| this.rangeFactory = rangeFactory; | ||
| this.min = PrimitiveType.getMinimum(field.getType()); | ||
| } | ||
|
|
||
| Range getRange() { | ||
| return rangeFactory.createRange(fieldName, min, minInclusive, max, maxInclusive); | ||
| } | ||
| } | ||
|
|
||
| private static void updateRegionWithFilter(MutableRegion mutableRegion, Filter filter, Set<String> rowKeyFieldNames) { | ||
| if (filter instanceof EqualTo) { | ||
| String fieldName = ((EqualTo) filter).attribute(); | ||
| if (!rowKeyFieldNames.contains(fieldName)) { | ||
| return; | ||
| } | ||
| Object wantedKey = ((EqualTo) filter).value(); | ||
| MutableRange mutableRange = mutableRegion.getMutableRange(fieldName); | ||
| mutableRange.minInclusive = true; | ||
| mutableRange.min = wantedKey; | ||
| mutableRange.maxInclusive = true; | ||
| mutableRange.max = wantedKey; | ||
| } else if (filter instanceof GreaterThan) { | ||
| String fieldName = ((GreaterThan) filter).attribute(); | ||
| if (!rowKeyFieldNames.contains(fieldName)) { | ||
| return; | ||
| } | ||
| Object minimumKey = ((GreaterThan) filter).value(); | ||
| MutableRange mutableRange = mutableRegion.getMutableRange(fieldName); | ||
| mutableRange.minInclusive = false; | ||
| mutableRange.min = minimumKey; | ||
| } else if (filter instanceof GreaterThanOrEqual) { | ||
| String fieldName = ((GreaterThanOrEqual) filter).attribute(); | ||
| if (!rowKeyFieldNames.contains(fieldName)) { | ||
| return; | ||
| } | ||
| Object minimumKey = ((GreaterThanOrEqual) filter).value(); | ||
| MutableRange mutableRange = mutableRegion.getMutableRange(fieldName); | ||
| mutableRange.minInclusive = true; | ||
| mutableRange.min = minimumKey; | ||
| } else if (filter instanceof LessThan) { | ||
| String fieldName = ((LessThan) filter).attribute(); | ||
| if (!rowKeyFieldNames.contains(fieldName)) { | ||
| return; | ||
| } | ||
| Object maxiumKey = ((LessThan) filter).value(); | ||
| MutableRange mutableRange = mutableRegion.getMutableRange(fieldName); | ||
| mutableRange.maxInclusive = false; | ||
| mutableRange.max = maxiumKey; | ||
| } else if (filter instanceof LessThanOrEqual) { | ||
| String fieldName = ((LessThanOrEqual) filter).attribute(); | ||
| if (!rowKeyFieldNames.contains(fieldName)) { | ||
| return; | ||
| } | ||
| Object maxiumKey = ((LessThanOrEqual) filter).value(); | ||
| MutableRange mutableRange = mutableRegion.getMutableRange(fieldName); | ||
| mutableRange.maxInclusive = true; | ||
| mutableRange.max = maxiumKey; | ||
| } | ||
| } | ||
| } |
87 changes: 87 additions & 0 deletions
87
java/spark/src/main/java/sleeper/spark/CreateRegionsFromPushedFilters.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,87 @@ | ||
| /* | ||
| * Copyright 2022-2025 Crown Copyright | ||
| * | ||
| * Licensed 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 sleeper.spark; | ||
|
|
||
| import org.apache.spark.sql.sources.Filter; | ||
|
|
||
| import sleeper.core.range.Range.RangeFactory; | ||
| import sleeper.core.range.Region; | ||
| import sleeper.core.schema.Schema; | ||
| import sleeper.spark.SplitPushedFiltersIntoSingleAndMultiRegionFilters.SingleAndMultiRegionFilters; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Used to identify a list of the minimum Regions that the pushed filters correspond to. | ||
| */ | ||
| public class CreateRegionsFromPushedFilters { | ||
| private final Schema schema; | ||
| private final RangeFactory rangeFactory; | ||
|
|
||
| public CreateRegionsFromPushedFilters(Schema schema) { | ||
| this.schema = schema; | ||
| this.rangeFactory = new RangeFactory(schema); | ||
| } | ||
|
|
||
| /** | ||
| * Given the filters that have been pushed to Sleeper, identifies the minimum list of regions that need to | ||
| * be read to return results corresponding to those filters. Note that this is not a one-to-one map between | ||
| * the pushedFilters array and the list of regions. | ||
| * | ||
| * @param pushedFilters the filters that have been pushed to Sleeper | ||
| * @return a list of the smallest regions that correspond to the filters | ||
| */ | ||
| public List<Region> getMinimumRegionCoveringPushedFilters(Filter[] pushedFilters) { | ||
| SplitPushedFiltersIntoSingleAndMultiRegionFilters split = new SplitPushedFiltersIntoSingleAndMultiRegionFilters(); | ||
| SingleAndMultiRegionFilters singleAndMultiRegionFilters = split.splitPushedFilters(pushedFilters); | ||
| Region regionFromSingleRegionFilters = getRegionFromSingleRegionFilters(singleAndMultiRegionFilters.getSingleRegionFilters()); | ||
| List<Region> regionsFromMultiRegionFilters = getRegionsFromMultiRegionFilters(singleAndMultiRegionFilters.getMultiRegionFilters()); | ||
| if (!regionsFromMultiRegionFilters.isEmpty()) { | ||
| List<Region> regions = new ArrayList<>(); | ||
| for (Region region : regionsFromMultiRegionFilters) { | ||
| Optional<Region> optionalIntersectedRegion = RegionIntersector.intersectRegions(region, regionFromSingleRegionFilters, rangeFactory, schema); | ||
| if (optionalIntersectedRegion.isPresent()) { | ||
| regions.add(optionalIntersectedRegion.get()); | ||
| } | ||
| } | ||
| return regions; | ||
| } else { | ||
| return List.of(regionFromSingleRegionFilters); | ||
| } | ||
| } | ||
|
|
||
| private Region getRegionFromSingleRegionFilters(List<Filter> singleRegionFilters) { | ||
| if (singleRegionFilters == null || singleRegionFilters.isEmpty()) { | ||
| return Region.coveringAllValuesOfAllRowKeys(schema); | ||
| } | ||
| Region intersectedRegion = CreateRegionFromFilter.createRegionFromSimpleFilter(singleRegionFilters.get(0), schema); | ||
| for (int i = 1; i < singleRegionFilters.size(); i++) { | ||
| Region region = CreateRegionFromFilter.createRegionFromSimpleFilter(singleRegionFilters.get(i), schema); | ||
| intersectedRegion = RegionIntersector.intersectRegions(intersectedRegion, region, new RangeFactory(schema), schema).get(); | ||
| } | ||
| return intersectedRegion; | ||
| } | ||
|
|
||
| private List<Region> getRegionsFromMultiRegionFilters(List<Filter> multiRegionFilters) { | ||
| List<Region> regions = new ArrayList<>(); | ||
| for (Filter filter : multiRegionFilters) { | ||
| regions.addAll(CreateRegionFromFilter.createRegionsFromFilter(filter, schema).get()); | ||
| } | ||
| return regions; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Would it be worth making a parent module for clients? We could put both this and the current clients module under that. The naming might be awkward, I suppose it could be client/spark-client and client/clients.
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.
I propose we leave it as it is in this PR but later move this, the Athena and Trino code to one parent module: #6251