Hi all,


I'd like to share a new capability introduced by PR #18035: 
https://github.com/apache/iotdb/pull/18035


This PR adds IoTDBLocal, a framework-injected API for table-model UDFs (scalar 
functions, aggregate functions, and table functions). With it, UDF authors can 
execute read-only embedded SQL directly inside the server during UDF lifecycle 
hooks — without pulling in an external IoTDB client or opening a separate 
network connection. And it is compatible with previous versions, the UDF of old 
version can still run normally. Following are examples:


Example 1 — Preload an id to device-name mapping once using query interface of 
IoTDBLocal, to be used in later process:
```
// The beforeStart method of a scalar UDF 
@Override

public void beforeStart(FunctionArguments arguments, IoTDBLocal local) throws 
UDFException {
  local.info("DeviceNameFunction: loading device_info");
  Map<String, String> map = new HashMap<>();
  try (UDFResultSet rs = local.query("SELECT device_id, device_name FROM 
device_info")) {
    while (rs.hasNext()) {
      Record row = rs.next();
      map.put(row.getString(0), row.getString(1));
    }
  }
  idToName = map;
}
、、、

Example2 — Write server log using info / warn / error interfaces of IoTDBLocal: 
 
```
// The evaluate method of a scalar UDF 
@Override
public Object evaluate(Record input, IoTDBLocal local) {
  if (!firstRowLogged) {
    firstRowLogged = true;
    local.info("MyUdf: start processing, first device_id={}", 
input.getString(0));
  }
  if (input.isNull(1)) {
    local.error("MyUdf: temperature is null, device_id={}", input.getString(0));
    return null;
  }
  double temp = input.getDouble(1);
  if (temp > 30.0) {
    local.warn("MyUdf: high temperature detected, device_id={}, temp={}",
        input.getString(0), temp);
  }
  return buildResult(input);
}
```

Yours, 
Weihao Li

Reply via email to