use of org.hsqldb_voltpatches.Expression.SimpleColumnContext in project voltdb by VoltDB.
the class StatementDMQL method voltGetXMLExpression.
static VoltXMLElement voltGetXMLExpression(QueryExpression queryExpr, ExpressionColumn[] parameters, Session session) throws HSQLParseException {
// "select" statements/clauses are always represented by a QueryExpression of type QuerySpecification.
// The only other instances of QueryExpression are direct QueryExpression instances instantiated in XreadSetOperation
// to represent UNION, etc.
int exprType = queryExpr.getUnionType();
if (exprType == QueryExpression.NOUNION) {
// "select" statements/clauses are always represented by a QueryExpression of type QuerySpecification.
if (!(queryExpr instanceof QuerySpecification)) {
throw new HSQLParseException(queryExpr.operatorName() + " is not supported.");
}
QuerySpecification select = (QuerySpecification) queryExpr;
return voltGetXMLSpecification(select, parameters, session);
}
if (exprType == QueryExpression.UNION || exprType == QueryExpression.UNION_ALL || exprType == QueryExpression.EXCEPT || exprType == QueryExpression.EXCEPT_ALL || exprType == QueryExpression.INTERSECT || exprType == QueryExpression.INTERSECT_ALL) {
VoltXMLElement unionExpr = new VoltXMLElement("union");
unionExpr.attributes.put("uniontype", queryExpr.operatorName());
VoltXMLElement leftExpr = voltGetXMLExpression(queryExpr.getLeftQueryExpression(), parameters, session);
VoltXMLElement rightExpr = voltGetXMLExpression(queryExpr.getRightQueryExpression(), parameters, session);
// parameters
voltAppendParameters(session, unionExpr, parameters);
// Limit/Offset
List<VoltXMLElement> limitOffsetXml = voltGetLimitOffsetXMLFromSortAndSlice(session, queryExpr.sortAndSlice);
for (VoltXMLElement elem : limitOffsetXml) {
unionExpr.children.add(elem);
}
// Order By
if (queryExpr.sortAndSlice.getOrderLength() > 0) {
List<Expression> displayCols = getDisplayColumnsForSetOp(queryExpr);
SimpleColumnContext context = new SimpleColumnContext(session, displayCols);
VoltXMLElement orderCols = new VoltXMLElement("ordercolumns");
unionExpr.children.add(orderCols);
for (int i = 0; i < queryExpr.sortAndSlice.exprList.size(); ++i) {
Expression e = (Expression) queryExpr.sortAndSlice.exprList.get(i);
assert (e.getLeftNode() != null);
// Get the display column with a corresponding index
int index = e.getLeftNode().queryTableColumnIndex;
assert (index < displayCols.size());
Expression column = displayCols.get(index);
e.setLeftNode(column);
VoltXMLElement xml = e.voltGetXML(context.withStartKey(i), null);
orderCols.children.add(xml);
}
}
/**
* Try to merge parent and the child nodes for UNION and INTERSECT (ALL) set operation
* only if they don't have their own limit/offset/order by clauses
* In case of EXCEPT(ALL) operation only the left child can be merged with the parent in order to preserve
* associativity - (Select1 EXCEPT Select2) EXCEPT Select3 vs. Select1 EXCEPT (Select2 EXCEPT Select3)
*/
boolean canMergeLeft = !hasLimitOrOrder(leftExpr);
if (canMergeLeft && "union".equalsIgnoreCase(leftExpr.name) && queryExpr.operatorName().equalsIgnoreCase(leftExpr.attributes.get("uniontype"))) {
unionExpr.children.addAll(leftExpr.children);
} else {
unionExpr.children.add(leftExpr);
}
boolean canMergeRight = !hasLimitOrOrder(rightExpr);
if (canMergeRight && exprType != QueryExpression.EXCEPT && exprType != QueryExpression.EXCEPT_ALL && "union".equalsIgnoreCase(rightExpr.name) && queryExpr.operatorName().equalsIgnoreCase(rightExpr.attributes.get("uniontype"))) {
unionExpr.children.addAll(rightExpr.children);
} else {
unionExpr.children.add(rightExpr);
}
return unionExpr;
}
throw new HSQLParseException(queryExpr.operatorName() + " tuple set operator is not supported.");
}
use of org.hsqldb_voltpatches.Expression.SimpleColumnContext in project voltdb by VoltDB.
the class StatementDMQL method voltGetXMLSpecification.
private static VoltXMLElement voltGetXMLSpecification(QuerySpecification select, ExpressionColumn[] parameters, Session session) throws HSQLParseException {
// select
VoltXMLElement query = new VoltXMLElement("select");
if (select.isDistinctSelect) {
query.attributes.put("distinct", "true");
}
List<VoltXMLElement> limitOffsetXml = voltGetLimitOffsetXMLFromSortAndSlice(session, select.sortAndSlice);
for (VoltXMLElement elem : limitOffsetXml) {
query.children.add(elem);
}
// columns
VoltXMLElement cols = new VoltXMLElement("columns");
query.children.add(cols);
java.util.ArrayList<Expression> orderByCols = new java.util.ArrayList<>();
java.util.ArrayList<Expression> groupByCols = new java.util.ArrayList<>();
select.displayCols.clear();
java.util.ArrayList<Pair<Integer, HsqlNameManager.SimpleName>> aliases = new java.util.ArrayList<>();
/*
* select.exprColumn stores all of the columns needed by HSQL to
* calculate the query's result set. It contains more than just the
* columns in the output; for example, it contains columns representing
* aliases, columns for groups, etc.
*
* Volt uses multiple collections to organize these columns.
*
* Observing this loop in a debugger, the following seems true:
*
* 1. Columns in exprColumns that appear in the output schema, appear in
* exprColumns in the same order that they occur in the output schema.
*
* 2. expr.columnIndex is an index back in to the select.exprColumns
* array. This allows multiple exprColumn entries to refer to each
* other; for example, an OpType.SIMPLE_COLUMN type storing an alias
* will have its columnIndex set to the offset of the expr it aliases.
*/
for (int i = 0; i < select.exprColumns.length; i++) {
final Expression expr = select.exprColumns[i];
if (expr.alias != null) {
/*
* Remember how aliases relate to columns. Will iterate again later
* and mutate the exprColumn entries setting the alias string on the aliased
* column entry.
*/
if (expr instanceof ExpressionColumn) {
ExpressionColumn exprColumn = (ExpressionColumn) expr;
if (exprColumn.alias != null && exprColumn.columnName == null) {
aliases.add(Pair.of(expr.columnIndex, expr.alias));
}
} else if (expr.columnIndex > -1) {
/*
* Only add it to the list of aliases that need to be
* propagated to columns if the column index is valid.
* ExpressionArithmetic will have an alias but not
* necessarily a column index.
*/
aliases.add(Pair.of(expr.columnIndex, expr.alias));
}
}
// it's easier to patch up display column ordering later.
if (expr.columnIndex == -1) {
expr.columnIndex = i;
}
if (isGroupByColumn(select, i)) {
groupByCols.add(expr);
} else if (expr.opType == OpTypes.ORDER_BY) {
if (select.sortAndSlice.hasOrder())
// If the selectQuerySpecification's sort structure has been reset,
// do not add orderByCols.
orderByCols.add(expr);
} else if (expr.equals(select.getHavingCondition())) {
// Having
if (!(expr instanceof ExpressionLogical && expr.isAggregate)) {
throw new HSQLParseException("VoltDB does not support HAVING clause without aggregation. " + "Consider using WHERE clause if possible");
}
} else if (expr.opType != OpTypes.SIMPLE_COLUMN || (expr.isAggregate && expr.alias != null)) {
// Add aggregate aliases to the display columns to maintain
// the output schema column ordering.
select.displayCols.add(expr);
}
// else, other simple columns are ignored. If others exist, maybe
// volt infers a display column from another column collection?
}
for (Pair<Integer, HsqlNameManager.SimpleName> alias : aliases) {
// set the alias data into the expression being aliased.
select.exprColumns[alias.getFirst()].alias = alias.getSecond();
}
/*
* The columns chosen above as display columns aren't always the same
* expr objects HSQL would use as display columns - some data were
* unified (namely, SIMPLE_COLUMN aliases were pushed into COLUMNS).
*
* However, the correct output schema ordering was correct in exprColumns.
* This order was maintained by adding SIMPLE_COLUMNs to displayCols.
*
* Now need to serialize the displayCols, serializing the non-simple-columns
* corresponding to simple_columns for any simple_columns that woodchucks
* could chuck.
*
* Serialize the display columns in the exprColumn order.
*/
SimpleColumnContext context = new SimpleColumnContext(session, select.displayCols);
// having
Expression havingCondition = select.getHavingCondition();
if (havingCondition != null) {
VoltXMLElement having = new VoltXMLElement("having");
query.children.add(having);
VoltXMLElement havingExpr = havingCondition.voltGetXML(context.withStartKey(0), null);
having.children.add(havingExpr);
}
for (int jj = 0; jj < select.displayCols.size(); ++jj) {
Expression expr = select.displayCols.get(jj);
if (context.disabledTheColumnForDisplay(jj)) {
continue;
}
VoltXMLElement xml = expr.voltGetXML(context.withStartKey(jj), null);
cols.children.add(xml);
assert (xml != null);
}
// parameters
voltAppendParameters(session, query, parameters);
// scans
VoltXMLElement scans = new VoltXMLElement("tablescans");
query.children.add(scans);
assert (scans != null);
for (RangeVariable rangeVariable : select.rangeVariables) {
scans.children.add(rangeVariable.voltGetRangeVariableXML(session));
}
// groupby
if (select.isGrouped) {
VoltXMLElement groupCols = new VoltXMLElement("groupcolumns");
query.children.add(groupCols);
for (int jj = 0; jj < groupByCols.size(); ++jj) {
Expression expr = groupByCols.get(jj);
VoltXMLElement xml = expr.voltGetXML(context.withStartKey(jj), null);
groupCols.children.add(xml);
}
}
// orderby
if (orderByCols.size() > 0) {
VoltXMLElement orderCols = new VoltXMLElement("ordercolumns");
query.children.add(orderCols);
for (int jj = 0; jj < orderByCols.size(); ++jj) {
Expression expr = orderByCols.get(jj);
VoltXMLElement xml = expr.voltGetXML(context.withStartKey(jj), null);
orderCols.children.add(xml);
}
}
// the effects of a join if there is only one table scan in the statement.
if (scans.children.size() > 1) {
List<VoltXMLElement> exprCols = query.extractSubElements("operation", "optype", "operator_case_when");
resolveUsingExpressions(exprCols, select.rangeVariables);
}
return query;
}
Aggregations