diff --git a/lib/client.js b/lib/client.js index 9ea5ab8..53a21f1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -36,6 +36,7 @@ module.exports = class Client extends EventEmitter { this.schema = opts.schema || null this.user = opts.user || null this.timezone = opts.timezone || null + this.parametricDatetime = opts.parametricDatetime || false this.pollInterval = msOption('pollInterval', opts.pollInterval || 1e3) this.socketTimeout = msOption('socketTimeout', opts.socketTimeout || 120e3) @@ -58,11 +59,13 @@ module.exports = class Client extends EventEmitter { const schema = opts.schema || this.schema const timezone = opts.timezone || this.timezone const user = opts.user || this.user + const parametricDatetime = opts.parametricDatetime || this.parametricDatetime // TODO: these may not be necessary for GET /v1/statement/{queryId}/{token} if (catalog) headers['X-Presto-Catalog'] = catalog if (schema) headers['X-Presto-Schema'] = schema if (timezone) headers['X-Presto-Time-Zone'] = timezone + if (parametricDatetime) headers['X-Presto-Client-Capabilities'] = 'PARAMETRIC_DATETIME' if (user) headers['X-Presto-User'] = user if (opts.json) headers['Accept'] = 'application/json' diff --git a/readme.md b/readme.md index 442048c..d6eaad4 100644 --- a/readme.md +++ b/readme.md @@ -87,6 +87,7 @@ Options: - `protocol`: string, one of `http:` (default) or `https:` - `user`: string, default none. Sent as `X-Presto-User` header. - `timezone`: string, for example `UTC`, default none. Sent as `X-Presto-Time-Zone` header. +- `parametricDatetime`: boolean, default false. Opt-in to datetime types with variable precision, for example `timestamp(6)`. When not set, datetime types are returned with a precision of 3. You can specify a [catalog](https://prestosql.io/docs/current/overview/concepts.html#catalog) and [schema](https://prestosql.io/docs/current/overview/concepts.html#schema) to avoid writing fully-qualified table names in queries: diff --git a/test/integration/index.js b/test/integration/index.js index cd8bcf7..62379f5 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -9,7 +9,8 @@ const config = { port: 8080, user: 'test', schema: 'test', - catalog: 'memory' + catalog: 'memory', + timezone: 'UTC' } const factory = (opts) => lento({ ...config, ...opts }) @@ -40,3 +41,43 @@ test('basic', function (t) { }) }) }) + +test('timestamp is returned with default precision of 3', function (t) { + t.plan(4) + + const client = factory() + const table = 'timestamp_' + Date.now() + + client.query(`CREATE TABLE ${table} (ts timestamp(6))`, (err) => { + t.ifError(err, 'no query error') + + client.query(`INSERT INTO ${table} VALUES (timestamp '2020-09-05 10:01:48.123456')`, (err) => { + t.ifError(err, 'no query error') + + client.query(`SELECT * FROM ${table}`, { deserialize: false }, (err, rows) => { + t.ifError(err, 'no query error') + t.same(rows, [{ ts: '2020-09-05 10:01:48.123' }]) + }) + }) + }) +}) + +test('enable PARAMETRIC_DATETIME capability', function (t) { + t.plan(4) + + const client = factory({ parametricDatetime: true }) + const table = 'parametric_datetime_' + Date.now() + + client.query(`CREATE TABLE ${table} (ts timestamp(6))`, (err) => { + t.ifError(err, 'no query error') + + client.query(`INSERT INTO ${table} VALUES (timestamp '2020-09-05 10:01:48.123456')`, (err) => { + t.ifError(err, 'no query error') + + client.query(`SELECT * FROM ${table}`, { deserialize: false }, (err, rows) => { + t.ifError(err, 'no query error') + t.same(rows, [{ ts: '2020-09-05 10:01:48.123456' }]) + }) + }) + }) +})