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

[CALCITE-6285] Function ARRAY_INSERT produces an incorrect result for… #3748

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5316,6 +5316,10 @@ public static List arrayExcept(List list1, List list2) {
+ "and not exceeds the allowed limit.");
}

if (posInt == -1) {
// This means "append to the array"
posInt = baseArray.length + 1;
}
boolean usePositivePos = posInt > 0;

if (usePositivePos) {
Expand All @@ -5341,7 +5345,10 @@ public static List arrayExcept(List list1, List list2) {

return Arrays.asList(newArray);
} else {
int posIndex = posInt;
// 1-based index.
// The behavior of this function was changed in Spark 3.4.0.
// https://issues.apache.org/jira/browse/SPARK-44840
int posIndex = posInt + 1;

boolean newPosExtendsArrayLeft = baseArray.length + posIndex < 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7008,18 +7008,20 @@ void checkRegexpExtract(SqlOperatorFixture f0, FunctionAlias functionAlias) {
"[1, 2, null, 3]", "INTEGER ARRAY NOT NULL");
f1.checkScalar("array_insert(array[2, 3, 4], 1, 1)",
"[1, 2, 3, 4]", "INTEGER NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[1, 3, 4], -2, 2)",
f1.checkScalar("array_insert(array[1, 3, 4], -1, 2)",
Copy link
Member

Choose a reason for hiding this comment

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

I have a question, what will happen if the array length is 5, but I fill in -6? I can test it in spark

Copy link
Member

@caicancai caicancai Apr 2, 2024

Choose a reason for hiding this comment

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

@mihaibudiu Hello!
array_insert(array(1, 3, 4), -4, 2) Spark seems to allow this situation, should we add some test samples?

2024-04-02 23-10-05屏幕截图

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the docs say that this is a prepend. there is a test for this below

Copy link
Member

Choose a reason for hiding this comment

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

Thank you for your reply

"[1, 3, 4, 2]", "INTEGER NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[1, 3, 4], -3, 2)",
"[1, 2, 3, 4]", "INTEGER NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[2, 3, null, 4], -5, 1)",
f1.checkScalar("array_insert(array[2, 3, null, 4], -6, 1)",
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should write array(), spark does not allow array[]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these two forms are equivalent

Copy link
Member

Choose a reason for hiding this comment

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

thank you for your reply

"[1, null, 2, 3, null, 4]", "INTEGER ARRAY NOT NULL");
// check complex type
f1.checkScalar("array_insert(array[array[1,2]], 1, array[1])",
"[[1], [1, 2]]", "INTEGER NOT NULL ARRAY NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[array[1,2]], -1, array[1])",
"[[1], [1, 2]]", "INTEGER NOT NULL ARRAY NOT NULL ARRAY NOT NULL");
"[[1, 2], [1]]", "INTEGER NOT NULL ARRAY NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[map[1, 'a']], 1, map[2, 'b'])", "[{2=b}, {1=a}]",
"(INTEGER NOT NULL, CHAR(1) NOT NULL) MAP NOT NULL ARRAY NOT NULL");
f1.checkScalar("array_insert(array[map[1, 'a']], -1, map[2, 'b'])", "[{2=b}, {1=a}]",
f1.checkScalar("array_insert(array[map[1, 'a']], -1, map[2, 'b'])", "[{1=a}, {2=b}]",
"(INTEGER NOT NULL, CHAR(1) NOT NULL) MAP NOT NULL ARRAY NOT NULL");

// element cast to the biggest type
Expand Down