This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-28131 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5baee3afc4517c74d77a2f06bcf02a95ea50af3e Author: Claus Ibsen <[email protected]> AuthorDate: Sun Dec 6 11:16:38 2026 +0100 CAMEL-28131: camel-duckdb - a quoted table or column name must be one quoted identifier The insert and copy operations put table and column names in double quotes. A name that already began with a double quote was used as it was, without a check that it is one quoted identifier. Such a name must now be one complete quoted identifier, with every inner double quote doubled, otherwise the operation fails with a DuckDbException before a statement is run. A qualified table name is split at the dots outside of quotes, so a quoted segment can contain a dot, and an empty segment fails. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../camel/catalog/docs/duckdb-component.adoc | 6 ++ .../src/main/docs/duckdb-component.adoc | 6 ++ .../camel/component/duckdb/DuckDbJdbcSupport.java | 79 ++++++++++++++++++++-- .../component/duckdb/DuckDbJdbcSupportTest.java | 60 ++++++++++++++++ .../camel/component/duckdb/DuckDbProducerTest.java | 50 ++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_22.adoc | 10 +++ .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc | 10 +++ 7 files changed, 214 insertions(+), 7 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/duckdb-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/duckdb-component.adoc index f0a4179fdf1f..8602bdb7c5b0 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/duckdb-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/duckdb-component.adoc @@ -60,6 +60,12 @@ keys as the first row, otherwise the insert fails with a `DuckDbException` * `copy` — bulk load one local file, from a `File`, a `Path` or a path as `String` (or bytes), using DuckDB `read_csv` / `read_parquet` / `read_json` * `ping` — `SELECT 1` connectivity check; sets `CamelDuckDbPingOk` +The keys of the `Map` rows are the column names of the `insert`. Camel puts each table and column name in double +quotes, and doubles a double quote inside the name, so a reserved word or any other text is used as one name. A name +that is already in double quotes, such as `"MixedCase"` or `main."Events"`, is used as it is. It must be one complete +quoted identifier (with every inner double quote doubled), otherwise the operation fails with a `DuckDbException` and +no statement is run. A dot inside a quoted segment of a table name, as in `"my.schema"."events"`, is part of the name. + The `CamelDuckDbOperation` header takes the operation name (case-insensitive); an unknown value fails with an error that lists the valid operations. diff --git a/components/camel-duckdb/src/main/docs/duckdb-component.adoc b/components/camel-duckdb/src/main/docs/duckdb-component.adoc index f0a4179fdf1f..8602bdb7c5b0 100644 --- a/components/camel-duckdb/src/main/docs/duckdb-component.adoc +++ b/components/camel-duckdb/src/main/docs/duckdb-component.adoc @@ -60,6 +60,12 @@ keys as the first row, otherwise the insert fails with a `DuckDbException` * `copy` — bulk load one local file, from a `File`, a `Path` or a path as `String` (or bytes), using DuckDB `read_csv` / `read_parquet` / `read_json` * `ping` — `SELECT 1` connectivity check; sets `CamelDuckDbPingOk` +The keys of the `Map` rows are the column names of the `insert`. Camel puts each table and column name in double +quotes, and doubles a double quote inside the name, so a reserved word or any other text is used as one name. A name +that is already in double quotes, such as `"MixedCase"` or `main."Events"`, is used as it is. It must be one complete +quoted identifier (with every inner double quote doubled), otherwise the operation fails with a `DuckDbException` and +no statement is run. A dot inside a quoted segment of a table name, as in `"my.schema"."events"`, is part of the name. + The `CamelDuckDbOperation` header takes the operation name (case-insensitive); an unknown value fails with an error that lists the valid operations. diff --git a/components/camel-duckdb/src/main/java/org/apache/camel/component/duckdb/DuckDbJdbcSupport.java b/components/camel-duckdb/src/main/java/org/apache/camel/component/duckdb/DuckDbJdbcSupport.java index ed1735db158c..f3f5038b1393 100644 --- a/components/camel-duckdb/src/main/java/org/apache/camel/component/duckdb/DuckDbJdbcSupport.java +++ b/components/camel-duckdb/src/main/java/org/apache/camel/component/duckdb/DuckDbJdbcSupport.java @@ -49,11 +49,17 @@ final class DuckDbJdbcSupport { } /** - * Quotes a single SQL identifier such as a column name. + * Quotes a single SQL identifier such as a column name. A name that begins with a double quote is used as it is, + * and must be one quoted identifier, with every inner double quote doubled. */ static String quoteIdentifier(String identifier) { String trimmed = identifier.trim(); if (trimmed.startsWith("\"")) { + if (!isQuotedIdentifier(trimmed)) { + throw new DuckDbException( + "Invalid identifier: " + identifier + ". A name that begins with a double quote must be one" + + " quoted identifier, with every inner double quote doubled."); + } return trimmed; } return '"' + trimmed.replace("\"", "\"\"") + '"'; @@ -61,20 +67,79 @@ final class DuckDbJdbcSupport { /** * Quotes a possibly qualified SQL identifier such as {@code schema.table}, quoting each segment on its own so the - * qualification is preserved. + * qualification is preserved. A segment in double quotes can contain dots, such as {@code "my.schema"."events"}. */ static String quoteQualifiedIdentifier(String identifier) { String trimmed = identifier.trim(); - if (trimmed.startsWith("\"")) { - return trimmed; - } StringBuilder sb = new StringBuilder(); - for (String segment : trimmed.split("\\.", -1)) { + int start = 0; + while (true) { + int end = segmentEnd(trimmed, start); + String segment = trimmed.substring(start, end); + if (segment.isBlank()) { + throw new DuckDbException("Invalid identifier: " + identifier + ". A qualified name has an empty segment."); + } if (!sb.isEmpty()) { sb.append('.'); } sb.append(quoteIdentifier(segment)); + if (end == trimmed.length()) { + return sb.toString(); + } + start = end + 1; + } + } + + /** + * Returns the index of the dot that ends the segment that begins at {@code start}, or the length of the text. A dot + * inside a quoted segment does not end the segment. + */ + private static int segmentEnd(String text, int start) { + int i = start; + while (i < text.length() && Character.isWhitespace(text.charAt(i))) { + i++; + } + if (i < text.length() && text.charAt(i) == '"') { + i++; + while (i < text.length()) { + if (text.charAt(i) == '"') { + if (i + 1 < text.length() && text.charAt(i + 1) == '"') { + // a doubled quote is part of the name + i++; + } else { + // the closing quote + i++; + break; + } + } + i++; + } + } + int dot = text.indexOf('.', i); + return dot < 0 ? text.length() : dot; + } + + /** + * Whether the text is one quoted identifier: it starts and ends with a double quote, and every double quote in + * between is doubled. + */ + private static boolean isQuotedIdentifier(String text) { + int last = text.length() - 1; + if (last < 1 || text.charAt(0) != '"' || text.charAt(last) != '"') { + return false; + } + int i = 1; + while (i < last) { + if (text.charAt(i) == '"') { + if (i + 1 < last && text.charAt(i + 1) == '"') { + // a doubled quote is part of the name + i += 2; + continue; + } + return false; + } + i++; } - return sb.toString(); + return true; } } diff --git a/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbJdbcSupportTest.java b/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbJdbcSupportTest.java index 07305c2bceb6..8c3791e460c7 100644 --- a/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbJdbcSupportTest.java +++ b/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbJdbcSupportTest.java @@ -20,6 +20,7 @@ import org.duckdb.DuckDBDriver; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; class DuckDbJdbcSupportTest { @@ -65,4 +66,63 @@ class DuckDbJdbcSupportTest { assertThat(DuckDbJdbcSupport.quoteIdentifier("\"MixedCase\"")).isEqualTo("\"MixedCase\""); assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("\"main\".\"events\"")).isEqualTo("\"main\".\"events\""); } + + @Test + void keepsQuotedIdentifierWithDoubledInnerQuotes() { + assertThat(DuckDbJdbcSupport.quoteIdentifier("\"we\"\"ird\"")).isEqualTo("\"we\"\"ird\""); + assertThat(DuckDbJdbcSupport.quoteIdentifier(" \"end\"\"\" ")).isEqualTo("\"end\"\"\""); + } + + @Test + void rejectsQuotedIdentifierWithTextAfterClosingQuote() { + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"a\"x")) + .isInstanceOf(DuckDbException.class) + .hasMessageContaining("\"a\"x") + .hasMessageContaining("one quoted identifier"); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"a\" b")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"a\"\"b\"c\"")) + .isInstanceOf(DuckDbException.class); + } + + @Test + void rejectsUnterminatedQuotedIdentifier() { + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"abc")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"a\"\"")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteIdentifier("\"")) + .isInstanceOf(DuckDbException.class); + } + + @Test + void quotesQualifiedIdentifierWithQuotedAndPlainSegments() { + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("main.\"Events\"")).isEqualTo("\"main\".\"Events\""); + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("\"Main\".events")).isEqualTo("\"Main\".\"events\""); + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("db.\"Main\".events")) + .isEqualTo("\"db\".\"Main\".\"events\""); + } + + @Test + void keepsDotInsideQuotedSegment() { + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("\"my.schema\".\"t\"")).isEqualTo("\"my.schema\".\"t\""); + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("\"my.schema\".t")).isEqualTo("\"my.schema\".\"t\""); + assertThat(DuckDbJdbcSupport.quoteQualifiedIdentifier("\"a\"\".b\"")).isEqualTo("\"a\"\".b\""); + } + + @Test + void rejectsQualifiedIdentifierWithMalformedSegment() { + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteQualifiedIdentifier("\"main\"x.events")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteQualifiedIdentifier("main.\"events\"x")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteQualifiedIdentifier("\"main.events")) + .isInstanceOf(DuckDbException.class); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteQualifiedIdentifier("main..events")) + .isInstanceOf(DuckDbException.class) + .hasMessageContaining("empty segment"); + assertThatThrownBy(() -> DuckDbJdbcSupport.quoteQualifiedIdentifier("main.")) + .isInstanceOf(DuckDbException.class) + .hasMessageContaining("empty segment"); + } } diff --git a/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbProducerTest.java b/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbProducerTest.java index 76d5570ce489..60fc2becf5c3 100644 --- a/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbProducerTest.java +++ b/components/camel-duckdb/src/test/java/org/apache/camel/component/duckdb/DuckDbProducerTest.java @@ -183,6 +183,56 @@ class DuckDbProducerTest extends CamelTestSupport { assertThat(rows).extracting(row -> row.get("group")).containsExactly("cli"); } + @Test + void insertKeepsQuotedMixedCaseColumnName(@TempDir Path tempDir) throws Exception { + String uri = jdbcEndpoint(tempDir.resolve("test.db")); + template.sendBody(uri + "&operation=execute", "CREATE TABLE events (id INTEGER, \"Name\" VARCHAR)"); + template.sendBody(uri + "&operation=insert&table=events", List.of(Map.of("id", 1, "\"Name\"", "ada"))); + + @SuppressWarnings("unchecked") + List<Map<String, Object>> rows + = template.requestBody(uri + "&operation=query", "SELECT \"Name\" FROM events", List.class); + assertThat(rows).extracting(row -> row.get("Name")).containsExactly("ada"); + } + + @Test + void insertFailsWhenColumnNameIsNotOneQuotedIdentifier(@TempDir Path tempDir) throws Exception { + String uri = jdbcEndpoint(tempDir.resolve("test.db")); + template.sendBody(uri + "&operation=execute", "CREATE TABLE events (id INTEGER, name VARCHAR)"); + + assertThatThrownBy(() -> template.sendBody(uri + "&operation=insert&table=events", + List.of(Map.of("id", 1, "\"name\"x", "ada")))) + .isInstanceOf(CamelExecutionException.class) + .hasRootCauseInstanceOf(DuckDbException.class) + .rootCause().hasMessageContaining("\"name\"x") + .hasMessageContaining("one quoted identifier"); + + @SuppressWarnings("unchecked") + List<Map<String, Object>> rows + = template.requestBody(uri + "&operation=query", "SELECT count(*) AS total FROM events", List.class); + assertThat(rows).extracting(row -> row.get("total")).containsExactly(0L); + } + + @Test + void copyFailsWhenTableHeaderIsNotOneQuotedIdentifier(@TempDir Path tempDir) throws Exception { + String uri = jdbcEndpoint(tempDir.resolve("test.db")); + template.sendBody(uri + "&operation=execute", "CREATE TABLE events (id INTEGER, name VARCHAR)"); + + Path csv = tempDir.resolve("data.csv"); + Files.writeString(csv, "id,name\n2,bob\n"); + + assertThatThrownBy(() -> template.sendBodyAndHeader(uri + "&operation=copy&table=events&format=csv", csv.toFile(), + DuckDbConstants.TABLE, "\"events\"x")) + .isInstanceOf(CamelExecutionException.class) + .hasRootCauseInstanceOf(DuckDbException.class) + .rootCause().hasMessageContaining("one quoted identifier"); + + @SuppressWarnings("unchecked") + List<Map<String, Object>> rows + = template.requestBody(uri + "&operation=query", "SELECT count(*) AS total FROM events", List.class); + assertThat(rows).extracting(row -> row.get("total")).containsExactly(0L); + } + @Test void readOnlyPreventsWrites(@TempDir Path tempDir) throws Exception { Path dbFile = tempDir.resolve("readonly.db"); diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc index 30df8a66cc56..88b37c7a0463 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_22.adoc @@ -157,6 +157,16 @@ A route that connects to a local server that does not use TLS, such as Pinecone explicitly. A route that connects to an index in the Pinecone cloud needs no change. The control plane (REST) is unaffected: it still uses the scheme of the `host` option. +=== camel-duckdb - a quoted table or column name must be one quoted identifier + +The `insert` and `copy` operations of the `duckdb` producer put table and column names in double quotes. A name that +already started with a double quote was used as it was. Now such a name must be one complete quoted identifier: it +ends with the closing double quote, and every double quote inside it is doubled (`"we""ird"`). A name with text after +the closing quote, or without a closing quote, fails with a `DuckDbException` before a statement is run. A qualified +table name is split at the dots outside of quotes, so `main."Events"` and `"my.schema"."events"` work, and a +qualified name with an empty segment (such as `main.`) fails. Names that do not start with a double quote are quoted +as before. + === camel-main - an explicit useGlobalSslContextParameters=false is honoured When global SSL is enabled (`camel.ssl.enabled=true`), the HTTP server and the HTTP management server use it diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc index 7b5932c756cc..5401d1fdf366 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc @@ -680,6 +680,16 @@ every element must be a `Map`, and a row whose keys differ from the keys of the `DuckDbException` that names the row. A route that inserts rows with different keys must give every row the same keys (with `null` for a missing value). +=== camel-duckdb - a quoted table or column name must be one quoted identifier + +The `insert` and `copy` operations of the `duckdb` producer put table and column names in double quotes. A name that +already started with a double quote was used as it was. Now such a name must be one complete quoted identifier: it +ends with the closing double quote, and every double quote inside it is doubled (`"we""ird"`). A name with text after +the closing quote, or without a closing quote, fails with a `DuckDbException` before a statement is run. A qualified +table name is split at the dots outside of quotes, so `main."Events"` and `"my.schema"."events"` work, and a +qualified name with an empty segment (such as `main.`) fails. Names that do not start with a double quote are quoted +as before. + === camel-resilience4j - the config option is used The `config` option of the Resilience4j EIP configuration (and `camel.resilience4j.config`) refers to a
