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

fix(go/adbc/driver/snowflake): call GetObjects with null catalog at catalog depth #2194

Merged
merged 5 commits into from
Oct 2, 2024
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
48 changes: 34 additions & 14 deletions go/adbc/driver/snowflake/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ const (
defaultStatementQueueSize = 200
defaultPrefetchConcurrency = 10

queryTemplateGetObjectsAll = "get_objects_all.sql"
queryTemplateGetObjectsCatalogs = "get_objects_catalogs.sql"
queryTemplateGetObjectsDbSchemas = "get_objects_dbschemas.sql"
queryTemplateGetObjectsTables = "get_objects_tables.sql"
queryTemplateGetObjectsAll = "get_objects_all.sql"
queryTemplateGetObjectsCatalogs = "get_objects_catalogs.sql"
queryTemplateGetObjectsDbSchemas = "get_objects_dbschemas.sql"
queryTemplateGetObjectsTables = "get_objects_tables.sql"
queryTemplateGetObjectsTerseCatalogs = "get_objects_terse_catalogs.sql"
)

//go:embed queries/*
Expand Down Expand Up @@ -74,7 +75,7 @@ type connectionImpl struct {

func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth, catalog *string, dbSchema *string, tableName *string, columnName *string, tableType []string) (array.RecordReader, error) {
var (
pkQueryID, fkQueryID, uniqueQueryID string
pkQueryID, fkQueryID, uniqueQueryID, terseDbQueryID string
)

conn, err := c.sqldb.Conn(ctx)
Expand All @@ -83,11 +84,28 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
}
defer conn.Close()

gConstraints, gContraintsCtx := errgroup.WithContext(ctx)
gQueryIDs, gQueryIDsCtx := errgroup.WithContext(ctx)
queryFile := queryTemplateGetObjectsAll
switch depth {
case adbc.ObjectDepthCatalogs:
queryFile = queryTemplateGetObjectsCatalogs
if catalog == nil {
queryFile = queryTemplateGetObjectsTerseCatalogs
// if the catalog is null, show the terse databases
// which doesn't require a database context
gQueryIDs.Go(func() error {
return conn.Raw(func(driverConn any) error {
rows, err := driverConn.(driver.QueryerContext).QueryContext(gQueryIDsCtx, "SHOW TERSE DATABASES", nil)
if err != nil {
return err
}

terseDbQueryID = rows.(gosnowflake.SnowflakeRows).GetQueryID()
return rows.Close()
})
})
} else {
queryFile = queryTemplateGetObjectsCatalogs
}
case adbc.ObjectDepthDBSchemas:
queryFile = queryTemplateGetObjectsDbSchemas
case adbc.ObjectDepthTables:
Expand All @@ -96,9 +114,9 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
default:
// Detailed constraint info not available in information_schema
// Need to dispatch SHOW queries and use conn.Raw to extract the queryID for reuse in GetObjects query
gConstraints.Go(func() error {
gQueryIDs.Go(func() error {
return conn.Raw(func(driverConn any) error {
rows, err := driverConn.(driver.QueryerContext).QueryContext(gContraintsCtx, "SHOW PRIMARY KEYS", nil)
rows, err := driverConn.(driver.QueryerContext).QueryContext(gQueryIDsCtx, "SHOW PRIMARY KEYS", nil)
if err != nil {
return err
}
Expand All @@ -108,9 +126,9 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
})
})

gConstraints.Go(func() error {
gQueryIDs.Go(func() error {
return conn.Raw(func(driverConn any) error {
rows, err := driverConn.(driver.QueryerContext).QueryContext(gContraintsCtx, "SHOW IMPORTED KEYS", nil)
rows, err := driverConn.(driver.QueryerContext).QueryContext(gQueryIDsCtx, "SHOW IMPORTED KEYS", nil)
if err != nil {
return err
}
Expand All @@ -120,9 +138,9 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
})
})

gConstraints.Go(func() error {
gQueryIDs.Go(func() error {
return conn.Raw(func(driverConn any) error {
rows, err := driverConn.(driver.QueryerContext).QueryContext(gContraintsCtx, "SHOW UNIQUE KEYS", nil)
rows, err := driverConn.(driver.QueryerContext).QueryContext(gQueryIDsCtx, "SHOW UNIQUE KEYS", nil)
if err != nil {
return err
}
Expand All @@ -145,7 +163,7 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
}

// Need constraint subqueries to complete before we can query GetObjects
if err := gConstraints.Wait(); err != nil {
if err := gQueryIDs.Wait(); err != nil {
return nil, err
}

Expand All @@ -157,9 +175,11 @@ func (c *connectionImpl) GetObjects(ctx context.Context, depth adbc.ObjectDepth,
driverbase.PatternToNamedArg("COLUMN", columnName),

// QueryIDs for constraint data if depth is tables or deeper
// or if the depth is catalog and catalog is null
sql.Named("PK_QUERY_ID", pkQueryID),
sql.Named("FK_QUERY_ID", fkQueryID),
sql.Named("UNIQUE_QUERY_ID", uniqueQueryID),
sql.Named("SHOW_DB_QUERY_ID", terseDbQueryID),
}

// the connection that is used is not the same connection context where the database may have been set
Expand Down
6 changes: 6 additions & 0 deletions go/adbc/driver/snowflake/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2158,3 +2158,9 @@ func (suite *SnowflakeTests) TestGetSetClientConfigFile() {
suite.NoError(err)
suite.True(file == result)
}

func (suite *SnowflakeTests) TestGetObjectsWithNilCatalog() {
// this test demonstrates calling GetObjects with the catalog depth and a nil catalog
_, err := suite.cnxn.GetObjects(suite.ctx, adbc.ObjectDepthCatalogs, nil, nil, nil, nil, nil)
suite.NoError(err)
}
24 changes: 24 additions & 0 deletions go/adbc/driver/snowflake/queries/get_objects_terse_catalogs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you under the Apache License, Version 2.0 (the
-- "License"); you may not use this file except in compliance
-- with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing,
-- software distributed under the License is distributed on an
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-- KIND, either express or implied. See the License for the
-- specific language governing permissions and limitations
-- under the License.

-- SHOW TERSE DATABASES would run before this
SELECT
{
'catalog_name': "name",
'catalog_db_schemas': null
} get_objects
FROM TABLE(RESULT_SCAN(:SHOW_DB_QUERY_ID))
Loading