Search in sources :

Example 26 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestSetQueryParsing method testMultipleValues.

@Test
public void testMultipleValues() {
    SetQuery setQuery = new SetQuery(Operation.UNION);
    setQuery.setAll(true);
    Select select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c1"));
    Query query = new Query();
    query.setSelect(select);
    setQuery.setLeftQuery(query);
    select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c2"));
    query = new Query();
    query.setSelect(select);
    setQuery.setRightQuery(query);
    // $NON-NLS-1$
    TestParser.helpTest(// $NON-NLS-1$
    "values (c1), (c2)", // $NON-NLS-1$
    "SELECT c1 UNION ALL SELECT c2", setQuery);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) Select(org.teiid.query.sql.lang.Select) Test(org.junit.Test)

Example 27 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class TestSetQueryParsing method testThreeUnions.

/**
 * select c1 from g1 union select c2 from g2 union all select c3 from g3 union select c4 from g4
 */
@Test
public void testThreeUnions() {
    SetQuery setQuery = new SetQuery(Operation.UNION);
    setQuery.setAll(false);
    // $NON-NLS-1$
    GroupSymbol g = new GroupSymbol("g1");
    From from = new From();
    from.addGroup(g);
    Select select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c1"));
    Query query = new Query();
    query.setSelect(select);
    query.setFrom(from);
    setQuery.setLeftQuery(query);
    // $NON-NLS-1$
    g = new GroupSymbol("g2");
    from = new From();
    from.addGroup(g);
    select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c2"));
    query = new Query();
    query.setSelect(select);
    query.setFrom(from);
    setQuery.setRightQuery(query);
    // $NON-NLS-1$
    g = new GroupSymbol("g3");
    from = new From();
    from.addGroup(g);
    select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c3"));
    query = new Query();
    query.setSelect(select);
    query.setFrom(from);
    setQuery = new SetQuery(SetQuery.Operation.UNION, true, setQuery, query);
    // $NON-NLS-1$
    g = new GroupSymbol("g4");
    from = new From();
    from.addGroup(g);
    select = new Select();
    // $NON-NLS-1$
    select.addSymbol(new ElementSymbol("c4"));
    query = new Query();
    query.setSelect(select);
    query.setFrom(from);
    setQuery = new SetQuery(SetQuery.Operation.UNION, false, setQuery, query);
    // $NON-NLS-1$
    TestParser.helpTest(// $NON-NLS-1$
    "select c1 from g1 union select c2 from g2 union all select c3 from g3 union select c4 from g4", // $NON-NLS-1$
    "SELECT c1 FROM g1 UNION SELECT c2 FROM g2 UNION ALL SELECT c3 FROM g3 UNION SELECT c4 FROM g4", setQuery);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Query(org.teiid.query.sql.lang.Query) SetQuery(org.teiid.query.sql.lang.SetQuery) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Select(org.teiid.query.sql.lang.Select) From(org.teiid.query.sql.lang.From) Test(org.junit.Test)

Example 28 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class CommandBuilder method expandAllSymbol.

/**
 * Convert the "*" in "select * from..." to the list of column names for the data source.
 */
protected void expandAllSymbol(Command command) {
    if (command instanceof Query) {
        Query query = (Query) command;
        Select select = query.getSelect();
        List<Expression> originalSymbols = select.getSymbols();
        List<Expression> expandedSymbols = new ArrayList<Expression>();
        for (Iterator<Expression> i = originalSymbols.iterator(); i.hasNext(); ) {
            Expression next = i.next();
            if (next instanceof MultipleElementSymbol) {
                MultipleElementSymbol allSymbol = (MultipleElementSymbol) next;
                expandedSymbols.addAll(allSymbol.getElementSymbols());
            } else {
                expandedSymbols.add(next);
            }
        }
        select.setSymbols(expandedSymbols);
    }
}
Also used : MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Query(org.teiid.query.sql.lang.Query) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.query.sql.lang.Select) ArrayList(java.util.ArrayList)

Example 29 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class XMLQueryVisitationStrategy method produceMsg.

/**
 * Produce a JDOM Element for an instance of a JDBC ResultSet object.
 * <br>
 * @param object for which the JDOM Element is to be produced.
 * @return the JDOM element of the results object that was converted to XML.
 * @exception JDOMException if there is an error producing XML.
 * @exception SQLException if there is an error walking through the ResultSet object.
 */
public Element produceMsg(ResultSet object, Element resultsElement) throws JDOMException, SQLException {
    // -----------------------------------
    try {
        ResultSetMetaData rmdata = object.getMetaData();
        List identList = new ArrayList(rmdata.getColumnCount());
        for (int i = 1; i <= rmdata.getColumnCount(); i++) {
            identList.add(new ElementSymbol(rmdata.getColumnName(i)));
        }
        Select select = new Select(identList);
        resultsElement = produceMsg(select, rmdata, resultsElement);
        // -------------------------
        // Add the Table element ...
        // -------------------------
        resultsElement.addContent(new Element(TagNames.Elements.TABLE));
        Element tableElement = resultsElement.getChild(TagNames.Elements.TABLE);
        int rowCount = 0;
        int colCount = rmdata.getColumnCount();
        while (object.next()) {
            // -------------------------
            // Add the ROW element ...
            // -------------------------
            Element rowElement = new Element(TagNames.Elements.TABLE_ROW);
            for (int i = 1; i <= colCount; i++) {
                // -------------------------
                // Add the Cell element ...
                // -------------------------
                Element cellElement = new Element(TagNames.Elements.TABLE_CELL);
                Object cellValue = object.getObject(i);
                if (cellValue != null) {
                    cellElement = produceMsg(cellValue, cellElement);
                } else {
                    cellElement = cellElement.addContent(TagNames.Elements.NULL);
                }
                rowElement.addContent(cellElement);
            }
            tableElement.addContent(rowElement);
            rowCount++;
        }
        Attribute rowCountAttribute = new Attribute(TagNames.Attributes.TABLE_ROW_COUNT, Integer.toString(rowCount));
        Attribute columnCountAttribute = new Attribute(TagNames.Attributes.TABLE_COLUMN_COUNT, Integer.toString(colCount));
        tableElement.setAttribute(rowCountAttribute);
        tableElement.setAttribute(columnCountAttribute);
    } catch (SQLException e) {
        // error while reading results
        throw (e);
    }
    return resultsElement;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Attribute(org.jdom.Attribute) Element(org.jdom.Element) Select(org.teiid.query.sql.lang.Select)

Example 30 with Select

use of org.teiid.query.sql.lang.Select in project teiid by teiid.

the class RuleDecomposeJoin method buildUnion.

private PlanNode buildUnion(PlanNode unionNode, PlanNode otherSide, List<Criteria> criteria, List<int[]> matches, List<PlanNode> branches, List<PlanNode> otherBranches, JoinType joinType) {
    SymbolMap symbolMap = (SymbolMap) unionNode.getParent().getProperty(Info.SYMBOL_MAP);
    SymbolMap otherSymbolMap = (SymbolMap) otherSide.getProperty(Info.SYMBOL_MAP);
    List<PlanNode> joins = new LinkedList<PlanNode>();
    for (int i = 0; i < matches.size(); i++) {
        int[] is = matches.get(i);
        PlanNode branch = branches.get(is[0]);
        PlanNode branchSource = createSource(unionNode.getParent().getGroups().iterator().next(), branch, symbolMap);
        PlanNode otherBranch = otherBranches.get(is[1]);
        PlanNode otherBranchSource = createSource(otherSide.getGroups().iterator().next(), otherBranch, otherSymbolMap);
        PlanNode newJoinNode = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
        newJoinNode.addLastChild(branchSource);
        newJoinNode.addLastChild(otherBranchSource);
        newJoinNode.setProperty(Info.JOIN_STRATEGY, JoinStrategyType.NESTED_LOOP);
        newJoinNode.setProperty(Info.JOIN_TYPE, joinType);
        newJoinNode.setProperty(Info.JOIN_CRITERIA, LanguageObject.Util.deepClone(criteria, Criteria.class));
        newJoinNode.addGroups(branchSource.getGroups());
        newJoinNode.addGroups(otherBranchSource.getGroups());
        PlanNode projectPlanNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
        newJoinNode.addAsParent(projectPlanNode);
        Select allSymbols = new Select(symbolMap.getKeys());
        allSymbols.addSymbols(otherSymbolMap.getKeys());
        if (i == 0) {
            QueryRewriter.makeSelectUnique(allSymbols, false);
        }
        projectPlanNode.setProperty(NodeConstants.Info.PROJECT_COLS, allSymbols.getSymbols());
        projectPlanNode.addGroups(newJoinNode.getGroups());
        joins.add(projectPlanNode);
    }
    PlanNode newUnion = RulePlanUnions.buildUnionTree(unionNode, joins);
    return newUnion;
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Select(org.teiid.query.sql.lang.Select) SymbolMap(org.teiid.query.sql.util.SymbolMap) Criteria(org.teiid.query.sql.lang.Criteria) LinkedList(java.util.LinkedList)

Aggregations

Select (org.teiid.query.sql.lang.Select)50 Query (org.teiid.query.sql.lang.Query)30 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)26 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)26 From (org.teiid.query.sql.lang.From)25 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)24 Test (org.junit.Test)17 SetQuery (org.teiid.query.sql.lang.SetQuery)17 Constant (org.teiid.query.sql.symbol.Constant)15 UnaryFromClause (org.teiid.query.sql.lang.UnaryFromClause)11 Limit (org.teiid.query.sql.lang.Limit)10 ArrayList (java.util.ArrayList)5 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)5 Reference (org.teiid.query.sql.symbol.Reference)5 Element (org.jdom.Element)4 Expression (org.teiid.query.sql.symbol.Expression)4 List (java.util.List)3 Attribute (org.jdom.Attribute)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3 LanguageObject (org.teiid.query.sql.LanguageObject)3