Java Code Examples for org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo

The following examples show how to use org.apache.flink.api.common.typeinfo.BasicArrayTypeInfo. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Author: ljygz   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #2
Source Project: Flink-CEPplus   Author: ljygz   File: KeySelectorUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <X> ArrayKeySelector<X> getSelectorForArray(int[] positions, TypeInformation<X> typeInfo) {
	if (positions == null || positions.length == 0 || positions.length > Tuple.MAX_ARITY) {
		throw new IllegalArgumentException("Array keys must have between 1 and " + Tuple.MAX_ARITY + " fields.");
	}

	TypeInformation<?> componentType;

	if (typeInfo instanceof BasicArrayTypeInfo) {
		BasicArrayTypeInfo<X, ?>  arrayInfo = (BasicArrayTypeInfo<X, ?>) typeInfo;
		componentType = arrayInfo.getComponentInfo();
	}
	else if (typeInfo instanceof PrimitiveArrayTypeInfo) {
		PrimitiveArrayTypeInfo<X> arrayType = (PrimitiveArrayTypeInfo<X>) typeInfo;
		componentType = arrayType.getComponentType();
	}
	else {
		throw new IllegalArgumentException("This method only supports arrays of primitives and boxed primitives.");
	}

	TypeInformation<?>[] primitiveInfos = new TypeInformation<?>[positions.length];
	Arrays.fill(primitiveInfos, componentType);

	return new ArrayKeySelector<>(positions, new TupleTypeInfo<>(primitiveInfos));
}
 
Example #3
Source Project: Flink-CEPplus   Author: ljygz   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleNestedArrayKeyRejection() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<Tuple2<Integer[], String>> input = env.fromElements(
			new Tuple2<>(new Integer[] {1, 2}, "test-test"));

	TypeInformation<?> expectedTypeInfo = new TupleTypeInfo<Tuple2<Integer[], String>>(
			BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);

	// adjust the rule
	expectedException.expect(InvalidProgramException.class);
	expectedException.expectMessage(new StringStartsWith("Type " + expectedTypeInfo + " cannot be used as key."));

	input.keyBy(new KeySelector<Tuple2<Integer[], String>, Tuple2<Integer[], String>>() {
		@Override
		public Tuple2<Integer[], String> getKey(Tuple2<Integer[], String> value) throws Exception {
			return value;
		}
	});
}
 
Example #4
Source Project: Flink-CEPplus   Author: ljygz   File: ArrayKeySelectorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectArrays() {
	try {
		String[] array1 = { "a", "b", "c", "d", "e" };
		String[] array2 = { "v", "w", "x", "y", "z" };

		KeySelectorUtil.ArrayKeySelector<String[]> singleFieldSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {1}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple1<>("b"), singleFieldSelector.getKey(array1));
		assertEquals(new Tuple1<>("w"), singleFieldSelector.getKey(array2));

		KeySelectorUtil.ArrayKeySelector<String[]> twoFieldsSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {3, 0}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple2<>("d", "a"), twoFieldsSelector.getKey(array1));
		assertEquals(new Tuple2<>("y", "v"), twoFieldsSelector.getKey(array2));

	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #5
Source Project: flink   Author: flink-tpc-ds   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #6
Source Project: flink   Author: flink-tpc-ds   File: KeySelectorUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <X> ArrayKeySelector<X> getSelectorForArray(int[] positions, TypeInformation<X> typeInfo) {
	if (positions == null || positions.length == 0 || positions.length > Tuple.MAX_ARITY) {
		throw new IllegalArgumentException("Array keys must have between 1 and " + Tuple.MAX_ARITY + " fields.");
	}

	TypeInformation<?> componentType;

	if (typeInfo instanceof BasicArrayTypeInfo) {
		BasicArrayTypeInfo<X, ?>  arrayInfo = (BasicArrayTypeInfo<X, ?>) typeInfo;
		componentType = arrayInfo.getComponentInfo();
	}
	else if (typeInfo instanceof PrimitiveArrayTypeInfo) {
		PrimitiveArrayTypeInfo<X> arrayType = (PrimitiveArrayTypeInfo<X>) typeInfo;
		componentType = arrayType.getComponentType();
	}
	else {
		throw new IllegalArgumentException("This method only supports arrays of primitives and boxed primitives.");
	}

	TypeInformation<?>[] primitiveInfos = new TypeInformation<?>[positions.length];
	Arrays.fill(primitiveInfos, componentType);

	return new ArrayKeySelector<>(positions, new TupleTypeInfo<>(primitiveInfos));
}
 
Example #7
Source Project: flink   Author: flink-tpc-ds   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleNestedArrayKeyRejection() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<Tuple2<Integer[], String>> input = env.fromElements(
			new Tuple2<>(new Integer[] {1, 2}, "test-test"));

	TypeInformation<?> expectedTypeInfo = new TupleTypeInfo<Tuple2<Integer[], String>>(
			BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);

	// adjust the rule
	expectedException.expect(InvalidProgramException.class);
	expectedException.expectMessage(new StringStartsWith("Type " + expectedTypeInfo + " cannot be used as key."));

	input.keyBy(new KeySelector<Tuple2<Integer[], String>, Tuple2<Integer[], String>>() {
		@Override
		public Tuple2<Integer[], String> getKey(Tuple2<Integer[], String> value) throws Exception {
			return value;
		}
	});
}
 
Example #8
Source Project: flink   Author: flink-tpc-ds   File: ArrayKeySelectorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectArrays() {
	try {
		String[] array1 = { "a", "b", "c", "d", "e" };
		String[] array2 = { "v", "w", "x", "y", "z" };

		KeySelectorUtil.ArrayKeySelector<String[]> singleFieldSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {1}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple1<>("b"), singleFieldSelector.getKey(array1));
		assertEquals(new Tuple1<>("w"), singleFieldSelector.getKey(array2));

		KeySelectorUtil.ArrayKeySelector<String[]> twoFieldsSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {3, 0}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple2<>("d", "a"), twoFieldsSelector.getKey(array1));
		assertEquals(new Tuple2<>("y", "v"), twoFieldsSelector.getKey(array2));

	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #9
Source Project: flink   Author: apache   File: JsonRowDeserializationSchema.java    License: Apache License 2.0 6 votes vote down vote up
private Optional<DeserializationRuntimeConverter> createContainerConverter(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof RowTypeInfo) {
		return Optional.of(createRowConverter((RowTypeInfo) typeInfo));
	} else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((ObjectArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (typeInfo instanceof BasicArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((BasicArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (isPrimitiveByteArray(typeInfo)) {
		return Optional.of(createByteArrayConverter());
	} else if (typeInfo instanceof MapTypeInfo) {
		MapTypeInfo<?, ?> mapTypeInfo = (MapTypeInfo<?, ?>) typeInfo;
		return Optional.of(createMapConverter(mapTypeInfo.getKeyTypeInfo(), mapTypeInfo.getValueTypeInfo()));
	} else {
		return Optional.empty();
	}
}
 
Example #10
Source Project: flink   Author: apache   File: TypeExtractorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleWithPrimitiveArray() {
	RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>> function = new RichMapFunction<Integer, Tuple9<int[],double[],long[],byte[],char[],float[],short[], boolean[], String[]>>() {
		private static final long serialVersionUID = 1L;

		@Override
		public Tuple9<int[], double[], long[], byte[], char[], float[], short[], boolean[], String[]> map(Integer value) throws Exception {
			return null;
		}
	};
	
	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicTypeInfo.INT_TYPE_INFO);
	TupleTypeInfo<?> tti = (TupleTypeInfo<?>) ti;
	Assert.assertEquals(PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(0));
	Assert.assertEquals(PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(1));
	Assert.assertEquals(PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(2));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(3));
	Assert.assertEquals(PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(4));
	Assert.assertEquals(PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(5));
	Assert.assertEquals(PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(6));
	Assert.assertEquals(PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO, tti.getTypeAt(7));
	Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, tti.getTypeAt(8));
}
 
Example #11
Source Project: flink   Author: apache   File: KeySelectorUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static <X> ArrayKeySelector<X> getSelectorForArray(int[] positions, TypeInformation<X> typeInfo) {
	if (positions == null || positions.length == 0 || positions.length > Tuple.MAX_ARITY) {
		throw new IllegalArgumentException("Array keys must have between 1 and " + Tuple.MAX_ARITY + " fields.");
	}

	TypeInformation<?> componentType;

	if (typeInfo instanceof BasicArrayTypeInfo) {
		BasicArrayTypeInfo<X, ?>  arrayInfo = (BasicArrayTypeInfo<X, ?>) typeInfo;
		componentType = arrayInfo.getComponentInfo();
	}
	else if (typeInfo instanceof PrimitiveArrayTypeInfo) {
		PrimitiveArrayTypeInfo<X> arrayType = (PrimitiveArrayTypeInfo<X>) typeInfo;
		componentType = arrayType.getComponentType();
	}
	else {
		throw new IllegalArgumentException("This method only supports arrays of primitives and boxed primitives.");
	}

	TypeInformation<?>[] primitiveInfos = new TypeInformation<?>[positions.length];
	Arrays.fill(primitiveInfos, componentType);

	return new ArrayKeySelector<>(positions, new TupleTypeInfo<>(primitiveInfos));
}
 
Example #12
Source Project: flink   Author: apache   File: DataStreamTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTupleNestedArrayKeyRejection() {
	StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStream<Tuple2<Integer[], String>> input = env.fromElements(
			new Tuple2<>(new Integer[] {1, 2}, "test-test"));

	TypeInformation<?> expectedTypeInfo = new TupleTypeInfo<Tuple2<Integer[], String>>(
			BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO);

	// adjust the rule
	expectedException.expect(InvalidProgramException.class);
	expectedException.expectMessage(new StringStartsWith("Type " + expectedTypeInfo + " cannot be used as key."));

	input.keyBy(new KeySelector<Tuple2<Integer[], String>, Tuple2<Integer[], String>>() {
		@Override
		public Tuple2<Integer[], String> getKey(Tuple2<Integer[], String> value) throws Exception {
			return value;
		}
	});
}
 
Example #13
Source Project: flink   Author: apache   File: ArrayKeySelectorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectArrays() {
	try {
		String[] array1 = { "a", "b", "c", "d", "e" };
		String[] array2 = { "v", "w", "x", "y", "z" };

		KeySelectorUtil.ArrayKeySelector<String[]> singleFieldSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {1}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple1<>("b"), singleFieldSelector.getKey(array1));
		assertEquals(new Tuple1<>("w"), singleFieldSelector.getKey(array2));

		KeySelectorUtil.ArrayKeySelector<String[]> twoFieldsSelector =
				KeySelectorUtil.getSelectorForArray(new int[] {3, 0}, BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO);

		assertEquals(new Tuple2<>("d", "a"), twoFieldsSelector.getKey(array1));
		assertEquals(new Tuple2<>("y", "v"), twoFieldsSelector.getKey(array2));

	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example #14
Source Project: Flink-CEPplus   Author: ljygz   File: CsvRowSchemaConverter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert {@link TypeInformation} to {@link CsvSchema.ColumnType} based on Jackson's categories.
 */
private static CsvSchema.ColumnType convertType(String fieldName, TypeInformation<?> info) {
	if (STRING_TYPES.contains(info)) {
		return CsvSchema.ColumnType.STRING;
	} else if (NUMBER_TYPES.contains(info)) {
		return CsvSchema.ColumnType.NUMBER;
	} else if (BOOLEAN_TYPES.contains(info)) {
		return CsvSchema.ColumnType.BOOLEAN;
	} else if (info instanceof ObjectArrayTypeInfo) {
		validateNestedField(fieldName, ((ObjectArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof BasicArrayTypeInfo) {
		validateNestedField(fieldName, ((BasicArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof RowTypeInfo) {
		final TypeInformation<?>[] types = ((RowTypeInfo) info).getFieldTypes();
		for (TypeInformation<?> type : types) {
			validateNestedField(fieldName, type);
		}
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof PrimitiveArrayTypeInfo &&
			((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return CsvSchema.ColumnType.STRING;
	} else {
		throw new IllegalArgumentException(
			"Unsupported type information '" + info.toString() + "' for field '" + fieldName + "'.");
	}
}
 
Example #15
Source Project: Flink-CEPplus   Author: ljygz   File: TypeExtractorTest.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testBasicArray() {
	// use getCoGroupReturnTypes()
	RichCoGroupFunction<?, ?, ?> function = new RichCoGroupFunction<String[], String[], String[]>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void coGroup(Iterable<String[]> first, Iterable<String[]> second, Collector<String[]> out) throws Exception {
			// nothing to do
		}
	};

	TypeInformation<?> ti = TypeExtractor.getCoGroupReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}), (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}));

	Assert.assertFalse(ti.isBasicType());
	Assert.assertFalse(ti.isTupleType());
	
	// Due to a Java 6 bug the classification can be slightly wrong
	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?,?> || ti instanceof ObjectArrayTypeInfo<?,?>);
	
	if(ti instanceof BasicArrayTypeInfo<?,?>) {
		Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, ti);
	}
	else {
		Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((ObjectArrayTypeInfo<?,?>) ti).getComponentInfo());
	}		
}
 
Example #16
Source Project: Flink-CEPplus   Author: ljygz   File: TypeExtractorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicArray2() {
	RichMapFunction<Boolean[], ?> function = new IdentityMapper<Boolean[]>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicArrayTypeInfo.BOOLEAN_ARRAY_TYPE_INFO);

	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?, ?>);
	BasicArrayTypeInfo<?, ?> bati = (BasicArrayTypeInfo<?, ?>) ti;
	Assert.assertTrue(bati.getComponentInfo().isBasicType());
	Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, bati.getComponentInfo());
}
 
Example #17
Source Project: Flink-CEPplus   Author: ljygz   File: DataStreamTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicArrayKeyRejection() {

	KeySelector<Tuple2<Integer[], String>, Integer[]> keySelector =
			new KeySelector<Tuple2<Integer[], String>, Integer[]>() {

		@Override
		public Integer[] getKey(Tuple2<Integer[], String> value) throws Exception {
			return value.f0;
		}
	};

	testKeyRejection(keySelector, BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO);
}
 
Example #18
Source Project: Flink-CEPplus   Author: ljygz   File: FieldAccessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testArray() {
	int[] a = new int[]{3, 5};
	FieldAccessor<int[], Integer> fieldAccessor =
			(FieldAccessor<int[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(PrimitiveArrayTypeInfo.getInfoFor(a.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor.getFieldType().getTypeClass());

	assertEquals((Integer) a[1], fieldAccessor.get(a));

	a = fieldAccessor.set(a, 6);
	assertEquals((Integer) a[1], fieldAccessor.get(a));

	Integer[] b = new Integer[]{3, 5};
	FieldAccessor<Integer[], Integer> fieldAccessor2 =
			(FieldAccessor<Integer[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(BasicArrayTypeInfo.getInfoFor(b.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor2.getFieldType().getTypeClass());

	assertEquals(b[1], fieldAccessor2.get(b));

	b = fieldAccessor2.set(b, 6);
	assertEquals(b[1], fieldAccessor2.get(b));
}
 
Example #19
Source Project: flink   Author: flink-tpc-ds   File: LegacyTypeInfoDataTypeConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType toDataType(TypeInformation<?> typeInfo) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (typeInfo instanceof TimeIndicatorTypeInfo) {
		return convertToTimeAttributeType((TimeIndicatorTypeInfo) typeInfo);
	}

	final DataType foundDataType = typeInfoDataTypeMap.get(typeInfo);
	if (foundDataType != null) {
		return foundDataType;
	}

	if (typeInfo instanceof RowTypeInfo) {
		return convertToRowType((RowTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return convertToArrayType(
			typeInfo.getTypeClass(),
			((ObjectArrayTypeInfo) typeInfo).getComponentInfo());
	}

	else if (typeInfo instanceof BasicArrayTypeInfo) {
		return createLegacyType(LogicalTypeRoot.ARRAY, typeInfo);
	}

	else if (typeInfo instanceof MultisetTypeInfo) {
		return convertToMultisetType(((MultisetTypeInfo) typeInfo).getElementTypeInfo());
	}

	else if (typeInfo instanceof MapTypeInfo) {
		return convertToMapType((MapTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof CompositeType) {
		return createLegacyType(LogicalTypeRoot.STRUCTURED_TYPE, typeInfo);
	}

	return createLegacyType(LogicalTypeRoot.ANY, typeInfo);
}
 
Example #20
Source Project: flink   Author: flink-tpc-ds   File: CsvRowSchemaConverter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert {@link TypeInformation} to {@link CsvSchema.ColumnType} based on Jackson's categories.
 */
private static CsvSchema.ColumnType convertType(String fieldName, TypeInformation<?> info) {
	if (STRING_TYPES.contains(info)) {
		return CsvSchema.ColumnType.STRING;
	} else if (NUMBER_TYPES.contains(info)) {
		return CsvSchema.ColumnType.NUMBER;
	} else if (BOOLEAN_TYPES.contains(info)) {
		return CsvSchema.ColumnType.BOOLEAN;
	} else if (info instanceof ObjectArrayTypeInfo) {
		validateNestedField(fieldName, ((ObjectArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof BasicArrayTypeInfo) {
		validateNestedField(fieldName, ((BasicArrayTypeInfo) info).getComponentInfo());
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof RowTypeInfo) {
		final TypeInformation<?>[] types = ((RowTypeInfo) info).getFieldTypes();
		for (TypeInformation<?> type : types) {
			validateNestedField(fieldName, type);
		}
		return CsvSchema.ColumnType.ARRAY;
	} else if (info instanceof PrimitiveArrayTypeInfo &&
			((PrimitiveArrayTypeInfo) info).getComponentType() == Types.BYTE) {
		return CsvSchema.ColumnType.STRING;
	} else {
		throw new IllegalArgumentException(
			"Unsupported type information '" + info.toString() + "' for field '" + fieldName + "'.");
	}
}
 
Example #21
Source Project: flink   Author: flink-tpc-ds   File: JsonRowSerializationSchema.java    License: Apache License 2.0 5 votes vote down vote up
private Optional<SerializationRuntimeConverter> createContainerConverter(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof RowTypeInfo) {
		return Optional.of(createRowConverter((RowTypeInfo) typeInfo));
	} else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((ObjectArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (typeInfo instanceof BasicArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((BasicArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (isPrimitiveByteArray(typeInfo)) {
		return Optional.of((mapper, reuse, object) -> mapper.getNodeFactory().binaryNode((byte[]) object));
	} else {
		return Optional.empty();
	}
}
 
Example #22
Source Project: flink   Author: flink-tpc-ds   File: JsonRowDeserializationSchema.java    License: Apache License 2.0 5 votes vote down vote up
private Optional<DeserializationRuntimeConverter> createContainerConverter(TypeInformation<?> typeInfo) {
	if (typeInfo instanceof RowTypeInfo) {
		return Optional.of(createRowConverter((RowTypeInfo) typeInfo));
	} else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((ObjectArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (typeInfo instanceof BasicArrayTypeInfo) {
		return Optional.of(createObjectArrayConverter(((BasicArrayTypeInfo) typeInfo).getComponentInfo()));
	} else if (isPrimitiveByteArray(typeInfo)) {
		return Optional.of(createByteArrayConverter());
	} else {
		return Optional.empty();
	}
}
 
Example #23
Source Project: flink   Author: flink-tpc-ds   File: ParquetMapInputFormat.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void convert(Map<String, Object> map, Row row, TypeInformation<?>[] fieldTypes, String[] fieldNames) {
	for (int i = 0; i < fieldNames.length; i++) {
		if (row.getField(i) != null) {
			if (fieldTypes[i] instanceof BasicTypeInfo
				|| fieldTypes[i] instanceof PrimitiveArrayTypeInfo
				|| fieldTypes[i] instanceof BasicArrayTypeInfo) {
				map.put(fieldNames[i], row.getField(i));
			} else if (fieldTypes[i] instanceof RowTypeInfo) {
				Map<String, Object> nestedRow = new HashMap<>();
				RowTypeInfo nestedRowTypeInfo = (RowTypeInfo) fieldTypes[i];
				convert(nestedRow, (Row) row.getField(i),
					nestedRowTypeInfo.getFieldTypes(), nestedRowTypeInfo.getFieldNames());
				map.put(fieldNames[i], nestedRow);
			} else if (fieldTypes[i] instanceof MapTypeInfo) {
				Map<String, Object> nestedMap = new HashMap<>();
				MapTypeInfo mapTypeInfo = (MapTypeInfo) fieldTypes[i];
				convert(nestedMap, (Map<String, Object>) row.getField(i), mapTypeInfo);
				map.put(fieldNames[i], nestedMap);
			} else if (fieldTypes[i] instanceof ObjectArrayTypeInfo) {
				List<Object> nestedObjectList = new ArrayList<>();
				ObjectArrayTypeInfo objectArrayTypeInfo = (ObjectArrayTypeInfo) fieldTypes[i];
				convert(nestedObjectList, (Row[]) row.getField(i), objectArrayTypeInfo);
				map.put(fieldNames[i], nestedObjectList);
			}
		}
	}
}
 
Example #24
Source Project: flink   Author: flink-tpc-ds   File: ParquetSchemaConverter.java    License: Apache License 2.0 5 votes vote down vote up
private static TypeInformation<?> convertParquetPrimitiveListToFlinkArray(Type type) {
	// Backward-compatibility element group doesn't exist also allowed
	TypeInformation<?> flinkType = convertParquetTypeToTypeInfo(type);
	if (flinkType.isBasicType()) {
		return BasicArrayTypeInfo.getInfoFor(Array.newInstance(flinkType.getTypeClass(), 0).getClass());
	} else {
		// flinkType here can be either SqlTimeTypeInfo or BasicTypeInfo.BIG_DEC_TYPE_INFO,
		// So it should be converted to ObjectArrayTypeInfo
		return ObjectArrayTypeInfo.getInfoFor(flinkType);
	}
}
 
Example #25
Source Project: flink   Author: flink-tpc-ds   File: TypeExtractorTest.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testBasicArray() {
	// use getCoGroupReturnTypes()
	RichCoGroupFunction<?, ?, ?> function = new RichCoGroupFunction<String[], String[], String[]>() {
		private static final long serialVersionUID = 1L;

		@Override
		public void coGroup(Iterable<String[]> first, Iterable<String[]> second, Collector<String[]> out) throws Exception {
			// nothing to do
		}
	};

	TypeInformation<?> ti = TypeExtractor.getCoGroupReturnTypes(function, (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}), (TypeInformation) TypeInformation.of(new TypeHint<String[]>(){}));

	Assert.assertFalse(ti.isBasicType());
	Assert.assertFalse(ti.isTupleType());
	
	// Due to a Java 6 bug the classification can be slightly wrong
	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?,?> || ti instanceof ObjectArrayTypeInfo<?,?>);
	
	if(ti instanceof BasicArrayTypeInfo<?,?>) {
		Assert.assertEquals(BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO, ti);
	}
	else {
		Assert.assertEquals(BasicTypeInfo.STRING_TYPE_INFO, ((ObjectArrayTypeInfo<?,?>) ti).getComponentInfo());
	}		
}
 
Example #26
Source Project: flink   Author: flink-tpc-ds   File: TypeExtractorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicArray2() {
	RichMapFunction<Boolean[], ?> function = new IdentityMapper<Boolean[]>();

	TypeInformation<?> ti = TypeExtractor.getMapReturnTypes(function, BasicArrayTypeInfo.BOOLEAN_ARRAY_TYPE_INFO);

	Assert.assertTrue(ti instanceof BasicArrayTypeInfo<?, ?>);
	BasicArrayTypeInfo<?, ?> bati = (BasicArrayTypeInfo<?, ?>) ti;
	Assert.assertTrue(bati.getComponentInfo().isBasicType());
	Assert.assertEquals(BasicTypeInfo.BOOLEAN_TYPE_INFO, bati.getComponentInfo());
}
 
Example #27
Source Project: flink   Author: flink-tpc-ds   File: OneInputOperatorTransformation.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Partitions the operator state of a {@link OperatorTransformation} by the given key positions.
 *
 * @param fields The position of the fields on which the {@code OperatorTransformation} will be grouped.
 * @return The {@code OperatorTransformation} with partitioned state.
 */
public KeyedOperatorTransformation<Tuple, T> keyBy(int... fields) {
	if (dataSet.getType() instanceof BasicArrayTypeInfo || dataSet.getType() instanceof PrimitiveArrayTypeInfo) {
		return keyBy(KeySelectorUtil.getSelectorForArray(fields, dataSet.getType()));
	} else {
		return keyBy(new Keys.ExpressionKeys<>(fields, dataSet.getType()));
	}
}
 
Example #28
Source Project: flink   Author: flink-tpc-ds   File: DataStreamTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicArrayKeyRejection() {

	KeySelector<Tuple2<Integer[], String>, Integer[]> keySelector =
			new KeySelector<Tuple2<Integer[], String>, Integer[]>() {

		@Override
		public Integer[] getKey(Tuple2<Integer[], String> value) throws Exception {
			return value.f0;
		}
	};

	testKeyRejection(keySelector, BasicArrayTypeInfo.INT_ARRAY_TYPE_INFO);
}
 
Example #29
Source Project: flink   Author: flink-tpc-ds   File: FieldAccessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testArray() {
	int[] a = new int[]{3, 5};
	FieldAccessor<int[], Integer> fieldAccessor =
			(FieldAccessor<int[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(PrimitiveArrayTypeInfo.getInfoFor(a.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor.getFieldType().getTypeClass());

	assertEquals((Integer) a[1], fieldAccessor.get(a));

	a = fieldAccessor.set(a, 6);
	assertEquals((Integer) a[1], fieldAccessor.get(a));

	Integer[] b = new Integer[]{3, 5};
	FieldAccessor<Integer[], Integer> fieldAccessor2 =
			(FieldAccessor<Integer[], Integer>) (Object)
					FieldAccessorFactory.getAccessor(BasicArrayTypeInfo.getInfoFor(b.getClass()), 1, null);

	assertEquals(Integer.class, fieldAccessor2.getFieldType().getTypeClass());

	assertEquals(b[1], fieldAccessor2.get(b));

	b = fieldAccessor2.set(b, 6);
	assertEquals(b[1], fieldAccessor2.get(b));
}
 
Example #30
Source Project: flink   Author: apache   File: LegacyTypeInfoDataTypeConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType toDataType(TypeInformation<?> typeInfo) {
	// time indicators first as their hashCode/equals is shared with those of regular timestamps
	if (typeInfo instanceof TimeIndicatorTypeInfo) {
		return convertToTimeAttributeType((TimeIndicatorTypeInfo) typeInfo);
	}

	final DataType foundDataType = typeInfoDataTypeMap.get(typeInfo);
	if (foundDataType != null) {
		return foundDataType;
	}

	if (typeInfo instanceof RowTypeInfo) {
		return convertToRowType((RowTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof ObjectArrayTypeInfo) {
		return convertToArrayType(
			typeInfo.getTypeClass(),
			((ObjectArrayTypeInfo) typeInfo).getComponentInfo());
	}

	else if (typeInfo instanceof BasicArrayTypeInfo) {
		return createLegacyType(LogicalTypeRoot.ARRAY, typeInfo);
	}

	else if (typeInfo instanceof MultisetTypeInfo) {
		return convertToMultisetType(((MultisetTypeInfo) typeInfo).getElementTypeInfo());
	}

	else if (typeInfo instanceof MapTypeInfo) {
		return convertToMapType((MapTypeInfo) typeInfo);
	}

	else if (typeInfo instanceof CompositeType) {
		return createLegacyType(LogicalTypeRoot.STRUCTURED_TYPE, typeInfo);
	}

	return createLegacyType(LogicalTypeRoot.RAW, typeInfo);
}