Skip to content

Commit

Permalink
refactor: Refactored all the filter conditions for GetObjects into a …
Browse files Browse the repository at this point in the history
…single prepareFilterConditions function
  • Loading branch information
ryan-syed committed Dec 15, 2023
1 parent 8edb67e commit a9f0b11
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 58 deletions.
33 changes: 16 additions & 17 deletions go/adbc/driver/snowflake/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,16 +678,6 @@ func prepareCatalogsSQL() string {
}

func prepareDbSchemasSQL(matchingCatalogNames []string, catalog *string, dbSchema *string) string {
conditions := make([]string, 0)
if catalog != nil && *catalog != "" {
conditions = append(conditions, ` CATALOG_NAME ILIKE '`+*catalog+`'`)
}
if dbSchema != nil && *dbSchema != "" {
conditions = append(conditions, ` SCHEMA_NAME ILIKE '`+*dbSchema+`'`)
}

cond := strings.Join(conditions, " AND ")

query := ""
for _, catalog_name := range matchingCatalogNames {
if query != "" {
Expand All @@ -697,8 +687,9 @@ func prepareDbSchemasSQL(matchingCatalogNames []string, catalog *string, dbSchem
}

query = `SELECT CATALOG_NAME, SCHEMA_NAME FROM (` + query + `)`
if cond != "" {
query += " WHERE " + cond
conditions := prepareFilterConditions(adbc.ObjectDepthDBSchemas, catalog, dbSchema, nil, nil, make([]string, 0))
if conditions != "" {
query += " WHERE " + conditions
}

return query
Expand All @@ -714,7 +705,7 @@ func prepareTablesSQL(matchingCatalogNames []string, catalog *string, dbSchema *
}

query = `SELECT table_catalog, table_schema, table_name, table_type FROM (` + query + `)`
conditions := prepareFilterConditionsTablesAndColumns(catalog, dbSchema, tableName, nil, tableType)
conditions := prepareFilterConditions(adbc.ObjectDepthTables, catalog, dbSchema, tableName, nil, tableType)
if conditions != "" {
query += " WHERE " + conditions
}
Expand Down Expand Up @@ -745,7 +736,7 @@ func prepareColumnsSQL(matchingCatalogNames []string, catalog *string, dbSchema
identity_generation, identity_increment,
character_maximum_length, character_octet_length, datetime_precision, comment FROM (` + prefixQuery + `)`
ordering := ` ORDER BY table_catalog, table_schema, table_name, ordinal_position`
conditions := prepareFilterConditionsTablesAndColumns(catalog, dbSchema, tableName, columnName, tableType)
conditions := prepareFilterConditions(adbc.ObjectDepthColumns, catalog, dbSchema, tableName, columnName, tableType)
query := prefixQuery

if conditions != "" {
Expand All @@ -756,13 +747,21 @@ func prepareColumnsSQL(matchingCatalogNames []string, catalog *string, dbSchema
return query
}

func prepareFilterConditionsTablesAndColumns(catalog *string, dbSchema *string, tableName *string, columnName *string, tableType []string) string {
func prepareFilterConditions(depth adbc.ObjectDepth, catalog *string, dbSchema *string, tableName *string, columnName *string, tableType []string) string {
conditions := make([]string, 0)
if catalog != nil && *catalog != "" {
conditions = append(conditions, ` TABLE_CATALOG ILIKE '`+*catalog+`'`)
if depth == adbc.ObjectDepthDBSchemas {
conditions = append(conditions, ` CATALOG_NAME ILIKE '`+*catalog+`'`)
} else {
conditions = append(conditions, ` TABLE_CATALOG ILIKE '`+*catalog+`'`)
}
}
if dbSchema != nil && *dbSchema != "" {
conditions = append(conditions, ` TABLE_SCHEMA ILIKE '`+*dbSchema+`'`)
if depth == adbc.ObjectDepthDBSchemas {
conditions = append(conditions, ` SCHEMA_NAME ILIKE '`+*dbSchema+`'`)
} else {
conditions = append(conditions, ` TABLE_SCHEMA ILIKE '`+*dbSchema+`'`)
}
}
if tableName != nil && *tableName != "" {
conditions = append(conditions, ` TABLE_NAME ILIKE '`+*tableName+`'`)
Expand Down
80 changes: 39 additions & 41 deletions go/adbc/driver/snowflake/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPrepareDbSchemasSQLWithNoFilterOneCatalog(t *testing.T) {
schemaPattern := ""

expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.SCHEMATA
)`
Expand All @@ -35,12 +35,12 @@ func TestPrepareDbSchemasSQLWithNoFilter(t *testing.T) {
schemaPattern := ""

expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "HELLO_DB".INFORMATION_SCHEMA.SCHEMATA
)`
actual := prepareDbSchemasSQL(catalogNames[:], &catalogPattern, &schemaPattern)
Expand All @@ -49,18 +49,17 @@ func TestPrepareDbSchemasSQLWithNoFilter(t *testing.T) {
}

func TestPrepareDbSchemasSQLWithCatalogFilter(t *testing.T) {
//prepareDbSchemasSQL(catalogNames []string, catalog *string, dbSchema *string)
catalogNames := [3]string{"DEMO_DB", "DEMO'DB", "HELLO_DB"}
catalogPattern := "DEMO_DB"
schemaPattern := ""

expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "HELLO_DB".INFORMATION_SCHEMA.SCHEMATA
)
WHERE CATALOG_NAME ILIKE 'DEMO_DB'`
Expand All @@ -70,18 +69,17 @@ func TestPrepareDbSchemasSQLWithCatalogFilter(t *testing.T) {
}

func TestPrepareDbSchemasSQLWithSchemaFilter(t *testing.T) {
//prepareDbSchemasSQL(catalogNames []string, catalog *string, dbSchema *string)
catalogNames := [3]string{"DEMO_DB", "DEMO'DB", "HELLO_DB"}
catalogPattern := ""
schemaPattern := "PUBLIC"

expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "HELLO_DB".INFORMATION_SCHEMA.SCHEMATA
)
WHERE SCHEMA_NAME ILIKE 'PUBLIC'`
Expand All @@ -95,17 +93,17 @@ func TestPrepareDbSchemasSQL(t *testing.T) {
catalogPattern := "DEMO_DB"
schemaPattern := "PUBLIC"

expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
expected := `SELECT CATALOG_NAME, SCHEMA_NAME
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
SELECT * FROM "DEMOADB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "DEMOADB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.SCHEMATA
UNION ALL
UNION ALL
SELECT * FROM "HELLO_DB".INFORMATION_SCHEMA.SCHEMATA
)
)
WHERE CATALOG_NAME ILIKE 'DEMO_DB' AND SCHEMA_NAME ILIKE 'PUBLIC'`
actual := prepareDbSchemasSQL(catalogNames[:], &catalogPattern, &schemaPattern)

Expand All @@ -119,13 +117,13 @@ func TestPrepareTablesSQLWithNoFilter(t *testing.T) {
tableNamePattern := ""
tableType := make([]string, 0)

expected := `SELECT table_catalog, table_schema, table_name, table_type
FROM
expected := `SELECT table_catalog, table_schema, table_name, table_type
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMOADB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.TABLES
)`
actual := prepareTablesSQL(catalogNames[:], &catalogPattern, &schemaPattern, &tableNamePattern, tableType[:])
Expand All @@ -141,12 +139,12 @@ func TestPrepareTablesSQLWithNoTableTypeFilter(t *testing.T) {
tableType := make([]string, 0)

expected := `SELECT table_catalog, table_schema, table_name, table_type
FROM
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMOADB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.TABLES
)
WHERE TABLE_CATALOG ILIKE 'DEMO_DB' AND TABLE_SCHEMA ILIKE 'PUBLIC' AND TABLE_NAME ILIKE 'ADBC-TABLE'`
Expand All @@ -163,12 +161,12 @@ func TestPrepareTablesSQL(t *testing.T) {
tableType := [2]string{"BASE TABLE", "VIEW"}

expected := `SELECT table_catalog, table_schema, table_name, table_type
FROM
FROM
(
SELECT * FROM "DEMO_DB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMOADB".INFORMATION_SCHEMA.TABLES
UNION ALL
UNION ALL
SELECT * FROM "DEMO'DB".INFORMATION_SCHEMA.TABLES
)
WHERE TABLE_CATALOG ILIKE 'DEMO_DB' AND TABLE_SCHEMA ILIKE 'PUBLIC' AND TABLE_NAME ILIKE 'ADBC-TABLE' AND TABLE_TYPE IN ('BASE TABLE','VIEW')`
Expand All @@ -189,11 +187,11 @@ func TestPrepareColumnsSQLNoFilter(t *testing.T) {
ordinal_position, is_nullable::boolean, data_type, numeric_precision,
numeric_precision_radix, numeric_scale, is_identity::boolean,
identity_generation, identity_increment,
character_maximum_length, character_octet_length, datetime_precision, comment
FROM
character_maximum_length, character_octet_length, datetime_precision, comment
FROM
(
SELECT T.table_type, C.*
FROM
FROM
"DEMO_DB".INFORMATION_SCHEMA.TABLES AS T
JOIN
"DEMO_DB".INFORMATION_SCHEMA.COLUMNS AS C
Expand All @@ -203,11 +201,11 @@ func TestPrepareColumnsSQLNoFilter(t *testing.T) {
SELECT T.table_type, C.*
FROM
"DEMOADB".INFORMATION_SCHEMA.TABLES AS T
JOIN
JOIN
"DEMOADB".INFORMATION_SCHEMA.COLUMNS AS C
ON
T.table_catalog = C.table_catalog AND T.table_schema = C.table_schema AND t.table_name = C.table_name
)
)
ORDER BY table_catalog, table_schema, table_name, ordinal_position`
actual := prepareColumnsSQL(catalogNames[:], &catalogPattern, &schemaPattern, &tableNamePattern, &columnNamePattern, tableType[:])

Expand All @@ -226,11 +224,11 @@ func TestPrepareColumnsSQL(t *testing.T) {
ordinal_position, is_nullable::boolean, data_type, numeric_precision,
numeric_precision_radix, numeric_scale, is_identity::boolean,
identity_generation, identity_increment,
character_maximum_length, character_octet_length, datetime_precision, comment
FROM
character_maximum_length, character_octet_length, datetime_precision, comment
FROM
(
SELECT T.table_type, C.*
FROM
FROM
"DEMO_DB".INFORMATION_SCHEMA.TABLES AS T
JOIN
"DEMO_DB".INFORMATION_SCHEMA.COLUMNS AS C
Expand All @@ -240,12 +238,12 @@ func TestPrepareColumnsSQL(t *testing.T) {
SELECT T.table_type, C.*
FROM
"DEMOADB".INFORMATION_SCHEMA.TABLES AS T
JOIN
JOIN
"DEMOADB".INFORMATION_SCHEMA.COLUMNS AS C
ON
T.table_catalog = C.table_catalog AND T.table_schema = C.table_schema AND t.table_name = C.table_name
)
WHERE TABLE_CATALOG ILIKE 'DEMO_DB' AND TABLE_SCHEMA ILIKE 'PUBLIC' AND TABLE_NAME ILIKE 'ADBC-TABLE' AND COLUMN_NAME ILIKE 'creationDate' AND TABLE_TYPE IN ('BASE TABLE','VIEW')
)
WHERE TABLE_CATALOG ILIKE 'DEMO_DB' AND TABLE_SCHEMA ILIKE 'PUBLIC' AND TABLE_NAME ILIKE 'ADBC-TABLE' AND COLUMN_NAME ILIKE 'creationDate' AND TABLE_TYPE IN ('BASE TABLE','VIEW')
ORDER BY table_catalog, table_schema, table_name, ordinal_position`
actual := prepareColumnsSQL(catalogNames[:], &catalogPattern, &schemaPattern, &tableNamePattern, &columnNamePattern, tableType[:])

Expand Down

0 comments on commit a9f0b11

Please sign in to comment.