comphead commented on code in PR #26266: URL: https://github.com/apache/datafusion/pull/26266#discussion_r4261071480
########## datafusion/sqllogictest/test_files/spark/generator/stack.slt: ########## @@ -0,0 +1,156 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# Tests for Spark-compatible stack function +# https://spark.apache.org/docs/latest/api/sql/index.html#stack +# +# Test cases derived from Spark GeneratorFunctionSuite, GeneratorExpressionSuite, +# TypeCoercionSuite and the generators.sql, table-valued-functions.sql and +# join-lateral.sql golden files: +# https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/GeneratorFunctionSuite.scala +# +# Spark's stack generates rows. Here it returns them as a list of structs, which +# unnest expands. `n` must be a bare integer literal, so it is never cast. + +# Values fill n rows of ceil(k / n) columns in order, padded with NULLs +query ? +SELECT stack(1, 1, 2, 3); +---- +[{col0: 1, col1: 2, col2: 3}] + +query ? +SELECT stack(2, 1, 2, 3); +---- +[{col0: 1, col1: 2}, {col0: 3, col1: NULL}] + +query ? +SELECT stack(3, 1, 2, 3); +---- +[{col0: 1}, {col0: 2}, {col0: 3}] + +query ? +SELECT stack(4, 1, 2, 3); +---- +[{col0: 1}, {col0: 2}, {col0: 3}, {col0: NULL}] + +query TB +SELECT arrow_typeof(stack(2, a, b, c)), arrow_field(stack(2, a, b, c))['nullable'] FROM (VALUES (1, 2, 3)) AS t(a, b, c); +---- +List(non-null Struct("col0": Int64, "col1": Int64)) false + +# Various column types +query ? +SELECT stack(3, 1, 1.1, 'a', 2, 2.2, 'b', 3, 3.3, 'c'); +---- +[{col0: 1, col1: 1.1, col2: a}, {col0: 2, col1: 2.2, col2: b}, {col0: 3, col1: 3.3, col2: c}] + +query ? +SELECT stack(2, 1.5::DECIMAL(10, 2), '2024-01-01'::DATE, true, [1, 2], named_struct('a', 1), NULL, NULL, false, [3], named_struct('a', 2)); +---- +[{col0: 1.50, col1: 2024-01-01, col2: true, col3: [1, 2], col4: {a: 1}}, {col0: NULL, col1: NULL, col2: false, col3: [3], col4: {a: 2}}] + +# NULLs take the type of the first non-NULL value in their column +query ? +SELECT stack(3, 1, 1.1, NULL, 2, NULL, 'b', NULL, 3.3, 'c'); +---- +[{col0: 1, col1: 1.1, col2: NULL}, {col0: 2, col1: NULL, col2: b}, {col0: NULL, col1: 3.3, col2: c}] + +query TTTT +SELECT arrow_typeof(stack(3, 1, 2, NULL)), arrow_typeof(stack(3, NULL, '2', '3')), arrow_typeof(stack(2, NULL, NULL, 1, '2')), arrow_typeof(stack(3, NULL, NULL, NULL)); +---- +List(non-null Struct("col0": Int64)) List(non-null Struct("col0": Utf8)) List(non-null Struct("col0": Int64, "col1": Utf8)) List(non-null Struct("col0": Null)) + +# Array inputs +query ? +SELECT stack(2, a, b, c) FROM (VALUES (1, 2, 3), (4, 5, 6)) AS t(a, b, c); +---- +[{col0: 1, col1: 2}, {col0: 3, col1: NULL}] +[{col0: 4, col1: 5}, {col0: 6, col1: NULL}] + +# Array, scalar and NULL values in the same columns Review Comment: Test notes: - This case does not mix an array and a scalar in one column. Column 0 is `[a, b]` and column 1 is `[100, NULL]`, so the scalar fill in `stack_primitive` is only reached by the Rust test. A query with the layout of `test_stack_matches_interleave`, such as `stack(3, i, 'a', s, -1, NULL, 'b', i)` over a few rows, covers the strided, `repeat` and `interleave` paths in SQL. The 1000-row half of that Rust test can then go. The zero-row half has to stay, since `FilterExec` never emits an empty batch. - The `unnest(unnest(stack(2, 1, 2, 3)))` and `WHERE a > 1` cases do not exercise `stack`. The first is literal-only and is folded once during planning, so it only tests `unnest` of a constant. In the second no batch reaches the projection. - `stack(3, 1, 1.1, 'a', ...)` is covered by the NULL variant below it, and the `(1) ... (5)` mismatch case by the argument-order case after it. - There is no dictionary-encoded input. `stack(2, 'a', d)` and `stack(2, d, 'a')` with `d` cast to `Dictionary(Int32, Utf8)` cover the cast in both directions. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
