Skip to content

Conversation

@tartas1
Copy link

@tartas1 tartas1 commented Jan 20, 2024

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
Copy link

google-cla bot commented Jan 20, 2024

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.

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