You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2023/04/13 00:36:08 UTC

[GitHub] [spark] zhengruifeng commented on a diff in pull request #40750: [SPARK-43105][CONNECT] Abbreviate Bytes and Strings in proto message

zhengruifeng commented on code in PR #40750:
URL: https://github.com/apache/spark/pull/40750#discussion_r1164803678


##########
connector/connect/common/src/main/scala/org/apache/spark/sql/connect/common/ProtoUtils.scala:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.connect.common
+
+import scala.collection.JavaConverters._
+
+import com.google.protobuf.{ByteString, Message}
+import com.google.protobuf.Descriptors.FieldDescriptor
+
+private[connect] object ProtoUtils {
+  private val format = java.text.NumberFormat.getInstance()
+  private val MAX_BYTES_SIZE = 8
+  private val MAX_STRING_SIZE = 1024
+
+  def abbreviate(message: Message): Message = {
+    val builder = message.toBuilder
+
+    message.getAllFields.asScala.iterator.foreach {
+      case (field: FieldDescriptor, string: String)
+          if field.getJavaType == FieldDescriptor.JavaType.STRING && string != null =>
+        val size = string.size
+        if (size > MAX_STRING_SIZE) {
+          builder.setField(field, createString(string.take(MAX_STRING_SIZE), size))
+        } else {
+          builder.setField(field, string)
+        }
+
+      case (field: FieldDescriptor, byteString: ByteString)
+          if field.getJavaType == FieldDescriptor.JavaType.BYTE_STRING && byteString != null =>
+        val size = byteString.size
+        if (size > MAX_BYTES_SIZE) {
+          val prefix = Array.tabulate(MAX_BYTES_SIZE)(byteString.byteAt)
+          builder.setField(field, createByteString(prefix, size))
+        } else {
+          builder.setField(field, byteString)
+        }
+
+      case (field: FieldDescriptor, byteArray: Array[Byte])
+          if field.getJavaType == FieldDescriptor.JavaType.BYTE_STRING && byteArray != null =>
+        val size = byteArray.size
+        if (size > MAX_BYTES_SIZE) {
+          val prefix = byteArray.take(MAX_BYTES_SIZE)
+          builder.setField(field, createByteString(prefix, size))
+        } else {
+          builder.setField(field, byteArray)
+        }
+
+      // TODO: should also support 1, repeated msg; 2, map<xxx, msg>
+      case (field: FieldDescriptor, msg: Message)
+          if field.getJavaType == FieldDescriptor.JavaType.MESSAGE && msg != null =>
+        builder.setField(field, abbreviate(msg))
+
+      case (field: FieldDescriptor, value: Any) => builder.setField(field, value)
+    }
+
+    builder.build()
+  }
+
+  private def createByteString(prefix: Array[Byte], size: Int): ByteString = {
+    ByteString.copyFrom(
+      List(
+        ByteString.copyFrom(prefix),
+        ByteString.copyFromUtf8(s"*********(redacted, size=${format.format(size)})")).asJava)

Review Comment:
   done



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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