-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PyArrow: Avoid buffer-overflow by avoid doing a sort
This was already being discussed back here: #208 (comment) This PR changes from doing a sort, and then a single pass over the table to the the approach where we determine the unique partition tuples then filter on them one by one. Fixes #1491 Because the sort caused buffers to be joined where it would overflow in Arrow. I think this is an issue on the Arrow side, and it should automatically break up into smaller buffers. The `combine_chunks` method does this correctly. Now: ``` 0.42877754200890195 Run 1 took: 0.2507691659993725 Run 2 took: 0.24833179199777078 Run 3 took: 0.24401691700040828 Run 4 took: 0.2419595829996979 Average runtime of 0.28 seconds ``` Before: ``` Run 0 took: 1.0768639159941813 Run 1 took: 0.8784021250030492 Run 2 took: 0.8486490420036716 Run 3 took: 0.8614017910003895 Run 4 took: 0.8497851670108503 Average runtime of 0.9 seconds ``` So it comes with a nice speedup as well :)
- Loading branch information
Showing
4 changed files
with
144 additions
and
68 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,71 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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 | ||
# | ||
# http://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. | ||
import statistics | ||
import timeit | ||
import urllib | ||
|
||
import pyarrow as pa | ||
import pyarrow.parquet as pq | ||
import pytest | ||
|
||
from pyiceberg.transforms import DayTransform | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def taxi_dataset(tmp_path_factory: pytest.TempPathFactory) -> pa.Table: | ||
"""Reads the Taxi dataset to disk""" | ||
taxi_dataset = "https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2022-01.parquet" | ||
taxi_dataset_dest = tmp_path_factory.mktemp("taxi_dataset") / "yellow_tripdata_2022-01.parquet" | ||
urllib.request.urlretrieve(taxi_dataset, taxi_dataset_dest) | ||
|
||
return pq.read_table(taxi_dataset_dest) | ||
|
||
|
||
def test_partitioned_write(tmp_path_factory: pytest.TempPathFactory, taxi_dataset: pa.Table) -> None: | ||
"""Tests writing to a partitioned table with something that would be close a production-like situation""" | ||
from pyiceberg.catalog.sql import SqlCatalog | ||
|
||
warehouse_path = str(tmp_path_factory.mktemp("warehouse")) | ||
catalog = SqlCatalog( | ||
"default", | ||
uri=f"sqlite:///{warehouse_path}/pyiceberg_catalog.db", | ||
warehouse=f"file://{warehouse_path}", | ||
) | ||
|
||
catalog.create_namespace("default") | ||
|
||
tbl = catalog.create_table("default.taxi_partitioned", schema=taxi_dataset.schema) | ||
|
||
with tbl.update_spec() as spec: | ||
spec.add_field("tpep_pickup_datetime", DayTransform()) | ||
|
||
# Profiling can sometimes be handy as well | ||
# with cProfile.Profile() as pr: | ||
# tbl.append(taxi_dataset) | ||
# | ||
# pr.print_stats(sort=True) | ||
|
||
runs = [] | ||
for run in range(5): | ||
start_time = timeit.default_timer() | ||
tbl.append(taxi_dataset) | ||
elapsed = timeit.default_timer() - start_time | ||
|
||
print(f"Run {run} took: {elapsed}") | ||
runs.append(elapsed) | ||
|
||
print(f"Average runtime of {round(statistics.mean(runs), 2)} seconds") |
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