-
Notifications
You must be signed in to change notification settings - Fork 109
expose write options #1006
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
Draft
matko
wants to merge
1
commit into
apache:main
Choose a base branch
from
vectorlink-ai:feat/expose-write-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−22
Draft
expose write options #1006
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,47 @@ | ||
# 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. | ||
"""Option conversion functions.""" | ||
|
||
from typing import Optional, Dict | ||
from datafusion.expr import sort_list_to_raw_sort_list | ||
|
||
|
||
def write_options_to_raw_write_options(write_options: Optional[Dict]) -> Dict: | ||
"""Convert a dictionary of write options into the format expected by the pyo3 bindings. | ||
Validates that no superfluous keys are specified, then: | ||
- adds default keys for each expected write option. | ||
- converts "sort_by" into a raw sort list. | ||
""" | ||
defaults = { | ||
"insert_operation": None, | ||
"single_file_output": None, | ||
"partition_by": None, | ||
"sort_by": None, | ||
} | ||
|
||
if write_options is not None: | ||
invalid_write_options = set(write_options) - set(defaults) | ||
if invalid_write_options: | ||
raise ValueError(f"Invalid write options: {invalid_write_options}") | ||
|
||
results = {**defaults, **write_options} | ||
if "sort_by" in write_options: | ||
results["sort_by"] = sort_list_to_raw_sort_list(write_options["sort_by"]) | ||
|
||
return results | ||
else: | ||
return defaults |
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
// 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. | ||
|
||
use datafusion::{dataframe::DataFrameWriteOptions, logical_expr::dml::InsertOp}; | ||
use pyo3::{exceptions::PyValueError, FromPyObject, PyErr, PyResult}; | ||
|
||
use crate::expr::sort_expr::{to_sort_expressions, PySortExpr}; | ||
|
||
#[derive(FromPyObject)] | ||
#[pyo3(from_item_all)] | ||
pub struct PyDataFrameWriteOptions { | ||
insert_operation: Option<String>, | ||
single_file_output: Option<bool>, | ||
partition_by: Option<Vec<String>>, | ||
sort_by: Option<Vec<PySortExpr>>, | ||
} | ||
|
||
impl TryInto<DataFrameWriteOptions> for PyDataFrameWriteOptions { | ||
type Error = PyErr; | ||
|
||
fn try_into(self) -> PyResult<DataFrameWriteOptions> { | ||
let mut options = DataFrameWriteOptions::new(); | ||
if let Some(insert_op) = self.insert_operation { | ||
let op = match insert_op.as_str() { | ||
"append" => InsertOp::Append, | ||
"overwrite" => InsertOp::Overwrite, | ||
"replace" => InsertOp::Replace, | ||
_ => { | ||
return Err(PyValueError::new_err(format!( | ||
"Unrecognized insert op {insert_op}" | ||
))) | ||
} | ||
}; | ||
options = options.with_insert_operation(op); | ||
} | ||
if let Some(single_file_output) = self.single_file_output { | ||
options = options.with_single_file_output(single_file_output); | ||
} | ||
|
||
if let Some(partition_by) = self.partition_by { | ||
options = options.with_partition_by(partition_by); | ||
} | ||
|
||
if let Some(sort_by) = self.sort_by { | ||
options = options.with_sort_by(to_sort_expressions(sort_by)); | ||
} | ||
|
||
Ok(options) | ||
} | ||
} | ||
|
||
pub fn make_dataframe_write_options( | ||
write_options: Option<PyDataFrameWriteOptions>, | ||
) -> PyResult<DataFrameWriteOptions> { | ||
if let Some(wo) = write_options { | ||
wo.try_into() | ||
} else { | ||
Ok(DataFrameWriteOptions::new()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you wanted, you could use an
Enum
here instead of aString
, and then defer more of the validation to pyo3 instead of manually checking the string values below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your comment. I did initially try to do this with an enum. Unfortunately, to use an enum in an object that derives
FromPyObject
, the enum itself has to also deriveFromPyObject
, but tagging a simple enum (no variants, just tags) with#[derive(FromPyObject)]
results inerror: cannot derive FromPyObject for empty structs and variants
.So this errors:
This might be where my pyo3 knowledge is lacking. Can you point me in the right direction on how to do this as an enum properly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I usually separate out the enum but then implement the
FromPyObject
directly on that enum. It's simpler with better separation of concerns, I think. And if you ever need to usePyInsertOperation
from multiple functions, then you can reuse the same implementation