-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql support for dynamic parameters #6974
Changes from 44 commits
0cc2c66
3c98981
f668777
bcce2be
33c1894
568364d
be84ac8
3bedbd9
17d32aa
a1038bc
37668fc
62ac051
ed23a8c
c6306ef
88f6084
2c00301
57b1342
c7968d8
27cebc6
01fcf4e
a7075be
3c50d9b
5ea94c4
7b554c0
ed4a40f
e018a55
6b4175f
53b155f
059320f
9ff9667
97ea9e5
ee5b46d
01cbeae
6e670d2
c773e21
aacae42
d069f83
682833c
8987d1b
2819431
e1dceef
8bdc691
d967e12
8f669ee
6279270
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,9 @@ like `100` (denoting an integer), `100.0` (denoting a floating point value), or | |
timestamps can be written like `TIMESTAMP '2000-01-01 00:00:00'`. Literal intervals, used for time arithmetic, can be | ||
written like `INTERVAL '1' HOUR`, `INTERVAL '1 02:03' DAY TO MINUTE`, `INTERVAL '1-2' YEAR TO MONTH`, and so on. | ||
|
||
Druid SQL supports dynamic parameters using the `?` syntax where parameters are bound to `?` in order. Replace any | ||
literal with a `?` and supply parameters to the query and the values will be bound at execution time. | ||
|
||
Druid SQL supports SELECT queries with the following structure: | ||
|
||
``` | ||
|
@@ -518,6 +521,17 @@ of configuration. | |
You can make Druid SQL queries using JSON over HTTP by posting to the endpoint `/druid/v2/sql/`. The request should | ||
be a JSON object with a "query" field, like `{"query" : "SELECT COUNT(*) FROM data_source WHERE foo = 'bar'"}`. | ||
|
||
##### Request | ||
|
||
|Property|Type|Description|Required| | ||
|--------|----|-----------|--------| | ||
|`query`|`String`| SQL query to run| yes | | ||
|`resultFormat`|`String` (`ResultFormat`)| Result format for output | no (default `"object"`)| | ||
|`header`|`Boolean`| Write column name header for supporting formats| no (default `false`)| | ||
|`context`|`Object`| Connection context map. see [connection context parameters](#connection-context)| no | | ||
|`parameters`|`SqlParameter` list| List of query parameters for parameterized queries. | no | | ||
|
||
|
||
You can use _curl_ to send SQL queries from the command-line: | ||
|
||
```bash | ||
|
@@ -540,7 +554,27 @@ like: | |
} | ||
``` | ||
|
||
Metadata is available over the HTTP API by querying [system tables](#metadata-tables). | ||
Parameterized SQL queries are also supported: | ||
|
||
```json | ||
{ | ||
"query" : "SELECT COUNT(*) FROM data_source WHERE foo = ? AND __time > ?", | ||
"parameters": [ | ||
{ "type": "VARCHAR", "value": "bar"}, | ||
{ "type": "TIMESTAMP", "value": "2000-01-01 00:00:00" } | ||
] | ||
} | ||
``` | ||
|
||
##### SqlParameter | ||
|
||
|Property|Type|Description|Required| | ||
|--------|----|-----------|--------| | ||
|`type`|`String` (`SqlType`) | String value of `SqlType` of parameter. [`SqlType`](https://calcite.apache.org/avatica/apidocs/org/apache/calcite/avatica/SqlType.html) is an friendly wrapper around [`java.sql.Types`](https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html?is-external=true)|yes| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: "...is a friendly wrapper..." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|`value`|`Object`| Value of the parameter|yes| | ||
|
||
|
||
Metadata is also available over the HTTP API by querying [system tables](#metadata-tables). | ||
|
||
#### Responses | ||
|
||
|
@@ -617,8 +651,7 @@ try (Connection connection = DriverManager.getConnection(url, connectionProperti | |
``` | ||
|
||
Table metadata is available over JDBC using `connection.getMetaData()` or by querying the | ||
["INFORMATION_SCHEMA" tables](#metadata-tables). Parameterized queries (using `?` or other placeholders) don't work properly, | ||
so avoid those. | ||
["INFORMATION_SCHEMA" tables](#metadata-tables). | ||
|
||
#### Connection stickiness | ||
|
||
|
@@ -630,6 +663,17 @@ the necessary stickiness even with a normal non-sticky load balancer. Please see | |
|
||
Note that the non-JDBC [JSON over HTTP](#json-over-http) API is stateless and does not require stickiness. | ||
|
||
### Dynamic Parameters | ||
|
||
Parameterized queries are supported with JDBC: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I misread this at first as meaning JDBC enables parameterized queries somehow. Is this wording clearer: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed |
||
|
||
```java | ||
PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) AS cnt FROM druid.foo WHERE dim1 = ? OR dim1 = ?"); | ||
statement.setString(1, "abc"); | ||
statement.setString(2, "def"); | ||
final ResultSet resultSet = statement.executeQuery(); | ||
``` | ||
|
||
### Connection context | ||
|
||
Druid SQL supports setting connection parameters on the client. The parameters in the table below affect SQL planning. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: This description feels like it could be expanded upon just a bit, just to draw out what is meant by "in order," for one thing. How about something like:
"Druid SQL supports dynamic parameters in question mark (
?
) syntax, where parameters are bound to the?
placeholders at execution time. To use dynamic parameters, replace any literal in the query with a?
character and ensure that corresponding parameter values are provided at execution time. Parameters are bound to the placeholders in the order in which they are passed."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm, changed