Skip to content

Commit ca2f5e6

Browse files
Nodejs183, crash when using full table path on array binding (#466)
* issue 183, fix failed on array binding using full path * add try catch on write file * add error handle on write file for array binding * update the connection string * add longer timeout for testArrayBind - full path * fix test case
1 parent 7c52609 commit ca2f5e6

File tree

3 files changed

+97
-17
lines changed

3 files changed

+97
-17
lines changed

lib/connection/bind_uploader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ function BindUploader(options, services, connectionConfig, requestId)
101101
}
102102

103103
var putStmt = "PUT file://" + fileName + "'" + stageName + "' overwrite=true auto_compress=false source_compression=gzip";
104-
fs.writeFileSync(fileName, data);
104+
try
105+
{
106+
fs.writeFileSync(fileName, data);
107+
}
108+
catch(e)
109+
{
110+
Logger.getInstance().debug('Failed to write file: %s', fileName);
111+
throw e;
112+
}
105113
this.files.push(fileName);
106114
this.datas.push(data);
107115
this.puts.push(putStmt);

lib/connection/statement.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,30 @@ exports.createStatementPreExec = function (
9191
}
9292
Logger.getInstance().debug('threshold = %d', threshold);
9393

94-
var isUsingBindUpload = false;
9594
// check array binding,
9695
if(numBinds > threshold)
9796
{
9897
var bindUploaderRequestId = uuidv4();
9998
var bind = new Bind.BindUploader(options, services, connectionConfig, bindUploaderRequestId);
100-
var bindData = bind.Upload(context.binds);
101-
99+
var bindData;
100+
try
101+
{
102+
bindData = bind.Upload(context.binds);
103+
}
104+
catch(e)
105+
{
106+
Logger.getInstance().debug('bind upload error, use normal binding');
107+
return createRowStatementPreExec(
108+
options, context, services, connectionConfig);
109+
}
102110
if(bindData != null)
103111
{
104-
isUsingBindUpload = true;
105-
context.bindStage = Bind.GetStageName(bindUploaderRequestId);
106-
Logger.getInstance().debug('context.bindStage = %s', context.bindStage);
107-
createStage(services, connectionConfig, bindData, options, context);
112+
context.bindStage = Bind.GetStageName(bindUploaderRequestId);
113+
Logger.getInstance().debug('context.bindStage = %s', context.bindStage);
114+
return createStage(services, connectionConfig, bindData, options, context);
108115
}
109116
}
110-
111-
if (!isUsingBindUpload)
117+
else
112118
{
113119
return createRowStatementPreExec(
114120
options, context, services, connectionConfig);
@@ -123,12 +129,21 @@ function createStage(services, connectionConfig, bindData, options, context)
123129
{
124130
Logger.getInstance().debug('stream');
125131
Logger.getInstance().debug('err '+err);
126-
var stream = stmt.streamRows();
127-
stream.on('data', function (rows)
132+
if(err)
128133
{
129-
Logger.getInstance().debug('stream on data');
130-
uploadFiles(services, connectionConfig, bindData, options, context);
131-
});
134+
context.bindStage = null;
135+
return createRowStatementPreExec(
136+
options, context, services, connectionConfig);
137+
}
138+
else
139+
{
140+
var stream = stmt.streamRows();
141+
stream.on('data', function (rows)
142+
{
143+
Logger.getInstance().debug('stream on data');
144+
return uploadFiles(services, connectionConfig, bindData, options, context);
145+
});
146+
}
132147
}
133148
}
134149
Logger.getInstance().debug('CREATE_STAGE_STMT = %s', Bind.GetCreateStageStmt());
@@ -144,8 +159,8 @@ function uploadFiles(services, connectionConfig, bindData, options, context, cur
144159
sqlText: bindData.puts[curIndex],
145160
complete: function (err, stmt, rows) {
146161
if (err) {
147-
Logger.getInstance().debug('err ' + err);
148-
throw err;
162+
return createRowStatementPreExec(
163+
options, context, services, connectionConfig);
149164
}
150165
Logger.getInstance().debug('uploadFiles done ');
151166
var stream = stmt.streamRows();

test/integration/testArrayBind.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,60 @@ describe('Test Array Bind', function ()
428428
);
429429
});
430430
});
431+
432+
describe('testArrayBind - full path', function ()
433+
{
434+
this.timeout(600000);
435+
var connection;
436+
var createABTable = `create or replace table ${DATABASE_NAME}.${SCHEMA_NAME}.testAB(colA string, colB number, colC date, colD time, colE TIMESTAMP_NTZ, colF TIMESTAMP_TZ)`;
437+
var insertAB = `insert into ${DATABASE_NAME}.${SCHEMA_NAME}.testAB values(?, ?, ?, ?, ?, ?)`;
438+
439+
before(function (done)
440+
{
441+
connection = snowflake.createConnection({
442+
accessUrl: connOption.valid.accessUrl,
443+
account: connOption.valid.account,
444+
username: connOption.valid.username,
445+
password: connOption.valid.password,
446+
warehouse: connOption.valid.warehouse,
447+
role: connOption.valid.role,
448+
arrayBindingThreshold: 3
449+
});
450+
testUtil.connect(connection, function ()
451+
{
452+
connection.execute({
453+
sqlText: createABTable,
454+
complete: function (err)
455+
{
456+
testUtil.checkError(err);
457+
done();
458+
}
459+
});
460+
});
461+
});
462+
463+
it('Full path array bind', function ()
464+
{
465+
var arrBind = [];
466+
var count = 100;
467+
for(var i = 0; i<count; i++)
468+
{
469+
arrBind.push([null, i, "2020-05-11", "12:35:41.3333333", "2022-04-01 23:59:59", "2022-07-08 12:05:30.9999999"]);
470+
}
471+
472+
var insertABStmt = connection.execute({
473+
sqlText: insertAB,
474+
binds: arrBind,
475+
complete: function (err, stmt) {
476+
testUtil.checkError(err);
477+
assert.strictEqual(stmt.getNumUpdatedRows(), count);
478+
callback();
479+
}
480+
});
481+
});
482+
after(function (done)
483+
{
484+
testUtil.destroyConnection(connection, done);
485+
});
486+
487+
});

0 commit comments

Comments
 (0)