flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fhueske <...@git.apache.org>
Subject [GitHub] flink pull request #2169: [FLINK-3943] Add support for EXCEPT operator
Date Wed, 29 Jun 2016 23:36:06 GMT
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2169#discussion_r69047378
  
    --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/plan/nodes/dataset/DataSetMinus.scala
---
    @@ -0,0 +1,106 @@
    +/*
    + * 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.flink.api.table.plan.nodes.dataset
    +
    +import org.apache.calcite.plan.{RelOptCluster, RelOptCost, RelOptPlanner, RelTraitSet}
    +import org.apache.calcite.rel.`type`.RelDataType
    +import org.apache.calcite.rel.metadata.RelMetadataQuery
    +import org.apache.calcite.rel.{BiRel, RelNode, RelWriter}
    +import org.apache.flink.api.common.typeinfo.TypeInformation
    +import org.apache.flink.api.java.DataSet
    +import org.apache.flink.api.table.BatchTableEnvironment
    +
    +import scala.collection.JavaConverters._
    +import scala.collection.JavaConversions._
    +
    +/**
    +  * Flink RelNode which matches along with DataSetOperator.
    +  *
    +  */
    +class DataSetMinus(
    +    cluster: RelOptCluster,
    +    traitSet: RelTraitSet,
    +    left: RelNode,
    +    right: RelNode,
    +    rowType: RelDataType,
    +    all: Boolean)
    +  extends BiRel(cluster, traitSet, left, right)
    +    with DataSetRel {
    +
    +  override def deriveRowType() = rowType
    +
    +  override def copy(traitSet: RelTraitSet, inputs: java.util.List[RelNode]): RelNode
= {
    +    new DataSetMinus(
    +      cluster,
    +      traitSet,
    +      inputs.get(0),
    +      inputs.get(1),
    +      rowType,
    +      all
    +    )
    +  }
    +
    +  override def toString: String = {
    +    s"SetMinus(setMinus: (${rowType.getFieldNames.asScala.toList.mkString(", ")}))"
    +  }
    +
    +  override def explainTerms(pw: RelWriter): RelWriter = {
    +    super.explainTerms(pw).item("setMinus", setMinusSelectionToString)
    +  }
    +
    +  override def computeSelfCost (planner: RelOptPlanner, metadata: RelMetadataQuery):
RelOptCost = {
    +
    +    val children = this.getInputs
    +    val rowCnt = children.foldLeft(0D) { (rows, child) =>
    +      rows + metadata.getRowCount(child)
    +    }
    +
    +    planner.getCostFactory.makeCost(rowCnt, 0, 0)
    +  }
    +
    +  override def translateToPlan(
    +      tableEnv: BatchTableEnvironment,
    +      expectedType: Option[TypeInformation[Any]]): DataSet[Any] = {
    +
    +    var leftDataSet: DataSet[Any] = null
    +    var rightDataSet: DataSet[Any] = null
    +
    +    expectedType match {
    +      case None =>
    +        leftDataSet = left.asInstanceOf[DataSetRel].translateToPlan(tableEnv)
    +        rightDataSet =
    +          right.asInstanceOf[DataSetRel].translateToPlan(tableEnv, Some(leftDataSet.getType))
    +      case _ =>
    +        leftDataSet = left.asInstanceOf[DataSetRel].translateToPlan(tableEnv, expectedType)
    +        rightDataSet = right.asInstanceOf[DataSetRel].translateToPlan(tableEnv, expectedType)
    +    }
    +
    +    val minusRes = leftDataSet.minus(rightDataSet)
    +    if (!all) {
    +      minusRes.distinct()
    --- End diff --
    
    I would like to move the code of `DataSet.minus()` here. However, I think the semantics
of `EXCEPT ALL` are a bit different than in your implementation. It is not simply checking
if there is a match in the second input and forwarding everything if there is none. It basically
removes for each match in the second input one matching record from the first input (see also
the [PostgreSQL docs](https://www.postgresql.org/docs/9.4/static/sql-select.html#SQL-EXCEPT)).
    
    I would be in favor of an implementation that is similar to @wuchong's implementation
of `INTERSECT` / `INTERSECT ALL` in PR #2159.


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

Mime
View raw message