PNixx / clickhouse-activerecord

A Ruby database ActiveRecord driver for ClickHouse

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduce `final` function for select queries

Loriowar opened this issue · comments

In ClickHouse you can specify FINAL in the end of FROM statement (doc). This is usefull for MergeTree-engine family. However, ActiveRecord known nothing about this because FINAL is not a part of standard SQL syntax. So, will be good to implement a final function. Example of usage:

ClickHouse::MyModel.where(column1: 42).final.to_sql
=> "SELECT my_table.* FROM my_table FINAL WHERE my_table.column1 = 42"

Currently, we can use a hack for handle this: MyModel.where(...).from("#{my_model.table_name} FINAL") but this has a list of limitations and looks not so good as it could be.

Optional improvement

In addition to final function for DSL, we can add a model attribute like self.always_use_final = true (like self.table_name = 'my_table' and similar). Because in most use cases of ReplacingMergeTree (and similar MergeTree engines) you want to get a merged result. Hence, final has to be applied for all queries of a specific model.