Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support struct/list load query options. #183

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Support struct/list load query options. (#182)
jheer committed Sep 21, 2023
commit 7898fac27a6e1f207503cff3e10869b44ef68dd7
32 changes: 25 additions & 7 deletions packages/sql/src/load/load.js
Original file line number Diff line number Diff line change
@@ -33,12 +33,30 @@ export function loadObjects(tableName, data, options = {}) {

function parameters(options) {
return Object.entries(options)
.map(([key, value]) => {
const t = typeof value;
const v = t === 'boolean' ? String(value)
: t === 'string' ? `'${value}'`
: value;
return `${key}=${v}`;
})
.map(([key, value]) => `${key}=${toDuckDBValue(value)}`)
.join(', ');
}

function toDuckDBValue(value) {
switch (typeof value) {
case 'boolean':
return String(value);
case 'string':
return `'${value}'`;
case 'undefined':
case 'object':
if (value == null) {
return 'NULL';
} else if (Array.isArray(value)) {
return '[' + value.map(v => toDuckDBValue(v)).join(', ') + ']';
} else {
return '{'
+ Object.entries(value)
.map(([k, v]) => `'${k}': ${toDuckDBValue(v)}`)
.join(', ')
+ '}';
}
default:
return value;
}
}
42 changes: 42 additions & 0 deletions packages/sql/test/load-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import assert from 'node:assert';
import { loadCSV } from '../src/index.js';

describe('loadCSV', () => {
it('accepts query options', () => {
const base = {
select: ['colA', 'colB'],
where: 'colX > 5'
};
assert.strictEqual(
loadCSV('table', 'data.csv', base),
`CREATE TEMP TABLE IF NOT EXISTS table AS SELECT colA, colB FROM read_csv('data.csv', auto_detect=true, sample_size=-1) WHERE colX > 5`
);

const ext = {
...base,
view: true,
temp: false,
replace: true
};
assert.strictEqual(
loadCSV('table', 'data.csv', ext),
`CREATE OR REPLACE VIEW table AS SELECT colA, colB FROM read_csv('data.csv', auto_detect=true, sample_size=-1) WHERE colX > 5`
);
});

it('accepts DuckDB options', () => {
const opt = {
auto_detect: false,
all_varchar: true,
columns: {line: 'VARCHAR'},
force_not_null: ['line'],
new_line: '\\n',
header: false,
skip: 2
};
assert.strictEqual(
loadCSV('table', 'data.csv', opt),
`CREATE TEMP TABLE IF NOT EXISTS table AS SELECT * FROM read_csv('data.csv', auto_detect=false, sample_size=-1, all_varchar=true, columns={'line': 'VARCHAR'}, force_not_null=['line'], new_line='\\n', header=false, skip=2)`
);
});
});