Pyspark 中的未定义函数 bool_and

Undefined function bool_and in Pyspark

我正在使用下面的代码:

df_logical = df_parcial.groupBy("customer_id", "person_id").agg(
when(expr("bool_and(is_online_store)"), "Online")
.when(expr("bool_and(!is_online_store)"), "Offline")
.when(expr("bool_and(is_online_store)").isNull(), None)
.otherwise("Hybrid").alias("type_person"))

我的规则如下:

但是当我去上传代码到production时,出现如下错误:

Undefined function: 'bool_and'. This function is neither a registered temporary function nor a permanent function registered in the database 'default'.; line 1 pos 0

如何解决这个错误?

Table原文:

customer PersonId is_online_store
afabd2d2 4 true
afabd2d2 8 true
afabd2d2 3 true
afabd2d2 2 false
afabd2d2 4 false

Table 应该是:

customer PersonId type_person
afabd2d2 4 Hybrid
afabd2d2 8 Online
afabd2d2 3 Online
afabd2d2 2 Offline

你得到那个错误是因为 bool_and 函数只在 Spark 3 之后可用。你可以使用这样的条件计数来实现相同的目的:

df_logical = df_parcial.groupBy("customer", "PersonId").agg(
    F.when(
        F.count(F.when(F.col("is_online_store") == "true", 1)) == F.count("*"), "Online"
    ).when(
        F.count(F.when(F.col("is_online_store") == "false", 1)) == F.count("*"), "Offline"
    ).otherwise("Hybrid").alias("New_Column")
)