Skip to content

Commit

Permalink
clickhouse support fixed. (date parsing will need fixing prob)
Browse files Browse the repository at this point in the history
  • Loading branch information
drizk1 committed Apr 3, 2024
1 parent 1c03d36 commit f04a304
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ Supported helper functions include
DuckDB is the default backend.

Switch to Postgres, MySQL, MSSQL, or SQLite using
`set_sql_mode(:postgres)`
`set_sql_mode(:mysql)`
`set_sql_mode(:mssql)`
`set_sql_mode(:lite)`
- `set_sql_mode(:postgres)`
- `set_sql_mode(:mysql)`
- `set_sql_mode(:mssql)`
- `set_sql_mode(:lite)`
- `set_sql_mode(:clickhouse)`

DuckDB support enables:
- directly reading in .parquet, .json, and .csv files paths.
Expand Down
8 changes: 8 additions & 0 deletions src/TBD_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ macro select(sqlquery, exprs...)
let columns = parse_tidy_db($exprs_str, $(esc(sqlquery)).metadata)
columns_str = join(["SELECT ", join([string(column) for column in columns], ", ")])
$(esc(sqlquery)).select = columns_str
#bc of alphabetized return issue this is needed for clickhouse
if current_sql_mode[] == :clickhouse
$(esc(sqlquery)).metadata.current_selxn .= 0
selected_indices = indexin(columns, $(esc(sqlquery)).metadata.name)
$(esc(sqlquery)).metadata.current_selxn[selected_indices[.!isnothing.(selected_indices)]] .= 1
end
end

$(esc(sqlquery))
end
end
Expand Down Expand Up @@ -627,6 +634,7 @@ macro collect(sqlquery)
return quote
# Extract the database connection from the SQLQuery object
db = $(esc(sqlquery)).db
sq = $(esc(sqlquery))
# Finalize the query to get the SQL string
final_query = finalize_query($(esc(sqlquery)))
df_result = DataFrame()
Expand Down
11 changes: 4 additions & 7 deletions src/TidierDB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import ClickHouse

@reexport using DataFrames: DataFrame
@reexport using Chain
@reexport using SQLite: DB, load!

import DuckDB: open as duckdb_open
import DuckDB: connect as duckdb_connect
Expand Down Expand Up @@ -57,7 +56,7 @@ function expr_to_sql(expr, sq; from_summarize::Bool = false)
elseif current_sql_mode[] == :mssql
return expr_to_sql_mssql(expr, sq; from_summarize=from_summarize)
elseif current_sql_mode[] == :clickhouse
return expr_to_sql_mssql(expr, sq; from_summarize=from_summarize)
return expr_to_sql_clickhouse(expr, sq; from_summarize=from_summarize)
else
error("Unsupported SQL mode: $(current_sql_mode[])")
end
Expand Down Expand Up @@ -135,7 +134,7 @@ function finalize_query(sqlquery::SQLQuery)
"FROM )" => ")" , "SELECT SELECT " => "SELECT ", "SELECT SELECT " => "SELECT ", "DISTINCT SELECT " => "DISTINCT ",
"SELECT SELECT SELECT " => "SELECT ", "PARTITION BY GROUP BY" => "PARTITION BY", "GROUP BY GROUP BY" => "GROUP BY", "HAVING HAVING" => "HAVING", )

if current_sql_mode[] == :postgres || current_sql_mode[] == :duckdb || current_sql_mode[] == :mysql || current_sql_mode[] == :mssql
if current_sql_mode[] == :postgres || current_sql_mode[] == :duckdb || current_sql_mode[] == :mysql || current_sql_mode[] == :mssql || current_sql_mode[] == :clickhouse
complete_query = replace(complete_query, "\"" => "'", "==" => "=")
end

Expand Down Expand Up @@ -203,7 +202,6 @@ function get_table_metadata(conn::MySQL.Connection, table_name::String)
end

# MSSQL

function get_table_metadata(conn::ODBC.Connection, table_name::String)
# Query to get column names and types from INFORMATION_SCHEMA
query = """
Expand All @@ -212,15 +210,14 @@ function get_table_metadata(conn::ODBC.Connection, table_name::String)
WHERE table_name = '$table_name'
ORDER BY ordinal_position;
"""

result = DBInterface.execute(conn, query) |> DataFrame
#result[!, :DATA_TYPE] = map(x -> String(x), result.DATA_TYPE)
result[!, :current_selxn] .= 1
return select(result, 1 => :name, 2 => :type, :current_selxn)
return select(result, :column_name => :name, :data_type => :type, :current_selxn)
end

# ClickHouse

function get_table_metadata(conn::ClickHouse.ClickHouseSock, table_name::String)
# Query to get column names and types from INFORMATION_SCHEMA
query = """
Expand Down
2 changes: 1 addition & 1 deletion src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ SELECT *
const docstring_copy_to =
"""
copy_to(conn, df_or_path, "name")
Allows user to copy a df to the database connection. Currently supports DuckDB, SQLite
Allows user to copy a df to the database connection. Currently supports DuckDB, SQLite, MySql
# Arguments
-`conn`: the database connection
Expand Down

0 comments on commit f04a304

Please sign in to comment.