From 3680e13dd18be3c79f0f448207ce3c659c7e8e17 Mon Sep 17 00:00:00 2001 From: Hyunsik Choi Date: Thu, 24 Jul 2014 14:07:35 +0900 Subject: [PATCH] TAJO-975: alias name which is the same to existing column name may cause NPE during PPD. --- .../rewrite/ProjectionPushDownRule.java | 10 ++++++++++ .../tajo/engine/query/TestSelectQuery.java | 9 +++++++++ ...stSelectColumnAliasExistingInRelation3.sql | 19 +++++++++++++++++++ ...electColumnAliasExistingInRelation3.result | 11 +++++++++++ 4 files changed, 49 insertions(+) create mode 100644 tajo-core/src/test/resources/queries/TestSelectQuery/testSelectColumnAliasExistingInRelation3.sql create mode 100644 tajo-core/src/test/resources/results/TestSelectQuery/testSelectColumnAliasExistingInRelation3.result diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/planner/rewrite/ProjectionPushDownRule.java b/tajo-core/src/main/java/org/apache/tajo/engine/planner/rewrite/ProjectionPushDownRule.java index e5a329d902..4a12d99e49 100644 --- a/tajo-core/src/main/java/org/apache/tajo/engine/planner/rewrite/ProjectionPushDownRule.java +++ b/tajo-core/src/main/java/org/apache/tajo/engine/planner/rewrite/ProjectionPushDownRule.java @@ -191,6 +191,7 @@ private String add(String specifiedName, EvalNode evalNode) throws PlanningExcep // if a name already exists, it only just keeps an actual // expression instead of a column reference. if (nameToIdBiMap.containsKey(specifiedName)) { + int refId = nameToIdBiMap.get(specifiedName); EvalNode found = idToEvalBiMap.get(refId); if (found != null) { @@ -204,7 +205,16 @@ private String add(String specifiedName, EvalNode evalNode) throws PlanningExcep } if (found.getType() == EvalType.FIELD) { + Integer daggling = idToEvalBiMap.inverse().get(evalNode); idToEvalBiMap.forcePut(refId, evalNode); + if (daggling != null) { + String name = getPrimaryName(daggling); + idToNamesMap.remove(daggling); + nameToIdBiMap.put(name, refId); + if (!idToNamesMap.get(refId).contains(name)) { + TUtil.putToNestedList(idToNamesMap, refId, name); + } + } } } } diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java index 8898067fe4..088a7c4dfa 100644 --- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java +++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java @@ -138,6 +138,15 @@ public final void testSelectColumnAliasExistingInRelation2() throws Exception { cleanupQuery(res); } + @Test + public final void testSelectColumnAliasExistingInRelation3() throws Exception { + // This is a reproduction code and validator of TAJO-975 Bug + // Please see TAJO-975 in order to know this test in detail. + ResultSet res = executeQuery(); + assertResultSet(res); + cleanupQuery(res); + } + @Test public final void testSelectSameConstantsWithDifferentAliases() throws Exception { diff --git a/tajo-core/src/test/resources/queries/TestSelectQuery/testSelectColumnAliasExistingInRelation3.sql b/tajo-core/src/test/resources/queries/TestSelectQuery/testSelectColumnAliasExistingInRelation3.sql new file mode 100644 index 0000000000..98336b7a41 --- /dev/null +++ b/tajo-core/src/test/resources/queries/TestSelectQuery/testSelectColumnAliasExistingInRelation3.sql @@ -0,0 +1,19 @@ +SELECT l_orderkey FROM ( + +-- actual test query + SELECT + T1.l_orderkey + FROM + LINEITEM + INNER JOIN ( + SELECT + T1.l_orderkey + FROM ( + SELECT + LINEITEM.l_orderkey AS l_orderkey + FROM + LINEITEM + ) T1 + ) T1 ON LINEITEM.l_orderkey=T1.l_orderkey + +) A ORDER BY l_orderkey; -- for determinant query result \ No newline at end of file diff --git a/tajo-core/src/test/resources/results/TestSelectQuery/testSelectColumnAliasExistingInRelation3.result b/tajo-core/src/test/resources/results/TestSelectQuery/testSelectColumnAliasExistingInRelation3.result new file mode 100644 index 0000000000..3b40488a98 --- /dev/null +++ b/tajo-core/src/test/resources/results/TestSelectQuery/testSelectColumnAliasExistingInRelation3.result @@ -0,0 +1,11 @@ +l_orderkey +------------------------------- +1 +1 +1 +1 +2 +3 +3 +3 +3 \ No newline at end of file