• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

TidierOrg / TidierDB.jl / 14422067913

12 Apr 2025 06:02PM UTC coverage: 93.477% (-0.005%) from 93.482%
14422067913

Pull #128

github

web-flow
Merge 58e56eb7f into dec61c25b
Pull Request #128: switch to repl print

1 of 3 new or added lines in 1 file covered. (33.33%)

9 existing lines in 3 files now uncovered.

1218 of 1303 relevant lines covered (93.48%)

379.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/union_intersect_setdiff.jl
1
# A unified function that performs the set operation, preserving existing logic
2

3
function perform_set_operation(sq::SQLQuery, uq_or_table, op::String; all::Bool=false)
66✔
4

5
    if !isa(sq, SQLQuery)
33✔
6
        error("Expected sqlquery to be an instance of SQLQuery")
×
7
    end
8

9
    # 1) Possibly create a new CTE for the left query (sq)
10
    needs_new_cte_sq = !isempty(sq.select) || !isempty(sq.where) || sq.is_aggregated || !isempty(sq.ctes)
63✔
11
    if needs_new_cte_sq
33✔
12
        sq.cte_count += 1
3✔
13
        cte_name_sq = "cte_" * string(sq.cte_count)
3✔
14
        most_recent_source_sq = !isempty(sq.ctes) ? "cte_" * string(sq.cte_count - 1) : sq.from
3✔
15
        select_sql_sq = "SELECT * FROM " * most_recent_source_sq
3✔
16
        new_cte_sq = CTE(name=cte_name_sq, select=select_sql_sq)
3✔
17
        up_cte_name(sq, cte_name_sq)
3✔
18
        
19
        push!(sq.ctes, new_cte_sq)
3✔
20
        sq.from = cte_name_sq
3✔
21
    end
22

23
    local op_clause
24
    if all
33✔
25
        op_clause = op * " ALL"
12✔
26
    else
27
        op_clause = op
21✔
28
    end
29

UNCOV
30
    local union_sql
31
    if isa(uq_or_table, SQLQuery)
33✔
32
        uq = uq_or_table
27✔
33
        # Possibly create a new CTE for the right query (uq)
34
        needs_new_cte_uq = !isempty(uq.select) || !isempty(uq.where) || uq.is_aggregated || !isempty(uq.ctes)
45✔
35
        if needs_new_cte_uq
27✔
36
            sq.join_count += 1
9✔
37
            joinc = "j" * string(sq.join_count)
9✔
38
            for cte in uq.ctes
9✔
39
                cte.name = joinc * cte.name
15✔
40
            end
15✔
41
            uq.cte_count += 1
9✔
42
            cte_name_uq = joinc * "cte_" * string(uq.cte_count)
12✔
43
            most_recent_source_uq = !isempty(uq.ctes) ? joinc * "cte_" * string(uq.cte_count - 1) : uq.from
12✔
44
            select_sql_uq = finalize_query_jq(uq, most_recent_source_uq)
9✔
45
            new_cte_uq = CTE(name=cte_name_uq, select=select_sql_uq)
9✔
46
            push!(uq.ctes, new_cte_uq)
9✔
47
            uq.from = cte_name_uq
9✔
48
        end
49

50
        # Combine
51
        union_sql = "SELECT * FROM " * sq.from * " " * op_clause * " SELECT * FROM " * uq.from
36✔
52

53
        sq.ctes = vcat(sq.ctes, uq.ctes)
27✔
54
        # sq.metadata = vcat(sq.metadata, uq.metadata)
55

56
    else
57
        # Treat uq_or_table as a table name
58
        tbl_name = string(uq_or_table)
6✔
59
        union_sql = "SELECT * FROM " * sq.from * " " * op_clause * " SELECT * FROM " * tbl_name
8✔
60

61
        # Update metadata (commented out as in original)
62
        if current_sql_mode[] != :athena
12✔
63
            new_metadata = get_table_metadata(sq.db, tbl_name)
12✔
64
        else
UNCOV
65
            new_metadata = get_table_metadata_athena(sq.db, tbl_name, sq.athena_params)
×
66
        end
67
        # sq.metadata = vcat(sq.metadata, new_metadata)
68
    end
69

70
    # 4) Create a new CTE for the combined result
71
    sq.cte_count += 1
33✔
72
    union_cte_name = "cte_" * string(sq.cte_count)
33✔
73
    union_cte = CTE(name=union_cte_name, select=union_sql)
33✔
74
    up_cte_name(sq, union_cte_name)
33✔
75
    push!(sq.ctes, union_cte)
33✔
76
    sq.from = union_cte_name
33✔
77

78
    return sq
33✔
79
end
80

81

82
"""
83
$docstring_union
84
"""
85
macro union(sqlquery, union_query, args...)
18✔
86
    # parse the `all` argument exactly as in the original logic
87
    all_flag = false
18✔
88
    for arg in args
18✔
89
        if isa(arg, Expr) && arg.head == :(=)
6✔
90
            if arg.args[1] == :all && arg.args[2] == true
6✔
91
                all_flag = true
3✔
92
            end
93
        end
94
    end
6✔
95
    return quote
18✔
96
        sq = $(esc(sqlquery))
97
        sq = sq.post_first ? t($(esc(sqlquery))) : sq
98
        sq.post_first = false; 
99
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
100
 
101
        perform_set_operation(
102
            sq,
103
            uq,
104
            "UNION";
105
            all = $(all_flag)
106
        )
107
    end
108
end
109

110
"""
111
$docstring_union_all
112
"""
113
macro union_all(sqlquery, union_query)
3✔
114
    return quote
3✔
115
        sq = $(esc(sqlquery))
116
        sq = sq.post_first ? t($(esc(sqlquery))) : sq
117
        sq.post_first = false; 
118
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
119
 
120
        perform_set_operation(
121
            sq,
122
            uq,
123
            "UNION";  # We'll let the function append " ALL"
124
            all = true
125
        )
126
    end
127
end
128

129
"""
130
$docstring_intersect
131
"""
132
macro intersect(sqlquery, union_query, args...)
6✔
133
    # parse the `all` argument exactly as in the original logic
134
    all_flag = false
6✔
135
    for arg in args
6✔
136
        if isa(arg, Expr) && arg.head == :(=)
3✔
137
            if arg.args[1] == :all && arg.args[2] == true
3✔
138
                all_flag = true
3✔
139
            end
140
        end
141
    end
3✔
142

143
    return quote
6✔
144
        sq = $(esc(sqlquery))
145
        sq = sq.post_first ? t($(esc(sqlquery))) : sq
146
        sq.post_first = false; 
147
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
148
 
149
        perform_set_operation(
150
            sq,
151
            uq,
152
            "INTERSECT";
153
            all = $(all_flag)
154
        )
155
    end
156
end
157

158
"""
159
$docstring_setdiff
160
"""
161
macro setdiff(sqlquery, union_query, args...)
6✔
162
    # parse the `all` argument exactly as in the original logic
163
    all_flag = false
6✔
164
    for arg in args
6✔
165
        if isa(arg, Expr) && arg.head == :(=)
3✔
166
            if arg.args[1] == :all && arg.args[2] == true
3✔
167
                all_flag = true
3✔
168
            end
169
        end
170
    end
3✔
171

172
    return quote
6✔
173
        sq = $(esc(sqlquery))
174
        sq = sq.post_first ? t($(esc(sqlquery))) : sq
175
        sq.post_first = false; 
176
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
177
 
178
        
179
        perform_set_operation(
180
            sq,
181
            uq,
182
            "EXCEPT";
183
            all = $(all_flag)
184
        )
185
    end
186
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc