Skip to content

Commit cc3b49c

Browse files
committed
CASSANALYTICS-171: Avoid Spark 4 partitioning warnings during reads
Spark 4 ignores custom DataSource V2 Partitioning implementations and logs a warning. Cassandra scan partitions are token ranges rather than keyed groups, so report UnknownPartitioning while preserving the input partition count.
1 parent 87c553c commit cc3b49c

4 files changed

Lines changed: 59 additions & 14 deletions

File tree

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
0.5.0
22
-----
3+
* Avoid Spark 4 partitioning warnings during bulk reads (CASSANALYTICS-171)
34
* Spark 4.0 Support (CASSANALYTICS-34)
45
* Add IAM credential support for S3 storage transport (CASSANALYTICS-155)
56
* Make BulkWriterConfig extensible (CASSANALYTICS-168)

cassandra-analytics-core/src/main/spark4/org/apache/cassandra/spark/sparksql/CassandraPartitioning.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,16 @@
1919

2020
package org.apache.cassandra.spark.sparksql;
2121

22-
import org.apache.cassandra.spark.data.DataLayer;
23-
import org.apache.spark.sql.connector.read.partitioning.Partitioning;
22+
import org.apache.spark.sql.connector.read.partitioning.UnknownPartitioning;
2423

25-
class CassandraPartitioning implements Partitioning
24+
/**
25+
* Cassandra input partitions are token ranges, not groups of rows sharing one partition key value,
26+
* so the connector reports Spark's unknown partitioning rather than keyed-group partitioning.
27+
*/
28+
class CassandraPartitioning extends UnknownPartitioning
2629
{
27-
final DataLayer dataLayer;
28-
29-
CassandraPartitioning(DataLayer dataLayer)
30-
{
31-
this.dataLayer = dataLayer;
32-
}
33-
34-
@Override
35-
public int numPartitions()
30+
CassandraPartitioning(int numPartitions)
3631
{
37-
return dataLayer.partitionCount();
32+
super(numPartitions);
3833
}
3934
}

cassandra-analytics-core/src/main/spark4/org/apache/cassandra/spark/sparksql/CassandraScanBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public PartitionReaderFactory createReaderFactory()
121121
@Override
122122
public Partitioning outputPartitioning()
123123
{
124-
return new CassandraPartitioning(dataLayer);
124+
return new CassandraPartitioning(dataLayer.partitionCount());
125125
}
126126

127127
private List<PartitionKeyFilter> buildPartitionKeyFilters()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.cassandra.spark.sparksql;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.apache.cassandra.spark.data.DataLayer;
25+
import org.apache.spark.sql.connector.read.partitioning.Partitioning;
26+
import org.apache.spark.sql.connector.read.partitioning.UnknownPartitioning;
27+
import org.apache.spark.sql.types.StructType;
28+
import org.apache.spark.sql.util.CaseInsensitiveStringMap;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.when;
33+
34+
class CassandraScanBuilderTest
35+
{
36+
@Test
37+
void outputPartitioningReportsUnknownPartitioningWithPartitionCount()
38+
{
39+
DataLayer dataLayer = mock(DataLayer.class);
40+
when(dataLayer.partitionCount()).thenReturn(7);
41+
CassandraScanBuilder builder =
42+
new CassandraScanBuilder(dataLayer, new StructType(), CaseInsensitiveStringMap.empty());
43+
44+
Partitioning partitioning = builder.outputPartitioning();
45+
46+
assertThat(partitioning).isInstanceOf(UnknownPartitioning.class);
47+
assertThat(partitioning.numPartitions()).isEqualTo(7);
48+
}
49+
}

0 commit comments

Comments
 (0)