This is an automated email from the ASF dual-hosted git repository.

vavila pushed a commit to branch feat/to_datetime_jinja_filter
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/feat/to_datetime_jinja_filter 
by this push:
     new 4c730a9557 Addressing PR feedback
4c730a9557 is described below

commit 4c730a955773a48a2b42d81a3c8c4a027f5e5e35
Author: Vitor Avila <[email protected]>
AuthorDate: Mon Mar 24 16:23:28 2025 -0300

    Addressing PR feedback
---
 docs/docs/configuration/sql-templating.mdx | 34 ++++++++++++++
 tests/unit_tests/jinja_context_test.py     | 71 ++++++++++++++++--------------
 2 files changed, 72 insertions(+), 33 deletions(-)

diff --git a/docs/docs/configuration/sql-templating.mdx 
b/docs/docs/configuration/sql-templating.mdx
index 0e618fd9c1..8364ca01a8 100644
--- a/docs/docs/configuration/sql-templating.mdx
+++ b/docs/docs/configuration/sql-templating.mdx
@@ -461,3 +461,37 @@ This macro avoids copy/paste, allowing users to centralize 
the metric definition
 
 The `dataset_id` parameter is optional, and if not provided Superset will use 
the current dataset from context (for example, when using this macro in the 
Chart Builder, by default the `macro_key` will be searched in the dataset 
powering the chart).
 The parameter can be used in SQL Lab, or when fetching a metric from another 
dataset.
+
+## Available Macros
+
+Superset supports [builtin filters from the Jinja2 templating 
package](https://jinja.palletsprojects.com/en/stable/templates/#builtin-filters).
 Custom filters have also been implemented:
+
+**Where In**
+Parses a list into a SQL-compatible statement. This is useful with macros that 
return an array (for example the `filter_values` macro):
+
+```
+Dashboard filter with "First", "Second" and "Third" options selected
+{{ filter_values('column') }} => ["First", "Second", "Third"]
+{{ filter_values('column')|where_in }} => ('First', 'Second', 'Third')
+```
+
+By default, this filter returns `()` (as a string) in case the value is null. 
The `default_to_none` parameter can be se to `True` to return null in this case:
+
+```
+Dashboard filter without any value applied
+{{ filter_values('column') }} => ()
+{{ filter_values('column')|where_in(default_to_none=True) }} => None
+```
+
+**To Datetime**
+
+Loads a string as a `datetime` object. This is useful when performing date 
operations. For example:
+```
+{% set from_expr = get_time_filter("dttm", strftime="%Y-%m-%d").from_expr %}
+{% set to_expr = get_time_filter("dttm", strftime="%Y-%m-%d").to_expr %}
+{% if (to_expr|to_datetime(format="%Y-%m-%d") - 
from_expr|to_datetime(format="%Y-%m-%d")).days > 100 %}
+   do something
+{% else %}
+   do something else
+{% endif %}
+```
diff --git a/tests/unit_tests/jinja_context_test.py 
b/tests/unit_tests/jinja_context_test.py
index 8764aafc92..2123bed4ac 100644
--- a/tests/unit_tests/jinja_context_test.py
+++ b/tests/unit_tests/jinja_context_test.py
@@ -431,52 +431,57 @@ def test_where_in_empty_list() -> None:
     assert where_in([], default_to_none=True) is None
 
 
-def test_to_datetime() -> None:
[email protected](
+    "value,format,output",
+    [
+        ("2025-03-20 15:55:00", None, datetime(2025, 3, 20, 15, 55)),
+        (None, None, None),
+        ("2025-03-20", "%Y-%m-%d", datetime(2025, 3, 20)),
+        ("'2025-03-20'", "%Y-%m-%d", datetime(2025, 3, 20)),
+    ],
+)
+def test_to_datetime(
+    value: str | None, format: str | None, output: datetime | None
+) -> None:
     """
     Test the ``to_datetime`` Jinja2 filter.
     """
 
-    result = to_datetime("2025-03-20 15:55:00")
-    assert result == datetime(2025, 3, 20, 15, 55)
-
-    assert to_datetime(None) is None
-
-
-def test_to_datetime_custom_format() -> None:
-    """
-    Test the ``to_datetime`` Jinja2 filter when specifying a format.
-    """
-    result = to_datetime("2025-03-20", format="%Y-%m-%d")
-    assert result == datetime(2025, 3, 20)
-
-
-def test_to_datetime_including_quotes() -> None:
-    """
-    Test the ``to_datetime`` Jinja2 when a string wrapped
-    in quotes is used.
-
-    This might happen when passing a value from a temporal macro.
-    """
-    result = to_datetime("'2025-03-20'", format="%Y-%m-%d")
-    assert result == datetime(2025, 3, 20)
+    result = (
+        to_datetime(value, format=format) if format is not None else 
to_datetime(value)
+    )
+    assert result == output
 
 
-def test_to_datetime_raises() -> None:
[email protected](
+    "value,format,match",
+    [
+        (
+            "2025-03-20",
+            None,
+            "time data '2025-03-20' does not match format '%Y-%m-%d %H:%M:%S'",
+        ),
+        (
+            "2025-03-20 15:55:00",
+            "%Y-%m-%d",
+            "unconverted data remains:  15:55:00",
+        ),
+    ],
+)
+def test_to_datetime_raises(value: str, format: str | None, match: str) -> 
None:
     """
     Test the ``to_datetime`` Jinja2 raises with an incorrect
     format.
     """
     with pytest.raises(
         ValueError,
-        match="time data '2025-03-20' does not match format '%Y-%m-%d 
%H:%M:%S'",
+        match=match,
     ):
-        to_datetime("2025-03-20")
-
-    with pytest.raises(
-        ValueError,
-        match="unconverted data remains:  15:55:00",
-    ):
-        to_datetime("2025-03-20 15:55:00", format="%Y-%m-%d")
+        (
+            to_datetime(value, format=format)
+            if format is not None
+            else to_datetime(value)
+        )
 
 
 def test_dataset_macro(mocker: MockerFixture) -> None:

Reply via email to