use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class MapEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("array(row(K,V))")
public Block mapFromEntries(@TypeParameter("row(K,V)") RowType rowType, @SqlType("map(K,V)") Block block) {
verify(rowType.getTypeParameters().size() == 2);
verify(block.getPositionCount() % 2 == 0);
Type keyType = rowType.getTypeParameters().get(0);
Type valueType = rowType.getTypeParameters().get(1);
ArrayType arrayType = new ArrayType(rowType);
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = block.getPositionCount() / 2;
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
for (int i = 0; i < entryCount; i++) {
BlockBuilder rowBuilder = entryBuilder.beginBlockEntry();
keyType.appendTo(block, 2 * i, rowBuilder);
valueType.appendTo(block, 2 * i + 1, rowBuilder);
entryBuilder.closeEntry();
}
blockBuilder.closeEntry();
pageBuilder.declarePosition();
return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class RowEqualOperator method equals.
public static Boolean equals(RowType rowType, List<MethodHandle> fieldEqualOperators, Block leftRow, Block rightRow) {
boolean indeterminate = false;
for (int fieldIndex = 0; fieldIndex < leftRow.getPositionCount(); fieldIndex++) {
if (leftRow.isNull(fieldIndex) || rightRow.isNull(fieldIndex)) {
indeterminate = true;
continue;
}
Type fieldType = rowType.getTypeParameters().get(fieldIndex);
Object leftField = readNativeValue(fieldType, leftRow, fieldIndex);
Object rightField = readNativeValue(fieldType, rightRow, fieldIndex);
try {
MethodHandle equalOperator = fieldEqualOperators.get(fieldIndex);
Boolean result = (Boolean) equalOperator.invoke(leftField, rightField);
if (result == null) {
indeterminate = true;
} else if (!result) {
return false;
}
} catch (Throwable t) {
throw internalError(t);
}
}
if (indeterminate) {
return null;
}
return true;
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class MultimapFromEntriesFunction method multimapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,array(V))")
@SqlNullable
public Block multimapFromEntries(@TypeParameter("map(K,array(V))") MapType mapType, @SqlType("array(row(K,V))") Block block) {
Type keyType = mapType.getKeyType();
Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = block.getPositionCount();
if (entryCount > entryIndicesList.length) {
initializeEntryIndicesList(entryCount);
}
TypedSet keySet = new TypedSet(keyType, entryCount, NAME);
for (int i = 0; i < entryCount; i++) {
if (block.isNull(i)) {
clearEntryIndices(keySet.size());
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block rowBlock = rowType.getObject(block, i);
if (rowBlock.isNull(0)) {
clearEntryIndices(keySet.size());
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (keySet.contains(rowBlock, 0)) {
entryIndicesList[keySet.positionOf(rowBlock, 0)].add(i);
} else {
keySet.add(rowBlock, 0);
entryIndicesList[keySet.size() - 1].add(i);
}
}
BlockBuilder multimapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder singleMapWriter = multimapBlockBuilder.beginBlockEntry();
for (int i = 0; i < keySet.size(); i++) {
keyType.appendTo(rowType.getObject(block, entryIndicesList[i].getInt(0)), 0, singleMapWriter);
BlockBuilder singleArrayWriter = singleMapWriter.beginBlockEntry();
for (int entryIndex : entryIndicesList[i]) {
valueType.appendTo(rowType.getObject(block, entryIndex), 1, singleArrayWriter);
}
singleMapWriter.closeEntry();
}
multimapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
clearEntryIndices(keySet.size());
return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class MapFromEntriesFunction method mapFromEntries.
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block block) {
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
int entryCount = block.getPositionCount();
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");
for (int i = 0; i < entryCount; i++) {
if (block.isNull(i)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
}
Block rowBlock = rowType.getObject(block, i);
if (rowBlock.isNull(0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (uniqueKeys.contains(rowBlock, 0)) {
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, String.format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, rowBlock, 0)));
}
uniqueKeys.add(rowBlock, 0);
keyType.appendTo(rowBlock, 0, resultBuilder);
valueType.appendTo(rowBlock, 1, resultBuilder);
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class ZipFunction method zip.
@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
int biggestCardinality = 0;
for (Block array : arrays) {
biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
}
RowType rowType = RowType.anonymous(types);
BlockBuilder outputBuilder = rowType.createBlockBuilder(null, biggestCardinality);
for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
rowBuilder.appendNull();
} else {
types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
}
}
outputBuilder.closeEntry();
}
return outputBuilder.build();
}
Aggregations