From e74f838f1b17d1487de142ec729a120469d4f480 Mon Sep 17 00:00:00 2001 From: Kyle Goodrick Date: Mon, 13 Jan 2025 22:19:09 -0700 Subject: [PATCH 1/6] Add mermaid generation to rust --- crates/polars-lazy/Cargo.toml | 2 + crates/polars-lazy/src/lib.rs | 5 ++- crates/polars-lazy/src/mermaid.rs | 40 +++++++++++++++++++ crates/polars-plan/Cargo.toml | 2 + crates/polars-python/Cargo.toml | 1 + crates/polars-python/src/lazyframe/general.rs | 5 +++ crates/polars/Cargo.toml | 2 + crates/polars/src/lib.rs | 1 + docs/source/user-guide/installation.md | 1 + 9 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 crates/polars-lazy/src/mermaid.rs diff --git a/crates/polars-lazy/Cargo.toml b/crates/polars-lazy/Cargo.toml index ed172a178cb1..866cd61111c4 100644 --- a/crates/polars-lazy/Cargo.toml +++ b/crates/polars-lazy/Cargo.toml @@ -226,6 +226,7 @@ dynamic_group_by = [ ewma = ["polars-plan/ewma"] ewma_by = ["polars-plan/ewma_by"] dot_diagram = ["polars-plan/dot_diagram"] +mermaid_diagram = ["polars-plan/mermaid_diagram"] diagonal_concat = [] unique_counts = ["polars-plan/unique_counts"] log = ["polars-plan/log"] @@ -382,6 +383,7 @@ features = [ "list_to_struct", "log", "merge_sorted", + "mermaid_diagram", "meta", "mode", "moment", diff --git a/crates/polars-lazy/src/lib.rs b/crates/polars-lazy/src/lib.rs index f3dff5710170..8bf61d4ed752 100644 --- a/crates/polars-lazy/src/lib.rs +++ b/crates/polars-lazy/src/lib.rs @@ -200,13 +200,16 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] extern crate core; -#[cfg(feature = "dot_diagram")] +#[cfg(any(feature = "dot_diagram", feature = "mermaid_diagram"))] mod dot; pub mod dsl; pub mod frame; pub mod physical_plan; pub mod prelude; +#[cfg(feature = "mermaid_diagram")] +mod mermaid; + mod scan; #[cfg(test)] mod tests; diff --git a/crates/polars-lazy/src/mermaid.rs b/crates/polars-lazy/src/mermaid.rs new file mode 100644 index 000000000000..496213282d46 --- /dev/null +++ b/crates/polars-lazy/src/mermaid.rs @@ -0,0 +1,40 @@ +use polars_core::{export::regex, prelude::*}; + +use crate::prelude::*; + +impl LazyFrame { + pub fn to_mermaid(&self, optimized: bool) -> PolarsResult { + // Mermaid strings are very similar to dot strings, so + // we can reuse the dot implementation. + let dot = self.to_dot(optimized)?; + + let edge_regex = regex::Regex::new(r"(?P\w+) -- (?P\w+)").unwrap(); + let node_regex = regex::Regex::new(r#"(?P\w+)(\s+)?\[label="(?P