Skip to content

Commit

Permalink
fix: error when dealing with wild card query
Browse files Browse the repository at this point in the history
  • Loading branch information
irainia committed Jul 20, 2023
1 parent 08f4123 commit 0fd02f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 14 additions & 3 deletions task/bq2bq/upstream/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,29 @@ func buildQuery(group *ResourceGroup) string {
suffix := "*"
if strings.HasSuffix(n, suffix) {
prefix, _ := strings.CutSuffix(n, suffix)
prefixQuery := fmt.Sprintf("or STARTS_WITH(%s, %s)", n, prefix)
prefixQuery := fmt.Sprintf("STARTS_WITH(table_name, '%s')", prefix)
prefixQueries = append(prefixQueries, prefixQuery)
} else {
nameQuery := fmt.Sprintf("'%s'", n)
nameQueries = append(nameQueries, nameQuery)
}
}

names := strings.Join(nameQueries, ", ")
prefixes := strings.Join(prefixQueries, " or\n")

var whereClause string
if len(nameQueries) > 0 && len(prefixQueries) > 0 {
whereClause = fmt.Sprintf("WHERE table_name in (%s) or %s", names, prefixes)
} else if len(nameQueries) > 0 {
whereClause = fmt.Sprintf("WHERE table_name in (%s)", names)
} else if len(prefixQueries) > 0 {
whereClause = fmt.Sprintf("WHERE %s", prefixes)
}

return "SELECT table_catalog, table_schema, table_name, table_type, ddl\n" +
fmt.Sprintf("FROM `%s.%s.INFORMATION_SCHEMA.TABLES`\n", group.Project, group.Dataset) +
fmt.Sprintf("WHERE table_name in (%s)\n", strings.Join(nameQueries, ", ")) +
strings.Join(prefixQueries, "\n")
whereClause
}

func convertToSchema(values []bigquery.Value) (*Schema, error) {
Expand Down
17 changes: 14 additions & 3 deletions task/bq2bq/upstream/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,29 @@ func buildQuery(group *upstream.ResourceGroup) string {
suffix := "*"
if strings.HasSuffix(n, suffix) {
prefix, _ := strings.CutSuffix(n, suffix)
prefixQuery := fmt.Sprintf("or STARTS_WITH(%s, %s)", n, prefix)
prefixQuery := fmt.Sprintf("STARTS_WITH(table_name, '%s')", prefix)
prefixQueries = append(prefixQueries, prefixQuery)
} else {
nameQuery := fmt.Sprintf("'%s'", n)
nameQueries = append(nameQueries, nameQuery)
}
}

names := strings.Join(nameQueries, ", ")
prefixes := strings.Join(prefixQueries, " or\n")

var whereClause string
if len(nameQueries) > 0 && len(prefixQueries) > 0 {
whereClause = fmt.Sprintf("WHERE table_name in (%s) or %s", names, prefixes)
} else if len(nameQueries) > 0 {
whereClause = fmt.Sprintf("WHERE table_name in (%s)", names)
} else if len(prefixQueries) > 0 {
whereClause = fmt.Sprintf("WHERE %s", prefixes)
}

return "SELECT table_catalog, table_schema, table_name, table_type, ddl\n" +
fmt.Sprintf("FROM `%s.%s.INFORMATION_SCHEMA.TABLES`\n", group.Project, group.Dataset) +
fmt.Sprintf("WHERE table_name in (%s)\n", strings.Join(nameQueries, ", ")) +
strings.Join(prefixQueries, "\n")
whereClause
}

type ClientMock struct {
Expand Down

0 comments on commit 0fd02f5

Please sign in to comment.