Skip to content

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

chore: better handle large sheets (#12442)

Danny Xu03425efc62f5
diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.go
index f07cab77e1..9833d59a02 100644
--- a/backend/plugin/db/pg/pg.go
+++ b/backend/plugin/db/pg/pg.go
@@ -363,18 +363,34 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
if err != nil {
return 0, err
}
- singleSQLs, err := pgparser.SplitSQL(statement)
- if err != nil {
- return 0, err
- }
+
+ var commands []base.SingleSQL
var originalIndex []int
- singleSQLs, originalIndex = base.FilterEmptySQLWithIndexes(singleSQLs)
+ var isPlsql bool
+ if len(statement) <= common.MaxSheetCheckSize {
+ singleSQLs, err := pgparser.SplitSQL(statement)
+ if err != nil {
+ return 0, err
+ }
+ commands, originalIndex = base.FilterEmptySQLWithIndexes(singleSQLs)
- // If the statement is a single statement and is a PL/pgSQL block,
- // we should execute it as a single statement without transaction.
- if len(singleSQLs) == 1 && isPlSQLBlock(singleSQLs[0].Text) {
+ // If the statement is a single statement and is a PL/pgSQL block,
+ // we should execute it as a single statement without transaction.
// If the statement is a PL/pgSQL block, we should execute it as a single statement.
// https://www.postgresql.org/docs/current/plpgsql-control-structures.html
+ if len(singleSQLs) == 1 && isPlSQLBlock(singleSQLs[0].Text) {
+ isPlsql = true
+ }
+ } else {
+ commands = []base.SingleSQL{
+ {
+ Text: statement,
+ },
+ }
+ originalIndex = []int{0}
+ }
+
+ if isPlsql {
conn, err := driver.db.Conn(ctx)
if err != nil {
return 0, errors.Wrapf(err, "failed to get connection")
@@ -386,7 +402,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
return 0, errors.Wrapf(err, "failed to set role to database owner %q", owner)
}
opts.LogCommandExecute([]int32{0})
- if _, err := conn.ExecContext(ctx, singleSQLs[0].Text); err != nil {
+ if _, err := conn.ExecContext(ctx, statement); err != nil {
opts.LogCommandResponse([]int32{0}, 0, []int32{0}, err.Error())
return 0, err
}
@@ -399,7 +415,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
var remainingSQLsIndex, nonTransactionAndSetRoleStmtsIndex []int
var nonTransactionAndSetRoleStmts []string
- for i, singleSQL := range singleSQLs {
+ for i, singleSQL := range commands {
if isIgnoredStatement(singleSQL.Text) {
continue
}
@@ -449,23 +465,16 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
defer conn.Close()
if len(remainingSQLs) != 0 {
- var totalCommands int
var chunks [][]base.SingleSQL
- if len(statement) <= common.MaxSheetCheckSize {
- totalCommands = len(remainingSQLs)
- ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
- if err != nil {
- return 0, errors.Wrapf(err, "failed to chunk sql")
- }
- chunks = ret
- } else {
- chunks = [][]base.SingleSQL{
- remainingSQLs,
- }
+ totalCommands := len(remainingSQLs)
+ ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
+ if err != nil {
+ return 0, errors.Wrapf(err, "failed to chunk sql")
}
+ chunks = ret
currentIndex := 0
- err := conn.Raw(func(driverConn any) error {
+ err = conn.Raw(func(driverConn any) error {
conn := driverConn.(*stdlib.Conn).Conn()
tx, err := conn.Begin(ctx)
diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.go
index 85450c129f..2614b61cdb 100644
--- a/backend/plugin/db/redshift/redshift.go
+++ b/backend/plugin/db/redshift/redshift.go
@@ -178,18 +178,25 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
if err != nil {
return 0, err
}
- singleSQLs, err := pgparser.SplitSQL(statement)
- if err != nil {
- return 0, err
- }
- singleSQLs = base.FilterEmptySQL(singleSQLs)
- if len(singleSQLs) == 0 {
- return 0, nil
+
+ var commands []base.SingleSQL
+ if len(statement) <= common.MaxSheetCheckSize {
+ singleSQLs, err := pgparser.SplitSQL(statement)
+ if err != nil {
+ return 0, err
+ }
+ commands = base.FilterEmptySQL(singleSQLs)
+ } else {
+ commands = []base.SingleSQL{
+ {
+ Text: statement,
+ },
+ }
}
var remainingSQLs []base.SingleSQL
var nonTransactionStmts []string
- for _, singleSQL := range singleSQLs {
+ for _, singleSQL := range commands {
if isNonTransactionStatement(singleSQL.Text) {
nonTransactionStmts = append(nonTransactionStmts, singleSQL.Text)
continue
@@ -211,20 +218,13 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
totalRowsAffected := int64(0)
if len(remainingSQLs) != 0 {
- var totalCommands int
var chunks [][]base.SingleSQL
- if len(statement) <= common.MaxSheetCheckSize {
- totalCommands = len(remainingSQLs)
- ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
- if err != nil {
- return 0, errors.Wrapf(err, "failed to chunk sql")
- }
- chunks = ret
- } else {
- chunks = [][]base.SingleSQL{
- remainingSQLs,
- }
+ totalCommands := len(remainingSQLs)
+ ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
+ if err != nil {
+ return 0, errors.Wrapf(err, "failed to chunk sql")
}
+ chunks = ret
currentIndex := 0
tx, err := driver.db.BeginTx(ctx, nil)
diff --git a/backend/plugin/db/risingwave/risingwave.go b/backend/plugin/db/risingwave/risingwave.go
index 78d0e7d3c7..8814037410 100644
--- a/backend/plugin/db/risingwave/risingwave.go
+++ b/backend/plugin/db/risingwave/risingwave.go
@@ -244,18 +244,24 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
return 0, nil
}
- singleSQLs, err := pgparser.SplitSQL(statement)
- if err != nil {
- return 0, err
- }
- singleSQLs = base.FilterEmptySQL(singleSQLs)
- if len(singleSQLs) == 0 {
- return 0, nil
+ var commands []base.SingleSQL
+ if len(statement) <= common.MaxSheetCheckSize {
+ singleSQLs, err := pgparser.SplitSQL(statement)
+ if err != nil {
+ return 0, err
+ }
+ commands = base.FilterEmptySQL(singleSQLs)
+ } else {
+ commands = []base.SingleSQL{
+ {
+ Text: statement,
+ },
+ }
}
var remainingSQLs []base.SingleSQL
var nonTransactionStmts []string
- for _, singleSQL := range singleSQLs {
+ for _, singleSQL := range commands {
if isNonTransactionStatement(singleSQL.Text) {
nonTransactionStmts = append(nonTransactionStmts, singleSQL.Text)
continue
@@ -265,20 +271,13 @@ func (driver *Driver) Execute(ctx context.Context, statement string, opts db.Exe
totalRowsAffected := int64(0)
if len(remainingSQLs) != 0 {
- var totalCommands int
var chunks [][]base.SingleSQL
- if len(statement) <= common.MaxSheetCheckSize {
- totalCommands = len(remainingSQLs)
- ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
- if err != nil {
- return 0, errors.Wrapf(err, "failed to chunk sql")
- }
- chunks = ret
- } else {
- chunks = [][]base.SingleSQL{
- remainingSQLs,
- }
+ totalCommands := len(remainingSQLs)
+ ret, err := util.ChunkedSQLScript(remainingSQLs, common.MaxSheetChunksCount)
+ if err != nil {
+ return 0, errors.Wrapf(err, "failed to chunk sql")
}
+ chunks = ret
currentIndex := 0
tx, err := driver.db.BeginTx(ctx, nil)