use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestSerDeUtils method testStructBlock.
@Test
public void testStructBlock() {
// test simple structs
InnerStruct innerStruct = new InnerStruct(13, 14L);
com.facebook.presto.common.type.Type rowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class));
Block expected = rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L);
assertBlockEquals(actual, expected);
// test complex structs
OuterStruct outerStruct = new OuterStruct();
outerStruct.byteVal = (byte) 1;
outerStruct.shortVal = (short) 2;
outerStruct.intVal = 3;
outerStruct.longVal = 4L;
outerStruct.floatVal = 5.01f;
outerStruct.doubleVal = 6.001d;
outerStruct.stringVal = "seven";
outerStruct.byteArray = new byte[] { '2' };
InnerStruct is1 = new InnerStruct(2, -5L);
InnerStruct is2 = new InnerStruct(-10, 0L);
outerStruct.structArray = new ArrayList<>(2);
outerStruct.structArray.add(is1);
outerStruct.structArray.add(is2);
outerStruct.map = new TreeMap<>();
outerStruct.map.put("twelve", new InnerStruct(0, 5L));
outerStruct.map.put("fifteen", new InnerStruct(-5, -10L));
outerStruct.innerStruct = new InnerStruct(18, 19L);
com.facebook.presto.common.type.Type innerRowType = RowType.anonymous(ImmutableList.of(INTEGER, BIGINT));
com.facebook.presto.common.type.Type arrayOfInnerRowType = new ArrayType(innerRowType);
com.facebook.presto.common.type.Type mapOfInnerRowType = mapType(createUnboundedVarcharType(), innerRowType);
List<com.facebook.presto.common.type.Type> outerRowParameterTypes = ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, createUnboundedVarcharType(), createUnboundedVarcharType(), arrayOfInnerRowType, mapOfInnerRowType, innerRowType);
com.facebook.presto.common.type.Type outerRowType = RowType.anonymous(outerRowParameterTypes);
actual = toBinaryBlock(outerRowType, outerStruct, getInspector(OuterStruct.class));
ImmutableList.Builder<Object> outerRowValues = ImmutableList.builder();
outerRowValues.add((byte) 1);
outerRowValues.add((short) 2);
outerRowValues.add(3);
outerRowValues.add(4L);
outerRowValues.add(5.01f);
outerRowValues.add(6.001d);
outerRowValues.add("seven");
outerRowValues.add(new byte[] { '2' });
outerRowValues.add(arrayBlockOf(innerRowType, rowBlockOf(innerRowType.getTypeParameters(), 2, -5L), rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -10, 0L)));
outerRowValues.add(mapBlockOf(VARCHAR, innerRowType, new Object[] { utf8Slice("fifteen"), utf8Slice("twelve") }, new Object[] { rowBlockOf(innerRowType.getTypeParameters(), -5, -10L), rowBlockOf(innerRowType.getTypeParameters(), 0, 5L) }));
outerRowValues.add(rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 18, 19L));
assertBlockEquals(actual, rowBlockOf(outerRowParameterTypes, outerRowValues.build().toArray()));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class ExpressionConverter method toIcebergExpression.
private static Expression toIcebergExpression(String columnName, Type type, Domain domain) {
if (domain.isAll()) {
return alwaysTrue();
}
if (domain.getValues().isNone()) {
return domain.isNullAllowed() ? isNull(columnName) : alwaysFalse();
}
if (domain.getValues().isAll()) {
return domain.isNullAllowed() ? alwaysTrue() : not(isNull(columnName));
}
// Skip structural types. TODO: Evaluate Apache Iceberg's support for predicate on structural types
if (type instanceof ArrayType || type instanceof MapType || type instanceof RowType) {
return alwaysTrue();
}
ValueSet domainValues = domain.getValues();
Expression expression = null;
if (domain.isNullAllowed()) {
expression = isNull(columnName);
}
if (domainValues instanceof SortedRangeSet) {
List<Range> orderedRanges = ((SortedRangeSet) domainValues).getOrderedRanges();
expression = firstNonNull(expression, alwaysFalse());
for (Range range : orderedRanges) {
Marker low = range.getLow();
Marker high = range.getHigh();
Marker.Bound lowBound = low.getBound();
Marker.Bound highBound = high.getBound();
// case col <> 'val' is represented as (col < 'val' or col > 'val')
if (lowBound == EXACTLY && highBound == EXACTLY) {
// case ==
if (getIcebergLiteralValue(type, low).equals(getIcebergLiteralValue(type, high))) {
expression = or(expression, equal(columnName, getIcebergLiteralValue(type, low)));
} else {
// case between
Expression between = and(greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)), lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
expression = or(expression, between);
}
} else {
if (lowBound == EXACTLY && low.getValueBlock().isPresent()) {
// case >=
expression = or(expression, greaterThanOrEqual(columnName, getIcebergLiteralValue(type, low)));
} else if (lowBound == ABOVE && low.getValueBlock().isPresent()) {
// case >
expression = or(expression, greaterThan(columnName, getIcebergLiteralValue(type, low)));
}
if (highBound == EXACTLY && high.getValueBlock().isPresent()) {
// case <=
if (low.getValueBlock().isPresent()) {
expression = and(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
} else {
expression = or(expression, lessThanOrEqual(columnName, getIcebergLiteralValue(type, high)));
}
} else if (highBound == BELOW && high.getValueBlock().isPresent()) {
// case <
if (low.getValueBlock().isPresent()) {
expression = and(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
} else {
expression = or(expression, lessThan(columnName, getIcebergLiteralValue(type, high)));
}
}
}
}
return expression;
}
throw new VerifyException("Did not expect a domain value set other than SortedRangeSet but got " + domainValues.getClass().getSimpleName());
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
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);
int entryCount = block.getPositionCount() / 2;
BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, entryCount);
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();
return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class HiveTypes method toListTypeInfo.
private static TypeInfo toListTypeInfo(Type type) {
if (type instanceof ArrayType) {
ArrayType array = (ArrayType) type;
Type element = array.getElementType();
return getListTypeInfo(toTypeInfo(element));
}
throw unsupportedType(type);
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestSubfieldExtractor method testToRowExpression.
@Test
public void testToRowExpression() {
assertToRowExpression("a", DATE);
assertToRowExpression("a[1]", new ArrayType(INTEGER));
assertToRowExpression("a.b", rowType(ImmutableMap.of("b", VARCHAR, "c", DOUBLE)));
assertToRowExpression("a[1]", mapType(BIGINT, DOUBLE));
assertToRowExpression("a[\"hello\"]", mapType(VARCHAR, REAL));
assertToRowExpression("a[\"hello\"].b", mapType(VARCHAR, rowType(ImmutableMap.of("b", BIGINT))));
assertToRowExpression("a[\"hello\"].b.c", mapType(VARCHAR, rowType(ImmutableMap.of("b", rowType(ImmutableMap.of("c", BIGINT))))));
}
Aggregations