From d7e115798b90a6c06a4ff922cc17f5bc54bdbc2c Mon Sep 17 00:00:00 2001 From: Daniel Rizk Date: Sat, 19 Oct 2024 09:34:14 -0400 Subject: [PATCH] small improvment in filepath reading --- NEWS.md | 2 +- Project.toml | 2 +- docs/examples/UserGuide/from_queryex.jl | 28 ++++--------------------- src/TidierDB.jl | 2 +- src/joins_sq.jl | 9 ++++++-- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/NEWS.md b/NEWS.md index 454d6ee..a0d42da 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,7 +3,7 @@ Breaking Changes: - All join syntax now matches TidierData's `(table1, table2, t1_col = t2_col)` Additions: -- `@compute`for DuckDB, MySQL, PostGres, GBQ to write a table to the db and the end of a query. +- `@compute`for DuckDB, MySQL, PostGres, GBQ to write a table to the db at the end of a query. - expands `@create_view` to MySQL, PostGres, GBQ - Support for performing multiple joins of TidierDB queries in a single chain with further tests - `dmy`, `mdy`, `ymd` support DuckDB, Postgres, GBQ, Clickhouse, MySQL, MsSQL, Athena, MsSQL diff --git a/Project.toml b/Project.toml index dcc7c08..741e7b9 100644 --- a/Project.toml +++ b/Project.toml @@ -43,7 +43,7 @@ ClickHouse = "0.2" DataFrames = "1.5" Dates = "1.9" Documenter = "0.27, 1" -DuckDB = "1.0" +DuckDB = "1.0, 1.1" GZip = "0.6" GoogleCloud = "0.11" HTTP = "1.1" diff --git a/docs/examples/UserGuide/from_queryex.jl b/docs/examples/UserGuide/from_queryex.jl index ec9ab41..29a17e9 100644 --- a/docs/examples/UserGuide/from_queryex.jl +++ b/docs/examples/UserGuide/from_queryex.jl @@ -1,5 +1,5 @@ # While using TidierDB, you may need to generate part of a query and reuse it multiple times. There are two ways to do this -# 1. `from_query(query)` or `t(query)` +# 1. `from_query(query)` or its alias `t(query)` # 2. `@create_view(name)` # ## Setup @@ -12,7 +12,7 @@ # Start a query to analyze fuel efficiency by number of cylinders. However, to further build on this query later, end the chain without using `@show_query` or `@collect` # ```julia -# query = DB.@chain DB.t(query) begin +# query = DB.@chain DB.t(mtcars) begin # DB.@group_by cyl # DB.@summarize begin # across(mpg, (mean, minimum, maximum)) @@ -31,7 +31,7 @@ # Now, `from_query`, or `t()` a convienece wrapper, will allow you to reuse the query to calculate the average horsepower for each efficiency category # ```julia # DB.@chain DB.t(query) begin -# DB.@left_join("mtcars2", cyl, cyl) +# DB.@left_join(DB.t(mtcars), cyl = cyl) # DB.@group_by(efficiency) # DB.@summarize(avg_hp = mean(hp)) # DB.@collect @@ -46,26 +46,6 @@ # 2 │ High 82.6364 # ``` -# Reuse the query again to find the car with the highest MPG for each cylinder category -# ```julia -# DB.@chain DB.t(mtcars) begin -# DB.@left_join("mtcars2", cyl, cyl) -# DB.@group_by cyl -# DB.@slice_max(mpg) -# DB.@select model cyl mpg -# DB.@collect -# end -# ``` -# ``` -# 3×3 DataFrame -# Row │ model cyl mpg -# │ String? Int64? Float64? -# ─────┼──────────────────────────────────── -# 1 │ Pontiac Firebird 8 19.2 -# 2 │ Toyota Corolla 4 33.9 -# 3 │ Hornet 4 Drive 6 21.4 -# ``` - # ## @create_view # This can also be done with `@create_view`. # ```julia @@ -87,7 +67,7 @@ # # # DB.@chain DB.db_table(db, "viewer") begin -# DB.@left_join(DB.t(query2), cyl, cyl) +# DB.@left_join(DB.t(query2), cyl = cyl) # DB.@group_by(efficiency) # DB.@summarize(avg_mean = mean(mpg)) # DB.@mutate(mean = avg_mean / 4 ) diff --git a/src/TidierDB.jl b/src/TidierDB.jl index ed6d94a..7767048 100644 --- a/src/TidierDB.jl +++ b/src/TidierDB.jl @@ -260,7 +260,7 @@ function db_table(db, table, athena_params::Any=nothing; iceberg::Bool=false, de elseif delta "delta_scan('$table_name')" elseif occursin(r"[:/]", table_name) && !(iceberg || delta) && !startswith(table_name, "read") - "'$table_name'" + "'$table_name' AS $(split(basename(table_name), '.')[1]) " elseif startswith(table_name, "read") "$table_name" else diff --git a/src/joins_sq.jl b/src/joins_sq.jl index a028c3c..f253c0a 100644 --- a/src/joins_sq.jl +++ b/src/joins_sq.jl @@ -6,7 +6,11 @@ function gbq_join_parse(input) if current_sql_mode[] == gbq() && length(parts) >=2 return parts[end] elseif occursin(".", input) - return split(input, '.')[end] + if occursin(r"[:/]", input) + return split(basename(input), '.')[1] + else + return split(input, '.')[end] + end else return input end @@ -78,7 +82,7 @@ $docstring_left_join """ macro left_join(sqlquery, join_table, expr) lhs_col_str, rhs_col_str = parse_join_expression(expr) - + return quote sq = $(esc(sqlquery)) jq = $(esc(join_table)) # Evaluate join_table @@ -160,6 +164,7 @@ macro left_join(sqlquery, join_table, expr) join_table_name = jq.from else # When join_table is a table name + join_table_name = string(jq) if current_sql_mode[] != :athena new_metadata = get_table_metadata(sq.db, join_table_name)