Skip to content

Commit

Permalink
refactor: use new query engine in the eval function
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Dec 19, 2024
1 parent d590264 commit 63e81f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class OSQLEngine {
Expand Down Expand Up @@ -115,6 +116,18 @@ public static OOrBlock parsePredicate(String predicate) throws OCommandSQLParsin
}
}

public static Optional<OOrBlock> maybeParsePredicate(String predicate)
throws OCommandSQLParsingException {
final InputStream is = new ByteArrayInputStream(predicate.getBytes());
try {
final OrientSql osql = new OrientSql(is);
OOrBlock result = osql.OrBlock();
return Optional.of(result);
} catch (ParseException e) {
return Optional.empty();
}
}

public static OExpression parseExpression(String predicate) throws OCommandSQLParsingException {
final InputStream is = new ByteArrayInputStream(predicate.getBytes());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.filter.OSQLPredicate;
import com.orientechnologies.orient.core.sql.OSQLEngine;
import com.orientechnologies.orient.core.sql.parser.OExpression;
import com.orientechnologies.orient.core.sql.parser.OOrBlock;
import java.util.List;
import java.util.Optional;

/**
* Evaluates a complex expression.
Expand All @@ -38,7 +41,8 @@ public class OSQLFunctionEval extends OSQLFunctionMathAbstract {

public static final String NAME = "eval";

private OSQLPredicate predicate;
private OOrBlock predicate;
private OExpression expression;

public OSQLFunctionEval() {
super(NAME, 1, 1);
Expand All @@ -53,13 +57,23 @@ public Object execute(
if (iParams.length < 1) {
throw new OCommandExecutionException("invalid ");
}
if (predicate == null) predicate = new OSQLPredicate(String.valueOf(iParams[0]));

final ODocument currentResult =
iCurrentResult instanceof ODocument ? (ODocument) iCurrentResult : null;
if (predicate == null && expression == null) {
Optional<OOrBlock> res = OSQLEngine.maybeParsePredicate(String.valueOf(iParams[0]));
if (res.isPresent()) {
this.predicate = res.get();
} else {
expression = OSQLEngine.parseExpression(String.valueOf(iParams[0]));
}
}

final ODocument currentResult = iRecord instanceof ODocument ? (ODocument) iRecord : null;
try {
return predicate.evaluate(
iRecord != null ? iRecord.getRecord() : null, currentResult, iContext);
if (predicate != null) {
return predicate.evaluate(currentResult, iContext);
} else {
return expression.execute(currentResult, iContext);
}
} catch (ArithmeticException e) {
logger.error("Division by 0", e);
// DIVISION BY 0
Expand Down

0 comments on commit 63e81f4

Please sign in to comment.