use of net.sf.jsqlparser.statement.select.AllColumns in project herddb by diennea.
the class SQLPlanner method buildSelectStatement.
private ExecutionPlan buildSelectStatement(String defaultTableSpace, Select s, boolean scan, int maxRows) throws StatementExecutionException {
PlainSelect selectBody = (PlainSelect) s.getSelectBody();
net.sf.jsqlparser.schema.Table fromTable = (net.sf.jsqlparser.schema.Table) selectBody.getFromItem();
TableRef mainTable = TableRef.buildFrom(fromTable, defaultTableSpace);
String mainTableAlias = mainTable.tableAlias;
String tableSpace = mainTable.tableSpace;
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new TableSpaceDoesNotExistException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(mainTable.tableName);
if (tableManager == null) {
throw new TableDoesNotExistException("no such table " + mainTable.tableName + " in tablespace " + tableSpace);
}
// linked hash map retains the order of insertions
LinkedHashMap<String, JoinSupport> joins = new LinkedHashMap<>();
boolean joinPresent = false;
joins.put(mainTable.tableAlias, new JoinSupport(mainTable, tableManager));
if (selectBody.getJoins() != null) {
for (Join join : selectBody.getJoins()) {
joinPresent = true;
if (join.isLeft() || join.isCross() || join.isRight() || join.isOuter() || join.isSimple()) {
throw new StatementExecutionException("unsupported JOIN type: " + join);
}
net.sf.jsqlparser.schema.Table joinedTable = (net.sf.jsqlparser.schema.Table) join.getRightItem();
TableRef joinedTableRef = TableRef.buildFrom(joinedTable, defaultTableSpace);
if (!joinedTableRef.tableSpace.equalsIgnoreCase(mainTable.tableSpace)) {
throw new TableDoesNotExistException("unsupported cross-tablespace JOIN " + "between" + mainTable.tableSpace + "." + mainTable.tableName + " and " + joinedTableRef.tableSpace + "." + joinedTableRef.tableName);
}
AbstractTableManager joinedTableManager = tableSpaceManager.getTableManager(joinedTableRef.tableName);
if (joinedTableManager == null) {
throw new TableDoesNotExistException("no such table " + joinedTableRef.tableName + " in tablespace " + tableSpace);
}
JoinSupport joinSupport = new JoinSupport(joinedTableRef, joinedTableManager);
joins.put(joinedTableRef.tableAlias, joinSupport);
}
}
Projection mainTableProjection;
Table table = tableManager.getTable();
boolean allColumns = false;
boolean containsAggregateFunctions = false;
for (SelectItem c : selectBody.getSelectItems()) {
if (c instanceof AllColumns) {
allColumns = true;
break;
} else if (c instanceof AllTableColumns) {
AllTableColumns allTableColumns = (AllTableColumns) c;
TableRef ref = TableRef.buildFrom(allTableColumns.getTable(), defaultTableSpace);
if (!joinPresent && ref.tableAlias.equals(mainTable.tableAlias)) {
// select a.* FROM table a
allColumns = true;
} else {
// select a.*, b.* FROM table a JOIN table b
joins.get(ref.tableAlias).allColumns = true;
}
} else if (c instanceof SelectExpressionItem) {
SelectExpressionItem se = (SelectExpressionItem) c;
if (isAggregateFunction(se.getExpression())) {
containsAggregateFunctions = true;
}
if (!joinPresent) {
joins.get(mainTable.tableAlias).selectItems.add(c);
} else {
ColumnReferencesDiscovery discoverMainTableAlias = discoverMainTableAlias(se.getExpression());
String mainTableAliasForItem = discoverMainTableAlias.getMainTableAlias();
if (discoverMainTableAlias.isContainsMixedAliases()) {
throw new StatementExecutionException("unsupported single SELECT ITEM with mixed aliases: " + c);
}
if (mainTableAliasForItem == null) {
mainTableAliasForItem = mainTable.tableAlias;
}
joins.get(mainTableAliasForItem).selectItems.add(c);
}
} else {
throw new StatementExecutionException("unsupported SELECT ITEM type: " + c);
}
}
if (allColumns) {
mainTableProjection = Projection.IDENTITY(table.columnNames, table.columns);
for (Map.Entry<String, JoinSupport> join : joins.entrySet()) {
JoinSupport support = join.getValue();
support.projection = Projection.IDENTITY(support.table.columnNames, support.table.columns);
support.allColumns = true;
}
} else {
if (!joinPresent) {
mainTableProjection = new SQLProjection(table, mainTableAlias, selectBody.getSelectItems());
} else {
for (JoinSupport support : joins.values()) {
if (support.allColumns) {
support.projection = Projection.IDENTITY(support.table.columnNames, support.table.columns);
} else {
support.projection = new SQLProjection(support.table, support.tableRef.tableAlias, support.selectItems);
}
}
mainTableProjection = joins.get(mainTableAlias).projection;
}
}
if (scan) {
if (!joinPresent) {
SQLRecordPredicate where = selectBody.getWhere() != null ? new SQLRecordPredicate(table, mainTableAlias, selectBody.getWhere()) : null;
if (where != null) {
discoverIndexOperations(selectBody.getWhere(), table, mainTableAlias, where, tableSpaceManager);
}
Aggregator aggregator = null;
ScanLimitsImpl scanLimits = null;
if (containsAggregateFunctions || (selectBody.getGroupByColumnReferences() != null && !selectBody.getGroupByColumnReferences().isEmpty())) {
aggregator = new SQLAggregator(selectBody.getSelectItems(), selectBody.getGroupByColumnReferences(), manager.getRecordSetFactory());
}
TupleComparator comparatorOnScan = null;
TupleComparator comparatorOnPlan = null;
if (selectBody.getOrderByElements() != null && !selectBody.getOrderByElements().isEmpty()) {
if (aggregator != null) {
comparatorOnPlan = SingleColumnSQLTupleComparator.make(mainTableAlias, selectBody.getOrderByElements(), null);
} else {
comparatorOnScan = SingleColumnSQLTupleComparator.make(mainTableAlias, selectBody.getOrderByElements(), table.primaryKey);
}
}
Limit limit = selectBody.getLimit();
Top top = selectBody.getTop();
if (limit != null && top != null) {
throw new StatementExecutionException("LIMIT and TOP cannot be used on the same query");
}
if (limit != null) {
if (limit.isLimitAll() || limit.isLimitNull() || limit.getOffset() instanceof JdbcParameter) {
throw new StatementExecutionException("Invalid LIMIT clause (limit=" + limit + ")");
}
if (maxRows > 0 && limit.getRowCount() instanceof JdbcParameter) {
throw new StatementExecutionException("Invalid LIMIT clause (limit=" + limit + ") and JDBC setMaxRows=" + maxRows);
}
int rowCount;
int rowCountJdbcParameter = -1;
if (limit.getRowCount() instanceof JdbcParameter) {
rowCount = -1;
rowCountJdbcParameter = ((JdbcParameter) limit.getRowCount()).getIndex() - 1;
} else {
rowCount = ((Number) resolveValue(limit.getRowCount(), false)).intValue();
}
int offset = limit.getOffset() != null ? ((Number) resolveValue(limit.getOffset(), false)).intValue() : 0;
scanLimits = new ScanLimitsImpl(rowCount, offset, rowCountJdbcParameter + 1);
} else if (top != null) {
if (top.isPercentage() || top.getExpression() == null) {
throw new StatementExecutionException("Invalid TOP clause (top=" + top + ")");
}
try {
int rowCount = Integer.parseInt(resolveValue(top.getExpression(), false) + "");
scanLimits = new ScanLimitsImpl(rowCount, 0);
} catch (NumberFormatException error) {
throw new StatementExecutionException("Invalid TOP clause: " + error, error);
}
}
if (maxRows > 0) {
if (scanLimits == null) {
scanLimits = new ScanLimitsImpl(maxRows, 0);
} else if (scanLimits.getMaxRows() <= 0 || scanLimits.getMaxRows() > maxRows) {
scanLimits = new ScanLimitsImpl(maxRows, scanLimits.getOffset());
}
}
ScanLimitsImpl limitsOnScan = null;
ScanLimitsImpl limitsOnPlan = null;
if (aggregator != null) {
limitsOnPlan = scanLimits;
} else {
limitsOnScan = scanLimits;
}
try {
ScanStatement statement = new ScanStatement(tableSpace, mainTable.tableName, mainTableProjection, where, comparatorOnScan, limitsOnScan);
return ExecutionPlan.make(statement, aggregator, limitsOnPlan, comparatorOnPlan);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
} else {
if (containsAggregateFunctions || (selectBody.getGroupByColumnReferences() != null && !selectBody.getGroupByColumnReferences().isEmpty())) {
throw new StatementExecutionException("AGGREGATEs are not yet supported with JOIN");
}
Limit limit = selectBody.getLimit();
Top top = selectBody.getTop();
if (limit != null && top != null) {
throw new StatementExecutionException("LIMIT and TOP cannot be used on the same query");
}
ScanLimitsImpl scanLimits = null;
if (limit != null) {
if (limit.isLimitAll() || limit.isLimitNull() || limit.getOffset() instanceof JdbcParameter) {
throw new StatementExecutionException("Invalid LIMIT clause (limit=" + limit + ")");
}
if (maxRows > 0 && limit.getRowCount() instanceof JdbcParameter) {
throw new StatementExecutionException("Invalid LIMIT clause (limit=" + limit + ") and JDBC setMaxRows=" + maxRows);
}
int rowCount;
int rowCountJdbcParameter = -1;
if (limit.getRowCount() instanceof JdbcParameter) {
rowCount = -1;
rowCountJdbcParameter = ((JdbcParameter) limit.getRowCount()).getIndex() - 1;
} else {
rowCount = ((Number) resolveValue(limit.getRowCount(), false)).intValue();
}
int offset = limit.getOffset() != null ? ((Number) resolveValue(limit.getOffset(), false)).intValue() : 0;
scanLimits = new ScanLimitsImpl(rowCount, offset, rowCountJdbcParameter + 1);
} else if (top != null) {
if (top.isPercentage() || top.getExpression() == null) {
throw new StatementExecutionException("Invalid TOP clause");
}
try {
int rowCount = Integer.parseInt(resolveValue(top.getExpression(), false) + "");
scanLimits = new ScanLimitsImpl(rowCount, 0);
} catch (NumberFormatException error) {
throw new StatementExecutionException("Invalid TOP clause: " + error, error);
}
}
if (maxRows > 0) {
if (scanLimits == null) {
scanLimits = new ScanLimitsImpl(maxRows, 0);
} else if (scanLimits.getMaxRows() <= 0 || scanLimits.getMaxRows() > maxRows) {
scanLimits = new ScanLimitsImpl(maxRows, scanLimits.getOffset());
}
}
List<ColumnReferencesDiscovery> conditionsOnJoinedResult = new ArrayList<>();
List<ScanStatement> scans = new ArrayList<>();
for (Map.Entry<String, JoinSupport> join : joins.entrySet()) {
String alias = join.getKey();
JoinSupport joinSupport = join.getValue();
Expression collectedConditionsForAlias = collectConditionsForAlias(alias, selectBody.getWhere(), conditionsOnJoinedResult, mainTableAlias);
LOG.severe("Collected WHERE for alias " + alias + ": " + collectedConditionsForAlias);
if (collectedConditionsForAlias == null) {
joinSupport.predicate = null;
} else {
joinSupport.predicate = new SQLRecordPredicate(join.getValue().table, alias, collectedConditionsForAlias);
}
}
for (Join join : selectBody.getJoins()) {
if (join.getOnExpression() != null) {
ColumnReferencesDiscovery discoverMainTableAliasForJoinCondition = discoverMainTableAlias(join.getOnExpression());
conditionsOnJoinedResult.add(discoverMainTableAliasForJoinCondition);
LOG.severe("Collected ON-condition on final JOIN result: " + join.getOnExpression());
}
}
for (ColumnReferencesDiscovery e : conditionsOnJoinedResult) {
LOG.severe("Collected WHERE on final JOIN result: " + e.getExpression());
for (Map.Entry<String, List<net.sf.jsqlparser.schema.Column>> entry : e.getColumnsByTable().entrySet()) {
String tableAlias = entry.getKey();
List<net.sf.jsqlparser.schema.Column> filteredColumnsOnJoin = entry.getValue();
LOG.severe("for TABLE " + tableAlias + " we need to load " + filteredColumnsOnJoin);
JoinSupport support = joins.get(tableAlias);
if (support == null) {
throw new StatementExecutionException("invalid table alias " + tableAlias);
}
if (!support.allColumns) {
for (net.sf.jsqlparser.schema.Column c : filteredColumnsOnJoin) {
support.selectItems.add(new SelectExpressionItem(c));
}
support.projection = new SQLProjection(support.table, support.tableRef.tableAlias, support.selectItems);
}
}
}
Map<String, Table> tables = new HashMap<>();
for (Map.Entry<String, JoinSupport> join : joins.entrySet()) {
JoinSupport joinSupport = join.getValue();
tables.put(join.getKey(), joinSupport.table);
ScanStatement statement = new ScanStatement(tableSpace, joinSupport.table.name, joinSupport.projection, joinSupport.predicate, null, null);
scans.add(statement);
}
TuplePredicate joinFilter = null;
if (!conditionsOnJoinedResult.isEmpty()) {
joinFilter = new SQLRecordPredicate(null, null, composeAndExpression(conditionsOnJoinedResult));
}
Projection joinProjection = null;
if (!allColumns) {
joinProjection = new SQLProjection(tableSpace, tables, selectBody.getSelectItems());
}
TupleComparator comparatorOnPlan = null;
if (selectBody.getOrderByElements() != null && !selectBody.getOrderByElements().isEmpty()) {
comparatorOnPlan = SingleColumnSQLTupleComparator.make(mainTableAlias, selectBody.getOrderByElements(), null);
}
try {
return ExecutionPlan.joinedScan(scans, joinFilter, joinProjection, scanLimits, comparatorOnPlan);
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
} else {
if (selectBody.getWhere() == null) {
throw new StatementExecutionException("unsupported GET without WHERE");
}
if (joinPresent) {
throw new StatementExecutionException("unsupported GET with JOIN");
}
// SELECT * FROM WHERE KEY=? AND ....
SQLRecordKeyFunction keyFunction = findPrimaryKeyIndexSeek(selectBody.getWhere(), table, mainTableAlias);
if (keyFunction == null || !keyFunction.isFullPrimaryKey()) {
throw new StatementExecutionException("unsupported GET not on PK, bad where clause: " + selectBody.getWhere() + " (" + selectBody.getWhere().getClass() + ")");
}
Predicate where = buildSimplePredicate(selectBody.getWhere(), table, mainTableAlias);
try {
return ExecutionPlan.simple(new GetStatement(tableSpace, mainTable.tableName, keyFunction, where, false));
} catch (IllegalArgumentException err) {
throw new StatementExecutionException(err);
}
}
}
use of net.sf.jsqlparser.statement.select.AllColumns in project spanner-jdbc by olavloite.
the class CloudSpannerResultSetMetaData method initColumns.
private void initColumns(Select select) {
columns = new ArrayList<>();
aliases = new ArrayList<>();
select.getSelectBody().accept(new SelectVisitorAdapter() {
@Override
public void visit(PlainSelect plainSelect) {
for (SelectItem selectItem : plainSelect.getSelectItems()) {
selectItem.accept(new SelectItemVisitor() {
private boolean foundColumn = false;
@Override
public void visit(SelectExpressionItem selectExpressionItem) {
selectExpressionItem.getExpression().accept(new ExpressionVisitorAdapter() {
@Override
public void visit(Column column) {
registerColumn(column, selectExpressionItem.getAlias());
foundColumn = true;
}
});
if (!foundColumn) {
registerColumn(null, selectExpressionItem.getAlias());
}
}
@Override
public void visit(AllTableColumns allTableColumns) {
registerAllTableColumns(allTableColumns.getTable());
}
@Override
public void visit(AllColumns allColumns) {
for (Table table : tables) {
registerAllTableColumns(table);
}
}
});
}
}
});
}
use of net.sf.jsqlparser.statement.select.AllColumns 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