Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.spark.sql.connector.read;

import java.util.OptionalLong;

/**
* A mix-in for input partitions whose records are clustered on the same set of partition keys
* (provided via {@link SupportsReportPartitioning}, see below). Data sources can opt-in to
* implement this interface for the partitions they report to Spark, which will use the info
* to decide whether partition grouping should be applied or not.
*
* @see org.apache.spark.sql.connector.read.SupportsReportPartitioning
* @since 4.0.0
*/
public interface HasPartitionSize extends InputPartition {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can make this more general and support partition level stats as well, like number of rows.

@zhuqi-lucas zhuqi-lucas Mar 6, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @sunchao for review, and this is a good suggestion, i check the Iceberg code, it includes the sizeBytes and estimatedRowsCount, filesCount. Let me address this!

  @Override
  default long sizeBytes() {
    return tasks().stream().mapToLong(ScanTask::sizeBytes).sum();
  }

  @Override
  default long estimatedRowsCount() {
    return tasks().stream().mapToLong(ScanTask::estimatedRowsCount).sum();
  }

  @Override
  default int filesCount() {
    return tasks().stream().mapToInt(ScanTask::filesCount).sum();
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in latest PR.

/**
* Returns the value of the partition size associated to this partition.
*/
OptionalLong partitionSizeInBytes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ object InMemoryBaseTable {
}

class BufferedRows(val key: Seq[Any] = Seq.empty) extends WriterCommitMessage
with InputPartition with HasPartitionKey with Serializable {
with InputPartition with HasPartitionKey with HasPartitionSize with Serializable {
val rows = new mutable.ArrayBuffer[InternalRow]()
val deletes = new mutable.ArrayBuffer[Int]()

Expand All @@ -617,7 +617,7 @@ class BufferedRows(val key: Seq[Any] = Seq.empty) extends WriterCommitMessage
def keyString(): String = key.toArray.mkString("/")

override def partitionKey(): InternalRow = PartitionInternalRow(key.toArray)

override def partitionSizeInBytes(): OptionalLong = OptionalLong.of(100L)
def clear(): Unit = rows.clear()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,39 @@ class DataSourceV2SQLSuiteV1Filter
}
}

test("Check HasPartitionSize from InMemoryPartitionTable") {
val t = "testpart.tbl"
withTable(t) {
sql(s"CREATE TABLE $t (id string) USING foo PARTITIONED BY (key int)")
val table = catalog("testpart").asTableCatalog
.loadTable(Identifier.of(Array(), "tbl"))
.asInstanceOf[InMemoryPartitionTable]

var partSizes = table.data.map(_.partitionSizeInBytes().getAsLong)
assert(partSizes.length == 0)

sql(s"INSERT INTO $t VALUES ('a', 1), ('b', 2), ('c', 3)")
partSizes = table.data.map(_.partitionSizeInBytes().getAsLong)
assert(partSizes.length == 3)
assert(partSizes.toSet == Set(100, 100, 100))

sql(s"ALTER TABLE $t DROP PARTITION (key=3)")
partSizes = table.data.map(_.partitionSizeInBytes().getAsLong)
assert(partSizes.length == 2)
assert(partSizes.toSet == Set(100, 100))

sql(s"ALTER TABLE $t ADD PARTITION (key=4)")
partSizes = table.data.map(_.partitionSizeInBytes().getAsLong)
assert(partSizes.length == 3)
assert(partSizes.toSet == Set(100, 100, 100))

sql(s"INSERT INTO $t VALUES ('c', 3), ('e', 5)")
partSizes = table.data.map(_.partitionSizeInBytes().getAsLong)
assert(partSizes.length == 5)
assert(partSizes.toSet == Set(100, 100, 100, 100, 100))
}
}

test("time travel") {
sql("use testcat")
// The testing in-memory table simply append the version/timestamp to the table name when
Expand Down