public static boolean isSortKey(int fieldPos, TypeInformation<?> type) { if (!type.isTupleType() || !(type instanceof CompositeType)) { throw new InvalidProgramException("Specifying keys via field positions is only valid " + "for tuple data types. Type: " + type); } if (type.getArity() == 0) { throw new InvalidProgramException("Tuple size must be greater than 0. Size: " + type.getArity()); } if(fieldPos < 0 || fieldPos >= type.getArity()) { throw new IndexOutOfBoundsException("Tuple position is out of range: " + fieldPos); } TypeInformation<?> sortKeyType = ((CompositeType<?>)type).getTypeAt(fieldPos); return sortKeyType.isSortKeyType(); }
private static TypeInformation<?> getTypeOfPojoField(TypeInformation<?> pojoInfo, Field field) { for (int j = 0; j < pojoInfo.getArity(); j++) { PojoField pf = ((PojoTypeInfo<?>) pojoInfo).getPojoFieldAt(j); if (pf.getField().getName().equals(field.getName())) { return pf.getTypeInformation(); } } return null; }
protected StreamProjection(DataStream<IN> dataStream, int[] fieldIndexes) { if (!dataStream.getType().isTupleType()) { throw new RuntimeException("Only Tuple DataStreams can be projected"); } if (fieldIndexes.length == 0) { throw new IllegalArgumentException("project() needs to select at least one (1) field."); } else if (fieldIndexes.length > Tuple.MAX_ARITY - 1) { throw new IllegalArgumentException( "project() may select only up to (" + (Tuple.MAX_ARITY - 1) + ") fields."); } int maxFieldIndex = (dataStream.getType()).getArity(); for (int i = 0; i < fieldIndexes.length; i++) { Preconditions.checkElementIndex(fieldIndexes[i], maxFieldIndex); } this.dataStream = dataStream; this.fieldIndexes = fieldIndexes; }
@Override protected CassandraSink<Row> createSink() throws Exception { final CassandraRowSink sink = new CassandraRowSink( typeInfo.getArity(), query, builder, configBuilder.build(), failureHandler); return new CassandraSink<>(input.addSink(sink).name("Cassandra Sink")); }
@Test public void testPartitionerLambda() { Partitioner<Tuple2<Integer, String>> partitioner = (key, numPartitions) -> key.f1.length() % numPartitions; final TypeInformation<?> ti = TypeExtractor.getPartitionerTypes(partitioner, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(0), BasicTypeInfo.INT_TYPE_INFO); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testFunctionInputInOutputMultipleTimes() { RichMapFunction<Float, ?> function = new FieldDuplicator<Float>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.FLOAT_TYPE_INFO); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti.getTypeAt(1)); }
@Test public void testMapPartitionLambda() { MapPartitionFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, String>> f = (i, o) -> {}; TypeInformation<?> ti = TypeExtractor.getMapPartitionReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testJoinLambda() { JoinFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, Double>, Tuple2<Tuple1<Integer>, String>> f = (i1, i2) -> null; TypeInformation<?> ti = TypeExtractor.getJoinReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, NESTED_TUPLE_DOUBLE_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testKeySelectorLambda() { KeySelector<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, String>> f = (i) -> null; TypeInformation<?> ti = TypeExtractor.getKeySelectorTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testMapLambda() { MapFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, String>> f = (i) -> null; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testCoGroupLambda() { CoGroupFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, Double>, Tuple2<Tuple1<Integer>, String>> f = (i1, i2, o) -> {}; TypeInformation<?> ti = TypeExtractor.getCoGroupReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, NESTED_TUPLE_DOUBLE_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@Test public void testRow() { Row row = new Row(2); row.setField(0, "string"); row.setField(1, 15); TypeInformation<Row> rowInfo = TypeExtractor.getForObject(row); Assert.assertEquals(rowInfo.getClass(), RowTypeInfo.class); Assert.assertEquals(2, rowInfo.getArity()); Assert.assertEquals( new RowTypeInfo( BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO), rowInfo); Row nullRow = new Row(2); TypeInformation<Row> genericRowInfo = TypeExtractor.getForObject(nullRow); Assert.assertEquals(genericRowInfo, new GenericTypeInfo<>(Row.class)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testDuplicateValueNested() { TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) new DuplicateValueNested<String>(), TypeInformation.of(new TypeHint<Tuple1<Tuple1<String>>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); }
@Test public void testFlatMapLambda() { FlatMapFunction<Tuple2<Tuple1<Integer>, Boolean>, Tuple2<Tuple1<Integer>, String>> f = (i, out) -> out.collect(null); TypeInformation<?> ti = TypeExtractor.getFlatMapReturnTypes(f, NESTED_TUPLE_BOOLEAN_TYPE, null, true); if (!(ti instanceof MissingTypeInfo)) { assertTrue(ti.isTupleType()); assertEquals(2, ti.getArity()); assertTrue(((TupleTypeInfo<?>) ti).getTypeAt(0).isTupleType()); assertEquals(((TupleTypeInfo<?>) ti).getTypeAt(1), BasicTypeInfo.STRING_TYPE_INFO); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testFunctionDependingOnInputWithCustomTupleInput() { IdentityMapper<SameTypeVariable<String>> function = new IdentityMapper<SameTypeVariable<String>>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<String, String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testDuplicateValue() { TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) new DuplicateValue<String>(), TypeInformation.of(new TypeHint<Tuple1<String>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testInputInference2() { EdgeMapper2<Boolean> em = new EdgeMapper2<Boolean>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) em, Types.BOOLEAN); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti.getTypeAt(2)); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testInputInference1() { EdgeMapper<String, Double> em = new EdgeMapper<String, Double>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes((MapFunction) em, TypeInformation.of(new TypeHint<Tuple3<String, String, Double>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti.getTypeAt(2)); }
@Test public void testFunctionDependingPartialOnInput2() { RichMapFunction<DoubleValue, ?> function = new OneAppender<DoubleValue>(); TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, new ValueTypeInfo<DoubleValue>(DoubleValue.class)); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertTrue(tti.getTypeAt(0) instanceof ValueTypeInfo<?>); ValueTypeInfo<?> vti = (ValueTypeInfo<?>) tti.getTypeAt(0); Assert.assertEquals(DoubleValue.class, vti.getTypeClass()); Assert.assertTrue(tti.getTypeAt(1).isBasicType()); Assert.assertEquals(Integer.class , tti.getTypeAt(1).getTypeClass()); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testFunctionDependingPartialOnInput() { RichMapFunction<?, ?> function = new OneAppender<DoubleValue>() { private static final long serialVersionUID = 1L; }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<DoubleValue>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertTrue(tti.getTypeAt(0) instanceof ValueTypeInfo<?>); ValueTypeInfo<?> vti = (ValueTypeInfo<?>) tti.getTypeAt(0); Assert.assertEquals(DoubleValue.class, vti.getTypeClass()); Assert.assertTrue(tti.getTypeAt(1).isBasicType()); Assert.assertEquals(Integer.class , tti.getTypeAt(1).getTypeClass()); }