amoghrajesh opened a new pull request, #57614:
URL: https://github.com/apache/airflow/pull/57614

   <!--
    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/
   -->
   
   ## Problem
   
   When migrating from Airflow 2.x to 3.0, the migration 
`0049_3_0_0_remove_pickled_data_from_xcom_table` was failing.
   
   
   This occurred when XCOM data contained the string "NaN" as part of a value, 
for example:
   ```json
   {"craft": "Tiangong", "name": "Ye GuangfuNaN"}
   ```
   
   The migration was using a simple `REPLACE()` function that replaced all 
occurrences of `'NaN'` with `'"nan"'`, which broke the JSON structure when 
`NaN` appeared inside string values. The broken JSON would become:
   ```json
   {"craft": "Tiangong", "name": "Ye Guangfu"nan""}
   ```
   
   And things would break.
   
   I changed the `NaN` sanitization to use regex with word boundaries to match 
only standalone `NaN` tokens (like `"key": NaN`) while preserving `NaN` that 
appears inside string values.
   
   - PostgreSQL: Uses `regexp_replace` with word boundaries (`\bNaN\b`) 
(https://neon.com/postgresql/postgresql-string-functions/regexp_replace)
   - MySQL: Uses `REGEXP_REPLACE` with word boundaries (`[[:<:]]NaN[[:>:]]`) 
(https://dev.mysql.com/doc/refman/8.4/en/regexp.html)
   - SQLite: Kept simple with original `REPLACE()` approach as sqlite doesn't 
have regex support(?)
   
   ## Testing
   
   1. Installed 2.11 Airflow using: `breeze start-airflow --backend postgres 
--executor CeleryExecutor --use-airflow-version 2.11.0 --db-reset`
   2. Ran this DAG:
   
   ```python
   from airflow import Dataset
   from airflow.decorators import dag, task
   from pendulum import datetime
   import requests
   
   
   # Define the basic parameters of the DAG, like schedule and start_date
   @dag(
       start_date=datetime(2024, 1, 1),
       schedule="@daily",
       catchup=False,
       doc_md=__doc__,
       default_args={"owner": "Astro", "retries": 3},
       tags=["example"],
   )
   def example_astronauts():
       @task
       def get_astronauts(**context) -> list[dict]:
           number_of_people_in_space = 1
           list_of_people_in_space = [
   
               {"craft": "Tiangong", "name": "Ye GuangfuNaN"},
           ]
   
           context["ti"].xcom_push(
               key="number_of_people_in_space", value=number_of_people_in_space
           )
           return list_of_people_in_space
   
       @task
       def print_astronaut_craft(greeting: str, person_in_space: dict) -> None:
           """
           This task creates a print statement with the name of an
           Astronaut in space and the craft they are flying on from
           the API request results of the previous task, along with a
           greeting which is hard-coded in this example.
           """
           craft = person_in_space["craft"]
           name = person_in_space["name"]
   
           print(f"{name} is currently in space flying on the {craft}! 
{greeting}")
   
       print_astronaut_craft.partial(greeting="Hello! :)").expand(
           person_in_space=get_astronauts()  # Define dependencies using 
TaskFlow API syntax
       )
   
   
   # Instantiate the DAG
   example_astronauts()
   
   ```
   
   <img width="3454" height="1460" alt="image (51)" 
src="https://github.com/user-attachments/assets/93246d11-f865-472d-9f69-4b7b040777d1";
 />
   
   
   3. Upgraded airflow to 3.11 using breeze: `breeze start-airflow --backend 
postgres --executor CeleryExecutor --use-airflow-version 3.1.1`
   
   No migration error:
   ```
   Performing upgrade to the metadata database 
postgresql+psycopg2://postgres:***@postgres/airflow
   2025-10-31T10:40:09.197045Z [info     ] Context impl PostgresqlImpl.   
[alembic.runtime.migration] loc=migration.py:211
   2025-10-31T10:40:09.197133Z [info     ] Will assume transactional DDL. 
[alembic.runtime.migration] loc=migration.py:214
   2025-10-31T10:40:09.197938Z [info     ] Context impl PostgresqlImpl.   
[alembic.runtime.migration] loc=migration.py:211
   2025-10-31T10:40:09.197991Z [info     ] Will assume transactional DDL. 
[alembic.runtime.migration] loc=migration.py:214
   2025-10-31T10:40:09.198247Z [info     ] Creating Airflow database tables 
from the ORM [airflow.utils.db] loc=db.py:684
   2025-10-31T10:40:09.198296Z [info     ] Creating context               
[airflow.utils.db] loc=db.py:689
   2025-10-31T10:40:09.200174Z [info     ] Binding engine                 
[airflow.utils.db] loc=db.py:691
   2025-10-31T10:40:09.200245Z [info     ] Pool status: Pool size: 5  
Connections in pool: 0 Current Overflow: -3 Current Checked out connections: 2 
[airflow.utils.db] loc=db.py:693
   2025-10-31T10:40:09.200284Z [info     ] Creating metadata              
[airflow.utils.db] loc=db.py:695
   2025-10-31T10:40:09.424717Z [info     ] Getting alembic config         
[airflow.utils.db] loc=db.py:698
   2025-10-31T10:40:09.425147Z [info     ] Stamping migration head        
[airflow.utils.db] loc=db.py:705
   2025-10-31T10:40:09.426209Z [info     ] Context impl PostgresqlImpl.   
[alembic.runtime.migration] loc=migration.py:211
   2025-10-31T10:40:09.426272Z [info     ] Will assume transactional DDL. 
[alembic.runtime.migration] loc=migration.py:214
   2025-10-31T10:40:09.450286Z [info     ] Running stamp_revision  -> 
cc92b33c6709 [alembic.runtime.migration] loc=migration.py:622
   2025-10-31T10:40:09.451776Z [info     ] Airflow database tables created 
[airflow.utils.db] loc=db.py:708
   ```
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request 
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.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 
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to