You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by twalthr <gi...@git.apache.org> on 2016/02/13 12:42:04 UTC

[GitHub] flink pull request: [FLINK-3226] Casting support for arithmetic op...

GitHub user twalthr opened a pull request:

    https://github.com/apache/flink/pull/1634

    [FLINK-3226] Casting support for arithmetic operators

    This PR implements auto-casting for numeric arithmetic operators.
    
    @fhueske @vasia mergable?

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

    $ git pull https://github.com/twalthr/flink ArithmeticCasting

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

    https://github.com/apache/flink/pull/1634.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 #1634
    
----
commit fd783341b93d7a7ffba11c827a317adb35248ea0
Author: twalthr <tw...@apache.org>
Date:   2016-02-13T11:38:12Z

    [FLINK-3226] Casting support for arithmetic operators

----


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

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

    https://github.com/apache/flink/pull/1634


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

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

    https://github.com/apache/flink/pull/1634#discussion_r52924026
  
    --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/OperatorCodeGen.scala ---
    @@ -18,20 +18,56 @@
     package org.apache.flink.api.table.codegen
     
     import org.apache.flink.api.common.typeinfo.BasicTypeInfo.BOOLEAN_TYPE_INFO
    -import org.apache.flink.api.common.typeinfo.TypeInformation
    +import org.apache.flink.api.common.typeinfo.{NumericTypeInfo, TypeInformation}
     import org.apache.flink.api.table.codegen.CodeGenUtils._
     
     object OperatorCodeGen {
     
    -   def generateArithmeticOperator(
    +  def generateArithmeticOperator(
           operator: String,
           nullCheck: Boolean,
           resultType: TypeInformation[_],
           left: GeneratedExpression,
           right: GeneratedExpression)
         : GeneratedExpression = {
    -    generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +    // String arithmetic // TODO rework
    +    if (isString(left)) {
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
           (leftTerm, rightTerm) => s"$leftTerm $operator $rightTerm"
    +      }
    +    }
    +    // Numeric arithmetic
    +    else if (isNumeric(left) && isNumeric(right)) {
    +      val leftType = left.resultType.asInstanceOf[NumericTypeInfo[_]]
    +      val rightType = right.resultType.asInstanceOf[NumericTypeInfo[_]]
    +
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +      (leftTerm, rightTerm) =>
    +        // insert auto casting for "narrowing primitive conversions"
    +        if (leftType != rightType) {
    +          // leftType can not be casted to rightType automatically -> narrow
    +          if (!leftType.shouldAutocastTo(rightType)) {
    --- End diff --
    
    Wouldn't this downcast `double` to `short` given an expression: `double + short`?


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

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

    https://github.com/apache/flink/pull/1634#discussion_r52921997
  
    --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/OperatorCodeGen.scala ---
    @@ -18,20 +18,56 @@
     package org.apache.flink.api.table.codegen
     
     import org.apache.flink.api.common.typeinfo.BasicTypeInfo.BOOLEAN_TYPE_INFO
    -import org.apache.flink.api.common.typeinfo.TypeInformation
    +import org.apache.flink.api.common.typeinfo.{NumericTypeInfo, TypeInformation}
     import org.apache.flink.api.table.codegen.CodeGenUtils._
     
     object OperatorCodeGen {
     
    -   def generateArithmeticOperator(
    +  def generateArithmeticOperator(
           operator: String,
           nullCheck: Boolean,
           resultType: TypeInformation[_],
           left: GeneratedExpression,
           right: GeneratedExpression)
         : GeneratedExpression = {
    -    generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +    // String arithmetic // TODO rework
    +    if (isString(left)) {
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
           (leftTerm, rightTerm) => s"$leftTerm $operator $rightTerm"
    +      }
    +    }
    +    // Numeric arithmetic
    +    else if (isNumeric(left) && isNumeric(right)) {
    +      val leftType = left.resultType.asInstanceOf[NumericTypeInfo[_]]
    +      val rightType = right.resultType.asInstanceOf[NumericTypeInfo[_]]
    +
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +      (leftTerm, rightTerm) =>
    +        // insert auto casting for "narrowing primitive conversions"
    +        if (leftType != rightType) {
    --- End diff --
    
    Shouldn't we compare and cast to the result type?


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:

    https://github.com/apache/flink/pull/1634#issuecomment-184672115
  
    Merged, please close @twalthr 


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the pull request:

    https://github.com/apache/flink/pull/1634#issuecomment-184581189
  
    Thanks for the update. LGTM, merging this


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

Posted by twalthr <gi...@git.apache.org>.
Github user twalthr commented on the pull request:

    https://github.com/apache/flink/pull/1634#issuecomment-184412261
  
    @fhueske you were totally right. I reworked the casting again.


---
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] flink pull request: [FLINK-3226] Casting support for arithmetic op...

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

    https://github.com/apache/flink/pull/1634#discussion_r52925323
  
    --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/OperatorCodeGen.scala ---
    @@ -18,20 +18,56 @@
     package org.apache.flink.api.table.codegen
     
     import org.apache.flink.api.common.typeinfo.BasicTypeInfo.BOOLEAN_TYPE_INFO
    -import org.apache.flink.api.common.typeinfo.TypeInformation
    +import org.apache.flink.api.common.typeinfo.{NumericTypeInfo, TypeInformation}
     import org.apache.flink.api.table.codegen.CodeGenUtils._
     
     object OperatorCodeGen {
     
    -   def generateArithmeticOperator(
    +  def generateArithmeticOperator(
           operator: String,
           nullCheck: Boolean,
           resultType: TypeInformation[_],
           left: GeneratedExpression,
           right: GeneratedExpression)
         : GeneratedExpression = {
    -    generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +    // String arithmetic // TODO rework
    +    if (isString(left)) {
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
           (leftTerm, rightTerm) => s"$leftTerm $operator $rightTerm"
    +      }
    +    }
    +    // Numeric arithmetic
    +    else if (isNumeric(left) && isNumeric(right)) {
    +      val leftType = left.resultType.asInstanceOf[NumericTypeInfo[_]]
    +      val rightType = right.resultType.asInstanceOf[NumericTypeInfo[_]]
    +
    +      generateOperatorIfNotNull(nullCheck, resultType, left, right) {
    +      (leftTerm, rightTerm) =>
    +        // insert auto casting for "narrowing primitive conversions"
    +        if (leftType != rightType) {
    +          // leftType can not be casted to rightType automatically -> narrow
    +          if (!leftType.shouldAutocastTo(rightType)) {
    --- End diff --
    
    Just checked, double is actually downcasted.
    The following test fails:
    ```
    val t = env.fromElements((100000.0d: Double, 1: Byte)).toTable.select('_1 + '_2)
    val expected = "100001.0"
    ```


---
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.
---