zabetak commented on a change in pull request #2230:
URL: https://github.com/apache/calcite/pull/2230#discussion_r514134782
##########
File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
##########
@@ -2956,7 +2963,12 @@ public static Object structAccess(Object structObject,
int index, String fieldNa
} else {
Class<?> beanClass = structObject.getClass();
try {
- Field structField = beanClass.getDeclaredField(fieldName);
+ Field structField;
+ if (index >= 0 && index < beanClass.getDeclaredFields().length) {
+ structField = beanClass.getDeclaredFields()[index];
+ } else {
+ structField = beanClass.getDeclaredField(fieldName);
+ }
Review comment:
Is there a reason of why access by index is preferred over access by
name? `getDeclaredFields()` might be less efficient than `getDeclaredField()`;
both involve a copy but the first one will do a copy of all fields.
##########
File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
##########
@@ -2517,6 +2517,18 @@ public static Object item(Object object, Object index) {
if (object instanceof List && index instanceof Number) {
return arrayItem((List) object, ((Number) index).intValue());
}
+ if (index instanceof Number) {
Review comment:
I guess that if everything passes from the validator then we should
never arrive here with something that is not an integer but to be safe we could
add an assertion or `IllegalStateException` to be a bit more defensive.
##########
File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
##########
@@ -2517,6 +2517,18 @@ public static Object item(Object object, Object index) {
if (object instanceof List && index instanceof Number) {
return arrayItem((List) object, ((Number) index).intValue());
}
+ if (index instanceof Number) {
+ return structAccess(object, ((Number) index).intValue() - 1, ""); // 1
indexed
+ }
+ if (index instanceof String) {
Review comment:
Agree `operator.iq` sees more adequate for this.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]