public void addOnCondition(ParseNode node) throws SQLException { node.accept(onNodeVisitor); }
public static ParseNode rewrite(ParseNode where, ParseNodeRewriter rewriter) throws SQLException { if (where == null) { return null; } rewriter.reset(); return where.accept(rewriter); }
/** * Returns whether this ParseNode is a SubqueryParseNode * or contains any SubqueryParseNode descendant. */ public boolean hasSubquery() { SubqueryFinder finder = new SubqueryFinder(); try { this.accept(finder); } catch (SQLException e) { // Not possible. } return finder.hasSubquery; }
public static Expression compile(StatementContext context, ParseNode whereNode) throws SQLException { WhereExpressionCompiler viewWhereCompiler = new WhereExpressionCompiler(context, true); return whereNode.accept(viewWhereCompiler); }
final <T> List<T> acceptChildren(ParseNodeVisitor<T> visitor) throws SQLException { List<T> l = visitor.newElementList(children.size()); for (int i = 0; i < children.size(); i++) { T e = children.get(i).accept(visitor); visitor.addElement(l, e); } return l; }
private JoinSpec(JoinType type, ParseNode onNode, JoinTable joinTable, boolean singleValueOnly, ColumnResolver resolver) throws SQLException { this.type = type; this.onConditions = new ArrayList<EqualParseNode>(); this.joinTable = joinTable; this.singleValueOnly = singleValueOnly; this.dependencies = new HashSet<TableRef>(); this.onNodeVisitor = new OnNodeVisitor(resolver, onConditions, dependencies, joinTable, statement.getConnection()); if (onNode != null) { onNode.accept(this.onNodeVisitor); } }
@Override protected ParseNode leaveBooleanNode(ParseNode node, List<ParseNode> l) throws SQLException { columnResolveVisitor.reset(); node.accept(columnResolveVisitor); ColumnResolveVisitor.ColumnResolveType type = columnResolveVisitor.getColumnResolveType(); if (type != ColumnResolveVisitor.ColumnResolveType.NONE && type != ColumnResolveVisitor.ColumnResolveType.LOCAL) throw new SQLFeatureNotSupportedException("Does not support non-standard or non-equi correlated-subquery conditions."); return node; }
private static Expression compilePostFilterExpression(StatementContext context, List<ParseNode> postFilters) throws SQLException { if (postFilters.isEmpty()) return null; ExpressionCompiler expressionCompiler = new ExpressionCompiler(context); List<Expression> expressions = new ArrayList<Expression>(postFilters.size()); for (ParseNode postFilter : postFilters) { expressionCompiler.reset(); Expression expression = postFilter.accept(expressionCompiler); expressions.add(expression); } if (expressions.size() == 1) return expressions.get(0); return AndExpression.create(expressions); }
@Override protected ParseNode leaveBooleanNode(ParseNode node, List<ParseNode> l) throws SQLException { ParseNode translatedNode = IndexStatementRewriter.translate(node, dataResolver); expressionCompiler.reset(); try { translatedNode.accept(expressionCompiler); } catch (ColumnNotFoundException e) { extractedConditions.add(node); return null; } return translatedNode; }
public void addFilter(ParseNode filter) throws SQLException { if (joinSpecs.isEmpty()) { table.addFilter(filter); return; } WhereNodeVisitor visitor = new WhereNodeVisitor(origResolver, table, postFilters, Collections.<TableRef>singletonList(table.getTableRef()), isPrefilterAccepted, prefilterAcceptedTables, statement.getConnection()); filter.accept(visitor); }
/** * Rewrite the select statement by filtering out expression nodes from the WHERE clause * @param statement the select statement from which to filter. * @param removeNodes expression nodes to filter out of WHERE clause. * @return new select statement * @throws SQLException */ public static SelectStatement removeFromWhereClause(SelectStatement statement, Set<ParseNode> removeNodes) throws SQLException { if (removeNodes.isEmpty()) { return statement; } ParseNode where = statement.getWhere(); SelectStatementRewriter rewriter = new SelectStatementRewriter(removeNodes); where = where.accept(rewriter); // Return new SELECT statement with updated WHERE clause return NODE_FACTORY.select(statement, where); }
private SelectStatement applyPostFilters(SelectStatement statement, List<ParseNode> postFilters) throws SQLException { List<ParseNode> postFiltersRewrite = Lists.<ParseNode>newArrayListWithExpectedSize(postFilters.size()); for (ParseNode node : postFilters) { postFiltersRewrite.add(node.accept(this)); } if (statement.getGroupBy().isEmpty()) { ParseNode where = statement.getWhere(); if (where != null) { postFiltersRewrite.add(where); } return NODE_FACTORY.select(statement, combine(postFiltersRewrite)); } ParseNode having = statement.getHaving(); if (having != null) { postFiltersRewrite.add(having); } return NODE_FACTORY.select(statement, statement.getWhere(), combine(postFiltersRewrite)); }
public static SelectStatement rewrite(StatementContext context, SelectStatement statement, GroupBy groupBy) throws SQLException { ParseNode having = statement.getHaving(); if (having == null) { return statement; } HavingClauseVisitor visitor = new HavingClauseVisitor(context, groupBy); having.accept(visitor); statement = SelectStatementRewriter.moveFromHavingToWhereClause(statement, visitor.getMoveToWhereClauseExpressions()); return statement; }
public static Integer compile(StatementContext context, FilterableStatement statement) throws SQLException { LimitNode limitNode = statement.getLimit(); if (limitNode == null) { return null; } LimitParseNodeVisitor visitor = new LimitParseNodeVisitor(context); limitNode.getLimitParseNode().accept(visitor); return visitor.getLimit(); }
public static Integer compile(StatementContext context, FilterableStatement statement) throws SQLException { OffsetNode offsetNode = statement.getOffset(); if (offsetNode == null) { return null; } OffsetParseNodeVisitor visitor = new OffsetParseNodeVisitor(context); offsetNode.getOffsetParseNode().accept(visitor); return visitor.getOffset(); }
/** * Rewrite the select statement by filtering out expression nodes from the HAVING clause * and anding them with the WHERE clause. * @param statement the select statement from which to move the nodes. * @param moveNodes expression nodes to filter out of HAVING clause and add to WHERE clause. * @return new select statement * @throws SQLException */ public static SelectStatement moveFromHavingToWhereClause(SelectStatement statement, Set<ParseNode> moveNodes) throws SQLException { if (moveNodes.isEmpty()) { return statement; } ParseNode andNode = NODE_FACTORY.and(new ArrayList<ParseNode>(moveNodes)); ParseNode having = statement.getHaving(); SelectStatementRewriter rewriter = new SelectStatementRewriter(moveNodes); having = having.accept(rewriter); ParseNode where = statement.getWhere(); if (where == null) { where = andNode; } else { where = NODE_FACTORY.and(Arrays.asList(where,andNode)); } // Return new SELECT statement with updated WHERE and HAVING clauses return NODE_FACTORY.select(statement, where, having); }
protected List<ParseNode> generateDummyValues( ParseNode lhs, boolean multipleValues) throws SQLException { Expression expr = lhs.accept(expressionCompiler); PDataType type = expr.getDataType(); if (!multipleValues) { return Arrays.<ParseNode> asList(NODE_FACTORY.literal(type.getSampleValue(), type)); } return Arrays.<ParseNode> asList( NODE_FACTORY.literal(type.getSampleValue(), type), NODE_FACTORY.literal(type.getSampleValue(), type), NODE_FACTORY.literal(type.getSampleValue(), type)); }
@Override protected Void leaveBooleanNode(ParseNode node, List<Void> l) throws SQLException { columnRefVisitor.reset(); node.accept(columnRefVisitor); ColumnRefParseNodeVisitor.ColumnRefType type = columnRefVisitor.getContentType(joinTable.getTableRefs()); if (type == ColumnRefParseNodeVisitor.ColumnRefType.NONE || type == ColumnRefParseNodeVisitor.ColumnRefType.SELF_ONLY) { joinTable.addFilter(node); } else { throwAmbiguousJoinConditionException(); } return null; }
@Override public TableNode visit(JoinTableNode joinNode) throws SQLException { TableNode lhsNode = joinNode.getLHS(); TableNode rhsNode = joinNode.getRHS(); ParseNode onNode = joinNode.getOnNode(); TableNode normLhsNode = lhsNode.accept(this); TableNode normRhsNode = rhsNode.accept(this); parseNodeRewriter.reset(); ParseNode normOnNode = onNode == null ? null : onNode.accept(parseNodeRewriter); if (lhsNode == normLhsNode && rhsNode == normRhsNode && onNode == normOnNode) return joinNode; return NODE_FACTORY.join(joinNode.getType(), normLhsNode, normRhsNode, normOnNode, joinNode.isSingleValueOnly()); }
public static Expression compile(StatementContext context, SelectStatement statement, GroupBy groupBy) throws SQLException { ParseNode having = statement.getHaving(); if (having == null) { return null; } ExpressionCompiler expressionBuilder = new ExpressionCompiler(context, groupBy); Expression expression = having.accept(expressionBuilder); if (expression.getDataType() != PBoolean.INSTANCE) { throw TypeMismatchException.newException(PBoolean.INSTANCE, expression.getDataType(), expression.toString()); } if (LiteralExpression.isBooleanFalseOrNull(expression)) { context.setScanRanges(ScanRanges.NOTHING); return null; } else if (LiteralExpression.isTrue(expression)) { return null; } if (!expressionBuilder.isAggregate()) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.ONLY_AGGREGATE_IN_HAVING_CLAUSE).build().buildException(); } return expression; }