首页 > 解决方案 > `instanceof Date` 在 BigQuery UDF 中的行为不符合预期

问题描述

任何人都知道为什么会出现以下结果false而不是true

CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return d instanceof Date;
""";
SELECT test();

退货false (意外)


解决方法:

CREATE TEMPORARY FUNCTION test()
RETURNS BOOL
LANGUAGE js
AS
"""
const d = new Date();
return Object.prototype.toString.call(d) === '[object Date]';
""";
SELECT test();

退货true (如预期)

标签: google-bigquerybigquery-udf

解决方案


instanceof Date接缝不工作。

对于其他对象,就像String它工作正常。

有一个 JavaScript 解决方法可能

CREATE TEMPORARY FUNCTION test()
RETURNS  BOOL
LANGUAGE js
AS
"""
var  d = new Date();
var s= new String('String created with constructor');
//return s instanceof String;
return typeof d.getMonth === 'function';
""";

SELECT test();

推荐阅读