use of com.facebook.presto.type.ArrayType in project presto by prestodb.
the class TestArrayTransformFunction method testTypeCombinations.
@Test
public void testTypeCombinations() throws Exception {
assertFunction("transform(ARRAY [25, 26], x -> x + 1)", new ArrayType(INTEGER), ImmutableList.of(26, 27));
assertFunction("transform(ARRAY [25, 26], x -> x + 1.0)", new ArrayType(DOUBLE), ImmutableList.of(26.0, 27.0));
assertFunction("transform(ARRAY [25, 26], x -> x = 25)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [25, 26], x -> to_base(x, 16))", new ArrayType(createVarcharType(64)), ImmutableList.of("19", "1a"));
assertFunction("transform(ARRAY [25, 26], x -> ARRAY[x + 1])", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(26), ImmutableList.of(27)));
assertFunction("transform(ARRAY [25.6, 27.3], x -> CAST(x AS BIGINT))", new ArrayType(BIGINT), ImmutableList.of(26L, 27L));
assertFunction("transform(ARRAY [25.6, 27.3], x -> x + 1.0)", new ArrayType(DOUBLE), ImmutableList.of(26.6, 28.3));
assertFunction("transform(ARRAY [25.6, 27.3], x -> x = 25.6)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [25.6, 27.3], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("25.6", "27.3"));
assertFunction("transform(ARRAY [25.6, 27.3], x -> MAP(ARRAY[x + 1], ARRAY[true]))", new ArrayType(new MapType(DOUBLE, BOOLEAN)), ImmutableList.of(ImmutableMap.of(26.6, true), ImmutableMap.of(28.3, true)));
assertFunction("transform(ARRAY [true, false], x -> if(x, 25, 26))", new ArrayType(INTEGER), ImmutableList.of(25, 26));
assertFunction("transform(ARRAY [false, true], x -> if(x, 25.6, 28.9))", new ArrayType(DOUBLE), ImmutableList.of(28.9, 25.6));
assertFunction("transform(ARRAY [true, false], x -> not x)", new ArrayType(BOOLEAN), ImmutableList.of(false, true));
assertFunction("transform(ARRAY [false, true], x -> CAST(x AS VARCHAR))", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("false", "true"));
assertFunction("transform(ARRAY [true, false], x -> ARRAY[x])", new ArrayType(new ArrayType(BOOLEAN)), ImmutableList.of(ImmutableList.of(true), ImmutableList.of(false)));
assertFunction("transform(ARRAY ['41', '42'], x -> from_base(x, 16))", new ArrayType(BIGINT), ImmutableList.of(65L, 66L));
assertFunction("transform(ARRAY ['25.6', '27.3'], x -> CAST(x AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(25.6, 27.3));
assertFunction("transform(ARRAY ['abc', 'def'], x -> 'abc' = x)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY ['abc', 'def'], x -> x || x)", new ArrayType(createUnboundedVarcharType()), ImmutableList.of("abcabc", "defdef"));
assertFunction("transform(ARRAY ['123', '456'], x -> ROW(x, CAST(x AS INTEGER), x > '3'))", new ArrayType(new RowType(ImmutableList.of(createVarcharType(3), INTEGER, BOOLEAN), Optional.empty())), ImmutableList.of(ImmutableList.of("123", 123, false), ImmutableList.of("456", 456, true)));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> from_base(x[3], 10))", new ArrayType(BIGINT), ImmutableList.of(123L, 456L));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> CAST(x[3] AS DOUBLE))", new ArrayType(DOUBLE), ImmutableList.of(123.0, 456.0));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2] IS NULL)", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("transform(ARRAY [ARRAY ['abc', null, '123'], ARRAY ['def', 'x', '456']], x -> x[2])", new ArrayType(createVarcharType(3)), asList(null, "x"));
assertFunction("transform(ARRAY [MAP(ARRAY['abc', 'def'], ARRAY[123, 456]), MAP(ARRAY['ghi', 'jkl'], ARRAY[234, 567])], x -> map_keys(x))", new ArrayType(new ArrayType(createVarcharType(3))), ImmutableList.of(ImmutableList.of("abc", "def"), ImmutableList.of("ghi", "jkl")));
}
use of com.facebook.presto.type.ArrayType in project presto by prestodb.
the class TestZipWithFunction method testWithNull.
@Test
public void testWithNull() throws Exception {
assertFunction("zip_with(CAST(NULL AS ARRAY(UNKNOWN)), ARRAY[], (x, y) -> (y, x))", new ArrayType(new RowType(ImmutableList.of(UNKNOWN, UNKNOWN), Optional.empty())), null);
assertFunction("zip_with(ARRAY[NULL], ARRAY[NULL], (x, y) -> (y, x))", new ArrayType(new RowType(ImmutableList.of(UNKNOWN, UNKNOWN), Optional.empty())), ImmutableList.of(asList(null, null)));
assertFunction("zip_with(ARRAY[NULL], ARRAY[NULL], (x, y) -> x IS NULL AND y IS NULL)", new ArrayType(BOOLEAN), ImmutableList.of(true));
assertFunction("zip_with(ARRAY['a', NULL], ARRAY[NULL, 1], (x, y) -> x IS NULL OR y IS NULL)", new ArrayType(BOOLEAN), ImmutableList.of(true, true));
assertFunction("zip_with(ARRAY[1, NULL], ARRAY[3, 4], (x, y) -> x + y)", new ArrayType(INTEGER), asList(4, null));
assertFunction("zip_with(ARRAY['a', 'b'], ARRAY[1, 3], (x, y) -> NULL)", new ArrayType(UNKNOWN), asList(null, null));
}
use of com.facebook.presto.type.ArrayType in project presto by prestodb.
the class TestPageProcessorCompiler method testNoCaching.
@Test
public void testNoCaching() throws Throwable {
MetadataManager metadata = METADATA_MANAGER;
ExpressionCompiler compiler = new ExpressionCompiler(metadata);
ImmutableList.Builder<RowExpression> projectionsBuilder = ImmutableList.builder();
ArrayType arrayType = new ArrayType(VARCHAR);
Signature signature = new Signature("concat", SCALAR, arrayType.getTypeSignature(), arrayType.getTypeSignature(), arrayType.getTypeSignature());
projectionsBuilder.add(new CallExpression(signature, arrayType, ImmutableList.of(new InputReferenceExpression(0, arrayType), new InputReferenceExpression(1, arrayType))));
ImmutableList<RowExpression> projections = projectionsBuilder.build();
PageProcessor pageProcessor = compiler.compilePageProcessor(new ConstantExpression(true, BOOLEAN), projections).get();
PageProcessor pageProcessor2 = compiler.compilePageProcessor(new ConstantExpression(true, BOOLEAN), projections).get();
assertTrue(pageProcessor != pageProcessor2);
}
use of com.facebook.presto.type.ArrayType in project presto by prestodb.
the class TestRegexpFunctions method testRegexpExtractAll.
@Test
public void testRegexpExtractAll() {
assertFunction("REGEXP_EXTRACT_ALL('rat cat\nbat dog', '.at')", new ArrayType(createVarcharType(15)), ImmutableList.of("rat", "cat", "bat"));
assertFunction("REGEXP_EXTRACT_ALL('rat cat\nbat dog', '(.)at', 1)", new ArrayType(createVarcharType(15)), ImmutableList.of("r", "c", "b"));
List<String> nullList = new ArrayList<>();
nullList.add(null);
assertFunction("REGEXP_EXTRACT_ALL('rat cat\nbat dog', 'ra(.)|blah(.)(.)', 2)", new ArrayType(createVarcharType(15)), nullList);
assertInvalidFunction("REGEXP_EXTRACT_ALL('hello', '(.)', 2)", "Pattern has 1 groups. Cannot access group 2");
assertFunction("REGEXP_EXTRACT_ALL('12345', '')", new ArrayType(createVarcharType(5)), ImmutableList.of("", "", "", "", "", ""));
assertInvalidFunction("REGEXP_EXTRACT_ALL('12345', '(')", INVALID_FUNCTION_ARGUMENT);
}
use of com.facebook.presto.type.ArrayType in project presto by prestodb.
the class MultiKeyValuePairs method toMultimapNativeEncoding.
/**
* Serialize as a multimap: map(key, array(value)), each key can be associated with multiple values
*/
public Block toMultimapNativeEncoding() {
Block keys = keyBlockBuilder.build();
Block values = valueBlockBuilder.build();
// Merge values of the same key into an array
BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(new BlockBuilderStatus(), keys.getPositionCount(), expectedValueSize(keyType, EXPECTED_ENTRY_SIZE));
ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
valueArrayBlockBuilders.ensureCapacity(keys.getPositionCount());
TypedSet keySet = new TypedSet(keyType, keys.getPositionCount());
for (int keyValueIndex = 0; keyValueIndex < keys.getPositionCount(); keyValueIndex++) {
if (!keySet.contains(keys, keyValueIndex)) {
keySet.add(keys, keyValueIndex);
keyType.appendTo(keys, keyValueIndex, distinctKeyBlockBuilder);
BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(new BlockBuilderStatus(), 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
valueArrayBlockBuilders.set(keySet.positionOf(keys, keyValueIndex), valueArrayBuilder);
}
valueType.appendTo(values, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(keys, keyValueIndex)));
}
// Write keys and value arrays into one Block
Block distinctKeys = distinctKeyBlockBuilder.build();
Type valueArrayType = new ArrayType(valueType);
BlockBuilder multimapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueArrayType), new BlockBuilderStatus(), distinctKeyBlockBuilder.getPositionCount());
for (int i = 0; i < distinctKeys.getPositionCount(); i++) {
keyType.appendTo(distinctKeys, i, multimapBlockBuilder);
valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
}
return multimapBlockBuilder.build();
}
Aggregations