Skip to content

Commit bc5aef7

Browse files
authored
Merge pull request #141 from TidierOrg/temptable
add temp table arg
2 parents 899438b + 67a1a8f commit bc5aef7

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# TidierDB.jl updates
2+
## v.8.7 - 2025-07-07
3+
- AWS Athena backend bug fixes
4+
- add `temp` option to `@create_table`, default is `true`
5+
26
## v.8.6 - 2025-05-05
37
- add `@pivot_longer`
48
- resolve issue when calling `@summarize` consecutively

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TidierDB"
22
uuid = "86993f9b-bbba-4084-97c5-ee15961ad48b"
33
authors = ["Daniel Rizk <rizk.daniel.12@gmail.com> and contributors"]
4-
version = "0.8.6"
4+
version = "0.8.7"
55

66
[deps]
77
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"

src/TidierDB.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using GZip
2323
export db_table, set_sql_mode, connect, from_query, update_con,
2424
clickhouse, duckdb, sqlite, mysql, mssql, postgres, athena, snowflake, gbq,
2525
oracle, databricks, SQLQuery, show_tables,
26-
t, @create_view, drop_view, @compute, warnings, copy_to, dt,
26+
t, @create_view, drop_view, @create_table, warnings, copy_to, dt,
2727
@show_query, @collect, @window_order, @window_frame, write_file
2828

2929
abstract type SQLBackend end

src/docstrings.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,9 +1768,9 @@ julia> drop_view(db, "viewer");
17681768
```
17691769
"""
17701770

1771-
const docstring_compute =
1771+
const docstring_create_table =
17721772
"""
1773-
@compute(sql_query, name, replace = false)
1773+
@compute(sql_query, name, replace = false, temp = true)
17741774
17751775
Creates a remote table on database memory from a SQL query. Currently supports DuckDB, MySQL, GBQ, Postgres
17761776
@@ -1779,14 +1779,15 @@ Creates a remote table on database memory from a SQL query. Currently supports D
17791779
- `sql_query`: The SQL query
17801780
- `name`: The name of the table to create.
17811781
- `replace`: defaults to false if table should be replaced if it already exists.
1782+
- `temp`: defaults to true if table should be created as a temporary table.
17821783
17831784
# Examples
17841785
```jldoctest
17851786
julia> db = connect(duckdb());
17861787
17871788
julia> df = DataFrame(id = [1, 2, 3], value = [10, 20, 30]);
17881789
1789-
julia> @chain dt(db, df, "df1") @compute(table2, true);
1790+
julia> @chain dt(db, df, "df1") @create_table(table2, replace = true, temp = true);
17901791
17911792
julia> dt(db, "table2")
17921793
SQLQuery("", "table", "", "", "", "", "", "", false, false, 2×4 DataFrame

src/view_compute.jl

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,54 @@ function drop_view(db, name)
4242
end
4343

4444

45-
macro compute(sqlquery, name, replace = false)
46-
47-
if replace == true
48-
sql_cr_or_replace = "CREATE OR REPLACE Table $name AS "
49-
elseif replace == false
50-
sql_cr_or_replace = "CREATE Table $name AS "
45+
macro create_table(sqlquery, name, args...)
46+
replace_flag = false
47+
temp_flag = false # new flag
48+
49+
for arg in args
50+
if arg === true || arg === false # legacy positional arg
51+
replace_flag = arg
52+
elseif isa(arg, Expr) && arg.head === :(=)
53+
lhs, rhs = arg.args
54+
if lhs === :replace
55+
replace_flag = rhs
56+
elseif lhs === :temp
57+
temp_flag = rhs
58+
else
59+
error("@create_table: unknown keyword $(lhs)")
60+
end
61+
else
62+
error("@create_table: unsupported argument $(arg)")
63+
end
5164
end
52-
return quote
53-
# prin
65+
66+
replace_clause = replace_flag ? " OR REPLACE" : ""
67+
temp_clause = temp_flag ? " TEMP" : ""
68+
sql_prefix = "CREATE$(replace_clause)$(temp_clause) TABLE $name AS "
69+
70+
quote
5471
backend = current_sql_mode[]
5572
sq = $(esc(sqlquery))
73+
5674
if backend == duckdb()
5775
final_query = finalize_query(sq)
58-
final_query = $sql_cr_or_replace * final_query
76+
final_query = $sql_prefix * final_query
77+
#println(final_query)
5978
DBInterface.execute(sq.db, final_query)
60-
elseif backend == postgres()
61-
final_compute($(esc(sqlquery)), postgres, $sql_cr_or_replace)
79+
80+
elseif backend == postgres()
81+
final_compute($(esc(sqlquery)), postgres, $sql_prefix)
82+
6283
elseif backend == gbq()
63-
final_compute(sq, gbq, $sql_cr_or_replace)
64-
elseif current_sql_mode[] == mysql()
65-
final_compute($(esc(sqlquery)), mysql, $sql_cr_or_replace)
66-
else
67-
backend = current_sql_mode[]
68-
print("$backend not yet supported") # COV_EXCL_LINE
84+
final_compute(sq, gbq, $sql_prefix)
85+
86+
elseif backend == mysql()
87+
final_compute($(esc(sqlquery)), mysql, $sql_prefix)
88+
89+
else
90+
backend = current_sql_mode[]
91+
print("$(backend) not yet supported") # COV_EXCL_LINE
6992
end
70-
7193
end
7294
end
7395

0 commit comments

Comments
 (0)