Issue with Spring data jpa Db2 pagination - spring

I am using Spring JPA with DB2, when i use paging repository and queries for second page it throws error.
This is the generated query
SELECT *
FROM (SELECT inner2_.*,
ROWNUMBER()
OVER(
ORDER BY ORDER OF inner2_) AS rownumber_
FROM (SELECT db2DATAa0_.c_type AS col_0_0_,
db2DATAa0_.h_proc AS col_1_0_,
db2DATAa0_.n_vin AS col_2_0_,
db2DATAa0_.i_cust AS col_3_0_
FROM dcu.v_rpt_data_hist db2DATAa0_
WHERE db2DATAa0_.reportid = '0H000488089'
AND ( db2DATAa0_.c_type = 'S'
OR db2DATAa0_.c_type = 'N'
OR db2DATAa0_.c_type = 'A'
OR db2DATAa0_.c_type = 'T' )
ORDER BY db2DATAa0_.h_proc desc
FETCH first 30 ROWS only) AS inner2_) AS inner1_
WHERE rownumber_ > 15
ORDER BY rownumber_
Error:
2719372 [2016-10-21 16:29:02,040] [RxCachedThreadScheduler-13] WARN org.hibern
ate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -199, SQLState: 42601
2719379 [2016-10-21 16:29:02,047] [RxCachedThreadScheduler-13] ERROR org.hibern
ate.engine.jdbc.spi.SqlExceptionHelper - DB2 SQL Error: SQLCODE=-199, SQLSTATE=
42601, SQLERRMC=OF;??( [ DESC ASC NULLS RANGE CONCAT || / MICROSECONDS MICROSECO
ND, DRIVER=3.57.82
Any idea?

Your error states the ILLEGAL USE OF KEYWORD OF. TOKEN [DESC ASC NULLS RANGE CONCAT] WAS EXPECTED.
I identified this as the critical part of the query:
ORDER BY ORDER OF inner2_
DB2 expects one of DESC, ASC, NULLS, RANGE, CONCAT after the second ORDER keyword.

This issue can be resolve by change dialect.
Change dialect in configuration or property file to DB2ZOSDialect

Related

Column name is not passed to PostgreSQL on JDBC Scan in Apache Drill

While trying to run SQL query for PostgreSQL, instead of the column names from the table referred it it pushing down * to the database.
select
m.m_id,
cnt_c_no
from (
select
m_id
from pg_test_main.test1.table1
where
last_date >= '2019-01-01 00:00:00'
) as m
left join (
select
ci.m_id,
count(ci.c_no) as cnt_c_no
from (
select
m_id,
c_no
from pg_test.public.table2
) as ci
inner join (
select
c_no
from pg_test.public.table3
where
is_del = 'F'
) as c on ci.c_no = c.c_no
group by
ci.m_id
) as join1 on m.m_id = join1.m_id;
00-00 Screen
00-01 Project(m_id=[$0], cnt_c_no=[$1])
00-02 Project(m_id=[$0], cnt_c_no=[$2])
00-03 HashJoin(condition=[=($0, $1)], joinType=[left], semi-join: =[false])
00-05 Jdbc(sql=[SELECT "m_id" FROM "test1"."table1" WHERE "last_date" >= '2019-01-01 00:00:00' ])
00-04 Project(m_id0=[$0], cnt_c_no=[$1])
00-06 HashAgg(group=[{0}], cnt_c_no=[COUNT($1)])
00-07 Project(m_id=[$0], c_no=[$1])
00-08 HashJoin(condition=[=($1, $2)], joinType=[inner], semi-join: =[false])
00-10 Project(m_id=[$3], c_no=[$1])
00-12 Jdbc(sql=[SELECT * FROM "public"."table2" ])
00-09 Project(c_no0=[$0])
00-11 Project(c_no=[$0])
00-13 SelectionVectorRemover
00-14 Filter(condition=[=($60, 'F')])
00-15 Jdbc(sql=[SELECT * FROM "public"."table3" ])
As you can see, Jdbc Scan for table1 was using column names.
but, Jdbc Scan for table2 and table3 was not using column names. It pushed down * to the database.
How can I control jdbc scan so that it can push down colume names?
Apache Drill version is 1.16.0 (embedded-mode)
I tried to reproduce it with MySQL on both Drill 1.17 and Drill 1.15, but for the query, similar to the query you have specified, all the query is pushed into the JDBC storage:
SELECT m.person_id,
cnt_c_no
FROM
(SELECT person_id
FROM mysql.`drill_mysql_test1`.person1
WHERE date_field >= '2019-01-01 00:00:00') AS m
LEFT JOIN
(SELECT ci.person_id,
count(ci.last_name) AS cnt_c_no
FROM
(SELECT person_id,
last_name
FROM mysql.`drill_mysql_test`.person) AS ci
INNER JOIN
(SELECT last_name
FROM mysql.`drill_mysql_test`.person2
WHERE boolean_field = 'F' ) AS c ON ci.last_name = c.last_name
GROUP BY ci.person_id) AS join1 ON m.person_id = join1.person_id
The plan for this query:
00-00 Screen
00-01 Project(person_id=[$0], cnt_c_no=[$1])
00-02 Jdbc(sql=[SELECT `t0`.`person_id`, `t5`.`cnt_c_no` FROM (SELECT `person_id` FROM `drill_mysql_test1`.`person1` WHERE `date_field` >= '2019-01-01 00:00:00') AS `t0` LEFT JOIN (SELECT `t1`.`person_id`, COUNT(`t1`.`last_name`) AS `cnt_c_no` FROM (SELECT `person_id`, `last_name` FROM `drill_mysql_test`.`person`) AS `t1` INNER JOIN (SELECT `last_name` FROM `drill_mysql_test`.`person2` WHERE `boolean_field` = 'F') AS `t3` ON `t1`.`last_name` = `t3`.`last_name` GROUP BY `t1`.`person_id`) AS `t5` ON `t0`.`person_id` = `t5`.`person_id` ])
Could you please provide CTAS for Postgres tables, so I will try to reproduce it again with specific data types. Also, if possible, please check whether this issue is still reproduced on Drill 1.17.
UPD:
Comment under this answer helped to discover that this problem was caused by the following issue: https://issues.apache.org/jira/browse/DRILL-7340 and it will be resolved in Drill 1.18.0.

Laravel and CTE's

I'm trying to convert the following SQL query into Laravel code using taudenmeirs' laravel-cte package.
WITH `cte` AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY channel_id ORDER BY created_at DESC) AS row_number
FROM `Videos`
)
SELECT `Channels`.*, `cte`.*
FROM `Channels`
LEFT JOIN `cte`
ON `Channels`.`id` = `cte`.`channel_id`
WHERE `cte`.`row_number` = 1;
The problem is I keep getting the following response from the server:
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or
access violation: 1140 Mixing of GROUP columns
(MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there
is no GROUP BY clause (SQL: with cte as (select , ROW_NUMBER() OVER
(PARTITION BY channel_id ORDER BY created_at DESC) AS row_number from
Videos) select Channels. from Channels left join cte on
Channels.id = cte.channel_id where cte.rn = 1) in file
/Users/mark/Workspace/api.site/vendor/laravel/framework/src/Illuminate/Database/Connection.php
on line 665
I actually got this answer from taudenmeirs over on github where I submitted this issue as a bug. It turns out that the error is caused by a bug in MariaDB: https://jira.mariadb.org/browse/MDEV-17785
The ONLY_FULL_GROUP_BY mode is enabled in Laravel by default. You can disable it by setting 'strict' => false in your config/database.php file.
This actually does solve the issue. However it would be better if the fine folks over at Maria could solve the underlying bug.

Teradata to Oracle Query eaxdata Conversion

I need help with converting the below SQL query in Taradata to Oracle exadata. Not sure how to convert the date, I changed the CAST date function To_Date but get a few errors.
SELECT DISTINCT
pat.pat_id,
pat.pat_mrn_id AS patientmrn,
pat.pat_name AS patientname,
adt_pat_class_c,
pat.death_date AS deathdate,
cast(patenc.hosp_admsn_time AS DATE format 'mm/dd/yyyy') AS
admitdate,
cast(patenc.hosp_disch_time AS DATE format 'mm/dd/yyyy') AS
dischargedate,
extract(year FROM acct.adm_date_time)-extract(year FROM
pat.birth_date) - CASE WHEN acct.adm_date_time (format
'MMDD')CHAR(4))
<pat.birth_date (format 'MMDD') (CHAR(4)) THEN 1 ELSE 0
END AS patage,
adt_billing_type_c,
adt_patient_stat_c,
hosp_admsn_type_c,
acct_basecls_ha_c,
ordproc.order_proc_id AS order_id,
ordproc.ordering_date
FROM patient pat
inner join pat_enc_hsp patenc ON pat.pat_id = patenc.pat_id
inner join hsp_account acct ON acct.prim_enc_csn_id = patenc.pat_enc_csn_id
inner join order_proc ordproc ON acct.prim_enc_csn_id ON
ordproc.pat_enc_csn_id
inner join clarity_ser ser ON ser.prov_id = ordproc.authrzing_prov_id
inner join identity_ser_id idser ON ser.prov_id=idser.prov_id
inner join clarity_loc loc ON loc.loc_id = acct.loc_id
inner join zc_loc_rpt_grp_7 grp7 ON loc.rpt_grp_seven = grp7.rpt_grp_seven
WHERE grp7.name = 'AB'
AND cast(patenc.hosp_disch_time AS DATE format 'mm/dd/yyyy') >= '01/01/2019'
AND admit_conf_stat_c IN (1,4)
AND description LIKE '%CULTURE%'
AND ordproc.lab_status_c = 3
AND adt_pat_class_c IN ('1204','12113')
AND result_time > patenc.hosp_disch_time;
I changed the top part of the query(one with dates) -
select distinct
pat.pat_id,
pat.PAT_MRN_ID as PatientMRN,
pat.PAT_NAME as PatientName,
ADT_PAT_CLASS_C,
pat.DEATH_DATE as DeathDate,
TO_DATE(patenc.HOSP_ADMSN_TIME, 'mm/dd/yyyy') as AdmitDate,
TO_DATE(patenc.HOSP_DISCH_TIME, 'mm/dd/yyyy') as DischargeDate----
I get the below errors -
ORA-12801: error signaled in parallel query server P02G, instance nzcladb01xm.nndc.kp.org:CDB001N41 (1)
ORA-01843: not a valid month
12801. 00000 - "error signaled in parallel query server %s"
*Cause: A parallel query server reached an exception condition.
*Action: Check the following error message for the cause, and consult
your error manual for the appropriate action.
*Comment: This error can be turned off with event 10397, in which
case the server's actual error is signaled instead.

using subquery factoring result in where clause

Why can't I use a subquery factoring clause result in the where clause of as depicted in the following sql:
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = rpt.id
and
tg.gene not in('TMB','MS')
The subquery is named rptand used in the select statement's where clause. When executed encountering the following error: ORA-00904: "RPT"."ID": invalid identifier
UPDATE:
In fact nested query for the same thing is also giving me the same issue. The nested subquery is only returning a single column value from a single row:
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
where
rt.reportid = (select id from reports where caseid = :case_id and
rownum=1 order by created desc)
and
tg.gene not in('TMB','MS')
You missed to add the table rpt in your query, thus that error.
with rpt as(
select * from reports where caseid =
:case_id and rownum=1 order by created desc
)
select
distinct rt.trialid
from
report_trials rt
join
trial_genes tg on rt.id=tg.trialid
join
rpt on rt.reportid = rpt.id
where
tg.gene not in('TMB','MS')

oracle with clause error in adodb

I am not able to use the with clause in oracle adodb connection. I have tried both wrapping the qry is a select * from (myquery) and adding a , before my with clause. All create errors.
Here is the query
`WITH TEMPa AS (select _nbr, sum(ending) as Cost from t1 group by _nbr),
TEMPB AS (select _nbr, cost,entity
from t2
where end_tms is null and sale_date is null and entity = 110)
SELECT
TEMPA._nbr
,TEMPA.cost
,TEMPb._nbr
,TEMPB._cost
,tempb.entity
,(tempb._cost - tempa.cost) as Difference
FROM TEMPA, TEMPB
WHERE TEMPA._nbr = TEMPB._nbr(+)
and tempa.cost <> tempb.cost`
Any help would be awesome!

Resources