use of org.apache.flink.table.types.logical.ArrayType in project flink by apache.
the class ArrayToStringCastRule method generateCodeBlockInternal.
/* Example generated code for ARRAY<INT> -> CHAR(10)
isNull$0 = _myInputIsNull;
if (!isNull$0) {
builder$1.setLength(0);
builder$1.append("[");
for (int i$3 = 0; i$3 < _myInput.size(); i$3++) {
if (builder$1.length() > 10) {
break;
}
if (i$3 != 0) {
builder$1.append(", ");
}
int element$4 = -1;
boolean elementIsNull$5 = _myInput.isNullAt(i$3);
if (!elementIsNull$5) {
element$4 = _myInput.getInt(i$3);
isNull$2 = false;
if (!isNull$2) {
result$3 = org.apache.flink.table.data.binary.BinaryStringData.fromString("" + element$4);
isNull$2 = result$3 == null;
} else {
result$3 = org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
}
builder$1.append(result$3);
} else {
builder$1.append("NULL");
}
}
builder$1.append("]");
java.lang.String resultString$2;
resultString$2 = builder$1.toString();
if (builder$1.length() > 10) {
resultString$2 = builder$1.substring(0, java.lang.Math.min(builder$1.length(), 10));
} else {
if (resultString$2.length() < 10) {
int padLength$6;
padLength$6 = 10 - resultString$2.length();
java.lang.StringBuilder sbPadding$7;
sbPadding$7 = new java.lang.StringBuilder();
for (int i$8 = 0; i$8 < padLength$6; i$8++) {
sbPadding$7.append(" ");
}
resultString$2 = resultString$2 + sbPadding$7.toString();
}
}
result$1 = org.apache.flink.table.data.binary.BinaryStringData.fromString(resultString$2);
isNull$0 = result$1 == null;
} else {
result$1 = org.apache.flink.table.data.binary.BinaryStringData.EMPTY_UTF8;
}
*/
@Override
protected String generateCodeBlockInternal(CodeGeneratorCastRule.Context context, String inputTerm, String returnVariable, LogicalType inputLogicalType, LogicalType targetLogicalType) {
final LogicalType innerInputType = ((ArrayType) inputLogicalType).getElementType();
final String builderTerm = newName("builder");
context.declareClassField(className(StringBuilder.class), builderTerm, constructorCall(StringBuilder.class));
final String resultStringTerm = newName("resultString");
final int length = LogicalTypeChecks.getLength(targetLogicalType);
CastRuleUtils.CodeWriter writer = new CastRuleUtils.CodeWriter().stmt(methodCall(builderTerm, "setLength", 0)).stmt(methodCall(builderTerm, "append", strLiteral("["))).forStmt(methodCall(inputTerm, "size"), (indexTerm, loopBodyWriter) -> {
String elementTerm = newName("element");
String elementIsNullTerm = newName("elementIsNull");
CastCodeBlock codeBlock = // Null check is done at the array access level
CastRuleProvider.generateAlwaysNonNullCodeBlock(context, elementTerm, innerInputType, STRING_TYPE);
if (!context.legacyBehaviour() && couldTrim(length)) {
// Break if the target length is already exceeded
loopBodyWriter.ifStmt(stringExceedsLength(builderTerm, length), CastRuleUtils.CodeWriter::breakStmt);
}
loopBodyWriter.ifStmt(indexTerm + " != 0", thenBodyWriter -> thenBodyWriter.stmt(methodCall(builderTerm, "append", strLiteral(", ")))).declPrimitiveStmt(innerInputType, elementTerm).declStmt(boolean.class, elementIsNullTerm, methodCall(inputTerm, "isNullAt", indexTerm)).ifStmt("!" + elementIsNullTerm, thenBodyWriter -> thenBodyWriter.assignStmt(elementTerm, rowFieldReadAccess(indexTerm, inputTerm, innerInputType)).append(codeBlock).stmt(methodCall(builderTerm, "append", codeBlock.getReturnTerm())), elseBodyWriter -> elseBodyWriter.stmt(methodCall(builderTerm, "append", nullLiteral(context.legacyBehaviour()))));
}).stmt(methodCall(builderTerm, "append", strLiteral("]")));
return CharVarCharTrimPadCastRule.padAndTrimStringIfNeeded(writer, targetLogicalType, context.legacyBehaviour(), length, resultStringTerm, builderTerm).assignStmt(returnVariable, CastRuleUtils.staticCall(BINARY_STRING_DATA_FROM_STRING(), resultStringTerm)).toString();
}
use of org.apache.flink.table.types.logical.ArrayType in project flink by apache.
the class LogicalTypeJsonDeserializer method deserializeCollection.
private static LogicalType deserializeCollection(LogicalTypeRoot typeRoot, JsonNode logicalTypeNode, SerdeContext serdeContext) {
final JsonNode elementNode = logicalTypeNode.get(FIELD_NAME_ELEMENT_TYPE);
final LogicalType elementType = deserialize(elementNode, serdeContext);
switch(typeRoot) {
case ARRAY:
return new ArrayType(elementType);
case MULTISET:
return new MultisetType(elementType);
default:
throw new TableException("Collection type root expected.");
}
}
use of org.apache.flink.table.types.logical.ArrayType in project flink by apache.
the class RowDataSerializerTest method testRowDataSerializerWithComplexTypes.
private static Object[] testRowDataSerializerWithComplexTypes() {
InternalTypeInfo<RowData> typeInfo = InternalTypeInfo.ofFields(new IntType(), new DoubleType(), VarCharType.STRING_TYPE, new ArrayType(new IntType()), new MapType(new IntType(), new IntType()));
GenericRowData[] data = new GenericRowData[] { createRow(null, null, null, null, null), createRow(0, null, null, null, null), createRow(0, 0.0, null, null, null), createRow(0, 0.0, fromString("a"), null, null), createRow(1, 0.0, fromString("a"), null, null), createRow(1, 1.0, fromString("a"), null, null), createRow(1, 1.0, fromString("b"), null, null), createRow(1, 1.0, fromString("b"), createArray(1), createMap(new int[] { 1 }, new int[] { 1 })), createRow(1, 1.0, fromString("b"), createArray(1, 2), createMap(new int[] { 1, 4 }, new int[] { 1, 2 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3), createMap(new int[] { 1, 5 }, new int[] { 1, 3 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4), createMap(new int[] { 1, 6 }, new int[] { 1, 4 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4, 5), createMap(new int[] { 1, 7 }, new int[] { 1, 5 })), createRow(1, 1.0, fromString("b"), createArray(1, 2, 3, 4, 5, 6), createMap(new int[] { 1, 8 }, new int[] { 1, 6 })) };
RowDataSerializer serializer = typeInfo.toRowSerializer();
return new Object[] { serializer, data };
}
Aggregations