uros-b commented on code in PR #56550:
URL: https://github.com/apache/spark/pull/56550#discussion_r3431816808


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala:
##########
@@ -354,6 +354,60 @@ object JdbcUtils extends Logging with SQLConfHelper {
     internalRows.map(fromRow)
   }
 
+  /**
+   * Estimates the size in bytes of a row given its schema. For 
variable-length types
+   * (String, Binary), uses actual value size; for fixed-width types, uses 
defaultSize.
+   * Null fields contribute 0 bytes.
+   */
+  private[jdbc] def estimateInternalRowSize(row: InternalRow, schema: 
StructType): Long = {
+    var size = 0L
+    var i = 0
+    while (i < schema.length) {
+      if (!row.isNullAt(i)) {
+        schema.fields(i).dataType match {
+          case _: StringType =>
+            size += row.getUTF8String(i).numBytes()
+          case BinaryType =>
+            size += row.getBinary(i).length
+          case at: ArrayType =>
+            size += row.getArray(i).numElements().toLong * 
at.elementType.defaultSize

Review Comment:
   One note that's worth calling out here: ArrayType estimation is a bit coarse 
for variable-length elements. Namely, for arrays of strings or binary, 
defaultSize is 20 and 100 respectively, so not quite the actual element sizes. 
JDBC does support arrays (Postgres, etc.), so metrics for ARRAY(TEXT) or 
ARRAY(BYTEA) could potentially be significantly off.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to