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

feat: Arrays should be indexable from the end too #3004

Merged
merged 11 commits into from
Jul 11, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,30 @@ protected Pair<String, Schema> visitSubscriptExpression(
SchemaUtil.getJavaType(internalSchema).getCanonicalName();
switch (internalSchema.type()) {
case ARRAY:
return new Pair<>(
String.format("((%s) ((%s)%s).get((int)(%s)))",
SchemaUtil.getJavaType(internalSchema.valueSchema()).getSimpleName(),
internalSchemaJavaType,
process(node.getBase(), context).getLeft(),
process(node.getIndex(), context).getLeft()
),
internalSchema.valueSchema()
);
final String listName = process(node.getBase(), context).getLeft();
if (node.getIndex().toString().startsWith("-")) {
ouertani marked this conversation as resolved.
Show resolved Hide resolved
return new Pair<>(
String.format("((%s) ((%s)%s).get((int)((%s)%s).size()%s))",
SchemaUtil.getJavaType(internalSchema.valueSchema()).getSimpleName(),
internalSchemaJavaType,
listName,
internalSchemaJavaType,
listName,
process(node.getIndex(), context).getLeft()
),
internalSchema.valueSchema()
);
} else {
return new Pair<>(
String.format("((%s) ((%s)%s).get((int)(%s)))",
SchemaUtil.getJavaType(internalSchema.valueSchema()).getSimpleName(),
internalSchemaJavaType,
listName,
process(node.getIndex(), context).getLeft()
),
internalSchema.valueSchema()
);
}
case MAP:
return new Pair<>(
String.format("((%s) ((%s)%s).get(%s))",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public void shouldProcessArrayExpressionCorrectly() {
equalTo("((Double) ((java.util.List)TEST1_COL4).get((int)(Integer.parseInt(\"0\"))))"));
}

@Test
public void shouldProcessArrayNegativeIndexExpressionCorrectly() {
final String simpleQuery = "SELECT col4[-1] FROM test1 WHERE col0 > 100;";
ouertani marked this conversation as resolved.
Show resolved Hide resolved
final Analysis analysis = analyzeQuery(simpleQuery, metaStore);

final String javaExpression = sqlToJavaVisitor
.process(analysis.getSelectExpressions().get(0));

assertThat(javaExpression,
equalTo("((Double) ((java.util.List)TEST1_COL4).get((int)((java.util.List)TEST1_COL4).size()-Integer.parseInt(\"1\")))"));
}

@Test
public void shouldProcessMapExpressionCorrectly() {
final String simpleQuery = "SELECT col5['key1'] FROM test1 WHERE col0 > 100;";
Expand Down