Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.5.0

- feat: #129: Add `toYearWeek` datetime functionality

### 1.4.0

- feat: #119 Allow query results returned in columns and deserialized to `numpy` objects
Expand Down
23 changes: 23 additions & 0 deletions clickhouse_backend/models/functions/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"toYYYYMM",
"toYYYYMMDD",
"toYYYYMMDDhhmmss",
"toYearWeek",
]


Expand Down Expand Up @@ -77,3 +78,25 @@ class toStartOfFifteenMinutes(toStartOfMinute):

class toStartOfHour(toStartOfMinute):
pass


class toYearWeek(Func):
output_field = fields.UInt32Field()

def __init__(self, *expressions):
arity = len(expressions)
if arity < 1 or arity > 3:
raise TypeError(
"'%s' takes between 1 and 3 arguments (%s given)"
% (
self.__class__.__name__,
len(expressions),
)
)

expressions = (
expressions[0],
*(models.Value(expr) for expr in expressions[1:]),
)

super().__init__(*expressions)
23 changes: 23 additions & 0 deletions tests/clickhouse_functions/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def setUpTestData(cls):
datetime(2023, 11, 30, hour=16, minute=59, second=59), is_dst=False
),
)
cls.sarah = Author.objects.create(
name="Sarah Connor",
alias="sconnor",
birthday=pytz.utc.localize(
datetime(2023, 12, 31, hour=23, minute=30, second=00), is_dst=False
),
)

def test_yyyymm(self):
john = Author.objects.annotate(v=models.toYYYYMM("birthday")).get(
Expand Down Expand Up @@ -177,3 +184,19 @@ def test_tostartofhour(self):
elena.v,
datetime(2023, 11, 30, hour=10, minute=00, second=00),
)

def test_toyearweek(self):
sarah = Author.objects.annotate(v=models.toYearWeek("birthday")).get(
id=self.sarah.id
)
self.assertEqual(sarah.v, 202353)

sarah = Author.objects.annotate(v=models.toYearWeek("birthday", 1)).get(
id=self.sarah.id
)
self.assertEqual(sarah.v, 202352)

sarah = Author.objects.annotate(
v=models.toYearWeek("birthday", 1, "Pacific/Kiritimati")
).get(id=self.sarah.id)
self.assertEqual(sarah.v, 202401)
Loading