Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ dremio query run "SELECT * FROM myspace.orders LIMIT 5" --output pretty
# Search the catalog for anything matching "revenue"
dremio search "revenue"

# Search for anything matching either term
dremio search "revenue" "sales"

# Search only jobs and limit the first page size
dremio search "revenue" --filter 'category in ["JOB"]' --max-results 20

Expand Down
7 changes: 5 additions & 2 deletions src/drs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ def get_client() -> DremioClient:

@app.command("search")
def search_command(
term: str = typer.Argument(help="Search term (matches table names, view names, source names)"),
terms: list[str] = typer.Argument(
help="One or more search terms (matches table names, view names, source names). "
"Multiple terms are searched as alternatives."
),
filter_: str | None = typer.Option(None, "--filter", help="CEL filter expression to refine search results"),
max_results: int | None = typer.Option(None, "--max-results", min=1, help="Maximum results to return per page"),
next_page_token: str | None = typer.Option(
Expand All @@ -194,7 +197,7 @@ async def _execute():
}
if next_page_token is not None:
search_kwargs["next_page_token"] = next_page_token
return await client.search(term, **search_kwargs)
return await client.search(terms, **search_kwargs)
except httpx.HTTPStatusError as exc:
raise handle_api_error(exc) from exc
finally:
Expand Down
4 changes: 2 additions & 2 deletions src/drs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,12 @@ async def get_catalog_by_path(self, path_parts: list[str]) -> dict:

async def search(
self,
query: str,
queries: list[str],
filter_: str | None = None,
max_results: int | None = None,
next_page_token: str | None = None,
) -> dict:
body: dict[str, Any] = {"query": query}
body: dict[str, Any] = {"queries": queries}
if filter_:
body["filter"] = filter_
if max_results is not None:
Expand Down
8 changes: 7 additions & 1 deletion src/drs/introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@
"mechanism": "REST",
"endpoints": ["POST /v0/projects/{pid}/search"],
"parameters": [
{"name": "term", "type": "string", "required": True, "positional": True, "description": "Search term"},
{
"name": "terms",
"type": "string",
"required": True,
"positional": True,
"description": "One or more search terms, searched as alternatives",
},
{
"name": "filter",
"type": "string",
Expand Down
26 changes: 24 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_search_command_passes_filter_and_max_results(monkeypatch) -> None:
result = runner.invoke(app, ["search", "revenue", "--filter", 'category in ["JOB"]', "--max-results", "20"])

assert result.exit_code == 0
search_mock.assert_awaited_once_with("revenue", filter_='category in ["JOB"]', max_results=20)
search_mock.assert_awaited_once_with(["revenue"], filter_='category in ["JOB"]', max_results=20)
close_mock.assert_awaited_once()


Expand All @@ -247,5 +247,27 @@ def test_search_command_passes_next_page_token(monkeypatch) -> None:
result = runner.invoke(app, ["search", "revenue", "--next-page-token", "token-123"])

assert result.exit_code == 0
search_mock.assert_awaited_once_with("revenue", filter_=None, max_results=None, next_page_token="token-123")
search_mock.assert_awaited_once_with(["revenue"], filter_=None, max_results=None, next_page_token="token-123")
close_mock.assert_awaited_once()


def test_search_command_passes_multiple_terms(monkeypatch) -> None:
search_mock = AsyncMock(return_value={"results": []})
close_mock = AsyncMock()
client = MagicMock()
client.search = search_mock
client.close = close_mock

monkeypatch.setattr("drs.cli.get_client", lambda: client)

result = runner.invoke(app, ["search", "query a", "query b"])

assert result.exit_code == 0
search_mock.assert_awaited_once_with(["query a", "query b"], filter_=None, max_results=None)
close_mock.assert_awaited_once()


def test_search_command_requires_a_term() -> None:
result = runner.invoke(app, ["search"])

assert result.exit_code != 0
35 changes: 31 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ async def _capture(request: httpx.Request) -> httpx.Response:

client._client = httpx.AsyncClient(transport=httpx.MockTransport(_capture))

await client.search("revenue", filter_='category in ["JOB"]', max_results=20)
await client.search(["revenue"], filter_='category in ["JOB"]', max_results=20)

assert captured["body"] == {
"query": "revenue",
"queries": ["revenue"],
"filter": 'category in ["JOB"]',
"maxResults": 20,
}
Expand All @@ -136,13 +136,40 @@ async def _capture(request: httpx.Request) -> httpx.Response:

client._client = httpx.AsyncClient(transport=httpx.MockTransport(_capture))

await client.search("revenue", next_page_token="token-123")
await client.search(["revenue"], next_page_token="token-123")

assert captured["body"] == {
"query": "revenue",
"queries": ["revenue"],
"pageToken": "token-123",
}

@pytest.mark.parametrize(
("queries", "expected_body"),
[
(["revenue"], {"queries": ["revenue"]}),
(["revenue", "sales"], {"queries": ["revenue", "sales"]}),
([" ", "sales"], {"queries": [" ", "sales"]}),
([""], {"queries": [""]}),
],
)
@pytest.mark.asyncio
async def test_search_sends_queries_list_and_legacy_query(
self, client: DremioClient, queries: list[str], expected_body: dict
) -> None:
captured: dict = {}

async def _capture(request: httpx.Request) -> httpx.Response:
import json

captured["body"] = json.loads(request.content)
return httpx.Response(200, json={"results": []})

client._client = httpx.AsyncClient(transport=httpx.MockTransport(_capture))

await client.search(queries)

assert captured["body"] == expected_body


class TestSQLBreadcrumb:
@pytest.mark.asyncio
Expand Down
Loading