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

Revert "Preserve case for RowType's field name and JSON content when CAST" #21866

Merged
merged 1 commit into from
Feb 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public static Field field(Type type)
return new Field(Optional.empty(), type);
}

public static Field field(String name, Type type, boolean delimited)
{
return new Field(Optional.of(name), type, delimited);
}

private static TypeSignature makeSignature(List<Field> fields)
{
int size = fields.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
Expand Down Expand Up @@ -1323,7 +1324,7 @@ public static void parseJsonToSingleRowBlock(
if (parser.currentToken() != FIELD_NAME) {
throw new JsonCastException(format("Expected a json field name, but got %s", parser.getText()));
}
String fieldName = parser.getText();
String fieldName = parser.getText().toLowerCase(Locale.ENGLISH);
Integer fieldIndex = fieldNameToIndex.get().get(fieldName);
parser.nextToken();
if (fieldIndex != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.facebook.presto.common.type.RowType;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.operator.scalar.AbstractTestFunctions;
import com.facebook.presto.operator.scalar.FunctionAssertions;
import com.facebook.presto.spi.StandardErrorCode;
Expand Down Expand Up @@ -409,35 +408,35 @@ public void testJsonToRow()
"\"array2\": [1, 2, null, 3], " +
"\"array1\": [], " +
"\"array3\": null, " +
"\"map3\": {\"a\": 1, \"b\": 2, \"none\": null, \"Three\": 3}, " +
"\"map3\": {\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, " +
"\"map1\": {}, " +
"\"map2\": null, " +
"\"rowAsJsonArray1\": [1, 2, null, 3], " +
"\"rowAsJsonArray2\": null, " +
"\"rowAsJsonObject2\": {\"a\": 1, \"b\": 2, \"none\": null, \"Three\": 3}, " +
"\"rowAsJsonObject2\": {\"a\": 1, \"b\": 2, \"none\": null, \"three\": 3}, " +
"\"rowAsJsonObject1\": null}' " +
"AS ROW(array1 array<BIGINT>, array2 ARRAY<bigint>, array3 ARRAY<BIGINT>, " +
"map1 MAP<VARCHAR, BIGINT>, map2 map<varchar, BIGINT>, map3 MAP<VARCHAR, BIGINT>, " +
"\"rowAsJsonArray1\" row(BIGINT, bigint, BIGINT, BIGINT), \"rowAsJsonArray2\" ROW(BIGINT)," +
"\"rowAsJsonObject1\" ROW(nothing BIGINT), \"rowAsJsonObject2\" ROW(a BIGINT, b BIGINT, \"Three\" BIGINT, none BIGINT)))",
"AS ROW(array1 ARRAY<BIGINT>, array2 ARRAY<BIGINT>, array3 ARRAY<BIGINT>, " +
"map1 MAP<VARCHAR, BIGINT>, map2 MAP<VARCHAR, BIGINT>, map3 MAP<VARCHAR, BIGINT>, " +
"rowAsJsonArray1 ROW(BIGINT, BIGINT, BIGINT, BIGINT), rowAsJsonArray2 ROW(BIGINT)," +
"rowAsJsonObject1 ROW(nothing BIGINT), rowAsJsonObject2 ROW(a BIGINT, b BIGINT, three BIGINT, none BIGINT)))",
RowType.from(ImmutableList.of(
RowType.field("array1", new ArrayType(BIGINT)),
RowType.field("array2", new ArrayType(BIGINT)),
RowType.field("array3", new ArrayType(BIGINT)),
RowType.field("map1", mapType(VARCHAR, BIGINT)),
RowType.field("map2", mapType(VARCHAR, BIGINT)),
RowType.field("map3", mapType(VARCHAR, BIGINT)),
RowType.field("rowAsJsonArray1", RowType.anonymous(ImmutableList.of(BIGINT, BIGINT, BIGINT, BIGINT)), true),
RowType.field("rowAsJsonArray2", RowType.anonymous(ImmutableList.of(BIGINT)), true),
RowType.field("rowAsJsonObject1", RowType.from(ImmutableList.of(RowType.field("nothing", BIGINT))), true),
RowType.field("rowAsJsonObject2", RowType.from(ImmutableList.of(
RowType.field("rowasjsonarray1", RowType.anonymous(ImmutableList.of(BIGINT, BIGINT, BIGINT, BIGINT))),
RowType.field("rowasjsonarray2", RowType.anonymous(ImmutableList.of(BIGINT))),
RowType.field("rowasjsonobject1", RowType.from(ImmutableList.of(RowType.field("nothing", BIGINT)))),
RowType.field("rowasjsonobject2", RowType.from(ImmutableList.of(
RowType.field("a", BIGINT),
RowType.field("b", BIGINT),
RowType.field("Three", BIGINT, true),
RowType.field("none", BIGINT))), true))),
RowType.field("three", BIGINT),
RowType.field("none", BIGINT)))))),
asList(
emptyList(), asList(1L, 2L, null, 3L), null,
ImmutableMap.of(), null, asMap(ImmutableList.of("a", "b", "none", "Three"), asList(1L, 2L, null, 3L)),
ImmutableMap.of(), null, asMap(ImmutableList.of("a", "b", "none", "three"), asList(1L, 2L, null, 3L)),
asList(1L, 2L, null, 3L), null,
null, asList(1L, 2L, 3L, null)));

Expand Down Expand Up @@ -526,14 +525,6 @@ public void testRowCast()
RowType.field("d", VARCHAR),
RowType.field("e", new ArrayType(BIGINT)))),
asList(2L, 1.5, true, "abc", ImmutableList.of(1L, 2L)));

assertFunction(
"MAP(ARRAY['myFirstRow', 'mySecondRow'], ARRAY[cast(row('row1FieldValue1', 'row1FieldValue2') as row(\"firstField\" varchar, \"secondField\" varchar)), cast(row('row2FieldValue1', 'row2FieldValue2') as row(\"firstField\" varchar, \"secondField\" varchar))])",
mapType(VarcharType.createVarcharType(11), RowType.from(ImmutableList.of(
RowType.field("firstField", VARCHAR, true),
RowType.field("secondField", VARCHAR, true)))),
ImmutableMap.of("myFirstRow", asList("row1FieldValue1", "row1FieldValue2"),
"mySecondRow", asList("row2FieldValue1", "row2FieldValue2")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Objects;
import java.util.Optional;

import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public final class Cast
Expand Down Expand Up @@ -66,7 +67,7 @@ public Cast(Optional<NodeLocation> location, Expression expression, String type,
requireNonNull(type, "type is null");

this.expression = expression;
this.type = transformCase(type);
this.type = type.toLowerCase(ENGLISH);
this.safe = safe;
this.typeOnly = typeOnly;
}
Expand All @@ -91,34 +92,6 @@ public boolean isTypeOnly()
return typeOnly;
}

/**
* We uniformly change the whole type string to lower case,
* except field names which are enclosed in double quotation marks
* */
private static String transformCase(String input)
{
if (input == null) {
return null;
}

StringBuilder sb = new StringBuilder();
boolean insideQuotes = false;
for (int i = 0, l = input.length(); i < l; i++) {
char ch = input.charAt(i);
if (ch == '"') {
insideQuotes = !insideQuotes;
}

if (insideQuotes) {
sb.append(ch);
}
else {
sb.append(Character.toLowerCase(ch));
}
}
return sb.toString();
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context)
{
Expand Down
Loading