@@ -70,7 +70,7 @@ SELECT js_create_scalar('function_name', 'function_code');
70
70
71
71
``` sql
72
72
-- Create a custom function to calculate age from birth date
73
- SELECT js_create_scalar(' age' , ' function(args) {
73
+ SELECT js_create_scalar(' age' , ' ( function(args) {
74
74
const birthDate = new Date(args[0]);
75
75
const today = new Date();
76
76
let age = today.getFullYear() - birthDate.getFullYear();
@@ -79,7 +79,7 @@ SELECT js_create_scalar('age', 'function(args) {
79
79
age--;
80
80
}
81
81
return age;
82
- }' );
82
+ }) ' );
83
83
84
84
-- Use the function
85
85
SELECT name, age(birth_date) FROM people;
@@ -111,20 +111,20 @@ SELECT js_create_aggregate('median',
111
111
' values = [];' ,
112
112
113
113
-- Step code: collect values from each row
114
- ' function(args) {
114
+ ' ( function(args) {
115
115
values.push(args[0]);
116
- }' ,
116
+ }) ' ,
117
117
118
118
-- Final code: calculate the median
119
- ' function() {
119
+ ' ( function() {
120
120
values.sort((a, b) => a - b);
121
121
const mid = Math.floor(values.length / 2);
122
122
if (values.length % 2 === 0) {
123
123
return (values[mid-1] + values[mid]) / 2;
124
124
} else {
125
125
return values[mid];
126
126
}
127
- }'
127
+ }) '
128
128
);
129
129
130
130
-- Use the function
@@ -159,24 +159,24 @@ SELECT js_create_window('moving_avg',
159
159
' sum = 0; count = 0;' ,
160
160
161
161
-- Step code: process each row
162
- ' function(args) {
162
+ ' ( function(args) {
163
163
sum += args[0];
164
164
count++;
165
- }' ,
165
+ }) ' ,
166
166
167
167
-- Final code: not needed for this example
168
- ' function() { }' ,
168
+ ' ( function() { }) ' ,
169
169
170
170
-- Value code: return current average
171
- ' function() {
171
+ ' ( function() {
172
172
return count > 0 ? sum / count : null;
173
- }' ,
173
+ }) ' ,
174
174
175
175
-- Inverse code: remove a value from the window
176
- ' function(args) {
176
+ ' ( function(args) {
177
177
sum -= args[0];
178
178
count--;
179
- }'
179
+ }) '
180
180
);
181
181
182
182
-- Use the function
@@ -203,7 +203,7 @@ SELECT js_create_collation('collation_name', 'collation_function');
203
203
204
204
``` sql
205
205
-- Create a case-insensitive natural sort collation
206
- SELECT js_create_collation(' natural_nocase' , ' function(a, b) {
206
+ SELECT js_create_collation(' natural_nocase' , ' ( function(a, b) {
207
207
// Extract numbers for natural comparison
208
208
const splitA = a.toLowerCase().split(/(\d +)/);
209
209
const splitB = b.toLowerCase().split(/(\d +)/);
@@ -217,7 +217,7 @@ SELECT js_create_collation('natural_nocase', 'function(a, b) {
217
217
}
218
218
}
219
219
return splitA.length - splitB.length;
220
- }' );
220
+ }) ' );
221
221
222
222
-- Use the collation
223
223
SELECT * FROM files ORDER BY name COLLATE natural_nocase;
@@ -263,10 +263,10 @@ SELECT js_eval('new Date(1629381600000).toLocaleDateString()');
263
263
264
264
``` sql
265
265
-- Create a function to extract domain from email
266
- SELECT js_create_scalar(' get_domain' , ' function(args) {
266
+ SELECT js_create_scalar(' get_domain' , ' ( function(args) {
267
267
const email = args[0];
268
268
return email.split("@")[1] || null;
269
- }' );
269
+ }) ' );
270
270
271
271
-- Use it in a query
272
272
SELECT email, get_domain(email) AS domain FROM users;
@@ -279,18 +279,18 @@ SELECT email, get_domain(email) AS domain FROM users;
279
279
SELECT js_create_aggregate(' stddev' ,
280
280
' sum = 0; sumSq = 0; count = 0;' ,
281
281
282
- ' function(args) {
282
+ ' ( function(args) {
283
283
const val = args[0];
284
284
sum += val;
285
285
sumSq += val * val;
286
286
count++;
287
- }' ,
287
+ }) ' ,
288
288
289
- ' function() {
289
+ ' ( function() {
290
290
if (count < 2) return null;
291
291
const variance = (sumSq - (sum * sum) / count) / (count - 1);
292
292
return Math.sqrt(variance);
293
- }'
293
+ }) '
294
294
);
295
295
296
296
-- Use it in a query
@@ -304,26 +304,26 @@ SELECT department, stddev(salary) FROM employees GROUP BY department;
304
304
SELECT js_create_window(' percentile_rank' ,
305
305
' values = [];' ,
306
306
307
- ' function(args) {
307
+ ' ( function(args) {
308
308
values.push(args[0]);
309
- }' ,
309
+ }) ' ,
310
310
311
- ' function() {
311
+ ' ( function() {
312
312
values.sort((a, b) => a - b);
313
- }' ,
313
+ }) ' ,
314
314
315
- ' function() {
315
+ ' ( function() {
316
316
const current = values[values.length - 1];
317
317
const rank = values.indexOf(current);
318
318
return (rank / (values.length - 1)) * 100;
319
- }' ,
319
+ }) ' ,
320
320
321
- ' function(args) {
321
+ ' ( function(args) {
322
322
const index = values.indexOf(args[0]);
323
323
if (index !== -1) {
324
324
values.splice(index, 1);
325
325
}
326
- }'
326
+ }) '
327
327
);
328
328
329
329
-- Use it in a query
0 commit comments