dolphindb 3.0.38 → 3.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -24,7 +24,8 @@ export declare enum DdbForm {
24
24
  /** 节点内部通信可能会使用,调用函数执行脚本一般不会返回这种类型 */
25
25
  chunk = 8,
26
26
  /** sysobj */
27
- object = 9
27
+ object = 9,
28
+ tensor = 10
28
29
  }
29
30
  /** DolphinDB DataType
30
31
  对应的 array vector 类型为 64 + 基本类型
@@ -173,6 +174,35 @@ export interface DdbMatrixValue {
173
174
  cols: DdbVectorObj | null;
174
175
  data: DdbVectorValue;
175
176
  }
177
+ /** 工具,取得某个 DdbType 的字节数 */
178
+ export declare const ddb_tensor_bytes: Record<DdbType.bool | DdbType.char | DdbType.short | DdbType.int | DdbType.long | DdbType.float | DdbType.double, number>;
179
+ type TensorElem = TensorData | boolean | number | bigint | null | string;
180
+ interface TensorData extends Array<TensorElem> {
181
+ }
182
+ interface DdbTensorMetadata {
183
+ /** Tensor 的元素的数据类型 */
184
+ data_type: DdbType;
185
+ /** Tensor 类型 */
186
+ tensor_type: number;
187
+ /** 设备类型 */
188
+ device_type: number;
189
+ /** Tensor Flags */
190
+ tensor_flags: number;
191
+ /** 维度 */
192
+ dimensions: number;
193
+ /** shape, shape[i] 表示第 i 个维度的 size */
194
+ shape: number[];
195
+ /** strides, strides[i] 表示在第 i 个维度,一个元素与下一个元素的距离 */
196
+ strides: number[];
197
+ /** 保留值 */
198
+ preserve_value: bigint;
199
+ /** 元素个数 */
200
+ elem_count: number;
201
+ }
202
+ export interface DdbTensorValue extends DdbTensorMetadata {
203
+ /** 数据 */
204
+ data: Uint8Array;
205
+ }
176
206
  export type DdbDictValue = [DdbVectorObj, DdbVectorObj];
177
207
  export interface DdbChartValue {
178
208
  /** 原属性 chartType original: chartType */
@@ -207,7 +237,7 @@ export type DdbVectorValue = null | Uint8Array | Int8Array | Int16Array | Int32A
207
237
  Uint8Array[] | // blob
208
238
  DdbObj[] | // any
209
239
  DdbSymbolExtendedValue | DdbArrayVectorValue | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | DdbDurationVectorValue;
210
- export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue;
240
+ export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue | DdbTensorValue;
211
241
  export type DdbStringObj = DdbObj<string>;
212
242
  export type DdbVectorObj<TValue extends DdbVectorValue = DdbVectorValue> = DdbObj<TValue>;
213
243
  export type DdbVectorAnyObj = DdbVectorObj<DdbObj[]>;
@@ -216,6 +246,7 @@ export type DdbTableObj<TColumns extends DdbVectorObj[] = DdbVectorObj[]> = DdbO
216
246
  export type DdbDictObj<TKeys extends DdbVectorObj = DdbVectorObj, TValues extends DdbVectorObj = DdbVectorObj> = DdbObj<[TKeys, TValues]>;
217
247
  export type DdbMatrixObj<TValue extends DdbMatrixValue = DdbMatrixValue> = DdbObj<TValue>;
218
248
  export type DdbChartObj = DdbObj<DdbChartValue>;
249
+ export type DdbTensorObj = DdbObj<DdbTensorValue>;
219
250
  /** DdbObj.data() 返回的表格对象 */
220
251
  export interface DdbTableData<TRow = any> {
221
252
  /** 表名 */
@@ -227,6 +258,11 @@ export interface DdbTableData<TRow = any> {
227
258
  /** 表格数据,每一行的对象组成的数组 */
228
259
  data: TRow[];
229
260
  }
261
+ /** DdbObj.data() 返回的 Tensor 对象 */
262
+ export interface DdbTensorData extends DdbTensorMetadata {
263
+ /** 数据 */
264
+ data: TensorData;
265
+ }
230
266
  /** DdbObj.data() 返回的矩阵对象 */
231
267
  export interface DdbMatrixData {
232
268
  /** 原始数据类型 */
@@ -294,7 +330,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
294
330
  pack(): Uint8Array;
295
331
  static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
296
332
  /** 将 DdbObj 转换为 js 原生数据类型
297
- - 标量对应 number, bigint 或者字符串
333
+ - 标量对应 number, bigint 或者字符串 (其中时间类型转换为常用的字符串表示)
298
334
  - 数组对应 js 原生数组
299
335
  - 表格对应 DdbTableData
300
336
  - 矩阵对应 DdbMatrixData
@@ -302,6 +338,20 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
302
338
  - 图对应 DdbChartValue */
303
339
  data<TResult extends any[]>(this: DdbVectorObj, options?: ConvertOptions): TResult;
304
340
  data<TResult = any>(this: DdbObj, options?: ConvertOptions): TResult;
341
+ /** 构建 Tensor
342
+ @param buildParams 构建参数
343
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
344
+ @returns */
345
+ static parse_tensor(buildParams: {
346
+ currentDim: number;
347
+ dimensions: number;
348
+ rawData: Uint8Array;
349
+ le: boolean;
350
+ dataByte: number;
351
+ dataType: DdbType;
352
+ shape: number[];
353
+ strides: number[];
354
+ }, limit?: number): TensorData;
305
355
  [inspect.custom](depth: number, _options: InspectOptions, _inspect: any): string;
306
356
  inspect_type(): string;
307
357
  /** 自动转换 js string, boolean 为 DdbObj */
@@ -346,8 +396,10 @@ export declare function formati(obj: DdbVectorObj, index: number, options?: Insp
346
396
  export interface ConvertOptions {
347
397
  /** `'string'` blob 类型数据的格式 */
348
398
  blob?: 'string' | 'binary';
399
+ /** timestamp 类型转换为字符串表示时显示到秒还是毫秒 */
400
+ timestamp?: 's' | 'ms';
349
401
  }
350
- export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob }?: ConvertOptions): string | number | bigint | boolean | Uint8Array | Int32Array | BigInt64Array | string[] | Int8Array | Int16Array | Float32Array | Float64Array | Uint8Array[] | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | DdbArrayVectorValue | DdbMatrixValue | DdbObj<DdbValue>[] | DdbDurationVectorValue | DdbChartValue;
402
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob }?: ConvertOptions): string | number | bigint | boolean | Uint8Array | Int32Array | BigInt64Array | string[] | Int8Array | Int16Array | Float32Array | Float64Array | Uint8Array[] | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | DdbArrayVectorValue | DdbMatrixValue | DdbObj<DdbValue>[] | DdbDurationVectorValue | DdbTensorValue | DdbChartValue;
351
403
  /** 转换一个向量到 js 原生数组 */
352
404
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
353
405
  export declare class DdbVoid extends DdbObj<undefined> {
package/index.js CHANGED
@@ -30,6 +30,7 @@ export var DdbForm;
30
30
  DdbForm[DdbForm["chunk"] = 8] = "chunk";
31
31
  /** sysobj */
32
32
  DdbForm[DdbForm["object"] = 9] = "object";
33
+ DdbForm[DdbForm["tensor"] = 10] = "tensor";
33
34
  })(DdbForm || (DdbForm = {}));
34
35
  /** DolphinDB DataType
35
36
  对应的 array vector 类型为 64 + 基本类型
@@ -126,6 +127,16 @@ export var DdbVoidType;
126
127
  DdbVoidType[DdbVoidType["default"] = 2] = "default";
127
128
  })(DdbVoidType || (DdbVoidType = {}));
128
129
  export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long]);
130
+ /** 工具,取得某个 DdbType 的字节数 */
131
+ export const ddb_tensor_bytes = {
132
+ [DdbType.bool]: 1,
133
+ [DdbType.char]: 1,
134
+ [DdbType.short]: 2,
135
+ [DdbType.int]: 4,
136
+ [DdbType.long]: 8,
137
+ [DdbType.float]: 4,
138
+ [DdbType.double]: 8,
139
+ };
129
140
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
130
141
  export class DdbObj {
131
142
  static dec = new TextDecoder('utf-8');
@@ -363,6 +374,47 @@ export class DdbObj {
363
374
  },
364
375
  });
365
376
  }
377
+ case DdbForm.tensor: {
378
+ // 元数据
379
+ const tensorType = buf[2];
380
+ const deviceType = buf[3];
381
+ const dv = new DataView(buf.buffer, buf.byteOffset);
382
+ const tensorFlags = dv.getUint32(4, le);
383
+ const dimensions = dv.getInt32(8, le);
384
+ const shapes = [];
385
+ const strides = [];
386
+ const shapeStart = 12;
387
+ const stridesStart = shapeStart + dimensions * 8;
388
+ const preserveValueStart = stridesStart + dimensions * 8;
389
+ const preserveValue = dv.getBigInt64(preserveValueStart, le);
390
+ const storageStart = preserveValueStart + 8;
391
+ const elemCount = dv.getBigInt64(storageStart, le);
392
+ const dataStart = storageStart + 8;
393
+ for (let d = 0; d < dimensions; d++) {
394
+ const getNumOffset = d * 8;
395
+ shapes.push(Number(dv.getBigInt64(shapeStart + getNumOffset, le)));
396
+ strides.push(Number(dv.getBigInt64(stridesStart + getNumOffset, le)));
397
+ }
398
+ const dataBuffer = buf.subarray(dataStart);
399
+ return new this({
400
+ le,
401
+ form,
402
+ type,
403
+ length: i_data + buf_data.length,
404
+ value: {
405
+ data_type: type,
406
+ tensor_type: tensorType,
407
+ device_type: deviceType,
408
+ tensor_flags: tensorFlags,
409
+ dimensions,
410
+ shape: shapes,
411
+ strides,
412
+ preserve_value: preserveValue,
413
+ elem_count: Number(elemCount),
414
+ data: dataBuffer
415
+ }
416
+ });
417
+ }
366
418
  default:
367
419
  return new this({
368
420
  le,
@@ -1013,6 +1065,38 @@ export class DdbObj {
1013
1065
  ...DdbObj.pack_vector_body(data, this.type, this.rows * this.cols)
1014
1066
  ];
1015
1067
  }
1068
+ case DdbForm.tensor: {
1069
+ const { le, value } = this;
1070
+ const { tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data } = value;
1071
+ // 计算总的字节长度
1072
+ const totalLength = 10 + dimensions * 8 * 2 + 8 + 8 + data.length; // 12 字节元数据 + 维度信息 + 保留值和元素数量 + 数据部分
1073
+ const buffer = new ArrayBuffer(totalLength);
1074
+ const dv = new DataView(buffer);
1075
+ const uint8Array = new Uint8Array(buffer);
1076
+ // 写入元数据
1077
+ // uint8Array[0] = type;
1078
+ // uint8Array[1] = form;
1079
+ uint8Array[0] = tensor_type;
1080
+ uint8Array[1] = device_type;
1081
+ dv.setUint32(2, tensor_flags, le);
1082
+ dv.setInt32(6, dimensions, le);
1083
+ // 写入形状和步长
1084
+ const shapeStart = 10;
1085
+ const stridesStart = shapeStart + dimensions * 8;
1086
+ for (let d = 0; d < dimensions; d++) {
1087
+ dv.setBigInt64(shapeStart + d * 8, BigInt(shape[d]), le);
1088
+ dv.setBigInt64(stridesStart + d * 8, BigInt(strides[d]), le);
1089
+ }
1090
+ // 写入保留值和元素数量
1091
+ const preserveValueStart = stridesStart + dimensions * 8;
1092
+ dv.setBigInt64(preserveValueStart, preserve_value, le);
1093
+ const storageStart = preserveValueStart + 8;
1094
+ dv.setBigInt64(storageStart, BigInt(elem_count), le);
1095
+ // 写入数据
1096
+ const dataStart = storageStart + 8;
1097
+ uint8Array.set(data, dataStart);
1098
+ return [uint8Array];
1099
+ }
1016
1100
  default:
1017
1101
  throw new Error(t('vector {{type}} 暂不支持序列化', { type: get_type_name(type) }));
1018
1102
  }
@@ -1163,10 +1247,99 @@ export class DdbObj {
1163
1247
  data: seq(rows, i => seq(ncolumns, j => jsdata[j * rows + i]))
1164
1248
  };
1165
1249
  }
1250
+ case DdbForm.tensor: {
1251
+ const { data_type, tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data, } = this.value;
1252
+ const dataByte = ddb_tensor_bytes[data_type];
1253
+ const returnData = {
1254
+ data_type,
1255
+ tensor_type,
1256
+ device_type,
1257
+ tensor_flags,
1258
+ dimensions,
1259
+ shape,
1260
+ strides,
1261
+ preserve_value,
1262
+ elem_count,
1263
+ data: DdbObj.parse_tensor({ currentDim: 0, dimensions, rawData: data, le: this.le, dataByte, dataType: data_type, shape, strides })
1264
+ };
1265
+ return returnData;
1266
+ }
1166
1267
  default:
1167
1268
  throw new Error(t('{{form}} {{type}} 暂不支持 data()', { form, type: get_type_name(type) }));
1168
1269
  }
1169
1270
  }
1271
+ /** 构建 Tensor
1272
+ @param buildParams 构建参数
1273
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
1274
+ @returns */
1275
+ static parse_tensor(buildParams, limit = -1) {
1276
+ const { currentDim, dimensions, rawData, le, dataByte, dataType, shape, strides } = buildParams;
1277
+ const tensor = [];
1278
+ const dv = new DataView(rawData.buffer, rawData.byteOffset);
1279
+ for (let i = 0; i < shape[currentDim]; i++)
1280
+ if (currentDim >= dimensions - 1) {
1281
+ if (limit > 0 && i >= limit) {
1282
+ tensor.push('...');
1283
+ break;
1284
+ }
1285
+ // 直接转换到对应的数组
1286
+ const offset = i * dataByte * Number(strides[currentDim]);
1287
+ switch (dataType) {
1288
+ case DdbType.bool: {
1289
+ const value = dv.getInt8(offset);
1290
+ tensor.push(value === nulls.int8 ? null : Boolean(value));
1291
+ break;
1292
+ }
1293
+ case DdbType.char: {
1294
+ const value = dv.getInt8(offset);
1295
+ tensor.push(value === nulls.int8 ? null : value);
1296
+ break;
1297
+ }
1298
+ case DdbType.short: {
1299
+ const value = dv.getInt16(offset, le);
1300
+ tensor.push(value === nulls.int16 ? null : value);
1301
+ break;
1302
+ }
1303
+ case DdbType.int: {
1304
+ const value = dv.getInt32(offset, le);
1305
+ tensor.push(value === nulls.int32 ? null : value);
1306
+ break;
1307
+ }
1308
+ case DdbType.long: {
1309
+ const value = dv.getBigInt64(offset, le);
1310
+ tensor.push(value === nulls.int64 ? null : value);
1311
+ break;
1312
+ }
1313
+ case DdbType.float: {
1314
+ const value = dv.getFloat32(offset, le);
1315
+ tensor.push(value === nulls.float32 ? null : value);
1316
+ break;
1317
+ }
1318
+ case DdbType.double: {
1319
+ const value = dv.getFloat64(offset, le);
1320
+ tensor.push(value === nulls.double ? null : value);
1321
+ break;
1322
+ }
1323
+ }
1324
+ }
1325
+ else {
1326
+ // 起点
1327
+ const start = strides[currentDim] * i * dataByte;
1328
+ // 终点
1329
+ const end = start + strides[currentDim] * 1 * dataByte;
1330
+ tensor.push(DdbObj.parse_tensor({
1331
+ currentDim: currentDim + 1,
1332
+ dimensions,
1333
+ rawData: rawData.subarray(Number(start), Number(end)),
1334
+ le,
1335
+ dataByte,
1336
+ shape,
1337
+ strides,
1338
+ dataType
1339
+ }, limit));
1340
+ }
1341
+ return tensor;
1342
+ }
1170
1343
  [inspect.custom](depth, _options, _inspect) {
1171
1344
  const options = { nullstr: true, ..._options };
1172
1345
  const type = this.inspect_type();
@@ -1295,6 +1468,20 @@ export class DdbObj {
1295
1468
  }
1296
1469
  }
1297
1470
  }
1471
+ case DdbForm.tensor: {
1472
+ const tensorVal = this.value;
1473
+ const retd = DdbObj.parse_tensor({
1474
+ currentDim: 0,
1475
+ dimensions: tensorVal.dimensions,
1476
+ rawData: tensorVal.data,
1477
+ le: this.le,
1478
+ dataByte: ddb_tensor_bytes[tensorVal.data_type],
1479
+ dataType: tensorVal.data_type,
1480
+ shape: tensorVal.shape,
1481
+ strides: tensorVal.strides
1482
+ }, 5);
1483
+ return inspect(retd, options).replaceAll("\'...\'", '...');
1484
+ }
1298
1485
  }
1299
1486
  if (this.value instanceof Uint8Array)
1300
1487
  return inspect(typed_array_to_buffer(this.value), options);
@@ -1329,6 +1516,8 @@ export class DdbObj {
1329
1516
  return `chart<${DdbChartType[this.value.type]}>`;
1330
1517
  case DdbForm.matrix:
1331
1518
  return `matrix<${tname}>[${this.rows}r][${this.cols}c]`;
1519
+ case DdbForm.tensor:
1520
+ return `tensor<${generate_array_type(DdbType[this.value.data_type], this.value.shape)}>`;
1332
1521
  default:
1333
1522
  return `${DdbForm[this.form]} ${tname}`;
1334
1523
  }
@@ -3343,6 +3532,13 @@ function set_big_uint_128(dataView, byte_offset, value, le = true) {
3343
3532
  function set_big_int_128(dataview, byte_offset, value, le = true) {
3344
3533
  set_big_uint_128(dataview, byte_offset, value, le);
3345
3534
  }
3535
+ function generate_array_type(baseType, dimensions) {
3536
+ let result = baseType;
3537
+ dimensions.forEach(dimension => {
3538
+ result += `[${dimension}]`;
3539
+ });
3540
+ return result;
3541
+ }
3346
3542
  // 大端
3347
3543
  // const dataBE = new ArrayBuffer(16)
3348
3544
  // const dataViewBE = new DataView(dataBE)