Skip to content

Commit

Permalink
fixes to mysql date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
drizk1 committed Mar 31, 2024
1 parent 4a446e7 commit 083299b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/mysql_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ function expr_to_sql_mysql(expr, sq; from_summarize::Bool)
return "EXTRACT(MINUTE FROM " * string(a) * ")"
elseif @capture(x, second(a_))
return "EXTRACT(SECOND FROM " * string(a) * ")"
elseif @capture(x, floordate(unit_, time_column_))
return :(DATE_TRUNC($unit, $time_column))
elseif @capture(x, floordate(time_column_, unit_))
# Call floordate_to_sql with the captured variables
return floordate_to_sql(unit, time_column)
elseif @capture(x, replacemissing(column_, replacement_value_))
return :(COALESCE($column, $replacement_value))
elseif @capture(x, missingif(column_, value_to_replace_))
Expand All @@ -120,7 +121,7 @@ function expr_to_sql_mysql(expr, sq; from_summarize::Bool)
return "CAST(" * string(column) * " AS DECIMAL)"
elseif x.args[1] == :as_integer && length(x.args) == 2
column = x.args[2]
return "CAST(" * string(column) * " AS INT)"
return "CAST(" * string(column) * " AS UNSIGNED)"
elseif x.args[1] == :as_string && length(x.args) == 2
column = x.args[2]
return "CAST(" * string(column) * " AS STRING)"
Expand All @@ -139,3 +140,23 @@ function expr_to_sql_mysql(expr, sq; from_summarize::Bool)
return x
end
end


function floordate_to_sql(unit::String, time_column::Symbol)
sql_format = ""
if unit == "minute"
sql_format = "%Y-%m-%d %H:%i:00"
elseif unit == "hour"
sql_format = "%Y-%m-%d %H:00:00"
elseif unit == "day"
sql_format = "%Y-%m-%d 00:00:00"
elseif unit == "month"
sql_format = "%Y-%m-01 00:00:00"
elseif unit == "year"
sql_format = "%Y-01-01 00:00:00"
else
throw(ArgumentError("Unsupported unit: $unit"))
end
# Construct the SQL string
return "DATE_FORMAT($(string(time_column)), '$sql_format')"
end
2 changes: 1 addition & 1 deletion src/postgresparsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function expr_to_sql_postgres(expr, sq; from_summarize::Bool)
return "EXTRACT(MINUTE FROM " * string(a) * ")"
elseif @capture(x, second(a_))
return "EXTRACT(SECOND FROM " * string(a) * ")"
elseif @capture(x, floordate(unit_, time_column_))
elseif @capture(x, floordate(time_column_, unit_))
return :(DATE_TRUNC($unit, $time_column))
elseif @capture(x, replacemissing(column_, replacement_value_))
return :(COALESCE($column, $replacement_value))
Expand Down

0 comments on commit 083299b

Please sign in to comment.