Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(oracle): retry paged selects on TTC decode errors

ordinary beingc2a2b4198fef
diff --git a/agents/drivers/oracle-go/main.go b/agents/drivers/oracle-go/main.go
index 41cddd41a4..d5cf6579de 100644
--- a/agents/drivers/oracle-go/main.go
+++ b/agents/drivers/oracle-go/main.go
@@ -4218,14 +4218,30 @@ func (s *server) runPagedOracleSelect(sqlText string, opts queryOptions, pageSiz
var ownedRows *sql.Rows
defer func() { s.closeRows(ownedRows) }()
for attempt := 0; ; attempt++ {
+ // Only the first page may be retried: once rows have been returned the
+ // caller owns them and a second execution would duplicate data.
+ retrySQL := func(err error) (string, bool) {
+ if attempt != 0 || s.hasManualTransaction() || oracleSQLLocksRows(sqlText) {
+ return "", false
+ }
+ return oraclePagedRetrySQL(sqlText, s.loadOracleColumnMeta, err)
+ }
rows, err := s.queryRowsWithOracleValueRewriteIfNeeded(sqlText, opts.TimeoutSecs, opts.DeferLOBs)
if err != nil {
+ if rewritten, ok := retrySQL(err); ok {
+ sqlText = rewritten
+ continue
+ }
return queryPageResult{}, nil, err
}
ownedRows = rows
columns, err := rows.Columns()
if err != nil {
s.closeRows(rows)
+ if rewritten, ok := retrySQL(err); ok {
+ sqlText = rewritten
+ continue
+ }
return queryPageResult{}, nil, err
}
columnTypes := columnTypeNames(rows)
@@ -4238,12 +4254,9 @@ func (s *server) runPagedOracleSelect(sqlText string, opts queryOptions, pageSiz
result.ExecutionTimeMS = time.Since(start).Milliseconds()
if err != nil {
s.closeRows(rows)
- var panicErr oracleDriverPanicError
- if attempt == 0 && !s.hasManualTransaction() && !oracleSQLLocksRows(sqlText) && errors.As(err, &panicErr) {
- if placeholder, ok := oraclePlaceholderRetrySQL(sqlText, s.loadOracleColumnMeta); ok {
- sqlText = placeholder
- continue
- }
+ if rewritten, ok := retrySQL(err); ok {
+ sqlText = rewritten
+ continue
}
return queryPageResult{}, nil, err
}
@@ -4267,6 +4280,33 @@ func oraclePlaceholderRetrySQL(sqlText string, loadColumns oracleColumnMetaLoade
return placeholder, true
}
+// oraclePagedRetrySQL returns the statement to retry with when the first page
+// of a paged Oracle SELECT fails while reading the response.
+//
+// A driver panic means the driver state for the scanned value is unknown, so it
+// keeps the placeholder projection used by the non-paged fallback. Response
+// reading failures such as go-ora's "TTC error: received code ..." mean a column
+// value could not be decoded (SDO_GEOMETRY, XMLTYPE, ...); those retry with the
+// value rewrite so the columns come back as WKT / serialized text instead of
+// failing the whole page (issue #13179). The non-paged executeSelect path
+// already performs this rewrite in executeOracleSelectWithXMLTypeRetry; without
+// it the paged path (table data and paged query results) surfaced the raw TTC
+// error and the table could not be opened.
+func oraclePagedRetrySQL(sqlText string, loadColumns oracleColumnMetaLoader, err error) (string, bool) {
+ var panicErr oracleDriverPanicError
+ if errors.As(err, &panicErr) {
+ return oraclePlaceholderRetrySQL(sqlText, loadColumns)
+ }
+ if !shouldRetryOracleXMLTypeRewrite(err) {
+ return "", false
+ }
+ rewritten, rewriteErr := rewriteOracleSelectSQL(sqlText, loadColumns, false)
+ if rewriteErr != nil || rewritten == sqlText {
+ return "", false
+ }
+ return rewritten, true
+}
+
func (s *server) fetchQueryPage(sessionID string, pageSize int) (queryPageResult, error) {
session := s.sessions[sessionID]
if session == nil {
diff --git a/agents/drivers/oracle-go/main_test.go b/agents/drivers/oracle-go/main_test.go
index 0b20a9824b..7bf537ae8d 100644
--- a/agents/drivers/oracle-go/main_test.go
+++ b/agents/drivers/oracle-go/main_test.go
@@ -3579,6 +3579,7 @@ type oracleViewSourceQueryStep struct {
columns []string
rows [][]driver.Value
err error
+ nextErr error
exec bool
panicText string
nextPanicText string
@@ -3635,7 +3636,7 @@ func (c *oracleViewSourceConn) QueryContext(
values[index] = arg.Value
}
if !reflect.DeepEqual(values, step.args) {
- return nil, errors.New("unexpected query arguments")
+ return nil, fmt.Errorf("unexpected query arguments: got %v, want %v (query %q)", values, step.args, query)
}
if step.err != nil {
return nil, step.err
@@ -3647,6 +3648,7 @@ func (c *oracleViewSourceConn) QueryContext(
return &oracleViewSourceRows{
columns: columns,
values: step.rows,
+ nextErr: step.nextErr,
nextPanicText: step.nextPanicText,
columnsPanicText: step.columnsPanicText,
columnsPanicCall: step.columnsPanicCall,
@@ -3683,6 +3685,7 @@ type oracleViewSourceRows struct {
columns []string
values [][]driver.Value
next int
+ nextErr error
nextPanicText string
columnsPanicText string
columnsPanicCall int
@@ -3705,6 +3708,9 @@ func (r *oracleViewSourceRows) Next(dest []driver.Value) error {
if r.nextPanicText != "" {
panic(r.nextPanicText)
}
+ if r.nextErr != nil {
+ return r.nextErr
+ }
if r.next >= len(r.values) {
return io.EOF
}
@@ -4086,6 +4092,110 @@ func TestExecuteQueryPageSurfacesPanicWhenPlaceholderNotApplicable(t *testing.T)
}
}
+func TestExecuteQueryPageRetriesWithWKTRewriteOnTTCError(t *testing.T) {
+ // go-ora raises "TTC error: received code ... during response reading" when a
+ // response contains a value it cannot decode, e.g. SDO_GEOMETRY. The paged
+ // path must retry with the value rewrite (geometry as WKT) instead of
+ // surfacing the raw error and leaving the table unreadable (issue #13179).
+ db, scripted := openOracleViewSourceTestDB(t, []oracleViewSourceQueryStep{
+ {
+ queryContains: "SELECT * FROM SDE.TEST_GEOM",
+ args: []driver.Value{},
+ columns: []string{"ID", "GEOM"},
+ rows: [][]driver.Value{{int64(1), "raw geometry bytes"}},
+ nextErr: errors.New("TTC error: received code 3 during response reading"),
+ },
+ {
+ queryContains: "ALL_TAB_COLUMNS",
+ args: []driver.Value{"SDE", "TEST_GEOM"},
+ columns: []string{"COLUMN_NAME", "DATA_TYPE", "DATA_TYPE_OWNER"},
+ rows: [][]driver.Value{
+ {"ID", "NUMBER", nil},
+ {"GEOM", "SDO_GEOMETRY", "PUBLIC"},
+ },
+ },
+ {
+ queryContains: `SDO_UTIL.TO_WKTGEOMETRY("GEOM") AS "GEOM"`,
+ args: []driver.Value{},
+ columns: []string{"ID", "GEOM"},
+ rows: [][]driver.Value{{int64(1), "POINT (1 2)"}},
+ },
+ })
+ s := newServer()
+ s.db = db
+ result, err := s.executeQueryPage(queryOptions{SQL: "SELECT * FROM SDE.TEST_GEOM"}, 10)
+ if err != nil {
+ t.Fatalf("expected the WKT retry to serve the first page, got %v", err)
+ }
+ if scripted.next != len(scripted.steps) {
+ t.Fatalf("expected %d queries, got %d", len(scripted.steps), scripted.next)
+ }
+ if len(result.Rows) != 1 {
+ t.Fatalf("expected one rewritten row, got %v", result.Rows)
+ }
+ want := []any{int64(1), "POINT (1 2)"}
+ if !reflect.DeepEqual(result.Rows[0], want) {
+ t.Fatalf("unexpected row: %v, want %v", result.Rows[0], want)
+ }
+ if result.SessionID != nil || result.HasMore {
+ t.Fatalf("fully drained page must not keep a session: %v %v", result.SessionID, result.HasMore)
+ }
+}
+
+func TestExecuteQueryPageKeepsTTCErrorWhenNoRewriteApplies(t *testing.T) {
+ db, scripted := openOracleViewSourceTestDB(t, []oracleViewSourceQueryStep{
+ {
+ queryContains: "SELECT * FROM SDE.TEST_GEOM",
+ args: []driver.Value{},
+ columns: []string{"ID"},
+ rows: [][]driver.Value{{int64(1)}},
+ nextErr: errors.New("TTC error: received code 3 during response reading"),
+ },
+ {
+ queryContains: "ALL_TAB_COLUMNS",
+ args: []driver.Value{"SDE", "TEST_GEOM"},
+ columns: []string{"COLUMN_NAME", "DATA_TYPE", "DATA_TYPE_OWNER"},
+ rows: [][]driver.Value{{"ID", "NUMBER", nil}},
+ },
+ })
+ s := newServer()
+ s.db = db
+ _, err := s.executeQueryPage(queryOptions{SQL: "SELECT * FROM SDE.TEST_GEOM"}, 10)
+ if err == nil || !strings.Contains(err.Error(), "TTC error: received code 3") {
+ t.Fatalf("expected the original TTC error, got %v", err)
+ }
+ if scripted.next != len(scripted.steps) {
+ t.Fatalf("expected %d queries, got %d", len(scripted.steps), scripted.next)
+ }
+}
+
+func TestOraclePagedRetrySQLChoosesRewriteByFailureKind(t *testing.T) {
+ loadColumns := func(string, string) ([]oracleColumnMeta, error) {
+ return []oracleColumnMeta{{Name: "GEOM", DataType: "SDO_GEOMETRY", DataTypeOwner: "PUBLIC"}}, nil
+ }
+ ttcErr := errors.New("TTC error: received code 3 during response reading")
+
+ rewritten, ok := oraclePagedRetrySQL("SELECT * FROM TEST_GEOM", loadColumns, ttcErr)
+ if !ok {
+ t.Fatal("expected a TTC-error retry")
+ }
+ if !strings.Contains(rewritten, `SDO_UTIL.TO_WKTGEOMETRY("GEOM") AS "GEOM"`) {
+ t.Fatalf("TTC retry must read geometry as WKT, got %s", rewritten)
+ }
+
+ placeholder, ok := oraclePagedRetrySQL("SELECT * FROM TEST_GEOM", loadColumns, oracleDriverPanicError{value: "decode panic"})
+ if !ok {
+ t.Fatal("expected a panic retry")
+ }
+ if !strings.Contains(placeholder, "'<SDO_GEOMETRY>'") {
+ t.Fatalf("panic retry must use the placeholder projection, got %s", placeholder)
+ }
+
+ if _, ok := oraclePagedRetrySQL("SELECT * FROM TEST_GEOM", loadColumns, errors.New("ORA-00942: table or view does not exist")); ok {
+ t.Fatal("unrelated errors must not be retried")
+ }
+}
+
func TestRuntimeHandleLineRecoversRequestPanic(t *testing.T) {
db, _ := openOracleViewSourceTestDB(t, []oracleViewSourceQueryStep{
{