The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix two gaps in Metabot's read-only SQL check (#84457)
Chris Truter37bec5901730
diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex 6dec10b56d7..7d9202d6a67 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -1961,6 +1961,10 @@ def _writes_or_locks(node: exp.Expression) -> bool:return Trueif isinstance(node, exp.Anonymous):return node.name.lower() in _SEQUENCE_WRITE_FUNCTIONS+ # MySQL assigns a user variable inside a SELECT with `@x := ...`, and the value outlives the query on a pooled+ # connection. A bare name on the left is a named argument instead, as in DuckDB and Postgres, and changes nothing.+ if isinstance(node, exp.PropertyEQ):+ return isinstance(node.this, exp.Parameter)if isinstance(node, exp.Column):return bool(node.table) and node.name.lower() == "nextval"if isinstance(node, exp.WithTableHint):@@ -2031,13 +2035,20 @@ def _hidden_sql(sql: str, dialect: str):return None+def _unbracketed(stmt: exp.Expression) -> exp.Expression:+ """`stmt` without the brackets around the whole query: `(SELECT 1)` parses as a subquery with no alias."""+ while isinstance(stmt, exp.Subquery) and not stmt.alias:+ stmt = stmt.this+ return stmt++def _not_read_only_reason(stmts, sql: str, dialect: str):"""`(reason, detail)` for why `stmts`, parsed from `sql`, are not one read-only SELECT, else None."""if not stmts:return ("not-a-select", None)if len(stmts) != 1:return ("multiple-statements", None)- if not isinstance(stmts[0], (exp.Select, exp.SetOperation)):+ if not isinstance(_unbracketed(stmts[0]), (exp.Select, exp.SetOperation)):return ("not-a-select", None)if not _read_only(stmts[0]):return ("writes-or-locks", None)@@ -2071,7 +2082,7 @@ def is_single_stmt_of_type(sql: str, stmt_type: str = "read", dialect: str = Nonresult["reason"], result["reason_detail"] = problemif len(stmts) == 1:result["is_single_stmt?"] = True- if isinstance(stmts[0], allowed_types) and problem is None:+ if isinstance(_unbracketed(stmts[0]), allowed_types) and problem is None:result["allowed_stmt_type?"] = Trueresult["sql"] = stmts[0].sql(dialect=dialect) if dialect else stmts[0].sql()except Exception as e:diff --git a/test/metabase/sql_tools/core_test.clj b/test/metabase/sql_tools/core_test.cljindex 1dbb6f4998a..3ac3efabf68 100644--- a/test/metabase/sql_tools/core_test.clj+++ b/test/metabase/sql_tools/core_test.clj@@ -303,6 +303,8 @@"WITH x AS (SELECT a FROM t) SELECT a, ROW_NUMBER() OVER (ORDER BY a) FROM x""SELECT a FROM t UNION SELECT a FROM u INTERSECT SELECT a FROM v EXCEPT SELECT a FROM w""SELECT * FROM (SELECT 1) s WHERE a IN (SELECT b FROM u)"+ "(SELECT 1)"+ "((SELECT a FROM t))""SELECT 1;""-- leading\nSELECT 1 -- trailing""/* leading */ SELECT 1; -- trailing"))@@ -364,11 +366,17 @@:mysql "SELECT RELEASE_ALL_LOCKS()":mysql "SELECT SLEEP(10)":mysql "SELECT BENCHMARK(1000000, MD5('x'))"))- (testing "reading a sequence or hinting a plain read is read-only"+ (testing "a SELECT that assigns a user variable changes the session, so it is not read-only"+ (are [driver sql] (false? (sql-tools/read-only-select? driver sql))+ :mysql "SELECT @x := 1"+ :mysql "SELECT @total := @total + a FROM t"))+ (testing "reading a sequence or a user variable, hinting a plain read, or naming a function argument is read-only"(are [driver sql] (true? (sql-tools/read-only-select? driver sql)):sqlserver "SELECT * FROM t WITH (NOLOCK)":sqlserver "SELECT * FROM dbo.fn(1)"- :postgres "SELECT currval('seq')")))+ :postgres "SELECT currval('seq')"+ :mysql "SELECT a FROM t WHERE b = @x"+ :postgres "SELECT make_interval(days := 1)")))(deftest ^:parallel read-only-select?-sql-executing-function-test(testing "a SELECT that calls a function taking a SQL string to run is not read-only, whatever the string holds"