HeartSaVioR commented on code in PR #43425:
URL: https://github.com/apache/spark/pull/43425#discussion_r1365035655


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/state/StreamStreamJoinStatePartitionReader.scala:
##########
@@ -0,0 +1,139 @@
+/*
+ * 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.spark.sql.execution.datasources.v2.state
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference, 
GenericInternalRow, Literal, UnsafeProjection, UnsafeRow}
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.connector.read.PartitionReader
+import 
org.apache.spark.sql.execution.datasources.v2.state.StateDataSourceV2.JoinSideValues
+import org.apache.spark.sql.execution.datasources.v2.state.utils.SchemaUtil
+import org.apache.spark.sql.execution.streaming.StatefulOperatorStateInfo
+import 
org.apache.spark.sql.execution.streaming.StreamingSymmetricHashJoinHelper.{JoinSide,
 LeftSide, RightSide}
+import org.apache.spark.sql.execution.streaming.state.{StateStoreConf, 
SymmetricHashJoinStateManager}
+import org.apache.spark.sql.types.{BooleanType, StructType}
+import org.apache.spark.util.SerializableConfiguration
+
+class StreamStreamJoinStatePartitionReader(
+    storeConf: StateStoreConf,
+    hadoopConf: SerializableConfiguration,
+    partition: StateStoreInputPartition,
+    userFacingSchema: StructType,
+    stateSchema: StructType) extends PartitionReader[InternalRow] with Logging 
{
+
+  private val keySchema = SchemaUtil.getSchemaAsDataType(stateSchema, "key")
+    .asInstanceOf[StructType]
+  private val valueSchema = SchemaUtil.getSchemaAsDataType(stateSchema, 
"value")
+    .asInstanceOf[StructType]
+
+  private val userFacingValueSchema = 
SchemaUtil.getSchemaAsDataType(userFacingSchema, "value")
+    .asInstanceOf[StructType]
+
+  private val joinSide: JoinSide = partition.joinSide match {
+    case JoinSideValues.left => LeftSide
+    case JoinSideValues.right => RightSide
+    case JoinSideValues.none =>
+      throw new IllegalStateException("Unexpected join side for stream-stream 
read!")
+  }
+
+  private val (inputAttributes, formatVersion) = {
+    val maybeMatchedColumn = valueSchema.last
+    val (fields, version) = {
+      if (maybeMatchedColumn.name == "matched" && maybeMatchedColumn.dataType 
== BooleanType) {
+        (valueSchema.dropRight(1), 2)
+      } else {
+        (valueSchema, 1)
+      }
+    }
+
+    assert(fields.toArray.sameElements(userFacingValueSchema.fields),
+      "Exposed fields should be same with given user facing schema for value! 
" +
+        s"Exposed fields: ${fields.mkString("(", ", ", ")")} / " +
+        s"User facing value fields: 
${userFacingValueSchema.fields.mkString("(", ", ", ")")}")
+
+    val attrs = fields.map {
+      f => AttributeReference(f.name, f.dataType, f.nullable)()
+    }
+    (attrs, version)
+  }
+
+  private var joinStateManager: SymmetricHashJoinStateManager = _
+
+  private lazy val iter = {
+    if (joinStateManager == null) {
+      val stateInfo = StatefulOperatorStateInfo(
+        partition.stateCheckpointRootLocation, partition.queryId, 
partition.operatorId,
+        partition.batchId + 1, -1)
+      joinStateManager = new SymmetricHashJoinStateManager(
+        joinSide,
+        inputAttributes,
+        joinKeys = DataTypeUtils.toAttributes(keySchema),
+        stateInfo = Some(stateInfo),
+        storeConf = storeConf,
+        hadoopConf = hadoopConf.value,
+        partitionId = partition.partition,
+        formatVersion,
+        skippedNullValueCount = None
+      )
+    }
+
+    // state format 2
+    val valueWithMatchedExprs = inputAttributes :+ Literal(true)
+    val indexOrdinalInValueWithMatchedRow = inputAttributes.size
+    val valueWithMatchedRowGenerator = 
UnsafeProjection.create(valueWithMatchedExprs,
+      inputAttributes)
+
+    joinStateManager.iterator.map { pair =>

Review Comment:
   self-review: need to handle metadata column



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