From 3143c986de52ebef82028de4a1dd1765b926f884 Mon Sep 17 00:00:00 2001
From: DavIvek <david.ivekovic@memgraph.io>
Date: Mon, 9 Oct 2023 11:31:49 +0200
Subject: [PATCH] Fix crash caused by using exists() in a RETURN statement
 (#1303)

---
 src/query/frontend/semantic/symbol_generator.cpp     |  4 ++++
 .../memgraph_V1/features/memgraph_exists.feature     | 12 ++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/src/query/frontend/semantic/symbol_generator.cpp b/src/query/frontend/semantic/symbol_generator.cpp
index 709df16c0..5fbc5d8a3 100644
--- a/src/query/frontend/semantic/symbol_generator.cpp
+++ b/src/query/frontend/semantic/symbol_generator.cpp
@@ -518,6 +518,10 @@ bool SymbolGenerator::PreVisit(Exists &exists) {
     throw utils::NotYetImplemented("WITH can not be used with exists, but only during matching!");
   }
 
+  if (scope.in_return) {
+    throw utils::NotYetImplemented("RETURN can not be used with exists, but only during matching!");
+  }
+
   scope.in_exists = true;
 
   const auto &symbol = CreateAnonymousSymbol();
diff --git a/tests/gql_behave/tests/memgraph_V1/features/memgraph_exists.feature b/tests/gql_behave/tests/memgraph_V1/features/memgraph_exists.feature
index fb24f6270..aef6940ce 100644
--- a/tests/gql_behave/tests/memgraph_V1/features/memgraph_exists.feature
+++ b/tests/gql_behave/tests/memgraph_V1/features/memgraph_exists.feature
@@ -527,3 +527,15 @@ Feature: WHERE exists
           MATCH (n:Two) SET n.prop = exists((n)<-[:TYPE]-()) RETURN n.prop;
           """
       Then an error should be raised
+
+  Scenario: Test exists does not work in RETURN clauses
+      Given an empty graph
+      And having executed:
+          """
+          CREATE (:One {prop:1})-[:TYPE]->(:Two);
+          """
+      When executing query:
+          """
+          MATCH (n) RETURN exists((n)-[]-());
+          """
+      Then an error should be raised