Skip to content

Commit 6198e10

Browse files
andrew-colemanmattbaileyuk
authored andcommitted
fix generation of tuple stream from input arrays
Signed-off-by: andrew-coleman <andrew_coleman@uk.ibm.com>
1 parent 12a36f0 commit 6198e10

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/jsonata.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ var jsonata = (function() {
163163
// expr is an array of steps
164164
// if the first step is a variable reference ($...), including root reference ($$),
165165
// then the path is absolute rather than relative
166-
if (expr.steps[0].type === 'variable') {
167-
inputSequence = createSequence(input); // dummy singleton sequence for first (absolute) step
168-
} else if (Array.isArray(input)) {
166+
if (Array.isArray(input) && expr.steps[0].type !== 'variable') {
169167
inputSequence = input;
170168
} else {
171169
// if input is not an array, make it so
@@ -334,7 +332,7 @@ var jsonata = (function() {
334332
result.tupleStream = true;
335333
var stepEnv = environment;
336334
if(tupleBindings === undefined) {
337-
tupleBindings = [{'@': input}];
335+
tupleBindings = input.map(item => { return {'@': item} });
338336
}
339337

340338
for(var ee = 0; ee < tupleBindings.length; ee++) {

src/parser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ const parser = (() => {
10121012
}
10131013
// throw error if there are any predicates defined at this point
10141014
// at this point the only type of stages can be predicates
1015-
if(typeof step.stages !== 'undefined') {
1015+
if(typeof step.stages !== 'undefined' || typeof step.predicate !== 'undefined') {
10161016
throw {
10171017
code: "S0215",
10181018
stack: (new Error()).stack,
@@ -1038,6 +1038,12 @@ const parser = (() => {
10381038
step = result;
10391039
if (result.type === 'path') {
10401040
step = result.steps[result.steps.length - 1];
1041+
} else {
1042+
result = {type: 'path', steps: [result]};
1043+
if (typeof step.predicate !== 'undefined') {
1044+
step.stages = step.predicate;
1045+
delete step.predicate;
1046+
}
10411047
}
10421048
if (typeof step.stages === 'undefined') {
10431049
step.index = expr.rhs.value;

test/test-suite/groups/joins/index.json

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"expr": "$.$#$pos[$pos<3]",
3+
"expr": "$#$pos[$pos<3]",
44
"data": [3,1,4,1,5,9],
55
"bindings": {},
66
"result": [
@@ -10,7 +10,25 @@
1010
]
1111
},
1212
{
13-
"expr": "$.$#$pos[$pos<3]^($)",
13+
"expr": "$#$pos[$pos<3] = $[[0..2]]",
14+
"data": [3,1,4,1,5,9],
15+
"bindings": {},
16+
"result": true
17+
},
18+
{
19+
"expr": "$.$#$pos[$pos<3]",
20+
"data": [3,1,4,1,5,9],
21+
"bindings": {},
22+
"result": [3,1,4,1,5,9]
23+
},
24+
{
25+
"expr": "$.$#$pos[$pos<3] = $.$[[0..2]]",
26+
"data": [3,1,4,1,5,9],
27+
"bindings": {},
28+
"result": true
29+
},
30+
{
31+
"expr": "$#$pos[$pos<3]^($)",
1432
"data": [3,1,4,1,5,9],
1533
"bindings": {},
1634
"result": [
@@ -20,7 +38,7 @@
2038
]
2139
},
2240
{
23-
"expr": "$.$#$pos[$pos<3]^(>$)",
41+
"expr": "$#$pos[$pos<3]^(>$)",
2442
"data": [3,1,4,1,5,9],
2543
"bindings": {},
2644
"result": [
@@ -30,7 +48,7 @@
3048
]
3149
},
3250
{
33-
"expr": "$.$^($)#$pos[$pos<3] ",
51+
"expr": "$^($)#$pos[$pos<3] ",
3452
"data": [3,1,4,1,5,9],
3553
"bindings": {},
3654
"result": [
@@ -40,35 +58,41 @@
4058
]
4159
},
4260
{
43-
"expr": "$.$#$pos[$pos<3][1]",
61+
"expr": "$#$pos[$pos<3][1]",
4462
"data": [3,1,4,1,5,9],
4563
"bindings": {},
4664
"result": 1
4765
},
4866
{
49-
"expr": "$.$#$pos[$pos<3][1][]",
67+
"expr": "$#$pos[$pos<3][1][]",
5068
"data": [3,1,4,1,5,9],
5169
"bindings": {},
5270
"result": [1]
5371
},
5472
{
55-
"expr": "$.$#$pos[$pos<3]^($)[-1]",
73+
"expr": "$#$pos[$pos<3]^($)[-1]",
5674
"data": [3,1,4,1,5,9],
5775
"bindings": {},
5876
"result": 4
5977
},
6078
{
61-
"expr": "$.$#$pos[][$pos<3]^($)[-1]",
79+
"expr": "$#$pos[][$pos<3]^($)[-1]",
6280
"data": [3,1,4,1,5,9],
6381
"bindings": {},
6482
"result": [4]
6583
},
6684
{
67-
"expr": "$.$#$pos[$pos<3]^($)[-1][]",
85+
"expr": "$#$pos[$pos<3]^($)[-1][]",
6886
"data": [3,1,4,1,5,9],
6987
"bindings": {},
7088
"result": [4]
7189
},
90+
{
91+
"expr": "$[[1..4]]#$pos[$pos>=2]",
92+
"data": [3,1,4,1,5,9],
93+
"bindings": {},
94+
"result": [1,5]
95+
},
7296
{
7397
"expr": "Account.Order#$o.Product[ProductID=858383].{ 'Product': `Product Name`, 'Order Index': $o }",
7498
"dataset": "dataset5",

0 commit comments

Comments
 (0)