Repository: phoenix Updated Branches: refs/heads/4.x-HBase-0.98 655e2d33d -> f5a2f6d76 refs/heads/4.x-HBase-1.1 3eddc4a41 -> da5d33b08 refs/heads/4.x-HBase-1.2 1089db92b -> e746c225e refs/heads/master bd2751266 -> e27fae931
PHOENIX-3843 Improve logging for UNION ALL errors Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/e27fae93 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/e27fae93 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/e27fae93 Branch: refs/heads/master Commit: e27fae93191d9540574765065900a23bda90905f Parents: bd27512 Author: Sergey Soldatov <s...@apache.org> Authored: Wed May 10 00:44:17 2017 -0700 Committer: Sergey Soldatov <s...@apache.org> Committed: Mon May 22 10:57:56 2017 -0700 ---------------------------------------------------------------------- .../apache/phoenix/compile/UnionCompiler.java | 7 ++- .../phoenix/compile/QueryCompilerTest.java | 47 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/e27fae93/phoenix-core/src/main/java/org/apache/phoenix/compile/UnionCompiler.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/UnionCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/UnionCompiler.java index e5e18e3..c7f798c 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/UnionCompiler.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/UnionCompiler.java @@ -58,7 +58,8 @@ public class UnionCompiler { for (QueryPlan plan : selectPlans) { if (columnCount !=plan.getProjector().getColumnCount()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode - .SELECT_COLUMN_NUM_IN_UNIONALL_DIFFS).setMessage(".") + .SELECT_COLUMN_NUM_IN_UNIONALL_DIFFS).setMessage("1st query has " + columnCount + " columns whereas 2nd " + + "query has " + plan.getProjector().getColumnCount()) .build().buildException(); } ColumnProjector colproj = plan.getProjector().getColumnProjector(i); @@ -116,7 +117,9 @@ public class UnionCompiler { targetTypes.get(i).setType(type); } else { throw new SQLExceptionInfo.Builder(SQLExceptionCode - .SELECT_COLUMN_TYPE_IN_UNIONALL_DIFFS).setMessage(".") + .SELECT_COLUMN_TYPE_IN_UNIONALL_DIFFS).setMessage("Column # " + i + " is " + + targetTypes.get(i).getType().getSqlTypeName() + " in 1st query where as it is " + + type.getSqlTypeName() + " in 2nd query") .build().buildException(); } Integer len = expression.getMaxLength(); http://git-wip-us.apache.org/repos/asf/phoenix/blob/e27fae93/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java index 4bc7d2b..9d0e3d2 100644 --- a/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java +++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/QueryCompilerTest.java @@ -4134,4 +4134,51 @@ public class QueryCompilerTest extends BaseConnectionlessQueryTest { } } } + + @Test + public void testUnionDifferentColumnNumber() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + Statement statement = conn.createStatement(); + try { + String create = "CREATE TABLE s.t1 (k integer not null primary key, f1.v1 varchar, f1.v2 varchar, " + + "f2.v3 varchar, v4 varchar)"; + statement.execute(create); + create = "CREATE TABLE s.t2 (k integer not null primary key, f1.v1 varchar, f1.v2 varchar, f2.v3 varchar)"; + statement.execute(create); + String query = "SELECT * FROM s.t1 UNION ALL select * FROM s.t2"; + statement.executeQuery(query); + fail("Should fail with different column numbers "); + } catch (SQLException e) { + assertEquals(e.getMessage(), "ERROR 525 (42902): SELECT column number differs in a Union All query " + + "is not allowed. 1st query has 5 columns whereas 2nd query has 4"); + } finally { + statement.execute("DROP TABLE IF EXISTS s.t1"); + statement.execute("DROP TABLE IF EXISTS s.t2"); + conn.close(); + } + } + + @Test + public void testUnionDifferentColumnType() throws Exception { + Connection conn = DriverManager.getConnection(getUrl()); + Statement statement = conn.createStatement(); + try { + String create = "CREATE TABLE s.t1 (k integer not null primary key, f1.v1 varchar, f1.v2 varchar, " + + "f2.v3 varchar, v4 varchar)"; + statement.execute(create); + create = "CREATE TABLE s.t2 (k integer not null primary key, f1.v1 varchar, f1.v2 integer, " + + "f2.v3 varchar, f2.v4 varchar)"; + statement.execute(create); + String query = "SELECT * FROM s.t1 UNION ALL select * FROM s.t2"; + statement.executeQuery(query); + fail("Should fail with different column types "); + } catch (SQLException e) { + assertEquals(e.getMessage(), "ERROR 526 (42903): SELECT column types differ in a Union All query " + + "is not allowed. Column # 2 is VARCHAR in 1st query where as it is INTEGER in 2nd query"); + } finally { + statement.execute("DROP TABLE IF EXISTS s.t1"); + statement.execute("DROP TABLE IF EXISTS s.t2"); + conn.close(); + } + } }