public Result translate(Project e) { // This variant is for the top project as we want to keep // the column aliases instead of producing STAR Result x = visitChild(0, e.getInput()); parseCorrelTable(e, 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(); }
private List<SqlNode> toSql(RexProgram program, List<RexNode> operandList) { final List<SqlNode> list = new ArrayList<>(); for (RexNode rex : operandList) { list.add(toSql(program, rex)); } return list; }
private List<SqlNode> toSql(RexProgram program, List<RexNode> operandList) { final List<SqlNode> list = new ArrayList<>(); for (RexNode rex : operandList) { list.add(toSql(program, rex)); } return list; }
private SqlCall toSql(RexProgram program, RexOver rexOver) { final RexWindow rexWindow = rexOver.getWindow(); final SqlNodeList partitionList = new SqlNodeList( toSql(program, rexWindow.partitionKeys), POS); orderNodes.add(toSql(program, rfc)); orderList, isRows, lowerBound, upperBound, allowPartial, POS); final List<SqlNode> nodeList = toSql(program, rexOver.getOperands()); final SqlCall aggFunctionCall = rexOver.getAggOperator().createCall(POS, nodeList);
/** @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(); }
/** @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(); }
/** @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; }
/** @see #dispatch */ public Result visit(Filter e) { final RelNode input = e.getInput(); Result x = visitChild(0, input); parseCorrelTable(e, x); if (input instanceof Aggregate) { final Builder builder; if (((Aggregate) input).getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.HAVING); } else { builder = x.builder(e, Clause.HAVING); } builder.setHaving(builder.context.toSql(null, e.getCondition())); return builder.result(); } else { final Builder builder = x.builder(e, Clause.WHERE); builder.setWhere(builder.context.toSql(null, e.getCondition())); return builder.result(); } }
/** @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; }
private SqlNode createSqlWindowBound(RexWindowBound rexWindowBound) { if (rexWindowBound.isCurrentRow()) { return SqlWindow.createCurrentRow(POS); } if (rexWindowBound.isPreceding()) { if (rexWindowBound.isUnbounded()) { return SqlWindow.createUnboundedPreceding(POS); } else { SqlNode literal = toSql(null, rexWindowBound.getOffset()); return SqlWindow.createPreceding(literal, POS); } } if (rexWindowBound.isFollowing()) { if (rexWindowBound.isUnbounded()) { return SqlWindow.createUnboundedFollowing(POS); } else { SqlNode literal = toSql(null, rexWindowBound.getOffset()); return SqlWindow.createFollowing(literal, POS); } } throw new AssertionError("Unsupported Window bound: " + rexWindowBound); }
private SqlNode createSqlWindowBound(RexWindowBound rexWindowBound) { if (rexWindowBound.isCurrentRow()) { return SqlWindow.createCurrentRow(POS); } if (rexWindowBound.isPreceding()) { if (rexWindowBound.isUnbounded()) { return SqlWindow.createUnboundedPreceding(POS); } else { SqlNode literal = toSql(null, rexWindowBound.getOffset()); return SqlWindow.createPreceding(literal, POS); } } if (rexWindowBound.isFollowing()) { if (rexWindowBound.isUnbounded()) { return SqlWindow.createUnboundedFollowing(POS); } else { SqlNode literal = toSql(null, rexWindowBound.getOffset()); return SqlWindow.createFollowing(literal, POS); } } throw new AssertionError("Unsupported Window bound: " + rexWindowBound); }
/** @see #dispatch */ public Result visit(Filter e) { final RelNode input = e.getInput(); Result x = visitChild(0, input); parseCorrelTable(e, x); if (input instanceof Aggregate) { final Builder builder; if (((Aggregate) input).getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.HAVING); } else { builder = x.builder(e, Clause.HAVING); } builder.setHaving(builder.context.toSql(null, e.getCondition())); return builder.result(); } else { final Builder builder = x.builder(e, Clause.WHERE); builder.setWhere(builder.context.toSql(null, e.getCondition())); return builder.result(); } }
private SqlNode toSql(RexProgram program, RexFieldCollation rfc) { SqlNode node = toSql(program, rfc.left); switch (rfc.getDirection()) { case DESCENDING: case STRICTLY_DESCENDING: node = SqlStdOperatorTable.DESC.createCall(POS, node); } if (rfc.getNullDirection() != dialect.defaultNullDirection(rfc.getDirection())) { switch (rfc.getNullDirection()) { case FIRST: node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node); break; case LAST: node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node); break; } } return node; }
private SqlNode toSql(RexProgram program, RexFieldCollation rfc) { SqlNode node = toSql(program, rfc.left); switch (rfc.getDirection()) { case DESCENDING: case STRICTLY_DESCENDING: node = SqlStdOperatorTable.DESC.createCall(POS, node); } if (rfc.getNullDirection() != dialect.defaultNullDirection(rfc.getDirection())) { switch (rfc.getNullDirection()) { case FIRST: node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node); break; case LAST: node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node); break; } } return node; }
/** @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(); }
void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { final boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(toSql(field)); }
public void addOrderItem(List<SqlNode> orderByList, RelFieldCollation field) { if (field.nullDirection != RelFieldCollation.NullDirection.UNSPECIFIED) { boolean first = field.nullDirection == RelFieldCollation.NullDirection.FIRST; SqlNode nullDirectionNode = dialect.emulateNullDirection(context.field(field.getFieldIndex()), first, field.direction.isDescending()); if (nullDirectionNode != null) { orderByList.add(nullDirectionNode); field = new RelFieldCollation(field.getFieldIndex(), field.getDirection(), RelFieldCollation.NullDirection.UNSPECIFIED); } } orderByList.add(context.toSql(field)); }
/** @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(); }
/** Converts a list of {@link RexNode} expressions to {@link SqlNode} * expressions. */ private SqlNodeList exprList(final Context context, List<? extends RexNode> exprs) { return new SqlNodeList( Lists.transform(exprs, e -> context.toSql(null, e)), POS); }
/** Converts a list of {@link RexNode} expressions to {@link SqlNode} * expressions. */ private SqlNodeList exprList(final Context context, List<? extends RexNode> exprs) { return new SqlNodeList( Lists.transform(exprs, e -> context.toSql(null, e)), POS); }