zhengruifeng commented on code in PR #41980:
URL: https://github.com/apache/spark/pull/41980#discussion_r1261988227
##########
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:
it seems `cache` doesn't help:
```
In [1]: df = spark.createDataFrame([(1, 4), (2, 4), (3, 6)], ["A", "B"])
In [2]: df.cache()
Out[2]: DataFrame[A: bigint, B: bigint]
In [3]: df.createOrReplaceTempView("t")
In [4]: df2 = spark.sql("select * from t")
In [5]: spark.catalog.dropTempView("t")
Out[5]: True
In [6]: df2.show()
23/07/13 13:03:56 ERROR SparkConnectService: Error during: execute. UserId:
ruifeng.zheng. SessionId: 547b8abe-9802-4bc0-9b1c-241107b2c66c.
org.apache.spark.sql.AnalysisException: [TABLE_OR_VIEW_NOT_FOUND] The table
or view `t` cannot be found. Verify the spelling and correctness of the schema
and catalog.
If you did not qualify the name with a schema, verify the current_schema()
output, or qualify the name with the correct schema and catalog.
To tolerate the error on drop use DROP VIEW IF EXISTS or DROP TABLE IF
EXISTS.; line 1 pos 14;
'Project [*]
+- 'UnresolvedRelation [t], [], false
```
--
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]