use of java.util.Optional in project presto by prestodb.
the class JmxRecordSetProvider method getLiveRow.
public List<Object> getLiveRow(JmxTableHandle tableHandle, List<? extends ColumnHandle> columns, long entryTimestamp) throws JMException {
ImmutableMap<String, Optional<Object>> attributes = getAttributes(getColumnNames(columns), tableHandle);
List<Object> row = new ArrayList<>();
for (ColumnHandle column : columns) {
JmxColumnHandle jmxColumn = (JmxColumnHandle) column;
if (jmxColumn.getColumnName().equals(JmxMetadata.NODE_COLUMN_NAME)) {
row.add(nodeId);
} else if (jmxColumn.getColumnName().equals(JmxMetadata.TIMESTAMP_COLUMN_NAME)) {
row.add(entryTimestamp);
} else {
Optional<Object> optionalValue = attributes.get(jmxColumn.getColumnName());
if (optionalValue == null || !optionalValue.isPresent()) {
row.add(null);
} else {
Object value = optionalValue.get();
Class<?> javaType = jmxColumn.getColumnType().getJavaType();
if (javaType == boolean.class) {
if (value instanceof Boolean) {
row.add(value);
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == long.class) {
if (value instanceof Number) {
row.add(((Number) value).longValue());
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == double.class) {
if (value instanceof Number) {
row.add(((Number) value).doubleValue());
} else {
// mbeans can lie about types
row.add(null);
}
} else if (javaType == Slice.class) {
if (value.getClass().isArray()) {
// return a string representation of the array
if (value.getClass().getComponentType() == boolean.class) {
row.add(Arrays.toString((boolean[]) value));
} else if (value.getClass().getComponentType() == byte.class) {
row.add(Arrays.toString((byte[]) value));
} else if (value.getClass().getComponentType() == char.class) {
row.add(Arrays.toString((char[]) value));
} else if (value.getClass().getComponentType() == double.class) {
row.add(Arrays.toString((double[]) value));
} else if (value.getClass().getComponentType() == float.class) {
row.add(Arrays.toString((float[]) value));
} else if (value.getClass().getComponentType() == int.class) {
row.add(Arrays.toString((int[]) value));
} else if (value.getClass().getComponentType() == long.class) {
row.add(Arrays.toString((long[]) value));
} else if (value.getClass().getComponentType() == short.class) {
row.add(Arrays.toString((short[]) value));
} else {
row.add(Arrays.toString((Object[]) value));
}
} else {
row.add(value.toString());
}
}
}
}
}
return row;
}
use of java.util.Optional in project presto by prestodb.
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(t -> !t.isPresent())) {
return Optional.empty();
}
Set<TypeSignature> signatures = subTypes.stream().map(t -> t.get()).collect(toSet());
if (signatures.size() == 1) {
typeSignature = new TypeSignature(StandardTypes.ARRAY, signatures.stream().map(s -> TypeSignatureParameter.of(s)).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(String.format("%s%d", implicitPrefix, idx + 1), 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()) {
return Optional.empty();
}
parameters.add(TypeSignatureParameter.of(new NamedTypeSignature(key, fieldType.get())));
}
typeSignature = new TypeSignature(StandardTypes.ROW, parameters);
}
return Optional.ofNullable(typeSignature);
}
use of java.util.Optional in project presto by prestodb.
the class RaptorMetadata method beginInsert.
@Override
public ConnectorInsertTableHandle beginInsert(ConnectorSession session, ConnectorTableHandle tableHandle) {
RaptorTableHandle handle = (RaptorTableHandle) tableHandle;
long tableId = handle.getTableId();
ImmutableList.Builder<RaptorColumnHandle> columnHandles = ImmutableList.builder();
ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
for (TableColumn column : dao.listTableColumns(tableId)) {
columnHandles.add(new RaptorColumnHandle(connectorId, column.getColumnName(), column.getColumnId(), column.getDataType()));
columnTypes.add(column.getDataType());
}
long transactionId = shardManager.beginTransaction();
setTransactionId(transactionId);
Optional<String> externalBatchId = getExternalBatchId(session);
List<RaptorColumnHandle> sortColumnHandles = getSortColumnHandles(tableId);
List<RaptorColumnHandle> bucketColumnHandles = getBucketColumnHandles(tableId);
Optional<RaptorColumnHandle> temporalColumnHandle = Optional.ofNullable(dao.getTemporalColumnId(tableId)).map(temporalColumnId -> getOnlyElement(columnHandles.build().stream().filter(columnHandle -> columnHandle.getColumnId() == temporalColumnId).collect(toList())));
return new RaptorInsertTableHandle(connectorId, transactionId, tableId, columnHandles.build(), columnTypes.build(), externalBatchId, sortColumnHandles, nCopies(sortColumnHandles.size(), ASC_NULLS_FIRST), handle.getBucketCount(), bucketColumnHandles, temporalColumnHandle);
}
use of java.util.Optional in project presto by prestodb.
the class ConnectorMetadata method getInsertLayout.
/**
* Get the physical layout for a inserting into an existing table.
*/
default default Optional<ConnectorNewTableLayout> getInsertLayout(ConnectorSession session, ConnectorTableHandle tableHandle) {
List<ConnectorTableLayout> layouts = getTableLayouts(session, tableHandle, new Constraint<>(TupleDomain.all(), map -> true), Optional.empty()).stream().map(ConnectorTableLayoutResult::getTableLayout).filter(layout -> layout.getNodePartitioning().isPresent()).collect(toList());
if (layouts.isEmpty()) {
return Optional.empty();
}
if (layouts.size() > 1) {
throw new PrestoException(NOT_SUPPORTED, "Tables with multiple layouts can not be written");
}
ConnectorTableLayout layout = layouts.get(0);
ConnectorPartitioningHandle partitioningHandle = layout.getNodePartitioning().get().getPartitioningHandle();
Map<ColumnHandle, String> columnNamesByHandle = getColumnHandles(session, tableHandle).entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
List<String> partitionColumns = layout.getNodePartitioning().get().getPartitioningColumns().stream().map(columnNamesByHandle::get).collect(toList());
return Optional.of(new ConnectorNewTableLayout(partitioningHandle, partitionColumns));
}
use of java.util.Optional in project presto by prestodb.
the class TpchIndexMetadata method resolveIndex.
@Override
public Optional<ConnectorResolvedIndex> resolveIndex(ConnectorSession session, ConnectorTableHandle tableHandle, Set<ColumnHandle> indexableColumns, Set<ColumnHandle> outputColumns, TupleDomain<ColumnHandle> tupleDomain) {
TpchTableHandle tpchTableHandle = (TpchTableHandle) tableHandle;
// Keep the fixed values that don't overlap with the indexableColumns
// Note: technically we could more efficiently utilize the overlapped columns, but this way is simpler for now
Map<ColumnHandle, NullableValue> fixedValues = TupleDomain.extractFixedValues(tupleDomain).orElse(ImmutableMap.of()).entrySet().stream().filter(entry -> !indexableColumns.contains(entry.getKey())).filter(// strip nulls since meaningless in index join lookups
entry -> !entry.getValue().isNull()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
// determine all columns available for index lookup
Set<String> lookupColumnNames = ImmutableSet.<String>builder().addAll(handleToNames(ImmutableList.copyOf(indexableColumns))).addAll(handleToNames(ImmutableList.copyOf(fixedValues.keySet()))).build();
// do we have an index?
if (!indexedData.getIndexedTable(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(), lookupColumnNames).isPresent()) {
return Optional.empty();
}
TupleDomain<ColumnHandle> filteredTupleDomain = tupleDomain;
if (!tupleDomain.isNone()) {
filteredTupleDomain = TupleDomain.withColumnDomains(Maps.filterKeys(tupleDomain.getDomains().get(), not(in(fixedValues.keySet()))));
}
TpchIndexHandle indexHandle = new TpchIndexHandle(tpchTableHandle.getTableName(), tpchTableHandle.getScaleFactor(), lookupColumnNames, TupleDomain.fromFixedValues(fixedValues));
return Optional.of(new ConnectorResolvedIndex(indexHandle, filteredTupleDomain));
}
Aggregations