You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2020/09/16 21:25:04 UTC

[GitHub] [phoenix] gjacoby126 commented on a change in pull request #864: PHOENIX-5261: Implement ALTER TABLE ADD COLUMN CASCADE

gjacoby126 commented on a change in pull request #864:
URL: https://github.com/apache/phoenix/pull/864#discussion_r489752459



##########
File path: phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
##########
@@ -3629,14 +3632,25 @@ private void mutateStringProperty(String tenantId, String schemaName, String tab
 
     public MutationState addColumn(AddColumnStatement statement) throws SQLException {
         PTable table = FromCompiler.getResolver(statement, connection).getTables().get(0).getTable();
-        return addColumn(table, statement.getColumnDefs(), statement.getProps(), statement.ifNotExists(), false, statement.getTable(), statement.getTableType());
+        return addColumn(table, statement.getColumnDefs(), statement.getProps(), statement.ifNotExists(), false, statement.getTable(), statement.getTableType(), statement.isCascade(), statement.getIndexes());
     }
 
     public MutationState addColumn(PTable table, List<ColumnDef> origColumnDefs,
             ListMultimap<String, Pair<String, Object>> stmtProperties, boolean ifNotExists,
-            boolean removeTableProps, NamedTableNode namedTableNode, PTableType tableType)
+            boolean removeTableProps, NamedTableNode namedTableNode, PTableType tableType, boolean cascade, List<NamedNode> indexes)
                     throws SQLException {
         connection.rollback();
+        List<PTable> indexesPTable = Lists.newArrayListWithExpectedSize(indexes != null ?
+                indexes.size() : table.getIndexes().size());
+        // if cascade keyword is passed and indexes are provided either implicitly or explicitly
+        if (cascade && (indexes == null || !indexes.isEmpty())) {
+            boolean isView = table.getType().equals(PTableType.VIEW);
+            indexesPTable = indexes == null ?
+                    table.getIndexes(isView) : table.getIndexes(indexes);
+            if (indexesPTable == null) {
+                throw new RuntimeException(INCORRECT_INDEX_NAME_S);

Review comment:
       Shouldn't this be a SQLException with its own SQLExceptionCode?

##########
File path: phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterAddCascadeIndexIT.java
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.end2end;
+
+import org.apache.phoenix.schema.PTableType;
+import org.apache.phoenix.schema.types.PDecimal;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.PFloat;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.util.ColumnInfo;
+import org.apache.phoenix.util.SchemaUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Properties;
+
+import static org.apache.phoenix.schema.MetaDataClient.INCORRECT_INDEX_NAME_S;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+public class AlterAddCascadeIndexIT extends ParallelStatsDisabledIT {
+
+    public static final String SYNTAX_ERROR = "Syntax error";
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+    private static Connection conn;
+    private Properties prop;
+    private boolean isViewIndex;
+    private String phoenixObjectName;
+    private String indexesName;
+    private final String tableDDLOptions;
+
+
+    public AlterAddCascadeIndexIT(boolean isViewIndex, boolean mutable) {
+        this.isViewIndex = isViewIndex;
+        StringBuilder optionBuilder = new StringBuilder();
+        if (!mutable) {
+            optionBuilder.append(" IMMUTABLE_ROWS=true");
+        }
+        this.tableDDLOptions = optionBuilder.toString();
+    }
+
+    @Parameters(name="AlterAddCascadeIndexIT_isViewIndex={0},mutable={1}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                { true, true},
+                { true, false},
+                { false, true},
+                { false, false},
+        });
+    }
+
+    @Before
+    public void setup() throws SQLException {
+        prop = new Properties();

Review comment:
       Please create new tables for each test rather than dropping identical tables after each test. Dropping tables is really slow. See PHOENIX-5942.

##########
File path: phoenix-core/src/main/java/org/apache/phoenix/schema/PTableImpl.java
##########
@@ -1524,6 +1525,40 @@ public Integer getBucketNum() {
         return indexes;
     }
 
+    @Override
+    public List<PTable> getIndexes(boolean viewOnly) {
+        List<PTable> indexes = new ArrayList<>();
+        indexes.addAll(this.indexes);
+        if (viewOnly) {
+            for (PTable index: this.indexes) {
+                if(index.getTableName().toString().contains("#")) {
+                    indexes.remove(index);
+                }
+            }
+        }
+        return indexes;
+    }
+
+    @Override
+    public List<PTable> getIndexes(List<NamedNode> indexes) {
+        List<PTable> indexesPTable = Lists.newArrayListWithExpectedSize(indexes.size());
+        List<String> indexesParam = Lists.newArrayListWithExpectedSize(indexes.size());
+        for (NamedNode index : indexes) {
+            indexesParam.add(index.getName());
+        }
+        for (PTable index : this.indexes) {
+            if(indexesParam.contains(index.getTableName().getString())) {

Review comment:
       nit: List.remove() returns the item if the item was present, so a contains() followed by a remove() is redundant because remove() itself can be used as a contains(). 

##########
File path: phoenix-core/src/it/java/org/apache/phoenix/end2end/AlterAddCascadeIndexIT.java
##########
@@ -0,0 +1,335 @@
+/*
+ * 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.end2end;
+
+import org.apache.phoenix.schema.PTableType;
+import org.apache.phoenix.schema.types.PDecimal;
+import org.apache.phoenix.schema.types.PDouble;
+import org.apache.phoenix.schema.types.PFloat;
+import org.apache.phoenix.schema.types.PVarchar;
+import org.apache.phoenix.util.ColumnInfo;
+import org.apache.phoenix.util.SchemaUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Properties;
+
+import static org.apache.phoenix.schema.MetaDataClient.INCORRECT_INDEX_NAME_S;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+public class AlterAddCascadeIndexIT extends ParallelStatsDisabledIT {
+
+    public static final String SYNTAX_ERROR = "Syntax error";
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+    private static Connection conn;
+    private Properties prop;
+    private boolean isViewIndex;
+    private String phoenixObjectName;
+    private String indexesName;
+    private final String tableDDLOptions;
+
+
+    public AlterAddCascadeIndexIT(boolean isViewIndex, boolean mutable) {
+        this.isViewIndex = isViewIndex;
+        StringBuilder optionBuilder = new StringBuilder();
+        if (!mutable) {
+            optionBuilder.append(" IMMUTABLE_ROWS=true");
+        }
+        this.tableDDLOptions = optionBuilder.toString();
+    }
+
+    @Parameters(name="AlterAddCascadeIndexIT_isViewIndex={0},mutable={1}")
+    public static Collection<Object[]> data() {
+        return Arrays.asList(new Object[][] {
+                { true, true},
+                { true, false},
+                { false, true},
+                { false, false},
+        });
+    }
+
+    @Before
+    public void setup() throws SQLException {
+        prop = new Properties();
+        conn = DriverManager.getConnection(getUrl(), prop);
+        conn.setAutoCommit(true);
+        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS test.us_population (\n" +
+                "      state CHAR(2) NOT NULL,\n" +
+                "      city VARCHAR NOT NULL,\n" +
+                "      population BIGINT,\n" +
+                "      CONSTRAINT my_pk PRIMARY KEY (state, city)) " + tableDDLOptions);
+
+        if(isViewIndex) {
+            conn.createStatement().execute("CREATE VIEW IF NOT EXISTS test.us_population_gv" +
+                    "(city_area INTEGER, avg_fam_size INTEGER) AS " +
+                    "SELECT * FROM test.us_population WHERE state = 'CA'");
+
+            conn.createStatement().execute("CREATE INDEX IF NOT EXISTS us_population_gv_gi ON " +
+                    "test.us_population_gv (city_area) INCLUDE (population)");
+            conn.createStatement().execute("CREATE INDEX IF NOT EXISTS us_population_gv_gi_2 ON " +

Review comment:
       Do we need a local index too?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org