From 585910a1fe74e45489f77845a21b10c97942b7ea Mon Sep 17 00:00:00 2001 From: tartas1 <150056971+tartas1@users.noreply.github.com> Date: Sat, 20 Jan 2024 06:37:03 +0200 Subject: [PATCH] Update typeof. Support of ' Now typeof can work with JSON '{"a": "1", "b": 2}' EXAMPLE: CREATE temp FUNCTION typeof (input ANY TYPE) AS ( ( SELECT CASE -- Process NUMERIC, DATE, DATETIME, TIME, TIMESTAMP, WHEN REGEXP_CONTAINS(literal, r'^[A-Z]+ ("|\')') THEN REGEXP_EXTRACT(literal, r'^([A-Z]+) (?:"|\')') --modified regexp WHEN REGEXP_CONTAINS(literal, r'^-?[0-9]*$') THEN 'INT64' WHEN REGEXP_CONTAINS(literal, r'^(-?[0-9]+[.e].*|CAST\("([^"]*)" AS FLOAT64\))$') THEN 'FLOAT64' WHEN literal IN ('true', 'false') THEN 'BOOL' WHEN literal LIKE '"%' OR literal LIKE "'%" THEN 'STRING' WHEN literal LIKE 'b"%' THEN 'BYTES' WHEN literal LIKE '[%' THEN 'ARRAY' WHEN REGEXP_CONTAINS(literal, r'^(STRUCT)?\(') THEN 'STRUCT' WHEN literal LIKE 'ST_%' THEN 'GEOGRAPHY' WHEN literal = 'NULL' THEN 'NULL' ELSE 'UNKNOWN' END FROM UNNEST([FORMAT('%T', input)]) AS literal)); SELECT typeof(JSON '"text"'); SELECT typeof(JSON '{"gate": "A4", "flight_number": 2005}'); --- udfs/community/typeof.sqlx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/udfs/community/typeof.sqlx b/udfs/community/typeof.sqlx index 63baa67f9..5e1698d5c 100644 --- a/udfs/community/typeof.sqlx +++ b/udfs/community/typeof.sqlx @@ -23,7 +23,7 @@ AS ( ( SELECT CASE -- Process NUMERIC, DATE, DATETIME, TIME, TIMESTAMP, - WHEN REGEXP_CONTAINS(literal, r'^[A-Z]+ "') THEN REGEXP_EXTRACT(literal, r'^([A-Z]+) "') + WHEN REGEXP_CONTAINS(literal, r'^[A-Z]+ ("|\')') THEN REGEXP_EXTRACT(literal, r'^([A-Z]+) (?:"|\')') WHEN REGEXP_CONTAINS(literal, r'^-?[0-9]*$') THEN 'INT64' WHEN REGEXP_CONTAINS(literal, r'^(-?[0-9]+[.e].*|CAST\("([^"]*)" AS FLOAT64\))$') THEN 'FLOAT64' WHEN literal IN ('true', 'false') THEN 'BOOL'