use of herddb.sql.expressions.ColumnRef in project herddb by diennea.
the class JSQLParserPlanner method planAggregate.
private PlannerOp planAggregate(List<SelectExpressionItem> fieldList, OpSchema inputSchema, PlannerOp input, OpSchema originalTableSchema, GroupByElement groupBy) {
List<Column> outputSchemaKeysInGroupBy = new ArrayList<>();
List<String> fieldnamesKeysInGroupBy = new ArrayList<>();
List<Column> outputSchemaAggregationResults = new ArrayList<>();
List<String> fieldnamesAggregationResults = new ArrayList<>();
List<Integer> projectionAggregationResults = new ArrayList<>();
List<List<Integer>> argLists = new ArrayList<>();
List<String> aggtypes = new ArrayList<>();
List<Column> originalOutputSchema = new ArrayList<>();
List<String> originalFieldNames = new ArrayList<>();
Set<String> nonAggregateColumns = new HashSet<>();
int k = 0;
for (SelectExpressionItem sel : fieldList) {
Alias alias = sel.getAlias();
String fieldName = null;
if (alias != null) {
checkSupported(alias.getAliasColumns() == null);
fieldName = fixMySqlBackTicks(alias.getName().toLowerCase());
}
Expression exp = sel.getExpression();
Function fn = SQLParserExpressionCompiler.detectAggregatedFunction(exp);
if (fn != null) {
int type = SQLParserExpressionCompiler.getAggregateFunctionType(exp, inputSchema);
ColumnRef additionalColumn = SQLParserExpressionCompiler.getAggregateFunctionArgument(fn, inputSchema);
aggtypes.add(fixMySqlBackTicks(fn.getName().toLowerCase()));
if (additionalColumn != null) {
// SELECT min(col) -> we have to add "col" to the scan
IntHolder pos = new IntHolder();
findColumnInSchema(additionalColumn.tableName, fixMySqlBackTicks(additionalColumn.name), originalTableSchema, pos);
checkSupported(pos.value >= 0);
argLists.add(Collections.singletonList(pos.value));
} else {
argLists.add(Collections.emptyList());
}
if (fieldName == null) {
fieldName = "expr$" + k;
}
Column col = Column.column(fieldName, type);
outputSchemaAggregationResults.add(col);
originalFieldNames.add(fieldName);
originalOutputSchema.add(col);
fieldnamesAggregationResults.add(fieldName);
projectionAggregationResults.add(k);
} else if (exp instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
String tableAlias = extractTableName(colRef);
ColumnRef colInSchema = findColumnInSchema(tableAlias, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, new IntHolder());
checkSupported(colInSchema != null);
if (fieldName == null) {
fieldName = colInSchema.name;
}
Column col = Column.column(fieldName, colInSchema.type);
originalFieldNames.add(fieldName);
originalOutputSchema.add(col);
nonAggregateColumns.add(fieldName);
} else {
checkSupported(false);
}
k++;
}
// GROUP BY
List<Integer> groupedFieldsIndexes = new ArrayList<>();
List<Integer> projectionForGroupByFields = new ArrayList<>();
if (groupBy != null) {
checkSupported(groupBy.getGroupingSets() == null || groupBy.getGroupingSets().isEmpty());
int posInGroupBy = 0;
for (Expression exp : groupBy.getGroupByExpressions()) {
if (exp instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column colRef = (net.sf.jsqlparser.schema.Column) exp;
String tableName = extractTableName(colRef);
IntHolder pos = new IntHolder();
ColumnRef colInSchema = findColumnInSchema(tableName, fixMySqlBackTicks(colRef.getColumnName()), inputSchema, pos);
checkSupported(colInSchema != null);
groupedFieldsIndexes.add(pos.value);
fieldnamesKeysInGroupBy.add(fixMySqlBackTicks(colRef.getColumnName()));
outputSchemaKeysInGroupBy.add(colInSchema.toColumn());
projectionForGroupByFields.add(posInGroupBy);
} else {
checkSupported(false);
}
posInGroupBy++;
}
} else {
// -> column MUST be in GROUP BY !
for (String s : nonAggregateColumns) {
throw new StatementExecutionException("field " + s + " MUST appear in GROUP BY clause");
}
}
PlannerOp op;
if (!groupedFieldsIndexes.isEmpty()) {
// this is tricky,
// the AggregateOp always create a result in the form of
// FIELD1,FIELD2......AGG1,AGG2
// Basically it puts all of the results of the aggregation to the right
// So we have to add a projection that makes the resultset appear
// as it is defined in the SELECT clause
List<Column> outputSchema = new ArrayList<>();
outputSchema.addAll(outputSchemaKeysInGroupBy);
outputSchema.addAll(outputSchemaAggregationResults);
Column[] outputSchemaArray = outputSchema.toArray(new Column[0]);
List<String> aggreateFieldNames = new ArrayList<>();
aggreateFieldNames.addAll(fieldnamesKeysInGroupBy);
aggreateFieldNames.addAll(fieldnamesAggregationResults);
op = new AggregateOp(input, aggreateFieldNames.toArray(new String[0]), outputSchemaArray, aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
String[] reodereded = originalFieldNames.toArray(new String[0]);
int[] projections = new int[originalFieldNames.size()];
int i = 0;
for (int pos : projectionForGroupByFields) {
projections[pos] = i++;
}
for (int pos : projectionAggregationResults) {
projections[pos] = i++;
}
ProjectOp.ZeroCopyProjection projection = new ProjectOp.ZeroCopyProjection(reodereded, originalOutputSchema.toArray(new Column[0]), projections);
op = new ProjectOp(projection, op);
} else {
// no "GROUP BY", so no need for an additional projection
// this is the SELECT COUNT(*) FROM TABLE case
op = new AggregateOp(input, originalFieldNames.toArray(new String[0]), originalOutputSchema.toArray(new Column[0]), aggtypes.toArray(new String[0]), argLists, groupedFieldsIndexes);
}
return op;
}
use of herddb.sql.expressions.ColumnRef in project herddb by diennea.
the class JSQLParserPlanner method buildProjection.
private Projection buildProjection(final List<SelectExpressionItem> projects, final boolean allowIdentity, final OpSchema tableSchema) {
boolean allowZeroCopyProjection = true;
List<CompiledSQLExpression> fields = new ArrayList<>(projects.size());
Column[] columns = new Column[projects.size()];
String[] fieldNames = new String[columns.length];
int i = 0;
int[] zeroCopyProjections = new int[fieldNames.length];
boolean identity = allowIdentity && tableSchema != null && tableSchema.columns.length == fieldNames.length;
for (SelectExpressionItem node : projects) {
int type = ColumnTypes.ANYTYPE;
CompiledSQLExpression exp;
String alias = null;
if (node.getAlias() != null) {
alias = fixMySqlBackTicks(node.getAlias().getName().toLowerCase());
checkSupported(node.getAlias().getAliasColumns() == null);
}
if (node.getExpression() instanceof net.sf.jsqlparser.schema.Column && !isBooleanLiteral((net.sf.jsqlparser.schema.Column) node.getExpression())) {
net.sf.jsqlparser.schema.Column col = (net.sf.jsqlparser.schema.Column) node.getExpression();
// checkSupported(col.getTable() == null);
String columnName = fixMySqlBackTicks(col.getColumnName());
String tableAlias = extractTableName(col);
if (alias == null) {
alias = columnName;
}
IntHolder indexInSchema = new IntHolder(-1);
ColumnRef found = findColumnInSchema(tableAlias, columnName, tableSchema, indexInSchema);
if (indexInSchema.value == -1 || found == null) {
String nameInError = tableAlias != null ? tableAlias + "." + columnName : columnName;
throw new StatementExecutionException("Column " + nameInError + " not found in target table (schema " + tableSchema + ")");
}
exp = new AccessCurrentRowExpression(indexInSchema.value, found.type);
type = found.type;
} else {
exp = SQLParserExpressionCompiler.compileExpression(node.getExpression(), tableSchema);
if (alias == null) {
alias = "col" + i;
}
}
if (exp instanceof AccessCurrentRowExpression) {
AccessCurrentRowExpression accessCurrentRowExpression = (AccessCurrentRowExpression) exp;
int mappedIndex = accessCurrentRowExpression.getIndex();
zeroCopyProjections[i] = mappedIndex;
if (i != mappedIndex) {
identity = false;
}
} else {
allowZeroCopyProjection = false;
}
fields.add(exp);
Column col = Column.column(alias, type);
identity = identity && col.name.equals(tableSchema.columns[i].name);
fieldNames[i] = alias;
columns[i++] = col;
}
if (allowZeroCopyProjection) {
if (identity) {
return Projection.IDENTITY(fieldNames, columns);
}
return new ProjectOp.ZeroCopyProjection(fieldNames, columns, zeroCopyProjections);
} else {
return new ProjectOp.BasicProjection(fieldNames, columns, fields);
}
}
use of herddb.sql.expressions.ColumnRef in project herddb by diennea.
the class JSQLParserPlanner method getTableSchema.
private OpSchema getTableSchema(String defaultTableSpace, net.sf.jsqlparser.schema.Table table) {
String tableSpace = table.getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
TableSpaceManager tableSpaceManager = getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
clearCache();
throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
}
String tableName = fixMySqlBackTicks(table.getName().toLowerCase());
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new TableDoesNotExistException("no table " + tableName + " here for " + tableSpace);
}
Table tableImpl = tableManager.getTable();
String aliasTable = tableName;
if (table.getAlias() != null) {
aliasTable = fixMySqlBackTicks(table.getAlias().getName());
checkSupported(table.getAlias().getAliasColumns() == null);
}
ColumnRef[] refs = new ColumnRef[tableImpl.columns.length];
for (int i = 0; i < refs.length; i++) {
refs[i] = new ColumnRef(aliasTable, tableImpl.columns[i]);
}
return new OpSchema(tableSpace, tableName, aliasTable, tableImpl.columnNames, refs);
}
use of herddb.sql.expressions.ColumnRef in project herddb by diennea.
the class JSQLParserPlanner method buildSelectBody.
private PlannerOp buildSelectBody(String defaultTableSpace, int maxRows, SelectBody selectBody, boolean forceScan) throws StatementExecutionException {
if (selectBody instanceof SetOperationList) {
SetOperationList list = (SetOperationList) selectBody;
return buildSetOperationList(defaultTableSpace, maxRows, list, forceScan);
}
checkSupported(selectBody instanceof PlainSelect, selectBody.getClass().getName());
PlainSelect plainSelect = (PlainSelect) selectBody;
checkSupported(!plainSelect.getMySqlHintStraightJoin());
checkSupported(!plainSelect.getMySqlSqlCalcFoundRows());
checkSupported(!plainSelect.getMySqlSqlNoCache());
checkSupported(plainSelect.getDistinct() == null);
checkSupported(plainSelect.getFetch() == null);
checkSupported(plainSelect.getFirst() == null);
checkSupported(plainSelect.getForUpdateTable() == null);
checkSupported(plainSelect.getForXmlPath() == null);
checkSupported(plainSelect.getHaving() == null);
checkSupported(plainSelect.getIntoTables() == null);
checkSupported(plainSelect.getOffset() == null);
checkSupported(plainSelect.getOptimizeFor() == null);
checkSupported(plainSelect.getOracleHierarchical() == null);
checkSupported(plainSelect.getOracleHint() == null);
checkSupported(plainSelect.getSkip() == null);
checkSupported(plainSelect.getWait() == null);
checkSupported(plainSelect.getKsqlWindow() == null);
FromItem fromItem = plainSelect.getFromItem();
checkSupported(fromItem instanceof net.sf.jsqlparser.schema.Table);
OpSchema primaryTableSchema = getTableSchema(defaultTableSpace, (net.sf.jsqlparser.schema.Table) fromItem);
OpSchema[] joinedTables = new OpSchema[0];
int totalJoinOutputFieldsCount = primaryTableSchema.columns.length;
if (plainSelect.getJoins() != null) {
joinedTables = new OpSchema[plainSelect.getJoins().size()];
int i = 0;
for (Join join : plainSelect.getJoins()) {
checkSupported(!join.isApply());
checkSupported(!join.isCross());
checkSupported(!join.isFull() || (join.isFull() && join.isOuter()));
checkSupported(!join.isSemi());
checkSupported(!join.isStraight());
checkSupported(!join.isWindowJoin());
checkSupported(join.getJoinWindow() == null);
checkSupported(join.getUsingColumns() == null);
FromItem rightItem = join.getRightItem();
checkSupported(rightItem instanceof net.sf.jsqlparser.schema.Table);
OpSchema joinedTable = getTableSchema(defaultTableSpace, (net.sf.jsqlparser.schema.Table) rightItem);
joinedTables[i++] = joinedTable;
totalJoinOutputFieldsCount += joinedTable.columns.length;
}
}
int pos = 0;
String[] joinOutputFieldnames = new String[totalJoinOutputFieldsCount];
ColumnRef[] joinOutputColumns = new ColumnRef[totalJoinOutputFieldsCount];
System.arraycopy(primaryTableSchema.columnNames, 0, joinOutputFieldnames, 0, primaryTableSchema.columnNames.length);
System.arraycopy(primaryTableSchema.columns, 0, joinOutputColumns, 0, primaryTableSchema.columns.length);
pos += primaryTableSchema.columnNames.length;
for (OpSchema joinedTable : joinedTables) {
System.arraycopy(joinedTable.columnNames, 0, joinOutputFieldnames, pos, joinedTable.columnNames.length);
System.arraycopy(joinedTable.columns, 0, joinOutputColumns, pos, joinedTable.columns.length);
pos += joinedTable.columnNames.length;
}
OpSchema currentSchema = primaryTableSchema;
// single JOIN only at the moment
checkSupported(joinedTables.length <= 1);
if (joinedTables.length > 0) {
currentSchema = new OpSchema(primaryTableSchema.tableSpace, null, null, joinOutputFieldnames, joinOutputColumns);
}
List<SelectItem> selectItems = plainSelect.getSelectItems();
checkSupported(!selectItems.isEmpty());
Predicate predicate = null;
TupleComparator comparator = null;
ScanLimits limits = null;
boolean identityProjection = false;
Projection projection;
List<SelectExpressionItem> selectedFields = new ArrayList<>(selectItems.size());
boolean containsAggregatedFunctions = false;
if (selectItems.size() == 1 && selectItems.get(0) instanceof AllColumns) {
projection = Projection.IDENTITY(currentSchema.columnNames, ColumnRef.toColumnsArray(currentSchema.columns));
identityProjection = true;
} else {
checkSupported(!selectItems.isEmpty());
for (SelectItem item : selectItems) {
if (item instanceof SelectExpressionItem) {
SelectExpressionItem selectExpressionItem = (SelectExpressionItem) item;
selectedFields.add(selectExpressionItem);
if (SQLParserExpressionCompiler.detectAggregatedFunction(selectExpressionItem.getExpression()) != null) {
containsAggregatedFunctions = true;
}
} else if (item instanceof AllTableColumns) {
// expand alias.* to the full list of columns (according to pyhsical schema!)
AllTableColumns allTablesColumn = (AllTableColumns) item;
net.sf.jsqlparser.schema.Table table = allTablesColumn.getTable();
String tableName = fixMySqlBackTicks(table.getName());
boolean found = false;
if (primaryTableSchema.isTableOrAlias(tableName)) {
for (ColumnRef col : primaryTableSchema.columns) {
net.sf.jsqlparser.schema.Column c = new net.sf.jsqlparser.schema.Column(table, col.name);
SelectExpressionItem selectExpressionItem = new SelectExpressionItem(c);
selectedFields.add(selectExpressionItem);
found = true;
}
} else {
for (OpSchema joinedTableSchema : joinedTables) {
if (joinedTableSchema.isTableOrAlias(tableName)) {
for (ColumnRef col : joinedTableSchema.columns) {
net.sf.jsqlparser.schema.Column c = new net.sf.jsqlparser.schema.Column(table, col.name);
SelectExpressionItem selectExpressionItem = new SelectExpressionItem(c);
selectedFields.add(selectExpressionItem);
}
found = true;
break;
}
}
}
if (!found) {
checkSupported(false, "Bad table ref " + tableName + ".*");
}
} else {
checkSupported(false);
}
}
if (!containsAggregatedFunctions) {
// building the projection
// we have the current physical schema, we can create references by position (as Calcite does)
// in order to not need accessing columns by name and also making it easier to support
// ZeroCopyProjections
projection = buildProjection(selectedFields, true, currentSchema);
} else {
// start by full table scan, the AggregateOp operator will create the final projection
projection = Projection.IDENTITY(currentSchema.columnNames, ColumnRef.toColumnsArray(currentSchema.columns));
identityProjection = true;
}
}
TableSpaceManager tableSpaceManager = this.manager.getTableSpaceManager(primaryTableSchema.tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(primaryTableSchema.name);
Table tableImpl = tableManager.getTable();
CompiledSQLExpression whereExpression = null;
if (plainSelect.getWhere() != null) {
whereExpression = SQLParserExpressionCompiler.compileExpression(plainSelect.getWhere(), currentSchema);
if (joinedTables.length == 0 && whereExpression != null) {
SQLRecordPredicate sqlWhere = new SQLRecordPredicate(tableImpl, null, whereExpression);
IndexUtils.discoverIndexOperations(primaryTableSchema.tableSpace, whereExpression, tableImpl, sqlWhere, selectBody, tableSpaceManager);
predicate = sqlWhere;
}
}
// start with a TableScan + filters
ScanStatement scan = new ScanStatement(primaryTableSchema.tableSpace, primaryTableSchema.name, Projection.IDENTITY(primaryTableSchema.columnNames, ColumnRef.toColumnsArray(primaryTableSchema.columns)), predicate, comparator, limits);
scan.setTableDef(tableImpl);
PlannerOp op = new BindableTableScanOp(scan);
PlannerOp[] scanJoinedTables = new PlannerOp[joinedTables.length];
int ji = 0;
for (OpSchema joinedTable : joinedTables) {
ScanStatement scanSecondaryTable = new ScanStatement(joinedTable.tableSpace, joinedTable.name, Projection.IDENTITY(joinedTable.columnNames, ColumnRef.toColumnsArray(joinedTable.columns)), null, null, null);
scan.setTableDef(tableImpl);
checkSupported(joinedTable.tableSpace.equalsIgnoreCase(primaryTableSchema.tableSpace));
PlannerOp opSecondaryTable = new BindableTableScanOp(scanSecondaryTable);
scanJoinedTables[ji++] = opSecondaryTable;
}
if (scanJoinedTables.length > 0) {
// assuming only one JOIN clause
Join joinClause = plainSelect.getJoins().get(0);
List<CompiledSQLExpression> joinConditions = new ArrayList<>();
if (joinClause.isNatural()) {
// NATURAL join adds a constraint on every column with the same name
List<CompiledSQLExpression> naturalJoinConstraints = new ArrayList<>();
int posInRowLeft = 0;
for (ColumnRef ref : primaryTableSchema.columns) {
int posInRowRight = primaryTableSchema.columns.length;
for (ColumnRef ref2 : joinedTables[0].columns) {
// assuming only one join
if (ref2.name.equalsIgnoreCase(ref.name)) {
CompiledSQLExpression equals = new CompiledEqualsExpression(new AccessCurrentRowExpression(posInRowLeft, ref.type), new AccessCurrentRowExpression(posInRowRight, ref2.type));
naturalJoinConstraints.add(equals);
}
posInRowRight++;
}
posInRowLeft++;
}
CompiledSQLExpression naturalJoin = new CompiledMultiAndExpression(naturalJoinConstraints.toArray(new CompiledSQLExpression[0]));
joinConditions.add(naturalJoin);
}
// handle "ON" clause
Expression onExpression = joinClause.getOnExpression();
if (onExpression != null) {
// TODO: this works for INNER join, but not for LEFT/RIGHT joins
CompiledSQLExpression onCondition = SQLParserExpressionCompiler.compileExpression(onExpression, currentSchema);
joinConditions.add(onCondition);
}
op = new JoinOp(joinOutputFieldnames, ColumnRef.toColumnsArray(joinOutputColumns), new int[0], op, new int[0], scanJoinedTables[0], // generateNullsOnLeft
joinClause.isRight() || (joinClause.isFull() && joinClause.isOuter()), // generateNullsOnRight
joinClause.isLeft() || (joinClause.isFull() && joinClause.isOuter()), // mergeJoin
false, // "ON" conditions
joinConditions);
// handle "WHERE" in case of JOIN
if (whereExpression != null) {
op = new FilterOp(op, whereExpression);
}
}
// add aggregations
if (containsAggregatedFunctions) {
checkSupported(scanJoinedTables.length == 0);
op = planAggregate(selectedFields, currentSchema, op, currentSchema, plainSelect.getGroupBy());
currentSchema = new OpSchema(currentSchema.tableSpace, currentSchema.name, currentSchema.alias, ColumnRef.toColumnsRefsArray(currentSchema.name, op.getOutputSchema()));
}
// add order by
if (plainSelect.getOrderByElements() != null) {
op = planSort(op, currentSchema, plainSelect.getOrderByElements());
}
// add limit
if (plainSelect.getLimit() != null) {
checkSupported(scanJoinedTables.length == 0);
// cannot mix LIMIT and TOP
checkSupported(plainSelect.getTop() == null);
Limit limit = plainSelect.getLimit();
CompiledSQLExpression offset;
if (limit.getOffset() != null) {
offset = SQLParserExpressionCompiler.compileExpression(limit.getOffset(), currentSchema);
} else {
offset = new ConstantExpression(0, ColumnTypes.NOTNULL_LONG);
}
CompiledSQLExpression rowCount = null;
if (limit.getRowCount() != null) {
rowCount = SQLParserExpressionCompiler.compileExpression(limit.getRowCount(), currentSchema);
}
op = new LimitOp(op, rowCount, offset);
}
if (plainSelect.getTop() != null) {
checkSupported(scanJoinedTables.length == 0);
// cannot mix LIMIT and TOP
checkSupported(plainSelect.getLimit() == null);
Top limit = plainSelect.getTop();
CompiledSQLExpression rowCount = null;
if (limit.getExpression() != null) {
rowCount = SQLParserExpressionCompiler.compileExpression(limit.getExpression(), currentSchema);
}
op = new LimitOp(op, rowCount, new ConstantExpression(0, ColumnTypes.NOTNULL_LONG));
}
if (!containsAggregatedFunctions && !identityProjection) {
// add projection
op = new ProjectOp(projection, op);
}
// additional maxrows from JDBC PreparedStatement
if (maxRows > 0) {
op = new LimitOp(op, new ConstantExpression(maxRows, ColumnTypes.NOTNULL_LONG), new ConstantExpression(0, ColumnTypes.NOTNULL_LONG)).optimize();
}
return op;
}
Aggregations