gengliangwang commented on code in PR #55586:
URL: https://github.com/apache/spark/pull/55586#discussion_r3222081332


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/InsertIntoTests.scala:
##########
@@ -210,6 +219,15 @@ trait InsertIntoSQLOnlyTests
   /** Check that the results in `tableName` match the `expected` DataFrame. */
   protected def verifyTable(tableName: String, expected: DataFrame): Unit
 
+  protected def checkInsertMetrics(tableName: String, numInsertedRows: Long): 
Unit = {
+    val inMemoryTable = 
spark.table(tableName).queryExecution.analyzed.collectFirst {
+      case ExtractV2Table(t) => t.asInstanceOf[InMemoryBaseTable]
+    }.get
+    val summary = 
inMemoryTable.commits.last.writeSummary.get.asInstanceOf[InsertSummary]

Review Comment:
   `.get.asInstanceOf[InsertSummary]` chains two cryptic failure modes:
   - `writeSummary.get` throws `NoSuchElementException` if the last commit 
didn't carry a summary (e.g. the test ordering ever changes and an Overwrite 
ends up being `commits.last`).
   - The cast throws `ClassCastException` if the last commit's summary isn't an 
`InsertSummary` (e.g. a MERGE earlier in the same `withTable` block).
   
   Both surface as opaque stack traces from a test helper with no hint which 
table or which prior operation caused the mismatch. A clearer fail message is 
cheap here:
   
   ```suggestion
       val summary = inMemoryTable.commits.last.writeSummary match {
         case Some(s: InsertSummary) => s
         case Some(other) => fail(s"Expected InsertSummary on $tableName, got 
${other.getClass.getSimpleName}")
         case None => fail(s"Last commit on $tableName had no write summary")
       }
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to