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/browser.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
  toString(options?: InspectOptions): string;
306
356
  inspect_type(): string;
307
357
  /** 自动转换 js string, boolean 为 DdbObj */
@@ -357,7 +407,7 @@ export interface ConvertOptions {
357
407
  /** `'string'` blob 类型数据的格式 */
358
408
  blob?: 'string' | 'binary';
359
409
  }
360
- export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob }?: ConvertOptions): string | number | bigint | boolean | Uint8Array | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | Int32Array | DdbDecimal64VectorValue | BigInt64Array | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | string[] | Int8Array | Int16Array | Float32Array | Float64Array | DdbArrayVectorValue | DdbMatrixValue | Uint8Array[] | DdbObj<DdbValue>[] | DdbDurationVectorValue | DdbChartValue | number[];
410
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob }?: ConvertOptions): string | number | bigint | boolean | Uint8Array | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | Int32Array | DdbDecimal64VectorValue | BigInt64Array | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | string[] | Int8Array | Int16Array | Float32Array | Float64Array | DdbArrayVectorValue | DdbMatrixValue | Uint8Array[] | DdbObj<DdbValue>[] | DdbDurationVectorValue | number[] | DdbTensorValue | DdbChartValue;
361
411
  /** 转换一个向量到 js 原生数组 */
362
412
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
363
413
  export declare class DdbVoid extends DdbObj<undefined> {
package/browser.js CHANGED
@@ -33,6 +33,7 @@ export var DdbForm;
33
33
  DdbForm[DdbForm["chunk"] = 8] = "chunk";
34
34
  /** sysobj */
35
35
  DdbForm[DdbForm["object"] = 9] = "object";
36
+ DdbForm[DdbForm["tensor"] = 10] = "tensor";
36
37
  })(DdbForm || (DdbForm = {}));
37
38
  /** DolphinDB DataType
38
39
  对应的 array vector 类型为 64 + 基本类型
@@ -129,6 +130,16 @@ export var DdbVoidType;
129
130
  DdbVoidType[DdbVoidType["default"] = 2] = "default";
130
131
  })(DdbVoidType || (DdbVoidType = {}));
131
132
  export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long]);
133
+ /** 工具,取得某个 DdbType 的字节数 */
134
+ export const ddb_tensor_bytes = {
135
+ [DdbType.bool]: 1,
136
+ [DdbType.char]: 1,
137
+ [DdbType.short]: 2,
138
+ [DdbType.int]: 4,
139
+ [DdbType.long]: 8,
140
+ [DdbType.float]: 4,
141
+ [DdbType.double]: 8,
142
+ };
132
143
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
133
144
  export class DdbObj {
134
145
  static dec = new TextDecoder('utf-8');
@@ -366,6 +377,47 @@ export class DdbObj {
366
377
  },
367
378
  });
368
379
  }
380
+ case DdbForm.tensor: {
381
+ // 元数据
382
+ const tensorType = buf[2];
383
+ const deviceType = buf[3];
384
+ const dv = new DataView(buf.buffer, buf.byteOffset);
385
+ const tensorFlags = dv.getUint32(4, le);
386
+ const dimensions = dv.getInt32(8, le);
387
+ const shapes = [];
388
+ const strides = [];
389
+ const shapeStart = 12;
390
+ const stridesStart = shapeStart + dimensions * 8;
391
+ const preserveValueStart = stridesStart + dimensions * 8;
392
+ const preserveValue = dv.getBigInt64(preserveValueStart, le);
393
+ const storageStart = preserveValueStart + 8;
394
+ const elemCount = dv.getBigInt64(storageStart, le);
395
+ const dataStart = storageStart + 8;
396
+ for (let d = 0; d < dimensions; d++) {
397
+ const getNumOffset = d * 8;
398
+ shapes.push(Number(dv.getBigInt64(shapeStart + getNumOffset, le)));
399
+ strides.push(Number(dv.getBigInt64(stridesStart + getNumOffset, le)));
400
+ }
401
+ const dataBuffer = buf.subarray(dataStart);
402
+ return new this({
403
+ le,
404
+ form,
405
+ type,
406
+ length: i_data + buf_data.length,
407
+ value: {
408
+ data_type: type,
409
+ tensor_type: tensorType,
410
+ device_type: deviceType,
411
+ tensor_flags: tensorFlags,
412
+ dimensions,
413
+ shape: shapes,
414
+ strides,
415
+ preserve_value: preserveValue,
416
+ elem_count: Number(elemCount),
417
+ data: dataBuffer
418
+ }
419
+ });
420
+ }
369
421
  default:
370
422
  return new this({
371
423
  le,
@@ -1166,10 +1218,99 @@ export class DdbObj {
1166
1218
  data: seq(rows, i => seq(ncolumns, j => jsdata[j * rows + i]))
1167
1219
  };
1168
1220
  }
1221
+ case DdbForm.tensor: {
1222
+ const { data_type, tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data, } = this.value;
1223
+ const dataByte = ddb_tensor_bytes[data_type];
1224
+ const returnData = {
1225
+ data_type,
1226
+ tensor_type,
1227
+ device_type,
1228
+ tensor_flags,
1229
+ dimensions,
1230
+ shape,
1231
+ strides,
1232
+ preserve_value,
1233
+ elem_count,
1234
+ data: DdbObj.parse_tensor({ currentDim: 0, dimensions, rawData: data, le: this.le, dataByte, dataType: data_type, shape, strides })
1235
+ };
1236
+ return returnData;
1237
+ }
1169
1238
  default:
1170
1239
  throw new Error(t('{{form}} {{type}} 暂不支持 data()', { form, type: get_type_name(type) }));
1171
1240
  }
1172
1241
  }
1242
+ /** 构建 Tensor
1243
+ @param buildParams 构建参数
1244
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
1245
+ @returns */
1246
+ static parse_tensor(buildParams, limit = -1) {
1247
+ const { currentDim, dimensions, rawData, le, dataByte, dataType, shape, strides } = buildParams;
1248
+ const tensor = [];
1249
+ const dv = new DataView(rawData.buffer, rawData.byteOffset);
1250
+ for (let i = 0; i < shape[currentDim]; i++)
1251
+ if (currentDim >= dimensions - 1) {
1252
+ if (limit > 0 && i >= limit) {
1253
+ tensor.push('...');
1254
+ break;
1255
+ }
1256
+ // 直接转换到对应的数组
1257
+ const offset = i * dataByte * Number(strides[currentDim]);
1258
+ switch (dataType) {
1259
+ case DdbType.bool: {
1260
+ const value = dv.getInt8(offset);
1261
+ tensor.push(value === nulls.int8 ? null : Boolean(value));
1262
+ break;
1263
+ }
1264
+ case DdbType.char: {
1265
+ const value = dv.getInt8(offset);
1266
+ tensor.push(value === nulls.int8 ? null : value);
1267
+ break;
1268
+ }
1269
+ case DdbType.short: {
1270
+ const value = dv.getInt16(offset, le);
1271
+ tensor.push(value === nulls.int16 ? null : value);
1272
+ break;
1273
+ }
1274
+ case DdbType.int: {
1275
+ const value = dv.getInt32(offset, le);
1276
+ tensor.push(value === nulls.int32 ? null : value);
1277
+ break;
1278
+ }
1279
+ case DdbType.long: {
1280
+ const value = dv.getBigInt64(offset, le);
1281
+ tensor.push(value === nulls.int64 ? null : value);
1282
+ break;
1283
+ }
1284
+ case DdbType.float: {
1285
+ const value = dv.getFloat32(offset, le);
1286
+ tensor.push(value === nulls.float32 ? null : value);
1287
+ break;
1288
+ }
1289
+ case DdbType.double: {
1290
+ const value = dv.getFloat64(offset, le);
1291
+ tensor.push(value === nulls.double ? null : value);
1292
+ break;
1293
+ }
1294
+ }
1295
+ }
1296
+ else {
1297
+ // 起点
1298
+ const start = strides[currentDim] * i * dataByte;
1299
+ // 终点
1300
+ const end = start + strides[currentDim] * 1 * dataByte;
1301
+ tensor.push(DdbObj.parse_tensor({
1302
+ currentDim: currentDim + 1,
1303
+ dimensions,
1304
+ rawData: rawData.subarray(Number(start), Number(end)),
1305
+ le,
1306
+ dataByte,
1307
+ shape,
1308
+ strides,
1309
+ dataType
1310
+ }, limit));
1311
+ }
1312
+ return tensor;
1313
+ }
1173
1314
  toString(options = { nullstr: true, quote: true }) {
1174
1315
  const type = this.inspect_type();
1175
1316
  const data = (() => {
@@ -1298,6 +1439,20 @@ export class DdbObj {
1298
1439
  }
1299
1440
  case DdbForm.chart:
1300
1441
  return JSON.stringify(this.value, (key, value) => key === 'data' ? value.toString(options) : value, 4);
1442
+ case DdbForm.tensor: {
1443
+ const tensorVal = this.value;
1444
+ const retd = DdbObj.parse_tensor({
1445
+ currentDim: 0,
1446
+ dimensions: tensorVal.dimensions,
1447
+ rawData: tensorVal.data,
1448
+ le: this.le,
1449
+ dataByte: ddb_tensor_bytes[tensorVal.data_type],
1450
+ dataType: tensorVal.data_type,
1451
+ shape: tensorVal.shape,
1452
+ strides: tensorVal.strides
1453
+ }, 5);
1454
+ return JSON.stringify(retd).replaceAll('"..."', '...');
1455
+ }
1301
1456
  }
1302
1457
  return String(this.value);
1303
1458
  })();
@@ -1330,6 +1485,8 @@ export class DdbObj {
1330
1485
  return `chart<${DdbChartType[this.value.type]}>`;
1331
1486
  case DdbForm.matrix:
1332
1487
  return `matrix<${tname}>[${this.rows}r][${this.cols}c]`;
1488
+ case DdbForm.tensor:
1489
+ return `tensor<${generate_array_type(DdbType[this.value.data_type], this.value.shape)}>`;
1333
1490
  default:
1334
1491
  return `${DdbForm[this.form]} ${tname}`;
1335
1492
  }
@@ -3361,6 +3518,13 @@ function set_big_uint_128(dataView, byte_offset, value, le = true) {
3361
3518
  function set_big_int_128(dataview, byte_offset, value, le = true) {
3362
3519
  set_big_uint_128(dataview, byte_offset, value, le);
3363
3520
  }
3521
+ function generate_array_type(baseType, dimensions) {
3522
+ let result = baseType;
3523
+ dimensions.forEach(dimension => {
3524
+ result += `[${dimension}]`;
3525
+ });
3526
+ return result;
3527
+ }
3364
3528
  // 大端
3365
3529
  // const dataBE = new ArrayBuffer(16)
3366
3530
  // const dataViewBE = new DataView(dataBE)