GoogleCloudPlatform / bigquery-utils

Useful scripts, udfs, views, and other utilities for migration and data warehouse operations in BigQuery.
https://cloud.google.com/bigquery/
Apache License 2.0
1.07k stars 269 forks source link

Update typeof. Support of single quoted JSON #395

Open tartas1 opened 5 months ago

tartas1 commented 5 months ago

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 REGEXPCONTAINS(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}');

google-cla[bot] commented 5 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.