Java Code Examples for org.apache.flink.api.common.typeinfo.TypeInformation#getTotalFields()

The following examples show how to use org.apache.flink.api.common.typeinfo.TypeInformation#getTotalFields() . 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: AggregateOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private void validateAlias(
	List<String> aliases,
	TableAggregateFunctionDefinition aggFunctionDefinition) {

	TypeInformation<?> resultType = aggFunctionDefinition.getResultTypeInfo();

	int callArity = resultType.getTotalFields();
	int aliasesSize = aliases.size();

	if (aliasesSize > 0 && aliasesSize != callArity) {
		throw new ValidationException(String.format(
			"List of column aliases must have same degree as table; " +
				"the returned table of function '%s' has " +
				"%d columns, whereas alias list has %d columns",
			aggFunctionDefinition,
			callArity,
			aliasesSize));
	}
}
 
Example 2
Source File: AggregateOperationFactory.java    From flink with Apache License 2.0 6 votes vote down vote up
private void validateAlias(
	List<String> aliases,
	TableAggregateFunctionDefinition aggFunctionDefinition) {

	TypeInformation<?> resultType = aggFunctionDefinition.getResultTypeInfo();

	int callArity = resultType.getTotalFields();
	int aliasesSize = aliases.size();

	if (aliasesSize > 0 && aliasesSize != callArity) {
		throw new ValidationException(String.format(
			"List of column aliases must have same degree as table; " +
				"the returned table of function '%s' has " +
				"%d columns, whereas alias list has %d columns",
			aggFunctionDefinition,
			callArity,
			aliasesSize));
	}
}
 
Example 3
Source File: CalculatedTableFactory.java    From flink with Apache License 2.0 5 votes vote down vote up
private CalculatedQueryOperation<?> createFunctionCall(
		TableFunctionDefinition tableFunctionDefinition,
		List<String> aliases,
		List<ResolvedExpression> parameters) {
	TypeInformation<?> resultType = tableFunctionDefinition.getResultType();

	int callArity = resultType.getTotalFields();
	int aliasesSize = aliases.size();

	String[] fieldNames;
	if (aliasesSize == 0) {
		fieldNames = FieldInfoUtils.getFieldNames(resultType, Arrays.asList(leftTableFieldNames));
	} else if (aliasesSize != callArity) {
		throw new ValidationException(String.format(
			"List of column aliases must have same degree as table; " +
				"the returned table of function '%s' has " +
				"%d columns, whereas alias list has %d columns",
			tableFunctionDefinition.toString(),
			callArity,
			aliasesSize));
	} else {
		fieldNames = aliases.toArray(new String[aliasesSize]);
	}

	TypeInformation<?>[] fieldTypes = FieldInfoUtils.getFieldTypes(resultType);

	return new CalculatedQueryOperation(
		tableFunctionDefinition.getTableFunction(),
		parameters,
		tableFunctionDefinition.getResultType(),
		new TableSchema(fieldNames, fieldTypes));
}
 
Example 4
Source File: SemanticPropUtil.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
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 5
Source File: Keys.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * 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 6
Source File: CompositeType.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Generic implementation of the comparator creation. Composite types are supplying the infrastructure
 * to create the actual comparators
 * @return The comparator
 */
@PublicEvolving
public TypeComparator<T> createComparator(int[] logicalKeyFields, boolean[] orders, int logicalFieldOffset, ExecutionConfig config) {

	TypeComparatorBuilder<T> builder = createTypeComparatorBuilder();

	builder.initializeTypeComparatorBuilder(logicalKeyFields.length);

	for (int logicalKeyFieldIndex = 0; logicalKeyFieldIndex < logicalKeyFields.length; logicalKeyFieldIndex++) {
		int logicalKeyField = logicalKeyFields[logicalKeyFieldIndex];
		int logicalField = logicalFieldOffset; // this is the global/logical field number
		boolean comparatorAdded = false;

		for (int localFieldId = 0; localFieldId < this.getArity() && logicalField <= logicalKeyField && !comparatorAdded; localFieldId++) {
			TypeInformation<?> localFieldType = this.getTypeAt(localFieldId);
			
			if (localFieldType instanceof AtomicType && logicalField == logicalKeyField) {
				// we found an atomic key --> create comparator
				builder.addComparatorField(
					localFieldId,
					((AtomicType<?>) localFieldType).createComparator(
						orders[logicalKeyFieldIndex],
						config));

				comparatorAdded = true;
			}
			// must be composite type and check that the logicalKeyField is within the bounds
			// of the composite type's logical fields
			else if (localFieldType instanceof CompositeType &&
				logicalField <= logicalKeyField &&
				logicalKeyField <= logicalField + (localFieldType.getTotalFields() - 1)) {
				// we found a compositeType that is containing the logicalKeyField we are looking for --> create comparator
				builder.addComparatorField(
					localFieldId,
					((CompositeType<?>) localFieldType).createComparator(
						new int[]{logicalKeyField},
						new boolean[]{orders[logicalKeyFieldIndex]},
						logicalField,
						config)
				);

				comparatorAdded = true;
			}

			if (localFieldType instanceof CompositeType) {
				// we need to subtract 1 because we are not accounting for the local field (not accessible for the user)
				logicalField += localFieldType.getTotalFields() - 1;
			}
			
			logicalField++;
		}

		if (!comparatorAdded) {
			throw new IllegalArgumentException("Could not add a comparator for the logical" +
				"key field index " + logicalKeyFieldIndex + ".");
		}
	}

	return builder.createTypeComparator(config);
}
 
Example 7
Source File: SemanticPropUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
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 8
Source File: SemanticPropUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
public static DualInputSemanticProperties createProjectionPropertiesDual(
	int[] fields, boolean[] isFromFirst, TypeInformation<?> inType1, TypeInformation<?> inType2) {
	DualInputSemanticProperties dsp = new DualInputSemanticProperties();

	int[] sourceOffsets1;
	if (inType1 instanceof TupleTypeInfo<?>) {
		sourceOffsets1 = new int[inType1.getArity()];
		sourceOffsets1[0] = 0;
		for (int i = 1; i < inType1.getArity(); i++) {
			sourceOffsets1[i] = ((TupleTypeInfo<?>) inType1).getTypeAt(i - 1).getTotalFields() + sourceOffsets1[i - 1];
		}
	} else {
		sourceOffsets1 = new int[]{0};
	}

	int[] sourceOffsets2;
	if (inType2 instanceof TupleTypeInfo<?>) {
		sourceOffsets2 = new int[inType2.getArity()];
		sourceOffsets2[0] = 0;
		for (int i = 1; i < inType2.getArity(); i++) {
			sourceOffsets2[i] = ((TupleTypeInfo<?>) inType2).getTypeAt(i - 1).getTotalFields() + sourceOffsets2[i - 1];
		}
	} else {
		sourceOffsets2 = new int[]{0};
	}

	int targetOffset = 0;
	for (int i = 0; i < fields.length; i++) {
		int sourceOffset;
		int numFieldsToCopy;
		int input;
		if (isFromFirst[i]) {
			input = 0;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType1.getTotalFields();
			} else {
				sourceOffset = sourceOffsets1[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType1).getTypeAt(fields[i]).getTotalFields();
			}
		} else {
			input = 1;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType2.getTotalFields();
			} else {
				sourceOffset = sourceOffsets2[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType2).getTypeAt(fields[i]).getTotalFields();
			}
		}

		for (int j = 0; j < numFieldsToCopy; j++) {
			dsp.addForwardedField(input, sourceOffset + j, targetOffset + j);
		}
		targetOffset += numFieldsToCopy;
	}

	return dsp;
}
 
Example 9
Source File: Keys.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * 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 10
Source File: CompositeType.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Generic implementation of the comparator creation. Composite types are supplying the infrastructure
 * to create the actual comparators
 * @return The comparator
 */
@PublicEvolving
public TypeComparator<T> createComparator(int[] logicalKeyFields, boolean[] orders, int logicalFieldOffset, ExecutionConfig config) {

	TypeComparatorBuilder<T> builder = createTypeComparatorBuilder();

	builder.initializeTypeComparatorBuilder(logicalKeyFields.length);

	for (int logicalKeyFieldIndex = 0; logicalKeyFieldIndex < logicalKeyFields.length; logicalKeyFieldIndex++) {
		int logicalKeyField = logicalKeyFields[logicalKeyFieldIndex];
		int logicalField = logicalFieldOffset; // this is the global/logical field number
		boolean comparatorAdded = false;

		for (int localFieldId = 0; localFieldId < this.getArity() && logicalField <= logicalKeyField && !comparatorAdded; localFieldId++) {
			TypeInformation<?> localFieldType = this.getTypeAt(localFieldId);
			
			if (localFieldType instanceof AtomicType && logicalField == logicalKeyField) {
				// we found an atomic key --> create comparator
				builder.addComparatorField(
					localFieldId,
					((AtomicType<?>) localFieldType).createComparator(
						orders[logicalKeyFieldIndex],
						config));

				comparatorAdded = true;
			}
			// must be composite type and check that the logicalKeyField is within the bounds
			// of the composite type's logical fields
			else if (localFieldType instanceof CompositeType &&
				logicalField <= logicalKeyField &&
				logicalKeyField <= logicalField + (localFieldType.getTotalFields() - 1)) {
				// we found a compositeType that is containing the logicalKeyField we are looking for --> create comparator
				builder.addComparatorField(
					localFieldId,
					((CompositeType<?>) localFieldType).createComparator(
						new int[]{logicalKeyField},
						new boolean[]{orders[logicalKeyFieldIndex]},
						logicalField,
						config)
				);

				comparatorAdded = true;
			}

			if (localFieldType instanceof CompositeType) {
				// we need to subtract 1 because we are not accounting for the local field (not accessible for the user)
				logicalField += localFieldType.getTotalFields() - 1;
			}
			
			logicalField++;
		}

		if (!comparatorAdded) {
			throw new IllegalArgumentException("Could not add a comparator for the logical" +
				"key field index " + logicalKeyFieldIndex + ".");
		}
	}

	return builder.createTypeComparator(config);
}
 
Example 11
Source File: SemanticPropUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
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: SemanticPropUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
public static DualInputSemanticProperties createProjectionPropertiesDual(
	int[] fields, boolean[] isFromFirst, TypeInformation<?> inType1, TypeInformation<?> inType2) {
	DualInputSemanticProperties dsp = new DualInputSemanticProperties();

	int[] sourceOffsets1;
	if (inType1 instanceof TupleTypeInfo<?>) {
		sourceOffsets1 = new int[inType1.getArity()];
		sourceOffsets1[0] = 0;
		for (int i = 1; i < inType1.getArity(); i++) {
			sourceOffsets1[i] = ((TupleTypeInfo<?>) inType1).getTypeAt(i - 1).getTotalFields() + sourceOffsets1[i - 1];
		}
	} else {
		sourceOffsets1 = new int[]{0};
	}

	int[] sourceOffsets2;
	if (inType2 instanceof TupleTypeInfo<?>) {
		sourceOffsets2 = new int[inType2.getArity()];
		sourceOffsets2[0] = 0;
		for (int i = 1; i < inType2.getArity(); i++) {
			sourceOffsets2[i] = ((TupleTypeInfo<?>) inType2).getTypeAt(i - 1).getTotalFields() + sourceOffsets2[i - 1];
		}
	} else {
		sourceOffsets2 = new int[]{0};
	}

	int targetOffset = 0;
	for (int i = 0; i < fields.length; i++) {
		int sourceOffset;
		int numFieldsToCopy;
		int input;
		if (isFromFirst[i]) {
			input = 0;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType1.getTotalFields();
			} else {
				sourceOffset = sourceOffsets1[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType1).getTypeAt(fields[i]).getTotalFields();
			}
		} else {
			input = 1;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType2.getTotalFields();
			} else {
				sourceOffset = sourceOffsets2[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType2).getTypeAt(fields[i]).getTotalFields();
			}
		}

		for (int j = 0; j < numFieldsToCopy; j++) {
			dsp.addForwardedField(input, sourceOffset + j, targetOffset + j);
		}
		targetOffset += numFieldsToCopy;
	}

	return dsp;
}
 
Example 13
Source File: Keys.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * 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 14
Source File: CompositeType.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Generic implementation of the comparator creation. Composite types are supplying the infrastructure
 * to create the actual comparators
 * @return The comparator
 */
@PublicEvolving
public TypeComparator<T> createComparator(int[] logicalKeyFields, boolean[] orders, int logicalFieldOffset, ExecutionConfig config) {

	TypeComparatorBuilder<T> builder = createTypeComparatorBuilder();

	builder.initializeTypeComparatorBuilder(logicalKeyFields.length);

	for (int logicalKeyFieldIndex = 0; logicalKeyFieldIndex < logicalKeyFields.length; logicalKeyFieldIndex++) {
		int logicalKeyField = logicalKeyFields[logicalKeyFieldIndex];
		int logicalField = logicalFieldOffset; // this is the global/logical field number
		boolean comparatorAdded = false;

		for (int localFieldId = 0; localFieldId < this.getArity() && logicalField <= logicalKeyField && !comparatorAdded; localFieldId++) {
			TypeInformation<?> localFieldType = this.getTypeAt(localFieldId);
			
			if (localFieldType instanceof AtomicType && logicalField == logicalKeyField) {
				// we found an atomic key --> create comparator
				builder.addComparatorField(
					localFieldId,
					((AtomicType<?>) localFieldType).createComparator(
						orders[logicalKeyFieldIndex],
						config));

				comparatorAdded = true;
			}
			// must be composite type and check that the logicalKeyField is within the bounds
			// of the composite type's logical fields
			else if (localFieldType instanceof CompositeType &&
				logicalField <= logicalKeyField &&
				logicalKeyField <= logicalField + (localFieldType.getTotalFields() - 1)) {
				// we found a compositeType that is containing the logicalKeyField we are looking for --> create comparator
				builder.addComparatorField(
					localFieldId,
					((CompositeType<?>) localFieldType).createComparator(
						new int[]{logicalKeyField},
						new boolean[]{orders[logicalKeyFieldIndex]},
						logicalField,
						config)
				);

				comparatorAdded = true;
			}

			if (localFieldType instanceof CompositeType) {
				// we need to subtract 1 because we are not accounting for the local field (not accessible for the user)
				logicalField += localFieldType.getTotalFields() - 1;
			}
			
			logicalField++;
		}

		if (!comparatorAdded) {
			throw new IllegalArgumentException("Could not add a comparator for the logical" +
				"key field index " + logicalKeyFieldIndex + ".");
		}
	}

	return builder.createTypeComparator(config);
}
 
Example 15
Source File: SemanticPropUtil.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public static DualInputSemanticProperties createProjectionPropertiesDual(
	int[] fields, boolean[] isFromFirst, TypeInformation<?> inType1, TypeInformation<?> inType2) {
	DualInputSemanticProperties dsp = new DualInputSemanticProperties();

	int[] sourceOffsets1;
	if (inType1 instanceof TupleTypeInfo<?>) {
		sourceOffsets1 = new int[inType1.getArity()];
		sourceOffsets1[0] = 0;
		for (int i = 1; i < inType1.getArity(); i++) {
			sourceOffsets1[i] = ((TupleTypeInfo<?>) inType1).getTypeAt(i - 1).getTotalFields() + sourceOffsets1[i - 1];
		}
	} else {
		sourceOffsets1 = new int[]{0};
	}

	int[] sourceOffsets2;
	if (inType2 instanceof TupleTypeInfo<?>) {
		sourceOffsets2 = new int[inType2.getArity()];
		sourceOffsets2[0] = 0;
		for (int i = 1; i < inType2.getArity(); i++) {
			sourceOffsets2[i] = ((TupleTypeInfo<?>) inType2).getTypeAt(i - 1).getTotalFields() + sourceOffsets2[i - 1];
		}
	} else {
		sourceOffsets2 = new int[]{0};
	}

	int targetOffset = 0;
	for (int i = 0; i < fields.length; i++) {
		int sourceOffset;
		int numFieldsToCopy;
		int input;
		if (isFromFirst[i]) {
			input = 0;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType1.getTotalFields();
			} else {
				sourceOffset = sourceOffsets1[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType1).getTypeAt(fields[i]).getTotalFields();
			}
		} else {
			input = 1;
			if (fields[i] == -1) {
				sourceOffset = 0;
				numFieldsToCopy = inType2.getTotalFields();
			} else {
				sourceOffset = sourceOffsets2[fields[i]];
				numFieldsToCopy = ((TupleTypeInfo<?>) inType2).getTypeAt(fields[i]).getTotalFields();
			}
		}

		for (int j = 0; j < numFieldsToCopy; j++) {
			dsp.addForwardedField(input, sourceOffset + j, targetOffset + j);
		}
		targetOffset += numFieldsToCopy;
	}

	return dsp;
}
 
Example 16
Source File: TupleTypeInfoBase.java    From flink with Apache License 2.0 3 votes vote down vote up
public TupleTypeInfoBase(Class<T> tupleType, TypeInformation<?>... types) {
	super(tupleType);

	this.types = checkNotNull(types);

	int fieldCounter = 0;

	for(TypeInformation<?> type : types) {
		fieldCounter += type.getTotalFields();
	}

	totalFields = fieldCounter;
}
 
Example 17
Source File: TupleTypeInfoBase.java    From flink with Apache License 2.0 3 votes vote down vote up
public TupleTypeInfoBase(Class<T> tupleType, TypeInformation<?>... types) {
	super(tupleType);

	this.types = checkNotNull(types);

	int fieldCounter = 0;

	for(TypeInformation<?> type : types) {
		fieldCounter += type.getTotalFields();
	}

	totalFields = fieldCounter;
}
 
Example 18
Source File: TupleTypeInfoBase.java    From Flink-CEPplus with Apache License 2.0 3 votes vote down vote up
public TupleTypeInfoBase(Class<T> tupleType, TypeInformation<?>... types) {
	super(tupleType);

	this.types = checkNotNull(types);

	int fieldCounter = 0;

	for(TypeInformation<?> type : types) {
		fieldCounter += type.getTotalFields();
	}

	totalFields = fieldCounter;
}