共用方式為


try_mod

傳回 dividend/divisor 後的餘數。 若 為 0, divisor 其結果總是空。 支援 Spark Connect。

關於對應的 Databricks SQL 函式,請參見 try_mod 函數

語法

from pyspark.databricks.sql import functions as dbf

dbf.try_mod(left=<left>, right=<right>)

參數

參數 類型 Description
left pyspark.sql.Column or column name 股息
right pyspark.sql.Column or column name 除子

範例

from pyspark.databricks.sql import functions as dbf
spark.createDataFrame(
    [(6000, 15), (3, 2), (1234, 0)], ["a", "b"]
).select("*", dbf.try_mod("a", "b")).show()
+----+---+-------------+
|   a|  b|try_mod(a, b)|
+----+---+-------------+
|6000| 15|            0|
|   3|  2|            1|
|1234|  0|         NULL|
+----+---+-------------+

from pyspark.databricks.sql import functions as dbf
origin = spark.conf.get("spark.sql.ansi.enabled")
spark.conf.set("spark.sql.ansi.enabled", "true")
try:
    spark.range(1).select(dbf.try_mod("id", dbf.lit(0))).show()
finally:
    spark.conf.set("spark.sql.ansi.enabled", origin)
+--------------+
|try_mod(id, 0)|
+--------------+
|          NULL|
+--------------+