AngersZhuuuu commented on a change in pull request #25602: [SPARK-28613][SQL] 
Add config option for limiting uncompressed result size in SQL
URL: https://github.com/apache/spark/pull/25602#discussion_r321577763
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/SizeLimitingByteArrayDecoder.scala
 ##########
 @@ -0,0 +1,80 @@
+/*
+ * 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.execution
+
+import java.io.{ByteArrayInputStream, DataInputStream}
+
+import org.apache.spark.{SparkEnv, SparkException}
+import org.apache.spark.internal.{config, Logging}
+import org.apache.spark.io.CompressionCodec
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.UnsafeRow
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.util.Utils
+
+/**
+ * Provides methods for converting compressed byte arrays back to UnsafeRows.
+ * Additionally, can enforce a limit on the total, decoded size of all decoded 
UnsafeRows.
+ * Enforcing the limit is controlled via a sql config and if it is turned on 
the encoder will
+ * throw a SparkException when the limit is reached.
+ */
+private[spark] class SizeLimitingByteArrayDecoder(
+     nFields: Int,
+     sqlConf: SQLConf) extends Logging {
+  private var totalUncompressedResultSize = 0L
+  private val maxUncompressedResultSize = sqlConf.maxUncompressedResultSize
+
+  /**
+   * Decodes the byte arrays back to UnsafeRows and puts them into buffer.
+   */
+  def decodeUnsafeRows(bytes: Array[Byte]): Iterator[InternalRow] = {
+    val codec = CompressionCodec.createCodec(SparkEnv.get.conf)
+    val bis = new ByteArrayInputStream(bytes)
+    val ins = new DataInputStream(codec.compressedInputStream(bis))
+
+    new Iterator[InternalRow] {
+      private var sizeOfNextRow = ins.readInt()
+
+      override def hasNext: Boolean = sizeOfNextRow >= 0
+
+      override def next(): InternalRow = {
+        ensureCanFetchMoreResults(sizeOfNextRow)
+        val bs = new Array[Byte](sizeOfNextRow)
+        ins.readFully(bs)
+        val row = new UnsafeRow(nFields)
+        row.pointTo(bs, sizeOfNextRow)
+        sizeOfNextRow = ins.readInt()
+        row
+      }
+    }
+  }
+
+  private def ensureCanFetchMoreResults(sizeOfNextRow: Int): Unit = {
+    totalUncompressedResultSize += sizeOfNextRow
 
 Review comment:
   > This is not more helpful than OOM can tell you. You still can't process 
the collected data...
   
   I meet this problem in SparkThriftServer. it's will make server hangs. 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to