You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@texera.apache.org by gi...@apache.org on 2026/07/04 06:27:42 UTC

(texera) branch gh-readonly-queue/main/pr-6078-186115d19a6e878c6daa81fdff07cc847c51ca9b created (now 4067c64d31)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-6078-186115d19a6e878c6daa81fdff07cc847c51ca9b
in repository https://gitbox.apache.org/repos/asf/texera.git


      at 4067c64d31 test(workflow-operator): add unit test coverage for SQLSourceOpDesc schema introspection (#6078)

This branch includes the following new commits:

     new 4067c64d31 test(workflow-operator): add unit test coverage for SQLSourceOpDesc schema introspection (#6078)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(texera) 01/01: test(workflow-operator): add unit test coverage for SQLSourceOpDesc schema introspection (#6078)

Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-6078-186115d19a6e878c6daa81fdff07cc847c51ca9b
in repository https://gitbox.apache.org/repos/asf/texera.git

commit 4067c64d316ee6cb75ad89638d1047795c9caad6
Author: Xinyuan Lin <xi...@uci.edu>
AuthorDate: Fri Jul 3 23:27:20 2026 -0700

    test(workflow-operator): add unit test coverage for SQLSourceOpDesc schema introspection (#6078)
    
    ### What changes were proposed in this PR?
    
    Add unit test coverage for `SQLSourceOpDesc.querySchema` — the abstract
    SQL source descriptor's JDBC schema-introspection path — selected from
    the Codecov report. No production-code changes.
    
    `SQLSourceOpDesc` (40.5% covered) is abstract; its concrete subclasses
    (MySQL/PostgreSQL/AsterixDB) never exercise `querySchema` because their
    specs supply no connection. The 27 missed lines are covered here via a
    small test subclass whose `establishConn` returns **ScalaMock-backed**
    `java.sql.Connection`/`DatabaseMetaData`/`ResultSet` — no live database.
    
    Covered:
    - the connection-field null guard (returns `null` until all fields set)
    - every JDBC-`Types` → `AttributeType` mapping arm (INTEGER/SMALLINT,
    DOUBLE/NUMERIC, BOOLEAN, BINARY, VARCHAR, BIGINT, TIMESTAMP)
    - default-port resolution via `updatePort()` and multi-column iteration
    - the unknown-data-type `RuntimeException`
    - the `SQLException` → wrapped-`RuntimeException` catch
    - the abstract base `establishConn` default (returns no connection →
    `NullPointerException`)
    
    ### Any related issues, documentation, discussions?
    
    Follow-up to the review feedback on #6043: prioritize tests that fill
    uncovered code paths.
    
    ### How was this PR tested?
    
    - `sbt "WorkflowOperator/testOnly *SQLSourceOpDescSpec"` — 16 tests, all
    green
    - `sbt "WorkflowOperator/Test/scalafmtCheck"` and `sbt
    "WorkflowOperator/scalafixAll --check"` — clean
    
    ### Was this PR authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Code (Opus 4.8 [1M context])
---
 .../operator/source/sql/SQLSourceOpDescSpec.scala  | 164 +++++++++++++++++++++
 1 file changed, 164 insertions(+)

diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/sql/SQLSourceOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/sql/SQLSourceOpDescSpec.scala
new file mode 100644
index 0000000000..5f715bb2da
--- /dev/null
+++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/source/sql/SQLSourceOpDescSpec.scala
@@ -0,0 +1,164 @@
+/*
+ * 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.operator.source.sql
+
+import org.apache.texera.amber.core.tuple.AttributeType
+import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity}
+import org.apache.texera.amber.core.workflow.{OutputPort, PhysicalOp}
+import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
+import org.scalamock.scalatest.MockFactory
+import org.scalatest.flatspec.AnyFlatSpec
+import org.scalatest.matchers.should.Matchers
+
+import java.sql.{Connection, DatabaseMetaData, ResultSet, SQLException, Types}
+
+class SQLSourceOpDescSpec extends AnyFlatSpec with Matchers with MockFactory {
+
+  /** Concrete subclass returning a supplied (mocked) connection. */
+  private class TestSQLSourceOpDesc(conn: Connection) extends SQLSourceOpDesc {
+    override def getPhysicalOp(
+        workflowId: WorkflowIdentity,
+        executionId: ExecutionIdentity
+    ): PhysicalOp = ??? // never invoked by these tests
+    override def operatorInfo: OperatorInfo =
+      OperatorInfo(
+        "Test SQL",
+        "test",
+        OperatorGroupConstants.DATABASE_GROUP,
+        inputPorts = List.empty,
+        outputPorts = List(OutputPort())
+      )
+    override protected def establishConn: Connection = conn
+    override protected def updatePort(): Unit =
+      port = if (port != null && port.trim.equals("default")) "1234" else port
+  }
+
+  /** Subclass that leaves establishConn as the abstract base default (null). */
+  private class BaseConnSQLSourceOpDesc extends SQLSourceOpDesc {
+    override def getPhysicalOp(
+        workflowId: WorkflowIdentity,
+        executionId: ExecutionIdentity
+    ): PhysicalOp = ???
+    override def operatorInfo: OperatorInfo =
+      OperatorInfo("t", "t", OperatorGroupConstants.DATABASE_GROUP, List.empty, List(OutputPort()))
+    override protected def updatePort(): Unit = {}
+  }
+
+  private def configure(desc: SQLSourceOpDesc): SQLSourceOpDesc = {
+    desc.host = "h"
+    desc.port = "default"
+    desc.database = "db"
+    desc.table = "tbl"
+    desc.username = "u"
+    desc.password = "p"
+    desc
+  }
+
+  /** Introspect a single-column table whose one column has the given JDBC type. */
+  private def attributeTypeFor(jdbcType: Int): AttributeType = {
+    val rs = mock[ResultSet]
+    val meta = mock[DatabaseMetaData]
+    val conn = mock[Connection]
+    (conn.setReadOnly _).expects(true)
+    (conn.getMetaData _).expects().returning(meta)
+    (meta.getColumns _).expects(null, null, "tbl", null).returning(rs)
+    inSequence {
+      (rs.next _).expects().returning(true)
+      (rs.next _).expects().returning(false)
+    }
+    (rs.getString(_: String)).expects("COLUMN_NAME").returning("c")
+    (rs.getInt(_: String)).expects("DATA_TYPE").returning(jdbcType)
+    (conn.close _).expects()
+    configure(new TestSQLSourceOpDesc(conn)).sourceSchema().getAttribute("c").getType
+  }
+
+  "SQLSourceOpDesc.sourceSchema" should "return null until every connection field is set" in {
+    val desc = configure(new TestSQLSourceOpDesc(mock[Connection]))
+    desc.table = null
+    assert(desc.sourceSchema() == null)
+  }
+
+  it should "map each JDBC data type to its Texera attribute type" in {
+    attributeTypeFor(Types.INTEGER) shouldBe AttributeType.INTEGER
+    attributeTypeFor(Types.SMALLINT) shouldBe AttributeType.INTEGER
+    attributeTypeFor(Types.DOUBLE) shouldBe AttributeType.DOUBLE
+    attributeTypeFor(Types.NUMERIC) shouldBe AttributeType.DOUBLE
+    attributeTypeFor(Types.BOOLEAN) shouldBe AttributeType.BOOLEAN
+    attributeTypeFor(Types.BINARY) shouldBe AttributeType.BINARY
+    attributeTypeFor(Types.VARCHAR) shouldBe AttributeType.STRING
+    attributeTypeFor(Types.BIGINT) shouldBe AttributeType.LONG
+    attributeTypeFor(Types.TIMESTAMP) shouldBe AttributeType.TIMESTAMP
+  }
+
+  it should "resolve the default port and read the full column set" in {
+    val rs = mock[ResultSet]
+    val meta = mock[DatabaseMetaData]
+    val conn = mock[Connection]
+    (conn.setReadOnly _).expects(true)
+    (conn.getMetaData _).expects().returning(meta)
+    (meta.getColumns _).expects(null, null, "tbl", null).returning(rs)
+    inSequence {
+      (rs.next _).expects().returning(true)
+      (rs.next _).expects().returning(true)
+      (rs.next _).expects().returning(false)
+    }
+    inSequence {
+      (rs.getString(_: String)).expects("COLUMN_NAME").returning("id")
+      (rs.getString(_: String)).expects("COLUMN_NAME").returning("name")
+    }
+    inSequence {
+      (rs.getInt(_: String)).expects("DATA_TYPE").returning(Types.INTEGER)
+      (rs.getInt(_: String)).expects("DATA_TYPE").returning(Types.VARCHAR)
+    }
+    (conn.close _).expects()
+    val desc = configure(new TestSQLSourceOpDesc(conn))
+    val schema = desc.sourceSchema()
+    schema.getAttributes.map(a => (a.getName, a.getType)) shouldBe List(
+      ("id", AttributeType.INTEGER),
+      ("name", AttributeType.STRING)
+    )
+    desc.port shouldBe "1234" // updatePort() resolved "default"
+  }
+
+  it should "reject an unknown JDBC data type" in {
+    val rs = mock[ResultSet]
+    val meta = mock[DatabaseMetaData]
+    val conn = mock[Connection]
+    (conn.setReadOnly _).expects(true)
+    (conn.getMetaData _).expects().returning(meta)
+    (meta.getColumns _).expects(null, null, "tbl", null).returning(rs)
+    (rs.next _).expects().returning(true)
+    (rs.getString(_: String)).expects("COLUMN_NAME").returning("c")
+    (rs.getInt(_: String)).expects("DATA_TYPE").returning(Types.ARRAY)
+    val ex = intercept[RuntimeException](configure(new TestSQLSourceOpDesc(conn)).sourceSchema())
+    ex.getMessage should endWith(": unknown data type: " + Types.ARRAY)
+  }
+
+  it should "wrap a SQLException raised while introspecting" in {
+    val conn = mock[Connection]
+    (conn.setReadOnly _).expects(true).throwing(new SQLException("boom"))
+    val ex = intercept[RuntimeException](configure(new TestSQLSourceOpDesc(conn)).sourceSchema())
+    ex.getMessage should endWith(" failed to connect to the database. boom")
+  }
+
+  it should "fail when the base establishConn returns no connection" in {
+    intercept[NullPointerException](configure(new BaseConnSQLSourceOpDesc).sourceSchema())
+  }
+}