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

Jianhui Dong commented on CALCITE-5592:
---------------------------------------

Sorry for not clarifying the problem, I see some logical inconsistencies in the 
case but I'm not sure which one is incorrect.

when we validate the sql, as you can see in SqlValidatorImpl#validateWhereOrOn:

 
{code:java}
protected void validateWhereOrOn(
    SqlValidatorScope scope,
    SqlNode condition,
    String clause) {
  validateNoAggs(aggOrOverOrGroupFinder, condition, clause);
  inferUnknownTypes(
      booleanType,
      scope,
      condition);
  condition.validate(this, scope);

  final RelDataType type = deriveType(scope, condition);
  if (!SqlTypeUtil.inBooleanFamily(type)) {
    throw newValidationError(condition, RESOURCE.condMustBeBoolean(clause));
  }
} {code}
inferUnknownTypes would treat the number literal 1 as VARCHAR, but when we 
convert the SqlNode to RelNode, as you can see in 
org.apache.calcite.sql2rel.StandardConvertletTable#convertCall:
{code:java}
private RexNode convertCall(
    SqlRexContext cx, SqlOperator op, List<SqlNode> operands) {
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlOperandTypeChecker.Consistency consistency =
      op.getOperandTypeChecker() == null
          ? SqlOperandTypeChecker.Consistency.NONE
          : op.getOperandTypeChecker().getConsistency();
  final List<RexNode> exprs =
      convertExpressionList(cx, operands, consistency);
  RelDataType type = rexBuilder.deriveReturnType(op, exprs);
  return rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));
} {code}
it would treat the number literal 1 as INTEGER.

 

The consequence is that when I try to filter out data that do not equal '1' 
with flink sql(old version like 1.12), the where clause does not take effect. 
However, flink forbidden the '=' operator in 
https://issues.apache.org/jira/browse/FLINK-24492.

 
 
 

> SqlValidatorImpl#validateWhereOrOn does not derive the the correct operand 
> type for '='
> ---------------------------------------------------------------------------------------
>
>                 Key: CALCITE-5592
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5592
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: Jianhui Dong
>            Priority: Major
>
> org.apache.calcite.sql.fun.SqlStdOperatorTable#EQUALS use 
> InferTypes.FIRST_KNOWN as its SqlOperandTypeInference, and it will derive the 
> type incorrectly as follows:
> {code:java}
> CREATE TABLE source (
>     a VARCHAR
> )
> SELECT * FROM source WHERE a = 1;{code}
> it will think that the type of operands `source.a` and `1` both be 
> VARCHAR(65536), but exactly the type of `1` maybe INTEGER, and I want to know 
> if should we throw an exception for this kind type check of where clause?
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to