ClickHouse / ClickHouse

ClickHouse® is a real-time analytics DBMS
https://clickhouse.com
Apache License 2.0
36.83k stars 6.81k forks source link

Parameterized View: add default parameter value #56571

Open 12frame opened 10 months ago

12frame commented 10 months ago

Use case create view test as select addDays({report_date:Date}, 1); success: select * from test(report_date='2023-11-10'); fail: select * from test(report_date=today());

Describe the solution you'd like create view test as select addDays({report_date:Date Default today()}, 1); select * from test; or parameter can use a function or a result set create view test as select addDays({report_date:Date}, 1); 1: select * from test(report_date=today()); 2: select * from test(report_date=(select today()));

If there are other solutions, I very much hope to be able to give me advice, thank you

kssenii commented 10 months ago

cc @SmitaRKulkarni

den-crane commented 10 months ago

BTW examples here are not fully supported RN.

https://github.com/ClickHouse/ClickHouse/issues/56564

create view test as select  {report_date:Date} ;

Received exception from server (version 23.10.2):
Code: 47. DB::Exception: Received from localhost:9000. 
DB::Exception: Unknown column: report_date, there are only columns dummy. (UNKNOWN_IDENTIFIER)
(query: create view test as select  {report_date:Date} ;)

I think parameters work only in WHERE section normally.

12frame commented 10 months ago

Yes, I agree with you, but there may be some problems with what I mean. Please see the following example. Thank you very much.

create table t(zid Int32, ts DateTime ) 
Engine=Memory;

insert into t(zid, ts) values(1, today());
insert into t(zid, ts) values(1, yesterday());

CREATE VIEW q AS
SELECT  zid,   ts  FROM  t   WHERE ts = {dt:Date};

select * from q(dt=today());

Received exception from server (version 23.10.3):
Code: 456. DB::Exception: Received from localhost:9000. DB::Exception: Substitution `dt` is not set. (UNKNOWN_QUERY_PARAMETER)
(query: select * from q(dt=today());)

https://fiddle.clickhouse.com/df7a7438-5519-459a-880b-74281d3c52ac