You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by tl...@apache.org on 2020/12/15 13:38:32 UTC

[ignite] branch sql-calcite updated: IGNITE-13785 CREATE|DROP|ALTER table execute using h2 engine

This is an automated email from the ASF dual-hosted git repository.

tledkov pushed a commit to branch sql-calcite
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/sql-calcite by this push:
     new c44ef65  IGNITE-13785 CREATE|DROP|ALTER table execute using h2 engine
c44ef65 is described below

commit c44ef656555516793664f02c4782d2f800905a37
Author: zstan <st...@gmail.com>
AuthorDate: Tue Dec 15 16:38:13 2020 +0300

    IGNITE-13785 CREATE|DROP|ALTER table execute using h2 engine
---
 .../query/calcite/CalciteQueryProcessorTest.java   | 24 +++++++++++++
 .../query/calcite/jdbc/JdbcQueryTest.java          | 42 ++++++++++++----------
 .../org/apache/ignite/IgniteSystemProperties.java  |  5 +++
 .../processors/odbc/jdbc/JdbcRequestHandler.java   |  7 ++--
 .../processors/query/GridQueryProcessor.java       | 35 ++++++++++++++++++
 5 files changed, 90 insertions(+), 23 deletions(-)

diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
index 62f7490..922ef97 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessorTest.java
@@ -39,6 +39,8 @@ import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Ignore;
 import org.junit.Test;
 
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_EXPERIMENTAL_SQL_ENGINE;
+
 /**
  *
  */
@@ -362,6 +364,28 @@ public class CalciteQueryProcessorTest extends GridCommonAbstractTest {
         assertEquals(1, rows.size());
     }
 
+    /**
+     * Temporary redirects create|drop|alter commands into h2 engine.
+     */
+    @Test
+    @WithSystemProperty(key = IGNITE_EXPERIMENTAL_SQL_ENGINE, value = "true")
+    public void testUseH2Functionality() {
+        execute(grid(1), "CREATE TABLE IF NOT EXISTS Person(\"id\" INT, PRIMARY KEY(\"id\"), \"name\" VARCHAR)");
+
+        execute(grid(1), "alter table Person add column age int");
+        execute(grid(1),"drop table Person");
+    }
+
+    /**
+     * Execute SQL statement on given node.
+     *
+     * @param node Node.
+     * @param sql Statement.
+     */
+    protected List<List<?>> execute(IgniteEx node, String sql) {
+        return node.context().query().querySqlFields(new SqlFieldsQuery(sql).setSchema("PUBLIC"), true).getAll();
+    }
+
     /** */
     @Test
     public void aggregate() throws Exception {
diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java
index c755974..d9a9991 100644
--- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java
+++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcQueryTest.java
@@ -46,24 +46,6 @@ public class JdbcQueryTest extends GridCommonAbstractTest {
     /** Statement. */
     private Statement stmt;
 
-    /**
-     * @throws SQLException If failed.
-     */
-    @Test
-    public void testSimpleQuery() throws Exception {
-        stmt.execute("CREATE TABLE Person(\"id\" INT, PRIMARY KEY(\"id\"), \"name\" VARCHAR)");
-
-        grid(0).context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(3, 2)).get(10_000, TimeUnit.MILLISECONDS);
-
-        stmt.executeUpdate("INSERT INTO Person VALUES (10, 'Name')");
-        try (ResultSet rs = stmt.executeQuery("select p.*, (1+1) as synthetic from Person p")) {
-            assertTrue(rs.next());
-            assertEquals(10, rs.getInt(1));
-            assertEquals("Name", rs.getString(2));
-            assertEquals(2, rs.getInt(3));
-        }
-    }
-
     /** {@inheritDoc} */
     @Override protected void beforeTest() throws Exception {
         startGrids(nodesCnt);
@@ -88,4 +70,28 @@ public class JdbcQueryTest extends GridCommonAbstractTest {
         assert stmt.isClosed();
         assert conn.isClosed();
     }
+
+    /**
+     * @throws SQLException If failed.
+     */
+    @Test
+    public void testSimpleQuery() throws Exception {
+        stmt.execute("CREATE TABLE Person(\"id\" INT, PRIMARY KEY(\"id\"), \"name\" VARCHAR)");
+
+        grid(0).context().cache().context().exchange().affinityReadyFuture(new AffinityTopologyVersion(3, 2)).get(10_000, TimeUnit.MILLISECONDS);
+
+        stmt.executeUpdate("INSERT INTO Person VALUES (10, 'Name')");
+        try (ResultSet rs = stmt.executeQuery("select p.*, (1+1) as synthetic from Person p")) {
+            assertTrue(rs.next());
+            assertEquals(10, rs.getInt(1));
+            assertEquals("Name", rs.getString(2));
+            assertEquals(2, rs.getInt(3));
+        }
+
+        stmt.execute("alter table Person add column age int");
+
+        stmt.execute("drop table Person");
+
+        stmt.close();
+    }
 }
diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
index 9b029cd..fe1c847 100644
--- a/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
+++ b/modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
@@ -116,6 +116,7 @@ import static org.apache.ignite.internal.processors.failure.FailureProcessor.DFL
 import static org.apache.ignite.internal.processors.job.GridJobProcessor.DFLT_JOBS_HISTORY_SIZE;
 import static org.apache.ignite.internal.processors.jobmetrics.GridJobMetricsProcessor.DFLT_JOBS_METRICS_CONCURRENCY_LEVEL;
 import static org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageImpl.DFLT_MAX_HISTORY_BYTES;
+import static org.apache.ignite.internal.processors.query.GridQueryProcessor.DFLT_IGNITE_EXPERIMENTAL_SQL_ENGINE;
 import static org.apache.ignite.internal.processors.query.QueryUtils.DFLT_INDEXING_DISCOVERY_HISTORY_SIZE;
 import static org.apache.ignite.internal.processors.rest.GridRestProcessor.DFLT_SES_TIMEOUT;
 import static org.apache.ignite.internal.processors.rest.GridRestProcessor.DFLT_SES_TOKEN_INVALIDATE_INTERVAL;
@@ -1913,6 +1914,10 @@ public final class IgniteSystemProperties {
         defaults = "" + DFLT_DUMP_TX_COLLISIONS_INTERVAL)
     public static final String IGNITE_DUMP_TX_COLLISIONS_INTERVAL = "IGNITE_DUMP_TX_COLLISIONS_INTERVAL";
 
+    @SystemProperty(value = "Determines whether to use the experimental sql, calcite based, engine.",
+        defaults = "" + DFLT_IGNITE_EXPERIMENTAL_SQL_ENGINE)
+    public static final String IGNITE_EXPERIMENTAL_SQL_ENGINE = "IGNITE_EXPERIMENTAL_SQL_ENGINE";
+
     /**
      * Enforces singleton.
      */
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
index 394a6ff..e67e70c 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/odbc/jdbc/JdbcRequestHandler.java
@@ -115,6 +115,7 @@ import static org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequest.QRY_CL
 import static org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequest.QRY_EXEC;
 import static org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequest.QRY_FETCH;
 import static org.apache.ignite.internal.processors.odbc.jdbc.JdbcRequest.QRY_META;
+import static org.apache.ignite.internal.processors.query.GridQueryProcessor.H2_REDIRECTION_RULES;
 
 /**
  * JDBC request handler.
@@ -785,12 +786,8 @@ public class JdbcRequestHandler implements ClientListenerRequestHandler {
     /** */
     private List<FieldsQueryCursor<List<?>>> querySqlFields(SqlFieldsQueryEx qry, GridQueryCancel cancel) {
         if (experimentalQueryEngine != null) {
-            try {
+            if (!H2_REDIRECTION_RULES.matcher(qry.getSql()).find())
                 return experimentalQueryEngine.query(QueryContext.of(qry, cancel), qry.getSchema(), qry.getSql(), qry.getArgs());
-            }
-            catch (IgniteSQLException e) {
-                U.warn(log, "Failed to execute SQL query using experimental engine. [qry=" + qry + ']', e);
-            }
         }
 
         return connCtx.kernalContext().query().querySqlFields(null, qry,
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
index d00a710..8e7e0c8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java
@@ -34,6 +34,7 @@ import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.regex.Pattern;
 import javax.cache.Cache;
 import javax.cache.CacheException;
 import org.apache.ignite.IgniteCheckedException;
@@ -58,6 +59,7 @@ import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.configuration.NearCacheConfiguration;
 import org.apache.ignite.events.CacheQueryExecutedEvent;
+import org.apache.ignite.internal.GridComponent;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.IgniteInternalFuture;
 import org.apache.ignite.internal.NodeStoppingException;
@@ -113,6 +115,7 @@ import org.apache.ignite.internal.util.lang.IgniteOutClosureX;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.T2;
 import org.apache.ignite.internal.util.typedef.T3;
+import org.apache.ignite.internal.util.typedef.X;
 import org.apache.ignite.internal.util.typedef.internal.CU;
 import org.apache.ignite.internal.util.typedef.internal.LT;
 import org.apache.ignite.internal.util.typedef.internal.SB;
@@ -130,6 +133,9 @@ import org.jetbrains.annotations.Nullable;
 import static java.util.Collections.newSetFromMap;
 import static java.util.Objects.isNull;
 import static java.util.Objects.nonNull;
+import static java.util.regex.Pattern.CASE_INSENSITIVE;
+import static org.apache.ignite.IgniteSystemProperties.IGNITE_EXPERIMENTAL_SQL_ENGINE;
+import static org.apache.ignite.IgniteSystemProperties.getBoolean;
 import static org.apache.ignite.cache.CacheMode.PARTITIONED;
 import static org.apache.ignite.events.EventType.EVT_CACHE_QUERY_EXECUTED;
 import static org.apache.ignite.internal.GridTopic.TOPIC_SCHEMA;
@@ -232,6 +238,19 @@ public class GridQueryProcessor extends GridProcessorAdapter {
     /** Cache name - value typeId pairs for which type mismatch message was logged. */
     private final Set<Long> missedCacheTypes = ConcurrentHashMap.newKeySet();
 
+    /** Experimental calcite query engine. */
+    private QueryEngine experimentalQueryEngine;
+
+    /** h2 redirection stub. */
+    public static final Pattern H2_REDIRECTION_RULES =
+        Pattern.compile("\\s*(create\\s*table|drop\\s*table|alter\\s*table)", CASE_INSENSITIVE);
+
+    /** @see IgniteSystemProperties#IGNITE_EXPERIMENTAL_SQL_ENGINE */
+    public static final boolean DFLT_IGNITE_EXPERIMENTAL_SQL_ENGINE = false;
+
+    /** Use experimental engine flag. */
+    private final boolean useExperimentalSqlEngine;
+
     /**
      * @param ctx Kernal context.
      */
@@ -261,6 +280,17 @@ public class GridQueryProcessor extends GridProcessorAdapter {
                     U.warn(log, "Unsupported IO message: " + msg);
             }
         };
+
+        for (GridComponent cmp : ctx.components()) {
+            if (!(cmp instanceof QueryEngine))
+                continue;
+
+            experimentalQueryEngine = (QueryEngine) cmp;
+
+            break;
+        }
+
+        useExperimentalSqlEngine = getBoolean(IGNITE_EXPERIMENTAL_SQL_ENGINE);
     }
 
     /** {@inheritDoc} */
@@ -2762,6 +2792,11 @@ public class GridQueryProcessor extends GridProcessorAdapter {
         if (qry.isLocal() && ctx.clientNode() && (cctx == null || cctx.config().getCacheMode() != CacheMode.LOCAL))
             throw new CacheException("Execution of local SqlFieldsQuery on client node disallowed.");
 
+        if (experimentalQueryEngine != null && useExperimentalSqlEngine) {
+            if (!H2_REDIRECTION_RULES.matcher(qry.getSql()).find())
+                return experimentalQueryEngine.query(QueryContext.of(qry), qry.getSchema(), qry.getSql(), X.EMPTY_OBJECT_ARRAY);
+        }
+
         return executeQuerySafe(cctx, () -> {
             assert idx != null;