Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
sharp1FL
Frequent Visitor

spark.sql() ArrayIndexOutOfBoundsException

I have the following Pyspark code that reads a table schema and formats a MERGE statement to perform an upsert.  This works for all of the  30 or so required tables except for 1. On one particular table, I get java.lang.ArrayIndexOutOfBoundsException. 

 

We are on Runtime v 1.3

 

How can I troubleshoot or correct this issue?

Thanks.

 

 

# Get column names from TEMP_TABLE
temp_cols = [f.name for f in df_clean.schema.fields]

# Dynamically build MERGE statement
merge_sql = f"""
MERGE INTO {target_table} AS t
USING {temp_table} AS s
ON t.{primary_key_col} = s.{primary_key_col}  -- join on primary key
WHEN MATCHED THEN UPDATE SET
"""

first_col = 0

for col in temp_cols[0:]:  # Skip the first column used in the join condition
  if (col != primary_key_col 😞
    if( first_col == 0) :
        first_col = 1
    else:
        merge_sql += f"""
            , """
    merge_sql += f"""t.{col} = s.{col}"""

merge_sql += f"""
  WHEN NOT MATCHED THEN INSERT ({', '.join(temp_cols)})
  VALUES ({', '.join(['s.' + col for col in temp_cols])})
   """

print(merge_sql )

merged_df = spark.sql( merge_sql )

display( merged_df )
1 ACCEPTED SOLUTION
v-prasare
Community Support
Community Support

Hi @sharp1FL , 

In this scenario i suggest you to raise a support ticket here. so, that they can assit you in addressing the issue you are facing. please follow below link on how to raise a support ticket:

How to create a Fabric and Power BI Support ticket - Power BI | Microsoft Learn

 

 

Thanks,

Prashanth

 

View solution in original post

4 REPLIES 4
v-prasare
Community Support
Community Support

Hi, We are following up once again regarding your query. Could you please confirm if the issue has been resolved through the support ticket with Microsoft?

If the issue has been resolved, we kindly request you to share the resolution or key insights here to help others in the community. If we don’t hear back, we’ll go ahead and close this thread.

Should you need further assistance in the future, we encourage you to reach out via the Microsoft Fabric Community Forum and create a new thread. We’ll be happy to help.

 

Thank you for your understanding and participation.

v-prasare
Community Support
Community Support

Hi @sharp1FL , 

In this scenario i suggest you to raise a support ticket here. so, that they can assit you in addressing the issue you are facing. please follow below link on how to raise a support ticket:

How to create a Fabric and Power BI Support ticket - Power BI | Microsoft Learn

 

 

Thanks,

Prashanth

 

sharp1FL
Frequent Visitor

Thanks for the reply. I reviewed generated merge_sql. There are no complex data types or syntax errors. I removed each column to try an isolate an issue, to no effect on the error.

v-prasare
Community Support
Community Support

Hi @sharp1FL,

 

  • Print and Review Generated SQL
    Use print(merge_sql) to manually check for SQL syntax errors or invalid column references.

  • Fix Code Syntax Error & Wrap Column Names in Backticks
    Handle special characters: t. \{col}` = s.`{col}``
  • Avoid Complex/Nested Types: MERGE fails with arrays/structs—check using df_clean.printSchema()

  • Check for Duplicate Columns: Ensure column names are unique and clean.

  • Validate Runtime Compatibility

 

 

Thanks,

Prashanth Are

MS Fabric community support

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly and give Kudos if helped you resolve your query

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.