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

func.sum may returns Decimal that break rest APIs. #15585

Merged
merged 1 commit into from Apr 29, 2021

Conversation

suiting-young
Copy link
Contributor

sqlalchemy.func.sum has a known "issue" that it may returns Decimal value (in my case MySQL 5.7).

This will cause problem when calling rest APIs. E.g.

GET /airflow/api/v1/pools?limit=100

...
TypeError: Object of type 'Decimal' is not JSON serializable

@ashb and @kaxil please have a review, thanks!

session.query(func.sum(TaskInstance.pool_slots))
.filter(TaskInstance.pool == self.pool)
.filter(TaskInstance.state.in_(list(EXECUTION_STATES)))
.scalar()
) or 0
) or 0)
Copy link
Member

Choose a reason for hiding this comment

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

0 does not need to be converted, so I’d just cast the query part:

return int(
    session.query(func.sum(TaskInstance.pool_slots))
    .filter(TaskInstance.pool == self.pool)
    .filter(TaskInstance.state.in_(list(EXECUTION_STATES)))
    .scalar()
) or 0

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@uranusjr I tried this first, but failed, when filter matched nothing, it returns None 😭

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per MySQL doc:

If there are no matching rows, SUM() returns NULL.

Copy link
Member

Choose a reason for hiding this comment

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

Oh… alright then.

@XD-DENG
Copy link
Member

XD-DENG commented Apr 29, 2021

Hi @suiting-young , the change makes sense to me. Please fix the static check errors. Thanks

Which will break APIs for JSON serialization.
@kaxil kaxil merged commit 053d903 into apache:master Apr 29, 2021
@suiting-young suiting-young deleted the api-pools-decimal branch August 12, 2021 06:09
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.

None yet

4 participants