Repository: phoenix Updated Branches: refs/heads/calcite ba8c82430 -> e7ac8cbbe
Merge CREATE VIEW commits by Julian Hyde. Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/e7ac8cbb Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/e7ac8cbb Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/e7ac8cbb Branch: refs/heads/calcite Commit: e7ac8cbbede5729453649da21616fad86080791a Parents: ba8c824 Author: maryannxue <wei....@intel.com> Authored: Thu Sep 3 23:33:26 2015 -0400 Committer: maryannxue <wei....@intel.com> Committed: Thu Sep 3 23:33:26 2015 -0400 ---------------------------------------------------------------------- .../org/apache/phoenix/calcite/CalciteIT.java | 4 ++ phoenix-core/src/main/codegen/data/Parser.tdd | 3 +- .../src/main/codegen/includes/parserImpls.ftl | 30 +++++++++ .../calcite/jdbc/PhoenixPrepareImpl.java | 23 +++++++ .../phoenix/calcite/parse/SqlCreateView.java | 64 ++++++++++++++++++++ .../phoenix/calcite/parse/SqlDdlOperator.java | 20 ++++++ 6 files changed, 143 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java index 6b4c574..ab9aaf1 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/calcite/CalciteIT.java @@ -1070,6 +1070,10 @@ public class CalciteIT extends BaseClientManagedTimeIT { start().sql("commit").execute(); } + @Test public void testCreateView() { + start().sql("create view v as select * from (values (1, 'a'), (2, 'b')) as t(x, y)").execute(); + } + @Test public void testConnectJoinHsqldb() { final Start start = new Start(new Properties(), false) { @Override http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/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 90d592f..9406eda 100644 --- a/phoenix-core/src/main/codegen/data/Parser.tdd +++ b/phoenix-core/src/main/codegen/data/Parser.tdd @@ -31,7 +31,8 @@ # List of methods for parsing custom SQL statements. statementParserMethods: [ - "SqlCommit()" + "SqlCommit()", + "SqlCreateView()" ] # List of methods for parsing custom literals. http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/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 42aa48c..bc1640e 100644 --- a/phoenix-core/src/main/codegen/includes/parserImpls.ftl +++ b/phoenix-core/src/main/codegen/includes/parserImpls.ftl @@ -34,3 +34,33 @@ SqlNode SqlCommit() : return new SqlCommit(pos); } } + +// Remove when +// [CALCITE-851] Add original SQL string as a field in the parser +// is fixed. +JAVACODE +public String originalSql() { + return org.apache.phoenix.calcite.jdbc.PhoenixPrepareImpl.THREAD_SQL_STRING.get(); +} + +SqlNode SqlCreateView() : +{ + SqlParserPos pos; + SqlIdentifier name; + SqlNode query; +} +{ + <CREATE> { pos = getPos(); } <VIEW> name = CompoundIdentifier() + <AS> + query = OrderedQueryOrExpr(ExprContext.ACCEPT_QUERY) + { + String sql = originalSql(); + SqlParserPos pos2 = query.getParserPosition(); + SqlParserPos pos3 = getPos(); + int start = SqlParserUtil.lineColToIndex(sql, pos2.getLineNum(), pos2.getColumnNum()); + int end = SqlParserUtil.lineColToIndex(sql, pos3.getEndLineNum(), pos3.getEndColumnNum()); + String queryString = sql.substring(start, end + 1); + System.out.println("[" + queryString + "]"); + return new SqlCreateView(pos.plus(pos3), name, query, queryString); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java index 22d5d04..226cf70 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/jdbc/PhoenixPrepareImpl.java @@ -7,8 +7,10 @@ import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.plan.RelOptRule; import org.apache.calcite.prepare.CalcitePrepareImpl; import org.apache.calcite.rel.rules.JoinCommuteRule; +import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.parser.SqlParser; import org.apache.phoenix.calcite.PhoenixSchema; +import org.apache.phoenix.calcite.parse.SqlCreateView; import org.apache.phoenix.calcite.parser.PhoenixParserImpl; import org.apache.phoenix.calcite.rules.PhoenixAddScanLimitRule; import org.apache.phoenix.calcite.rules.PhoenixCompactClientSortRule; @@ -17,6 +19,9 @@ import org.apache.phoenix.calcite.rules.PhoenixInnerSortRemoveRule; import org.apache.phoenix.calcite.rules.PhoenixJoinSingleValueAggregateMergeRule; public class PhoenixPrepareImpl extends CalcitePrepareImpl { + public static final ThreadLocal<String> THREAD_SQL_STRING = + new ThreadLocal<>(); + protected final RelOptRule[] defaultConverterRules; public PhoenixPrepareImpl(RelOptRule[] defaultConverterRules) { @@ -30,6 +35,12 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { .setParserFactory(PhoenixParserImpl.FACTORY); } + protected SqlParser createParser(String sql, + SqlParser.ConfigBuilder parserConfig) { + THREAD_SQL_STRING.set(sql); + return SqlParser.create(sql, parserConfig.build()); + } + @Override protected RelOptPlanner createPlanner( final CalcitePrepare.Context prepareContext, @@ -61,4 +72,16 @@ public class PhoenixPrepareImpl extends CalcitePrepareImpl { return planner; } + + @Override + public void executeDdl(Context context, SqlNode node) { + switch (node.getKind()) { + case CREATE_VIEW: + final SqlCreateView cv = (SqlCreateView) node; + System.out.println("Create view: " + cv.name); + break; + default: + throw new AssertionError("unknown DDL type " + node.getKind() + " " + node.getClass()); + } + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlCreateView.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlCreateView.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlCreateView.java new file mode 100644 index 0000000..8d5af1a --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlCreateView.java @@ -0,0 +1,64 @@ +/* + * 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.calcite.parse; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.parser.SqlParserPos; + +import java.util.List; + +/** + * Parse tree node for SQL {@code CREATE VIEW} command. + */ +public class SqlCreateView extends SqlCall { + public static final SqlOperator OPERATOR = new SqlDdlOperator("CREATE VIEW", SqlKind.CREATE_VIEW); + + public final SqlIdentifier name; + public final SqlNode query; + public final String queryString; + + /** Creates a CREATE VIEW. */ + public SqlCreateView(SqlParserPos pos, SqlIdentifier name, SqlNode query, String queryString) { + super(pos); + this.name = name; + this.query = query; + this.queryString = queryString; + } + + public SqlOperator getOperator() { + return OPERATOR; + } + + public List<SqlNode> getOperandList() { + return ImmutableList.of(name, query); + } + + @Override + public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { + writer.keyword("CREATE VIEW"); + name.unparse(writer, 0, 0); + writer.keyword(" "); + query.unparse(writer, 0, 0); + } +} http://git-wip-us.apache.org/repos/asf/phoenix/blob/e7ac8cbb/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlDdlOperator.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlDdlOperator.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlDdlOperator.java new file mode 100644 index 0000000..9c7a18b --- /dev/null +++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/parse/SqlDdlOperator.java @@ -0,0 +1,20 @@ +package org.apache.phoenix.calcite.parse; + +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlSpecialOperator; +import org.apache.calcite.sql.SqlWriter; + +/** + * Operator for a DDL statement. + */ +class SqlDdlOperator extends SqlSpecialOperator { + public SqlDdlOperator(String name, SqlKind kind) { + super(name, kind); + } + + @Override + public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { + call.unparse(writer, leftPrec, rightPrec); + } +}