Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
maiadegraaf committed Jan 15, 2024
2 parents f053e00 + 6b90a94 commit 8457b52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
16 changes: 16 additions & 0 deletions duckdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,22 @@ func TestParquetExtension(t *testing.T) {
require.NoError(t, err)
}

func TestQueryTimeout(t *testing.T) {
db := openDB(t)
defer db.Close()

ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*250)
defer cancel()

now := time.Now()
_, err := db.ExecContext(ctx, "CREATE TABLE test AS SELECT * FROM range(10000000) t1, range(1000000) t2;")
require.ErrorIs(t, err, context.DeadlineExceeded)

// a very defensive time check, but should be good enough
// the query takes much longer than 10 seconds
require.Less(t, time.Since(now), 10*time.Second)
}

func openDB(t *testing.T) *sql.DB {
db, err := sql.Open("duckdb", "")
require.NoError(t, err)
Expand Down
29 changes: 15 additions & 14 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,31 @@ func (s *stmt) execute(ctx context.Context, args []driver.NamedValue) (*C.duckdb
}
defer C.duckdb_destroy_pending(&pendingRes)

for {
done := make(chan bool)
defer close(done)

go func() {
select {
// if context is cancelled or deadline exceeded, don't execute further
case <-ctx.Done():
return nil, ctx.Err()
default:
// continue
}
state := C.duckdb_pending_execute_task(pendingRes)
if state == C.DUCKDB_PENDING_ERROR {
dbErr := C.GoString(C.duckdb_pending_error(pendingRes))
return nil, errors.New(dbErr)
}
if C.duckdb_pending_execution_is_finished(state) {
break
// also need to interrupt to cancel the query
C.duckdb_interrupt(*s.c.con)
return
case <-done:
return
}
}
}()

var res C.duckdb_result
if state := C.duckdb_execute_pending(pendingRes, &res); state == C.DuckDBError {
if ctx.Err() != nil {
return nil, ctx.Err()
}

dbErr := C.GoString(C.duckdb_result_error(&res))
C.duckdb_destroy_result(&res)
return nil, errors.New(dbErr)
}

return &res, nil
}

Expand Down

0 comments on commit 8457b52

Please sign in to comment.