Search in sources :

Example 1 with TableReference

use of org.teiid.language.TableReference in project teiid by teiid.

the class TestMongoDBQueryExecution method testGeoFunctionInWhereWithGeometry.

@Test
public void testGeoFunctionInWhereWithGeometry() throws Exception {
    Table table = this.utility.createRuntimeMetadata().getTable("northwind.Categories");
    NamedTable namedTable = new NamedTable("Categories", "g0", table);
    ColumnReference colRef = new ColumnReference(namedTable, "CategoryName", table.getColumnByName("CategoryName"), String.class);
    DerivedColumn col = new DerivedColumn("CategoryName", colRef);
    Select select = new Select();
    select.setDerivedColumns(Arrays.asList(col));
    List<TableReference> tables = new ArrayList<TableReference>();
    tables.add(namedTable);
    select.setFrom(tables);
    final GeometryType geo = GeometryUtils.geometryFromClob(new ClobType(new ClobImpl("POLYGON ((1.0 2.0,3.0 4.0,5.0 6.0,1.0 2.0))")));
    Function function = new // $NON-NLS-1$
    Function(// $NON-NLS-1$
    "mongo.geoWithin", // $NON-NLS-1$
    Arrays.asList(colRef, new Literal(geo, GeometryType.class)), // $NON-NLS-2$
    Boolean.class);
    function.setMetadataObject(getFunctionMethod("mongo.geoWithin"));
    Comparison c = new Comparison(function, new Literal(true, Boolean.class), Comparison.Operator.EQ);
    select.setWhere(c);
    DBCollection dbCollection = helpExecute(select, new String[] { "Categories" }, 2);
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("CategoryName");
    // $NON-NLS-1$
    builder.push("$geoWithin");
    // $NON-NLS-1$
    builder.add("$geometry", "{\"type\":\"Polygon\",\"coordinates\":[[[1.0,2.0],[3.0,4.0],[5.0,6.0],[1.0,2.0]]]}");
    BasicDBObject result = new BasicDBObject();
    result.append("CategoryName", "$CategoryName");
    List<DBObject> pipeline = buildArray(new BasicDBObject("$match", builder.get()), new BasicDBObject("$project", result));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : NamedTable(org.teiid.language.NamedTable) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) ArrayList(java.util.ArrayList) ClobType(org.teiid.core.types.ClobType) GeometryType(org.teiid.core.types.GeometryType) Function(org.teiid.language.Function) TableReference(org.teiid.language.TableReference) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn) ClobImpl(org.teiid.core.types.ClobImpl) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 2 with TableReference

use of org.teiid.language.TableReference in project teiid by teiid.

the class CoherenceVisitor method visit.

public void visit(Select query) {
    super.visit(query);
    Iterator<DerivedColumn> selectSymbolItr = query.getDerivedColumns().iterator();
    attributeNames = new String[query.getDerivedColumns().size()];
    attributeTypes = new Class[query.getDerivedColumns().size()];
    int i = 0;
    while (selectSymbolItr.hasNext()) {
        Column e = getElementFromSymbol(selectSymbolItr.next());
        attributeNames[i] = this.getNameFromElement(e);
        attributeTypes[i] = e.getJavaType();
        i++;
    }
    List<TableReference> tables = query.getFrom();
    TableReference t = tables.get(0);
    if (t instanceof NamedTable) {
        Table group = ((NamedTable) t).getMetadataObject();
        tableName = group.getName();
    }
}
Also used : NamedTable(org.teiid.language.NamedTable) TableReference(org.teiid.language.TableReference) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) DerivedColumn(org.teiid.language.DerivedColumn) DerivedColumn(org.teiid.language.DerivedColumn)

Example 3 with TableReference

use of org.teiid.language.TableReference in project teiid by teiid.

the class PhoenixExecutionFactory method translateCommand.

@Override
public List<?> translateCommand(Command command, ExecutionContext context) {
    if (command instanceof SetQuery) {
        SetQuery set = (SetQuery) command;
        if (!set.isAll()) {
            // distinct is not supported, convert to an inline view and add distinct
            Select s = new Select();
            s.setDistinct(true);
            s.setDerivedColumns(new ArrayList<DerivedColumn>());
            s.setOrderBy(set.getOrderBy());
            for (DerivedColumn dc : set.getProjectedQuery().getDerivedColumns()) {
                // it's expected that the columns will be aliases
                Assertion.assertTrue(dc.getAlias() != null);
                ColumnReference cr = new ColumnReference(null, dc.getAlias(), null, dc.getExpression().getType());
                s.getDerivedColumns().add(new DerivedColumn(null, cr));
            }
            set.setOrderBy(null);
            s.setLimit(set.getLimit());
            set.setLimit(null);
            set.setAll(true);
            // $NON-NLS-1$
            s.setFrom(Arrays.asList((TableReference) new DerivedTable(set, "x")));
            return Arrays.asList(s);
        }
    }
    return super.translateCommand(command, context);
}
Also used : TableReference(org.teiid.language.TableReference) SetQuery(org.teiid.language.SetQuery) DerivedTable(org.teiid.language.DerivedTable) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn) ColumnReference(org.teiid.language.ColumnReference)

Example 4 with TableReference

use of org.teiid.language.TableReference in project teiid by teiid.

the class JPQLSelectVisitor method handleJoin.

private JoinTable handleJoin(Join obj) throws TranslatorException {
    TableReference left = obj.getLeftItem();
    TableReference right = obj.getRightItem();
    if ((left instanceof NamedTable) && (right instanceof NamedTable)) {
        JoinTable join = handleJoin(obj.getJoinType(), left, right, true);
        this.joins.add(join);
        return join;
    }
    JoinTable leftJoin = null;
    if (left instanceof Join) {
        leftJoin = handleJoin((Join) left);
        if (right instanceof NamedTable) {
            JoinTable join = handleJoin(obj.getJoinType(), leftJoin, (NamedTable) right);
            this.joins.add(join);
            return join;
        }
    }
    JoinTable rightJoin = null;
    if (right instanceof Join) {
        rightJoin = handleJoin((Join) right);
        if (left instanceof NamedTable) {
            JoinTable join = handleJoin(obj.getJoinType(), (NamedTable) left, rightJoin);
            this.joins.add(join);
            return join;
        }
    }
    throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14005));
}
Also used : NamedTable(org.teiid.language.NamedTable) TableReference(org.teiid.language.TableReference) Join(org.teiid.language.Join) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

TableReference (org.teiid.language.TableReference)4 DerivedColumn (org.teiid.language.DerivedColumn)3 NamedTable (org.teiid.language.NamedTable)3 ColumnReference (org.teiid.language.ColumnReference)2 Select (org.teiid.language.Select)2 Table (org.teiid.metadata.Table)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ClobImpl (org.teiid.core.types.ClobImpl)1 ClobType (org.teiid.core.types.ClobType)1 GeometryType (org.teiid.core.types.GeometryType)1 Comparison (org.teiid.language.Comparison)1 DerivedTable (org.teiid.language.DerivedTable)1 Function (org.teiid.language.Function)1 Join (org.teiid.language.Join)1 Literal (org.teiid.language.Literal)1 SetQuery (org.teiid.language.SetQuery)1 Column (org.teiid.metadata.Column)1 TranslatorException (org.teiid.translator.TranslatorException)1