zhengruifeng commented on code in PR #41980:
URL: https://github.com/apache/spark/pull/41980#discussion_r1262091888


##########
python/pyspark/sql/connect/session.py:
##########
@@ -489,13 +489,31 @@ def createDataFrame(
 
     createDataFrame.__doc__ = PySparkSession.createDataFrame.__doc__
 
-    def sql(self, sqlQuery: str, args: Optional[Union[Dict[str, Any], List]] = 
None) -> "DataFrame":
-        cmd = SQL(sqlQuery, args)
-        data, properties = 
self.client.execute_command(cmd.command(self._client))
-        if "sql_command_result" in properties:
-            return 
DataFrame.withPlan(CachedRelation(properties["sql_command_result"]), self)
-        else:
-            return DataFrame.withPlan(SQL(sqlQuery, args), self)
+    def sql(
+        self,
+        sqlQuery: str,
+        args: Optional[Union[Dict[str, Any], List]] = None,
+        **kwargs: Any,
+    ) -> "DataFrame":
+
+        if len(kwargs) > 0:
+            from pyspark.sql.connect.sql_formatter import SQLStringFormatter
+
+            formatter = SQLStringFormatter(self)
+            sqlQuery = formatter.format(sqlQuery, **kwargs)
+
+        try:
+            cmd = SQL(sqlQuery, args)
+            data, properties = 
self.client.execute_command(cmd.command(self._client))
+            if "sql_command_result" in properties:
+                return 
DataFrame.withPlan(CachedRelation(properties["sql_command_result"]), self)
+            else:
+                return DataFrame.withPlan(SQL(sqlQuery, args), self)
+        finally:
+            if len(kwargs) > 0:

Review Comment:
   Even if I cache the result dataframe `df2`, trigger an action on it, it 
still not work.
   I think the reason is that, each time the `df2` is used, it just send a 
`UnresolvedRelation [t]` to the server. After we drop the temp view, it can no 
longer be resolved.
   
   This PR will create extra temp views, if `kwargs` contains DataFrames vars.
   On way I can image is to cache the resolved plan in the server side, by 
using 
   
https://github.com/apache/spark/blob/57bbb4c6e7309e0beaeb3618978dcaf75ce9a7fe/connector/connect/server/src/main/scala/org/apache/spark/sql/connect/service/SessionHolder.scala#L131-L150



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