|
| 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:); |
0 commit comments