Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go/adbc/driver/snowflake): add query tag option #2484

Merged
merged 1 commit into from
Jan 28, 2025
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
41 changes: 41 additions & 0 deletions go/adbc/driver/snowflake/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2253,3 +2253,44 @@ func TestJSONUnmarshal(t *testing.T) {
}
}
}

func (suite *SnowflakeTests) TestQueryTag() {
u, err := uuid.NewV7()
suite.Require().NoError(err)
tag := u.String()
suite.Require().NoError(suite.stmt.SetOption(driver.OptionStatementQueryTag, tag))

val, err := suite.stmt.(adbc.GetSetOptions).GetOption(driver.OptionStatementQueryTag)
suite.Require().NoError(err)
suite.Require().Equal(tag, val)

suite.Require().NoError(suite.stmt.SetSqlQuery("SELECT 1"))
rdr, n, err := suite.stmt.ExecuteQuery(suite.ctx)
suite.Require().NoError(err)
defer rdr.Release()
Comment on lines +2268 to +2270
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i doubt it, but is there any way we can test / confirm that the tag is being propagated for the query?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the test: we query the query history with the tag and make sure it comes back

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i get it. I missed that's what the query below was doing. Makes sense! Thanks!


suite.EqualValues(1, n)
suite.True(rdr.Next())
suite.False(rdr.Next())
suite.Require().NoError(rdr.Err())

// Unset tag
suite.Require().NoError(suite.stmt.SetOption(driver.OptionStatementQueryTag, ""))

suite.Require().NoError(suite.stmt.SetSqlQuery(fmt.Sprintf(`
SELECT query_text
FROM table(information_schema.query_history())
WHERE query_tag = '%s'
ORDER BY start_time;
`, tag)))
rdr, n, err = suite.stmt.ExecuteQuery(suite.ctx)
suite.Require().NoError(err)
defer rdr.Release()

suite.EqualValues(1, n)
suite.True(rdr.Next())
result := rdr.Record()
suite.Require().Equal("SELECT 1", result.Column(0).(*array.String).Value(0))
suite.False(rdr.Next())
suite.Require().NoError(rdr.Err())
}
23 changes: 23 additions & 0 deletions go/adbc/driver/snowflake/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
)

const (
OptionStatementQueryTag = "adbc.snowflake.statement.query_tag"
OptionStatementQueueSize = "adbc.rpc.result_queue_size"
OptionStatementPrefetchConcurrency = "adbc.snowflake.rpc.prefetch_concurrency"
OptionStatementIngestWriterConcurrency = "adbc.snowflake.statement.ingest_writer_concurrency"
Expand All @@ -54,11 +55,20 @@ type statement struct {
targetTable string
ingestMode string
ingestOptions *ingestOptions
queryTag string

bound arrow.Record
streamBind array.RecordReader
}

// setQueryContext applies the query tag if present.
func (st *statement) setQueryContext(ctx context.Context) context.Context {
if st.queryTag != "" {
ctx = gosnowflake.WithQueryTag(ctx, st.queryTag)
}
return ctx
}

// Close releases any relevant resources associated with this statement
// and closes it (particularly if it is a prepared statement).
//
Expand All @@ -82,6 +92,10 @@ func (st *statement) Close() error {
}

func (st *statement) GetOption(key string) (string, error) {
switch key {
case OptionStatementQueryTag:
return st.queryTag, nil
}
return "", adbc.Error{
Msg: fmt.Sprintf("[Snowflake] Unknown statement option '%s'", key),
Code: adbc.StatusNotFound,
Expand Down Expand Up @@ -186,6 +200,9 @@ func (st *statement) SetOption(key string, val string) error {
}
}
return st.SetOptionInt(key, int64(size))
case OptionStatementQueryTag:
st.queryTag = val
return nil
case OptionUseHighPrecision:
switch val {
case adbc.OptionValueEnabled:
Expand Down Expand Up @@ -449,6 +466,8 @@ func (st *statement) executeIngest(ctx context.Context) (int64, error) {
//
// This invalidates any prior result sets on this statement.
func (st *statement) ExecuteQuery(ctx context.Context) (array.RecordReader, int64, error) {
ctx = st.setQueryContext(ctx)

if st.targetTable != "" {
n, err := st.executeIngest(ctx)
return nil, n, err
Expand Down Expand Up @@ -500,6 +519,8 @@ func (st *statement) ExecuteQuery(ctx context.Context) (array.RecordReader, int6
// ExecuteUpdate executes a statement that does not generate a result
// set. It returns the number of rows affected if known, otherwise -1.
func (st *statement) ExecuteUpdate(ctx context.Context) (int64, error) {
ctx = st.setQueryContext(ctx)

if st.targetTable != "" {
return st.executeIngest(ctx)
}
Expand Down Expand Up @@ -558,6 +579,8 @@ func (st *statement) ExecuteUpdate(ctx context.Context) (int64, error) {

// ExecuteSchema gets the schema of the result set of a query without executing it.
func (st *statement) ExecuteSchema(ctx context.Context) (*arrow.Schema, error) {
ctx = st.setQueryContext(ctx)

if st.targetTable != "" {
return nil, adbc.Error{
Msg: "cannot execute schema for ingestion",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class StatementOptions(enum.Enum):
#: Number of concurrent streams being prefetched for a result set.
#: Defaults to 10.
PREFETCH_CONCURRENCY = "adbc.snowflake.rpc.prefetch_concurrency"
#: An identifier for a query/queries that can be used to find the query in
#: the query history. Use a blank string to unset the tag.
QUERY_TAG = "adbc.snowflake.statement.query_tag"
Comment on lines +115 to +117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did we ever update the docs to point out the difference between statement options and connection options etc.?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. In general I'd like to improve the docs...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#: Number of parquet files to write in parallel for bulk ingestion
#: Defaults to NumCPU
INGEST_WRITER_CONCURRENCY = "adbc.snowflake.statement.ingest_writer_concurrency"
Expand Down
Loading