use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project hive by apache.
the class SemanticAnalyzer method getNotNullConstraintExpr.
private ExprNodeDesc getNotNullConstraintExpr(Table targetTable, Operator input, String dest) throws SemanticException {
boolean forceNotNullConstraint = conf.getBoolVar(ConfVars.HIVE_ENFORCE_NOT_NULL_CONSTRAINT);
if (!forceNotNullConstraint) {
return null;
}
ImmutableBitSet nullConstraintBitSet = null;
try {
nullConstraintBitSet = getEnabledNotNullConstraints(targetTable);
} catch (Exception e) {
if (e instanceof SemanticException) {
throw (SemanticException) e;
} else {
throw (new RuntimeException(e));
}
}
if (nullConstraintBitSet == null) {
return null;
}
List<ColumnInfo> colInfos = input.getSchema().getSignature();
ExprNodeDesc currUDF = null;
// Add NOT NULL constraints
int constraintIdx = 0;
for (int colExprIdx = 0; colExprIdx < colInfos.size(); colExprIdx++) {
if (updating(dest) && colExprIdx == 0) {
// for updates first column is _rowid
continue;
}
if (nullConstraintBitSet.indexOf(constraintIdx) != -1) {
ExprNodeDesc currExpr = TypeCheckProcFactory.toExprNodeDesc(colInfos.get(colExprIdx));
ExprNodeDesc isNotNullUDF = TypeCheckProcFactory.DefaultExprProcessor.getFuncExprNodeDesc("isnotnull", currExpr);
if (currUDF != null) {
currUDF = TypeCheckProcFactory.DefaultExprProcessor.getFuncExprNodeDesc("and", currUDF, isNotNullUDF);
} else {
currUDF = isNotNullUDF;
}
}
constraintIdx++;
}
return currUDF;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project calcite by apache.
the class TableScanNode method createProjectableFilterable.
private static TableScanNode createProjectableFilterable(Compiler compiler, TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects, ProjectableFilterableTable pfTable) {
final DataContext root = compiler.getDataContext();
final ImmutableIntList originalProjects = projects;
for (; ; ) {
final List<RexNode> mutableFilters = Lists.newArrayList(filters);
final int[] projectInts;
if (projects == null || projects.equals(TableScan.identity(rel.getTable()))) {
projectInts = null;
} else {
projectInts = projects.toIntArray();
}
final Enumerable<Object[]> enumerable1 = pfTable.scan(root, mutableFilters, projectInts);
for (RexNode filter : mutableFilters) {
if (!filters.contains(filter)) {
throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
}
}
final ImmutableBitSet usedFields = RelOptUtil.InputFinder.bits(mutableFilters, null);
if (projects != null) {
int changeCount = 0;
for (int usedField : usedFields) {
if (!projects.contains(usedField)) {
// A field that is not projected is used in a filter that the
// table rejected. We won't be able to apply the filter later.
// Try again without any projects.
projects = ImmutableIntList.copyOf(Iterables.concat(projects, ImmutableList.of(usedField)));
++changeCount;
}
}
if (changeCount > 0) {
continue;
}
}
final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable1);
final ImmutableIntList rejectedProjects;
if (Objects.equals(projects, originalProjects)) {
rejectedProjects = null;
} else {
// We projected extra columns because they were needed in filters. Now
// project the leading columns.
rejectedProjects = ImmutableIntList.identity(originalProjects.size());
}
return createEnumerable(compiler, rel, rowEnumerable, projects, mutableFilters, rejectedProjects);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet in project calcite by apache.
the class ProfilerLatticeStatisticProvider method cardinality.
public double cardinality(List<Lattice.Column> columns) {
final ImmutableBitSet build = Lattice.Column.toBitSet(columns);
final double cardinality = profile.get().cardinality(build);
// System.out.println(columns + ": " + cardinality);
return cardinality;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.util.ImmutableBitSet 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.util.ImmutableBitSet in project calcite by apache.
the class RelMdColumnUniqueness method areColumnsUnique.
public Boolean areColumnsUnique(Join rel, RelMetadataQuery mq, ImmutableBitSet columns, boolean ignoreNulls) {
if (columns.cardinality() == 0) {
return false;
}
final RelNode left = rel.getLeft();
final RelNode right = rel.getRight();
// Divide up the input column mask into column masks for the left and
// right sides of the join
final Pair<ImmutableBitSet, ImmutableBitSet> leftAndRightColumns = splitLeftAndRightColumns(rel.getLeft().getRowType().getFieldCount(), columns);
final ImmutableBitSet leftColumns = leftAndRightColumns.left;
final ImmutableBitSet rightColumns = leftAndRightColumns.right;
// If the original column mask contains columns from both the left and
// right hand side, then the columns are unique if and only if they're
// unique for their respective join inputs
Boolean leftUnique = mq.areColumnsUnique(left, leftColumns, ignoreNulls);
Boolean rightUnique = mq.areColumnsUnique(right, rightColumns, ignoreNulls);
if ((leftColumns.cardinality() > 0) && (rightColumns.cardinality() > 0)) {
if ((leftUnique == null) || (rightUnique == null)) {
return null;
} else {
return leftUnique && rightUnique;
}
}
// If we're only trying to determine uniqueness for columns that
// originate from one join input, then determine if the equijoin
// columns from the other join input are unique. If they are, then
// the columns are unique for the entire join if they're unique for
// the corresponding join input, provided that input is not null
// generating.
final JoinInfo joinInfo = rel.analyzeCondition();
if (leftColumns.cardinality() > 0) {
if (rel.getJoinType().generatesNullsOnLeft()) {
return false;
}
Boolean rightJoinColsUnique = mq.areColumnsUnique(right, joinInfo.rightSet(), ignoreNulls);
if ((rightJoinColsUnique == null) || (leftUnique == null)) {
return null;
}
return rightJoinColsUnique && leftUnique;
} else if (rightColumns.cardinality() > 0) {
if (rel.getJoinType().generatesNullsOnRight()) {
return false;
}
Boolean leftJoinColsUnique = mq.areColumnsUnique(left, joinInfo.leftSet(), ignoreNulls);
if ((leftJoinColsUnique == null) || (rightUnique == null)) {
return null;
}
return leftJoinColsUnique && rightUnique;
}
throw new AssertionError();
}
Aggregations