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

Neo4j Hook does not return query response #18005

Closed
2 tasks done
minu7 opened this issue Sep 3, 2021 · 2 comments · Fixed by #18007
Closed
2 tasks done

Neo4j Hook does not return query response #18005

minu7 opened this issue Sep 3, 2021 · 2 comments · Fixed by #18007
Assignees
Labels
area:providers kind:bug This is a clearly a bug

Comments

@minu7
Copy link
Contributor

minu7 commented Sep 3, 2021

Apache Airflow Provider(s)

neo4j

Versions of Apache Airflow Providers

apache-airflow-providers-neo4j version 2.0.0

Apache Airflow version

2.1.0

Operating System

macOS Big Sur Version 11.4

Deployment

Docker-Compose

Deployment details

docker-compose version 1.29.2, build 5becea4c
Docker version 20.10.7, build f0df350

What happened

neo4j = Neo4jHook(conn_id=self.neo4j_conn_id)
sql = "MATCH (n) RETURN COUNT(n)"
result = neo4j.run(sql)
logging.info(result)

If i run the code snippet above i always get an empty response.

What you expected to happen

i would like to have the query response.

How to reproduce

from airflow.models.baseoperator import BaseOperator
from airflow.providers.neo4j.hooks.neo4j import Neo4jHook
import logging

class testNeo4jHookOperator(BaseOperator):

    def __init__(
            self,
            neo4j_conn_id: str,
            **kwargs) -> None:
        super().__init__(**kwargs)
        self.neo4j_conn_id = neo4j_conn_id

    def execute(self, context):
        neo4j = Neo4jHook(conn_id=self.neo4j_conn_id)
        sql = "MATCH (n) RETURN COUNT(n)"
        result = neo4j.run(sql)
        logging.info(result)
        return result

I created this custom operator to test the bug.

Anything else

The bug is related to the way the hook is implemented and how the Neo4j driver works:

def run(self, query) -> Result:
        """
        Function to create a neo4j session
        and execute the query in the session.
        :param query: Neo4j query
        :return: Result
        """
        driver = self.get_conn()
        if not self.connection.schema:
            with driver.session() as session:
                result = session.run(query)
        else:
            with driver.session(database=self.connection.schema) as session:
                result = session.run(query)
        return result

In my opinion, the above hook run method should be changed to:

    def run(self, query) -> Result:
        """
        Function to create a neo4j session
        and execute the query in the session.


        :param query: Neo4j query
        :return: Result
        """
        driver = self.get_conn()
        if not self.connection.schema:
            with driver.session() as session:
                result = session.run(query)
                return result.data()
        else:
            with driver.session(database=self.connection.schema) as session:
                result = session.run(query)
                return result.data()

This is because when trying to get result.data (or result in general) externally the session is always empty.
I tried this solution and it seems to work.

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

@minu7 minu7 added area:providers kind:bug This is a clearly a bug labels Sep 3, 2021
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 3, 2021

Thanks for opening your first issue here! Be sure to follow the issue template!

@potiuk
Copy link
Member

potiuk commented Sep 3, 2021

Cool. Go for it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers kind:bug This is a clearly a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants