Skip to content

Commit 7baf171

Browse files
committed
feat(let): add strftime function
1 parent 6e9d321 commit 7baf171

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

python/xorq/backends/let/datafusion/compiler.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class DataFusionCompiler(SQLGlotCompiler):
9393
ops.CountDistinctStar,
9494
ops.DateDelta,
9595
ops.RowID,
96-
ops.Strftime,
9796
ops.TimeDelta,
9897
ops.TimestampDelta,
9998
)
@@ -664,5 +663,8 @@ def visit_ArrayAny(self, op, *, arg):
664663
),
665664
)
666665

666+
def visit_Strftime(self, op, *, arg, format_str):
667+
return self.f.temporal_strftime(arg, format_str)
668+
667669

668670
compiler = DataFusionCompiler()

python/xorq/backends/let/datafusion/udfs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,15 @@ def regex_split(s: str, pattern: str) -> list[str]:
129129
)
130130
pattern = patterns[0].as_py()
131131
return pc.split_pattern_regex(s, pattern)
132+
133+
134+
def temporal_strftime(array: dt.Timestamp(scale=9), pattern: str) -> dt.string:
135+
patterns = pattern.unique()
136+
137+
if len(patterns) != 1:
138+
raise com.XorqError(
139+
"Only a single scalar pattern is supported for DataFusion strftime"
140+
)
141+
142+
pattern = patterns[0].as_py()
143+
return pc.strftime(array, pattern)

python/xorq/backends/let/tests/test_temporal.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ def test_timestamp_extract_week_of_year(alltypes, alltypes_df):
158158
assert_series_equal(result, expected)
159159

160160

161+
@pytest.mark.parametrize(
162+
("expr_fn", "pandas_pattern"),
163+
[
164+
param(
165+
lambda t: t.timestamp_col.strftime("%Y%m%d").name("formatted"),
166+
"%Y%m%d",
167+
id="literal_format_str",
168+
),
169+
],
170+
)
171+
def test_strftime(alltypes, alltypes_df, expr_fn, pandas_pattern):
172+
expr = expr_fn(alltypes)
173+
expected = alltypes_df.timestamp_col.dt.strftime(pandas_pattern).rename("formatted")
174+
175+
result = expr.execute()
176+
assert_series_equal(result, expected)
177+
178+
161179
PANDAS_UNITS = {
162180
"m": "Min",
163181
"ms": "L",

0 commit comments

Comments
 (0)