Why is my query failing with SQLCODE=-204? - db2

I'm writing some SQL for a report (this is my first time actually writing SQL so don't slam my syntax too much, lol) and the subquery seems to be failing. I'm not sure why because the report compiles just fine and I can get it into Jaspersoft Studio before it errors out when trying to preview it. Am I even doing the subquery correctly?
Thanks in advance.
SELECT
PERSON.LAST_NAME,
ACCOUNT.ACCOUNT_NUMBER,
SHARE_ESCROW.ID,
LOAN.PAYMENT_DUE_DATE,
LN_IMPOUND_ANALYSIS.CUSHION_AMOUNT,
LN_IMPOUND_ANALYSIS.NEXT_P_AND_I_AMOUNT,
LOAN.IMPOUND_AMOUNT,
LN_IMPOUND_ANALYSIS.NEXT_IMPOUND_AMOUNT,
SHARE_ESCROW.BALANCE,
SHARE_ESCROW.DESCRIPTION,
SHARE_ESCROW.CLOSE_DATE,
i.LTV,
CASE LOAN.PAYMENT_METHOD
WHEN 'T' THEN 'Automated Transfer'
WHEN 'A' THEN 'ACH'
WHEN 'C' THEN 'Cash'
END AS PAYMENT_METHOD
FROM
CORE.ACCOUNT AS ACCOUNT INNER JOIN CORE.LOAN AS LOAN ON ACCOUNT.SERIAL = LOAN.PARENT_SERIAL INNER JOIN
CORE.LN_IMPOUND_ANALYSIS AS LN_IMPOUND_ANALYSIS ON LOAN.SERIAL = LN_IMPOUND_ANALYSIS.PARENT_SERIAL INNER JOIN
CORE.SHARE AS SHARE_ESCROW ON ACCOUNT.SERIAL = SHARE_ESCROW.PARENT_SERIAL INNER JOIN
CORE.PERSON AS PERSON ON PERSON.SERIAL = ACCOUNT.PRIMARY_PERSON_SERIAL,
(SELECT
CASE COLLATERAL.AMOUNT
WHEN 0 THEN 0
WHEN NULL THEN 0
ELSE LOAN.BALANCE / COLLATERAL.AMOUNT
END AS LTV
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN LOAN ON LOAN.SERIAL = COLLATERAL.PARENT_SERIAL
) i
WHERE
SHARE_ESCROW.DESCRIPTION = 'Escrow Share' AND
SHARE_ESCROW.CLOSE_DATE IS NULL AND
i.LTV != 0
Edit:
Here's the error:
net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:466)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$18(ReportControler.java:441)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler$4.run(ReportControler.java:333)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at net.sf.jasperreports.engine.fill.JRFillSubreport.prepare(JRFillSubreport.java:809)
at net.sf.jasperreports.components.table.fill.FillTableSubreport.prepareSubreport(FillTableSubreport.java:156)
at net.sf.jasperreports.components.table.fill.FillTable.prepare(FillTable.java:400)
at net.sf.jasperreports.engine.fill.JRFillComponentElement.prepare(JRFillComponentElement.java:151)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.prepareElements(JRFillElementContainer.java:332)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:384)
at net.sf.jasperreports.engine.fill.JRFillBand.fill(JRFillBand.java:358)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummaryNoLastFooterSamePage(JRVerticalFiller.java:1102)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillSummary(JRVerticalFiller.java:1065)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportEnd(JRVerticalFiller.java:329)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:159)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)
at net.sf.jasperreports.engine.fill.BaseFillHandle$ReportFiller.run(BaseFillHandle.java:120)
at java.lang.Thread.run(Unknown Source)
Caused by: net.sf.jasperreports.engine.JRException: Error executing SQL statement for : Escrow32Analysis324532WIP_TableDataset_1579697577915_289752
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:240)
at net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1114)
at net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:691)
at net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1314)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:931)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:873)
at net.sf.jasperreports.engine.fill.JRFillSubreport.fillSubreport(JRFillSubreport.java:665)
at net.sf.jasperreports.engine.fill.JRSubreportRunnable.run(JRSubreportRunnable.java:59)
at net.sf.jasperreports.engine.fill.AbstractThreadSubreportRunner.run(AbstractThreadSubreportRunner.java:203)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
... 1 more
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=CMURPHY.LOAN, DRIVER=4.13.127
at com.ibm.db2.jcc.am.id.a(id.java:677)
at com.ibm.db2.jcc.am.id.a(id.java:60)
at com.ibm.db2.jcc.am.id.a(id.java:127)
at com.ibm.db2.jcc.am.no.c(no.java:2653)
at com.ibm.db2.jcc.am.no.d(no.java:2641)
at com.ibm.db2.jcc.am.no.a(no.java:2090)
at com.ibm.db2.jcc.am.oo.a(oo.java:7671)
at com.ibm.db2.jcc.t4.cb.h(cb.java:141)
at com.ibm.db2.jcc.t4.cb.b(cb.java:41)
at com.ibm.db2.jcc.t4.q.a(q.java:32)
at com.ibm.db2.jcc.t4.sb.i(sb.java:135)
at com.ibm.db2.jcc.am.no.ib(no.java:2059)
at com.ibm.db2.jcc.am.oo.sc(oo.java:3559)
at com.ibm.db2.jcc.am.oo.b(oo.java:4348)
at com.ibm.db2.jcc.am.oo.fc(oo.java:743)
at com.ibm.db2.jcc.am.oo.executeQuery(oo.java:713)
at net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.createDatasource(JRJdbcQueryExecuter.java:233)
... 11 more

The message you see in the log:
DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=CMURPHY.LOAN
means that the object (in your case a table) LOAN in the schema CMURPHY cannot be found (SQLCODE -204). As others have mentioned in comments, the only table reference in your query that is missing the explicit schema is this:
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN LOAN ON LOAN.SERIAL ...
---------------------------------------------^^^^
so the database server by default looks for that table in the default schema, the one matching your user ID (CMURPHY). Apparently it's not there.
Add the explicit schema name to the table reference:
FROM
CORE.COLLATERAL AS COLLATERAL INNER JOIN CORE.LOAN AS LOAN ON LOAN.SERIAL ...

Related

Getting error when I try to create an iceberg table using dataFrame.write() in spark and store it in a cloud Filesystem source

Following is the script I wrote:
var df2 = spark.read.parquet("<file_path>")
df2.write.format("iceberg").save(<destination_path>)
When I ran the script I am getting the following error:
RuntimeException: Failed to get table info from metastore gs://dremio-qa/flatten.listofstructwithnulls30_iceberg
Caused by: MetaException: Exception thrown when executing query : SELECT DISTINCT 'org.apache.hadoop.hive.metastore.model.MTable' AS NUCLEUS_TYPE,A0.CREATE_TIME,A0.LAST_ACCESS_TIME,A0.OWNER,A0.RETENTION,A0.IS_REWRITE_ENABLED,A0.TBL_NAME,A0.TBL_TYPE,A0.TBL_ID FROM TBLS A0 LEFT OUTER JOIN DBS B0 ON A0.DB_ID = B0.DB_ID WHERE A0.TBL_NAME = ? AND B0.`NAME` = ?
Caused by: JDOException: Exception thrown when executing query : SELECT DISTINCT 'org.apache.hadoop.hive.metastore.model.MTable' AS NUCLEUS_TYPE,A0.CREATE_TIME,A0.LAST_ACCESS_TIME,A0.OWNER,A0.RETENTION,A0.IS_REWRITE_ENABLED,A0.TBL_NAME,A0.TBL_TYPE,A0.TBL_ID FROM TBLS A0 LEFT OUTER JOIN DBS B0 ON A0.DB_ID = B0.DB_ID WHERE A0.TBL_NAME = ? AND B0.`NAME` = ?
Caused by: SQLSyntaxErrorException: (conn=65340) Unknown column 'A0.IS_REWRITE_ENABLED' in 'field list'
Caused by: MariaDbSqlException: Unknown column 'A0.IS_REWRITE_ENABLED' in 'field list'
Caused by: SQLException: Unknown column 'A0.IS_REWRITE_ENABLED' in 'field list'
data.write
.format("iceberg")
.mode("append")
.save("db.table")
You should user db.table insteand of '<destination_path>'

Implicit conversion of UUID to character varying when using TEIID with postgres

I have a VDB containing a view with a UUID column with this definition
<vdb name="_GENERATED_users_info" version="6">
<import-vdb name="_INTERNAL_internal" version="1"/>
<model name="GEN_view" type="VIRTUAL">
<metadata type="DDL">
<![CDATA[ CREATE VIEW "_view_workflow_data" ("uuid" VARCHAR, "email" VARCHAR) OPTIONS (UPDATABLE 'true') AS SELECT "public"."security_users_info"."uuid" AS "uuid", "public"."security_users_info"."email" AS "email" FROM "public"."security_users_info" ]]></metadata>
</model>
</vdb>
Column security_users_info.uuid is of type UUID.
When I try to execute a query
SELECT "uuid", "email" FROM "_view_workflow_data" WHERE ("uuid" IN (?''718e52dd-058a-4306-9213-c999181163c7') AND 1 = 1)
I get an error org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = character varying. The error doesn't come from the query I sent but from a query generated by TEIID executed on the physical table
TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?]
I tried to convert the column in the view definition to varchar like this CONVERT("public"."security_users_info"."uuid", VARCHAR) AS "uuid" but I get the same error.
The failing query works fine directly in postgresql as it implicitly converts the uuid text into a uuid type.
I'm aware of the section about UID types in http://teiid.github.io/teiid-documents/master/content/reference/as_jdbc-translators.html but as the implicit conversion seems to work when I execute the query directly in postgresql, I would expect that section not to apply in this situation.
I'm using TEIID 16 with postgresql 9.5 and postgres JDBC driver 42.2.
Am I doing something in a wrong way or is this not supposed to work?
Whole stacktrace:
ERROR# - org.jooq.exception.DataAccessException: SQL [select "uuid", "email" from "_view_workflow_data" where ("uuid" in (?) and 1 = 1)]; TEIID30504 Remote org.teiid.core.TeiidProcessingException: TEIID30504 _SOURCE_7772c4bdad42f2468b3ecdc71d89634bd9b80ff2ca87900862ab740105049694: 0 TEIID11008:TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?] (c.m.w.c.d.DataObjectVerticle#?)org.jooq.exception.DataAccessException: SQL [select "uuid", "email" from "_view_workflow_data" where ("uuid" in (?) and 1 = 1)]; TEIID30504 Remote org.teiid.core.TeiidProcessingException: TEIID30504 _SOURCE_7772c4bdad42f2468b3ecdc71d89634bd9b80ff2ca87900862ab740105049694: 0 TEIID11008:TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?]
at org.jooq_3.13.0-SNAPSHOT.DEFAULT.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2717)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:755)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:382)
at com.mathesio.workflow.core.dataObject.DataObjectService.lambda$select$16(DataObjectService.java:365)
at com.mathesio.teiid.Connections.ConnectionManager.lambda$getConnectionJOOQ$0(ConnectionManager.java:187)
at org.jooq.impl.DefaultDSLContext$3.run(DefaultDSLContext.java:609)
at org.jooq.impl.DefaultDSLContext$3.run(DefaultDSLContext.java:606)
at org.jooq.impl.DefaultDSLContext.lambda$transactionResult0$0(DefaultDSLContext.java:537)
at org.jooq.impl.Tools$12$1.block(Tools.java:4847)
at java.base/java.util.concurrent.ForkJoinPool.managedBlock(ForkJoinPool.java:3128)
at org.jooq.impl.Tools$12.get(Tools.java:4844)
at org.jooq.impl.DefaultDSLContext.transactionResult0(DefaultDSLContext.java:589)
at org.jooq.impl.DefaultDSLContext.transactionResult(DefaultDSLContext.java:506)
at org.jooq.impl.DefaultDSLContext.transaction(DefaultDSLContext.java:606)
at com.mathesio.teiid.Connections.ConnectionManager.lambda$getConnectionJOOQ$1(ConnectionManager.java:186)
at com.mathesio.workflow.core.database.transaction.TransactionJPA.lambda$new$42(TransactionJPA.java:1813)
at com.mathesio.workflow.core.database.transaction.TransactionJPA.lambda$new$19(TransactionJPA.java:388)
at com.mathesio.workflow.core.database.transaction.TransactionJPA.runCore(TransactionJPA.java:993)
at com.mathesio.workflow.core.database.transaction.TransactionJPA.lambda$run$25(TransactionJPA.java:1306)
at com.mathesio.workflow.core.database.transaction.TransactionJPA$1.call(TransactionJPA.java:755)
at com.mathesio.workflow.core.database.transaction.TransactionJPA$1.call(TransactionJPA.java:738)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at com.mathesio.workflow.core.database.transaction.PriorityFuture.run(PriorityFuture.java:58)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.teiid.jdbc.TeiidSQLException: TEIID30504 Remote org.teiid.core.TeiidProcessingException: TEIID30504 _SOURCE_7772c4bdad42f2468b3ecdc71d89634bd9b80ff2ca87900862ab740105049694: 0 TEIID11008:TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?]
at org.teiid.jdbc.TeiidSQLException.create(TeiidSQLException.java:131)
at org.teiid.jdbc.TeiidSQLException.create(TeiidSQLException.java:67)
at org.teiid.jdbc.StatementImpl.postReceiveResults(StatementImpl.java:783)
at org.teiid.jdbc.StatementImpl.access$100(StatementImpl.java:62)
at org.teiid.jdbc.StatementImpl$2.onCompletion(StatementImpl.java:581)
at org.teiid.client.util.ResultsFuture.done(ResultsFuture.java:131)
at org.teiid.client.util.ResultsFuture.access$200(ResultsFuture.java:36)
at org.teiid.client.util.ResultsFuture$1.receiveResults(ResultsFuture.java:75)
at org.teiid.net.socket.SocketServerInstanceImpl.receivedMessage(SocketServerInstanceImpl.java:281)
at org.teiid.net.socket.SocketServerInstanceImpl.read(SocketServerInstanceImpl.java:333)
at org.teiid.net.socket.SocketServerInstanceImpl$RemoteInvocationHandler$1.get(SocketServerInstanceImpl.java:423)
at org.teiid.jdbc.StatementImpl.executeSql(StatementImpl.java:590)
at org.teiid.jdbc.PreparedStatementImpl.execute(PreparedStatementImpl.java:240)
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:209)
at org.jooq.impl.Tools.executeStatementAndGetFirstResultSet(Tools.java:3935)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:294)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:368)
... 23 common frames omitted
Caused by: org.teiid.core.TeiidProcessingException: TEIID30504 Remote org.teiid.core.TeiidProcessingException: TEIID30504 _SOURCE_7772c4bdad42f2468b3ecdc71d89634bd9b80ff2ca87900862ab740105049694: 0 TEIID11008:TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?]
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.DataTierTupleSource.exceptionOccurred(DataTierTupleSource.java:397)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.DataTierTupleSource.nextTuple(DataTierTupleSource.java:157)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.relational.AccessNode.nextBatchDirect(AccessNode.java:401)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.relational.RelationalNode.nextBatch(RelationalNode.java:277)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.relational.RelationalPlan.nextBatch(RelationalPlan.java:139)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.QueryProcessor.nextBatchDirect(QueryProcessor.java:147)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.QueryProcessor.nextBatch(QueryProcessor.java:110)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.BatchCollector.collectTuples(BatchCollector.java:160)
at org.jboss.teiid#16.0.0//org.teiid.query.processor.BatchCollector.collectTuples(BatchCollector.java:142)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.RequestWorkItem.processMore(RequestWorkItem.java:491)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.RequestWorkItem.process(RequestWorkItem.java:361)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.AbstractWorkItem.run(AbstractWorkItem.java:43)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.RequestWorkItem.run(RequestWorkItem.java:284)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.DQPWorkContext.runInContext(DQPWorkContext.java:283)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.ThreadReuseExecutor$RunnableWrapper.run(ThreadReuseExecutor.java:124)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.ThreadReuseExecutor$2.run(ThreadReuseExecutor.java:212)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.teiid.translator.TranslatorException: 0 Remote org.teiid.translator.jdbc.JDBCExecutionException: 0 TEIID11008:TEIID11004 Error executing statement(s): [Prepared Values: ['718e52dd-058a-4306-9213-c999181163c7'] SQL: SELECT cast(g_0."uuid" AS varchar), g_0."email" FROM "public"."security_users_info" AS g_0 WHERE g_0."uuid" = ?]
at org.jboss.teiid.translator.jdbc#16.0.0//org.teiid.translator.jdbc.JDBCQueryExecution.execute(JDBCQueryExecution.java:127)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.datamgr.ConnectorWorkItem.execute(ConnectorWorkItem.java:405)
at jdk.internal.reflect.GeneratedMethodAccessor106.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.datamgr.ConnectorManager$1.invoke(ConnectorManager.java:228)
at org.jboss.teiid#16.0.0//com.sun.proxy.$Proxy45.execute(Unknown Source)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.DataTierTupleSource.getResults(DataTierTupleSource.java:302)
at org.jboss.teiid#16.0.0//org.teiid.dqp.internal.process.DataTierTupleSource.nextTuple(DataTierTupleSource.java:138)
... 17 common frames omitted
Caused by: java.sql.SQLException: Remote org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = character varying
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Position: 109
at org.postgresql#42.2.2//org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
at org.postgresql#42.2.2//org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
at org.postgresql#42.2.2//org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
at org.postgresql#42.2.2//org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql#42.2.2//org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql#42.2.2//org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
at org.postgresql#42.2.2//org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:118)
at org.jboss.ironjacamar.jdbcadapters#1.4.20.Final//org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:504)
at org.jboss.teiid.translator.jdbc#16.0.0//org.teiid.translator.jdbc.JDBCQueryExecution.execute(JDBCQueryExecution.java:119)
... 25 common frames omitted

Using BigDecimal field in #Query and populating it with #Param field is throwing Sql Error

This following is the query i am using for setting BigDecimal value in Query but failing as error in SQL Syntax
#Query(value="Select f.id,s.student_id,f.feesPaid,f.fees_pending,f.paid_datetime from Fees f inner join Student s where f.feesPaid > :amt")
List<Fees> findFirst3ByFeesPaidGreaterThan( #Param(value = "amt") BigDecimal amt);
the following is the error
Hibernate: select fees0_.id as col_0_0_, student1_.student_id as col_1_0_, fees0_.fees_paid as col_2_0_, fees0_.fees_pending as col_3_0_, fees0_.paid_datetime as col_4_0_ from fees fees0_ inner join student student1_ on where fees0_.fees_paid>?
2019-05-07 20:06:16.779 WARN 21752 --- [nio-8082-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2019-05-07 20:06:16.779 ERROR 21752 --- [nio-8082-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where fees0_.fees_paid>500' at line 1
I am able to use the query method name but i wanted to do it using Query as mentioned above.

PostgreSQL: Why does this join query throw a syntax error

I don't understand, why the following query produced an error. Whereas in other dbms (eg mysql) this sort of query is absolutely legal.
ERROR: Syntax error at the end of input
LINE 1: SELECT * FROM "addr_country" AS c JOIN "addr_state" AS s
^
SQL state: 42601
Character: 59
Adding an on-clause to the query makes it working:
SELECT * FROM "addr_country" AS c JOIN "addr_state" AS s
ON c."id" = s."country" AND s.id = 10
Due to this answer, MySQL makes a cross join out of a join w/o conditions

How to specify an `order by` on ambiguous column using spring batch and postgres?

I'm extending a JdbcPagingItemReader to do a query that sorts by a column, but it is not working, it seems spring batch and postgres create incompatible constraints.
In essence, the query (created using a SqlPagingQueryProviderFactoryBean) is:
select ta.my_id as i
from table_a ta join table_b tb on ta.my_id = tb.my_id
where ta.unrelated_field = 42
order by i;
but this fails on the second page of results because the PostgresPagingQueryProvider modifies it here (called from here) to where (ta.unrelated_field = 42) AND ((i > the_last_value_in_the_previous_page)), and postgres does not allow putting column aliases (i) in the where clause.
I have tried other ways to specify the sorting key, but I don't see how any way could work because of the next constraints:
Postgres does not allow aliases in the where clause, such as i.
Postgres does not allow ambiguous columns in the order by clause, such as my_id, as it can refer to the column from 2 tables.
JdbcPagingItemReader assumes here that the sort key and the column in the select clause are called exactly the same
That call to ResultSet.getObject will compare the key with a list of column names without the table qualifier, so getObject("ta.my_id") won't work.
So, how can I make my initial query work? did I miss something? or is this a bug in spring batch? I can not rename my_id so that it is different in each table.
Edit
This question faces the exact same issue, but in his case he could use the unqualified column name, which doesn't work in my case, because the column appears in several tables.
More details:
To provide actual code and error messages, below there's the actual error I get when I use the alias in the order by clause (the i in the example is ss_id in the code, and my_id is subsnp_id, the code is here):
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT distinct sub.subsnp_id AS ss_id,loc.snp_id AS rs_id,hgvs.hgvs_c AS hgvs_c_string,hgvs.start_c+1 AS hgvs_c_start,hgvs.stop_c+1 AS hgvs_c_stop,hgvs.ref_allele_c AS reference_c,hgvs.hgvs_t AS hgvs_t_string,hgvs.start_t+1 AS hgvs_t_start,hgvs.stop_t+1 AS hgvs_t_stop,hgvs.ref_allele_t AS reference_t,hgvs.var_allele AS alternate,obsvariation.pattern AS alleles,ctg.contig_name AS contig_name,loc.asn_from +1 AS contig_start,loc.asn_to +1 AS contig_end,loc.loc_type AS loc_type,ctg.contig_chr AS chromosome,loc.phys_pos_from + 1 AS chromosome_start,loc.phys_pos_from + 1 + loc.asn_to - loc.asn_from AS chromosome_end,batch.loc_batch_id_upp AS batch_name,CASE WHEN hgvs.orient_c = 2 THEN -1 ELSE 1 END AS hgvs_c_orientation,CASE WHEN hgvs.orient_t = 2 THEN -1 ELSE 1 END AS hgvs_t_orientation,CASE WHEN loc.orientation = 1 THEN -1 ELSE 1 END AS snp_orientation,CASE WHEN ctg.orient = 1 THEN -1 ELSE 1 END AS contig_orientation,CASE WHEN link.substrand_reversed_flag = 1 THEN -1 ELSE 1 END AS subsnp_orientation FROM b150_snpcontigloc loc JOIN b150_contiginfo ctg ON ctg.ctg_id = loc.ctg_id JOIN snpsubsnplink link ON loc.snp_id = link.snp_id JOIN subsnp sub ON link.subsnp_id = sub.subsnp_id JOIN batch on sub.batch_id = batch.batch_id JOIN b150_snphgvslink hgvs ON hgvs.snp_link = loc.snp_id JOIN dbsnp_shared.obsvariation ON obsvariation.var_id = sub.variation_id WHERE (batch.batch_id = ? AND ctg.group_term IN (?) AND ctg.group_label LIKE ?) AND ((ss_id > ?)) ORDER BY ss_id ASC LIMIT 100]; nested exception is org.postgresql.util.PSQLException: ERROR: column "ss_id" does not exist
Position: 1472
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:684) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:711) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:761) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:192) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:199) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.batch.item.database.JdbcPagingItemReader.doReadPage(JdbcPagingItemReader.java:218) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.item.database.AbstractPagingItemReader.doRead(AbstractPagingItemReader.java:108) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar!/:3.0.8.RELEASE]
at uk.ac.ebi.eva.dbsnpimporter.io.readers.SubSnpCoreFieldsReader.read(SubSnpCoreFieldsReader.java:131) ~[classes!/:0.2-SNAPSHOT]
... 59 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: column "ss_id" does not exist
Position: 1472
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:116) ~[postgresql-9.4.1212.jre7.jar!/:9.4.1212.jre7]
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:692) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633) ~[spring-jdbc-4.3.10.RELEASE.jar!/:4.3.10.RELEASE]
... 68 common frames omitted
We ran into this issue, if you alias the fully qualified column name in your select it will work. Your analysis is correct, and if you do select ta.my_id as "ta.my_id" then the value you need gets returned in the result set and the same sort key can be referenced in where and order by clauses.
The solution I took was to use JdbcCursorItemReader instead of JdbcPagingItemReader, which solves my problem but does not answer if JdbcPagingItemReader is meant to (not) work in this use case.
The changes I made to our code is here.
You can try as below with alias and can use paginated reader
select ta.my_id as \"ta.my_id\" from table_a ta join table_b tb on ta.my_id = tb.my_id where ta.unrelated_field = 42 order by ta.my_id;