如何使用spark中的“and”将空条件应用于sql select?

q8l4jmvw  于 2021-07-12  发布在  Spark
关注(0)|答案(1)|浏览(298)

我有一个uuidconditionset,当if条件错误时,我想对我的select语句应用一个空字符串(或者忽略这个uuidconditionset),但是我得到了这个错误。如何解决这个问题?

mismatched input 'FROM' expecting <EOF>(line 10, pos 3)

这是选择

(SELECT
 item,
 amount,
 date
 from my_table
 where record_type = 'myType'
 and ( date_format(date, "yyyy-MM-dd") >= '2020-02-27'
 and  date_format(date, "yyyy-MM-dd") <= '2020-02-28' )
 and ()
var UuidConditionSet = ""
var UuidCondition = Seq.empty[String]
    if(!UuidList.mkString.isEmpty) {
    UuidCondition = for {
        Uuid <- UuidList
        UuidConditionSet = s"${SQLColumnHelper.EVENT_INFO_STRUCT_NAME}.${SQLColumnHelper.UUID} = '".concat(eventUuid).concat("'")
        } yield UuidConditionSet
    UuidConditionSet = UuidCondition.reduce(_.concat(" or ").concat(_))
}

    s"""SELECT
        | ${SQLColumnHelper.STRUCT_NAME_ITEM},
        | ${SQLColumnHelper.STRUCT_NAME_AMOUNT},
        | ${SQLColumnHelper.DATE}
        | from ${sqlTableHelper.TABLE}
        | where ${SQLColumnHelper.EVENT_INFO_STRUCT_NAME} = '${RECORD_TYPE}'
        | and ( date_format(${SQLColumnHelper.DATE}, "${Constant.STAY_DATE_FORMAT}") >= '${stayDateRangeTuple._1}'
        | and  date_format(${SQLColumnHelper.DATE}, "${Constant.STAY_DATE_FORMAT}") <= '${stayDateRangeTuple._2}' )
        | and ($UuidConditionSet)
vmdwslir

vmdwslir1#

您可以在列表中使用模式匹配 UuidList 检查大小并在列表为空时返回空字符串。另外,你可以使用 IN 而不是多个 OR 他在这儿。
试试这个:

val UuidCondition = UuidList match {
  case l if (l.size > 0) => {
    l.map(u => s"'$u'").mkString(
        s"and ${SQLColumnHelper.EVENT_INFO_STRUCT_NAME}.${SQLColumnHelper.UUID} in (",
        ",",
        ")"
      )
  }
  case _ => ""
}

s"""SELECT
  | ${SQLColumnHelper.STRUCT_NAME_ITEM},
  | ${SQLColumnHelper.STRUCT_NAME_AMOUNT},
  | ${SQLColumnHelper.DATE}
  | from ${sqlTableHelper.TABLE}
  | where ${SQLColumnHelper.EVENT_INFO_STRUCT_NAME} = '${RECORD_TYPE}'
  | and date_format(${SQLColumnHelper.DATE}, "${Constant.STAY_DATE_FORMAT}") >= '${stayDateRangeTuple._1}'
  | and date_format(${SQLColumnHelper.DATE}, "${Constant.STAY_DATE_FORMAT}") <= '${stayDateRangeTuple._2}'
  | $UuidCondition
"""

相关问题