From e703e955a5cd3cfd413cfd8797970b128c1e220b Mon Sep 17 00:00:00 2001 From: Mislav Bradac Date: Tue, 14 Nov 2017 14:42:58 +0100 Subject: [PATCH] Fix bug in InListOperator Reviewers: florijan, teon.banek Reviewed By: florijan Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D981 --- src/query/interpret/eval.hpp | 11 ++++++++--- tests/unit/query_expression_evaluator.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/query/interpret/eval.hpp b/src/query/interpret/eval.hpp index 3c5c6263e..d0927b30d 100644 --- a/src/query/interpret/eval.hpp +++ b/src/query/interpret/eval.hpp @@ -157,9 +157,14 @@ class ExpressionEvaluator : public TreeVisitor { _list.type()); } auto list = _list.Value>(); - if (literal.IsNull()) { - return TypedValue::Null; - } + + // If literal is NULL there is no need to try to compare it with every + // element in the list since result of every comparison will be NULL. There + // is one special case that we must test explicitly: if list is empty then + // result is false since no comparison will be performed. + if (list.size() == 0U) return false; + if (literal.IsNull()) return TypedValue::Null; + auto has_null = false; for (const auto &element : list) { auto result = literal == element; diff --git a/tests/unit/query_expression_evaluator.cpp b/tests/unit/query_expression_evaluator.cpp index 14cc3e71d..a4f881978 100644 --- a/tests/unit/query_expression_evaluator.cpp +++ b/tests/unit/query_expression_evaluator.cpp @@ -344,6 +344,21 @@ TEST(ExpressionEvaluator, InListOperator) { auto value = op->Accept(eval.eval); EXPECT_TRUE(value.IsNull()); } + { + // Null literal. + auto *op = storage.Create( + storage.Create(TypedValue::Null), list_literal); + auto value = op->Accept(eval.eval); + EXPECT_TRUE(value.IsNull()); + } + { + // Null literal, empty list. + auto *op = storage.Create( + storage.Create(TypedValue::Null), + storage.Create(std::vector())); + auto value = op->Accept(eval.eval); + EXPECT_FALSE(value.ValueBool()); + } } TEST(ExpressionEvaluator, ListMapIndexingOperator) {