use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestDomainTranslator method testSubfields.
@Test
public void testSubfields() {
Map<String, RowExpression> expressions = ImmutableMap.<String, RowExpression>builder().put("c_bigint", C_BIGINT).put("c_bigint_array[5]", arraySubscript(C_BIGINT_ARRAY, 5)).put("c_bigint_to_bigint_map[5]", mapSubscript(C_BIGINT_TO_BIGINT_MAP, constant(5L, BIGINT))).put("c_varchar_to_bigint_map[\"foo\"]", mapSubscript(C_VARCHAR_TO_BIGINT_MAP, constant(Slices.utf8Slice("foo"), VARCHAR))).put("c_struct.a", dereference(C_STRUCT, 0)).put("c_struct.b.x", dereference(dereference(C_STRUCT, 1), 0)).put("c_struct.c[5]", arraySubscript(dereference(C_STRUCT, 2), 5)).put("c_struct.d[5]", mapSubscript(dereference(C_STRUCT, 3), constant(5L, BIGINT))).put("c_struct.e[\"foo\"]", mapSubscript(dereference(C_STRUCT, 4), constant(Slices.utf8Slice("foo"), VARCHAR))).build();
for (Map.Entry<String, RowExpression> entry : expressions.entrySet()) {
String subfield = entry.getKey();
RowExpression expression = entry.getValue();
assertPredicateTranslates(greaterThan(expression, bigintLiteral(2L)), subfield, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 2L)), false));
assertPredicateTranslates(equal(expression, bigintLiteral(2L)), subfield, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 2L)), false));
assertPredicateTranslates(between(expression, bigintLiteral(1L), bigintLiteral(2L)), subfield, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 2L, true)), false));
assertPredicateTranslates(bigintIn(expression, ImmutableList.of(1L)), subfield, Domain.singleValue(BIGINT, 1L));
assertPredicateTranslates(bigintIn(expression, ImmutableList.of(1L, 2L)), subfield, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false));
}
Type arrayType = C_BIGINT_ARRAY.getType();
assertPredicateTranslates(isNull(C_BIGINT_ARRAY), C_BIGINT_ARRAY.getName(), Domain.create(ValueSet.none(arrayType), true));
assertPredicateTranslates(not(isNull(C_BIGINT_ARRAY)), C_BIGINT_ARRAY.getName(), Domain.create(ValueSet.all(arrayType), false));
assertPredicateDoesNotTranslate(equal(C_BIGINT_ARRAY, createConstantExpression(createArrayBigintBlock(ImmutableList.of(ImmutableList.of(1L, 2L, 3L))), arrayType)));
MapType mapType = (MapType) C_BIGINT_TO_BIGINT_MAP.getType();
assertPredicateTranslates(isNull(C_BIGINT_TO_BIGINT_MAP), C_BIGINT_TO_BIGINT_MAP.getName(), Domain.create(ValueSet.none(mapType), true));
assertPredicateTranslates(not(isNull(C_BIGINT_TO_BIGINT_MAP)), C_BIGINT_TO_BIGINT_MAP.getName(), Domain.create(ValueSet.all(mapType), false));
assertPredicateDoesNotTranslate(equal(C_BIGINT_TO_BIGINT_MAP, createConstantExpression(createMapBlock(mapType, ImmutableMap.of(1, 100)), mapType)));
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testNestedMap.
@Test
public void testNestedMap() {
MapType innerMap = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayType arrayType = new ArrayType(innerMap);
MapType mapType = new MapType(BIGINT, arrayType, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
MapBlockBuilder beginEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
BlockBuilder mapWriter = beginEntryBlockBuilder.beginBlockEntry();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(mapWriter, i * 7 + j);
BlockBuilder arrayWriter = mapWriter.beginBlockEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
BlockBuilder innerMapWriter = arrayWriter.beginBlockEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapWriter, k * 10 + l);
VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value" + l));
}
arrayWriter.closeEntry();
arrayWriter.appendNull();
}
mapWriter.closeEntry();
}
beginEntryBlockBuilder.closeEntry();
}
MapBlockBuilder directEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
BlockBuilder keyBuilder = directEntryBlockBuilder.getKeyBlockBuilder();
ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getValueBlockBuilder();
MapBlockBuilder innerMapBuilder = (MapBlockBuilder) arrayBuilder.getElementBlockBuilder();
BlockBuilder innerMapKeyBuilder = innerMapBuilder.getKeyBlockBuilder();
BlockBuilder innerMapValueBuilder = innerMapBuilder.getValueBlockBuilder();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(keyBuilder, i * 7 + j);
arrayBuilder.beginDirectEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
innerMapBuilder.beginDirectEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapKeyBuilder, k * 10 + l);
VARCHAR.writeSlice(innerMapValueBuilder, utf8Slice("Value" + l));
}
innerMapBuilder.closeEntry();
innerMapBuilder.appendNull();
}
arrayBuilder.closeEntry();
}
directEntryBlockBuilder.closeEntry();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestMapBlock method assertValue.
private static void assertValue(Block mapBlock, int position, Map<String, Long> map) {
MapType mapType = mapType(VARCHAR, BIGINT);
MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, VARCHAR);
MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, VARCHAR, VARCHAR);
MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(VARCHAR));
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(VARCHAR));
// null maps are handled by assertPositionValue
requireNonNull(map, "map is null");
assertFalse(mapBlock.isNull(position));
SingleMapBlock elementBlock = (SingleMapBlock) mapType.getObject(mapBlock, position);
assertEquals(elementBlock.getPositionCount(), map.size() * 2);
// Test new/hash-index access: assert inserted keys
for (Map.Entry<String, Long> entry : map.entrySet()) {
int pos = elementBlock.seekKey(utf8Slice(entry.getKey()), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
assertNotEquals(pos, -1);
if (entry.getValue() == null) {
assertTrue(elementBlock.isNull(pos));
} else {
assertFalse(elementBlock.isNull(pos));
assertEquals(BIGINT.getLong(elementBlock, pos), (long) entry.getValue());
}
}
// Test new/hash-index access: assert non-existent keys
for (int i = 0; i < 10; i++) {
assertEquals(elementBlock.seekKey(utf8Slice("not-inserted-" + i), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), -1);
}
// Test legacy/iterative access
for (int i = 0; i < elementBlock.getPositionCount(); i += 2) {
String actualKey = VARCHAR.getSlice(elementBlock, i).toStringUtf8();
Long actualValue;
if (elementBlock.isNull(i + 1)) {
actualValue = null;
} else {
actualValue = BIGINT.getLong(elementBlock, i + 1);
}
assertTrue(map.containsKey(actualKey));
assertEquals(actualValue, map.get(actualKey));
}
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class JsonUtil method canCastFromJson.
public static boolean canCastFromJson(Type type) {
TypeSignature signature = type.getTypeSignature();
String baseType = signature.getBase();
if (signature.isEnum()) {
return true;
}
if (baseType.equals(StandardTypes.BOOLEAN) || baseType.equals(StandardTypes.TINYINT) || baseType.equals(StandardTypes.SMALLINT) || baseType.equals(StandardTypes.INTEGER) || baseType.equals(StandardTypes.BIGINT) || baseType.equals(StandardTypes.REAL) || baseType.equals(StandardTypes.DOUBLE) || baseType.equals(StandardTypes.VARCHAR) || baseType.equals(StandardTypes.DECIMAL) || baseType.equals(StandardTypes.JSON)) {
return true;
}
if (type instanceof ArrayType) {
return canCastFromJson(((ArrayType) type).getElementType());
}
if (type instanceof MapType) {
return isValidJsonObjectKeyType(((MapType) type).getKeyType()) && canCastFromJson(((MapType) type).getValueType());
}
if (type instanceof RowType) {
return type.getTypeParameters().stream().allMatch(JsonUtil::canCastFromJson);
}
return false;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class MapFlatBatchStreamReader method readBlock.
@Override
public Block readBlock() throws IOException {
if (!rowGroupOpen) {
openRowGroup();
}
if (readOffset > 0) {
if (presentStream != null) {
// skip ahead the present bit reader, but count the set bits
// and use this as the skip size for the data reader
readOffset = presentStream.countBitsSet(readOffset);
}
if (readOffset > 0) {
for (int i = 0; i < valueStreamReaders.size(); i++) {
int valueReadOffset = inMapStreams.get(i).countBitsSet(readOffset);
valueStreamReaders.get(i).prepareNextRead(valueReadOffset);
}
}
}
boolean[][] inMapVectors = new boolean[inMapStreamSources.size()][];
boolean[] nullVector = null;
int totalMapEntries = 0;
if (presentStream == null) {
for (int keyIndex = 0; keyIndex < inMapStreams.size(); keyIndex++) {
inMapVectors[keyIndex] = new boolean[nextBatchSize];
totalMapEntries += inMapStreams.get(keyIndex).getSetBits(nextBatchSize, inMapVectors[keyIndex]);
}
} else {
nullVector = new boolean[nextBatchSize];
int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
if (nullValues != nextBatchSize) {
for (int i = 0; i < inMapStreams.size(); i++) {
inMapVectors[i] = new boolean[nextBatchSize];
totalMapEntries += inMapStreams.get(i).getSetBits(nextBatchSize, inMapVectors[i], nullVector);
}
}
}
MapType mapType = (MapType) type;
Type valueType = mapType.getValueType();
Block[] valueBlocks = new Block[valueStreamReaders.size()];
if (totalMapEntries > 0) {
for (int keyIndex = 0; keyIndex < valueStreamReaders.size(); keyIndex++) {
int mapsContainingKey = 0;
for (int mapIndex = 0; mapIndex < nextBatchSize; mapIndex++) {
if (inMapVectors[keyIndex][mapIndex]) {
mapsContainingKey++;
}
}
if (mapsContainingKey > 0) {
BatchStreamReader streamReader = valueStreamReaders.get(keyIndex);
streamReader.prepareNextRead(mapsContainingKey);
valueBlocks[keyIndex] = streamReader.readBlock();
} else {
valueBlocks[keyIndex] = valueType.createBlockBuilder(null, 0).build();
}
}
}
int[] valueBlockPositions = new int[inMapVectors.length];
BlockBuilder valueBlockBuilder = valueType.createBlockBuilder(null, totalMapEntries);
int[] keyIds = new int[totalMapEntries];
int keyIdsIndex = 0;
int[] mapOffsets = new int[nextBatchSize + 1];
mapOffsets[0] = 0;
for (int mapIndex = 0; mapIndex < nextBatchSize; mapIndex++) {
int mapLength = 0;
if (totalMapEntries > 0) {
for (int keyIndex = 0; keyIndex < inMapVectors.length; keyIndex++) {
if (inMapVectors[keyIndex][mapIndex]) {
mapLength++;
valueType.appendTo(valueBlocks[keyIndex], valueBlockPositions[keyIndex], valueBlockBuilder);
keyIds[keyIdsIndex++] = keyIndex;
valueBlockPositions[keyIndex]++;
}
}
}
mapOffsets[mapIndex + 1] = mapOffsets[mapIndex] + mapLength;
}
Block block = mapType.createBlockFromKeyValue(nextBatchSize, Optional.ofNullable(nullVector), mapOffsets, new DictionaryBlock(keyBlockTemplate, keyIds), valueBlockBuilder);
readOffset = 0;
nextBatchSize = 0;
return block;
}
Aggregations