简体   繁体   English

BigQuery - 等待 UDF function 和 JavaScript 代码

[英]BigQuery - wait UDF function with JavaScript code

In Bigquery does not exist a native function like DBMS_LOCK.SLEEP (or DBMS_SESSION.SLEEP) from Oracle ("Puts a procedure to sleep for a specific time").在 Bigquery 中不存在像 Oracle 中的 DBMS_LOCK.SLEEP(或 DBMS_SESSION.SLEEP)这样的本机 function(“让程序休眠特定时间”)。 I tried to use an UDF with JavaScript code but it does not work as I expect: it does not stop other than forced.我尝试使用带有 JavaScript 代码的 UDF,但它没有像我预期的那样工作:它不会停止,除非被强制。 Can you help me understand what's going on?你能帮我理解这是怎么回事吗? Thanks!谢谢!

CREATE TEMP FUNCTION JS_Sleep(x FLOAT64)
 RETURNS FLOAT64
LANGUAGE js AS r"""
  function sleep(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
  }
  return sleep(x);

  """;
select JS_Sleep(10000);

In BigQuery, CURRENT_TIMESTAMP() similar with Date.now() always returns same value in a single statement or in a single transaction as if time stops regardless how many it appears in a query.在 BigQuery 中,与 Date.now() 类似的 CURRENT_TIMESTAMP() 总是在单个语句或单个事务中返回相同的值,就好像时间停止一样,无论它在查询中出现多少次。

In my experience, this also applies to Date.now().根据我的经验,这也适用于 Date.now()。 In your case, JS_Sleep() runs forever cause time stops within your query.在您的情况下, JS_Sleep() 永远运行会导致查询中的时间停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM