You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "hussein-awala (via GitHub)" <gi...@apache.org> on 2023/07/15 23:37:59 UTC
[GitHub] [airflow] hussein-awala opened a new pull request, #32630: Improve getting the query count in Airflow API endpoints
hussein-awala opened a new pull request, #32630:
URL: https://github.com/apache/airflow/pull/32630
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
related: #28723
When I run the Airflow webserver, I'm getting:
```
SADeprecationWarning: Implicit coercion of SELECT and textual SELECT constructs into FROM clauses is deprecated; please call .subquery() on any Core select or ORM Query object in order to produce a subquery object.
```
This PR creates a new method with the new style to get the count of a query, and utilizes it instead of duplicating the count query:
```python
def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
"""Get count of query."""
count_stmt = select(func.count()).select_from(query_stmt.order_by(None).subquery())
return session.scalar(count_stmt)
```
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1264883865
##########
airflow/api_connexion/parameters.py:
##########
@@ -125,3 +126,9 @@ def apply_sorting(
else:
order_by = f"{lstriped_orderby} asc"
return query.order_by(text(order_by))
+
+
+def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
Review Comment:
So seems like a db optimisation issue. Since this is in a function, adding the reset seems to have no drawbacks. Perhaps a comment explaining the query difference would prevent someone coming in and mistakenly assuming the order_by is superfulous.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala merged pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala merged PR #32630:
URL: https://github.com/apache/airflow/pull/32630
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1264599650
##########
airflow/api_connexion/parameters.py:
##########
@@ -125,3 +126,9 @@ def apply_sorting(
else:
order_by = f"{lstriped_orderby} asc"
return query.order_by(text(order_by))
+
+
+def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
Review Comment:
Also, is the `order_by` reset necessary?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1264652989
##########
airflow/api_connexion/parameters.py:
##########
@@ -125,3 +126,9 @@ def apply_sorting(
else:
order_by = f"{lstriped_orderby} asc"
return query.order_by(text(order_by))
+
+
+def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
Review Comment:
> Also, is the order_by reset necessary?
TBH, I am not sure how sqlalchemy processes this and generates the query, but according to the documentation, it just select from the subquery: `<query> FROM (<subquery>) AS subquery`, so if we compare this format with and without order by:
```
airflow=# EXPLAIN SELECT COUNT(1) FROM (SELECT * FROM dag_run WHERE state='failed' ORDER BY data_interval_start) AS subquery;
QUERY PLAN
----------------------------------------------------------------------
Aggregate (cost=1.04..1.05 rows=1 width=8)
-> Sort (cost=1.02..1.03 rows=1 width=1459)
Sort Key: dag_run.data_interval_start
-> Seq Scan on dag_run (cost=0.00..1.01 rows=1 width=1459)
Filter: ((state)::text = 'failed'::text)
(5 rows)
airflow=# EXPLAIN SELECT COUNT(1) FROM (SELECT * FROM dag_run WHERE state='failed') AS subquery;
QUERY PLAN
-------------------------------------------------------------
Aggregate (cost=1.01..1.02 rows=1 width=8)
-> Seq Scan on dag_run (cost=0.00..1.01 rows=1 width=0)
Filter: ((state)::text = 'failed'::text)
(3 rows)
```
There is no many rows in my DB (fresh breeze DB), so this slight difference could be much bigger in the big DB.
Personally I always reset the order_by when I don't need it to be safe and to ensure a better performance. WDYT?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1264599631
##########
airflow/api_connexion/parameters.py:
##########
@@ -125,3 +126,9 @@ def apply_sorting(
else:
order_by = f"{lstriped_orderby} asc"
return query.order_by(text(order_by))
+
+
+def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
Review Comment:
May be a good idea to put this in `airflow.utils.db` instead? Importing this in `airflow.www.views` feels wrong.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] hussein-awala commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "hussein-awala (via GitHub)" <gi...@apache.org>.
hussein-awala commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1264651918
##########
airflow/api_connexion/parameters.py:
##########
@@ -125,3 +126,9 @@ def apply_sorting(
else:
order_by = f"{lstriped_orderby} asc"
return query.order_by(text(order_by))
+
+
+def get_query_count(query_stmt: sqlalchemy.sql.selectable.Select, session: sqlalchemy.orm.Session) -> int:
Review Comment:
> May be a good idea to put this in airflow.utils.db instead? Importing this in airflow.www.views feels wrong.
I agree, I just moved it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [airflow] uranusjr commented on a diff in pull request #32630: Improve getting the query count in Airflow API endpoints
Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #32630:
URL: https://github.com/apache/airflow/pull/32630#discussion_r1267352895
##########
airflow/utils/db.py:
##########
@@ -1872,3 +1873,11 @@ def get_sqla_model_classes():
return [mapper.class_ for mapper in Base.registry.mappers]
except AttributeError:
return Base._decl_class_registry.values()
+
+
+def get_query_count(query_stmt: Select, session: Session) -> int:
+ """Get count of query."""
+ # Remove ORDER BY clause from the subquery statement since it's unnecessary for count
+ # in order to improve the query performance.
Review Comment:
```suggestion
"""Get count of query.
A SELECT COUNT() FROM is issued against the subquery built from the
given statement. The ORDER BY clause is stripped from the statement
since it's unnecessary for COUNT, and can impact query planning and
degrade performance.
:meta private:
"""
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org