Skip to content

Commit

Permalink
Merge pull request #63 from TidierOrg/main
Browse files Browse the repository at this point in the history
add upstream changes
  • Loading branch information
drizk1 authored Sep 23, 2024
2 parents 807c20e + 4099414 commit ae097ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/TidierDB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ end
function connect(::duckdb, token::String)
if token == "md:"
return DBInterface.connect(DuckDB.DB, "md:")
elseif endswith(token, ".duckdb")
return DuckDB.DB(token)
else
return DBInterface.connect(DuckDB.DB, "md:$token")
end
Expand Down
25 changes: 18 additions & 7 deletions src/joins_sq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ function gbq_join_parse(input)
end
end

function get_join_columns(db, join_table, lhs_col_str)
if current_sql_mode[] == mssql()
cols = get_table_metadata(db, string(join_table))
matching_indices = findall(cols.name .== lhs_col_str)
cols.current_selxn[matching_indices] .= 0
cols_names = cols.name[cols.current_selxn .== 1] |> Vector
return join([string(join_table, ".", col) for col in cols_names], ", ") * " FROM "
else current_sql_mode[] == gbq()
return string(gbq_join_parse(join_table)) * ".* FROM "
end
end


"""
$docstring_left_join
Expand All @@ -22,14 +34,13 @@ macro left_join(sqlquery, join_table, lhs_column, rhs_column)
sq = $(esc(sqlquery))
if isa(sq, SQLQuery)
needs_new_cte = !isempty(sq.select) || !isempty(sq.where) || sq.is_aggregated || !isempty(sq.ctes)

if needs_new_cte
sq.cte_count += 1
cte_name = "cte_" * string(sq.cte_count)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" LEFT JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down Expand Up @@ -76,7 +87,7 @@ macro right_join(sqlquery, join_table, lhs_column, rhs_column)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" RIGHT JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down Expand Up @@ -124,7 +135,7 @@ macro inner_join(sqlquery, join_table, lhs_column, rhs_column)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" INNER JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down Expand Up @@ -172,7 +183,7 @@ macro full_join(sqlquery, join_table, lhs_column, rhs_column)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" FULL JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down Expand Up @@ -220,7 +231,7 @@ macro semi_join(sqlquery, join_table, lhs_column, rhs_column)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" SEMI JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down Expand Up @@ -268,7 +279,7 @@ macro anti_join(sqlquery, join_table, lhs_column, rhs_column)

most_recent_source = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from

join_sql = " " * most_recent_source * ".*, " * string(gbq_join_parse($(esc(join_table)))) * ".* FROM " * gbq_join_parse(most_recent_source) *
join_sql = " " * most_recent_source * ".*, " * get_join_columns(sq.db, string($(esc(join_table))), $lhs_col_str) * gbq_join_parse(most_recent_source) *
" ANTI JOIN " * string($(esc(join_table))) * " ON " * string(gbq_join_parse($(esc(join_table))), ".", $lhs_col_str, " = ", gbq_join_parse(most_recent_source), ".", $rhs_col_str)

# Create and add the new CTE
Expand Down

0 comments on commit ae097ee

Please sign in to comment.