<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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.
 */
 #include "stdafx.h"

#include <typeinfo>

#include "functions/func_sequences.h"
#include "functions/func_sequences_impl.h"

#include "runtime/sequences/sequences.h"
#include "runtime/sequences/SequencesImpl.h"
#include "runtime/core/var_iterators.h"
#include "runtime/core/item_iterator.h"
#include "runtime/core/arithmetic_impl.h"
#include "runtime/numerics/NumericsImpl.h"

#include "runtime/collections/collections_impl.h"
#include "runtime/collections/collections.h"
#include "runtime/indexing/index_ddl.h"

#include "system/globalenv.h"

#include "compiler/expression/expr.h"
#include "compiler/expression/fo_expr.h"
#include "compiler/expression/var_expr.h"
#include "compiler/expression/path_expr.h"

#include "types/typeops.h"

namespace zorba
{


/*******************************************************************************
********************************************************************************/
inline void 
rewriteSubsequenceCollection(static_context* aSctx,
                             const QueryLoc& aLoc,
                             std::vector<PlanIter_t>& aArgs)
{
  ZorbaCollectionIterator* collIter 
    = dynamic_cast<ZorbaCollectionIterator*>(aArgs[0].getp());
  assert(collIter);
  std::vector<PlanIter_t>& lCollectionArgs = collIter->getChildren();

  int lNumCollArgs = lCollectionArgs.size();
  if (lNumCollArgs == 1)
  {
    // argument is of type collection(qname)
    // simply move the pos of subsequence into the skip of 
    // collection function
    // subsequence(collection(qname), 10, 20) 
    //   -> subsequence(collection(qname, 10), 10, 20)
    PlanIterator* lNewCollSkipIter = aArgs[1].getp();
    lCollectionArgs.push_back(lNewCollSkipIter);
  }
  else if (lNumCollArgs <= 3 && lNumCollArgs != 0)
  {
    // argument is of type collection(qname,skip) or 
    // collection(qname,start_uri,skip)
    int lSkipPosition = 1;
    if(lNumCollArgs == 3) 
    {
      // collection function with start reference -> skip is the 3rd param
      lSkipPosition = 2;
    }
      
    // add position of subsequence to collection skip
    // subsequence(collection(qname, 10), 10, 20) 
    //   -> subsequence(collection(qname, 20), 10, 20)
    PlanIter_t& lCollSkipIter = lCollectionArgs[lSkipPosition];
    PlanIter_t& lSubseqPosIter = aArgs[1];
    lCollectionArgs[lSkipPosition] 
      = new NumArithIterator<zorba::AddOperation>(
          collIter->getStaticContext(), collIter->getLocation(), 
          lSubseqPosIter, lCollSkipIter);
  }
  else
  {
    // no collection function with 0 or >3 params
    assert(false);
  }
  aArgs[0] = new ZorbaCollectionIterator(collIter->getStaticContext(),
      collIter->getLocation(), lCollectionArgs, collIter->isDynamic());

  // after pushing the position param down we need to rewrite the actual 
  // position to 0:
  // subsequence(collection(qname, 20), 10, 20) 
  //   -> subsequence(collection(qname, 20), 0, 20)
  store::Item_t lposItem;
  GENV_ITEMFACTORY->createInteger(lposItem, xs_integer::zero());
  aArgs[1] = new SingletonIterator (aSctx, aLoc, lposItem);
}


/*******************************************************************************
********************************************************************************/
xqtref_t fn_unordered::getReturnType(const fo_expr* caller) const
{
  return caller->get_arg(0)->get_return_type();
}


BoolAnnotationValue fn_unordered::ignoresSortedNodes(expr* fo, csize input) const
{
  return ANNOTATION_TRUE;
}


BoolAnnotationValue fn_unordered::ignoresDuplicateNodes(expr* fo, csize input) const
{
  return fo->getIgnoresDuplicateNodes();
}

<commit_msg>fixed wrong rewrite of subsequence position to collection skip param.<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 *
 * 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.
 */
 #include "stdafx.h"

#include <typeinfo>

#include "functions/func_sequences.h"
#include "functions/func_sequences_impl.h"

#include "runtime/sequences/sequences.h"
#include "runtime/sequences/SequencesImpl.h"
#include "runtime/core/var_iterators.h"
#include "runtime/core/item_iterator.h"
#include "runtime/core/arithmetic_impl.h"
#include "runtime/numerics/NumericsImpl.h"

#include "runtime/collections/collections_impl.h"
#include "runtime/collections/collections.h"
#include "runtime/indexing/index_ddl.h"

#include "system/globalenv.h"

#include "compiler/expression/expr.h"
#include "compiler/expression/fo_expr.h"
#include "compiler/expression/var_expr.h"
#include "compiler/expression/path_expr.h"

#include "types/typeops.h"

namespace zorba
{


/*******************************************************************************
********************************************************************************/
inline void 
rewriteSubsequenceCollection(static_context* aSctx,
                             const QueryLoc& aLoc,
                             std::vector<PlanIter_t>& aArgs)
{
  ZorbaCollectionIterator* collIter 
    = dynamic_cast<ZorbaCollectionIterator*>(aArgs[0].getp());
  assert(collIter);
  std::vector<PlanIter_t>& lCollectionArgs = collIter->getChildren();

  // prepare helper
  store::Item_t lItemOne;
  GENV_ITEMFACTORY->createInteger(lItemOne, Integer(1));

  int lNumCollArgs = lCollectionArgs.size();
  if (lNumCollArgs == 1)
  {
    // argument is of type collection(qname)
    // simply move the (pos-1) of subsequence into the skip of 
    // collection function
    // subsequence(collection(qname), 10, 20) 
    //   -> subsequence(collection(qname, 10-1), 10, 20)
    PlanIter_t& lNewCollSkipIter = aArgs[1];
    PlanIter_t lOneIter = new SingletonIterator (aSctx, aLoc, lItemOne);
    lCollectionArgs.push_back(
        new NumArithIterator<zorba::SubtractOperation>(
          collIter->getStaticContext(), collIter->getLocation(),
          lNewCollSkipIter, lOneIter)
        );
  }
  else if (lNumCollArgs <= 3 && lNumCollArgs != 0)
  {
    // argument is of type collection(qname,skip) or 
    // collection(qname,start_uri,skip)
    int lSkipPosition = 1;
    if(lNumCollArgs == 3) 
    {
      // collection function with start reference -> skip is the 3rd param
      lSkipPosition = 2;
    }
      
    // add position-1 of subsequence to collection skip
    // subsequence(collection(qname, 10), 10, 20) 
    //   -> subsequence(collection(qname, 10+10-1), 10, 20)
    PlanIter_t& lOldCollSkipIter = lCollectionArgs[lSkipPosition];
    PlanIter_t lOneIter = new SingletonIterator (aSctx, aLoc, lItemOne);
    PlanIter_t& lSubseqPosIter = aArgs[1];
    PlanIter_t lCollSkipAdditionIter
      = new NumArithIterator<zorba::SubtractOperation>(
          collIter->getStaticContext(), collIter->getLocation(),
          lSubseqPosIter, lOneIter);
    lCollectionArgs[lSkipPosition] 
      = new NumArithIterator<zorba::AddOperation>(
          collIter->getStaticContext(), collIter->getLocation(), 
          lOldCollSkipIter, lCollSkipAdditionIter);
  }
  else
  {
    // no collection function with 0 or >3 params
    assert(false);
  }
  aArgs[0] = new ZorbaCollectionIterator(collIter->getStaticContext(),
      collIter->getLocation(), lCollectionArgs, collIter->isDynamic());

  // after pushing the position param down we need to rewrite the actual 
  // position to 1:
  // subsequence(collection(qname, 10+10-1), 10, 20) 
  //   -> subsequence(collection(qname, 10+10-1), 1, 20)
  aArgs[1] = new SingletonIterator (aSctx, aLoc, lItemOne);
}


/*******************************************************************************
********************************************************************************/
xqtref_t fn_unordered::getReturnType(const fo_expr* caller) const
{
  return caller->get_arg(0)->get_return_type();
}


BoolAnnotationValue fn_unordered::ignoresSortedNodes(expr* fo, csize input) const
{
  return ANNOTATION_TRUE;
}


BoolAnnotationValue fn_unordered::ignoresDuplicateNodes(expr* fo, csize input) const
{
  return fo->getIgnoresDuplicateNodes();
}


{'fraction_non_alphanumeric': 0.17530100519164918, 'fraction_numerical': 0.014912183806472992, 'mean_word_length': 4.288551401869159, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6185922', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2630, 'n_words': 647}