Skip to content
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

Update doc for dynamic parameters supporting array #16660

Merged
merged 15 commits into from
Aug 7, 2024
4 changes: 4 additions & 0 deletions docs/api-reference/sql-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ The request body takes the following properties:
{
"type": "VARCHAR",
"value": "bar"
},
{
"type": "ARRAY",
"value": [-25.7, null, 36.85]
}
]
```
Expand Down
20 changes: 20 additions & 0 deletions docs/api-reference/sql-jdbc.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ statement.setString(2, "def");
final ResultSet resultSet = statement.executeQuery();
```

Sample code where dynamic parameters replace arrays using STRING_TO_ARRAY:
```java
PreparedStatement statement = connection.prepareStatement("select l1 from numfoo where SCALAR_IN_ARRAY(l1, STRING_TO_ARRAY(CAST(? as varchar),','))");
List<Integer> li = ImmutableList.of(0, 7);
String sqlArg = Joiner.on(",").join(li);
statement.setString(1, sqlArg);
statement.executeQuery();
```

Sample code using native array:
```java
PreparedStatement statement = connection.prepareStatement("select l1 from numfoo where SCALAR_IN_ARRAY(l1, ?)");
Iterable<Object> list = ImmutableList.of(0, 7);
ArrayFactoryImpl arrayFactoryImpl = new ArrayFactoryImpl(TimeZone.getDefault());
AvaticaType type = ColumnMetaData.scalar(Types.INTEGER, SqlType.INTEGER.name(), Rep.INTEGER);
Array array = arrayFactoryImpl.createArray(type, list);
statement.setArray(1, array);
statement.executeQuery();
```

## Examples

<!-- docs/tutorial-jdbc.md redirects here -->
Expand Down
16 changes: 15 additions & 1 deletion docs/querying/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,20 @@ SELECT * FROM druid.foo WHERE dim1 like CONCAT('%', ?, '%')

To solve this issue, explicitly provide the type of the dynamic parameter using the `CAST` keyword. Consider the fix for the preceding example:

```
```sql
SELECT * FROM druid.foo WHERE dim1 like CONCAT('%', CAST (? AS VARCHAR), '%')
```

Dynamic parameters can even replace arrays, reducing the parsing time. Refer to parameters in [api request-body](../api-reference/sql-api.md#request-body) for usage.

```sql
SELECT arrayColumn from druid.table where ARRAY_CONTAINS(?, arrayColumn)
```

With this, an IN filter being supplied with a lot of values, can be replaced by a dynamic parameter passed inside [SCALAR_IN_ARRAY](sql-functions.md#scalar_in_array)

```sql
SELECT count(city) from druid.table where SCALAR_IN_ARRAY(city, ?)
```
sreemanamala marked this conversation as resolved.
Show resolved Hide resolved

sample java code using dynamic parameters is provided [here](../api-reference/sql-jdbc.md#dynamic-parameters).
Loading