Exemplo n.º 1
0
 public List<SqlMoniker> getAllSchemaObjectNames(List<String> names) {
   List<SqlMoniker> result;
   switch (names.size()) {
     case 0:
       // looking for schema names
       result = new ArrayList<SqlMoniker>();
       for (MockSchema schema : schemas.values()) {
         result.add(new SqlMonikerImpl(schema.name, SqlMonikerType.Schema));
       }
       return result;
     case 1:
       // looking for table names in the given schema
       MockSchema schema = schemas.get(names.get(0));
       if (schema == null) {
         return Collections.emptyList();
       }
       result = new ArrayList<SqlMoniker>();
       for (String tableName : schema.tableNames) {
         result.add(new SqlMonikerImpl(tableName, SqlMonikerType.Table));
       }
       return result;
     default:
       return Collections.emptyList();
   }
 }
Exemplo n.º 2
0
 /**
  * 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 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(
     RelDataType leftRowType, RelDataType rightRowType) {
   List<String> naturalColumnNames = new ArrayList<String>();
   final List<String> leftNames = leftRowType.getFieldNames();
   final List<String> rightNames = rightRowType.getFieldNames();
   for (String name : leftNames) {
     if ((Collections.frequency(leftNames, name) == 1)
         && (Collections.frequency(rightNames, name) == 1)) {
       naturalColumnNames.add(name);
     }
   }
   return naturalColumnNames;
 }
Exemplo n.º 3
0
 /**
  * Resolves a multi-part identifier such as "SCHEMA.EMP.EMPNO" to a namespace. The returned
  * namespace may represent a schema, table, column, etc.
  *
  * @pre names.size() > 0
  * @post return != null
  */
 public static SqlValidatorNamespace lookup(SqlValidatorScope scope, List<String> names) {
   Util.pre(names.size() > 0, "names.size() > 0");
   SqlValidatorNamespace namespace = null;
   for (int i = 0; i < names.size(); i++) {
     String name = names.get(i);
     if (i == 0) {
       namespace = scope.resolve(name, null, null);
     } else {
       namespace = namespace.lookupChild(name);
     }
   }
   Util.permAssert(namespace != null, "post: namespace != null");
   return namespace;
 }
Exemplo n.º 4
0
 public Prepare.PreparingTable getTable(final List<String> names) {
   switch (names.size()) {
     case 1:
       // assume table in SALES schema (the original default)
       // if it's not supplied, because SqlValidatorTest is effectively
       // using SALES as its default schema.
       return tables.get(ImmutableList.of(defaultCatalog, defaultSchema, names.get(0)));
     case 2:
       return tables.get(ImmutableList.of(defaultCatalog, names.get(0), names.get(1)));
     case 3:
       return tables.get(names);
     default:
       return null;
   }
 }
Exemplo n.º 5
0
  public static void getSchemaObjectMonikers(
      SqlValidatorCatalogReader catalogReader, List<String> names, List<SqlMoniker> hints) {
    // Assume that the last name is 'dummy' or similar.
    List<String> subNames = Util.skipLast(names);
    hints.addAll(catalogReader.getAllSchemaObjectNames(subNames));

    // If the name has length 0, try prepending the name of the default
    // schema. So, the empty name would yield a list of tables in the
    // default schema, as well as a list of schemas from the above code.
    if (subNames.size() == 0) {
      hints.addAll(
          catalogReader.getAllSchemaObjectNames(
              Collections.singletonList(catalogReader.getSchemaName())));
    }
  }
Exemplo n.º 6
0
 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<RelDataTypeField>(columnNameList.size());
   for (String name : columnNameList) {
     RelDataTypeField field = type.getField(name, caseSensitive);
     fields.add(type.getFieldList().get(field.getIndex()));
   }
   return typeFactory.createStructType(fields);
 }
Exemplo n.º 7
0
 public RelOptTable getTableForMember(List<String> names) {
   final SqlValidatorTable table = catalogReader.getTable(names);
   final RelDataType rowType = table.getRowType();
   final List<RelCollation> collationList = deduceMonotonicity(table);
   if (names.size() < 3) {
     String[] newNames2 = {"CATALOG", "SALES", ""};
     List<String> newNames = new ArrayList<String>();
     int i = 0;
     while (newNames.size() < newNames2.length) {
       newNames.add(i, newNames2[i]);
       ++i;
     }
     names = newNames;
   }
   return createColumnSet(table, names, rowType, collationList);
 }
Exemplo n.º 8
0
  void andOrList(SqlWriter writer, SqlKind sepKind) {
    SqlBinaryOperator sepOp =
        sepKind == SqlKind.AND ? SqlStdOperatorTable.AND : SqlStdOperatorTable.OR;
    for (int i = 0; i < list.size(); i++) {
      SqlNode node = list.get(i);
      writer.sep(sepKind.name(), false);

      // The precedence pulling on the LHS of a node is the
      // right-precedence of the separator operator, except at the start
      // of the list; similarly for the RHS of a node. If the operator
      // has left precedence 4 and right precedence 5, the precedences
      // in a 3-node list will look as follows:
      //   0 <- node1 -> 4  5 <- node2 -> 4  5 <- node3 -> 0
      int lprec = (i == 0) ? 0 : sepOp.getRightPrec();
      int rprec = (i == (list.size() - 1)) ? 0 : sepOp.getLeftPrec();
      node.unparse(writer, lprec, rprec);
    }
  }
Exemplo n.º 9
0
 public boolean equalsDeep(SqlNode node, boolean fail) {
   if (!(node instanceof SqlNodeList)) {
     assert !fail : this + "!=" + node;
     return false;
   }
   SqlNodeList that = (SqlNodeList) node;
   if (this.size() != that.size()) {
     assert !fail : this + "!=" + node;
     return false;
   }
   for (int i = 0; i < list.size(); i++) {
     SqlNode thisChild = list.get(i);
     final SqlNode thatChild = that.list.get(i);
     if (!thisChild.equalsDeep(thatChild, fail)) {
       return false;
     }
   }
   return true;
 }
Exemplo n.º 10
0
  private static List<RelCollation> deduceMonotonicity(Prepare.PreparingTable table) {
    final List<RelCollation> collationList = new ArrayList<RelCollation>();

    // Deduce which fields the table is sorted on.
    int i = -1;
    for (RelDataTypeField field : table.getRowType().getFieldList()) {
      ++i;
      final SqlMonotonicity monotonicity = table.getMonotonicity(field.getName());
      if (monotonicity != SqlMonotonicity.NotMonotonic) {
        final RelFieldCollation.Direction direction =
            monotonicity.isDecreasing()
                ? RelFieldCollation.Direction.Descending
                : RelFieldCollation.Direction.Ascending;
        collationList.add(
            RelCollationImpl.of(
                new RelFieldCollation(i, direction, RelFieldCollation.NullDirection.UNSPECIFIED)));
      }
    }
    return collationList;
  }
Exemplo n.º 11
0
    private List<RelCollation> deduceMonotonicity(SqlValidatorTable table) {
      final RelDataType rowType = table.getRowType();
      final List<RelCollation> collationList = new ArrayList<RelCollation>();

      // 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(
              RelCollationImpl.of(
                  new RelFieldCollation(
                      i, direction, RelFieldCollation.NullDirection.UNSPECIFIED)));
        }
      }
      return collationList;
    }
Exemplo n.º 12
0
 // implement RelDataType
 public RelDataTypeField getField(String fieldName, boolean caseSensitive) {
   for (RelDataTypeField field : fieldList) {
     if (Util.match(caseSensitive, field.getName(), fieldName)) {
       return field;
     }
   }
   // Extra field
   if (fieldList.size() > 0) {
     final RelDataTypeField lastField = Iterables.getLast(fieldList);
     if (lastField.getName().equals("_extra")) {
       return new RelDataTypeFieldImpl(fieldName, -1, lastField.getType());
     }
   }
   return null;
 }
Exemplo n.º 13
0
 public void addTable(String name) {
   tableNames.add(name);
 }
Exemplo n.º 14
0
 public void add(SqlNode node) {
   list.add(node);
 }
Exemplo n.º 15
0
 public SqlNode[] toArray() {
   return list.toArray(new SqlNode[list.size()]);
 }
Exemplo n.º 16
0
 // implement Iterable<SqlNode>
 public Iterator<SqlNode> iterator() {
   return list.iterator();
 }
Exemplo n.º 17
0
 // implement RelDataType
 public int getFieldCount() {
   assert isStruct() : this;
   return fieldList.size();
 }
Exemplo n.º 18
0
 public SqlNode set(int n, SqlNode node) {
   return list.set(n, node);
 }
Exemplo n.º 19
0
 public int size() {
   return list.size();
 }
Exemplo n.º 20
0
 public SqlNode get(int n) {
   return list.get(n);
 }
  public int reduceExpr(int opOrdinal, List<Object> list) {
    final SqlParserUtil.ToTreeListItem betweenNode =
        (SqlParserUtil.ToTreeListItem) list.get(opOrdinal);
    SqlOperator op = betweenNode.getOperator();
    assert op == this;

    // Break the expression up into expressions. For example, a simple
    // expression breaks down as follows:
    //
    //            opOrdinal   endExp1
    //            |           |
    //     a + b BETWEEN c + d AND e + f
    //    |_____|       |_____|   |_____|
    //     exp0          exp1      exp2
    // Create the expression between 'BETWEEN' and 'AND'.
    final SqlParserPos pos = ((SqlNode) list.get(opOrdinal + 1)).getParserPosition();
    SqlNode exp1 = SqlParserUtil.toTreeEx(list, opOrdinal + 1, 0, SqlKind.AND);
    if ((opOrdinal + 2) >= list.size()) {
      SqlParserPos lastPos = ((SqlNode) list.get(list.size() - 1)).getParserPosition();
      final int line = lastPos.getEndLineNum();
      final int col = lastPos.getEndColumnNum() + 1;
      SqlParserPos errPos = new SqlParserPos(line, col, line, col);
      throw SqlUtil.newContextException(
          errPos, EigenbaseResource.instance().BetweenWithoutAnd.ex());
    }
    final Object o = list.get(opOrdinal + 2);
    if (!(o instanceof SqlParserUtil.ToTreeListItem)) {
      SqlParserPos errPos = ((SqlNode) o).getParserPosition();
      throw SqlUtil.newContextException(
          errPos, EigenbaseResource.instance().BetweenWithoutAnd.ex());
    }
    if (((SqlParserUtil.ToTreeListItem) o).getOperator().getKind() != SqlKind.AND) {
      SqlParserPos errPos = ((SqlParserUtil.ToTreeListItem) o).getPos();
      throw SqlUtil.newContextException(
          errPos, EigenbaseResource.instance().BetweenWithoutAnd.ex());
    }

    // Create the expression after 'AND', but stopping if we encounter an
    // operator of lower precedence.
    //
    // For example,
    //   a BETWEEN b AND c + d OR e
    // becomes
    //   (a BETWEEN b AND c + d) OR e
    // because OR has lower precedence than BETWEEN.
    SqlNode exp2 = SqlParserUtil.toTreeEx(list, opOrdinal + 3, getRightPrec(), SqlKind.OTHER);

    // Create the call.
    SqlNode exp0 = (SqlNode) list.get(opOrdinal - 1);
    SqlCall newExp =
        createCall(
            betweenNode.getPos(),
            exp0,
            exp1,
            exp2,
            SqlLiteral.createSymbol(flag, SqlParserPos.ZERO));

    // Replace all of the matched nodes with the single reduced node.
    SqlParserUtil.replaceSublist(list, opOrdinal - 1, opOrdinal + 4, newExp);

    // Return the ordinal of the new current node.
    return opOrdinal - 1;
  }