mengw15 commented on code in PR #9219:
URL: https://github.com/apache/texera/pull/9219#discussion_r4417722559


##########
common/workflow-core/src/main/scala/org/apache/texera/amber/core/tuple/TupleUtils.scala:
##########
@@ -35,4 +37,86 @@ object TupleUtils {
     objectNode
   }
 
+  private val binaryPreviewLeadingBits: Int = 10
+  private val binaryPreviewTrailingBits: Int = 3
+
+  private def bytesToBinaryString(bytes: Array[Byte]): String =
+    bytes
+      .map(b =>
+        String.format(s"%${BitsPerByte}s", Integer.toBinaryString(b & 
0xff)).replace(' ', '0')
+      )
+      .mkString("")
+
+  /**
+    * Converts a collection of Tuples to a list of JSON ObjectNodes.
+    *
+    * This function takes a collection of Tuples and converts each tuple into 
a JSON ObjectNode.
+    * For binary data, it formats the bytes into a readable hex string 
representation with length info.
+    * For string values longer than maxStringLength (100), it truncates them.
+    * NULL values are converted to the string "NULL".
+    *
+    * @param tuples The collection of Tuples to convert
+    * @param isVisualization Whether this is for visualization rendering 
(affects string truncation)
+    * @return A List of ObjectNodes containing the JSON representation of the 
tuples
+    */
+  def convertTuplesToJson(
+      tuples: Iterable[Tuple],
+      isVisualization: Boolean = false
+  ): List[ObjectNode] = {
+    val maxStringLength = 100
+
+    tuples.map { tuple =>
+      val processedFields = tuple.schema.getAttributes.zipWithIndex
+        .map {
+          case (attr, idx) =>
+            val fieldValue = tuple.getField[AnyRef](idx)
+
+            Option(fieldValue) match {
+              case None => "NULL"
+              case Some(value) =>
+                attr.getType match {
+                  case AttributeType.BINARY =>
+                    value match {
+                      case byteArray: Array[Byte] =>
+                        val totalSize = byteArray.length
+                        val sizeFormatted = f"$totalSize%,d"
+                        val totalBits = totalSize * BitsPerByte
+                        val preview =
+                          if (totalBits <= binaryPreviewLeadingBits + 
binaryPreviewTrailingBits)
+                            bytesToBinaryString(byteArray)
+                          else {
+                            val leadingBytesNeeded =
+                              math.ceil(binaryPreviewLeadingBits.toDouble / 
BitsPerByte).toInt
+                            val trailingBytesNeeded =
+                              math.ceil(binaryPreviewTrailingBits.toDouble / 
BitsPerByte).toInt
+                            val leading = 
bytesToBinaryString(byteArray.take(leadingBytesNeeded))
+                              .take(binaryPreviewLeadingBits)
+                            val trailing = bytesToBinaryString(
+                              byteArray.takeRight(trailingBytesNeeded)
+                            ).takeRight(binaryPreviewTrailingBits)
+                            s"$leading...$trailing"
+                          }
+                        s"<binary $preview, size = $sizeFormatted bytes>"
+
+                      case _ =>
+                        throw new RuntimeException(

Review Comment:
   Moved verbatim; changing the exception type would change behaviour, which 
this PR avoids.



##########
common/workflow-core/src/test/scala/org/apache/texera/amber/core/storage/ExecutionResultUrisSpec.scala:
##########
@@ -0,0 +1,237 @@
+/*
+ * 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.texera.amber.core.storage
+
+import org.apache.texera.amber.core.virtualidentity.{
+  ExecutionIdentity,
+  OperatorIdentity,
+  PhysicalOpIdentity,
+  WorkflowIdentity
+}
+import org.apache.texera.amber.core.workflow.{GlobalPortIdentity, PortIdentity}
+import org.apache.texera.amber.util.serde.GlobalPortIdentitySerde.SerdeOps
+import org.apache.texera.dao.MockTexeraDB
+import org.apache.texera.dao.jooq.generated.Tables._
+import org.apache.texera.dao.jooq.generated.tables.daos.{
+  UserDao,
+  WorkflowDao,
+  WorkflowExecutionsDao,
+  WorkflowVersionDao
+}
+import org.apache.texera.dao.jooq.generated.tables.pojos.{
+  User,
+  Workflow,
+  WorkflowExecutions,
+  WorkflowVersion
+}
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
+
+import java.net.URI
+import java.sql.Timestamp
+import java.util.UUID
+
+class ExecutionResultUrisSpec
+    extends AnyFlatSpec
+    with BeforeAndAfterAll
+    with BeforeAndAfterEach
+    with MockTexeraDB {
+
+  private val testWorkflowWid = 3000 + scala.util.Random.nextInt(1000)
+  private val testUserId = 1000 + scala.util.Random.nextInt(1000)

Review Comment:
   Copied from the original spec. Each suite that mixes in `MockTexeraDB` gets 
its own database (`texera_db_<uuid>`), so ids from different suites can't 
collide.



-- 
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]

Reply via email to