PHOENIX-4957 Add NO_INDEX hint so IndexTool uses the data table instead of 
index table


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/17a6ee9e
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/17a6ee9e
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/17a6ee9e

Branch: refs/heads/4.14-cdh5.14
Commit: 17a6ee9e1461f211a1738700b575bc72617091cc
Parents: 73060c9
Author: Vincent Poon <vincentp...@apache.org>
Authored: Tue Oct 9 00:38:09 2018 +0100
Committer: Pedro Boado <pbo...@apache.org>
Committed: Wed Oct 17 20:44:56 2018 +0100

----------------------------------------------------------------------
 .../phoenix/compile/PostIndexDDLCompiler.java   |  2 +-
 .../compile/PostIndexDDLCompilerTest.java       | 68 ++++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/17a6ee9e/phoenix-core/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
index b3cedf6..1bd3aed 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/compile/PostIndexDDLCompiler.java
@@ -104,7 +104,7 @@ public class PostIndexDDLCompiler {
         updateStmtStr.append("UPSERT /*+ NO_INDEX */ INTO 
").append(schemaName.length() == 0 ? "" : '"' + schemaName + 
"\".").append('"').append(tableName).append("\"(")
            .append(indexColumns).append(") ");
         final StringBuilder selectQueryBuilder = new StringBuilder();
-        selectQueryBuilder.append(" SELECT ").append(dataColumns).append(" 
FROM ")
+        selectQueryBuilder.append(" SELECT /*+ NO_INDEX */ 
").append(dataColumns).append(" FROM ")
         .append(schemaName.length() == 0 ? "" : '"' + schemaName + 
"\".").append('"').append(dataTable.getTableName().getString()).append('"');
         this.selectQuery = selectQueryBuilder.toString();
         updateStmtStr.append(this.selectQuery);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/17a6ee9e/phoenix-core/src/test/java/org/apache/phoenix/compile/PostIndexDDLCompilerTest.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/test/java/org/apache/phoenix/compile/PostIndexDDLCompilerTest.java
 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/PostIndexDDLCompilerTest.java
new file mode 100644
index 0000000..9df2fec
--- /dev/null
+++ 
b/phoenix-core/src/test/java/org/apache/phoenix/compile/PostIndexDDLCompilerTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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 org.apache.phoenix.compile;
+
+import static org.junit.Assert.assertEquals;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixStatement;
+import org.apache.phoenix.query.BaseConnectionlessQueryTest;
+import org.apache.phoenix.schema.PTable;
+import org.apache.phoenix.schema.PTableKey;
+import org.apache.phoenix.schema.TableRef;
+import org.junit.Test;
+
+public class PostIndexDDLCompilerTest extends BaseConnectionlessQueryTest {
+
+    @Test
+    public void testHintInSubquery() throws Exception {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            setupTables(conn);
+            PhoenixStatement stmt = 
conn.createStatement().unwrap(PhoenixStatement.class);
+            String query = "UPSERT /*+ NO_INDEX */ INTO T(k, v1) SELECT /*+ 
NO_INDEX */  k,v1 FROM T WHERE v1 = '4'";
+            MutationPlan plan = stmt.compileMutation(query);
+            assertEquals("T", 
plan.getQueryPlan().getTableRef().getTable().getTableName().getString());
+            query = "UPSERT INTO T(k, v1) SELECT /*+ NO_INDEX */  k,v1 FROM T 
WHERE v1 = '4'";
+            plan = stmt.compileMutation(query);
+            // TODO the following should actually use data table T if we 
supported hints in subqueries
+            assertEquals("IDX", 
plan.getQueryPlan().getTableRef().getTable().getTableName().getString());
+        }
+    }
+
+    @Test
+    public void testCompile() throws Exception {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            setupTables(conn);
+            PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
+            PTable pDataTable = pConn.getTable(new PTableKey(null, "T"));
+            PostIndexDDLCompiler compiler = new PostIndexDDLCompiler(pConn, 
new TableRef(pDataTable));
+            MutationPlan plan = compiler.compile(pConn.getTable(new 
PTableKey(null, "IDX")));
+            assertEquals("T", 
plan.getQueryPlan().getTableRef().getTable().getTableName().getString());
+        }
+    }
+
+    private void setupTables(Connection conn) throws SQLException {
+        conn.createStatement().execute("CREATE TABLE T (k VARCHAR NOT NULL 
PRIMARY KEY, v1 CHAR(15), v2 VARCHAR)");
+        conn.createStatement().execute("CREATE INDEX IDX ON T(v1, v2)");
+    }
+
+}

Reply via email to