title: How to use the Spark SQL broadcast function
body: <p>Spark SQL provides a function <code>broadcast</code> to indicate that the dataset is smaller enough and should be broadcast</p>
  
  <pre><code>def broadcast[T](df: Dataset[T]): Dataset[T] = {
    Dataset[T](df.sparkSession,
        ResolvedHint(df.logicalPlan, HintInfo(broadcast = true)))(df.exprEnc)
  }
  </code></pre>
  
  <p>I know I could use it in the following way:</p>
  
  <pre><code>val a = spark.range(100).as("a")
  val b = spark.range(100).as("b")
  val df = a.join(broadcast(b)).where($"a.id" === $"b.id")
  </code></pre>
  
  <p>I want to use it in the SQL statement like:</p>
  
  <pre><code>spark.range(100).createOrReplaceTempView("a")
  spark.range(100).createOrReplaceTempView("b")
  
  val df = spark.sql("select a.id, c.id from a join broadcast(b)  c on a.id = c.id")
  </code></pre>
  
  <p>But it throws an exception:</p>
  
  <pre><code>cannot resolve '`b`' given input columns: []
  </code></pre>
  
  <p>It looks that SQL treats <code>b</code> like a column? I would ask how I can use the <code>broadcast</code> function in SQL statement.</p>