sadpandajoe commented on code in PR #45271:
URL: https://github.com/apache/superset/pull/45271#discussion_r4353679614
##########
superset/sql/parse.py:
##########
@@ -268,6 +268,33 @@ def has_aggregate(
)
+def is_constant_expression(expression: str, engine: str = "base") -> bool:
+ """
+ Return True if the SQL expression is a bare constant: a string or numeric
+ literal, ``NULL``, or (outside SQLite) a boolean literal, optionally
wrapped
+ in parentheses.
+
+ Anything that merely contains a constant (``CASE WHEN 1=1 THEN 'x' END``,
+ ``CAST('x' AS VARCHAR)``, ``'a' || col``) is not a bare constant. Fails
closed
+ (returns False) on a parse error, so an expression that can't be proven
+ constant is never treated as one.
+
+ ``TRUE``/``FALSE`` are not bare constants on SQLite: they are ordinary
+ identifiers there, resolved to a column of that name when one exists.
+ """
+ dialect = SQLGLOT_DIALECTS.get(engine)
+ try:
+ parsed = sqlglot.parse_one(f"SELECT {expression}", dialect=dialect)
+ except Exception:
+ return False
+ if not isinstance(parsed, exp.Select) or len(parsed.expressions) != 1:
+ return False
+ node = parsed.expressions[0].unnest()
+ if isinstance(node, exp.Boolean):
+ return dialect != Dialects.SQLITE
+ return isinstance(node, (exp.Literal, exp.Null))
Review Comment:
Update: I rebased the branch onto current master (it was behind, which broke
an unrelated required e2e step). The patch is unchanged; this fix is now
0e34933 (was ef2a16b).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]