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


##########
amber/src/test/scala/org/apache/texera/web/service/ExecutionResultServiceSpec.scala:
##########
@@ -19,14 +19,17 @@
 
 package org.apache.texera.web.service
 
-import com.fasterxml.jackson.databind.JsonNode
 import com.fasterxml.jackson.databind.node.ObjectNode
 import io.reactivex.rxjava3.disposables.Disposable
 import org.apache.pekko.actor.{ActorSystem, Cancellable}
 import org.apache.texera.amber.core.executor.OpExecInitInfo
 import org.apache.texera.amber.core.storage.model.BufferedItemWriter
 import org.apache.texera.amber.core.storage.result.{OperatorResultMetadata, 
WorkflowResultStore}
-import org.apache.texera.amber.core.storage.{DocumentFactory, VFSURIFactory}
+import org.apache.texera.amber.core.storage.{
+  DocumentFactory,

Review Comment:
   **Advisory:**
   
   The PR title (the squash commit subject) is 81 characters, and 
[AGENTS.md](https://github.com/apache/texera/blob/main/AGENTS.md#branch-and-commit-naming)
 asks for about 60. The type and scopeless form fit this move, so only the 
length needs to change. Suggested: `refactor: move workflow access and result 
helpers to common` (59 characters).



##########
common/workflow-core/src/test/scala/org/apache/texera/amber/core/tuple/TupleUtilsSpec.scala:
##########
@@ -63,4 +67,494 @@ class TupleUtilsSpec extends AnyFlatSpec {
     val node = TupleUtils.tuple2json(new Schema(), Array.empty[Any])
     assert(node.size() == 0)
   }
+
+  // --- convertTuplesToJson 
---------------------------------------------------
+
+  "convertTuplesToJson" should "convert tuples with various field types 
correctly" in {
+    // Create a schema with different attribute types
+    val attributes = List(
+      new Attribute("stringCol", AttributeType.STRING),
+      new Attribute("intCol", AttributeType.INTEGER),
+      new Attribute("boolCol", AttributeType.BOOLEAN),
+      new Attribute("nullCol", AttributeType.ANY),
+      new Attribute("longStringCol", AttributeType.STRING),
+      new Attribute("shortBinaryCol", AttributeType.BINARY),
+      new Attribute("longBinaryCol", AttributeType.BINARY)
+    )
+
+    val schema = new Schema(attributes)
+
+    // Create a string longer than maxStringLength (100)
+    val longString = "a" * 150
+
+    // Create binary data
+    val shortBinaryData = Array[Byte](1, 2, 3, 4, 5)
+    val longBinaryData = Array.tabulate[Byte](100)(_.toByte)
+
+    // Create a tuple with all the test data
+    val tuple = Tuple
+      .builder(schema)
+      .add("stringCol", AttributeType.STRING, "regular string")
+      .add("intCol", AttributeType.INTEGER, 42)
+      .add("boolCol", AttributeType.BOOLEAN, true)
+      .add("nullCol", AttributeType.ANY, null)
+      .add("longStringCol", AttributeType.STRING, longString)
+      .add("shortBinaryCol", AttributeType.BINARY, shortBinaryData)
+      .add("longBinaryCol", AttributeType.BINARY, longBinaryData)
+      .build()
+
+    // Convert to JSON
+    val result = TupleUtils.convertTuplesToJson(List(tuple))
+
+    // Verify the result
+    result should have size 1
+    val jsonNode = result.head
+
+    // Check regular values
+    jsonNode.get("stringCol").asText() shouldBe "regular string"
+    jsonNode.get("intCol").asInt() shouldBe 42
+    jsonNode.get("boolCol").asBoolean() shouldBe true
+
+    // Check NULL value
+    jsonNode.get("nullCol").asText() shouldBe "NULL"
+
+    // Check long string truncation
+    jsonNode.get("longStringCol").asText() should (
+      have length 103 and // 100 chars + "..."
+        startWith("a" * 100) and
+        endWith("...")
+    )
+
+    // Check short binary representation
+    val shortBinaryString = jsonNode.get("shortBinaryCol").asText()
+    shortBinaryString should (
+      startWith("<binary") and
+        include("...") and
+        include("size = 5 bytes")
+    )
+
+    // Check long binary representation
+    val longBinaryString = jsonNode.get("longBinaryCol").asText()
+    longBinaryString should (
+      startWith("<binary") and
+        include("...") and
+        include("size = 100 bytes")
+    )
+  }
+
+  it should "handle empty collections of tuples" in {
+    val result = TupleUtils.convertTuplesToJson(List())
+    result shouldBe empty
+  }
+
+  it should "handle collections with multiple tuples" in {
+    // Create a simple schema
+    val attributes = List(
+      new Attribute("id", AttributeType.INTEGER),
+      new Attribute("name", AttributeType.STRING)
+    )
+
+    val schema = new Schema(attributes)
+
+    // Create multiple tuples
+    val tuple1 = Tuple
+      .builder(schema)
+      .add("id", AttributeType.INTEGER, 1)
+      .add("name", AttributeType.STRING, "Alice")
+      .build()
+
+    val tuple2 = Tuple
+      .builder(schema)
+      .add("id", AttributeType.INTEGER, 2)
+      .add("name", AttributeType.STRING, "Bob")
+      .build()
+
+    // Convert to JSON
+    val results = TupleUtils.convertTuplesToJson(List(tuple1, tuple2))
+
+    // Verify the results
+    results should have size 2
+    results.head.get("id").asInt() shouldBe 1
+    results.head.get("name").asText() shouldBe "Alice"
+    results(1).get("id").asInt() shouldBe 2
+    results(1).get("name").asText() shouldBe "Bob"
+  }
+
+  it should "handle string exactly at the maximum length" in {
+    val attributes = List(
+      new Attribute("exactLengthString", AttributeType.STRING)
+    )
+    val schema = new Schema(attributes)
+
+    // Create string exactly at maxStringLength (100)
+    val exactLengthString = "x" * 100
+
+    val tuple = Tuple
+      .builder(schema)
+      .add("exactLengthString", AttributeType.STRING, exactLengthString)
+      .build()
+
+    val result = TupleUtils.convertTuplesToJson(List(tuple))
+
+    result should have size 1
+    val jsonNode = result.head
+
+    jsonNode.get("exactLengthString").asText() shouldBe exactLengthString
+    jsonNode.get("exactLengthString").asText() should have length 100
+  }
+
+  it should "handle empty binary data" in {
+    val attributes = List(
+      new Attribute("emptyBinary", AttributeType.BINARY)
+    )
+    val schema = new Schema(attributes)
+
+    // Empty binary data
+    val emptyBinaryData = Array[Byte]()
+
+    val tuple = Tuple
+      .builder(schema)
+      .add("emptyBinary", AttributeType.BINARY, emptyBinaryData)
+      .build()
+
+    val result = TupleUtils.convertTuplesToJson(List(tuple))
+
+    result should have size 1
+    val jsonNode = result.head
+
+    val emptyBinaryString = jsonNode.get("emptyBinary").asText()
+    emptyBinaryString should include("size = 0 bytes")
+  }
+
+  it should "handle binary data with single ByteBuffer" in {

Review Comment:
   **Polish:**
   
   The fixture is `"Hello, world!".getBytes()`, an `Array[Byte]`, not a 
`ByteBuffer` (BINARY fields hold `Array[Byte]`, see `TupleUtils.scala:79`). The 
comment at line 235 has the same leftover.
   ```suggestion
     it should "handle binary data from a single byte array" in {
   ```



##########
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)
+
+  private var testVersion: WorkflowVersion = _
+
+  override protected def beforeAll(): Unit = {
+    initializeDBAndReplaceDSLContext()
+  }
+
+  override protected def beforeEach(): Unit = {
+    cleanupTestData()
+
+    val testUser = new User
+    testUser.setUid(testUserId)
+    testUser.setName("test_user")
+    testUser.setEmail("[email protected]")
+    new UserDao(getDSLContext.configuration()).insert(testUser)
+
+    val testWorkflow = new Workflow
+    testWorkflow.setWid(testWorkflowWid)
+    testWorkflow.setName("test_workflow_" + 
UUID.randomUUID().toString.substring(0, 8))
+    testWorkflow.setContent("{}")
+    testWorkflow.setCreationTime(new Timestamp(System.currentTimeMillis()))
+    testWorkflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis()))
+    new WorkflowDao(getDSLContext.configuration()).insert(testWorkflow)
+
+    testVersion = new WorkflowVersion
+    testVersion.setWid(testWorkflowWid)
+    testVersion.setContent("{}")
+    testVersion.setCreationTime(new Timestamp(System.currentTimeMillis()))
+    new WorkflowVersionDao(getDSLContext.configuration()).insert(testVersion)
+  }
+
+  override protected def afterEach(): Unit = {
+    cleanupTestData()
+  }
+
+  override protected def afterAll(): Unit = {
+    closeConnectionPool()
+  }
+
+  // Child rows before parents: port results, executions, versions, the 
workflow, the user.
+  private def cleanupTestData(): Unit = {
+    val vids = getDSLContext
+      .select(WORKFLOW_VERSION.VID)
+      .from(WORKFLOW_VERSION)
+      .where(WORKFLOW_VERSION.WID.eq(testWorkflowWid))
+    getDSLContext
+      .deleteFrom(OPERATOR_PORT_EXECUTIONS)
+      .where(
+        OPERATOR_PORT_EXECUTIONS.WORKFLOW_EXECUTION_ID.in(
+          getDSLContext
+            .select(WORKFLOW_EXECUTIONS.EID)
+            .from(WORKFLOW_EXECUTIONS)
+            .where(WORKFLOW_EXECUTIONS.VID.in(vids))
+        )
+      )
+      .execute()
+    
getDSLContext.deleteFrom(WORKFLOW_EXECUTIONS).where(WORKFLOW_EXECUTIONS.VID.in(vids)).execute()
+    getDSLContext
+      .deleteFrom(WORKFLOW_VERSION)
+      .where(WORKFLOW_VERSION.WID.eq(testWorkflowWid))
+      .execute()
+    
getDSLContext.deleteFrom(WORKFLOW).where(WORKFLOW.WID.eq(testWorkflowWid)).execute()
+    getDSLContext.deleteFrom(USER).where(USER.UID.eq(testUserId)).execute()
+  }
+
+  private def insertExecution(): WorkflowExecutions = {
+    val execution = new WorkflowExecutions
+    execution.setVid(testVersion.getVid)
+    execution.setUid(testUserId)
+    execution.setEnvironmentVersion("test-env-1.0")
+    new WorkflowExecutionsDao(getDSLContext.configuration()).insert(execution)
+    execution
+  }
+
+  private def insertOperatorPortResult(
+      eid: ExecutionIdentity,
+      globalPortId: GlobalPortIdentity,
+      uri: URI
+  ): Unit =
+    getDSLContext
+      .insertInto(OPERATOR_PORT_EXECUTIONS)
+      .columns(
+        OPERATOR_PORT_EXECUTIONS.WORKFLOW_EXECUTION_ID,
+        OPERATOR_PORT_EXECUTIONS.GLOBAL_PORT_ID,
+        OPERATOR_PORT_EXECUTIONS.RESULT_URI
+      )
+      .values(Integer.valueOf(eid.id.toInt), globalPortId.serializeAsString, 
uri.toString)
+      .execute()
+
+  "getResultUrisByExecutionId" should "return inserted URIs and filter out 
null/empty entries" in {

Review Comment:
   **Polish:**
   
   The name promises null filtering, but the body only inserts an empty-string 
row (line 177), so the `uri != null` arm of `ExecutionResultUris.scala:48` is 
never exercised. Either insert a NULL `RESULT_URI` row too, or narrow the name.
   ```suggestion
     "getResultUrisByExecutionId" should "return inserted URIs and filter out 
empty entries" in {
   ```



##########
common/auth/src/main/scala/org/apache/texera/auth/util/WorkflowAccess.scala:
##########
@@ -0,0 +1,110 @@
+// 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.auth.util
+
+import org.apache.texera.dao.SqlServer
+import org.apache.texera.dao.jooq.generated.Tables._
+import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum
+import org.jooq.impl.DSL
+import org.jooq.{DSLContext, Record1, Select}
+
+object WorkflowAccess {
+  private def context: DSLContext =
+    SqlServer
+      .getInstance()
+      .createDSLContext()
+
+  /**
+    * Identifies whether the given user has read-only access over the given 
workflow

Review Comment:
   **Polish:**
   
   "read-only access" undersells this check. Line 40 is also true for WRITE 
grantees, owners and any public workflow, so it means "can read". Now that this 
is the shared entry point result-service will call, worth fixing in the #8995 
doc pass along with the hex-string Scaladoc.
   ```suggestion
       * Identifies whether the given user can read the given workflow: a READ 
or WRITE grant, ownership, or a public workflow
   ```



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