Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pkey name overflow in _drop_primary_key #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion timescale/db/backends/postgis/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def _drop_primary_key(self, model):
"""
db_table = model._meta.db_table
table = self.quote_name(db_table)
pkey = self.quote_name(f'{db_table}_pkey')
pkey_length = self.connection.ops.max_name_length()
pkey = self.quote_name(f'{db_table[:pkey_length - 5]}_pkey')

sql = self.sql_drop_primary_key.format(table=table, pkey=pkey)

Expand Down
3 changes: 2 additions & 1 deletion timescale/db/backends/postgresql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def _drop_primary_key(self, model):
"""
db_table = model._meta.db_table
table = self.quote_name(db_table)
pkey = self.quote_name(f'{db_table}_pkey')
pkey_length = self.connection.ops.max_name_length()
pkey = self.quote_name(f'{db_table[:pkey_length - 5]}_pkey')

sql = self.sql_drop_primary_key.format(table=table, pkey=pkey)

Expand Down
14 changes: 14 additions & 0 deletions timescale/db/models/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def __init__(self, expression, min_value, max_value, bucket):
super().__init__(expression, min_value, max_value, bucket)


class LTTB(models.Func):
function = 'lttb'
name = 'lttb'
output_field = models.DateTimeField()

def __init__(self, time, value, count, field):
self.fieldname = field
super().__init__(time, value, count)

def as_sql(self, compiler, connection, **extra_context):
sql, params = super().as_sql(compiler, connection, **extra_context)
return f'(unnest({sql})).{self.fieldname}', params


class Last(models.Aggregate):
function = 'last'
name = 'last'
Expand Down
3 changes: 3 additions & 0 deletions timescale/db/models/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ def time_bucket_gapfill(self, field: str, interval: str, start: datetime, end: d

def histogram(self, field: str, min_value: float, max_value: float, num_of_buckets: int = 5):
return self.get_queryset().histogram(field, min_value, max_value, num_of_buckets)

def lttb(self, time: str, value: str, num_of_counts: int = 20):
return self.get_queryset().lttb(time, value, num_of_counts)
11 changes: 10 additions & 1 deletion timescale/db/models/querysets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from timescale.db.models.expressions import TimeBucket, TimeBucketGapFill, TimeBucketNG
from timescale.db.models.aggregates import Histogram
from timescale.db.models.aggregates import Histogram, LTTB
from typing import Dict, Optional
from datetime import datetime

Expand Down Expand Up @@ -35,6 +35,15 @@ def histogram(self, field: str, min_value: float, max_value: float, num_of_bucke
"""
return self.values(histogram=Histogram(field, min_value, max_value, num_of_buckets))

def lttb(self, time: str, value: str, num_of_counts: int = 20):
"""
Wraps the TimescaleDB toolkit lttb function into a queryset method.
"""
return self.values(
lttb_t=LTTB(time, value, num_of_counts, time),
lttb_v=LTTB(time, value, num_of_counts, value)
)

def to_list(self, normalise_datetimes: bool = False):
if normalise_datetimes:
normalised = []
Expand Down