Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Make db a parameter of serialize() #208

Merged
merged 12 commits into from
Aug 4, 2023
Merged

Make db a parameter of serialize() #208

merged 12 commits into from
Aug 4, 2023

Conversation

xuebinsu
Copy link
Contributor

@xuebinsu xuebinsu commented Jul 28, 2023

Previously, db is an attribute of class Expr and its value
cannot be decided when init a new object. As a result, when the
Expr is serialized, we need to explicitly bind it to a db to
create functions or types. However, in some cases we forgot to
do that, which lead to errros.

To avoid forgotting this, this patch moves db to a parameter of
serialize() and pass db to it each time we call it.

This patch fixes the following bugs in serialize():

  1. Database is not found when trying to create a function or data
    type.
  2. Star notation * is not serialized correctly in column fileds
    when expanding a composite type result.
  3. count(*) returns wrong result when dataframe contains None.

@xuebinsu xuebinsu marked this pull request as ready for review August 1, 2023 07:55
@@ -546,20 +546,20 @@ def in_(self, container: Union["Expr", List[Any]]) -> "InExpr":
from psycopg2.extensions import adapt # type: ignore


def _serialize(value: Any) -> str:
def _serialize_to_expr(obj: Any, db: Optional[Database] = None) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

why not name it to serialize_to_expr? since others will import it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just want to prevent it from being imported by the user.

Comment on lines 60 to 85
# def _bind(
# self,
# dataframe: Optional["DataFrame"] = None,
# db: Optional[Database] = None,
# column_name: str = None,
# ) -> "Expr":
# # noqa D102
# self._db = db
# if isinstance(self._obj, Expr):
# self._obj = self._obj._bind(
# dataframe=dataframe,
# db=db,
# )
# return self
#
# def apply(
# self, expand: bool = False, column_name: Optional[str] = None, row_id: Optional[str] = None
# ) -> "DataFrame":
# # noqa D102
# from greenplumpython.dataframe import DataFrame

# if expand and column_name is None:
# column_name = "func_" + uuid4().hex
# return DataFrame(
# f"""
# SELECT {(row_id + ',') if row_id is not None else ''}
Copy link
Contributor

Choose a reason for hiding this comment

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

can this be dropped?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Removed.

Copy link
Contributor

@ruxuez ruxuez left a comment

Choose a reason for hiding this comment

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

LGTM. Perhaps we should add an extra test cases to cover operators, select conditions in brackets and with where() method to add a bit more complexity to the test.

@xuebinsu xuebinsu merged commit 5086dac into main Aug 4, 2023
6 checks passed
@xuebinsu xuebinsu deleted the fix_serialize branch August 4, 2023 10:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants