Guide to Using SQL Statement Placeholders in Spark Jobs
What Is a Placeholder?
A placeholder is a parameter marker used in SQL statements, represented in the format :parameter_name to replace specific values or text. It is equivalent to first writing the complete SQL logic framework, marking the data positions to be filled with placeholders. When executed, the system automatically and securely fills user input data into these positions. This method completely separates the SQL logic framework from the data, avoiding SQL injection risks and syntax errors caused by directly concatenating user input. It ensures security while improving code maintainability.
Placeholders can only replace values and cannot be used to replace SQL structures such as table names, column names, or keywords.
Why Use Placeholders?
- Type safety.
After declaring value_type, the system automatically converts the input value to the correct type, avoiding implicit conversion errors.
For example, if 123 is passed but declared as a DATE type, the system processes it according to date rules rather than directly concatenating it as the string 123.
- SQL injection prevention.
Placeholders can only replace values and cannot be used to replace SQL structures such as column names or table names. Parameters and SQL logic are completely separated, fundamentally defending against SQL injection attacks.
- Execution plan reuse.
The SparkSQL engine can cache logical plans that do not contain specific parameters. When the same SQL is executed with different parameter values, the cached execution plan can be reused directly, improving compilation speed.
Use Cases
- Using placeholders in Spark SQL jobs: When executing a single SQL statement by calling an API, placeholders can be used in the SQL, and the placeholder values and types can be specified in the request parameters.
- Using placeholders in Spark jobs: When submitting a job of type spark_sql_scripting_job by calling an API, placeholders can be used in the SQL script file.
Parameter Types Supported by SQL Statement Placeholders
| Type | Description | Input Value Format | Example Input Value |
|---|---|---|---|
| STRING | String type (default type) | Pass a string directly. | hello |
| INTEGER | Integer type | Pass a numeric string directly. | 100 |
| DECIMAL | Fixed-point number type | Pass a decimal string directly. | 12.1 |
| DATE | Date type | Only supports long values (millisecond timestamp). | -1500000000000 |
| TIMESTAMP | Timestamp type | Only supports long values (millisecond timestamp). | -1779866327983 |
Using SQL Statement Placeholders in Spark SQL Jobs
- Mark placeholders in the SQL statement using :parameter-name.
INSERT INTO test_users_0527 VALUES (3, 'liu', 20, :param2, :param3)
Here, :param2 and :param3 are placeholders and can be customized.
- Specify the value and type of each placeholder in the parameters array of the request body.
{ "endpoint_name": "spark-sql-cwk", "catalog_context": { "catalog_name": "lzh", "database_name": "test" }, "statement": "INSERT INTO test_users_0527 VALUES (3, 'liu', 20, :param2, :param3)", "parameters": [ { "key": "param2", "value": "string1", "value_type": "STRING" }, { "key": "param3", "value": "123", "value_type": "DATE" } ], "spark_config": { "spark.driver.cores": "2" }, "timeout": { "launching_timeout": 10800 }, "labels": [ { "key": "sql", "value": "test" } ] }Table 2 Request body placeholder fields Field
Mandatory
Description
key
Yes
Placeholder name, must match :parameter_name in the SQL.
value
Yes
Actual value to replace.
value_type
No
Value type. It defaults to STRING if unspecified.
The order of parameters in the parameters array in the request body is not important. The system matches parameters by the key name.
Using SQL Statement Placeholders in Spark Jobs
- Write a SQL script file (must be wrapped with BEGIN and END).
For example, create a file named sql_scripting_insert_ljh.sql.
BEGIN SELECT * FROM (VALUES (3, :param1, 20, :param2, :param3, :param4)) AS t(id, name, age, date_birth, time_birth, decimal_test); END
- Specify the placeholder values in sql_scripting_parameters in the job configuration.
{ "endpoint_name": "spark-job-vu9e", "name": "test-job-dsds", "spark_version": "4.0.0", "job_config": { "type": "spark_sql_scripting_job", "sql_scripting_file": "obs://obs-jiaxg/zhaoyu/spark_sql_scripting/sql/sql_scripting_insert_ljh.sql", "sql_scripting_result_to_obs": true, "sql_scripting_parameters": [ { "key": "param1", "value": "liu" }, { "key": "param2", "value": "-1500000000000", "value_type": "DATE" }, { "key": "param3", "value": "-1779866327983", "value_type": "TIMESTAMP" }, { "key": "param4", "value": "12.1", "value_type": "DECIMAL" } ] } }Table 3 Some fields in the request body Field
Mandatory
Description
sql_scripting_file
Yes
Path of the SQL script file on OBS.
sql_scripting_result_to_obs
No
Whether to write the result to OBS.
sql_scripting_parameters
No
List of placeholder parameters.
- The value of key in sql_scripting_parameters in the request body must match :parameter_name in the SQL.
- The order of parameters in sql_scripting_parameters in the request body is not important. The system matches parameters by the key name.
Reference Templates
- Spark SQL job request body template
{ "endpoint_name": "<endpoint-name>", "catalog_context": { "catalog_name": "<catalog-name>", "database_name": "<database-name>" }, "statement": "<SQL-statement-containing-placeholders>", "parameters": [ { "key": "<placeholder-name>", "value": "<replacement-value>", "value_type": "STRING|INTEGER|DECIMAL|DATE|TIMESTAMP" } ] } - Spark job request body template.
{ "endpoint_name": "<endpoint-name>", "name": "<job-name>", "spark_version": "4.0.0", "job_config": { "type": "spark_sql_scripting_job", "sql_scripting_file": "<path-of-the-SQL-script-file-on-OBS>", "sql_scripting_result_to_obs": true, "sql_scripting_parameters": [ { "key": "<placeholder-name>", "value": "<replacement-value>", "value_type": "STRING|INTEGER|DECIMAL|DATE|TIMESTAMP" } ] } }
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot