You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ku...@apache.org on 2019/02/28 10:10:22 UTC

[flink] branch master updated: [FLINK-11714][table-planner-blink] Add cost model for both batch and streaming

This is an automated email from the ASF dual-hosted git repository.

kurt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ce0609  [FLINK-11714][table-planner-blink] Add cost model for both batch and streaming
0ce0609 is described below

commit 0ce0609cb100f066f5f1c4b359341681c27efb0f
Author: godfreyhe <go...@163.com>
AuthorDate: Tue Feb 26 16:44:59 2019 +0800

    [FLINK-11714][table-planner-blink] Add cost model for both batch and streaming
    
    This closes #7836
---
 .../apache/flink/table/plan/cost/FlinkCost.scala   | 305 +++++++++++++++++++++
 .../flink/table/plan/cost/FlinkCostBase.scala      |  37 +++
 .../flink/table/plan/cost/FlinkCostFactory.scala   |  49 ++++
 .../table/plan/cost/FlinkCostFactoryBase.scala     |  35 +++
 .../flink/table/plan/cost/FlinkCostTest.scala      | 187 +++++++++++++
 5 files changed, 613 insertions(+)

diff --git a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCost.scala b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCost.scala
new file mode 100644
index 0000000..177f825
--- /dev/null
+++ b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCost.scala
@@ -0,0 +1,305 @@
+/*
+ * 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.table.plan.cost
+
+import org.apache.calcite.plan.{RelOptCost, RelOptUtil}
+import org.apache.calcite.runtime.Utilities
+
+/**
+  * This class is based on Apache Calcite's [[org.apache.calcite.plan.volcano.VolcanoCost]]
+  * and has an adapted cost comparison method `isLe(other: RelOptCost)`
+  * that takes io, cpu, network and memory into account.
+  */
+class FlinkCost(
+    val rowCount: Double,
+    val cpu: Double,
+    val io: Double,
+    val network: Double,
+    val memory: Double)
+  extends FlinkCostBase {
+
+  /**
+    * @return number of rows processed; this should not be confused with the
+    *         row count produced by a relational expression
+    *         ({ @link org.apache.calcite.rel.RelNode#estimateRowCount})
+    */
+  override def getRows: Double = rowCount
+
+  /**
+    * @return usage of CPU resources
+    */
+  override def getCpu: Double = cpu
+
+  /**
+    * @return usage of I/O resources
+    */
+  override def getIo: Double = io
+
+  /**
+    * @return usage of network resources
+    */
+  override def getNetwork: Double = network
+
+  /**
+    * @return usage of memory resources
+    */
+  override def getMemory: Double = memory
+
+  /**
+    * @return true iff this cost represents an expression that hasn't actually
+    *         been implemented (e.g. a pure relational algebra expression) or can't
+    *         actually be implemented, e.g. a transfer of data between two disconnected
+    *         sites
+    */
+  override def isInfinite: Boolean = {
+    (this eq FlinkCost.Infinity) ||
+      (this.rowCount == Double.PositiveInfinity) ||
+      (this.cpu == Double.PositiveInfinity) ||
+      (this.io == Double.PositiveInfinity) ||
+      (this.network == Double.PositiveInfinity) ||
+      (this.memory == Double.PositiveInfinity)
+  }
+
+  /**
+    * Compares this to another cost.
+    *
+    * @param other another cost
+    * @return true iff this is exactly equal to other cost
+    */
+  override def equals(other: RelOptCost): Boolean = {
+    (this eq other) ||
+      other.isInstanceOf[FlinkCost] &&
+        (this.rowCount == other.asInstanceOf[FlinkCost].rowCount) &&
+        (this.cpu == other.asInstanceOf[FlinkCost].cpu) &&
+        (this.io == other.asInstanceOf[FlinkCost].io) &&
+        (this.network == other.asInstanceOf[FlinkCost].network) &&
+        (this.memory == other.asInstanceOf[FlinkCost].memory)
+  }
+
+  /**
+    * Compares this to another cost, allowing for slight roundoff errors.
+    *
+    * @param other another cost
+    * @return true iff this is the same as the other cost within a roundoff
+    *         margin of error
+    */
+  override def isEqWithEpsilon(other: RelOptCost): Boolean = {
+    if (!other.isInstanceOf[FlinkCost]) {
+      return false
+    }
+    val that: FlinkCost = other.asInstanceOf[FlinkCost]
+    (this eq that) ||
+      ((Math.abs(this.rowCount - that.rowCount) < RelOptUtil.EPSILON) &&
+        (Math.abs(this.cpu - that.cpu) < RelOptUtil.EPSILON) &&
+        (Math.abs(this.io - that.io) < RelOptUtil.EPSILON) &&
+        (Math.abs(this.network - that.network) < RelOptUtil.EPSILON) &&
+        (Math.abs(this.memory - that.memory) < RelOptUtil.EPSILON))
+  }
+
+  /**
+    * Compares this to another cost.
+    *
+    * <p>NOTES:
+    * * The optimization goal is to use minimal resources now, so the comparison order of factors
+    * is:
+    * * 1. first compare CPU. Each operator will use CPU, so we think it's the most important
+    * factor.
+    * * 2.then compare MEMORY, NETWORK and IO as a normalized value. Comparison order of them is
+    * * not easy to decide, so convert them to CPU cost by different ratio.
+    * * 3.finally compare ROWS. ROWS has been counted when calculating other factory.
+    * * e.g. CPU of Sort = nLogN(ROWS) * number of sort keys,
+    * * CPU of Filter = ROWS * condition cost on a row.
+    *
+    * @param other another cost
+    * @return true iff this is less than or equal to other cost
+    */
+  override def isLe(other: RelOptCost): Boolean = {
+    val that: FlinkCost = other.asInstanceOf[FlinkCost]
+    if (this eq that) {
+      return true
+    }
+
+    val cost1 = normalizeCost(this.memory, this.network, this.io)
+    val cost2 = normalizeCost(that.memory, that.network, that.io)
+    (this.cpu < that.cpu) ||
+      (this.cpu == that.cpu && cost1 < cost2) ||
+      (this.cpu == that.cpu && cost1 == cost2 && this.rowCount <= that.rowCount)
+  }
+
+  private def normalizeCost(memory: Double, network: Double, io: Double): Double = {
+    memory * FlinkCost.MEMORY_TO_CPU_RATIO +
+      network * FlinkCost.NETWORK_TO_CPU_RATIO +
+      io * FlinkCost.IO_TO_CPU_RATIO
+  }
+
+  /**
+    * Compares this to another cost.
+    *
+    * @param other another cost
+    * @return true iff this is strictly less than other cost
+    */
+  override def isLt(other: RelOptCost): Boolean = isLe(other) && !this.equals(other)
+
+  /**
+    * Adds another cost to this.
+    *
+    * @param other another cost
+    * @return sum of this and other cost
+    */
+  override def plus(other: RelOptCost): RelOptCost = {
+    val that: FlinkCost = other.asInstanceOf[FlinkCost]
+    if ((this eq FlinkCost.Infinity) || (that eq FlinkCost.Infinity)) {
+      return FlinkCost.Infinity
+    }
+    new FlinkCost(
+      this.rowCount + that.rowCount,
+      this.cpu + that.cpu,
+      this.io + that.io,
+      this.network + that.network,
+      this.memory + that.memory)
+  }
+
+  /**
+    * Subtracts another cost from this.
+    *
+    * @param other another cost
+    * @return difference between this and other cost
+    */
+  override def minus(other: RelOptCost): RelOptCost = {
+    if (this eq FlinkCost.Infinity) {
+      return this
+    }
+    val that: FlinkCost = other.asInstanceOf[FlinkCost]
+    new FlinkCost(
+      this.rowCount - that.rowCount,
+      this.cpu - that.cpu,
+      this.io - that.io,
+      this.network - that.network,
+      this.memory - that.memory)
+  }
+
+  /**
+    * Multiplies this cost by a scalar factor.
+    *
+    * @param factor scalar factor
+    * @return scalar product of this and factor
+    */
+  override def multiplyBy(factor: Double): RelOptCost = {
+    if (this eq FlinkCost.Infinity) {
+      return this
+    }
+    new FlinkCost(
+      rowCount * factor,
+      cpu * factor,
+      io * factor,
+      network * factor,
+      memory * factor)
+  }
+
+  /**
+    * Computes the ratio between this cost and another cost.
+    *
+    * <p>divideBy is the inverse of multiplyBy(double). For any
+    * finite, non-zero cost and factor f, <code>
+    * cost.divideBy(cost.multiplyBy(f))</code> yields <code>1 / f</code>.
+    *
+    * @param cost Other cost
+    * @return Ratio between costs
+    */
+  override def divideBy(cost: RelOptCost): Double = {
+    val that: FlinkCost = cost.asInstanceOf[FlinkCost]
+    var d: Double = 1.0
+    var n: Double = 0.0
+    if ((this.rowCount != 0) && !this.rowCount.isInfinite &&
+      (that.rowCount != 0) && !that.rowCount.isInfinite) {
+      d *= this.rowCount / that.rowCount
+      n += 1
+    }
+    if ((this.cpu != 0) && !this.cpu.isInfinite && (that.cpu != 0) && !that.cpu.isInfinite) {
+      d *= this.cpu / that.cpu
+      n += 1
+    }
+    if ((this.io != 0) && !this.io.isInfinite && (that.io != 0) && !that.io.isInfinite) {
+      d *= this.io / that.io
+      n += 1
+    }
+    if ((this.network != 0) && !this.network.isInfinite &&
+      (that.network != 0) && !that.network.isInfinite) {
+      d *= this.network / that.network
+      n += 1
+    }
+    if ((this.memory != 0) && !this.memory.isInfinite &&
+      (that.memory != 0) && !that.memory.isInfinite) {
+      d *= this.memory / that.memory
+      n += 1
+    }
+    if (n == 0) {
+      return 1.0
+    }
+    Math.pow(d, 1 / n)
+  }
+
+  override def hashCode(): Int = {
+    Utilities.hashCode(rowCount) +
+      Utilities.hashCode(cpu) +
+      Utilities.hashCode(io) +
+      Utilities.hashCode(network) +
+      Utilities.hashCode(memory)
+  }
+
+  override def toString: String = {
+    s"{$rowCount rows, $cpu cpu, $io io, $network network, $memory memory}"
+  }
+
+}
+
+object FlinkCost {
+
+  private[flink] val Infinity = new FlinkCost(
+    Double.PositiveInfinity,
+    Double.PositiveInfinity,
+    Double.PositiveInfinity,
+    Double.PositiveInfinity,
+    Double.PositiveInfinity) {
+    override def toString: String = "{inf}"
+  }
+
+  private[flink] val Huge = new FlinkCost(
+    Double.MaxValue, Double.MaxValue, Double.MaxValue, Double.MaxValue, Double.MaxValue) {
+    override def toString: String = "{huge}"
+  }
+
+  private[flink] val Zero = new FlinkCost(0.0, 0.0, 0.0, 0.0, 0.0) {
+    override def toString: String = "{0}"
+  }
+
+  private[flink] val Tiny = new FlinkCost(1.0, 1.0, 0.0, 0.0, 0.0) {
+    override def toString = "{tiny}"
+  }
+
+  val FACTORY: FlinkCostFactory = new FlinkCostFactory
+
+  // The ratio to convert memory cost into CPU cost.
+  val MEMORY_TO_CPU_RATIO = 1.0
+  // The ratio to convert io cost into CPU cost.
+  val IO_TO_CPU_RATIO = 2.0
+  // The ratio to convert network cost into CPU cost.
+  val NETWORK_TO_CPU_RATIO = 4.0
+
+}
diff --git a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostBase.scala b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostBase.scala
new file mode 100644
index 0000000..3c68190
--- /dev/null
+++ b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostBase.scala
@@ -0,0 +1,37 @@
+/*
+ * 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.table.plan.cost
+
+import org.apache.calcite.plan.RelOptCost
+
+/**
+  * A [[RelOptCost]] that extends network cost and memory cost.
+  */
+trait FlinkCostBase extends RelOptCost {
+
+  /**
+    * @return usage of network resources
+    */
+  def getNetwork: Double
+
+  /**
+    * @return usage of memory resources
+    */
+  def getMemory: Double
+}
diff --git a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactory.scala b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactory.scala
new file mode 100644
index 0000000..3296a41
--- /dev/null
+++ b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactory.scala
@@ -0,0 +1,49 @@
+/*
+ * 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.table.plan.cost
+
+import org.apache.calcite.plan.RelOptCost
+
+/**
+  * This class is based on Apache Calcite's [[org.apache.calcite.plan.volcano.VolcanoCost#Factory]].
+  */
+class FlinkCostFactory extends FlinkCostFactoryBase {
+
+  override def makeCost(
+      rowCount: Double,
+      cpu: Double,
+      io: Double,
+      network: Double,
+      memory: Double): RelOptCost = {
+    new FlinkCost(rowCount, cpu, io, network, memory)
+  }
+
+  override def makeCost(dRows: Double, dCpu: Double, dIo: Double): RelOptCost = {
+    new FlinkCost(dRows, dCpu, dIo, 0.0, 0.0)
+  }
+
+  override def makeHugeCost: RelOptCost = FlinkCost.Huge
+
+  override def makeInfiniteCost: RelOptCost = FlinkCost.Infinity
+
+  override def makeTinyCost: RelOptCost = FlinkCost.Tiny
+
+  override def makeZeroCost: RelOptCost = FlinkCost.Zero
+
+}
diff --git a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactoryBase.scala b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactoryBase.scala
new file mode 100644
index 0000000..8cf5ae1
--- /dev/null
+++ b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/plan/cost/FlinkCostFactoryBase.scala
@@ -0,0 +1,35 @@
+/*
+ * 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.table.plan.cost
+
+import org.apache.calcite.plan.{RelOptCost, RelOptCostFactory}
+
+/**
+  * A [[RelOptCostFactory]] that adds makeCost methods with network and memory parameters.
+  */
+trait FlinkCostFactoryBase extends RelOptCostFactory {
+
+  def makeCost(
+    rowCount: Double,
+    cpu: Double,
+    io: Double,
+    network: Double,
+    memory: Double): RelOptCost
+
+}
diff --git a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/plan/cost/FlinkCostTest.scala b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/plan/cost/FlinkCostTest.scala
new file mode 100644
index 0000000..cad9a4e
--- /dev/null
+++ b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/plan/cost/FlinkCostTest.scala
@@ -0,0 +1,187 @@
+/*
+ * 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.table.plan.cost
+
+import org.apache.calcite.plan.RelOptCostImpl
+import org.junit.Assert._
+import org.junit.Test
+
+class FlinkCostTest {
+
+  @Test
+  def testIsInfinite(): Unit = {
+    assertFalse(FlinkCost.Zero.isInfinite)
+    assertFalse(FlinkCost.Tiny.isInfinite)
+    assertFalse(FlinkCost.Huge.isInfinite)
+    assertTrue(FlinkCost.Infinity.isInfinite)
+    assertTrue(new FlinkCost(100.0, 1000.0, Double.PositiveInfinity, 0.0, 0.0).isInfinite)
+  }
+
+  @Test
+  def testEquals(): Unit = {
+    assertTrue(FlinkCost.Tiny.equals(FlinkCost.Tiny))
+    assertFalse(FlinkCost.Tiny.equals(RelOptCostImpl.FACTORY.makeTinyCost()))
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    val cost2 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    assertTrue(cost1.equals(cost2))
+
+    val cost3 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, Double.PositiveInfinity, 0.0, 0.0)
+    val cost4 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, Double.PositiveInfinity, 0.0, 0.0)
+    assertTrue(cost3.equals(cost4))
+
+    val cost5 = new FlinkCost(Double.PositiveInfinity, Double.PositiveInfinity,
+      Double.PositiveInfinity, Double.PositiveInfinity, Double.PositiveInfinity)
+    assertTrue(FlinkCost.Infinity.equals(cost5))
+
+    val cost6 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 1.0E-6)
+    assertFalse(cost1.equals(cost6))
+  }
+
+  @Test
+  def testIsEqWithEpsilon(): Unit = {
+    assertTrue(FlinkCost.Tiny.isEqWithEpsilon(FlinkCost.Tiny))
+    assertFalse(FlinkCost.Tiny.isEqWithEpsilon(RelOptCostImpl.FACTORY.makeTinyCost()))
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.123456, 1000.12345, 1.0E-6, 500.123, 1.0E-7)
+    val cost2 = FlinkCost.FACTORY.makeCost(100.123457, 1000.12346, 1.0E-7, 500.123, 1.0E-6)
+    assertTrue(cost1.isEqWithEpsilon(cost2))
+
+    val cost3 = FlinkCost.FACTORY.makeCost(100.123456, 1000.12347, 1.0E-6, 500.123, 1.0E-7)
+    assertFalse(cost1.isEqWithEpsilon(cost3))
+  }
+
+  @Test
+  def testIsLe(): Unit = {
+    assertTrue(FlinkCost.Tiny.isLe(FlinkCost.Tiny))
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    val cost2 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    assertTrue(cost1.isLe(cost2))
+    assertTrue(cost2.isLe(cost1))
+
+    val cost3 = FlinkCost.FACTORY.makeCost(100.0, 1200.0, 0.0, 500.0, 0.0)
+    assertTrue(cost1.isLe(cost3))
+    assertFalse(cost3.isLe(cost1))
+
+    val cost4 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 50.0, 300.0, 200.0)
+    assertFalse(cost1.isLe(cost4))
+    assertTrue(cost4.isLe(cost1))
+  }
+
+  @Test(expected = classOf[ClassCastException])
+  def testIsLe_WithDiffCost(): Unit = {
+    FlinkCost.Tiny.isLe(RelOptCostImpl.FACTORY.makeTinyCost())
+  }
+
+  @Test
+  def testIsLt(): Unit = {
+    assertFalse(FlinkCost.Tiny.isLt(FlinkCost.Tiny))
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    val cost2 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    assertFalse(cost1.isLt(cost2))
+    assertFalse(cost2.isLt(cost1))
+
+    val cost3 = FlinkCost.FACTORY.makeCost(100.0, 1200.0, 0.0, 500.0, 0.0)
+    assertTrue(cost1.isLt(cost3))
+    assertFalse(cost3.isLt(cost1))
+
+    val cost4 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 50.0, 300.0, 200.0)
+    assertFalse(cost1.isLt(cost4))
+    assertTrue(cost4.isLt(cost1))
+  }
+
+  @Test(expected = classOf[ClassCastException])
+  def testIsLt_WithDiffCost(): Unit = {
+    FlinkCost.Tiny.isLt(RelOptCostImpl.FACTORY.makeTinyCost())
+  }
+
+  @Test
+  def testPlus(): Unit = {
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    assertTrue(FlinkCost.Infinity.equals(cost1.plus(FlinkCost.Infinity)))
+    assertTrue(FlinkCost.Infinity.equals(FlinkCost.Infinity.plus(cost1)))
+
+    val cost2 = FlinkCost.FACTORY.makeCost(500.0, 3000.0, 27.0, 100.1234567, 0.0)
+    val expectedCost1 = FlinkCost.FACTORY.makeCost(600.0, 4000.0, 27.0, 600.1234567, 0.0)
+    assertTrue(expectedCost1.equals(cost1.plus(cost2)))
+
+    val cost3 = FlinkCost.FACTORY.makeCost(
+      500.0, 3000.0, 27.0, 100.1234567, Double.PositiveInfinity)
+    val expectedCost2 = FlinkCost.FACTORY.makeCost(
+      600.0, 4000.0, 27.0, 600.1234567, Double.PositiveInfinity)
+    assertTrue(expectedCost2.equals(cost1.plus(cost3)))
+  }
+
+  @Test(expected = classOf[ClassCastException])
+  def testPlus_WithDiffCost(): Unit = {
+    FlinkCost.Tiny.plus(RelOptCostImpl.FACTORY.makeTinyCost())
+  }
+
+  @Test
+  def testMinus(): Unit = {
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    assertTrue(FlinkCost.Infinity.equals(FlinkCost.Infinity.minus(cost1)))
+
+    val expectedCost1 = FlinkCost.FACTORY.makeCost(
+      Double.NegativeInfinity, Double.NegativeInfinity, Double.NegativeInfinity,
+      Double.NegativeInfinity, Double.NegativeInfinity)
+    assertTrue(expectedCost1.equals(cost1.minus(FlinkCost.Infinity)))
+
+    val cost2 = FlinkCost.FACTORY.makeCost(500.0, 3000.0, 27.0, 100.1234567, 0.0)
+    val expectedCost2 = FlinkCost.FACTORY.makeCost(-400.0, -2000.0, -27.0, 399.8765433, 0.0)
+    assertTrue(expectedCost2.equals(cost1.minus(cost2)))
+
+    val expectedCost3 = FlinkCost.FACTORY.makeCost(400.0, 2000.0, 27.0, -399.8765433, 0.0)
+    assertTrue(expectedCost3.equals(cost2.minus(cost1)))
+  }
+
+  @Test(expected = classOf[ClassCastException])
+  def testMinus_WithDiffCost(): Unit = {
+    FlinkCost.Tiny.minus(RelOptCostImpl.FACTORY.makeTinyCost())
+  }
+
+  @Test
+  def testMultiplyBy(): Unit = {
+    assertTrue(FlinkCost.Infinity.equals(FlinkCost.Infinity.multiplyBy(10.0)))
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, 0.0)
+    val expectedCost1 = FlinkCost.FACTORY.makeCost(1000.0, 10000.0, 0.0, 5000.0, 0.0)
+    assertTrue(expectedCost1.equals(cost1.multiplyBy(10.0)))
+
+    val cost2 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 0.0, 500.0, Double.PositiveInfinity)
+    val expectedCost2 = FlinkCost.FACTORY.makeCost(
+      1000.0, 10000.0, 0.0, 5000.0, Double.PositiveInfinity)
+    assertTrue(expectedCost2.equals(cost2.multiplyBy(10.0)))
+  }
+
+  @Test
+  def testDivideBy(): Unit = {
+    assertEquals(1.0, FlinkCost.Infinity.divideBy(FlinkCost.Infinity), 1e-5)
+    assertEquals(1.0, FlinkCost.Tiny.divideBy(FlinkCost.Infinity), 1e-5)
+    assertEquals(1.0, FlinkCost.Tiny.divideBy(FlinkCost.Zero), 1e-5)
+
+    val cost1 = FlinkCost.FACTORY.makeCost(100.0, 1000.0, 200.0, 500.0, 600.0)
+    val cost2 = FlinkCost.FACTORY.makeCost(50.0, 100.0, 40.0, 25.0, 200.0)
+    assertEquals(Math.pow(6000.0, 1 / 5.0), cost1.divideBy(cost2), 1e-5)
+    assertEquals(Math.pow(1 / 6000.0, 1 / 5.0), cost2.divideBy(cost1), 1e-5)
+  }
+
+}