From 8ac8c3fc924a306282f25f767377be00bf038c7e Mon Sep 17 00:00:00 2001 From: J-Bakuli Date: Mon, 16 Nov 2026 18:18:08 +0300 Subject: [PATCH] IGNITE-29296 Quoting of H2 schema and table identifiers --- .../internal/processors/query/h2/H2Utils.java | 12 ++- .../query/IgniteSqlSchemaIndexingTest.java | 83 +++++++++++++++++++ 2 files changed, 88 insertions(+), 7 deletions(-) diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java index 8541243ab982b..dedbc91fb3a4d 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/H2Utils.java @@ -82,6 +82,7 @@ import org.h2.result.SortOrder; import org.h2.table.IndexColumn; import org.h2.util.LocalDateTimeUtils; +import org.h2.util.StringUtils; import org.h2.value.CompareMode; import org.h2.value.DataType; import org.h2.value.Value; @@ -147,9 +148,6 @@ public class H2Utils { private static final String SPATIAL_IDX_FACTORY_CLS = "org.apache.ignite.internal.processors.query.h2.opt.GeoSpatialUtils"; - /** Quotation character. */ - private static final char ESC_CH = '\"'; - /** * @param c1 First column. * @param c2 Second column. @@ -300,13 +298,13 @@ public static boolean checkSpatialIndexEnabled() { } /** - * Add quotes around the name. + * Add quotes around the name. A quote inside the name is doubled so it stays inside that identifier. * - * @param str String. - * @return String with quotes. + * @param str Name. + * @return Quoted name. */ public static String withQuotes(String str) { - return ESC_CH + str + ESC_CH; + return StringUtils.quoteIdentifier(str); } /** diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java index 1678a1e8c3e5f..0eab87016da64 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/IgniteSqlSchemaIndexingTest.java @@ -17,6 +17,9 @@ package org.apache.ignite.internal.processors.query; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import javax.cache.CacheException; @@ -26,11 +29,13 @@ import org.apache.ignite.Ignition; import org.apache.ignite.cache.CacheAtomicityMode; import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cache.QueryEntity; import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.annotations.QuerySqlField; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.IgniteConfiguration; import org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest; +import org.apache.ignite.internal.processors.query.h2.H2PooledConnection; import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing; import org.apache.ignite.testframework.GridTestUtils; import org.junit.Ignore; @@ -194,6 +199,84 @@ public void testSchemaEscapeAll() throws Exception { ignite(0).destroyCache(cfgEsc.getName()); } + /** + * Tests that creating a cache with this schema name does not create SCHEMA_INJECTED, + * and a row is readable through that schema. + * + * @throws Exception If failed. + */ + @Test + public void testSchemaNameIsNotExecuted() throws Exception { + startGrid(0); + + String schemaName = "A\";CREATE TABLE PUBLIC.SCHEMA_INJECTED(ID INT);--"; + + String quotedSchema = "\"" + schemaName.replace("\"", "\"\"") + "\""; + + IgniteCache cache = ignite(0).createCache(cacheConfig("stmtSchema", true, Integer.class, Fact.class) + .setSqlSchema("\"" + schemaName + "\"")); + + assertH2TableAbsent("SCHEMA_INJECTED"); + + cache.put(1, new Fact(1, "v")); + + List> rows = cache.query(new SqlFieldsQuery( + "select f.name from " + quotedSchema + ".Fact f")).getAll(); + + assertEquals(1, rows.size()); + assertEquals("v", rows.get(0).get(0)); + } + + /** + * Tests that creating a cache with this table name does not create TABLE_INJECTED, + * and a row is readable through that table. + * + * @throws Exception If failed. + */ + @Test + public void testTableNameIsNotExecuted() throws Exception { + startGrid(0); + + String tblName = "A\";CREATE TABLE PUBLIC.TABLE_INJECTED(ID INT);--"; + + String quotedTbl = "\"" + tblName.replace("\"", "\"\"") + "\""; + + QueryEntity entity = new QueryEntity() + .setTableName(tblName) + .setKeyType(Integer.class.getName()) + .setValueFieldName("name") + .setValueType(String.class.getName()) + .addQueryField("name", String.class.getName(), null); + + IgniteCache cache = ignite(0).createCache(new CacheConfiguration("stmtTable") + .setQueryEntities(Collections.singletonList(entity))); + + assertH2TableAbsent("TABLE_INJECTED"); + + cache.put(1, "v"); + + List> rows = cache.query(new SqlFieldsQuery( + "select name from " + quotedTbl)).getAll(); + + assertEquals(1, rows.size()); + assertEquals("v", rows.get(0).get(0)); + } + + /** + * @param tblName Table name. + */ + private void assertH2TableAbsent(String tblName) throws Exception { + IgniteH2Indexing idx = (IgniteH2Indexing)ignite(0).context().query().getIndexing(); + + try (H2PooledConnection conn = idx.connections().connection()) { + try (Statement stmt = conn.connection().createStatement(); + ResultSet rs = stmt.executeQuery( + "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '" + tblName + "'")) { + assertFalse(rs.next()); + } + } + } + /** * Executes query with and without escaped schema name. * @param cache cache for querying