Skip to content

Commit 8fba90b

Browse files
authored
Catching up to main.
Catching up to main.
2 parents f1f0194 + 954b38a commit 8fba90b

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

compiler/dialect_libraries/clickhouse_library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
{arg: a.arg, value: a.value, lim: l});
4040
4141
Array(a) = SqlExpr(
42-
"groupArray({value} ORDER BY {arg})",
42+
"arrayMap(x -> x.2, arraySort(groupArray(({arg}, {value}))))",
4343
{arg: a.arg, value: a.value});
4444
4545
RecordAsJson(r) = SqlExpr("toJSONString({x})", {x: r});

compiler/dialects.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def BuiltInFunctions(self):
264264
'Count': 'countDistinct(%s)',
265265
'AnyValue': 'any(%s)',
266266
'ArrayConcat': 'arrayConcat({0}, {1})',
267+
# Used internally to disambiguate aggregation scope inside (combine ...).
268+
# Mirrors PostgreSQL/DuckDB behavior.
269+
'MagicalEntangle': '(if({1} = 0, {0}, NULL))',
267270
# The compiler implements "x in some_array" as an UNNEST with a
268271
# generated table alias; this extracts the element from that alias.
269272
'ValueOfUnnested': '{0}.unnested_pod',
@@ -295,6 +298,9 @@ def ArrayPhrase(self):
295298
def GroupBySpecBy(self):
296299
return 'expr'
297300

301+
def DecorateCombineRule(self, rule, var):
302+
return DecorateCombineRule(rule, var)
303+
298304

299305
class Presto(Dialect):
300306

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@Engine("clickhouse");
2+
3+
Items(item: "Soap", price: 20);
4+
Items(item: "Milk", price: 10);
5+
Items(item: "Bread", price: 5);
6+
Items(item: "Coffee", price: 7);
7+
Items(item: "Firewood", price: 15);
8+
9+
MoreExpensiveThan(item1) Array= item2 -> item2 :-
10+
Items(item: item1, price: price1),
11+
Items(item: item2, price: price2),
12+
price1 > price2;
13+
14+
BuyEvent(purchase_id: 1, item: "Soap", quantity: 3);
15+
BuyEvent(purchase_id: 2, item: "Milk", quantity: 1);
16+
BuyEvent(purchase_id: 3, item: "Bread", quantity: 2);
17+
BuyEvent(purchase_id: 3, item: "Coffee", quantity: 1);
18+
BuyEvent(purchase_id: 4, item: "Firewood", quantity: 5);
19+
BuyEvent(purchase_id: 4, item: "Soap", quantity: 1);
20+
BuyEvent(purchase_id: 5, item: "Milk", quantity: 4);
21+
BuyEvent(purchase_id: 5, item: "Bread", quantity: 1);
22+
BuyEvent(purchase_id: 5, item: "Coffee", quantity: 2);
23+
BuyEvent(purchase_id: 6, item: "Firewood", quantity: 1);
24+
BuyEvent(purchase_id: 6, item: "Soap", quantity: 3);
25+
BuyEvent(purchase_id: 7, item: "Milk", quantity: 1);
26+
BuyEvent(purchase_id: 7, item: "Bread", quantity: 2);
27+
BuyEvent(purchase_id: 7, item: "Coffee", quantity: 1);
28+
BuyEvent(purchase_id: 8, item: "Firewood", quantity: 5);
29+
BuyEvent(purchase_id: 8, item: "Soap", quantity: 1);
30+
31+
Buyer(buyer_id: 11, purchase_id: 1);
32+
Buyer(buyer_id: 12, purchase_id: 2);
33+
Buyer(buyer_id: 13, purchase_id: 3);
34+
Buyer(buyer_id: 14, purchase_id: 4);
35+
Buyer(buyer_id: 12, purchase_id: 5);
36+
Buyer(buyer_id: 13, purchase_id: 6);
37+
Buyer(buyer_id: 14, purchase_id: 7);
38+
Buyer(buyer_id: 11, purchase_id: 8);
39+
40+
# ClickHouse limitation: correlated subqueries in FROM are not supported.
41+
# We avoid `item_record in items` by re-joining BuyEvent for expensive_items.
42+
PurchaseRaw(purchase_id:, items:, expensive_items:, buyer_id:) :-
43+
Buyer(buyer_id:, purchase_id:),
44+
items Array= (
45+
item -> {item:, quantity:, price:} :-
46+
BuyEvent(purchase_id:, item:, quantity:),
47+
Items(item:, price:)
48+
),
49+
expensive_items Array= (
50+
{item:, more_expensive_than:} -> {item:, more_expensive_than:} :-
51+
BuyEvent(purchase_id:, item:),
52+
more_expensive_than = MoreExpensiveThan(item)
53+
);
54+
55+
# Sorting step: Order the already-materialized rows.
56+
@OrderBy(Purchase, "purchase_id");
57+
Purchase(purchase_id:, items:, expensive_items:, buyer_id:) :-
58+
PurchaseRaw(purchase_id:, items:, expensive_items:, buyer_id:);
59+
60+
Test(purchase_id:, items:, expensive_items:, buyer_id:) :-
61+
Purchase(purchase_id:, items:, expensive_items:, buyer_id:);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+
2+
| purchase_id | items | expensive_items | buyer_id |
3+
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+
4+
| 1 | [('Soap',20,3)] | [('Soap',['Bread','Coffee','Firewood','Milk'])] | 11 |
5+
| 2 | [('Milk',10,1)] | [('Milk',['Bread','Coffee'])] | 12 |
6+
| 3 | [('Bread',5,2),('Coffee',7,1)] | [('Coffee',['Bread'])] | 13 |
7+
| 4 | [('Firewood',15,5),('Soap',20,1)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 14 |
8+
| 5 | [('Bread',5,1),('Coffee',7,2),('Milk',10,4)] | [('Coffee',['Bread']),('Milk',['Bread','Coffee'])] | 12 |
9+
| 6 | [('Firewood',15,1),('Soap',20,3)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 13 |
10+
| 7 | [('Bread',5,2),('Coffee',7,1),('Milk',10,1)] | [('Coffee',['Bread']),('Milk',['Bread','Coffee'])] | 14 |
11+
| 8 | [('Firewood',15,5),('Soap',20,1)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 11 |
12+
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+

integration_tests/run_tests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def RunAll(test_presto=False, test_trino=False, test_clingo=True, test_clickhous
6565
RunTest("dialects/trino/joins_test")
6666
RunTest("dialects/trino/joins_test")
6767

68-
if test_clickhouse:
68+
if test_clickhouse or logica_test.TestManager.RUN_ONLY:
6969
RunTest("dialects/clickhouse/shipping_funfacts_test")
70+
RunTest("dialects/clickhouse/purchase_test")
7071

7172
if test_clingo:
7273
from common import duckdb_logica

0 commit comments

Comments
 (0)