数据类型强制在median()
或percentileCont()
中似乎不起作用。数据类型强制与max()
和min()
之类的其他聚合函数配合得很好。结果产生的Postgres查询表明类型转换没有应用到最终结果中。以下是jOOQ
和Postgres的摘录供参考。截至目前,我还没有变通办法或不知道此问题的公开票。
任何方向将不胜感激!
selectFields.add(
median(
field(String.format("%s.%s", a.getDataSourceName(), a.getField()))
.coerce(Double.class)) // Seems to not successfully coerce data types
.as(
String.format(
"%s.%s.%s", a.getDataSourceName(), a.getField(), "median")));
SQL输出select
tableA.columnA,
percentile_cont(0.5) within group (order by tableA.columnA) as "tableA.columnA.median"
from tableA
group by tableA.columnA
limit 100;
ERROR: function percentile_cont(numeric, text) does not exist
selectFields.add(
percentileCont(a.getPercentileValue())
.withinGroupOrderBy(
field(String.format("%s.%s", a.getDataSourceName(), a.getField()))
.coerce(Double.class)) // Seems to not successfully coerce data types
.as(
String.format(
"%s.%s.%s", a.getDataSourceName(), a.getField(), "percentile_" + Math.round(a.getPercentileValue() * 100))));
SQL输出select
tableA.columnA,
percentile_cont(0.0) within group (order by tableA.columnA) as "tableA.columnA.percentile_0"
from tableA.columnA
group by tableA.columnA
limit 100;
ERROR: function percentile_cont(numeric, text) does not exist
select
percentile_cont(0.5)
within group (
order by tableA.columnA::INTEGER
)
as "tableA.columnA.median"
from tableA.columnA
group by (select 1)
https://www.jooq.org/javadoc/latest/org.jooq/module-summary.html
您不是在寻找强制,这在jOOQ中来说意味着仅更改数据类型在客户端中,而不会让服务器知道。这在获取某些类型的数据(例如Integer
)时非常有用,尽管jOOQ会生成其他数据类型(例如BigInteger
)。请参见Field.coerce()
上的Javadoc>
与强制转换不同,强制性不影响数据库查看字段类型的方式。
Field.coerce()
Cleary,您想改为
// This binds an int value to a JDBC PreparedStatement DSL.val(1).coerce(String.class); // This binds an int value to a JDBC PreparedStatement // and casts it to VARCHAR in SQL DSL.val(1).cast(String.class);
,就像您实际使用强制转换为Field.cast()
的示例一样。