|
3 | 3 | from decimal import Decimal |
4 | 4 |
|
5 | 5 | from test.splitgraph.conftest import _mount_elasticsearch |
6 | | -from splitgraph.core.repository import Repository |
7 | | -from splitgraph.engine import get_engine |
| 6 | +from splitgraph.engine import get_engine, ResultShape |
8 | 7 |
|
9 | 8 |
|
10 | 9 | def _extract_query_from_explain(result): |
11 | | - query_str = "{" |
| 10 | + query_str = "" |
12 | 11 |
|
13 | | - for o in result[3:]: |
14 | | - query_str += o[0] + "\n" |
| 12 | + for o in result: |
| 13 | + if query_str != "": |
| 14 | + query_str += o[0] + "\n" |
| 15 | + elif "Multicorn: Query:" in o[0]: |
| 16 | + query_str = "{" |
| 17 | + |
| 18 | + if o == ("}",): |
| 19 | + break |
15 | 20 |
|
16 | 21 | return json.loads(query_str) |
17 | 22 |
|
@@ -45,3 +50,187 @@ def test_elasticsearch_aggregation_functions_only(local_engine_empty): |
45 | 50 |
|
46 | 51 | # Assert aggregation result |
47 | 52 | assert result[0] == (999, 25714.837, 49989, 25714837, 20, Decimal("30.171")) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.mounting |
| 56 | +def test_elasticsearch_gropuing_clauses_only(local_engine_empty): |
| 57 | + _mount_elasticsearch() |
| 58 | + |
| 59 | + # Single column grouping |
| 60 | + query = "SELECT state FROM es.account GROUP BY state" |
| 61 | + |
| 62 | + # Ensure grouping is going to be pushed down |
| 63 | + result = get_engine().run_sql("EXPLAIN " + query) |
| 64 | + assert len(result) > 2 |
| 65 | + assert _extract_query_from_explain(result) == { |
| 66 | + "aggs": { |
| 67 | + "group_buckets": { |
| 68 | + "composite": {"sources": [{"state": {"terms": {"field": "state"}}}], "size": 5} |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + # Ensure results are correct |
| 74 | + result = get_engine().run_sql(query, return_shape=ResultShape.MANY_ONE) |
| 75 | + assert len(result) == 51 |
| 76 | + |
| 77 | + # Assert aggregation result |
| 78 | + assert result == [ |
| 79 | + "AK", |
| 80 | + "AL", |
| 81 | + "AR", |
| 82 | + "AZ", |
| 83 | + "CA", |
| 84 | + "CO", |
| 85 | + "CT", |
| 86 | + "DC", |
| 87 | + "DE", |
| 88 | + "FL", |
| 89 | + "GA", |
| 90 | + "HI", |
| 91 | + "IA", |
| 92 | + "ID", |
| 93 | + "IL", |
| 94 | + "IN", |
| 95 | + "KS", |
| 96 | + "KY", |
| 97 | + "LA", |
| 98 | + "MA", |
| 99 | + "MD", |
| 100 | + "ME", |
| 101 | + "MI", |
| 102 | + "MN", |
| 103 | + "MO", |
| 104 | + "MS", |
| 105 | + "MT", |
| 106 | + "NC", |
| 107 | + "ND", |
| 108 | + "NE", |
| 109 | + "NH", |
| 110 | + "NJ", |
| 111 | + "NM", |
| 112 | + "NV", |
| 113 | + "NY", |
| 114 | + "OH", |
| 115 | + "OK", |
| 116 | + "OR", |
| 117 | + "PA", |
| 118 | + "RI", |
| 119 | + "SC", |
| 120 | + "SD", |
| 121 | + "TN", |
| 122 | + "TX", |
| 123 | + "UT", |
| 124 | + "VA", |
| 125 | + "VT", |
| 126 | + "WA", |
| 127 | + "WI", |
| 128 | + "WV", |
| 129 | + "WY", |
| 130 | + ] |
| 131 | + |
| 132 | + # Multi-column grouping |
| 133 | + query = "SELECT gender, age FROM es.account GROUP BY age, gender" |
| 134 | + |
| 135 | + # Ensure grouping is going to be pushed down |
| 136 | + result = get_engine().run_sql("EXPLAIN " + query) |
| 137 | + assert len(result) > 2 |
| 138 | + assert _extract_query_from_explain(result) == { |
| 139 | + "aggs": { |
| 140 | + "group_buckets": { |
| 141 | + "composite": { |
| 142 | + "sources": [ |
| 143 | + {"gender": {"terms": {"field": "gender"}}}, |
| 144 | + {"age": {"terms": {"field": "age"}}}, |
| 145 | + ], |
| 146 | + "size": 5, |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + # Ensure results are correct |
| 153 | + result = get_engine().run_sql(query) |
| 154 | + assert len(result) == 42 |
| 155 | + |
| 156 | + # Assert aggregation result |
| 157 | + gender = "F" |
| 158 | + age = 20 |
| 159 | + for row in result: |
| 160 | + assert row == (gender, age) |
| 161 | + |
| 162 | + age += 1 |
| 163 | + if age == 41: |
| 164 | + age = 20 |
| 165 | + gender = "M" |
| 166 | + |
| 167 | + |
| 168 | +@pytest.mark.mounting |
| 169 | +def test_elasticsearch_gropuing_and_aggregations_bare(local_engine_empty): |
| 170 | + _mount_elasticsearch() |
| 171 | + |
| 172 | + # Single column grouping |
| 173 | + query = "SELECT gender, avg(balance), avg(age) FROM es.account GROUP BY gender" |
| 174 | + |
| 175 | + # Ensure query is going to be pushed down |
| 176 | + result = get_engine().run_sql("EXPLAIN " + query) |
| 177 | + assert len(result) > 2 |
| 178 | + assert _extract_query_from_explain(result) == { |
| 179 | + "aggs": { |
| 180 | + "group_buckets": { |
| 181 | + "composite": {"sources": [{"gender": {"terms": {"field": "gender"}}}], "size": 5}, |
| 182 | + "aggregations": { |
| 183 | + "avg.balance": {"avg": {"field": "balance"}}, |
| 184 | + "avg.age": {"avg": {"field": "age"}}, |
| 185 | + }, |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + # Ensure results are correct |
| 191 | + result = get_engine().run_sql(query) |
| 192 | + assert len(result) == 2 |
| 193 | + |
| 194 | + # Assert aggregation result |
| 195 | + assert result == [ |
| 196 | + ("F", 25623.34685598377, Decimal("30.3184584178499")), |
| 197 | + ("M", 25803.800788954635, Decimal("30.027613412228796")), |
| 198 | + ] |
| 199 | + |
| 200 | + |
| 201 | +@pytest.mark.mounting |
| 202 | +def test_elasticsearch_not_pushed_down(local_engine_empty): |
| 203 | + _mount_elasticsearch() |
| 204 | + |
| 205 | + _bare_filtering_query = {"query": {"bool": {"must": [{"range": {"age": {"gt": 30}}}]}}} |
| 206 | + |
| 207 | + # Aggregation only filtering is not going to be pushed down |
| 208 | + result = get_engine().run_sql( |
| 209 | + "EXPLAIN SELECT max(balance), avg(age) FROM es.account WHERE age > 30" |
| 210 | + ) |
| 211 | + assert len(result) > 2 |
| 212 | + assert _extract_query_from_explain(result) == _bare_filtering_query |
| 213 | + |
| 214 | + # Grouping only filtering is not going to be pushed down |
| 215 | + result = get_engine().run_sql("EXPLAIN SELECT age FROM es.account WHERE age > 30 GROUP BY age") |
| 216 | + assert len(result) > 2 |
| 217 | + assert _extract_query_from_explain(result) == _bare_filtering_query |
| 218 | + |
| 219 | + # Aggregation and grouping filtering is not going to be pushed down |
| 220 | + result = get_engine().run_sql( |
| 221 | + "EXPLAIN SELECT age, min(balance) FROM es.account WHERE age > 30 GROUP BY age" |
| 222 | + ) |
| 223 | + assert len(result) > 2 |
| 224 | + assert _extract_query_from_explain(result) == _bare_filtering_query |
| 225 | + |
| 226 | + _bare_sequential_scan = {"query": {"bool": {"must": []}}} |
| 227 | + |
| 228 | + # SELECT STAR is not going to be pushed down |
| 229 | + result = get_engine().run_sql("EXPLAIN SELECT * FROM es.account") |
| 230 | + assert len(result) > 2 |
| 231 | + assert _extract_query_from_explain(result) == _bare_sequential_scan |
| 232 | + |
| 233 | + # DISTINCT queries are not going to be pushed down |
| 234 | + result = get_engine().run_sql("EXPLAIN SELECT COUNT(DISTINCT city) FROM es.account") |
| 235 | + assert len(result) > 2 |
| 236 | + assert _extract_query_from_explain(result) == _bare_sequential_scan |
0 commit comments