Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typeof. Support of single quoted JSON #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

None yet

1 participant