Skip to content

Commit 4af45e5

Browse files
h3n4lclaude
andauthored
fix(databricks): apply user-selected row limit to SQL Editor queries (#20190)
The Databricks driver discarded `db.QueryContext`, so the row-limit toggle in the SQL Editor (default 1000) was silently ignored. Wide queries against shared catalogs hit Databricks' INLINE response cap (~25 MiB) and aborted with no result available: Statements executed with disposition=INLINE can have a result size of at most 20214400 bytes. Forward `queryContext.Limit` into `execStatementSync` and set `RowLimit` on the SDK request when non-zero. Sync and dump paths pass 0 so admin queries (SHOW CREATE TABLE, DESC FORMATTED, SELECT VERSION) remain unbounded. This also surfaces a `truncated` flag on the response when the limit trims the result set. Refs BYT-9379. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 695d534 commit 4af45e5

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

backend/plugin/db/databricks/databricks.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (*Driver) GetDB() *sql.DB {
7878
return nil
7979
}
8080

81-
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, _ db.QueryContext) ([]*v1pb.QueryResult, error) {
81+
func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, queryContext db.QueryContext) ([]*v1pb.QueryResult, error) {
8282
var results []*v1pb.QueryResult
8383
stmts, err := base.SplitMultiSQL(storepb.Engine_DATABRICKS, statement)
8484
if err != nil {
@@ -88,7 +88,7 @@ func (d *Driver) QueryConn(ctx context.Context, _ *sql.Conn, statement string, _
8888
for _, stmt := range stmts {
8989
result := &v1pb.QueryResult{}
9090
startTime := time.Now()
91-
dataArr, colInfo, err := d.execStatementSync(ctx, stmt.Text)
91+
dataArr, colInfo, err := d.execStatementSync(ctx, stmt.Text, int64(queryContext.Limit))
9292
if err != nil {
9393
return nil, err
9494
}
@@ -158,15 +158,25 @@ func (d *Driver) Execute(ctx context.Context, statement string, _ db.ExecuteOpti
158158
}
159159

160160
// Execute SQL statement synchronously and return row data or error.
161-
func (d *Driver) execStatementSync(ctx context.Context, statement string) ([][]string, []dbsql.ColumnInfo, error) {
161+
// rowLimit caps the result set when > 0; 0 means no limit (used by sync
162+
// and dump paths). DDL/DML callers should pass 0.
163+
func (d *Driver) execStatementSync(ctx context.Context, statement string, rowLimit int64) ([][]string, []dbsql.ColumnInfo, error) {
162164
// Bind the connected Bytebase database (= Unity Catalog) to the
163165
// session, so unqualified `schema.table` references resolve like in
164166
// other RDBMSes. Equivalent to issuing `USE CATALOG <name>` first.
165-
resp, err := d.Client.StatementExecution.ExecuteAndWait(ctx, dbsql.ExecuteStatementRequest{
167+
req := dbsql.ExecuteStatementRequest{
166168
Statement: statement,
167169
WarehouseId: d.WarehouseID,
168170
Catalog: d.curCatalog,
169-
})
171+
}
172+
// Honor the user-selected row limit (default 1000 from the SQL Editor).
173+
// Without it, INLINE results above ~25 MiB are aborted server-side
174+
// with no result available — see DispositionExternalLinks for the
175+
// large-result path.
176+
if rowLimit > 0 {
177+
req.RowLimit = rowLimit
178+
}
179+
resp, err := d.Client.StatementExecution.ExecuteAndWait(ctx, req)
170180
if err != nil {
171181
return nil, nil, err
172182
}

backend/plugin/db/databricks/dump.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (d *Driver) Dump(ctx context.Context, writer io.Writer, _ *storepb.Database
129129
}
130130

131131
func (d *Driver) showCreateTable(ctx context.Context, qualifiedTblName string) (string, error) {
132-
rows, colInfo, err := d.execStatementSync(ctx, fmt.Sprintf("SHOW CREATE TABLE %s", qualifiedTblName))
132+
rows, colInfo, err := d.execStatementSync(ctx, fmt.Sprintf("SHOW CREATE TABLE %s", qualifiedTblName), 0)
133133
if err != nil {
134134
return "", err
135135
}
@@ -170,7 +170,7 @@ func (d *Driver) descTbl(ctx context.Context, tblName string) (*tableAdditionalI
170170
tblAddInfo := &tableAdditionalInfo{
171171
tblProps: make(map[string]string),
172172
}
173-
rows, _, err := d.execStatementSync(ctx, fmt.Sprintf("DESC FORMATTED %s", tblName))
173+
rows, _, err := d.execStatementSync(ctx, fmt.Sprintf("DESC FORMATTED %s", tblName), 0)
174174
if err != nil {
175175
return nil, err
176176
}

backend/plugin/db/databricks/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (d *Driver) SyncInstance(ctx context.Context) (*db.InstanceMetadata, error)
5454
instanceMetadata := &db.InstanceMetadata{}
5555

5656
// fetch version.
57-
versionData, _, err := d.execStatementSync(ctx, "SELECT VERSION()")
57+
versionData, _, err := d.execStatementSync(ctx, "SELECT VERSION()", 0)
5858
if err != nil {
5959
return nil, err
6060
}

0 commit comments

Comments
 (0)