Java Code Examples for org.apache.calcite.util.ImmutableBitSet#intersect()

The following examples show how to use org.apache.calcite.util.ImmutableBitSet#intersect() . 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: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the bit-set to the column ordinals in the source for columns that
 * intersect in the target.
 *
 * @param sourceRowType The source upon which to ordinate the bit set.
 * @param indexToField  The map of ordinals to target fields.
 */
public static ImmutableBitSet getOrdinalBitSet(
    RelDataType sourceRowType,
    Map<Integer, RelDataTypeField> indexToField) {
  ImmutableBitSet source = ImmutableBitSet.of(
      Lists.transform(sourceRowType.getFieldList(),
          RelDataTypeField::getIndex));
  ImmutableBitSet target =
      ImmutableBitSet.of(indexToField.keySet());
  return source.intersect(target);
}
 
Example 2
Source File: SqlValidatorUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the bit-set to the column ordinals in the source for columns that
 * intersect in the target.
 *
 * @param sourceRowType The source upon which to ordinate the bit set.
 * @param indexToField  The map of ordinals to target fields.
 */
public static ImmutableBitSet getOrdinalBitSet(
    RelDataType sourceRowType,
    Map<Integer, RelDataTypeField> indexToField) {
  ImmutableBitSet source = ImmutableBitSet.of(
      Lists.transform(sourceRowType.getFieldList(),
          RelDataTypeField::getIndex));
  ImmutableBitSet target =
      ImmutableBitSet.of(indexToField.keySet());
  return source.intersect(target);
}
 
Example 3
Source File: RelMdPredicates.java    From Bats with Apache License 2.0 4 votes vote down vote up
/** Converts a predicate on a particular set of columns into a predicate on
 * a subset of those columns, weakening if necessary.
 *
 * <p>If not possible to simplify, returns {@code true}, which is the weakest
 * possible predicate.
 *
 * <p>Examples:<ol>
 * <li>The predicate {@code $7 = $9} on columns [7]
 *     becomes {@code $7 is not null}
 * <li>The predicate {@code $7 = $9 + $11} on columns [7, 9]
 *     becomes {@code $7 is not null or $9 is not null}
 * <li>The predicate {@code $7 = $9 and $9 = 5} on columns [7] becomes
 *   {@code $7 = 5}
 * <li>The predicate
 *   {@code $7 = $9 and ($9 = $1 or $9 = $2) and $1 > 3 and $2 > 10}
 *   on columns [7] becomes {@code $7 > 3}
 * </ol>
 *
 * <p>We currently only handle examples 1 and 2.
 *
 * @param rexBuilder Rex builder
 * @param input Input relational expression
 * @param r Predicate expression
 * @param columnsMapped Columns which the final predicate can reference
 * @return Predicate expression narrowed to reference only certain columns
 */
private RexNode projectPredicate(final RexBuilder rexBuilder, RelNode input,
    RexNode r, ImmutableBitSet columnsMapped) {
  ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r);
  if (columnsMapped.contains(rCols)) {
    // All required columns are present. No need to weaken.
    return r;
  }
  if (columnsMapped.intersects(rCols)) {
    final List<RexNode> list = new ArrayList<>();
    for (int c : columnsMapped.intersect(rCols)) {
      if (input.getRowType().getFieldList().get(c).getType().isNullable()
          && Strong.isNull(r, ImmutableBitSet.of(c))) {
        list.add(
            rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(input, c)));
      }
    }
    if (!list.isEmpty()) {
      return RexUtil.composeDisjunction(rexBuilder, list);
    }
  }
  // Cannot weaken to anything non-trivial
  return rexBuilder.makeLiteral(true);
}
 
Example 4
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source        The values being inserted
 * @param targetRowType The target type for the view
 */
private void checkConstraint(
	SqlValidatorTable validatorTable,
	SqlNode source,
	RelDataType targetRowType) {
	final ModifiableViewTable modifiableViewTable =
		validatorTable.unwrap(ModifiableViewTable.class);
	if (modifiableViewTable != null && source instanceof SqlCall) {
		final Table table = modifiableViewTable.unwrap(Table.class);
		final RelDataType tableRowType = table.getRowType(typeFactory);
		final List<RelDataTypeField> tableFields = tableRowType.getFieldList();

		// Get the mapping from column indexes of the underlying table
		// to the target columns and view constraints.
		final Map<Integer, RelDataTypeField> tableIndexToTargetField =
			SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
		final Map<Integer, RexNode> projectMap =
			RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);

		// Determine columns (indexed to the underlying table) that need
		// to be validated against the view constraint.
		final ImmutableBitSet targetColumns =
			ImmutableBitSet.of(tableIndexToTargetField.keySet());
		final ImmutableBitSet constrainedColumns =
			ImmutableBitSet.of(projectMap.keySet());
		final ImmutableBitSet constrainedTargetColumns =
			targetColumns.intersect(constrainedColumns);

		// Validate insert values against the view constraint.
		final List<SqlNode> values = ((SqlCall) source).getOperandList();
		for (final int colIndex : constrainedTargetColumns.asList()) {
			final String colName = tableFields.get(colIndex).getName();
			final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
			for (SqlNode row : values) {
				final SqlCall call = (SqlCall) row;
				final SqlNode sourceValue = call.operand(targetField.getIndex());
				final ValidationError validationError =
					new ValidationError(sourceValue,
						RESOURCE.viewConstraintNotSatisfied(colName,
							Util.last(validatorTable.getQualifiedName())));
				RelOptUtil.validateValueAgainstConstraint(sourceValue,
					projectMap.get(colIndex), validationError);
			}
		}
	}
}
 
Example 5
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Validates insert values against the constraint of a modifiable view.
 *
 * @param validatorTable Table that may wrap a ModifiableViewTable
 * @param source        The values being inserted
 * @param targetRowType The target type for the view
 */
private void checkConstraint(
	SqlValidatorTable validatorTable,
	SqlNode source,
	RelDataType targetRowType) {
	final ModifiableViewTable modifiableViewTable =
		validatorTable.unwrap(ModifiableViewTable.class);
	if (modifiableViewTable != null && source instanceof SqlCall) {
		final Table table = modifiableViewTable.unwrap(Table.class);
		final RelDataType tableRowType = table.getRowType(typeFactory);
		final List<RelDataTypeField> tableFields = tableRowType.getFieldList();

		// Get the mapping from column indexes of the underlying table
		// to the target columns and view constraints.
		final Map<Integer, RelDataTypeField> tableIndexToTargetField =
			SqlValidatorUtil.getIndexToFieldMap(tableFields, targetRowType);
		final Map<Integer, RexNode> projectMap =
			RelOptUtil.getColumnConstraints(modifiableViewTable, targetRowType, typeFactory);

		// Determine columns (indexed to the underlying table) that need
		// to be validated against the view constraint.
		final ImmutableBitSet targetColumns =
			ImmutableBitSet.of(tableIndexToTargetField.keySet());
		final ImmutableBitSet constrainedColumns =
			ImmutableBitSet.of(projectMap.keySet());
		final ImmutableBitSet constrainedTargetColumns =
			targetColumns.intersect(constrainedColumns);

		// Validate insert values against the view constraint.
		final List<SqlNode> values = ((SqlCall) source).getOperandList();
		for (final int colIndex : constrainedTargetColumns.asList()) {
			final String colName = tableFields.get(colIndex).getName();
			final RelDataTypeField targetField = tableIndexToTargetField.get(colIndex);
			for (SqlNode row : values) {
				final SqlCall call = (SqlCall) row;
				final SqlNode sourceValue = call.operand(targetField.getIndex());
				final ValidationError validationError =
					new ValidationError(sourceValue,
						RESOURCE.viewConstraintNotSatisfied(colName,
							Util.last(validatorTable.getQualifiedName())));
				RelOptUtil.validateValueAgainstConstraint(sourceValue,
					projectMap.get(colIndex), validationError);
			}
		}
	}
}
 
Example 6
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Converts a predicate on a particular set of columns into a predicate on
 * a subset of those columns, weakening if necessary.
 *
 * <p>If not possible to simplify, returns {@code true}, which is the weakest
 * possible predicate.
 *
 * <p>Examples:<ol>
 * <li>The predicate {@code $7 = $9} on columns [7]
 *     becomes {@code $7 is not null}
 * <li>The predicate {@code $7 = $9 + $11} on columns [7, 9]
 *     becomes {@code $7 is not null or $9 is not null}
 * <li>The predicate {@code $7 = $9 and $9 = 5} on columns [7] becomes
 *   {@code $7 = 5}
 * <li>The predicate
 *   {@code $7 = $9 and ($9 = $1 or $9 = $2) and $1 > 3 and $2 > 10}
 *   on columns [7] becomes {@code $7 > 3}
 * </ol>
 *
 * <p>We currently only handle examples 1 and 2.
 *
 * @param rexBuilder Rex builder
 * @param input Input relational expression
 * @param r Predicate expression
 * @param columnsMapped Columns which the final predicate can reference
 * @return Predicate expression narrowed to reference only certain columns
 */
private RexNode projectPredicate(final RexBuilder rexBuilder, RelNode input,
    RexNode r, ImmutableBitSet columnsMapped) {
  ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r);
  if (columnsMapped.contains(rCols)) {
    // All required columns are present. No need to weaken.
    return r;
  }
  if (columnsMapped.intersects(rCols)) {
    final List<RexNode> list = new ArrayList<>();
    for (int c : columnsMapped.intersect(rCols)) {
      if (input.getRowType().getFieldList().get(c).getType().isNullable()
          && Strong.isNull(r, ImmutableBitSet.of(c))) {
        list.add(
            rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(input, c)));
      }
    }
    if (!list.isEmpty()) {
      return RexUtil.composeDisjunction(rexBuilder, list);
    }
  }
  // Cannot weaken to anything non-trivial
  return rexBuilder.makeLiteral(true);
}