You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "grundprinzip (via GitHub)" <gi...@apache.org> on 2023/07/13 05:42:18 UTC

[GitHub] [spark] grundprinzip commented on a diff in pull request #41980: [SPARK-41811][PYTHON][CONNECT] Implement SparkSession.sql's string formatter

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


##########
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:
   Strictly logically speaking the existing behavior is weird because it caches the analysis result even though the view is gone. IMHO this behavior is almost undefined but I understand that it's existing. 
   
   The current behavior almost feels like reference counting :( 
   
   TBH I think it's ok with the difference in behavior. My 2c



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org