Weiwei Su created FLINK-41824:
---------------------------------

             Summary: SEARCH / Sarg filter evaluation on BYTES (VARBINARY) 
columns silently drops all non-NULL matches in IN and OR predicates
                 Key: FLINK-41824
                 URL: https://issues.apache.org/jira/browse/FLINK-41824
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner, Table SQL / Runtime
    Affects Versions: 2.3.0
         Environment: Apache Flink 2.3.0 (official image 
flink:2.3.0-scala_2.12-java17, Scala 2.12, Java 17, Docker / Linux), Flink SQL 
Client in batch mode (execution.target=local, execution.runtime-mode=batch).
            Reporter: Weiwei Su


h3. Summary
In Apache Flink 2.3.0 (Table SQL, batch mode), whenever Calcite folds 
predicates on a {{BYTES}} ({{VARBINARY}}) column into a {{SEARCH(col, 
Sarg[...])}} expression — such as:
* {{WHERE b IN (X'8000', X'0001')}}
* {{WHERE b = X'8000' OR b = X'0001'}}
* {{WHERE b = X'8000' OR b IS NULL}}

Flink silently drops *all* non-NULL matching rows (returning {{0}} rows for 
{{IN}} / {{OR}}, and only the {{NULL}} rows for {{= X'...' OR IS NULL}}).

Meanwhile, the equivalent single equality predicate {{WHERE b = X'8000'}} 
(which is not folded into {{SEARCH}} / {{Sarg}}) returns the expected rows.

The bug is connector-independent and reproduces on a 3-row built-in 
{{filesystem}} ({{json}}) table in Flink 2.3.0 (as well as on Apache Iceberg 
tables).

h3. Minimal Reproducer (Pure Apache Flink 2.3.0 SQL, no external connectors)
{code:sql}
SET 'execution.target' = 'local';
SET 'execution.runtime-mode' = 'batch';
SET 'sql-client.execution.result-mode' = 'tableau';

CREATE TABLE t_bin (
  id INT,
  b BYTES
) WITH (
  'connector' = 'filesystem',
  'path' = 'file:///tmp/fb_min',
  'format' = 'json'
);

INSERT INTO t_bin
VALUES
  (1, X'8000'),
  (2, X'0001'),
  (3, CAST(NULL AS BYTES));

-- Controls (not folded into SEARCH/Sarg): PASS
SELECT 'EQ_ONLY' AS t, COUNT(*) AS c
FROM t_bin
WHERE b = X'8000';
-- Actual: 1, Expected: 1

SELECT 'NULL_ONLY' AS t, COUNT(*) AS c
FROM t_bin
WHERE b IS NULL;
-- Actual: 1, Expected: 1

-- Folded into SEARCH(b, Sarg[...]): SILENT DATA LOSS
SELECT 'EQ_OR_NULL' AS t, COUNT(*) AS c
FROM t_bin
WHERE b = X'8000'
  OR b IS NULL;
-- Actual: 1 (row (1, X'8000') silently dropped!), Expected: 2

SELECT 'IN_TWO' AS t, COUNT(*) AS c
FROM t_bin
WHERE b IN (X'8000', X'0001');
-- Actual: 0 (all matching rows silently dropped!), Expected: 2

SELECT 'OR_TWO' AS t, COUNT(*) AS c
FROM t_bin
WHERE b = X'8000'
  OR b = X'0001';
-- Actual: 0 (all matching rows silently dropped!), Expected: 2
{code}

h3. Verified Results Matrix (Apache Flink 2.3.0)
|| Test Case || SQL Predicate || Optimized Plan Operator || Actual Count || 
Expected Count ||
| {{EQ_ONLY}} | {{b = X'8000'}} | Binary equality | 1 | 1 |
| {{NULL_ONLY}} | {{b IS NULL}} | {{IS NULL}} | 1 | 1 |
| {{EQ_OR_NULL}} | {{b = X'8000' OR b IS NULL}} | {{SEARCH(b, Sarg[8000; NULL 
AS TRUE])}} | *1* | 2 |
| {{IN_TWO}} | {{b IN (X'8000', X'0001')}} | {{SEARCH(b, Sarg[0001, 8000])}} | 
*0* | 2 |
| {{OR_TWO}} | {{b = X'8000' OR b = X'0001'}} | {{SEARCH(b, Sarg[0001, 8000])}} 
| *0* | 2 |

h3. Root Cause Analysis
In the physical plan, Calcite simplifies {{b = X'8000' OR b IS NULL}}, {{b = 
X'8000' OR b = X'0001'}}, and {{b IN (X'8000', X'0001')}} into {{Calc(..., 
where=[SEARCH(b, Sarg[...])])}}.

In Flink's expression code generation for {{SEARCH}} / {{IN}} ({{Sarg}} 
point-set lookup), {{BYTES}} / {{VARBINARY}} values are represented as Java 
{{byte[]}}. Because Java {{byte[]}} uses reference-based {{Object.equals}} and 
{{Object.hashCode}} rather than content-based equality ({{Arrays.equals}} / 
{{Arrays.hashCode}}), checking membership of a scanned {{byte[]}} against a 
{{Set<byte[]>}} (or comparing by reference) always returns {{false}}. Only the 
{{NULL AS TRUE}} branch of the {{Sarg}} succeeds.

h3. Expected Behavior
* {{WHERE b = X'8000' OR b IS NULL}} should return {{2}}.
* {{WHERE b IN (X'8000', X'0001')}} should return {{2}}.
* {{WHERE b = X'8000' OR b = X'0001'}} should return {{2}}.

For {{BINARY}} / {{VARBINARY}} ({{byte[]}}) types, {{SEARCH}} / {{Sarg}} 
codegen should compare elements by byte content (e.g., 
{{java.util.Arrays.equals}} / {{ByteBuffer.wrap}}) or expand point checks into 
binary equality comparisons rather than relying on reference equality in 
{{HashSet<byte[]>}}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to