org.apache.calcite.rel.type.RelDataTypeField Java Examples

The following examples show how to use org.apache.calcite.rel.type.RelDataTypeField. 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: ExplicitOperandTypeChecker.java    From calcite with Apache License 2.0 6 votes vote down vote up
public boolean checkOperandTypes(
    SqlCallBinding callBinding,
    boolean throwOnFailure) {
  List<SqlTypeFamily> families = new ArrayList<>();

  List<RelDataTypeField> fieldList = type.getFieldList();
  for (int i = 0; i < fieldList.size(); i++) {
    RelDataTypeField field = fieldList.get(i);
    SqlTypeName sqlTypeName = field.getType().getSqlTypeName();
    if (sqlTypeName == SqlTypeName.ROW) {
      if (field.getType().equals(callBinding.getOperandType(i))) {
        families.add(SqlTypeFamily.ANY);
      }
    } else {
      families.add(field.getType().getSqlTypeName().getFamily());
    }
  }
  return OperandTypes.family(families).checkOperandTypes(callBinding, throwOnFailure);
}
 
Example #2
Source File: MaterializedViewSubstitutionVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> transformRex(List<RexNode> nodes, final List<RelDataTypeField> oldFields,
        final List<RelDataTypeField> newFields) {
    RexShuttle shuttle = new RexShuttle() {
        @Override
        public RexNode visitInputRef(RexInputRef ref) {
            RelDataTypeField f = oldFields.get(ref.getIndex());
            for (int index = 0; index < newFields.size(); index++) {
                RelDataTypeField newf = newFields.get(index);
                if (f.getKey().equals(newf.getKey()) && f.getValue() == newf.getValue()) {
                    return RexBuilder.getRexFactory().makeInputRef(index, f.getValue());
                }
            }
            throw MatchFailed.INSTANCE;
        }
    };
    return shuttle.apply(nodes);
}
 
Example #3
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public SqlImplementor.Result visit(Window e) {
  SqlImplementor.Result x = visitChild(0, e.getInput());
  SqlImplementor.Builder builder = x.builder(e);
  RelNode input = e.getInput();
  List<RexOver> rexOvers = WindowUtil.getOver(e);

  final List<SqlNode> selectList = new ArrayList<>();
  final Set<String> fields = ImmutableSet.copyOf(e.getRowType().getFieldNames());
  for (RelDataTypeField field : input.getRowType().getFieldList()) {
    if (fields.contains(field.getName())) {
      addSelect(selectList, builder.context.field(field.getIndex()), e.getRowType());
    }
  }

  for (RexOver rexOver : rexOvers) {
    addSelect(selectList, builder.context.toSql(null, rexOver), e.getRowType());
  }
  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example #4
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes {@link #trimFields}, or the appropriate method for the type
 * of the rel parameter, using multi-method dispatch.
 *
 * @param rel        Relational expression
 * @param fieldsUsed Bitmap of fields needed by the consumer
 * @return New relational expression and its field mapping
 */
protected final TrimResult dispatchTrimFields(
    RelNode rel,
    ImmutableBitSet fieldsUsed,
    Set<RelDataTypeField> extraFields) {
  final TrimResult trimResult =
      trimFieldsDispatcher.invoke(rel, fieldsUsed, extraFields);
  final RelNode newRel = trimResult.left;
  final Mapping mapping = trimResult.right;
  final int fieldCount = rel.getRowType().getFieldCount();
  assert mapping.getSourceCount() == fieldCount
      : "source: " + mapping.getSourceCount() + " != " + fieldCount;
  final int newFieldCount = newRel.getRowType().getFieldCount();
  assert mapping.getTargetCount() + extraFields.size() == newFieldCount
      || Bug.TODO_FIXED
      : "target: " + mapping.getTargetCount()
      + " + " + extraFields.size()
      + " != " + newFieldCount;
  if (Bug.TODO_FIXED) {
    assert newFieldCount > 0 : "rel has no fields after trim: " + rel;
  }
  if (newRel.equals(rel)) {
    return result(rel, mapping);
  }
  return trimResult;
}
 
Example #5
Source File: RexProgramBuilder.java    From calcite with 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 File: PushProjector.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Clones an expression tree and walks through it, adjusting each
 * RexInputRef index by some amount, and converting expressions that need to
 * be preserved to field references.
 *
 * @param rex         the expression
 * @param destFields  fields that the new expressions will be referencing
 * @param adjustments the amount each input reference index needs to be
 *                    adjusted by
 * @return modified expression tree
 */
public RexNode convertRefsAndExprs(
    RexNode rex,
    List<RelDataTypeField> destFields,
    int[] adjustments) {
  return rex.accept(
      new RefAndExprConverter(
          rexBuilder,
          childFields,
          destFields,
          adjustments,
          childPreserveExprs,
          nProject,
          rightPreserveExprs,
          nProject + childPreserveExprs.size() + nRightProject));
}
 
Example #7
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalJoin join) {
  // to the best of my knowledge join.systemFieldList is always empty
  Preconditions.checkState(join.getSystemFieldList().isEmpty(), "join.systemFieldList is not empty!");

  final RelNode left = join.getLeft().accept(this);
  final RelNode right = join.getRight().accept(this);

  return new LogicalJoin(
    cluster,
    copyOf(join.getTraitSet()),
    left,
    right,
    copyOf(join.getCondition()),
    join.getVariablesSet(),
    join.getJoinType(),
    join.isSemiJoinDone(),
    ImmutableList.<RelDataTypeField>of()
  );
}
 
Example #8
Source File: RexProgram.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a program that projects its input fields but with possibly
 * different names for the output fields.
 */
public static RexProgram createIdentity(
    RelDataType rowType,
    RelDataType outputRowType) {
  if (rowType != outputRowType
      && !Pair.right(rowType.getFieldList()).equals(
          Pair.right(outputRowType.getFieldList()))) {
    throw new IllegalArgumentException(
        "field type mismatch: " + rowType + " vs. " + outputRowType);
  }
  final List<RelDataTypeField> fields = rowType.getFieldList();
  final List<RexLocalRef> projectRefs = new ArrayList<>();
  final List<RexInputRef> refs = new ArrayList<>();
  for (int i = 0; i < fields.size(); i++) {
    final RexInputRef ref = RexInputRef.of(i, fields);
    refs.add(ref);
    projectRefs.add(new RexLocalRef(i, ref.getType()));
  }
  return new RexProgram(rowType, refs, projectRefs, null, outputRowType);
}
 
Example #9
Source File: PushProjector.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new projection based on the original projection, adjusting all
 * input refs using an adjustment array passed in. If there was no original
 * projection, create a new one that selects every field from the underlying
 * rel.
 *
 * <p>If the resulting projection would be trivial, return the child.
 *
 * @param projChild   child of the new project
 * @param adjustments array indicating how much each input reference should
 *                    be adjusted by
 * @return the created projection
 */
public RelNode createNewProject(RelNode projChild, int[] adjustments) {
  final List<Pair<RexNode, String>> projects = new ArrayList<>();

  if (origProj != null) {
    for (Pair<RexNode, String> p : origProj.getNamedProjects()) {
      projects.add(
          Pair.of(
              convertRefsAndExprs(
                  p.left,
                  projChild.getRowType().getFieldList(),
                  adjustments),
              p.right));
    }
  } else {
    for (Ord<RelDataTypeField> field : Ord.zip(childFields)) {
      projects.add(
          Pair.of(
              rexBuilder.makeInputRef(
                  field.e.getType(), field.i), field.e.getName()));
    }
  }
  return relBuilder.push(projChild)
      .project(Pair.left(projects), Pair.right(projects))
      .build();
}
 
Example #10
Source File: RexTransformerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-833">[CALCITE-833]
 * RelOptUtil.splitJoinCondition attempts to split a Join-Condition which
 * has a remaining condition</a>. */
@Test void testSplitJoinCondition() {
  final String sql = "select *\n"
      + "from emp a\n"
      + "INNER JOIN dept b\n"
      + "ON CAST(a.empno AS int) <> b.deptno";

  final RelNode relNode = toRel(sql);
  final LogicalProject project = (LogicalProject) relNode;
  final LogicalJoin join = (LogicalJoin) project.getInput(0);
  final List<RexNode> leftJoinKeys = new ArrayList<>();
  final List<RexNode> rightJoinKeys = new ArrayList<>();
  final ArrayList<RelDataTypeField> sysFieldList = new ArrayList<>();
  final RexNode remaining = RelOptUtil.splitJoinCondition(sysFieldList,
      join.getInputs().get(0),
      join.getInputs().get(1),
      join.getCondition(),
      leftJoinKeys,
      rightJoinKeys,
      null,
      null);

  assertThat(remaining.toString(), is("<>($0, $9)"));
  assertThat(leftJoinKeys.isEmpty(), is(true));
  assertThat(rightJoinKeys.isEmpty(), is(true));
}
 
Example #11
Source File: WithItemNamespace.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override protected RelDataType validateImpl(RelDataType targetRowType) {
  final SqlValidatorNamespace childNs =
      validator.getNamespace(withItem.query);
  final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
  if (withItem.columnList == null) {
    return rowType;
  }
  final RelDataTypeFactory.Builder builder =
      validator.getTypeFactory().builder();
  for (Pair<SqlNode, RelDataTypeField> pair
      : Pair.zip(withItem.columnList, rowType.getFieldList())) {
    builder.add(((SqlIdentifier) pair.left).getSimple(),
        pair.right.getType());
  }
  return builder.build();
}
 
Example #12
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether two types are scalar types of the same family, or struct types whose fields
 * are pairwise of the same family.
 *
 * @param type1 First type
 * @param type2 Second type
 * @return Whether types have the same family
 */
private static boolean isSameFamily(RelDataType type1, RelDataType type2) {
  if (type1.isStruct() != type2.isStruct()) {
    return false;
  }

  if (type1.isStruct()) {
    int n = type1.getFieldCount();
    if (n != type2.getFieldCount()) {
      return false;
    }
    for (Pair<RelDataTypeField, RelDataTypeField> pair
        : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
      if (!isSameFamily(pair.left.getType(), pair.right.getType())) {
        return false;
      }
    }
    return true;
  }

  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);
  return family1 == family2;
}
 
Example #13
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static RexNode shiftFilter(
    int start,
    int end,
    int offset,
    RexBuilder rexBuilder,
    List<RelDataTypeField> joinFields,
    int nTotalFields,
    List<RelDataTypeField> rightFields,
    RexNode filter) {
  int[] adjustments = new int[nTotalFields];
  for (int i = start; i < end; i++) {
    adjustments[i] = offset;
  }
  return filter.accept(
      new RexInputConverter(
          rexBuilder,
          joinFields,
          rightFields,
          adjustments));
}
 
Example #14
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Returns the row type of a table after any {@link ColumnStrategy#VIRTUAL}
 * columns have been removed. This is the type of the records that are
 * actually stored. */
public static RelDataType realRowType(RelOptTable table) {
  final RelDataType rowType = table.getRowType();
  final List<ColumnStrategy> strategies = columnStrategies(table);
  if (!strategies.contains(ColumnStrategy.VIRTUAL)) {
    return rowType;
  }
  final RelDataTypeFactory.Builder builder =
      table.getRelOptSchema().getTypeFactory().builder();
  for (RelDataTypeField field : rowType.getFieldList()) {
    if (strategies.get(field.getIndex()) != ColumnStrategy.VIRTUAL) {
      builder.add(field);
    }
  }
  return builder.build();
}
 
Example #15
Source File: PlanExecutor.java    From quark with Apache License 2.0 6 votes vote down vote up
private RelDataType getDataSourceRowType() throws SQLException {

    List<RelDataTypeField> relDataTypeFields =
        ImmutableList.<RelDataTypeField>of(
            new RelDataTypeFieldImpl("id", 1, getIntegerJavaType()),
            new RelDataTypeFieldImpl("type", 2, getStringJavaType()),
            new RelDataTypeFieldImpl("url", 3, getStringJavaType()),
            new RelDataTypeFieldImpl("name", 4, getStringJavaType()),
            new RelDataTypeFieldImpl("ds_set_id", 5, getIntegerJavaType()),
            new RelDataTypeFieldImpl("datasource_type", 6, getStringJavaType()),
            new RelDataTypeFieldImpl("auth_token", 7, getStringJavaType()),
            new RelDataTypeFieldImpl("dbtap_id", 8, getIntegerJavaType()),
            new RelDataTypeFieldImpl("username", 9, getStringJavaType()),
            new RelDataTypeFieldImpl("password", 10, getStringJavaType()));

    return new RelRecordType(relDataTypeFields);
  }
 
Example #16
Source File: DruidQuery.java    From calcite with Apache License 2.0 6 votes vote down vote up
private ColumnMetaData.Rep getPrimitive(RelDataTypeField field) {
  switch (field.getType().getSqlTypeName()) {
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
  case TIMESTAMP:
    return ColumnMetaData.Rep.JAVA_SQL_TIMESTAMP;
  case BIGINT:
    return ColumnMetaData.Rep.LONG;
  case INTEGER:
    return ColumnMetaData.Rep.INTEGER;
  case SMALLINT:
    return ColumnMetaData.Rep.SHORT;
  case TINYINT:
    return ColumnMetaData.Rep.BYTE;
  case REAL:
    return ColumnMetaData.Rep.FLOAT;
  case DOUBLE:
  case FLOAT:
    return ColumnMetaData.Rep.DOUBLE;
  default:
    return null;
  }
}
 
Example #17
Source File: RuntimeFilterVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Find a join condition's left input source scan Prel. If we can't find a target scan Prel then this
 * RuntimeFilter can not pushed down to a probe side scan Prel.
 *
 * @param fieldName   left join condition field Name
 * @param leftRelNode left RelNode of a BiRel or the SingleRel
 * @return a left scan Prel which contains the left join condition name or null
 */
private ScanPrel findLeftScanPrel(String fieldName, RelNode leftRelNode) {
  if (leftRelNode instanceof ScanPrel) {
    RelDataType scanRowType = leftRelNode.getRowType();
    RelDataTypeField field = scanRowType.getField(fieldName, true, true);
    if (field != null) {
      //found
      return (ScanPrel) leftRelNode;
    } else {
      return null;
    }
  } else if (leftRelNode instanceof RelSubset) {
    RelNode bestNode = ((RelSubset) leftRelNode).getBest();
    if (bestNode != null) {
      return findLeftScanPrel(fieldName, bestNode);
    } else {
      return null;
    }
  } else {
    List<RelNode> relNodes = leftRelNode.getInputs();
    RelNode leftNode = relNodes.get(0);
    return findLeftScanPrel(fieldName, leftNode);
  }
}
 
Example #18
Source File: SqlTypeUtil.java    From calcite with 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 #19
Source File: PigToSqlAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelDataType createRecordType(RelBuilder relBuilder, List<Integer> fields) {
  final List<String> destNames = new ArrayList<>();
  final List<RelDataType> destTypes = new ArrayList<>();
  for (Integer index : fields) {
    final RelDataTypeField field = relBuilder.peek().getRowType().getFieldList().get(index);
    destNames.add(field.getName());
    destTypes.add(field.getType());
  }
  return TYPE_FACTORY.createStructType(destTypes, destNames);
}
 
Example #20
Source File: LogicalJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode left,
    RelNode right, RexNode condition, JoinRelType joinType,
    Set<String> variablesStopped, boolean semiJoinDone,
    ImmutableList<RelDataTypeField> systemFieldList) {
  this(cluster, traitSet, ImmutableList.of(), left, right, condition,
      CorrelationId.setOf(variablesStopped), joinType, semiJoinDone,
      systemFieldList);
}
 
Example #21
Source File: FileTableScan.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType deriveRowType() {
  final List<RelDataTypeField> fieldList = table.getRowType().getFieldList();
  final RelDataTypeFactory.Builder builder =
      getCluster().getTypeFactory().builder();
  for (int field : fields) {
    builder.add(fieldList.get(field));
  }
  return builder.build();
}
 
Example #22
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns a map from field names to indexes. */
public static Map<String, Integer> mapNameToIndex(List<RelDataTypeField> fields) {
  ImmutableMap.Builder<String, Integer> output = ImmutableMap.builder();
  for (RelDataTypeField field : fields) {
    output.put(field.getName(), field.getIndex());
  }
  return output.build();
}
 
Example #23
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 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);
}
 
Example #24
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls project above the join from its RHS input. Enforces nullability
 * for join output.
 *
 * @param join          Join
 * @param project       Original project as the right-hand input of the join
 * @param nullIndicatorPos Position of null indicator
 * @return the subtree with the new Project at the root
 */
private RelNode projectJoinOutputWithNullability(LogicalJoin join, LogicalProject project, int nullIndicatorPos) {
    final RelDataTypeFactory typeFactory = join.getCluster().getTypeFactory();
    final RelNode left = join.getLeft();
    final JoinRelType joinType = join.getJoinType();

    RexInputRef nullIndicator = RexBuilder.getRexFactory().makeInputRef(nullIndicatorPos, typeFactory
            .createTypeWithNullability(join.getRowType().getFieldList().get(nullIndicatorPos).getType(), true));

    // now create the new project
    List<Pair<RexNode, String>> newProjExprs = new ArrayList<>();

    // project everything from the LHS and then those from the original
    // projRel
    List<RelDataTypeField> leftInputFields = left.getRowType().getFieldList();

    for (int i = 0; i < leftInputFields.size(); i++) {
        newProjExprs.add(RexInputRef.of2(i, leftInputFields));
    }

    // Marked where the projected expr is coming from so that the types will
    // become nullable for the original projections which are now coming out
    // of the nullable side of the OJ.
    boolean projectPulledAboveLeftCorrelator = joinType.generatesNullsOnRight();

    for (Pair<RexNode, String> pair : project.getNamedProjects()) {
        RexNode newProjExpr = removeCorrelationExpr(pair.left, projectPulledAboveLeftCorrelator, nullIndicator);

        newProjExprs.add(Pair.of(newProjExpr, pair.right));
    }

    return relBuilder.push(join).projectNamed(Pair.left(newProjExprs), Pair.right(newProjExprs), true).build();
}
 
Example #25
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RelDataType createTypeFromProjection(RelDataType type,
    List<String> columnNameList, RelDataTypeFactory typeFactory,
    boolean caseSensitive) {
  // If the names in columnNameList and type have case-sensitive differences,
  // the resulting type will use those from type. These are presumably more
  // canonical.
  final List<RelDataTypeField> fields =
      new ArrayList<>(columnNameList.size());
  for (String name : columnNameList) {
    RelDataTypeField field = type.getField(name, caseSensitive, false);
    fields.add(type.getFieldList().get(field.getIndex()));
  }
  return typeFactory.createStructType(fields);
}
 
Example #26
Source File: SqlTypeUtil.java    From Bats with 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 #27
Source File: LateralJoinPrel.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RelNode rename(RelNode input, List<RelDataTypeField> inputFields, List<String> outputFieldNames) {
  List<RexNode> exprs = Lists.newArrayList();

  for (RelDataTypeField field : inputFields) {
    RexNode expr = input.getCluster().getRexBuilder().makeInputRef(field.getType(), field.getIndex());
    exprs.add(expr);
  }

  RelDataType rowType = RexUtil.createStructType(input.getCluster().getTypeFactory(),
      exprs, outputFieldNames, null);

  ProjectPrel proj = new ProjectPrel(input.getCluster(), input.getTraitSet(), input, exprs, rowType);

  return proj;
}
 
Example #28
Source File: AddProjectOnPartialJoinVisitor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Prel visitJoin(JoinPrel prel, Void value) throws RuntimeException {

  List<RelNode> children = Lists.newArrayList();

  for(Prel child : prel){
    child = child.accept(this, null);
    children.add(child);
  }

  if ((prel.getRowType().getFieldCount() != prel.getInputRowType().getFieldCount())
      || !(prel.getRowType().getFieldNames().equals(prel.getInputRowType().getFieldNames()))) {
    List<String> outputFieldNames = new ArrayList<>();
    List<RexNode> exprs = new ArrayList<>();
    List<RelDataTypeField> fields = prel.getRowType().getFieldList();
    for (RelDataTypeField field : fields) {
      RexNode expr = prel.getCluster().getRexBuilder().makeInputRef(field.getType(), field.getIndex());
      exprs.add(expr);
      outputFieldNames.add(field.getName());
    }

    RelDataType rowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), exprs, outputFieldNames);

    JoinPrel newJoin = (JoinPrel) prel.copy(prel.getTraitSet(), children);
    ProjectPrel project = ProjectPrel.create(newJoin.getCluster(), newJoin.getTraitSet(), newJoin, exprs, rowType);
    return (Prel) project;
  }

  return (Prel) prel.copy(prel.getTraitSet(), children);
}
 
Example #29
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Pulls a {@link Project} above a {@link Correlate} from its RHS input.
 * Enforces nullability for join output.
 *
 * @param correlate  Correlate
 * @param project the original project as the RHS input of the join
 * @param isCount Positions which are calls to the <code>COUNT</code>
 *                aggregation function
 * @return the subtree with the new Project at the root
 */
private RelNode aggregateCorrelatorOutput(
    Correlate correlate,
    LogicalProject project,
    Set<Integer> isCount) {
  final RelNode left = correlate.getLeft();
  final JoinRelType joinType = correlate.getJoinType();

  // now create the new project
  final List<Pair<RexNode, String>> newProjects = new ArrayList<>();

  // Project everything from the LHS and then those from the original
  // project
  final List<RelDataTypeField> leftInputFields =
      left.getRowType().getFieldList();

  for (int i = 0; i < leftInputFields.size(); i++) {
    newProjects.add(RexInputRef.of2(i, leftInputFields));
  }

  // Marked where the projected expr is coming from so that the types will
  // become nullable for the original projections which are now coming out
  // of the nullable side of the OJ.
  boolean projectPulledAboveLeftCorrelator =
      joinType.generatesNullsOnRight();

  for (Pair<RexNode, String> pair : project.getNamedProjects()) {
    RexNode newProjExpr =
        removeCorrelationExpr(
            pair.left,
            projectPulledAboveLeftCorrelator,
            isCount);
    newProjects.add(Pair.of(newProjExpr, pair.right));
  }

  return relBuilder.push(correlate)
      .projectNamed(Pair.left(newProjects), Pair.right(newProjects), true)
      .build();
}
 
Example #30
Source File: RelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Ensures that the field names match those given.
 *
 * <p>If all fields have the same name, adds nothing;
 * if any fields do not have the same name, adds a {@link Project}.
 *
 * <p>Note that the names can be short-lived. Other {@code RelBuilder}
 * operations make no guarantees about the field names of the rows they
 * produce.
 *
 * @param fieldNames List of desired field names; may contain null values or
 * have fewer fields than the current row type
 */
public RelBuilder rename(List<String> fieldNames) {
  final List<String> oldFieldNames = peek().getRowType().getFieldNames();
  Preconditions.checkArgument(fieldNames.size() <= oldFieldNames.size(),
      "More names than fields");
  final List<String> newFieldNames = new ArrayList<>(oldFieldNames);
  for (int i = 0; i < fieldNames.size(); i++) {
    final String s = fieldNames.get(i);
    if (s != null) {
      newFieldNames.set(i, s);
    }
  }
  if (oldFieldNames.equals(newFieldNames)) {
    return this;
  }
  if (peek() instanceof Values) {
    // Special treatment for VALUES. Re-build it rather than add a project.
    final Values v = (Values) build();
    final RelDataTypeFactory.Builder b = getTypeFactory().builder();
    for (Pair<String, RelDataTypeField> p
        : Pair.zip(newFieldNames, v.getRowType().getFieldList())) {
      b.add(p.left, p.right.getType());
    }
    return values(v.tuples, b.build());
  }

  return project(fields(), newFieldNames, true);
}