Search in sources :

Example 1 with TableFilter

use of org.h2.table.TableFilter in project ignite by apache.

the class GridH2CollocationModel method childFilters.

/**
 * @param childFilters New child filters.
 * @return {@code true} If child filters were updated.
 */
private boolean childFilters(TableFilter[] childFilters) {
    assert childFilters != null;
    assert view;
    Select select = childFilters[0].getSelect();
    assert this.select == null || this.select == select;
    if (this.select == null) {
        this.select = select;
        assert this.childFilters == null;
    } else if (Arrays.equals(this.childFilters, childFilters))
        return false;
    if (this.childFilters == null) {
        // We have to clone because H2 reuses array and reorders elements.
        this.childFilters = childFilters.clone();
        children = new GridH2CollocationModel[childFilters.length];
    } else {
        assert this.childFilters.length == childFilters.length;
        // We have to copy because H2 reuses array and reorders elements.
        System.arraycopy(childFilters, 0, this.childFilters, 0, childFilters.length);
        Arrays.fill(children, null);
    }
    // Reset results.
    type = null;
    multiplier = 0;
    return true;
}
Also used : Select(org.h2.command.dml.Select)

Example 2 with TableFilter

use of org.h2.table.TableFilter in project ignite by apache.

the class GridH2CollocationModel method buildCollocationModel.

/**
 * @param qctx Query context.
 * @param info Sub-query info.
 * @param filters Filters.
 * @param filter Filter.
 * @param validate Query validation flag.
 * @return Collocation.
 */
public static GridH2CollocationModel buildCollocationModel(GridH2QueryContext qctx, SubQueryInfo info, TableFilter[] filters, int filter, boolean validate) {
    GridH2CollocationModel cm;
    if (info != null) {
        // Go up until we reach the root query.
        cm = buildCollocationModel(qctx, info.getUpper(), info.getFilters(), info.getFilter(), validate);
    } else {
        // We are at the root query.
        cm = qctx.queryCollocationModel();
        if (cm == null) {
            cm = createChildModel(null, -1, null, true, validate);
            qctx.queryCollocationModel(cm);
        }
    }
    assert cm.view;
    Select select = filters[0].getSelect();
    // For sub-queries we will drop collocation models, so that they will be recalculated anyways.
    if (cm.select != null && cm.select != select) {
        List<GridH2CollocationModel> unions = cm.getOrCreateUnions();
        // Start with 1 because at 0 it always will be c.
        for (int i = 1; i < unions.size(); i++) {
            GridH2CollocationModel u = unions.get(i);
            if (u.select == select) {
                cm = u;
                break;
            }
        }
        // Nothing was found, need to create new child in union.
        if (cm.select != select)
            cm = createChildModel(cm.upper, cm.filter, unions, true, validate);
    }
    cm.childFilters(filters);
    return cm.child(filter, true);
}
Also used : Select(org.h2.command.dml.Select)

Example 3 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class Parser method parseMerge.

private Prepared parseMerge() {
    Merge command = new Merge(session);
    currentPrepared = command;
    int start = lastParseIndex;
    read("INTO");
    List<String> excludeIdentifiers = Arrays.asList("USING", "KEY", "VALUES");
    TableFilter targetTableFilter = readSimpleTableFilter(0, excludeIdentifiers);
    command.setTargetTableFilter(targetTableFilter);
    Table table = command.getTargetTable();
    if (readIf("USING")) {
        return parseMergeUsing(command, start);
    }
    if (readIf("(")) {
        if (isSelect()) {
            command.setQuery(parseSelect());
            read(")");
            return command;
        }
        Column[] columns = parseColumnList(table);
        command.setColumns(columns);
    }
    if (readIf("KEY")) {
        read("(");
        Column[] keys = parseColumnList(table);
        command.setKeys(keys);
    }
    if (readIf("VALUES")) {
        do {
            ArrayList<Expression> values = New.arrayList();
            read("(");
            if (!readIf(")")) {
                do {
                    if (readIf("DEFAULT")) {
                        values.add(null);
                    } else {
                        values.add(readExpression());
                    }
                } while (readIfMore(false));
            }
            command.addRow(values.toArray(new Expression[0]));
        } while (readIf(","));
    } else {
        command.setQuery(parseSelect());
    }
    return command;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) Merge(org.h2.command.dml.Merge) TableFilter(org.h2.table.TableFilter) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Example 4 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class Parser method parseSelectSimpleFromPart.

private void parseSelectSimpleFromPart(Select command) {
    do {
        TableFilter filter = readTableFilter();
        parseJoinTableFilter(filter, command);
    } while (readIf(","));
    // to get the order as it was in the original query.
    if (session.isForceJoinOrder()) {
        sortTableFilters(command.getTopFilters());
    }
}
Also used : TableFilter(org.h2.table.TableFilter)

Example 5 with TableFilter

use of org.h2.table.TableFilter in project h2database by h2database.

the class Parser method parseMergeUsing.

private MergeUsing parseMergeUsing(Merge oldCommand, int start) {
    MergeUsing command = new MergeUsing(oldCommand);
    currentPrepared = command;
    if (readIf("(")) {
        /* a select query is supplied */
        if (isSelect()) {
            command.setQuery(parseSelect());
            read(")");
        }
        command.setQueryAlias(readFromAlias(null, Collections.singletonList("ON")));
        String[] querySQLOutput = { null };
        List<Column> columnTemplateList = TableView.createQueryColumnTemplateList(null, command.getQuery(), querySQLOutput);
        TableView temporarySourceTableView = createCTEView(command.getQueryAlias(), querySQLOutput[0], columnTemplateList, false, /* no recursion */
        false, /* do not add to session */
        false, /* isPersistent */
        session);
        TableFilter sourceTableFilter = new TableFilter(session, temporarySourceTableView, command.getQueryAlias(), rightsChecked, (Select) command.getQuery(), 0, null);
        command.setSourceTableFilter(sourceTableFilter);
    } else {
        /* Its a table name, simulate a query by building a select query for the table */
        List<String> excludeIdentifiers = Collections.singletonList("ON");
        TableFilter sourceTableFilter = readSimpleTableFilter(0, excludeIdentifiers);
        command.setSourceTableFilter(sourceTableFilter);
        StringBuilder buff = new StringBuilder("SELECT * FROM ");
        appendTableWithSchemaAndAlias(buff, sourceTableFilter.getTable(), sourceTableFilter.getTableAlias());
        Prepared preparedQuery = prepare(session, buff.toString(), null);
        command.setQuery((Select) preparedQuery);
    }
    read("ON");
    read("(");
    Expression condition = readExpression();
    command.setOnCondition(condition);
    read(")");
    if (readIfAll("WHEN", "MATCHED", "THEN")) {
        int startMatched = lastParseIndex;
        if (readIf("UPDATE")) {
            Update updateCommand = new Update(session);
            // currentPrepared = updateCommand;
            TableFilter filter = command.getTargetTableFilter();
            updateCommand.setTableFilter(filter);
            parseUpdateSetClause(updateCommand, filter, startMatched);
            command.setUpdateCommand(updateCommand);
        }
        startMatched = lastParseIndex;
        if (readIf("DELETE")) {
            Delete deleteCommand = new Delete(session);
            TableFilter filter = command.getTargetTableFilter();
            deleteCommand.setTableFilter(filter);
            parseDeleteGivenTable(deleteCommand, null, startMatched);
            command.setDeleteCommand(deleteCommand);
        }
    }
    if (readIfAll("WHEN", "NOT", "MATCHED", "THEN")) {
        if (readIf("INSERT")) {
            Insert insertCommand = new Insert(session);
            insertCommand.setTable(command.getTargetTable());
            parseInsertGivenTable(insertCommand, command.getTargetTable());
            command.setInsertCommand(insertCommand);
        }
    }
    setSQL(command, "MERGE", start);
    // build and prepare the targetMatchQuery ready to test each rows
    // existence in the target table (using source row to match)
    StringBuilder targetMatchQuerySQL = new StringBuilder("SELECT _ROWID_ FROM ");
    appendTableWithSchemaAndAlias(targetMatchQuerySQL, command.getTargetTable(), command.getTargetTableFilter().getTableAlias());
    targetMatchQuerySQL.append(" WHERE ").append(command.getOnCondition().getSQL());
    command.setTargetMatchQuery((Select) parse(targetMatchQuerySQL.toString()));
    return command;
}
Also used : Delete(org.h2.command.dml.Delete) ValueString(org.h2.value.ValueString) Update(org.h2.command.dml.Update) Insert(org.h2.command.dml.Insert) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) MergeUsing(org.h2.command.dml.MergeUsing) TableView(org.h2.table.TableView)

Aggregations

TableFilter (org.h2.table.TableFilter)39 IndexColumn (org.h2.table.IndexColumn)21 Column (org.h2.table.Column)20 Expression (org.h2.expression.Expression)18 ValueExpression (org.h2.expression.ValueExpression)14 ExpressionColumn (org.h2.expression.ExpressionColumn)12 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)11 Table (org.h2.table.Table)11 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)9 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)9 Select (org.h2.command.dml.Select)9 ValueString (org.h2.value.ValueString)9 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)6 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)6 CreateTable (org.h2.command.ddl.CreateTable)6 DropTable (org.h2.command.ddl.DropTable)6 Query (org.h2.command.dml.Query)6 IndexCondition (org.h2.index.IndexCondition)6 ArrayList (java.util.ArrayList)5 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)5