private static Pipe addFilter( RelOptCluster cluster, RexProgram program, Pipe pipe )
{
final Fields incomingFields = createTypedFields( cluster, program.getInputRowType(), false );
BlockBuilder statements = new BlockBuilder();
Expression condition = RexToLixTranslator.translateCondition(
program,
(JavaTypeFactory) cluster.getTypeFactory(),
statements,
new RexToLixTranslator.InputGetter()
{
public Expression field( BlockBuilder list, int index )
{
return Expressions.parameter( incomingFields.getTypeClass( index ), incomingFields.get( index ).toString() );
}
} );
// if condition is constant and true, we don't need an expression filter to keep it around
boolean keepsAllRecords = condition instanceof ConstantExpression && Boolean.TRUE.equals( ( (ConstantExpression) condition ).value );
if( keepsAllRecords )
return pipe;
// create a filter to remove records that don't meet the expression
Expression nullToFalse = Expressions.call( Functions.class, "falseIfNull", condition );
Expression not = Expressions.not( nullToFalse ); // matches #isRemove semantics in Filter
statements.add( Expressions.return_( null, not ) );
BlockStatement block = statements.toBlock();
String expression = Expressions.toString( block );