cube-js / cube

📊 Cube — The Semantic Layer for Building Data Applications
https://cube.dev
Other
17.69k stars 1.75k forks source link

Cannot use a condition with static comparison #7880

Open alexbadm opened 6 months ago

alexbadm commented 6 months ago

Failed SQL

SELECT
  date_trunc('month', created_at) AS "time",
  count(total_count) AS "Count"
FROM talents
WHERE
  created_at >= '2019-08-31T22:00:00Z'
  AND created_at <= '2024-03-07T13:39:28.923Z'
  AND ('ALL' = 'Armenia' OR country IN ('Armenia'))
GROUP BY 1
ORDER BY 1

Logical Plan Search for Can't rewrite plan log message.

Error during rewrite: Can't detect Cube query and it may be not supported yet. Please check logs for additional information.

Version: v0.34.25

Additional context The query fails when I add AND ('ALL' = $COUNTRIES OR country IN ($COUNTRIES)) The $COUNTRIES is a variable in Grafana, which is query select distinct country from talents and "Include All option" is enabled. When I select some countries it works fine:

select
  date_trunc('month', created_at) "time",
  count(total_count) "Count"
from
  talents
where
  created_at >= '2019-08-31T22:00:00Z' and created_at <= '2024-03-07T13:49:15.895Z'
  and country in ('Australia','Austria')
group by 1
order by 1

When I select the All option, I get the error:

Error during rewrite: Can't find rewrite due to 10081 AST node limit reached. Please check logs for additional information.

The countries number is slightly above 100 and cube server fails to perform this query.

And my attempt to overcome this limitation is:

But this also fails for cube. Although such a query works to psql directly:

select count(*) from analytics.talents where ('ALL' = 'ALL' or country in ('ALL'));
select count(*) from analytics.talents where ('ALL' = 'Austria' or country in ('Austria'));

Both return result. The first is equivalent to select count(*) from analytics.talents; and the second is equivalent to select count(*) from analytics.talents where country in ('Austria');

igorlukanin commented 5 months ago

Hi @alexbadm 👋 Could you please check the advice in this issue? I think it's quite similar to yours: https://github.com/cube-js/cube/issues/6307

igorlukanin commented 3 months ago

@alexbadm Did my advice help?