Skip to content

Conversation

@mihailoale-db
Copy link
Contributor

@mihailoale-db mihailoale-db commented Apr 15, 2025

What changes were proposed in this pull request?

Following query throw Cannot cast NullType to Arraytype:

SELECT get(null, 0);

instead of throwing a more user friendly one. I propose that we fix that.

Why are the changes needed?

To correct behavior of get function.

Does this PR introduce any user-facing change?

Query that were failing with internal error are now throwing a more user friendly one.

How was this patch tested?

Added tests.

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

No.

@github-actions github-actions bot added the SQL label Apr 15, 2025
Copy link
Member

@MaxGekk MaxGekk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you address another types of null:

spark-sql (default)> SELECT get(cast(null as string), 0);
[INTERNAL_ERROR] The Spark SQL phase analysis failed with an internal error. You hit a bug in Spark or the Spark plugins you use. Please, report this bug to the corresponding communities or vendors, and provide the full stack trace. SQLSTATE: XX000

@mihailoale-db
Copy link
Contributor Author

After offline discussion with @srielau we agreed that throwing is the way to go to make it on par with element_at function.

@mihailoale-db mihailoale-db changed the title [SPARK-51805] [SQL] Get with null argument should return null instead of throwing cast error [SPARK-51805] [SQL] Get with null argument should throw proper exception instead of an internal one Apr 16, 2025
@mihailoale-db mihailoale-db changed the title [SPARK-51805] [SQL] Get with null argument should throw proper exception instead of an internal one [SPARK-51805] [SQL] Get function with improper argument should throw proper exception instead of an internal one Apr 16, 2025
@mihailoale-db mihailoale-db force-pushed the getnull branch 4 times, most recently from 5cc7a9d to 069d411 Compare April 22, 2025 14:49
@mihailoale-db
Copy link
Contributor Author

@MaxGekk PTAL when you have time. Thanks

@mihailoale-db
Copy link
Contributor Author

@MaxGekk PTAL when you have time. Thanks


override def checkInputDataTypes(): TypeCheckResult = {
(left.dataType, right.dataType) match {
case (_: ArrayType, e2) if !e2.isInstanceOf[IntegralType] =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a test to examine this branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they have existed before (please check array.sql). But on the other hand select get(array(1),null) passes because GetArrayItem extends ExpectsInputTypes and because of that NullType is casted to IntegerType. Should we remove this check as it is noop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep it for safety (so we don't throw internal error if we somehow fail to apply type coercion rule). @cloud-fan

@cloud-fan
Copy link
Contributor

cloud-fan commented Apr 25, 2025

thanks, merging to master!

@cloud-fan cloud-fan closed this in 1c1d80f Apr 25, 2025
yhuang-db pushed a commit to yhuang-db/spark that referenced this pull request Jun 9, 2025
…roper exception instead of an internal one

### What changes were proposed in this pull request?
Following query throw `Cannot cast NullType to Arraytype`:

```
SELECT get(null, 0);
```

instead of throwing a more user friendly one. I propose that we fix that.

### Why are the changes needed?
To correct behavior of `get` function.

### Does this PR introduce _any_ user-facing change?
Query that were failing with internal error are now throwing a more user friendly one.

### How was this patch tested?
Added tests.

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

Closes apache#50590 from mihailoale-db/getnull.

Authored-by: mihailoale-db <mihailo.aleksic@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
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.

4 participants