Created
October 4, 2026 02:43
-
-
Save rjdudley/e63a25b96bed9cbd2cfa7ee04c7ce934 to your computer and use it in GitHub Desktop.
The silver transformation generated by Genie Code from trips.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pyspark import pipelines as dp | |
| from pyspark.sql import functions as F | |
| @dp.table( | |
| name="silver.nyctaxi.trips_cleaned", | |
| comment="Cleaned NYC taxi trips with padded zip codes; timestamps displayed as Eastern via session timezone", | |
| ) | |
| def trips_cleaned(): | |
| """Silver layer: read raw NYC taxi trips, preserve UTC timestamps as TIMESTAMP_LTZ | |
| (displayed as Eastern via pipeline session timezone), pad zip codes to 5 characters, | |
| and compute trip duration in minutes. Only valid records are included; invalid | |
| records are routed to bronze.nyctaxi.invalid_trips.""" | |
| return ( | |
| spark.readStream.table("samples.nyctaxi.trips") | |
| .withColumn( | |
| "pickup_datetime", | |
| F.col("tpep_pickup_datetime"), | |
| ) | |
| .withColumn( | |
| "dropoff_datetime", | |
| F.col("tpep_dropoff_datetime"), | |
| ) | |
| .withColumn( | |
| "pickup_zip", | |
| F.lpad(F.col("pickup_zip").cast("string"), 5, "0"), | |
| ) | |
| .withColumn( | |
| "dropoff_zip", | |
| F.lpad(F.col("dropoff_zip").cast("string"), 5, "0"), | |
| ) | |
| .withColumn( | |
| "trip_duration_minutes", | |
| (F.unix_timestamp("tpep_dropoff_datetime") - F.unix_timestamp("tpep_pickup_datetime")) / 60.0, | |
| ) | |
| .filter( | |
| F.col("pickup_datetime").isNotNull() | |
| & F.col("dropoff_datetime").isNotNull() | |
| & (F.col("trip_distance") > 0) | |
| & (F.col("fare_amount") >= 0) | |
| ) | |
| .select( | |
| "pickup_datetime", | |
| "dropoff_datetime", | |
| "trip_distance", | |
| "fare_amount", | |
| "pickup_zip", | |
| "dropoff_zip", | |
| "trip_duration_minutes", | |
| ) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment