Skip to content

Commit

Permalink
Merge pull request #7 from dorosch/resolve/5
Browse files Browse the repository at this point in the history
Added support of chunk_time_interval parameter
  • Loading branch information
dorosch authored Jul 3, 2023
2 parents b103e57 + 9a705ab commit 4c85fb6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ class Metric(Base):
)
```

## Parameters

* [chunk_time_interval](6)

## Functions

Timescaledb functions implemented:

### [first(value, time)][6]
### [first(value, time)][7]

```Python
func.first(Metric.value, Metric.timestamp)
```

### [last(value, time)][7]
### [last(value, time)][8]

```Python
func.last(Metric.value, Metric.timestamp)
Expand All @@ -85,5 +89,6 @@ func.last(Metric.value, Metric.timestamp)
[3]: https://codecov.io/gh/dorosch/sqlalchemy-timescaledb
[4]: https://pepy.tech/project/sqlalchemy-timescaledb
[5]: https://docs.timescale.com/api/latest/hypertable/create_hypertable/#optional-arguments
[6]: https://docs.timescale.com/api/latest/hyperfunctions/first/
[7]: https://docs.timescale.com/api/latest/hyperfunctions/last/
[6]: https://docs.timescale.com/api/latest/hypertable/set_chunk_time_interval/
[7]: https://docs.timescale.com/api/latest/hyperfunctions/first/
[8]: https://docs.timescale.com/api/latest/hyperfunctions/last/
12 changes: 11 additions & 1 deletion sqlalchemy_timescaledb/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ def post_create_table(self, table):

@staticmethod
def ddl_hypertable(table_name, hypertable):
time_column_name = hypertable['time_column_name']
chunk_time_interval = hypertable.get('chunk_time_interval', '7 days')

if isinstance(chunk_time_interval, str):
if chunk_time_interval.isdigit():
chunk_time_interval = int(chunk_time_interval)
else:
chunk_time_interval = f"INTERVAL '{chunk_time_interval}'"

return DDL(
f"""
SELECT create_hypertable(
'{table_name}',
'{hypertable['time_column_name']}',
'{time_column_name}',
chunk_time_interval => {chunk_time_interval},
if_not_exists => TRUE
);
"""
Expand Down
43 changes: 43 additions & 0 deletions tests/test_ddl_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest
from sqlalchemy import DDL

from sqlalchemy_timescaledb.dialect import TimescaledbDDLCompiler


class TestTimescaledbDDLCompiler:
def test_default_params(self):
assert TimescaledbDDLCompiler.ddl_hypertable(
'test', {'time_column_name': 'timestamp'}
).compile().string == DDL(
f"""
SELECT create_hypertable(
'test',
'timestamp',
chunk_time_interval => INTERVAL '7 days',
if_not_exists => TRUE
);
"""
).compile().string

@pytest.mark.parametrize('interval,expected', [
('1 days', "INTERVAL '1 days'"),
('7 hour', "INTERVAL '7 hour'"),
(86400, 86400),
('86400', 86400)
])
def test_chunk_time_interval(self, interval, expected):
assert TimescaledbDDLCompiler.ddl_hypertable(
'test', {
'time_column_name': 'timestamp',
'chunk_time_interval': interval
}
).compile().string == DDL(
f"""
SELECT create_hypertable(
'test',
'timestamp',
chunk_time_interval => {expected},
if_not_exists => TRUE
);
"""
).compile().string

0 comments on commit 4c85fb6

Please sign in to comment.