cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot pass arrays to spark.sql() using named parameter markers

runninsavvy
New Contributor II

Hello all,

I am attempting to use named parameter markers as shown in this article: https://docs.databricks.com/en/sql/language-manual/sql-ref-parameter-marker.html#named-parameter-mar...

I can pass strings and numbers in perfectly fine, but the issues arise when I try to pass in arrays. An example:

 

 

 

spark.sql("SELECT 1 IN (1, 2, 3)", args=params).show()

 

 

 

Works just fine, and the function is able to run. However:

 

 

 

spark.sql("SELECT 1 IN :test", args={"test": [1, 2, 3]}).show()

 

 

 

and also 

 

 

 

spark.sql("SELECT 1 IN ARRAY(:test)", args={"test": [1, 2, 3]}).show()

 

 

 

give me vague syntax errors, and even just trying to reference the passed-in array in a basic sense like 

 

 

 

spark.sql("SELECT :test", args={"test": [1, 2, 3]}).show()

 

 

 

gives the error "[INVALID_SQL_ARG] The argument test of 'sql()' is invalid. Consider to replace it by a SQL literal."

 

Is there any way to pass in an array using named parameters to pyspark.sql()? Thanks 

1 ACCEPTED SOLUTION

Accepted Solutions

User16502773013
Contributor II
Contributor II

Hello @runninsavvy ,

The following code sample can be used in such case

 

val argArray = Array(1, 2, 3)
val argMap = Map("param" -> argArray.mkString(","))

spark.sql("SELECT 1 IN (SELECT explode(split(:param, ',')))",argMap).show()

View solution in original post

2 REPLIES 2

User16502773013
Contributor II
Contributor II

Hello @runninsavvy ,

The following code sample can be used in such case

 

val argArray = Array(1, 2, 3)
val argMap = Map("param" -> argArray.mkString(","))

spark.sql("SELECT 1 IN (SELECT explode(split(:param, ',')))",argMap).show()

That worked, thanks!

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!