JDBI query when using type annotations? - postgresql

I'm writing a Dropwizard app that needs to connect to the database, and using SQL objects to query the DB, per the Dropwizard docs.
The issue I'm running into is when my query is using a type specifier (::) being confused for a binding variable. Something like
SELECT
(a,
b,
c)::user_type
FROM ...
WHERE id = :id
The parser is reading ::user_type as a parameter placeholder. When I've escaped them with \, it says the query is not returning any rows, but if I run the query through psql, it does, so I'm guessing it's not so simple. Any help would be appreciated.
Thanks! :D

The easiest solution is to just use the SQL-standard CAST syntax not the PostgreSQL extension :: syntax.
CAST (c AS user_type)

Related

Redshift Spectrum table doesnt recognize array

I have ran a crawler on json S3 file for updating an existing external table.
Once finished I checked the SVL_S3LOG to see the structure of the external table and saw it was updated and I have new column with Array<int> type like expected.
When I have tried to execute select * on the external table I got this error: "Invalid operation: Nested tables do not support '*' in the SELECT clause.;"
So I have tried to detailed the select statement with all columns names:
select name, date, books.... (books is the Array<int> type)
from external_table_a1
and got this error:
Invalid operation: column "books" does not exist in external_table_a1;"
I have also checked under "AWS Glue" the table external_table_a1 and saw that column "books" is recognized and have the type Array<int>.
Can someone explain why my simple query is wrong?
What am I missing?
Querying JSON data is a bit of a hassle with Redshift: when parsing is enabled (eg using the appropriate SerDe configuration) the JSON is stored as a SUPER type. In your case that's the Array<int>.
The AWS documentation on Querying semistructured data seems pretty straightforward, mentioning that PartiQL uses "dotted notation and array subscript for path navigation when accessing nested data". This doesn't work for me, although I don't find any reasons in their SUPER Limitations Documentation.
Solution 1
What I have to do is set the flags set json_serialization_enable to true; and set json_serialization_parse_nested_strings to true; which will parse the SUPER type as JSON (ie back to JSON). I can then use JSON-functions to query the data. Unnesting data gets even crazier because you can only use the unnest syntax select item from table as t, t.items as item on SUPER types. I genuinely don't think that this is the supposed way to query and unnest SUPER objects but that's the only approach that worked for me.
They described that in some older "Amazon Redshift Developer Guide".
Solution 2
When you are writing your query or creating a query Redshift will try to fit the output into one of the basic column data types. If the result of your query does not match any of those types, Redshift will not process the query. Hence, in order to convert a SUPER to a compatible type you will have to unnest it (using the rather peculiar Redshift unnest syntax).
For me, this works in certain cases but I'm not always able to properly index arrays, not can I access the array index (using my_table.array_column as array_entry at array_index syntax).

On laravel using builder query whereRaw ?&

I have problem using query whereraw on laravel 5.5 postgresql, for this case i want to select data by colors. Data example
Source postgresql documentation postgres. I'm success to try on execute sql like this success example execute query. But fail using laravel example source code. Error on laravel
The problem is that your statement contains a question mark. When using the whereRaw method, the question mark is an expected parameter, which aren't providing in your call.
However it seems that there isn't a real solution for this issue. I suggest you take a look at Question mark operator in query, it handles about a similar issue.

Calling Postgres function using QueryDSL

I have a package with similarity functions installed on my Postgres DB. I was calling them using pure SQL on a JDBC, and it was working as expected.
Now I'm trying to refactor my code to use QueryDSL. The first step of the similarity function is set a limit, between 0 and 1, so that only results above that given threshold are returned. I'm trying this way:
NumberExpression<Float> sim = Expressions.numberTemplate(Float.class, "set_limit({0})", "0.2");
query.singleResult(sim);
I get the following error:
java.lang.IllegalArgumentException: No joins given
I know I haven't passed an EntityPath. If I try and use any EntityPath, I get the error:
`[[IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode
\-[METHOD_CALL] MethodNode: '('
+-[METHOD_NAME] IdentNode: 'set_limit' {originalText=set_limit}
\-[EXPR_LIST] SqlNode: 'exprList'
\-[NAMED_PARAM] ParameterNode: '?' {name=1, expectedType=null}
]]`
After that, I'd call another DB function, but I guess the solution would be the same.
Is this possible with QueryDSL?
set_limit isn't valid JPQL, that's why Hibernate throws an exception. Querydsl JPA maps internally to JPQL, and Querydsl SQL to SQL.
If you need SQL expressivity, like in this case, you will need to use Querydsl SQL instead.

How to convert Lambda Expression to SQL Server Query syntax?

Here's the lambda expression, I want to convert this to SQL Server query syntax.
{x => ((True AndAlso x.Name.ToLower().Contains("_")) AndAlso Not(x.IsDeleted))}
Note : The lambda expression is equivalent to Where Clause of Sql server.
I want to convert it to sql syntax and then pass it to sql server stored procedure.
Is there any way I can achieve this?
Generally, you can use the ToString method on the IQueryable object returned by the LINQ statement to find the exact query that would be executed on the database. But, in this case, I would guess something like this might be generated for the WHERE:
WHERE CONTAINS(Name, '_') AND NOT IsDeleted
But, you haven't provided any detail that would allow me to verify that.
If you don't have full-text on, then the following might be more applicable:
WHERE Name like '%_%' AND NOT IsDeleted

JPA call Store Procedure returned wrong result

I am using JPA native query to call a stored procedure and map the return result to a class
createNativeQuery(String sqlString, Class resultClass);
Here sqlString is a stored procedure in this format:
{call storeProcedureName parameter1, paramter2, parameter3}
I noticed that when one of the parameter contains a forward slash (/), the result result will be wrong. Has any body encountered this problem before and how to solve it? thanks
I am using EclipseLink and glassfish server.
Are you in-lining the parameters into your SQL, or using parameters on your query? You should use parameters on your query, in-lining parameters into SQL is very bad (can lead to SQL injection attacks).
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Native#Parameters