use of io.prestosql.spi.type.TypeSignatureParameter in project hetu-core by openlookeng.
the class ParquetReader method readStruct.
private ColumnChunk readStruct(GroupField field) throws IOException {
List<TypeSignatureParameter> fields = field.getType().getTypeSignature().getParameters();
Block[] localBlocks = new Block[fields.size()];
ColumnChunk columnChunk = null;
List<Optional<Field>> parameters = field.getChildren();
for (int i = 0; i < fields.size(); i++) {
Optional<Field> parameter = parameters.get(i);
if (parameter.isPresent()) {
columnChunk = readColumnChunk(parameter.get());
localBlocks[i] = columnChunk.getBlock();
}
}
for (int i = 0; i < fields.size(); i++) {
if (localBlocks[i] == null) {
localBlocks[i] = RunLengthEncodedBlock.create(field.getType(), null, columnChunk.getBlock().getPositionCount());
}
}
BooleanList structIsNull = StructColumnReader.calculateStructOffsets(field, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
boolean[] structIsNullVector = structIsNull.toBooleanArray();
Block rowBlock = RowBlock.fromFieldBlocks(structIsNullVector.length, Optional.of(structIsNullVector), localBlocks);
return new ColumnChunk(rowBlock, columnChunk.getDefinitionLevels(), columnChunk.getRepetitionLevels());
}
use of io.prestosql.spi.type.TypeSignatureParameter in project hetu-core by openlookeng.
the class OrcTester method rowType.
private static Type rowType(Type... fieldTypes) {
ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder();
for (int i = 0; i < fieldTypes.length; i++) {
String filedName = "field_" + i;
Type fieldType = fieldTypes[i];
typeSignatureParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(filedName, false)), fieldType.getTypeSignature())));
}
return METADATA.getFunctionAndTypeManager().getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build());
}
use of io.prestosql.spi.type.TypeSignatureParameter in project hetu-core by openlookeng.
the class BuiltInTypeRegistry method instantiateParametricType.
private Type instantiateParametricType(TypeManager typeManager, TypeSignature signature) {
List<TypeParameter> parameters = new ArrayList<>();
for (TypeSignatureParameter parameter : signature.getParameters()) {
TypeParameter typeParameter = TypeParameter.of(parameter, typeManager);
parameters.add(typeParameter);
}
ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
if (parametricType == null) {
throw new TypeNotFoundException(signature);
}
Type instantiatedType;
try {
instantiatedType = parametricType.createType(typeManager, parameters);
} catch (IllegalArgumentException e) {
throw new TypeNotFoundException(signature, e);
}
// TODO: reimplement this check? Currently "varchar(Integer.MAX_VALUE)" fails with "varchar"
return instantiatedType;
}
use of io.prestosql.spi.type.TypeSignatureParameter in project hetu-core by openlookeng.
the class MongoPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (isArrayType(type)) {
if (value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
((List<?>) value).forEach(element -> appendTo(type.getTypeParameters().get(0), element, builder));
output.closeEntry();
return;
}
} else if (isMapType(type)) {
if (value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
if (!(element instanceof Map<?, ?>)) {
continue;
}
Map<?, ?> document = (Map<?, ?>) element;
if (document.containsKey("key") && document.containsKey("value")) {
appendTo(type.getTypeParameters().get(0), document.get("key"), builder);
appendTo(type.getTypeParameters().get(1), document.get("value"), builder);
}
}
output.closeEntry();
return;
} else if (value instanceof Map) {
BlockBuilder builder = output.beginBlockEntry();
Map<?, ?> document = (Map<?, ?>) value;
for (Map.Entry<?, ?> entry : document.entrySet()) {
appendTo(type.getTypeParameters().get(0), entry.getKey(), builder);
appendTo(type.getTypeParameters().get(1), entry.getValue(), builder);
}
output.closeEntry();
return;
}
} else if (isRowType(type)) {
if (value instanceof Map) {
Map<?, ?> mapValue = (Map<?, ?>) value;
BlockBuilder builder = output.beginBlockEntry();
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), mapValue.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
} else if (value instanceof List<?>) {
List<?> listValue = (List<?>) value;
BlockBuilder builder = output.beginBlockEntry();
for (int index = 0; index < type.getTypeParameters().size(); index++) {
if (index < listValue.size()) {
appendTo(type.getTypeParameters().get(index), listValue.get(index), builder);
} else {
builder.appendNull();
}
}
output.closeEntry();
return;
}
} else {
throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
// not a convertible value
output.appendNull();
}
use of io.prestosql.spi.type.TypeSignatureParameter in project hetu-core by openlookeng.
the class MongoSession method guessFieldType.
private Optional<TypeSignature> guessFieldType(Object value) {
if (value == null) {
return Optional.empty();
}
TypeSignature typeSignature = null;
if (value instanceof String) {
typeSignature = createUnboundedVarcharType().getTypeSignature();
} else if (value instanceof Integer || value instanceof Long) {
typeSignature = BIGINT.getTypeSignature();
} else if (value instanceof Boolean) {
typeSignature = BOOLEAN.getTypeSignature();
} else if (value instanceof Float || value instanceof Double) {
typeSignature = DOUBLE.getTypeSignature();
} else if (value instanceof Date) {
typeSignature = TIMESTAMP.getTypeSignature();
} else if (value instanceof ObjectId) {
typeSignature = OBJECT_ID.getTypeSignature();
} else if (value instanceof List) {
List<Optional<TypeSignature>> subTypes = ((List<?>) value).stream().map(this::guessFieldType).collect(toList());
if (subTypes.isEmpty() || subTypes.stream().anyMatch(Optional::isPresent)) {
return Optional.empty();
}
Set<TypeSignature> signatures = subTypes.stream().map(Optional::get).collect(toSet());
if (signatures.size() == 1) {
typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(TypeSignatureParameter::of).collect(Collectors.toList()));
} else {
// TODO: presto cli doesn't handle empty field name row type yet
typeSignature = new TypeSignature(StandardTypes.ROW, IntStream.range(0, subTypes.size()).mapToObj(idx -> TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(format("%s%d", implicitPrefix, idx + 1), false)), subTypes.get(idx).get()))).collect(toList()));
}
} else if (value instanceof Document) {
List<TypeSignatureParameter> parameters = new ArrayList<>();
for (String key : ((Document) value).keySet()) {
Optional<TypeSignature> fieldType = guessFieldType(((Document) value).get(key));
if (fieldType.isPresent()) {
parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(key, false)), fieldType.get())));
}
}
if (!parameters.isEmpty()) {
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
}
return Optional.ofNullable(typeSignature);
}
Aggregations