You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@linkis.apache.org by "ycycse (via GitHub)" <gi...@apache.org> on 2024/09/13 04:14:03 UTC

[PR] Support resultSet direct push for SparkSqlExecutor [linkis]

ycycse opened a new pull request, #5170:
URL: https://github.com/apache/linkis/pull/5170

   Using direct push of result sets, data does not need to be cached on disk and can be calculated in real-time and returned directly from memory. This PR supports direct pushing of resultSets under SparkSqlExecutor.


-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


Re: [PR] Support resultSet direct push for SparkSqlExecutor [linkis]

Posted by "ycycse (via GitHub)" <gi...@apache.org>.
ycycse commented on code in PR #5170:
URL: https://github.com/apache/linkis/pull/5170#discussion_r1771771016


##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/executor/SparkSqlExecutor.scala:
##########
@@ -47,6 +44,35 @@ class SparkSqlExecutor(sparkEngineSession: SparkEngineSession, id: Long)
 
   override protected def getKind: Kind = SparkSQL()
 
+  // Only used in the scenario of direct pushing, dataFrame won't be fetched at a time,
+  // It will cache the lazy dataFrame in memory and return the result when client .
+  private def submitResultSetIterator(taskId: String, df: DataFrame): Unit = {
+    if (!DirectPushCache.isTaskCached(taskId)) {
+      DirectPushCache.submitExecuteResult(taskId, df)
+    } else {
+      logger.error(s"Task $taskId already exists in resultSet cache.")
+    }
+  }
+
+  override def isFetchMethodOfDirectPush(taskId: String): Boolean = {
+    DirectPushCache.isTaskCached(taskId)
+  }
+
+  // This method is not idempotent. After fetching a result set of size fetchSize each time, the corresponding results will be removed from the cache.
+  override def fetchMoreResultSet(taskId: String, fetchSize: Int): FetchResultResponse = {
+    val dataFrameResponse = DirectPushCache.fetchResultSetOfDataFrame(taskId, fetchSize)
+    if (dataFrameResponse.dataFrame != null) {
+      if (!dataFrameResponse.hasMoreData) {
+        succeedTasks.increase()
+      }
+      FetchResultResponse(
+        hasMoreData = dataFrameResponse.hasMoreData,
+        ArrowUtils.toArrow(dataFrameResponse.dataFrame)
+      )
+    }

Review Comment:
   done. After removing the  job-way to fetch data. This method isn't needed anymore.



-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


Re: [PR] Support resultSet direct push for SparkSqlExecutor [linkis]

Posted by "ycycse (via GitHub)" <gi...@apache.org>.
ycycse commented on code in PR #5170:
URL: https://github.com/apache/linkis/pull/5170#discussion_r1759703100


##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/utils/ArrowUtils.scala:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.linkis.engineplugin.spark.utils
+
+import org.apache.arrow.memory.RootAllocator
+import org.apache.arrow.vector.{
+  BigIntVector,
+  BitVector,
+  FieldVector,
+  Float8Vector,
+  IntVector,
+  VarCharVector,
+  VectorSchemaRoot
+}
+import org.apache.arrow.vector.ipc.ArrowStreamWriter
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.types.{BooleanType, DoubleType, IntegerType, LongType, StringType}
+
+import java.io.ByteArrayOutputStream
+
+object ArrowUtils {
+
+  def toArrow(df: DataFrame): Array[Byte] = {
+    val allocator = new RootAllocator(Long.MaxValue)
+    val (root, fieldVectors) = createArrowVectors(df, allocator)
+    val outStream = new ByteArrayOutputStream()
+    val writer = new ArrowStreamWriter(root, null, outStream)
+
+    writer.start()
+    writer.writeBatch()
+    writer.end()
+    writer.close()
+
+    val arrowBytes = outStream.toByteArray
+    fieldVectors.foreach(_.close())
+    allocator.close()
+    arrowBytes
+  }
+
+  private def createArrowVectors(
+      df: DataFrame,
+      allocator: RootAllocator
+  ): (VectorSchemaRoot, List[FieldVector]) = {
+    val schema = df.schema
+    val fieldVectors = schema.fields.map { field =>
+      field.dataType match {
+        case IntegerType =>
+          val vector = new IntVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case LongType =>
+          val vector = new BigIntVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case DoubleType =>
+          val vector = new Float8Vector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case BooleanType =>
+          val vector = new BitVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case StringType =>
+          val vector = new VarCharVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case _ => throw new IllegalArgumentException(s"Unsupported data type: ${field.dataType}")

Review Comment:
   You're right, I will improve this part.



-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


Re: [PR] Support resultSet direct push for SparkSqlExecutor [linkis]

Posted by "peacewong (via GitHub)" <gi...@apache.org>.
peacewong commented on code in PR #5170:
URL: https://github.com/apache/linkis/pull/5170#discussion_r1771310196


##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/executor/SparkSqlExecutor.scala:
##########
@@ -47,6 +44,35 @@ class SparkSqlExecutor(sparkEngineSession: SparkEngineSession, id: Long)
 
   override protected def getKind: Kind = SparkSQL()
 
+  // Only used in the scenario of direct pushing, dataFrame won't be fetched at a time,
+  // It will cache the lazy dataFrame in memory and return the result when client .
+  private def submitResultSetIterator(taskId: String, df: DataFrame): Unit = {
+    if (!DirectPushCache.isTaskCached(taskId)) {
+      DirectPushCache.submitExecuteResult(taskId, df)
+    } else {
+      logger.error(s"Task $taskId already exists in resultSet cache.")
+    }
+  }
+
+  override def isFetchMethodOfDirectPush(taskId: String): Boolean = {
+    DirectPushCache.isTaskCached(taskId)
+  }
+
+  // This method is not idempotent. After fetching a result set of size fetchSize each time, the corresponding results will be removed from the cache.
+  override def fetchMoreResultSet(taskId: String, fetchSize: Int): FetchResultResponse = {
+    val dataFrameResponse = DirectPushCache.fetchResultSetOfDataFrame(taskId, fetchSize)
+    if (dataFrameResponse.dataFrame != null) {
+      if (!dataFrameResponse.hasMoreData) {
+        succeedTasks.increase()
+      }
+      FetchResultResponse(
+        hasMoreData = dataFrameResponse.hasMoreData,
+        ArrowUtils.toArrow(dataFrameResponse.dataFrame)
+      )
+    }

Review Comment:
   Else is missing here



-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


Re: [PR] Support resultSet direct push for SparkSqlExecutor [linkis]

Posted by "peacewong (via GitHub)" <gi...@apache.org>.
peacewong merged PR #5170:
URL: https://github.com/apache/linkis/pull/5170


-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org


Re: [PR] Support resultSet direct push for SparkSqlExecutor [linkis]

Posted by "peacewong (via GitHub)" <gi...@apache.org>.
peacewong commented on code in PR #5170:
URL: https://github.com/apache/linkis/pull/5170#discussion_r1758822687


##########
linkis-engineconn-plugins/spark/src/main/scala/org/apache/linkis/engineplugin/spark/utils/ArrowUtils.scala:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.linkis.engineplugin.spark.utils
+
+import org.apache.arrow.memory.RootAllocator
+import org.apache.arrow.vector.{
+  BigIntVector,
+  BitVector,
+  FieldVector,
+  Float8Vector,
+  IntVector,
+  VarCharVector,
+  VectorSchemaRoot
+}
+import org.apache.arrow.vector.ipc.ArrowStreamWriter
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.types.{BooleanType, DoubleType, IntegerType, LongType, StringType}
+
+import java.io.ByteArrayOutputStream
+
+object ArrowUtils {
+
+  def toArrow(df: DataFrame): Array[Byte] = {
+    val allocator = new RootAllocator(Long.MaxValue)
+    val (root, fieldVectors) = createArrowVectors(df, allocator)
+    val outStream = new ByteArrayOutputStream()
+    val writer = new ArrowStreamWriter(root, null, outStream)
+
+    writer.start()
+    writer.writeBatch()
+    writer.end()
+    writer.close()
+
+    val arrowBytes = outStream.toByteArray
+    fieldVectors.foreach(_.close())
+    allocator.close()
+    arrowBytes
+  }
+
+  private def createArrowVectors(
+      df: DataFrame,
+      allocator: RootAllocator
+  ): (VectorSchemaRoot, List[FieldVector]) = {
+    val schema = df.schema
+    val fieldVectors = schema.fields.map { field =>
+      field.dataType match {
+        case IntegerType =>
+          val vector = new IntVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case LongType =>
+          val vector = new BigIntVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case DoubleType =>
+          val vector = new Float8Vector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case BooleanType =>
+          val vector = new BitVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case StringType =>
+          val vector = new VarCharVector(field.name, allocator)
+          vector.allocateNew(df.count().toInt)
+          vector
+        case _ => throw new IllegalArgumentException(s"Unsupported data type: ${field.dataType}")

Review Comment:
   Are other types not supported?  Can we convert to String by default if the type is not supported ?



-- 
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: notifications-unsubscribe@linkis.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org