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

Merge a pre-computed column / array. #824

Merged
merged 16 commits into from
Jun 6, 2023
61 changes: 49 additions & 12 deletions python/python/lance/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,39 @@ def join(
Not implemented (just override pyarrow dataset to prevent segfault)
"""
raise NotImplementedError("Versioning not yet supported in Rust")

def merge(
self,
data_obj: ReaderLike,
left_on: str,
right_on: Optional[str] = None,
):
"""
Merge another dataset into this one.

Performs a left join, where the dataset is the left side and data_obj
is the right side. Rows existing in the dataset but not on the left will
be filled with null values, unless Lance doesn't support null values for
some types, in which case an error will be raised.

Parameters
----------
data_obj: Reader-like
The data to be merged. Acceptable types are:
- Pandas DataFrame, Pyarrow Table, Dataset, Scanner, or RecordBatchReader
left_on: str
The name of the column in the dataset to join on.
right_on: str or None
The name of the column in data_obj to join on. If None, defaults to
left_on.
"""
if right_on is None:
right_on = left_on

reader = _coerce_reader(data_obj)

self._ds.merge(reader, left_on, right_on)


def versions(self):
"""
Expand Down Expand Up @@ -808,18 +841,7 @@ def write_dataset(
The max number of rows before starting a new group (in the same file)

"""
if isinstance(data_obj, pd.DataFrame):
reader = pa.Table.from_pandas(data_obj, schema=schema).to_reader()
elif isinstance(data_obj, pa.Table):
reader = data_obj.to_reader()
elif isinstance(data_obj, pa.dataset.Dataset):
reader = pa.dataset.Scanner.from_dataset(data_obj).to_reader()
elif isinstance(data_obj, pa.dataset.Scanner):
reader = data_obj.to_reader()
elif isinstance(data_obj, pa.RecordBatchReader):
reader = data_obj
else:
raise TypeError(f"Unknown data_obj type {type(data_obj)}")
reader = _coerce_reader(data_obj)
# TODO add support for passing in LanceDataset and LanceScanner here

params = {
Expand All @@ -831,3 +853,18 @@ def write_dataset(
uri = os.fspath(uri) if isinstance(uri, Path) else uri
_write_dataset(reader, uri, params)
return LanceDataset(uri)


def _coerce_reader(data_obj: ReaderLike, schema: Optional[pa.Schema] = None) -> pa.RecordBatchReader:
if isinstance(data_obj, pd.DataFrame):
return pa.Table.from_pandas(data_obj, schema=schema).to_reader()
elif isinstance(data_obj, pa.Table):
return data_obj.to_reader()
elif isinstance(data_obj, pa.dataset.Dataset):
return pa.dataset.Scanner.from_dataset(data_obj).to_reader()
elif isinstance(data_obj, pa.dataset.Scanner):
return data_obj.to_reader()
elif isinstance(data_obj, pa.RecordBatchReader):
return data_obj
else:
raise TypeError(f"Unknown data_obj type {type(data_obj)}")
30 changes: 30 additions & 0 deletions python/python/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,33 @@ def test_load_scanner_from_fragments(tmp_path: Path):
# Accepts an iterator
scanner = dataset.scanner(fragments=iter(fragments[0:2]), scan_in_order=False)
assert scanner.to_table().num_rows == 2 * 100


def test_merge_data(tmp_path: Path):
tab = pa.table({"a": range(100), "b": range(100)})
lance.write_dataset(tab, tmp_path / "dataset", mode="append")

dataset = lance.dataset(tmp_path / "dataset")

# rejects partial data for non-nullable types
new_tab = pa.table({"a": range(40), "c": range(40)})
# TODO: this should be ValueError
with pytest.raises(OSError, match=".+Lance does not yet support nulls for type Int64."):
dataset.merge(new_tab, "a")

# accepts a full merge
new_tab = pa.table({"a": range(100), "c": range(100)})
dataset.merge(new_tab, "a")
assert dataset.version == 2
assert dataset.to_table() == pa.table({"a": range(100), "b": range(100), "c": range(100)})

# accepts a partial for string
new_tab = pa.table({"a2": range(5), "d": ["a", "b", "c", "d", "e"]})
dataset.merge(new_tab, left_on="a", right_on="a2")
assert dataset.version == 3
assert dataset.to_table() == pa.table({
"a": range(100),
"b": range(100),
"c": range(100),
"d": ["a", "b", "c", "d", "e"] + [None] * 95
})
16 changes: 16 additions & 0 deletions python/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ impl Dataset {
batch.to_pyarrow(self_.py())
}

fn merge(
&mut self,
reader: PyArrowType<ArrowArrayStreamReader>,
left_on: &str,
right_on: &str,
) -> PyResult<()> {
let mut reader: Box<dyn RecordBatchReader> = Box::new(reader.0);
let mut new_self = self.ds.as_ref().clone();
let fut = new_self.merge(&mut reader, left_on, right_on);
self.rt.block_on(
async move { fut.await.map_err(|err| PyIOError::new_err(err.to_string())) },
)?;
self.ds = Arc::new(new_self);
Ok(())
}

fn versions(self_: PyRef<'_, Self>) -> PyResult<Vec<PyObject>> {
let versions = self_
.list_versions()
Expand Down
3 changes: 3 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ arrow-cast = "37.0.0"
arrow-data = "37.0"
arrow-ipc = { version = "37.0", features = ["zstd"] }
arrow-ord = "37.0"
arrow-row = "37.0"
arrow-schema = "37.0"
arrow-select = "37.0"
async-recursion = "1.0"
async-trait = "0.1.60"
byteorder = "1.4.3"
chrono = "0.4.23"
clap = { version = "4.1.1", features = ["derive"], optional = true }
# This is already used by datafusion
dashmap = "5"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Curious what is benefit of this lib over std::HashMap in this case

Copy link
Contributor

Choose a reason for hiding this comment

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

dashmap allows multiple threads to insert concurrently, whereas std::HashMap doesn't allow concurrent inserts. This is just useful for building up the hashmap very fast, given we have the RHS data in memory already for most cases. And as mentioned in the comment, this is already used by DataFusion, so there's no incremental compilation cost from adding it to our dependencies.

Copy link
Contributor Author

@eddyxu eddyxu Jun 6, 2023

Choose a reason for hiding this comment

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

ok, i am fine with that.

Minor question: we need to keep dashmap version sync with datafusion, right? Will cargo pull two different packages if we used the different versions? Not blocker for sure

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we do. They use 5 right now. We should check all of our dependencies for duplicates periodically, including tokio.

object_store = { version = "0.5.6", features = ["aws_profile", "gcp"] }
reqwest = { version = "0.11.16" }
aws-config = "0.54"
Expand Down
35 changes: 21 additions & 14 deletions rust/src/arrow/record_batch.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
// 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
// Copyright 2023 Lance Developers.
//
// http://www.apache.org/licenses/LICENSE-2.0
// 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
//
// 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.
// 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.

//! Additional utility for [`RecordBatch`]
//!
Expand All @@ -23,6 +20,9 @@ use arrow_schema::{ArrowError, SchemaRef};

use crate::Result;

/// RecordBatchBuffer is a in-memory buffer for multiple [`RecordBatch`]s.
///
///
#[derive(Debug)]
pub struct RecordBatchBuffer {
pub batches: Vec<RecordBatch>,
Expand Down Expand Up @@ -69,3 +69,10 @@ impl Iterator for RecordBatchBuffer {
}
}
}

impl FromIterator<RecordBatch> for RecordBatchBuffer {
fn from_iter<T: IntoIterator<Item = RecordBatch>>(iter: T) -> Self {
let batches = iter.into_iter().collect::<Vec<_>>();
Self::new(batches)
}
}
Loading