cancel
Showing results for 
Search instead for 
Did you mean: 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results for 
Search instead for 
Did you mean: 

SparkException: Job aborted due to stage failure when attempting to run grid_pointascellid

kll
New Contributor III

I am attempting to apply Mosaic's `grid_pointascellid` method on a spark dataframe with `lat`, `lon` columns.

```
import pyspark.sql.functions as F

# Create a Spark DataFrame with a lat and lon column
df = spark.createDataFrame([
("point1", 10.0, 20.0),
("point2", 30.0, 40.0),
("point3", 50.0, 60.0),
], ["name", "lat", "lon"])

# Print the results of the DataFrame
df.show()

df1 = df.withColumn("id", grid_pointascellid(st_point(df["lon"], df["lat"]), lit(7)))
df1.show()

```

The schema of the dataframe is:

```

|-- name: string (nullable = true)
|-- lat: decimal(9,6) (nullable = true)
|-- lng: decimal(9,6) (nullable = true)

```

Exception:


org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 251.0 failed 4 times, most recent failure: Lost task 0.3 in stage 251.0 (TID 15016) (10.138.221.80 executor 50): java.lang.ClassCastException: org.apache.spark.sql.types.Decimal cannot be cast to java.lang.Double

https://databrickslabs.github.io/mosaic/api/spatial-indexing.html?highlight=grid_boundary#grid-point...

4 REPLIES 4

Lakshay
Databricks Employee
Databricks Employee

At which step are you getting the error? Also, can you share the code for "grid_pointascellid" function to understand what it is doing?

kll
New Contributor III

@Lakshay 

On this step, when I attempt to apply the method: 

```

df1 = df.withColumn("id", grid_pointascellid(st_point(df["lon"], df["lat"]), lit(7)))
df1.show()

```

Anonymous
Not applicable

Hi @kll 

Hope you are well. Just wanted to see if you were able to find an answer to your question and would you like to mark an answer as best? It would be really helpful for the other members too.

Cheers!

Tharun-Kumar
Databricks Employee
Databricks Employee

@kll 

This error appears because the function grid_pointascellid expects a Double type column and a Decimal column type was provided as input.

To overcome this, before you apply the grid_pointascellid, I would recommend casting the columns lat and lon.

df1 = df.withColumn("lat", df["lat"].cast("double")).withColumn("lon", df["lon"].cast("double"))

df2 = df1.withColumn("id", grid_pointascellid(st_point(df1["lon"], df1["lat"]), lit(7)))
df2.show()

By performing this explicit cast before calling the mosaic functions, i was able to get the output below.

| name.   | lat.   | lon.   | id                                | 
|point1.   |10.0. |20.0.  |609880166050562047|
|point2.   |30.0  |40.0.  |609448747927076863|
|point3.   |50.0  |60.0.  |608566547836829695|

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now