Scala Slick incompatible with Kerberos? - scala

We have an Scala Play application, where its used both plain queries with jdbc and Slick ORM.
From 2 weeks, my team migrated all databases on Kerberos, so I was forced to update the connection settings. All good using Connection from Java for our plain queries, but encountered problems after updating connection string for Slick.
Encountered: STRING LITERAL
Expected: DEFAULT, IDENTIFIER
CAUSED BY: Exception: Syntax error
), Query: select "col1", "col2", "col3" from "tableName".
[^[[31merror^[[0m] d.d.s.ClassName- 1620996573139 -
[Cloudera][ImpalaJDBCDriver](500051) ERROR processing query/statement.
Error Code: 0, SQL state:
TStatus(statusCode:ERROR_STATUS, sqlState:HY000, errorMessage:ParseException:
Syntax error in line 1: ...col", "col" from "tableName"
I copied the query generated by Slick and execute it. All columns are writen between quotation mark and the query results will contains only column names as values:
select "col1", "col2"
from "tableName"
limit 10;
results in:
col1, col2
col1, col2
col1, col2
Can be this the problem? And how I can specify to Slick to remove quotation marks from query, if is so?!
le: I forgot to specify: is a Impala Kudu database
thanks

After a minutious debug, I found that Slick throw a NonFatal error with the message: "Cannot initialize ExecutionContext; AsyncExecutor already shut down";
I can't understand why/what is that. Anyway, I rennounced at slick and wrote plain sql using jdbc to execute it.

Related

Scala anorm running query to postgreSQL with set role command

I need to execute query in PostgreSQL using anorm. In order to handle authorization I have to set role along with query. My query is as follows.
set role myuser; select country from country_list
This works fine when executed directly in postgress. But when tried via anorm gives me no result found exception PSQLException: No results were returned by the query.
But this is working fine when set role command is removed.
Code working fine
import anorm.SqlParser._
SQL("select country from country_list").as(str("name").*)
Code throwing exception
import anorm.SqlParser._
SQL("set role myuser; select country from country_list").as(str("name").*)
Exception
org.postgresql.util.PSQLException: No results were returned by the query.
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:115)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
at anorm.Sql.$anonfun$resultSet$2(Anorm.scala:78)
Did you try by running the 2 queries one after the other? Usually libraries like Anorm expect one query at a time.
SQL("set role myuser").execute()
SQL("select country from country_list").as(str("name").*)

Teradata & Tableau Query Issue

Getting a following error on Tableau for my Teradata Query
[Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: ORDER BY is not allowed in subqueries.
Even after removing the ORDERBY, I get the following error
[Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: All expressions in a derived table must have an explicit name.
Pleas see my query
Tableau, Teradata and ORDER BY don't jive, but you can order once you're in Tableau without the ORDER BY in the query.
For the explicit name, you need to be sure all aggregated columns are given a column name or alias.

Could not execute query in spring boot framework

SELECT * FROM work_hour where start_time between '2020-04-06 23:03' and '2020-04-09 23:03';
This works when set search_path to foo
But Ii want to use the same query instead of strings I want to use parameters
So i'm using this annotation in my service function:
#Query(value = "SELECT s FROM foo.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')\\:\\:timestamp BETWEEN :start_time\\:\\:timestamp AND now()\\:\\:timestamp", nativeQuery = true)
But this seems to not work.
There was an unexpected error (type=Internal Server Error, status=500). could not execute query; SQL [SELECT s FROM logines.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')::timestamp BETWEEN ?::timestamp AND now()::timestamp]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.
How it can be done?
As that is a native query, you need to use proper (native) SQL syntax.
select s from work_hour s
results in a result that has a single column which is a record with multiple fields. But you want multiple columns, so you have to use
select s.*
from work_hour s
See the difference in the output in this online example

Replace-function via EclipseLink on DB2 z/OS

I am migrating an application from JPA 2.0 with OpenJPA on WebSphere 8.5 to JPA 2.1 with EclipseLink on WebSphere 9.0, using DB2 12 on z/OS. Generally it is working, but one rather complex query is failing. I could localize the problem to the usage of a custom DB2-function call within a criteria-query. The call looks something like this:
criteriaBuilder.function("REPLACE", String.class, fromMyEntity.get("myField"), criteriaBuilder.literal("a"), criteriaBuilder.literal("b"));
This produces the following error (had to translate some error texts, since WebSphere localizes them, and anonymize my field/table names, so labels/names might not be 100% exact):
Error: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.3.WAS-v20160414-bd51c70): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.ibm.websphere.ce.cm.StaleConnectionException: DB2 SQL Error: SQLCODE=-171, SQLSTATE=42815, SQLERRMC=2;REPLACE, DRIVER=3.72.44
Errorcode: -171
Call: SELECT COUNT(ID) FROM MY_TABLE WHERE REPLACE(MYFIELD, ?, ?) LIKE ?
bind => [abc, def, %g%]
Query: ReportQuery(referenceClass=MyEntity sql="SELECT COUNT(ID) FROM MY_TABLE WHERE REPLACE(MYFIELD, ?, ?) LIKE ?").
What really confuses me, if I take the generated query, replace the placeholders with the given bound parameters, and execute that in a database client myself, it works without error.
The documentation states, the first parameter must not be empty (https://www.ibm.com/support/knowledgecenter/en/SSEPEK_12.0.0/sqlref/src/tpc/db2z_bif_replace.html), and indeed if I use an empty string either as a literal in the query or in my database client, it will produce the above error. But none of the rows in the database contain an empty value. There are checks in place to prevent this in the old environment, but they don't appear to work with the new environment, so I disabled them while searching for the problem, and made sure myself no empty values exist. I can even use the primary key as the first parameter, and it will still fail, and that can't even contain an empty/null value.
Using other functions (like TRANSLATE) works, I also tried using "SYSIBM.REPLACE" as name, and different combinations of parameters, but as soon as I use a real column to replace data in, it fails. Anybody got any ideas what I am doing wrong here?
This is my table definition:
CREATE TABLE "MY_TABLE" (
"ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (NO MINVALUE NO MAXVALUE NO CYCLE CACHE 20 NO ORDER ),
"MYFIELD" VARCHAR(160) FOR MIXED DATA WITH DEFAULT NULL,
[....]
) IN "<Database>"."<Tablespace>" PARTITION BY SIZE EVERY 4 G AUDIT NONE DATA CAPTURE NONE CCSID UNICODE;

OpenJpa how to find length of string in JPQL

I am using
length(ze.string)>2 in openJpa query. but i am getting
SQLCODE=-440, SQLSTATE=42884, SQLERRMC=CHAR_LENGTH;FUNCTION, DRIVER=3.53.95 {prepstmnt 1776269692 SELECT t0.f1, t0.f2, t0.f3, t0.f4, t0.f5, t0.f6, t0.f7, t0.f8, t0.f9, t0.f10, t0.f11, t0.f12, t0.f13, t0.f14, t0.f15, t0.f16, t0.f17 FROM table t0 WHERE (t0.f1 = ? AND CHAR_LENGTH(?) > ? AND .....
In plain query when i do length operation i am getting record but using jpa its not working. I looked Here used size it doesn't work. and the field is varchar and db2. trying from past 1 hour.
DB2 requires use of the SQL function LENGTH, yet OpenJPA seems to be incorrectly converting your JPQL to use SQL function CHAR_LENGTH (hence the error message - not that DB2 gives out clear messages saying what is wrong, who knows what SQLCODE=-440 is without having to search!!).
Raise a bug on your JPA provider.
See https://www.ibm.com/support/knowledgecenter/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000818.html
You would need to give more details about your entity, persistence.xml, and query to get to the bottom or this. However, I do not see how OpenJPA would use CHAR_LENGTH instead of LENGTH for DB2. Let me explain. If you look at DBDictionary here:
https://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java?view=markup
You can see it defines something called "stringLengthFunction" as follows:
public String stringLengthFunction = "CHAR_LENGTH({0})";
This is the string length function which should be used for each individual dictionary (i.e. Database config). However, for DB2, the AbstractDB2Dictionary, see here:
https://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/AbstractDB2Dictionary.java?view=markup
overrides this as follows:
stringLengthFunction = "LENGTH({0})";
Given this, for DB2, LENGTH should be used. I took the following simple query:
"select me.id from MyEntity me where length(me.name)>2"
And executed it on OpenJPA using DB2, and I got this:
SELECT t0.ID FROM MYENTITY t0 WHERE (CAST(LENGTH(t0.ID) AS BIGINT) > CAST(? AS BIGINT)) [params=(long) 2]
Thanks,
Heath Thomann