Flink SQL 报错 Illegal use of ‘NULL‘

Flink SQL 报错 Illegal use of 'NULL'

报错详细信息:
c 复制代码
Exception in thread "main" org.apache.flink.table.api.ValidationException: SQL validation failed. From line 1, column 18 to line 1, column 21: Illegal use of 'NULL'
	at org.apache.flink.table.planner.calcite.FlinkPlannerImpl.org$apache$flink$table$planner$calcite$FlinkPlannerImpl$$validate(FlinkPlannerImpl.scala:156)
	at org.apache.flink.table.planner.calcite.FlinkPlannerImpl.validate(FlinkPlannerImpl.scala:107)
	at org.apache.flink.table.planner.operations.SqlToOperationConverter.convert(SqlToOperationConverter.java:205)
	at org.apache.flink.table.planner.delegation.ParserImpl.parse(ParserImpl.java:101)
	at org.apache.flink.table.api.internal.TableEnvironmentImpl.sqlQuery(TableEnvironmentImpl.java:704)
	at com.abcd.FlinkSqlTest.main(FlinkSqlTest.java:108)
Caused by: org.apache.calcite.runtime.CalciteContextException: From line 1, column 18 to line 1, column 21: Illegal use of 'NULL'
	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.runtime.Resources$ExInstWithCause.ex(Resources.java:467)
	at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:883)
	at org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:868)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(SqlValidatorImpl.java:4860)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.inferUnknownTypes(SqlValidatorImpl.java:1836)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.inferUnknownTypes(SqlValidatorImpl.java:1911)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.expandSelectItem(SqlValidatorImpl.java:418)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelectList(SqlValidatorImpl.java:4060)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect(SqlValidatorImpl.java:3346)
	at org.apache.calcite.sql.validate.SelectNamespace.validateImpl(SelectNamespace.java:60)
	at org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:84)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:996)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:974)
	at org.apache.calcite.sql.SqlSelect.validate(SqlSelect.java:232)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:951)
	at org.apache.calcite.sql.validate.SqlValidatorImpl.validate(SqlValidatorImpl.java:703)
	at org.apache.flink.table.planner.calcite.FlinkPlannerImpl.org$apache$flink$table$planner$calcite$FlinkPlannerImpl$$validate(FlinkPlannerImpl.scala:151)
	... 5 more
Caused by: org.apache.calcite.sql.validate.SqlValidatorException: Illegal use of 'NULL'
	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.runtime.Resources$ExInstWithCause.ex(Resources.java:467)
	at org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:560)
	... 21 more
场景复现代码:
c 复制代码
    public static void main(String[] args) throws Exception {
        // 1. 创建执行环境并模拟数据流
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        EnvironmentSettings environmentSettings = EnvironmentSettings.newInstance().build();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env, environmentSettings);
        
        // 2.执行Flinksql
        tableEnv.toRetractStream(tableEnv.sqlQuery("SELECT SUBSTRING(null, 1, 2)"), Row.class).print();

        // 3.执行任务
        env.execute("Flink Sql Test");
    }
原因分析:

在Flink SQL中,使用 NULL 值会导致SQL验证失败,为了解决这个问题,可以使用 CAST(NULL AS STRING) 来代替直接使用 NULL 。通过使用 CAST(NULL AS STRING) ,可以明确地将 NULL 值转换为字符串类型,以便在后续的字符串操作中正确处理空值情况。以下是修改后的SQL语句:

c 复制代码
SELECT SUBSTRING(CAST(NULL AS STRING), 1, 2);
Flinksql替换后的执行结果:
c 复制代码
(true,+I[null])

Process finished with exit code 0

提示:Flink SQL 提交时不能带有分号;,否则会报如下sql解析失败:

Exception in thread "main" org.apache.flink.table.api.SqlParserException: SQL parse failed. Encountered ";" at line 1, column 45.

Was expecting one of:

...

相关推荐
q***385140 分钟前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
小白学大数据1 小时前
Python爬虫伪装策略:如何模拟浏览器正常访问JSP站点
java·开发语言·爬虫·python
程序员西西2 小时前
SpringBoot接口安全:APIKey保护指南
java·spring boot·计算机·程序员·编程·编程开发
summer_west_fish2 小时前
单体VS微服务:架构选择实战指南
java·微服务·架构
v***8572 小时前
Ubuntu介绍、与centos的区别、基于VMware安装Ubuntu Server 22.04、配置远程连接、安装jdk+Tomcat
java·ubuntu·centos
烤麻辣烫2 小时前
黑马程序员大事件后端概览(表现效果升级版)
java·开发语言·学习·spring·intellij-idea
q***96582 小时前
Spring总结(上)
java·spring·rpc
思密吗喽2 小时前
宠物商城系统
java·开发语言·vue·毕业设计·springboot·课程设计·宠物
ss2733 小时前
019:深入解析可重入互斥锁:原理、实现与线程安全实践
java·数据库·redis
luyun0202023 小时前
牛批了,某音录播神器
java·windows·figma