From bcb5e5b0f53aab781d905be86b5cb8d6058ac034 Mon Sep 17 00:00:00 2001 From: Jihoon Son Date: Tue, 6 Oct 2015 11:49:11 +0900 Subject: [PATCH] TAJO-1912 --- .../query/TestQueryOnSelfDescTable.java | 98 +++++++++++++++++++ .../testJoinAliasedTables.1.result | 4 + .../testJoinAliasedTables2.1.result | 3 + ...lfDescTablesWithQualifiedColumns2.1.result | 3 + ...lfDescTablesWithQualifiedColumns3.1.result | 3 + .../testSelectFromAliasedTable.1.result | 3 + .../rewrite/SelfDescSchemaBuildPhase.java | 38 ++++--- 7 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables.1.result create mode 100644 tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables2.1.result create mode 100644 tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns2.1.result create mode 100644 tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns3.1.result create mode 100644 tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testSelectFromAliasedTable.1.result diff --git a/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestQueryOnSelfDescTable.java b/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestQueryOnSelfDescTable.java index 0e5e577a9d..367269740c 100644 --- a/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestQueryOnSelfDescTable.java +++ b/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestQueryOnSelfDescTable.java @@ -67,6 +67,15 @@ public final void testSelect2() throws Exception { runSimpleTests(); } + @Test + @Option(sort = true) + @SimpleTest( + queries = @QuerySpec("select t1.glossary.title from self_desc_table2 t1") + ) + public final void testSelectFromAliasedTable() throws Exception { + runSimpleTests(); + } + @Test @Option(sort = true) @SimpleTest( @@ -217,6 +226,63 @@ public final void testJoinOfSelfDescTables() throws Exception { " user.favourites_count = name.first_name"); } + + @Test + @Option(sort = true) + @SimpleTest( + queries = @QuerySpec("" + + "select\n" + + " user.favourites_count::int8,\n" + + " l_linenumber,\n" + + " l_comment\n" + + "from\n" + + " default.lineitem l1, self_desc_table3 t1\n" + + "where\n" + + " user.favourites_count::int8 = (l_orderkey - 1)" + ) + ) + public final void testJoinAliasedTables() throws Exception { + runSimpleTests(); + } + + @Test + @Option(sort = true) + @SimpleTest( + queries = @QuerySpec("" + + "select\n" + + " user.favourites_count::int8,\n" + + " l_linenumber,\n" + + " l_comment\n" + + "from\n" + + " default.lineitem l1, self_desc_table3 t3, default.orders o1, default.supplier s1\n" + + "where\n" + + " user.favourites_count::int8 = (l_orderkey - 1) and l_orderkey = o_orderkey and l_linenumber = s_suppkey" + ) + ) + public final void testJoinAliasedTables2() throws Exception { + runSimpleTests(); + } + + @Test(expected = AmbiguousColumnException.class) + public final void testJoinAliasedTables3() throws Exception { + executeString("" + + "select " + + " user.favourites_count::int8, " + + " l_linenumber, " + + " l_comment " + + "from " + + " default.lineitem l1, " + + " self_desc_table1 t1, " + + " self_desc_table3 t2, " + + " default.orders o2, " + + " default.supplier s2 " + + "where " + + " user.favourites_count::int8 = (l_orderkey - 1) and " + + " l_orderkey = o_orderkey and " + + " l_linenumber = s_suppkey and " + + " self_desc_table3.user.favourites_count = self_desc_table1.name.first_name"); + } + @Test @Option(sort = true) @SimpleTest @@ -224,6 +290,38 @@ public final void testJoinOfSelfDescTablesWithQualifiedColumns() throws Exceptio runSimpleTests(); } + @Test + @Option(sort = true) + @SimpleTest( + queries = @QuerySpec("" + + "select\n" + + " t1.user.favourites_count::int8\n" + + "from\n" + + " github g1, self_desc_table3 t1\n" + + "where\n" + + " t1.user.favourites_count = (g1.actor.id::int8 - 206379)::text" + ) + ) + public final void testJoinOfSelfDescTablesWithQualifiedColumns2() throws Exception { + runSimpleTests(); + } + + @Test + @Option(sort = true) + @SimpleTest( + queries = @QuerySpec("" + + "select\n" + + " t1.user.favourites_count::int8\n" + + "from\n" + + " github g1, self_desc_table3 t1\n" + + "where\n" + + " self_desc_table3.user.favourites_count = (github.actor.id::int8 - 206379)::text" + ) + ) + public final void testJoinOfSelfDescTablesWithQualifiedColumns3() throws Exception { + runSimpleTests(); + } + @Test(expected = AmbiguousColumnException.class) public final void testJoinWithSingleQualifiedColumn() throws Exception { executeString("" + diff --git a/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables.1.result b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables.1.result new file mode 100644 index 0000000000..1754d7f1a9 --- /dev/null +++ b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables.1.result @@ -0,0 +1,4 @@ +?cast,l_linenumber,l_comment +------------------------------- +0,1,egular courts above the +0,2,ly final dependencies: slyly bold diff --git a/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables2.1.result b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables2.1.result new file mode 100644 index 0000000000..ef95de348c --- /dev/null +++ b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinAliasedTables2.1.result @@ -0,0 +1,3 @@ +?cast,l_linenumber,l_comment +------------------------------- +0,2,ly final dependencies: slyly bold diff --git a/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns2.1.result b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns2.1.result new file mode 100644 index 0000000000..9e68a1929e --- /dev/null +++ b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns2.1.result @@ -0,0 +1,3 @@ +?cast +------------------------------- +0 diff --git a/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns3.1.result b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns3.1.result new file mode 100644 index 0000000000..9e68a1929e --- /dev/null +++ b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testJoinOfSelfDescTablesWithQualifiedColumns3.1.result @@ -0,0 +1,3 @@ +?cast +------------------------------- +0 diff --git a/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testSelectFromAliasedTable.1.result b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testSelectFromAliasedTable.1.result new file mode 100644 index 0000000000..6f9137e9e1 --- /dev/null +++ b/tajo-core-tests/src/test/resources/results/TestQueryOnSelfDescTable/testSelectFromAliasedTable.1.result @@ -0,0 +1,3 @@ +glossary/title +------------------------------- +example glossary diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/rewrite/SelfDescSchemaBuildPhase.java b/tajo-plan/src/main/java/org/apache/tajo/plan/rewrite/SelfDescSchemaBuildPhase.java index f609db2705..d697d22c05 100644 --- a/tajo-plan/src/main/java/org/apache/tajo/plan/rewrite/SelfDescSchemaBuildPhase.java +++ b/tajo-plan/src/main/java/org/apache/tajo/plan/rewrite/SelfDescSchemaBuildPhase.java @@ -62,17 +62,17 @@ public String getName() { return "Self-describing schema build phase"; } - private static String getQualifiedRelationName(PlanContext context, Relation relation) { - return CatalogUtil.isFQTableName(relation.getName()) ? - relation.getName() : - CatalogUtil.buildFQName(context.getQueryContext().get(SessionVars.CURRENT_DATABASE), relation.getName()); + private static String getQualifiedName(PlanContext context, String simpleName) { + return CatalogUtil.isFQTableName(simpleName) ? + simpleName : + CatalogUtil.buildFQName(context.getQueryContext().get(SessionVars.CURRENT_DATABASE), simpleName); } @Override public boolean isEligible(PlanContext context, Expr expr) throws TajoException { Set relations = ExprFinderIncludeSubquery.finds(expr, OpType.Relation); for (Relation eachRelation : relations) { - TableDesc tableDesc = catalog.getTableDesc(getQualifiedRelationName(context, eachRelation)); + TableDesc tableDesc = catalog.getTableDesc(getQualifiedName(context, eachRelation.getName())); if (tableDesc.hasEmptySchema()) { return true; } @@ -94,7 +94,7 @@ private static class ExprFinderIncludeSubquery extends SimpleAlgebraVisitor Set finds(Expr expr, OpType type) throws TajoException { FinderContext context = new FinderContext<>(type); ExprFinderIncludeSubquery finder = new ExprFinderIncludeSubquery(); - finder.visit(context, new Stack(), expr); + finder.visit(context, new Stack<>(), expr); return context.set; } @@ -138,7 +138,7 @@ public LogicalNode process(PlanContext context, Expr expr) throws TajoException if (processor == null) { processor = new Processor(); } - return processor.visit(new ProcessorContext(context), new Stack(), expr); + return processor.visit(new ProcessorContext(context), new Stack<>(), expr); } static class ProcessorContext { @@ -361,19 +361,29 @@ public LogicalNode visitRelation(ProcessorContext ctx, Stack stack, Relati TableDesc desc = scan.getTableDesc(); if (desc.hasEmptySchema()) { - if (ctx.projectColumns.containsKey(getQualifiedRelationName(ctx.planContext, expr))) { - Set columns = new HashSet<>(); - for (ColumnReferenceExpr col : ctx.projectColumns.get(getQualifiedRelationName(ctx.planContext, expr))) { + Set columns = new HashSet<>(); + if (ctx.projectColumns.containsKey(getQualifiedName(ctx.planContext, expr.getName()))) { + for (ColumnReferenceExpr col : ctx.projectColumns.get(getQualifiedName(ctx.planContext, expr.getName()))) { columns.add(NameResolver.resolve(plan, queryBlock, col, NameResolvingMode.RELS_ONLY, true)); } + } - desc.setSchema(buildSchemaFromColumnSet(columns)); - scan.init(desc); - } else { + if (expr.hasAlias()) { + if (ctx.projectColumns.containsKey(getQualifiedName(ctx.planContext, expr.getAlias()))) { + for (ColumnReferenceExpr col : ctx.projectColumns.get(getQualifiedName(ctx.planContext, expr.getAlias()))) { + columns.add(NameResolver.resolve(plan, queryBlock, col, NameResolvingMode.RELS_ONLY, true)); + } + } + } + + if (columns.isEmpty()) { // error throw new TajoInternalError( - "Columns projected from " + getQualifiedRelationName(ctx.planContext, expr) + " is not found."); + "Columns projected from " + getQualifiedName(ctx.planContext, expr.getName()) + " is not found."); } + + desc.setSchema(buildSchemaFromColumnSet(columns)); + scan.init(desc); } return scan;