Skip to content

Commit

Permalink
Fix MV query failure when projection has cast expression with alias
Browse files Browse the repository at this point in the history
  • Loading branch information
akashrn5 committed Jun 19, 2019
1 parent fc8c9d0 commit afb7bf7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
Expand Up @@ -289,7 +289,7 @@ private void initializeDataMapCatalogs(DataMapProvider dataMapProvider) throws I
if (null == dataMapCatalog) {
throw new RuntimeException("Internal Error.");
}
dataMapCatalogs.put(schema.getProviderName(), dataMapCatalog);
dataMapCatalogs.put(schema.getProviderName().toLowerCase(), dataMapCatalog);
}
try {
dataMapCatalog.registerSchema(schema);
Expand Down
Expand Up @@ -111,6 +111,23 @@ object MVUtil {
}
case a@Alias(_, name) =>
checkIfComplexDataTypeExists(a)
val arrayBuffer: ArrayBuffer[ColumnTableRelation] = new ArrayBuffer[ColumnTableRelation]()
a.collect {
case attr: AttributeReference =>
val carbonTable = getCarbonTable(logicalRelation, attr)
if (null != carbonTable) {
val relation = getColumnRelation(attr.name,
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier.getTableId,
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier.getTableName,
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier.getDatabaseName,
carbonTable)
if (null != relation) {
arrayBuffer += relation
}
}
}
fieldToDataMapFieldMap +=
getFieldToDataMapFields(a.name, a.dataType, None, "arithmetic", arrayBuffer, "")
}
fieldToDataMapFieldMap
}
Expand Down
Expand Up @@ -121,8 +121,10 @@ object SelectSelectNoChildDelta extends DefaultMatchPattern with PredicateHelper
} else if (subsumee.asInstanceOf[Select].outputList.contains(exprE)) {
exprE match {
case a@Alias(_, _) =>
exprListR.exists(a1 => a1.isInstanceOf[Alias] &&
a1.asInstanceOf[Alias].child.semanticEquals(a.child)) ||
exprListR
.exists(a1 => a1.isInstanceOf[Alias] &&
a.name.equalsIgnoreCase(a1.asInstanceOf[Alias].name) &&
a1.asInstanceOf[Alias].child.semanticEquals(a.child)) ||
exprListR.exists(_.semanticEquals(exprE) || canEvaluate(exprE, subsumer))
case exp => exprListR.exists(_.semanticEquals(exp) || canEvaluate(exp, subsumer))
}
Expand Down
Expand Up @@ -1092,6 +1092,20 @@ class MVCreateTestCase extends QueryTest with BeforeAndAfterAll {
assert(TestUtil.verifyMVDataMap(analyzed4, "constant_mv"))
}

test("test cast 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,m_month 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"))
}


def drop(): Unit = {
sql("drop table IF EXISTS fact_table1")
sql("drop table IF EXISTS fact_table2")
Expand Down
Expand Up @@ -101,6 +101,7 @@ object HarmonizedRelation {
case alias: Alias =>
alias.child.isInstanceOf[AttributeReference] ||
alias.child.isInstanceOf[Literal] ||
alias.child.isInstanceOf[Expression] ||
(alias.child match {
case AggregateExpression(First(_, _), _, _, _) => true
case AggregateExpression(Last(_, _), _, _, _) => true
Expand Down

0 comments on commit afb7bf7

Please sign in to comment.