Skip to content

Commit

Permalink
SNOW-928823: Add rows vs streaming sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dprzybysz committed Oct 2, 2023
1 parent 2645782 commit 3502988
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
8 changes: 7 additions & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ npm run jsonParserComparison 300000 300000 Function
or
```
npm run jsonParserComparison 300000 300000 JSON
```
```

Run query reading all rows vs streaming the rows
----------------------------------------------------------------------

1. Set your query in [`rowsVsStreamingQuery.js`](./rowsVsStreamingQuery.js)`
2. Run `npm run rowsVsStreamingQuery`
14 changes: 13 additions & 1 deletion samples/helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const snowflake = require('snowflake-sdk');

exports.executeQuery = async function (connection, query, binds) {
await new Promise((resolve, reject) => {
connection.execute({
Expand Down Expand Up @@ -53,4 +54,15 @@ exports.connectUsingEnv = async () => {
}
);
});
};
};

exports.destroyAsync = connection => new Promise((resolve, reject) => {
connection.destroy(err => {
if (err) {
reject(err);
} else {
resolve();
}
}
);
});
5 changes: 3 additions & 2 deletions samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
"dependencies": {
"better-eval": "^1.3.0",
"blocked-at": "^1.2.0",
"snowflake-sdk": "^1.8.0",
"snowflake-sdk": "^1.9.0",
"vm": "^0.1.0"
},
"scripts": {
"jsonParserComparison": "node jsonParserComparison.js"
"jsonParserComparison": "node jsonParserComparison.js",
"rowsVsStreamingQuery": "node rowsVsStreamingQuery.js"
}
}
55 changes: 55 additions & 0 deletions samples/rowsVsStreamingQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* eslint-disable no-console */
const snowflake = require('snowflake-sdk');
const { connectUsingEnv, destroyAsync } = require('./helpers');

const executeQuery = (connection, query, binds = undefined) => new Promise((resolve, reject) => {
connection.execute({
sqlText: query,
binds: binds,
complete: function (err, stmt, rows) {
if (!err) {
resolve(rows);
} else {
reject(err);
}
}
});
});

const executeQueryStreaming = (connection, query, binds = undefined) => new Promise((resolve, reject) => {
const stmt = connection.execute({
sqlText: query,
binds: binds,
streamResult: true,
});
stmt.streamRows()
.on('error', err => reject(err))
.on('data', () => {})
.on('end', () => resolve());
});

async function runQueryReadingResultsFromRows(query){
const connection = await connectUsingEnv();
console.time('without streaming');
await executeQuery(connection, query);
console.timeEnd('without streaming');
await destroyAsync(connection);
}

async function runQueryReadingResultsFromStream(query){
const connection = await connectUsingEnv();
console.time('with streaming');
await executeQueryStreaming(connection, query);
console.timeEnd('with streaming');
await destroyAsync(connection);
}

async function main() {
const query = 'Select 1'; // Set your query here
snowflake.configure({ logLevel: 'ERROR' });
console.log(`Executing query: ${query}`);
await runQueryReadingResultsFromRows(query);
await runQueryReadingResultsFromStream(query);
}

main();

0 comments on commit 3502988

Please sign in to comment.