Skip to content
Snippets Groups Projects
Commit 3e9bbecc authored by Riccardo Boero's avatar Riccardo Boero :innocent:
Browse files

Fixed nothing to NULL MySQL problem with workround trhough missing.

parent f4689a97
No related branches found
No related tags found
No related merge requests found
...@@ -316,7 +316,6 @@ This will insert each row of `df` into the `geographical_features` table in the ...@@ -316,7 +316,6 @@ This will insert each row of `df` into the `geographical_features` table in the
function custom_load(conn::MySQL.Connection, table_name::String, df::DataFrame) function custom_load(conn::MySQL.Connection, table_name::String, df::DataFrame)
# Preprocess DataFrame to ensure data integrity # Preprocess DataFrame to ensure data integrity
preprocess_dataframe!(df) preprocess_dataframe!(df)
println(df)
col_names = join(["`$(name)`" for name in names(df)], ", ") col_names = join(["`$(name)`" for name in names(df)], ", ")
placeholders = join(["?" for _ in 1:length(names(df))], ", ") placeholders = join(["?" for _ in 1:length(names(df))], ", ")
...@@ -324,8 +323,8 @@ function custom_load(conn::MySQL.Connection, table_name::String, df::DataFrame) ...@@ -324,8 +323,8 @@ function custom_load(conn::MySQL.Connection, table_name::String, df::DataFrame)
prepared_stmt = DBInterface.prepare(conn, insert_stmt) prepared_stmt = DBInterface.prepare(conn, insert_stmt)
for row in eachrow(df) for row in eachrow(df)
# Convert row to Tuple, directly handling `nothing` (for NULL handling in SQL) # Convert row to Tuple, directly handling `missing` (for NULL handling in SQL)
values = Tuple([x === nothing ? nothing : x for x in row]) values = Tuple([x === missing ? missing : x for x in row])
DBInterface.execute(prepared_stmt, values) DBInterface.execute(prepared_stmt, values)
end end
end end
...@@ -369,7 +368,7 @@ function preprocess_dataframe!(df::DataFrame) ...@@ -369,7 +368,7 @@ function preprocess_dataframe!(df::DataFrame)
col_type = eltype(df[!, col]) col_type = eltype(df[!, col])
# Convert missing values to nothing for SQL NULL compatibility # Convert missing values to nothing for SQL NULL compatibility
df[!, col] = map(x -> ismissing(x) ? nothing : x, df[!, col]) df[!, col] = map(x -> ismissing(x) ? missing : x, df[!, col])
# Clamp numeric values if range is defined for the column type # Clamp numeric values if range is defined for the column type
if haskey(SQL_RANGES, col_type) if haskey(SQL_RANGES, col_type)
...@@ -379,7 +378,7 @@ function preprocess_dataframe!(df::DataFrame) ...@@ -379,7 +378,7 @@ function preprocess_dataframe!(df::DataFrame)
# Replace NaN and Inf with nothing in numeric columns # Replace NaN and Inf with nothing in numeric columns
if col_type <: AbstractFloat if col_type <: AbstractFloat
df[!, col] = map(x -> isfinite(x) ? x : nothing, df[!, col]) df[!, col] = map(x -> isfinite(x) ? x : missing, df[!, col])
end end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment