databricks / databricks-sql-go

Golang database/sql driver for Databricks SQL.
Apache License 2.0
37 stars 41 forks source link

API to get databricks query ID #133

Closed agchang closed 7 months ago

agchang commented 1 year ago

Apologies if I missed something, but is there currently an API to get the databricks query id from a SQL query? Specifically, the one that shows up on the "Query History" on the Databricks UI. If I enable logging as in the docs, I can see it, but not sure how to programmatically access this.

rcypher-databricks commented 1 year ago

The driverctx package exposes a function to allow adding a hook to the Context object passed to QueryContext() or ExecContext() which will be called with the query ID. type IdCallbackFunc func(string) func NewContextWithQueryIdCallback(ctx context.Context, callback IdCallbackFunc) context.Context

var queryId string
callback := func(id string){queryId = id}
ctx := driverctx.NewContextWithQueryIdCallback(context.Background(), callback)
db := sql.OpenDB(...)
defer db.Close()
rows, err1 := db.QueryContext(ctx, `select * from foo`)
fmt.Println(queryId)

There is also a corresponding function for getting the connection Id. NewContextWithConnIdCallback

agchang commented 1 year ago

Thanks for the pointer. Are we guaranteed that the callback runs after the db.QueryContext statement?

agchang commented 1 year ago

Ah, I guess it is a callback to get around the fact that there isn't a straightforward way to pass this information back when implementing a db/sql driver. Another approach is using a channel to communicate this: https://github.com/snowflakedb/gosnowflake/blob/v1.6.22/util.go#L48.

Thanks for answering - might want to add some documentation as it wasn't super obvious to me :-)

rcypher-databricks commented 1 year ago

I just double checked the package documentation at https://pkg.go.dev/github.com/databricks/databricks-sql-go There's a section called 'Programmatically Retrieving Connection and Query Id' that describes this. If it's alright with you I will mark this as closed.