title: Apache Flink - enable join ordering
body: <p>I have noticed that Apache Flink does not optimise the order in which the tables are joined.  At the moment, it keeps the user-specified join order (basically,  it takes the the query literally). I suppose that Apache Calcite can optimise the order of joins but for some reason these rules are not in use in Apache Flink.</p>
  
  <p>If, for example, we have two tables '<strong>R</strong>' and '<strong>S</strong>' </p>
  
  <pre><code>private val tableEnv: BatchTableEnvironment = TableEnvironment.getTableEnvironment(env)
  private val fileNumber = 1
  tableEnv.registerTableSource("R", getDataSourceR(fileNumber))
  tableEnv.registerTableSource("S", getDataSourceS(fileNumber))
  private val r = tableEnv.scan("R")
  private val s = tableEnv.scan("S")
  </code></pre>
  
  <p>and we suppose that '<strong>S</strong>' is empty and we want to join these tables in two ways:</p>
  
  <pre><code>val tableOne = r.as("x1, x2").join(r.as("x3, x4")).where("x2 === x3").select("x1, x4")
          .join(s.as("x5, x6")).where("x4 === x5 ").select("x1, x6")
  
  
  val tableTwo = s.as("x1, x2").join(r.as("x3, x4")).where("x2 === x3").select("x1, x4")
            .join(r.as("x5, x6")).where("x4 === x5 ").select("x1, x6")
  </code></pre>
  
  <p>If we want to count the number of rows in <strong>tableOne</strong> and in <strong>tableTwo</strong> the result will be zero in both cases. 
  The problem is that evaluating <strong>tableOne</strong> will take much longer than evaluating <strong>tableTwo</strong>.</p>
  
  <p>Is there any way by which we can automatically optimise the order of how the join are executed, or even enable a possible plan cost operation by adding some statistics? How can these statistic can be added?</p>
  
  <p>In the documentation at this <a href="https://ci.apache.org/projects/flink/flink-docs-master/dev/table/common.html#query-optimization" rel="nofollow noreferrer">link</a> it is written that maybe it is necessary to change the Table environment CalciteConfig but it is not clear to me how to do it. </p>
  
  <p>Please help.</p>