<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Parameterized_queries_(Spark_SQL)</id>
	<title>Parameterized queries (Spark SQL) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Parameterized_queries_(Spark_SQL)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Parameterized_queries_(Spark_SQL)&amp;action=history"/>
	<updated>2026-12-06T00:07:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Parameterized_queries_(Spark_SQL)&amp;diff=165&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Parameterized_queries_(Spark_SQL)&amp;diff=165&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:47Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Parameterized queries (Spark SQL)&lt;br /&gt;
| also_known_as   = Parameter markers, parameterized SQL, bind parameters&lt;br /&gt;
| field           = Query languages, application security&lt;br /&gt;
| introduced      = Apache Spark 3.4.0 (named parameters); 3.5.0 (positional parameters)&lt;br /&gt;
| key_parts       = Named markers (&amp;lt;code&amp;gt;:name&amp;lt;/code&amp;gt;), unnamed markers (&amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt;), &amp;lt;code&amp;gt;args&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;SparkSession.sql()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;EXECUTE IMMEDIATE ... USING&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;IDENTIFIER&amp;lt;/code&amp;gt; clause&lt;br /&gt;
| implementations = [[Apache Spark]]&lt;br /&gt;
| related         = [[Spark SQL]], [[Spark Connect]], [[SQL injection]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (docs/sql-ref-parameter-markers.md, docs/sql-ref-syntax-aux-exec-imm.md, docs/sql-ref-identifier-clause.md, python/pyspark/sql/session.py, python/pyspark/sql/sql_formatter.py, SQLConf.scala, ParameterHandler.scala, SparkSqlParser.scala, error-conditions.json) and the Spark 3.4.0, 3.5.0, 4.0.0 and 4.1.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]] SQL, a &amp;#039;&amp;#039;&amp;#039;parameterized query&amp;#039;&amp;#039;&amp;#039; is a SQL statement that contains typed placeholders, called parameter markers, whose values are supplied separately by the calling API instead of being written into the SQL text.&amp;lt;ref name=&amp;quot;markers&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-parameter-markers.html |title=Parameter markers (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Spark supports named markers (&amp;lt;code&amp;gt;:name&amp;lt;/code&amp;gt;) and unnamed, positional markers (&amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt;). Because the values never become part of the statement text, the Spark documentation states that parameter markers protect code from SQL injection attacks.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What are parameter markers in Spark SQL? ==&lt;br /&gt;
A Spark SQL parameter marker stands for a value that the caller binds when the statement runs. The Spark documentation describes two kinds:&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Named parameter markers&amp;#039;&amp;#039;&amp;#039; are written as a colon followed by an unqualified identifier, for example &amp;lt;code&amp;gt;:min_amount&amp;lt;/code&amp;gt;. The caller passes name-value pairs. The same name can appear several times in one statement, and the caller may pass names that the statement does not use. The colon keeps parameter names apart from column names and SQL variables.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Unnamed parameter markers&amp;#039;&amp;#039;&amp;#039; are written as &amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt;. The caller passes an ordered list, and each &amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt; consumes the next value. Extra values are allowed.&lt;br /&gt;
&lt;br /&gt;
A statement cannot mix named and unnamed markers.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt; Spark raises the error &amp;lt;code&amp;gt;INVALID_QUERY_MIXED_QUERY_PARAMETERS&amp;lt;/code&amp;gt; (SQLSTATE 42613) when it does, and &amp;lt;code&amp;gt;UNBOUND_SQL_PARAMETER&amp;lt;/code&amp;gt; (SQLSTATE 42P02) when a marker has no value.&amp;lt;ref name=&amp;quot;errors&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/common/utils/src/main/resources/error/error-conditions.json |title=error-conditions.json (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do parameterized queries prevent SQL injection? ==&lt;br /&gt;
A Spark SQL parameter marker always receives a value as a typed literal, never as SQL text written by the caller, so the value cannot change the structure of the statement. The Spark documentation gives this separation of values from SQL text as the reason that parameter markers protect against SQL injection.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt; A string such as &amp;lt;code&amp;gt;&amp;#039; OR 1=1 --&amp;lt;/code&amp;gt; passed as a parameter is treated as a string value, not as SQL.&lt;br /&gt;
&lt;br /&gt;
In Spark 4.3 the binding happens in one of two ways. By default, a pre-parser finds the markers and replaces each one with the SQL form of a resolved &amp;lt;code&amp;gt;Literal&amp;lt;/code&amp;gt; expression, which quotes and escapes string values; the parameter handler rejects any value that is not a resolved literal. With the legacy setting described below, the markers are kept in the parsed plan and substitution is deferred to the analysis phase.&amp;lt;ref name=&amp;quot;handler&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParameterHandler.scala |title=ParameterHandler.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;parser&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkSqlParser.scala |title=SparkSqlParser.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Parameter markers replace literal values only. Table names, column names and other identifiers cannot be passed as plain markers. For those, Spark SQL provides the &amp;lt;code&amp;gt;IDENTIFIER&amp;lt;/code&amp;gt; clause, which turns a constant string, which can be built from string literals and parameter markers, into an object name. The Spark documentation states that the clause exists to allow templating of identifiers without opening up the risk of SQL injection, and that the string must be a valid, possibly qualified, identifier.&amp;lt;ref name=&amp;quot;identifier&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-identifier-clause.html |title=IDENTIFIER clause (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How are parameters passed from PySpark? ==&lt;br /&gt;
In PySpark, &amp;lt;code&amp;gt;SparkSession.sql(sqlQuery, args=None, **kwargs)&amp;lt;/code&amp;gt; takes the parameters in its &amp;lt;code&amp;gt;args&amp;lt;/code&amp;gt; argument: a dictionary for named markers or a list for positional markers.&amp;lt;ref name=&amp;quot;session&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/session.py |title=session.py (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Each value is a Python object that Spark converts to a SQL literal, or a &amp;lt;code&amp;gt;Column&amp;lt;/code&amp;gt; made with a literal or with a collection constructor such as &amp;lt;code&amp;gt;array()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;map()&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;struct()&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt; The method docstring marks parameterized SQL and Spark Connect support as added in 3.4.0 and positional parameters as added in 3.5.0.&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt; Scala and Java use the &amp;lt;code&amp;gt;args&amp;lt;/code&amp;gt; parameter of &amp;lt;code&amp;gt;SparkSession.sql()&amp;lt;/code&amp;gt; with a &amp;lt;code&amp;gt;Map&amp;lt;/code&amp;gt; or an array.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following PySpark example runs on Spark 4.3, in classic mode or through [[Spark Connect]]. The first query uses named markers, the second positional markers, and the third an &amp;lt;code&amp;gt;IDENTIFIER&amp;lt;/code&amp;gt; clause for the view name:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
spark.range(10).createOrReplaceTempView(&amp;quot;numbers&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Named markers: a dictionary&lt;br /&gt;
spark.sql(&lt;br /&gt;
    &amp;quot;SELECT id FROM numbers WHERE id &amp;gt; :lo AND id &amp;lt; :hi&amp;quot;,&lt;br /&gt;
    args={&amp;quot;lo&amp;quot;: 3, &amp;quot;hi&amp;quot;: 7},&lt;br /&gt;
).show()&lt;br /&gt;
&lt;br /&gt;
# Positional markers: a list, consumed in order&lt;br /&gt;
spark.sql(&amp;quot;SELECT id FROM numbers WHERE id BETWEEN ? AND ?&amp;quot;, args=[2, 4]).show()&lt;br /&gt;
&lt;br /&gt;
# A table name passed safely through IDENTIFIER&lt;br /&gt;
spark.sql(&amp;quot;SELECT count(*) AS n FROM IDENTIFIER(:tbl)&amp;quot;, args={&amp;quot;tbl&amp;quot;: &amp;quot;numbers&amp;quot;}).show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How are parameters passed in pure SQL? ==&lt;br /&gt;
Spark SQL binds parameters without a host language through &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt;, which runs a SQL statement given as a string. Its &amp;lt;code&amp;gt;USING&amp;lt;/code&amp;gt; clause supplies constant expressions that bind to the markers, by position for unnamed markers and by name or alias for named markers. An optional &amp;lt;code&amp;gt;INTO&amp;lt;/code&amp;gt; clause stores the result of a single-row query in SQL variables.&amp;lt;ref name=&amp;quot;execimm&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-ref-syntax-aux-exec-imm.html |title=EXECUTE IMMEDIATE (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
DECLARE lo = 3;&lt;br /&gt;
EXECUTE IMMEDIATE &amp;#039;SELECT id FROM range(10) WHERE id &amp;gt; :lo AND id &amp;lt; :hi&amp;#039;&lt;br /&gt;
  USING lo AS lo, 7 AS hi;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Where can parameter markers appear? ==&lt;br /&gt;
The Spark 4.3 documentation states that parameter markers can be used wherever literal values are allowed in SQL syntax.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt; Spark 4.1.0 extended this with SPARK-53573, which allows parameter markers everywhere through a pre-parser.&amp;lt;ref name=&amp;quot;r410&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4.1.0.html |title=Spark Release 4.1.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The internal configuration &amp;lt;code&amp;gt;spark.sql.legacy.parameterSubstitution.constantsOnly&amp;lt;/code&amp;gt; (default &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;, added in 4.1.0) restores the older behavior, in which markers are allowed only where constant literals are expected in queries and data manipulation statements.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt; reference shows markers used inside a type definition, &amp;lt;code&amp;gt;DECIMAL(:p, :s)&amp;lt;/code&amp;gt;, and in a column alias through &amp;lt;code&amp;gt;IDENTIFIER(:col)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;execimm&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Parameter markers vs Python string formatting ==&lt;br /&gt;
PySpark&amp;#039;s &amp;lt;code&amp;gt;SparkSession.sql()&amp;lt;/code&amp;gt; also accepts keyword arguments (&amp;lt;code&amp;gt;**kwargs&amp;lt;/code&amp;gt;) that it substitutes into the SQL text with the Python standard string formatter, using &amp;lt;code&amp;gt;{name}&amp;lt;/code&amp;gt; placeholders. The docstring marks this as added in 3.3.0 and describes it as experimental and unstable.&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt; The two mechanisms differ:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Parameter markers (&amp;lt;code&amp;gt;args&amp;lt;/code&amp;gt;) !! Formatter placeholders (&amp;lt;code&amp;gt;**kwargs&amp;lt;/code&amp;gt;)&lt;br /&gt;
|-&lt;br /&gt;
! Syntax in SQL&lt;br /&gt;
| &amp;lt;code&amp;gt;:name&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;?&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;{name}&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! When the value is applied&lt;br /&gt;
| Bound as a typed literal by Spark, separately from the SQL text the caller writes&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;handler&amp;quot; /&amp;gt;&lt;br /&gt;
| Rendered into the SQL text before parsing&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Handling of values&lt;br /&gt;
| Python values converted to SQL literals; &amp;lt;code&amp;gt;Column&amp;lt;/code&amp;gt; literals and collection constructors accepted&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt;&lt;br /&gt;
| A &amp;lt;code&amp;gt;DataFrame&amp;lt;/code&amp;gt; becomes a temporary view name, a &amp;lt;code&amp;gt;Column&amp;lt;/code&amp;gt; must be a plain column reference, a Python string becomes an escaped string literal, and other values are inserted as formatted by Python&amp;lt;ref name=&amp;quot;formatter&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/sql_formatter.py |title=sql_formatter.py (tag v4.3.0) |publisher=Apache Spark on GitHub |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Available in&lt;br /&gt;
| PySpark, Scala, Java, and SQL through &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
| PySpark only&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Both can be used in one call; the PySpark docstring includes an example that references a DataFrame with &amp;lt;code&amp;gt;{df}&amp;lt;/code&amp;gt; and binds a value with &amp;lt;code&amp;gt;:minB&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;session&amp;quot; /&amp;gt; For values that come from users, the parameter markers are the mechanism that the Spark documentation describes as injection-safe.&amp;lt;ref name=&amp;quot;markers&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 3.4.0&amp;#039;&amp;#039;&amp;#039; added parameterized SQL (SPARK-41271, SPARK-42702) and PySpark support for it (SPARK-41666).&amp;lt;ref name=&amp;quot;r340&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-4-0.html |title=Spark Release 3.4.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 3.5.0&amp;#039;&amp;#039;&amp;#039; added positional parameters in Scala and Java (SPARK-44066) and in Python (SPARK-44140), and the &amp;lt;code&amp;gt;IDENTIFIER&amp;lt;/code&amp;gt; clause (SPARK-43205).&amp;lt;ref name=&amp;quot;r350&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-5-0.html |title=Spark Release 3.5.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.0.0&amp;#039;&amp;#039;&amp;#039; added &amp;lt;code&amp;gt;EXECUTE IMMEDIATE&amp;lt;/code&amp;gt; (SPARK-46246), and its release notes list support for map and array parameters in parameterized &amp;lt;code&amp;gt;sql()&amp;lt;/code&amp;gt; (SPARK-45137, SPARK-45235).&amp;lt;ref name=&amp;quot;r400&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-0-0.html |title=Spark Release 4.0.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.1.0&amp;#039;&amp;#039;&amp;#039; allowed parameter markers everywhere through a pre-parser (SPARK-53573).&amp;lt;ref name=&amp;quot;r410&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Spark SQL]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
* [[Higher-order functions (Spark SQL)]]&lt;br /&gt;
* [[SQL injection]]&lt;br /&gt;
* [[Apache Spark]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>