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:
...