-
Notifications
You must be signed in to change notification settings - Fork 72
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
Add implementation of SQL Except operation #135
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,69 @@ | ||||||
import dask.dataframe as dd | ||||||
|
||||||
from dask_sql.physical.rex import RexConverter | ||||||
from dask_sql.physical.rel.base import BaseRelPlugin | ||||||
from dask_sql.datacontainer import DataContainer, ColumnContainer | ||||||
|
||||||
|
||||||
class LogicalMinusPlugin(BaseRelPlugin): | ||||||
""" | ||||||
LogicalUnion is used on EXCEPT clauses. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you still would like to update that docstring :-) |
||||||
It just concatonates the two data frames. | ||||||
""" | ||||||
|
||||||
class_name = "org.apache.calcite.rel.logical.LogicalMinus" | ||||||
|
||||||
def convert( | ||||||
self, rel: "org.apache.calcite.rel.RelNode", context: "dask_sql.Context" | ||||||
) -> DataContainer: | ||||||
first_dc, second_dc = self.assert_inputs(rel, 2, context) | ||||||
|
||||||
first_df = first_dc.df | ||||||
first_cc = first_dc.column_container | ||||||
|
||||||
second_df = second_dc.df | ||||||
second_cc = second_dc.column_container | ||||||
|
||||||
# For concatenating, they should have exactly the same fields | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
output_field_names = [str(x) for x in rel.getRowType().getFieldNames()] | ||||||
assert len(first_cc.columns) == len(output_field_names) | ||||||
first_cc = first_cc.rename( | ||||||
columns={ | ||||||
col: output_col | ||||||
for col, output_col in zip(first_cc.columns, output_field_names) | ||||||
} | ||||||
) | ||||||
first_dc = DataContainer(first_df, first_cc) | ||||||
|
||||||
assert len(second_cc.columns) == len(output_field_names) | ||||||
second_cc = second_cc.rename( | ||||||
columns={ | ||||||
col: output_col | ||||||
for col, output_col in zip(second_cc.columns, output_field_names) | ||||||
} | ||||||
) | ||||||
second_dc = DataContainer(second_df, second_cc) | ||||||
|
||||||
# To concat the to dataframes, we need to make sure the | ||||||
# columns actually have the specified names in the | ||||||
# column containers | ||||||
# Otherwise the concat won't work | ||||||
first_df = first_dc.assign() | ||||||
second_df = second_dc.assign() | ||||||
|
||||||
self.check_columns_from_row_type(first_df, rel.getExpectedInputRowType(0)) | ||||||
self.check_columns_from_row_type(second_df, rel.getExpectedInputRowType(1)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is now a lot of code duplication in this and the |
||||||
|
||||||
df = first_df.merge( | ||||||
second_df, | ||||||
how="left", | ||||||
indicator=True, | ||||||
) | ||||||
|
||||||
df = df[df.iloc[:, -1] == "left_only"].iloc[:, :-1] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is super cool! |
||||||
|
||||||
cc = ColumnContainer(df.columns) | ||||||
cc = self.fix_column_to_row_type(cc, rel.getRowType()) | ||||||
dc = DataContainer(df, cc) | ||||||
dc = self.fix_dtype_to_row_type(dc, rel.getRowType()) | ||||||
return dc |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def test_except_empty(c, df): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you do not need the |
||
result_df = c.sql( | ||
""" | ||
SELECT * FROM df | ||
EXCEPT | ||
SELECT * FROM df | ||
""" | ||
) | ||
result_df = result_df.compute() | ||
assert len(result_df) == 0 | ||
|
||
|
||
def test_except_non_empty(c, df): | ||
result_df = c.sql( | ||
""" | ||
( | ||
SELECT 1 as "a" | ||
UNION | ||
SELECT 2 as "a" | ||
UNION | ||
SELECT 3 as "a" | ||
) | ||
EXCEPT | ||
SELECT 2 as "a" | ||
""" | ||
) | ||
result_df = result_df.compute() | ||
assert result_df.columns == "a" | ||
assert set(result_df["a"]) == set([1, 3]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding a test with NaNs? You can also use one of the prepared tables (e.g. |
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.
Ok, makes sense to do that. Good idea.