duckdb 0.9.2-dev2.0 → 0.9.2-dev22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/HighPriorityIssues.yml +36 -0
- package/.github/workflows/NodeJS.yml +18 -26
- package/README.md +2 -2
- package/lib/duckdb.d.ts +18 -0
- package/lib/duckdb.js +12 -0
- package/package.json +1 -1
- package/src/database.cpp +30 -1
- package/src/duckdb_node.cpp +29 -8
- package/src/duckdb_node.hpp +2 -0
- package/src/statement.cpp +9 -0
- package/test/test_all_types.test.ts +13 -12
- package/test/tokenize.test.ts +74 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
name: Create Internal issue when the "High Priority" label is applied
|
2
|
+
on:
|
3
|
+
issues:
|
4
|
+
types:
|
5
|
+
- labeled
|
6
|
+
|
7
|
+
env:
|
8
|
+
GH_TOKEN: ${{ secrets.DUCKDBLABS_BOT_TOKEN }}
|
9
|
+
# an event triggering this workflow is either an issue or a pull request,
|
10
|
+
# hence only one of the numbers will be filled in the TITLE_PREFIX
|
11
|
+
TITLE_PREFIX: "[duckdb-node/#${{ github.event.issue.number }}]"
|
12
|
+
PUBLIC_ISSUE_TITLE: ${{ github.event.issue.title }}
|
13
|
+
|
14
|
+
jobs:
|
15
|
+
create_or_label_issue:
|
16
|
+
if: github.event.label.name == 'High Priority'
|
17
|
+
runs-on: ubuntu-latest
|
18
|
+
steps:
|
19
|
+
- name: Get mirror issue number
|
20
|
+
run: |
|
21
|
+
gh issue list --repo duckdblabs/duckdb-internal --search "${TITLE_PREFIX}" --json title,number --jq ".[] | select(.title | startswith(\"$TITLE_PREFIX\")).number" > mirror_issue_number.txt
|
22
|
+
echo "MIRROR_ISSUE_NUMBER=$(cat mirror_issue_number.txt)" >> $GITHUB_ENV
|
23
|
+
|
24
|
+
- name: Print whether mirror issue exists
|
25
|
+
run: |
|
26
|
+
if [ "$MIRROR_ISSUE_NUMBER" == "" ]; then
|
27
|
+
echo "Mirror issue with title prefix '$TITLE_PREFIX' does not exist yet"
|
28
|
+
else
|
29
|
+
echo "Mirror issue with title prefix '$TITLE_PREFIX' exists with number $MIRROR_ISSUE_NUMBER"
|
30
|
+
fi
|
31
|
+
|
32
|
+
- name: Create or label issue
|
33
|
+
run: |
|
34
|
+
if [ "$MIRROR_ISSUE_NUMBER" == "" ]; then
|
35
|
+
gh issue create --repo duckdblabs/duckdb-internal --label "Node.js" --label "High Priority" --title "$TITLE_PREFIX - $PUBLIC_ISSUE_TITLE" --body "See https://github.com/duckdb/duckdb-node/issues/${{ github.event.issue.number }}"
|
36
|
+
fi
|
@@ -2,6 +2,8 @@ name: NodeJS
|
|
2
2
|
on:
|
3
3
|
push:
|
4
4
|
pull_request:
|
5
|
+
workflow_dispatch:
|
6
|
+
repository_dispatch:
|
5
7
|
|
6
8
|
concurrency:
|
7
9
|
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || '' }}-${{ github.base_ref || '' }}-${{ github.ref != 'refs/heads/main' || github.sha }}
|
@@ -34,55 +36,36 @@ jobs:
|
|
34
36
|
name: node.js Linux
|
35
37
|
runs-on: ubuntu-20.04
|
36
38
|
needs: set-up-npm
|
39
|
+
continue-on-error: ${{ matrix.node != '18' && matrix.node != '20' && matrix.node != '21' }}
|
37
40
|
env:
|
38
41
|
TARGET_ARCH: ${{ matrix.target_arch }}
|
39
42
|
DUCKDB_NODE_BUILD_CACHE: 0
|
40
43
|
strategy:
|
41
44
|
matrix:
|
42
45
|
# node.js current support policy to be found at https://github.com/duckdb/duckdb-node/tree/main/#Supported-Node-versions
|
43
|
-
node: [ '12', '14', '16', '17', '18', '19', '20' ]
|
46
|
+
node: [ '12', '14', '16', '17', '18', '19', '20', '21']
|
44
47
|
target_arch: [ x64, arm64 ]
|
45
48
|
isRelease:
|
46
49
|
- ${{ startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main' }}
|
47
50
|
exclude:
|
48
51
|
- isRelease: false
|
49
52
|
node: 12
|
50
|
-
target_arch: x64
|
51
53
|
- isRelease: false
|
52
54
|
node: 14
|
53
|
-
target_arch: x64
|
54
55
|
- isRelease: false
|
55
56
|
node: 16
|
56
|
-
target_arch: x64
|
57
57
|
- isRelease: false
|
58
58
|
node: 17
|
59
|
-
target_arch: x64
|
60
|
-
- isRelease: false
|
61
|
-
node: 18
|
62
|
-
target_arch: x64
|
63
59
|
- isRelease: false
|
64
60
|
node: 19
|
65
|
-
target_arch: x64
|
66
|
-
- isRelease: false
|
67
|
-
node: 12
|
68
|
-
target_arch: arm64
|
69
|
-
- isRelease: false
|
70
|
-
node: 14
|
71
|
-
target_arch: arm64
|
72
|
-
- isRelease: false
|
73
|
-
node: 16
|
74
|
-
target_arch: arm64
|
75
|
-
- isRelease: false
|
76
|
-
node: 17
|
77
|
-
target_arch: arm64
|
78
61
|
- isRelease: false
|
79
62
|
node: 18
|
80
63
|
target_arch: arm64
|
81
64
|
- isRelease: false
|
82
|
-
node:
|
65
|
+
node: 20
|
83
66
|
target_arch: arm64
|
84
67
|
- isRelease: false
|
85
|
-
node:
|
68
|
+
node: 21
|
86
69
|
target_arch: arm64
|
87
70
|
|
88
71
|
steps:
|
@@ -127,10 +110,11 @@ jobs:
|
|
127
110
|
name: node.js OSX
|
128
111
|
runs-on: macos-latest
|
129
112
|
needs: linux-nodejs
|
113
|
+
continue-on-error: ${{ matrix.node != '18' && matrix.node != '20' && matrix.node != '21' }}
|
130
114
|
strategy:
|
131
115
|
matrix:
|
132
116
|
target_arch: [ x64, arm64 ]
|
133
|
-
node: [ '12', '14', '16', '17', '18', '19', '20' ]
|
117
|
+
node: [ '12', '14', '16', '17', '18', '19', '20', '21']
|
134
118
|
isRelease:
|
135
119
|
- ${{ startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main' }}
|
136
120
|
exclude:
|
@@ -143,7 +127,7 @@ jobs:
|
|
143
127
|
- isRelease: false
|
144
128
|
node: 17
|
145
129
|
- isRelease: false
|
146
|
-
node:
|
130
|
+
node: 19
|
147
131
|
- target_arch: arm64
|
148
132
|
node: 12
|
149
133
|
- target_arch: arm64
|
@@ -158,6 +142,11 @@ jobs:
|
|
158
142
|
with:
|
159
143
|
fetch-depth: 0
|
160
144
|
|
145
|
+
# Default Python (3.12) doesn't have support for distutils
|
146
|
+
- uses: actions/setup-python@v4
|
147
|
+
with:
|
148
|
+
python-version: '3.11'
|
149
|
+
|
161
150
|
- name: Setup Ccache
|
162
151
|
uses: hendrikmuhs/ccache-action@main
|
163
152
|
with:
|
@@ -184,12 +173,13 @@ jobs:
|
|
184
173
|
name: node.js Windows
|
185
174
|
runs-on: windows-latest
|
186
175
|
needs: linux-nodejs
|
176
|
+
continue-on-error: ${{ matrix.node != '18' && matrix.node != '20' && matrix.node != '21' }}
|
187
177
|
env:
|
188
178
|
npm_config_msvs_version: 2019
|
189
179
|
|
190
180
|
strategy:
|
191
181
|
matrix:
|
192
|
-
node: [ '12', '14', '16', '17', '18', '19', '20' ]
|
182
|
+
node: [ '12', '14', '16', '17', '18', '19', '20', '21']
|
193
183
|
isRelease:
|
194
184
|
- ${{ startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main' }}
|
195
185
|
exclude:
|
@@ -205,6 +195,8 @@ jobs:
|
|
205
195
|
node: 18
|
206
196
|
- isRelease: false
|
207
197
|
node: 19
|
198
|
+
- isRelease: false
|
199
|
+
node: 20
|
208
200
|
|
209
201
|
steps:
|
210
202
|
- uses: actions/setup-python@v4
|
package/README.md
CHANGED
@@ -101,10 +101,10 @@ var stmt = con.prepare('select ?::INTEGER as fortytwo', function(err, stmt) {
|
|
101
101
|
```
|
102
102
|
|
103
103
|
## Supported Node versions
|
104
|
-
We actively support only LTS and In-Support Node versions, as per July 2023, they are: Node
|
104
|
+
We actively support only LTS and In-Support Node versions, as per July 2023, they are: Node 18, Node 20 and Node 21.
|
105
105
|
Release schedule for Node.js can be checked here: https://github.com/nodejs/release#release-schedule.
|
106
106
|
|
107
|
-
We currently bundle and test DuckDB also for Node 10, 12, 14, 17 and 19. We plan of going so going forward as long as the tooling supports it.
|
107
|
+
We currently bundle and test DuckDB also for Node 10, 12, 14, 16, 17 and 19. We plan of going so going forward as long as the tooling supports it.
|
108
108
|
As per July 2023, Node 15 has been removed from the supported versions.
|
109
109
|
|
110
110
|
## Development
|
package/lib/duckdb.d.ts
CHANGED
@@ -78,6 +78,8 @@ export type ArrowArray = Uint8Array[];
|
|
78
78
|
export class Connection {
|
79
79
|
constructor(db: Database, callback?: Callback<any>);
|
80
80
|
|
81
|
+
close(callback?: Callback<void>): void;
|
82
|
+
|
81
83
|
all(sql: string, ...args: [...any, Callback<TableData>] | []): void;
|
82
84
|
arrowIPCAll(sql: string, ...args: [...any, Callback<ArrowArray>] | []): void;
|
83
85
|
each(sql: string, ...args: [...any, Callback<RowData>] | []): void;
|
@@ -127,6 +129,20 @@ export type ReplacementScanCallback = (
|
|
127
129
|
table: string
|
128
130
|
) => ReplacementScanResult | null;
|
129
131
|
|
132
|
+
export enum TokenType {
|
133
|
+
IDENTIFIER = 0,
|
134
|
+
NUMERIC_CONSTANT = 1,
|
135
|
+
STRING_CONSTANT = 2,
|
136
|
+
OPERATOR = 3,
|
137
|
+
KEYWORD = 4,
|
138
|
+
COMMENT = 5,
|
139
|
+
}
|
140
|
+
|
141
|
+
export interface ScriptTokens {
|
142
|
+
offsets: number[];
|
143
|
+
types: TokenType[];
|
144
|
+
}
|
145
|
+
|
130
146
|
export class Database {
|
131
147
|
constructor(path: string, accessMode?: number | Record<string,string>, callback?: Callback<any>);
|
132
148
|
constructor(path: string, callback?: Callback<any>);
|
@@ -169,6 +185,8 @@ export class Database {
|
|
169
185
|
registerReplacementScan(
|
170
186
|
replacementScan: ReplacementScanCallback
|
171
187
|
): Promise<void>;
|
188
|
+
|
189
|
+
tokenize(text: string): ScriptTokens;
|
172
190
|
}
|
173
191
|
|
174
192
|
export type GenericTypeInfo = {
|
package/lib/duckdb.js
CHANGED
@@ -64,6 +64,10 @@ var Statement = duckdb.Statement;
|
|
64
64
|
* @class
|
65
65
|
*/
|
66
66
|
var QueryResult = duckdb.QueryResult;
|
67
|
+
/**
|
68
|
+
* Types of tokens return by `tokenize`.
|
69
|
+
*/
|
70
|
+
var TokenType = duckdb.TokenType;
|
67
71
|
|
68
72
|
/**
|
69
73
|
* @method
|
@@ -631,6 +635,14 @@ Database.prototype.unregister_udf = function () {
|
|
631
635
|
|
632
636
|
Database.prototype.registerReplacementScan;
|
633
637
|
|
638
|
+
/**
|
639
|
+
* Return positions and types of tokens in given text
|
640
|
+
* @method
|
641
|
+
* @arg text
|
642
|
+
* @return {ScriptTokens}
|
643
|
+
*/
|
644
|
+
Database.prototype.tokenize;
|
645
|
+
|
634
646
|
/**
|
635
647
|
* Not implemented
|
636
648
|
*/
|
package/package.json
CHANGED
package/src/database.cpp
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#include "duckdb/parser/expression/constant_expression.hpp"
|
2
2
|
#include "duckdb/parser/expression/function_expression.hpp"
|
3
|
+
#include "duckdb/parser/parser.hpp"
|
3
4
|
#include "duckdb/parser/tableref/table_function_ref.hpp"
|
4
5
|
#include "duckdb/storage/buffer_manager.hpp"
|
5
6
|
#include "duckdb_node.hpp"
|
@@ -18,7 +19,8 @@ Napi::FunctionReference Database::Init(Napi::Env env, Napi::Object exports) {
|
|
18
19
|
{InstanceMethod("close_internal", &Database::Close), InstanceMethod("wait", &Database::Wait),
|
19
20
|
InstanceMethod("serialize", &Database::Serialize), InstanceMethod("parallelize", &Database::Parallelize),
|
20
21
|
InstanceMethod("connect", &Database::Connect), InstanceMethod("interrupt", &Database::Interrupt),
|
21
|
-
InstanceMethod("registerReplacementScan", &Database::RegisterReplacementScan)
|
22
|
+
InstanceMethod("registerReplacementScan", &Database::RegisterReplacementScan),
|
23
|
+
InstanceMethod("tokenize", &Database::Tokenize)});
|
22
24
|
|
23
25
|
exports.Set("Database", t);
|
24
26
|
|
@@ -364,4 +366,31 @@ Napi::Value Database::RegisterReplacementScan(const Napi::CallbackInfo &info) {
|
|
364
366
|
return deferred.Promise();
|
365
367
|
}
|
366
368
|
|
369
|
+
Napi::Value Database::Tokenize(const Napi::CallbackInfo &info) {
|
370
|
+
auto env = info.Env();
|
371
|
+
|
372
|
+
if (info.Length() < 1) {
|
373
|
+
throw Napi::TypeError::New(env, "Text argument expected");
|
374
|
+
}
|
375
|
+
|
376
|
+
std::string text = info[0].As<Napi::String>();
|
377
|
+
|
378
|
+
auto tokens = duckdb::Parser::Tokenize(text);
|
379
|
+
auto numTokens = tokens.size();
|
380
|
+
|
381
|
+
auto offsets = Napi::Array::New(env, numTokens);
|
382
|
+
auto types = Napi::Array::New(env, numTokens);
|
383
|
+
|
384
|
+
for (size_t i = 0; i < numTokens; i++) {
|
385
|
+
auto token = tokens[i];
|
386
|
+
offsets.Set(i, token.start);
|
387
|
+
types.Set(i, (uint8_t)token.type);
|
388
|
+
}
|
389
|
+
|
390
|
+
auto result = Napi::Object::New(env);
|
391
|
+
result.Set("offsets", offsets);
|
392
|
+
result.Set("types", types);
|
393
|
+
return result;
|
394
|
+
}
|
395
|
+
|
367
396
|
} // namespace node_duckdb
|
package/src/duckdb_node.cpp
CHANGED
@@ -12,15 +12,36 @@ NodeDuckDB::NodeDuckDB(Napi::Env env, Napi::Object exports) {
|
|
12
12
|
statement_constructor = node_duckdb::Statement::Init(env, exports);
|
13
13
|
query_result_constructor = node_duckdb::QueryResult::Init(env, exports);
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
auto token_type_enum = Napi::Object::New(env);
|
16
|
+
|
17
|
+
token_type_enum.Set("IDENTIFIER", 0);
|
18
|
+
token_type_enum.Set("NUMERIC_CONSTANT", 1);
|
19
|
+
token_type_enum.Set("STRING_CONSTANT", 2);
|
20
|
+
token_type_enum.Set("OPERATOR", 3);
|
21
|
+
token_type_enum.Set("KEYWORD", 4);
|
22
|
+
token_type_enum.Set("COMMENT", 5);
|
23
|
+
|
24
|
+
// TypeScript enums expose an inverse mapping.
|
25
|
+
token_type_enum.Set((uint32_t)0, "IDENTIFIER");
|
26
|
+
token_type_enum.Set((uint32_t)1, "NUMERIC_CONSTANT");
|
27
|
+
token_type_enum.Set((uint32_t)2, "STRING_CONSTANT");
|
28
|
+
token_type_enum.Set((uint32_t)3, "OPERATOR");
|
29
|
+
token_type_enum.Set((uint32_t)4, "KEYWORD");
|
30
|
+
token_type_enum.Set((uint32_t)5, "COMMENT");
|
31
|
+
|
32
|
+
token_type_enum_ref = Napi::ObjectReference::New(token_type_enum);
|
33
|
+
|
34
|
+
exports.DefineProperties(
|
35
|
+
{DEFINE_CONSTANT_INTEGER(exports, node_duckdb::Database::DUCKDB_NODEJS_ERROR, ERROR) DEFINE_CONSTANT_INTEGER(
|
17
36
|
exports, node_duckdb::Database::DUCKDB_NODEJS_READONLY, OPEN_READONLY) // same as SQLite
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
37
|
+
DEFINE_CONSTANT_INTEGER(exports, 0, OPEN_READWRITE) // ignored
|
38
|
+
DEFINE_CONSTANT_INTEGER(exports, 0, OPEN_CREATE) // ignored
|
39
|
+
DEFINE_CONSTANT_INTEGER(exports, 0, OPEN_FULLMUTEX) // ignored
|
40
|
+
DEFINE_CONSTANT_INTEGER(exports, 0, OPEN_SHAREDCACHE) // ignored
|
41
|
+
DEFINE_CONSTANT_INTEGER(exports, 0, OPEN_PRIVATECACHE) // ignored
|
42
|
+
|
43
|
+
Napi::PropertyDescriptor::Value("TokenType", token_type_enum,
|
44
|
+
static_cast<napi_property_attributes>(napi_enumerable | napi_configurable))});
|
24
45
|
}
|
25
46
|
|
26
47
|
NODE_API_ADDON(NodeDuckDB);
|
package/src/duckdb_node.hpp
CHANGED
@@ -23,6 +23,7 @@ public:
|
|
23
23
|
Napi::FunctionReference connection_constructor;
|
24
24
|
Napi::FunctionReference statement_constructor;
|
25
25
|
Napi::FunctionReference query_result_constructor;
|
26
|
+
Napi::ObjectReference token_type_enum_ref;
|
26
27
|
};
|
27
28
|
|
28
29
|
namespace node_duckdb {
|
@@ -109,6 +110,7 @@ public:
|
|
109
110
|
Napi::Value Interrupt(const Napi::CallbackInfo &info);
|
110
111
|
Napi::Value Close(const Napi::CallbackInfo &info);
|
111
112
|
Napi::Value RegisterReplacementScan(const Napi::CallbackInfo &info);
|
113
|
+
Napi::Value Tokenize(const Napi::CallbackInfo &info);
|
112
114
|
|
113
115
|
public:
|
114
116
|
constexpr static int DUCKDB_NODEJS_ERROR = -1;
|
package/src/statement.cpp
CHANGED
@@ -187,6 +187,15 @@ static Napi::Value convert_col_val(Napi::Env &env, duckdb::Value dval, duckdb::L
|
|
187
187
|
const auto scale = duckdb::Interval::SECS_PER_DAY * duckdb::Interval::MSECS_PER_SEC;
|
188
188
|
value = Napi::Date::New(env, double(dval.GetValue<int32_t>() * scale));
|
189
189
|
} break;
|
190
|
+
case duckdb::LogicalTypeId::TIMESTAMP_NS: {
|
191
|
+
value = Napi::Date::New(env, double(dval.GetValue<int64_t>() / (duckdb::Interval::MICROS_PER_MSEC * 1000)));
|
192
|
+
} break;
|
193
|
+
case duckdb::LogicalTypeId::TIMESTAMP_MS: {
|
194
|
+
value = Napi::Date::New(env, double(dval.GetValue<int64_t>()));
|
195
|
+
} break;
|
196
|
+
case duckdb::LogicalTypeId::TIMESTAMP_SEC: {
|
197
|
+
value = Napi::Date::New(env, double(dval.GetValue<int64_t>() * duckdb::Interval::MSECS_PER_SEC));
|
198
|
+
} break;
|
190
199
|
case duckdb::LogicalTypeId::TIMESTAMP:
|
191
200
|
case duckdb::LogicalTypeId::TIMESTAMP_TZ: {
|
192
201
|
value = Napi::Date::New(env, double(dval.GetValue<int64_t>() / duckdb::Interval::MICROS_PER_MSEC));
|
@@ -22,10 +22,12 @@ function timedelta(obj: { days: number; micros: number; months: number }) {
|
|
22
22
|
const replacement_values: Record<string, string> = {
|
23
23
|
timestamp:
|
24
24
|
"'1990-01-01 00:00:00'::TIMESTAMP, '9999-12-31 23:59:59'::TIMESTAMP, NULL::TIMESTAMP",
|
25
|
-
|
26
|
-
|
27
|
-
//
|
28
|
-
|
25
|
+
timestamp_s:
|
26
|
+
"'1990-01-01 00:00:00'::TIMESTAMP_S, '9999-12-31 23:59:59'::TIMESTAMP_S, NULL::TIMESTAMP_S",
|
27
|
+
// note: timestamp_ns does not support extreme values
|
28
|
+
timestamp_ns: "'1990-01-01 00:00:00'::TIMESTAMP_NS, NULL::TIMESTAMP_NS",
|
29
|
+
timestamp_ms:
|
30
|
+
"'1990-01-01 00:00:00'::TIMESTAMP_MS, '9999-12-31 23:59:59'::TIMESTAMP_MS, NULL::TIMESTAMP_MS",
|
29
31
|
timestamp_tz:
|
30
32
|
"'1990-01-01 00:00:00Z'::TIMESTAMPTZ, '9999-12-31 23:59:59.999999Z'::TIMESTAMPTZ, NULL::TIMESTAMPTZ",
|
31
33
|
date: "'1990-01-01'::DATE, '9999-12-31'::DATE, NULL::DATE",
|
@@ -157,7 +159,7 @@ const correct_answer_map: Record<string, any[]> = {
|
|
157
159
|
null,
|
158
160
|
],
|
159
161
|
map: ["{}", "{key1=🦆🦆🦆🦆🦆🦆, key2=goose}", null],
|
160
|
-
union: [
|
162
|
+
union: ["Frank", "5", null],
|
161
163
|
|
162
164
|
time_tz: ["00:00:00-1559", "23:59:59.999999+1559", null],
|
163
165
|
interval: [
|
@@ -176,16 +178,15 @@ const correct_answer_map: Record<string, any[]> = {
|
|
176
178
|
null,
|
177
179
|
],
|
178
180
|
date: [new Date("1990-01-01"), new Date("9999-12-31"), null],
|
179
|
-
timestamp_s: [
|
180
|
-
|
181
|
-
|
182
|
-
"1677-09-21 00:12:43.145225",
|
183
|
-
"2262-04-11 23:47:16.854775",
|
181
|
+
timestamp_s: [
|
182
|
+
new Date(Date.UTC(1990, 0, 1)),
|
183
|
+
new Date("9999-12-31T23:59:59.000Z"),
|
184
184
|
null,
|
185
185
|
],
|
186
|
+
timestamp_ns: [new Date(Date.UTC(1990, 0, 1)), null],
|
186
187
|
timestamp_ms: [
|
187
|
-
|
188
|
-
"
|
188
|
+
new Date(Date.UTC(1990, 0, 1)),
|
189
|
+
new Date("9999-12-31T23:59:59.000Z"),
|
189
190
|
null,
|
190
191
|
],
|
191
192
|
timestamp_tz: [
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import * as assert from 'assert';
|
2
|
+
import * as duckdb from '..';
|
3
|
+
|
4
|
+
describe('tokenize', function () {
|
5
|
+
it('should return correct tokens for a single statement', function () {
|
6
|
+
const db = new duckdb.Database(':memory:');
|
7
|
+
const output = db.tokenize('select 1;');
|
8
|
+
assert.deepStrictEqual(output, {
|
9
|
+
offsets: [0, 7, 8],
|
10
|
+
types: [duckdb.TokenType.KEYWORD, duckdb.TokenType.NUMERIC_CONSTANT, duckdb.TokenType.OPERATOR]
|
11
|
+
});
|
12
|
+
});
|
13
|
+
it('should return correct tokens for a multiple statements', function () {
|
14
|
+
const db = new duckdb.Database(':memory:');
|
15
|
+
const output = db.tokenize('select 1; select 2;');
|
16
|
+
assert.deepStrictEqual(output, {
|
17
|
+
offsets: [0, 7, 8, 10, 17, 18],
|
18
|
+
types: [
|
19
|
+
duckdb.TokenType.KEYWORD, duckdb.TokenType.NUMERIC_CONSTANT, duckdb.TokenType.OPERATOR,
|
20
|
+
duckdb.TokenType.KEYWORD, duckdb.TokenType.NUMERIC_CONSTANT, duckdb.TokenType.OPERATOR
|
21
|
+
]
|
22
|
+
});
|
23
|
+
});
|
24
|
+
it('should return no tokens for an empty string', function () {
|
25
|
+
const db = new duckdb.Database(':memory:');
|
26
|
+
const output = db.tokenize('');
|
27
|
+
assert.deepStrictEqual(output, {
|
28
|
+
offsets: [],
|
29
|
+
types: []
|
30
|
+
});
|
31
|
+
});
|
32
|
+
it('should handle quoted semicolons in string constants', function () {
|
33
|
+
const db = new duckdb.Database(':memory:');
|
34
|
+
const output = db.tokenize(`select ';';`);
|
35
|
+
assert.deepStrictEqual(output, {
|
36
|
+
offsets: [0, 7, 10],
|
37
|
+
types: [duckdb.TokenType.KEYWORD, duckdb.TokenType.STRING_CONSTANT, duckdb.TokenType.OPERATOR]
|
38
|
+
});
|
39
|
+
});
|
40
|
+
it('should handle quoted semicolons in identifiers', function () {
|
41
|
+
const db = new duckdb.Database(':memory:');
|
42
|
+
const output = db.tokenize(`from ";";`);
|
43
|
+
assert.deepStrictEqual(output, {
|
44
|
+
offsets: [0, 5, 8],
|
45
|
+
types: [duckdb.TokenType.KEYWORD, duckdb.TokenType.IDENTIFIER, duckdb.TokenType.OPERATOR]
|
46
|
+
});
|
47
|
+
});
|
48
|
+
it('should handle comments', function () {
|
49
|
+
const db = new duckdb.Database(':memory:');
|
50
|
+
const output = db.tokenize(`select /* comment */ 1`);
|
51
|
+
// Note that the tokenizer doesn't return tokens for comments.
|
52
|
+
assert.deepStrictEqual(output, {
|
53
|
+
offsets: [0, 21],
|
54
|
+
types: [duckdb.TokenType.KEYWORD, duckdb.TokenType.NUMERIC_CONSTANT]
|
55
|
+
});
|
56
|
+
});
|
57
|
+
it('should handle invalid syntax', function () {
|
58
|
+
const db = new duckdb.Database(':memory:');
|
59
|
+
const output = db.tokenize(`selec 1`);
|
60
|
+
// The misspelled keyword is scanned as an identifier.
|
61
|
+
assert.deepStrictEqual(output, {
|
62
|
+
offsets: [0, 6],
|
63
|
+
types: [duckdb.TokenType.IDENTIFIER, duckdb.TokenType.NUMERIC_CONSTANT]
|
64
|
+
});
|
65
|
+
});
|
66
|
+
it('should support inverse TokenType mapping', function () {
|
67
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.IDENTIFIER], "IDENTIFIER");
|
68
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.NUMERIC_CONSTANT], "NUMERIC_CONSTANT");
|
69
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.STRING_CONSTANT], "STRING_CONSTANT");
|
70
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.OPERATOR], "OPERATOR");
|
71
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.KEYWORD], "KEYWORD");
|
72
|
+
assert.equal(duckdb.TokenType[duckdb.TokenType.COMMENT], "COMMENT");
|
73
|
+
});
|
74
|
+
});
|