File tree Expand file tree Collapse file tree 3 files changed +63
-5
lines changed
Expand file tree Collapse file tree 3 files changed +63
-5
lines changed Original file line number Diff line number Diff line change @@ -63,17 +63,21 @@ class Metric(Base):
6363 )
6464```
6565
66+ ## Parameters
67+
68+ * [ chunk_time_interval] ( 6 )
69+
6670## Functions
6771
6872Timescaledb functions implemented:
6973
70- ### [ first(value, time)] [ 6 ]
74+ ### [ first(value, time)] [ 7 ]
7175
7276``` Python
7377func.first(Metric.value, Metric.timestamp)
7478```
7579
76- ### [ last(value, time)] [ 7 ]
80+ ### [ last(value, time)] [ 8 ]
7781
7882``` Python
7983func.last(Metric.value, Metric.timestamp)
@@ -85,5 +89,6 @@ func.last(Metric.value, Metric.timestamp)
8589[ 3 ] : https://codecov.io/gh/dorosch/sqlalchemy-timescaledb
8690[ 4 ] : https://pepy.tech/project/sqlalchemy-timescaledb
8791[ 5 ] : https://docs.timescale.com/api/latest/hypertable/create_hypertable/#optional-arguments
88- [ 6 ] : https://docs.timescale.com/api/latest/hyperfunctions/first/
89- [ 7 ] : https://docs.timescale.com/api/latest/hyperfunctions/last/
92+ [ 6 ] : https://docs.timescale.com/api/latest/hypertable/set_chunk_time_interval/
93+ [ 7 ] : https://docs.timescale.com/api/latest/hyperfunctions/first/
94+ [ 8 ] : https://docs.timescale.com/api/latest/hyperfunctions/last/
Original file line number Diff line number Diff line change @@ -33,11 +33,21 @@ def post_create_table(self, table):
3333
3434 @staticmethod
3535 def ddl_hypertable (table_name , hypertable ):
36+ time_column_name = hypertable ['time_column_name' ]
37+ chunk_time_interval = hypertable .get ('chunk_time_interval' , '7 days' )
38+
39+ if isinstance (chunk_time_interval , str ):
40+ if chunk_time_interval .isdigit ():
41+ chunk_time_interval = int (chunk_time_interval )
42+ else :
43+ chunk_time_interval = f"INTERVAL '{ chunk_time_interval } '"
44+
3645 return DDL (
3746 f"""
3847 SELECT create_hypertable(
3948 '{ table_name } ',
40- '{ hypertable ['time_column_name' ]} ',
49+ '{ time_column_name } ',
50+ chunk_time_interval => { chunk_time_interval } ,
4151 if_not_exists => TRUE
4252 );
4353 """
Original file line number Diff line number Diff line change 1+ import pytest
2+ from sqlalchemy import DDL
3+
4+ from sqlalchemy_timescaledb .dialect import TimescaledbDDLCompiler
5+
6+
7+ class TestTimescaledbDDLCompiler :
8+ def test_default_params (self ):
9+ assert TimescaledbDDLCompiler .ddl_hypertable (
10+ 'test' , {'time_column_name' : 'timestamp' }
11+ ).compile ().string == DDL (
12+ f"""
13+ SELECT create_hypertable(
14+ 'test',
15+ 'timestamp',
16+ chunk_time_interval => INTERVAL '7 days',
17+ if_not_exists => TRUE
18+ );
19+ """
20+ ).compile ().string
21+
22+ @pytest .mark .parametrize ('interval,expected' , [
23+ ('1 days' , "INTERVAL '1 days'" ),
24+ ('7 hour' , "INTERVAL '7 hour'" ),
25+ (86400 , 86400 ),
26+ ('86400' , 86400 )
27+ ])
28+ def test_chunk_time_interval (self , interval , expected ):
29+ assert TimescaledbDDLCompiler .ddl_hypertable (
30+ 'test' , {
31+ 'time_column_name' : 'timestamp' ,
32+ 'chunk_time_interval' : interval
33+ }
34+ ).compile ().string == DDL (
35+ f"""
36+ SELECT create_hypertable(
37+ 'test',
38+ 'timestamp',
39+ chunk_time_interval => { expected } ,
40+ if_not_exists => TRUE
41+ );
42+ """
43+ ).compile ().string
You can’t perform that action at this time.
0 commit comments