Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: sql parsing and polars date summaries (#2382)

Myles Scolnick3f10e32c76c6
diff --git a/frontend/src/core/codemirror/language/__tests__/sql.test.ts b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
index 846f882c00..01e5c91718 100644
--- a/frontend/src/core/codemirror/language/__tests__/sql.test.ts
+++ b/frontend/src/core/codemirror/language/__tests__/sql.test.ts
@@ -202,5 +202,40 @@ describe("SQLLanguageAdapter", () => {
),
).toBe(true);
});
+
+ it("should support SQL strings with output flag multi-line", () => {
+ expect(
+ adapter.isSupported(
+ `
+ countries = mo.sql(
+ f"""
+ SELECT * from "https://raw.githubusercontent.com/data.csv"
+ """,
+ output=False
+ )`.trim(),
+ ),
+ ).toBe(true);
+ expect(
+ adapter.isSupported(
+ `
+ countries = mo.sql(
+ f"""
+ SELECT * from "https://raw.githubusercontent.com/data.csv"
+ """,
+ output=False,
+ )`.trim(),
+ ),
+ ).toBe(true);
+ expect(
+ adapter.isSupported(
+ `
+ countries = mo.sql(
+ f"""
+ SELECT * from "https://raw.githubusercontent.com/data.csv"
+ """,
+ output=False)`.trim(),
+ ),
+ ).toBe(true);
+ });
});
});
diff --git a/frontend/src/core/codemirror/language/sql.ts b/frontend/src/core/codemirror/language/sql.ts
index 206d5887fa..97a1386a2b 100644
--- a/frontend/src/core/codemirror/language/sql.ts
+++ b/frontend/src/core/codemirror/language/sql.ts
@@ -39,7 +39,7 @@ const regexes = pairs.map(
[
start,
new RegExp(
- `^(?<dataframe>\\w*)\\s*=\\s*mo\\.sql\\(\\s*${start}(?<sql>.*)${end}\\s*(?:,\\s*output\\s*=\\s*(?<output>True|False))?\\s*\\)$`,
+ `^(?<dataframe>\\w*)\\s*=\\s*mo\\.sql\\(\\s*${start}(?<sql>.*)${end}\\s*(?:,\\s*output\\s*=\\s*(?<output>True|False),?)?\\s*\\)$`,
"s",
),
] as const,
diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.py
index 7a4c9be5e0..fd4c6eb861 100644
--- a/marimo/_plugins/ui/_impl/tables/polars_table.py
+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py
@@ -172,6 +172,15 @@ class PolarsTableManagerFactory(TableManagerFactory):
true=cast(int, col.sum()),
false=cast(int, total - col.sum()),
)
+ if col.dtype == pl.Date:
+ return ColumnSummary(
+ total=total,
+ nulls=col.null_count(),
+ min=cast(NonNestedLiteral, col.min()),
+ max=cast(NonNestedLiteral, col.max()),
+ mean=cast(NonNestedLiteral, col.mean()),
+ median=cast(NonNestedLiteral, col.median()),
+ )
if col.dtype.is_temporal():
return ColumnSummary(
total=total,
diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.py
index cb5ef52e16..e47d13ef05 100644
--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py
@@ -277,7 +277,7 @@ class TestPolarsTableManagerFactory(unittest.TestCase):
false=1,
)
- def test_summary_date(self) -> None:
+ def test_summary_datetime(self) -> None:
column = "E"
summary = self.manager.get_summary(column)
assert summary == ColumnSummary(
@@ -289,6 +289,25 @@ class TestPolarsTableManagerFactory(unittest.TestCase):
median=datetime.datetime(2021, 1, 2, 0, 0),
)
+ def test_summary_date(self) -> None:
+ import polars as pl
+
+ data = pl.DataFrame(
+ {
+ "A": [datetime.date(2021, 1, 1), datetime.date(2021, 1, 2)],
+ }
+ )
+ manager = self.factory.create()(data)
+ summary = manager.get_summary("A")
+ assert summary == ColumnSummary(
+ total=2,
+ nulls=0,
+ min=datetime.date(2021, 1, 1),
+ max=datetime.date(2021, 1, 2),
+ mean=datetime.datetime(2021, 1, 1, 12, 0),
+ median=datetime.datetime(2021, 1, 1, 12, 0),
+ )
+
def test_sort_values(self) -> None:
sorted_df = self.manager.sort_values("A", descending=True).data
expected_df = self.data.sort("A", descending=True)