This is an automated email from the ASF dual-hosted git repository.

ravipesala pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/carbondata.git


The following commit(s) were added to refs/heads/master by this push:
     new 771d436  [CARBONDATA-3457][MV] Fix Column not found issue with Query 
having Cast Expression
771d436 is described below

commit 771d436fe2ed2d34ccf0ee1d8f555af30c382345
Author: Indhumathi27 <indhumathi...@gmail.com>
AuthorDate: Thu Jun 27 17:09:20 2019 +0530

    [CARBONDATA-3457][MV] Fix Column not found issue with Query having Cast 
Expression
    
    Problem:
    For Cast(exp), alias reference is not included, hence throws column not 
found exception for column given inside cast expression.
    
    Solution:
    AliasMap has to be created for CAST[EXP] also and should be replaced with 
subsmer alias map references.
    
    This closes #3312
---
 .../carbondata/mv/rewrite/DefaultMatchMaker.scala  | 16 ++++++
 .../carbondata/mv/rewrite/MVCreateTestCase.scala   | 58 ++++++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git 
a/datamap/mv/core/src/main/scala/org/apache/carbondata/mv/rewrite/DefaultMatchMaker.scala
 
b/datamap/mv/core/src/main/scala/org/apache/carbondata/mv/rewrite/DefaultMatchMaker.scala
index 9a9a2a6..5329608 100644
--- 
a/datamap/mv/core/src/main/scala/org/apache/carbondata/mv/rewrite/DefaultMatchMaker.scala
+++ 
b/datamap/mv/core/src/main/scala/org/apache/carbondata/mv/rewrite/DefaultMatchMaker.scala
@@ -53,6 +53,14 @@ abstract class DefaultMatchPattern extends 
MatchPattern[ModularPlan] {
             (a.child.asInstanceOf[Attribute], a.toAttribute)
           })
 
+    // Create aliasMap with Expression to alias reference attribute
+    val aliasMapExp =
+      subsumer.outputList.collect {
+        case a: Alias if a.child.isInstanceOf[Expression] &&
+                         !a.child.isInstanceOf[AggregateExpression] =>
+          a.child -> a.toAttribute
+      }.toMap
+
     // Check and replace all alias references with subsumer alias map 
references.
     val compensation1 = compensation.transform {
       case plan if !plan.skip && plan != subsumer =>
@@ -66,6 +74,14 @@ abstract class DefaultMatchPattern extends 
MatchPattern[ModularPlan] {
                   exprId = ref.exprId,
                   qualifier = a.qualifier)
               }.getOrElse(a)
+          case a: Expression =>
+            aliasMapExp
+              .get(a)
+              .map { ref =>
+                AttributeReference(
+                  ref.name, ref.dataType)(
+                  exprId = ref.exprId)
+              }.getOrElse(a)
           }
     }
 
diff --git 
a/datamap/mv/core/src/test/scala/org/apache/carbondata/mv/rewrite/MVCreateTestCase.scala
 
b/datamap/mv/core/src/test/scala/org/apache/carbondata/mv/rewrite/MVCreateTestCase.scala
index 1d259c8..ca6c0c5 100644
--- 
a/datamap/mv/core/src/test/scala/org/apache/carbondata/mv/rewrite/MVCreateTestCase.scala
+++ 
b/datamap/mv/core/src/test/scala/org/apache/carbondata/mv/rewrite/MVCreateTestCase.scala
@@ -1169,6 +1169,64 @@ class MVCreateTestCase extends QueryTest with 
BeforeAndAfterAll {
     assert(TestUtil.verifyMVDataMap(analyzed1, "da_cast"))
   }
 
+  test("test cast of expression with mv") {
+    sql("drop table IF EXISTS maintable")
+    sql("create table maintable (m_month bigint, c_code string, " +
+        "c_country smallint, d_dollar_value double, q_quantity double, u_unit 
smallint, b_country smallint, i_id int, y_year smallint) stored by 
'carbondata'")
+    sql("insert into maintable select 10, 'xxx', 123, 456, 45, 5, 23, 1, 2000")
+    sql("drop datamap if exists da_cast")
+    sql(
+      "create datamap da_cast using 'mv' as select cast(floor((m_month +1000) 
/ 900) * 900 - 2000 AS INT) as a, c_code as abc from maintable")
+    val df1 = sql(
+      " select cast(floor((m_month +1000) / 900) * 900 - 2000 AS INT) as a 
,c_code as abc  from maintable")
+    val df2 = sql(
+      " select cast(floor((m_month +1000) / 900) * 900 - 2000 AS INT),c_code 
as abc  from maintable")
+    val analyzed1 = df1.queryExecution.analyzed
+    assert(TestUtil.verifyMVDataMap(analyzed1, "da_cast"))
+  }
+
+  test("test cast with & without alias") {
+    sql("drop table IF EXISTS maintable")
+    sql("create table maintable (m_month bigint, c_code string, " +
+        "c_country smallint, d_dollar_value double, q_quantity double, u_unit 
smallint, b_country smallint, i_id int, y_year smallint) stored by 
'carbondata'")
+    sql("insert into maintable select 10, 'xxx', 123, 456, 45, 5, 23, 1, 2000")
+    sql("drop datamap if exists da_cast")
+    sql(
+      "create datamap da_cast using 'mv' as select cast(m_month + 1000 AS INT) 
as a, c_code as abc from maintable")
+    checkAnswer(sql("select cast(m_month + 1000 AS INT) as a, c_code as abc 
from maintable"), Seq(Row(1010, "xxx")))
+    var df1 = sql("select cast(m_month + 1000 AS INT) as a, c_code as abc from 
maintable")
+    var analyzed1 = df1.queryExecution.analyzed
+    assert(TestUtil.verifyMVDataMap(analyzed1, "da_cast"))
+    sql("drop datamap if exists da_cast")
+    sql(
+      "create datamap da_cast using 'mv' as select cast(m_month + 1000 AS 
INT), c_code from maintable")
+    df1 = sql("select cast(m_month + 1000 AS INT), c_code from maintable")
+    analyzed1 = df1.queryExecution.analyzed
+    assert(TestUtil.verifyMVDataMap(analyzed1, "da_cast"))
+    checkAnswer(sql("select cast(m_month + 1000 AS INT), c_code from 
maintable"), Seq(Row(1010, "xxx")))
+  }
+
+  test("test mv with floor & ceil exp") {
+    sql("drop table IF EXISTS maintable")
+    sql("create table maintable (m_month bigint, c_code string, " +
+        "c_country smallint, d_dollar_value double, q_quantity double, u_unit 
smallint, b_country smallint, i_id int, y_year smallint) stored by 
'carbondata'")
+    sql("insert into maintable select 10, 'xxx', 123, 456, 45, 5, 23, 1, 2000")
+    sql("drop datamap if exists da_floor")
+    sql(
+      "create datamap da_floor using 'mv' as select floor(m_month) as a, 
c_code as abc from maintable")
+    checkAnswer(sql("select floor(m_month) as a, c_code as abc from 
maintable"), Seq(Row(10, "xxx")))
+    var df1 = sql("select floor(m_month) as a, c_code as abc from maintable")
+    var analyzed1 = df1.queryExecution.analyzed
+    assert(TestUtil.verifyMVDataMap(analyzed1, "da_floor"))
+    sql("drop datamap if exists da_ceil")
+    sql(
+      "create datamap da_ceil using 'mv' as select ceil(m_month) as a, c_code 
as abc from maintable")
+    checkAnswer(sql("select ceil(m_month) as a, c_code as abc from 
maintable"), Seq(Row(10, "xxx")))
+    var df2 = sql("select ceil(m_month) as a, c_code as abc from maintable")
+    var analyzed2 = df2.queryExecution.analyzed
+    assert(TestUtil.verifyMVDataMap(analyzed2, "da_ceil"))
+  }
+
   def drop(): Unit = {
     sql("drop table IF EXISTS fact_table1")
     sql("drop table IF EXISTS fact_table2")

Reply via email to