diff --git a/docs/api-reference/sql-api.md b/docs/api-reference/sql-api.md index e1fb53bc6eba..3a6234256188 100644 --- a/docs/api-reference/sql-api.md +++ b/docs/api-reference/sql-api.md @@ -93,6 +93,10 @@ The request body takes the following properties: { "type": "VARCHAR", "value": "bar" + }, + { + "type": "ARRAY", + "value": [-25.7, null, 36.85] } ] ``` diff --git a/docs/api-reference/sql-jdbc.md b/docs/api-reference/sql-jdbc.md index 8223e764ed49..affe9ea738b5 100644 --- a/docs/api-reference/sql-jdbc.md +++ b/docs/api-reference/sql-jdbc.md @@ -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 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 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 diff --git a/docs/querying/sql.md b/docs/querying/sql.md index 25de2adec421..056ea9119b62 100644 --- a/docs/querying/sql.md +++ b/docs/querying/sql.md @@ -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 the parameters in the [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, ?) +``` + +sample java code using dynamic parameters is provided [here](../api-reference/sql-jdbc.md#dynamic-parameters).