Skip to content

Commit

Permalink
Fix type hint to transformations.preview (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino authored May 30, 2024
1 parent aeaaa57 commit 0ad6ab1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.44.1] - 2024-05-24
### Added
- Missing parameter `timeout` to `client.transformations.preview`.

## [7.44.0] - 2024-05-24
### Added
- New utility function `datetime_to_ms_iso_timestamp` in `cognite.client.utils` to convert a datetime object to a string representing a timestamp in the format expected by the Cognite GraphQL API.
Expand Down
32 changes: 28 additions & 4 deletions cognite/client/_api/transformations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,20 @@ def preview(
self,
query: str | None = None,
convert_to_string: bool = False,
limit: int = 100,
limit: int | None = 100,
source_limit: int | None = 100,
infer_schema_limit: int | None = 1000,
infer_schema_limit: int | None = 10_000,
timeout: int | None = 240,
) -> TransformationPreviewResult:
"""`Preview the result of a query. <https://developer.cognite.com/api#tag/Query/operation/runPreview>`_
Args:
query (str | None): SQL query to run for preview.
convert_to_string (bool): Stringify values in the query results, default is False.
limit (int): Maximum number of rows to return in the final result, default is 100.
limit (int | None): Maximum number of rows to return in the final result, default is 100.
source_limit (int | None): Maximum number of items to read from the data source or None to run without limit, default is 100.
infer_schema_limit (int | None): Limit for how many rows that are used for inferring result schema, default is 1000.
infer_schema_limit (int | None): Limit for how many rows that are used for inferring result schema, default is 10 000.
timeout (int | None): Number of seconds to wait before cancelling a query. The default, and maximum, is 240.
Returns:
TransformationPreviewResult: Result of the executed query
Expand All @@ -533,13 +535,35 @@ def preview(
>>> client = CogniteClient()
>>>
>>> df = client.transformations.preview(query="select * from _cdf.assets").to_pandas()
Notice that the results are limited both by the `limit` and `source_limit` parameters. If you have
a query that converts one source row to one result row, you may need to increase the `source_limit`.
For example, given that you have a query that reads from a raw table with 10,903 rows
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>>
>>> result = client.transformations.preview(query="select * from my_raw_db.my_raw_table", limit=None)
>>> print(result.results)
100
To get all rows, you also need to set the `source_limit` to None:
>>> from cognite.client import CogniteClient
>>> client = CogniteClient()
>>>
>>> result = client.transformations.preview(query="select * from my_raw_db.my_raw_table", limit=None, source_limit=None)
>>> print(result.results)
10903
"""
request_body = {
"query": query,
"convertToString": convert_to_string,
"limit": limit,
"sourceLimit": source_limit,
"inferSchemaLimit": infer_schema_limit,
"timeout": timeout,
}
response = self._post(url_path=self._RESOURCE_PATH + "/query/run", json=request_body)
result = TransformationPreviewResult._load(response.json(), cognite_client=self._cognite_client)
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.44.0"
__version__ = "7.44.1"
__api_subversion__ = "20230101"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.44.0"
version = "7.44.1"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down

0 comments on commit 0ad6ab1

Please sign in to comment.