You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "pengzhiwei (JIRA)" <ji...@apache.org> on 2019/04/24 12:41:00 UTC
[jira] [Comment Edited] (CALCITE-3018) User defined scalar function
which returns a Date/Timestamp value causes error.
[ https://issues.apache.org/jira/browse/CALCITE-3018?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16825093#comment-16825093 ]
pengzhiwei edited comment on CALCITE-3018 at 4/24/19 12:40 PM:
---------------------------------------------------------------
It seems that the execute result type is not match with the _ColumnMetaData.type.rep_
in the case:
{code:java}
select my_date_func(1),1 from xx;
{code}
The _EnumerableCalc_ will translate the _outputJavaType_ of the "select" to Object[].class, and the execute result type of my_date_func(1) will be "Date".
However, the _ColumnMetaData.type.rep_ is a Integer type, which leading to this exception.
{code:java}
rotected Accessor createAccessor(ColumnMetaData columnMetaData,...
...)
{
+140 case Types.DATE:
+141 switch (columnMetaData.type.rep) {
+142 case PRIMITIVE_INT:
case INTEGER:
case NUMBER:
return new DateFromNumberAccessor(getter, localCalendar);
case JAVA_SQL_DATE:
return new DateAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
{code}
In another case:
{code:java}
select my_date_func(1) from xx;
{code}
The _EnumerableCalc_ translate the _outputJavaType of the "select" to "Integer",_ which is match with the _ColumnMetaData.type.rep._ So it can pass the test.
was (Author: pzw2018):
It seems that the execute result is not match with the _ColumnMetaData.type.rep_
in the case:
{code:java}
select my_date_func(1),1 from xx;
{code}
The _EnumerableCalc_ will translate the _outputJavaType_ of the "select" __ to Object[].class because the rowType of the "select" is a ArrayType, and the execute result of my_date_func(1) will be "Date".
However, the _ColumnMetaData.type.rep_ is a Integer type, which leading to this exception.
{code:java}
rotected Accessor createAccessor(ColumnMetaData columnMetaData,...
...)
{
+140 case Types.DATE:
+141 switch (columnMetaData.type.rep) {
+142 case PRIMITIVE_INT:
case INTEGER:
case NUMBER:
return new DateFromNumberAccessor(getter, localCalendar);
case JAVA_SQL_DATE:
return new DateAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
{code}
In another case:
{code:java}
select my_date_func(1) from xx;
{code}
The _EnumerableCalc_ will __ translate the outputJavaType of the "select" to "Integer", which is match with the _ColumnMetaData.type.rep._ So it can pass the test.
> User defined scalar function which returns a Date/Timestamp value causes error.
> -------------------------------------------------------------------------------
>
> Key: CALCITE-3018
> URL: https://issues.apache.org/jira/browse/CALCITE-3018
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.19.0
> Reporter: fangyc
> Priority: Major
>
> I've defined a user defined function that returns a date or timestamp and register it on root schema :
> {quote} public static java.sql.Date test(long value) {
> return new java.sql.Date(value);
> }
> {quote}
>
> {quote}rootSchema.add("my_test", ScalarFunctionImpl.create(CalciteTest.class, "test"));
> {quote}
> If I use this function in the select part of a query as the only column, the result works fine:
> {quote}select my_test(1) from northwind.product
> {quote}
> However, if I add an additional column in the select part, the error occurs:
> {quote}select my_test(1), 2 from northwind.product
> {quote}
> Error message:
> {quote} Exception in thread "main" java.lang.ClassCastException: java.sql.Date cannot be cast to java.lang.Number
> at org.apache.calcite.avatica.util.AbstractCursor$NumberAccessor.getNumber(AbstractCursor.java:726)
> at org.apache.calcite.avatica.util.AbstractCursor$DateFromNumberAccessor.getDate(AbstractCursor.java:915)
> at org.apache.calcite.avatica.AvaticaSite.get(AvaticaSite.java:326)
> at org.apache.calcite.avatica.AvaticaResultSet.getObject(AvaticaResultSet.java:393)
> {quote}
>
> I tried with calcite.debug=true, and the difference seems as follows:
> * 1 column
> {quote}/* 51 */ public Object current() {
> /* 52 */ return org.apache.calcite.runtime.SqlFunctions.toIntOptional(CalciteTest.test(1));
> /* 53 */ }
> {quote} * 2 columns
> {quote}/* 51 */ public Object current() {
> /* 52 */ return new Object[] {
> /* 53 */ CalciteTest.test(1),
> /* 54 */ 2};
> /* 55 */ }
> {quote}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)