MasseGuillaume commented on code in PR #3238:
URL: https://github.com/apache/calcite/pull/3238#discussion_r1222041507
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5543,6 +5543,65 @@ private static void checkIf(SqlOperatorFixture f) {
"INTEGER ARRAY NOT NULL");
}
+ /** Tests {@code MAP_FROM_ARRAYS} function from Spark. */
+ @Test void testMapFromArraysFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.MAP_FROM_ARRAYS);
+ f0.checkFails("^map_from_arrays(array[1, 2], array['foo', 'bar'])^",
+ "No match found for function signature MAP_FROM_ARRAYS\\(<INTEGER
ARRAY>, "
+ + "<CHAR\\(3\\) ARRAY>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("map_from_arrays(array[1, 2], array['foo', 'bar'])",
"{1=foo, 2=bar}",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array[1, 1, null], array['foo', 'bar',
'name'])",
+ "{1=bar, null=name}", "(INTEGER, CHAR(4) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array(), array())",
+ "{}", "(UNKNOWN NOT NULL, UNKNOWN NOT NULL) MAP NOT NULL");
+ f.checkType("map_from_arrays(cast(null as integer array), array['foo',
'bar'])",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP");
+ f.checkNull("map_from_arrays(cast(null as integer array), array['foo',
'bar'])");
+
+ f.checkFails("^map_from_arrays(array[1, 2], 2)^",
Review Comment:
I would check both ways:`f.checkFails("^map_from_arrays(2, array[1, 2])^",`
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5543,6 +5543,65 @@ private static void checkIf(SqlOperatorFixture f) {
"INTEGER ARRAY NOT NULL");
}
+ /** Tests {@code MAP_FROM_ARRAYS} function from Spark. */
+ @Test void testMapFromArraysFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.MAP_FROM_ARRAYS);
+ f0.checkFails("^map_from_arrays(array[1, 2], array['foo', 'bar'])^",
+ "No match found for function signature MAP_FROM_ARRAYS\\(<INTEGER
ARRAY>, "
+ + "<CHAR\\(3\\) ARRAY>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("map_from_arrays(array[1, 2], array['foo', 'bar'])",
"{1=foo, 2=bar}",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array[1, 1, null], array['foo', 'bar',
'name'])",
+ "{1=bar, null=name}", "(INTEGER, CHAR(4) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array(), array())",
+ "{}", "(UNKNOWN NOT NULL, UNKNOWN NOT NULL) MAP NOT NULL");
+ f.checkType("map_from_arrays(cast(null as integer array), array['foo',
'bar'])",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP");
+ f.checkNull("map_from_arrays(cast(null as integer array), array['foo',
'bar'])");
+
+ f.checkFails("^map_from_arrays(array[1, 2], 2)^",
+ "Cannot apply 'MAP_FROM_ARRAYS' to arguments of type
'MAP_FROM_ARRAYS\\(<INTEGER ARRAY>,"
+ + " <INTEGER>\\)'. Supported form\\(s\\):
'MAP_FROM_ARRAYS\\(<ARRAY>, <ARRAY>\\)'",
+ false);
+ f.checkFails("map_from_arrays(array[1, 2], array['foo'])",
+ "Illegal arguments: The length of the keys array 2 is not equal to the
length "
+ + "of the values array 1 in MAP_FROM_ARRAYS function",
+ true);
+ }
+
+ /** Tests {@code STR_TO_MAP} function from Spark. */
+ @Test void testStrToMapFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.STR_TO_MAP);
+ f0.checkFails("^str_to_map('a=1,b=2', ',', '=')^",
+ "No match found for function signature STR_TO_MAP\\("
+ + "<CHARACTER>, <CHARACTER>, <CHARACTER>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("str_to_map('a=1,b=2', ',', '=')", "{a=1, b=2}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('a:1,b:2')", "{a=1, b=2}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('a:1,b:2', ',')", "{a=1, b=2}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('a=1&b=2', '&', '=')", "{a=1, b=2}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('k#2%v#3', '%', '#')", "{k=2, v=3}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('a:1&b:2', '&')", "{a=1, b=2}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('k:2%v:3', '%')", "{k=2, v=3}",
+ "(CHAR(7) NOT NULL, CHAR(7) NOT NULL) MAP NOT NULL");
+ f.checkScalar("str_to_map('a')", "{a=null}",
+ "(CHAR(1) NOT NULL, CHAR(1) NOT NULL) MAP NOT NULL");
+ f.checkType("str_to_map(cast(null as varchar))",
+ "(VARCHAR, VARCHAR) MAP");
+ f.checkNull("str_to_map(cast(null as varchar))");
+ }
Review Comment:
```
spark.sql("""select str_to_map("a,b,c")""").show(false)
+---------------------------------+
|str_to_map(a,b,c, ,, :) |
+---------------------------------+
|{a -> null, b -> null, c -> null}|
+---------------------------------+
spark.sql("""select str_to_map("a-b--c", "--")""").show(false)
+-------------------------+
|str_to_map(a-b--c, --, :)|
+-------------------------+
|{a-b -> null, c -> null} |
+-------------------------+
```
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5543,6 +5543,65 @@ private static void checkIf(SqlOperatorFixture f) {
"INTEGER ARRAY NOT NULL");
}
+ /** Tests {@code MAP_FROM_ARRAYS} function from Spark. */
+ @Test void testMapFromArraysFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.MAP_FROM_ARRAYS);
+ f0.checkFails("^map_from_arrays(array[1, 2], array['foo', 'bar'])^",
+ "No match found for function signature MAP_FROM_ARRAYS\\(<INTEGER
ARRAY>, "
+ + "<CHAR\\(3\\) ARRAY>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("map_from_arrays(array[1, 2], array['foo', 'bar'])",
"{1=foo, 2=bar}",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array[1, 1, null], array['foo', 'bar',
'name'])",
+ "{1=bar, null=name}", "(INTEGER, CHAR(4) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array(), array())",
+ "{}", "(UNKNOWN NOT NULL, UNKNOWN NOT NULL) MAP NOT NULL");
+ f.checkType("map_from_arrays(cast(null as integer array), array['foo',
'bar'])",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP");
+ f.checkNull("map_from_arrays(cast(null as integer array), array['foo',
'bar'])");
+
+ f.checkFails("^map_from_arrays(array[1, 2], 2)^",
+ "Cannot apply 'MAP_FROM_ARRAYS' to arguments of type
'MAP_FROM_ARRAYS\\(<INTEGER ARRAY>,"
+ + " <INTEGER>\\)'. Supported form\\(s\\):
'MAP_FROM_ARRAYS\\(<ARRAY>, <ARRAY>\\)'",
+ false);
+ f.checkFails("map_from_arrays(array[1, 2], array['foo'])",
+ "Illegal arguments: The length of the keys array 2 is not equal to the
length "
+ + "of the values array 1 in MAP_FROM_ARRAYS function",
+ true);
+ }
Review Comment:
```
spark.sql("""select map_from_arrays(array(null), array(1))""").show
java.lang.RuntimeException: Cannot use null as map key!
spark.sql("""select map_from_arrays(array("a"), null)""").show
+-------------------------------+
|map_from_arrays(array(a), NULL)|
+-------------------------------+
| null|
+-------------------------------+
```
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5543,6 +5543,65 @@ private static void checkIf(SqlOperatorFixture f) {
"INTEGER ARRAY NOT NULL");
}
+ /** Tests {@code MAP_FROM_ARRAYS} function from Spark. */
+ @Test void testMapFromArraysFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.MAP_FROM_ARRAYS);
+ f0.checkFails("^map_from_arrays(array[1, 2], array['foo', 'bar'])^",
+ "No match found for function signature MAP_FROM_ARRAYS\\(<INTEGER
ARRAY>, "
+ + "<CHAR\\(3\\) ARRAY>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("map_from_arrays(array[1, 2], array['foo', 'bar'])",
"{1=foo, 2=bar}",
Review Comment:
```
spark.sql("""select map_from_arrays(array(1, "1", true), array("a", "b",
"c"))""").show
org.apache.spark.sql.AnalysisException: cannot resolve 'array(1, '1', true)'
due to data type mismatch: input to function array should all be the same type,
but it's [int, string, boolean]; line 1 pos 23;
spark.sql("""select map_from_arrays(array("a", "b", "c"), array(1, "1",
true))""").show
org.apache.spark.sql.AnalysisException: cannot resolve 'array(1, '1', true)'
due to data type mismatch: input to function array should all be the same type,
but it's [int, string, boolean]; line 1 pos 45;
```
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -5543,6 +5543,65 @@ private static void checkIf(SqlOperatorFixture f) {
"INTEGER ARRAY NOT NULL");
}
+ /** Tests {@code MAP_FROM_ARRAYS} function from Spark. */
+ @Test void testMapFromArraysFunc() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.MAP_FROM_ARRAYS);
+ f0.checkFails("^map_from_arrays(array[1, 2], array['foo', 'bar'])^",
+ "No match found for function signature MAP_FROM_ARRAYS\\(<INTEGER
ARRAY>, "
+ + "<CHAR\\(3\\) ARRAY>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("map_from_arrays(array[1, 2], array['foo', 'bar'])",
"{1=foo, 2=bar}",
+ "(INTEGER NOT NULL, CHAR(3) NOT NULL) MAP NOT NULL");
+ f.checkScalar("map_from_arrays(array[1, 1, null], array['foo', 'bar',
'name'])",
+ "{1=bar, null=name}", "(INTEGER, CHAR(4) NOT NULL) MAP NOT NULL");
Review Comment:
```
spark.sql("""select map_from_arrays(array(1, 1, null), array('foo', 'bar',
'name'))""").show
java.lang.RuntimeException: Duplicate map key 1 was found, please check the
input data. If you want to remove the duplicated keys, you can set
spark.sql.mapKeyDedupPolicy to LAST_WIN so that the key inserted at last takes
precedence.
```
--
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]