• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TidierOrg / TidierDB.jl / 16898021533

12 Aug 2025 03:19AM UTC coverage: 90.061%. Remained the same
16898021533

Pull #153

github

web-flow
Merge af9005196 into b45ef052f
Pull Request #153: Bump actions/checkout from 4 to 5

1323 of 1469 relevant lines covered (90.06%)

332.91 hits per line

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

96.92
/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
        build_cte!(sq) 
3✔
13
       # sq.from = cte_name_sq
14
    end
15

16
    local op_clause
17
    if all
33✔
18
        op_clause = op * " ALL"
12✔
19
    else
20
        op_clause = op
21✔
21
    end
22

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

44
        # Combine
45
        union_sql = "SELECT * FROM " * sq.from * " " * op_clause * " SELECT * FROM " * uq.from
36✔
46

47
        sq.ctes = vcat(sq.ctes, uq.ctes)
27✔
48
        # sq.metadata = vcat(sq.metadata, uq.metadata)
49

50
    else
51
        # Treat uq_or_table as a table name
52
        tbl_name = string(uq_or_table)
6✔
53
        union_sql = "SELECT * FROM " * sq.from * " " * op_clause * " SELECT * FROM " * tbl_name
8✔
54

55
        # Update metadata (commented out as in original)
56
        if current_sql_mode[] != :athena
12✔
57
            new_metadata = get_table_metadata(sq.db, tbl_name)
12✔
58
        else
59
            new_metadata = get_table_metadata_athena(sq.db, tbl_name, sq.athena_params)
×
60
        end
61
        # sq.metadata = vcat(sq.metadata, new_metadata)
62
    end
63

64
    # 4) Create a new CTE for the combined result
65
    sq.cte_count += 1
33✔
66
    union_cte_name = "cte_" * string(sq.cte_count)
33✔
67
    union_cte = CTE(name=union_cte_name, select=union_sql)
33✔
68
    up_cte_name(sq, union_cte_name)
33✔
69
    push!(sq.ctes, union_cte)
33✔
70
    sq.from = union_cte_name
33✔
71

72
    return sq
33✔
73
end
74

75

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

102
"""
103
$docstring_union_all
104
"""
105
macro union_all(sqlquery, union_query)
3✔
106
    return quote
3✔
107
        sq = t($(esc(sqlquery)))
108
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
109
 
110
        perform_set_operation(
111
            sq,
112
            uq,
113
            "UNION";  # We'll let the function append " ALL"
114
            all = true
115
        )
116
    end
117
end
118

119
"""
120
$docstring_intersect
121
"""
122
macro intersect(sqlquery, union_query, args...)
6✔
123
    # parse the `all` argument exactly as in the original logic
124
    all_flag = false
6✔
125
    for arg in args
6✔
126
        if isa(arg, Expr) && arg.head == :(=)
3✔
127
            if arg.args[1] == :all && arg.args[2] == true
3✔
128
                all_flag = true
3✔
129
            end
130
        end
131
    end
3✔
132

133
    return quote
6✔
134
        sq = t($(esc(sqlquery)))
135
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
136
 
137
        perform_set_operation(
138
            sq,
139
            uq,
140
            "INTERSECT";
141
            all = $(all_flag)
142
        )
143
    end
144
end
145

146
"""
147
$docstring_setdiff
148
"""
149
macro setdiff(sqlquery, union_query, args...)
6✔
150
    # parse the `all` argument exactly as in the original logic
151
    all_flag = false
6✔
152
    for arg in args
6✔
153
        if isa(arg, Expr) && arg.head == :(=)
3✔
154
            if arg.args[1] == :all && arg.args[2] == true
3✔
155
                all_flag = true
3✔
156
            end
157
        end
158
    end
3✔
159

160
    return quote
6✔
161
        sq = t($(esc(sqlquery)))
162
        uq = isa($(esc(union_query)), String) ? $(esc(union_query)) : t($(esc(union_query))) 
163
 
164
        
165
        perform_set_operation(
166
            sq,
167
            uq,
168
            "EXCEPT";
169
            all = $(all_flag)
170
        )
171
    end
172
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