Skip to content

Update typeof. Support of single quoted JSON#395

Open
tartas1 wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
tartas1:master
Open

Update typeof. Support of single quoted JSON#395
tartas1 wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
tartas1:master

Conversation

@tartas1

@tartas1 tartas1 commented Jan 20, 2024

Copy link
Copy Markdown

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

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}');
@google-cla

google-cla Bot commented Jan 20, 2024

Copy link
Copy Markdown

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.

Hackerpilot pushed a commit to Hackerpilot/bigquery-utils that referenced this pull request Jun 10, 2026
…oogleCloudPlatform#395)

b/341297017

- add a query based on the part of DMA's pbdsinfo.sql unrelated to app_schemas
- add a query based on DMA's dbfeatures.sql
- add a select on gv$instance, to provide the same data as the dbinstances query in DMA
- replace 'metadata' with 'native' as the description of non-AWR, non-Statspack SQLs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant