hasura / hasura/graphql-engine

Generated BQ code using wrong function

Open
#10,137 0 comments 0 reactions 0 assignees View on GitHub
k/bug
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: V2.25

### Environment

Docker version, local macbook.

### What is the current behaviour?

I am using a relationship from a PSQL table to a BQ table. One of the columns in PSQL is an array type, and the generated BQ code uses `JSON_VALUE`. dataset is the array column:

```
SELECT
`unnest1`.`__argument_id__` AS `__argument_id__`,
`unnest1`.`reported_metric_id` AS `reported_metric_id`,
`unnest1`.`dataset` AS `dataset`
FROM (SELECT CAST(JSON_VALUE(`json`, '$.__argument_id__') AS INT64) AS `__argument_id__`,
CAST(JSON_VALUE(`json`, '$.reported_metric_id') AS INT64) AS `reported_metric_id`,
CAST(JSON_VALUE(`json`, '$.dataset') AS STRING) AS `dataset`
FROM UNNEST(JSON_QUERY_ARRAY(@param0)) AS `json`
) AS `unnest1`
```

### What is the expected behaviour?
JSON_VALUE returns null for arrays, it should be JSON_QUERY:

```
SELECT
`unnest1`.`__argument_id__` AS `__argument_id__`,
`unnest1`.`reported_metric_id` AS `reported_metric_id`,
`unnest1`.`dataset` AS `dataset`
FROM (SELECT CAST(JSON_VALUE(`json`, '$.__argument_id__') AS INT64) AS `__argument_id__`,
CAST(JSON_VALUE(`json`, '$.reported_metric_id') AS INT64) AS `reported_metric_id`,
CAST(JSON_QUERY(`json`, '$.dataset') AS STRING) AS `dataset`
FROM UNNEST(JSON_QUERY_ARRAY(@param0)) AS `json`
) AS `unnest1`
```

### How to reproduce the issue?

1. Create a BQ table, with an array_to_string column
2. Create a PSQL table with an array column
3. make a relationship from PSQL to BQ including that array column
4. Query them and check the generated sql

### Screenshots or Screencast

image

### Any possible solutions/workarounds you're aware of?

Add a generated column to the PSQL table that converts array to string, and use that in the relationship
NOTE for anyone who needs the workaround: You need an immutable function to use in the generated column, this will work: https://dba.stackexchange.com/questions/299039/optimize-query-matching-first-n-items-of-an-array

### Keywords
JSON_QUERY
JSON_VALUE
array in relationship

Contributor guide

Open the contributing guide

Research direction

Start by tracing the BigQuery relationship SQL generation described in the issue, focusing on how array-valued fields are mapped to JSON_VALUE. Reproduce the PostgreSQL-to-BigQuery relationship using the listed steps and verify that array fields use JSON_QUERY while scalar fields retain JSON_VALUE.

Written by the indexing model from the issue text.

Assessment

Tech stack
google-cloud, postgresql, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.