解决办法:把udaf自定义函数中的decimal位数增大一点即可
@F.pandas_udf("decimal(12,10)")
def udf_lx(qx:pd.Series,lx:pd.Series) -> decimal:
decimal.getcontext().rounding = "ROUND_HALF_DOWN"
temp1 = Decimal(0)
temp2 = Decimal(0)
for i in range(0,len(qx)):
if i == 0:
temp1 = Decimal(lx[i])
temp2 = Decimal(qx[i])
else:
temp1 = (temp1 * (1-temp2)).quantize(Decimal('0.0000000000'))
temp2 = qx[i]
return temp1
spark.udf.register('udf_lx',udf_lx)