forked from databricks/databricks-sql-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_types.js
193 lines (179 loc) · 5.22 KB
/
data_types.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { DBSQLClient, utils } = require('../');
const client = new DBSQLClient();
const host = '****.databricks.com';
const path = '/sql/1.0/endpoints/****';
const token = 'dapi********************************';
client.connect({ host, path, token }).then(async (client) => {
try {
client.on('error', (error) => {
console.error(error);
});
const session = await client.openSession();
await testPrimitiveTypes(session);
await testComplexTypes(session);
await testIntervals(session);
const status = await session.close();
console.log(status.success());
await client.close();
} catch (error) {
console.error(error);
await client.close();
}
});
const testPrimitiveTypes = async (session) => {
try {
console.log('[info] create primitiveTypes');
await execute(
session,
`
CREATE TABLE IF NOT EXISTS primitiveTypes (
bool boolean,
tiny_int tinyint,
small_int smallint,
int_type int,
big_int bigint,
flt float,
dbl double,
dec decimal(3,2),
str string,
ts timestamp,
bin binary,
chr char(10),
vchr varchar(10),
dat date
)`,
);
console.log('[info] insert into primitiveTypes');
await execute(
session,
`INSERT INTO primitiveTypes (
bool,
tiny_int,
small_int,
int_type,
big_int,
flt,
dbl,
dec,
str,
ts,
bin,
chr,
vchr,
dat
) VALUES (
true,
127,
32000,
4000000,
372036854775807,
1.2,
2.2,
3.2,
'data',
'2014-01-17 00:17:13',
'data',
'a',
'b',
'2014-01-17'
)`,
);
console.log('[info] fetch primitiveTypes');
const result = await execute(session, 'SELECT * FROM primitiveTypes');
console.log(result);
} finally {
await execute(session, 'DROP TABLE primitiveTypes');
}
};
const testIntervals = async (session) => {
try {
console.log('[info] create intervalTypes');
await execute(
session,
`
CREATE TABLE IF NOT EXISTS intervalTypes AS
SELECT INTERVAL '1' day AS day_interval,
INTERVAL '1' month AS month_interval
`,
);
console.log('[info] describe intervalTypes');
console.log(await execute(session, `DESC intervalTypes`));
console.log('[info] fetch intervalTypes');
console.log(await execute(session, 'SELECT * FROM intervalTypes'));
} finally {
await execute(session, 'DROP TABLE intervalTypes');
}
};
const testComplexTypes = async (session) => {
try {
console.log('[info] create dummy');
await execute(session, `DROP TABLE IF EXISTS dummy`);
await execute(session, `CREATE TABLE dummy( id STRING )`);
console.log('[info] insert dummy value');
await execute(session, `INSERT INTO dummy (id) VALUES (1)`);
console.log('[info] create complexTypes');
await execute(
session,
`
CREATE TABLE complexTypes (
arr_type array<string>,
map_type map<string, int>,
struct_type struct<city:string,State:string>
)
`,
);
console.log('[info] 1. insert complexTypes');
await execute(
session,
`
INSERT INTO TABLE complexTypes SELECT
array('a', 'b') AS arr_type,
map('key', 12) AS map_type,
named_struct('city','Tampa','State','FL') AS struct_type
FROM dummy
`,
);
console.log('[info] 2. insert complexTypes');
await execute(
session,
`
INSERT INTO TABLE complexTypes SELECT
array('c', 'd') AS arr_type,
map('key2', 12) AS map_type,
named_struct('city','Albany','State','NY') AS struct_type
FROM dummy
`,
);
console.log('[info] 3. insert complexTypes');
await execute(
session,
`
INSERT INTO TABLE complexTypes SELECT
array('e', 'd') AS arr_type,
map('key2', 13) AS map_type,
named_struct('city','Los Angeles','State','CA') AS struct_type
FROM dummy
`,
);
console.log('[info] fetch complexTypes');
console.log(await execute(session, 'SELECT * FROM complexTypes'));
} finally {
await Promise.all([execute(session, 'DROP TABLE dummy'), execute(session, 'DROP TABLE complexTypes')]);
}
};
const execute = async (session, statement) => {
const operation = await session.executeStatement(statement, { runAsync: true });
const result = await operation.fetchAll({
progress: true,
callback: (stateResponse) => {
return;
if (stateResponse.taskStatus) {
console.log(stateResponse.taskStatus);
} else {
console.log(utils.formatProgress(stateResponse.progressUpdateResponse));
}
},
});
await operation.close();
return result;
};