Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Refactor decimal conversion in PyArrow tables to use direct casting #544

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jayantsing-db
Copy link

@jayantsing-db jayantsing-db commented Apr 9, 2025

What type of PR is this?

  • Refactor
  • Feature
  • Bug Fix
  • Other

Description

This PR replaces the previous implementation of convert_decimals_in_arrow_table() with a more efficient approach that uses PyArrow's native casting operation instead of going through pandas conversion and array creation.

  • Remove conversion to pandas DataFrame via to_pandas() and apply() methods
  • Remove intermediate steps of creating array from decimal column and setting it back
  • Replace with direct type casting using PyArrow's cast() method
  • Build a new table with transformed columns rather than modifying the original table
  • Create a new schema based on the modified fields

The new approach is more performant by avoiding pandas conversion overhead. The table below highlights substantial performance improvements when retrieving all rows from a table containing decimal columns, particularly when compression is disabled. Even greater gains were observed with compression enabled—showing approximately an 84% improvement (6 seconds compared to 39 seconds). Benchmarking was performed against e2-dogfood, with the client located in the us-west-2 region.
image

How is this tested?

  • Unit tests
  • E2E Tests
  • Manually
  • N/A

Benchmarking was performed against e2-dogfood, with the client located in the us-west-2 region.

Related Tickets & Documents

@jayantsing-db jayantsing-db changed the title Improvements to fetch results #1 Refactor decimal conversion in PyArrow tables to use direct casting May 5, 2025
@jayantsing-db jayantsing-db marked this pull request as ready for review May 5, 2025 22:06
Copy link
Contributor

@jprakash-db jprakash-db left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for making the changes

precision, scale = description[i][4], description[i][5]
assert scale is not None
assert precision is not None
# Spark limits decimal to a maximum scale of 38,
# so 128 is guaranteed to be big enough
# create the target decimal type
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Feel this comment is unnecessary

table = table.set_column(i, field, col_data)
return table

# convert the column directly using PyArrow's cast operation
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Same don't think this comment is needed as code is descriptive enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants