You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-zh@flink.apache.org by Sebastian Liu <li...@gmail.com> on 2021/01/04 08:49:25 UTC

Re: FlinkSQL 下推的值类型与字段类型不对应

Hi automths,

RexNode中的Literal type,在calcite convert to relNode的过程中,以col1 > 10为例,
10从calcite parse出来首先是SqlNumericLiteral, 其中类型会是Decimal(prec: 2, scale: 0).
在创建其对应的RelDataType时,如果其值域在Interger.MIN ~ Interger.Max之间,那就是Interger type。
如果不在就是decimal, 这里没有类似Hive的auto cast功能,而是calcite进行了隐式类型转换。
这里具体隐式转换的规则可以参考:
https://calcite.apache.org/docs/reference.html#implicit-type-conversion

对于Function中,参数的类型,Flink也有一套规则进行推导。

select * from shortRow1 where col1 > CAST(10 AS SMALLINT) and col1 <=
CAST(15 AS SMALLINT) 可以保证
在applyPredicates时看到的expression中,literal是预期的type,但不是特别通用,建议在相关connector中实现
一个TypeVisitor, 把literal转成预期的type。

Just my thoughts

automths <au...@163.com> 于2021年1月4日周一 上午9:36写道:

>
>
> 谢谢你的回答。
>
> 但是我的col1,col2就已经是SMALLINT类型的了,我的问题是where条件中值下推过程中是Integer类型的,我希望值也是SMALLINT的。
>
>
>
> 祝好!
> | |
> automths
> |
> |
> automths@163.com
> |
> 在2020年12月31日 18:17,whirly<wh...@163.com> 写道:
> Hi.
>
> 查询语句中可以使用 cast 内置函数将值强制转换为指定的类型,如 select CAST(A.`col1` AS SMALLINT) as
> col1 from table
>
>
> 参考:
>
> https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/table/functions/systemFunctions.html#type-conversion-functions
>
> https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/table/types.html#data-types
>
>
>
>
> best 2021.
>
>
> 在 2020-12-31 17:13:20,"automths" <au...@163.com> 写道:
> Hi:
> 我自定义connect并实现了FilterableTableSource接口,可是发现flink planner 下推的Filter中,
> Literal类型与字段类型不匹配。
> 比如:下面的SQL:
> select * from shortRow1 where key in (1, 2, 3) and col1 > 10 and col1 <= 15
> 其中DDL定义时, key、col1、col1都是SMALLINT类型
> 在下推的Filter中, GreaterThan中的Literal是Integer类型,这样是合理的吗?或者我的查询语句中要做什么处理?
>
>
> 祝好!
> | |
> automths
> |
> |
> automths@163.com
> |
>
>

-- 

*With kind regards
------------------------------------------------------------
Sebastian Liu 刘洋
Institute of Computing Technology, Chinese Academy of Science
Mobile\WeChat: +86—15201613655
E-mail: liuyang0704@gmail.com <li...@gmail.com>
QQ: 3239559*

回复: FlinkSQL 下推的值类型与字段类型不对应

Posted by automths <au...@163.com>.
非常感谢你的指导,我将按照你的建议,实现一个TypeVisitor来对下推的类型转化成预期的类型。


祝好!
| |
automths
|
|
automths@163.com
|
在2021年01月4日 16:49,Sebastian Liu<li...@gmail.com> 写道:
Hi automths,

RexNode中的Literal type,在calcite convert to relNode的过程中,以col1 > 10为例,
10从calcite parse出来首先是SqlNumericLiteral, 其中类型会是Decimal(prec: 2, scale: 0).
在创建其对应的RelDataType时,如果其值域在Interger.MIN ~ Interger.Max之间,那就是Interger type。
如果不在就是decimal, 这里没有类似Hive的auto cast功能,而是calcite进行了隐式类型转换。
这里具体隐式转换的规则可以参考:
https://calcite.apache.org/docs/reference.html#implicit-type-conversion

对于Function中,参数的类型,Flink也有一套规则进行推导。

select * from shortRow1 where col1 > CAST(10 AS SMALLINT) and col1 <=
CAST(15 AS SMALLINT) 可以保证
在applyPredicates时看到的expression中,literal是预期的type,但不是特别通用,建议在相关connector中实现
一个TypeVisitor, 把literal转成预期的type。

Just my thoughts

automths <au...@163.com> 于2021年1月4日周一 上午9:36写道:



谢谢你的回答。

但是我的col1,col2就已经是SMALLINT类型的了,我的问题是where条件中值下推过程中是Integer类型的,我希望值也是SMALLINT的。



祝好!
| |
automths
|
|
automths@163.com
|
在2020年12月31日 18:17,whirly<wh...@163.com> 写道:
Hi.

查询语句中可以使用 cast 内置函数将值强制转换为指定的类型,如 select CAST(A.`col1` AS SMALLINT) as
col1 from table


参考:

https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/table/functions/systemFunctions.html#type-conversion-functions

https://ci.apache.org/projects/flink/flink-docs-release-1.12/dev/table/types.html#data-types




best 2021.


在 2020-12-31 17:13:20,"automths" <au...@163.com> 写道:
Hi:
我自定义connect并实现了FilterableTableSource接口,可是发现flink planner 下推的Filter中,
Literal类型与字段类型不匹配。
比如:下面的SQL:
select * from shortRow1 where key in (1, 2, 3) and col1 > 10 and col1 <= 15
其中DDL定义时, key、col1、col1都是SMALLINT类型
在下推的Filter中, GreaterThan中的Literal是Integer类型,这样是合理的吗?或者我的查询语句中要做什么处理?


祝好!
| |
automths
|
|
automths@163.com
|



--

*With kind regards
------------------------------------------------------------
Sebastian Liu 刘洋
Institute of Computing Technology, Chinese Academy of Science
Mobile\WeChat: +86—15201613655
E-mail: liuyang0704@gmail.com <li...@gmail.com>
QQ: 3239559*