Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions experimental/aitools/cmd/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ type batchResultError struct {
// params, if non-nil, are bound on every statement. The same parameter set is
// reused across the batch, so callers must ensure each SQL uses only markers
// that are covered.
func executeBatch(ctx context.Context, api sql.StatementExecutionInterface, warehouseID string, sqls []string, params []sql.StatementParameterListItem, concurrency int) []batchResult {
client := sqlexec.New(api, warehouseID)
//
// Tests pass sqlexec.WithPollInterval in opts to skip the real poll backoff.
func executeBatch(ctx context.Context, api sql.StatementExecutionInterface, warehouseID string, sqls []string, params []sql.StatementParameterListItem, concurrency int, opts ...sqlexec.Option) []batchResult {
client := sqlexec.New(api, warehouseID, opts...)

pollCtx, pollCancel := context.WithCancel(ctx)
defer pollCancel()
Expand Down
37 changes: 24 additions & 13 deletions experimental/aitools/cmd/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ func TestExecuteBatchSetsOnWaitTimeoutContinue(t *testing.T) {
}

func TestExecuteBatchPreservesInputOrder(t *testing.T) {
// Index 0 is slow (PENDING then SUCCEEDED on first poll); 1 and 2 are
// immediate. Despite the staggered completion, results stay in input order.
// Index 0 is slow (PENDING, then SUCCEEDED on first poll); 1 and 2 are
// immediate. With two slots, the 'fast2' submission can only start after
// 'fast1' has finished, and the slow poll blocks until that submission
// starts. So 'slow' always completes last, yet results stay in input order.
ctx := cmdio.MockDiscard(t.Context())
mockAPI := mocksql.NewMockStatementExecutionInterface(t)
fast2Submitted := make(chan struct{})

mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool {
return req.Statement == "SELECT 'slow'"
Expand All @@ -199,23 +202,31 @@ func TestExecuteBatchPreservesInputOrder(t *testing.T) {
Status: &sql.StatementStatus{State: sql.StatementStatePending},
}, nil).Once()

mockAPI.EXPECT().GetStatementByStatementId(mock.Anything, "stmt-slow").Return(&sql.StatementResponse{
mockAPI.EXPECT().GetStatementByStatementId(mock.Anything, "stmt-slow").Run(func(context.Context, string) {
<-fast2Submitted
}).Return(&sql.StatementResponse{
StatementId: "stmt-slow",
Status: &sql.StatementStatus{State: sql.StatementStateSucceeded},
}, nil).Once()

for i, sqlStr := range []string{"SELECT 'fast1'", "SELECT 'fast2'"} {
sid := fmt.Sprintf("stmt-fast-%d", i+1)
mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool {
return req.Statement == sqlStr
})).Return(&sql.StatementResponse{
StatementId: sid,
Status: &sql.StatementStatus{State: sql.StatementStateSucceeded},
}, nil).Once()
}
mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool {
return req.Statement == "SELECT 'fast1'"
})).Return(&sql.StatementResponse{
StatementId: "stmt-fast-1",
Status: &sql.StatementStatus{State: sql.StatementStateSucceeded},
}, nil).Once()

mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool {
return req.Statement == "SELECT 'fast2'"
})).Run(func(context.Context, sql.ExecuteStatementRequest) {
close(fast2Submitted)
}).Return(&sql.StatementResponse{
StatementId: "stmt-fast-2",
Status: &sql.StatementStatus{State: sql.StatementStateSucceeded},
}, nil).Once()

sqls := []string{"SELECT 'slow'", "SELECT 'fast1'", "SELECT 'fast2'"}
results := executeBatch(ctx, mockAPI, "wh-1", sqls, nil, 8)
results := executeBatch(ctx, mockAPI, "wh-1", sqls, nil, 2, fastPoll)

require.Len(t, results, 3)
for i, r := range results {
Expand Down
9 changes: 7 additions & 2 deletions experimental/aitools/cmd/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/experimental/libs/sqlcli"
Expand All @@ -21,6 +22,10 @@ import (
"github.com/stretchr/testify/require"
)

// fastPoll keeps the package's polling tests from waiting on the engine's
// real 1s+ backoff.
var fastPoll = sqlexec.WithPollInterval(time.Millisecond, time.Millisecond)

func TestCleanSQL(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -129,7 +134,7 @@ func TestExecuteAndPollWithPolling(t *testing.T) {
Result: &sql.ResultData{DataArray: [][]string{{"42"}}},
}, nil).Once()

client := sqlexec.New(mockAPI, "wh-123")
client := sqlexec.New(mockAPI, "wh-123", fastPoll)
stmt, err := executeAndPoll(ctx, client, "SELECT 42", nil)
require.NoError(t, err)
assert.Equal(t, sql.StatementStateSucceeded, stmt.State)
Expand All @@ -156,7 +161,7 @@ func TestExecuteAndPollFailsDuringPolling(t *testing.T) {
},
}, nil).Once()

_, err := executeAndPoll(ctx, sqlexec.New(mockAPI, "wh-123"), "SELECT 1", nil)
_, err := executeAndPoll(ctx, sqlexec.New(mockAPI, "wh-123", fastPoll), "SELECT 1", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "RESOURCE_EXHAUSTED")
}
Expand Down
6 changes: 4 additions & 2 deletions experimental/aitools/cmd/statement_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ invoked the synchronous path.)`,
// Context cancellation propagates from the poll WITHOUT cancelling the
// server-side statement (intentional: 'get' is a poll-only operation; use
// 'cancel' to terminate explicitly).
func getStatementResult(ctx context.Context, api sql.StatementExecutionInterface, statementID string) (statementInfo, error) {
//
// Tests pass sqlexec.WithPollInterval in opts to skip the real poll backoff.
func getStatementResult(ctx context.Context, api sql.StatementExecutionInterface, statementID string, opts ...sqlexec.Option) (statementInfo, error) {
// Get/Poll don't use the warehouse ID.
client := sqlexec.New(api, "")
client := sqlexec.New(api, "", opts...)

// Fetch the current state first so Poll can short-circuit if the statement
// is already terminal.
Expand Down
2 changes: 1 addition & 1 deletion experimental/aitools/cmd/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestGetStatementResultPolls(t *testing.T) {
Result: &sql.ResultData{DataArray: [][]string{{"42"}}},
}, nil).Once()

info, err := getStatementResult(ctx, mockAPI, "stmt-1")
info, err := getStatementResult(ctx, mockAPI, "stmt-1", fastPoll)
require.NoError(t, err)
assert.Equal(t, "stmt-1", info.StatementID)
assert.Equal(t, sql.StatementStateSucceeded, info.State)
Expand Down
Loading