damccorm commented on code in PR #41690:
URL: https://github.com/apache/beam/pull/41690#discussion_r4756377970
##########
sdks/python/apache_beam/ml/rag/enrichment/bigquery_vector_search.py:
##########
@@ -185,11 +186,24 @@ def _format_restrict(self, item: EmbeddableItem) -> str:
def format_query(self, items: list[EmbeddableItem]) -> str:
"""Format the vector search query template."""
base_columns_str = ", ".join(f"base.{col}" for col in self.columns)
- columns_str = ", ".join(self.columns)
+ select_columns = list(self.columns)
+ normalized_embedding_col = (
+ self.embedding_column.strip().strip("`").lower())
+ if not any(
+ col.strip().strip("`").lower() == normalized_embedding_col
+ for col in select_columns):
Review Comment:
`yapf==0.43.0` expects this without a line break after `any(`:
```suggestion
if not any(col.strip().strip("`").lower() == normalized_embedding_col
for col in select_columns):
```
##########
sdks/python/apache_beam/ml/rag/enrichment/bigquery_vector_search.py:
##########
@@ -185,11 +186,24 @@ def _format_restrict(self, item: EmbeddableItem) -> str:
def format_query(self, items: list[EmbeddableItem]) -> str:
"""Format the vector search query template."""
base_columns_str = ", ".join(f"base.{col}" for col in self.columns)
- columns_str = ", ".join(self.columns)
+ select_columns = list(self.columns)
+ normalized_embedding_col = (
+ self.embedding_column.strip().strip("`").lower())
+ if not any(
+ col.strip().strip("`").lower() == normalized_embedding_col
+ for col in select_columns):
+ select_columns.append(self.embedding_column)
+ select_columns_str = ", ".join(select_columns)
distance_clause = (
f", distance_type => '{self.distance_type}'"
if self.distance_type else "")
- options_clause = (f", options => {self.options}" if self.options else "")
+ if self.options:
+ escaped_options = (
+ json.dumps(self.options, allow_nan=False).replace(
+ "\\", "\\\\").replace("'", "\\'"))
Review Comment:
`yapf==0.43.0` wraps before `allow_nan=False` here:
```suggestion
json.dumps(self.options,
allow_nan=False).replace("\\", "\\\\").replace("'",
"\\'"))
```
##########
sdks/python/apache_beam/ml/rag/enrichment/bigquery_vector_search_test.py:
##########
@@ -0,0 +1,159 @@
+#
+# 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.
+#
+
+import json
+import re
+import unittest
+
+from apache_beam.ml.rag.types import Content
+from apache_beam.ml.rag.types import EmbeddableItem
+from apache_beam.ml.rag.types import Embedding
+
+# pylint: disable=ungrouped-imports
+try:
+ from apache_beam.ml.rag.enrichment.bigquery_vector_search import
BigQueryVectorSearchParameters
+except ImportError:
+ raise unittest.SkipTest('BigQuery dependencies not installed')
+
+
+class BigQueryVectorSearchParametersTest(unittest.TestCase):
+ def setUp(self):
+ self.item = EmbeddableItem(
+ id='1',
+ content=Content(text='hello'),
+ embedding=Embedding(dense_embedding=[0.1, 0.2]),
+ metadata={'language': 'en'})
+
+ def test_format_query_serializes_options_as_json_string(self):
+ options = {'use_brute_force': True, 'fraction_lists_to_search': 0.01}
+ params = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content'],
+ neighbor_count=5,
+ options=options)
+
+ query = params.format_query([self.item])
+ expected_json = json.dumps(options)
+ self.assertIn(f", options => '{expected_json}'", query)
+
+ match = re.search(r"options => '([^']+)'", query)
+ assert match is not None
+ self.assertEqual(json.loads(match.group(1)), options)
+
+ def test_format_query_escapes_special_chars_in_options(self):
+ options = {'note': "it's a \\test"}
+ params = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content'],
+ neighbor_count=5,
+ options=options)
+
+ query = params.format_query([self.item])
+ self.assertIn(", options => '{\"note\": \"it\\'s a \\\\\\\\test\"}'",
query)
+
+ def test_format_query_omits_options_when_none_or_empty(self):
+ params_none = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content'],
+ neighbor_count=5,
+ options=None)
+ self.assertNotIn('options =>', params_none.format_query([self.item]))
+
+ params_empty = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content'],
+ neighbor_count=5,
+ options={})
+ self.assertNotIn('options =>', params_empty.format_query([self.item]))
+
+ def test_format_query_deduplicates_embedding_column_in_subquery(self):
+ params = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content', 'embedding'],
+ neighbor_count=5)
+
+ query = params.format_query([self.item])
+ self.assertIn('(SELECT content, embedding\n', query)
+ self.assertNotIn('embedding, embedding', query)
+ self.assertIn('base.content, base.embedding', query)
+
+ def test_format_query_appends_embedding_column_when_not_in_columns(self):
+ params = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content', 'metadata'],
+ neighbor_count=5)
+
+ query = params.format_query([self.item])
+ self.assertIn('(SELECT content, metadata, embedding\n', query)
+ self.assertIn('base.content, base.metadata', query)
+
+ def test_format_query_with_distance_type_and_include_distance(self):
+ params = BigQueryVectorSearchParameters(
+ project='my-proj',
+ table_name='my-proj.ds.tbl',
+ embedding_column='embedding',
+ columns=['content'],
+ neighbor_count=3,
+ distance_type='COSINE',
+ include_distance=True)
+
+ query = params.format_query([self.item])
+ self.assertIn("distance_type => 'COSINE'", query)
+ self.assertRegex(query, r'STRUCT\(distance,\s+base\.content\)')
+
+
+ def test_format_query_deduplicates_case_and_backtick_embedding_column(self):
Review Comment:
Extra blank line between methods (`yapf==0.43.0` will flag this):
```suggestion
def
test_format_query_deduplicates_case_and_backtick_embedding_column(self):
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]