use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestRowExpressionFormatter method testConstants.
@Test
public void testConstants() {
// null
RowExpression constantExpression = constantNull(UNKNOWN);
assertEquals(format(constantExpression), "null");
// boolean
constantExpression = constant(true, BOOLEAN);
assertEquals(format(constantExpression), "BOOLEAN'true'");
// double
constantExpression = constant(1.1, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'1.1'");
constantExpression = constant(Double.NaN, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'NaN'");
constantExpression = constant(Double.POSITIVE_INFINITY, DOUBLE);
assertEquals(format(constantExpression), "DOUBLE'Infinity'");
// real
constantExpression = constant((long) floatToIntBits(1.1f), REAL);
assertEquals(format(constantExpression), "REAL'1.1'");
constantExpression = constant((long) floatToIntBits(Float.NaN), REAL);
assertEquals(format(constantExpression), "REAL'NaN'");
constantExpression = constant((long) floatToIntBits(Float.POSITIVE_INFINITY), REAL);
assertEquals(format(constantExpression), "REAL'Infinity'");
// string
constantExpression = constant(utf8Slice("abcde"), VARCHAR);
assertEquals(format(constantExpression), "VARCHAR'abcde'");
constantExpression = constant(utf8Slice("fgh"), createCharType(3));
assertEquals(format(constantExpression), "CHAR'fgh'");
// integer
constantExpression = constant(1L, TINYINT);
assertEquals(format(constantExpression), "TINYINT'1'");
constantExpression = constant(1L, SMALLINT);
assertEquals(format(constantExpression), "SMALLINT'1'");
constantExpression = constant(1L, INTEGER);
assertEquals(format(constantExpression), "INTEGER'1'");
constantExpression = constant(1L, BIGINT);
assertEquals(format(constantExpression), "BIGINT'1'");
// varbinary
Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456AB"));
constantExpression = constant(value, VARBINARY);
assertEquals(format(constantExpression), "X'12 34 56 ab'");
// color
constantExpression = constant(256L, COLOR);
assertEquals(format(constantExpression), "COLOR'256'");
// long and short decimals
constantExpression = constant(decimal("1.2345678910"), DecimalType.createDecimalType(11, 10));
assertEquals(format(constantExpression), "DECIMAL'1.2345678910'");
constantExpression = constant(decimal("1.281734081274028174012432412423134"), DecimalType.createDecimalType(34, 33));
assertEquals(format(constantExpression), "DECIMAL'1.281734081274028174012432412423134'");
// time
constantExpression = constant(662727600000L, TIMESTAMP);
assertEquals(format(constantExpression), "TIMESTAMP'1991-01-01 00:00:00.000'");
constantExpression = constant(7670L, DATE);
assertEquals(format(constantExpression), "DATE'1991-01-01'");
// interval
constantExpression = constant(24L, INTERVAL_DAY_TIME);
assertEquals(format(constantExpression), "INTERVAL DAY TO SECOND'0 00:00:00.024'");
constantExpression = constant(25L, INTERVAL_YEAR_MONTH);
assertEquals(format(constantExpression), "INTERVAL YEAR TO MONTH'2-1'");
// block
constantExpression = constant(new LongArrayBlockBuilder(null, 4).writeLong(1L).writeLong(2).build(), new ArrayType(BIGINT));
assertEquals(format(constantExpression), "[Block: position count: 2; size: 96 bytes]");
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestRowExpressionFormatter method testComplex.
@Test
public void testComplex() {
RowExpression complexExpression;
RowExpression expression = createCallExpression(ADD);
complexExpression = call(SUBTRACT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBTRACT, fromTypes(BIGINT, BIGINT)), BIGINT, C_BIGINT, expression);
assertEquals(format(complexExpression), "(c_bigint) - ((c_bigint) + (BIGINT'5'))");
RowExpression expression1 = createCallExpression(ADD);
RowExpression expression2 = call(MULTIPLY.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(MULTIPLY, fromTypes(BIGINT, BIGINT)), BIGINT, expression1, C_BIGINT);
RowExpression expression3 = createCallExpression(GREATER_THAN);
complexExpression = new SpecialFormExpression(OR, BOOLEAN, expression2, expression3);
assertEquals(format(complexExpression), "(((c_bigint) + (BIGINT'5')) * (c_bigint)) OR ((c_bigint) > (BIGINT'5'))");
ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
Type elementType = arrayType.getElementType();
expression1 = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(5L, INTEGER)));
expression2 = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression1.getType())), expression1.getType(), expression1);
expression3 = call(ADD.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(ADD, fromTypes(expression2.getType(), BIGINT)), BIGINT, expression2, constant(5L, BIGINT));
assertEquals(format(expression3), "(-(c_bigint_array[INTEGER'5'])) + (BIGINT'5')");
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestRowExpressionFormatter method testCalls.
@Test
public void testCalls() {
RowExpression callExpression;
// arithmetic
callExpression = createCallExpression(ADD);
assertEquals(format(callExpression), "(c_bigint) + (BIGINT'5')");
callExpression = createCallExpression(SUBTRACT);
assertEquals(format(callExpression), "(c_bigint) - (BIGINT'5')");
callExpression = createCallExpression(MULTIPLY);
assertEquals(format(callExpression), "(c_bigint) * (BIGINT'5')");
callExpression = createCallExpression(DIVIDE);
assertEquals(format(callExpression), "(c_bigint) / (BIGINT'5')");
callExpression = createCallExpression(MODULUS);
assertEquals(format(callExpression), "(c_bigint) % (BIGINT'5')");
// comparison
callExpression = createCallExpression(GREATER_THAN);
assertEquals(format(callExpression), "(c_bigint) > (BIGINT'5')");
callExpression = createCallExpression(LESS_THAN);
assertEquals(format(callExpression), "(c_bigint) < (BIGINT'5')");
callExpression = createCallExpression(GREATER_THAN_OR_EQUAL);
assertEquals(format(callExpression), "(c_bigint) >= (BIGINT'5')");
callExpression = createCallExpression(LESS_THAN_OR_EQUAL);
assertEquals(format(callExpression), "(c_bigint) <= (BIGINT'5')");
callExpression = createCallExpression(EQUAL);
assertEquals(format(callExpression), "(c_bigint) = (BIGINT'5')");
callExpression = createCallExpression(NOT_EQUAL);
assertEquals(format(callExpression), "(c_bigint) <> (BIGINT'5')");
callExpression = createCallExpression(IS_DISTINCT_FROM);
assertEquals(format(callExpression), "(c_bigint) IS DISTINCT FROM (BIGINT'5')");
// negation
RowExpression expression = createCallExpression(ADD);
callExpression = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression.getType())), expression.getType(), expression);
assertEquals(format(callExpression), "-((c_bigint) + (BIGINT'5'))");
// subscript
ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
Type elementType = arrayType.getElementType();
RowExpression subscriptExpression = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(0L, INTEGER)));
callExpression = subscriptExpression;
assertEquals(format(callExpression), "c_bigint_array[INTEGER'0']");
// cast
callExpression = call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, TINYINT.getTypeSignature(), BIGINT.getTypeSignature()), BIGINT, constant(1L, TINYINT));
assertEquals(format(callExpression), "CAST(TINYINT'1' AS bigint)");
// between
callExpression = call(BETWEEN.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(BETWEEN, fromTypes(BIGINT, BIGINT, BIGINT)), BOOLEAN, subscriptExpression, constant(1L, BIGINT), constant(5L, BIGINT));
assertEquals(format(callExpression), "c_bigint_array[INTEGER'0'] BETWEEN (BIGINT'1') AND (BIGINT'5')");
// other
callExpression = call(HASH_CODE.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(HASH_CODE, fromTypes(BIGINT)), BIGINT, constant(1L, BIGINT));
assertEquals(format(callExpression), "HASH_CODE(BIGINT'1')");
// like
callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, VARCHAR.getTypeSignature(), LIKE_PATTERN.getTypeSignature()), LIKE_PATTERN, constant(utf8Slice("prefix%"), VARCHAR)));
assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'prefix%'");
callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
// like escape
callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call("LIKE_PATTERN", FUNCTION_RESOLUTION.likePatternFunction(), LIKE_PATTERN, constant(utf8Slice("%escaped$_"), VARCHAR), constant(utf8Slice("$"), VARCHAR)));
assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'%escaped$_' ESCAPE VARCHAR'$'");
callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class BigQueryResultPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (type instanceof ArrayType && value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
appendTo(type.getTypeParameters().get(0), element, builder);
}
output.closeEntry();
return;
}
if (type instanceof RowType && value instanceof GenericRecord) {
GenericRecord record = (GenericRecord) 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), record.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
}
throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestTypeConversions method testConvertStringArrayColumn.
@Test
public void testConvertStringArrayColumn() {
BigQueryColumnHandle column = new BigQueryColumnHandle("test", BigQueryType.STRING, REPEATED, ImmutableList.of(), null);
ColumnMetadata metadata = column.getColumnMetadata();
assertThat(metadata.getType()).isEqualTo(new ArrayType(VarcharType.VARCHAR));
}
Aggregations