question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support PostgreSQL's JSON data type(s)

See original GitHub issue
Original issue description:

Hi there,

in order to insert JSON data into the database, CrateDB’s documentation says:

You can insert objects using JSON strings. To do this, you must type cast the string to an object with an implicit cast (i.e., passing a string into an object column) or an explicit cast (i.e., using the ::object syntax). 1

Relating to 2, I wonder if CrateDB could provide a ::json alias to its type casting implementation underpinning the ::object syntax notation in order to implement at least some features of the official PostgreSQL JSON support [3,4], maybe even without further ado? In the case of 2, it might improve the compatibility with how Confluent’s JDBC Sink Connector for Kafka works 5.

@asavran reports at 2:

We tried to execute the same INSERT command manually by substituting the ::json with ::object and it worked. Is there a workaround for this?

I know that things might turn out to be more complicated more often than not, so I am just creating this as a basis for discussions.

With kind regards, Andreas.

Updated description:


Problem description

Kafka-connect-JDBC adds explicit ::json casts for target columns of type object (exposed in pg_catalog as json).

This currently breaks because json isn’t available as type.

The reason for kafka-connect-JDBC doing that is that otherwise PostgreSQL errors when using prepared statements. (http://sqlfiddle.com/#!17/7e29e/12, https://github.com/pgjdbc/pgjdbc/issues/265, https://github.com/confluentinc/kafka-connect-jdbc/pull/875)

Proposed potential solutions so far

  1. Add a json alias for object
  2. Add a dedicated new json type (without storage and cast support from text → json, json -> object, object → json)
  3. Make kafka-connect-jdbc CrateDB aware or configurable, so that no explicit casts are added for json columns (see https://github.com/crate/crate/issues/11347#issuecomment-839751709)

Estimate is for option 2, which is the first choice here.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:16 (16 by maintainers)

github_iconTop GitHub Comments

4reactions
hammerheadcommented, May 12, 2021

The issue can be reproduced by following our Kafka Connect Guide and adjusting the there mentioned cratedb_connector.json as follows:

{
  "name": "cratedb-connector",
  "config": {
    "connector.class": "io.confluent.connect.jdbc.JdbcSinkConnector",
    "topics": "metrics",
    "connection.url": "jdbc:postgresql://localhost:5432/doc?user=crate",
    "tasks.max": 3,
    "insert.mode": "insert",
    "table.name.format": "metrics",
    "transforms": "HoistPayload",
    "transforms.HoistPayload.field": "payload",
    "value.converter": "org.apache.kafka.connect.storage.StringConverter",
    "transforms.HoistPayload.type": "org.apache.kafka.connect.transforms.HoistField$Value"
  }
}

Confluent Connect derives the necessity to perform an explicit cast based on the column definition, see their PostgreSqlDatabaseDialect.java.

PG has a different syntax to interact with json fields, though we already return a pg json (oid 114) on SELECT on an OBJECT anyway through jdbc

Returning the column as json in pg_catalog is exactly what causes Confluent Connect to add the ::json cast. In this commit the cast was added:

the PostgreSqlDatabaseDialect class is modified to add the cast expression (e.g., ::uuid) to the variable placeholder (e.g, ?) if the column type matches one of the database types that requires casting, which in this case is either uuid, json, or jsonb.

In my test cases on PostgreSQL (12.6), inserts into JSON/JSONB columns worked fine without explicit casting, but I guess Confluent makes the assumption that if a column signals to be a json in pg_catalog, it would also support inserting values casted to json. Maybe it was required in older PostgreSQL versions to have that explicit cast.

3reactions
hammerheadcommented, Jul 30, 2021

FYI, I was able to successfully test the new JSON data type with the Confluent JDBC Sink connector. An updated version of the Kafka Connect Howto is available as a draft for when 4.7 is released (crate/crate-howtos#290).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.14. JSON Types - PostgreSQL
The jsonpath type implements support for the SQL/JSON path language in PostgreSQL to efficiently query JSON data. It provides a binary representation of...
Read more >
PostgreSQL Data Types: JSON
The json datatype, being a text datatype, stores the data presentation exactly as it is sent to PostgreSQL, including whitespace and indentation ...
Read more >
How to use the JSON Datatype in PostgreSQL - Data Virtuality
Learn to use the JSON datatype in PostgreSQL, using its great functionality to work with JSON objects, including sample data and SQL queries....
Read more >
PostgreSQL JSON Tutorial
PostgreSQL supports native JSON data type since version 9.2. It provides many functions and operators for manipulating JSON data.
Read more >
PostgreSQL - JSON Data Type - GeeksforGeeks
PostgreSQL has support for native JSON data type since version 9.2. It offers numerous functions and operators for handling JSON data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found