spark过滤器下推

idfiyjo8  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(464)

我有一张小table adm 有一列 x 它只包含10行。现在我想过滤另一个表 big 这是由 y 值来自 adm 使用分区修剪。
在这里的时候

select * from big b 
where b.y = ( select max(a.x) from adm a)

分区过滤器下推工作正常,但不幸的是:

select * from big b
where b.y IN (select a.x from adm a )

结果在 a 以及 b 即使在我使用 IN

btxsgosb

btxsgosb1#

这是因为子查询本身的结果是rdd,所以spark以真正分布式的方式处理它——通过广播和连接——就像处理任何其他列一样,而不一定是分区。
要解决这个问题,您需要分别执行子查询,收集结果并将其格式化为一个在中可用的值 IN 条款。

scala> val ax = spark.sql("select a.x from adm a")
scala> val inclause = ax.as(Encoders.STRING).map(x => "'"+x+"'").collectAsList().asScala.mkString(",")
scala> spark.sql("select * from big b where b.y IN (" + inclause + ")")

(假设 x 以及 y 是字符串。)

相关问题