You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "paulk-asert (via GitHub)" <gi...@apache.org> on 2026/04/18 22:56:27 UTC
[PR] GROOVY-5436: add Sql.inList for collection expansion in SQL IN clauses [groovy]
paulk-asert opened a new pull request, #2474:
URL: https://github.com/apache/groovy/pull/2474
(no comment)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GROOVY-5436: add Sql.inList for collection expansion in SQL IN clauses [groovy]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2474:
URL: https://github.com/apache/groovy/pull/2474#discussion_r3106028199
##########
subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java:
##########
@@ -938,6 +945,48 @@ public Object getObject() {
};
}
+ /**
+ * Wraps a collection so that it expands to a comma-separated list of
+ * positional placeholders when bound to a SQL {@code IN} clause.
+ * <p>
+ * Example usage:
+ * <pre>
+ * def ids = [1, 2, 3]
+ * sql.rows("SELECT name FROM t WHERE id IN (?)", [Sql.inList(ids)])
+ * // executed as: SELECT name FROM t WHERE id IN (?, ?, ?)
+ * // params: [1, 2, 3]
+ *
+ * // GString form:
+ * sql.rows "SELECT name FROM t WHERE id IN (${Sql.inList(ids)})"
+ *
+ * // Named-parameter form:
+ * sql.rows("SELECT name FROM t WHERE id IN (:ids)", [ids: Sql.inList(ids)])
+ * </pre>
+ * <p>
+ * The collection is snapshotted at construction. Null or empty collections
+ * are rejected with an {@link IllegalArgumentException} — callers must
+ * guard empty input explicitly (e.g. return early or omit the clause).
+ *
+ * @param values the collection to expand; must be non-null and non-empty
+ * @return a marker usable as a parameter value in any Sql execution method
Review Comment:
The Javadoc states the returned marker is usable "in any Sql execution method", but this isn't true for APIs that prepare the statement once and then bind repeatedly (e.g., withBatch(...) using BatchingPreparedStatementWrapper). Those code paths don't run through InListExpander, and also can't safely support varying IN-list sizes without re-preparing. Please narrow the claim (e.g., rows/eachRow/executeUpdate/call variants) or explicitly document the unsupported cases.
```suggestion
* <p>
* This marker is supported by Sql execution methods that expand SQL text
* before preparing the statement, such as {@code rows}, {@code eachRow},
* {@code executeUpdate} and {@code call} variants that accept parameters.
* It is not supported by APIs that prepare a statement once and then bind
* repeatedly, such as {@code withBatch(...)}.
*
* @param values the collection to expand; must be non-null and non-empty
* @return a marker usable as a parameter value in supported Sql execution methods
```
##########
subprojects/groovy-sql/src/main/java/groovy/sql/InListExpander.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.sql;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Rewrites a SQL string and parameter list so that {@link InListParameter}
+ * markers expand to {@code ?, ?, ..., ?} placeholders in the SQL and their
+ * contained values are spliced into the parameter list at the corresponding
+ * position.
+ * <p>
+ * The SQL is walked character-by-character so that {@code ?} characters
+ * appearing inside single-quoted strings, double-quoted identifiers, line
+ * comments, and block comments are ignored.
+ * <p>
+ * Package-private; consumed by {@link Sql} prior to {@code prepareStatement}.
+ */
+final class InListExpander {
+
+ private InListExpander() {}
+
+ static SqlWithParams expand(String sql, List<?> params) {
+ if (!containsInList(params)) {
+ return new SqlWithParams(sql, new ArrayList<>(params));
+ }
+ StringBuilder out = new StringBuilder(sql.length());
+ List<Object> expanded = new ArrayList<>(params.size());
+ int paramIdx = 0;
+ int i = 0;
+ int len = sql.length();
+ while (i < len) {
+ char c = sql.charAt(i);
+ switch (c) {
+ case '\'':
+ i = copyQuoted(sql, i, '\'', out);
+ break;
+ case '"':
+ i = copyQuoted(sql, i, '"', out);
+ break;
+ case '-':
+ if (i + 1 < len && sql.charAt(i + 1) == '-') {
+ i = copyLineComment(sql, i, out);
+ } else {
+ out.append(c);
+ i++;
+ }
+ break;
+ case '/':
+ if (i + 1 < len && sql.charAt(i + 1) == '*') {
+ i = copyBlockComment(sql, i, out);
+ } else {
+ out.append(c);
+ i++;
+ }
+ break;
+ case '?':
+ if (paramIdx < params.size() && params.get(paramIdx) instanceof InListParameter) {
+ Collection<?> values = ((InListParameter) params.get(paramIdx)).getValues();
+ Iterator<?> it = values.iterator();
+ out.append('?');
+ expanded.add(it.next());
+ while (it.hasNext()) {
Review Comment:
InListExpander assumes every InListParameter has at least one value and calls iterator.next() unconditionally. Since InListParameter is public, a custom implementation (or an unexpected getValues() result) could return an empty collection and trigger a NoSuchElementException here. Consider validating that values is non-null/non-empty and throwing an IllegalArgumentException with a clear message (consistent with Sql.inList) instead of failing with NoSuchElementException.
##########
subprojects/groovy-sql/src/main/java/groovy/sql/InListExpander.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package groovy.sql;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Rewrites a SQL string and parameter list so that {@link InListParameter}
+ * markers expand to {@code ?, ?, ..., ?} placeholders in the SQL and their
+ * contained values are spliced into the parameter list at the corresponding
+ * position.
+ * <p>
+ * The SQL is walked character-by-character so that {@code ?} characters
+ * appearing inside single-quoted strings, double-quoted identifiers, line
+ * comments, and block comments are ignored.
+ * <p>
+ * Package-private; consumed by {@link Sql} prior to {@code prepareStatement}.
+ */
+final class InListExpander {
+
+ private InListExpander() {}
+
+ static SqlWithParams expand(String sql, List<?> params) {
+ if (!containsInList(params)) {
+ return new SqlWithParams(sql, new ArrayList<>(params));
Review Comment:
expand(...) allocates a new ArrayList and wraps it in a new SqlWithParams even when there are no InListParameter markers. Given that downstream code (logging + setParameters) doesn't mutate the params list, this creates avoidable per-call allocations on the hot path. Consider returning the original params list unchanged in the fast path (or only copying when you actually expand) to reduce overhead.
```suggestion
return new SqlWithParams(sql, params);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GROOVY-5436: add Sql.inList for collection expansion in SQL IN clauses [groovy]
Posted by "paulk-asert (via GitHub)" <gi...@apache.org>.
paulk-asert merged PR #2474:
URL: https://github.com/apache/groovy/pull/2474
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GROOVY-5436: add Sql.inList for collection expansion in SQL IN clauses [groovy]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2474:
URL: https://github.com/apache/groovy/pull/2474#issuecomment-4274758082
## [Codecov](https://app.codecov.io/gh/apache/groovy/pull/2474?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `75.00000%` with `25 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 67.1562%. Comparing base ([`4a57272`](https://app.codecov.io/gh/apache/groovy/commit/4a57272e43772bc4991712df921a49ff8eb0de36?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`d40a1b4`](https://app.codecov.io/gh/apache/groovy/commit/d40a1b4854e5261ed816e51a296b5d157fa8b603?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2474?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...y-sql/src/main/java/groovy/sql/InListExpander.java](https://app.codecov.io/gh/apache/groovy/pull/2474?src=pr&el=tree&filepath=subprojects%2Fgroovy-sql%2Fsrc%2Fmain%2Fjava%2Fgroovy%2Fsql%2FInListExpander.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VicHJvamVjdHMvZ3Jvb3Z5LXNxbC9zcmMvbWFpbi9qYXZhL2dyb292eS9zcWwvSW5MaXN0RXhwYW5kZXIuamF2YQ==) | 72.2222% | [14 Missing and 11 partials :warning: ](https://app.codecov.io/gh/apache/groovy/pull/2474?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/groovy/pull/2474?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## master #2474 +/- ##
==================================================
+ Coverage 67.1472% 67.1562% +0.0090%
- Complexity 30944 30964 +20
==================================================
Files 1438 1439 +1
Lines 120148 120245 +97
Branches 21311 21328 +17
==================================================
+ Hits 80676 80752 +76
- Misses 32724 32734 +10
- Partials 6748 6759 +11
```
| [Files with missing lines](https://app.codecov.io/gh/apache/groovy/pull/2474?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [...jects/groovy-sql/src/main/java/groovy/sql/Sql.java](https://app.codecov.io/gh/apache/groovy/pull/2474?src=pr&el=tree&filepath=subprojects%2Fgroovy-sql%2Fsrc%2Fmain%2Fjava%2Fgroovy%2Fsql%2FSql.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VicHJvamVjdHMvZ3Jvb3Z5LXNxbC9zcmMvbWFpbi9qYXZhL2dyb292eS9zcWwvU3FsLmphdmE=) | `65.7754% <100.0000%> (+0.2149%)` | :arrow_up: |
| [...y-sql/src/main/java/groovy/sql/InListExpander.java](https://app.codecov.io/gh/apache/groovy/pull/2474?src=pr&el=tree&filepath=subprojects%2Fgroovy-sql%2Fsrc%2Fmain%2Fjava%2Fgroovy%2Fsql%2FInListExpander.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VicHJvamVjdHMvZ3Jvb3Z5LXNxbC9zcmMvbWFpbi9qYXZhL2dyb292eS9zcWwvSW5MaXN0RXhwYW5kZXIuamF2YQ==) | `72.2222% <72.2222%> (ø)` | |
... and [6 files with indirect coverage changes](https://app.codecov.io/gh/apache/groovy/pull/2474/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@groovy.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org