Skip to content
Open
Show file tree
Hide file tree
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 Nov 5, 2025
5a2103c
Merge branch 'develop' into spark-client
gaffer01 Nov 6, 2025
03b75cd
Tidying spark classes
gaffer01 Nov 10, 2025
82fe0c4
Added ability to read as columnar batches
gaffer01 Nov 10, 2025
7f10d1d
Handle more Filter types in SleeperScanBuilder
gaffer01 Nov 12, 2025
e809b1c
Merge branch 'develop' into spark-client
gaffer01 Nov 12, 2025
0eb0a28
Merge branch 'develop' into spark-client
gaffer01 Nov 12, 2025
2815cd7
Spark - refactor methods to create regions matching filters
gaffer01 Nov 21, 2025
e56cd20
Merge branch 'develop' into spark-client
gaffer01 Nov 21, 2025
842adc8
Spark - more refactoring
gaffer01 Nov 24, 2025
daa514b
Merge branch 'develop' into spark-client
gaffer01 Nov 24, 2025
8eb725f
Merge branch 'develop' into spark-client
gaffer01 Nov 27, 2025
0357738
Merge branch 'develop' into spark-client
gaffer01 Nov 28, 2025
4b453d4
Spark - more tests
gaffer01 Nov 28, 2025
b9fc68e
Spark - code tidying and Javadoc
gaffer01 Nov 28, 2025
ac56c45
Spark - fix checkstyle issues
gaffer01 Nov 28, 2025
888f972
Spark - rename package
gaffer01 Nov 28, 2025
47e51cc
Spark - add unit test for FindFiltersToPush
gaffer01 Nov 28, 2025
70747b2
Spark - add unit test for SplitPushedFiltersIntoSingleAndMultiRegionF…
gaffer01 Nov 28, 2025
5437042
Spark - code tidy
gaffer01 Nov 28, 2025
97a403a
Merge branch 'develop' into spark-client
gaffer01 Nov 28, 2025
96c5106
Spark - minor fix
gaffer01 Nov 28, 2025
19f3632
Merge branch 'develop' into spark-client
gaffer01 Dec 2, 2025
1344eb3
Spark - Update pom.xml to 0.35.0-SNAPSHOT
gaffer01 Dec 2, 2025
43d5662
Update to chunk configuration
rtjd6554 Dec 3, 2025
06f023d
Merge branch 'develop' into spark-client
gaffer01 Dec 3, 2025
ab756e9
Merge branch 'develop' into spark-client
gaffer01 Dec 5, 2025
44239a4
Merge branch 'develop' into spark-client
gaffer01 Dec 12, 2025
f2087ea
Spark - remove dependency on clients module
gaffer01 Dec 12, 2025
b987509
Merge branch 'develop' into spark-client
gaffer01 Dec 12, 2025
300cc26
Merge branch 'develop' into spark-client
gaffer01 Dec 16, 2025
6c7f942
Spark - fixes based on comments on PR
gaffer01 Dec 16, 2025
7c5b73d
Merge branch 'develop' into spark-client
gaffer01 Dec 16, 2025
6118739
Merge branch 'develop' into spark-client
gaffer01 Dec 18, 2025
0057eb4
Spark - changes based on PR comments
gaffer01 Dec 18, 2025
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
1 change: 1 addition & 0 deletions .github/config/chunks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ chunks:
workflow: chunk-clients-cdk.yaml
modules:
- clients
- spark
- deployment/cdk
- deployment/cdk-custom-resources
- deployment/cdk-environment
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/chunk-clients-cdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- 'java/deployment/pom.xml'
- 'java/system-test/pom.xml'
- 'java/clients/**'
- 'java/spark/**'
- 'java/deployment/cdk/**'
- 'java/deployment/cdk-custom-resources/**'
- 'java/deployment/cdk-environment/**'
Expand Down
1 change: 1 addition & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>statestore-lambda</module>
<module>system-test</module>
<module>trino</module>
<module>spark</module>
</modules>

<properties>
Expand Down
86 changes: 86 additions & 0 deletions java/spark/pom.xml
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>
Copy link
Collaborator

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.

Copy link
Member Author

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


<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>
</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 java/spark/src/main/java/sleeper/spark/CreateRegionFromFilter.java
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;
}
}
}
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;
}
}
Loading
Loading