Skip to content

Commit

Permalink
Removed unnecessary argument for each function and added arrow functi…
Browse files Browse the repository at this point in the history
…on handling for sum()
  • Loading branch information
Lwiel committed Jan 16, 2023
1 parent dd4ed1b commit d324407
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 28 deletions.
40 changes: 31 additions & 9 deletions src/Raven.Server/Documents/Patch/JavaScriptUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Esprima.Ast;
using Jint;
using Jint.Native;
using Jint.Native.Array;
using Jint.Native.Function;
using Jint.Native.Global;
using Jint.Native.Object;
using Jint.Runtime;
Expand Down Expand Up @@ -39,6 +41,7 @@ private JsonOperationContext Context
private readonly List<IDisposable> _disposables = new List<IDisposable>();
private readonly Engine _scriptEngine;
private static readonly Dictionary<object,object> EmptyMetadataDummy = new Dictionary<object, object>();
internal JsValue CurrentlyProcessedObject;

public bool ReadOnly;

Expand All @@ -50,9 +53,9 @@ public JavaScriptUtils(ScriptRunner runner, Engine engine)

internal JsValue Count(JsValue self, JsValue[] args)
{
if (args.Length != 1 || (args[0].AsObject() is not BlittableObjectInstance doc))
if (args.Length != 0 || (CurrentlyProcessedObject is not BlittableObjectInstance doc))
{
throw new InvalidOperationException("count(doc) must be called with a single entity argument");
throw new InvalidOperationException("count() must be called without arguments");
}

doc.TryGetValue(Constants.Documents.Indexing.Fields.CountFieldName, out var countValue);
Expand All @@ -62,9 +65,9 @@ internal JsValue Count(JsValue self, JsValue[] args)

internal JsValue Key(JsValue self, JsValue[] args)
{
if (args.Length != 1 || (args[0].AsObject() is not BlittableObjectInstance doc))
if (args.Length != 0 || (CurrentlyProcessedObject is not BlittableObjectInstance doc))
{
throw new InvalidOperationException("key(doc) must be called with a single entity argument");
throw new InvalidOperationException("key() must be called without arguments");
}

var groupByFields = doc.Projection._query.Metadata.GroupBy;
Expand Down Expand Up @@ -95,14 +98,32 @@ internal JsValue Key(JsValue self, JsValue[] args)

internal JsValue Sum(JsValue self, JsValue[] args)
{
if (args.Length != 2 || (args[0].AsObject() is not BlittableObjectInstance doc) || (args[1].AsString() is not string fieldName))
if (args.Length != 1 || CurrentlyProcessedObject is not BlittableObjectInstance doc)
{
throw new InvalidOperationException("sum(doc, field) must be called with a two arguments - entity and fieldname");
throw new InvalidOperationException("sum(field) must be called with a single argument");
}

doc.TryGetValue(fieldName, out var sumValue);

return sumValue;
if (args[0] is ScriptFunctionInstance sfi)
{
if (sfi.FunctionDeclaration.ChildNodes[1] is StaticMemberExpression sme)
{
if (sme.Property is Identifier identifier)
{
doc.TryGetValue(identifier.Name, out var sumValue);

return sumValue;
}
}
}

if (args[0] is JsString fieldName)
{
doc.TryGetValue(fieldName, out var sumValue);

return sumValue;
}

return null;
}

internal JsValue GetMetadata(JsValue self, JsValue[] args)
Expand Down Expand Up @@ -473,6 +494,7 @@ public void Clear()
{
foreach (var disposable in _disposables)
disposable.Dispose();
CurrentlyProcessedObject = null;
_disposables.Clear();
_context = null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Raven.Server/Documents/Patch/ScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,7 @@ public ScriptRunnerResult Run(JsonOperationContext jsonCtx, DocumentsOperationCo

try
{
JavaScriptUtils.CurrentlyProcessedObject = _args[0];
var call = ScriptEngine.GetValue(method).TryCast<ICallable>();
var result = call.Call(Undefined.Instance, _args);
return new ScriptRunnerResult(this, result);
Expand All @@ -1957,6 +1958,7 @@ public ScriptRunnerResult Run(JsonOperationContext jsonCtx, DocumentsOperationCo
}
finally
{
JavaScriptUtils.CurrentlyProcessedObject = null;
_refResolver.ExplodeArgsOn(null, null);
_scope = null;
_loadScope = null;
Expand Down
19 changes: 14 additions & 5 deletions src/Raven.Server/Documents/Queries/HasSpecialMethodVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,26 @@ public override void VisitCallExpression(CallExpression callExpression)
break;
case "sum":
_queryMetadata.SumInJs = null;
if (callExpression.Arguments.Count == 2)

if (callExpression.Arguments.Count == 1)
{
var fieldNameNode = callExpression.Arguments.AsNodes()[1];
if (callExpression.Arguments[0] is ArrowFunctionExpression afe)
{
if (afe.ChildNodes[1] is StaticMemberExpression sme)
{
if (sme.Property is Identifier identifier)
{
_queryMetadata.SumInJs = identifier.Name;
}
}
}

if (fieldNameNode is Literal fieldNameLiteral)
if (callExpression.Arguments.AsNodes()[0] is Literal fieldNameLiteral)
{
_queryMetadata.SumInJs = fieldNameLiteral.StringValue;
}
}

break;
case "load":
case "include":
Expand Down
41 changes: 27 additions & 14 deletions test/SlowTests/Issues/RavenDB-16556.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ order by count() as long desc
load o.EmpId as e
select
{
Key: key(o),
Value: sum(o, ""Price"")
Key: key(),
Value: sum(""Price"")
}")]
[InlineData(@"declare function
output(o){
return {
Key: key(o),
Value: sum(o, ""Price"")
Key: key(),
Value: sum(""Price"")
}
}
from Orders as o
group by o.CompId, o.EmpId
order by count() as long desc
load o.EmpId as e
select output(o)")]
[InlineData(@"declare function
output(o){
return {
Key: key(),
Value: sum(x => x.Price)
}
}
Expand Down Expand Up @@ -94,14 +107,14 @@ order by count() as long desc
load o.EmpId as e
select
{
Key: key(o),
Value: count(o)
Key: key(),
Value: count()
}")]
[InlineData(@"declare function
output(o){
return {
Key: key(o),
Value: count(o)
Key: key(),
Value: count()
}
}
Expand Down Expand Up @@ -159,14 +172,14 @@ order by count() as long desc
load o.EmpId as e
select
{
Key: key(o),
Value: sum(o)
Key: key(),
Value: sum()
}")]
[InlineData(@"declare function
output(o){
return {
Key: key(o),
Value: sum(o)
Key: key(),
Value: sum()
}
}
Expand All @@ -177,10 +190,10 @@ load o.EmpId as e
select output(o)")]
[InlineData(@"declare function
output(o){
var empId = key(o).EmpId;
var empId = key().EmpId;
return {
Key: empId,
Value: sum(o)
Value: sum()
}
}
Expand Down

0 comments on commit d324407

Please sign in to comment.