use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class TableScanNode method createEnumerable.
private static TableScanNode createEnumerable(Compiler compiler, TableScan rel, Enumerable<Row> enumerable, final ImmutableIntList acceptedProjects, List<RexNode> rejectedFilters, final ImmutableIntList rejectedProjects) {
if (!rejectedFilters.isEmpty()) {
final RexNode filter = RexUtil.composeConjunction(rel.getCluster().getRexBuilder(), rejectedFilters, false);
assert filter != null;
// Re-map filter for the projects that have been applied already
final RexNode filter2;
final RelDataType inputRowType;
if (acceptedProjects == null) {
filter2 = filter;
inputRowType = rel.getRowType();
} else {
final Mapping mapping = Mappings.target(acceptedProjects, rel.getTable().getRowType().getFieldCount());
filter2 = RexUtil.apply(mapping, filter);
final RelDataTypeFactory.Builder builder = rel.getCluster().getTypeFactory().builder();
final List<RelDataTypeField> fieldList = rel.getTable().getRowType().getFieldList();
for (int acceptedProject : acceptedProjects) {
builder.add(fieldList.get(acceptedProject));
}
inputRowType = builder.build();
}
final Scalar condition = compiler.compile(ImmutableList.of(filter2), inputRowType);
final Context context = compiler.createContext();
enumerable = enumerable.where(new Predicate1<Row>() {
@Override
public boolean apply(Row row) {
context.values = row.getValues();
Boolean b = (Boolean) condition.execute(context);
return b != null && b;
}
});
}
if (rejectedProjects != null) {
enumerable = enumerable.select(new Function1<Row, Row>() {
final Object[] values = new Object[rejectedProjects.size()];
@Override
public Row apply(Row row) {
final Object[] inValues = row.getValues();
for (int i = 0; i < rejectedProjects.size(); i++) {
values[i] = inValues[rejectedProjects.get(i)];
}
return Row.asCopy(values);
}
});
}
return new TableScanNode(compiler, rel, enumerable);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class JaninoRexCompiler method compile.
public Scalar compile(List<RexNode> nodes, RelDataType inputRowType) {
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
for (RexNode node : nodes) {
programBuilder.addProject(node, null);
}
final RexProgram program = programBuilder.getProgram();
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression context_ = Expressions.parameter(Context.class, "context");
final ParameterExpression outputValues_ = Expressions.parameter(Object[].class, "outputValues");
final JavaTypeFactoryImpl javaTypeFactory = new JavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
// public void execute(Context, Object[] outputValues)
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.<Expression, PhysType>of(Expressions.field(context_, BuiltInMethod.CONTEXT_VALUES.field), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final Function1<String, RexToLixTranslator.InputGetter> correlates = new Function1<String, RexToLixTranslator.InputGetter>() {
public RexToLixTranslator.InputGetter apply(String a0) {
throw new UnsupportedOperationException();
}
};
final Expression root = Expressions.field(context_, BuiltInMethod.CONTEXT_ROOT.field);
final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder, null, root, inputGetter, correlates);
for (int i = 0; i < list.size(); i++) {
builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues_, Expressions.constant(i)), list.get(i))));
}
return baz(context_, outputValues_, builder.toBlock());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class RelMdColumnUniqueness method simplyProjects.
private boolean simplyProjects(RelNode rel, ImmutableBitSet columns) {
if (!(rel instanceof Project)) {
return false;
}
Project project = (Project) rel;
final List<RexNode> projects = project.getProjects();
for (int column : columns) {
if (column >= projects.size()) {
return false;
}
if (!(projects.get(column) instanceof RexInputRef)) {
return false;
}
final RexInputRef ref = (RexInputRef) projects.get(column);
if (ref.getIndex() != column) {
return false;
}
}
return true;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class RelMdColumnUniqueness method areColumnsUnique.
public Boolean areColumnsUnique(Project rel, RelMetadataQuery mq, ImmutableBitSet columns, boolean ignoreNulls) {
// LogicalProject maps a set of rows to a different set;
// Without knowledge of the mapping function(whether it
// preserves uniqueness), it is only safe to derive uniqueness
// info from the child of a project when the mapping is f(a) => a.
//
// Also need to map the input column set to the corresponding child
// references
List<RexNode> projExprs = rel.getProjects();
ImmutableBitSet.Builder childColumns = ImmutableBitSet.builder();
for (int bit : columns) {
RexNode projExpr = projExprs.get(bit);
if (projExpr instanceof RexInputRef) {
childColumns.set(((RexInputRef) projExpr).getIndex());
} else if (projExpr instanceof RexCall && ignoreNulls) {
// If the expression is a cast such that the types are the same
// except for the nullability, then if we're ignoring nulls,
// it doesn't matter whether the underlying column reference
// is nullable. Check that the types are the same by making a
// nullable copy of both types and then comparing them.
RexCall call = (RexCall) projExpr;
if (call.getOperator() != SqlStdOperatorTable.CAST) {
continue;
}
RexNode castOperand = call.getOperands().get(0);
if (!(castOperand instanceof RexInputRef)) {
continue;
}
RelDataTypeFactory typeFactory = rel.getCluster().getTypeFactory();
RelDataType castType = typeFactory.createTypeWithNullability(projExpr.getType(), true);
RelDataType origType = typeFactory.createTypeWithNullability(castOperand.getType(), true);
if (castType.equals(origType)) {
childColumns.set(((RexInputRef) castOperand).getIndex());
}
} else {
// projection, then skip it.
continue;
}
}
// If no columns can affect uniqueness, then return unknown
if (childColumns.cardinality() == 0) {
return null;
}
return mq.areColumnsUnique(rel.getInput(), childColumns.build(), ignoreNulls);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexNode in project calcite by apache.
the class RelMdDistinctRowCount method getDistinctRowCount.
public Double getDistinctRowCount(SemiJoin rel, RelMetadataQuery mq, ImmutableBitSet groupKey, RexNode predicate) {
if (predicate == null || predicate.isAlwaysTrue()) {
if (groupKey.isEmpty()) {
return 1D;
}
}
// create a RexNode representing the selectivity of the
// semijoin filter and pass it to getDistinctRowCount
RexNode newPred = RelMdUtil.makeSemiJoinSelectivityRexNode(mq, rel);
if (predicate != null) {
RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
newPred = rexBuilder.makeCall(SqlStdOperatorTable.AND, newPred, predicate);
}
return mq.getDistinctRowCount(rel.getLeft(), groupKey, newPred);
}
Aggregations