Skip to content

Commit dced932

Browse files
committed
feat: normalize decimal column types in bind parameters and update related tests
1 parent c042c87 commit dced932

3 files changed

Lines changed: 60 additions & 64 deletions

File tree

nodejs/src/stmt/wsParams2.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,25 @@ export class Stmt2BindParams extends StmtBindParams implements IDataEncoder {
3434
if (this._fieldParams) {
3535
if (this.paramsCount > 0) {
3636
if (this._fieldParams[this.paramIndex]) {
37-
if (
38-
this._fieldParams[this.paramIndex].dataType !== dataType ||
39-
this._fieldParams[this.paramIndex].columnType !== columnType
40-
) {
37+
const currentFieldParam = this._fieldParams[this.paramIndex];
38+
const columnTypeMatches =
39+
currentFieldParam.columnType === columnType ||
40+
(this.isDecimalColumnType(currentFieldParam.columnType) &&
41+
this.isDecimalColumnType(columnType));
42+
if (currentFieldParam.dataType !== dataType || !columnTypeMatches) {
4143
throw new TaosError(
4244
ErrorCode.ERR_INVALID_PARAMS,
4345
`StmtBindParams params type is not match! ${this.paramIndex
4446
} ${this.paramsCount} ${JSONBig.stringify({
4547
dataType,
4648
columnType,
4749
})} vs ${JSONBig.stringify({
48-
dataType: this._fieldParams[this.paramIndex].dataType,
49-
columnType: this._fieldParams[this.paramIndex].columnType,
50+
dataType: currentFieldParam.dataType,
51+
columnType: currentFieldParam.columnType,
5052
})}`
5153
);
5254
}
53-
this._fieldParams[this.paramIndex].params.push(...params);
55+
currentFieldParam.params.push(...params);
5456
} else {
5557
let bindType = this._fields[this.paramIndex].bind_type || 0;
5658
this._fieldParams[this.paramIndex] = new FieldBindParams(
@@ -80,6 +82,13 @@ export class Stmt2BindParams extends StmtBindParams implements IDataEncoder {
8082
}
8183
}
8284

85+
private isDecimalColumnType(columnType: number): boolean {
86+
return (
87+
columnType === TDengineTypeCode.DECIMAL ||
88+
columnType === TDengineTypeCode.DECIMAL64
89+
);
90+
}
91+
8392
mergeParams(bindParams: StmtBindParams): void {
8493
if (
8594
!bindParams ||

nodejs/src/stmt/wsStmt2.ts

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -281,65 +281,31 @@ export class WsStmt2 implements WsStmt {
281281
const colFields = this.fields.filter(
282282
(f) => f.bind_type === FieldBindType.TAOS_FIELD_COL
283283
);
284-
await this._currentTableInfo.setParams(
285-
this.createInsertBindParamsWithNormalizedDecimalType(
286-
paramsArray,
287-
colFields
288-
)
289-
);
290-
} else {
291-
await this._currentTableInfo.setParams(paramsArray);
284+
for (let i = 0; i < paramsArray._fieldParams.length; i++) {
285+
const fieldParam = paramsArray._fieldParams[i];
286+
if (!fieldParam) {
287+
continue;
288+
}
289+
fieldParam.columnType = this.resolveDecimalColumnType(
290+
fieldParam.columnType,
291+
colFields[i]?.field_type
292+
);
293+
}
292294
}
295+
await this._currentTableInfo.setParams(paramsArray);
293296
}
294297

295298
return Promise.resolve();
296299
}
297300

298-
private createInsertBindParamsWithNormalizedDecimalType(
299-
paramsArray: StmtBindParams,
300-
colFields: Array<StmtFieldInfo>
301-
): Stmt2BindParams {
302-
const normalizedParams = new Stmt2BindParams(
303-
colFields.length,
304-
this._precision,
305-
colFields
306-
);
307-
if (!paramsArray._fieldParams) {
308-
return normalizedParams;
309-
}
310-
311-
const sourceFieldParams = paramsArray._fieldParams;
312-
for (let i = 0; i < sourceFieldParams.length; i++) {
313-
const fieldParam = sourceFieldParams[i];
314-
if (!fieldParam) {
315-
continue;
316-
}
317-
318-
normalizedParams.addParams(
319-
fieldParam.params,
320-
fieldParam.dataType,
321-
fieldParam.typeLen,
322-
this.resolveDecimalColumnType(
323-
fieldParam.columnType,
324-
colFields[i]?.field_type
325-
)
326-
);
327-
}
328-
329-
return normalizedParams;
330-
}
331-
332301
private resolveDecimalColumnType(
333302
columnType: number,
334303
fieldType: number | undefined | null
335304
): number {
336305
if (columnType !== TDengineTypeCode.DECIMAL) {
337306
return columnType;
338307
}
339-
if (
340-
fieldType === TDengineTypeCode.DECIMAL ||
341-
fieldType === TDengineTypeCode.DECIMAL64
342-
) {
308+
if (fieldType === TDengineTypeCode.DECIMAL64) {
343309
return fieldType;
344310
}
345311
return columnType;

nodejs/test/stmt/stmt2.decimal.test.ts

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ describe("Stmt2 decimal bind behavior (mock)", () => {
290290
);
291291
});
292292

293-
test("bind should not mutate caller params so full-binding params can be reused", async () => {
293+
test("bind should normalize full-binding decimal params in place", async () => {
294294
const fields = createInsertFields();
295295
const stmt = createBareStmt(fields, true);
296296
const params = new Stmt2BindParams(fields.length, 13, fields);
@@ -303,17 +303,11 @@ describe("Stmt2 decimal bind behavior (mock)", () => {
303303

304304
await stmt.bind(params);
305305

306-
expect(params._fieldParams?.[3].columnType).toBe(TDengineTypeCode.DECIMAL);
306+
expect(params._fieldParams?.[3].columnType).toBe(TDengineTypeCode.DECIMAL64);
307307

308-
params.setVarchar(["d0"]);
309-
params.setVarchar(["beijing"]);
310-
params.setInt([1]);
311-
expect(() => {
312-
params.setDecimal(["223.456700"]);
313-
}).not.toThrow();
314-
params.setInt([11]);
315-
316-
expect(params._fieldParams?.[3].params.length).toBe(2);
308+
const tableInfo = stmt._stmtTableInfo.get("d0");
309+
const colParams = tableInfo?.getParams();
310+
expect(colParams?._fieldParams?.[0].columnType).toBe(TDengineTypeCode.DECIMAL64);
317311
});
318312

319313
test("bind should override DECIMAL to real column decimal type in non-full-binding insert", async () => {
@@ -327,6 +321,7 @@ describe("Stmt2 decimal bind behavior (mock)", () => {
327321
await stmt.bind(params);
328322

329323
const bindParams = stmt._currentTableInfo.getParams();
324+
expect(bindParams).toBe(params);
330325
expect(bindParams?._fieldParams?.[0].columnType).toBe(
331326
TDengineTypeCode.DECIMAL64
332327
);
@@ -335,6 +330,32 @@ describe("Stmt2 decimal bind behavior (mock)", () => {
335330
);
336331
});
337332

333+
test("bind should still allow setDecimal after in-place normalization", async () => {
334+
const fields = createInsertFields();
335+
const stmt = createBareStmt(fields, true);
336+
const params = new Stmt2BindParams(fields.length, 13, fields);
337+
338+
params.setVarchar(["d0"]);
339+
params.setVarchar(["beijing"]);
340+
params.setInt([1]);
341+
params.setDecimal(["123.456700"]);
342+
params.setInt([10]);
343+
344+
await stmt.bind(params);
345+
346+
expect(params._fieldParams?.[3].columnType).toBe(TDengineTypeCode.DECIMAL64);
347+
348+
params.setVarchar(["d0"]);
349+
params.setVarchar(["beijing"]);
350+
params.setInt([1]);
351+
expect(() => {
352+
params.setDecimal(["223.456700"]);
353+
}).not.toThrow();
354+
params.setInt([11]);
355+
356+
expect(params._fieldParams?.[3].params.length).toBe(2);
357+
});
358+
338359
test("query mode should keep DECIMAL default type", async () => {
339360
const fields = createInsertFields();
340361
const stmt = createBareStmt(fields, false);

0 commit comments

Comments
 (0)