Java Code Examples for org.apache.flink.api.common.typeutils.CompositeType#getFlatFields()

The following examples show how to use org.apache.flink.api.common.typeutils.CompositeType#getFlatFields() . 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   File: PojoTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
@PublicEvolving
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if(!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid POJO field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if(field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for(PojoField pField : fields) {
			if(pField.getTypeInformation() instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>)pField.getTypeInformation();
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields()-1;
			} else {
				result.add(
					new NamedFlatFieldDescriptor(
						pField.getField().getName(),
						offset + keyPosition,
						pField.getTypeInformation()));
			}
			keyPosition++;
		}
		return;
	} else {
		field = matcher.group(1);
	}

	// get field
	int fieldPos = -1;
	TypeInformation<?> fieldType = null;
	for (int i = 0; i < fields.length; i++) {
		if (fields[i].getField().getName().equals(field)) {
			fieldPos = i;
			fieldType = fields[i].getTypeInformation();
			break;
		}
	}
	if (fieldPos == -1) {
		throw new InvalidFieldReferenceException("Unable to find field \""+field+"\" in type "+this+".");
	}
	String tail = matcher.group(3);
	if(tail == null) {
		if(fieldType instanceof CompositeType) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			// add all fields of composite type
			((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
		} else {
			// we found the field to add
			// compute flat field position by adding skipped fields
			int flatFieldPos = offset;
			for(int i=0; i<fieldPos; i++) {
				flatFieldPos += this.getTypeAt(i).getTotalFields();
			}
			result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
		}
	} else {
		if(fieldType instanceof CompositeType<?>) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
		} else {
			throw new InvalidFieldReferenceException("Nested field expression \""+tail+"\" not possible on atomic type "+fieldType+".");
		}
	}
}
 
Example 2
Source Project: Flink-CEPplus   File: RowTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {
	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);

	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException(
			"Invalid tuple field reference \"" + fieldExpression + "\".");
	}

	String field = matcher.group(0);

	if ((field.equals(ExpressionKeys.SELECT_ALL_CHAR)) ||
		(field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA))) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> fType : types) {
			if (fType instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) fType;
				cType.getFlatFields(ExpressionKeys.SELECT_ALL_CHAR, offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, fType));
			}
			keyPosition++;
		}
	} else {
		field = matcher.group(1);

		Matcher intFieldMatcher = PATTERN_INT_FIELD.matcher(field);
		int fieldIndex;
		if (intFieldMatcher.matches()) {
			// field expression is an integer
			fieldIndex = Integer.valueOf(field);
		} else {
			fieldIndex = this.getFieldIndex(field);
		}
		// fetch the field type will throw exception if the index is illegal
		TypeInformation<?> fieldType = this.getTypeAt(fieldIndex);
		// compute the offset,
		for (int i = 0; i < fieldIndex; i++) {
			offset += this.getTypeAt(i).getTotalFields();
		}

		String tail = matcher.group(3);

		if (tail == null) {
			// expression hasn't nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields("*", offset, result);
			} else {
				result.add(new FlatFieldDescriptor(offset, fieldType));
			}
		} else {
			// expression has nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException(
					"Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 3
Source Project: Flink-CEPplus   File: TupleTypeInfoBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid tuple field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if (field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> type : types) {
			if (type instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) type;
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, type));
			}
			keyPosition++;
		}
	} else {
		String fieldStr = matcher.group(1);
		Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);

		if (!fieldMatcher.matches()) {
			throw new RuntimeException("Invalid matcher pattern");
		}

		field = fieldMatcher.group(2);
		int fieldPos = Integer.valueOf(field);

		if (fieldPos >= this.getArity()) {
			throw new InvalidFieldReferenceException("Tuple field expression \"" + fieldStr + "\" out of bounds of " + this.toString() + ".");
		}
		TypeInformation<?> fieldType = this.getTypeAt(fieldPos);
		String tail = matcher.group(5);
		if (tail == null) {
			if (fieldType instanceof CompositeType) {
				// forward offsets
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				// add all fields of composite type
				((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
			} else {
				// we found the field to add
				// compute flat field position by adding skipped fields
				int flatFieldPos = offset;
				for (int i = 0; i < fieldPos; i++) {
					flatFieldPos += this.getTypeAt(i).getTotalFields();
				}
				result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
			}
		} else {
			if (fieldType instanceof CompositeType<?>) {
				// forward offset
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 4
Source Project: Flink-CEPplus   File: Keys.java    License: 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 5
Source Project: flink   File: PojoTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
@PublicEvolving
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if(!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid POJO field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if(field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for(PojoField pField : fields) {
			if(pField.getTypeInformation() instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>)pField.getTypeInformation();
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields()-1;
			} else {
				result.add(
					new NamedFlatFieldDescriptor(
						pField.getField().getName(),
						offset + keyPosition,
						pField.getTypeInformation()));
			}
			keyPosition++;
		}
		return;
	} else {
		field = matcher.group(1);
	}

	// get field
	int fieldPos = -1;
	TypeInformation<?> fieldType = null;
	for (int i = 0; i < fields.length; i++) {
		if (fields[i].getField().getName().equals(field)) {
			fieldPos = i;
			fieldType = fields[i].getTypeInformation();
			break;
		}
	}
	if (fieldPos == -1) {
		throw new InvalidFieldReferenceException("Unable to find field \""+field+"\" in type "+this+".");
	}
	String tail = matcher.group(3);
	if(tail == null) {
		if(fieldType instanceof CompositeType) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			// add all fields of composite type
			((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
		} else {
			// we found the field to add
			// compute flat field position by adding skipped fields
			int flatFieldPos = offset;
			for(int i=0; i<fieldPos; i++) {
				flatFieldPos += this.getTypeAt(i).getTotalFields();
			}
			result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
		}
	} else {
		if(fieldType instanceof CompositeType<?>) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
		} else {
			throw new InvalidFieldReferenceException("Nested field expression \""+tail+"\" not possible on atomic type "+fieldType+".");
		}
	}
}
 
Example 6
Source Project: flink   File: RowTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {
	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);

	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException(
			"Invalid tuple field reference \"" + fieldExpression + "\".");
	}

	String field = matcher.group(0);

	if ((field.equals(ExpressionKeys.SELECT_ALL_CHAR)) ||
		(field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA))) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> fType : types) {
			if (fType instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) fType;
				cType.getFlatFields(ExpressionKeys.SELECT_ALL_CHAR, offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, fType));
			}
			keyPosition++;
		}
	} else {
		field = matcher.group(1);

		Matcher intFieldMatcher = PATTERN_INT_FIELD.matcher(field);
		int fieldIndex;
		if (intFieldMatcher.matches()) {
			// field expression is an integer
			fieldIndex = Integer.valueOf(field);
		} else {
			fieldIndex = this.getFieldIndex(field);
		}
		// fetch the field type will throw exception if the index is illegal
		TypeInformation<?> fieldType = this.getTypeAt(fieldIndex);
		// compute the offset,
		for (int i = 0; i < fieldIndex; i++) {
			offset += this.getTypeAt(i).getTotalFields();
		}

		String tail = matcher.group(3);

		if (tail == null) {
			// expression hasn't nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields("*", offset, result);
			} else {
				result.add(new FlatFieldDescriptor(offset, fieldType));
			}
		} else {
			// expression has nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException(
					"Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 7
Source Project: flink   File: TupleTypeInfoBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid tuple field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if (field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> type : types) {
			if (type instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) type;
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, type));
			}
			keyPosition++;
		}
	} else {
		String fieldStr = matcher.group(1);
		Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);

		if (!fieldMatcher.matches()) {
			throw new RuntimeException("Invalid matcher pattern");
		}

		field = fieldMatcher.group(2);
		int fieldPos = Integer.valueOf(field);

		if (fieldPos >= this.getArity()) {
			throw new InvalidFieldReferenceException("Tuple field expression \"" + fieldStr + "\" out of bounds of " + this.toString() + ".");
		}
		TypeInformation<?> fieldType = this.getTypeAt(fieldPos);
		String tail = matcher.group(5);
		if (tail == null) {
			if (fieldType instanceof CompositeType) {
				// forward offsets
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				// add all fields of composite type
				((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
			} else {
				// we found the field to add
				// compute flat field position by adding skipped fields
				int flatFieldPos = offset;
				for (int i = 0; i < fieldPos; i++) {
					flatFieldPos += this.getTypeAt(i).getTotalFields();
				}
				result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
			}
		} else {
			if (fieldType instanceof CompositeType<?>) {
				// forward offset
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 8
Source Project: flink   File: Keys.java    License: 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 9
Source Project: flink   File: PojoTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
@PublicEvolving
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if(!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid POJO field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if(field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for(PojoField pField : fields) {
			if(pField.getTypeInformation() instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>)pField.getTypeInformation();
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields()-1;
			} else {
				result.add(
					new NamedFlatFieldDescriptor(
						pField.getField().getName(),
						offset + keyPosition,
						pField.getTypeInformation()));
			}
			keyPosition++;
		}
		return;
	} else {
		field = matcher.group(1);
	}

	// get field
	int fieldPos = -1;
	TypeInformation<?> fieldType = null;
	for (int i = 0; i < fields.length; i++) {
		if (fields[i].getField().getName().equals(field)) {
			fieldPos = i;
			fieldType = fields[i].getTypeInformation();
			break;
		}
	}
	if (fieldPos == -1) {
		throw new InvalidFieldReferenceException("Unable to find field \""+field+"\" in type "+this+".");
	}
	String tail = matcher.group(3);
	if(tail == null) {
		if(fieldType instanceof CompositeType) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			// add all fields of composite type
			((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
		} else {
			// we found the field to add
			// compute flat field position by adding skipped fields
			int flatFieldPos = offset;
			for(int i=0; i<fieldPos; i++) {
				flatFieldPos += this.getTypeAt(i).getTotalFields();
			}
			result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
		}
	} else {
		if(fieldType instanceof CompositeType<?>) {
			// forward offset
			for(int i=0; i<fieldPos; i++) {
				offset += this.getTypeAt(i).getTotalFields();
			}
			((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
		} else {
			throw new InvalidFieldReferenceException("Nested field expression \""+tail+"\" not possible on atomic type "+fieldType+".");
		}
	}
}
 
Example 10
Source Project: flink   File: RowTypeInfo.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {
	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);

	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException(
			"Invalid tuple field reference \"" + fieldExpression + "\".");
	}

	String field = matcher.group(0);

	if ((field.equals(ExpressionKeys.SELECT_ALL_CHAR)) ||
		(field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA))) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> fType : types) {
			if (fType instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) fType;
				cType.getFlatFields(ExpressionKeys.SELECT_ALL_CHAR, offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, fType));
			}
			keyPosition++;
		}
	} else {
		field = matcher.group(1);

		Matcher intFieldMatcher = PATTERN_INT_FIELD.matcher(field);
		int fieldIndex;
		if (intFieldMatcher.matches()) {
			// field expression is an integer
			fieldIndex = Integer.valueOf(field);
		} else {
			fieldIndex = this.getFieldIndex(field);
		}
		// fetch the field type will throw exception if the index is illegal
		TypeInformation<?> fieldType = this.getTypeAt(fieldIndex);
		// compute the offset,
		for (int i = 0; i < fieldIndex; i++) {
			offset += this.getTypeAt(i).getTotalFields();
		}

		String tail = matcher.group(3);

		if (tail == null) {
			// expression hasn't nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields("*", offset, result);
			} else {
				result.add(new FlatFieldDescriptor(offset, fieldType));
			}
		} else {
			// expression has nested field
			if (fieldType instanceof CompositeType) {
				((CompositeType) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException(
					"Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 11
Source Project: flink   File: TupleTypeInfoBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void getFlatFields(String fieldExpression, int offset, List<FlatFieldDescriptor> result) {

	Matcher matcher = PATTERN_NESTED_FIELDS_WILDCARD.matcher(fieldExpression);
	if (!matcher.matches()) {
		throw new InvalidFieldReferenceException("Invalid tuple field reference \""+fieldExpression+"\".");
	}

	String field = matcher.group(0);
	if (field.equals(ExpressionKeys.SELECT_ALL_CHAR) || field.equals(ExpressionKeys.SELECT_ALL_CHAR_SCALA)) {
		// handle select all
		int keyPosition = 0;
		for (TypeInformation<?> type : types) {
			if (type instanceof CompositeType) {
				CompositeType<?> cType = (CompositeType<?>) type;
				cType.getFlatFields(String.valueOf(ExpressionKeys.SELECT_ALL_CHAR), offset + keyPosition, result);
				keyPosition += cType.getTotalFields() - 1;
			} else {
				result.add(new FlatFieldDescriptor(offset + keyPosition, type));
			}
			keyPosition++;
		}
	} else {
		String fieldStr = matcher.group(1);
		Matcher fieldMatcher = PATTERN_FIELD.matcher(fieldStr);

		if (!fieldMatcher.matches()) {
			throw new RuntimeException("Invalid matcher pattern");
		}

		field = fieldMatcher.group(2);
		int fieldPos = Integer.valueOf(field);

		if (fieldPos >= this.getArity()) {
			throw new InvalidFieldReferenceException("Tuple field expression \"" + fieldStr + "\" out of bounds of " + this.toString() + ".");
		}
		TypeInformation<?> fieldType = this.getTypeAt(fieldPos);
		String tail = matcher.group(5);
		if (tail == null) {
			if (fieldType instanceof CompositeType) {
				// forward offsets
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				// add all fields of composite type
				((CompositeType<?>) fieldType).getFlatFields("*", offset, result);
			} else {
				// we found the field to add
				// compute flat field position by adding skipped fields
				int flatFieldPos = offset;
				for (int i = 0; i < fieldPos; i++) {
					flatFieldPos += this.getTypeAt(i).getTotalFields();
				}
				result.add(new FlatFieldDescriptor(flatFieldPos, fieldType));
			}
		} else {
			if (fieldType instanceof CompositeType<?>) {
				// forward offset
				for (int i = 0; i < fieldPos; i++) {
					offset += this.getTypeAt(i).getTotalFields();
				}
				((CompositeType<?>) fieldType).getFlatFields(tail, offset, result);
			} else {
				throw new InvalidFieldReferenceException("Nested field expression \"" + tail + "\" not possible on atomic type " + fieldType + ".");
			}
		}
	}
}
 
Example 12
Source Project: flink   File: Keys.java    License: 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);
	}
}