pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug in JDBC.driver CopyManager method when copying json output
4+ messages / 4 participants
[nested] [flat]

* Bug in JDBC.driver CopyManager method when copying json output
@ 2018-10-19 14:03  Stadlmann, Gerson <Gerson.Stadlmann@ifco.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Stadlmann, Gerson @ 2018-10-19 14:03 UTC (permalink / raw)
  To: pgsql-jdbc

Dear JDBC-Devs,
i think i found an escaping bug that causes duplicate escaping with backslash when using jdbc drivers CopyManager with JSON results.

Sample Query:
select to_json( 'my name is "rabbit"'::text);

Executing this query as Statement
<java>
Statement statement = connection.createStatement()
ResultSet resultSet = statement.executeQuery("select to_json('my name is \"rabbit\"'::text)");
String result=resultSet.next().getString(1);// => "my name is \"rabbit\""
</java>

Execution of this query using CopyManager (see result file attachment)
This Execution does the duplicate escaping for JSON output causing invalid json text.
<java>
BaseConnection bc=(BaseConnection) connection;
final CopyManager cm=new CopyManager(bc);
File someFile=new File("test.json");
try(FileWriter fw=new FileWriter(someFile)){
                cm.copyOut("COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH NULL AS ''", fw);
                // => my name is \\"rabbit\\" instead
}
</java>


My Driver:
<dependency>
           <groupId>org.postgresql</groupId>
           <artifactId>postgresql</artifactId>
           <version>42.2.5</version>
       </dependency>

My pg Server (Docker):
10.4 (Debian 10.4-2.pgdg90+1)

If that is required behavior I suggest to introduce another format "json" in addition to "text" and "csv".

Kind regards

Gerson Stadlmann
Manager Software Development International

[IFCOGlobal_logo_RGB - email v2]

IFCO Systems Austria GmbH
Unterthalham Straße 2  |  4694 Ohlsdorf |  Austria
T: +43 (0)7612-787 782   |  M: +43 664 3855 154
gerson.stadlmann@ifco.com<mailto:gerson.stadlmann@ifco.com>  |  www.ifco.com<http://www.ifco.com/;

A better supply chain serves us all. Let's eat.

Board of Management: Wolfgang Orgeldinger / Chairman, Margit Kendler, Christoph Trixl, Heimo Thaler FN213156s, LG Wels, UID-Nr. ATU 52552107 SAVE PAPER - THINK BEFORE YOU PRINT

Attachments:

image002.png (image/png, 6.6K) download view image
test.json (application/octet-stream, 26B) download ^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: Bug in JDBC.driver CopyManager method when copying json output
@ 2018-10-20 13:43  Dave Cramer <pg@fastcrypt.com>
  parent: Stadlmann, Gerson <Gerson.Stadlmann@ifco.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Dave Cramer @ 2018-10-20 13:43 UTC (permalink / raw)
  To: Gerson.Stadlmann@ifco.com; +Cc: pgsql-jdbc

I suggest this is a backend problem...

This is done with psql...

select to_json('my name is \"rabbit\"'::text);
           to_json
-----------------------------
 "my name is \\\"rabbit\\\""
(1 row)

test=# COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH
NULL AS ''";
test"#
test=# COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH
NULL AS '';
"my name is \\\\\\"rabbit\\\\\\""


Dave Cramer

davec@postgresintl.com
www.postgresintl.com


On Fri, 19 Oct 2018 at 10:12, Stadlmann, Gerson <Gerson.Stadlmann@ifco.com>
wrote:

> Dear JDBC-Devs,
>
> i think i found an escaping bug that causes duplicate escaping with
> backslash when using jdbc drivers CopyManager with JSON results.
>
>
>
> *Sample Query:*
>
> select to_json( 'my name is "rabbit"'::text);
>
>
>
> *Executing this query as Statement*
>
> <java>
>
> Statement statement = connection.createStatement()
>
> ResultSet resultSet = statement.executeQuery("select to_json('my name is
> \"rabbit\"'::text)");
>
> String result=resultSet.next().getString(1);// => "my name is \"rabbit\""
>
> </java>
>
>
>
> *Execution of this query using CopyManager (see result file attachment)*
>
> This Execution does the duplicate escaping for JSON output causing invalid
> json text.
>
> <java>
>
> BaseConnection bc=(BaseConnection)
> connection;
>
> final CopyManager cm=new CopyManager(bc);
>
> File someFile=new File("test.json");
>
> try(FileWriter fw=new FileWriter(someFile)){
>
>                 cm.copyOut("COPY (select to_json('my name is
> \"rabbit\"'::text)) TO STDOUT WITH NULL AS ''", fw);
>
>                 // => my name is *\*\"rabbit*\*\" instead
>
> }
>
> </java>
>
>
>
>
>
> *My Driver:*
>
> <dependency>
>
>            <groupId>org.postgresql</groupId>
>
>            <artifactId>*postgresql*</artifactId>
>
>            <version>42.2.5</version>
>
>        </dependency>
>
>
>
> *My pg Server (Docker):*
>
> 10.4 (Debian 10.4-2.pgdg90+1)
>
>
>
> If that is required behavior I suggest to introduce another format “json”
> in addition to “text” and “csv”.
>
>
>
> Kind regards
>
>
>
> *Gerson Stadlmann*
>
> Manager Software Development International
>
>
>
> [image: IFCOGlobal_logo_RGB - email v2]
>
>
>
> IFCO Systems Austria GmbH
>
> Unterthalham Straße 2  |  4694 Ohlsdorf |  Austria
> T: +43 (0)7612-787 782   |  M: +43 664 3855 154
> gerson.stadlmann@ifco.com  |  www.ifco.com
>
>
>
> A better supply chain serves us all. Let’s eat.
>
>
> Board of Management: Wolfgang Orgeldinger / Chairman, Margit Kendler,
> Christoph Trixl, Heimo Thaler FN213156s, LG Wels, UID-Nr. ATU 52552107 SAVE
> PAPER - THINK BEFORE YOU PRINT
>

Attachments:

image002.png (image/png, 6.6K) download view image
image002.png (image/png, 6.6K) download view image
^ permalink raw reply [nested|flat] 4+ messages in thread

* Bug in JDBC.driver CopyManager method when copying json output
@ 2018-10-20 14:44  David G. Johnston <david.g.johnston@gmail.com>
  parent: Dave Cramer <pg@fastcrypt.com>
  0 siblings, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2018-10-20 14:44 UTC (permalink / raw)
  To: Dave Cramer <pg@fastcrypt.com>; +Cc: Gerson.Stadlmann@ifco.com <Gerson.Stadlmann@ifco.com>; pgsql-jdbc

The backend is respecting the documented meanings and usage of the QUOTE
and ESCAPE options of COPY.

Not sure what a JSON mode would look like though I’m thinking it’s not what
was described here...

For now you either need to fiddle with those options of COPY or avoid it
and generate your own output document.  Didn’t try but I do presume feeding
the output to COPY FROM reaults in a valid import.

David J.

On Saturday, October 20, 2018, Dave Cramer <pg@fastcrypt.com> wrote:

> I suggest this is a backend problem...
>
>
>
> On Fri, 19 Oct 2018 at 10:12, Stadlmann, Gerson <Gerson.Stadlmann@ifco.com>
> wrote:
>
>> Dear JDBC-Devs,
>>
>> i think i found an escaping bug that causes duplicate escaping with
>> backslash when using jdbc drivers CopyManager with JSON results.
>>
>>
>>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Bug in JDBC.driver CopyManager method when copying json output
@ 2018-10-20 17:16  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2018-10-20 17:16 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Dave Cramer <pg@fastcrypt.com>; Gerson.Stadlmann@ifco.com <Gerson.Stadlmann@ifco.com>; pgsql-jdbc

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> The backend is respecting the documented meanings and usage of the QUOTE
> and ESCAPE options of COPY.

Yeah, I don't see any bug here.  You've got one layer of backslash-quoting
that is required by JSON string syntax, and then a second layer that is
imposed by COPY's text format.

> Not sure what a JSON mode would look like though I’m thinking it’s not what
> was described here...

I can't see how that would do anything except move the pain somewhere
else.  Multiple quoting layers always result in "ugly" output (but if
the receipient is correctly coded and reverses the operations in the
right sequence, the same underlying data will emerge).

The OP might want to consider using COPY BINARY, which uses a length
prefix instead of character-escaping to keep fields separate.  Still,
though, this is fundamentally just cosmetic.  If you don't apply the
correct reverse conversion steps in the correct order, you won't get
the right results.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-10-20 17:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-10-19 14:03 Bug in JDBC.driver CopyManager method when copying json output Stadlmann, Gerson <Gerson.Stadlmann@ifco.com>
2018-10-20 13:43 ` Dave Cramer <pg@fastcrypt.com>
2018-10-20 14:44   ` David G. Johnston <david.g.johnston@gmail.com>
2018-10-20 17:16     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox