-
Notifications
You must be signed in to change notification settings - Fork 18
Feature/Implement Semantic Search for BigQuery resources #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srinivasan-acn
wants to merge
10
commits into
GoogleCloudDataproc:main
Choose a base branch
from
Shubha-accenture:feature/semantic-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d349f1
React mui table use in preview
aditee-accenture 638bcb7
css for heading
aditee-accenture 408c9cb
filter update
aditee-accenture 9591fb3
filter changes
aditee-accenture 7665a2e
Table preview - Bigquery Jobs Integration added
srinivasan-acn bcea304
Merge branch 'preview-ui-change' of https://github.com/Shubha-accentu…
vysakhviswama 70eda6c
Revert "Merge branch 'preview-ui-change' of https://github.com/Shubha…
vysakhviswama 2ec786e
bigquery_semantic_search with pagination and scoping
srinivasan-acn b71c5b5
Gemini comments addressed.
srinivasan-acn 293202c
test case for semantic search feature.
srinivasan-acn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed 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 | ||
| # | ||
| # https://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 pytest | ||
| import aiohttp | ||
| import asyncio | ||
| import time | ||
| from unittest.mock import AsyncMock, patch, Mock | ||
|
|
||
| from dataproc_jupyter_plugin.services.bigquery import Client | ||
| from dataproc_jupyter_plugin.commons.constants import ( | ||
| DATAPLEX_SERVICE_NAME | ||
| ) | ||
|
|
||
| @pytest.fixture | ||
| def mock_credentials(): | ||
| return { | ||
| "access_token": "mock-token", | ||
| "project_id": "test-project", | ||
| "region_id": "us-central1" | ||
| } | ||
|
|
||
| @pytest.fixture | ||
| def mock_log(): | ||
| return Mock() | ||
|
|
||
| @pytest.fixture | ||
| def mock_client_session(): | ||
| return AsyncMock(spec=aiohttp.ClientSession) | ||
|
|
||
| @pytest.fixture | ||
| def mock_dataplex_url(): | ||
| with patch("dataproc_jupyter_plugin.urls.gcp_service_url") as mock_url: | ||
| mock_url.return_value = "https://dataplex.googleapis.com/" | ||
| yield mock_url | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_semantic_search_payload_construction( | ||
| mock_credentials, mock_log, mock_client_session, mock_dataplex_url | ||
| ): | ||
| """Verifies that the Dataplex query and JSON payload are correctly formed.""" | ||
| mock_response = AsyncMock() | ||
| mock_response.status = 200 | ||
| mock_response.json.return_value = { | ||
| "results": [{"dataplexEntry": {"displayName": "sales_table"}}], | ||
| "totalSize": 1 | ||
| } | ||
| mock_client_session.post.return_value.__aenter__.return_value = mock_response | ||
|
|
||
| client = Client(mock_credentials, mock_log, mock_client_session) | ||
|
|
||
| await client.bigquery_semantic_search( | ||
| search_string="revenue", | ||
| search_type="TABLE|VIEW", | ||
| system="BIGQUERY", | ||
| projects=["proj-1"], | ||
| scope=True, | ||
| locations=["us"], | ||
| page_size=20, | ||
| page_token="token-abc" | ||
| ) | ||
|
|
||
| # Validate the generated URL | ||
| expected_url = "https://dataplex.googleapis.com/v1/projects/test-project/locations/global:searchEntries" | ||
|
|
||
| # Validate the complex query string construction | ||
| expected_query = "revenue AND system=BIGQUERY AND (type=TABLE OR type=VIEW) AND (projectid=proj-1) AND (location=us)" | ||
|
|
||
| # Validate the JSON payload including the semanticSearch toggle | ||
| expected_payload = { | ||
| "name": "projects/test-project/locations/global", | ||
| "query": expected_query, | ||
| "pageSize": 20, | ||
| "semanticSearch": True, | ||
| "pageToken": "token-abc", | ||
| "scope": "projects/test-project" | ||
| } | ||
|
|
||
| call_args = mock_client_session.post.call_args | ||
| assert call_args[0][0] == expected_url | ||
| assert call_args[1]["json"] == expected_payload | ||
| assert call_args[1]["headers"]["X-Goog-User-Project"] == "test-project" | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_semantic_search_empty_input_handling( | ||
| mock_credentials, mock_log, mock_client_session | ||
| ): | ||
| """Ensures the client returns early with a warning if no search parameters exist.""" | ||
| client = Client(mock_credentials, mock_log, mock_client_session) | ||
|
|
||
| result = await client.bigquery_semantic_search( | ||
| "", "", "", [], False, [] | ||
| ) | ||
|
|
||
| assert result == {} | ||
| mock_log.warning.assert_called_once_with("No search query provided. Returning empty result.") | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_semantic_search_api_failure( | ||
| mock_credentials, mock_log, mock_client_session, mock_dataplex_url | ||
| ): | ||
| """Verifies error handling when the Dataplex API returns a non-200 status.""" | ||
| mock_response = AsyncMock() | ||
| mock_response.status = 403 | ||
| mock_response.reason = "Forbidden" | ||
| mock_response.text.return_value = "Access Denied" | ||
| mock_client_session.post.return_value.__aenter__.return_value = mock_response | ||
|
|
||
| client = Client(mock_credentials, mock_log, mock_client_session) | ||
|
|
||
| result = await client.bigquery_semantic_search("test", "TABLE", "BQ", [], False, []) | ||
|
|
||
| assert "error" in result | ||
| assert "Dataplex API Error: 403" in result["error"] | ||
| mock_log.exception.assert_called_once() | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_tornado_responsiveness_during_semantic_search( | ||
| mock_credentials, mock_log, mock_client_session, mock_dataplex_url | ||
| ): | ||
| """ | ||
| Verifies that the Tornado event loop remains responsive (non-blocking) | ||
| while the semantic search API call is in flight. | ||
| """ | ||
| # Create a mock response object first | ||
| mock_json_body = AsyncMock() | ||
| mock_json_body.return_value = {} | ||
|
|
||
| mock_response = AsyncMock() | ||
| mock_response.status = 200 | ||
| mock_response.json = mock_json_body | ||
| # This is the key: Mock the context manager __aenter__ to return our response | ||
| mock_response.__aenter__ = AsyncMock(return_value=mock_response) | ||
|
|
||
| async def slow_response(*args, **kwargs): | ||
| await asyncio.sleep(0.5) | ||
| return mock_response | ||
|
|
||
| # Use side_effect to trigger the delay | ||
| mock_client_session.post.side_effect = slow_response | ||
| client = Client(mock_credentials, mock_log, mock_client_session) | ||
|
|
||
| start_canary = time.perf_counter() | ||
|
|
||
| # Start the search task | ||
| search_task = asyncio.create_task( | ||
| client.bigquery_semantic_search( | ||
| search_string="revenue", | ||
| search_type="TABLE", | ||
| system="BQ", | ||
| projects=["*"], | ||
| scope=False, | ||
| locations=[] | ||
| ) | ||
| ) | ||
|
|
||
| # Canary sleep to check for blocking | ||
| await asyncio.sleep(0.05) | ||
|
|
||
| end_canary = time.perf_counter() | ||
| latency = end_canary - start_canary | ||
|
|
||
| # Await the actual task to clean up the coroutine | ||
| await search_task | ||
|
|
||
| assert latency < 0.1, f"Tornado loop was blocked! Latency was {latency:.4f}s" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.