Skip to content

Commit

Permalink
Add parametricDatetime option
Browse files Browse the repository at this point in the history
To opt-in to datetime types with variable precision. Closes #32.

Ref trinodb/trino#1284
  • Loading branch information
vweevers committed Sep 5, 2020
1 parent 7104d4a commit ab27af8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
43 changes: 42 additions & 1 deletion test/integration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const config = {
port: 8080,
user: 'test',
schema: 'test',
catalog: 'memory'
catalog: 'memory',
timezone: 'UTC'
}

const factory = (opts) => lento({ ...config, ...opts })
Expand Down Expand Up @@ -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' }])
})
})
})
})

0 comments on commit ab27af8

Please sign in to comment.