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

cte in continuous aggregate #1931

Open
gma2th opened this issue May 23, 2020 · 6 comments
Open

cte in continuous aggregate #1931

gma2th opened this issue May 23, 2020 · 6 comments

Comments

@gma2th
Copy link

gma2th commented May 23, 2020

Relevant system information:

  • OS: OSX
  • PostgreSQL version (output of postgres --version): 12.2
  • TimescaleDB version (output of \dx in psql): 1.7.1
  • Installation method: source

Describe the bug
Using a cte in a continuous aggregate will raise the following error:

ERROR:  0A000: invalid SELECT query for continuous aggregate
LOCATION:  cagg_validate_query, create.c:745

To Reproduce

create table t (ts timestamptz, a int);
select create_hypertable ('t' , 'ts');
insert into t values (now() , 1) , (now() , 2);

create view v with (timescaledb.continuous) as
with my_t as (select * from t)
select a, time_bucket(interval '1 minute', ts) bucket from t group by a, bucket ;

create view v with (timescaledb.continuous) as
with my_bucket as (select a, time_bucket(interval '1 minute', ts) bucket from t group by a, bucket)
select * from my_bucket 

Expected behavior
Is there a reason to not support cte in continuous aggregate? Is it planned to support them in the future?

@k-rus
Copy link
Contributor

k-rus commented May 23, 2020

Currently only one hypertable can be used in FROM clause in cagg definition. If I don't mistake, any query with CTE confirming this requirement can be easily converted to a query without CTE.
@gma2th Do you have any specific reason why you need such simple CTEs? It seems there is no point to have them with the current limitation on caggs.

@gma2th
Copy link
Author

gma2th commented May 24, 2020

I query only one hypertable. I can do it without a cte, but using a cte avoid to repeat myself and simplify the query as I make several operations between the columns and their aggregated value.

@k-rus
Copy link
Contributor

k-rus commented Jun 2, 2020

@gma2th Thank you for clarifying! Can you provide an example(s) where you demonstrate that CTE with single hypertable helps to define simple query? E.g., the same cagg definition with CTE and without CTE where it is easy to see your point. The examples in OP don't really support well your request.

@Wintermute79
Copy link

Here's my current issue as an example for a CTE:

I want to create 7 very similar continuous aggregate which differ only in their categories from 1 to 7.

... WHERE id IN (SELECT id FROM sensors_to_categories WHERE cat = 7)

Table sensors_to_categories holds many thousand entries, assigning each sensor to either of the 7 categories.

I would not store the category id in the hypertable along with every single sensor value, so there is no version without CTE.

Is there a different way to achieve the same goal? Otherwise original sensor data is deleted after 30 days via retention_policy.

@sdebruyn
Copy link

sdebruyn commented Apr 7, 2024

Given that CTEs are part of ANSI SQL, the lack of support for it should be mentioned as a clear warning on the documentation pages for continuous aggregates.
Even the simple CTE in OP shows how CTEs can help you to properly make your SQL clearer and more understandable.
Why you should care about CTEs

@ewoolsey
Copy link

+1, Would also be very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants