Ok... at this point I'm not sure whether to file a bug, or a new feature 
request.

I can't tell from the documentation if whether the difference() function is 
supposed to act as the analogous EXCEPT operator in SQL. The name 
"difference" could mean the opposite of the "intersect()" function, but I 
don't know. 

I also checked out the OrientDB GIT repository... I've looked through the 
code of the OSQLFunctionDifference class, and it seems like it is trying to 
accomplish functionality similar to the EXCEPT in SQL, but though I see it 
initially adding all the vertexes found, it also removes them all.

I tried taking the OSQLFunctionIntersect class, duplicating and renaming it 
to OSQLFunctionExcept. I inverted the logic to remove duplicate results. It 
seems to do what I need to have done. Is that what the 
OSQLFunctionDifference function was supposed to do? If so, I probably need 
to file a bug. If I'm totally misunderstanding the difference() function 
(which I might be, the documentation doesn't give me much to work with 
except the name itself), then I probably need to file for a new enhancement.

I've seen most of the influential members of the OrientDB community 
commenting and offering advice on other topics since I originally posted 
this one nearly 5 days ago... I don't know if that means they don't have 
any answers, or I'm asking a question that makes so little sense it doesn't 
deserve an answer. I'd love to get any feedback the OrientDB experts might 
have on this. 

Thanks in advance for your time!

Regards,

Andrew

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
/*
 * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * 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.
 */
package com.orientechnologies.orient.core.sql.functions.coll;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable;
import com.orientechnologies.orient.core.sql.functions.coll.OSQLFunctionMultiValueAbstract;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * This operator can work as aggregate or inline. If only one argument is passed than aggregates, otherwise executes, and returns,
 * the INTERSECTION of the collections received as parameters.
 * 
 * @author Luca Garulli (l.garulli--at--orientechnologies.com)
 * 
 */
public class OSQLFunctionExcept extends OSQLFunctionMultiValueAbstract<Set<Object>> {
  public static final String NAME = "except";

  private Set<Object> duplicateItems;

  public OSQLFunctionExcept() {
    super(NAME, 1, -1);
  }


  private static void addItemToResult(Object o, Set<Object> uniqueItems, Set<Object> duplicateItems) {
    if (!uniqueItems.contains(o) && !duplicateItems.contains(o)) {
      uniqueItems.add(o);
    } else {
      uniqueItems.remove(o);
      duplicateItems.add(o);
    }
  }

  private static void addItemsToResult(Collection<Object> co, Set<Object> uniqueItems, Set<Object> duplicateItems) {
    for (Object o : co) {
      addItemToResult(o, uniqueItems, duplicateItems);
    }
  }


  public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams,
      OCommandContext iContext) {
    Object value = iParams[0];

    if (value instanceof OSQLFilterItemVariable)
      value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);

    if (value == null)
      return Collections.emptySet();

    if (!(value instanceof Collection<?>))
      value = Arrays.asList(value);

    final Collection<?> coll = (Collection<?>) value;

    if (iParams.length == 1) {
      // AGGREGATION MODE (STATEFUL)
      if (context == null) {
        // ADD ALL THE ITEMS OF THE FIRST COLLECTION
        context = new HashSet<Object>(coll);
      } else {
        // INTERSECT IT AGAINST THE CURRENT COLLECTION
        if (duplicateItems == null) {
          duplicateItems = new HashSet<Object>();
        }

        if (value instanceof Collection<?>) {
          addItemsToResult((Collection<Object>) value, context, duplicateItems);
        } else {
          addItemToResult(value, context, duplicateItems);
        }
        // context.retainAll(coll);
      }
      return null;
    } else {
      // IN-LINE MODE (STATELESS)
      final HashSet<Object> result = new HashSet<Object>(coll);

      for (int i = 1; i < iParams.length; ++i) {
        value = iParams[i];

        if (value instanceof OSQLFilterItemVariable)
          value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);

        if (value != null) {
          if (!(value instanceof Collection<?>))
            // CONVERT IT INTO A COLLECTION
            value = Arrays.asList(value);

          result.removeAll((Collection<?>) value);
        } else
          result.clear();
      }

      return result;
    }
  }

  public String getSyntax() {
    return "except(<field>*)";
  }

  @SuppressWarnings("unchecked")
  @Override
  public Object mergeDistributedResult(List<Object> resultsToMerge) {
    final Collection<Object> result = new HashSet<Object>();
    if (!resultsToMerge.isEmpty()) {
      final Collection<Object> items = (Collection<Object>) resultsToMerge.get(0);
      if (items != null) {
        result.addAll(items);
      }
    }
    for (int i = 1; i < resultsToMerge.size(); i++) {
      final Collection<Object> items = (Collection<Object>) resultsToMerge.get(i);
      if (items != null) {
        result.removeAll(items);
      }
    }
    return result;
  }
}

Reply via email to