-1

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)

Dave Templin
  • 1,764
  • 1
  • 11
  • 5

1 Answers1

0

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();
Samuel
  • 2,923
  • 1
  • 4
  • 19
  • Thanks, yes I came up with a similar workaround... return Object.prototype.toString.call(d) === '[object Date]; Mainly just wanted to share this quirk as it cost me hours to track a bug down to this behavior. – Dave Templin Aug 20 '21 at 20:26