Ejemplo n.º 1
0
 /** Deduces the names and types of a table's columns by reading the first line of a CSV file. */
 static RelDataType deduceRowType(
     JavaTypeFactory typeFactory, File file, List<CsvFieldType> fieldTypes) {
   final List<RelDataType> types = new ArrayList<RelDataType>();
   final List<String> names = new ArrayList<String>();
   CSVReader reader = null;
   try {
     reader = openCsv(file);
     final String[] strings = reader.readNext();
     for (String string : strings) {
       final String name;
       final CsvFieldType fieldType;
       final int colon = string.indexOf(':');
       if (colon >= 0) {
         name = string.substring(0, colon);
         String typeString = string.substring(colon + 1);
         fieldType = CsvFieldType.of(typeString);
         if (fieldType == null) {
           System.out.println(
               "WARNING: Found unknown type: "
                   + typeString
                   + " in file: "
                   + file.getAbsolutePath()
                   + " for column: "
                   + name
                   + ". Will assume the type of column is string");
         }
       } else {
         name = string;
         fieldType = null;
       }
       final RelDataType type;
       if (fieldType == null) {
         type = typeFactory.createJavaType(String.class);
       } else {
         type = fieldType.toType(typeFactory);
       }
       names.add(name);
       types.add(type);
       if (fieldTypes != null) {
         fieldTypes.add(fieldType);
       }
     }
   } catch (IOException e) {
     // ignore
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         // ignore
       }
     }
   }
   if (names.isEmpty()) {
     names.add("line");
     types.add(typeFactory.createJavaType(String.class));
   }
   return typeFactory.createStructType(Pair.zip(names, types));
 }
Ejemplo n.º 2
0
 private static List<Integer> nullableArgs(List<Integer> list0, List<RelDataType> types) {
   final List<Integer> list = new ArrayList<Integer>();
   for (Pair<Integer, RelDataType> pair : Pair.zip(list0, types)) {
     if (pair.right.isNullable()) {
       list.add(pair.left);
     }
   }
   return list;
 }
Ejemplo n.º 3
0
 private void translateOp2(String op, String name, RexLiteral right) {
   if (op == null) {
     // E.g.: {deptno: 100}
     eqMap.put(name, right);
   } else {
     // E.g. {deptno: {$lt: 100}}
     // which may later be combined with other conditions:
     // E.g. {deptno: [$lt: 100, $gt: 50]}
     multimap.put(name, Pair.of(op, right));
   }
 }
Ejemplo n.º 4
0
 /**
  * Catch-all implementation for {@link BuiltInMetadata.Size#averageRowSize()}, invoked using
  * reflection.
  *
  * @see org.apache.calcite.rel.metadata.RelMetadataQuery#getAverageRowSize
  */
 public Double averageRowSize(RelNode rel, RelMetadataQuery mq) {
   final List<Double> averageColumnSizes = mq.getAverageColumnSizes(rel);
   if (averageColumnSizes == null) {
     return null;
   }
   Double d = 0d;
   final List<RelDataTypeField> fields = rel.getRowType().getFieldList();
   for (Pair<Double, RelDataTypeField> p : Pair.zip(averageColumnSizes, fields)) {
     if (p.left == null) {
       d += averageFieldValueSize(p.right);
     } else {
       d += p.left;
     }
   }
   return d;
 }
Ejemplo n.º 5
0
  /**
   * Push any equi join conditions that are not column references as Projections on top of the
   * children.
   *
   * @param factory Project factory to use.
   * @param inputRels inputs to a join
   * @param leftJoinKeys expressions for LHS of join key
   * @param rightJoinKeys expressions for RHS of join key
   * @param systemColCount number of system columns, usually zero. These columns are projected at
   *     the leading edge of the output row.
   * @param leftKeys on return this contains the join key positions from the new project rel on the
   *     LHS.
   * @param rightKeys on return this contains the join key positions from the new project rel on the
   *     RHS.
   * @return the join condition after the equi expressions pushed down.
   */
  public static RexNode projectNonColumnEquiConditions(
      ProjectFactory factory,
      RelNode[] inputRels,
      List<RexNode> leftJoinKeys,
      List<RexNode> rightJoinKeys,
      int systemColCount,
      List<Integer> leftKeys,
      List<Integer> rightKeys) {
    RelNode leftRel = inputRels[0];
    RelNode rightRel = inputRels[1];
    RexBuilder rexBuilder = leftRel.getCluster().getRexBuilder();
    RexNode outJoinCond = null;

    int origLeftInputSize = leftRel.getRowType().getFieldCount();
    int origRightInputSize = rightRel.getRowType().getFieldCount();

    List<RexNode> newLeftFields = new ArrayList<RexNode>();
    List<String> newLeftFieldNames = new ArrayList<String>();

    List<RexNode> newRightFields = new ArrayList<RexNode>();
    List<String> newRightFieldNames = new ArrayList<String>();
    int leftKeyCount = leftJoinKeys.size();
    int i;

    for (i = 0; i < origLeftInputSize; i++) {
      final RelDataTypeField field = leftRel.getRowType().getFieldList().get(i);
      newLeftFields.add(rexBuilder.makeInputRef(field.getType(), i));
      newLeftFieldNames.add(field.getName());
    }

    for (i = 0; i < origRightInputSize; i++) {
      final RelDataTypeField field = rightRel.getRowType().getFieldList().get(i);
      newRightFields.add(rexBuilder.makeInputRef(field.getType(), i));
      newRightFieldNames.add(field.getName());
    }

    int newKeyCount = 0;
    List<Pair<Integer, Integer>> origColEqConds = new ArrayList<Pair<Integer, Integer>>();
    for (i = 0; i < leftKeyCount; i++) {
      RexNode leftKey = leftJoinKeys.get(i);
      RexNode rightKey = rightJoinKeys.get(i);

      if (leftKey instanceof RexInputRef && rightKey instanceof RexInputRef) {
        origColEqConds.add(
            Pair.of(((RexInputRef) leftKey).getIndex(), ((RexInputRef) rightKey).getIndex()));
      } else {
        newLeftFields.add(leftKey);
        newLeftFieldNames.add(null);
        newRightFields.add(rightKey);
        newRightFieldNames.add(null);
        newKeyCount++;
      }
    }

    for (i = 0; i < origColEqConds.size(); i++) {
      Pair<Integer, Integer> p = origColEqConds.get(i);
      RexNode leftKey = leftJoinKeys.get(i);
      RexNode rightKey = rightJoinKeys.get(i);
      leftKeys.add(p.left);
      rightKeys.add(p.right);
      RexNode cond =
          rexBuilder.makeCall(
              SqlStdOperatorTable.EQUALS,
              rexBuilder.makeInputRef(leftKey.getType(), systemColCount + p.left),
              rexBuilder.makeInputRef(
                  rightKey.getType(), systemColCount + origLeftInputSize + newKeyCount + p.right));
      if (outJoinCond == null) {
        outJoinCond = cond;
      } else {
        outJoinCond = rexBuilder.makeCall(SqlStdOperatorTable.AND, outJoinCond, cond);
      }
    }

    if (newKeyCount == 0) {
      return outJoinCond;
    }

    int newLeftOffset = systemColCount + origLeftInputSize;
    int newRightOffset = systemColCount + origLeftInputSize + origRightInputSize + newKeyCount;
    for (i = 0; i < newKeyCount; i++) {
      leftKeys.add(origLeftInputSize + i);
      rightKeys.add(origRightInputSize + i);
      RexNode cond =
          rexBuilder.makeCall(
              SqlStdOperatorTable.EQUALS,
              rexBuilder.makeInputRef(
                  newLeftFields.get(origLeftInputSize + i).getType(), newLeftOffset + i),
              rexBuilder.makeInputRef(
                  newRightFields.get(origRightInputSize + i).getType(), newRightOffset + i));
      if (outJoinCond == null) {
        outJoinCond = cond;
      } else {
        outJoinCond = rexBuilder.makeCall(SqlStdOperatorTable.AND, outJoinCond, cond);
      }
    }

    // added project if need to produce new keys than the original input
    // fields
    if (newKeyCount > 0) {
      leftRel =
          factory.createProject(
              leftRel, newLeftFields, SqlValidatorUtil.uniquify(newLeftFieldNames));
      rightRel =
          factory.createProject(
              rightRel, newRightFields, SqlValidatorUtil.uniquify(newRightFieldNames));
    }

    inputRels[0] = leftRel;
    inputRels[1] = rightRel;

    return outJoinCond;
  }