Skip to content

Commit 00fe3a2

Browse files
authored
Merge pull request #28 from microsoft/fixers
Fixers
2 parents d6a8259 + b2c14e3 commit 00fe3a2

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

flowquery-py/src/parsing/operations/group_by.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def generate_results(
114114
if node is None:
115115
node = self._root
116116

117+
if mapper_index == 0 and len(node.children) == 0 and len(self.mappers) > 0:
118+
return
119+
117120
if len(node.children) > 0:
118121
for child in node.children.values():
119122
self.mappers[mapper_index].overridden = child.value

flowquery-py/tests/compute/test_runner.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,21 @@ async def test_aggregated_with_and_return(self):
378378
assert results[0] == {"i": 1, "sum": 12}
379379
assert results[1] == {"i": 2, "sum": 12}
380380

381+
@pytest.mark.asyncio
382+
async def test_aggregated_with_on_empty_result_set(self):
383+
"""Test aggregated with on empty result set does not crash."""
384+
runner = Runner(
385+
"""
386+
unwind [] as i
387+
unwind [1, 2] as j
388+
with i, count(j) as cnt
389+
return i, cnt
390+
"""
391+
)
392+
await runner.run()
393+
results = runner.results
394+
assert len(results) == 0
395+
381396
@pytest.mark.asyncio
382397
async def test_aggregated_with_using_collect_and_return(self):
383398
"""Test aggregated with using collect and return."""

src/parsing/operations/group_by.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class GroupBy extends Projection {
5050
}
5151
private map() {
5252
let node: Node = this.current;
53-
for(const mapper of this.mappers) {
53+
for (const mapper of this.mappers) {
5454
const value: any = mapper.value();
5555
let child: Node | undefined = node.children.get(value);
56-
if(child === undefined) {
56+
if (child === undefined) {
5757
child = new Node(value);
5858
node.children.set(value, child);
5959
}
@@ -62,38 +62,46 @@ class GroupBy extends Projection {
6262
this.current = node;
6363
}
6464
private reduce() {
65-
if(this.current.elements === null) {
66-
this.current.elements = this.reducers.map(reducer => reducer.element());
65+
if (this.current.elements === null) {
66+
this.current.elements = this.reducers.map((reducer) => reducer.element());
6767
}
6868
const elements: AggregationElement[] = this.current.elements;
6969
this.reducers.forEach((reducer, index) => {
7070
reducer.reduce(elements[index]);
7171
});
7272
}
7373
private get mappers(): Expression[] {
74-
if(this._mappers === null) {
74+
if (this._mappers === null) {
7575
this._mappers = [...this._generate_mappers()];
7676
}
7777
return this._mappers;
7878
}
7979
private *_generate_mappers(): Generator<Expression> {
80-
for(const [expression, _] of this.expressions()) {
81-
if(expression.mappable()) {
80+
for (const [expression, _] of this.expressions()) {
81+
if (expression.mappable()) {
8282
yield expression;
8383
}
8484
}
8585
}
8686
private get reducers(): AggregateFunction[] {
87-
if(this._reducers === null) {
88-
this._reducers = this.children.map((child) => {
89-
return (child as Expression).reducers();
90-
}).flat();
87+
if (this._reducers === null) {
88+
this._reducers = this.children
89+
.map((child) => {
90+
return (child as Expression).reducers();
91+
})
92+
.flat();
9193
}
9294
return this._reducers;
9395
}
94-
public *generate_results(mapperIndex: number = 0, node: Node = this.root): Generator<Record<string, any>> {
95-
if(node.children.size > 0) {
96-
for(const child of node.children.values()) {
96+
public *generate_results(
97+
mapperIndex: number = 0,
98+
node: Node = this.root
99+
): Generator<Record<string, any>> {
100+
if (mapperIndex === 0 && node.children.size === 0 && this.mappers.length > 0) {
101+
return;
102+
}
103+
if (node.children.size > 0) {
104+
for (const child of node.children.values()) {
97105
this.mappers[mapperIndex].overridden = child.value;
98106
yield* this.generate_results(mapperIndex + 1, child);
99107
}
@@ -102,10 +110,10 @@ class GroupBy extends Projection {
102110
this.reducers[reducerIndex].overridden = element.value;
103111
});
104112
const record: Record<string, any> = {};
105-
for(const [expression, alias] of this.expressions()) {
113+
for (const [expression, alias] of this.expressions()) {
106114
record[alias] = expression.value();
107115
}
108-
if(this.where) {
116+
if (this.where) {
109117
yield record;
110118
}
111119
}
@@ -114,11 +122,11 @@ class GroupBy extends Projection {
114122
this._where = where;
115123
}
116124
public get where(): boolean {
117-
if(this._where === null) {
125+
if (this._where === null) {
118126
return true;
119127
}
120128
return this._where.value();
121129
}
122-
};
130+
}
123131

124-
export default GroupBy;
132+
export default GroupBy;

tests/compute/runner.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,20 @@ test("Test aggregated with and return", async () => {
334334
expect(results[1]).toEqual({ i: 2, sum: 12 });
335335
});
336336

337+
test("Test aggregated with on empty result set", async () => {
338+
const runner = new Runner(
339+
`
340+
unwind [] as i
341+
unwind [1, 2] as j
342+
with i, count(j) as cnt
343+
return i, cnt
344+
`
345+
);
346+
await runner.run();
347+
const results = runner.results;
348+
expect(results.length).toBe(0);
349+
});
350+
337351
test("Test aggregated with using collect and return", async () => {
338352
const runner = new Runner(
339353
`

0 commit comments

Comments
 (0)