Search in sources :

Example 66 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project hive by apache.

the class HiveJdbcImplementor method visit.

@Override
public Result visit(Sort e) {
    Result x = visitInput(e, 0, Clause.ORDER_BY, Clause.FETCH, Clause.OFFSET);
    Builder builder = x.builder(e);
    List<SqlNode> orderByList = Expressions.list();
    for (RelFieldCollation field : e.getCollation().getFieldCollations()) {
        builder.addOrderItem(orderByList, field);
    }
    // Create select list as we want to keep the column aliases
    // instead of producing STAR
    final List<SqlNode> selectList = new ArrayList<>();
    for (int i = 0; i < e.getRowType().getFieldCount(); i++) {
        RexInputRef ref = RexInputRef.of(i, e.getRowType());
        SqlNode sqlExpr = builder.context.toSql(null, ref);
        addSelect(selectList, sqlExpr, e.getRowType());
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
    if (!orderByList.isEmpty()) {
        builder.setOrderBy(new SqlNodeList(orderByList, POS));
        x = builder.result();
    }
    if (e.fetch != null) {
        builder = x.builder(e);
        builder.setFetch(builder.context.toSql(null, e.fetch));
        x = builder.result();
    }
    if (e.offset != null) {
        builder = x.builder(e);
        builder.setOffset(builder.context.toSql(null, e.offset));
        x = builder.result();
    }
    return x;
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) ArrayList(java.util.ArrayList) RexInputRef(org.apache.calcite.rex.RexInputRef) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 67 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Calc e) {
    Result x = visitChild(0, e.getInput());
    parseCorrelTable(e, x);
    final RexProgram program = e.getProgram();
    Builder builder = program.getCondition() != null ? x.builder(e, Clause.WHERE) : x.builder(e);
    if (!isStar(program)) {
        final List<SqlNode> selectList = new ArrayList<>();
        for (RexLocalRef ref : program.getProjectList()) {
            SqlNode sqlExpr = builder.context.toSql(program, ref);
            addSelect(selectList, sqlExpr, e.getRowType());
        }
        builder.setSelect(new SqlNodeList(selectList, POS));
    }
    if (program.getCondition() != null) {
        builder.setWhere(builder.context.toSql(program, program.getCondition()));
    }
    return builder.result();
}
Also used : RexProgram(org.apache.calcite.rex.RexProgram) ArrayList(java.util.ArrayList) RexLocalRef(org.apache.calcite.rex.RexLocalRef) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Example 68 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Aggregate e) {
    // "select a, b, sum(x) from ( ... ) group by a, b"
    final Result x = visitChild(0, e.getInput());
    final Builder builder;
    if (e.getInput() instanceof Project) {
        builder = x.builder(e);
        builder.clauses.add(Clause.GROUP_BY);
    } else {
        builder = x.builder(e, Clause.GROUP_BY);
    }
    List<SqlNode> groupByList = Expressions.list();
    final List<SqlNode> selectList = new ArrayList<>();
    for (int group : e.getGroupSet()) {
        final SqlNode field = builder.context.field(group);
        addSelect(selectList, field, e.getRowType());
        groupByList.add(field);
    }
    for (AggregateCall aggCall : e.getAggCallList()) {
        SqlNode aggCallSqlNode = builder.context.toSql(aggCall);
        if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) {
            aggCallSqlNode = dialect.rewriteSingleValueExpr(aggCallSqlNode);
        }
        addSelect(selectList, aggCallSqlNode, e.getRowType());
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
    if (!groupByList.isEmpty() || e.getAggCallList().isEmpty()) {
        // Some databases don't support "GROUP BY ()". We can omit it as long
        // as there is at least one aggregate function.
        builder.setGroupBy(new SqlNodeList(groupByList, POS));
    }
    return builder.result();
}
Also used : AggregateCall(org.apache.calcite.rel.core.AggregateCall) Project(org.apache.calcite.rel.core.Project) ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlSingleValueAggFunction(org.apache.calcite.sql.fun.SqlSingleValueAggFunction) SqlNode(org.apache.calcite.sql.SqlNode)

Example 69 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Project e) {
    Result x = visitChild(0, e.getInput());
    parseCorrelTable(e, x);
    if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
        return x;
    }
    final Builder builder = x.builder(e, Clause.SELECT);
    final List<SqlNode> selectList = new ArrayList<>();
    for (RexNode ref : e.getChildExps()) {
        SqlNode sqlExpr = builder.context.toSql(null, ref);
        addSelect(selectList, sqlExpr, e.getRowType());
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
    return builder.result();
}
Also used : ArrayList(java.util.ArrayList) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode)

Example 70 with SqlNodeList

use of org.apache.calcite.sql.SqlNodeList in project calcite by apache.

the class RelToSqlConverter method visit.

/**
 * @see #dispatch
 */
public Result visit(Sort e) {
    Result x = visitChild(0, e.getInput());
    Builder builder = x.builder(e, Clause.ORDER_BY);
    List<SqlNode> orderByList = Expressions.list();
    for (RelFieldCollation field : e.getCollation().getFieldCollations()) {
        builder.addOrderItem(orderByList, field);
    }
    if (!orderByList.isEmpty()) {
        builder.setOrderBy(new SqlNodeList(orderByList, POS));
        x = builder.result();
    }
    if (e.fetch != null) {
        builder = x.builder(e, Clause.FETCH);
        builder.setFetch(builder.context.toSql(null, e.fetch));
        x = builder.result();
    }
    if (e.offset != null) {
        builder = x.builder(e, Clause.OFFSET);
        builder.setOffset(builder.context.toSql(null, e.offset));
        x = builder.result();
    }
    return x;
}
Also used : RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) SqlNodeList(org.apache.calcite.sql.SqlNodeList) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNodeList (org.apache.calcite.sql.SqlNodeList)123 SqlNode (org.apache.calcite.sql.SqlNode)97 ArrayList (java.util.ArrayList)45 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)43 RelDataType (org.apache.calcite.rel.type.RelDataType)39 SqlCall (org.apache.calcite.sql.SqlCall)30 SqlSelect (org.apache.calcite.sql.SqlSelect)29 BitString (org.apache.calcite.util.BitString)23 RexNode (org.apache.calcite.rex.RexNode)13 SqlLiteral (org.apache.calcite.sql.SqlLiteral)11 ImmutableList (com.google.common.collect.ImmutableList)10 List (java.util.List)10 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)10 NlsString (org.apache.calcite.util.NlsString)9 RelNode (org.apache.calcite.rel.RelNode)8 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)7 SqlWriter (org.apache.calcite.sql.SqlWriter)7 SqlParserPos (org.apache.calcite.sql.parser.SqlParserPos)7 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)6