org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor Java Examples
The following examples show how to use
org.apache.flink.api.common.typeutils.CompositeType.FlatFieldDescriptor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RowTypeInfoTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testGetFlatFields() { RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"}); List<FlatFieldDescriptor> result = new ArrayList<>(); typeInfo1.getFlatFields("row.*", 0, result); assertEquals(2, result.size()); assertEquals( new FlatFieldDescriptor(1, BasicTypeInfo.SHORT_TYPE_INFO).toString(), result.get(0).toString()); assertEquals( new FlatFieldDescriptor(2, BasicTypeInfo.BIG_DEC_TYPE_INFO).toString(), result.get(1).toString()); result.clear(); typeInfo1.getFlatFields("string", 0, result); assertEquals(1, result.size()); assertEquals( new FlatFieldDescriptor(3, BasicTypeInfo.STRING_TYPE_INFO).toString(), result.get(0).toString()); }
Example #2
Source File: Keys.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
public SelectorFunctionKeys(KeySelector<T, K> keyExtractor, TypeInformation<T> inputType, TypeInformation<K> keyType) { if (keyExtractor == null) { throw new NullPointerException("Key extractor must not be null."); } if (keyType == null) { throw new NullPointerException("Key type must not be null."); } if (!keyType.isKeyType()) { throw new InvalidProgramException("Return type "+keyType+" of KeySelector "+keyExtractor.getClass()+" is not a valid key type"); } this.keyExtractor = keyExtractor; this.inputType = inputType; this.keyType = keyType; this.originalKeyTypes = new TypeInformation[] {keyType}; if (keyType instanceof CompositeType) { this.keyFields = ((CompositeType<T>)keyType).getFlatFields(ExpressionKeys.SELECT_ALL_CHAR); } else { this.keyFields = new ArrayList<>(1); this.keyFields.add(new FlatFieldDescriptor(0, keyType)); } }
Example #3
Source File: Keys.java From flink with Apache License 2.0 | 6 votes |
public SelectorFunctionKeys(KeySelector<T, K> keyExtractor, TypeInformation<T> inputType, TypeInformation<K> keyType) { if (keyExtractor == null) { throw new NullPointerException("Key extractor must not be null."); } if (keyType == null) { throw new NullPointerException("Key type must not be null."); } if (!keyType.isKeyType()) { throw new InvalidProgramException("Return type "+keyType+" of KeySelector "+keyExtractor.getClass()+" is not a valid key type"); } this.keyExtractor = keyExtractor; this.inputType = inputType; this.keyType = keyType; this.originalKeyTypes = new TypeInformation[] {keyType}; if (keyType instanceof CompositeType) { this.keyFields = ((CompositeType<T>)keyType).getFlatFields(ExpressionKeys.SELECT_ALL_CHAR); } else { this.keyFields = new ArrayList<>(1); this.keyFields.add(new FlatFieldDescriptor(0, keyType)); } }
Example #4
Source File: RowTypeInfoTest.java From flink with Apache License 2.0 | 6 votes |
@Test public void testGetFlatFields() { RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"}); List<FlatFieldDescriptor> result = new ArrayList<>(); typeInfo1.getFlatFields("row.*", 0, result); assertEquals(2, result.size()); assertEquals( new FlatFieldDescriptor(1, BasicTypeInfo.SHORT_TYPE_INFO).toString(), result.get(0).toString()); assertEquals( new FlatFieldDescriptor(2, BasicTypeInfo.BIG_DEC_TYPE_INFO).toString(), result.get(1).toString()); result.clear(); typeInfo1.getFlatFields("string", 0, result); assertEquals(1, result.size()); assertEquals( new FlatFieldDescriptor(3, BasicTypeInfo.STRING_TYPE_INFO).toString(), result.get(0).toString()); }
Example #5
Source File: Keys.java From flink with Apache License 2.0 | 6 votes |
public SelectorFunctionKeys(KeySelector<T, K> keyExtractor, TypeInformation<T> inputType, TypeInformation<K> keyType) { if (keyExtractor == null) { throw new NullPointerException("Key extractor must not be null."); } if (keyType == null) { throw new NullPointerException("Key type must not be null."); } if (!keyType.isKeyType()) { throw new InvalidProgramException("Return type "+keyType+" of KeySelector "+keyExtractor.getClass()+" is not a valid key type"); } this.keyExtractor = keyExtractor; this.inputType = inputType; this.keyType = keyType; this.originalKeyTypes = new TypeInformation[] {keyType}; if (keyType instanceof CompositeType) { this.keyFields = ((CompositeType<T>)keyType).getFlatFields(ExpressionKeys.SELECT_ALL_CHAR); } else { this.keyFields = new ArrayList<>(1); this.keyFields.add(new FlatFieldDescriptor(0, keyType)); } }
Example #6
Source File: RowTypeInfoTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Test public void testGetFlatFields() { RowTypeInfo typeInfo1 = new RowTypeInfo(typeList, new String[]{"int", "row", "string"}); List<FlatFieldDescriptor> result = new ArrayList<>(); typeInfo1.getFlatFields("row.*", 0, result); assertEquals(2, result.size()); assertEquals( new FlatFieldDescriptor(1, BasicTypeInfo.SHORT_TYPE_INFO).toString(), result.get(0).toString()); assertEquals( new FlatFieldDescriptor(2, BasicTypeInfo.BIG_DEC_TYPE_INFO).toString(), result.get(1).toString()); result.clear(); typeInfo1.getFlatFields("string", 0, result); assertEquals(1, result.size()); assertEquals( new FlatFieldDescriptor(3, BasicTypeInfo.STRING_TYPE_INFO).toString(), result.get(0).toString()); }
Example #7
Source File: SemanticPropUtil.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
private static List<FlatFieldDescriptor> getFlatFields(String fieldStr, TypeInformation<?> typeInfo) { if (typeInfo instanceof CompositeType<?>) { return ((CompositeType<?>) typeInfo).getFlatFields(fieldStr); } else { Matcher wildcardMatcher = PATTERN_WILDCARD.matcher(fieldStr); if (wildcardMatcher.matches()) { return Collections.singletonList(new FlatFieldDescriptor(0, typeInfo)); } else { throw new InvalidFieldReferenceException("Nested field expression \"" + fieldStr + "\" not possible on atomic type (" + typeInfo + ")."); } } }
Example #8
Source File: SemanticPropUtil.java From flink with Apache License 2.0 | 5 votes |
private static List<FlatFieldDescriptor> getFlatFields(String fieldStr, TypeInformation<?> typeInfo) { if (typeInfo instanceof CompositeType<?>) { return ((CompositeType<?>) typeInfo).getFlatFields(fieldStr); } else { Matcher wildcardMatcher = PATTERN_WILDCARD.matcher(fieldStr); if (wildcardMatcher.matches()) { return Collections.singletonList(new FlatFieldDescriptor(0, typeInfo)); } else { throw new InvalidFieldReferenceException("Nested field expression \"" + fieldStr + "\" not possible on atomic type (" + typeInfo + ")."); } } }
Example #9
Source File: SemanticPropUtil.java From flink with Apache License 2.0 | 5 votes |
private static List<FlatFieldDescriptor> getFlatFields(String fieldStr, TypeInformation<?> typeInfo) { if (typeInfo instanceof CompositeType<?>) { return ((CompositeType<?>) typeInfo).getFlatFields(fieldStr); } else { Matcher wildcardMatcher = PATTERN_WILDCARD.matcher(fieldStr); if (wildcardMatcher.matches()) { return Collections.singletonList(new FlatFieldDescriptor(0, typeInfo)); } else { throw new InvalidFieldReferenceException("Nested field expression \"" + fieldStr + "\" not possible on atomic type (" + typeInfo + ")."); } } }
Example #10
Source File: SemanticPropUtil.java From flink with Apache License 2.0 | 4 votes |
private static void parseNonForwardedFields(SemanticProperties sp, String[] nonForwardedStr, TypeInformation<?> inType, TypeInformation<?> outType, int input, boolean skipIncompatibleTypes) { if (nonForwardedStr == null) { return; } FieldSet excludedFields = new FieldSet(); for (String s : nonForwardedStr) { // remove white characters s = s.replaceAll("\\s", ""); if (s.equals("")) { continue; } if (!inType.equals(outType)) { if (skipIncompatibleTypes) { continue; } else { throw new InvalidSemanticAnnotationException("Non-forwarded fields annotation only allowed for identical input and output types."); } } Matcher matcher = PATTERN_LIST.matcher(s); if (!matcher.matches()) { throw new InvalidSemanticAnnotationException("Invalid format of non-forwarded fields annotation \"" + s + "\"."); } // process individual fields matcher = PATTERN_FIELD.matcher(s); while (matcher.find()) { String fieldStr = matcher.group(); try { // get and add all flat field positions List<FlatFieldDescriptor> inFFDs = getFlatFields(fieldStr, inType); for (FlatFieldDescriptor ffd : inFFDs) { excludedFields = excludedFields.addField(ffd.getPosition()); } } catch (InvalidFieldReferenceException ifre) { throw new InvalidSemanticAnnotationException("Invalid field reference in non-forwarded fields annotation \"" + fieldStr + "\".", ifre); } } } for (int i = 0; i < inType.getTotalFields(); i++) { if (!excludedFields.contains(i)) { if (sp instanceof SingleInputSemanticProperties) { ((SingleInputSemanticProperties) sp).addForwardedField(i, i); } else if (sp instanceof DualInputSemanticProperties) { ((DualInputSemanticProperties) sp).addForwardedField(input, i, i); } } } }
Example #11
Source File: SemanticPropUtil.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
private static void parseNonForwardedFields(SemanticProperties sp, String[] nonForwardedStr, TypeInformation<?> inType, TypeInformation<?> outType, int input, boolean skipIncompatibleTypes) { if (nonForwardedStr == null) { return; } FieldSet excludedFields = new FieldSet(); for (String s : nonForwardedStr) { // remove white characters s = s.replaceAll("\\s", ""); if (s.equals("")) { continue; } if (!inType.equals(outType)) { if (skipIncompatibleTypes) { continue; } else { throw new InvalidSemanticAnnotationException("Non-forwarded fields annotation only allowed for identical input and output types."); } } Matcher matcher = PATTERN_LIST.matcher(s); if (!matcher.matches()) { throw new InvalidSemanticAnnotationException("Invalid format of non-forwarded fields annotation \"" + s + "\"."); } // process individual fields matcher = PATTERN_FIELD.matcher(s); while (matcher.find()) { String fieldStr = matcher.group(); try { // get and add all flat field positions List<FlatFieldDescriptor> inFFDs = getFlatFields(fieldStr, inType); for (FlatFieldDescriptor ffd : inFFDs) { excludedFields = excludedFields.addField(ffd.getPosition()); } } catch (InvalidFieldReferenceException ifre) { throw new InvalidSemanticAnnotationException("Invalid field reference in non-forwarded fields annotation \"" + fieldStr + "\".", ifre); } } } for (int i = 0; i < inType.getTotalFields(); i++) { if (!excludedFields.contains(i)) { if (sp instanceof SingleInputSemanticProperties) { ((SingleInputSemanticProperties) sp).addForwardedField(i, i); } else if (sp instanceof DualInputSemanticProperties) { ((DualInputSemanticProperties) sp).addForwardedField(input, i, i); } } } }
Example #12
Source File: PojoSerializerTest.java From flink with Apache License 2.0 | 4 votes |
/** * This tests if the hashes returned by the pojo and tuple comparators are the same */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testTuplePojoTestEquality() { // test with a simple, string-key first. PojoTypeInfo<TestUserClass> pType = (PojoTypeInfo<TestUserClass>) type; List<FlatFieldDescriptor> result = new ArrayList<FlatFieldDescriptor>(); pType.getFlatFields("nestedClass.dumm2", 0, result); int[] fields = new int[1]; // see below fields[0] = result.get(0).getPosition(); TypeComparator<TestUserClass> pojoComp = pType.createComparator( fields, new boolean[]{true}, 0, new ExecutionConfig()); TestUserClass pojoTestRecord = new TestUserClass(0, "abc", 3d, new int[] {1,2,3}, new Date(), new NestedTestUserClass(1, "haha", 4d, new int[] {5,4,3})); int pHash = pojoComp.hash(pojoTestRecord); Tuple1<String> tupleTest = new Tuple1<String>("haha"); TupleTypeInfo<Tuple1<String>> tType = (TupleTypeInfo<Tuple1<String>>)TypeExtractor.getForObject(tupleTest); TypeComparator<Tuple1<String>> tupleComp = tType.createComparator(new int[] {0}, new boolean[] {true}, 0, new ExecutionConfig()); int tHash = tupleComp.hash(tupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable", pHash == tHash); Tuple3<Integer, String, Double> multiTupleTest = new Tuple3<Integer, String, Double>(1, "haha", 4d); // its important here to use the same values. TupleTypeInfo<Tuple3<Integer, String, Double>> multiTupleType = (TupleTypeInfo<Tuple3<Integer, String, Double>>)TypeExtractor.getForObject(multiTupleTest); ExpressionKeys fieldKey = new ExpressionKeys(new int[]{1,0,2}, multiTupleType); ExpressionKeys expressKey = new ExpressionKeys(new String[] {"nestedClass.dumm2", "nestedClass.dumm1", "nestedClass.dumm3"}, pType); try { Assert.assertTrue("Expecting the keys to be compatible", fieldKey.areCompatible(expressKey)); } catch (IncompatibleKeysException e) { e.printStackTrace(); Assert.fail("Keys must be compatible: "+e.getMessage()); } TypeComparator<TestUserClass> multiPojoComp = pType.createComparator( expressKey.computeLogicalKeyPositions(), new boolean[]{true, true, true}, 0, new ExecutionConfig()); int multiPojoHash = multiPojoComp.hash(pojoTestRecord); // pojo order is: dumm2 (str), dumm1 (int), dumm3 (double). TypeComparator<Tuple3<Integer, String, Double>> multiTupleComp = multiTupleType.createComparator(fieldKey.computeLogicalKeyPositions(), new boolean[] {true, true,true}, 0, new ExecutionConfig()); int multiTupleHash = multiTupleComp.hash(multiTupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable. Also for those with multiple key fields", multiPojoHash == multiTupleHash); }
Example #13
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithPojo() { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple2<Long, CustomType>, Tuple2<Long, CustomType>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<Long, CustomType> map(Tuple2<Long, CustomType> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<Long, CustomType>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple2.class, tti.getTypeClass()); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); tti.getFlatFields("f0", 0, ffd); Assert.assertEquals(1, ffd.size()); Assert.assertEquals(0, ffd.get(0).getPosition() ); // Long Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Long.class) ); ffd.clear(); tti.getFlatFields("f1.myField1", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); tti.getFlatFields("f1.myField2", 0, ffd); Assert.assertEquals(2, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Integer.class) ); Assert.assertEquals(Long.class, tti.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti.getTypeAt(1).getTypeClass()); // use getForObject() Tuple2<?, ?> t = new Tuple2<Long, CustomType>(1L, new CustomType("Hello", 1)); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(2, ti2.getArity()); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) ti2; Assert.assertEquals(Tuple2.class, tti2.getTypeClass()); Assert.assertEquals(Long.class, tti2.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti2.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti2.getTypeAt(1).getTypeClass()); }
Example #14
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithTuples() { // use getFlatMapReturnTypes() RichFlatMapFunction<?, ?> function = new RichFlatMapFunction<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>, Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>() { private static final long serialVersionUID = 1L; @Override public void flatMap(Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> value, Collector<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>> out) throws Exception { // nothing to do } }; TypeInformation<?> ti = TypeExtractor.getFlatMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertEquals(0, ffd.get(0).getPosition() ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertTrue( ffd.get(0).getType() instanceof BasicTypeInfo ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f1.f0", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); ffd.clear(); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple3.class, tti.getTypeClass()); Assert.assertTrue(tti.getTypeAt(0).isTupleType()); Assert.assertTrue(tti.getTypeAt(1).isTupleType()); Assert.assertTrue(tti.getTypeAt(2).isTupleType()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(0).getTypeClass()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(1).getTypeClass()); Assert.assertEquals(Tuple2.class, tti.getTypeAt(2).getTypeClass()); Assert.assertEquals(1, tti.getTypeAt(0).getArity()); Assert.assertEquals(1, tti.getTypeAt(1).getArity()); Assert.assertEquals(2, tti.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(1)); // use getForObject() Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> t = new Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>( new Tuple1<String>("hello"), new Tuple1<Integer>(1), new Tuple2<Long, Long>(2L, 3L)); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(1, tti2.getTypeAt(0).getArity()); Assert.assertEquals(1, tti2.getTypeAt(1).getArity()); Assert.assertEquals(2, tti2.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(1)); }
Example #15
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithBasicTypes() throws Exception { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>, Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>() { private static final long serialVersionUID = 1L; @Override public Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> map( Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(9, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f3", 0, ffd); Assert.assertTrue(ffd.size() == 1); Assert.assertEquals(3, ffd.get(0).getPosition() ); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple9.class, tti.getTypeClass()); for (int i = 0; i < 9; i++) { Assert.assertTrue(tti.getTypeAt(i) instanceof BasicTypeInfo); } Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti.getTypeAt(8)); // use getForObject() Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> t = new Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>( 1, 1L, 1.0, 1.0F, false, "Hello World", 'w', (short) 1, (byte) 1); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti2.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti2.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti2.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti2.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti2.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti2.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti2.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti2.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti2.getTypeAt(8)); // test that getForClass does not work try { TypeExtractor.getForClass(Tuple9.class); Assert.fail("Exception expected here"); } catch (InvalidTypesException e) { // that is correct } }
Example #16
Source File: Keys.java From flink with Apache License 2.0 | 4 votes |
/** * Create int-based (non-nested) field position keys on a tuple type. */ public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) { 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 (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) { throw new IllegalArgumentException("The grouping fields must not be empty."); } this.keyFields = new ArrayList<>(); if (keyPositions == null || keyPositions.length == 0) { // use all tuple fields as key fields keyPositions = createIncrIntArray(type.getArity()); } else { rangeCheckFields(keyPositions, type.getArity() - 1); } checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point"); // extract key field types CompositeType<T> cType = (CompositeType<T>)type; this.keyFields = new ArrayList<>(type.getTotalFields()); // for each key position, find all (nested) field types String[] fieldNames = cType.getFieldNames(); this.originalKeyTypes = new TypeInformation<?>[keyPositions.length]; ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>(); for (int i = 0; i < keyPositions.length; i++) { int keyPos = keyPositions[i]; tmpList.clear(); // get all flat fields this.originalKeyTypes[i] = cType.getTypeAt(keyPos); cType.getFlatFields(fieldNames[keyPos], 0, tmpList); // check if fields are of key type for(FlatFieldDescriptor ffd : tmpList) { if(!ffd.getType().isKeyType()) { throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key."); } } this.keyFields.addAll(tmpList); } }
Example #17
Source File: Keys.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * Create int-based (non-nested) field position keys on a tuple type. */ public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) { 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 (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) { throw new IllegalArgumentException("The grouping fields must not be empty."); } this.keyFields = new ArrayList<>(); if (keyPositions == null || keyPositions.length == 0) { // use all tuple fields as key fields keyPositions = createIncrIntArray(type.getArity()); } else { rangeCheckFields(keyPositions, type.getArity() - 1); } checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point"); // extract key field types CompositeType<T> cType = (CompositeType<T>)type; this.keyFields = new ArrayList<>(type.getTotalFields()); // for each key position, find all (nested) field types String[] fieldNames = cType.getFieldNames(); this.originalKeyTypes = new TypeInformation<?>[keyPositions.length]; ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>(); for (int i = 0; i < keyPositions.length; i++) { int keyPos = keyPositions[i]; tmpList.clear(); // get all flat fields this.originalKeyTypes[i] = cType.getTypeAt(keyPos); cType.getFlatFields(fieldNames[keyPos], 0, tmpList); // check if fields are of key type for(FlatFieldDescriptor ffd : tmpList) { if(!ffd.getType().isKeyType()) { throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key."); } } this.keyFields.addAll(tmpList); } }
Example #18
Source File: TypeExtractorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithBasicTypes() throws Exception { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>, Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>() { private static final long serialVersionUID = 1L; @Override public Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> map( Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(9, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f3", 0, ffd); Assert.assertTrue(ffd.size() == 1); Assert.assertEquals(3, ffd.get(0).getPosition() ); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple9.class, tti.getTypeClass()); for (int i = 0; i < 9; i++) { Assert.assertTrue(tti.getTypeAt(i) instanceof BasicTypeInfo); } Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti.getTypeAt(8)); // use getForObject() Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> t = new Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>( 1, 1L, 1.0, 1.0F, false, "Hello World", 'w', (short) 1, (byte) 1); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti2.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti2.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti2.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti2.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti2.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti2.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti2.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti2.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti2.getTypeAt(8)); // test that getForClass does not work try { TypeExtractor.getForClass(Tuple9.class); Assert.fail("Exception expected here"); } catch (InvalidTypesException e) { // that is correct } }
Example #19
Source File: SemanticPropUtil.java From flink with Apache License 2.0 | 4 votes |
private static void parseNonForwardedFields(SemanticProperties sp, String[] nonForwardedStr, TypeInformation<?> inType, TypeInformation<?> outType, int input, boolean skipIncompatibleTypes) { if (nonForwardedStr == null) { return; } FieldSet excludedFields = new FieldSet(); for (String s : nonForwardedStr) { // remove white characters s = s.replaceAll("\\s", ""); if (s.equals("")) { continue; } if (!inType.equals(outType)) { if (skipIncompatibleTypes) { continue; } else { throw new InvalidSemanticAnnotationException("Non-forwarded fields annotation only allowed for identical input and output types."); } } Matcher matcher = PATTERN_LIST.matcher(s); if (!matcher.matches()) { throw new InvalidSemanticAnnotationException("Invalid format of non-forwarded fields annotation \"" + s + "\"."); } // process individual fields matcher = PATTERN_FIELD.matcher(s); while (matcher.find()) { String fieldStr = matcher.group(); try { // get and add all flat field positions List<FlatFieldDescriptor> inFFDs = getFlatFields(fieldStr, inType); for (FlatFieldDescriptor ffd : inFFDs) { excludedFields = excludedFields.addField(ffd.getPosition()); } } catch (InvalidFieldReferenceException ifre) { throw new InvalidSemanticAnnotationException("Invalid field reference in non-forwarded fields annotation \"" + fieldStr + "\".", ifre); } } } for (int i = 0; i < inType.getTotalFields(); i++) { if (!excludedFields.contains(i)) { if (sp instanceof SingleInputSemanticProperties) { ((SingleInputSemanticProperties) sp).addForwardedField(i, i); } else if (sp instanceof DualInputSemanticProperties) { ((DualInputSemanticProperties) sp).addForwardedField(input, i, i); } } } }
Example #20
Source File: TypeExtractorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithTuples() { // use getFlatMapReturnTypes() RichFlatMapFunction<?, ?> function = new RichFlatMapFunction<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>, Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>() { private static final long serialVersionUID = 1L; @Override public void flatMap(Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> value, Collector<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>> out) throws Exception { // nothing to do } }; TypeInformation<?> ti = TypeExtractor.getFlatMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertEquals(0, ffd.get(0).getPosition() ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertTrue( ffd.get(0).getType() instanceof BasicTypeInfo ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f1.f0", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); ffd.clear(); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple3.class, tti.getTypeClass()); Assert.assertTrue(tti.getTypeAt(0).isTupleType()); Assert.assertTrue(tti.getTypeAt(1).isTupleType()); Assert.assertTrue(tti.getTypeAt(2).isTupleType()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(0).getTypeClass()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(1).getTypeClass()); Assert.assertEquals(Tuple2.class, tti.getTypeAt(2).getTypeClass()); Assert.assertEquals(1, tti.getTypeAt(0).getArity()); Assert.assertEquals(1, tti.getTypeAt(1).getArity()); Assert.assertEquals(2, tti.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(1)); // use getForObject() Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> t = new Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>( new Tuple1<String>("hello"), new Tuple1<Integer>(1), new Tuple2<Long, Long>(2L, 3L)); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(1, tti2.getTypeAt(0).getArity()); Assert.assertEquals(1, tti2.getTypeAt(1).getArity()); Assert.assertEquals(2, tti2.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(1)); }
Example #21
Source File: PojoSerializerTest.java From flink with Apache License 2.0 | 4 votes |
/** * This tests if the hashes returned by the pojo and tuple comparators are the same */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testTuplePojoTestEquality() { // test with a simple, string-key first. PojoTypeInfo<TestUserClass> pType = (PojoTypeInfo<TestUserClass>) type; List<FlatFieldDescriptor> result = new ArrayList<FlatFieldDescriptor>(); pType.getFlatFields("nestedClass.dumm2", 0, result); int[] fields = new int[1]; // see below fields[0] = result.get(0).getPosition(); TypeComparator<TestUserClass> pojoComp = pType.createComparator( fields, new boolean[]{true}, 0, new ExecutionConfig()); TestUserClass pojoTestRecord = new TestUserClass(0, "abc", 3d, new int[] {1,2,3}, new Date(), new NestedTestUserClass(1, "haha", 4d, new int[] {5,4,3})); int pHash = pojoComp.hash(pojoTestRecord); Tuple1<String> tupleTest = new Tuple1<String>("haha"); TupleTypeInfo<Tuple1<String>> tType = (TupleTypeInfo<Tuple1<String>>)TypeExtractor.getForObject(tupleTest); TypeComparator<Tuple1<String>> tupleComp = tType.createComparator(new int[] {0}, new boolean[] {true}, 0, new ExecutionConfig()); int tHash = tupleComp.hash(tupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable", pHash == tHash); Tuple3<Integer, String, Double> multiTupleTest = new Tuple3<Integer, String, Double>(1, "haha", 4d); // its important here to use the same values. TupleTypeInfo<Tuple3<Integer, String, Double>> multiTupleType = (TupleTypeInfo<Tuple3<Integer, String, Double>>)TypeExtractor.getForObject(multiTupleTest); ExpressionKeys fieldKey = new ExpressionKeys(new int[]{1,0,2}, multiTupleType); ExpressionKeys expressKey = new ExpressionKeys(new String[] {"nestedClass.dumm2", "nestedClass.dumm1", "nestedClass.dumm3"}, pType); try { Assert.assertTrue("Expecting the keys to be compatible", fieldKey.areCompatible(expressKey)); } catch (IncompatibleKeysException e) { e.printStackTrace(); Assert.fail("Keys must be compatible: "+e.getMessage()); } TypeComparator<TestUserClass> multiPojoComp = pType.createComparator( expressKey.computeLogicalKeyPositions(), new boolean[]{true, true, true}, 0, new ExecutionConfig()); int multiPojoHash = multiPojoComp.hash(pojoTestRecord); // pojo order is: dumm2 (str), dumm1 (int), dumm3 (double). TypeComparator<Tuple3<Integer, String, Double>> multiTupleComp = multiTupleType.createComparator(fieldKey.computeLogicalKeyPositions(), new boolean[] {true, true,true}, 0, new ExecutionConfig()); int multiTupleHash = multiTupleComp.hash(multiTupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable. Also for those with multiple key fields", multiPojoHash == multiTupleHash); }
Example #22
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithPojo() { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple2<Long, CustomType>, Tuple2<Long, CustomType>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<Long, CustomType> map(Tuple2<Long, CustomType> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<Long, CustomType>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple2.class, tti.getTypeClass()); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); tti.getFlatFields("f0", 0, ffd); Assert.assertEquals(1, ffd.size()); Assert.assertEquals(0, ffd.get(0).getPosition() ); // Long Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Long.class) ); ffd.clear(); tti.getFlatFields("f1.myField1", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); tti.getFlatFields("f1.myField2", 0, ffd); Assert.assertEquals(2, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Integer.class) ); Assert.assertEquals(Long.class, tti.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti.getTypeAt(1).getTypeClass()); // use getForObject() Tuple2<?, ?> t = new Tuple2<Long, CustomType>(1L, new CustomType("Hello", 1)); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(2, ti2.getArity()); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) ti2; Assert.assertEquals(Tuple2.class, tti2.getTypeClass()); Assert.assertEquals(Long.class, tti2.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti2.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti2.getTypeAt(1).getTypeClass()); }
Example #23
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithTuples() { // use getFlatMapReturnTypes() RichFlatMapFunction<?, ?> function = new RichFlatMapFunction<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>, Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>() { private static final long serialVersionUID = 1L; @Override public void flatMap(Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> value, Collector<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>> out) throws Exception { // nothing to do } }; TypeInformation<?> ti = TypeExtractor.getFlatMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(3, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertEquals(0, ffd.get(0).getPosition() ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f0.f0", 0, ffd); Assert.assertTrue( ffd.get(0).getType() instanceof BasicTypeInfo ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); ((TupleTypeInfo) ti).getFlatFields("f1.f0", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); ffd.clear(); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple3.class, tti.getTypeClass()); Assert.assertTrue(tti.getTypeAt(0).isTupleType()); Assert.assertTrue(tti.getTypeAt(1).isTupleType()); Assert.assertTrue(tti.getTypeAt(2).isTupleType()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(0).getTypeClass()); Assert.assertEquals(Tuple1.class, tti.getTypeAt(1).getTypeClass()); Assert.assertEquals(Tuple2.class, tti.getTypeAt(2).getTypeClass()); Assert.assertEquals(1, tti.getTypeAt(0).getArity()); Assert.assertEquals(1, tti.getTypeAt(1).getArity()); Assert.assertEquals(2, tti.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti.getTypeAt(2)).getTypeAt(1)); // use getForObject() Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>> t = new Tuple3<Tuple1<String>, Tuple1<Integer>, Tuple2<Long, Long>>( new Tuple1<String>("hello"), new Tuple1<Integer>(1), new Tuple2<Long, Long>(2L, 3L)); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(1, tti2.getTypeAt(0).getArity()); Assert.assertEquals(1, tti2.getTypeAt(1).getArity()); Assert.assertEquals(2, tti2.getTypeAt(2).getArity()); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(0)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(1)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, ((TupleTypeInfo<?>) tti2.getTypeAt(2)).getTypeAt(1)); }
Example #24
Source File: TypeExtractorTest.java From flink with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithBasicTypes() throws Exception { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>, Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>() { private static final long serialVersionUID = 1L; @Override public Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> map( Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(9, ti.getArity()); Assert.assertTrue(ti instanceof TupleTypeInfo); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); ((TupleTypeInfo) ti).getFlatFields("f3", 0, ffd); Assert.assertTrue(ffd.size() == 1); Assert.assertEquals(3, ffd.get(0).getPosition() ); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple9.class, tti.getTypeClass()); for (int i = 0; i < 9; i++) { Assert.assertTrue(tti.getTypeAt(i) instanceof BasicTypeInfo); } Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti.getTypeAt(8)); // use getForObject() Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte> t = new Tuple9<Integer, Long, Double, Float, Boolean, String, Character, Short, Byte>( 1, 1L, 1.0, 1.0F, false, "Hello World", 'w', (short) 1, (byte) 1); Assert.assertTrue(TypeExtractor.getForObject(t) instanceof TupleTypeInfo); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) TypeExtractor.getForObject(t); Assert.assertEquals(BasicTypeInfo.INT_TYPE_INFO, tti2.getTypeAt(0)); Assert.assertEquals(BasicTypeInfo.LONG_TYPE_INFO, tti2.getTypeAt(1)); Assert.assertEquals(BasicTypeInfo.DOUBLE_TYPE_INFO, tti2.getTypeAt(2)); Assert.assertEquals(BasicTypeInfo.FLOAT_TYPE_INFO, tti2.getTypeAt(3)); Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, tti2.getTypeAt(4)); Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, tti2.getTypeAt(5)); Assert.assertEquals(BasicTypeInfo.CHAR_TYPE_INFO, tti2.getTypeAt(6)); Assert.assertEquals(BasicTypeInfo.SHORT_TYPE_INFO, tti2.getTypeAt(7)); Assert.assertEquals(BasicTypeInfo.BYTE_TYPE_INFO, tti2.getTypeAt(8)); // test that getForClass does not work try { TypeExtractor.getForClass(Tuple9.class); Assert.fail("Exception expected here"); } catch (InvalidTypesException e) { // that is correct } }
Example #25
Source File: Keys.java From flink with Apache License 2.0 | 4 votes |
/** * Create int-based (non-nested) field position keys on a tuple type. */ public ExpressionKeys(int[] keyPositions, TypeInformation<T> type, boolean allowEmpty) { 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 (!allowEmpty && (keyPositions == null || keyPositions.length == 0)) { throw new IllegalArgumentException("The grouping fields must not be empty."); } this.keyFields = new ArrayList<>(); if (keyPositions == null || keyPositions.length == 0) { // use all tuple fields as key fields keyPositions = createIncrIntArray(type.getArity()); } else { rangeCheckFields(keyPositions, type.getArity() - 1); } checkArgument(keyPositions.length > 0, "Grouping fields can not be empty at this point"); // extract key field types CompositeType<T> cType = (CompositeType<T>)type; this.keyFields = new ArrayList<>(type.getTotalFields()); // for each key position, find all (nested) field types String[] fieldNames = cType.getFieldNames(); this.originalKeyTypes = new TypeInformation<?>[keyPositions.length]; ArrayList<FlatFieldDescriptor> tmpList = new ArrayList<>(); for (int i = 0; i < keyPositions.length; i++) { int keyPos = keyPositions[i]; tmpList.clear(); // get all flat fields this.originalKeyTypes[i] = cType.getTypeAt(keyPos); cType.getFlatFields(fieldNames[keyPos], 0, tmpList); // check if fields are of key type for(FlatFieldDescriptor ffd : tmpList) { if(!ffd.getType().isKeyType()) { throw new InvalidProgramException("This type (" + ffd.getType() + ") cannot be used as key."); } } this.keyFields.addAll(tmpList); } }
Example #26
Source File: TypeExtractorTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void testTupleWithPojo() { // use getMapReturnTypes() RichMapFunction<?, ?> function = new RichMapFunction<Tuple2<Long, CustomType>, Tuple2<Long, CustomType>>() { private static final long serialVersionUID = 1L; @Override public Tuple2<Long, CustomType> map(Tuple2<Long, CustomType> value) throws Exception { return null; } }; TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<Tuple2<Long, CustomType>>(){})); Assert.assertTrue(ti.isTupleType()); Assert.assertEquals(2, ti.getArity()); TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti; Assert.assertEquals(Tuple2.class, tti.getTypeClass()); List<FlatFieldDescriptor> ffd = new ArrayList<FlatFieldDescriptor>(); tti.getFlatFields("f0", 0, ffd); Assert.assertEquals(1, ffd.size()); Assert.assertEquals(0, ffd.get(0).getPosition() ); // Long Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Long.class) ); ffd.clear(); tti.getFlatFields("f1.myField1", 0, ffd); Assert.assertEquals(1, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(String.class) ); ffd.clear(); tti.getFlatFields("f1.myField2", 0, ffd); Assert.assertEquals(2, ffd.get(0).getPosition() ); Assert.assertTrue( ffd.get(0).getType().getTypeClass().equals(Integer.class) ); Assert.assertEquals(Long.class, tti.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti.getTypeAt(1).getTypeClass()); // use getForObject() Tuple2<?, ?> t = new Tuple2<Long, CustomType>(1L, new CustomType("Hello", 1)); TypeInformation<?> ti2 = TypeExtractor.getForObject(t); Assert.assertTrue(ti2.isTupleType()); Assert.assertEquals(2, ti2.getArity()); TupleTypeInfo<?> tti2 = (TupleTypeInfo<?>) ti2; Assert.assertEquals(Tuple2.class, tti2.getTypeClass()); Assert.assertEquals(Long.class, tti2.getTypeAt(0).getTypeClass()); Assert.assertTrue(tti2.getTypeAt(1) instanceof PojoTypeInfo); Assert.assertEquals(CustomType.class, tti2.getTypeAt(1).getTypeClass()); }
Example #27
Source File: PojoSerializerTest.java From Flink-CEPplus with Apache License 2.0 | 4 votes |
/** * This tests if the hashes returned by the pojo and tuple comparators are the same */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Test public void testTuplePojoTestEquality() { // test with a simple, string-key first. PojoTypeInfo<TestUserClass> pType = (PojoTypeInfo<TestUserClass>) type; List<FlatFieldDescriptor> result = new ArrayList<FlatFieldDescriptor>(); pType.getFlatFields("nestedClass.dumm2", 0, result); int[] fields = new int[1]; // see below fields[0] = result.get(0).getPosition(); TypeComparator<TestUserClass> pojoComp = pType.createComparator( fields, new boolean[]{true}, 0, new ExecutionConfig()); TestUserClass pojoTestRecord = new TestUserClass(0, "abc", 3d, new int[] {1,2,3}, new Date(), new NestedTestUserClass(1, "haha", 4d, new int[] {5,4,3})); int pHash = pojoComp.hash(pojoTestRecord); Tuple1<String> tupleTest = new Tuple1<String>("haha"); TupleTypeInfo<Tuple1<String>> tType = (TupleTypeInfo<Tuple1<String>>)TypeExtractor.getForObject(tupleTest); TypeComparator<Tuple1<String>> tupleComp = tType.createComparator(new int[] {0}, new boolean[] {true}, 0, new ExecutionConfig()); int tHash = tupleComp.hash(tupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable", pHash == tHash); Tuple3<Integer, String, Double> multiTupleTest = new Tuple3<Integer, String, Double>(1, "haha", 4d); // its important here to use the same values. TupleTypeInfo<Tuple3<Integer, String, Double>> multiTupleType = (TupleTypeInfo<Tuple3<Integer, String, Double>>)TypeExtractor.getForObject(multiTupleTest); ExpressionKeys fieldKey = new ExpressionKeys(new int[]{1,0,2}, multiTupleType); ExpressionKeys expressKey = new ExpressionKeys(new String[] {"nestedClass.dumm2", "nestedClass.dumm1", "nestedClass.dumm3"}, pType); try { Assert.assertTrue("Expecting the keys to be compatible", fieldKey.areCompatible(expressKey)); } catch (IncompatibleKeysException e) { e.printStackTrace(); Assert.fail("Keys must be compatible: "+e.getMessage()); } TypeComparator<TestUserClass> multiPojoComp = pType.createComparator( expressKey.computeLogicalKeyPositions(), new boolean[]{true, true, true}, 0, new ExecutionConfig()); int multiPojoHash = multiPojoComp.hash(pojoTestRecord); // pojo order is: dumm2 (str), dumm1 (int), dumm3 (double). TypeComparator<Tuple3<Integer, String, Double>> multiTupleComp = multiTupleType.createComparator(fieldKey.computeLogicalKeyPositions(), new boolean[] {true, true,true}, 0, new ExecutionConfig()); int multiTupleHash = multiTupleComp.hash(multiTupleTest); Assert.assertTrue("The hashing for tuples and pojos must be the same, so that they are mixable. Also for those with multiple key fields", multiPojoHash == multiTupleHash); }