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

The following examples show how to use org.apache.calcite.rel.type.RelDataType#getFieldNames() . 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 calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Derives the list of column names suitable for NATURAL JOIN. These are the
 * columns that occur exactly once on each side of the join.
 *
 * @param nameMatcher Whether matches are case-sensitive
 * @param leftRowType  Row type of left input to the join
 * @param rightRowType Row type of right input to the join
 * @return List of columns that occur once on each side
 */
public static List<String> deriveNaturalJoinColumnList(
    SqlNameMatcher nameMatcher,
    RelDataType leftRowType,
    RelDataType rightRowType) {
  final List<String> naturalColumnNames = new ArrayList<>();
  final List<String> leftNames = leftRowType.getFieldNames();
  final List<String> rightNames = rightRowType.getFieldNames();
  for (String name : leftNames) {
    if (nameMatcher.frequency(leftNames, name) == 1
        && nameMatcher.frequency(rightNames, name) == 1) {
      naturalColumnNames.add(name);
    }
  }
  return naturalColumnNames;
}
 
Example 2
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a relational expression that projects the given fields of the
 * input.
 *
 * <p>Optimizes if the fields are the identity projection.
 *
 * @param factory ProjectFactory
 * @param child Input relational expression
 * @param posList Source of each projected field
 * @return Relational expression that projects given fields
 */
public static RelNode createProject(final RelFactories.ProjectFactory factory,
    final RelNode child, final List<Integer> posList) {
  RelDataType rowType = child.getRowType();
  final List<String> fieldNames = rowType.getFieldNames();
  final RelBuilder relBuilder =
      RelBuilder.proto(factory).create(child.getCluster(), null);
  final List<RexNode> exprs = new AbstractList<RexNode>() {
    public int size() {
      return posList.size();
    }

    public RexNode get(int index) {
      final int pos = posList.get(index);
      return relBuilder.getRexBuilder().makeInputRef(child, pos);
    }
  };
  final List<String> names = Util.select(fieldNames, posList);
  return relBuilder
      .push(child)
      .projectNamed(exprs, names, false)
      .build();
}
 
Example 3
Source File: RelNodeConvertor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private static Schema logicProject(RelNode relNode) {
    LogicalProject project = (LogicalProject) relNode;
    Schema schema = convertRelNode(project.getInput());
    List<String> fieldNames = project.getInput().getRowType().getFieldNames();
    List<Expr> expr = getExprs(project.getChildExps(), null);
    RelDataType outRowType = project.getRowType();
    List<String> outFieldNames = outRowType.getFieldNames();
    ArrayList<Expr> outExpr = new ArrayList<>();

    List<RelDataTypeField> outputRel = relNode.getRowType().getFieldList();
    for (int i = 0; i < outputRel.size(); i++) {
        Expr expr1 = expr.get(i);
        SqlTypeName outType = outputRel.get(i).getType().getSqlTypeName();
        SqlTypeName inType = project.getChildExps().get(i).getType().getSqlTypeName();
        if (!outType.equals(inType)) {
            expr1 = new Expr(HBTOp.CAST, Arrays.asList(expr1, new Identifier(ExprExplain.type(outType))));
        }
        String outName = outputRel.get(i).getName();
        Identifier identifier = new Identifier(outName);
        if (!expr1.equals(identifier)) {
            expr1 = new Expr(HBTOp.AS_COLUMN_NAME, Arrays.asList(expr1, identifier));
        }
        outExpr.add(expr1);
    }
    return new MapSchema(schema, outExpr);
}
 
Example 4
Source File: SqlValidatorUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Derives the list of column names suitable for NATURAL JOIN. These are the
 * columns that occur exactly once on each side of the join.
 *
 * @param nameMatcher Whether matches are case-sensitive
 * @param leftRowType  Row type of left input to the join
 * @param rightRowType Row type of right input to the join
 * @return List of columns that occur once on each side
 */
public static List<String> deriveNaturalJoinColumnList(
    SqlNameMatcher nameMatcher,
    RelDataType leftRowType,
    RelDataType rightRowType) {
  final List<String> naturalColumnNames = new ArrayList<>();
  final List<String> leftNames = leftRowType.getFieldNames();
  final List<String> rightNames = rightRowType.getFieldNames();
  for (String name : leftNames) {
    if (nameMatcher.frequency(leftNames, name) == 1
        && nameMatcher.frequency(rightNames, name) == 1) {
      naturalColumnNames.add(name);
    }
  }
  return naturalColumnNames;
}
 
Example 5
Source File: MutableRels.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Equivalence to {@link org.apache.calcite.plan.RelOptUtil#createCastRel}
 * for {@link MutableRel}. */
public static MutableRel createCastRel(MutableRel rel,
    RelDataType castRowType, boolean rename) {
  RelDataType rowType = rel.rowType;
  if (RelOptUtil.areRowTypesEqual(rowType, castRowType, rename)) {
    // nothing to do
    return rel;
  }
  List<RexNode> castExps =
      RexUtil.generateCastExpressions(rel.cluster.getRexBuilder(),
          castRowType, rowType);
  final List<String> fieldNames =
      rename ? castRowType.getFieldNames() : rowType.getFieldNames();
  return MutableProject.of(rel, castExps, fieldNames);
}
 
Example 6
Source File: QuerySemantics.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private static void populateSemanticFields(RelDataType relDataType, VirtualDatasetState.Builder state){
  for (String colName : relDataType.getFieldNames()) {
    state.addColumns(
      Column.newBuilder()
        .setName(colName)
        .setExpression(Expression.newBuilder()
          .setType(ExpressionType.REFERENCE)
          .setValue(colName)));
  }
}
 
Example 7
Source File: StarColumnHelper.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean containsStarColumn(RelDataType type) {
  if (! type.isStruct()) {
    return false;
  }

  List<String> fieldNames = type.getFieldNames();

  for (String s : fieldNames) {
    if (s.startsWith(STAR_COLUMN)) {
      return true;
    }
  }

  return false;
}
 
Example 8
Source File: PrelUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static ProjectPushInfo getColumns(RelDataType rowType, List<RexNode> projects) {
  final List<String> fieldNames = rowType.getFieldNames();
  if (fieldNames.isEmpty()) {
    return null;
  }

  RefFieldsVisitor v = new RefFieldsVisitor(rowType);
  for (RexNode exp : projects) {
    PathSegment segment = exp.accept(v);
    v.addColumn(segment);
  }

  return v.getInfo();

}
 
Example 9
Source File: RelRoot.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a simple RelRoot. */
public static RelRoot of(RelNode rel, RelDataType rowType, SqlKind kind) {
  final ImmutableIntList refs =
      ImmutableIntList.identity(rowType.getFieldCount());
  final List<String> names = rowType.getFieldNames();
  return new RelRoot(rel, rowType, kind, Pair.zip(refs, names),
      RelCollations.EMPTY, new ArrayList<>());
}
 
Example 10
Source File: RexToExpr.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static List<NamedExpression> aggsToExpr(
    RelDataType rowType, RelNode input, ImmutableBitSet groupSet, List<AggregateCall> aggCalls) {
  final List<String> fields = rowType.getFieldNames();
  final List<String> childFields = input.getRowType().getFieldNames();
  final List<NamedExpression> aggExprs = Lists.newArrayList();
  for (Ord<AggregateCall> aggCall : Ord.zip(aggCalls)) {
    int aggExprOrdinal = groupSet.cardinality() + aggCall.i;
    FieldReference ref = FieldReference.getWithQuotedRef(fields.get(aggExprOrdinal));
    LogicalExpression expr = toExpr(aggCall.e, childFields);
    NamedExpression ne = new NamedExpression(expr, ref);
    aggExprs.add(ne);
  }
  return aggExprs;
}
 
Example 11
Source File: MutableRels.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Equivalence to {@link org.apache.calcite.plan.RelOptUtil#createCastRel}
 * for {@link MutableRel}. */
public static MutableRel createCastRel(MutableRel rel,
    RelDataType castRowType, boolean rename) {
  RelDataType rowType = rel.rowType;
  if (RelOptUtil.areRowTypesEqual(rowType, castRowType, rename)) {
    // nothing to do
    return rel;
  }
  List<RexNode> castExps =
      RexUtil.generateCastExpressions(rel.cluster.getRexBuilder(),
          castRowType, rowType);
  final List<String> fieldNames =
      rename ? castRowType.getFieldNames() : rowType.getFieldNames();
  return MutableProject.of(rel, castExps, fieldNames);
}
 
Example 12
Source File: StarColumnHelper.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static boolean containsStarColumn(RelDataType type) {
  if (! type.isStruct()) {
    return false;
  }

  List<String> fieldNames = type.getFieldNames();

  for (String fieldName : fieldNames) {
    if (SchemaPath.DYNAMIC_STAR.equals(fieldName)) {
      return true;
    }
  }

  return false;
}
 
Example 13
Source File: PrelUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static List<Ordering> getOrdering(RelCollation collation, RelDataType rowType) {
    List<Ordering> orderExpr = Lists.newArrayList();

    final List<String> childFields = rowType.getFieldNames();

    for (RelFieldCollation fc : collation.getFieldCollations()) {
        FieldReference fr = new FieldReference(childFields.get(fc.getFieldIndex()), ExpressionPosition.UNKNOWN,
                false);
        orderExpr.add(new Ordering(fc.getDirection(), fr, fc.nullDirection));
    }

    return orderExpr;
}
 
Example 14
Source File: AbstractIndexPlanGenerator.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static int getRowKeyIndex(RelDataType rowType, DrillScanRelBase origScan) {
  List<String> fieldNames = rowType.getFieldNames();
  int idx = 0;
  for (String field : fieldNames) {
    if (field.equalsIgnoreCase(((DbGroupScan)IndexPlanUtils.getGroupScan(origScan)).getRowKeyName())) {
      return idx;
    }
    idx++;
  }
  return -1;
}
 
Example 15
Source File: ElasticsearchToEnumerableConverter.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
        final ElasticsearchRelNode.Implementor esImplementor = new ElasticsearchRelNode.Implementor();
        esImplementor.visitChild(0, getInput());
        List<String> listField = esImplementor.getListField();
        List<String> listField2 = new ArrayList<String>();
        for (String field : listField){                            //数据处理函数expr$0 转为lower.fieldName
                listField2.add(field);
        }
        ElasticsearchTable esTable = esImplementor.elasticsearchTable;

        final RelDataType rowType2 = esImplementor.elasticsearchTable.getRowType2();
        final List<String> fieldNames1  = rowType2.getFieldNames();          //得到输出顺序
        PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType2,
                pref.prefer(JavaRowFormat.ARRAY));



        final RelDataType rowType = esImplementor.elasticsearchTable.getRowType();
        PhysType physType2 = PhysTypeImpl.of(implementor.getTypeFactory(), rowType,
                pref.prefer(JavaRowFormat.ARRAY));
//        esTable.setOutNames(fieldNames1);
        List<String> fieldNames = ElasticsearchRules.elasticsearchFieldNames(rowType2);
        List<ElasticSearchFieldType> typeList = new ArrayList<ElasticSearchFieldType>();
        for(int i = 0 ; i < fieldNames.size(); i++){

            Class type = physType.fieldClass(i);
            String typeName = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase();
            if("integer".equals(typeName)){
                typeName = "int";
            }
            typeList.add(ElasticSearchFieldType.of(typeName));
        }
        boolean oneColumFlag = false;
        if(typeList.size() == 1){
            oneColumFlag = true;
        }
        TwoTuple<List<Object>, List<Object[]>> listTwoTuple = esTable.find(typeList,listField,oneColumFlag);
//        List<Object[]> resultList = esTable.find(typeList, oneColumFlag);
        ConstantExpression constant = oneColumFlag ? Expressions.constant(listTwoTuple.first.toArray()) : Expressions.constant(listTwoTuple.second.toArray());
        Result result = implementor.result(physType2,
                Blocks.toBlock(Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, constant)));
        return result;
    }
 
Example 16
Source File: ElasticsearchToEnumerableConverter.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public Result implement(EnumerableRelImplementor implementor, Prefer pref) {
        final ElasticsearchRelNode.Implementor esImplementor = new ElasticsearchRelNode.Implementor();
        esImplementor.visitChild(0, getInput());
        List<String> listField = esImplementor.getListField();
        List<String> listField2 = new ArrayList<String>();
        for (String field : listField){                            //数据处理函数expr$0 转为lower.fieldName
                listField2.add(field);
        }
        ElasticsearchTable esTable = esImplementor.elasticsearchTable;

        final RelDataType rowType2 = esImplementor.elasticsearchTable.getRowType2();
        final List<String> fieldNames1  = rowType2.getFieldNames();          //得到输出顺序
        PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType2,
                pref.prefer(JavaRowFormat.ARRAY));



        final RelDataType rowType = esImplementor.elasticsearchTable.getRowType();
        PhysType physType2 = PhysTypeImpl.of(implementor.getTypeFactory(), rowType,
                pref.prefer(JavaRowFormat.ARRAY));
//        esTable.setOutNames(fieldNames1);
        List<String> fieldNames = ElasticsearchRules.elasticsearchFieldNames(rowType2);
        List<ElasticSearchFieldType> typeList = new ArrayList<ElasticSearchFieldType>();
        for(int i = 0 ; i < fieldNames.size(); i++){

            Class type = physType.fieldClass(i);
            String typeName = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase();
            if("integer".equals(typeName)){
                typeName = "int";
            }
            typeList.add(ElasticSearchFieldType.of(typeName));
        }
        boolean oneColumFlag = false;
        if(typeList.size() == 1){
            oneColumFlag = true;
        }
        TwoTuple<List<Object>, List<Object[]>> listTwoTuple = esTable.find(typeList,listField,oneColumFlag);
//        List<Object[]> resultList = esTable.find(typeList, oneColumFlag);
        ConstantExpression constant = oneColumFlag ? Expressions.constant(listTwoTuple.first.toArray()) : Expressions.constant(listTwoTuple.second.toArray());
        Result result = implementor.result(physType2,
                Blocks.toBlock(Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, constant)));
        return result;
    }
 
Example 17
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
ProjectFieldsVisitor(RelDataType rowType) {
    super(true);
    this.fieldNames = rowType.getFieldNames();
    this.fields = rowType.getFieldList();
}
 
Example 18
Source File: PrelUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public RefFieldsVisitor(RelDataType rowType) {
  super(true);
  this.fieldNames = rowType.getFieldNames();
  this.fields = rowType.getFieldList();
}
 
Example 19
Source File: SqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static RelRootPlus of(RelNode rel, RelDataType validatedRowType, SqlKind kind, boolean contextSensitive) {
  final ImmutableIntList refs = ImmutableIntList.identity(validatedRowType.getFieldCount());
  final List<String> names = validatedRowType.getFieldNames();
  return new RelRootPlus(rel, validatedRowType, kind, Pair.zip(refs, names), RelCollations.EMPTY, contextSensitive);
}
 
Example 20
Source File: SimpleRexRemap.java    From Bats with Apache License 2.0 4 votes vote down vote up
public FieldsMarker(RelDataType rowType) {
    super(true);
    this.fieldNames = rowType.getFieldNames();
    this.fields = rowType.getFieldList();
    this.stackDepth = 0;
}