Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-928823: Add rows vs streaming sample #657

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();