use of io.prestosql.spi.type.RowType.Field in project hetu-core by openlookeng.
the class JsonToRowCast method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
checkArgument(arity == 1, "Expected arity to be 1");
RowType rowType = (RowType) boundVariables.getTypeVariable("T");
checkCondition(canCastFromJson(rowType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", rowType);
List<Field> rowFields = rowType.getFields();
BlockBuilderAppender[] fieldAppenders = rowFields.stream().map(rowField -> createBlockBuilderAppender(rowField.getType())).toArray(BlockBuilderAppender[]::new);
MethodHandle methodHandle = METHOD_HANDLE.bindTo(rowType).bindTo(fieldAppenders).bindTo(getFieldNameToIndex(rowFields));
return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
use of io.prestosql.spi.type.RowType.Field in project hetu-core by openlookeng.
the class TypeCoercion method typeCompatibilityForRow.
private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
List<Field> firstFields = firstType.getFields();
List<Field> secondFields = secondType.getFields();
if (firstFields.size() != secondFields.size()) {
return TypeCompatibility.incompatible();
}
ImmutableList.Builder<Field> fields = ImmutableList.builder();
boolean coercible = true;
for (int i = 0; i < firstFields.size(); i++) {
Type firstFieldType = firstFields.get(i).getType();
Type secondFieldType = secondFields.get(i).getType();
TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
Type commonParameterType = typeCompatibility.getCommonSuperType();
Optional<String> firstParameterName = firstFields.get(i).getName();
Optional<String> secondParameterName = secondFields.get(i).getName();
Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
// ignore parameter name for coercible
coercible &= typeCompatibility.isCoercible();
fields.add(new Field(commonName, commonParameterType));
}
return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
use of io.prestosql.spi.type.RowType.Field in project hetu-core by openlookeng.
the class BuiltInTypeRegistry method typeCompatibilityForRow.
private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
List<Field> firstFields = firstType.getFields();
List<Field> secondFields = secondType.getFields();
if (firstFields.size() != secondFields.size()) {
return TypeCompatibility.incompatible();
}
ImmutableList.Builder<Field> fields = ImmutableList.builder();
boolean coercible = true;
for (int i = 0; i < firstFields.size(); i++) {
Type firstFieldType = firstFields.get(i).getType();
Type secondFieldType = secondFields.get(i).getType();
TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
if (!typeCompatibility.isCompatible()) {
return TypeCompatibility.incompatible();
}
Type commonParameterType = typeCompatibility.getCommonSuperType();
Optional<String> firstParameterName = firstFields.get(i).getName();
Optional<String> secondParameterName = secondFields.get(i).getName();
Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
// ignore parameter name for coercible
coercible &= typeCompatibility.isCoercible();
fields.add(new Field(commonName, commonParameterType));
}
return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
use of io.prestosql.spi.type.RowType.Field in project pulsar by apache.
the class PulsarProtobufNativeColumnDecoder method serializeRow.
private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
if (value == null) {
checkState(parentBlockBuilder != null, "parent block builder is null");
parentBlockBuilder.appendNull();
return null;
}
BlockBuilder blockBuilder;
if (parentBlockBuilder != null) {
blockBuilder = parentBlockBuilder;
} else {
blockBuilder = type.createBlockBuilder(null, 1);
}
BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
checkState(value instanceof DynamicMessage, "Row Field value should be DynamicMessage type.");
DynamicMessage record = (DynamicMessage) value;
List<Field> fields = ((RowType) type).getFields();
for (Field field : fields) {
checkState(field.getName().isPresent(), "field name not found");
serializeObject(singleRowBuilder, record.getField(((DynamicMessage) value).getDescriptorForType().findFieldByName(field.getName().get())), field.getType(), columnName);
}
blockBuilder.closeEntry();
if (parentBlockBuilder == null) {
return blockBuilder.getObject(0, Block.class);
}
return null;
}
use of io.prestosql.spi.type.RowType.Field in project pulsar by apache.
the class PulsarAvroColumnDecoder method serializeRow.
private static Block serializeRow(BlockBuilder parentBlockBuilder, Object value, Type type, String columnName) {
if (value == null) {
checkState(parentBlockBuilder != null, "parent block builder is null");
parentBlockBuilder.appendNull();
return null;
}
BlockBuilder blockBuilder;
if (parentBlockBuilder != null) {
blockBuilder = parentBlockBuilder;
} else {
blockBuilder = type.createBlockBuilder(null, 1);
}
BlockBuilder singleRowBuilder = blockBuilder.beginBlockEntry();
GenericRecord record = (GenericRecord) value;
List<Field> fields = ((RowType) type).getFields();
for (Field field : fields) {
checkState(field.getName().isPresent(), "field name not found");
serializeObject(singleRowBuilder, record.get(field.getName().get()), field.getType(), columnName);
}
blockBuilder.closeEntry();
if (parentBlockBuilder == null) {
return blockBuilder.getObject(0, Block.class);
}
return null;
}
Aggregations