dolphindb 3.0.38 → 3.0.39

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
  /** 原始数据类型 */
@@ -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 */
@@ -347,7 +397,7 @@ export interface ConvertOptions {
347
397
  /** `'string'` blob 类型数据的格式 */
348
398
  blob?: 'string' | 'binary';
349
399
  }
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;
400
+ 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
401
  /** 转换一个向量到 js 原生数组 */
352
402
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
353
403
  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,
@@ -1163,10 +1215,99 @@ export class DdbObj {
1163
1215
  data: seq(rows, i => seq(ncolumns, j => jsdata[j * rows + i]))
1164
1216
  };
1165
1217
  }
1218
+ case DdbForm.tensor: {
1219
+ const { data_type, tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data, } = this.value;
1220
+ const dataByte = ddb_tensor_bytes[data_type];
1221
+ const returnData = {
1222
+ data_type,
1223
+ tensor_type,
1224
+ device_type,
1225
+ tensor_flags,
1226
+ dimensions,
1227
+ shape,
1228
+ strides,
1229
+ preserve_value,
1230
+ elem_count,
1231
+ data: DdbObj.parse_tensor({ currentDim: 0, dimensions, rawData: data, le: this.le, dataByte, dataType: data_type, shape, strides })
1232
+ };
1233
+ return returnData;
1234
+ }
1166
1235
  default:
1167
1236
  throw new Error(t('{{form}} {{type}} 暂不支持 data()', { form, type: get_type_name(type) }));
1168
1237
  }
1169
1238
  }
1239
+ /** 构建 Tensor
1240
+ @param buildParams 构建参数
1241
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
1242
+ @returns */
1243
+ static parse_tensor(buildParams, limit = -1) {
1244
+ const { currentDim, dimensions, rawData, le, dataByte, dataType, shape, strides } = buildParams;
1245
+ const tensor = [];
1246
+ const dv = new DataView(rawData.buffer, rawData.byteOffset);
1247
+ for (let i = 0; i < shape[currentDim]; i++)
1248
+ if (currentDim >= dimensions - 1) {
1249
+ if (limit > 0 && i >= limit) {
1250
+ tensor.push('...');
1251
+ break;
1252
+ }
1253
+ // 直接转换到对应的数组
1254
+ const offset = i * dataByte * Number(strides[currentDim]);
1255
+ switch (dataType) {
1256
+ case DdbType.bool: {
1257
+ const value = dv.getInt8(offset);
1258
+ tensor.push(value === nulls.int8 ? null : Boolean(value));
1259
+ break;
1260
+ }
1261
+ case DdbType.char: {
1262
+ const value = dv.getInt8(offset);
1263
+ tensor.push(value === nulls.int8 ? null : value);
1264
+ break;
1265
+ }
1266
+ case DdbType.short: {
1267
+ const value = dv.getInt16(offset, le);
1268
+ tensor.push(value === nulls.int16 ? null : value);
1269
+ break;
1270
+ }
1271
+ case DdbType.int: {
1272
+ const value = dv.getInt32(offset, le);
1273
+ tensor.push(value === nulls.int32 ? null : value);
1274
+ break;
1275
+ }
1276
+ case DdbType.long: {
1277
+ const value = dv.getBigInt64(offset, le);
1278
+ tensor.push(value === nulls.int64 ? null : value);
1279
+ break;
1280
+ }
1281
+ case DdbType.float: {
1282
+ const value = dv.getFloat32(offset, le);
1283
+ tensor.push(value === nulls.float32 ? null : value);
1284
+ break;
1285
+ }
1286
+ case DdbType.double: {
1287
+ const value = dv.getFloat64(offset, le);
1288
+ tensor.push(value === nulls.double ? null : value);
1289
+ break;
1290
+ }
1291
+ }
1292
+ }
1293
+ else {
1294
+ // 起点
1295
+ const start = strides[currentDim] * i * dataByte;
1296
+ // 终点
1297
+ const end = start + strides[currentDim] * 1 * dataByte;
1298
+ tensor.push(DdbObj.parse_tensor({
1299
+ currentDim: currentDim + 1,
1300
+ dimensions,
1301
+ rawData: rawData.subarray(Number(start), Number(end)),
1302
+ le,
1303
+ dataByte,
1304
+ shape,
1305
+ strides,
1306
+ dataType
1307
+ }, limit));
1308
+ }
1309
+ return tensor;
1310
+ }
1170
1311
  [inspect.custom](depth, _options, _inspect) {
1171
1312
  const options = { nullstr: true, ..._options };
1172
1313
  const type = this.inspect_type();
@@ -1295,6 +1436,20 @@ export class DdbObj {
1295
1436
  }
1296
1437
  }
1297
1438
  }
1439
+ case DdbForm.tensor: {
1440
+ const tensorVal = this.value;
1441
+ const retd = DdbObj.parse_tensor({
1442
+ currentDim: 0,
1443
+ dimensions: tensorVal.dimensions,
1444
+ rawData: tensorVal.data,
1445
+ le: this.le,
1446
+ dataByte: ddb_tensor_bytes[tensorVal.data_type],
1447
+ dataType: tensorVal.data_type,
1448
+ shape: tensorVal.shape,
1449
+ strides: tensorVal.strides
1450
+ }, 5);
1451
+ return inspect(retd, options).replaceAll("\'...\'", '...');
1452
+ }
1298
1453
  }
1299
1454
  if (this.value instanceof Uint8Array)
1300
1455
  return inspect(typed_array_to_buffer(this.value), options);
@@ -1329,6 +1484,8 @@ export class DdbObj {
1329
1484
  return `chart<${DdbChartType[this.value.type]}>`;
1330
1485
  case DdbForm.matrix:
1331
1486
  return `matrix<${tname}>[${this.rows}r][${this.cols}c]`;
1487
+ case DdbForm.tensor:
1488
+ return `tensor<${generate_array_type(DdbType[this.value.data_type], this.value.shape)}>`;
1332
1489
  default:
1333
1490
  return `${DdbForm[this.form]} ${tname}`;
1334
1491
  }
@@ -3343,6 +3500,13 @@ function set_big_uint_128(dataView, byte_offset, value, le = true) {
3343
3500
  function set_big_int_128(dataview, byte_offset, value, le = true) {
3344
3501
  set_big_uint_128(dataview, byte_offset, value, le);
3345
3502
  }
3503
+ function generate_array_type(baseType, dimensions) {
3504
+ let result = baseType;
3505
+ dimensions.forEach(dimension => {
3506
+ result += `[${dimension}]`;
3507
+ });
3508
+ return result;
3509
+ }
3346
3510
  // 大端
3347
3511
  // const dataBE = new ArrayBuffer(16)
3348
3512
  // const dataViewBE = new DataView(dataBE)