<commit_before>/*
 *
 *  *  Copyright 2014 Orient Technologies LTD (info(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.
 *  *
 *  * For more information: http://www.orientechnologies.com
 *
 */
package com.orientechnologies.orient.core.sql.filter;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.orient.core.collate.OCollate;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.config.OStorageConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.exception.OQueryParsingException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.sql.OSQLHelper;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime;
import com.orientechnologies.orient.core.sql.operator.OQueryOperator;
import com.orientechnologies.orient.core.sql.query.OSQLQuery;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Run-time query condition evaluator.
 *
 * @author <NAME>
 */
public class OSQLFilterCondition {
  private static final String NULL_VALUE = "null";
  protected Object            left;
  protected OQueryOperator    operator;
  protected Object            right;
  protected boolean           inBraces   = false;

  public OSQLFilterCondition(final Object iLeft, final OQueryOperator iOperator) {
    this.left = iLeft;
    this.operator = iOperator;
  }

  public OSQLFilterCondition(final Object iLeft, final OQueryOperator iOperator, final Object iRight) {
    this.left = iLeft;
    this.operator = iOperator;
    this.right = iRight;
  }

  public Object evaluate(final OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final OCommandContext iContext) {
    // EXECUTE SUB QUERIES ONCE
    if (left instanceof OSQLQuery<?>) {
      left = ((OSQLQuery<?>) left).setContext(iContext).execute();
    }
    Object l = evaluate(iCurrentRecord, iCurrentResult, left, iContext);

    if (operator != null && operator.canShortCircuit(l)) {
      return l;
    }

    if (right instanceof OSQLQuery<?>) {
      right = ((OSQLQuery<?>) right).setContext(iContext).execute();
    }

    Object r = evaluate(iCurrentRecord, iCurrentResult, right, iContext);

    final OCollate collate = getCollate();

    final Object[] convertedValues = checkForConversion(iCurrentRecord, l, r, collate);
    if (convertedValues != null) {
      l = convertedValues[0];
      r = convertedValues[1];
    }

    if (operator == null) {
      if (l == null)
      // THE LEFT RETURNED NULL
      {
        return Boolean.FALSE;
      }

      // UNITARY OPERATOR: JUST RETURN LEFT RESULT
      return l;
    }

    Object result;
    try {
      result = operator.evaluateRecord(iCurrentRecord, iCurrentResult, this, l, r, iContext);
    } catch (Exception e) {
      result = Boolean.FALSE;
    }

    return result;
  }

  public OCollate getCollate() {
    if (left instanceof OSQLFilterItemField) {
      return ((OSQLFilterItemField) left).getCollate();
    } else if (right instanceof OSQLFilterItemField) {
      return ((OSQLFilterItemField) right).getCollate();
    }
    return null;
  }

  public ORID getBeginRidRange() {
    if (operator == null) {
      if (left instanceof OSQLFilterCondition) {
        return ((OSQLFilterCondition) left).getBeginRidRange();
      } else {
        return null;
      }
    }

    return operator.getBeginRidRange(left, right);
  }<commit_msg>Better manage command exceptions thrown by query operators<commit_after>/*
 *
 *  *  Copyright 2014 Orient Technologies LTD (info(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.
 *  *
 *  * For more information: http://www.orientechnologies.com
 *
 */
package com.orientechnologies.orient.core.sql.filter;

import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.orient.core.collate.OCollate;
import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.config.OStorageConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.exception.OQueryParsingException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.sql.OSQLHelper;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime;
import com.orientechnologies.orient.core.sql.operator.OQueryOperator;
import com.orientechnologies.orient.core.sql.query.OSQLQuery;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Run-time query condition evaluator.
 *
 * @author <NAME>
 */
public class OSQLFilterCondition {
  private static final String NULL_VALUE = "null";
  protected Object            left;
  protected OQueryOperator    operator;
  protected Object            right;
  protected boolean           inBraces   = false;

  public OSQLFilterCondition(final Object iLeft, final OQueryOperator iOperator) {
    this.left = iLeft;
    this.operator = iOperator;
  }

  public OSQLFilterCondition(final Object iLeft, final OQueryOperator iOperator, final Object iRight) {
    this.left = iLeft;
    this.operator = iOperator;
    this.right = iRight;
  }

  public Object evaluate(final OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final OCommandContext iContext) {
    // EXECUTE SUB QUERIES ONCE
    if (left instanceof OSQLQuery<?>) {
      left = ((OSQLQuery<?>) left).setContext(iContext).execute();
    }
    Object l = evaluate(iCurrentRecord, iCurrentResult, left, iContext);

    if (operator != null && operator.canShortCircuit(l)) {
      return l;
    }

    if (right instanceof OSQLQuery<?>) {
      right = ((OSQLQuery<?>) right).setContext(iContext).execute();
    }

    Object r = evaluate(iCurrentRecord, iCurrentResult, right, iContext);

    final OCollate collate = getCollate();

    final Object[] convertedValues = checkForConversion(iCurrentRecord, l, r, collate);
    if (convertedValues != null) {
      l = convertedValues[0];
      r = convertedValues[1];
    }

    if (operator == null) {
      if (l == null)
      // THE LEFT RETURNED NULL
      {
        return Boolean.FALSE;
      }

      // UNITARY OPERATOR: JUST RETURN LEFT RESULT
      return l;
    }

    Object result;
    try {
      result = operator.evaluateRecord(iCurrentRecord, iCurrentResult, this, l, r, iContext);
    } catch (OCommandExecutionException e) {
      throw e;
    } catch (Exception e) {
      result = Boolean.FALSE;
    }

    return result;
  }

  public OCollate getCollate() {
    if (left instanceof OSQLFilterItemField) {
      return ((OSQLFilterItemField) left).getCollate();
    } else if (right instanceof OSQLFilterItemField) {
      return ((OSQLFilterItemField) right).getCollate();
    }
    return null;
  }

  public ORID getBeginRidRange() {
    if (operator == null) {
      if (left instanceof OSQLFilterCondition) {
        return ((OSQLFilterCondition) left).getBeginRidRange();
      } else {
        return null;
      }
    }

    return operator.getBeginRidRange(left, right);
  }
{'fraction_non_alphanumeric': 0.09880976810999384, 'fraction_numerical': 0.0020521239482864766, 'mean_word_length': 3.777941176470588, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3407454', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2857, 'n_words': 696}