Java Code Examples for org.apache.calcite.sql.validate.SqlValidatorUtil#getAlias()

The following examples show how to use org.apache.calcite.sql.validate.SqlValidatorUtil#getAlias() . 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: Lattice.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static void populateAliases(SqlNode from, List<String> aliases,
    String current) {
  if (from instanceof SqlJoin) {
    SqlJoin join = (SqlJoin) from;
    populateAliases(join.getLeft(), aliases, null);
    populateAliases(join.getRight(), aliases, null);
  } else if (from.getKind() == SqlKind.AS) {
    populateAliases(SqlUtil.stripAs(from), aliases,
        SqlValidatorUtil.getAlias(from, -1));
  } else {
    if (current == null) {
      current = SqlValidatorUtil.getAlias(from, -1);
    }
    aliases.add(current);
  }
}
 
Example 2
Source File: Lattice.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static void populateAliases(SqlNode from, List<String> aliases,
    @Nullable String current) {
  if (from instanceof SqlJoin) {
    SqlJoin join = (SqlJoin) from;
    populateAliases(join.getLeft(), aliases, null);
    populateAliases(join.getRight(), aliases, null);
  } else if (from.getKind() == SqlKind.AS) {
    populateAliases(SqlUtil.stripAs(from), aliases,
        SqlValidatorUtil.getAlias(from, -1));
  } else {
    if (current == null) {
      current = SqlValidatorUtil.getAlias(from, -1);
    }
    aliases.add(current);
  }
}
 
Example 3
Source File: SqlImplementor.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void addSelect(List<SqlNode> selectList, SqlNode node, RelDataType rowType) {
    String name = rowType.getFieldNames().get(selectList.size());
    String alias = SqlValidatorUtil.getAlias(node, -1);
    if (alias == null || !alias.equals(name)) {
        node = SqlStdOperatorTable.AS.createCall(POS, node, new SqlIdentifier(name, POS));
    }
    selectList.add(node);
}
 
Example 4
Source File: RelToSqlConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public void addSelect(List<SqlNode> selectList, SqlNode node,
    RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  final String lowerName = name.toLowerCase(Locale.ROOT);
  if (lowerName.startsWith("expr$")) {
    // Put it in ordinalMap
    ordinalMap.put(lowerName, node);
  } else if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}
 
Example 5
Source File: SqlConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public String deriveAlias(
    SqlNode node,
    int ordinal) {
  if (node instanceof SqlIdentifier) {
    SqlIdentifier tempNode = ((SqlIdentifier) node);
    changeNamesIfTableIsTemporary(tempNode);
  }
  return SqlValidatorUtil.getAlias(node, ordinal);
}
 
Example 6
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void addSelect(List<SqlNode> selectList, SqlNode node,
                      RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(
      POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example 7
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void addSelect(List<SqlNode> selectList, SqlNode node,
                      RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  final String lowerName = name.toLowerCase(Locale.ROOT);
  if (lowerName.startsWith("expr$")) {
    // Put it in ordinalMap
    ordinalMap.put(lowerName, node);
  } else if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}
 
Example 8
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void addSelect(List<SqlNode> selectList, SqlNode node, RelDataType rowType) {
  final String name = rowType.getFieldNames().get(selectList.size());
  final String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example 9
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result based on a single relational expression.
 */
public Result result(SqlNode node, Collection<Clause> clauses, RelNode rel) {
  final String alias2 = SqlValidatorUtil.getAlias(node, -1);
  final String alias3 = alias2 != null ? alias2 : "t";
  final String alias4 =
      SqlValidatorUtil.uniquify(
          alias3, aliasSet, SqlValidatorUtil.EXPR_SUGGESTER);
  final String alias5 = alias2 == null || !alias2.equals(alias4) ? alias4
      : null;
  return new Result(node, clauses, alias5,
      Collections.singletonList(Pair.of(alias4, rel.getRowType())));
}
 
Example 10
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
private void addSelect(
    List<SqlNode> selectList, SqlNode node, RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (name.toLowerCase().startsWith("expr$")) {
    //Put it in ordinalMap
    ordinalMap.put(name.toLowerCase(), node);
  } else if (alias == null || !alias.equals(name)) {
    node = SqlStdOperatorTable.AS.createCall(
        POS, node, new SqlIdentifier(name, POS));
  }
  selectList.add(node);
}
 
Example 11
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void addSelect(List<SqlNode> selectList, SqlNode node,
    RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}
 
Example 12
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public void addSelect(List<SqlNode> selectList, SqlNode node,
    RelDataType rowType) {
  String name = rowType.getFieldNames().get(selectList.size());
  String alias = SqlValidatorUtil.getAlias(node, -1);
  if (alias == null || !alias.equals(name)) {
    node = as(node, name);
  }
  selectList.add(node);
}
 
Example 13
Source File: DremioRelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private List<SqlNode> addJoinChildSelectNodes(SqlNode node, Set<String> usedNames) {
  final SqlNode childNode = (node.getKind() == SqlKind.AS) ? ((SqlBasicCall) node).getOperands()[0] : node;

  if (childNode.getKind() == SqlKind.JOIN) {
    final SqlJoin join = (SqlJoin)childNode;
    // Delegate to the children of the join to get the node list.
    final List<SqlNode> leftList = addJoinChildSelectNodes(join.getLeft(), usedNames);
    final List<SqlNode> rightList = addJoinChildSelectNodes(join.getRight(), usedNames);
    if (leftList == null || rightList == null) {
      // Not possible to get the nodes of one or the other child, abandon the effort.
      return null;
    }

    return ImmutableList.<SqlNode>builder().addAll(leftList).addAll(rightList).build();
  }

  if (childNode.getKind() != SqlKind.SELECT || ((SqlSelect)childNode).getSelectList() == null) {
    return null;
  }

  // Although we are expanding the * into a list of column references, don't do anything but the expansion as
  // the underlying references will have any necessary modifications (ie addition of explicit casts) done to them
  // already.
  final String tableAlias = SqlValidatorUtil.getAlias(node, -1);
  final List<SqlNode> selectList = new ArrayList<>();
  ((SqlSelect) childNode).getSelectList().getList().stream().forEach(n -> {
    String colAlias = SqlValidatorUtil.getAlias(n, -1);
    if (null == colAlias) {
      // Guard against possible null aliases being returned by generating a unique value.
      colAlias = SqlValidatorUtil.uniquify(colAlias, usedNames, SqlValidatorUtil.EXPR_SUGGESTER);
    } else if (colAlias.equals("\"*\"")) {
      // If * is used as an alias, it ends up getting double quoted when it should not be.
      colAlias = "*";
    }
    final List<String> names = (tableAlias != null) ? ImmutableList.of(tableAlias, colAlias) : ImmutableList.of(colAlias);

    if (n.getKind() == SqlKind.IDENTIFIER) {
      // Ensure we have unique names being used.
      final String alias = SqlValidatorUtil.uniquify(colAlias, usedNames, SqlValidatorUtil.EXPR_SUGGESTER);

      selectList.add(SqlStdOperatorTable.AS.createCall(
        POS,
        new SqlIdentifier(names, n.getParserPosition()),
        new SqlIdentifier(alias, POS)));
    } else if (n.getKind() == SqlKind.AS) {
      selectList.add(new SqlIdentifier(names, POS));
    } else {
      selectList.add(n);
    }

    usedNames.add(colAlias);
  });

  return selectList;
}
 
Example 14
Source File: SqlImplementor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Once you have a Result of implementing a child relational expression,
 * call this method to create a Builder to implement the current relational
 * expression by adding additional clauses to the SQL query.
 *
 * <p>You need to declare which clauses you intend to add. If the clauses
 * are "later", you can add to the same query. For example, "GROUP BY" comes
 * after "WHERE". But if they are the same or earlier, this method will
 * start a new SELECT that wraps the previous result.
 *
 * <p>When you have called
 * {@link Builder#setSelect(SqlNodeList)},
 * {@link Builder#setWhere(SqlNode)} etc. call
 * {@link Builder#result(SqlNode, Collection, RelNode, Map)}
 * to fix the new query.
 *
 * @param rel Relational expression being implemented
 * @param clauses Clauses that will be generated to implement current
 *                relational expression
 * @return A builder
 */
public Builder builder(RelNode rel, Clause... clauses) {
  final boolean needNew = needNewSubQuery(rel, clauses);
  SqlSelect select;
  Expressions.FluentList<Clause> clauseList = Expressions.list();
  if (needNew) {
    select = subSelect();
  } else {
    select = asSelect();
    clauseList.addAll(this.clauses);
  }
  clauseList.appendAll(clauses);
  final Context newContext;
  Map<String, RelDataType> newAliases = null;
  final SqlNodeList selectList = select.getSelectList();
  if (selectList != null) {
    newContext = new Context(dialect, selectList.size()) {
      public SqlNode field(int ordinal) {
        final SqlNode selectItem = selectList.get(ordinal);
        switch (selectItem.getKind()) {
        case AS:
          return ((SqlCall) selectItem).operand(0);
        }
        return selectItem;
      }

      @Override public SqlNode orderField(int ordinal) {
        // If the field expression is an unqualified column identifier
        // and matches a different alias, use an ordinal.
        // For example, given
        //    SELECT deptno AS empno, empno AS x FROM emp ORDER BY emp.empno
        // we generate
        //    SELECT deptno AS empno, empno AS x FROM emp ORDER BY 2
        // "ORDER BY empno" would give incorrect result;
        // "ORDER BY x" is acceptable but is not preferred.
        final SqlNode node = field(ordinal);
        if (node instanceof SqlIdentifier
            && ((SqlIdentifier) node).isSimple()) {
          final String name = ((SqlIdentifier) node).getSimple();
          for (Ord<SqlNode> selectItem : Ord.zip(selectList)) {
            if (selectItem.i != ordinal) {
              final String alias =
                  SqlValidatorUtil.getAlias(selectItem.e, -1);
              if (name.equalsIgnoreCase(alias)) {
                return SqlLiteral.createExactNumeric(
                    Integer.toString(ordinal + 1), SqlParserPos.ZERO);
              }
            }
          }
        }
        return node;
      }
    };
  } else {
    boolean qualified =
        !dialect.hasImplicitTableAlias() || aliases.size() > 1;
    // basically, we did a subSelect() since needNew is set and neededAlias is not null
    // now, we need to make sure that we need to update the alias context.
    // if our aliases map has a single element:  <neededAlias, rowType>,
    // then we don't need to rewrite the alias but otherwise, it should be updated.
    if (needNew
        && neededAlias != null
        && (aliases.size() != 1 || !aliases.containsKey(neededAlias))) {
      newAliases =
          ImmutableMap.of(neededAlias, rel.getInput(0).getRowType());
      newContext = aliasContext(newAliases, qualified);
    } else {
      newContext = aliasContext(aliases, qualified);
    }
  }
  return new Builder(rel, clauseList, select, newContext, isAnon(),
      needNew && !aliases.containsKey(neededAlias) ? newAliases : aliases);
}