-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add writing record fields in Append op.
- Loading branch information
coufon
committed
Dec 22, 2023
1 parent
67e879e
commit 80ceb28
Showing
18 changed files
with
529 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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 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 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,68 @@ | ||
# Copyright 2023 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. | ||
# | ||
"""Record manifest files writer and reader implementation.""" | ||
|
||
from typing import List, Optional | ||
|
||
import pyarrow as pa | ||
import pyarrow.parquet as pq | ||
|
||
from space.core.manifests.utils import write_index_manifest | ||
import space.core.proto.metadata_pb2 as meta | ||
from space.core.utils import paths | ||
from space.core.schema import constants | ||
|
||
|
||
def _manifest_schema() -> pa.Schema: | ||
fields = [(constants.FILE_PATH_FIELD, pa.utf8()), | ||
(constants.FIELD_ID_FIELD, pa.int32()), | ||
(constants.NUM_ROWS_FIELD, pa.int64()), | ||
(constants.UNCOMPRESSED_BYTES_FIELD, pa.int64())] | ||
return pa.schema(fields) | ||
|
||
|
||
class RecordManifestWriter: | ||
'''Writer of record manifest files.''' | ||
|
||
def __init__(self, metadata_dir: str): | ||
self._metadata_dir = metadata_dir | ||
self._manifest_schema = _manifest_schema() | ||
|
||
self._file_paths: List[str] = [] | ||
self._field_ids: List[int] = [] | ||
self._num_rows: List[int] = [] | ||
self._uncompressed_bytes: List[int] = [] | ||
|
||
def write(self, file_path: str, field_id: int, | ||
storage_statistics: meta.StorageStatistics) -> None: | ||
self._file_paths.append(file_path) | ||
self._field_ids.append(field_id) | ||
self._num_rows.append(storage_statistics.num_rows) | ||
self._uncompressed_bytes.append( | ||
storage_statistics.record_uncompressed_bytes) | ||
|
||
def finish(self) -> Optional[str]: | ||
if not self._file_paths: | ||
return None | ||
|
||
arrays = [ | ||
self._file_paths, self._field_ids, self._num_rows, | ||
self._uncompressed_bytes | ||
] | ||
manifest_table = pa.Table.from_arrays(arrays, schema=self._manifest_schema) | ||
|
||
file_path = paths.new_record_manifest_path(self._metadata_dir) | ||
write_index_manifest(file_path, self._manifest_schema, manifest_data) | ||
return file_path |
This file contains 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,27 @@ | ||
# Copyright 2023 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. | ||
# | ||
"""Manifest utilities.""" | ||
|
||
import pyarrow.parquet as pq | ||
|
||
|
||
def write_index_manifest(file_path: str, schema: pa.Schema, | ||
data: pa.Table) -> str: | ||
# TODO: currently assume this file is small, so always write a single file. | ||
writer = pq.ParquetWriter(file_path, schema) | ||
writer.write_table(data) | ||
|
||
writer.close() | ||
return file_path |
This file contains 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 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
Oops, something went wrong.