mangrrua commented on a change in pull request #5787:
URL: https://github.com/apache/incubator-pinot/pull/5787#discussion_r464317075



##########
File path: 
pinot-connectors/pinot-spark-connector/src/main/scala/org/apache/pinot/connector/spark/connector/PinotSplitter.scala
##########
@@ -0,0 +1,85 @@
+/**
+ * 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.pinot.connector.spark.connector
+
+import java.util.regex.{Matcher, Pattern}
+
+import org.apache.pinot.connector.spark.connector.Constants.PinotTableType
+import org.apache.pinot.connector.spark.connector.query.GeneratedSQLs
+import org.apache.pinot.connector.spark.exceptions.PinotException
+import org.apache.pinot.connector.spark.utils.Logging
+
+private[pinot] object PinotSplitter extends Logging {
+  private val PINOT_SERVER_PATTERN = Pattern.compile("Server_(.*)_(\\d+)")
+
+  def generatePinotSplits(
+      generatedSQLs: GeneratedSQLs,
+      routingTable: Map[String, Map[String, List[String]]],
+      segmentsPerSplit: Int): List[PinotSplit] = {
+    routingTable.flatMap {
+      case (tableType, serversToSegments) =>
+        serversToSegments
+          .map { case (server, segments) => parseServerInput(server, segments) 
}
+          .flatMap {
+            case (matcher, segments) =>
+              createPinotSplitsFromSubSplits(
+                tableType,
+                generatedSQLs,
+                matcher,
+                segments,
+                segmentsPerSplit
+              )
+          }
+    }.toList
+  }
+
+  private def parseServerInput(server: String, segments: List[String]): 
(Matcher, List[String]) = {
+    val matcher = PINOT_SERVER_PATTERN.matcher(server)
+    if (matcher.matches() && matcher.groupCount() == 2) matcher -> segments
+    else throw PinotException(s"'$server' did not match!?")
+  }
+
+  private def createPinotSplitsFromSubSplits(
+      tableType: PinotTableType,
+      generatedSQLs: GeneratedSQLs,
+      serverMatcher: Matcher,
+      segments: List[String],
+      segmentsPerSplit: Int): Iterator[PinotSplit] = {
+    val serverHost = serverMatcher.group(1)
+    val serverPort = serverMatcher.group(2)
+    val maxSegmentCount = Math.min(segments.size, segmentsPerSplit)
+    segments.grouped(maxSegmentCount).map { subSegments =>
+      val serverAndSegments =
+        PinotServerAndSegments(serverHost, serverPort, subSegments, tableType)
+      PinotSplit(generatedSQLs, serverAndSegments)
+    }
+  }
+}
+
+private[pinot] case class PinotSplit(
+    generatedSQLs: GeneratedSQLs,
+    serverAndSegments: PinotServerAndSegments)
+
+private[pinot] case class PinotServerAndSegments(
+    serverHost: String,
+    serverPort: String,
+    segments: List[String],
+    serverType: PinotTableType) {

Review comment:
       `GeneratedSQLs` contains the realtime and offline queries. `PinotSplit` 
contains the generated queries with the server and segment informations. In the 
executor side, we are send query to the specific server. Connector checks the 
`serverType` to learn which query will be converted, and sent to the which 
server. Thus we need to the `serverType` in the current design. This is the 
usage;
   
   ```scala
   val pinotServerAsyncQueryResponse = pinotSplit.serverAndSegments.serverType 
match {
         case TableType.REALTIME =>
           val realtimeBrokerRequest =
             
sqlCompiler.compileToBrokerRequest(pinotSplit.generatedSQLs.realtimeSelectQuery)
           submitRequestToPinotServer(null, null, realtimeBrokerRequest, 
routingTableForRequest)
         case TableType.OFFLINE =>
           val offlineBrokerRequest =
             
sqlCompiler.compileToBrokerRequest(pinotSplit.generatedSQLs.offlineSelectQuery)
           submitRequestToPinotServer(offlineBrokerRequest, 
routingTableForRequest, null, null)
       }
   ```




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to