mirror of
https://github.com/apache/superset.git
synced 2026-04-25 19:14:27 +00:00
feat: use sqlglot to set limit (#33473)
This commit is contained in:
@@ -469,39 +469,6 @@ class ParsedQuery:
|
||||
exec_sql += f"CREATE {method} {full_table_name} AS \n{sql}"
|
||||
return exec_sql
|
||||
|
||||
def set_or_update_query_limit(self, new_limit: int, force: bool = False) -> str:
|
||||
"""Returns the query with the specified limit.
|
||||
|
||||
Does not change the underlying query if user did not apply the limit,
|
||||
otherwise replaces the limit with the lower value between existing limit
|
||||
in the query and new_limit.
|
||||
|
||||
:param new_limit: Limit to be incorporated into returned query
|
||||
:return: The original query with new limit
|
||||
"""
|
||||
if not self._limit:
|
||||
return f"{self.stripped()}\nLIMIT {new_limit}"
|
||||
limit_pos = None
|
||||
statement = self._parsed[0]
|
||||
# Add all items to before_str until there is a limit
|
||||
for pos, item in enumerate(statement.tokens):
|
||||
if item.ttype in Keyword and item.value.lower() == "limit":
|
||||
limit_pos = pos
|
||||
break
|
||||
_, limit = statement.token_next(idx=limit_pos)
|
||||
# Override the limit only when it exceeds the configured value.
|
||||
if limit.ttype == sqlparse.tokens.Literal.Number.Integer and (
|
||||
force or new_limit < int(limit.value)
|
||||
):
|
||||
limit.value = new_limit
|
||||
elif limit.is_group:
|
||||
limit.value = f"{next(limit.get_identifiers())}, {new_limit}"
|
||||
|
||||
str_res = ""
|
||||
for i in statement.tokens:
|
||||
str_res += str(i.value)
|
||||
return str_res
|
||||
|
||||
|
||||
def sanitize_clause(clause: str) -> str:
|
||||
# clause = sqlparse.format(clause, strip_comments=True)
|
||||
|
||||
Reference in New Issue
Block a user