Skip to content

Commit

Permalink
feat: use load methods from sql package in duckdb server, add createS…
Browse files Browse the repository at this point in the history
…chema method
  • Loading branch information
domoritz committed Oct 10, 2024
1 parent d0319d5 commit a2ca519
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/DataCubeIndexer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Query, and, asColumn, create, isBetween, scaleTransform, sql
Query, and, asColumn, createTable, isBetween, scaleTransform, sql
} from '@uwdata/mosaic-sql';
import { indexColumns } from './util/index-columns.js';
import { fnv_hash } from './util/hash.js';
Expand Down Expand Up @@ -185,7 +185,7 @@ export class DataCubeIndexer {
info = dataCubeInfo(client.query(filter), active, indexCols, schema);
info.result = mc.exec([
`CREATE SCHEMA IF NOT EXISTS ${schema}`,
create(info.table, info.create, { temp: false })
createTable(info.table, info.create, { temp: false })
]);
info.result.catch(e => mc.logger().error(e));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/duckdb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"duckdb": "^1.0.0",
"ws": "^8.18.0"
"ws": "^8.18.0",
"@uwdata/mosaic-sql": "^0.11.0"
}
}
6 changes: 0 additions & 6 deletions packages/duckdb/src/load/create-table.js

This file was deleted.

9 changes: 3 additions & 6 deletions packages/duckdb/src/load/csv.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { createTable } from './create-table.js';
import { parameters } from './parameters.js';
import { loadCSV as loadCSVSQL } from "@uwdata/mosaic-sql";

export function loadCSV(db, tableName, fileName, options = {}) {
const { select = ['*'], temp, replace, ...csvOptions } = options;
const params = parameters({ auto_detect: true, sample_size: -1, ...csvOptions });
const query = `SELECT ${select.join(', ')} FROM read_csv('${fileName}', ${params})`;
return createTable(db, tableName, query, { temp, replace });
const query = loadCSVSQL(tableName, fileName, options)
return db.exec(query)
}
9 changes: 3 additions & 6 deletions packages/duckdb/src/load/json.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { createTable } from './create-table.js';
import { parameters } from './parameters.js';
import { loadJSON as loadJSONSQL } from "@uwdata/mosaic-sql";

export function loadJSON(db, tableName, fileName, options = {}) {
const { select = ['*'], temp, replace, ...jsonOptions } = options;
const params = parameters({ auto_detect: true, ...jsonOptions });
const query = `SELECT ${select.join(', ')} FROM read_json('${fileName}', ${params})`;
return createTable(db, tableName, query, { temp, replace });
const query = loadJSONSQL(tableName, fileName, options)
return db.exec(query)
}
11 changes: 0 additions & 11 deletions packages/duckdb/src/load/parameters.js

This file was deleted.

7 changes: 3 additions & 4 deletions packages/duckdb/src/load/parquet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createTable } from './create-table.js';
import { loadParquet as loadParquetSQL } from "@uwdata/mosaic-sql";

export function loadParquet(db, tableName, fileName, options = {}) {
const { select = ['*'], ...tableOptions } = options;
const query = `SELECT ${select.join(', ')} FROM read_parquet('${fileName}')`;
return createTable(db, tableName, query, tableOptions);
const query = loadParquetSQL(tableName, fileName, options)
return db.exec(query)
}
6 changes: 3 additions & 3 deletions packages/spec/src/ast/DataNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { create } from '@uwdata/mosaic-sql';
import { createTable } from '@uwdata/mosaic-sql';
import { DATA } from '../constants.js';
import { isArray, isString } from '../util.js';
import { ASTNode } from './ASTNode.js';
Expand Down Expand Up @@ -162,7 +162,7 @@ export class TableDataNode extends QueryDataNode {
instantiateQuery(ctx) {
const { name, query, options } = this;
if (query) {
return ctx.api.create(name, query, options.instantiate(ctx));
return ctx.api.createTable(name, query, options.instantiate(ctx));
}
}

Expand All @@ -174,7 +174,7 @@ export class TableDataNode extends QueryDataNode {
codegenQuery(ctx) {
const { name, query, options } = this;
if (query) {
return `\`${create(name, query, options.instantiate(ctx))}\``;
return `\`${createTable(name, query, options.instantiate(ctx))}\``;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/sql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export {
scaleTransform
} from './scales.js';

export { create } from './load/create.js';
export { createTable, createSchema } from './load/create.js';
export { loadExtension } from './load/extension.js';
export {
loadCSV,
Expand Down
10 changes: 9 additions & 1 deletion packages/sql/src/load/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function create(name, query, {
export function createTable(name, query, {
replace = false,
temp = true,
view = false
Expand All @@ -10,3 +10,11 @@ export function create(name, query, {
+ (replace ? ' ' : ' IF NOT EXISTS ')
+ name + ' AS ' + query;
}

export function createSchema(name, {
strict = false
} = {}) {
return 'CREATE SCHEMA '
+ (strict ? '' : 'IF NOT EXISTS ')
+ name;
}
6 changes: 3 additions & 3 deletions packages/sql/src/load/load.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { create } from './create.js';
import { createTable } from './create.js';
import { sqlFrom } from './sql-from.js';

export function load(method, tableName, fileName, options = {}, defaults = {}) {
Expand All @@ -7,7 +7,7 @@ export function load(method, tableName, fileName, options = {}, defaults = {}) {
const read = `${method}('${fileName}'${params ? ', ' + params : ''})`;
const filter = where ? ` WHERE ${where}` : '';
const query = `SELECT ${select.join(', ')} FROM ${read}${filter}`;
return create(tableName, query, { view, temp, replace });
return createTable(tableName, query, { view, temp, replace });
}

export function loadCSV(tableName, fileName, options) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function loadObjects(tableName, data, options = {}) {
const query = select.length === 1 && select[0] === '*'
? values
: `SELECT ${select} FROM ${values}`;
return create(tableName, query, opt);
return createTable(tableName, query, opt);
}

function parameters(options) {
Expand Down
24 changes: 24 additions & 0 deletions packages/sql/test/create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, describe, it } from 'vitest';
import { createSchema, createTable } from '../src/load/create.js';

describe('createTable', () => {
it('creates a table', () => {
expect(createTable('table', 'SELECT 1')).toBe(

Check failure on line 6 in packages/sql/test/create.test.js

View workflow job for this annotation

GitHub Actions / Test in Node

test/create.test.js > createTable > creates a table

AssertionError: expected 'CREATE TEMP TABLE IF NOT EXISTS table…' to be 'CREATE TABLE IF NOT EXISTS table AS S…' // Object.is equality Expected: "CREATE TABLE IF NOT EXISTS table AS SELECT 1" Received: "CREATE TEMP TABLE IF NOT EXISTS table AS SELECT 1" ❯ test/create.test.js:6:46
`CREATE TABLE IF NOT EXISTS table AS SELECT 1`
);
});
});

describe('createSchema', () => {
it('creates a schema strict', () => {
expect(createSchema('s1', { strict: true })).toBe(
`CREATE SCHEMA s1`
);
});

it('creates a schema if it does not exist', () => {
expect(createSchema('s1')).toBe(
`CREATE SCHEMA IF NOT EXISTS s1`
);
});
});
2 changes: 1 addition & 1 deletion packages/vgplot/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export {
isDistinct, isNotDistinct,
isNull, isNotNull,
centroid, centroidX, centroidY, geojson,
create, loadExtension,
createTable, createSchema, loadExtension,
loadCSV, loadJSON, loadObjects, loadParquet, loadSpatial
} from '@uwdata/mosaic-sql';

Expand Down

0 comments on commit a2ca519

Please sign in to comment.