Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(parser/doris): unblock read-only Doris queries in SQL Editor (BYT-10070) (#21208)

Danny Xu67e3e4d6807e
diff --git a/backend/plugin/parser/doris/query_test.go b/backend/plugin/parser/doris/query_test.go
index 77067f3798..950bd75da2 100644
--- a/backend/plugin/parser/doris/query_test.go
+++ b/backend/plugin/parser/doris/query_test.go
@@ -107,6 +107,70 @@ func TestValidateQuery(t *testing.T) {
valid: true,
description: "CTE-prefixed SELECT is read-only",
},
+ {
+ statement: "SELECT EXTRACT(YEAR FROM MONTHS_ADD(NOW(), -1))",
+ valid: true,
+ description: "EXTRACT(unit FROM expr) is read-only",
+ },
+ {
+ statement: `SELECT EXTRACT(
+ YEAR
+ FROM
+ MONTHS_ADD(NOW(), -1)
+)`,
+ valid: true,
+ description: "Multi-line EXTRACT is read-only",
+ },
+ {
+ statement: "WITH c AS (SELECT EXTRACT(MONTH FROM created_at) m FROM t) SELECT * FROM c",
+ valid: true,
+ description: "EXTRACT inside a CTE body is read-only",
+ },
+ {
+ statement: "SELECT TRIM(s), LEFT(s, 2), RIGHT(s, 2), IF(a, 1, 2) FROM t",
+ valid: true,
+ description: "Reserved keywords used as function names are read-only",
+ },
+ {
+ statement: "SELECT DATABASE()",
+ valid: true,
+ description: "DATABASE() is read-only",
+ },
+ {
+ statement: "SELECT CURRENT_TIMESTAMP(3)",
+ valid: true,
+ description: "Datetime function with precision is read-only",
+ },
+ {
+ statement: "SELECT @@version_comment, @@session.query_timeout, @uservar",
+ valid: true,
+ description: "Session and user variables are read-only",
+ },
+ {
+ statement: "SELECT [1, 2, 3], {'a': 1}, x'1f', b'101'",
+ valid: true,
+ description: "Array, map, hex and bit literals are read-only",
+ },
+ {
+ statement: "SELECT array_map(x -> x + 1, arr) FROM t",
+ valid: true,
+ description: "Lambda in a higher-order array function is read-only",
+ },
+ {
+ statement: "SELECT /*+ SET_VAR(query_timeout = 100) */ a FROM t",
+ valid: true,
+ description: "Statement with an optimizer hint is read-only",
+ },
+ {
+ statement: "SELECT a, SUM(b) FROM t GROUP BY CUBE(a)",
+ valid: true,
+ description: "GROUP BY CUBE is read-only",
+ },
+ {
+ statement: "SELECT CONVERT(s USING utf8), CONVERT(s, SIGNED), BINARY s FROM t",
+ valid: true,
+ description: "CONVERT and BINARY forms are read-only",
+ },
}
for _, tc := range tests {
@@ -138,6 +202,10 @@ func TestValidateQuery(t *testing.T) {
statement: "WITH c AS (SELECT 1) DELETE FROM t",
description: "CTE-prefixed DELETE must be rejected",
},
+ {
+ statement: "WITH c AS (SELECT 1) INSERT INTO t SELECT * FROM c",
+ description: "CTE-prefixed INSERT must be rejected",
+ },
{
statement: "SELECT a > (select max(a) from t1) FROM",
description: "Truncated SELECT must be rejected as syntax error",
diff --git a/backend/plugin/parser/starrocks/query_test.go b/backend/plugin/parser/starrocks/query_test.go
index e8fa9869e0..e89a81ab54 100644
--- a/backend/plugin/parser/starrocks/query_test.go
+++ b/backend/plugin/parser/starrocks/query_test.go
@@ -107,6 +107,74 @@ func TestValidateQuery(t *testing.T) {
valid: true,
description: "CTE-prefixed SELECT is read-only",
},
+ {
+ statement: "SELECT EXTRACT(YEAR FROM MONTHS_ADD(NOW(), -1))",
+ valid: true,
+ description: "EXTRACT(unit FROM expr) is read-only",
+ },
+ {
+ statement: `SELECT EXTRACT(
+ YEAR
+ FROM
+ MONTHS_ADD(NOW(), -1)
+)`,
+ valid: true,
+ description: "Multi-line EXTRACT is read-only",
+ },
+ {
+ statement: "WITH c AS (SELECT EXTRACT(MONTH FROM created_at) m FROM t) SELECT * FROM c",
+ valid: true,
+ description: "EXTRACT inside a CTE body is read-only",
+ },
+ {
+ statement: "SELECT TRIM(s), LEFT(s, 2), RIGHT(s, 2), IF(a, 1, 2) FROM t",
+ valid: true,
+ description: "Reserved keywords used as function names are read-only",
+ },
+ {
+ statement: "SELECT DATABASE()",
+ valid: true,
+ description: "DATABASE() is read-only",
+ },
+ {
+ statement: "SELECT CURRENT_TIMESTAMP(3)",
+ valid: true,
+ description: "Datetime function with precision is read-only",
+ },
+ {
+ statement: "SELECT @@version_comment, @@session.query_timeout, @uservar",
+ valid: true,
+ description: "Session and user variables are read-only",
+ },
+ {
+ // StarRocks requires the MAP prefix on a map constructor; a bare
+ // {...} is not a literal there.
+ statement: "SELECT [1, 2, 3], map{'a': 1}, x'1f', b'101'",
+ valid: true,
+ description: "Array, map, hex and bit literals are read-only",
+ },
+ {
+ statement: "SELECT array_map(x -> x + 1, arr) FROM t",
+ valid: true,
+ description: "Lambda in a higher-order array function is read-only",
+ },
+ {
+ statement: "SELECT /*+ SET_VAR(query_timeout = 100) */ a FROM t",
+ valid: true,
+ description: "Statement with an optimizer hint is read-only",
+ },
+ {
+ statement: "SELECT a, SUM(b) FROM t GROUP BY CUBE(a)",
+ valid: true,
+ description: "GROUP BY CUBE is read-only",
+ },
+ {
+ // StarRocks has no USING charset clause; CONVERT(x, type) and the
+ // BINARY operator are both accepted.
+ statement: "SELECT CONVERT(s, SIGNED), BINARY s FROM t",
+ valid: true,
+ description: "CONVERT and BINARY forms are read-only",
+ },
}
for _, tc := range tests {
@@ -138,6 +206,10 @@ func TestValidateQuery(t *testing.T) {
statement: "WITH c AS (SELECT 1) DELETE FROM t",
description: "CTE-prefixed DELETE must be rejected",
},
+ {
+ statement: "WITH c AS (SELECT 1) INSERT INTO t SELECT * FROM c",
+ description: "CTE-prefixed INSERT must be rejected",
+ },
{
statement: "SELECT a > (select max(a) from t1) FROM",
description: "Truncated SELECT must be rejected as syntax error",
diff --git a/go.mod b/go.mod
index 1e5b8bb066..f883ce576a 100644
--- a/go.mod
+++ b/go.mod
@@ -34,7 +34,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0
- github.com/bytebase/omni v0.0.0-20260817095119-ad01165bf7db
+ github.com/bytebase/omni v0.0.0-20260820081141-f107d677cca3
github.com/caarlos0/env/v11 v11.4.1
github.com/cenkalti/backoff/v7 v7.0.0
github.com/cockroachdb/cockroachdb-parser v0.25.2
diff --git a/go.sum b/go.sum
index 1b7e817e8a..3110d4c91b 100644
--- a/go.sum
+++ b/go.sum
@@ -250,8 +250,8 @@ github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735 h1:v8y6mXeHmYVsdM
github.com/bytebase/gomongo v0.0.0-20260730033313-67ba6e249735/go.mod h1:vTHjcvfn8Pmf65WjnV1ytTCYyC7xNhFjrniv4YWvtA8=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+pccl5m/wuu97fiYjDwpJH6IzURAq/3XA2U=
github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=
-github.com/bytebase/omni v0.0.0-20260817095119-ad01165bf7db h1:K2pZNwZi1YfwYrhWrkYjBUBLiWa/0ugIPc1Zd2tDxDQ=
-github.com/bytebase/omni v0.0.0-20260817095119-ad01165bf7db/go.mod h1:Gp5RN3FM07f9/FFOTZCeu8duTAIMeuinkjjBCrs2Dw4=
+github.com/bytebase/omni v0.0.0-20260820081141-f107d677cca3 h1:gbpLT03nnD5xVfy22UzoxUMaN5ymmcXoYx3ghgYNNO8=
+github.com/bytebase/omni v0.0.0-20260820081141-f107d677cca3/go.mod h1:Gp5RN3FM07f9/FFOTZCeu8duTAIMeuinkjjBCrs2Dw4=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767 h1:YuR5G3LcxpO5ScSTA+Kdirxg9RW4TRc/v1t7N2Hfmbw=
github.com/bytebase/pgx/v5 v5.0.0-20250212161523-96ff8aed8767/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=