Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48955][SQL] ArrayCompact's datatype should be containsNull = false #47430

Closed

Conversation

zhengruifeng
Copy link
Contributor

What changes were proposed in this pull request?

ArrayCompact's datatype should be containsNull = false

Why are the changes needed?

ArrayCompact - Removes null values from the array

Does this PR introduce any user-facing change?

No

How was this patch tested?

Added test

before:

scala> val df = spark.range(1).select(lit(Array(1,2,3)).alias("a"))
val df: org.apache.spark.sql.DataFrame = [a: array<int>]

scala> df.printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- a: array (nullable = false)
 |    |-- element: integer (containsNull = true)


scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = true)

after

scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = false)

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Jul 20, 2024
@HyukjinKwon
Copy link
Member

Merged to master.

@zhengruifeng zhengruifeng deleted the sql_array_compact_data_type branch July 22, 2024 00:41
jingz-db pushed a commit to jingz-db/spark that referenced this pull request Jul 22, 2024
…= false`

### What changes were proposed in this pull request?
`ArrayCompact`'s datatype should be `containsNull = false`

### Why are the changes needed?
`ArrayCompact` - Removes null values from the array

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Added test

before:
```
scala> val df = spark.range(1).select(lit(Array(1,2,3)).alias("a"))
val df: org.apache.spark.sql.DataFrame = [a: array<int>]

scala> df.printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- a: array (nullable = false)
 |    |-- element: integer (containsNull = true)

scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = true)
```

after
```
scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = false)
```

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#47430 from zhengruifeng/sql_array_compact_data_type.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
attilapiros pushed a commit to attilapiros/spark that referenced this pull request Oct 4, 2024
…= false`

### What changes were proposed in this pull request?
`ArrayCompact`'s datatype should be `containsNull = false`

### Why are the changes needed?
`ArrayCompact` - Removes null values from the array

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Added test

before:
```
scala> val df = spark.range(1).select(lit(Array(1,2,3)).alias("a"))
val df: org.apache.spark.sql.DataFrame = [a: array<int>]

scala> df.printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- a: array (nullable = false)
 |    |-- element: integer (containsNull = true)

scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = true)
```

after
```
scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = false)
```

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#47430 from zhengruifeng/sql_array_compact_data_type.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
HyukjinKwon pushed a commit that referenced this pull request Oct 12, 2024
### What changes were proposed in this pull request?
Fix `containsNull` of `ArrayCompact`, by adding a new expression `KnownNotContainsNull`

### Why are the changes needed?

#47430 attempted to set `containsNull = false` for `ArrayCompact` for further optimization, but in an incomplete way:

The `ArrayCompact` is a runtime replaceable expression, so will be replaced in optimizer, and cause the `containsNull` be reverted, e.g.

```sql
select array_compact(array(1, null))
```

Rule `ReplaceExpressions` changed `containsNull: false -> true`
```
old schema:
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,false),false)

new schema
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,true),false)
```

### Does this PR introduce _any_ user-facing change?
no

### How was this patch tested?
added tests

### Was this patch authored or co-authored using generative AI tooling?
no

Closes #48410 from zhengruifeng/fix_array_compact_null.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
himadripal pushed a commit to himadripal/spark that referenced this pull request Oct 19, 2024
…= false`

### What changes were proposed in this pull request?
`ArrayCompact`'s datatype should be `containsNull = false`

### Why are the changes needed?
`ArrayCompact` - Removes null values from the array

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
Added test

before:
```
scala> val df = spark.range(1).select(lit(Array(1,2,3)).alias("a"))
val df: org.apache.spark.sql.DataFrame = [a: array<int>]

scala> df.printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- a: array (nullable = false)
 |    |-- element: integer (containsNull = true)

scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = true)
```

after
```
scala> df.select(array_compact(col("a"))).printSchema
warning: 1 deprecation (since 2.13.3); for details, enable `:setting -deprecation` or `:replay -deprecation`
root
 |-- array_compact(a): array (nullable = false)
 |    |-- element: integer (containsNull = false)
```

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#47430 from zhengruifeng/sql_array_compact_data_type.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
himadripal pushed a commit to himadripal/spark that referenced this pull request Oct 19, 2024
### What changes were proposed in this pull request?
Fix `containsNull` of `ArrayCompact`, by adding a new expression `KnownNotContainsNull`

### Why are the changes needed?

apache#47430 attempted to set `containsNull = false` for `ArrayCompact` for further optimization, but in an incomplete way:

The `ArrayCompact` is a runtime replaceable expression, so will be replaced in optimizer, and cause the `containsNull` be reverted, e.g.

```sql
select array_compact(array(1, null))
```

Rule `ReplaceExpressions` changed `containsNull: false -> true`
```
old schema:
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,false),false)

new schema
StructField(array_compact(array(1, NULL)),ArrayType(IntegerType,true),false)
```

### Does this PR introduce _any_ user-facing change?
no

### How was this patch tested?
added tests

### Was this patch authored or co-authored using generative AI tooling?
no

Closes apache#48410 from zhengruifeng/fix_array_compact_null.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants