[
https://issues.apache.org/jira/browse/CALCITE-6717?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
kunzai111 updated CALCITE-6717:
-------------------------------
Description:
为什么 interger 列除以 integer 列不返回 double 或 decimal ,而是返回 integer
例:
选择 1/2 作为 a;
期望:
0.5
实际:
0
预期结果是 double 或 decimal,但要现在实现它,需要 Integer * 1.0/Integer
为了实现这个目标,我目前修改了 infraReturnType 方法,将类覆盖
!image-2024-12-05-11-57-14-615.png!
类:
SqlOperator.class 类
方法:
public RelDataType inferReturnType(
SqlOperatorBinding opBinding) {
if (returnTypeInference != null) {
RelDataType returnType = null;
todo 当遇到两个整型相除的时候,calcite会最后判断结果为整型,但实际上结果是小数,例如1/2=0.5
SqlOperator sqlOperator = opBinding.getOperator();
if(sqlOperator.kind.equals(SqlKind.DIVIDE)){
boolean allInteger = true;
对于 (RelDataType relDataType : opBinding.collectOperandTypes()) {
if(!relDataType.getSqlTypeName().equals(SqlTypeName.INTEGER))){
allInteger = false;
}
if(allInteger){
BasicSqlType basicSqlType = new
BasicSqlType(opBinding.getTypeFactory().getTypeSystem(),
SqlTypeName.DECIMAL,19,8,true);
returnType = basicSqlType;
} else{
returnType = returnTypeInference.inferReturnType(opBinding);
}
} else{
returnType = returnTypeInference.inferReturnType(opBinding);
}
if (returnType == null) {
throw new IllegalArgumentException(“无法推断 ”
+ opBinding.getOperator() + “的返回类型 ”;操作数类型: “
+ opBinding.collectOperandTypes());
}
应删除MEASURE包装器,例如,MEASURE<DOUBLE>应该只是DOUBLE
if (isMeasure(returnType) & returnType.getMeasureElementType() != null) {
returnType = Objects.requireNonNull(returnType.getMeasureElementType());
}
if (operandTypeInference != null
&& opBinding instanceof SqlCallBinding
&& this instanceof SqlFunction) {
final SqlCallBinding callBinding = (SqlCallBinding) opBinding;
final List<RelDataType> operandTypes = opBinding.collectOperandTypes();
if (operandTypes.stream().anyMatch(t -> t.getSqlTypeName() == SqlTypeName.ANY))
{
final RelDataType[] operandTypes2 = operandTypes.toArray(new RelDataType[0]);
操作绑定推理.inferOperandTypes(callBinding, returnType, operandTypes2);
((SqlValidatorImpl) callBinding.getValidator()).
callToOperandTypesMap.put
(callBinding.getCall(), ImmutableList.copyOf(operandTypes2));
}
}
return returnType;
}
> interger column divided by integer column Problem
> -------------------------------------------------
>
> Key: CALCITE-6717
> URL: https://issues.apache.org/jira/browse/CALCITE-6717
> Project: Calcite
> Issue Type: Bug
> Reporter: kunzai111
> Assignee: kunzai111
> Priority: Major
> Attachments: image-2024-12-05-11-56-22-416.png,
> image-2024-12-05-11-57-14-615.png
>
>
> 为什么 interger 列除以 integer 列不返回 double 或 decimal ,而是返回 integer
>
> 例:
> 选择 1/2 作为 a;
> 期望:
> 0.5
> 实际:
> 0
>
> 预期结果是 double 或 decimal,但要现在实现它,需要 Integer * 1.0/Integer
>
> 为了实现这个目标,我目前修改了 infraReturnType 方法,将类覆盖
> !image-2024-12-05-11-57-14-615.png!
> 类:
> SqlOperator.class 类
> 方法:
> public RelDataType inferReturnType(
> SqlOperatorBinding opBinding) {
> if (returnTypeInference != null) {
> RelDataType returnType = null;
> todo 当遇到两个整型相除的时候,calcite会最后判断结果为整型,但实际上结果是小数,例如1/2=0.5
> SqlOperator sqlOperator = opBinding.getOperator();
> if(sqlOperator.kind.equals(SqlKind.DIVIDE)){
> boolean allInteger = true;
> 对于 (RelDataType relDataType : opBinding.collectOperandTypes()) {
> if(!relDataType.getSqlTypeName().equals(SqlTypeName.INTEGER))){
> allInteger = false;
> }
> if(allInteger){
> BasicSqlType basicSqlType = new
> BasicSqlType(opBinding.getTypeFactory().getTypeSystem(),
> SqlTypeName.DECIMAL,19,8,true);
> returnType = basicSqlType;
> } else{
> returnType = returnTypeInference.inferReturnType(opBinding);
> }
> } else{
> returnType = returnTypeInference.inferReturnType(opBinding);
> }
> if (returnType == null) {
> throw new IllegalArgumentException(“无法推断 ”
> + opBinding.getOperator() + “的返回类型 ”;操作数类型: “
> + opBinding.collectOperandTypes());
> }
> 应删除MEASURE包装器,例如,MEASURE<DOUBLE>应该只是DOUBLE
> if (isMeasure(returnType) & returnType.getMeasureElementType() != null) {
> returnType = Objects.requireNonNull(returnType.getMeasureElementType());
> }
> if (operandTypeInference != null
> && opBinding instanceof SqlCallBinding
> && this instanceof SqlFunction) {
> final SqlCallBinding callBinding = (SqlCallBinding) opBinding;
> final List<RelDataType> operandTypes = opBinding.collectOperandTypes();
> if (operandTypes.stream().anyMatch(t -> t.getSqlTypeName() ==
> SqlTypeName.ANY)) {
> final RelDataType[] operandTypes2 = operandTypes.toArray(new RelDataType[0]);
> 操作绑定推理.inferOperandTypes(callBinding, returnType, operandTypes2);
> ((SqlValidatorImpl) callBinding.getValidator()).
> callToOperandTypesMap.put
> (callBinding.getCall(), ImmutableList.copyOf(operandTypes2));
> }
> }
> return returnType;
> }
>
>
>
--
This message was sent by Atlassian Jira
(v8.20.10#820010)