%3CLINGO-SUB%20id%3D%22lingo-sub-1593643%22%20slang%3D%22en-US%22%3EArithmetic%20overflow%20error%20converting%20double%20to%20data%20type%20FLOAT%20due%20to%20NaN%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1593643%22%20slang%3D%22en-US%22%3E%3CP%3ENaN%20stands%20for%20Not%20a%20Number.%20This%20scenario%20was%20a%20customer%20trying%20to%20insert%20a%20parquet%20file%20into%20SQL%2C%20but%20he%20was%20not%20able%20to%20do%20it.%3C%2FP%3E%0A%3CP%3EThat's%20because%20of%20the%20following%20error%3A%20%3CSTRONG%3EE%3C%2FSTRONG%3E%3CSPAN%3E%3CSTRONG%3Error%20converting%20values%20NaN%20or%20Infinity%20to%20type%20'FLOAT'%3C%2FSTRONG%3E.%20NaN%20and%20Infinity%20are%20not%20supported.%20This%20error%20was%20from%20SQL%20DW%20and%20I%20got%20when%20I%20tried%20to%20run%20a%20Select%20with%20openrowset%20pointing%20to%20that%20file.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFor%20a%20Select%20with%20openrowset%20example%2C%20you%20can%20use%20this%20as%20reference%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-synapse-analytics%2Fsynapse-studio-error-while-trying-to-read-data-from-storage%2Fba-p%2F1511965%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-synapse-analytics%2Fsynapse-studio-error-while-trying-to-read-data-from-storage%2Fba-p%2F1511965%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20he%20tried%20to%20insert%20direct%20the%20file%20on%20SQL%20from%20the%20notebook%20the%20error%20was%3A%26nbsp%3B%3CSTRONG%3EHadoopSqlException%3A%20Arithmetic%20overflow%20error%20converting%20double%20to%20data%20type%20FLOAT.'%3C%2FSTRONG%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20summary%20there%20were%20values%20on%20the%20float%20columns%20which%20were%20causing%20the%20errors%20above.%20Those%20values%20where%20NaN%20values.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20example%20bellow%20is%20based%20on%20this%20piece%20of%20documentation%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsynapse-analytics%2Fspark%2Fsynapse-spark-sql-pool-import-export%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsynapse-analytics%2Fspark%2Fsynapse-spark-sql-pool-import-export%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EI%20added%26nbsp%3B%20some%20customization%20and%20also%20I%20have%20some%20nice%20discussion%20with%20my%20colleague%20Diya%20Mothafar.%20So%20my%20colleague%20mentioned%20to%20do%20the%20same%20using%20Pandas%20which%20also%20valid.%20My%20demo%20will%20not%20use%20Pandas%2C%20but%20again%20it%20also%20does%20the%20job.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20the%20idea%20here%20is%20convert%20those%20%3CSTRONG%3ENaN%20values%20into%20NULL%3C%2FSTRONG%3E%20and%20after%20that%20load%20into%20SQL%20Server%2C%20we%20can%20do%20this%20with%20spark%26nbsp%3B%20notebooks.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFirst%20open%20Synapse%20Studio%20-%26gt%3B%20Notebook%20-%26gt%3BPySpark%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22phytonNotebook.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212762iF4C617A5DF375C97%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22phytonNotebook.png%22%20alt%3D%22phytonNotebook.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFig%201%20PySpark%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%25%25pyspark%0Afrom%20pyspark.sql.functions%20import%20col%2C%20when%0A%0Adata_path%20%3D%20spark.read.load('abfss%3A%2F%2Ffilesystemdatalake%40mystorage.dfs.core.windows.net%2Ftest%2Ffiletest.snappy.parquet'%2C%20format%3D'parquet')%0A%0A%23here%20we%20handle%20the%20NaN%20values%0Adata_path%20%3D%20data_path.replace(float('nan')%2C%20None)%0A%0Adata_path.createOrReplaceTempView(%22pysparkdftemptable%22)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAdd%20a%20Scala%20cell%20into%20the%20Notebook%20to%20add%20some%20magic.%20Note%20the%20table%20will%20be%20created%20with%20the%20JOB%20you%20do%20not%20need%20to%20create%20in%20advance.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22addcel.png%22%20style%3D%22width%3A%20221px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212763i45386144BD791A40%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22addcel.png%22%20alt%3D%22addcel.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFig%202%20Add%20Cell%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%25%25spark%0Aval%20scala_df%20%3D%20spark.sqlContext.sql%20(%22select%20*%20from%20pysparkdftemptable%22)%0A%2F%2Fscala_df.show(100)%0Ascala_df.write.sqlanalytics(%22YourDatabaseName.dbo.PySparkTable%22%2C%20Constants.INTERNAL)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EYour%20new%20cell%20should%20look%20like%20this%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22cell.png%22%20style%3D%22width%3A%20833px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F212764i12B504CFE9BC25AB%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22cell.png%22%20alt%3D%22cell.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EFig%203%20Cell%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EOnce%20the%20Job%20is%20complete.%20You%20can%20check%20the%20results%20by%20opening%20SSMS%20and%20querying%20the%20table.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%3A%20If%20instead%20of%20handling%20the%20NaN%20columns%20you%20want%20to%20filter%20it.%20You%20can%20follow%20the%20example%20bellow%3A%3C%2FP%3E%0A%3CP%3EPySpark%20example%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3Efrom%20pyspark.sql.functions%20import%20isnan%0A%0Adata_path.where%20(isnan(col(%22Column_name%22)))%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThat%20is%20it!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ELiliam%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EUK%20Engineer%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1593643%22%20slang%3D%22en-US%22%3E%3CP%3ENaN%20stands%20for%20Not%20a%20Number.%20This%20scenario%20was%20a%20customer%20trying%20to%20insert%20a%20parquet%20file%20into%20SQL%2C%20but%20he%20was%20not%20able%20to%20do%20it.%3C%2FP%3E%0A%3CP%3EThat's%20because%20of%20the%20following%20error%3A%20%3CSTRONG%3EE%3C%2FSTRONG%3E%3CSPAN%3E%3CSTRONG%3Error%20converting%20values%20NaN%20or%20Infinity%20to%20type%20'FLOAT'%3C%2FSTRONG%3E.%20NaN%20and%20Infinity%20are%20not%20supported.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1593643%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESynapse%20Spark%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESynapse%20Studio%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

NaN stands for Not a Number. This scenario was a customer trying to insert a parquet file into SQL, but he was not able to do it.

That's because of the following error: Error converting values NaN or Infinity to type 'FLOAT'. NaN and Infinity are not supported. This error was from SQL DW and I got when I tried to run a Select with openrowset pointing to that file.

 

For a Select with openrowset example, you can use this as reference: https://techcommunity.microsoft.com/t5/azure-synapse-analytics/synapse-studio-error-while-trying-to-...

 

If he tried to insert direct the file on SQL from the notebook the error was: HadoopSqlException: Arithmetic overflow error converting double to data type FLOAT.'

 

In summary there were values on the float columns which were causing the errors above. Those values where NaN values.

 

The example bellow is based on this piece of documentation:

https://docs.microsoft.com/en-us/azure/synapse-analytics/spark/synapse-spark-sql-pool-import-export

 

I added  some customization and also I have some nice discussion with my colleague Diya Mothafar. So my colleague mentioned to do the same using Pandas which also valid. My demo will not use Pandas, but again it also does the job.

 

So the idea here is convert those NaN values into NULL and after that load into SQL Server, we can do this with spark  notebooks.

 

First open Synapse Studio -> Notebook ->PySpark

phytonNotebook.png

Fig 1 PySpark

 

 

 

 

 

 

 

 

 

 

 

%%pyspark
from pyspark.sql.functions import col, when

data_path = spark.read.load('abfss://filesystemdatalake@mystorage.dfs.core.windows.net/test/filetest.snappy.parquet', format='parquet')

#here we handle the NaN values
data_path = data_path.replace(float('nan'), None)

data_path.createOrReplaceTempView("pysparkdftemptable")

 

 

 

 

 

 

 

 

 

 

 

Add a Scala cell into the Notebook to add some magic. Note the table will be created with the JOB you do not need to create in advance.

 

addcel.png

Fig 2 Add Cell

 

 

 

 

 

 

 

 

 

 

%%spark
val scala_df = spark.sqlContext.sql ("select * from pysparkdftemptable")
//scala_df.show(100)
scala_df.write.sqlanalytics("YourDatabaseName.dbo.PySparkTable", Constants.INTERNAL)

 

 

 

 

 

 

 

 

 

 

 

Your new cell should look like this

cell.png

Fig 3 Cell

 

Once the Job is complete. You can check the results by opening SSMS and querying the table.

 

Note: If instead of handling the NaN columns you want to filter it. You can follow the example bellow:

PySpark example:

 

 

 

 

 

 

 

 

 

from pyspark.sql.functions import isnan

data_path.where (isnan(col("Column_name")))

 

 

 

 

 

 

 

 

 

 

That is it!

Liliam 

UK Engineer