From 63e81f4a60b4dd32bfe9bdde3dd10772044c5a50 Mon Sep 17 00:00:00 2001 From: Tglman Date: Thu, 19 Dec 2024 22:13:46 +0000 Subject: [PATCH] refactor: use new query engine in the eval function --- .../orient/core/sql/OSQLEngine.java | 13 +++++++++ .../sql/functions/math/OSQLFunctionEval.java | 28 ++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/OSQLEngine.java b/core/src/main/java/com/orientechnologies/orient/core/sql/OSQLEngine.java index 898baa961ad..877a5e3c68b 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/OSQLEngine.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/OSQLEngine.java @@ -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 { @@ -115,6 +116,18 @@ public static OOrBlock parsePredicate(String predicate) throws OCommandSQLParsin } } + public static Optional 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 { diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/functions/math/OSQLFunctionEval.java b/core/src/main/java/com/orientechnologies/orient/core/sql/functions/math/OSQLFunctionEval.java index cbb364d7f48..8d86aa0f47a 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/functions/math/OSQLFunctionEval.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/functions/math/OSQLFunctionEval.java @@ -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. @@ -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); @@ -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 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