Skip to content

Commit

Permalink
Whenever a not equal to filter is applied on dictionary column with n…
Browse files Browse the repository at this point in the history
…umeric datatype, the cast added by spark plan is removed while creating carbon filters from spark filter. Due to this plan modification incorrect results are returned by spark.
  • Loading branch information
manishgupta88 committed Mar 22, 2017
1 parent a1b8afa commit 51bb05c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1,2015-07-23 00:00:00,china,aaa1,phone197,ASD69643,15000
7,2015-07-24 00:00:00,china,aaa2,phone756,ASD42892,15001
7,2015-07-25 00:00:00,china,aaa3,phone1904,ASD37014,15002
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.carbondata.spark.testsuite.filterexpr

import org.apache.spark.sql.common.util.QueryTest
import org.scalatest.BeforeAndAfterAll

import org.apache.carbondata.core.constants.CarbonCommonConstants
import org.apache.carbondata.core.util.CarbonProperties

/**
* Test cases for testing columns having \N or \null values for non numeric columns
*/
class TestNotEqualToFilter extends QueryTest with BeforeAndAfterAll {

override def beforeAll {
sql("drop table if exists test_not_equal_to_carbon")
sql("drop table if exists test_not_equal_to_hive")
CarbonProperties.getInstance()
.addProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT
)
sql(
"""
CREATE TABLE IF NOT EXISTS test_not_equal_to_carbon
(ID Int, date Timestamp, country String,
name String, phonetype String, serialname String, salary Int)
STORED BY 'org.apache.carbondata.format' TBLPROPERTIES('dictionary_include'='id')
""")
sql(
"""
CREATE TABLE IF NOT EXISTS test_not_equal_to_hive
(ID Int, date Timestamp, country String,
name String, phonetype String, serialname String, salary Int)
row format delimited fields terminated by ','
""")
sql(
s"""
LOAD DATA LOCAL INPATH '$resourcesPath/filter/notEqualToFilter.csv' into table
test_not_equal_to_carbon
OPTIONS('FILEHEADER'='ID,date,country,name,phonetype,serialname,salary')
""")
sql(
s"""
LOAD DATA LOCAL INPATH '$resourcesPath/filter/notEqualToFilter.csv' into table
test_not_equal_to_hive
""")
}

test("select Id from test_not_equal_to_carbon where id != '7'") {
checkAnswer(
sql("select Id from test_not_equal_to_carbon where id != '7'"),
sql("select Id from test_not_equal_to_hive where id != '7'")
)
}

test("select Id from test_not_equal_to_carbon where id != 7.0") {
checkAnswer(
sql("select Id from test_not_equal_to_carbon where id != 7.0"),
sql("select Id from test_not_equal_to_hive where id != 7.0")
)
}

test("select Id from test_not_equal_to_carbon where id != 7") {
checkAnswer(
sql("select Id from test_not_equal_to_carbon where id != 7"),
sql("select Id from test_not_equal_to_hive where id != 7")
)
}

override def afterAll {
sql("drop table if exists test_not_equal_to_carbon")
sql("drop table if exists test_not_equal_to_hive")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,10 @@ object CarbonFilters {
Some(sources.EqualTo(a.name, v))
case EqualTo(l@Literal(v, t), a: Attribute) =>
Some(sources.EqualTo(a.name, v))
case EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.EqualTo(a.name, v))
case EqualTo(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.EqualTo(a.name, v))
case Not(EqualTo(a: Attribute, Literal(v, t))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Literal(v, t), a: Attribute)) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Cast(a: Attribute, _), Literal(v, t))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Literal(v, t), Cast(a: Attribute, _))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case IsNotNull(a: Attribute) =>
Some(sources.IsNotNull(a.name))
case IsNull(a: Attribute) =>
Expand All @@ -169,20 +161,10 @@ object CarbonFilters {
case In(a: Attribute, list) if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.In(a.name, hSet.toArray))
case Not(In(Cast(a: Attribute, _), list)) if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.Not(sources.In(a.name, hSet.toArray)))
case In(Cast(a: Attribute, _), list) if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.In(a.name, hSet.toArray))
case GreaterThan(a: Attribute, Literal(v, t)) =>
Some(sources.GreaterThan(a.name, v))
case GreaterThan(Literal(v, t), a: Attribute) =>
Some(sources.LessThan(a.name, v))
case GreaterThan(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.GreaterThan(a.name, v))
case GreaterThan(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.LessThan(a.name, v))
case LessThan(a: Attribute, Literal(v, t)) =>
Some(sources.LessThan(a.name, v))
case LessThan(Literal(v, t), a: Attribute) =>
Expand All @@ -195,18 +177,10 @@ object CarbonFilters {
Some(sources.GreaterThanOrEqual(a.name, v))
case GreaterThanOrEqual(Literal(v, t), a: Attribute) =>
Some(sources.LessThanOrEqual(a.name, v))
case GreaterThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.GreaterThanOrEqual(a.name, v))
case GreaterThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.LessThanOrEqual(a.name, v))
case LessThanOrEqual(a: Attribute, Literal(v, t)) =>
Some(sources.LessThanOrEqual(a.name, v))
case LessThanOrEqual(Literal(v, t), a: Attribute) =>
Some(sources.GreaterThanOrEqual(a.name, v))
case LessThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.LessThanOrEqual(a.name, v))
case LessThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.GreaterThanOrEqual(a.name, v))

case others =>
if (!or) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,10 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
Some(sources.EqualTo(a.name, v))
case EqualTo(l@Literal(v, t), a: Attribute) =>
Some(sources.EqualTo(a.name, v))
case EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.EqualTo(a.name, v))
case EqualTo(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.EqualTo(a.name, v))

case Not(EqualTo(a: Attribute, Literal(v, t))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Literal(v, t), a: Attribute)) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Cast(a: Attribute, _), Literal(v, t))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case Not(EqualTo(Literal(v, t), Cast(a: Attribute, _))) =>
Some(sources.Not(sources.EqualTo(a.name, v)))
case IsNotNull(a: Attribute) => Some(sources.IsNotNull(a.name))
case IsNull(a: Attribute) => Some(sources.IsNull(a.name))
case Not(In(a: Attribute, list)) if !list.exists(!_.isInstanceOf[Literal]) =>
Expand All @@ -433,50 +424,22 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
case In(a: Attribute, list) if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.In(a.name, hSet.toArray))
case Not(In(Cast(a: Attribute, _), list))
if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.Not(sources.In(a.name, hSet.toArray)))
case In(Cast(a: Attribute, _), list) if !list.exists(!_.isInstanceOf[Literal]) =>
val hSet = list.map(e => e.eval(EmptyRow))
Some(sources.In(a.name, hSet.toArray))

case GreaterThan(a: Attribute, Literal(v, t)) =>
Some(sources.GreaterThan(a.name, v))
case GreaterThan(Literal(v, t), a: Attribute) =>
Some(sources.LessThan(a.name, v))
case GreaterThan(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.GreaterThan(a.name, v))
case GreaterThan(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.LessThan(a.name, v))

case LessThan(a: Attribute, Literal(v, t)) =>
Some(sources.LessThan(a.name, v))
case LessThan(Literal(v, t), a: Attribute) =>
Some(sources.GreaterThan(a.name, v))
case LessThan(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.LessThan(a.name, v))
case LessThan(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.GreaterThan(a.name, v))

case GreaterThanOrEqual(a: Attribute, Literal(v, t)) =>
Some(sources.GreaterThanOrEqual(a.name, v))
case GreaterThanOrEqual(Literal(v, t), a: Attribute) =>
Some(sources.LessThanOrEqual(a.name, v))
case GreaterThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.GreaterThanOrEqual(a.name, v))
case GreaterThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.LessThanOrEqual(a.name, v))

case LessThanOrEqual(a: Attribute, Literal(v, t)) =>
Some(sources.LessThanOrEqual(a.name, v))
case LessThanOrEqual(Literal(v, t), a: Attribute) =>
Some(sources.GreaterThanOrEqual(a.name, v))
case LessThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
Some(sources.LessThanOrEqual(a.name, v))
case LessThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
Some(sources.GreaterThanOrEqual(a.name, v))

case others => None
}
}
Expand Down

0 comments on commit 51bb05c

Please sign in to comment.