<commit_before>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
////////////////////////////////////////////////////////////////////////////////

#include "Basics/JsonHelper.h"
#include "Aql/Ast.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/TraversalConditionFinder.h"
#include "Aql/TraversalNode.h"

using namespace arangodb::aql;
using EN = arangodb::aql::ExecutionNode;

static bool checkPathVariableAccessFeasible(CalculationNode const* cn,
                                            TraversalNode* tn,
                                            Variable const* var,
                                            bool& conditionIsImpossible,
                                            Ast* ast) {
  auto node = cn->expression()->node();

  if (node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_OR) ||
      node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_IN) ||
      node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_NIN)) {
    return false;
  }

  std::vector<AstNode const*> currentPath;
  std::vector<std::vector<AstNode const*>> paths;

  node->findVariableAccess(currentPath, paths, var);

  for (auto const& onePath : paths) {
    size_t len = onePath.size();
    bool isEdgeAccess = false;

    for (auto const & node : onePath) {
      if (node->type == NODE_TYPE_FCALL) {
        //
        // we currently don't know how to execute functions in the
        // traversal (-> TraverserExpression::recursiveCheck
        return false;
      }
    }

    if (onePath[len - 2]->type == NODE_TYPE_ATTRIBUTE_ACCESS) {
      isEdgeAccess = strcmp(onePath[len - 2]->getStringValue(), "edges") == 0;

      if (!isEdgeAccess &&
          strcmp(onePath[len - 2]->getStringValue(), "vertices") != 0) {
        /* We can't catch all cases in which this error would occur, so we don't
           throw here.
           std::string message("TRAVERSAL: path only knows 'edges' and
           'vertices', not ");
           message += onePath[len - 2]->getStringValue();
           THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_QUERY_PARSE, message);
        */
        return false;
      }
    }

    // we now need to check for p.edges[n] whether n is >= 0
    if (onePath[len - 3]->type == NODE_TYPE_INDEXED_ACCESS) {
      auto indexAccessNode = onePath[len - 3]->getMember(1);
      if ((indexAccessNode->type != NODE_TYPE_VALUE) ||
          (indexAccessNode->value.type != VALUE_TYPE_INT) ||
          (indexAccessNode->value.value._int < 0)) {
        return false;
      }

      conditionIsImpossible =
          !tn->isInRange(indexAccessNode->value.value._int, isEdgeAccess);

    } else if ((onePath[len - 3]->type == NODE_TYPE_ITERATOR) &&
               (onePath[len - 4]->type == NODE_TYPE_EXPANSION)) {
      // we now need to check for p.edges[*] which becomes a fancy structure
      return false;
    } else {
      return false;
    }
  }

  return true;
}

////////////////////////////////////////////////////////////////////////////////

static bool extractSimplePathAccesses(AstNode const* node, TraversalNode* tn,
                                      Ast* ast) {
  std::vector<AstNode const*> currentPath;
  std::vector<std::vector<AstNode const*>> paths;
  std::vector<std::vector<AstNode const*>> clonePath;

  node->findVariableAccess(currentPath, paths, tn->pathOutVariable());

  for (auto const& onePath : paths) {
    size_t len = onePath.size();
    bool isEdgeAccess = false;
    size_t attrAccessTo = 0;

    if (onePath[len - 2]->type == NODE_TYPE_ATTRIBUTE_ACCESS) {
      isEdgeAccess = strcmp(onePath[len - 2]->getStringValue(), "edges") == 0;
    }
    // we now need to check for p.edges[n] whether n is >= 0
    if (onePath[len - 3]->type == NODE_TYPE_INDEXED_ACCESS) {
      auto indexAccessNode = onePath[len - 3]->getMember(1);
      attrAccessTo = indexAccessNode->value.value._int;
    }

    AstNode const* compareNode = nullptr;
    AstNode const* accessNodeBranch = nullptr;

    for (auto const& oneNode : onePath) {
      if (compareNode != nullptr && accessNodeBranch == nullptr) {
        accessNodeBranch = oneNode;
      }

      if ((oneNode->type == NODE_TYPE_OPERATOR_BINARY_EQ) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_NE) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_LT) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_LE) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_GT) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_GE)
          //  || As long as we need to twist the access, this is impossible:
          // (oneNode->type == NODE_TYPE_OPERATOR_BINARY_IN ) ||
          // (oneNode->type == NODE_TYPE_OPERATOR_BINARY_NIN))
          ) {
        compareNode = oneNode;
      }
    }

    if (compareNode != nullptr) {
      AstNode const* pathAccessNode;
      AstNode* filterByNode;
      bool flipOperator = false;

      if (compareNode->getMember(0) == accessNodeBranch) {
        pathAccessNode = accessNodeBranch;
        filterByNode = compareNode->getMember(1);
      } else {
        flipOperator = (compareNode->type == NODE_TYPE_OPERATOR_BINARY_LT) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_LE) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_GT) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_GE);

        pathAccessNode = accessNodeBranch;
        filterByNode = compareNode->getMember(0);
      }

      // TODO: warning: Called C++ object pointer is null
      if (accessNodeBranch->isSimple() && filterByNode->isDeterministic()) {<commit_msg>Added an additional nullptr check.<commit_after>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
////////////////////////////////////////////////////////////////////////////////

#include "Basics/JsonHelper.h"
#include "Aql/Ast.h"
#include "Aql/ExecutionPlan.h"
#include "Aql/TraversalConditionFinder.h"
#include "Aql/TraversalNode.h"

using namespace arangodb::aql;
using EN = arangodb::aql::ExecutionNode;

static bool checkPathVariableAccessFeasible(CalculationNode const* cn,
                                            TraversalNode* tn,
                                            Variable const* var,
                                            bool& conditionIsImpossible,
                                            Ast* ast) {
  auto node = cn->expression()->node();

  if (node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_OR) ||
      node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_IN) ||
      node->containsNodeType(NODE_TYPE_OPERATOR_BINARY_NIN)) {
    return false;
  }

  std::vector<AstNode const*> currentPath;
  std::vector<std::vector<AstNode const*>> paths;

  node->findVariableAccess(currentPath, paths, var);

  for (auto const& onePath : paths) {
    size_t len = onePath.size();
    bool isEdgeAccess = false;

    for (auto const & node : onePath) {
      if (node->type == NODE_TYPE_FCALL) {
        //
        // we currently don't know how to execute functions in the
        // traversal (-> TraverserExpression::recursiveCheck
        return false;
      }
    }

    if (onePath[len - 2]->type == NODE_TYPE_ATTRIBUTE_ACCESS) {
      isEdgeAccess = strcmp(onePath[len - 2]->getStringValue(), "edges") == 0;

      if (!isEdgeAccess &&
          strcmp(onePath[len - 2]->getStringValue(), "vertices") != 0) {
        /* We can't catch all cases in which this error would occur, so we don't
           throw here.
           std::string message("TRAVERSAL: path only knows 'edges' and
           'vertices', not ");
           message += onePath[len - 2]->getStringValue();
           THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_QUERY_PARSE, message);
        */
        return false;
      }
    }

    // we now need to check for p.edges[n] whether n is >= 0
    if (onePath[len - 3]->type == NODE_TYPE_INDEXED_ACCESS) {
      auto indexAccessNode = onePath[len - 3]->getMember(1);
      if ((indexAccessNode->type != NODE_TYPE_VALUE) ||
          (indexAccessNode->value.type != VALUE_TYPE_INT) ||
          (indexAccessNode->value.value._int < 0)) {
        return false;
      }

      conditionIsImpossible =
          !tn->isInRange(indexAccessNode->value.value._int, isEdgeAccess);

    } else if ((onePath[len - 3]->type == NODE_TYPE_ITERATOR) &&
               (onePath[len - 4]->type == NODE_TYPE_EXPANSION)) {
      // we now need to check for p.edges[*] which becomes a fancy structure
      return false;
    } else {
      return false;
    }
  }

  return true;
}

////////////////////////////////////////////////////////////////////////////////

static bool extractSimplePathAccesses(AstNode const* node, TraversalNode* tn,
                                      Ast* ast) {
  std::vector<AstNode const*> currentPath;
  std::vector<std::vector<AstNode const*>> paths;
  std::vector<std::vector<AstNode const*>> clonePath;

  node->findVariableAccess(currentPath, paths, tn->pathOutVariable());

  for (auto const& onePath : paths) {
    size_t len = onePath.size();
    bool isEdgeAccess = false;
    size_t attrAccessTo = 0;

    if (onePath[len - 2]->type == NODE_TYPE_ATTRIBUTE_ACCESS) {
      isEdgeAccess = strcmp(onePath[len - 2]->getStringValue(), "edges") == 0;
    }
    // we now need to check for p.edges[n] whether n is >= 0
    if (onePath[len - 3]->type == NODE_TYPE_INDEXED_ACCESS) {
      auto indexAccessNode = onePath[len - 3]->getMember(1);
      attrAccessTo = indexAccessNode->value.value._int;
    }

    AstNode const* compareNode = nullptr;
    AstNode const* accessNodeBranch = nullptr;

    for (auto const& oneNode : onePath) {
      if (compareNode != nullptr && accessNodeBranch == nullptr) {
        accessNodeBranch = oneNode;
      }

      if ((oneNode->type == NODE_TYPE_OPERATOR_BINARY_EQ) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_NE) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_LT) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_LE) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_GT) ||
          (oneNode->type == NODE_TYPE_OPERATOR_BINARY_GE)
          //  || As long as we need to twist the access, this is impossible:
          // (oneNode->type == NODE_TYPE_OPERATOR_BINARY_IN ) ||
          // (oneNode->type == NODE_TYPE_OPERATOR_BINARY_NIN))
          ) {
        compareNode = oneNode;
      }
    }

    if (compareNode != nullptr) {
      AstNode const* pathAccessNode;
      AstNode* filterByNode;
      bool flipOperator = false;

      if (compareNode->getMember(0) == accessNodeBranch) {
        pathAccessNode = accessNodeBranch;
        filterByNode = compareNode->getMember(1);
      } else {
        flipOperator = (compareNode->type == NODE_TYPE_OPERATOR_BINARY_LT) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_LE) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_GT) ||
                       (compareNode->type == NODE_TYPE_OPERATOR_BINARY_GE);

        pathAccessNode = accessNodeBranch;
        filterByNode = compareNode->getMember(0);
      }

      // Hacki: I do not think that the nullptr check can ever fail because of
      // the structure of onePath
      if (accessNodeBranch != nullptr && accessNodeBranch->isSimple() &&
          filterByNode->isDeterministic()) {
{'fraction_non_alphanumeric': 0.15182799037491268, 'fraction_numerical': 0.006830707133431654, 'mean_word_length': 2.2461577223481983, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7473683', 'n_tokens_mistral': 3830, 'n_tokens_neox': 3668, 'n_words': 908}