-
Notifications
You must be signed in to change notification settings - Fork 535
[spark] Add scan.max.records.per.partition config to split log table input partitions #3260
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import org.apache.fluss.client.table.scanner.log.LogScanner | |
| import org.apache.fluss.config.Configuration | ||
| import org.apache.fluss.metadata.{PartitionInfo, TableBucket, TableInfo, TablePath} | ||
| import org.apache.fluss.predicate.Predicate | ||
| import org.apache.fluss.spark.SparkFlussConf | ||
|
|
||
| import org.apache.spark.sql.connector.read.{Batch, InputPartition, PartitionReaderFactory} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
@@ -91,26 +92,64 @@ class FlussAppendBatch( | |
| } | ||
|
|
||
| override def planInputPartitions(): Array[InputPartition] = { | ||
| val bucketOffsetsRetrieverImpl = new BucketOffsetsRetrieverImpl(admin, tablePath) | ||
| val maxRecordsPerPartition: Option[Long] = { | ||
| val opt = flussConfig.getOptional(SparkFlussConf.SCAN_MAX_RECORDS_PER_PARTITION) | ||
| if (opt.isPresent) Some(opt.get().longValue()) else None | ||
| } | ||
|
|
||
| val bucketOffsetsRetrieverImpl = maxRecordsPerPartition match { | ||
| case Some(_) => new BucketOffsetsRetrieverImpl(admin, tablePath, true) | ||
| case None => new BucketOffsetsRetrieverImpl(admin, tablePath) | ||
| } | ||
| val buckets = (0 until tableInfo.getNumBuckets).toSeq | ||
|
|
||
| def splitOffsetRange( | ||
| tableBucket: TableBucket, | ||
| startOffset: Long, | ||
| stopOffset: Long, | ||
| maxRecords: Long): Seq[InputPartition] = { | ||
| if ( | ||
| startOffset < 0 || stopOffset <= startOffset || stopOffset <= (startOffset + maxRecords) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the earliest mode we have sentinel -2L, I think it would result in a bug here, since we clamp to 1 split |
||
| ) { | ||
| return Seq( | ||
| FlussAppendInputPartition(tableBucket, startOffset, stopOffset) | ||
| .asInstanceOf[InputPartition]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think it's redundant |
||
| } | ||
| val rangeSize = stopOffset - startOffset | ||
| val numSplits = ((rangeSize + maxRecords - 1) / maxRecords).toInt | ||
| val step = (rangeSize + numSplits - 1) / numSplits | ||
|
|
||
| Iterator | ||
| .from(0) | ||
| .take(numSplits) | ||
| .map(i => startOffset + i * step) | ||
| .map { | ||
| from => | ||
| FlussAppendInputPartition(tableBucket, from, math.min(from + step, stopOffset)) | ||
| .asInstanceOf[InputPartition] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| } | ||
| .toSeq | ||
| } | ||
|
|
||
| def createPartitions( | ||
| partitionId: Option[Long], | ||
| startBucketOffsets: Map[Integer, Long], | ||
| stoppingBucketOffsets: Map[Integer, Long]): Array[InputPartition] = { | ||
| buckets.map { | ||
| buckets.flatMap { | ||
| bucketId => | ||
| val (startBucketOffset, stoppingBucketOffset) = | ||
| val (startOffset, stopOffset) = | ||
| (startBucketOffsets(bucketId), stoppingBucketOffsets(bucketId)) | ||
| partitionId match { | ||
| case Some(partitionId) => | ||
| val tableBucket = new TableBucket(tableInfo.getTableId, partitionId, bucketId) | ||
| FlussAppendInputPartition(tableBucket, startBucketOffset, stoppingBucketOffset) | ||
| .asInstanceOf[InputPartition] | ||
| val tableBucket = partitionId match { | ||
| case Some(pid) => new TableBucket(tableInfo.getTableId, pid, bucketId) | ||
| case None => new TableBucket(tableInfo.getTableId, bucketId) | ||
| } | ||
| maxRecordsPerPartition match { | ||
| case Some(maxRecs) => | ||
| splitOffsetRange(tableBucket, startOffset, stopOffset, maxRecs) | ||
| case None => | ||
| val tableBucket = new TableBucket(tableInfo.getTableId, bucketId) | ||
| FlussAppendInputPartition(tableBucket, startBucketOffset, stoppingBucketOffset) | ||
| .asInstanceOf[InputPartition] | ||
| Seq( | ||
| FlussAppendInputPartition(tableBucket, startOffset, stopOffset) | ||
| .asInstanceOf[InputPartition]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| } | ||
| }.toArray | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -524,4 +524,13 @@ class SparkLogTableReadTest extends FlussSparkTestBase { | |
| assert(numRowsRead == 5L, s"Expected 5 rows read, got $numRowsRead") | ||
| } | ||
| } | ||
|
|
||
| test("Spark Read: split partition by config") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test only checks row order/values, not partition count. Also what about partitioned tables? Earliest mode? |
||
| withSampleTable { | ||
| withSQLConf("spark.sql.fluss.scan.max.records.per.partition" -> "2") { | ||
| val query = sql(s"SELECT amount FROM $DEFAULT_DATABASE.t ORDER BY orderId") | ||
| checkAnswer(query, Row(601) :: Row(602) :: Row(603) :: Row(604) :: Row(605) :: Nil) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
mb better to use primitive, since it doesn't allow null