Github user dilipbiswal commented on a diff in the pull request:
https://github.com/apache/spark/pull/22544#discussion_r220232666
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
---
@@ -2140,21 +2140,34 @@ case class ElementAt(left: Expression, right:
Expression) extends GetMapValueUti
}
override def inputTypes: Seq[AbstractDataType] = {
- Seq(TypeCollection(ArrayType, MapType),
- left.dataType match {
- case _: ArrayType => IntegerType
- case _: MapType => mapKeyType
- case _ => AnyDataType // no match for a wrong 'left' expression
type
- }
- )
+ (left.dataType, right.dataType) match {
+ case (ArrayType(e1, hasNull), e2: IntegralType) if (e2 != LongType)
=>
+ Seq(ArrayType(e1, hasNull), IntegerType)
+ case (MapType(keyType, valueType, hasNull), e2) =>
+ TypeCoercion.findTightestCommonType(keyType, e2) match {
+ case Some(dt) => Seq(MapType(dt, valueType, hasNull), dt)
+ case _ => Seq.empty
--- End diff --
@viirya
```select element_at(map(1,"one", 2, "two"), 2.2D);```
In this case, there is no key with value 2.2 in the map, right ? But we
return "two" as the answer. If we simply cast the right side to the key type of
the map, then we will cast 2.2 down to 2 and find "two". But by finding a
common type, we will compare both sides as double and correctly return "null"
as the answer.
Here is presto's output :
```SQL
presto:default> select element_at(MAP(array[1], array[1]), 1.1);
_col0
-------
NULL
(1 row)
Query 20180921_051929_00018_iif98, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]
presto:default> select element_at(MAP(array[1], array[1]), 1.0);
_col0
-------
1
(1 row)
```
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]