Skip to content

Commit

Permalink
[CARBONDATA-3457][MV] Fix Column not found issue with Query having Ca…
Browse files Browse the repository at this point in the history
…st Expression
  • Loading branch information
Indhumathi27 committed Jul 2, 2019
1 parent 188e7e4 commit 27adf14
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 27adf14

Please sign in to comment.