use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class ODataSQLBuilder method processOrderBy.
private OrderBy processOrderBy(OrderBy orderBy, List<OrderByItem> orderByItems, DocumentNode resource) throws TeiidException {
for (OrderByItem obitem : orderByItems) {
ODataExpressionToSQLVisitor visitor = new ODataExpressionToSQLVisitor(resource, false, getUriInfo(), this.metadata, this.odata, this.nameGenerator, this.params, this.parseService);
Expression expr = visitor.getExpression(obitem.getExpression());
if (expr instanceof ElementSymbol) {
orderBy.addVariable(expr, !obitem.isDescending());
} else {
AliasSymbol alias = new AliasSymbol("_orderByAlias", expr);
orderBy.addVariable(alias, !obitem.isDescending());
visitor.getEntityResource().addProjectedColumn(alias, EdmInt32.getInstance(), null, false);
}
}
return orderBy;
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class ODataSQLBuilder method selectQuery.
public Query selectQuery() throws TeiidException {
if (!this.exceptions.isEmpty()) {
throw this.exceptions.get(0);
}
Query query = this.context.buildQuery();
if (this.countQuery) {
AggregateSymbol aggregateSymbol = new AggregateSymbol(AggregateSymbol.Type.COUNT.name(), false, null);
Select select = new Select(Arrays.asList(aggregateSymbol));
query.setSelect(select);
} else if (this.orderBy != null) {
if (this.context.getIterator() != null) {
// currently this doesn't matter as the ordering can only be based upon the parent entity
((AggregateSymbol) ((AliasSymbol) query.getSelect().getSymbol(query.getSelect().getProjectedSymbols().size() - 1)).getSymbol()).setOrderBy(this.orderBy);
} else {
query.setOrderBy(this.orderBy);
}
}
if (this.expandOption != null) {
processExpandOption(this.expandOption, this.context, query, 1, null);
}
return query;
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class ODataExpressionToSQLVisitor method visit.
@Override
public void visit(UriResourceIt info) {
if (info.getType() instanceof SingletonPrimitiveType) {
org.teiid.query.sql.symbol.Expression ex = null;
if (this.ctxQuery.getIterator() == null) {
String group = this.nameGenerator.getNextGroup();
GroupSymbol groupSymbol = new GroupSymbol(group);
StoredProcedure procedure = new StoredProcedure();
procedure.setProcedureName("arrayiterate");
// the projected should only be the collection property at this point
// we may need more checks here to ensure that is valid
Collection<ProjectedColumn> values = this.ctxQuery.getProjectedColumns().values();
Assertion.assertTrue(values.size() == 1);
ProjectedColumn projectedColumn = values.iterator().next();
ElementSymbol projectedEs = (ElementSymbol) projectedColumn.getExpression();
List<SPParameter> params = new ArrayList<SPParameter>();
SPParameter param = new SPParameter(1, SPParameter.IN, "val");
param.setExpression(projectedEs);
params.add(param);
procedure.setParameter(param);
SubqueryFromClause fromClause = new SubqueryFromClause(group, procedure);
fromClause.setLateral(true);
ElementSymbol es = new ElementSymbol("col", groupSymbol);
String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
Function castFunction = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
DocumentNode itResource = new DocumentNode();
org.teiid.query.sql.symbol.Expression clone = (org.teiid.query.sql.symbol.Expression) castFunction.clone();
AggregateSymbol symbol = new AggregateSymbol(AggregateSymbol.Type.ARRAY_AGG.name(), false, clone);
AliasSymbol expression = new AliasSymbol(projectedEs.getShortName(), symbol);
itResource.setFromClause(fromClause);
itResource.setGroupSymbol(groupSymbol);
itResource.addProjectedColumn(expression, info.getType(), projectedColumn.getProperty(), true);
this.ctxQuery.getProjectedColumns().remove(projectedColumn.getExpression());
this.ctxQuery.setIterator(itResource);
ex = castFunction;
} else {
GroupSymbol groupSymbol = this.ctxQuery.getIterator().getGroupSymbol();
ElementSymbol es = new ElementSymbol("col", groupSymbol);
String type = ODataTypeManager.teiidType((SingletonPrimitiveType) info.getType(), false);
ex = new Function(CAST, new org.teiid.query.sql.symbol.Expression[] { es, new Constant(type) });
}
this.stack.push(ex);
} else {
boolean ex = true;
if (this.ctxQuery instanceof ExpandDocumentNode) {
ExpandDocumentNode node = (ExpandDocumentNode) this.ctxQuery;
DocumentNode parent = node.getCollectionContext();
if (parent != null) {
this.ctxExpression = parent;
ex = false;
}
}
if (ex) {
throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16010, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16010)));
}
}
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class TestAliasSymbol method testClone.
@Test
public void testClone() {
// $NON-NLS-1$ //$NON-NLS-2$
AliasSymbol a1 = new AliasSymbol("X", new ExpressionSymbol("x", new Constant(1)));
// $NON-NLS-1$
a1.setOutputName("foo");
AliasSymbol clone = (AliasSymbol) a1.clone();
assertEquals(a1, clone);
assertEquals(a1.getOutputName(), clone.getOutputName());
}
use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.
the class TestSelect method sample2.
public static final Select sample2() {
Select select = new Select();
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("a"));
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("b"));
// $NON-NLS-1$
select.addSymbol(new ElementSymbol("c"));
// $NON-NLS-1$ //$NON-NLS-2$
select.addSymbol(new AliasSymbol("Z", new ElementSymbol("ZZ 9 Plural Z Alpha")));
return select;
}
Aggregations