pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Simple queries with JDBC escaped scalar functions result in exceptions
7+ messages / 5 participants
[nested] [flat]

* Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-27 04:31  Adam Rauch <adam@labkey.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Adam Rauch @ 2016-08-27 04:31 UTC (permalink / raw)
  To: pgsql-jdbc

I upgraded to postgresql-9.4.1209.jar and fixed all the places in our 
code that used JDBC escaped scalar functions (e.g., {fn now()}) inside 
function definitions. However, we continued to see the telltale 
exception (org.postgresql.util.PSQLException: ERROR: syntax error at or 
near "{") when executing straightforward SELECT queries that included 
scalar functions. I tracked this down to comments; a simple repro is to 
execute the following query via JDBC:

    SELECT /* $ */ {fn curdate()}

The original comments included many characters, but the dollar sign 
seems to be the culprit. I think this indicates a problem with the 
parser that's attempting to detect string literals.

Thanks,
Adam

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 01:18  Dave Cramer <pg@fastcrypt.com>
  parent: Adam Rauch <adam@labkey.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Dave Cramer @ 2016-08-28 01:18 UTC (permalink / raw)
  To: Adam Rauch <adam@labkey.com>; +Cc: pgsql-jdbc

Looks like the JDBC mini parser is having troubles with that. Why do you
have comments in your sql ?

Dave Cramer

davec@postgresintl.com
www.postgresintl.com

On 27 August 2016 at 00:31, Adam Rauch <adam@labkey.com> wrote:

> I upgraded to postgresql-9.4.1209.jar and fixed all the places in our code
> that used JDBC escaped scalar functions (e.g., {fn now()}) inside function
> definitions. However, we continued to see the telltale exception (
> org.postgresql.util.PSQLException: ERROR: syntax error at or near "{")
> when executing straightforward SELECT queries that included scalar
> functions. I tracked this down to comments; a simple repro is to execute
> the following query via JDBC:
>
> SELECT /* $ */ {fn curdate()}
>
> The original comments included many characters, but the dollar sign seems
> to be the culprit. I think this indicates a problem with the parser that's
> attempting to detect string literals.
>
> Thanks,
> Adam
>
>

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 13:43  Andreas Joseph Krogh <andreas@visena.com>
  parent: Dave Cramer <pg@fastcrypt.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Andreas Joseph Krogh @ 2016-08-28 13:43 UTC (permalink / raw)
  To: Dave Cramer <pg@fastcrypt.com>; +Cc: Adam Rauch <adam@labkey.com>; pgsql-jdbc

På søndag 28. august 2016 kl. 03:18:42, skrev Dave Cramer <pg@fastcrypt.com 
<mailto:pg@fastcrypt.com>>:
Looks like the JDBC mini parser is having troubles with that. Why do you have 
comments in your sql ? 
 
I think the universial answer here is "Because you can". Many use comments in 
SQL as a way to both understand what's going on better (ie. when the SQL is 
generated by code) and to make logging easier. There's no reason why a driver 
should not cope with valid comments.
 
-- Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
andreas@visena.com <mailto:andreas@visena.com>
www.visena.com <https://www.visena.com;
 <https://www.visena.com;


 

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 14:20  Adam Rauch <adam@labkey.com>
  parent: Andreas Joseph Krogh <andreas@visena.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Adam Rauch @ 2016-08-28 14:20 UTC (permalink / raw)
  To: Andreas Joseph Krogh <andreas@visena.com>; Dave Cramer <pg@fastcrypt.com>; +Cc: pgsql-jdbc

In our development and test environments (but not production), our SQL 
generation engine inserts comments at strategic places in the queries it 
constructs, e.g., tagging a GUID with the real-world name of the object 
it represents or providing details about the requests from higher layers 
in the system that caused the query to be produced. When a query results 
in an exception, incorrect results, or poor performance, those comments 
help us better understand the query so we can fix or optimize it. Of 
course, this is legal and has been supported by the driver for many years.

The dollar sign ($) is the only case we came across in our test 
environments, but it looks like a single quote (') or double quote (") 
in a comment also trips up the parser. As do the two-character 
combinations {D, {E, {F, {O, and {T... and their lowercase equivalents. 
A full set of test queries that fail when executed via 
postgresql-9.4.1209.jar:

    SELECT /* " */ {fn curdate()};
    SELECT /* $ */ {fn curdate()};
    SELECT /* ' */ {fn curdate()};
    SELECT /* {D */ {fn curdate()};
    SELECT /* {E */ {fn curdate()};
    SELECT /* {F */ {fn curdate()};
    SELECT /* {O */ {fn curdate()};
    SELECT /* {T */ {fn curdate()};
    SELECT /* {d */ {fn curdate()};
    SELECT /* {e */ {fn curdate()};
    SELECT /* {f */ {fn curdate()};
    SELECT /* {o */ {fn curdate()};
    SELECT /* {t */ {fn curdate()};

Thanks,
Adam

On 8/28/2016 6:43 AM, Andreas Joseph Krogh wrote:
> På søndag 28. august 2016 kl. 03:18:42, skrev Dave Cramer 
> <pg@fastcrypt.com <mailto:pg@fastcrypt.com>>:
>
>     Looks like the JDBC mini parser is having troubles with that. Why
>     do you have comments in your sql ?
>
> I think the universial answer here is "Because you can". Many use 
> comments in SQL as a way to both understand what's going on better 
> (ie. when the SQL is generated by code) and to make logging easier. 
> There's no reason why a driver should not cope with valid comments.
>

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 15:00  Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  parent: Adam Rauch <adam@labkey.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Vladimir Sitnikov @ 2016-08-28 15:00 UTC (permalink / raw)
  To: Adam Rauch <adam@labkey.com>; Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-jdbc; Dave Cramer <pg@fastcrypt.com>

>The dollar sign ($) is the only case we came across in our test
environments, but it looks like a single quote (') or double quote (") in a
comment also trips up the parser. As do the two-character combinations {D,
{E, {F, {O, and {T... and their lowercase equivalents. A full set of test
queries that fail when executed via postgresql-9.4.1209.jar:

Thanks for the analysis.

I think
https://github.com/pgjdbc/pgjdbc/commit/2d9b313160dc5b73df2ae9001795592b37e8e8e0
should
fix the issue.

The fix is available in 9.4.1210-SNAPSHOT

Vladimir

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 17:02  Adam Rauch <adam@labkey.com>
  parent: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Adam Rauch @ 2016-08-28 17:02 UTC (permalink / raw)
  To: Vladimir Sitnikov <sitnikov.vladimir@gmail.com>; Andreas Joseph Krogh <andreas@visena.com>; +Cc: pgsql-jdbc; Dave Cramer <pg@fastcrypt.com>

Thanks, Vladimir! Local build of 9.4.1210-SNAPSHOT works great; all my 
legal test queries execute successfully now.

Just FYI, the new driver threw java.lang.ArrayIndexOutOfBoundsException 
with an _illegal_ query: SELECT /* /* */ 1

    java.lang.ArrayIndexOutOfBoundsException
         at
    java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:597)
         at java.lang.StringBuilder.append(StringBuilder.java:190)
         at org.postgresql.core.Parser.parseSql(Parser.java:1037)
         at org.postgresql.core.Parser.replaceProcessing(Parser.java:974)
         at
    org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:43)
         at
    org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:19)
         at org.postgresql.util.LruCache.borrow(LruCache.java:118)
         at
    org.postgresql.core.QueryExecutorBase.borrowQuery(QueryExecutorBase.java:261)
         at
    org.postgresql.jdbc.PgConnection.borrowQuery(PgConnection.java:146)
         at
    org.postgresql.jdbc.PgPreparedStatement.<init>(PgPreparedStatement.java:91)
         at
    org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:1259)
         at
    org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:1625)
         at
    org.postgresql.jdbc.PgConnection.prepareStatement(PgConnection.java:418)

I'm not suggesting this needs to be addressed... unbalanced comments 
will of course be rejected by the server anyway.

Thanks,
Adam

On 8/28/2016 8:00 AM, Vladimir Sitnikov wrote:
> >The dollar sign ($) is the only case we came across in our test environments, but it looks 
> like a single quote (') or double quote (") in a comment also trips up 
> the parser. As do the two-character combinations {D, {E, {F, {O, and 
> {T... and their lowercase equivalents. A full set of test queries that 
> fail when executed via postgresql-9.4.1209.jar:
>
> Thanks for the analysis.
>
> I think 
> https://github.com/pgjdbc/pgjdbc/commit/2d9b313160dc5b73df2ae9001795592b37e8e8e0 should 
> fix the issue.
>
> The fix is available in 9.4.1210-SNAPSHOT
>
> Vladimir

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

* Re: Simple queries with JDBC escaped scalar functions result in exceptions
@ 2016-08-28 17:17  danap <danap@itstriangle.com>
  parent: Dave Cramer <pg@fastcrypt.com>
  1 sibling, 0 replies; 7+ messages in thread

From: danap @ 2016-08-28 17:17 UTC (permalink / raw)
  To: pgsql-jdbc

Dave Cramer wrote:
> Looks like the JDBC mini parser is having troubles with that.

> Why do you have comments in your sql ?
>
> Dave Cramer
>

MyJSQLView SQL schema dump. All databases that my application supports
comments, and is documented.

--
-- MyJSQLView SQL Dump
-- Version: 7.07
-- WebSite: http://myjsqlview.com
--
-- Host: 127.0.0.1
-- Generated On: 2016.05.08 AD at 02:11:28 MDT
-- SQL version: PostgreSQL 9.4.1
-- Database: key_tables
--

-- ------------------------------------------

--
-- Table structure for table "public"."procedure_files"
--

DROP TABLE IF EXISTS "public"."procedure_files";
CREATE TABLE "public"."procedure_files" (
     "id" serial NOT NULL,
     "procedure_id" integer DEFAULT NULL,
     "file_name" varchar(null) DEFAULT NULL,
     "file_oid" oid DEFAULT NULL
);




-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



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


end of thread, other threads:[~2016-08-28 17:17 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-08-27 04:31 Simple queries with JDBC escaped scalar functions result in exceptions Adam Rauch <adam@labkey.com>
2016-08-28 01:18 ` Dave Cramer <pg@fastcrypt.com>
2016-08-28 13:43   ` Andreas Joseph Krogh <andreas@visena.com>
2016-08-28 14:20     ` Adam Rauch <adam@labkey.com>
2016-08-28 15:00       ` Vladimir Sitnikov <sitnikov.vladimir@gmail.com>
2016-08-28 17:02         ` Adam Rauch <adam@labkey.com>
2016-08-28 17:17   ` danap <danap@itstriangle.com>

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