title: Join two dataframe based on a condition using Spark / Java
body: <p>I have 3 dataframes on spark : dataframe1 , dataframe2 and dataframe3 .</p>
  <p>I want to join  dataframe1 with an other dataframe based on a condition.</p>
  <p>I use the following code :</p>
  <pre><code>Dataset &lt;Row&gt; df= dataframe1.filter(when(col(&quot;diffDate&quot;).lt(3888),dataframe1.join(dataframe2,
              dataframe2.col(&quot;id_device&quot;).equalTo(dataframe1.col(&quot;id_device&quot;)).
              and(dataframe2.col(&quot;id_vehicule&quot;).equalTo(dataframe1.col(&quot;id_vehicule&quot;))).
              and(dataframe2.col(&quot;tracking_time&quot;).lt(dataframe1.col(&quot;tracking_time&quot;)))).orderBy(dataframe2.col(&quot;tracking_time&quot;).desc())).
                     otherwise(dataframe1.join(dataframe3,
                     dataframe3.col(&quot;id_device&quot;).equalTo(dataframe1.col(&quot;id_device&quot;)).
                             and(dataframe3.col(&quot;id_vehicule&quot;).equalTo(dataframe1.col(&quot;id_vehicule&quot;))).
                             and(dataframe3.col(&quot;tracking_time&quot;).lt(dataframe1.col(&quot;tracking_time&quot;)))).orderBy(dataframe3.col(&quot;tracking_time&quot;).desc())));
  </code></pre>
  <p>But I get this exception</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.RuntimeException: Unsupported literal type class org.apache.spark.sql.Dataset
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>Input dataframes :</p>
  <p>dataframe1</p>
  <pre><code>+-----------+-------------+-------------+-------------+
  | diffDate  |id_device    |id_vehicule  |tracking_time|
  +-----------+-------------+-------------+-------------+
  |222        |1            |5            |2020-05-30   |          
  |4700       |8            |9            |2019-03-01   |
  +-----------+-------------+-------------+-------------+
  </code></pre>
  <p>dataframe2</p>
  <pre><code>+-----------+-------------+-------------+-------------+
  |id_device  |id_vehicule  |tracking_time|longitude    |
  +-----------+-------------+-------------+-------------+
  |1          |5            |2020-05-12   | 33.21111    |       
  |8          |9            |2019-03-01   |20.2222      |
  +-----------+-------------+-------------+-------------+
  </code></pre>
  <p>dataframe3</p>
  <pre><code>+-----------+-------------+-------------+-------------+
  |id_device  |id_vehicule  |tracking_time|latitude     |
  +-----------+-------------+-------------+-------------+
  |1          |5            |2020-05-12   | 40.333      |       
  |8          |9            |2019-02-28   |2.00000      |
  +-----------+-------------+-------------+-------------+
  </code></pre>
  <p>when diffDate &lt; 3888</p>
  <pre><code>+-----------+-------------+-------------+-------------+-----------+-------------+-------------+------------+
  | diffDate  |id_device    |id_vehicule  |tracking_time|id_device  |id_vehicule  |tracking_time|longitude|
  +-----------+-------------+-------------+-------------+ +-----------+-------------+-------------+-------------+
  |222        |1            |5            |2020-05-30   | 1          |5            |2020-05-12   | 33.21111    |       
  -----------+--------------+---------------+----------+----------+--------+-----------+--------------+-----------+         
  </code></pre>
  <p>when diffDate &gt; 3888</p>
  <pre><code> +-----------+-------------+-------------+-------------+-----------+-------------+-------------+------------+
  | diffDate  |id_device    |id_vehicule  |tracking_time|id_device  |id_vehicule  |tracking_time|latitude|
  +-----------+-------------+-------------+-------------+ +-----------+-------------+-------------+-------------+
  |4700        |9            |5            |2019-03-01   | 8          |9            |2019-02-28   | 2.00000    |       
  -----------+--------------+---------------+----------+----------+--------+-----------+--------------+-----------+         
  </code></pre>
  <p>I need your help</p>
  <p>Thank you.</p>