-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteArrowKit.jl
More file actions
165 lines (133 loc) · 4.89 KB
/
Copy pathSQLiteArrowKit.jl
File metadata and controls
165 lines (133 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
module SQLiteArrowKit
using DataFrames, Arrow, SQLite, CSV, DuckDB
export is_available, get_ArrowTable, get_DataFrame
const DB_PATH = "/content/popular.db"
#=
**********************************************
=#
function is_available(db::SQLite.DB, table::String)::Bool
name = uppercase(table)
list_tables = collect(SQLite.tables(db))
names = [t.name for t in list_tables]
return name in names
end
#=
**********************************************
=#
function get_ArrowTable(db::SQLite.DB, table::String)::Arrow.Table
name = uppercase(table)
io = IOBuffer()
cursor = DBInterface.execute(db, "SELECT * FROM $name")
Arrow.write(io, cursor)
seekstart(io)
arrow_table = Arrow.Table(io)
return arrow_table
end
#=
**********************************************
=#
function get_DataFrame(db::SQLite.DB, table::String)::DataFrame
name = uppercase(table)
return DBInterface.execute(db, "SELECT * FROM $name") |> DataFrame
end
#=
**********************************************
THIS FUNCTION IS ONLY A TEST AND IT MUSTN'T BE EXECUTED
=#
function main(csv_path::String, db_table::String)
println("DATA SIZE~437 MB")
println("\n", "*"^40)
db = SQLite.DB(DB_PATH)
csvstart = time()
songs_csv = CSV.read(csv_path, DataFrame; header = true, delim = ',')
csvend = time()
lecture_csv = round(csvend - csvstart, digits = 4)
println("LECTURE TIME, FROM CSV TO DATAFRAMES: $lecture_csv")
println("PROCESSING TIME, USING DATAFRAMES:")
start_df = time()
dfMode = combine(groupby(songs_csv,
"mode"
),
nrow => :frequency
)
dfModePopularity = combine(groupby(filter(:popularity => x -> x > 95,
songs_csv
),
[:spotify_id,
:name,
:mode]
),
nrow => :persistenceInPopularity
)
result= first(filter(:persistenceInPopularity => x -> x > 5000, dfModePopularity), 10)
end_df = time()
elap_df = round(end_df - start_df, digits = 4)
println("\n$dfMode")
println("\n$result\n")
println("\ntime~$elap_df")
if is_available(db, db_table)
duck = nothing
try
duck = DBInterface.connect(DuckDB.DB, ":memory:")
println("\n", "*"^40)
arrow_start = time()
duck_songs = get_ArrowTable(db, db_table)
arrow_end = time()
elap_arrow = round(arrow_end - arrow_start, digits = 4)
println("LECTURE TIME (EXPLICIT EXPORT), FROM CURSOR TO ARROW TABLE:\n$elap_arrow")
duck_start = time()
DuckDB.register_data_frame(duck, duck_songs, "SONGS")
mode = """
SELECT
mode,
COUNT(*) AS frequency
FROM
SONGS
GROUP BY
mode
"""
modePopularity = """
WITH
POPULAR AS(
SELECT
name,
mode,
COUNT(*) AS persistenceInPopularity95
FROM
SONGS
WHERE
popularity > 95
GROUP BY
spotify_id, name, mode)
SELECT
*
FROM
POPULAR
WHERE
persistenceInPopularity95 > 5000
"""
modeDuck = DBInterface.execute(duck, mode) |> DataFrame
popularityDuck = DBInterface.execute(duck, modePopularity) |> DataFrame
duck_end = time()
elap_duck = round(duck_end - duck_start, digits = 4)
println("PROCESSING TIME, USING DUCKDB QUERIES: $elap_duck")
println("\n$modeDuck")
println("\n$popularityDuck")
finally
DBInterface.close!(duck)
end
end
end
#=
**********************************************
=#
end
#=
**********************************************
=#
if Base.@isdefined(PROGRAM_FILE) &&
abspath(PROGRAM_FILE) == abspath(@__FILE__)
a = ARGS[1]
b = ARGS[2]
main(a, b)
end