viirya commented on code in PR #60313:
URL: https://github.com/apache/spark/pull/60313#discussion_r4591385178


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -5574,28 +5764,212 @@ class ExternalLookupJoinRealTimeModeSuite extends 
StreamRealTimeModeE2ESuiteBase
     }
   }
 
-  test("real-time mode rejects external lookup join") {
-    SimpleExternalLookupDataSource.resetCounters()
-    val input = LowLatencyMemoryStream[Int](2)
-    val left = input.toDF().toDF("id")
-    val lookup = 
spark.read.format(classOf[SimpleExternalLookupDataSource].getName).load()
-    val joined = left.join(lookup, left("id") === lookup("id"))
-      .select(concat(col("department"), lit("-"), 
left("id").cast("string")).as("output"))
+  Seq("inner", "left_outer", "left_semi", "left_anti").foreach { joinType =>
+    test(s"real-time mode sends each stream row's lookup request as it reads 
the row: $joinType") {
+      val clock = useManualClock()
+      val input = LowLatencyMemoryStream.singlePartition[Int]
+      val query = startJoin(s"external_lookup_rtm_one_row_$joinType", 
joinType, input,
+        lookupTable(maxBatchSize = 2))
+      try {
+        // The source allows two stream rows per request, but by default a 
real-time request
+        // takes one, so the join sends it and emits its output as soon as it 
reads the row, with
+        // the batch still running. Key 3 has no lookup row, so the inner and 
left semi joins emit
+        // nothing for it, but its request is still sent.
+        val ids = Seq(1, 3, 2, 4)
+        ids.indices.foreach { n =>
+          input.addData(ids(n))
+          awaitQuery(query) {
+            SimpleExternalLookupDataSource.requestCount.get() == n + 1 &&
+              outputs.sorted == joinOutputs(joinType, ids.take(n + 1): 
_*).sorted
+          }
+        }
+        assert(SimpleExternalLookupDataSource.requestSizes.asScala.toSeq === 
Seq(1, 1, 1, 1))
+        endBatch(query, clock)
+        awaitOutputs(query, joinOutputs(joinType, ids: _*))
+        assert(SimpleExternalLookupDataSource.requestCount.get() === 4)
+      } finally {
+        query.stop()
+      }
+      assert(query.exception.isEmpty)
+    }
+
+    test(s"real-time mode fills a request of more rows before it sends it: 
$joinType") {
+      
withSQLConf(SQLConf.JOIN_EXTERNAL_LOOKUP_REAL_TIME_MODE_MAX_BATCH_ROWS.key -> 
"1000") {
+        val clock = useManualClock()
+        val input = LowLatencyMemoryStream.singlePartition[Int]
+        val query = startJoin(s"external_lookup_rtm_$joinType", joinType, 
input,
+          lookupTable(maxBatchSize = 2))
+        try {
+          // A request takes up to the two stream rows that the source allows, 
and its output
+          // comes once the request is full, before the next stream row 
arrives. Each addition
+          // fills a request and starts the next. Each full request has a key 
with lookup rows and
+          // one without, so that every join type emits rows for it.
+          input.addData(1, 3, 2)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3))
+          input.addData(4, 1)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4))
+          input.addData(3, 5)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4, 1, 3))
+          // The last request is not full, so the join sends it when the batch 
ends.
+          endBatch(query, clock)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4, 1, 3, 5))
+          // The third request is served from the cache.
+          assert(SimpleExternalLookupDataSource.requestSizes.asScala.toSeq === 
Seq(2, 2, 1))

Review Comment:
   This failed once for me locally, for `inner`, at 59a8aaa49fc:
   ```
   List(2, 2) did not equal List(2, 2, 1) (ExternalLookupJoinSuite.scala:5817)
   ```
   I think the cause is that after `input.addData(3, 5)` the test only waits 
for the output of the request of 1 and 3, and then ends the batch. If the clock 
advances before the task reads row 5, the row goes to the next batch and sits 
there in a request that is not full. For inner and left semi, key 5 emits 
nothing, so the last `awaitOutputs` holds at once and this assertion runs 
before the request is sent; left outer and left anti would time out instead.
   
   Could it wait until row 5 has been read before `endBatch`, as the limitation 
test does with `CountingKeyUDF`, and check `requestSizes` inside `awaitQuery`?



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -5574,28 +5764,212 @@ class ExternalLookupJoinRealTimeModeSuite extends 
StreamRealTimeModeE2ESuiteBase
     }
   }
 
-  test("real-time mode rejects external lookup join") {
-    SimpleExternalLookupDataSource.resetCounters()
-    val input = LowLatencyMemoryStream[Int](2)
-    val left = input.toDF().toDF("id")
-    val lookup = 
spark.read.format(classOf[SimpleExternalLookupDataSource].getName).load()
-    val joined = left.join(lookup, left("id") === lookup("id"))
-      .select(concat(col("department"), lit("-"), 
left("id").cast("string")).as("output"))
+  Seq("inner", "left_outer", "left_semi", "left_anti").foreach { joinType =>
+    test(s"real-time mode sends each stream row's lookup request as it reads 
the row: $joinType") {
+      val clock = useManualClock()
+      val input = LowLatencyMemoryStream.singlePartition[Int]
+      val query = startJoin(s"external_lookup_rtm_one_row_$joinType", 
joinType, input,
+        lookupTable(maxBatchSize = 2))
+      try {
+        // The source allows two stream rows per request, but by default a 
real-time request
+        // takes one, so the join sends it and emits its output as soon as it 
reads the row, with
+        // the batch still running. Key 3 has no lookup row, so the inner and 
left semi joins emit
+        // nothing for it, but its request is still sent.
+        val ids = Seq(1, 3, 2, 4)
+        ids.indices.foreach { n =>
+          input.addData(ids(n))
+          awaitQuery(query) {
+            SimpleExternalLookupDataSource.requestCount.get() == n + 1 &&
+              outputs.sorted == joinOutputs(joinType, ids.take(n + 1): 
_*).sorted
+          }
+        }
+        assert(SimpleExternalLookupDataSource.requestSizes.asScala.toSeq === 
Seq(1, 1, 1, 1))
+        endBatch(query, clock)
+        awaitOutputs(query, joinOutputs(joinType, ids: _*))
+        assert(SimpleExternalLookupDataSource.requestCount.get() === 4)
+      } finally {
+        query.stop()
+      }
+      assert(query.exception.isEmpty)
+    }
+
+    test(s"real-time mode fills a request of more rows before it sends it: 
$joinType") {

Review Comment:
   Optional: one join type may be enough here. This is the non-default setting, 
and the micro-batch tests already cover a multi-row request for each join type.



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