-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: array access is now 1-indexed instead of 0-indexed (#4057)
BREAKING CHANGE: any queries that were using array index mechanism should change to use 1-base indexing instead of 0-base.
- Loading branch information
Showing
13 changed files
with
139 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ksql-execution/src/main/java/io/confluent/ksql/execution/codegen/helpers/ArrayAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.execution.codegen.helpers; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Used by reflection in the SqlToJavaVisitor to resolve | ||
* ArrayAccess operations in a 1-indexed fashion. | ||
*/ | ||
public final class ArrayAccess { | ||
|
||
private ArrayAccess() { } | ||
|
||
/** | ||
* @param list the input list | ||
* @param index the index, base-1 or negative (n from the end) | ||
* @return the {@code index}-th item in {@code list} | ||
*/ | ||
public static <T> T arrayAccess(List<T> list, int index) { | ||
// subtract by 1 because SQL standard uses 1-based indexing; since | ||
// SQL standard does not support negative (end-based) indexing, we | ||
// will use -1 to represent the last element | ||
final int trueIndex = index >= 0 ? index - 1 : list.size() + index; | ||
if (trueIndex >= list.size() || trueIndex < 0) { | ||
return null; | ||
} | ||
|
||
return list.get(trueIndex); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...-execution/src/test/java/io/confluent/ksql/execution/codegen/helpers/ArrayAccessTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"; you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.execution.codegen.helpers; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
public class ArrayAccessTest { | ||
|
||
@Test | ||
public void shouldBeOneIndexed() { | ||
// Given: | ||
List<Integer> list = ImmutableList.of(1, 2); | ||
|
||
// When: | ||
Integer access = ArrayAccess.arrayAccess(list, 1); | ||
|
||
// Then: | ||
assertThat(access, is(1)); | ||
} | ||
|
||
@Test | ||
public void shouldSupportNegativeIndex() { | ||
// Given: | ||
List<Integer> list = ImmutableList.of(1, 2); | ||
|
||
// When: | ||
Integer access = ArrayAccess.arrayAccess(list, -1); | ||
|
||
// Then: | ||
assertThat(access, is(2)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullOnOutOfBoundsIndex() { | ||
// Given: | ||
List<Integer> list = ImmutableList.of(1, 2); | ||
|
||
// When: | ||
Integer access = ArrayAccess.arrayAccess(list, 3); | ||
|
||
// Then: | ||
assertThat(access, nullValue()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnNullOnNegativeOutOfBoundsIndex() { | ||
// Given: | ||
List<Integer> list = ImmutableList.of(1, 2); | ||
|
||
// When: | ||
Integer access = ArrayAccess.arrayAccess(list, -3); | ||
|
||
// Then: | ||
assertThat(access, nullValue()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters