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

fix(ingest/datahub): Use server side cursor instead of local one #12129

Merged
merged 4 commits into from
Dec 16, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,53 @@ def query(self) -> str:
version
"""

def _get_rows(
self, from_createdon: datetime, stop_time: datetime
) -> Iterable[Dict[str, Any]]:
# Cursor for MySql and PostgreSQL are by default client side cursor and we need to create server side cursor -> https://docs.sqlalchemy.org/en/20/core/connections.html#using-server-side-cursors-a-k-a-stream-results
# For MySQL, we need to use SSCursor to create server side cursor
# For PostgreSQL, we need to use stream_results=True to create server side cursor and need to be run in transaction
with self.engine.connect() as conn:
if self.engine.dialect.name == "postgresql":
with conn.begin(): # Transaction required for PostgreSQL server-side cursor
conn = conn.execution_options(
stream_results=True,
yield_per=self.config.database_query_batch_size,
)
result = conn.execute(
self.query,
{
"exclude_aspects": list(self.config.exclude_aspects),
"since_createdon": from_createdon.strftime(DATETIME_FORMAT),
},
)
for row in result:
yield dict(row)
elif self.engine.dialect.name == "mysql": # MySQL
import MySQLdb

with contextlib.closing(
conn.connection.cursor(MySQLdb.cursors.SSCursor)
) as cursor:
logger.debug(f"Using Cursor type: {cursor.__class__.__name__}")
cursor.execute(
self.query,
{
"exclude_aspects": list(self.config.exclude_aspects),
"since_createdon": from_createdon.strftime(DATETIME_FORMAT),
Copy link
Collaborator

Choose a reason for hiding this comment

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

ideally we'd actually have a execute_server_cursor(query, params) -> Iterable[dict = row] method that handles the dialect-specific cursor logic, and then the get_rows method can stay simple and only have the query + parameter business logic

},
)

columns = [desc[0] for desc in cursor.description]
while True:
rows = cursor.fetchmany(self.config.database_query_batch_size)
if not rows:
break # Use break instead of return in generator
for row in rows:
yield dict(zip(columns, row))
else:
raise ValueError(f"Unsupported dialect: {self.engine.dialect.name}")

def get_aspects(
self, from_createdon: datetime, stop_time: datetime
) -> Iterable[Tuple[MetadataChangeProposalWrapper, datetime]]:
Expand All @@ -159,27 +206,6 @@ def get_aspects(
if mcp:
yield mcp, row["createdon"]

def _get_rows(
self, from_createdon: datetime, stop_time: datetime
) -> Iterable[Dict[str, Any]]:
with self.engine.connect() as conn:
with contextlib.closing(conn.connection.cursor()) as cursor:
cursor.execute(
self.query,
{
"exclude_aspects": list(self.config.exclude_aspects),
"since_createdon": from_createdon.strftime(DATETIME_FORMAT),
},
)

columns = [desc[0] for desc in cursor.description]
while True:
rows = cursor.fetchmany(self.config.database_query_batch_size)
if not rows:
return
for row in rows:
yield dict(zip(columns, row))

def get_soft_deleted_rows(self) -> Iterable[Dict[str, Any]]:
"""
Fetches all soft-deleted entities from the database.
Expand Down
Loading