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

TTL param for engine #111

Merged
merged 5 commits into from
Nov 30, 2020
Merged

TTL param for engine #111

merged 5 commits into from
Nov 30, 2020

Conversation

aamalev
Copy link
Contributor

@aamalev aamalev commented Nov 28, 2020

Implementation for TTL

CREATE TABLE example_table
(
    d DateTime,
    a Int
)
ENGINE = MergeTree
PARTITION BY toYYYYMM(d)
ORDER BY d
TTL d + INTERVAL 1 MONTH [DELETE],
    d + INTERVAL 1 WEEK TO VOLUME 'aaa',
    d + INTERVAL 2 WEEK TO DISK 'bbb';

Usage

    from clickhouse_sqlalchemy.sql.ddl import ttl_delete, ttl_to_disk, ttl_to_volume

    class TestTable(self.base):
        date = Column(types.Date, primary_key=True)
        x = Column(types.Int32)

        __table_args__ = (
            engines.MergeTree(
                ttl=[
                    ttl_delete(date + func.toIntervalDay(1)),
                    ttl_to_disk(date + func.toIntervalDay(1), 'hdd'),
                    ttl_to_volume(date + func.toIntervalDay(1), 'slow'),
                ],
            ),
        )

@xzkostyan
Copy link
Owner

Good job. Please fix flake8 and we'll merge this.

I don't know why travis do not post checks in PR's anymore.

@xzkostyan
Copy link
Owner

It would be great if reflection of TTL clause will be supported as well.

CREATE TABLE test_table (date Date, x Int32) ENGINE = MergeTree() ORDER BY tuple() TTL date + toIntervalDay(1);

CREATE TABLE test_table
(
    `date` Date,
    `x` Int32
)
ENGINE = MergeTree()
ORDER BY tuple()
TTL date + toIntervalDay(1)

Ok.

0 rows in set. Elapsed: 0.007 sec. 
select * from system.tables where name = 'test_table';

SELECT *
FROM system.tables
WHERE name = 'test_table'

Row 1:
──────
database:                   default
name:                       test_table
uuid:                       00000000-0000-0000-0000-000000000000
engine:                     MergeTree
is_temporary:               0
data_paths:                 ['/.../data/default/test_table/']
metadata_path:              /.../metadata/default/test_table.sql
metadata_modification_time: 2020-11-29 15:47:07
dependencies_database:      []
dependencies_table:         []
create_table_query:         CREATE TABLE default.test_table (`date` Date, `x` Int32) ENGINE = MergeTree() ORDER BY tuple() TTL date + toIntervalDay(1) SETTINGS index_granularity = 8192
engine_full:                MergeTree() ORDER BY tuple() TTL date + toIntervalDay(1) SETTINGS index_granularity = 8192
partition_key:              
sorting_key:                
primary_key:                
sampling_key:               
storage_policy:             default
total_rows:                 0
total_bytes:                0
lifetime_rows:              ᴺᵁᴸᴸ
lifetime_bytes:             ᴺᵁᴸᴸ

1 rows in set. Elapsed: 0.067 sec. 

Few highlights for it:

  • add engine_full parameter to method clickhouse_sqlalchemy.engines.mergetree.MergeTree._reflect_merge_tree signature;
  • check for ttl in engine_full;
  • parse TTL clause or simply wrap it in sqlalchemy.text();
  • add it to the engine constructor arguments rv.

@coveralls
Copy link

coveralls commented Nov 29, 2020

Coverage Status

Coverage increased (+0.05%) to 95.586% when pulling 9b625e2 on aamalev:ttl into 3c7277d on xzkostyan:master.

@xzkostyan xzkostyan merged commit eb43faa into xzkostyan:master Nov 30, 2020
xzkostyan added a commit that referenced this pull request Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants