PHOENIX-3460 Namespace separator : should not be allowed in table or schema name
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/474bc186 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/474bc186 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/474bc186 Branch: refs/heads/4.x-HBase-1.1 Commit: 474bc1866985e85c5647d8ce95a439b452a31301 Parents: 730f958 Author: Thomas D'Silva <tdsi...@apache.org> Authored: Thu Nov 2 12:08:07 2017 -0700 Committer: James Taylor <jtay...@salesforce.com> Committed: Wed Nov 15 10:46:40 2017 -0800 ---------------------------------------------------------------------- phoenix-core/src/main/antlr3/PhoenixSQL.g | 15 ++++++++++---- .../apache/phoenix/parse/QueryParserTest.java | 21 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/474bc186/phoenix-core/src/main/antlr3/PhoenixSQL.g ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g index 721b514..93e0ede 100644 --- a/phoenix-core/src/main/antlr3/PhoenixSQL.g +++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g @@ -1071,14 +1071,21 @@ cursor_name returns [CursorName ret] // TODO: figure out how not repeat this two times table_name returns [TableName ret] - : t=identifier {$ret = factory.table(null, t); } - | s=identifier DOT t=identifier {$ret = factory.table(s, t); } + : t=table_identifier {$ret = factory.table(null, t); } + | s=table_identifier DOT t=table_identifier {$ret = factory.table(s, t); } ; // TODO: figure out how not repeat this two times from_table_name returns [TableName ret] - : t=identifier {$ret = factory.table(null, t); } - | s=identifier DOT t=identifier {$ret = factory.table(s, t); } + : t=table_identifier {$ret = factory.table(null, t); } + | s=table_identifier DOT t=table_identifier {$ret = factory.table(s, t); } + ; + +table_identifier returns [String ret] + : c=identifier { + if (c.contains(QueryConstants.NAMESPACE_SEPARATOR) ) { throw new RuntimeException("Table or schema name cannot contain colon"); } + $ret = c; + } ; // The lowest level function, which includes literals, binds, but also parenthesized expressions, functions, and case statements. http://git-wip-us.apache.org/repos/asf/phoenix/blob/474bc186/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java b/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java index e7127b7..431f60b 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java @@ -29,6 +29,7 @@ import java.sql.SQLFeatureNotSupportedException; import java.util.List; import org.apache.hadoop.hbase.util.Pair; +import org.apache.phoenix.exception.PhoenixParserException; import org.apache.phoenix.exception.SQLExceptionCode; import org.apache.phoenix.jdbc.PhoenixStatement.Operation; import org.apache.phoenix.schema.SortOrder; @@ -56,6 +57,15 @@ public class QueryParserTest { assertEquals("Expected equality:\n" + sql + "\n" + newSQL, stmt, newStmt); } + private void parseQueryThatShouldFail(String sql) throws Exception { + try { + parseQuery(sql); + fail("Query should throw a PhoenixParserException \n " + sql); + } + catch (PhoenixParserException e){ + } + } + @Test public void testParsePreQuery0() throws Exception { String sql = (( @@ -782,4 +792,15 @@ public class QueryParserTest { String sql = Joiner.on(unicodeEnSpace).join(new String[] {"SELECT", "*", "FROM", "T"}); parseQuery(sql); } + + @Test + public void testInvalidTableOrSchemaName() throws Exception { + // namespace separator (:) cannot be used + parseQueryThatShouldFail("create table a:b (id varchar not null primary key)"); + parseQueryThatShouldFail("create table \"a:b\" (id varchar not null primary key)"); + // name separator (.) cannot be used without double quotes + parseQueryThatShouldFail("create table a.b.c.d (id varchar not null primary key)"); + parseQuery("create table \"a.b\".\"c.d\" (id varchar not null primary key)"); + parseQuery("create table \"a.b.c.d\" (id varchar not null primary key)"); + } }