Skip to content

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

feat: validate sql for redshift (#17157)

h3n4la4a9f6086222
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index 9c40252994..499d941354 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -397,7 +397,7 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string
statement = getStatementWithResultLimit(statement, queryContext.Limit)
}
- _, allQuery, err := base.ValidateSQLForEditor(storepb.Engine_POSTGRES, statement)
+ _, allQuery, err := base.ValidateSQLForEditor(storepb.Engine_REDSHIFT, statement)
if err != nil {
return nil, err
}
diff --git a/backend/plugin/parser/pg/query.go b/backend/plugin/parser/pg/query.go
index 731b1fdf61..0e4c7057c5 100644
--- a/backend/plugin/parser/pg/query.go
+++ b/backend/plugin/parser/pg/query.go
@@ -19,7 +19,7 @@ import (
func init() {
base.RegisterQueryValidator(storepb.Engine_POSTGRES, validateQuery)
- base.RegisterQueryValidator(storepb.Engine_REDSHIFT, validateQuery)
+ // Redshift has its own implementation in the redshift package
base.RegisterQueryValidator(storepb.Engine_COCKROACHDB, validateQuery)
}
diff --git a/backend/plugin/parser/redshift/query.go b/backend/plugin/parser/redshift/query.go
new file mode 100644
index 0000000000..fe99a266d1
--- /dev/null
+++ b/backend/plugin/parser/redshift/query.go
@@ -0,0 +1,219 @@
+package redshift
+
+import (
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/parser/redshift"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+func init() {
+ // Override the existing PG-based validator with Redshift-specific one
+ base.RegisterQueryValidator(storepb.Engine_REDSHIFT, ValidateSQLForEditor)
+}
+
+// ValidateSQLForEditor validates the SQL statement for SQL editor.
+// We support the following SQLs:
+// 1. EXPLAIN statement, except EXPLAIN ANALYZE (unless it's EXPLAIN ANALYZE SELECT)
+// 2. SELECT statement
+// We also support CTE with SELECT statements, but not with DML statements.
+// Returns (canRunInReadOnly, returnsData, error):
+// - canRunInReadOnly: whether the query can run in read-only mode
+// - returnsData: whether all queries return data
+// - error: parsing error if the statement is invalid
+func ValidateSQLForEditor(statement string) (bool, bool, error) {
+ // Parse the statement using the Redshift parser
+ result, err := ParseRedshift(statement)
+ if err != nil {
+ return false, false, err
+ }
+
+ // Create a validator listener to walk the parse tree
+ validator := &queryValidatorListener{
+ canRunInReadOnly: true,
+ returnsData: true,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(validator, result.Tree)
+
+ return validator.canRunInReadOnly, validator.returnsData, nil
+}
+
+type queryValidatorListener struct {
+ *parser.BaseRedshiftParserListener
+
+ canRunInReadOnly bool
+ returnsData bool
+}
+
+// EnterStmt is called when entering a statement
+func (l *queryValidatorListener) EnterStmt(ctx *parser.StmtContext) {
+ if ctx == nil {
+ return
+ }
+
+ // Check each statement type
+ switch {
+ case ctx.Selectstmt() != nil:
+ // SELECT is allowed
+ return
+
+ case ctx.Explainstmt() != nil:
+ // EXPLAIN is allowed, but check if it's EXPLAIN ANALYZE
+ if explainCtx, ok := ctx.Explainstmt().(*parser.ExplainstmtContext); ok {
+ l.handleExplainStmt(explainCtx)
+ }
+ return
+
+ // SHOW statements - different types of SHOW commands
+ case ctx.Variableshowstmt() != nil,
+ ctx.Showcolumnsstmt() != nil,
+ ctx.Showdatabasesstmt() != nil,
+ ctx.Showdatasharesstmt() != nil,
+ ctx.Showexternaltablestmt() != nil,
+ ctx.Showgrantsstmt() != nil,
+ ctx.Showmodelstmt() != nil,
+ ctx.Showprocedurestmt() != nil,
+ ctx.Showschemasstmt() != nil,
+ ctx.Showtablestmt() != nil,
+ ctx.Showtablesstmt() != nil,
+ ctx.Showviewstmt() != nil:
+ // All SHOW statements are allowed and return data
+ return
+
+ case ctx.Variablesetstmt() != nil:
+ // SET statements can run in read-only but don't return data
+ l.returnsData = false
+ return
+
+ // DML statements are not allowed in read-only mode
+ case ctx.Insertstmt() != nil,
+ ctx.Updatestmt() != nil,
+ ctx.Deletestmt() != nil,
+ ctx.Truncatestmt() != nil:
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ return
+
+ // DDL statements are not allowed in read-only mode
+ case ctx.Createstmt() != nil,
+ ctx.Dropstmt() != nil,
+ ctx.Altertablestmt() != nil,
+ ctx.Indexstmt() != nil,
+ ctx.Createdbstmt() != nil,
+ ctx.Dropdbstmt() != nil,
+ ctx.Createfunctionstmt() != nil,
+ ctx.Createexternalfunctionstmt() != nil,
+ ctx.Createprocedurestmt() != nil,
+ ctx.Creatematviewstmt() != nil,
+ ctx.Createexternalviewstmt() != nil,
+ ctx.Createschemastmt() != nil,
+ ctx.Createuserstmt() != nil,
+ ctx.Createrolestmt() != nil,
+ ctx.Alteruserstmt() != nil,
+ ctx.Alterrolestmt() != nil,
+ ctx.Dropuserstmt() != nil,
+ ctx.Droprolestmt() != nil:
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ return
+
+ // Administrative statements are not allowed in read-only mode
+ case ctx.Grantstmt() != nil,
+ ctx.Revokestmt() != nil,
+ ctx.Analyzestmt() != nil,
+ ctx.Vacuumstmt() != nil,
+ ctx.Copystmt() != nil,
+ ctx.Commentstmt() != nil,
+ ctx.Callstmt() != nil:
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ return
+
+ // Transaction statements are not allowed in read-only mode
+ case ctx.Transactionstmt() != nil:
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ return
+
+ default:
+ // For any unrecognized statement, be conservative and reject it
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ }
+}
+
+// handleExplainStmt handles EXPLAIN statements
+func (l *queryValidatorListener) handleExplainStmt(ctx *parser.ExplainstmtContext) {
+ if ctx == nil {
+ return
+ }
+
+ // Check if it's EXPLAIN ANALYZE by looking for the Analyze_keyword
+ if ctx.Analyze_keyword() != nil {
+ // EXPLAIN ANALYZE actually executes the query
+ // Check what statement is being explained using AST
+ if explainableStmt := ctx.Explainablestmt(); explainableStmt != nil {
+ // Use type assertion to check the actual explainable statement
+ if explainableCtx, ok := explainableStmt.(*parser.ExplainablestmtContext); ok {
+ // Check if it's a SELECT statement using the AST
+ if explainableCtx.Selectstmt() != nil {
+ // EXPLAIN ANALYZE SELECT is allowed in read-only but doesn't return data
+ l.returnsData = false
+ } else {
+ // EXPLAIN ANALYZE of non-SELECT (INSERT/UPDATE/DELETE/DECLARE CURSOR) is not allowed
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ }
+ }
+ }
+ }
+ // Regular EXPLAIN (without ANALYZE) is always allowed and returns data
+}
+
+// EnterCommon_table_expr is called when entering a CTE
+func (l *queryValidatorListener) EnterCommon_table_expr(ctx *parser.Common_table_exprContext) {
+ if ctx != nil {
+ // Check if the CTE contains DML statements
+ if l.hasDMLInContext(ctx) {
+ l.canRunInReadOnly = false
+ l.returnsData = false
+ }
+ }
+}
+
+// hasDMLInContext checks if the given context contains DML statements
+func (*queryValidatorListener) hasDMLInContext(ctx antlr.ParseTree) bool {
+ if ctx == nil {
+ return false
+ }
+
+ // Create a DML detector listener
+ dmlDetector := &dmlDetectorListener{
+ hasDML: false,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(dmlDetector, ctx)
+ return dmlDetector.hasDML
+}
+
+type dmlDetectorListener struct {
+ *parser.BaseRedshiftParserListener
+ hasDML bool
+}
+
+// Check for DELETE statement
+func (l *dmlDetectorListener) EnterDeletestmt(_ *parser.DeletestmtContext) {
+ l.hasDML = true
+}
+
+// Check for INSERT statement
+func (l *dmlDetectorListener) EnterInsertstmt(_ *parser.InsertstmtContext) {
+ l.hasDML = true
+}
+
+// Check for UPDATE statement
+func (l *dmlDetectorListener) EnterUpdatestmt(_ *parser.UpdatestmtContext) {
+ l.hasDML = true
+}
diff --git a/backend/plugin/parser/redshift/query_test.go b/backend/plugin/parser/redshift/query_test.go
new file mode 100644
index 0000000000..b701715ec3
--- /dev/null
+++ b/backend/plugin/parser/redshift/query_test.go
@@ -0,0 +1,520 @@
+package redshift
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestValidateSQLForEditor(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ wantCanRunReadOnly bool
+ wantReturnsData bool
+ wantErr bool
+ }{
+ // Basic SELECT statements (from PG tests)
+ {
+ name: "simple SELECT without space",
+ statement: `select* from t`,
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "simple SELECT",
+ statement: "SELECT * FROM users",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with JOIN",
+ statement: "SELECT u.name, o.order_id FROM users u JOIN orders o ON u.id = o.user_id",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with WHERE",
+ statement: "SELECT * FROM products WHERE price > 100",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with GROUP BY",
+ statement: "SELECT category, COUNT(*) FROM products GROUP BY category",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with special characters in string",
+ statement: "select * from t where a = 'klasjdfkljsa$tag$; -- lkjdlkfajslkdfj'",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+
+ // CTE statements (from PG tests)
+ {
+ name: "CTE with SELECT",
+ statement: "WITH cte AS (SELECT * FROM users) SELECT * FROM cte",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "CTE with keywords in strings and comments",
+ statement: `
+ With t as (
+ select * from t1 where a = 'insert'
+ ), tx as (
+ select * from "delete"
+ ) /* UPDATE */
+ select "update" from t;
+ `,
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "CTE with DELETE",
+ statement: `
+ With t as (
+ select * from t1
+ ), tx as (
+ delete from t2
+ )
+ select * from t;
+ `,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CTE with UPDATE",
+ statement: `
+ With t as (
+ select * from t1
+ ), tx as (
+ select * from t1
+ )
+ update t set a = 1;
+ `,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CTE with INSERT",
+ statement: `
+ With t as (
+ select * from t1
+ ), tx as (
+ select * from t1
+ )
+ insert into t values (1, 2, 3);
+ `,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // EXPLAIN statements (from PG tests)
+ {
+ name: "EXPLAIN SELECT",
+ statement: `explain select * from t;`,
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "EXPLAIN ANALYZE SELECT",
+ statement: `explain analyze select * from t`,
+ wantCanRunReadOnly: true,
+ wantReturnsData: false, // EXPLAIN ANALYZE executes but doesn't return query data
+ wantErr: false,
+ },
+ {
+ name: "EXPLAIN ANALYZE INSERT",
+ statement: `explain analyze insert into t values (1)`,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "EXPLAIN ANALYZE with CTE containing DELETE",
+ statement: `EXPLAIN ANALYZE WITH cte1 AS (DELETE FROM t RETURNING id) SELECT * FROM cte1`,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "EXPLAIN UPDATE",
+ statement: "EXPLAIN UPDATE users SET name = 'test'",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "EXPLAIN ANALYZE UPDATE",
+ statement: "EXPLAIN ANALYZE UPDATE users SET name = 'test'",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // SHOW statements - Redshift specific
+ {
+ name: "SHOW TABLES",
+ statement: "SHOW TABLES",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW TABLE",
+ statement: "SHOW TABLE users",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW DATABASES",
+ statement: "SHOW DATABASES",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW SCHEMAS",
+ statement: "SHOW SCHEMAS",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW COLUMNS",
+ statement: "SHOW COLUMNS FROM users",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW EXTERNAL TABLE",
+ statement: "SHOW EXTERNAL TABLE spectrum.users",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW DATASHARES",
+ statement: "SHOW DATASHARES",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW GRANTS",
+ statement: "SHOW GRANTS FOR user1",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW PROCEDURES",
+ statement: "SHOW PROCEDURES",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW MODEL",
+ statement: "SHOW MODEL my_model",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SHOW VIEW",
+ statement: "SHOW VIEW my_view",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+
+ // SET statements
+ {
+ name: "SET statement",
+ statement: "SET search_path TO public",
+ wantCanRunReadOnly: true,
+ wantReturnsData: false, // SET doesn't return data
+ wantErr: false,
+ },
+
+ // DML statements (not allowed)
+ {
+ name: "INSERT",
+ statement: "INSERT INTO users (name, email) VALUES ('John', '[email protected]')",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "UPDATE",
+ statement: "UPDATE users SET name = 'Jane' WHERE id = 1",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "DELETE",
+ statement: "DELETE FROM users WHERE id = 1",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "TRUNCATE",
+ statement: "TRUNCATE TABLE users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "MERGE",
+ statement: "MERGE INTO target USING source ON target.id = source.id WHEN MATCHED THEN UPDATE SET value = source.value",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // DDL statements (not allowed) - from PG tests
+ {
+ name: "CREATE TABLE",
+ statement: `create table t (a int);`,
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "DROP TABLE",
+ statement: "DROP TABLE users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "ALTER TABLE",
+ statement: "ALTER TABLE users ADD COLUMN age INT",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CREATE INDEX",
+ statement: "CREATE INDEX idx_users_name ON users(name)",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CREATE DATABASE",
+ statement: "CREATE DATABASE testdb",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CREATE SCHEMA",
+ statement: "CREATE SCHEMA analytics",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CREATE VIEW",
+ statement: "CREATE VIEW user_view AS SELECT * FROM users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "CREATE MATERIALIZED VIEW",
+ statement: "CREATE MATERIALIZED VIEW user_summary AS SELECT COUNT(*) FROM users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // Redshift-specific DDL
+ {
+ name: "CREATE EXTERNAL TABLE",
+ statement: "CREATE EXTERNAL TABLE spectrum.users (id INT, name VARCHAR(100)) STORED AS PARQUET LOCATION 's3://mybucket/data/'",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ // Skip CREATE EXTERNAL SCHEMA test as it requires complex syntax
+ // that the parser may not fully support yet
+ {
+ name: "CREATE LIBRARY",
+ statement: "CREATE LIBRARY urlparse LANGUAGE plpythonu FROM 's3://mybucket/urlparse.zip'",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // Administrative statements (not allowed)
+ {
+ name: "GRANT",
+ statement: "GRANT SELECT ON users TO user1",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "REVOKE",
+ statement: "REVOKE SELECT ON users FROM user1",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "ANALYZE",
+ statement: "ANALYZE users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "VACUUM",
+ statement: "VACUUM users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // Redshift-specific commands
+ {
+ name: "COPY",
+ statement: "COPY users FROM 's3://mybucket/data.csv' IAM_ROLE 'arn:aws:iam::123456789012:role/MyRedshiftRole'",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "UNLOAD",
+ statement: "UNLOAD ('SELECT * FROM users') TO 's3://mybucket/data/' IAM_ROLE 'arn:aws:iam::123456789012:role/MyRedshiftRole'",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "ALTER DATASHARE",
+ statement: "ALTER DATASHARE salesshare ADD SCHEMA public",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // Transaction statements (not allowed)
+ {
+ name: "BEGIN",
+ statement: "BEGIN",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "COMMIT",
+ statement: "COMMIT",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+ {
+ name: "ROLLBACK",
+ statement: "ROLLBACK",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: false,
+ },
+
+ // Redshift-specific SELECT features
+ {
+ name: "SELECT with Redshift-specific functions",
+ statement: "SELECT DATEADD(day, 1, GETDATE()) AS tomorrow",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with Redshift window functions",
+ statement: "SELECT name, RATIO_TO_REPORT(sales) OVER (PARTITION BY region) FROM sales_data",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with Redshift date functions",
+ statement: "SELECT DATEDIFF(day, start_date, end_date) FROM events",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT from external schema",
+ statement: "SELECT * FROM spectrum.external_table",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with LISTAGG",
+ statement: "SELECT department, LISTAGG(employee_name, ', ') WITHIN GROUP (ORDER BY employee_name) FROM employees GROUP BY department",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with APPROXIMATE COUNT",
+ statement: "SELECT APPROXIMATE(COUNT(DISTINCT customer_id)) FROM orders",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+ {
+ name: "SELECT with PERCENTILE_CONT",
+ statement: "SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY price) FROM products",
+ wantCanRunReadOnly: true,
+ wantReturnsData: true,
+ wantErr: false,
+ },
+
+ // Invalid syntax
+ {
+ name: "Invalid SQL",
+ statement: "SELEC * FORM users",
+ wantCanRunReadOnly: false,
+ wantReturnsData: false,
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ canRunReadOnly, returnsData, err := ValidateSQLForEditor(tt.statement)
+ if tt.wantErr {
+ require.Error(t, err)
+ } else {
+ require.NoError(t, err)
+ require.Equal(t, tt.wantCanRunReadOnly, canRunReadOnly, "canRunReadOnly mismatch")
+ require.Equal(t, tt.wantReturnsData, returnsData, "returnsData mismatch")
+ }
+ })
+ }
+}