Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/dialect_libraries/clickhouse_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{arg: a.arg, value: a.value, lim: l});

Array(a) = SqlExpr(
"groupArray({value} ORDER BY {arg})",
"arrayMap(x -> x.2, arraySort(groupArray(({arg}, {value}))))",
{arg: a.arg, value: a.value});

RecordAsJson(r) = SqlExpr("toJSONString({x})", {x: r});
Expand Down
6 changes: 6 additions & 0 deletions compiler/dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ def BuiltInFunctions(self):
'Count': 'countDistinct(%s)',
'AnyValue': 'any(%s)',
'ArrayConcat': 'arrayConcat({0}, {1})',
# Used internally to disambiguate aggregation scope inside (combine ...).
# Mirrors PostgreSQL/DuckDB behavior.
'MagicalEntangle': '(if({1} = 0, {0}, NULL))',
# The compiler implements "x in some_array" as an UNNEST with a
# generated table alias; this extracts the element from that alias.
'ValueOfUnnested': '{0}.unnested_pod',
Expand Down Expand Up @@ -295,6 +298,9 @@ def ArrayPhrase(self):
def GroupBySpecBy(self):
return 'expr'

def DecorateCombineRule(self, rule, var):
return DecorateCombineRule(rule, var)


class Presto(Dialect):

Expand Down
61 changes: 61 additions & 0 deletions integration_tests/dialects/clickhouse/purchase_test.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@Engine("clickhouse");

Items(item: "Soap", price: 20);
Items(item: "Milk", price: 10);
Items(item: "Bread", price: 5);
Items(item: "Coffee", price: 7);
Items(item: "Firewood", price: 15);

MoreExpensiveThan(item1) Array= item2 -> item2 :-
Items(item: item1, price: price1),
Items(item: item2, price: price2),
price1 > price2;

BuyEvent(purchase_id: 1, item: "Soap", quantity: 3);
BuyEvent(purchase_id: 2, item: "Milk", quantity: 1);
BuyEvent(purchase_id: 3, item: "Bread", quantity: 2);
BuyEvent(purchase_id: 3, item: "Coffee", quantity: 1);
BuyEvent(purchase_id: 4, item: "Firewood", quantity: 5);
BuyEvent(purchase_id: 4, item: "Soap", quantity: 1);
BuyEvent(purchase_id: 5, item: "Milk", quantity: 4);
BuyEvent(purchase_id: 5, item: "Bread", quantity: 1);
BuyEvent(purchase_id: 5, item: "Coffee", quantity: 2);
BuyEvent(purchase_id: 6, item: "Firewood", quantity: 1);
BuyEvent(purchase_id: 6, item: "Soap", quantity: 3);
BuyEvent(purchase_id: 7, item: "Milk", quantity: 1);
BuyEvent(purchase_id: 7, item: "Bread", quantity: 2);
BuyEvent(purchase_id: 7, item: "Coffee", quantity: 1);
BuyEvent(purchase_id: 8, item: "Firewood", quantity: 5);
BuyEvent(purchase_id: 8, item: "Soap", quantity: 1);

Buyer(buyer_id: 11, purchase_id: 1);
Buyer(buyer_id: 12, purchase_id: 2);
Buyer(buyer_id: 13, purchase_id: 3);
Buyer(buyer_id: 14, purchase_id: 4);
Buyer(buyer_id: 12, purchase_id: 5);
Buyer(buyer_id: 13, purchase_id: 6);
Buyer(buyer_id: 14, purchase_id: 7);
Buyer(buyer_id: 11, purchase_id: 8);

# ClickHouse limitation: correlated subqueries in FROM are not supported.
# We avoid `item_record in items` by re-joining BuyEvent for expensive_items.
PurchaseRaw(purchase_id:, items:, expensive_items:, buyer_id:) :-
Buyer(buyer_id:, purchase_id:),
items Array= (
item -> {item:, quantity:, price:} :-
BuyEvent(purchase_id:, item:, quantity:),
Items(item:, price:)
),
expensive_items Array= (
{item:, more_expensive_than:} -> {item:, more_expensive_than:} :-
BuyEvent(purchase_id:, item:),
more_expensive_than = MoreExpensiveThan(item)
);

# Sorting step: Order the already-materialized rows.
@OrderBy(Purchase, "purchase_id");
Purchase(purchase_id:, items:, expensive_items:, buyer_id:) :-
PurchaseRaw(purchase_id:, items:, expensive_items:, buyer_id:);

Test(purchase_id:, items:, expensive_items:, buyer_id:) :-
Purchase(purchase_id:, items:, expensive_items:, buyer_id:);
12 changes: 12 additions & 0 deletions integration_tests/dialects/clickhouse/purchase_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+
| purchase_id | items | expensive_items | buyer_id |
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+
| 1 | [('Soap',20,3)] | [('Soap',['Bread','Coffee','Firewood','Milk'])] | 11 |
| 2 | [('Milk',10,1)] | [('Milk',['Bread','Coffee'])] | 12 |
| 3 | [('Bread',5,2),('Coffee',7,1)] | [('Coffee',['Bread'])] | 13 |
| 4 | [('Firewood',15,5),('Soap',20,1)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 14 |
| 5 | [('Bread',5,1),('Coffee',7,2),('Milk',10,4)] | [('Coffee',['Bread']),('Milk',['Bread','Coffee'])] | 12 |
| 6 | [('Firewood',15,1),('Soap',20,3)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 13 |
| 7 | [('Bread',5,2),('Coffee',7,1),('Milk',10,1)] | [('Coffee',['Bread']),('Milk',['Bread','Coffee'])] | 14 |
| 8 | [('Firewood',15,5),('Soap',20,1)] | [('Firewood',['Bread','Coffee','Milk']),('Soap',['Bread','Coffee','Firewood','Milk'])] | 11 |
+-------------+----------------------------------------------+----------------------------------------------------------------------------------------+----------+
3 changes: 2 additions & 1 deletion integration_tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def RunAll(test_presto=False, test_trino=False, test_clingo=True, test_clickhous
RunTest("dialects/trino/joins_test")
RunTest("dialects/trino/joins_test")

if test_clickhouse:
if test_clickhouse or logica_test.TestManager.RUN_ONLY:
RunTest("dialects/clickhouse/shipping_funfacts_test")
RunTest("dialects/clickhouse/purchase_test")

if test_clingo:
from common import duckdb_logica
Expand Down