You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@carbondata.apache.org by sounakr <gi...@git.apache.org> on 2017/04/17 02:31:50 UTC

[GitHub] incubator-carbondata pull request #805: [WIP] Cast Push Down Optimization

GitHub user sounakr opened a pull request:

    https://github.com/apache/incubator-carbondata/pull/805

    [WIP] Cast Push Down Optimization

    Problem : With Filter Expression with Casts, PushDown to Carbon layer was not happening. 
    
    Analysis : Cast Expressions are handled in Spark. So Whenever Cast Expressions are present in the Filter predicates then Carbon Scan all its data and move it to spark for evaluation. This makes the query processing slow.
    
    Fix : Handle Cast Expression in Carbon Layer. As of now it is handled in Spark -2.1 

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/sounakr/incubator-carbondata casts

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-carbondata/pull/805.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #805
    
----
commit 2c65a98b5c181f144f3d9ff1033d01bbb604e567
Author: sounakr <so...@gmail.com>
Date:   2017-04-04T09:46:46Z

    Cast Push Down Optimization

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112638544
  
    --- Diff: core/src/main/java/org/apache/carbondata/core/scan/expression/CastExpression.java ---
    @@ -0,0 +1,43 @@
    +/*
    + * 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.core.scan.expression;
    +
    +import org.apache.carbondata.core.scan.expression.exception.FilterIllegalMemberException;
    +import org.apache.carbondata.core.scan.expression.exception.FilterUnsupportedException;
    +import org.apache.carbondata.core.scan.filter.intf.ExpressionType;
    +import org.apache.carbondata.core.scan.filter.intf.RowIntf;
    +
    +public class CastExpression extends Expression {
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112636099
  
    --- Diff: integration/spark/src/main/scala/org/apache/spark/sql/CarbonBoundReference.scala ---
    @@ -18,12 +18,15 @@
     package org.apache.spark.sql
     
     import org.apache.spark.sql.catalyst.InternalRow
    -import org.apache.spark.sql.catalyst.expressions.{Attribute, ExprId, LeafExpression, NamedExpression}
    +import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, ExprId, LeafExpression, NamedExpression}
     import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.sources.Filter
     import org.apache.spark.sql.types.DataType
     
     import org.apache.carbondata.core.scan.expression.ColumnExpression
     
    +case class CastExpr(expr: Expression) extends Filter {
    --- End diff --
    
    Remove the empty braces


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    LGTM


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112638519
  
    --- Diff: integration/spark/src/main/scala/org/apache/spark/sql/CarbonBoundReference.scala ---
    @@ -18,12 +18,15 @@
     package org.apache.spark.sql
     
     import org.apache.spark.sql.catalyst.InternalRow
    -import org.apache.spark.sql.catalyst.expressions.{Attribute, ExprId, LeafExpression, NamedExpression}
    +import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, ExprId, LeafExpression, NamedExpression}
     import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.sources.Filter
     import org.apache.spark.sql.types.DataType
     
     import org.apache.carbondata.core.scan.expression.ColumnExpression
     
    +case class CastExpr(expr: Expression) extends Filter {
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Failed  with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1665/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112674727
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CarbonLateDecodeStrategy.scala ---
    @@ -396,43 +402,41 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
         (unrecognizedPredicates ++ unhandledPredicates, translatedFilters)
       }
     
    +
       /**
        * Tries to translate a Catalyst [[Expression]] into data source [[Filter]].
        * @return a `Some[Filter]` if the input [[Expression]] is convertible, otherwise a `None`.
        */
    -  protected[sql] def translateFilter(predicate: Expression, or: Boolean = false): Option[Filter] = {
    +  protected[sql] def translateFilter(predicate: Expression): Option[Filter] = {
         predicate match {
           case or@Or(left, right) =>
     
    -        val leftFilter = translateFilter(left, true)
    -        val rightFilter = translateFilter(right, true)
    +        val leftFilter = translateFilter(left)
    +        val rightFilter = translateFilter(right)
             if (leftFilter.isDefined && rightFilter.isDefined) {
               Some(sources.Or(leftFilter.get, rightFilter.get))
             } else {
               None
             }
     
           case And(left, right) =>
    -        val leftFilter = translateFilter(left, or)
    -        val rightFilter = translateFilter(right, or)
    -        if (or) {
    -          if (leftFilter.isDefined && rightFilter.isDefined) {
    -            (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    -          } else {
    -            None
    -          }
    -        } else {
    -          (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    -        }
    -
    +        (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    --- End diff --
    
    Replaced Back.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112674740
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/optimizer/CarbonFilters.scala ---
    @@ -0,0 +1,380 @@
    +/*
    + * 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
    +
    +import java.text.SimpleDateFormat
    +import java.util.Date
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
    +import org.apache.spark.sql.execution.CastExpressionOptimization
    +import org.apache.spark.sql.optimizer.AttributeReferenceWrapper
    +import org.apache.spark.sql.sources
    +import org.apache.spark.sql.sources.Filter
    +import org.apache.spark.sql.types._
    +
    +import org.apache.carbondata.core.constants.CarbonCommonConstants
    +import org.apache.carbondata.core.keygenerator.directdictionary.timestamp.TimeStampDirectDictionaryGenerator
    +import org.apache.carbondata.core.metadata.datatype.DataType
    +import org.apache.carbondata.core.metadata.schema.table.CarbonTable
    +import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn
    +import org.apache.carbondata.core.scan.expression.{ColumnExpression => CarbonColumnExpression, Expression => CarbonExpression, LiteralExpression => CarbonLiteralExpression}
    +import org.apache.carbondata.core.scan.expression.conditional._
    +import org.apache.carbondata.core.scan.expression.logical.{AndExpression, FalseExpression, OrExpression}
    +import org.apache.carbondata.spark.util.CarbonScalaUtil
    +
    +
    +/**
    + * All filter conversions are done here.
    + */
    +object CarbonFilters {
    +
    +
    +  /**
    +   * Converts data sources filters to carbon filter predicates.
    +   */
    +  def createCarbonFilter(schema: StructType,
    +      predicate: sources.Filter): Option[CarbonExpression] = {
    +    val dataTypeOf = schema.map(f => f.name -> f.dataType).toMap
    +
    +    def createFilter(predicate: sources.Filter): Option[CarbonExpression] = {
    +      predicate match {
    +
    +        case sources.EqualTo(name, value) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.Not(sources.EqualTo(name, value)) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.EqualNullSafe(name, value) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.Not(sources.EqualNullSafe(name, value)) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.GreaterThan(name, value) =>
    +          Some(new GreaterThanExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.LessThan(name, value) =>
    +          Some(new LessThanExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.GreaterThanOrEqual(name, value) =>
    +          Some(new GreaterThanEqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.LessThanOrEqual(name, value) =>
    +          Some(new LessThanEqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.In(name, values) =>
    +          Some(new InExpression(getCarbonExpression(name),
    +            new ListExpression(
    +              convertToJavaList(values.map(f => getCarbonLiteralExpression(name, f)).toList))))
    +        case sources.Not(sources.In(name, values)) =>
    +          Some(new NotInExpression(getCarbonExpression(name),
    +            new ListExpression(
    +              convertToJavaList(values.map(f => getCarbonLiteralExpression(name, f)).toList))))
    +        case sources.IsNull(name) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, null), true))
    +        case sources.IsNotNull(name) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, null), true))
    +        case sources.And(lhs, rhs) =>
    +          (createFilter(lhs) ++ createFilter(rhs)).reduceOption(new AndExpression(_, _))
    +        case sources.Or(lhs, rhs) =>
    +          for {
    +            lhsFilter <- createFilter(lhs)
    +            rhsFilter <- createFilter(rhs)
    +          } yield {
    +            new OrExpression(lhsFilter, rhsFilter)
    +          }
    +        case CastExpr(expr: Expression) =>
    +          Some(transformExpression(expr))
    +        case _ => None
    +      }
    +    }
    +
    +    def getCarbonExpression(name: String) = {
    +      new CarbonColumnExpression(name,
    +        CarbonScalaUtil.convertSparkToCarbonDataType(dataTypeOf(name)))
    +    }
    +
    +    def getCarbonLiteralExpression(name: String, value: Any): CarbonExpression = {
    +      val dataTypeOfAttribute = CarbonScalaUtil.convertSparkToCarbonDataType(dataTypeOf(name))
    +      val dataType = if (Option(value).isDefined
    +                         && dataTypeOfAttribute == DataType.STRING
    +                         && value.isInstanceOf[Double]) {
    +        DataType.DOUBLE
    +      } else {
    +        dataTypeOfAttribute
    +      }
    +      new CarbonLiteralExpression(value, dataType)
    +    }
    +
    +    createFilter(predicate)
    +  }
    +
    +
    +  // Check out which filters can be pushed down to carbon, remaining can be handled in spark layer.
    +  // Mostly dimension filters are only pushed down since it is faster in carbon.
    +  def selectFilters(filters: Seq[Expression],
    +      attrList: java.util.HashSet[AttributeReferenceWrapper],
    +      aliasMap: CarbonAliasDecoderRelation): Unit = {
    +    def translate(expr: Expression, or: Boolean = false): Option[sources.Filter] = {
    +      expr match {
    +        case or@Or(left, right) =>
    +
    +          val leftFilter = translate(left, or = true)
    +          val rightFilter = translate(right, or = true)
    +          if (leftFilter.isDefined && rightFilter.isDefined) {
    +            Some(sources.Or(leftFilter.get, rightFilter.get))
    +          } else {
    +            or.collect {
    +              case attr: AttributeReference =>
    +                attrList.add(AttributeReferenceWrapper(aliasMap.getOrElse(attr, attr)))
    +            }
    +            None
    +          }
    +        case And(left, right) =>
    +          (translate(left) ++ translate(right)).reduceOption(sources.And)
    +        case EqualTo(a: Attribute, Literal(v, t)) =>
    +          Some(sources.EqualTo(a.name, v))
    +        case EqualTo(l@Literal(v, t), a: Attribute) =>
    +          Some(sources.EqualTo(a.name, v))
    +        case c@EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@EqualTo(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@Not(EqualTo(Cast(a: Attribute, _), Literal(v, t))) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@Not(EqualTo(Literal(v, t), Cast(a: Attribute, _))) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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]) =>
    +          val hSet = list.map(e => e.eval(EmptyRow))
    +          Some(sources.Not(sources.In(a.name, hSet.toArray)))
    +        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 c@Not(In(Cast(a: Attribute, _), list)) if !list.exists(!_.isInstanceOf[Literal]) =>
    +          Some(CastExpr(c))
    +        case c@In(Cast(a: Attribute, _), list) if !list.exists(!_.isInstanceOf[Literal]) =>
    +            Some(CastExpr(c))
    +        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 c@GreaterThan(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@GreaterThan(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@LessThan(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@LessThan(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@GreaterThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@GreaterThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@LessThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@LessThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@Cast(a: Attribute, _) =>
    +          Some(CastExpr(c))
    +        case others =>
    +          if (!or) {
    +            others.collect {
    +              case attr: AttributeReference =>
    +                attrList.add(AttributeReferenceWrapper(aliasMap.getOrElse(attr, attr)))
    +            }
    +          }
    +          None
    +      }
    +    }
    +
    +    filters.flatMap(translate(_, false)).toArray
    +  }
    +
    +  def transformExpression(expr: Expression): CarbonExpression = {
    +    expr match {
    +      case Or(left, right)
    +        if (isCarbonSupportedDataTypes(left) && isCarbonSupportedDataTypes(right)) => new
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/incubator-carbondata/pull/805


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112659241
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CarbonLateDecodeStrategy.scala ---
    @@ -396,43 +402,41 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
         (unrecognizedPredicates ++ unhandledPredicates, translatedFilters)
       }
     
    +
       /**
        * Tries to translate a Catalyst [[Expression]] into data source [[Filter]].
        * @return a `Some[Filter]` if the input [[Expression]] is convertible, otherwise a `None`.
        */
    -  protected[sql] def translateFilter(predicate: Expression, or: Boolean = false): Option[Filter] = {
    +  protected[sql] def translateFilter(predicate: Expression): Option[Filter] = {
         predicate match {
           case or@Or(left, right) =>
     
    -        val leftFilter = translateFilter(left, true)
    -        val rightFilter = translateFilter(right, true)
    +        val leftFilter = translateFilter(left)
    +        val rightFilter = translateFilter(right)
             if (leftFilter.isDefined && rightFilter.isDefined) {
               Some(sources.Or(leftFilter.get, rightFilter.get))
             } else {
               None
             }
     
           case And(left, right) =>
    -        val leftFilter = translateFilter(left, or)
    -        val rightFilter = translateFilter(right, or)
    -        if (or) {
    -          if (leftFilter.isDefined && rightFilter.isDefined) {
    -            (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    -          } else {
    -            None
    -          }
    -        } else {
    -          (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    -        }
    -
    +        (translateFilter(left) ++ translateFilter(right)).reduceOption(sources.And)
    --- End diff --
    
    Please check why this code is remmoved


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112659832
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CastExpressionOptimization.scala ---
    @@ -0,0 +1,387 @@
    +/*
    + * 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.spark.sql.execution
    +
    +import java.text.{ParseException, SimpleDateFormat}
    +import java.util
    +import java.util.{Date, Locale, TimeZone}
    +
    +import scala.collection.JavaConverters._
    +
    +import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, EmptyRow, EqualTo, Expression, GreaterThan, GreaterThanOrEqual, In, LessThan, LessThanOrEqual, Literal, Not}
    +import org.apache.spark.sql.sources
    +import org.apache.spark.sql.types.{DoubleType, IntegerType, StringType, TimestampType}
    +
    +import org.apache.carbondata.core.constants.CarbonCommonConstants
    +import org.apache.carbondata.core.util.CarbonProperties
    +import org.apache.carbondata.spark.CastExpr
    +
    +object CastExpressionOptimization {
    +
    +
    +  def typeCastStringToLong(v: Any): Any = {
    +    val parser: SimpleDateFormat = new SimpleDateFormat(CarbonProperties.getInstance
    +      .getProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
    +        CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT))
    +    try {
    +      val value = parser.parse(v.toString).getTime() * 1000L
    +      value
    +    } catch {
    +      case e: ParseException =>
    +        try {
    +          val parsenew: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz")
    +          parsenew.parse(v.toString).getTime() * 1000L
    +        } catch {
    +          case e: ParseException =>
    +            val gmtDay = new SimpleDateFormat("yyyy-MM-dd", Locale.US)
    +            gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))
    +            try {
    +              gmtDay.parse(v.toString).getTime()
    +            } catch {
    +              case e: ParseException =>
    +                v
    +              case e: Exception =>
    +                v
    +            }
    +          case e: Exception =>
    +            v
    +        }
    +      case e: Exception =>
    +        v
    +    }
    +  }
    +
    +  def typeCastStringToLongList(list: Seq[Expression]): Seq[Expression] = {
    +    val tempList = new util.ArrayList[Expression]()
    +    list.foreach { value =>
    +      val output = typeCastStringToLong(value)
    +      if (!output.equals(value)) {
    +        tempList.add(output.asInstanceOf[Expression])
    +      }
    +    }
    +    if (tempList.size() != list.size) {
    +      list
    +    } else {
    +      tempList.asScala
    +    }
    +  }
    +
    +  def typeCastDoubleToIntList(list: Seq[Expression]): Seq[Expression] = {
    +    val tempList = new util.ArrayList[Expression]()
    +    list.foreach { value =>
    +      val output = value.asInstanceOf[Double].toInt
    +      if (value.asInstanceOf[Double].toInt.equals(output)) {
    +        tempList.add(output.asInstanceOf[Expression])
    +      }
    +    }
    +    if (tempList.size() != list.size) {
    +      list
    +    } else {
    +      tempList.asScala
    +    }
    +  }
    +
    +  /**
    +   * This routines tries to apply rules on Cast Filter Predicates and if the rules applied and the
    +   * values can be toss back to native datatypes the cast is removed. Current two rules are applied
    +   * a) Left : timestamp column      Right : String Value
    +   * Input from Spark : cast (col as string) <> 'String Literal'
    +   * Change to        : Column <> 'Long value of Timestamp String'
    +   *
    +   * b) Left : Integer Column        Right : String Value
    +   * Input from Spark : cast (col as double) <> 'Double Literal'
    +   * Change to        : Column <> 'Int value'
    +   *
    +   * @param expr
    +   * @return
    +   */
    +  def checkIfCastCanBeRemove(expr: Expression): Option[sources.Filter] = {
    +    expr match {
    +      case c@EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
    +        if ((a.dataType.isInstanceOf[TimestampType]) && (t.sameType(StringType))) {
    --- End diff --
    
    remove extra parenthesis for all if conditions


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112655868
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CarbonLateDecodeStrategy.scala ---
    @@ -349,7 +353,9 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
           UnknownPartitioning(0)
         }
       }
    -
    +  case class CastExpr1(expr: Expression) extends Filter {
    --- End diff --
    
    Removed


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [WIP] Cast Push Down Optimization

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Success with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1652/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Success with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1700/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112636782
  
    --- Diff: core/src/main/java/org/apache/carbondata/core/scan/expression/CastExpression.java ---
    @@ -0,0 +1,43 @@
    +/*
    + * 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.core.scan.expression;
    +
    +import org.apache.carbondata.core.scan.expression.exception.FilterIllegalMemberException;
    +import org.apache.carbondata.core.scan.expression.exception.FilterUnsupportedException;
    +import org.apache.carbondata.core.scan.filter.intf.ExpressionType;
    +import org.apache.carbondata.core.scan.filter.intf.RowIntf;
    +
    +public class CastExpression extends Expression {
    --- End diff --
    
    Please remove the class as it is no where used


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112656484
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/SparkUnknownExpression.scala ---
    @@ -108,14 +108,28 @@ class SparkUnknownExpression(var sparkExp: SparkExpression)
       }
     
       def getColumnListFromExpressionTree(sparkCurrentExp: SparkExpression,
    -      list: java.util.List[ColumnExpression]): Unit = {
    -    sparkCurrentExp.children.foreach(getColumnListFromExpressionTree(_, list))
    +      lst: java.util.List[ColumnExpression]): Unit = {
    +    sparkCurrentExp match {
    +      case carbonBoundRef: CarbonBoundReference =>
    +        val foundExp = lst.asScala
    +          .find(p => p.getColumnName() == carbonBoundRef.colExp.getColumnName())
    --- End diff --
    
    Style Check is adding back the parenthesis once removed. 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Success with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1705/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Success with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1666/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112639983
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/CarbonBoundReference.scala ---
    @@ -0,0 +1,51 @@
    +/*
    + * 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
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112653137
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CarbonLateDecodeStrategy.scala ---
    @@ -349,7 +353,9 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy {
           UnknownPartitioning(0)
         }
       }
    -
    +  case class CastExpr1(expr: Expression) extends Filter {
    --- End diff --
    
    what is the use of this class


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112674762
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/execution/CastExpressionOptimization.scala ---
    @@ -0,0 +1,387 @@
    +/*
    + * 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.spark.sql.execution
    +
    +import java.text.{ParseException, SimpleDateFormat}
    +import java.util
    +import java.util.{Date, Locale, TimeZone}
    +
    +import scala.collection.JavaConverters._
    +
    +import org.apache.spark.sql.catalyst.expressions.{Attribute, Cast, EmptyRow, EqualTo, Expression, GreaterThan, GreaterThanOrEqual, In, LessThan, LessThanOrEqual, Literal, Not}
    +import org.apache.spark.sql.sources
    +import org.apache.spark.sql.types.{DoubleType, IntegerType, StringType, TimestampType}
    +
    +import org.apache.carbondata.core.constants.CarbonCommonConstants
    +import org.apache.carbondata.core.util.CarbonProperties
    +import org.apache.carbondata.spark.CastExpr
    +
    +object CastExpressionOptimization {
    +
    +
    +  def typeCastStringToLong(v: Any): Any = {
    +    val parser: SimpleDateFormat = new SimpleDateFormat(CarbonProperties.getInstance
    +      .getProperty(CarbonCommonConstants.CARBON_TIMESTAMP_FORMAT,
    +        CarbonCommonConstants.CARBON_TIMESTAMP_DEFAULT_FORMAT))
    +    try {
    +      val value = parser.parse(v.toString).getTime() * 1000L
    +      value
    +    } catch {
    +      case e: ParseException =>
    +        try {
    +          val parsenew: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz")
    +          parsenew.parse(v.toString).getTime() * 1000L
    +        } catch {
    +          case e: ParseException =>
    +            val gmtDay = new SimpleDateFormat("yyyy-MM-dd", Locale.US)
    +            gmtDay.setTimeZone(TimeZone.getTimeZone("GMT"))
    +            try {
    +              gmtDay.parse(v.toString).getTime()
    +            } catch {
    +              case e: ParseException =>
    +                v
    +              case e: Exception =>
    +                v
    +            }
    +          case e: Exception =>
    +            v
    +        }
    +      case e: Exception =>
    +        v
    +    }
    +  }
    +
    +  def typeCastStringToLongList(list: Seq[Expression]): Seq[Expression] = {
    +    val tempList = new util.ArrayList[Expression]()
    +    list.foreach { value =>
    +      val output = typeCastStringToLong(value)
    +      if (!output.equals(value)) {
    +        tempList.add(output.asInstanceOf[Expression])
    +      }
    +    }
    +    if (tempList.size() != list.size) {
    +      list
    +    } else {
    +      tempList.asScala
    +    }
    +  }
    +
    +  def typeCastDoubleToIntList(list: Seq[Expression]): Seq[Expression] = {
    +    val tempList = new util.ArrayList[Expression]()
    +    list.foreach { value =>
    +      val output = value.asInstanceOf[Double].toInt
    +      if (value.asInstanceOf[Double].toInt.equals(output)) {
    +        tempList.add(output.asInstanceOf[Expression])
    +      }
    +    }
    +    if (tempList.size() != list.size) {
    +      list
    +    } else {
    +      tempList.asScala
    +    }
    +  }
    +
    +  /**
    +   * This routines tries to apply rules on Cast Filter Predicates and if the rules applied and the
    +   * values can be toss back to native datatypes the cast is removed. Current two rules are applied
    +   * a) Left : timestamp column      Right : String Value
    +   * Input from Spark : cast (col as string) <> 'String Literal'
    +   * Change to        : Column <> 'Long value of Timestamp String'
    +   *
    +   * b) Left : Integer Column        Right : String Value
    +   * Input from Spark : cast (col as double) <> 'Double Literal'
    +   * Change to        : Column <> 'Int value'
    +   *
    +   * @param expr
    +   * @return
    +   */
    +  def checkIfCastCanBeRemove(expr: Expression): Option[sources.Filter] = {
    +    expr match {
    +      case c@EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
    +        if ((a.dataType.isInstanceOf[TimestampType]) && (t.sameType(StringType))) {
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112650427
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/SparkUnknownExpression.scala ---
    @@ -108,14 +108,28 @@ class SparkUnknownExpression(var sparkExp: SparkExpression)
       }
     
       def getColumnListFromExpressionTree(sparkCurrentExp: SparkExpression,
    -      list: java.util.List[ColumnExpression]): Unit = {
    -    sparkCurrentExp.children.foreach(getColumnListFromExpressionTree(_, list))
    +      lst: java.util.List[ColumnExpression]): Unit = {
    +    sparkCurrentExp match {
    +      case carbonBoundRef: CarbonBoundReference =>
    +        val foundExp = lst.asScala
    +          .find(p => p.getColumnName() == carbonBoundRef.colExp.getColumnName())
    --- End diff --
    
    remove parenthesis of methods, not necessory


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112638463
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/CarbonBoundReference.scala ---
    @@ -0,0 +1,51 @@
    +/*
    + * 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
    --- End diff --
    
    package does not match the directory structure


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112659542
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/optimizer/CarbonFilters.scala ---
    @@ -0,0 +1,380 @@
    +/*
    + * 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
    +
    +import java.text.SimpleDateFormat
    +import java.util.Date
    +
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.spark.sql.catalyst.expressions._
    +import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
    +import org.apache.spark.sql.execution.CastExpressionOptimization
    +import org.apache.spark.sql.optimizer.AttributeReferenceWrapper
    +import org.apache.spark.sql.sources
    +import org.apache.spark.sql.sources.Filter
    +import org.apache.spark.sql.types._
    +
    +import org.apache.carbondata.core.constants.CarbonCommonConstants
    +import org.apache.carbondata.core.keygenerator.directdictionary.timestamp.TimeStampDirectDictionaryGenerator
    +import org.apache.carbondata.core.metadata.datatype.DataType
    +import org.apache.carbondata.core.metadata.schema.table.CarbonTable
    +import org.apache.carbondata.core.metadata.schema.table.column.CarbonColumn
    +import org.apache.carbondata.core.scan.expression.{ColumnExpression => CarbonColumnExpression, Expression => CarbonExpression, LiteralExpression => CarbonLiteralExpression}
    +import org.apache.carbondata.core.scan.expression.conditional._
    +import org.apache.carbondata.core.scan.expression.logical.{AndExpression, FalseExpression, OrExpression}
    +import org.apache.carbondata.spark.util.CarbonScalaUtil
    +
    +
    +/**
    + * All filter conversions are done here.
    + */
    +object CarbonFilters {
    +
    +
    +  /**
    +   * Converts data sources filters to carbon filter predicates.
    +   */
    +  def createCarbonFilter(schema: StructType,
    +      predicate: sources.Filter): Option[CarbonExpression] = {
    +    val dataTypeOf = schema.map(f => f.name -> f.dataType).toMap
    +
    +    def createFilter(predicate: sources.Filter): Option[CarbonExpression] = {
    +      predicate match {
    +
    +        case sources.EqualTo(name, value) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.Not(sources.EqualTo(name, value)) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.EqualNullSafe(name, value) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.Not(sources.EqualNullSafe(name, value)) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.GreaterThan(name, value) =>
    +          Some(new GreaterThanExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.LessThan(name, value) =>
    +          Some(new LessThanExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.GreaterThanOrEqual(name, value) =>
    +          Some(new GreaterThanEqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.LessThanOrEqual(name, value) =>
    +          Some(new LessThanEqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, value)))
    +        case sources.In(name, values) =>
    +          Some(new InExpression(getCarbonExpression(name),
    +            new ListExpression(
    +              convertToJavaList(values.map(f => getCarbonLiteralExpression(name, f)).toList))))
    +        case sources.Not(sources.In(name, values)) =>
    +          Some(new NotInExpression(getCarbonExpression(name),
    +            new ListExpression(
    +              convertToJavaList(values.map(f => getCarbonLiteralExpression(name, f)).toList))))
    +        case sources.IsNull(name) =>
    +          Some(new EqualToExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, null), true))
    +        case sources.IsNotNull(name) =>
    +          Some(new NotEqualsExpression(getCarbonExpression(name),
    +            getCarbonLiteralExpression(name, null), true))
    +        case sources.And(lhs, rhs) =>
    +          (createFilter(lhs) ++ createFilter(rhs)).reduceOption(new AndExpression(_, _))
    +        case sources.Or(lhs, rhs) =>
    +          for {
    +            lhsFilter <- createFilter(lhs)
    +            rhsFilter <- createFilter(rhs)
    +          } yield {
    +            new OrExpression(lhsFilter, rhsFilter)
    +          }
    +        case CastExpr(expr: Expression) =>
    +          Some(transformExpression(expr))
    +        case _ => None
    +      }
    +    }
    +
    +    def getCarbonExpression(name: String) = {
    +      new CarbonColumnExpression(name,
    +        CarbonScalaUtil.convertSparkToCarbonDataType(dataTypeOf(name)))
    +    }
    +
    +    def getCarbonLiteralExpression(name: String, value: Any): CarbonExpression = {
    +      val dataTypeOfAttribute = CarbonScalaUtil.convertSparkToCarbonDataType(dataTypeOf(name))
    +      val dataType = if (Option(value).isDefined
    +                         && dataTypeOfAttribute == DataType.STRING
    +                         && value.isInstanceOf[Double]) {
    +        DataType.DOUBLE
    +      } else {
    +        dataTypeOfAttribute
    +      }
    +      new CarbonLiteralExpression(value, dataType)
    +    }
    +
    +    createFilter(predicate)
    +  }
    +
    +
    +  // Check out which filters can be pushed down to carbon, remaining can be handled in spark layer.
    +  // Mostly dimension filters are only pushed down since it is faster in carbon.
    +  def selectFilters(filters: Seq[Expression],
    +      attrList: java.util.HashSet[AttributeReferenceWrapper],
    +      aliasMap: CarbonAliasDecoderRelation): Unit = {
    +    def translate(expr: Expression, or: Boolean = false): Option[sources.Filter] = {
    +      expr match {
    +        case or@Or(left, right) =>
    +
    +          val leftFilter = translate(left, or = true)
    +          val rightFilter = translate(right, or = true)
    +          if (leftFilter.isDefined && rightFilter.isDefined) {
    +            Some(sources.Or(leftFilter.get, rightFilter.get))
    +          } else {
    +            or.collect {
    +              case attr: AttributeReference =>
    +                attrList.add(AttributeReferenceWrapper(aliasMap.getOrElse(attr, attr)))
    +            }
    +            None
    +          }
    +        case And(left, right) =>
    +          (translate(left) ++ translate(right)).reduceOption(sources.And)
    +        case EqualTo(a: Attribute, Literal(v, t)) =>
    +          Some(sources.EqualTo(a.name, v))
    +        case EqualTo(l@Literal(v, t), a: Attribute) =>
    +          Some(sources.EqualTo(a.name, v))
    +        case c@EqualTo(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@EqualTo(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@Not(EqualTo(Cast(a: Attribute, _), Literal(v, t))) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@Not(EqualTo(Literal(v, t), Cast(a: Attribute, _))) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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]) =>
    +          val hSet = list.map(e => e.eval(EmptyRow))
    +          Some(sources.Not(sources.In(a.name, hSet.toArray)))
    +        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 c@Not(In(Cast(a: Attribute, _), list)) if !list.exists(!_.isInstanceOf[Literal]) =>
    +          Some(CastExpr(c))
    +        case c@In(Cast(a: Attribute, _), list) if !list.exists(!_.isInstanceOf[Literal]) =>
    +            Some(CastExpr(c))
    +        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 c@GreaterThan(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@GreaterThan(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@LessThan(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@LessThan(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@GreaterThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@GreaterThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        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 c@LessThanOrEqual(Cast(a: Attribute, _), Literal(v, t)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@LessThanOrEqual(Literal(v, t), Cast(a: Attribute, _)) =>
    +          CastExpressionOptimization.checkIfCastCanBeRemove(c)
    +        case c@Cast(a: Attribute, _) =>
    +          Some(CastExpr(c))
    +        case others =>
    +          if (!or) {
    +            others.collect {
    +              case attr: AttributeReference =>
    +                attrList.add(AttributeReferenceWrapper(aliasMap.getOrElse(attr, attr)))
    +            }
    +          }
    +          None
    +      }
    +    }
    +
    +    filters.flatMap(translate(_, false)).toArray
    +  }
    +
    +  def transformExpression(expr: Expression): CarbonExpression = {
    +    expr match {
    +      case Or(left, right)
    +        if (isCarbonSupportedDataTypes(left) && isCarbonSupportedDataTypes(right)) => new
    --- End diff --
    
    remove unnecessary extra parenthesis in if conditions for all cases.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Failed  with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1701/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Success with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1742/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by sounakr <gi...@git.apache.org>.
Github user sounakr commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112640121
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/CarbonBoundReference.scala ---
    @@ -0,0 +1,51 @@
    +/*
    + * 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
    --- End diff --
    
    Done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata issue #805: [CARBONDATA-934] Cast Filter Expression Pus...

Posted by CarbonDataQA <gi...@git.apache.org>.
Github user CarbonDataQA commented on the issue:

    https://github.com/apache/incubator-carbondata/pull/805
  
    Build Failed  with Spark 1.6.2, Please check CI http://136.243.101.176:8080/job/ApacheCarbonPRBuilder/1664/



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-carbondata pull request #805: [CARBONDATA-934] Cast Filter Express...

Posted by ravipesala <gi...@git.apache.org>.
Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/incubator-carbondata/pull/805#discussion_r112650054
  
    --- Diff: integration/spark2/src/main/scala/org/apache/spark/sql/SparkUnknownExpression.scala ---
    @@ -15,7 +15,7 @@
      * limitations under the License.
      */
     
    -package org.apache.spark.sql
    +package org.apache.carbondata.spark
    --- End diff --
    
    Package name and directory name does not match


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---