Skip to content

Commit 3ff5b0f

Browse files
committed
update README
1 parent 3394e2f commit 3ff5b0f

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,39 @@ User.window('x', order: 'date', partition: 'name', rows: 'UNBOUNDED PRECEDING').
202202
#=> #<ActiveRecord::Relation [#<User *** >]>
203203
```
204204

205+
### CTE and CSE examples
206+
207+
For activation CSE ([Common Scalar Expressions](https://clickhouse.com/docs/sql-reference/statements/select/with#common-scalar-expressions)) in ClickHouse `value` in hash must be a `Symbol` class.
208+
`key` in a hash must be converting to:
209+
210+
* `String` -> quoted string
211+
* `Symbol` -> raw data
212+
* `Relation` -> sql query
213+
214+
See in examples:
215+
216+
```ruby
217+
# CTE
218+
Action.with(t: ActionView.where(event_name: 'test')).where(event_name: Action.from('t').select('event_name'))
219+
# Clickhouse (10.3ms) WITH t AS (SELECT action_view.* FROM action_view WHERE action_view.event_name = \'test\') SELECT actions.* FROM actions WHERE actions.event_name IN (SELECT event_name FROM t)
220+
#=> #<ActiveRecord::Relation [#<Action *** >]>
221+
222+
# CSE with string key
223+
Action.with('2026-01-01 15:23:00' => :t).where(Arel.sql('date = toDate(t)'))
224+
# Clickhouse (10.3ms) WITH '2026-01-01 15:23:00' AS t SELECT actions.* FROM actions WHERE (date = toDate(t))
225+
#=> #<ActiveRecord::Relation [#<Action *** >]>
226+
227+
# CSE with symbol key
228+
Action.with('(id, extension) -> concat(lower(id), extension)': :t).where(Arel.sql('date = toDate(t)'))
229+
# Clickhouse (10.3ms) WITH (id, extension) -> concat(lower(id), extension) AS t SELECT actions.* FROM actions WHERE (date = toDate(t))
230+
#=> #<ActiveRecord::Relation [#<Action *** >]>
231+
232+
# CSE with ActiveRecord relation key
233+
Action.with(ActionView.select(Arel.sql('min(date)')) => :min_date).where(Arel.sql('date = min_date'))
234+
# Clickhouse (10.3ms) WITH (SELECT min(date) FROM action_view) AS min_date SELECT actions.* FROM actions WHERE (date = min_date)
235+
#=> #<ActiveRecord::Relation [#<Action *** >]>
236+
```
237+
205238

206239
### Migration Data Types
207240

spec/single/model_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,13 +470,13 @@ class ModelPk < ActiveRecord::Base
470470

471471
describe '#cte & cse:' do
472472
it 'cte string' do
473-
sql = Model.with('t' => ModelJoin.where(event_name: 'test')).where(event_name: Model.from('f').select('event_name')).to_sql
474-
expect(sql).to eq('WITH t AS (SELECT joins.* FROM joins WHERE joins.event_name = \'test\') SELECT sample.* FROM sample WHERE sample.event_name IN (SELECT event_name FROM f)')
473+
sql = Model.with('t' => ModelJoin.where(event_name: 'test')).where(event_name: Model.from('t').select('event_name')).to_sql
474+
expect(sql).to eq('WITH t AS (SELECT joins.* FROM joins WHERE joins.event_name = \'test\') SELECT sample.* FROM sample WHERE sample.event_name IN (SELECT event_name FROM t)')
475475
end
476476

477477
it 'cte symbol' do
478-
sql = Model.with(t: ModelJoin.where(event_name: 'test')).where(event_name: Model.from('f').select('event_name')).to_sql
479-
expect(sql).to eq('WITH t AS (SELECT joins.* FROM joins WHERE joins.event_name = \'test\') SELECT sample.* FROM sample WHERE sample.event_name IN (SELECT event_name FROM f)')
478+
sql = Model.with(t: ModelJoin.where(event_name: 'test')).where(event_name: Model.from('t').select('event_name')).to_sql
479+
expect(sql).to eq('WITH t AS (SELECT joins.* FROM joins WHERE joins.event_name = \'test\') SELECT sample.* FROM sample WHERE sample.event_name IN (SELECT event_name FROM t)')
480480
end
481481

482482
it 'cse string variable' do

0 commit comments

Comments
 (0)