
org.apache.flink.api.java.operators.ProjectOperator Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.api.java.operators;
import java.util.Arrays;
import org.apache.flink.annotation.Internal;
import org.apache.flink.annotation.Public;
import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.common.InvalidProgramException;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.operators.Operator;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeutils.CompositeType;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.functions.SemanticPropUtil;
import org.apache.flink.api.java.operators.translation.PlanProjectOperator;
import org.apache.flink.api.java.typeutils.TupleTypeInfo;
//CHECKSTYLE.OFF: AvoidStarImport - Needed for TupleGenerator
import org.apache.flink.api.java.tuple.*;
//CHECKSTYLE.ON: AvoidStarImport
import com.google.common.base.Preconditions;
/**
* This operator represents the application of a projection operation on a data set, and the
* result data set produced by the function.
*
* @param The type of the data set projected by the operator.
* @param The type of data set that is the result of the projection.
*/
@Public
public class ProjectOperator
extends SingleInputOperator> {
protected final int[] fields;
public ProjectOperator(DataSet input, int[] fields, TupleTypeInfo returnType) {
super(input, returnType);
this.fields = fields;
}
@Override
protected org.apache.flink.api.common.operators.base.MapOperatorBase> translateToDataFlow(Operator input) {
String name = getName() != null ? getName() : "Projection " + Arrays.toString(fields);
// create operator
PlanProjectOperator ppo = new PlanProjectOperator(fields, name, getInputType(), getResultType(), context.getConfig());
// set input
ppo.setInput(input);
// set parallelism
ppo.setParallelism(this.getParallelism());
ppo.setSemanticProperties(SemanticPropUtil.createProjectionPropertiesSingle(fields, (CompositeType>) getInputType()));
return ppo;
}
/**
* Deprecated method only kept for compatibility.
*/
@SuppressWarnings("unchecked")
@Deprecated
@PublicEvolving
public ProjectOperator types(Class>... types) {
TupleTypeInfo typeInfo = (TupleTypeInfo)this.getResultType();
if(types.length != typeInfo.getArity()) {
throw new InvalidProgramException("Provided types do not match projection.");
}
for (int i=0; i typeClass = types[i];
if (!typeClass.equals(typeInfo.getTypeAt(i).getTypeClass())) {
throw new InvalidProgramException("Provided type "+typeClass.getSimpleName()+" at position "+i+" does not match projection");
}
}
return (ProjectOperator) this;
}
@Internal
public static class Projection {
private final DataSet ds;
private int[] fieldIndexes;
public Projection(DataSet ds, int[] fieldIndexes) {
if(!(ds.getType() instanceof TupleTypeInfo)) {
throw new UnsupportedOperationException("project() can only be applied to DataSets of Tuples.");
}
if(fieldIndexes.length == 0) {
throw new IllegalArgumentException("project() needs to select at least one (1) field.");
} else if(fieldIndexes.length > Tuple.MAX_ARITY - 1) {
throw new IllegalArgumentException(
"project() may select only up to (" + (Tuple.MAX_ARITY - 1) + ") fields.");
}
int maxFieldIndex = ds.getType().getArity();
for (int fieldIndexe : fieldIndexes) {
Preconditions.checkElementIndex(fieldIndexe, maxFieldIndex);
}
this.ds = ds;
this.fieldIndexes = fieldIndexes;
}
// --------------------------------------------------------------------------------------------
// The following lines are generated.
// --------------------------------------------------------------------------------------------
// BEGIN_OF_TUPLE_DEPENDENT_CODE
// GENERATED FROM org.apache.flink.api.java.tuple.TupleGenerator.
/**
* Chooses a projectTupleX according to the length of
* {@link org.apache.flink.api.java.operators.ProjectOperator.Projection#fieldIndexes}
*
* @return The projected DataSet.
*
* @see org.apache.flink.api.java.operators.ProjectOperator.Projection
*/
@SuppressWarnings("unchecked")
public ProjectOperator projectTupleX() {
ProjectOperator projOperator;
switch (fieldIndexes.length) {
case 1: projOperator = (ProjectOperator) projectTuple1(); break;
case 2: projOperator = (ProjectOperator) projectTuple2(); break;
case 3: projOperator = (ProjectOperator) projectTuple3(); break;
case 4: projOperator = (ProjectOperator) projectTuple4(); break;
case 5: projOperator = (ProjectOperator) projectTuple5(); break;
case 6: projOperator = (ProjectOperator) projectTuple6(); break;
case 7: projOperator = (ProjectOperator) projectTuple7(); break;
case 8: projOperator = (ProjectOperator) projectTuple8(); break;
case 9: projOperator = (ProjectOperator) projectTuple9(); break;
case 10: projOperator = (ProjectOperator) projectTuple10(); break;
case 11: projOperator = (ProjectOperator) projectTuple11(); break;
case 12: projOperator = (ProjectOperator) projectTuple12(); break;
case 13: projOperator = (ProjectOperator) projectTuple13(); break;
case 14: projOperator = (ProjectOperator) projectTuple14(); break;
case 15: projOperator = (ProjectOperator) projectTuple15(); break;
case 16: projOperator = (ProjectOperator) projectTuple16(); break;
case 17: projOperator = (ProjectOperator) projectTuple17(); break;
case 18: projOperator = (ProjectOperator) projectTuple18(); break;
case 19: projOperator = (ProjectOperator) projectTuple19(); break;
case 20: projOperator = (ProjectOperator) projectTuple20(); break;
case 21: projOperator = (ProjectOperator) projectTuple21(); break;
case 22: projOperator = (ProjectOperator) projectTuple22(); break;
case 23: projOperator = (ProjectOperator) projectTuple23(); break;
case 24: projOperator = (ProjectOperator) projectTuple24(); break;
case 25: projOperator = (ProjectOperator) projectTuple25(); break;
default: throw new IllegalStateException("Excessive arity in tuple.");
}
return projOperator;
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple1() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple2() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple3() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple4() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple5() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple6() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple7() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple8() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple9() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple10() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple11() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple12() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple13() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple14() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple15() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple16() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple17() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple18() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple19() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple20() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple21() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple22() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple23() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple24() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
/**
* Projects a {@link Tuple} {@link DataSet} to the previously selected fields.
*
* @return The projected DataSet.
*
* @see Tuple
* @see DataSet
*/
public ProjectOperator> projectTuple25() {
TypeInformation>[] fTypes = extractFieldTypes(fieldIndexes, ds.getType());
TupleTypeInfo> tType = new TupleTypeInfo>(fTypes);
return new ProjectOperator>(this.ds, this.fieldIndexes, tType);
}
// END_OF_TUPLE_DEPENDENT_CODE
// -----------------------------------------------------------------------------------------
private TypeInformation>[] extractFieldTypes(int[] fields, TypeInformation> inType) {
TupleTypeInfo> inTupleType = (TupleTypeInfo>) inType;
TypeInformation>[] fieldTypes = new TypeInformation[fields.length];
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy