Skip to content

Commit 3d409b8

Browse files
committed
fix: COUNT(col) nulls in 3 partial-agg paths, COUNT(DISTINCT string) in SQL executor, PERCENTILE in isAggregate; cache LIKE regex
1 parent 389d81d commit 3d409b8

3 files changed

Lines changed: 37 additions & 17 deletions

File tree

src/partial-agg.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ export function computePartialAgg(
186186
updatePartialAgg(states[i], val, val);
187187
} else if (typeof val === "bigint") {
188188
updatePartialAgg(states[i], Number(val), val);
189-
} else if (aggregates[i].fn === "count_distinct" && val !== null && val !== undefined) {
190-
updatePartialAgg(states[i], 0, val);
189+
} else if (val !== null && val !== undefined) {
190+
if (aggregates[i].fn === "count_distinct") {
191+
updatePartialAgg(states[i], 0, val);
192+
} else if (aggregates[i].fn === "count") {
193+
states[i].count++;
194+
}
191195
}
192196
}
193197
}
@@ -219,8 +223,12 @@ export function computePartialAggColumnar(
219223
updatePartialAgg(states[i], val, val);
220224
} else if (typeof val === "bigint") {
221225
updatePartialAgg(states[i], Number(val), val);
222-
} else if (aggregates[i].fn === "count_distinct" && val !== null && val !== undefined) {
223-
updatePartialAgg(states[i], 0, val);
226+
} else if (val !== null && val !== undefined) {
227+
if (aggregates[i].fn === "count_distinct") {
228+
updatePartialAgg(states[i], 0, val);
229+
} else if (aggregates[i].fn === "count") {
230+
states[i].count++;
231+
}
224232
}
225233
}
226234
}
@@ -257,8 +265,12 @@ export function computePartialAggColumnar(
257265
updatePartialAgg(states[i], val, val);
258266
} else if (typeof val === "bigint") {
259267
updatePartialAgg(states[i], Number(val), val);
260-
} else if (aggregates[i].fn === "count_distinct" && val !== null && val !== undefined) {
261-
updatePartialAgg(states[i], 0, val);
268+
} else if (val !== null && val !== undefined) {
269+
if (aggregates[i].fn === "count_distinct") {
270+
updatePartialAgg(states[i], 0, val);
271+
} else if (aggregates[i].fn === "count") {
272+
states[i].count++;
273+
}
262274
}
263275
}
264276
}

src/sql/evaluator.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,24 @@ function toNumber(val: unknown): number {
174174
}
175175

176176
/** Convert SQL LIKE pattern to case-insensitive match. % = any chars, _ = one char. */
177+
const _likeCache = new Map<string, RegExp>();
177178
function matchLike(value: string, pattern: string): boolean {
178-
let re = "^";
179-
for (let i = 0; i < pattern.length; i++) {
180-
const ch = pattern[i];
181-
if (ch === "%") re += ".*";
182-
else if (ch === "_") re += ".";
183-
else if (/[.*+?^${}()|[\]\\]/.test(ch)) re += "\\" + ch;
184-
else re += ch;
179+
let regex = _likeCache.get(pattern);
180+
if (!regex) {
181+
let re = "^";
182+
for (let i = 0; i < pattern.length; i++) {
183+
const ch = pattern[i];
184+
if (ch === "%") re += ".*";
185+
else if (ch === "_") re += ".";
186+
else if (/[.*+?^${}()|[\]\\]/.test(ch)) re += "\\" + ch;
187+
else re += ch;
188+
}
189+
re += "$";
190+
regex = new RegExp(re, "i");
191+
if (_likeCache.size > 64) _likeCache.clear();
192+
_likeCache.set(pattern, regex);
185193
}
186-
re += "$";
187-
return new RegExp(re, "i").test(value);
194+
return regex.test(value);
188195
}
189196

190197
function castValue(val: unknown, targetType: string): unknown {
@@ -228,5 +235,6 @@ export function rewriteAggregatesAsColumns(expr: SqlExpr): SqlExpr {
228235

229236
function isAggregate(name: string): boolean {
230237
return name === "COUNT" || name === "SUM" || name === "AVG" || name === "MIN" || name === "MAX" ||
231-
name === "COUNT_DISTINCT" || name === "STDDEV" || name === "VARIANCE" || name === "MEDIAN";
238+
name === "COUNT_DISTINCT" || name === "STDDEV" || name === "VARIANCE" || name === "MEDIAN" ||
239+
name === "PERCENTILE";
232240
}

src/sql/executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ function computeAgg(rows: Row[], agg: AggregateOp): number | bigint | string | b
196196
const v = row[agg.column];
197197
if (v === null || v === undefined) continue;
198198
const num = typeof v === "number" ? v : typeof v === "bigint" ? Number(v) : parseFloat(String(v));
199+
if (agg.fn === "count_distinct") seen.add(String(v));
199200
if (!isNaN(num)) {
200201
values.push(num);
201-
if (agg.fn === "count_distinct") seen.add(String(v));
202202
}
203203
}
204204

0 commit comments

Comments
 (0)