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

CockroachDB JSON fields read incorrectly #3689

Open
reflechant opened this issue Jan 16, 2025 · 0 comments
Open

CockroachDB JSON fields read incorrectly #3689

reflechant opened this issue Jan 16, 2025 · 0 comments
Labels

Comments

@reflechant
Copy link

reflechant commented Jan 16, 2025

I have found these related issues/pull requests

this bug seems to not be reported before

Description

if you read a JSON column from CockroachDB into a String field of a struct with query_as! macro (or query_as function) it reads incorrect data, adding a space (one \u{1} character) in the beginning of the JSON string representation.

Reproduction steps

I created a repo with a reproducible example, feel free to check it out: https://github.com/reflechant/sqlx-json-cockroach-bug

Steps to reproduce:

  1. Create a table in CockroachDB: CREATE TABLE IF NOT EXISTS data( tags JSON NOT NULL );
  2. Create a row with a valid JSON dictionary in the table: INSERT INTO data (tags) VALUES('{"a":"1"}'::json);. Other types of JSON documents are also affected (arrays, 'null', etc.)
  3. (Optional) check that the JSON is correct in the database with psql/dBeaver/...
  4. Use sqlx to read this JSON into a String field:
pub struct Data {
    pub tags: String,
}

let rows = sqlx::query_as!(Data, r#"SELECT tags as "tags: String" FROM data"#)
            .fetch_all(&self.db_pool)
            .await?;
  1. Observe incorrect JSON: Data { tags: "\u{1}{\"a\": \"1\"}" }

P.S. PostgreSQL is not affected.
P.P.S. Using sqlx::Json to work with JSON fields solves this issue.

SQLx version

0.8.3

Enabled SQLx features

runtime-tokio,postgres,json

Database server and version

CockroachDB v24.3.3 (from Docker Hub)

Operating system

Linux,MacOS

Rust version

1.84.0, 1.86.0-nightly

@reflechant reflechant added the bug label Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant