[jira] [Commented] (CALCITE-4772) PushProjector assigns incorrect field alias when handling RexCall

2021-09-15 Thread xzh_dz (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415889#comment-17415889
 ] 

xzh_dz commented on CALCITE-4772:
-

After the SQL optimized, the alias of SQL should be the same, we should fix it.

> PushProjector assigns incorrect field alias when handling RexCall
> -
>
> Key: CALCITE-4772
> URL: https://issues.apache.org/jira/browse/CALCITE-4772
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: qianshen
>Priority: Major
>  Labels: pull-request-available
> Attachments: image-2021-09-13-11-36-26-902.png
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
>  
> {code:java}
> // source sql
> SELECT  USER_ID
> ,USER_NAME
> ,DEPT_NO
> ,ROLE_ID
> ,ROLE_NAME
> ,ROLE_NO
> ,ID
> ,NAME
> ,id + age as dt
> 
> FROM(
> SELECT  user_id,user_name,dept_no
> FROMuser_info
> WHERE   dept_no > 20
> ) AS USER_INFO
> LEFT JOIN(
>  SELECT  role_id,role_name,role_no
>  FROMrole_info
>  WHERE   role_no > 20
>  ) AS role_info
> ON  USER_ID = ROLE_ID
> LEFT JOIN(
>  SELECT  id,name,age
>  FROMld_ant_test.dim
>  WHERE   age >22 and (name='a' or name ='b')
>  ) dim
> ON  dim.id = USER_INFO.USER_ID where user_name='a'
> //execute plan 
> LogicalProject(USER_ID=[$0], USER_NAME=[$1], DEPT_NO=[$2], ROLE_ID=[$3], 
> ROLE_NAME=[$4], ROLE_NO=[$5], ID=[$6], NAME=[$7], dt=[+($6, $8)])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalJoin(condition=[=($0, $3)], joinType=[left])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
> LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], age=[$2])
> LogicalFilter(condition=[AND(>($2, 22), OR(=($1, 'a'), =($1, 'b')))])
>   LogicalTableScan(table=[[LD_ANT_TEST, DIM]])
> {code}
> use HepPlanner RBO rules  optimize.
>  * CoreRules.FILTER_INTO_JOIN
>  * CoreRules.PROJECT_JOIN_TRANSPOSE
>  
> After optimize 
>  
> {code:java}
> //代码占位符
> SELECT *
> FROM (SELECT *
> FROM (SELECT *
> FROM (SELECT `USER_ID` AS `user_id`, `USER_NAME` AS `user_name`, `DEPT_NO` AS 
> `dept_no`
> FROM `LD_ANT_TEST`.`USER_INFO`
> WHERE `DEPT_NO` > 20) AS `t0`
> WHERE `user_name` = 'a') AS `t1`
> LEFT JOIN (SELECT `ROLE_ID` AS `role_id`, `ROLE_NAME` AS `role_name`, 
> `ROLE_NO` AS `role_no`
> FROM `LD_ANT_TEST`.`ROLE_INFO`
> WHERE `ROLE_NO` > 20) AS `t3` ON `t1`.`user_id` = `t3`.`role_id`) AS `t4`
> LEFT JOIN (SELECT `ID` AS `id`, `NAME` AS `name`, `ID` + `AGE` AS `+`
> FROM `LD_ANT_TEST`.`DIM`
> WHERE `AGE` > 22 AND (`NAME` = 'a' OR `NAME` = 'b')) AS `t7` ON 
> `t4`.`user_id` = `t7`.`id`
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2], role_id=[$3], 
> role_name=[$4], role_no=[$5])
> LogicalJoin(condition=[=($0, $3)], joinType=[left])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
>   LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
> LogicalFilter(condition=[>($2, 20)])
>   LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], +=[+($0, $2)])
> LogicalProject(id=[$0], name=[$1], age=[$2])
>   LogicalFilter(condition=[AND(>($2, 22), OR(=($1, 'a'), =($1, 'b')))])
> LogicalTableScan(table=[[LD_ANT_TEST, DIM]])
> {code}
>  
> In the original SQL, the query column{color:#FF} id + age alias was 
> dt{color}. After optimization, {color:#FF}the incorrect alias + was 
> used.{color}
> After querying the source code, the problem appears in 
> {color:#FF}PushProjector.java#567 {color}
> !image-2021-09-13-11-36-26-902.png!
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4778) Exchange operator deserialize fail

2021-09-15 Thread xzh_dz (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4778?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415857#comment-17415857
 ] 

xzh_dz commented on CALCITE-4778:
-

https://github.com/apache/calcite/pull/2519

> Exchange operator deserialize fail
> --
>
> Key: CALCITE-4778
> URL: https://issues.apache.org/jira/browse/CALCITE-4778
> Project: Calcite
>  Issue Type: Improvement
>Reporter: xzh_dz
>Priority: Major
>
> {code:java}
> // code placeholder
> org.apache.calcite.plan.RelWriterTest
> @Test public void testExchange() {
>   final FrameworkConfig config = RelBuilderTest.config().build();
>   final RelBuilder builder = RelBuilder.create(config);
>   final RelNode rel = builder
>   .scan("EMP")
>   .exchange(RelDistributions.hash(ImmutableList.of(0, 1)))
>   .build();
>   final String relJson = RelOptUtil.dumpPlan("", rel,
>   SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
>   String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
>   final String expected = ""
>   + "LogicalExchange(distribution=[hash[0, 1]])\n"
>   + "  LogicalTableScan(table=[[scott, EMP]])\n";
>   assertThat(s, isLinux(expected));
> }
> {code}
> java.lang.NullPointerException: no entry for tag 
> collationjava.lang.NullPointerException: no entry for tag 
> collationjava.lang.RuntimeException: java.lang.NullPointerException: no entry 
> for tag collation at 
> org.apache.calcite.tools.Frameworks.withPrepare(Frameworks.java:193) at 
> org.apache.calcite.tools.Frameworks.withPlanner(Frameworks.java:135) at 
> org.apache.calcite.tools.Frameworks.withPlanner(Frameworks.java:153)
> Caused by: java.lang.NullPointerException: no entry for tag collationCaused 
> by: java.lang.NullPointerException: no entry for tag collation at 
> java.util.Objects.requireNonNull(Objects.java:290) at 
> org.apache.calcite.rel.externalize.RelJsonReader$2.getNonNull(RelJsonReader.java:202)
>  at 
> org.apache.calcite.rel.externalize.RelJsonReader$2.getCollation(RelJsonReader.java:260)
>  at org.apache.calcite.rel.core.Exchange.(Exchange.java:73) at 
> org.apache.calcite.rel.logical.LogicalExchange.(LogicalExchange.java:44)
>  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>  at 
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>  at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at 
> org.apache.calcite.rel.externalize.RelJsonReader.readRel(RelJsonReader.java:289)
>  at 
> org.apache.calcite.rel.externalize.RelJsonReader.readRels(RelJsonReader.java:107)



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Created] (CALCITE-4778) Exchange operator deserialize fail

2021-09-15 Thread xzh_dz (Jira)
xzh_dz created CALCITE-4778:
---

 Summary: Exchange operator deserialize fail
 Key: CALCITE-4778
 URL: https://issues.apache.org/jira/browse/CALCITE-4778
 Project: Calcite
  Issue Type: Improvement
Reporter: xzh_dz


{code:java}
// code placeholder
org.apache.calcite.plan.RelWriterTest
@Test public void testExchange() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final RelNode rel = builder
  .scan("EMP")
  .exchange(RelDistributions.hash(ImmutableList.of(0, 1)))
  .build();
  final String relJson = RelOptUtil.dumpPlan("", rel,
  SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s = deserializeAndDumpToTextFormat(getSchema(rel), relJson);
  final String expected = ""
  + "LogicalExchange(distribution=[hash[0, 1]])\n"
  + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
{code}
java.lang.NullPointerException: no entry for tag 
collationjava.lang.NullPointerException: no entry for tag 
collationjava.lang.RuntimeException: java.lang.NullPointerException: no entry 
for tag collation at 
org.apache.calcite.tools.Frameworks.withPrepare(Frameworks.java:193) at 
org.apache.calcite.tools.Frameworks.withPlanner(Frameworks.java:135) at 
org.apache.calcite.tools.Frameworks.withPlanner(Frameworks.java:153)

Caused by: java.lang.NullPointerException: no entry for tag collationCaused by: 
java.lang.NullPointerException: no entry for tag collation at 
java.util.Objects.requireNonNull(Objects.java:290) at 
org.apache.calcite.rel.externalize.RelJsonReader$2.getNonNull(RelJsonReader.java:202)
 at 
org.apache.calcite.rel.externalize.RelJsonReader$2.getCollation(RelJsonReader.java:260)
 at org.apache.calcite.rel.core.Exchange.(Exchange.java:73) at 
org.apache.calcite.rel.logical.LogicalExchange.(LogicalExchange.java:44) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
 at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at 
org.apache.calcite.rel.externalize.RelJsonReader.readRel(RelJsonReader.java:289)
 at 
org.apache.calcite.rel.externalize.RelJsonReader.readRels(RelJsonReader.java:107)



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4771) change the value of the CAST function to be nullable

2021-09-15 Thread xzh_dz (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4771?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415852#comment-17415852
 ] 

xzh_dz commented on CALCITE-4771:
-

!image-2021-09-16-11-43-55-743.png|width=745,height=363!

When I test the MySQL database, I find that the converted values are different 
for different data types.

mysql version : 8.0.26

+1 with [~julianhyde] , we should implement {{TRY_CAST function.}}

> change the value of the CAST function to be nullable 
> -
>
> Key: CALCITE-4771
> URL: https://issues.apache.org/jira/browse/CALCITE-4771
> Project: Calcite
>  Issue Type: Improvement
>Reporter: xuyangzhong
>Priority: Major
> Attachments: image-2021-09-16-11-43-55-743.png
>
>
> In the sql "SELECT CAST('haha' AS INT)",the value the function CAST returns 
> will be parsed  into NOT NULL, because when parsing, the type CAST returns is 
> from the INT and the nullable attribute is from the 'haha', which doesn't 
> consider the condition that parsing a string to an int could be invalid and 
> return NULL values.
> I think there are two ways to improve this question:
>  * One is to change the value of the CAST function to be nullable, which 
> avoids the invalid parsing.
>  * The other way is to introduce a function named TRY_CAST, which is used in 
> SQL Server.If the parsing fails, TRY_CAST will return NULL instead of throws 
> exception that a NOT NULL field will be set with our unexpected value NULL.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Updated] (CALCITE-4771) change the value of the CAST function to be nullable

2021-09-15 Thread xzh_dz (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4771?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

xzh_dz updated CALCITE-4771:

Attachment: image-2021-09-16-11-43-55-743.png

> change the value of the CAST function to be nullable 
> -
>
> Key: CALCITE-4771
> URL: https://issues.apache.org/jira/browse/CALCITE-4771
> Project: Calcite
>  Issue Type: Improvement
>Reporter: xuyangzhong
>Priority: Major
> Attachments: image-2021-09-16-11-43-55-743.png
>
>
> In the sql "SELECT CAST('haha' AS INT)",the value the function CAST returns 
> will be parsed  into NOT NULL, because when parsing, the type CAST returns is 
> from the INT and the nullable attribute is from the 'haha', which doesn't 
> consider the condition that parsing a string to an int could be invalid and 
> return NULL values.
> I think there are two ways to improve this question:
>  * One is to change the value of the CAST function to be nullable, which 
> avoids the invalid parsing.
>  * The other way is to introduce a function named TRY_CAST, which is used in 
> SQL Server.If the parsing fails, TRY_CAST will return NULL instead of throws 
> exception that a NOT NULL field will be set with our unexpected value NULL.
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415827#comment-17415827
 ] 

xuyangzhong commented on CALCITE-4777:
--

Thank you very much [~nobigo].I'll begin to try to fix this bug.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not 

[jira] [Assigned] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread duan xiong (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

duan xiong reassigned CALCITE-4776:
---

Assignee: duan xiong

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Assignee: duan xiong
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415756#comment-17415756
 ] 

duan xiong commented on CALCITE-4777:
-

I think the doc is no problem. But the SQL you can make PR.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not actually 

[jira] [Commented] (CALCITE-4762) Upgrade to Avatica 1.19

2021-09-15 Thread Jack Scott (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4762?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415738#comment-17415738
 ] 

Jack Scott commented on CALCITE-4762:
-

Will fix CALCITE-4767 once this upgrade goes through with this relevant fix in 
Avatica: [#153|https://github.com/apache/calcite-avatica/pull/153]

> Upgrade to Avatica 1.19
> ---
>
> Key: CALCITE-4762
> URL: https://issues.apache.org/jira/browse/CALCITE-4762
> Project: Calcite
>  Issue Type: Bug
>  Components: jdbc-driver
>Reporter: Julian Hyde
>Priority: Major
>
> Upgrade Calcite to Avatica version 1.19.
> Currently version 1.19 has not been released.
> 1.19 fixes at least one issue whose test cases are attached as PRs.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Julian Hyde (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415707#comment-17415707
 ] 

Julian Hyde commented on CALCITE-4773:
--

The simplest repro requires a scalar subquery? Can you reproduce using "WHERE 0 
IN (SELECT COUNT\(\*) ..."?

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
> > at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> > at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
> > at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
> ...
> {noformat}
> The root cause seems to be in this piece of code inside {{RelDecorrelator}}'s 
> {{RemoveSingleAggregateRule#onMatch}}:
> {code:java}
> // singleAggRel produces a nullable type, so create the new
> // projection that casts proj expr to a nullable type.
> final RelBuilder relBuilder = call.builder();
> final RelDataType type =
>   relBuilder.getTypeFactory()
> .createTypeWithNullability(projExprs.get(0).getType(), true);
> final RexNode cast = relBuilder.getRexBuilder().makeCast(type, 
> projExprs.get(0));
> relBuilder.push(aggregate).project(cast);
> call.transformTo(relBuilder.build());
> {code}
> Note that the comment assumes that _"singleAggRel produces a nullable type"_, 
> but in this particular case, it seems to produce a non-nullable type, so 
> probably this piece of code needs to be adapted.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Comment Edited] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Ruben Q L (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415623#comment-17415623
 ] 

Ruben Q L edited comment on CALCITE-4773 at 9/15/21, 4:16 PM:
--

Yes, I managed to reproduce the issue with a simpler test:
{noformat}
# [CALCITE-4773] RelDecorrelator's RemoveSingleAggregateRule produces result 
with wrong row type
!use bookstore

SELECT au."name"
FROM "bookstore"."authors" au
WHERE (
SELECT COUNT(*) > 0
FROM UNNEST(au."books") AS "unnested"("title", "year", "pages")
WHERE "unnested"."year" < 1920
);
+-+
| name|
+-+
| Victor Hugo |
+-+
(1 row)

!ok
{noformat}

What do you think [~julianhyde]?
I'll update the PR as soon as I can.


was (Author: rubenql):
Yes, I manages to reproduce the issue with a simpler test:
{noformat}
# [CALCITE-4773] RelDecorrelator's RemoveSingleAggregateRule produces result 
with wrong row type
!use bookstore

SELECT au."name"
FROM "bookstore"."authors" au
WHERE (
SELECT COUNT(*) > 0
FROM UNNEST(au."books") AS "unnested"("title", "year", "pages")
WHERE "unnested"."year" < 1920
);
+-+
| name|
+-+
| Victor Hugo |
+-+
(1 row)

!ok
{noformat}

What do you think [~julianhyde]?
I'll update the PR as soon as I can.

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
> > at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> > at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
> > at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
> ...
> {noformat}
> The root cause seems to be in this piece of code inside {{RelDecorrelator}}'s 
> {{RemoveSingleAggregateRule#onMatch}}:
> {code:java}
> // singleAggRel produces a nullable type, so create the new
> // projection 

[jira] [Commented] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Ruben Q L (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415623#comment-17415623
 ] 

Ruben Q L commented on CALCITE-4773:


Yes, I manages to reproduce the issue with a simpler test:
{noformat}
# [CALCITE-4773] RelDecorrelator's RemoveSingleAggregateRule produces result 
with wrong row type
!use bookstore

SELECT au."name"
FROM "bookstore"."authors" au
WHERE (
SELECT COUNT(*) > 0
FROM UNNEST(au."books") AS "unnested"("title", "year", "pages")
WHERE "unnested"."year" < 1920
);
+-+
| name|
+-+
| Victor Hugo |
+-+
(1 row)

!ok
{noformat}

What do you think [~julianhyde]?
I'll update the PR as soon as I can.

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
> > at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> > at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
> > at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
> ...
> {noformat}
> The root cause seems to be in this piece of code inside {{RelDecorrelator}}'s 
> {{RemoveSingleAggregateRule#onMatch}}:
> {code:java}
> // singleAggRel produces a nullable type, so create the new
> // projection that casts proj expr to a nullable type.
> final RelBuilder relBuilder = call.builder();
> final RelDataType type =
>   relBuilder.getTypeFactory()
> .createTypeWithNullability(projExprs.get(0).getType(), true);
> final RexNode cast = relBuilder.getRexBuilder().makeCast(type, 
> projExprs.get(0));
> relBuilder.push(aggregate).project(cast);
> call.transformTo(relBuilder.build());
> {code}
> Note that the comment assumes that _"singleAggRel produces a nullable type"_, 
> but in this particular case, it seems to produce a non-nullable type, so 
> probably this piece of code needs to be adapted.



--
This 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415606#comment-17415606
 ] 

xuyangzhong commented on CALCITE-4777:
--

[~nobigo] Thanks for your answer :). So should we fix this bug that casting a 
non zero numeric decimal to boolean should be true, or just change the doc to 
avoid the wrong use by developers?

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415593#comment-17415593
 ] 

duan xiong commented on CALCITE-4777:
-

HI.[~xuyangzhong] This SqlFunctions is helping to implement SQL functions in 
generated code. I think this implementation just has stronger usability. And 
the type has been validated before Code-Gen.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and 

[jira] [Resolved] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread duan xiong (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

duan xiong resolved CALCITE-4776.
-
Resolution: Not A Bug

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Julian Hyde (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415570#comment-17415570
 ] 

Julian Hyde commented on CALCITE-4773:
--

I got confused because your test case is so complicated. I’ve never seen a 
Boolean scalar sub-query in WHERE before. Can you find a simpler test case?

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
> > at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> > at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
> > at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
> ...
> {noformat}
> The root cause seems to be in this piece of code inside {{RelDecorrelator}}'s 
> {{RemoveSingleAggregateRule#onMatch}}:
> {code:java}
> // singleAggRel produces a nullable type, so create the new
> // projection that casts proj expr to a nullable type.
> final RelBuilder relBuilder = call.builder();
> final RelDataType type =
>   relBuilder.getTypeFactory()
> .createTypeWithNullability(projExprs.get(0).getType(), true);
> final RexNode cast = relBuilder.getRexBuilder().makeCast(type, 
> projExprs.get(0));
> relBuilder.push(aggregate).project(cast);
> call.transformTo(relBuilder.build());
> {code}
> Note that the comment assumes that _"singleAggRel produces a nullable type"_, 
> but in this particular case, it seems to produce a non-nullable type, so 
> probably this piece of code needs to be adapted.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Comment Edited] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Ruben Q L (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415530#comment-17415530
 ] 

Ruben Q L edited comment on CALCITE-4773 at 9/15/21, 2:23 PM:
--

[~julianhyde], sorry to insist, but it seems to me that the logical plan (and 
rowTypes) in this case are correct.
We are dealing with this plan:
{noformat}
LogicalAggregate(group=[{}], agg#0=[SINGLE_VALUE($0)])
  LogicalProject(EXPR$0=[>($0, 0)])
LogicalAggregate(group=[{}], agg#0=[COUNT()])
  LogicalFilter(condition=[=($0, 4)])
LogicalProject(VAL=[$0])
  Uncollect
LogicalProject(Y=[$cor1.Y])
  LogicalValues(tuples=[[{ 0 }]])

RowTypes:
LogicalAggregate(SINGLE_VALUE): RecordType(BOOLEAN NOT NULL $f0) NOT NULL
LogicalProject: RecordType(BOOLEAN NOT NULL EXPR$0) NOT NULL
LogicalAggregate(COUNT): RecordType(BIGINT NOT NULL $f0) NOT NULL
...
{noformat}

The return type of the count is bigint *non-nullable* (see 
{{SqlCountAggFunction}}); which makes sense, since a COUNT can never return 
null (it can return zero, but not null), is that correct? Thus, the return type 
of the projection ($0 > 0) will consequently be a boolean non-null; and finally 
the single_value function has a return type inference based on its argument 
(see {{SqlSingleValueAggFunction}}), so we get the top boolean non-null.

Therefore, it would seem that the current precondition in {{RelDecorrelator}}'s 
{{RemoveSingleAggregateRule#onMatch}}: _"singleAggRel produces a nullable 
type..."_ has a loophole, and it can (in certain cases, at least this one) to 
produce a non-nullable type, so the proposed PR could be a valid solution for 
the issue.


was (Author: rubenql):
[~julianhyde], sorry to insist, but it seems to me that the logical plan (and 
rowTypes) in this case are correct.
 We are dealing with an Aggregate that contains as agg function a COUNT:
{noformat}
> expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
>   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> LogicalFilter(condition=[=($0, 4)])
>   LogicalProject(VAL=[$0])
> Uncollect
>   LogicalProject(Y=[$cor1.Y])
> LogicalValues(tuples=[[{ 0 }]])
{noformat}
and the return type of the count is BIGINT *non-nullable* (see 
{{SqlCountAggFunction}}); which makes sense, since a COUNT can never return 
null (it can return zero, but not null), is that correct?
 Therefore, it would seem that the current precondition in 
{{RelDecorrelator}}'s {{RemoveSingleAggregateRule#onMatch}}: _"singleAggRel 
produces a nullable type..."_ has a loophole, and it can (in certain cases, at 
least this one) to produce a non-nullable type, so the proposed PR could be a 
valid solution for the issue.

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > 

[jira] [Commented] (CALCITE-4773) RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

2021-09-15 Thread Ruben Q L (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4773?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415530#comment-17415530
 ] 

Ruben Q L commented on CALCITE-4773:


[~julianhyde], sorry to insist, but it seems to me that the logical plan (and 
rowTypes) in this case are correct.
 We are dealing with an Aggregate that contains as agg function a COUNT:
{noformat}
> expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
>   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> LogicalFilter(condition=[=($0, 4)])
>   LogicalProject(VAL=[$0])
> Uncollect
>   LogicalProject(Y=[$cor1.Y])
> LogicalValues(tuples=[[{ 0 }]])
{noformat}
and the return type of the count is BIGINT *non-nullable* (see 
{{SqlCountAggFunction}}); which makes sense, since a COUNT can never return 
null (it can return zero, but not null), is that correct?
 Therefore, it would seem that the current precondition in 
{{RelDecorrelator}}'s {{RemoveSingleAggregateRule#onMatch}}: _"singleAggRel 
produces a nullable type..."_ has a loophole, and it can (in certain cases, at 
least this one) to produce a non-nullable type, so the proposed PR could be a 
valid solution for the issue.

> RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row 
> type
> --
>
> Key: CALCITE-4773
> URL: https://issues.apache.org/jira/browse/CALCITE-4773
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: Ruben Q L
>Assignee: Ruben Q L
>Priority: Major
>  Labels: pull-request-available
> Fix For: 1.28.0
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
> The problem can be reproduced with the following test (to be added to 
> {{unnest.iq}}):
> {noformat}
> SELECT unnested_outer.val, COUNT(1) AS count_val
> FROM
> (
> SELECT *
> FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
> AS u(x, y)
> ) AS t, UNNEST(t.y) AS unnested_outer(val)
> WHERE
> (
> SELECT COUNT(unnested_inner.val) > 0
> FROM UNNEST(t.y) AS unnested_inner(val)
> WHERE unnested_inner.val = 4
> )
> GROUP BY unnested_outer.val
> ORDER BY count_val DESC, unnested_outer.val ASC;
> +-+---+
> | VAL | COUNT_VAL |
> +-+---+
> |   6 | 2 |
> |   4 | 1 |
> +-+---+
> (2 rows)
> !ok
> {noformat}
> When the test is executed, it fails with the following error:
> {noformat}
> > java.lang.AssertionError: Cannot add expression of different type to set:
> > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
> > expression type is RecordType(BOOLEAN $f0) NOT NULL
> > set is 
> > rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
> > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
> >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
> > LogicalFilter(condition=[=($0, 4)])
> >   LogicalProject(VAL=[$0])
> > Uncollect
> >   LogicalProject(Y=[$cor1.Y])
> > LogicalValues(tuples=[[{ 0 }]])
> 249a254,325
> > at 
> > org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
> > at 
> > org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
> > at 
> > org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
> > at 
> > org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
> > at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
> > at 
> > org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
> > at 
> > org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
> > at 
> > org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
> ...
> {noformat}
> The root cause seems to be in this piece of code inside {{RelDecorrelator}}'s 
> {{RemoveSingleAggregateRule#onMatch}}:
> {code:java}
> // singleAggRel produces a nullable type, so create the new
> // projection that casts proj expr to a nullable type.
> final RelBuilder relBuilder = call.builder();
> final RelDataType type =
>   

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415480#comment-17415480
 ] 

xuyangzhong commented on CALCITE-4777:
--

The table about conversion is : 
[https://calcite.apache.org/docs/reference.html#implicit-type-conversion]

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in 

[jira] [Issue Comment Deleted] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

xuyangzhong updated CALCITE-4777:
-
Comment: was deleted

(was: !calcite.png!)

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not actually exist. So the 
> exception is thrown. 
> I find in 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415479#comment-17415479
 ] 

xuyangzhong commented on CALCITE-4777:
--

And if casting from decimal to boolean is invalid, why we have the function 
named "toBoolean(Number number)" in SqlFunctions. In my understanding,  class 
Number contains the class Decimal. It's very confusing.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415478#comment-17415478
 ] 

duan xiong commented on CALCITE-4777:
-

[~xuyangzhong] Thanks again. Can you delete the picture and maybe describe this 
by In which table or link the code? Because this picture makes this issue looks 
not formated.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415475#comment-17415475
 ] 

xuyangzhong commented on CALCITE-4777:
--

Hi, [~nobigo]. So does the doc have a mistake?

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not actually exist. So 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415474#comment-17415474
 ] 

xuyangzhong commented on CALCITE-4777:
--

!calcite.png!

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not actually exist. So the 
> exception is thrown. 
> I 

[jira] [Updated] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

xuyangzhong updated CALCITE-4777:
-
Attachment: calcite.png

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
> Attachments: calcite.png
>
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's function named "call" calls the "booleanValue" 
> function in "java.math.BigDecimal", which does not actually exist. So the 
> exception is thrown. 
> I find in SqFunctions, we have 

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415470#comment-17415470
 ] 

duan xiong commented on CALCITE-4777:
-

[~xuyangzhong] Hi. Can you help me point out where it is in Calcite Docs?  
Maybe we should make this more accurate. (Only support 0 or 1)

In Calcite and PostgreSQL the SQL is valid:

 
{code:java}
select cast(1 as boolean)
select cast(0 as boolean)
{code}
 

But In Calcite:

 
{code:java}
select cast(1 as boolean) return EXPR$0=false
select cast(0 as boolean) return EXPR$0=false
{code}
In PostgreSQL:
{code:java}
select cast(1 as boolean) return EXPR$0=true
select cast(0 as boolean) return EXPR$0=false{code}
 So I think the Calcite is wrong in this SQL.

 

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> 

[jira] [Comment Edited] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415470#comment-17415470
 ] 

duan xiong edited comment on CALCITE-4777 at 9/15/21, 12:02 PM:


[~xuyangzhong] Hi. Can you help me point out where it is in Calcite Docs?  
Maybe we should make this more accurate. (Only support 0 or 1)

In Calcite and PostgreSQL the SQL is valid:
{code:java}
select cast(1 as boolean)
select cast(0 as boolean)
{code}
But In Calcite:
{code:java}
select cast(1 as boolean) return EXPR$0=false
select cast(0 as boolean) return EXPR$0=false
{code}
In PostgreSQL:
{code:java}
select cast(1 as boolean) return EXPR$0=true
select cast(0 as boolean) return EXPR$0=false{code}
 So I think the Calcite is wrong in this SQL.

 


was (Author: nobigo):
[~xuyangzhong] Hi. Can you help me point out where it is in Calcite Docs?  
Maybe we should make this more accurate. (Only support 0 or 1)

In Calcite and PostgreSQL the SQL is valid:

 
{code:java}
select cast(1 as boolean)
select cast(0 as boolean)
{code}
 

But In Calcite:

 
{code:java}
select cast(1 as boolean) return EXPR$0=false
select cast(0 as boolean) return EXPR$0=false
{code}
In PostgreSQL:
{code:java}
select cast(1 as boolean) return EXPR$0=true
select cast(0 as boolean) return EXPR$0=false{code}
 So I think the Calcite is wrong in this SQL.

 

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> 

[jira] [Commented] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread zhujiwen (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415456#comment-17415456
 ] 

zhujiwen commented on CALCITE-4776:
---

[~nobigo]  I think what you said is very reasonable,thank you

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread duan xiong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415450#comment-17415450
 ] 

duan xiong commented on CALCITE-4776:
-

[~zhujiwen] Hi, This function is a debugging function intended for internal use 
in Mysql  [WEIGHT_STRING IN 
MYSQL|https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_weight-string]
 And this is not a standard function according to SQL(ISO/IEC 9075) 2016.   So 
maybe you can implement this in others ways ?

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415447#comment-17415447
 ] 

xuyangzhong commented on CALCITE-4777:
--

But the CalCite Docs 
(+https://calcite.apache.org/docs/reference.html#implicit-type-conversion+) 
says converting from decimal to boolean is valid and only needs explicit cast, 
which confuses me.

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
> true, the Expression's 

[jira] [Commented] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread zhujiwen (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415443#comment-17415443
 ] 

zhujiwen commented on CALCITE-4776:
---

[~zabetak] You can execute this statement in MySQL console

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4772) PushProjector assigns incorrect field alias when handling RexCall

2021-09-15 Thread qianshen (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415436#comment-17415436
 ] 

qianshen commented on CALCITE-4772:
---

[~zabetak] I don't agree with you,i think it is bug. If the user specifically 
defines an alias, it must be important。The optimizer cannot lose anything 
user-defined。

For example。

now, I need to create a view。

define:

 
{code:java}
create view sale_info_view as 
select s."customer_id", s."store_sales" * s."store_cost" as "total"from 
"sales_fact_1997" as s
join "customer" as c
  on s."customer_id" = c."customer_id"{code}
now  view sale_info_view has two fields, customer_id and  total

 

---

After optimization, the definition of this view has changed

define:
{code:java}
create view sale_info_view as 
SELECT "t"."customer_id", "t"."$EXPR0" FROM (SELECT "customer_id", 
"store_sales" * "store_cost" AS "$EXPR0" FROM "foodmart"."sales_fact_1997") AS 
"t" INNER JOIN (SELECT "customer_id" FROM "foodmart"."customer") AS "t0" ON 
"t"."customer_id" = "t0"."customer_id"
{code}
now  view sale_info_view has changed, The fields are customer_id and  $EXPR0 。

so i think it is bug.

 

 

> PushProjector assigns incorrect field alias when handling RexCall
> -
>
> Key: CALCITE-4772
> URL: https://issues.apache.org/jira/browse/CALCITE-4772
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: qianshen
>Priority: Major
>  Labels: pull-request-available
> Attachments: image-2021-09-13-11-36-26-902.png
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
>  
> {code:java}
> // source sql
> SELECT  USER_ID
> ,USER_NAME
> ,DEPT_NO
> ,ROLE_ID
> ,ROLE_NAME
> ,ROLE_NO
> ,ID
> ,NAME
> ,id + age as dt
> 
> FROM(
> SELECT  user_id,user_name,dept_no
> FROMuser_info
> WHERE   dept_no > 20
> ) AS USER_INFO
> LEFT JOIN(
>  SELECT  role_id,role_name,role_no
>  FROMrole_info
>  WHERE   role_no > 20
>  ) AS role_info
> ON  USER_ID = ROLE_ID
> LEFT JOIN(
>  SELECT  id,name,age
>  FROMld_ant_test.dim
>  WHERE   age >22 and (name='a' or name ='b')
>  ) dim
> ON  dim.id = USER_INFO.USER_ID where user_name='a'
> //execute plan 
> LogicalProject(USER_ID=[$0], USER_NAME=[$1], DEPT_NO=[$2], ROLE_ID=[$3], 
> ROLE_NAME=[$4], ROLE_NO=[$5], ID=[$6], NAME=[$7], dt=[+($6, $8)])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalJoin(condition=[=($0, $3)], joinType=[left])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
> LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], age=[$2])
> LogicalFilter(condition=[AND(>($2, 22), OR(=($1, 'a'), =($1, 'b')))])
>   LogicalTableScan(table=[[LD_ANT_TEST, DIM]])
> {code}
> use HepPlanner RBO rules  optimize.
>  * CoreRules.FILTER_INTO_JOIN
>  * CoreRules.PROJECT_JOIN_TRANSPOSE
>  
> After optimize 
>  
> {code:java}
> //代码占位符
> SELECT *
> FROM (SELECT *
> FROM (SELECT *
> FROM (SELECT `USER_ID` AS `user_id`, `USER_NAME` AS `user_name`, `DEPT_NO` AS 
> `dept_no`
> FROM `LD_ANT_TEST`.`USER_INFO`
> WHERE `DEPT_NO` > 20) AS `t0`
> WHERE `user_name` = 'a') AS `t1`
> LEFT JOIN (SELECT `ROLE_ID` AS `role_id`, `ROLE_NAME` AS `role_name`, 
> `ROLE_NO` AS `role_no`
> FROM `LD_ANT_TEST`.`ROLE_INFO`
> WHERE `ROLE_NO` > 20) AS `t3` ON `t1`.`user_id` = `t3`.`role_id`) AS `t4`
> LEFT JOIN (SELECT `ID` AS `id`, `NAME` AS `name`, `ID` + `AGE` AS `+`
> FROM `LD_ANT_TEST`.`DIM`
> WHERE `AGE` > 22 AND (`NAME` = 'a' OR `NAME` = 'b')) AS `t7` ON 
> `t4`.`user_id` = `t7`.`id`
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2], role_id=[$3], 
> role_name=[$4], role_no=[$5])
> LogicalJoin(condition=[=($0, $3)], joinType=[left])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
>   LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
> LogicalFilter(condition=[>($2, 20)])
>   LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], +=[+($0, $2)])
>  

[jira] [Commented] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread Ruben Q L (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4777?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415431#comment-17415431
 ] 

Ruben Q L commented on CALCITE-4777:


Is casting decimal to boolean something standard? I just tried a similar query 
in PostgreSQL and it did not work either (it failed more gracefully with a 
message "ERROR:  cannot cast type numeric to boolean"). Perhaps the proper way 
to deal with this situation is using a {{CASE WHEN ...}} ?

> Casting from decimal to boolean throws an exception
> ---
>
> Key: CALCITE-4777
> URL: https://issues.apache.org/jira/browse/CALCITE-4777
> Project: Calcite
>  Issue Type: Bug
>Reporter: xuyangzhong
>Priority: Major
>
> My sql is the following:
> {code:java}
> // code placeholder
> select * from test where cast (0.10915913549909961 as boolean){code}
>  
> I want to simplify the cast. An exception is thrown:
>  
> {code:java}
> // code placeholder
> Exception in thread "main" java.lang.RuntimeException: while resolving method 
> 'booleanValue' in class class java.math.BigDecimal at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
> org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) 
> at 
> org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) 
> at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
>  at 
> org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
>  at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
>  at 
> org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
>  at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) 
> at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
> org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
> org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
> org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)
> Caused by: java.lang.NoSuchMethodException: 
> java.math.BigDecimal.booleanValue() at 
> java.lang.Class.getMethod(Class.java:1786) at 
> org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
> more
> {code}
> In order to avoid that I used the wrong rule or it caused by my bad coding, i 
> write the test case following:
>  
> {code:java}
> // code placeholder
> JavaTypeFactory typeFactory = new 
> JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
> RexBuilder rexBuilder = new RexBuilder(typeFactory);
> final RexSimplify simplify = new RexSimplify(rexBuilder, 
> RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
> RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
> SqlTypeName.BOOLEAN);
> RelDataType bb = new 
> BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
> SqlOperator op = new SqlCastFunction();
> RexLiteral lt = 
> rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
> List list = new ArrayList<>();
> list.add(lt);
> RexNode rexNode = rexBuilder.makeCall(type,op,list);
> simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
> {code}
> and it throws the same exception.
>  
> Actually, the cast simplify operation will enter the function _translateCast_ 
> in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
> EnumUtils. 

[jira] [Created] (CALCITE-4777) Casting from decimal to boolean throws an exception

2021-09-15 Thread xuyangzhong (Jira)
xuyangzhong created CALCITE-4777:


 Summary: Casting from decimal to boolean throws an exception
 Key: CALCITE-4777
 URL: https://issues.apache.org/jira/browse/CALCITE-4777
 Project: Calcite
  Issue Type: Bug
Reporter: xuyangzhong


My sql is the following:
{code:java}
// code placeholder
select * from test where cast (0.10915913549909961 as boolean){code}
 

I want to simplify the cast. An exception is thrown:

 
{code:java}
// code placeholder
Exception in thread "main" java.lang.RuntimeException: while resolving method 
'booleanValue' in class class java.math.BigDecimal at 
org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:424) at 
org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:435) at 
org.apache.calcite.linq4j.tree.Expressions.unbox(Expressions.java:1453) at 
org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:398) at 
org.apache.calcite.adapter.enumerable.EnumUtils.convert(EnumUtils.java:326) at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateCast(RexToLixTranslator.java:543)
 at 
org.apache.calcite.adapter.enumerable.RexImpTable$CastImplementor.implementSafe(RexImpTable.java:2450)
 at 
org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.genValueStatement(RexImpTable.java:2894)
 at 
org.apache.calcite.adapter.enumerable.RexImpTable$AbstractRexCallImplementor.implement(RexImpTable.java:2859)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:1089)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitCall(RexToLixTranslator.java:90)
 at org.apache.calcite.rex.RexCall.accept(RexCall.java:174) at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:975)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.visitLocalRef(RexToLixTranslator.java:90)
 at org.apache.calcite.rex.RexLocalRef.accept(RexLocalRef.java:75) at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:237)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.translate(RexToLixTranslator.java:231)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateList(RexToLixTranslator.java:823)
 at 
org.apache.calcite.adapter.enumerable.RexToLixTranslator.translateProjects(RexToLixTranslator.java:198)
 at org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:90) at 
org.apache.calcite.rex.RexExecutorImpl.compile(RexExecutorImpl.java:66) at 
org.apache.calcite.rex.RexExecutorImpl.reduce(RexExecutorImpl.java:128) at 
org.apache.calcite.rex.RexSimplify.simplifyCast(RexSimplify.java:2101) at 
org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:326) at 
org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:287) at 
org.apache.flink.table.examples.java.tests.CalciteTest.main(CalciteTest.java:47)

Caused by: java.lang.NoSuchMethodException: java.math.BigDecimal.booleanValue() 
at java.lang.Class.getMethod(Class.java:1786) at 
org.apache.calcite.linq4j.tree.Expressions.call(Expressions.java:421) ... 25 
more
{code}
In order to avoid that I used the wrong rule or it caused by my bad coding, i 
write the test case following:

 
{code:java}
// code placeholder
JavaTypeFactory typeFactory = new 
JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder rexBuilder = new RexBuilder(typeFactory);

final RexSimplify simplify = new RexSimplify(rexBuilder, 
RelOptPredicateList.EMPTY, RexUtil.EXECUTOR);
RelDataType type = new BasicSqlType(typeFactory.getTypeSystem(), 
SqlTypeName.BOOLEAN);
RelDataType bb = new 
BasicSqlType(typeFactory.getTypeSystem(),SqlTypeName.DECIMAL,18,17);
SqlOperator op = new SqlCastFunction();
RexLiteral lt = 
rexBuilder.makeExactLiteral(BigDecimal.valueOf(0.10915913549909961),bb);
List list = new ArrayList<>();
list.add(lt);

RexNode rexNode = rexBuilder.makeCall(type,op,list);

simplify.simplifyUnknownAs(rexNode, RexUnknownAs.UNKNOWN);
{code}
and it throws the same exception.

 

Actually, the cast simplify operation will enter the function _translateCast_ 
in _RexToLixTranslator_.It misses the "case BOOLEAN" and uses the convert in 
EnumUtils. However, because the Decimal's Primitive is null and fromNumber is 
true, the Expression's function named "call" calls the "booleanValue" function 
in "java.math.BigDecimal", which does not actually exist. So the exception is 
thrown. 

I find in SqFunctions, we have a function "toBoolean(Number number)" (which 
seems never to be used?). This function may very fit dealing with this 
question, right?

 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread Stamatis Zampetakis (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415410#comment-17415410
 ] 

Stamatis Zampetakis commented on CALCITE-4776:
--

[~zhujiwen] If you are trying to cast 'ab' to CHAR(4) the syntax you are using 
is not the correct one. 

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread Stamatis Zampetakis (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415409#comment-17415409
 ] 

Stamatis Zampetakis commented on CALCITE-4776:
--

Fix version cannot be 1.27.0 since it is already released. I removed the 
respective tag.

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Updated] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread Stamatis Zampetakis (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Stamatis Zampetakis updated CALCITE-4776:
-
Fix Version/s: (was: 1.27.0)

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4772) PushProjector assigns incorrect field alias when handling RexCall

2021-09-15 Thread Stamatis Zampetakis (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415408#comment-17415408
 ] 

Stamatis Zampetakis commented on CALCITE-4772:
--

[~qianshen] There is no guarantee an alias is going to be retained after 
applying transformations. This is not a bug and people should not rely on 
aliases.

Moreover, the code you cited has changed recently so I am not sure what the 
problem is or what you intend to do with the provided pull request. Can you 
please elaborate more?

Tips:
# Instead of attaching code using images it is better to incorporate it inside 
\{code:java\} tags. Text is searchable images are not. 
# When you refer to specific line of code you can put a permalink to code on 
GitHub.

> PushProjector assigns incorrect field alias when handling RexCall
> -
>
> Key: CALCITE-4772
> URL: https://issues.apache.org/jira/browse/CALCITE-4772
> Project: Calcite
>  Issue Type: Bug
>  Components: core
>Affects Versions: 1.27.0
>Reporter: qianshen
>Priority: Major
>  Labels: pull-request-available
> Attachments: image-2021-09-13-11-36-26-902.png
>
>  Time Spent: 10m
>  Remaining Estimate: 0h
>
>  
> {code:java}
> // source sql
> SELECT  USER_ID
> ,USER_NAME
> ,DEPT_NO
> ,ROLE_ID
> ,ROLE_NAME
> ,ROLE_NO
> ,ID
> ,NAME
> ,id + age as dt
> 
> FROM(
> SELECT  user_id,user_name,dept_no
> FROMuser_info
> WHERE   dept_no > 20
> ) AS USER_INFO
> LEFT JOIN(
>  SELECT  role_id,role_name,role_no
>  FROMrole_info
>  WHERE   role_no > 20
>  ) AS role_info
> ON  USER_ID = ROLE_ID
> LEFT JOIN(
>  SELECT  id,name,age
>  FROMld_ant_test.dim
>  WHERE   age >22 and (name='a' or name ='b')
>  ) dim
> ON  dim.id = USER_INFO.USER_ID where user_name='a'
> //execute plan 
> LogicalProject(USER_ID=[$0], USER_NAME=[$1], DEPT_NO=[$2], ROLE_ID=[$3], 
> ROLE_NAME=[$4], ROLE_NO=[$5], ID=[$6], NAME=[$7], dt=[+($6, $8)])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalJoin(condition=[=($0, $3)], joinType=[left])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
> LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], age=[$2])
> LogicalFilter(condition=[AND(>($2, 22), OR(=($1, 'a'), =($1, 'b')))])
>   LogicalTableScan(table=[[LD_ANT_TEST, DIM]])
> {code}
> use HepPlanner RBO rules  optimize.
>  * CoreRules.FILTER_INTO_JOIN
>  * CoreRules.PROJECT_JOIN_TRANSPOSE
>  
> After optimize 
>  
> {code:java}
> //代码占位符
> SELECT *
> FROM (SELECT *
> FROM (SELECT *
> FROM (SELECT `USER_ID` AS `user_id`, `USER_NAME` AS `user_name`, `DEPT_NO` AS 
> `dept_no`
> FROM `LD_ANT_TEST`.`USER_INFO`
> WHERE `DEPT_NO` > 20) AS `t0`
> WHERE `user_name` = 'a') AS `t1`
> LEFT JOIN (SELECT `ROLE_ID` AS `role_id`, `ROLE_NAME` AS `role_name`, 
> `ROLE_NO` AS `role_no`
> FROM `LD_ANT_TEST`.`ROLE_INFO`
> WHERE `ROLE_NO` > 20) AS `t3` ON `t1`.`user_id` = `t3`.`role_id`) AS `t4`
> LEFT JOIN (SELECT `ID` AS `id`, `NAME` AS `name`, `ID` + `AGE` AS `+`
> FROM `LD_ANT_TEST`.`DIM`
> WHERE `AGE` > 22 AND (`NAME` = 'a' OR `NAME` = 'b')) AS `t7` ON 
> `t4`.`user_id` = `t7`.`id`
> LogicalJoin(condition=[=($6, $0)], joinType=[left])
>   LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2], role_id=[$3], 
> role_name=[$4], role_no=[$5])
> LogicalJoin(condition=[=($0, $3)], joinType=[left])
>   LogicalFilter(condition=[=($1, 'a')])
> LogicalProject(user_id=[$0], user_name=[$1], dept_no=[$2])
>   LogicalFilter(condition=[>($2, 20)])
> LogicalTableScan(table=[[LD_ANT_TEST, USER_INFO]])
>   LogicalProject(role_id=[$0], role_name=[$1], role_no=[$2])
> LogicalFilter(condition=[>($2, 20)])
>   LogicalTableScan(table=[[LD_ANT_TEST, ROLE_INFO]])
>   LogicalProject(id=[$0], name=[$1], +=[+($0, $2)])
> LogicalProject(id=[$0], name=[$1], age=[$2])
>   LogicalFilter(condition=[AND(>($2, 22), OR(=($1, 'a'), =($1, 'b')))])
> LogicalTableScan(table=[[LD_ANT_TEST, DIM]])
> {code}
>  
> In the original SQL, the query column{color:#FF} id + age alias was 
> dt{color}. After optimization, {color:#FF}the incorrect alias + was 
> used.{color}
> After querying the source code, the problem 

[jira] [Updated] (CALCITE-4776) SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))

2021-09-15 Thread zhujiwen (Jira)


 [ 
https://issues.apache.org/jira/browse/CALCITE-4776?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

zhujiwen updated CALCITE-4776:
--
Summary: SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))(was: 
CALCITE-4768)

> SqlParseException:WEIGHT_STRING('ab' AS CHAR(4))  
> --
>
> Key: CALCITE-4776
> URL: https://issues.apache.org/jira/browse/CALCITE-4776
> Project: Calcite
>  Issue Type: Bug
>  Components: server
>Affects Versions: 1.27.0
>Reporter: zhujiwen
>Priority: Blocker
> Fix For: 1.27.0
>
>
> String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";
> SqlParser sqlParser = SqlParser.create(sql, config);
>  
> org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
> column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Created] (CALCITE-4776) CALCITE-4768

2021-09-15 Thread zhujiwen (Jira)
zhujiwen created CALCITE-4776:
-

 Summary: CALCITE-4768
 Key: CALCITE-4776
 URL: https://issues.apache.org/jira/browse/CALCITE-4776
 Project: Calcite
  Issue Type: Bug
  Components: server
Affects Versions: 1.27.0
Reporter: zhujiwen
 Fix For: 1.27.0


String sql = "SELECT WEIGHT_STRING('ab' AS CHAR(4))";

SqlParser sqlParser = SqlParser.create(sql, config);

 

org.apache.calcite.sql.parser.SqlParseException: Encountered "AS" at line *, 
column *.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)


[jira] [Commented] (CALCITE-4747) Remove outdated graph edges in HepPlanner

2021-09-15 Thread Stamatis Zampetakis (Jira)


[ 
https://issues.apache.org/jira/browse/CALCITE-4747?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17415392#comment-17415392
 ] 

Stamatis Zampetakis commented on CALCITE-4747:
--

[~Lam167] the transition from RESOLVED -> CLOSED is done by the release manager 
when the release goes out so you don't need to do it yourself. Friendly 
reminder for future cases :)

> Remove outdated graph edges in HepPlanner
> -
>
> Key: CALCITE-4747
> URL: https://issues.apache.org/jira/browse/CALCITE-4747
> Project: Calcite
>  Issue Type: Improvement
>Reporter: Jianhui Dong
>Priority: Minor
>  Labels: pull-request-available
> Fix For: 1.28.0
>
> Attachments: image-2021-08-19-12-12-41-534.png
>
>  Time Spent: 2h 50m
>  Remaining Estimate: 0h
>
>   !image-2021-08-19-12-12-41-534.png!
> After we use HepPlanner to optimize the RelNode,  we need to replace old 
> RelNode with the optimized RelNode, and update the graph in HepPlanner, but 
> calcite only remove the outdated RelNode(HepRelVertex) in line 970, the edges 
> between these vertices are still in the graph, which may cause a memory leak? 
> IMO, I think maybe we should also remove the outdated edge.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)