Yaoxuan Wu created FLINK-41757:
----------------------------------
Summary: Table API outer joins keep NOT NULL on the columns of the
side they pad with NULLs, so collect / INSERT fails
Key: FLINK-41757
URL: https://issues.apache.org/jira/browse/FLINK-41757
Project: Flink
Issue Type: Bug
Affects Versions: 2.3.0
Reporter: Yaoxuan Wu
Table API outer joins keep NOT NULL on the columns of the side they pad with
NULLs. A padded row then cannot be collected or inserted. The SQL form of the
same join types those columns nullable and returns the right rows.
{code:python}
from pyflink.table import EnvironmentSettings, TableEnvironment, DataTypes as D
from pyflink.table.expressions import col, lit
t = TableEnvironment.create(EnvironmentSettings.in_batch_mode())
a = t.from_elements([(1,), (2,)], D.ROW([D.FIELD('k',
D.INT())])).add_columns(lit(7).alias('c')) # c INT NOT NULL
b = t.from_elements([(3,)], D.ROW([D.FIELD('bk', D.INT())]))
j = a.full_outer_join(b, col('k') == col('bk'))
print(j.get_resolved_schema()) # c stays INT NOT NULL
print([tuple(r) for r in j.execute().collect()])
# expected: [(1, 7, None), (2, 7, None), (None, None, 3)]
# actual: EnforcerException: Column 'c' is NOT NULL, however, a null value is
being written into it.
{code}
{noformat}
Caused by:
org.apache.flink.table.runtime.operators.sink.constraint.EnforcerException:
Column 'c' is NOT NULL, however, a null value is being written into it. You can
set job configuration 'table.exec.sink.not-null-enforcer'='DROP' to suppress
this exception and drop such records silently.
at
org.apache.flink.table.runtime.operators.sink.constraint.NotNullConstraint.enforce(NotNullConstraint.java:55)
{noformat}
* The row for {{bk = 3}} has no match, so {{c}} is NULL there, but the join's
schema keeps {{c INT NOT NULL}}.
* Same for {{right_outer_join}} (left side padded) and {{left_outer_join}}
(right side padded), and with a filter {{c.is_null}} on top.
* With {{table.exec.sink.not-null-enforcer = DROP}} the padded row is silently
dropped: {{[(1, 7, None), (2, 7, None)]}}.
* {{SELECT * FROM a FULL OUTER JOIN b ON k = bk}} returns the expected rows.
* Reproduces on 2.0.1, 2.2.1, 2.3.0 and master (2.4-SNAPSHOT, 2f01434,
2026-11-19), batch mode. (On 2.0.1 the error is a {{TableException}} with the
same message.)
Cause: {{JoinQueryOperation.calculateResultingSchema}} builds the schema of
every join by concatenating the column data types of both inputs, without
looking at the join type, so the columns of the padded side keep NOT NULL. The
collect / insert sink then enforces that schema.
A possible fix: in {{calculateResultingSchema}}, make the columns of the right
side nullable for LEFT OUTER, of the left side for RIGHT OUTER, and of both
sides for FULL OUTER joins.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)