SPRKPY1019

pyspark.sql.functions.datediff

circle-exclamation

Message: pyspark.sql.functions.datediff has a workaround

Category: Warning.

Description

This issue appears when the tool detects the usage of pyspark.sql.functions.datediffarrow-up-right which has a workaround.

Scenario

Input

In this example we use datediff to calculate the diference in day from 'today' and others dates.

contacts = (contacts
            #days since last event
            .withColumn('daysSinceLastEvent', datediff(lit(today),'lastEvent'))
            #days since deployment
            .withColumn('daysSinceLastDeployment', datediff(lit(today),'lastDeploymentEnd'))
            #days since online training
            .withColumn('daysSinceLastTraining', datediff(lit(today),'lastTraining'))
            #days since last RC login
            .withColumn('daysSinceLastRollCallLogin', datediff(lit(today),'adx_identity_lastsuccessfullogin'))
            #days since last EMS login
            .withColumn('daysSinceLastEMSLogin', datediff(lit(today),'vms_lastuserlogin'))
           )

Output

SMA returns the EWI SPRKPY1019 over the line where datediff is used, so you can use to identify where to fix.

SMA convert pyspark.sql.functions.datediff onto snowflake.snowpark.functions.daydiffarrow-up-right that also calculates the diference in days between two dates.

Recommended fix

datediff(part: string ,end: ColumnOrName, start: ColumnOrName)

Action: Import snowflake.snowpark.functions, which contains an implementation for datediffarrow-up-right function that requires an extra parameter for date time partarrow-up-right and allows more versatility on calculate differences between dates.

Recommendation

Last updated