简体   繁体   中英

`instanceof Date` not behaving as expected within a BigQuery UDF

Anyone have any insight into why the following results in false instead of true ?

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

Returns false (unexpected)


WORKAROUND:

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

Returns true (as expected)

The instanceof Date seams not to work.

For other objects, like String it works fine.

There is a JavaScript workaround possible :

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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