Skip to content

Commit aa7041b

Browse files
committed
add sample code for query tags with both session and statement level
1 parent 9f98112 commit aa7041b

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

examples/query_tags.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { DBSQLClient } = require('..');
2+
3+
const client = new DBSQLClient();
4+
5+
const host = process.env.DATABRICKS_HOST;
6+
const path = process.env.DATABRICKS_HTTP_PATH;
7+
const token = process.env.DATABRICKS_TOKEN;
8+
9+
client
10+
.connect({ host, path, token })
11+
.then(async (client) => {
12+
// Session-level query tags: applied to every statement run on this session
13+
// (serialized into the session's QUERY_TAGS configuration).
14+
const session = await client.openSession({
15+
queryTags: {
16+
team: 'engineering',
17+
env: 'dev',
18+
driver: 'node',
19+
},
20+
});
21+
22+
// Statement A: inherits session-level tags only.
23+
const opA = await session.executeStatement('SELECT 1 AS inherits_session_tags');
24+
console.log(await opA.fetchAll());
25+
await opA.close();
26+
27+
// Statement B: statement-level query tags via executeStatement options.
28+
// These are passed via confOverlay as "query_tags" and apply ONLY to this statement.
29+
// Note: `env` here overrides the session-level `env: 'dev'` — for this statement
30+
// it will be `env: 'prod'`. Subsequent statements without statement-level tags
31+
// revert to the session-level values.
32+
const opB = await session.executeStatement('SELECT 2 AS has_statement_tags', {
33+
queryTags: {
34+
env: 'prod',
35+
request_id: 'abc-123',
36+
feature: 'reporting',
37+
},
38+
});
39+
console.log(await opB.fetchAll());
40+
await opB.close();
41+
42+
// Statement C: demonstrates escaping of special characters (`\`, `:`, `,`)
43+
// in tag values, plus null/undefined values which serialize as bare keys.
44+
const opC = await session.executeStatement('SELECT 3 AS escaped_and_null_tags', {
45+
queryTags: {
46+
path: 'C:\\users\\me',
47+
note: 'hello, world',
48+
flag: null,
49+
},
50+
});
51+
console.log(await opC.fetchAll());
52+
await opC.close();
53+
54+
await session.close();
55+
await client.close();
56+
})
57+
.catch((error) => {
58+
console.log(error);
59+
});

0 commit comments

Comments
 (0)