Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/1981#discussion_r64741369
  
    --- Diff: 
flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/plan/logical/operators.scala
 ---
    @@ -272,23 +272,46 @@ case class Join(
         left.output ++ right.output
       }
     
    +  private object JoinFieldReference {
    +
    +    def apply(
    +      name: String,
    +      resultType: TypeInformation[_],
    +      left: LogicalNode,
    +      right: LogicalNode): JoinFieldReference = {
    +
    +      val joinInputField = left.output.zipWithIndex.find(_._1.name == 
name).map(_._2)
    +                           
.orElse(right.output.zipWithIndex.find(_._1.name == name).map(x => 
left.output.length + x._2))
    +                           .getOrElse(
    +                             throw new NoSuchElementException(s"""Could 
not find field: $name"""))
    +                           .asInstanceOf[Int]
    +
    +      new JoinFieldReference(name, resultType, left, right, joinInputField)
    +    }
    +
    +  }
    +
       private case class JoinFieldReference(
    --- End diff --
    
    I am sorry, I think my last comment was confusing. Actually, I 
misinterpreted the semantics of the `RelBuilder.field(int, int, String)` 
method. I thought it would automatically add the offset of the left input. But 
apparently it doesn't...
    
    How about we change the `JoinFieldReference` to this:
    
    ```
    private case class JoinFieldReference(
          name: String,
          resultType: TypeInformation[_],
          left: LogicalNode,
          right: LogicalNode) extends Attribute {
    
        val isFromLeftInput = left.output.map(_.name).contains(name)
    
        val (indexInInput, indexInJoin) = if (isFromLeftInput) {
            val indexInLeft = left.output.map(_.name).indexOf(name)
            (indexInLeft, indexInLeft)
          } else {
            val indexInRight = right.output.map(_.name).indexOf(name)
            (indexInRight, indexInRight + left.output.length)
          }
    
        override def toString = s"'$name"
    
        override def toRexNode(implicit relBuilder: RelBuilder): RexNode = {
    
          // look up type of field
          val fieldType = relBuilder.field(2, if (isFromLeftInput) 0 else 1, 
name).getType()
          // create a new RexInputRef with index offset
          new RexInputRef(indexInJoin, fieldType)
        }
    
        override def withName(newName: String): Attribute = {
          if (newName == name) {
            this
          } else {
            JoinFieldReference(newName, resultType, left, right)
          }
        }
      }
    ```


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to