From a9f0b11f1c82d7919fc4de6407ded20474f836b8 Mon Sep 17 00:00:00 2001 From: Ryan Syed Date: Thu, 14 Dec 2023 18:03:44 -0800 Subject: [PATCH] refactor: Refactored all the filter conditions for GetObjects into a single prepareFilterConditions function --- go/adbc/driver/snowflake/connection.go | 33 +++++---- go/adbc/driver/snowflake/connection_test.go | 80 ++++++++++----------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/go/adbc/driver/snowflake/connection.go b/go/adbc/driver/snowflake/connection.go index 2f9b4f0b6e..3a3bd369d0 100644 --- a/go/adbc/driver/snowflake/connection.go +++ b/go/adbc/driver/snowflake/connection.go @@ -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 != "" { @@ -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 @@ -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 } @@ -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 != "" { @@ -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+`'`) diff --git a/go/adbc/driver/snowflake/connection_test.go b/go/adbc/driver/snowflake/connection_test.go index 53c86d982d..f6a0ea99da 100644 --- a/go/adbc/driver/snowflake/connection_test.go +++ b/go/adbc/driver/snowflake/connection_test.go @@ -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 )` @@ -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) @@ -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'` @@ -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'` @@ -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) @@ -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[:]) @@ -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'` @@ -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')` @@ -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 @@ -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[:]) @@ -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 @@ -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[:])