Skip to content
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

Fix loading issues with dehydrated load #121

Merged
merged 3 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

### Fixed

- Fix failure of pypgstac load for large items [#121](https://github.com/stac-utils/pgstac/pull/121)

## [v0.6.4]

### Fixed
- Fixed casts for numeric data when a property is not in the queryables table to use the type from the incoming json filter
- Fixed issue loader grouping an unordered iterable by partition, speeding up loads of items with mixed partitions [#116](https://github.com/stac-utils/pgstac/pull/116)
Expand Down
20 changes: 17 additions & 3 deletions pypgstac/pypgstac/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Generator,
TextIO,
)
import csv
import orjson
import psycopg
from orjson import JSONDecodeError
Expand Down Expand Up @@ -512,6 +511,8 @@ def read_dehydrated(self, file: Union[Path, str] = "stdin") -> Generator:
if isinstance(file, str):
open_file: Any = open_std(file, "r")
with open_file as f:
# Note: if 'content' is changed to be anything
# but the last field, the logic below will break.
fields = [
"id",
"geometry",
Expand All @@ -520,8 +521,21 @@ def read_dehydrated(self, file: Union[Path, str] = "stdin") -> Generator:
"end_datetime",
"content",
]
csvreader = csv.DictReader(f, fields, delimiter="\t")
for item in csvreader:

for line in f:
tab_split = line.split("\t")
item = {}
for i, field in enumerate(fields):
if field == "content":
# Join the remaining splits in case
# there were any tabs in the JSON content.
content_value = "\t".join(tab_split[i:])
# Replace quote characters that can be
# written on export and causes failures.
content_value = content_value.replace(r'\\"', r"\"")
item[field] = content_value
else:
item[field] = tab_split[i]
item["partition"] = self._partition_update(item)
yield item

Expand Down
19 changes: 19 additions & 0 deletions pypgstac/tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,22 @@ def test_s1_grd_load_and_query(loader: Loader) -> None:
)[0]
item = res["features"][0]
pystac.Item.from_dict(item).validate()


def test_load_dehydrated(loader: Loader) -> None:
"""Test loader for items dumped directly out of item table."""
collections = [
HERE / "data-files" / "hydration" / "collections" / "chloris-biomass.json",
]

for collection in collections:
loader.load_collections(
str(collection),
insert_mode=Methods.ignore,
)

dehydrated_items = HERE / "data-files" / "load" / "dehydrated.txt"

loader.load_items(
str(dehydrated_items), insert_mode=Methods.insert, dehydrated=True
)