@Override protected Void visitUnion(Union node, Integer indent) { Iterator<Relation> relations = node.getRelations().iterator(); while (relations.hasNext()) { processRelation(relations.next(), indent); if (relations.hasNext()) { builder.append("UNION "); if (!node.isDistinct()) { builder.append("ALL "); } } } return null; }
@Override protected Void visitIntersect(Intersect node, Integer indent) { Iterator<Relation> relations = node.getRelations().iterator(); while (relations.hasNext()) { processRelation(relations.next(), indent); if (relations.hasNext()) { builder.append(indentString(indent)); builder.append("INTERSECT "); if (!node.isDistinct()) { builder.append("ALL "); } } } return null; }
@Override protected List<QuerySource> visitRelation(Relation node, QueryState state){ if(node instanceof Join){ return node.accept(this, state); }else if( node instanceof SampledRelation){ state.addException("Sampled relations are not supported"); return null; }else if( node instanceof AliasedRelation){ AliasedRelation ar = (AliasedRelation)node; state.setKeyValue("table_alias", ar.getAlias()); List<QuerySource> relations = ar.getRelation().accept(this, state); for(QuerySource rr : relations) rr.setAlias(ar.getAlias()); return relations; }else if( node instanceof QueryBody){ return node.accept(this, state); }else{ state.addException("Unable to parse node because it has an unknown type :"+node.getClass()); return null; } }
@Override protected Void visitIntersect(Intersect node, Integer indent) { Iterator<Relation> relations = node.getRelations().iterator(); while (relations.hasNext()) { processRelation(relations.next(), indent); if (relations.hasNext()) { builder.append("INTERSECT "); if (!node.isDistinct()) { builder.append("ALL "); } } } return null; }
@Override public Node visitSampledRelation(SqlBaseParser.SampledRelationContext context) { Relation child = (Relation) visit(context.aliasedRelation()); if (context.TABLESAMPLE() == null) { return child; } Optional<List<Expression>> stratifyOn = Optional.empty(); if (context.STRATIFY() != null) { stratifyOn = Optional.of(visit(context.stratify, Expression.class)); } return new SampledRelation( getLocation(context), child, getSamplingMethod((Token) context.sampleType().getChild(0).getPayload()), (Expression) visit(context.percentage), context.RESCALED() != null, stratifyOn); }
private Relation render(List<ForeignKey> keys) { if (keys.isEmpty()) { return QueryUtil.table(new QualifiedName(baseTable)); } ForeignKey key = keys.get(0); if (keys.size() == 1) { return new Join(Join.Type.INNER, QueryUtil.table(new QualifiedName(key.getSourceTable())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); } return new Join(Join.Type.INNER, render(keys.subList(1, keys.size())), QueryUtil.table(new QualifiedName(key.getDestinationTable())), Optional.of(new JoinOn(new ComparisonExpression( ComparisonExpression.Type.EQUAL, new QualifiedNameReference(QualifiedName.of( key.getSourceTable(), key.getSourceColumn())), new QualifiedNameReference(QualifiedName.of( key.getDestinationTable(), key.getDestinationColumn())))))); }
private void processRelation(Relation relation, Integer indent) { if (relation instanceof Table) { builder.append("TABLE ") .append(((Table) relation).getName()) .append('\n'); } else { process(relation, indent); } }
/** * Gets the sources to query from the provided Relation. Parsed relations are put inside the state * @param relation * @param state * @param searchReq * @return if the set with relations contains the query cache identifier */ private SourcesResult getSources(Relation relation, BasicQueryState state){ List<QuerySource> sources = relation.accept(relationParser, state); boolean useCache = false; ParseResult subQueryInfo = null; if(state.hasException()) return new SourcesResult(false, null); if(sources.size() < 1) { state.addException("Specify atleast one valid table to execute the query on!"); return new SourcesResult(false, null); } for(int i=0; i<sources.size(); i++){ if(sources.get(i).getSource().toLowerCase().equals(props.getProperty(Utils.PROP_QUERY_CACHE_TABLE, "query_cache"))){ useCache = true; sources.remove(i); i--; }else if(sources.get(i).isSubQuery()){ QuerySource qs = sources.get(i); QueryParser subQueryParser = new QueryParser(); try { subQueryInfo = subQueryParser.parse(qs.getSource(), qs.getQuery(), maxRows, props, tableColumnInfo); } catch (SQLException e) { state.addException("Unable to parse sub-query due to: "+e.getMessage()); } //sources.remove(i); //i--; } } heading.setTypes(this.typesForColumns(sources)); state.setRelations(sources); return new SourcesResult(useCache, subQueryInfo); }
private void processRelation(Relation relation, Integer indent) { // TODO: handle this properly if (relation instanceof Table) { builder.append("TABLE ") .append(((Table) relation).getName()) .append('\n'); } else { process(relation, indent); } }
@Override public Node visitAliasedRelation(SqlBaseParser.AliasedRelationContext context) { Relation child = (Relation) visit(context.relationPrimary()); if (context.identifier() == null) { return child; } return new AliasedRelation(getLocation(context), child, context.identifier().getText(), getColumnAliases(context.columnAliases())); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, List<GroupingElement> groupBy, Optional<Expression> having, List<SortItem> ordering, Optional<String> limit) { return query(new QuerySpecification( select, Optional.of(from), where, groupBy, having, ordering, limit)); }
public Type[] getRelationCoercion(Relation relation) { return relationCoercions.get(relation); }
public void addRelationCoercion(Relation relation, Type[] types) { relationCoercions.put(relation, types); }
private static Relation from(String catalog, SchemaTableName table) { return table(QualifiedName.of(catalog, table.getSchemaName(), table.getTableName())); }
public static Relation subquery(Query query) { return new TableSubquery(query); }
public static Relation aliased(Relation relation, String alias, List<String> columnAliases) { return new AliasedRelation(relation, alias, columnAliases); }
public static Query simpleQuery(Select select, Relation from) { return simpleQuery(select, from, Optional.empty(), ImmutableList.of()); }
public static Query simpleQuery(Select select, Relation from, List<SortItem> ordering) { return simpleQuery(select, from, Optional.empty(), ordering); }
public static Query simpleQuery(Select select, Relation from, Expression where) { return simpleQuery(select, from, Optional.of(where), ImmutableList.of()); }
public static Query simpleQuery(Select select, Relation from, Expression where, List<SortItem> ordering) { return simpleQuery(select, from, Optional.of(where), ordering); }
public static Query simpleQuery(Select select, Relation from, Optional<Expression> where, List<SortItem> ordering) { return simpleQuery(select, from, where, ImmutableList.of(), Optional.empty(), ordering, Optional.empty()); }
@Override public Relation render() { List<ForeignKey> keys = new ArrayList<>(); keys.addAll(joins); return render(keys); }
@Override public Relation render() { return null; }
/** * Render the join into a Relation which can be selected from in the AST. * @return Relation that formulates the join(s) needed. */ public Relation render();