use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestBingTileFunctions method testBingTilesAround.
@Test
public void testBingTilesAround() {
assertFunction("transform(bing_tiles_around(30.12, 60, 1), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("0", "2", "1", "3"));
assertFunction("transform(bing_tiles_around(30.12, 60, 15), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("123030123010102", "123030123010120", "123030123010122", "123030123010103", "123030123010121", "123030123010123", "123030123010112", "123030123010130", "123030123010132"));
assertFunction("transform(bing_tiles_around(30.12, 60, 23), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("12303012301012121210122", "12303012301012121210300", "12303012301012121210302", "12303012301012121210123", "12303012301012121210301", "12303012301012121210303", "12303012301012121210132", "12303012301012121210310", "12303012301012121210312"));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestBingTileFunctions method assertBingTileChildren.
private void assertBingTileChildren(String quadkey, OptionalInt newZoom, List<String> childQuadkeys) {
String children;
if (newZoom.isPresent()) {
children = format("bing_tile_children(bing_tile('%s'), %s)", quadkey, newZoom.getAsInt());
} else {
children = format("bing_tile_children(bing_tile('%s'))", quadkey);
}
assertFunction(format("array_sort(transform(%s, x -> bing_tile_quadkey(x)))", children), new ArrayType(VARCHAR), ImmutableList.sortedCopyOf(childQuadkeys));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestBingTileFunctions method testBingTilesAroundCorner.
@Test
public void testBingTilesAroundCorner() {
// Different zoom Level
assertFunction("transform(bing_tiles_around(-85.05112878, -180, 1), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("0", "2", "1", "3"));
assertFunction("transform(bing_tiles_around(-85.05112878, -180, 3), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("220", "222", "221", "223"));
assertFunction("transform(bing_tiles_around(-85.05112878, -180, 15), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("222222222222220", "222222222222222", "222222222222221", "222222222222223"));
// Different Corners
// Starting Corner 0,3
assertFunction("transform(bing_tiles_around(-85.05112878, -180, 2), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("20", "22", "21", "23"));
assertFunction("transform(bing_tiles_around(-85.05112878, 180, 2), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("30", "32", "31", "33"));
assertFunction("transform(bing_tiles_around(85.05112878, -180, 2), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("00", "02", "01", "03"));
assertFunction("transform(bing_tiles_around(85.05112878, 180, 2), x -> bing_tile_quadkey(x))", new ArrayType(VARCHAR), ImmutableList.of("10", "12", "11", "13"));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class RelationPlanner method planCrossJoinUnnest.
private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
// Create variables for the result of unnesting
ImmutableList.Builder<VariableReferenceExpression> unnestedVariablesBuilder = ImmutableList.builder();
for (Field field : unnestOutputDescriptor.getVisibleFields()) {
VariableReferenceExpression variable = variableAllocator.newVariable(field);
unnestedVariablesBuilder.add(variable);
}
ImmutableList<VariableReferenceExpression> unnestedVariables = unnestedVariablesBuilder.build();
// Add a projection for all the unnest arguments
PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
planBuilder = planBuilder.appendProjections(node.getExpressions(), variableAllocator, idAllocator);
TranslationMap translations = planBuilder.getTranslations();
ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
UnmodifiableIterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
for (Expression expression : node.getExpressions()) {
Type type = analysis.getType(expression);
VariableReferenceExpression inputVariable = new VariableReferenceExpression(getSourceLocation(expression), translations.get(expression).getName(), type);
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
unnestVariableBuilder.add(unnestedVariablesIterator.next());
}
unnestVariables.put(inputVariable, unnestVariableBuilder.build());
} else {
unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next()));
}
} else if (type instanceof MapType) {
unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
} else {
throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
}
}
Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestVariables.build(), ordinalityVariable);
return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputVariables());
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestArrayOperators method testArrayToArrayConcat.
@Test
public void testArrayToArrayConcat() {
assertFunction("ARRAY [1, NULL] || ARRAY [3]", new ArrayType(INTEGER), Lists.newArrayList(1, null, 3));
assertFunction("ARRAY [1, 2] || ARRAY[3, 4]", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3, 4));
assertFunction("ARRAY [1, 2] || ARRAY[3, BIGINT '4']", new ArrayType(BIGINT), ImmutableList.of(1L, 2L, 3L, 4L));
assertFunction("ARRAY [1, 2] || ARRAY[3, 40000000000]", new ArrayType(BIGINT), ImmutableList.of(1L, 2L, 3L, 40000000000L));
assertFunction("ARRAY [NULL] || ARRAY[NULL]", new ArrayType(UNKNOWN), Lists.newArrayList(null, null));
assertFunction("ARRAY ['puppies'] || ARRAY ['kittens']", new ArrayType(createVarcharType(7)), ImmutableList.of("puppies", "kittens"));
assertFunction("ARRAY [TRUE] || ARRAY [FALSE]", new ArrayType(BOOLEAN), ImmutableList.of(true, false));
assertFunction("concat(ARRAY [1] , ARRAY[2,3])", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3));
assertFunction("ARRAY [TIMESTAMP '1970-01-01 00:00:01'] || ARRAY[TIMESTAMP '1973-07-08 22:00:01']", new ArrayType(TIMESTAMP), ImmutableList.of(sqlTimestampOf(1970, 1, 1, 0, 0, 1, 0, TEST_SESSION), sqlTimestampOf(1973, 7, 8, 22, 0, 1, 0, TEST_SESSION)));
assertFunction("ARRAY [ARRAY[ARRAY[1]]] || ARRAY [ARRAY[ARRAY[2]]]", new ArrayType(new ArrayType(new ArrayType(INTEGER))), asList(singletonList(Ints.asList(1)), singletonList(Ints.asList(2))));
assertFunction("ARRAY [] || ARRAY []", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("ARRAY [TRUE] || ARRAY [FALSE] || ARRAY [TRUE]", new ArrayType(BOOLEAN), ImmutableList.of(true, false, true));
assertFunction("ARRAY [1] || ARRAY [2] || ARRAY [3] || ARRAY [4]", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3, 4));
assertFunction("ARRAY [1] || ARRAY [2.0E0] || ARRAY [3] || ARRAY [4.0E0]", new ArrayType(DOUBLE), ImmutableList.of(1.0, 2.0, 3.0, 4.0));
assertFunction("ARRAY [ARRAY [1], ARRAY [2, 8]] || ARRAY [ARRAY [3, 6], ARRAY [4]]", new ArrayType(new ArrayType(INTEGER)), ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2, 8), ImmutableList.of(3, 6), ImmutableList.of(4)));
assertFunction("ARRAY[1.0] || ARRAY [2.0, 3.11]", new ArrayType(createDecimalType(3, 2)), ImmutableList.of(decimal("1.00"), decimal("2.00"), decimal("3.11")));
assertFunction("ARRAY[1.0] || ARRAY [2.0] || ARRAY [123456789123456.789]", new ArrayType(createDecimalType(18, 3)), ImmutableList.of(decimal("000000000000001.000"), decimal("000000000000002.000"), decimal("123456789123456.789")));
// Tests for concatenating multiple arrays
List<Object> nullList = Collections.nCopies(2, null);
assertFunction("concat(ARRAY[], ARRAY[NULL], ARRAY[], ARRAY[NULL], ARRAY[])", new ArrayType(UNKNOWN), nullList);
assertFunction("concat(ARRAY[], ARRAY[], ARRAY[], NULL, ARRAY[])", new ArrayType(UNKNOWN), null);
assertFunction("concat(ARRAY[], ARRAY[], ARRAY[], ARRAY[], ARRAY[])", new ArrayType(UNKNOWN), ImmutableList.of());
assertFunction("concat(ARRAY[], ARRAY[], ARRAY[333], ARRAY[], ARRAY[])", new ArrayType(INTEGER), ImmutableList.of(333));
assertFunction("concat(ARRAY[1], ARRAY[2,3], ARRAY[])", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3));
assertFunction("concat(ARRAY[1], ARRAY[2,3,3], ARRAY[2,1])", new ArrayType(INTEGER), ImmutableList.of(1, 2, 3, 3, 2, 1));
assertFunction("concat(ARRAY[1], ARRAY[], ARRAY[1,2])", new ArrayType(INTEGER), ImmutableList.of(1, 1, 2));
assertFunction("concat(ARRAY[], ARRAY[1], ARRAY[], ARRAY[3], ARRAY[], ARRAY[5], ARRAY[])", new ArrayType(INTEGER), ImmutableList.of(1, 3, 5));
assertFunction("concat(ARRAY[], ARRAY['123456'], CAST(ARRAY[1,2] AS ARRAY(varchar)), ARRAY[])", new ArrayType(VARCHAR), ImmutableList.of("123456", "1", "2"));
assertInvalidFunction("ARRAY [ARRAY[1]] || ARRAY[ARRAY[true], ARRAY[false]]", FUNCTION_NOT_FOUND);
// This query is ambiguous. The result can be [[1], NULL] or [[1], [NULL]] depending on interpretation
assertInvalidFunction("ARRAY [ARRAY [1]] || ARRAY [NULL]", AMBIGUOUS_FUNCTION_CALL);
try {
assertFunction("ARRAY [ARRAY [1]] || ARRAY [ARRAY ['x']]", new ArrayType(new ArrayType(INTEGER)), null);
fail("arrays must be of the same type");
} catch (RuntimeException e) {
// Expected
}
assertCachedInstanceHasBoundedRetainedSize("ARRAY [1, NULL] || ARRAY [3]");
}
Aggregations