|
| 1 | +-- Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +-- or more contributor license agreements. See the NOTICE file |
| 3 | +-- distributed with this work for additional information |
| 4 | +-- regarding copyright ownership. The ASF licenses this file |
| 5 | +-- to you under the Apache License, Version 2.0 (the |
| 6 | +-- "License"); you may not use this file except in compliance |
| 7 | +-- with the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, |
| 12 | +-- software distributed under the License is distributed on an |
| 13 | +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +-- KIND, either express or implied. See the License for the |
| 15 | +-- specific language governing permissions and limitations |
| 16 | +-- under the License. |
| 17 | + |
| 18 | +-- Spark adds no cast to IF when its branches differ only in whether a nested struct field, map |
| 19 | +-- value or array element can be NULL, so the native planner has to cast a branch to the common |
| 20 | +-- type, as it does for CASE WHEN. Without the cast, a batch in which every row took the ELSE |
| 21 | +-- branch returned the ELSE array with the other nullability, and the query failed with |
| 22 | +-- "column types must match schema types". A batch that mixed the two failed when the THEN branch |
| 23 | +-- was the one that could not be NULL. |
| 24 | +-- https://github.com/apache/datafusion-comet/issues/6334 |
| 25 | +-- |
| 26 | +-- The harness disables ConstantFolding, so these cover the constructor path. The folded map |
| 27 | +-- literal path is covered in CometMapExpressionSuite. |
| 28 | + |
| 29 | +statement |
| 30 | +CREATE TABLE test_if_nested(q boolean, i int, m map<string, int>, s struct<x: int>, ms map<string, struct<x: int>>) USING parquet |
| 31 | + |
| 32 | +-- Each INSERT writes its own files, and a batch never spans files. Every row of this one takes the |
| 33 | +-- THEN branch of IF(q, ...) and the ELSE branch of IF(m IS NULL, ...). |
| 34 | +statement |
| 35 | +INSERT INTO test_if_nested VALUES (true, 1, map('a', 1), named_struct('x', 1), map('a', named_struct('x', 1))), (true, NULL, map('b', CAST(NULL AS INT)), named_struct('x', CAST(NULL AS INT)), map('b', CAST(NULL AS STRUCT<x: INT>))) |
| 36 | + |
| 37 | +-- Every row of this one takes the ELSE branch of IF(q, ...) and the THEN branch of IF(m IS NULL, ...) |
| 38 | +statement |
| 39 | +INSERT INTO test_if_nested VALUES (false, 2, NULL, NULL, NULL), (NULL, NULL, NULL, NULL, NULL) |
| 40 | + |
| 41 | +-- Pairs of rows that take different branches, so these batches mix the two |
| 42 | +statement |
| 43 | +INSERT INTO test_if_nested VALUES (true, 3, map('c', 3), named_struct('x', 3), map('c', named_struct('x', 3))), (false, 4, NULL, NULL, NULL), (true, NULL, map('d', CAST(NULL AS INT)), named_struct('x', CAST(NULL AS INT)), map('d', named_struct('x', CAST(NULL AS INT)))), (NULL, 6, NULL, NULL, NULL), (true, 7, map('e', 7), named_struct('x', 7), map('e', CAST(NULL AS STRUCT<x: INT>))), (false, NULL, NULL, NULL, NULL), (true, 9, map('f', 9), named_struct('x', 9), map('f', named_struct('x', 9))), (false, 10, NULL, NULL, NULL), (true, 11, map('g', 11), named_struct('x', 11), map('g', named_struct('x', 11))), (NULL, 12, NULL, NULL, NULL) |
| 44 | + |
| 45 | +-- struct constructors: x can be NULL in one branch only |
| 46 | +query |
| 47 | +SELECT IF(q, named_struct('x', i), named_struct('x', 0)) FROM test_if_nested |
| 48 | + |
| 49 | +query |
| 50 | +SELECT IF(q, named_struct('x', 0), named_struct('x', i)) FROM test_if_nested |
| 51 | + |
| 52 | +-- map constructors: the value can be NULL in one branch only |
| 53 | +query |
| 54 | +SELECT IF(q, map('k', i), map('k', 0)) FROM test_if_nested |
| 55 | + |
| 56 | +query |
| 57 | +SELECT IF(q, map('k', 0), map('k', i)) FROM test_if_nested |
| 58 | + |
| 59 | +-- a map column, whose value can be NULL, and a map constructor |
| 60 | +query |
| 61 | +SELECT IF(q, m, map('z', 0)) FROM test_if_nested |
| 62 | + |
| 63 | +query |
| 64 | +SELECT IF(m IS NULL, map('z', 0), m) FROM test_if_nested |
| 65 | + |
| 66 | +-- a struct column and a struct constructor |
| 67 | +query |
| 68 | +SELECT IF(q, s, named_struct('x', 0)) FROM test_if_nested |
| 69 | + |
| 70 | +-- a struct inside a map value |
| 71 | +query |
| 72 | +SELECT IF(q, ms, map('z', named_struct('x', 0))) FROM test_if_nested |
| 73 | + |
| 74 | +-- an array inside a map value |
| 75 | +query |
| 76 | +SELECT IF(q, map('k', array(i)), map('k', array(0))) FROM test_if_nested |
| 77 | + |
| 78 | +-- arrays already agree, because CreateArray casts its elements to one type |
| 79 | +query |
| 80 | +SELECT IF(q, array(i), array(0)) FROM test_if_nested |
| 81 | + |
| 82 | +-- CASE WHEN already casts its branches to a common type |
| 83 | +query |
| 84 | +SELECT CASE WHEN q THEN named_struct('x', i) ELSE named_struct('x', 0) END FROM test_if_nested |
0 commit comments