Skip to content

Commit 01ce7c3

Browse files
committed
feat(source/bigquery): add semantic search integration tests
1 parent 9c3a748 commit 01ce7c3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

internal/tools/bigquery/bigquerysql/bigquerysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (t Tool) Invoke(ctx context.Context, resourceMgr tools.SourceProvider, para
219219
}
220220

221221
func (t Tool) EmbedParams(ctx context.Context, paramValues parameters.ParamValues, embeddingModelsMap map[string]embeddingmodels.EmbeddingModel) (parameters.ParamValues, error) {
222-
return parameters.EmbedParams(ctx, t.AllParams, paramValues, embeddingModelsMap, nil)
222+
return parameters.EmbedParams(ctx, t.AllParams, paramValues, embeddingModelsMap, embeddingmodels.FormatVectorForPgvector)
223223
}
224224

225225
func (t Tool) Manifest() tools.Manifest {

tests/bigquery/bigquery_integration_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func TestBigQueryToolEndpoints(t *testing.T) {
156156
tmplSelectCombined, tmplSelectFilterCombined := getBigQueryTmplToolStatement()
157157
toolsFile = tests.AddTemplateParamConfig(t, toolsFile, BigqueryToolType, tmplSelectCombined, tmplSelectFilterCombined, "")
158158

159+
// Set up table for semantic search
160+
vectorTableName, teardownVectorTable := setupBigQueryVectorTable(t, ctx, client, datasetName)
161+
defer teardownVectorTable(t)
162+
163+
// Add semantic search tool config
164+
insertStmt, searchStmt := getBigQueryVectorSearchStmts(vectorTableName)
165+
toolsFile = tests.AddSemanticSearchConfig(t, toolsFile, BigqueryToolType, insertStmt, searchStmt)
166+
159167
cmd, cleanup, err := tests.StartCmd(ctx, toolsFile, args...)
160168
if err != nil {
161169
t.Fatalf("command initialization returned an error: %s", err)
@@ -205,6 +213,7 @@ func TestBigQueryToolEndpoints(t *testing.T) {
205213
runBigQueryGetTableInfoToolInvokeTest(t, datasetName, tableName, tableInfoWant)
206214
runBigQueryConversationalAnalyticsInvokeTest(t, datasetName, tableName, dataInsightsWant)
207215
runBigQuerySearchCatalogToolInvokeTest(t, datasetName, tableName)
216+
tests.RunSemanticSearchToolInvokeTest(t, ddlWant, "", "The quick brown fox")
208217
}
209218

210219
func TestBigQueryToolWithDatasetRestriction(t *testing.T) {
@@ -3259,3 +3268,24 @@ func runAnalyzeContributionWithRestriction(t *testing.T, allowedTableFullName, d
32593268
})
32603269
}
32613270
}
3271+
3272+
// setupBigQueryVectorTable creates a vector table in BigQuery for semantic search testing
3273+
func setupBigQueryVectorTable(t *testing.T, ctx context.Context, client *bigqueryapi.Client, datasetName string) (string, func(*testing.T)) {
3274+
tableName := fmt.Sprintf("vector_table_%s", strings.ReplaceAll(uuid.New().String(), "-", ""))
3275+
fullTableName := fmt.Sprintf("`%s.%s.%s`", BigqueryProject, datasetName, tableName)
3276+
createStatement := fmt.Sprintf(`CREATE TABLE %s (
3277+
id INT64,
3278+
content STRING,
3279+
embedding ARRAY<FLOAT64>
3280+
)`, fullTableName)
3281+
3282+
teardownTable := setupBigQueryTable(t, ctx, client, createStatement, "", datasetName, fullTableName, nil)
3283+
return fullTableName, teardownTable
3284+
}
3285+
3286+
// getBigQueryVectorSearchStmts returns statements for bigquery semantic search
3287+
func getBigQueryVectorSearchStmts(vectorTableName string) (string, string) {
3288+
insertStmt := fmt.Sprintf("INSERT INTO %s (id, content, embedding) VALUES (1, @content, (SELECT ARRAY_AGG(CAST(x AS FLOAT64)) FROM UNNEST(JSON_VALUE_ARRAY(PARSE_JSON(@text_to_embed))) AS x))", vectorTableName)
3289+
searchStmt := fmt.Sprintf("SELECT id, content, ML.DISTANCE(embedding, (SELECT ARRAY_AGG(CAST(x AS FLOAT64)) FROM UNNEST(JSON_VALUE_ARRAY(PARSE_JSON(@query))) AS x), 'COSINE') AS distance FROM %s ORDER BY distance LIMIT 1", vectorTableName)
3290+
return insertStmt, searchStmt
3291+
}

0 commit comments

Comments
 (0)