Java Code Examples for org.apache.calcite.rel.type.RelDataType#getFieldList()

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getFieldList() . 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 Project: calcite   File: SqlToRelTestBase.java    License: Apache License 2.0 6 votes vote down vote up
private List<RelCollation> deduceMonotonicity(SqlValidatorTable table) {
  final RelDataType rowType = table.getRowType();
  final List<RelCollation> collationList = new ArrayList<>();

  // Deduce which fields the table is sorted on.
  int i = -1;
  for (RelDataTypeField field : rowType.getFieldList()) {
    ++i;
    final SqlMonotonicity monotonicity =
        table.getMonotonicity(field.getName());
    if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
      final RelFieldCollation.Direction direction =
          monotonicity.isDecreasing()
              ? RelFieldCollation.Direction.DESCENDING
              : RelFieldCollation.Direction.ASCENDING;
      collationList.add(
          RelCollations.of(new RelFieldCollation(i, direction)));
    }
  }
  return collationList;
}
 
Example 2
Source Project: herddb   File: CalcitePlanner.java    License: Apache License 2.0 6 votes vote down vote up
private PlannerOp planValues(EnumerableValues op) {

        List<List<CompiledSQLExpression>> tuples = new ArrayList<>(op.getTuples().size());
        RelDataType rowType = op.getRowType();
        List<RelDataTypeField> fieldList = rowType.getFieldList();

        Column[] columns = new Column[fieldList.size()];
        for (ImmutableList<RexLiteral> tuple : op.getTuples()) {
            List<CompiledSQLExpression> row = new ArrayList<>(tuple.size());
            for (RexLiteral node : tuple) {
                CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(node);
                row.add(exp);
            }
            tuples.add(row);
        }
        int i = 0;
        String[] fieldNames = new String[fieldList.size()];
        for (RelDataTypeField field : fieldList) {
            Column col = Column.column(field.getName(), convertToHerdType(field.getType()));
            fieldNames[i] = field.getName();
            columns[i++] = col;
        }
        return new ValuesOp(manager.getNodeId(), fieldNames,
                columns, tuples);

    }
 
Example 3
/**
 * Build a join condition based on the left/right keys
 *
 * @param leftRowType
 * @param rightRowType
 * @param leftKeys
 * @param rightKeys
 * @param filterNulls
 * @param builder
 * @return a conjunction of equi-join conditions
 */
static RexNode buildJoinCondition(RexBuilder builder, RelDataType leftRowType, RelDataType rightRowType, List<Integer> leftKeys, List<Integer> rightKeys, List<Boolean> filterNulls) {
  final List<RexNode> equijoinList = Lists.newArrayList();
  final int numLeftFields = leftRowType.getFieldCount();
  final List<RelDataTypeField> leftTypes = leftRowType.getFieldList();
  final List<RelDataTypeField> rightTypes = rightRowType.getFieldList();

  for (int i = 0; i < leftKeys.size(); i++) {
    int leftKeyOrdinal = leftKeys.get(i);
    int rightKeyOrdinal = rightKeys.get(i);

    SqlBinaryOperator operator = filterNulls.get(i) ? SqlStdOperatorTable.EQUALS : SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
    RexNode leftInput = builder.makeInputRef(leftTypes.get(leftKeyOrdinal).getType(), leftKeyOrdinal);
    RexNode rightInput = builder.makeInputRef(rightTypes.get(rightKeyOrdinal).getType(), rightKeyOrdinal + numLeftFields);
    equijoinList.add(builder.makeCall(operator, leftInput, rightInput));
  }

  return RexUtil.composeConjunction(builder, equijoinList, false);
}
 
Example 4
Source Project: calcite   File: RexUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the leading edge of a given array of expressions is
 * wholly {@link RexInputRef} objects with types corresponding to the
 * underlying datatype.
 */
public static boolean containIdentity(
    List<? extends RexNode> exprs,
    RelDataType rowType,
    Litmus litmus) {
  final List<RelDataTypeField> fields = rowType.getFieldList();
  if (exprs.size() < fields.size()) {
    return litmus.fail("exprs/rowType length mismatch");
  }
  for (int i = 0; i < fields.size(); i++) {
    if (!(exprs.get(i) instanceof RexInputRef)) {
      return litmus.fail("expr[{}] is not a RexInputRef", i);
    }
    RexInputRef inputRef = (RexInputRef) exprs.get(i);
    if (inputRef.getIndex() != i) {
      return litmus.fail("expr[{}] has ordinal {}", i, inputRef.getIndex());
    }
    if (!RelOptUtil.eq("type1",
        exprs.get(i).getType(),
        "type2",
        fields.get(i).getType(), litmus)) {
      return litmus.fail(null);
    }
  }
  return litmus.succeed();
}
 
Example 5
Source Project: calcite   File: RexProgramBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a program-builder.
 */
private RexProgramBuilder(RelDataType inputRowType, RexBuilder rexBuilder,
    RexSimplify simplify) {
  this.inputRowType = Objects.requireNonNull(inputRowType);
  this.rexBuilder = Objects.requireNonNull(rexBuilder);
  this.simplify = simplify; // may be null
  this.validating = assertionsAreEnabled();

  // Pre-create an expression for each input field.
  if (inputRowType.isStruct()) {
    final List<RelDataTypeField> fields = inputRowType.getFieldList();
    for (int i = 0; i < fields.size(); i++) {
      registerInternal(RexInputRef.of(i, fields), false);
    }
  }
}
 
Example 6
Source Project: calcite   File: RexUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Generates a cast for a row type.
 *
 * @param rexBuilder RexBuilder to use for constructing casts
 * @param lhsRowType target row type
 * @param rhsExps    expressions to be cast
 * @return cast expressions
 */
public static List<RexNode> generateCastExpressions(
    RexBuilder rexBuilder,
    RelDataType lhsRowType,
    List<RexNode> rhsExps) {
  List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
  List<RexNode> castExps = new ArrayList<>();
  for (Pair<RelDataTypeField, RexNode> pair
      : Pair.zip(lhsFields, rhsExps, true)) {
    RelDataTypeField lhsField = pair.left;
    RelDataType lhsType = lhsField.getType();
    final RexNode rhsExp = pair.right;
    RelDataType rhsType = rhsExp.getType();
    if (lhsType.equals(rhsType)) {
      castExps.add(rhsExp);
    } else {
      castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
    }
  }
  return castExps;
}
 
Example 7
private static List<FieldType> getFields(RelNode relNode) {
    RelDataType rowType = relNode.getRowType();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    ArrayList<FieldType> fieldSchemas = new ArrayList<>(fieldList.size());
    for (RelDataTypeField relDataTypeField : fieldList) {
        String name = relDataTypeField.getName();
        RelDataType type = relDataTypeField.getType();
        SqlTypeName sqlTypeName = type.getSqlTypeName();
        boolean nullable = type.isNullable();
        Integer precision = null;
        Integer scale = null;
        if (sqlTypeName.allowsPrec()) {
            precision = type.getPrecision();
        }
        if (sqlTypeName.allowsScale()) {
            scale = type.getScale();
        }
        fieldSchemas.add(new FieldType(name, ExprExplain.type(sqlTypeName), nullable, precision, scale));
    }
    return fieldSchemas;
}
 
Example 8
public RelBuilder values(RelDataType rowType, Object... columnValues) {
    int columnCount = rowType.getFieldCount();
    final ImmutableList.Builder<ImmutableList<RexLiteral>> listBuilder =
            ImmutableList.builder();
    final List<RexLiteral> valueList = new ArrayList<>();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    for (int i = 0; i < columnValues.length; i++) {
        RelDataTypeField relDataTypeField = fieldList.get(valueList.size());
        valueList.add((RexLiteral) literal(relDataTypeField.getType(), columnValues[i], false));
        if ((i + 1) % columnCount == 0) {
            listBuilder.add(ImmutableList.copyOf(valueList));
            valueList.clear();
        }
    }
    super.values(listBuilder.build(), rowType);
    return this;
}
 
Example 9
Source Project: calcite   File: AliasNamespace.java    License: Apache License 2.0 5 votes vote down vote up
private String getString(RelDataType rowType) {
  StringBuilder buf = new StringBuilder();
  buf.append("(");
  for (RelDataTypeField field : rowType.getFieldList()) {
    if (field.getIndex() > 0) {
      buf.append(", ");
    }
    buf.append("'");
    buf.append(field.getName());
    buf.append("'");
  }
  buf.append(")");
  return buf.toString();
}
 
Example 10
Source Project: Bats   File: LoptMultiJoin.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the fields corresponding to a join between a left and right
 * tree
 *
 * @param left left hand side of the join
 * @param right right hand side of the join
 *
 * @return fields of the join
 */
public List<RelDataTypeField> getJoinFields(
    LoptJoinTree left,
    LoptJoinTree right) {
  RelDataType rowType =
      factory.createJoinType(
          left.getJoinTree().getRowType(),
          right.getJoinTree().getRowType());
  return rowType.getFieldList();
}
 
Example 11
Source Project: dremio-oss   File: ExtendedToRelContext.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Confirm the row type is consistent with that of the validated node.
 * Performs the least restrictive check to confirm the validated field list is not smaller than the view's field list.
 *
 * @param validatedRowType    The validated row type from the RelRoot
 * @param rowType             The row type of the view.
 * @param datasetPath         The path of the dataset being expanded.
 *
 * @throws UserException indicating dataset definition is out of date.
 */
private void checkRowTypeConsistency(final RelDataType validatedRowType, final RelDataType rowType, String datasetPath) {
  List<RelDataTypeField> rowTypeFieldList = rowType.getFieldList();
  List<RelDataTypeField> validatedFieldList = validatedRowType.getFieldList();

  // Confirm that the validate field list is not smaller than view's field list.
  if (validatedFieldList.size() < rowTypeFieldList.size()) {
    throw UserException.validationError()
      .message(String.format("Definition of this dataset is out of date. There were schema changes in %s.\n",
        datasetPath))
      .addContext("Original", Describer.describe(CalciteArrowHelper.fromCalciteRowType(rowType)))
      .addContext("New", Describer.describe(CalciteArrowHelper.fromCalciteRowType(validatedRowType)))
      .build(logger);
  }
}
 
Example 12
Source Project: Bats   File: DrillRelOptUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the leading edge of a given array of expressions is
 * wholly {@link RexInputRef} objects with types and names corresponding
 * to the underlying row type. */
private static boolean containIdentity(List<? extends RexNode> exps, RelDataType rowType,
        RelDataType childRowType) {
    List<RelDataTypeField> fields = rowType.getFieldList();
    List<RelDataTypeField> childFields = childRowType.getFieldList();
    int fieldCount = childFields.size();
    if (exps.size() != fieldCount) {
        return false;
    }
    for (int i = 0; i < exps.size(); i++) {
        RexNode exp = exps.get(i);
        if (!(exp instanceof RexInputRef)) {
            return false;
        }
        RexInputRef var = (RexInputRef) exp;
        if (var.getIndex() != i) {
            return false;
        }
        if (!fields.get(i).getName().equals(childFields.get(i).getName())) {
            return false;
        }
        if (!fields.get(i).getType().equals(childFields.get(i).getType())) {
            return false;
        }
    }
    return true;
}
 
Example 13
Source Project: calcite   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether two types have the same name and structure, possibly with
 * differing modifiers. For example, VARCHAR(1) and VARCHAR(10) are
 * considered the same, while VARCHAR(1) and CHAR(1) are considered
 * different. Likewise, VARCHAR(1) MULTISET and VARCHAR(10) MULTISET are
 * considered the same.
 *
 * @return true if types have same name and structure
 */
public static boolean sameNamedType(RelDataType t1, RelDataType t2) {
  if (t1.isStruct() || t2.isStruct()) {
    if (!t1.isStruct() || !t2.isStruct()) {
      return false;
    }
    if (t1.getFieldCount() != t2.getFieldCount()) {
      return false;
    }
    List<RelDataTypeField> fields1 = t1.getFieldList();
    List<RelDataTypeField> fields2 = t2.getFieldList();
    for (int i = 0; i < fields1.size(); ++i) {
      if (!sameNamedType(
          fields1.get(i).getType(),
          fields2.get(i).getType())) {
        return false;
      }
    }
    return true;
  }
  RelDataType comp1 = t1.getComponentType();
  RelDataType comp2 = t2.getComponentType();
  if ((comp1 != null) || (comp2 != null)) {
    if ((comp1 == null) || (comp2 == null)) {
      return false;
    }
    if (!sameNamedType(comp1, comp2)) {
      return false;
    }
  }
  return t1.getSqlTypeName() == t2.getSqlTypeName();
}
 
Example 14
Source Project: calcite   File: LoptMultiJoin.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the fields corresponding to a join between a left and right
 * tree
 *
 * @param left left hand side of the join
 * @param right right hand side of the join
 *
 * @return fields of the join
 */
public List<RelDataTypeField> getJoinFields(
    LoptJoinTree left,
    LoptJoinTree right) {
  RelDataType rowType =
      factory.createJoinType(
          left.getJoinTree().getRowType(),
          right.getJoinTree().getRowType());
  return rowType.getFieldList();
}
 
Example 15
Source Project: calcite   File: SqlTypeFactoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
private RelDataType copyObjectType(RelDataType type, boolean nullable) {
  return new ObjectSqlType(
      type.getSqlTypeName(),
      type.getSqlIdentifier(),
      nullable,
      type.getFieldList(),
      type.getComparability());
}
 
Example 16
Source Project: Bats   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Selects data types of the specified fields from an input row type.
 * This is useful when identifying data types of a function that is going
 * to operate on inputs that are specified as field ordinals (e.g.
 * aggregate calls).
 *
 * @param rowType input row type
 * @param requiredFields ordinals of the projected fields
 * @return list of data types that are requested by requiredFields
 */
public static List<RelDataType> projectTypes(final RelDataType rowType,
    final List<? extends Number> requiredFields) {
  final List<RelDataTypeField> fields = rowType.getFieldList();

  return new AbstractList<RelDataType>() {
    @Override public RelDataType get(int index) {
      return fields.get(requiredFields.get(index).intValue()).getType();
    }

    @Override public int size() {
      return requiredFields.size();
    }
  };
}
 
Example 17
Source Project: Bats   File: SqlTypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns whether a type is flat. It is not flat if it is a record type that
 * has one or more fields that are themselves record types. */
public static boolean isFlat(RelDataType type) {
  if (type.isStruct()) {
    for (RelDataTypeField field : type.getFieldList()) {
      if (field.getType().isStruct()) {
        return false;
      }
    }
  }
  return true;
}
 
Example 18
Source Project: Bats   File: RelOptUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static boolean checkProjAndChildInputs(Project project, boolean checkNames) {
    int n = project.getProjects().size();
    RelDataType inputType = project.getInput().getRowType();
    if (inputType.getFieldList().size() != n) {
        return false;
    }
    List<RelDataTypeField> projFields = project.getRowType().getFieldList();
    List<RelDataTypeField> inputFields = inputType.getFieldList();
    boolean namesDifferent = false;
    for (int i = 0; i < n; ++i) {
        RexNode exp = project.getProjects().get(i);
        if (!(exp instanceof RexInputRef)) {
            return false;
        }
        RexInputRef fieldAccess = (RexInputRef) exp;
        if (i != fieldAccess.getIndex()) {
            // can't support reorder yet
            return false;
        }
        if (checkNames) {
            String inputFieldName = inputFields.get(i).getName();
            String projFieldName = projFields.get(i).getName();
            if (!projFieldName.equals(inputFieldName)) {
                namesDifferent = true;
            }
        }
    }

    // inputs are the same; return value depends on the checkNames
    // parameter
    return !checkNames || namesDifferent;
}
 
Example 19
Source Project: calcite   File: RelOptUtil.java    License: Apache License 2.0 4 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelNode createNullFilter(
    RelNode rel,
    Integer[] fieldOrdinals) {
  RexNode condition = null;
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RelDataType rowType = rel.getRowType();
  int n;
  if (fieldOrdinals != null) {
    n = fieldOrdinals.length;
  } else {
    n = rowType.getFieldCount();
  }
  List<RelDataTypeField> fields = rowType.getFieldList();
  for (int i = 0; i < n; ++i) {
    int iField;
    if (fieldOrdinals != null) {
      iField = fieldOrdinals[i];
    } else {
      iField = i;
    }
    RelDataType type = fields.get(iField).getType();
    if (!type.isNullable()) {
      continue;
    }
    RexNode newCondition =
        rexBuilder.makeCall(
            SqlStdOperatorTable.IS_NOT_NULL,
            rexBuilder.makeInputRef(type, iField));
    if (condition == null) {
      condition = newCondition;
    } else {
      condition =
          rexBuilder.makeCall(
              SqlStdOperatorTable.AND,
              condition,
              newCondition);
    }
  }
  if (condition == null) {
    // no filtering required
    return rel;
  }

  final RelFactories.FilterFactory factory =
      RelFactories.DEFAULT_FILTER_FACTORY;
  return factory.createFilter(rel, condition, ImmutableSet.of());
}
 
Example 20
Source Project: calcite   File: RelOptUtil.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Determines whether any of the fields in a given relational expression may
 * contain null values, taking into account constraints on the field types and
 * also deduced predicates.
 *
 * <p>The method is cautious: It may sometimes return {@code true} when the
 * actual answer is {@code false}. In particular, it does this when there
 * is no executor, or the executor is not a sub-class of
 * {@link RexExecutorImpl}.
 */
private static boolean containsNullableFields(RelNode r) {
  final RexBuilder rexBuilder = r.getCluster().getRexBuilder();
  final RelDataType rowType = r.getRowType();
  final List<RexNode> list = new ArrayList<>();
  final RelMetadataQuery mq = r.getCluster().getMetadataQuery();
  for (RelDataTypeField field : rowType.getFieldList()) {
    if (field.getType().isNullable()) {
      list.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(field.getType(), field.getIndex())));
    }
  }
  if (list.isEmpty()) {
    // All columns are declared NOT NULL.
    return false;
  }
  final RelOptPredicateList predicates = mq.getPulledUpPredicates(r);
  if (predicates.pulledUpPredicates.isEmpty()) {
    // We have no predicates, so cannot deduce that any of the fields
    // declared NULL are really NOT NULL.
    return true;
  }
  final RexExecutor executor = r.getCluster().getPlanner().getExecutor();
  if (!(executor instanceof RexExecutorImpl)) {
    // Cannot proceed without an executor.
    return true;
  }
  final RexImplicationChecker checker =
      new RexImplicationChecker(rexBuilder, executor,
          rowType);
  final RexNode first =
      RexUtil.composeConjunction(rexBuilder, predicates.pulledUpPredicates);
  final RexNode second = RexUtil.composeConjunction(rexBuilder, list);
  // Suppose we have EMP(empno INT NOT NULL, mgr INT),
  // and predicates [empno > 0, mgr > 0].
  // We make first: "empno > 0 AND mgr > 0"
  // and second: "mgr IS NOT NULL"
  // and ask whether first implies second.
  // It does, so we have no nullable columns.
  return !checker.implies(first, second);
}