iblaine opened a new issue #17765:
URL: https://github.com/apache/airflow/issues/17765


   <!--
   Welcome to Apache Airflow!
   
   Please complete the next sections or the issue will be closed.
   -->
   
   **Apache Airflow version**:
   
   2.2.0.dev0
   
   **OS**:
   
   Debian GNU/Linux
   
   **Apache Airflow Provider versions**:
   
   <!-- You can use `pip freeze | grep apache-airflow-providers` (you can leave 
only relevant ones)-->
   
   **Deployment**:
   
   docker-compose version 1.29.2
   
   **What happened**:
   
   Currently in hive hooks, when 
[get_pandas_df()](https://github.com/apache/airflow/blob/main/airflow/providers/apache/hive/hooks/hive.py#L1039)
 is used to create a dataframe, the first step creates the dataframe, the 
second step creates the columns.  The step to add columns throws an exception 
when the table is empty.
   
   ```
   hh = HiveServer2Hook()
   sql = "SELECT * FROM <table> WHERE 1=0"
   df = hh.get_pandas_df(sql)
   
   [2021-08-15 21:10:15,282] {{hive.py:449}} INFO - SELECT * FROM <table> WHERE 
1=0
   Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
     File 
"/venv/lib/python3.7/site-packages/airflow/providers/apache/hive/hooks/hive.py",
 line 1073, in get_pandas_df
       df.columns = [c[0] for c in res['header']]
     File "/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 
5154, in __setattr__
       return object.__setattr__(self, name, value)
     File "pandas/_libs/properties.pyx", line 66, in 
pandas._libs.properties.AxisProperty.__set__
     File "/venv/lib/python3.7/site-packages/pandas/core/generic.py", line 564, 
in _set_axis
       self._mgr.set_axis(axis, labels)
     File 
"/venv/lib/python3.7/site-packages/pandas/core/internals/managers.py", line 
227, in set_axis
       f"Length mismatch: Expected axis has {old_len} elements, new "
   ValueError: Length mismatch: Expected axis has 0 elements, new values have 1 
elements
   ```
   
   **What you expected to happen**:
   
   get_pandas_df() should return an empty dataframe, with the columns listed in 
the empty dataframe.  Ideally, `len(df.index)` produces `0` and `df.columns` 
lists the columns.
   
   
   **How to reproduce it**:
   
   Use your own hive server...
   ```
   from airflow.providers.apache.hive.hooks.hive import HiveServer2Hook
   hh = HiveServer2Hook()
   sql = "SELECT * FROM <table> WHERE 1=0"
   df = hh.get_pandas_df(sql)
   ```
   Where \<table> can be any table.
   
   **Anything else we need to know**:
   
   Here's a potential fix ...
   ```
       #df = pandas.DataFrame(res['data'], **kwargs)
       #df.columns = [c[0] for c in res['header']]
       df = pandas.DataFrame(res['data'], columns=[c[0] for c in 
res['header']], **kwargs)
   
   ```
   
   A PR is more or less ready but someone else should confirm this is an actual 
bug.  I can trigger the bug on an actual hive server.  I cannot trigger the bug 
using 
[test_hive.py](https://github.com/apache/airflow/blob/main/tests/providers/apache/hive/hooks/test_hive.py).
  Also, if a PR Is needed, and a new test is needed, then I may need some help 
updating test_hive.py.
   
   **Are you willing to submit a PR?**
   
   Yes.
   


-- 
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