andygrove commented on code in PR #402:
URL: https://github.com/apache/datafusion-spark/pull/402#discussion_r4669574177
##########
crates/spark/src/function/generator/stack.rs:
##########
@@ -198,29 +200,57 @@ fn column_types(
for (i, arg_field) in arg_fields.iter().enumerate() {
has_int32[i % num_fields] |= arg_field.data_type() == &DataType::Int32;
}
- let mut types = vec![DataType::Null; num_fields];
- for (i, arg_field) in arg_fields.iter().enumerate() {
+ let actual_type = |i: usize| -> &DataType {
let col = i % num_fields;
let literal = scalar_arguments.get(i).copied().flatten();
- let actual = if has_int32[col]
- && arg_field.data_type() == &DataType::Int64
- && literal.is_some_and(|literal|
literal.cast_to(&DataType::Int32).is_ok())
+ if has_int32[col]
+ && arg_fields[i].data_type() == &DataType::Int64
+ && literal.is_some_and(|lit| lit.cast_to(&DataType::Int32).is_ok())
{
&DataType::Int32
} else {
- arg_field.data_type()
- };
- let expected = &mut types[col];
- if expected.is_null() {
- *expected = actual.clone();
- } else if !actual.is_null() && !same_spark_type(actual, expected) {
+ arg_fields[i].data_type()
+ }
+ };
+
+ let is_null_literal = |i: usize| -> bool {
+ arg_fields[i].data_type().is_null()
+ && scalar_arguments.get(i).copied().flatten().is_some()
+ };
+
+ let mut types = Vec::with_capacity(num_fields);
+ for col in 0..num_fields {
+ if is_null_literal(col) {
Review Comment:
Nice to see the Spark error class and SQL type names here. Since the message
now aims to match Spark, I compared a couple of mixed cases on PySpark 4.2.0
and 3.5, with `t(a INT, b VOID)`:
- `stack(3, NULL, b, a)`: Spark reports `"INT" (1) <> "VOID" (2)`. This PR
reports `"VOID" (1) <> "BIGINT" (3)`.
- `stack(3, b, NULL, a)`: Spark reports `"VOID" (1) <> "INT" (2)`. This PR
reports `"VOID" (1) <> "BIGINT" (3)`.
Both are still rejected, so it is only the position and type in the message.
The cause is that Spark's `StackCoercion` gives a NULL literal the type of the
first value in its column that is not of `NullType` (`findDataType`), so a
`VOID` column is skipped along with the literals. `checkInputDataTypes` then
checks every value, the substituted literals included. Here the leading literal
takes the first non-literal value's type, which can be a `VOID` column, and
literals are skipped in the check.
What do you think about porting it directly? Each NULL literal gets the
first non-`Null` `actual_type` in its column (or `Null`). Each column's
expected type is its first value after that substitution. Then every value is
checked in argument order. I think that also lets the two loops collapse into
one. Could you add the two queries above as `query error` cases with the full
positions?
##########
crates/sqllogictest/test_files/generator/stack.slt:
##########
@@ -50,13 +50,31 @@ SELECT stack(2, NULL, NULL), arrow_typeof(stack(2, NULL,
NULL));
----
[{col0: NULL}, {col0: NULL}] List(Struct("col0": Null))
-# A NULL before an array, and a column of NULL type
-# xfail spark: stack accepts a NULL-typed column, Spark only converts NULL
literals and rejects VOID
(https://github.com/apache/datafusion-spark/issues/361)
-query ??
-SELECT stack(2, NULL, a), stack(2, a, b) FROM (VALUES (1, NULL), (NULL, NULL))
AS t(a, b);
+# A NULL before a column is accepted and coerced to the column's type
Review Comment:
Thanks for splitting the old combined record into separate cases. It makes
the NULL-literal and VOID-column behavior much easier to read.
The two new result records here need the same marker as the other result
records in this file, since `stack` returns a list of structs while Spark's
generator returns rows:
```
# xfail spark: intended: stack returns a list of structs for unnest, Spark's
generator returns rows
```
Without it the PySpark runner reports both `stack(2, NULL, a)` and `stack(2,
a, NULL)` as "results differ" and fails, and it is a required check on `.slt`
changes. I ran `crates/sqllogictest/scripts/pyspark_slt_runner.py` on this file
with PySpark 4.2.0 to confirm. The values match Spark, so only the marker is
missing. Could you add it above both records?
--
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]