Skip to content

Commit

Permalink
#292 Some $reduce(..., $append) expressions work very slow
Browse files Browse the repository at this point in the history
  • Loading branch information
eschava committed Jan 25, 2024
1 parent 3c9604e commit 761b1ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

public class AppendFunction extends FunctionBase {
public class AppendFunction extends FunctionBase implements NoContextFunction {

public static String ERR_BAD_CONTEXT = String.format(Constants.ERR_MSG_BAD_CONTEXT, Constants.FUNCTION_APPEND);
public static String ERR_ARG1BADTYPE = String.format(Constants.ERR_MSG_ARG1_BAD_TYPE, Constants.FUNCTION_APPEND);
Expand Down Expand Up @@ -98,6 +98,16 @@ public JsonNode invoke(ExpressionsVisitor expressionVisitor, Function_callContex
return result;
}

@Override
public JsonNode invoke(JsonNode... elements) {
ArrayNode a = ArrayUtils.ensureArray(elements[0]);
ArrayNode b = ArrayUtils.ensureArray(elements[1]);

a.addAll(b);

return a;
}

@Override
public String getSignature() {
// accepts anything, anything, returns an array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.api.jsonata4java.expressions.functions;

import com.fasterxml.jackson.databind.JsonNode;

/**
* Optimized version of {@link FunctionBase} where parameters don't have to be converted
* from {@link JsonNode} to call context
*/
public interface NoContextFunction {
JsonNode invoke(JsonNode... elements);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import com.api.jsonata4java.expressions.functions.NoContextFunction;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.ParserRuleContext;
Expand Down Expand Up @@ -666,6 +668,9 @@ public static JsonNode getValuesListExpressionOrNullNode(ExpressionsVisitor expr
*/
public static JsonNode processVariablesCallFunction(ExpressionsVisitor exprVisitor, FunctionBase function,
TerminalNode varid, Function_callContext ctx, JsonNode... elements) {
if (function instanceof NoContextFunction)
return ((NoContextFunction) function).invoke(elements);

ExprListContext elc = new ExprListContext(ctx.getParent(), ctx.invokingState);
ExprValuesContext evc = new ExprValuesContext(ctx.getParent(), ctx.invokingState);
evc.addAnyChild(new TerminalNodeImpl(CommonTokenFactory.DEFAULT.create(MappingExpressionParser.T__1, "(")));
Expand Down

0 comments on commit 761b1ca

Please sign in to comment.