-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathqueryExecuter.js
423 lines (359 loc) · 16.3 KB
/
queryExecuter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const { parseSelectQuery, parseInsertQuery, parseDeleteQuery } = require('./queryParser.js');
const { readCSV, readCSVForHLL, writeCSV } = require('./csvStorage.js');
const hll = require('hll');
function performInnerJoin(data, joinData, joinCondition, fields, table) {
return data.flatMap(mainRow => {
return joinData
.filter(joinRow => {
const mainValue = mainRow[joinCondition.left.split('.')[1]];
const joinValue = joinRow[joinCondition.right.split('.')[1]];
return mainValue === joinValue;
})
.map(joinRow => {
return fields.reduce((acc, field) => {
const [tableName, fieldName] = field.split('.');
acc[field] = tableName === table ? mainRow[fieldName] : joinRow[fieldName];
return acc;
}, {});
});
});
}
function performLeftJoin(data, joinData, joinCondition, fields, table) {
return data.flatMap(mainRow => {
const matchingJoinRows = joinData.filter(joinRow => {
const mainValue = getValueFromRow(mainRow, joinCondition.left);
const joinValue = getValueFromRow(joinRow, joinCondition.right);
return mainValue === joinValue;
});
if (matchingJoinRows.length === 0) {
return [createResultRow(mainRow, null, fields, table, true)];
}
return matchingJoinRows.map(joinRow => createResultRow(mainRow, joinRow, fields, table, true));
});
}
function getValueFromRow(row, compoundFieldName) {
const [tableName, fieldName] = compoundFieldName.split('.');
return row[`${tableName}.${fieldName}`] || row[fieldName];
}
function performRightJoin(data, joinData, joinCondition, fields, table) {
// Cache the structure of a main table row (keys only)
const mainTableRowStructure = data.length > 0 ? Object.keys(data[0]).reduce((acc, key) => {
acc[key] = null; // Set all values to null initially
return acc;
}, {}) : {};
return joinData.map(joinRow => {
const mainRowMatch = data.find(mainRow => {
const mainValue = getValueFromRow(mainRow, joinCondition.left);
const joinValue = getValueFromRow(joinRow, joinCondition.right);
return mainValue === joinValue;
});
// Use the cached structure if no match is found
const mainRowToUse = mainRowMatch || mainTableRowStructure;
// Include all necessary fields from the 'student' table
return createResultRow(mainRowToUse, joinRow, fields, table, true);
});
}
function createResultRow(mainRow, joinRow, fields, table, includeAllMainFields) {
const resultRow = {};
if (includeAllMainFields) {
// Include all fields from the main table
Object.keys(mainRow || {}).forEach(key => {
const prefixedKey = `${table}.${key}`;
resultRow[prefixedKey] = mainRow ? mainRow[key] : null;
});
}
// Now, add or overwrite with the fields specified in the query
fields.forEach(field => {
const [tableName, fieldName] = field.includes('.') ? field.split('.') : [table, field];
resultRow[field] = tableName === table && mainRow ? mainRow[fieldName] : joinRow ? joinRow[fieldName] : null;
});
return resultRow;
}
function evaluateCondition(row, clause) {
let { field, operator, value } = clause;
// Check if the field exists in the row
if (row[field] === undefined) {
throw new Error(`Invalid field: ${field}`);
}
// Parse row value and condition value based on their actual types
const rowValue = parseValue(row[field]);
let conditionValue = parseValue(value);
if (operator === 'LIKE') {
// Transform SQL LIKE pattern to JavaScript RegExp pattern
const regexPattern = '^' + value.replace(/%/g, '.*').replace(/_/g, '.') + '$';
const regex = new RegExp(regexPattern, 'i'); // 'i' for case-insensitive matching
return regex.test(row[field]);
}
switch (operator) {
case '=': return rowValue === conditionValue;
case '!=': return rowValue !== conditionValue;
case '>': return rowValue > conditionValue;
case '<': return rowValue < conditionValue;
case '>=': return rowValue >= conditionValue;
case '<=': return rowValue <= conditionValue;
default: throw new Error(`Unsupported operator: ${operator}`);
}
}
// Helper function to parse value based on its apparent type
function parseValue(value) {
// Return null or undefined as is
if (value === null || value === undefined) {
return value;
}
// If the value is a string enclosed in single or double quotes, remove them
if (typeof value === 'string' && ((value.startsWith("'") && value.endsWith("'")) || (value.startsWith('"') && value.endsWith('"')))) {
value = value.substring(1, value.length - 1);
}
// Check if value is a number
if (!isNaN(value) && value.trim() !== '') {
return Number(value);
}
// Assume value is a string if not a number
return value;
}
function applyGroupBy(data, groupByFields, aggregateFunctions) {
const groupResults = {};
data.forEach(row => {
// Generate a key for the group
const groupKey = groupByFields.map(field => row[field]).join('-');
// Initialize group in results if it doesn't exist
if (!groupResults[groupKey]) {
groupResults[groupKey] = { count: 0, sums: {}, mins: {}, maxes: {} };
groupByFields.forEach(field => groupResults[groupKey][field] = row[field]);
}
// Aggregate calculations
groupResults[groupKey].count += 1;
aggregateFunctions.forEach(func => {
const match = /(\w+)\((\w+)\)/.exec(func);
if (match) {
const [, aggFunc, aggField] = match;
const value = parseFloat(row[aggField]);
switch (aggFunc.toUpperCase()) {
case 'SUM':
groupResults[groupKey].sums[aggField] = (groupResults[groupKey].sums[aggField] || 0) + value;
break;
case 'MIN':
groupResults[groupKey].mins[aggField] = Math.min(groupResults[groupKey].mins[aggField] || value, value);
break;
case 'MAX':
groupResults[groupKey].maxes[aggField] = Math.max(groupResults[groupKey].maxes[aggField] || value, value);
break;
// Additional aggregate functions can be added here
}
}
});
});
// Convert grouped results into an array format
return Object.values(groupResults).map(group => {
// Construct the final grouped object based on required fields
const finalGroup = {};
groupByFields.forEach(field => finalGroup[field] = group[field]);
aggregateFunctions.forEach(func => {
const match = /(\w+)\((\*|\w+)\)/.exec(func);
if (match) {
const [, aggFunc, aggField] = match;
switch (aggFunc.toUpperCase()) {
case 'SUM':
finalGroup[func] = group.sums[aggField];
break;
case 'MIN':
finalGroup[func] = group.mins[aggField];
break;
case 'MAX':
finalGroup[func] = group.maxes[aggField];
break;
case 'COUNT':
finalGroup[func] = group.count;
break;
// Additional aggregate functions can be handled here
}
}
});
return finalGroup;
});
}
async function executeSELECTQuery(query) {
try {
const { fields, table, whereClauses, joinType, joinTable, joinCondition, groupByFields, hasAggregateWithoutGroupBy, isApproximateCount, orderByFields, limit, isDistinct, distinctFields, isCountDistinct } = parseSelectQuery(query);
if (isApproximateCount && fields.length === 1 && fields[0] === 'COUNT(*)' && whereClauses.length === 0) {
let hll = await readCSVForHLL(`${table}.csv`);
return [{ 'APPROXIMATE_COUNT(*)': hll.estimate() }];
}
let data = await readCSV(`${table}.csv`);
// Perform INNER JOIN if specified
if (joinTable && joinCondition) {
const joinData = await readCSV(`${joinTable}.csv`);
switch (joinType.toUpperCase()) {
case 'INNER':
data = performInnerJoin(data, joinData, joinCondition, fields, table);
break;
case 'LEFT':
data = performLeftJoin(data, joinData, joinCondition, fields, table);
break;
case 'RIGHT':
data = performRightJoin(data, joinData, joinCondition, fields, table);
break;
default:
throw new Error(`Unsupported JOIN type: ${joinType}`);
}
}
// Apply WHERE clause filtering after JOIN (or on the original data if no join)
let filteredData = whereClauses.length > 0
? data.filter(row => whereClauses.every(clause => evaluateCondition(row, clause)))
: data;
let groupResults = filteredData;
if (hasAggregateWithoutGroupBy) {
// Special handling for queries like 'SELECT COUNT(*) FROM table'
const result = {};
fields.forEach(field => {
const match = /(\w+)\((\*|\w+)\)/.exec(field);
if (match) {
const [, aggFunc, aggField] = match;
switch (aggFunc.toUpperCase()) {
case 'COUNT':
result[field] = filteredData.length;
break;
case 'SUM':
result[field] = filteredData.reduce((acc, row) => acc + parseFloat(row[aggField]), 0);
break;
case 'AVG':
result[field] = filteredData.reduce((acc, row) => acc + parseFloat(row[aggField]), 0) / filteredData.length;
break;
case 'MIN':
result[field] = Math.min(...filteredData.map(row => parseFloat(row[aggField])));
break;
case 'MAX':
result[field] = Math.max(...filteredData.map(row => parseFloat(row[aggField])));
break;
// Additional aggregate functions can be handled here
}
}
});
return [result];
// Add more cases here if needed for other aggregates
} else if (groupByFields) {
groupResults = applyGroupBy(filteredData, groupByFields, fields);
// Order them by the specified fields
let orderedResults = groupResults;
if (orderByFields) {
orderedResults = groupResults.sort((a, b) => {
for (let { fieldName, order } of orderByFields) {
if (a[fieldName] < b[fieldName]) return order === 'ASC' ? -1 : 1;
if (a[fieldName] > b[fieldName]) return order === 'ASC' ? 1 : -1;
}
return 0;
});
}
if (limit !== null) {
groupResults = groupResults.slice(0, limit);
}
return groupResults;
} else {
// Order them by the specified fields
let orderedResults = groupResults;
if (orderByFields) {
orderedResults = groupResults.sort((a, b) => {
for (let { fieldName, order } of orderByFields) {
if (a[fieldName] < b[fieldName]) return order === 'ASC' ? -1 : 1;
if (a[fieldName] > b[fieldName]) return order === 'ASC' ? 1 : -1;
}
return 0;
});
}
// Distinct inside count - example "SELECT COUNT (DISTINCT student.name) FROM student"
if (isCountDistinct) {
if (isApproximateCount) {
var h = hll({ bitSampleSize: 12, digestSize: 128 });
orderedResults.forEach(row => h.insert(distinctFields.map(field => row[field]).join('|')));
return [{ [`APPROXIMATE_${fields[0]}`]: h.estimate() }];
}
else {
let distinctResults = [...new Map(orderedResults.map(item => [distinctFields.map(field => item[field]).join('|'), item])).values()];
return [{ [fields[0]]: distinctResults.length }];
}
}
// Select the specified fields
let finalResults = orderedResults.map(row => {
const selectedRow = {};
fields.forEach(field => {
// Assuming 'field' is just the column name without table prefix
selectedRow[field] = row[field];
});
return selectedRow;
});
// console.log("CP-2", orderedResults)
// Remove duplicates if specified
let distinctResults = finalResults;
if (isDistinct) {
distinctResults = [...new Map(finalResults.map(item => [fields.map(field => item[field]).join('|'), item])).values()];
}
let limitResults = distinctResults;
if (limit !== null) {
limitResults = distinctResults.slice(0, limit);
}
return limitResults;
}
} catch (error) {
throw new Error(`Error executing query: ${error.message}`);
}
}
async function executeINSERTQuery(query) {
const { table, columns, values, returningColumns } = parseInsertQuery(query);
const data = await readCSV(`${table}.csv`);
// Check if 'id' column is included in the query and in CSV headers
let newId = null;
if (!columns.includes('id') && data.length > 0 && 'id' in data[0]) {
// 'id' column not included in the query, so we auto-generate an ID
const existingIds = data.map(row => parseInt(row.id)).filter(id => !isNaN(id));
const maxId = existingIds.length > 0 ? Math.max(...existingIds) : 0;
newId = maxId + 1;
columns.push('id');
values.push(newId.toString()); // Add as a string
}
// Create a new row object matching the CSV structure
const headers = data.length > 0 ? Object.keys(data[0]) : columns;
const newRow = {};
headers.forEach(header => {
const columnIndex = columns.indexOf(header);
if (columnIndex !== -1) {
let value = values[columnIndex];
if (value.startsWith("'") && value.endsWith("'")) {
value = value.substring(1, value.length - 1);
}
newRow[header] = value;
} else {
newRow[header] = header === 'id' ? newId.toString() : '';
}
});
// Add the new row to the data
data.push(newRow);
// Save the updated data back to the CSV file
await writeCSV(`${table}.csv`, data);
// Prepare the returning result if returningColumns are specified
let returningResult = {};
if (returningColumns.length > 0) {
returningColumns.forEach(column => {
returningResult[column] = newRow[column];
});
}
return {
message: "Row inserted successfully.",
insertedId: newId,
returning: returningResult
};
}
async function executeDELETEQuery(query) {
const { table, whereClauses } = parseDeleteQuery(query);
let data = await readCSV(`${table}.csv`);
if (whereClauses.length > 0) {
// Filter out the rows that meet the where clause conditions
data = data.filter(row => !whereClauses.every(clause => evaluateCondition(row, clause)));
} else {
// If no where clause, clear the entire table
data = [];
}
// Save the updated data back to the CSV file
await writeCSV(`${table}.csv`, data);
return { message: "Rows deleted successfully." };
}
module.exports = { executeSELECTQuery, executeINSERTQuery, executeDELETEQuery };