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

Error Result Sql After Optimize #2131

Closed
245Bluesky opened this issue Aug 31, 2023 · 5 comments
Closed

Error Result Sql After Optimize #2131

245Bluesky opened this issue Aug 31, 2023 · 5 comments

Comments

@245Bluesky
Copy link

Sql Fragment:
((CAST(random_id AS BIGINT) % 20 = 0) = TRUE)

Optimize with rules:
pushdown_predicates
simplify

Result:
CAST(random_id AS BIGINT) % 20 = 0 = TRUE

ps: The Result Sql is a wrong sql.

A Test Example:
sql = """
SELECT DATE_TRUNC('day', CAST("dt" AS TIMESTAMP)) AS "dt1",
APPROX_DISTINCT(CASE
WHEN "event" = 'Start' THEN "distinct_id"
END, 0.0040625) + 6 AS "metric#q3ll_g"
FROM table_1
WHERE "dt" < '2023-08-30'
AND "dt" >= '2023-08-29'
AND ((CAST("random_id" AS BIGINT) % 20 = 0) = TRUE)
GROUP BY DATE_TRUNC('day', CAST("dt" AS TIMESTAMP))
ORDER BY "metric#q3ll_g" DESC
LIMIT 10
"""

@tobymao
Copy link
Owner

tobymao commented Aug 31, 2023

@245Bluesky can you be more specific and explain why it's not correct?

@245Bluesky
Copy link
Author

After Run The Result Sql :
SELECT DATE_TRUNC('day', CAST("table_1"."dt" AS TIMESTAMP)) AS "dt1", APPROX_DISTINCT( CASE WHEN "table_1"."event" = '$AppStart' THEN "table_1"."distinct_id" END, 0.0040625 ) + 6 AS "metric#q3ll_g" FROM "table_1" AS "table_1" WHERE "table_1"."dt" < '2023-08-30' AND "table_1"."dt" >= '2023-08-29' AND CAST("table_1"."random_id" AS BIGINT) % 20 = 0 = TRUE AND "table_1"."event" = '$AppStart' GROUP BY DATE_TRUNC('day', CAST("table_1"."dt" AS TIMESTAMP)) ORDER BY "metric#q3ll_g" DESC LIMIT 10

With Trino
trino error: line 3:61: mismatched input '='. Expecting: '%', '*', '+', ',', '-', '.', '/', 'AND', 'AS', 'AT', 'EXCEPT', 'FETCH', 'FROM', 'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'OR', 'ORDER', 'UNION', 'WHERE', 'WINDOW', '[', '||', ,

With Awsathena:
awsathena error: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: line 3:61: mismatched input '='. Expecting: '%', '*', '+', ',', '-', '.', '/', 'AND', 'AS', 'AT', 'EXCEPT', 'FROM', 'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'OR', 'ORDER', 'UNION', 'WHERE', '[', '||', ,

@tobymao
Copy link
Owner

tobymao commented Aug 31, 2023

Thanks, please add this additional detail next time.

@245Bluesky
Copy link
Author

👍 ok, thanks. Solved so quickly!

@245Bluesky
Copy link
Author

245Bluesky commented Aug 31, 2023

Hi, tobymao, May I ask a question?

How to judge a expression in Select with ‘CASE WHEN’ is Nested or Parallel by using sqlglot?

for example:
1、Nested
CASE
WHEN condition_1 THEN
CASE
WHEN nested_condition_1 THEN result_1
WHEN nested_condition_2 THEN result_2
ELSE nested_result
END
WHEN condition_2 THEN result_3
ELSE result_4
END AS nested_case,

2、Parallel
CASE
WHEN condition_5 THEN result_5
WHEN condition_6 THEN result_6
ELSE result_7
END AS standalone_case1,
CASE
WHEN condition_8 THEN result_8
WHEN condition_9 THEN result_9
ELSE result_9
END AS standalone_case2

My code like:
for index, expression in enumerate(self.sql_ast.expressions):
sub_expression = self.sql_ast.args['expressions'][index]
for case in sub_expression.find_all(exp.Case):
how to judge?

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

No branches or pull requests

2 participants