Repository: phoenix
Updated Branches:
  refs/heads/calcite 84d92bffe -> 1998fb7a0


PHOENIX-3657 Support ALTER INDEX in Phoenix-Calcite Integration(Rajeshbabu)


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

Branch: refs/heads/calcite
Commit: 1998fb7a0d8b1cbf9d9ed11b29d423a8dc1291d2
Parents: 84d92bf
Author: Rajeshbabu Chintaguntla <[email protected]>
Authored: Tue Feb 14 22:49:23 2017 +0530
Committer: Rajeshbabu Chintaguntla <[email protected]>
Committed: Tue Feb 14 22:49:23 2017 +0530

----------------------------------------------------------------------
 phoenix-core/src/main/codegen/data/Parser.tdd   |  3 +-
 .../src/main/codegen/includes/parserImpls.ftl   | 32 ++++++++++++
 .../phoenix/calcite/PhoenixPrepareImpl.java     | 32 +++++++++++-
 .../phoenix/calcite/parse/SqlAlterIndex.java    | 54 ++++++++++++++++++++
 .../phoenix/calcite/parse/SqlAlterTable.java    |  4 +-
 5 files changed, 122 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/1998fb7a/phoenix-core/src/main/codegen/data/Parser.tdd
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/codegen/data/Parser.tdd 
b/phoenix-core/src/main/codegen/data/Parser.tdd
index 9f253dc..fbb1098 100644
--- a/phoenix-core/src/main/codegen/data/Parser.tdd
+++ b/phoenix-core/src/main/codegen/data/Parser.tdd
@@ -79,7 +79,8 @@
     "SqlAlterTable()",
     "SqlCreateSchema()",
     "SqlDropSchema()",
-    "SqlUseSchema()"
+    "SqlUseSchema()",
+    "SqlAlterIndex()"
   ]
 
   # List of methods for parsing custom literals.

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1998fb7a/phoenix-core/src/main/codegen/includes/parserImpls.ftl
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/codegen/includes/parserImpls.ftl 
b/phoenix-core/src/main/codegen/includes/parserImpls.ftl
index 0d6a7b7..defed7d 100644
--- a/phoenix-core/src/main/codegen/includes/parserImpls.ftl
+++ b/phoenix-core/src/main/codegen/includes/parserImpls.ftl
@@ -328,6 +328,38 @@ SqlNode SqlCreateIndex() :
 
 /**
  * Parses statement
+ *   ALTER INDEX
+ */
+SqlNode SqlAlterIndex() :
+{
+    SqlParserPos pos;
+    SqlIdentifier indexName;
+    SqlIdentifier dataTableName;
+    boolean ifExists = false;
+    SqlIdentifier indexState;
+    boolean async = false;
+}
+{
+    <ALTER> { pos = getPos(); } <INDEX>
+    [
+        <IF> <EXISTS> { ifExists = true; }
+    ]
+    indexName = SimpleIdentifier()
+    <ON> dataTableName = DualIdentifier()
+    indexState = SimpleIdentifier()
+    (
+        <ASYNC> {async = true;}
+    )?
+    {
+        return new SqlAlterIndex(pos.plus(getPos()), indexName,
+            dataTableName, indexState,
+            SqlLiteral.createBoolean(ifExists, SqlParserPos.ZERO),
+            SqlLiteral.createBoolean(async, SqlParserPos.ZERO));
+    }
+}
+
+/**
+ * Parses statement
  *   CREATE SEQUENCE
  */
 SqlNode SqlCreateSequence() :

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1998fb7a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
index 5b9ea60..2daa44e 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/PhoenixPrepareImpl.java
@@ -38,6 +38,7 @@ import org.apache.calcite.tools.Program;
 import org.apache.calcite.util.Holder;
 import org.apache.calcite.util.NlsString;
 import org.apache.hadoop.hbase.util.Pair;
+import org.apache.phoenix.calcite.parse.SqlAlterIndex;
 import org.apache.phoenix.calcite.parse.SqlAlterTable;
 import org.apache.phoenix.calcite.parse.SqlCreateFunction;
 import org.apache.phoenix.calcite.parse.SqlCreateIndex;
@@ -67,6 +68,7 @@ import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixStatement;
 import org.apache.phoenix.jdbc.PhoenixStatement.Operation;
 import org.apache.phoenix.parse.AddColumnStatement;
+import org.apache.phoenix.parse.AlterIndexStatement;
 import org.apache.phoenix.parse.ColumnDef;
 import org.apache.phoenix.parse.ColumnDefInPkConstraint;
 import org.apache.phoenix.parse.ColumnName;
@@ -97,6 +99,7 @@ import org.apache.phoenix.parse.UpdateStatisticsStatement;
 import org.apache.phoenix.parse.UseSchemaStatement;
 import org.apache.phoenix.query.QueryConstants;
 import org.apache.phoenix.schema.MetaDataClient;
+import org.apache.phoenix.schema.PIndexState;
 import org.apache.phoenix.schema.PTable.IndexType;
 import org.apache.phoenix.schema.PTableType;
 import org.apache.phoenix.schema.Sequence;
@@ -426,7 +429,8 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl {
                 client.dropSequence(drop);
                 break;                
             }
-            case ALTER_TABLE: {
+            case ALTER_TABLE: 
+            case ALTER_VIEW: {
                 final SqlAlterTable alterTable = (SqlAlterTable) node;
                 final TableName name;
                 if (alterTable.tableName.isSimple()) {
@@ -486,6 +490,32 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl 
{
                 }
                 break;
             }
+            case ALTER_INDEX: {
+                final SqlAlterIndex index = (SqlAlterIndex) node;
+                NamedTableNode namedTable =
+                        nodeFactory.namedTable(null, 
TableName.create(!index.dataTableName
+                                .isSimple() ? index.dataTableName.names.get(0) 
: null,
+                            index.indexName.getSimple()));
+                final String dataTableName;
+                if (index.dataTableName.isSimple()) {
+                    dataTableName = index.dataTableName.getSimple();
+                } else {
+                    dataTableName = index.dataTableName.names.get(1);
+                }
+                String indexState = index.indexState.names.get(0);
+                PIndexState state = null;
+                try {
+                    state = PIndexState.valueOf(indexState.toUpperCase());
+                } catch(IllegalArgumentException e) {
+                    throw new SQLException(indexState+" is not a valid index 
state.");
+                }
+                boolean ifExists = index.ifExists.booleanValue();
+                boolean async = index.async.booleanValue();
+                AlterIndexStatement alterIndex = new 
AlterIndexStatement(namedTable, dataTableName, ifExists, state, async);
+                MetaDataClient client = new MetaDataClient(connection);
+                client.alterIndex(alterIndex);
+                break;
+            }
             case OTHER_DDL: {
                 if (node instanceof SqlUpdateStatistics) {
                     SqlUpdateStatistics updateStatsNode = 
(SqlUpdateStatistics) node;

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1998fb7a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterIndex.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterIndex.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterIndex.java
new file mode 100644
index 0000000..3a73846
--- /dev/null
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterIndex.java
@@ -0,0 +1,54 @@
+package org.apache.phoenix.calcite.parse;
+
+import java.util.List;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.parser.SqlParserPos;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * Parse tree node for SQL {@code ALTER INDEX} command.
+ */
+public class SqlAlterIndex extends SqlCall {
+    public final SqlOperator operator;
+    public final SqlIdentifier indexName;
+    public final SqlIdentifier dataTableName;
+    public final SqlLiteral ifExists;
+    public final SqlIdentifier indexState;
+    public final SqlLiteral async;
+
+    /** Creates a ALTER TABLE. */
+    public SqlAlterIndex(
+            SqlParserPos pos,
+            SqlIdentifier indexName,
+            SqlIdentifier dataTableName,
+            SqlIdentifier indexState,
+            SqlLiteral ifExists,
+            SqlLiteral async) {
+        super(pos);
+        this.operator = new SqlDdlOperator("ALTER INDEX", SqlKind.ALTER_INDEX);
+        this.indexName = indexName;
+        this.dataTableName = dataTableName;
+        this.ifExists = ifExists;
+        this.indexState = indexState;
+        this.async = async;
+    }
+
+
+    @Override
+    public SqlOperator getOperator() {
+        return this.operator;
+    }
+
+    @Override
+    public List<SqlNode> getOperandList() {
+        return ImmutableList.of(indexName, dataTableName, ifExists, 
indexState, async);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/1998fb7a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterTable.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterTable.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterTable.java
index 5dde16f..00537ac 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterTable.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlAlterTable.java
@@ -37,7 +37,9 @@ public class SqlAlterTable extends SqlCall {
             SqlNodeList newColumnDefs,
             SqlNodeList tableOptions) {
         super(pos);
-        this.operator = new SqlDdlOperator("ALTER TABLE", SqlKind.ALTER_TABLE);
+        this.operator =
+                isView.booleanValue() ? new SqlDdlOperator("ALTER VIEW", 
SqlKind.ALTER_VIEW)
+                        : new SqlDdlOperator("ALTER TABLE", 
SqlKind.ALTER_TABLE);
         this.tableName = tableName;
         this.isView = isView;
         this.ifExists = ifExists;

Reply via email to