diff --git a/CHANGELOG.md b/CHANGELOG.md index b861f40..2352965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v8.3.0 (2024-09-02) - add support for `Query\Builder::whereNotInUnnest(...)` (#225) +- `Query\Builder::whereIn` will now wrap values in `UNNEST` if the number of values exceeds the limit (950). (#) # v8.2.0 (2024-08-05) diff --git a/README.md b/README.md index a8b2280..332d951 100644 --- a/README.md +++ b/README.md @@ -99,8 +99,9 @@ Currently only supports Spanner running GoogleSQL (PostgreSQL mode is not suppor ### Query - [Binding more than 950 parameters in a single query will result in an error](https://cloud.google.com/spanner/quotas#query-limits) - by the server. You may by-pass this limitation by using `Query\Builder::whereInUnnest(...)` method to pass the values - as an array and unnest them on the server side instead of using query parameters. + by the server. In order to by-pass this limitation, this driver will attempt to switch to using `Query\Builder::whereInUnnest(...)` + internally when the passed parameter exceeds the limit set by `parameter_unnest_threshold` config (default: `900`). + You can turn this feature off by setting the value to `false`. ### Eloquent If you use interleaved keys, you MUST define them in the `interleaveKeys` property, or else you won't be able to save. diff --git a/src/Query/Builder.php b/src/Query/Builder.php index e1731a6..38ae016 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -130,10 +130,9 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) { // If parameter is over the limit, Spanner will throw an error. We will bypass this limit by // using UNNEST(). This is enabled by default, but can be disabled by setting the config. - if (is_countable($values) && count($values) > self::PARAMETER_LIMIT) { - if ($this->connection->getConfig('use_unnest_on_parameter_overflow') ?? true) { - return $this->whereInUnnest($column, $values, $boolean, $not); - } + $unnestThreshold = $this->connection->getConfig('parameter_unnest_threshold') ?? 900; + if ($unnestThreshold !== false && is_countable($values) && count($values) > $unnestThreshold) { + return $this->whereInUnnest($column, $values, $boolean, $not); } return parent::whereIn($column, $values, $boolean, $not); diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index f780fdc..8bce39d 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -1099,7 +1099,7 @@ public function test_whereIn_with_unnest_overflow_flag_turned_off(): void $this->expectExceptionMessage('Number of parameters in query exceeds the maximum allowed limit of 950.'); $this->expectException(QueryException::class); - config()->set('database.connections.main.use_unnest_on_parameter_overflow', false); + config()->set('database.connections.main.parameter_unnest_threshold', false); $query = $this->getDefaultConnection()->table(self::TABLE_NAME_USER); $query->whereIn('userId', array_map(Uuid::uuid4()->toString(...), range(1, 1000)))->get(); }