Skip to content
Merged
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
24 changes: 23 additions & 1 deletion chakra_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .exceptions import ChakraAPIError

BASE_URL = "https://api.chakra.dev".rstrip("/")

DEFAULT_BATCH_SIZE = 1000
TOKEN_PREFIX = "DDB_"

Expand Down Expand Up @@ -250,6 +251,20 @@ def _import_data_from_presigned_url(self, table_name: str, s3_key: str) -> None:
)
response.raise_for_status()

def _import_data_from_append_only_dedupe_presigned_url(
self, table_name: str, s3_key: str, primary_key_columns: list[str]
) -> None:
"""Import data from a presigned URL into a table."""
response = self._session.post(
f"{BASE_URL}/api/v1/tables/s3_parquet_import_append_only_dedupe",
json={
"table_name": table_name,
"s3_key": s3_key,
"primary_key_columns": primary_key_columns,
},
)
response.raise_for_status()

def _delete_file_from_s3(self, s3_key: str) -> None:
"""Delete a file from S3."""
response = self._session.delete(
Expand All @@ -270,6 +285,8 @@ def push(
data: pd.DataFrame,
create_if_missing: bool = True,
replace_if_exists: bool = False,
dedupe_on_append: bool = False,
primary_key_columns: list[str] = [],
) -> None:
"""Push data to a table."""
Comment on lines +289 to 291
Copy link

Copilot AI Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a mutable default (an empty list) for primary_key_columns may lead to unexpected behavior if modified. Consider using None as the default and initializing an empty list within the function body.

Suggested change
primary_key_columns: list[str] = [],
) -> None:
"""Push data to a table."""
primary_key_columns: Optional[list[str]] = None,
) -> None:
"""Push data to a table."""
if primary_key_columns is None:
primary_key_columns = []

Copilot uses AI. Check for mistakes.
if not self.token:
Expand Down Expand Up @@ -320,7 +337,12 @@ def push(

# Import the data into the warehouse from the presigned URL
pbar.set_description("Importing data into warehouse...")
self._import_data_from_presigned_url(table_name, s3_key)
if dedupe_on_append:
self._import_data_from_append_only_dedupe_presigned_url(
table_name, s3_key, primary_key_columns
)
else:
self._import_data_from_presigned_url(table_name, s3_key)
pbar.update(1)

# Clean up the data that was previously uploaded
Expand Down