Skip to content

Commit

Permalink
Merge pull request #1277 from evidence-dev/duckdb-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
archiewood authored Oct 18, 2023
2 parents aa165b2 + eabe54f commit f9a8490
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-nails-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@evidence-dev/duckdb': patch
---

upgrade duckdb to 0.9.1
Binary file modified needful_things.duckdb
Binary file not shown.
35 changes: 34 additions & 1 deletion packages/duckdb/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ function nativeTypeToEvidenceType(data) {
return 'number';
case 'string':
return 'string';
case 'bigint':
return 'bigint';
case 'boolean':
return 'boolean';
case 'object':
Expand All @@ -28,6 +30,35 @@ function nativeTypeToEvidenceType(data) {
}
}

/**
* Convert a BigInt value to a number.
* If the value isn't a BigInt, returns the value unchanged.
*
* @param {*} value - The value to potentially convert.
* @returns {*} - The converted number or the unchanged value.
*/
function convertBigIntToNumber(value) {
if (typeof value === 'bigint') {
return Number(value);
}
return value;
}

/**
* Normalize a list of row objects, converting any BigInt values to numbers.
*
* @param {Object[]} rawRows - The rows to process.
* @returns {Object[]} - The processed rows with BigInt values converted to numbers.
*/
function normalizeRows(rawRows) {
for (const row of rawRows) {
for (const key in row) {
row[key] = convertBigIntToNumber(row[key]);
}
}
return rawRows;
}

const mapResultsToEvidenceColumnTypes = function (rows) {
return Object.entries(rows[0]).map(([name, value]) => {
let typeFidelity = 'precise';
Expand All @@ -52,7 +83,9 @@ const runQuery = async (queryString, database) => {

try {
const db = await Database.create(filepath, mode);
const rows = await db.all(queryString);
const rawRows = await db.all(queryString); // renaming rows to rawRows for clarity
const rows = normalizeRows(rawRows);

return { rows, columnTypes: mapResultsToEvidenceColumnTypes(rows) };
} catch (err) {
if (err.message) {
Expand Down
2 changes: 1 addition & 1 deletion packages/duckdb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@evidence-dev/db-commons": "workspace:^",
"duckdb-async": "0.8.1"
"duckdb-async": "0.9.1"
},
"devDependencies": {
"dotenv": "^16.0.1"
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f9a8490

Please sign in to comment.