From b61d5eadb72635c356c6d8615d3bea4273e599cf Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Thu, 14 May 2026 08:38:22 +0530 Subject: [PATCH 1/4] chore: add Corestore tutorial base dataset Co-Authored-By: Claude Sonnet 4.6 --- demos/tutorial_corestore_setup.txt | 91 ++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 demos/tutorial_corestore_setup.txt diff --git a/demos/tutorial_corestore_setup.txt b/demos/tutorial_corestore_setup.txt new file mode 100644 index 0000000000..4d9ffb891e --- /dev/null +++ b/demos/tutorial_corestore_setup.txt @@ -0,0 +1,91 @@ +# ================================================================ +# Corestore Tutorial Dataset +# ================================================================ +# Base setup for the Minigraf Datalog tutorial series. +# All tutorial sections assume this has been loaded first. +# +# Run with: cargo run < demos/tutorial_corestore_setup.txt +# +# After loading: tx_count = 3 +# ================================================================ + +# ── tx 1: Category hierarchy ───────────────────────────────────── +# Electronics +# ├── Laptops +# ├── Mobile +# ├── Audio +# │ └── Headphones +# │ └── Noise-Cancelling +# └── Accessories + +(transact [ + [:cat-electronics :category/name "Electronics"] + [:cat-laptops :category/name "Laptops"] + [:cat-laptops :category/parent :cat-electronics] + [:cat-mobile :category/name "Mobile"] + [:cat-mobile :category/parent :cat-electronics] + [:cat-audio :category/name "Audio"] + [:cat-audio :category/parent :cat-electronics] + [:cat-headphones :category/name "Headphones"] + [:cat-headphones :category/parent :cat-audio] + [:cat-nc :category/name "Noise-Cancelling"] + [:cat-nc :category/parent :cat-headphones] + [:cat-accessories :category/name "Accessories"] + [:cat-accessories :category/parent :cat-electronics] +]) + +# ── tx 2: Product catalog ───────────────────────────────────────── +# Two products per major leaf category for window function examples. + +(transact [ + [:laptop-pro :product/name "LaptopPro 15"] + [:laptop-pro :product/sku "LP-15"] + [:laptop-pro :product/price 1299] + [:laptop-pro :product/category :cat-laptops] + + [:laptop-budget :product/name "BudgetBook 14"] + [:laptop-budget :product/sku "LB-14"] + [:laptop-budget :product/price 699] + [:laptop-budget :product/category :cat-laptops] + + [:phone-x :product/name "PhoneX 12"] + [:phone-x :product/sku "PX-12"] + [:phone-x :product/price 799] + [:phone-x :product/category :cat-mobile] + + [:phone-prev :product/name "PhoneX 11"] + [:phone-prev :product/sku "PX-11"] + [:phone-prev :product/price 599] + [:phone-prev :product/category :cat-mobile] + + [:nc-headphones :product/name "NoiseCancel Pro"] + [:nc-headphones :product/sku "NC-PRO"] + [:nc-headphones :product/price 249] + [:nc-headphones :product/category :cat-nc] + + [:usb-cable :product/name "USB-C Cable 2m"] + [:usb-cable :product/sku "USB-C-2M"] + [:usb-cable :product/price 19] + [:usb-cable :product/category :cat-accessories] + + [:keyboard-k1 :product/name "Compact Keyboard"] + [:keyboard-k1 :product/sku "KB-K1"] + [:keyboard-k1 :product/price 89] + [:keyboard-k1 :product/category :cat-accessories] + + [:monitor-27 :product/name "ClearView 27\" Monitor"] + [:monitor-27 :product/sku "CV-27"] + [:monitor-27 :product/price 449] + [:monitor-27 :product/category :cat-electronics] +]) + +# ── tx 3: Customers ─────────────────────────────────────────────── + +(transact [ + [:alice :customer/name "Alice"] + [:alice :customer/email "alice@example.com"] + [:ben :customer/name "Ben"] + [:ben :customer/email "ben@example.com"] + [:clara :customer/name "Clara"] + [:clara :customer/email "clara@example.com"] +]) From 7bd4b7fd74a4bb9c8c032c1cb0f15d5639791a85 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Thu, 14 May 2026 10:03:31 +0530 Subject: [PATCH 2/4] chore: add tutorial UDF example (section 10) Adds examples/tutorial_udfs.rs demonstrating register_predicate (valid-promo? filter) and register_aggregate (delivery-score float ratio) with verified output matching the Section 10 wiki page. Co-Authored-By: Claude Sonnet 4.6 --- examples/tutorial_udfs.rs | 171 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 examples/tutorial_udfs.rs diff --git a/examples/tutorial_udfs.rs b/examples/tutorial_udfs.rs new file mode 100644 index 0000000000..e0305e033e --- /dev/null +++ b/examples/tutorial_udfs.rs @@ -0,0 +1,171 @@ +//! Tutorial Section 10 — User-Defined Functions +//! +//! Demonstrates `register_predicate` and `register_aggregate` in the context +//! of the Corestore e-commerce scenario. +//! +//! Run with: +//! cargo run --example tutorial_udfs + +use minigraf::{Minigraf, QueryResult, Value}; + +fn main() -> anyhow::Result<()> { + let db = Minigraf::in_memory()?; + + // ── Seed data ──────────────────────────────────────────────────────────── + // + // Promo codes: one valid Corestore code, one invalid short code, one from + // a different scheme. + + db.execute(r#"(transact [ + [:promo-1 :promo/code "CORESTORE-SUMMER2026"] + [:promo-2 :promo/code "SAVE10"] + [:promo-3 :promo/code "PARTNER-EXCLUSIVE"] + ])"#)?; + + // Customers and their orders with on-time-flag attributes. + // Alice: order-a (on time), order-b (late) → score = 1/2 = 0.5 + // Ben: order-c (on time) → score = 1/1 = 1.0 + + db.execute(r#"(transact [ + [:alice :customer/name "Alice"] + [:ben :customer/name "Ben"] + [:order-a :order/customer :alice] + [:order-a :order/on-time-flag 1] + [:order-b :order/customer :alice] + [:order-b :order/on-time-flag 0] + [:order-c :order/customer :ben] + [:order-c :order/on-time-flag 1] + ])"#)?; + + // ── Predicate UDF: valid-promo? ─────────────────────────────────────────── + // + // A valid Corestore promo code must start with "CORESTORE-" and be at + // least 15 characters long. + + db.register_predicate( + "valid-promo?", + |v: &Value| { + if let Value::String(s) = v { + s.starts_with("CORESTORE-") && s.len() >= 15 + } else { + false + } + }, + )?; + + println!("=== Predicate UDF: valid-promo? ==="); + println!(); + println!("Query: find promo codes that satisfy valid-promo?"); + println!(); + println!(" (query [:find ?code"); + println!(" :where [?p :promo/code ?code]"); + println!(" [(valid-promo? ?code)]])"); + println!(); + + let promo_result = db.execute( + r#"(query [:find ?code + :where [?p :promo/code ?code] + [(valid-promo? ?code)]])"#, + )?; + + let (promo_rows, promo_count) = match promo_result { + QueryResult::QueryResults { results, .. } => { + let n = results.len(); + (results, n) + } + _ => (vec![], 0), + }; + + println!("?code"); + println!("--------------------"); + for row in &promo_rows { + for val in row { + match val { + Value::String(s) => print!("\"{}\"", s), + other => print!("{:?}", other), + } + } + println!(); + } + println!(); + println!("{} result(s) found.", promo_count); + println!(); + + // ── Aggregate UDF: delivery-score ──────────────────────────────────────── + // + // delivery-score receives a stream of integer flags (1 = on time, 0 = late) + // and returns on_time_count / total_count as a float. + // + // Accumulator state: (on_time_count: i64, total_count: i64) + + db.register_aggregate( + "delivery-score", + || (0i64, 0i64), + |state: &mut (i64, i64), val: &Value| { + if let Value::Integer(flag) = val { + state.1 += 1; + if *flag == 1 { + state.0 += 1; + } + } + }, + |state: &(i64, i64), _n: usize| { + if state.1 == 0 { + Value::Null + } else { + Value::Float(state.0 as f64 / state.1 as f64) + } + }, + )?; + + println!("=== Aggregate UDF: delivery-score ==="); + println!(); + println!("Query: on-time delivery score per customer"); + println!(); + println!(" (query [:find ?name (delivery-score ?flag)"); + println!(" :where [?customer :customer/name ?name]"); + println!(" [?order :order/customer ?customer]"); + println!(" [?order :order/on-time-flag ?flag]])"); + println!(); + + let score_result = db.execute( + r#"(query [:find ?name (delivery-score ?flag) + :where [?customer :customer/name ?name] + [?order :order/customer ?customer] + [?order :order/on-time-flag ?flag]])"#, + )?; + + let (score_rows, score_count) = match score_result { + QueryResult::QueryResults { results, .. } => { + let n = results.len(); + (results, n) + } + _ => (vec![], 0), + }; + + println!("?name (delivery-score ?flag)"); + println!("------------------------------------"); + for row in &score_rows { + let name = match &row[0] { + Value::String(s) => format!("\"{}\"", s), + other => format!("{:?}", other), + }; + let score = match &row[1] { + Value::Float(f) => { + // Always show at least one decimal place for clarity + if f.fract() == 0.0 { + format!("{:.1}", f) + } else { + format!("{}", f) + } + } + Value::Null => "null".to_string(), + other => format!("{:?}", other), + }; + println!("{:<10}{}", name, score); + } + println!(); + println!("{} result(s) found.", score_count); + + Ok(()) +} From 9c947fe19bfeb748fd1bc2b69576a40c0b80e818 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Thu, 14 May 2026 14:36:06 +0530 Subject: [PATCH 3/4] chore: add window UDF example to tutorial_udfs.rs Co-Authored-By: Claude Sonnet 4.6 --- examples/tutorial_udfs.rs | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/examples/tutorial_udfs.rs b/examples/tutorial_udfs.rs index e0305e033e..de48521050 100644 --- a/examples/tutorial_udfs.rs +++ b/examples/tutorial_udfs.rs @@ -167,5 +167,51 @@ fn main() -> anyhow::Result<()> { println!(); println!("{} result(s) found.", score_count); + // ── UDF aggregate in a window clause ───────────────────────────────────────── + println!(); + println!("=== UDF aggregate in a window clause ==="); + println!(); + println!("Query: annotate each order with its customer delivery score"); + println!(); + + let window_result = db.execute( + r#"(query [:find ?order (delivery-score ?flag :over (:partition-by ?customer :order-by ?order)) + :where [?customer :customer/name ?name] + [?order :order/customer ?customer] + [?order :order/on-time-flag ?flag]])"#, + )?; + + let (window_rows, window_count) = match window_result { + QueryResult::QueryResults { results, .. } => { + let n = results.len(); + (results, n) + } + _ => (vec![], 0), + }; + + println!("?order (delivery-score ?flag :over ...)"); + println!("--------------------------------------------"); + for row in &window_rows { + let order = match &row[0] { + Value::Keyword(k) => format!(":{}", k), + Value::Ref(u) => format!("{}", &u.to_string()[..8]), + other => format!("{:?}", other), + }; + let score = match &row[1] { + Value::Float(f) => { + if f.fract() == 0.0 { + format!("{:.1}", f) + } else { + format!("{}", f) + } + } + Value::Null => "null".to_string(), + other => format!("{:?}", other), + }; + println!("{:<12}{}", order, score); + } + println!(); + println!("{} result(s) found.", window_count); + Ok(()) } From 93fd6acd7f7121cd338de66097886086a6596b9a Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Thu, 14 May 2026 14:46:04 +0530 Subject: [PATCH 4/4] chore: rustfmt tutorial_udfs.rs --- examples/tutorial_udfs.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/tutorial_udfs.rs b/examples/tutorial_udfs.rs index de48521050..4d5a6f7f2d 100644 --- a/examples/tutorial_udfs.rs +++ b/examples/tutorial_udfs.rs @@ -16,17 +16,20 @@ fn main() -> anyhow::Result<()> { // Promo codes: one valid Corestore code, one invalid short code, one from // a different scheme. - db.execute(r#"(transact [ + db.execute( + r#"(transact [ [:promo-1 :promo/code "CORESTORE-SUMMER2026"] [:promo-2 :promo/code "SAVE10"] [:promo-3 :promo/code "PARTNER-EXCLUSIVE"] - ])"#)?; + ])"#, + )?; // Customers and their orders with on-time-flag attributes. // Alice: order-a (on time), order-b (late) → score = 1/2 = 0.5 // Ben: order-c (on time) → score = 1/1 = 1.0 - db.execute(r#"(transact [ + db.execute( + r#"(transact [ [:alice :customer/name "Alice"] [:ben :customer/name "Ben"] [:order-a :order/customer :alice] @@ -35,23 +38,21 @@ fn main() -> anyhow::Result<()> { [:order-b :order/on-time-flag 0] [:order-c :order/customer :ben] [:order-c :order/on-time-flag 1] - ])"#)?; + ])"#, + )?; // ── Predicate UDF: valid-promo? ─────────────────────────────────────────── // // A valid Corestore promo code must start with "CORESTORE-" and be at // least 15 characters long. - db.register_predicate( - "valid-promo?", - |v: &Value| { - if let Value::String(s) = v { - s.starts_with("CORESTORE-") && s.len() >= 15 - } else { - false - } - }, - )?; + db.register_predicate("valid-promo?", |v: &Value| { + if let Value::String(s) = v { + s.starts_with("CORESTORE-") && s.len() >= 15 + } else { + false + } + })?; println!("=== Predicate UDF: valid-promo? ==="); println!();