Update typeof. Support of single quoted JSON #395
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fix is adding support for single-quoted JSON.
Currently, it works only with double-quoted JSON. There is no way to check JSON containing strings, dictionaries and lists.
This works:
SELECT typeof(JSON "null");
SELECT typeof(JSON "100");
This won't work:
SELECT typeof(JSON '"text"');
SELECT typeof(JSON '{"gate": "A4", "flight_number": 2005}');
==========================================================================
Fixed 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}');