cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Error when accessing 'num_inserted_rows' in Spark SQL (DBR 15.4 LTS)

Buranapat
New Contributor II

Hello Databricks Community,

I've encountered an issue while trying to capture the number of rows inserted after executing an SQL insert statement in Databricks (DBR 15.4 LTS). My code is attempting to access the number of inserted rows as follows:

 

rows_insert = spark.sql(insert_statement).first()['num_inserted_rows']

 

However, I am receiving the following error:

 

TypeError: 'NoneType' object is not subscriptable

 

Upon further investigation, I noticed that in the SQL execution result, the fields num_affected_rows and num_inserted_rows are shown, but no rows are actually returned. This can be seen in the screenshots I've attached:

  • The first image shows the result of my SQL insert, which displays the columns num_affected_rows and num_inserted_rows but with no actual rows returned.
    Buranapat_4-1727751428815.png

     

  • The second image shows the subsequent Python error when I try to access num_inserted_rows from the result.
    Buranapat_3-1727750986067.png

Has anyone else faced a similar issue? I would greatly appreciate any insights or suggestions on how to work around this problem.

Thanks in advance for your help!

3 REPLIES 3

MuthuLakshmi
Databricks Employee
Databricks Employee

@Buranapat Can you please try this check as a workaround?
result = spark.sql(insert_statement).first()
if result is not None and 'num_inserted_rows' in result:
          num_inserted_rows = result['num_inserted_rows']
else:
          num_inserted_rows = 0

Thank you for the suggestion! The workaround seems like a helpful way to quickly check insert success, though I’ve noticed that it sometimes returns None even when records appear to be successfully inserted. Do you know if this is a known issue, or if there are any plans to improve this behavior in future updates?

Thanks again for your help!

GavinReeves3
New Contributor II

Hey all, commenting here for my own experience.

I am also experiencing the same issue but the problem seems to be Liquid Clustering that is removing all metadata from an inserted rows statement. Whenever I insert rows, it always returns None and some of my error handling fails. Is this a known bug?

@MuthuLakshmi 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group