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;
}
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();
}
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();
}
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();
}
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;
}
Aggregations